-
Notifications
You must be signed in to change notification settings - Fork 3
/
xr_usb_serial_common.c
1596 lines (1333 loc) · 58 KB
/
xr_usb_serial_common.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
* Copyright (c) 2015 Exar Corporation, Inc.
*
* This driver will work with any USB UART function in these Exar devices:
* XR21V1410/1412/1414
* XR21B1411
* XR21B1420/1422/1424
* XR22801/802/804
*
* The driver has been tested on various kernel versions from 2.6.18 to 3.4.x.
* There is a different driver available from www.exar.com for kernel versions 3.6.x and newer.
*
* ChangeLog:
* Version 1A - Initial released version.
*/
#define DRIVER_VERSION "1A"
#define DRIVER_AUTHOR "<[email protected]>"
#define DRIVER_DESC "Exar USB UART (serial port) driver"
#undef XR21B142X_IWA
#include <linux/kernel.h>
#include <linux/jiffies.h>
#include <linux/errno.h>
#include <linux/tty.h>
#include <linux/tty_flip.h>
#include <linux/module.h>
#include <linux/usb.h>
#include <linux/usb/serial.h>
#include <linux/serial.h>
#include <linux/slab.h>
#include <asm/unaligned.h>
#include <asm/uaccess.h>
#include <linux/usb/cdc.h>
#ifndef CDC_DATA_INTERFACE_TYPE
#define CDC_DATA_INTERFACE_TYPE 0x0a
#endif
#ifndef USB_RT_ACM
#define USB_RT_ACM (USB_TYPE_CLASS | USB_RECIP_INTERFACE)
#define ACM_CTRL_DTR 0x01
#define ACM_CTRL_RTS 0x02
#define ACM_CTRL_DCD 0x01
#define ACM_CTRL_DSR 0x02
#define ACM_CTRL_BRK 0x04
#define ACM_CTRL_RI 0x08
#define ACM_CTRL_FRAMING 0x10
#define ACM_CTRL_PARITY 0x20
#define ACM_CTRL_OVERRUN 0x40
#endif
#include "linux/version.h"
#include "xr_usb_serial_common.h"
#define N_IN_URB 4
#define N_OUT_URB 12
#define IN_BUFLEN 4096
static int debug = 0;
/* -------------------------------------------------------------------------- */
#if defined(RHEL_RELEASE_CODE)
#if RHEL_RELEASE_CODE < RHEL_RELEASE_VERSION(5, 2)
#define true 1
static inline int usb_endpoint_dir_in(const struct usb_endpoint_descriptor *epd)
{
return ((epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN);
}
static inline int usb_endpoint_dir_out(const struct usb_endpoint_descriptor *epd)
{
return ((epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT);
}
static inline int usb_endpoint_xfer_bulk(const struct usb_endpoint_descriptor *epd)
{
return ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
USB_ENDPOINT_XFER_BULK);
}
static inline int usb_endpoint_is_bulk_in(const struct usb_endpoint_descriptor *epd)
{
return (usb_endpoint_xfer_bulk(epd) && usb_endpoint_dir_in(epd));
}
static inline int usb_endpoint_is_bulk_out(const struct usb_endpoint_descriptor *epd)
{
return (usb_endpoint_xfer_bulk(epd) && usb_endpoint_dir_out(epd));
}
#endif
#endif
/* -------------------------------------------------------------------------- */
#include "xr_usbserial_ioctl.h"
/* -------------------------------------------------------------------------- */
static struct usb_device_id id_table [] = {
{ USB_DEVICE(0x04e2, 0x1410)},
{ USB_DEVICE(0x04e2, 0x1411)},
{ USB_DEVICE(0x04e2, 0x1412)},
{ USB_DEVICE(0x04e2, 0x1414)},
{ USB_DEVICE(0x04e2, 0x1420)},
{ USB_DEVICE(0x04e2, 0x1422)},
{ USB_DEVICE(0x04e2, 0x1424)},
{ USB_DEVICE(0x04e2, 0x1400)},
{ USB_DEVICE(0x04e2, 0x1401)},
{ USB_DEVICE(0x04e2, 0x1402)},
{ USB_DEVICE(0x04e2, 0x1403)},
{ }
};
MODULE_DEVICE_TABLE(usb, id_table);
static void xr_usb_serial_disconnect(struct usb_interface *interface);
static struct usb_driver xr_usb_serial_driver = {
.name = "xr_usb_serial",
.probe = usb_serial_probe,
.disconnect = xr_usb_serial_disconnect,
.id_table = id_table,
.no_dynamic_id = 1,
};
/* -------------------------------------------------------------------------- */
struct xr_usb_serial_serial_private
{
struct usb_interface *data_interface;
unsigned short DeviceVendor;
unsigned short DeviceProduct;
};
struct xr_usb_serial_port_private {
spinlock_t lock; /* lock the structure */
int outstanding_urbs; /* number of out urbs in flight */
struct urb *in_urbs[N_IN_URB];
char *in_buffer[N_IN_URB];
int ctrlin;
int ctrlout;
int clocal;
int block;
struct reg_addr_map reg_map;
struct usb_cdc_line_coding line_coding;
int preciseflags; /* USB: wide mode, TTY: flags per character */
int trans9; /* USB: wide mode, serial 9N1 */
unsigned int baud_base; /* setserial: used to hack in non-standard baud rates */
int have_extra_byte;
int extra_byte;
int bcd_device;
unsigned int throttled:1; /* actually throttled */
unsigned int throttle_req:1; /* throttle requested */
#ifdef XR21B142X_IWA
int iwa;
#endif
unsigned short DeviceVendor;
unsigned short DeviceProduct;
};
/* -------------------------------------------------------------------------- */
static int xr_usb_serial_rev_a(struct usb_serial_port *port)
{
struct xr_usb_serial_port_private *portdata = usb_get_serial_port_data(port);
return portdata->bcd_device == 0;
}
static int xr_usb_serial_ctrl_msg(struct usb_serial_port *port,
int request, int value, void *buf, int len)
{
struct usb_serial *serial = port->serial;
int retval = usb_control_msg(serial->dev,
usb_sndctrlpipe(serial->dev, 0),
request,
USB_RT_ACM,
value,
serial->interface->cur_altsetting->desc.bInterfaceNumber,
buf,
len,
5000);
if (debug)
dev_dbg(&port->serial->dev->dev, "xr_usb_serial_control_msg: rq: 0x%02x val: %#x len: %#x result: %d\n", request, value, len, retval);
return retval < 0 ? retval : 0;
}
#include "xr_usb_serial_hal.c"
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19)
static void xr_usb_serial_set_termios(struct usb_serial_port *port,
struct termios *old_termios)
#elif LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 27)
static void xr_usb_serial_set_termios(struct usb_serial_port *port,
struct ktermios *old_termios)
#else
static void xr_usb_serial_set_termios(struct tty_struct *tty_param,
struct usb_serial_port *port,
struct ktermios *old_termios)
#endif
{
struct xr_usb_serial_port_private *portdata = usb_get_serial_port_data(port);
unsigned int cflag, block;
unsigned int data_bits, parity_type, char_format;
int result;
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 27)
struct tty_struct *tty = port->tty;
#else
struct tty_struct *tty = port->port.tty;
#endif
if (debug) dev_dbg(&port->serial->dev->dev, "%s\n", __func__);
/* mutex_lock(&config_mutex); */
cflag = tty->termios->c_cflag;
portdata->clocal = ((cflag & CLOCAL) != 0);
block = portdata->block;
xr_usb_serial_disable(port);
if ((cflag & CSIZE) == CS7) {
data_bits = 7;
} else if ((cflag & CSIZE) == CS5) {
/* Enabling 5-bit mode is really 9-bit mode! */
data_bits = 9;
} else {
data_bits = 8;
}
portdata->trans9 = (data_bits == 9);
if (cflag & PARENB) {
if (cflag & PARODD) {
if (cflag & CMSPAR) {
parity_type = USB_CDC_MARK_PARITY;
} else {
parity_type = USB_CDC_ODD_PARITY;
}
} else {
if (cflag & CMSPAR) {
parity_type = USB_CDC_SPACE_PARITY;
} else {
parity_type = USB_CDC_EVEN_PARITY;
}
}
} else {
parity_type = USB_CDC_NO_PARITY;
}
if (cflag & CSTOPB) {
char_format = USB_CDC_2_STOP_BITS;
} else {
char_format = USB_CDC_1_STOP_BITS;
}
#ifdef XR21B142X_IWA
if (data_bits == UART_FORMAT_SIZE_8)
{
portdata->iwa = parity_type;
if (portdata->iwa != USB_CDC_NO_PARITY)
{
data_bits = 9;
parity_type = USB_CDC_NO_PARITY;
}
}
else
{
portdata->iwa = USB_CDC_NO_PARITY;
}
#endif
xr_usb_serial_set_flow_mode(port,tty,cflag);
if (portdata->trans9)
{
/* Turn on wide mode if we're 9-bit transparent. */
xr_usb_serial_set_wide_mode(port,1);
#ifdef XR21B142X_IWA
}
else if (portdata->iwa != USB_CDC_NO_PARITY)
{
xr_usb_serial_set_wide_mode(port,1);
#endif
}
else if (!portdata->preciseflags)
{
xr_usb_serial_set_wide_mode(port,0);
}
portdata->line_coding.dwDTERate = cpu_to_le32(tty_get_baud_rate(tty));
portdata->line_coding.bCharFormat = char_format;
portdata->line_coding.bParityType = parity_type;
portdata->line_coding.bDataBits = data_bits;
if (debug)
dev_dbg(&port->serial->dev->dev, "%s: line coding: rate=%d format=%d parity=%d bits=%d\n", __func__,
cpu_to_le32(tty_get_baud_rate(tty)),
char_format,
parity_type,
data_bits);
result = xr_usb_serial_set_line(port, &portdata->line_coding);
if (result < 0) {
dev_dbg(&port->serial->dev->dev, "%s: cannot set line coding: rate=%d format=%d parity=%d bits=%d\n", __func__,
tty_get_baud_rate(tty),
portdata->line_coding.bCharFormat,
portdata->line_coding.bParityType,
portdata->line_coding.bDataBits);
}
xr_usb_serial_enable(port);
/* mutex_unlock(&config_mutex); */
}
#define UART_TX_BREAK 0x0A
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 27)
static void xr_usb_serial_break_ctl(struct usb_serial_port *port, int break_state)
#else
static void xr_usb_serial_break_ctl(struct tty_struct *tty, int break_state)
#endif
{
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 27)
#else
struct usb_serial_port *port = tty->driver_data;
#endif
if (debug) dev_dbg(&port->serial->dev->dev, "BREAK %d\n", break_state);
xr_usb_serial_send_break(port,break_state);
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 27)
static int xr_usb_serial_tiocmget(struct usb_serial_port *port, struct file *file)
#elif LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 39)
static int xr_usb_serial_tiocmget(struct tty_struct *tty, struct file *file)
#else
static int xr_usb_serial_tiocmget(struct tty_struct *tty)
#endif
{
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 27)
#else
struct usb_serial_port *port = tty->driver_data;
#endif
short data;
int result;
result = xr_usb_serial_get_gpio_status(port,&data);
if (result)
return ((data & 0x8) ? 0: TIOCM_DTR) | ((data & 0x20) ? 0:TIOCM_RTS ) | ((data & 0x4) ? 0:TIOCM_DSR) | ((data & 0x1) ? 0 : TIOCM_RI) | ((data & 0x2) ? 0:TIOCM_CD) | ((data & 0x10) ? 0 : TIOCM_CTS);
else
return -EFAULT;
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 27)
static int xr_usb_serial_tiocmset(struct usb_serial_port *port, struct file *file,
#elif LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 39)
static int xr_usb_serial_tiocmset(struct tty_struct *tty, struct file *file,
#else
static int xr_usb_serial_tiocmset(struct tty_struct *tty,
#endif
unsigned int set, unsigned int clear)
{
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 27)
#else
struct usb_serial_port *port = tty->driver_data;
#endif
struct xr_usb_serial_port_private *portdata = usb_get_serial_port_data(port);
unsigned int newctrl;
newctrl = portdata->ctrlout;
set = (set & TIOCM_DTR ? ACM_CTRL_DTR : 0) | (set & TIOCM_RTS ? ACM_CTRL_RTS : 0);
clear = (clear & TIOCM_DTR ? ACM_CTRL_DTR : 0) | (clear & TIOCM_RTS ? ACM_CTRL_RTS : 0);
newctrl = (newctrl & ~clear) | set;
if (portdata->ctrlout == newctrl)
return 0;
portdata->ctrlout = newctrl;
if (newctrl & ACM_CTRL_DTR)
xr_usb_serial_ctrl_dtr(port,0);
else
xr_usb_serial_ctrl_dtr(port,1);
if (newctrl & ACM_CTRL_RTS)
xr_usb_serial_ctrl_rts(port,0);
else
xr_usb_serial_ctrl_rts(port,1);
return 0;
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 27)
static int xr_usb_serial_ioctl(struct usb_serial_port *port, struct file *file, unsigned int cmd, unsigned long arg)
#elif LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 39)
static int xr_usb_serial_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
#else
static int xr_usb_serial_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
#endif
{
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 27)
#else
struct usb_serial_port *port = tty->driver_data;
#endif
struct xr_usb_serial_port_private *portdata = usb_get_serial_port_data(port);
unsigned int block, reg, val, match, preciseflags, unicast, broadcast, flow, selector;
short *data;
int result;
struct serial_struct ss;
if (debug) dev_dbg(&port->serial->dev->dev, "%s %08x\n", __func__, cmd);
switch (cmd) {
case TIOCGSERIAL:
if (!arg)
return -EFAULT;
memset(&ss, 0, sizeof(ss));
ss.baud_base = portdata->baud_base;
if (copy_to_user((void __user *)arg, &ss, sizeof(ss)))
return -EFAULT;
break;
case TIOCSSERIAL:
if (!arg)
return -EFAULT;
if (copy_from_user(&ss, (void __user *)arg, sizeof(ss)))
return -EFAULT;
portdata->baud_base = ss.baud_base;
portdata->line_coding.dwDTERate = cpu_to_le32(ss.baud_base);
if (debug) dev_dbg(&port->serial->dev->dev, "baud_base=%d\n", portdata->baud_base);
/* mutex_lock(&config_mutex); */
xr_usb_serial_disable(port);
if (debug)
dev_dbg(&port->serial->dev->dev, "%s: line coding: rate=%d format=%d parity=%d bits=%d\n", __func__,
ss.baud_base,
portdata->line_coding.bCharFormat,
portdata->line_coding.bParityType,
portdata->line_coding.bDataBits);
result = xr_usb_serial_set_line(port, &portdata->line_coding);
if (result < 0) {
//dev_dbg(&port->dev, "%d != %ld\n", result, sizeof(portdata->line_coding));
if (debug)
dev_dbg(&port->serial->dev->dev, "%s: cannot set line coding: rate=%d format=%d parity=%d bits=%d\n", __func__,
ss.baud_base,
portdata->line_coding.bCharFormat,
portdata->line_coding.bParityType,
portdata->line_coding.bDataBits);
return -EINVAL;
}
xr_usb_serial_enable(port);
/* mutex_unlock(&config_mutex); */
break;
case XRIOC_GET_REG:
if (get_user(block, (int __user *)arg))
return -EFAULT;
if (get_user(reg, (int __user *)(arg + sizeof(int))))
return -EFAULT;
data = kmalloc(2, GFP_KERNEL);
if (data == NULL) {
dev_err(&port->serial->dev->dev, "%s - Cannot allocate USB buffer.\n", __func__);
return -ENOMEM;
}
if (block == -1)
{
result = xr_usb_serial_get_reg(port,reg, data);
}
else
{
result = xr_usb_serial_get_reg_ext(port,block,reg,data);
}
if (result < 0 ) {
dev_err(&port->serial->dev->dev, "Cannot get register (%d) block=%d \n", result,block);
kfree(data);
return -EFAULT;
}
if (debug)
dev_dbg(&port->serial->dev->dev,"xr_usb_serial_get_reg<%d> value =%x\n",reg,*data);
if (put_user(data[0], (int __user *)(arg + 2 * sizeof(int)))) {
dev_err(&port->serial->dev->dev, "Cannot put user result\n");
kfree(data);
return -EFAULT;
}
kfree(data);
break;
case XRIOC_SET_REG:
if (get_user(block, (int __user *)arg))
return -EFAULT;
if (get_user(reg, (int __user *)(arg + sizeof(int))))
return -EFAULT;
if (get_user(val, (int __user *)(arg + 2 * sizeof(int))))
return -EFAULT;
if (block == -1)
{
result = xr_usb_serial_set_reg(port,reg, val);
}
else
{
result = xr_usb_serial_set_reg_ext(port,block,reg, val);
}
if (result < 0)
return -EFAULT;
break;
case XRIOC_SET_ADDRESS_MATCH:
match = arg;
if (debug) dev_dbg(&port->serial->dev->dev, "%s VIOC_SET_ADDRESS_MATCH %d\n", __func__, match);
xr_usb_serial_disable(port);
if (match & XR_ADDRESS_MATCH_DISABLE) {
flow = UART_FLOW_MODE_NONE;
} else {
flow = UART_FLOW_MODE_ADDR_MATCH_TX;
unicast = (match >> XR_ADDRESS_UNICAST_S) & 0xff;
broadcast = (match >> XR_ADDRESS_BROADCAST_S) & 0xff;
}
if (debug) dev_dbg(&port->dev, "address match: flow=%d ucast=%d bcast=%u\n",
flow, unicast, broadcast);
xr_usb_serial_enable(port);
break;
case XRIOC_SET_PRECISE_FLAGS:
preciseflags = arg;
if (debug) dev_dbg(&port->serial->dev->dev, "%s VIOC_SET_PRECISE_FLAGS %d\n", __func__, preciseflags);
xr_usb_serial_disable(port);
if (preciseflags) {
portdata->preciseflags = 1;
} else {
portdata->preciseflags = 0;
}
xr_usb_serial_set_wide_mode(port,portdata->preciseflags);
xr_usb_serial_enable(port);
break;
case XRIOC_LOOPBACK:
selector = arg;
dev_dbg(&port->serial->dev->dev, "VIOC_LOOPBACK 0x%02x\n", selector);
xr_usb_serial_set_loopback(port);
break;
default:
return -ENOIOCTLCMD;
}
return 0;
}
/* -------------------------------------------------------------------------- */
#ifdef XR21B142X_IWA
static const int xr_usb_serial_parity[] =
{
0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0
};
#endif
#if LINUX_VERSION_CODE == KERNEL_VERSION(2, 6, 18)
static void xr_usb_serial_out_callback(struct urb *urb, struct pt_regs *regs)
#else
static void xr_usb_serial_out_callback(struct urb *urb)
#endif
{
struct usb_serial_port *port = urb->context;
struct xr_usb_serial_port_private *portdata = usb_get_serial_port_data(port);
int status = urb->status;
unsigned long flags;
if (debug) dev_dbg(&port->serial->dev->dev, "%s - port %d\n", __func__, port->number);
/* free up the transfer buffer, as usb_free_urb() does not do this */
kfree(urb->transfer_buffer);
if (status)
if (debug) dev_dbg(&port->serial->dev->dev, "%s - nonzero write bulk status received: %d\n",
__func__, status);
spin_lock_irqsave(&portdata->lock, flags);
--portdata->outstanding_urbs;
spin_unlock_irqrestore(&portdata->lock, flags);
usb_serial_port_softint(port);
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 27)
static int xr_usb_serial_write_room(struct usb_serial_port *port)
#else
static int xr_usb_serial_write_room(struct tty_struct *tty)
#endif
{
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 27)
#else
struct usb_serial_port *port = tty->driver_data;
#endif
struct xr_usb_serial_port_private *portdata = usb_get_serial_port_data(port);
unsigned long flags;
if (debug)
dev_dbg(&port->dev, "%s - port %d\n", __func__, port->number);
/* try to give a good number back based on if we have any free urbs at
* this point in time */
spin_lock_irqsave(&portdata->lock, flags);
if (portdata->outstanding_urbs > N_OUT_URB * 2 / 3)
{
spin_unlock_irqrestore(&portdata->lock, flags);
if (debug)
dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
return 0;
}
spin_unlock_irqrestore(&portdata->lock, flags);
return 2048;
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 27)
static int xr_usb_serial_write(struct usb_serial_port *port,
#else
static int xr_usb_serial_write(struct tty_struct *tty, struct usb_serial_port *port,
#endif
const unsigned char *buf, int count)
{
struct xr_usb_serial_port_private *portdata = usb_get_serial_port_data(port);
struct usb_serial *serial = port->serial;
int bufsize = count;
unsigned long flags;
unsigned char *buffer;
struct urb *urb;
int status;
portdata = usb_get_serial_port_data(port);
if (debug) dev_dbg(&port->dev, "%s: write (%d chars)\n", __func__, count);
spin_lock_irqsave(&portdata->lock, flags);
if (portdata->outstanding_urbs > N_OUT_URB) {
spin_unlock_irqrestore(&portdata->lock, flags);
if (debug) dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
return 0;
}
portdata->outstanding_urbs++;
spin_unlock_irqrestore(&portdata->lock, flags);
#ifdef XR21B142X_IWA
if (portdata->iwa != UART_FORMAT_PARITY_NONE)
bufsize = count * 2;
#endif
buffer = kmalloc(bufsize, GFP_ATOMIC);
if (!buffer) {
dev_err(&port->dev, "out of memory\n");
count = -ENOMEM;
goto error_no_buffer;
}
urb = usb_alloc_urb(0, GFP_ATOMIC);
if (!urb) {
dev_err(&port->dev, "no more free urbs\n");
count = -ENOMEM;
goto error_no_urb;
}
#ifdef XR21B142X_IWA
if (portdata->iwa != UART_FORMAT_PARITY_NONE) {
int i;
char *b = buffer;
for (i = 0; i < count; ++i) {
int c, p = 0;
c = buf[i];
switch (portdata->iwa) {
case UART_FORMAT_PARITY_ODD: p = !xr_usb_serial_parity[c]; break;
case UART_FORMAT_PARITY_EVEN: p = xr_usb_serial_parity[c]; break;
case UART_FORMAT_PARITY_1: p = 1; break;
case UART_FORMAT_PARITY_0: p = 0; break;
}
*b++ = c;
*b++ = p;
}
} else
#endif
memcpy(buffer, buf, count);
/* usb_serial_debug_data(debug, &port->dev, __func__, bufsize, buffer); */
usb_fill_bulk_urb(urb, serial->dev,
usb_sndbulkpipe(serial->dev,
port->bulk_out_endpointAddress),
buffer, bufsize, xr_usb_serial_out_callback, port);
/* send it down the pipe */
status = usb_submit_urb(urb, GFP_ATOMIC);
if (status) {
dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed "
"with status = %d\n", __func__, status);
count = status;
goto error;
}
/* we are done with this urb, so let the host driver
* really free it when it is finished with it */
usb_free_urb(urb);
return count;
error:
usb_free_urb(urb);
error_no_urb:
kfree(buffer);
error_no_buffer:
spin_lock_irqsave(&portdata->lock, flags);
--portdata->outstanding_urbs;
spin_unlock_irqrestore(&portdata->lock, flags);
return count;
}
/* -------------------------------------------------------------------------- */
#if LINUX_VERSION_CODE == KERNEL_VERSION(2, 6, 18)
static void xr_usb_serial_in_callback(struct urb *urb, struct pt_regs *regs)
#else
static void xr_usb_serial_in_callback(struct urb *urb)
#endif
{
int endpoint = usb_pipeendpoint(urb->pipe);
struct usb_serial_port *port = urb->context;
struct xr_usb_serial_port_private *portdata = usb_get_serial_port_data(port);
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 27)
struct tty_struct *tty = port->tty;
#else
struct tty_struct *tty = port->port.tty;
#endif
int preciseflags = portdata->preciseflags;
char *transfer_buffer = urb->transfer_buffer;
int length, room, have_extra_byte;
int err;
unsigned long flags;
if (debug) dev_dbg(&port->dev, "%s: %p\n", __func__, urb);
if (urb->status) {
if (debug) dev_dbg(&port->dev, "%s: nonzero status: %d on endpoint %02x.\n",
__func__, urb->status, endpoint);
return;
}
#ifdef XR21B142X_IWA
if (portdata->iwa != UART_FORMAT_PARITY_NONE) {
preciseflags = true;
}
#endif
length = urb->actual_length;
if (length == 0) {
if (debug) dev_dbg(&port->dev, "%s: empty read urb received\n", __func__);
err = usb_submit_urb(urb, GFP_ATOMIC);
if (err)
dev_err(&port->dev, "resubmit read urb failed. (%d)\n", err);
return;
}
length = length + (portdata->have_extra_byte ? 1 : 0);
have_extra_byte = (preciseflags && (length & 1));
length = (preciseflags) ? (length / 2) : length;
room = tty_buffer_request_room(tty, length);
if (room != length) {
if (debug) dev_dbg(&port->dev, "Not enough room in TTY buf, dropped %d chars.\n", length - room);
}
if (room) {
if (preciseflags) {
char *dp = transfer_buffer;
int i, ch, ch_flags;
for (i = 0; i < room; ++i) {
char tty_flag;
if (i == 0) {
if (portdata->have_extra_byte) {
ch = portdata->extra_byte;
} else {
ch = *dp++;
}
} else {
ch = *dp++;
}
ch_flags = *dp++;
#ifdef XR21B142X_IWA
{
int p;
switch (portdata->iwa) {
case UART_FORMAT_PARITY_ODD: p = !xr_usb_serial_parity[ch]; break;
case UART_FORMAT_PARITY_EVEN: p = xr_usb_serial_parity[ch]; break;
case UART_FORMAT_PARITY_1: p = 1; break;
case UART_FORMAT_PARITY_0: p = 0; break;
default: p = 0; break;
}
ch_flags ^= p;
}
#endif
if (ch_flags & RAMCTL_BUFFER_PARITY)
tty_flag = TTY_PARITY;
else if (ch_flags & RAMCTL_BUFFER_BREAK)
tty_flag = TTY_BREAK;
else if (ch_flags & RAMCTL_BUFFER_FRAME)
tty_flag = TTY_FRAME;
else if (ch_flags & RAMCTL_BUFFER_OVERRUN)
tty_flag = TTY_OVERRUN;
else
tty_flag = TTY_NORMAL;
tty_insert_flip_char(tty, ch, tty_flag);
}
} else {
tty_insert_flip_string(tty, transfer_buffer, room);
}
tty_flip_buffer_push(tty);
}
portdata->have_extra_byte = have_extra_byte;
if (have_extra_byte) {
portdata->extra_byte = transfer_buffer[urb->actual_length - 1];
}
spin_lock_irqsave(&portdata->lock, flags);
portdata->throttled = portdata->throttle_req;
spin_unlock_irqrestore(&portdata->lock, flags);
if(!(portdata->throttled))
{
err = usb_submit_urb(urb, GFP_ATOMIC);
if (err)
dev_err(&port->dev, "resubmit read urb failed. (%d)\n", err);
}
}
/* -------------------------------------------------------------------------- */
#if LINUX_VERSION_CODE == KERNEL_VERSION(2, 6, 18)
static void xr_usb_serial_int_callback(struct urb *urb, struct pt_regs *regs)
#else
static void xr_usb_serial_int_callback(struct urb *urb)
#endif
{
struct usb_serial_port *port = urb->context;
struct xr_usb_serial_port_private *portdata = usb_get_serial_port_data(port);
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 27)
struct tty_struct *tty = port->tty;
#else
struct tty_struct *tty = port->port.tty;
#endif
struct usb_cdc_notification *dr = urb->transfer_buffer;
unsigned char *data;
int newctrl;
int status;
switch (urb->status) {
case 0:
/* success */
break;
case -ECONNRESET:
case -ENOENT:
case -ESHUTDOWN:
/* this urb is terminated, clean up */
if (debug) dev_dbg(&port->dev, "urb shutting down with status: %d\n", urb->status);
return;
default:
if (debug) dev_dbg(&port->dev, "nonzero urb status received: %d\n", urb->status);
goto exit;
}
data = (unsigned char *)(dr + 1);
switch (dr->bNotificationType) {
case USB_CDC_NOTIFY_NETWORK_CONNECTION:
if (debug) dev_dbg(&port->dev, "%s network\n", dr->wValue ? "connected to" : "disconnected from");
break;
case USB_CDC_NOTIFY_SERIAL_STATE:
newctrl = le16_to_cpu(get_unaligned((__le16 *)data));
if (!portdata->clocal && (portdata->ctrlin & ~newctrl & ACM_CTRL_DCD)) {
if (debug) dev_dbg(&port->dev, "calling hangup\n");
tty_hangup(tty);
}
portdata->ctrlin = newctrl;
if (debug) dev_dbg(&port->dev, "input control lines: dcd%c dsr%c break%c ring%c framing%c parity%c overrun%c\n",
portdata->ctrlin & ACM_CTRL_DCD ? '+' : '-',
portdata->ctrlin & ACM_CTRL_DSR ? '+' : '-',
portdata->ctrlin & ACM_CTRL_BRK ? '+' : '-',
portdata->ctrlin & ACM_CTRL_RI ? '+' : '-',
portdata->ctrlin & ACM_CTRL_FRAMING ? '+' : '-',
portdata->ctrlin & ACM_CTRL_PARITY ? '+' : '-',
portdata->ctrlin & ACM_CTRL_OVERRUN ? '+' : '-');
break;
default:
if (debug) dev_dbg(&port->dev, "unknown notification %d received: index %d len %d data0 %d data1 %d\n",
dr->bNotificationType, dr->wIndex,
dr->wLength, data[0], data[1]);
break;
}
exit:
if (debug) dev_dbg(&port->dev, "Resubmitting interrupt IN urb %p\n", urb);
status = usb_submit_urb(urb, GFP_ATOMIC);
if (status)
dev_err(&port->dev, "usb_submit_urb failed with result %d", status);
}
/* -------------------------------------------------------------------------- */
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 27)
static int xr_usb_serial_open(struct usb_serial_port *port, struct file *filp)
#elif LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 32)
static int xr_usb_serial_open(struct tty_struct *tty_param,
struct usb_serial_port *port, struct file *filp)
#else
static int xr_usb_serial_open(struct tty_struct *tty_param, struct usb_serial_port *port)
#endif
{
struct xr_usb_serial_port_private *portdata;
struct usb_serial *serial = port->serial;
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 27)
struct tty_struct *tty = port->tty;