-
Notifications
You must be signed in to change notification settings - Fork 0
/
xr_usb_serial_common.c
2086 lines (1823 loc) · 59.7 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
*/
/*
*
* 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 3.6.x to 5.11.x.
* This driver may work on newer versions as well. There is a different driver available
* from www.exar.com that will work with kernel versions 2.6.18 to 3.4.x.
*
* ChangeLog:
* Version 1B - Initial released version.
* Version 1C - Add 9-bit mode support
* Version 1D - GPIO support & Fixes. Check Readme for details.
*/
//#undef DEBUG
#undef VERBOSE_DEBUG
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/tty.h>
#include <linux/serial.h>
#include <linux/tty_driver.h>
#include <linux/tty_flip.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/uaccess.h>
#include <linux/usb.h>
#include <linux/usb/cdc.h>
#include <asm/byteorder.h>
#include <asm/unaligned.h>
#include <linux/list.h>
#include "linux/version.h"
#include <asm/io.h>
#include <linux/gpio.h>
#include <linux/idr.h>
#include <linux/delay.h>
#ifdef CONFIG_COMPAT
#include <linux/compat.h>
#endif
#include "xr_usb_serial_common.h"
#include "xr_usb_serial_ioctl.h"
#define DRIVER_AUTHOR "<uarttechsupport@exar.com>"
#define DRIVER_DESC "Exar/MxL USB UART (serial port) driver version 1D"
static struct usb_driver xr_usb_serial_driver;
static struct tty_driver *xr_usb_serial_tty_driver;
static struct xr_usb_serial *xr_usb_serial_table[XR_USB_SERIAL_TTY_MINORS];
static DEFINE_MUTEX(xr_usb_serial_table_lock);
/*
* xr_usb_serial_table accessors
*/
/*
* Look up an XR_USB_SERIAL structure by index. If found and not disconnected, increment
* its refcount and return it with its mutex held.
*/
static struct xr_usb_serial *xr_usb_serial_get_by_index(unsigned index)
{
struct xr_usb_serial *xr_usb_serial;
mutex_lock(&xr_usb_serial_table_lock);
xr_usb_serial = xr_usb_serial_table[index];
if (xr_usb_serial) {
mutex_lock(&xr_usb_serial->mutex);
if (xr_usb_serial->disconnected) {
mutex_unlock(&xr_usb_serial->mutex);
xr_usb_serial = NULL;
} else {
tty_port_get(&xr_usb_serial->port);
mutex_unlock(&xr_usb_serial->mutex);
}
}
mutex_unlock(&xr_usb_serial_table_lock);
return xr_usb_serial;
}
/*
* Try to find an available minor number and if found, associate it with 'xr_usb_serial'.
*/
static int xr_usb_serial_alloc_minor(struct xr_usb_serial *xr_usb_serial)
{
int minor;
mutex_lock(&xr_usb_serial_table_lock);
for (minor = 0; minor < XR_USB_SERIAL_TTY_MINORS; minor++) {
if (!xr_usb_serial_table[minor]) {
xr_usb_serial_table[minor] = xr_usb_serial;
break;
}
}
mutex_unlock(&xr_usb_serial_table_lock);
return minor;
}
/* Release the minor number associated with 'xr_usb_serial'. */
static void xr_usb_serial_release_minor(struct xr_usb_serial *xr_usb_serial)
{
mutex_lock(&xr_usb_serial_table_lock);
xr_usb_serial_table[xr_usb_serial->minor] = NULL;
mutex_unlock(&xr_usb_serial_table_lock);
}
/*
* Functions for XR_USB_SERIAL control messages.
*/
static int xr_usb_serial_ctrl_msg(struct xr_usb_serial *xr_usb_serial, int request, int value,
void *buf, int len)
{
int retval = usb_control_msg(xr_usb_serial->dev, usb_sndctrlpipe(xr_usb_serial->dev, 0),
request, USB_RT_XR_USB_SERIAL, value,
xr_usb_serial->control->altsetting[0].desc.bInterfaceNumber,
buf, len, 5000);
dev_dbg(&xr_usb_serial->control->dev,
"%s - rq 0x%02x, val %#x, len %#x, result %d\n",
__func__, request, value, len, retval);
return retval < 0 ? retval : 0;
}
#include "xr_usb_serial_hal.c"
/*
* Write buffer management.
* All of these assume proper locks taken by the caller.
*/
static int xr_usb_serial_wb_alloc(struct xr_usb_serial *xr_usb_serial)
{
int i, wbn;
struct xr_usb_serial_wb *wb;
wbn = 0;
i = 0;
for (;;) {
wb = &xr_usb_serial->wb[wbn];
if (!wb->use) {
wb->use = 1;
return wbn;
}
wbn = (wbn + 1) % XR_USB_SERIAL_NW;
if (++i >= XR_USB_SERIAL_NW)
return -1;
}
}
static int xr_usb_serial_wb_is_avail(struct xr_usb_serial *xr_usb_serial)
{
int i, n;
unsigned long flags;
n = XR_USB_SERIAL_NW;
spin_lock_irqsave(&xr_usb_serial->write_lock, flags);
for (i = 0; i < XR_USB_SERIAL_NW; i++)
n -= xr_usb_serial->wb[i].use;
spin_unlock_irqrestore(&xr_usb_serial->write_lock, flags);
return n;
}
/*
* Finish write. Caller must hold xr_usb_serial->write_lock
*/
static void xr_usb_serial_write_done(struct xr_usb_serial *xr_usb_serial, struct xr_usb_serial_wb *wb)
{
wb->use = 0;
xr_usb_serial->transmitting--;
usb_autopm_put_interface_async(xr_usb_serial->control);
}
/*
* Poke write.
*
* the caller is responsible for locking
*/
static int xr_usb_serial_start_wb(struct xr_usb_serial *xr_usb_serial, struct xr_usb_serial_wb *wb)
{
int rc;
xr_usb_serial->transmitting++;
wb->urb->transfer_buffer = wb->buf;
wb->urb->transfer_dma = wb->dmah;
wb->urb->transfer_buffer_length = wb->len;
wb->urb->dev = xr_usb_serial->dev;
rc = usb_submit_urb(wb->urb, GFP_ATOMIC);
if (rc < 0) {
dev_err(&xr_usb_serial->data->dev,
"%s - usb_submit_urb(write bulk) failed: %d\n",
__func__, rc);
xr_usb_serial_write_done(xr_usb_serial, wb);
}
return rc;
}
/*
* attributes exported through sysfs
*/
static ssize_t show_caps
(struct device *dev, struct device_attribute *attr, char *buf)
{
struct usb_interface *intf = to_usb_interface(dev);
struct xr_usb_serial *xr_usb_serial = usb_get_intfdata(intf);
return sprintf(buf, "%d", xr_usb_serial->ctrl_caps);
}
static DEVICE_ATTR(bmCapabilities, S_IRUGO, show_caps, NULL);
static ssize_t show_country_codes
(struct device *dev, struct device_attribute *attr, char *buf)
{
struct usb_interface *intf = to_usb_interface(dev);
struct xr_usb_serial *xr_usb_serial = usb_get_intfdata(intf);
memcpy(buf, xr_usb_serial->country_codes, xr_usb_serial->country_code_size);
return xr_usb_serial->country_code_size;
}
static DEVICE_ATTR(wCountryCodes, S_IRUGO, show_country_codes, NULL);
static ssize_t show_country_rel_date
(struct device *dev, struct device_attribute *attr, char *buf)
{
struct usb_interface *intf = to_usb_interface(dev);
struct xr_usb_serial *xr_usb_serial = usb_get_intfdata(intf);
return sprintf(buf, "%d", xr_usb_serial->country_rel_date);
}
static DEVICE_ATTR(iCountryCodeRelDate, S_IRUGO, show_country_rel_date, NULL);
/*
* Interrupt handlers for various XR_USB_SERIAL device responses
*/
/* control interface reports status changes with "interrupt" transfers */
static void xr_usb_serial_ctrl_irq(struct urb *urb)
{
struct xr_usb_serial *xr_usb_serial = urb->context;
struct usb_cdc_notification *dr = urb->transfer_buffer;
#if LINUX_VERSION_CODE > KERNEL_VERSION(3, 9, 0)
#else
struct tty_struct *tty;
#endif
unsigned char *data;
int newctrl;
int retval;
int status = urb->status;
int i;
unsigned char *p;
switch (status) {
case 0:
p = (unsigned char *)(urb->transfer_buffer);
for(i=0;i<urb->actual_length;i++)
{
dev_dbg(&xr_usb_serial->control->dev,"0x%02x\n",p[i]);
}
/* success */
break;
case -ECONNRESET:
case -ENOENT:
case -ESHUTDOWN:
/* this urb is terminated, clean up */
dev_dbg(&xr_usb_serial->control->dev,
"%s - urb shutting down with status: %d\n",
__func__, status);
return;
default:
dev_dbg(&xr_usb_serial->control->dev,
"%s - nonzero urb status received: %d\n",
__func__, status);
goto exit;
}
usb_mark_last_busy(xr_usb_serial->dev);
data = (unsigned char *)(dr + 1);
switch (dr->bNotificationType) {
case USB_CDC_NOTIFY_NETWORK_CONNECTION:
dev_dbg(&xr_usb_serial->control->dev, "%s - network connection: %d\n",
__func__, dr->wValue);
break;
case USB_CDC_NOTIFY_SERIAL_STATE:
#if LINUX_VERSION_CODE > KERNEL_VERSION(3, 9, 0)
newctrl = get_unaligned_le16(data);
if (!xr_usb_serial->clocal && (xr_usb_serial->ctrlin & ~newctrl & XR_USB_SERIAL_CTRL_DCD)) {
dev_dbg(&xr_usb_serial->control->dev, "%s - calling hangup\n",
__func__);
tty_port_tty_hangup(&xr_usb_serial->port, false);
}
#else
tty = tty_port_tty_get(&xr_usb_serial->port);
newctrl = get_unaligned_le16(data);
if (tty)
{
if (!xr_usb_serial->clocal &&
(xr_usb_serial->ctrlin & ~newctrl & XR_USB_SERIAL_CTRL_DCD)) {
dev_dbg(&xr_usb_serial->control->dev,
"%s - calling hangup\n", __func__);
tty_hangup(tty);
}
tty_kref_put(tty);
}
#endif
xr_usb_serial->ctrlin = newctrl;
dev_dbg(&xr_usb_serial->control->dev,
"%s - input control lines: dcd%c dsr%c break%c "
"ring%c framing%c parity%c overrun%c\n",
__func__,
xr_usb_serial->ctrlin & XR_USB_SERIAL_CTRL_DCD ? '+' : '-',
xr_usb_serial->ctrlin & XR_USB_SERIAL_CTRL_DSR ? '+' : '-',
xr_usb_serial->ctrlin & XR_USB_SERIAL_CTRL_BRK ? '+' : '-',
xr_usb_serial->ctrlin & XR_USB_SERIAL_CTRL_RI ? '+' : '-',
xr_usb_serial->ctrlin & XR_USB_SERIAL_CTRL_FRAMING ? '+' : '-',
xr_usb_serial->ctrlin & XR_USB_SERIAL_CTRL_PARITY ? '+' : '-',
xr_usb_serial->ctrlin & XR_USB_SERIAL_CTRL_OVERRUN ? '+' : '-');
break;
default:
dev_dbg(&xr_usb_serial->control->dev,
"%s - unknown notification %d received: index %d "
"len %d data0 %d data1 %d\n",
__func__,
dr->bNotificationType, dr->wIndex,
dr->wLength, data[0], data[1]);
break;
}
exit:
retval = usb_submit_urb(urb, GFP_ATOMIC);
if (retval)
dev_err(&xr_usb_serial->control->dev, "%s - usb_submit_urb failed: %d\n",
__func__, retval);
}
static int xr_usb_serial_submit_read_urb(struct xr_usb_serial *xr_usb_serial, int index, gfp_t mem_flags)
{
int res;
if (!test_and_clear_bit(index, &xr_usb_serial->read_urbs_free))
return 0;
dev_vdbg(&xr_usb_serial->data->dev, "%s - urb %d\n", __func__, index);
res = usb_submit_urb(xr_usb_serial->read_urbs[index], mem_flags);
if (res) {
if (res != -EPERM) {
dev_err(&xr_usb_serial->data->dev,
"%s - usb_submit_urb failed: %d\n",
__func__, res);
}
set_bit(index, &xr_usb_serial->read_urbs_free);
return res;
}
return 0;
}
static int xr_usb_serial_submit_read_urbs(struct xr_usb_serial *xr_usb_serial, gfp_t mem_flags)
{
int res;
int i;
for (i = 0; i < xr_usb_serial->rx_buflimit; ++i) {
res = xr_usb_serial_submit_read_urb(xr_usb_serial, i, mem_flags);
if (res)
return res;
}
return 0;
}
static void xr_usb_serial_process_read_urb(struct xr_usb_serial *xr_usb_serial, struct urb *urb)
{
#if LINUX_VERSION_CODE > KERNEL_VERSION(3, 9, 0)
#else
struct tty_struct *tty;
#endif
int preciseflags = xr_usb_serial->preciseflags;
int have_extra_byte;
int length;
if (!urb->actual_length)
return;
if (preciseflags)
{
char *dp = urb->transfer_buffer;
int i, ch, ch_flags;
length = urb->actual_length;
length = length + (xr_usb_serial->have_extra_byte ? 1 : 0);
have_extra_byte = (preciseflags && (length & 1));
length = (preciseflags) ? (length / 2) : length;
for (i = 0; i < length; ++i)
{
char tty_flag;
if (i == 0)
{
if (xr_usb_serial->have_extra_byte)
{
ch = xr_usb_serial->extra_byte;
}
else
{
ch = *dp++;
}
}
else
{
ch = *dp++;
}
ch_flags = *dp++;
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;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0)
tty_insert_flip_char(&xr_usb_serial->port, ch, tty_flag);
tty_flip_buffer_push(&xr_usb_serial->port);
#else
tty = tty_port_tty_get(&xr_usb_serial->port);
if (!tty)
return;
tty_insert_flip_char(&xr_usb_serial->port, ch, tty_flag);
tty_flip_buffer_push(tty);
tty_kref_put(tty);
#endif
}
}
else
{
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0)
tty_insert_flip_string(&xr_usb_serial->port, urb->transfer_buffer,
urb->actual_length);
tty_flip_buffer_push(&xr_usb_serial->port);
#else
tty = tty_port_tty_get(&xr_usb_serial->port);
if (!tty)
return;
tty_insert_flip_string(tty, urb->transfer_buffer, urb->actual_length);
tty_flip_buffer_push(tty);
tty_kref_put(tty);
#endif
}
}
static void xr_usb_serial_read_bulk_callback(struct urb *urb)
{
struct xr_usb_serial_rb *rb = urb->context;
struct xr_usb_serial *xr_usb_serial = rb->instance;
unsigned long flags;
dev_vdbg(&xr_usb_serial->data->dev, "%s - urb %d, len %d\n", __func__,
rb->index, urb->actual_length);
set_bit(rb->index, &xr_usb_serial->read_urbs_free);
if (!xr_usb_serial->dev) {
dev_dbg(&xr_usb_serial->data->dev, "%s - disconnected\n", __func__);
return;
}
usb_mark_last_busy(xr_usb_serial->dev);
if (urb->status) {
dev_dbg(&xr_usb_serial->data->dev, "%s - non-zero urb status: %d\n",
__func__, urb->status);
//return;
}
xr_usb_serial_process_read_urb(xr_usb_serial, urb);
/* throttle device if requested by tty */
spin_lock_irqsave(&xr_usb_serial->read_lock, flags);
xr_usb_serial->throttled = xr_usb_serial->throttle_req;
if (!xr_usb_serial->throttled && !xr_usb_serial->susp_count) {
spin_unlock_irqrestore(&xr_usb_serial->read_lock, flags);
xr_usb_serial_submit_read_urb(xr_usb_serial, rb->index, GFP_ATOMIC);
} else {
spin_unlock_irqrestore(&xr_usb_serial->read_lock, flags);
}
}
/* data interface wrote those outgoing bytes */
static void xr_usb_serial_write_bulk(struct urb *urb)
{
struct xr_usb_serial_wb *wb = urb->context;
struct xr_usb_serial *xr_usb_serial = wb->instance;
unsigned long flags;
if (urb->status || (urb->actual_length != urb->transfer_buffer_length))
dev_vdbg(&xr_usb_serial->data->dev, "%s - len %d/%d, status %d\n",
__func__,
urb->actual_length,
urb->transfer_buffer_length,
urb->status);
spin_lock_irqsave(&xr_usb_serial->write_lock, flags);
xr_usb_serial_write_done(xr_usb_serial, wb);
spin_unlock_irqrestore(&xr_usb_serial->write_lock, flags);
schedule_work(&xr_usb_serial->work);
}
static void xr_usb_serial_softint(struct work_struct *work)
{
struct xr_usb_serial *xr_usb_serial = container_of(work, struct xr_usb_serial, work);
#if LINUX_VERSION_CODE > KERNEL_VERSION(3, 9, 0)
#else
struct tty_struct *tty;
#endif
dev_vdbg(&xr_usb_serial->data->dev, "%s\n", __func__);
#if LINUX_VERSION_CODE > KERNEL_VERSION(3, 9, 0)
tty_port_tty_wakeup(&xr_usb_serial->port);
#else
tty = tty_port_tty_get(&xr_usb_serial->port);
if (!tty)
return;
tty_wakeup(tty);
tty_kref_put(tty);
#endif
}
/*
* TTY handlers
*/
static int xr_usb_serial_tty_install(struct tty_driver *driver, struct tty_struct *tty)
{
struct xr_usb_serial *xr_usb_serial;
int retval;
dev_dbg(tty->dev, "%s\n", __func__);
xr_usb_serial = xr_usb_serial_get_by_index(tty->index);
if (!xr_usb_serial)
return -ENODEV;
retval = tty_standard_install(driver, tty);
if (retval)
goto error_init_termios;
tty->driver_data = xr_usb_serial;
return 0;
error_init_termios:
tty_port_put(&xr_usb_serial->port);
return retval;
}
static int xr_usb_serial_tty_open(struct tty_struct *tty, struct file *filp)
{
struct xr_usb_serial *xr_usb_serial = tty->driver_data;
int result;
result = xr_usb_serial_fifo_reset(xr_usb_serial);
dev_dbg(tty->dev, "%s\n", __func__);
return tty_port_open(&xr_usb_serial->port, tty, filp);
}
static int xr_usb_serial_port_activate(struct tty_port *port, struct tty_struct *tty)
{
struct xr_usb_serial *xr_usb_serial = container_of(port, struct xr_usb_serial, port);
int retval = -ENODEV;
dev_dbg(&xr_usb_serial->control->dev, "%s\n", __func__);
mutex_lock(&xr_usb_serial->mutex);
if (xr_usb_serial->disconnected)
goto disconnected;
retval = usb_autopm_get_interface(xr_usb_serial->control);
if (retval)
goto error_get_interface;
/*
* FIXME: Why do we need this? Allocating 64K of physically contiguous
* memory is really nasty...
*/
set_bit(TTY_NO_WRITE_SPLIT, &tty->flags);
xr_usb_serial->control->needs_remote_wakeup = 1;
xr_usb_serial->ctrlurb->dev = xr_usb_serial->dev;
if (usb_submit_urb(xr_usb_serial->ctrlurb, GFP_KERNEL)) {
dev_err(&xr_usb_serial->control->dev,
"%s - usb_submit_urb(ctrl irq) failed\n", __func__);
goto error_submit_urb;
}
xr_usb_serial->ctrlout = XR_USB_SERIAL_CTRL_DTR | XR_USB_SERIAL_CTRL_RTS;
if (xr_usb_serial_set_control(xr_usb_serial, xr_usb_serial->ctrlout) < 0 &&
(xr_usb_serial->ctrl_caps & USB_CDC_CAP_LINE))
goto error_set_control;
usb_autopm_put_interface(xr_usb_serial->control);
/*
* Unthrottle device in case the TTY was closed while throttled.
*/
spin_lock_irq(&xr_usb_serial->read_lock);
xr_usb_serial->throttled = 0;
xr_usb_serial->throttle_req = 0;
spin_unlock_irq(&xr_usb_serial->read_lock);
if (xr_usb_serial_submit_read_urbs(xr_usb_serial, GFP_KERNEL))
goto error_submit_read_urbs;
mutex_unlock(&xr_usb_serial->mutex);
return 0;
error_submit_read_urbs:
xr_usb_serial->ctrlout = 0;
xr_usb_serial_set_control(xr_usb_serial, xr_usb_serial->ctrlout);
error_set_control:
usb_kill_urb(xr_usb_serial->ctrlurb);
error_submit_urb:
usb_autopm_put_interface(xr_usb_serial->control);
error_get_interface:
disconnected:
mutex_unlock(&xr_usb_serial->mutex);
return retval;
}
static void xr_usb_serial_port_destruct(struct tty_port *port)
{
struct xr_usb_serial *xr_usb_serial = container_of(port, struct xr_usb_serial, port);
dev_dbg(&xr_usb_serial->control->dev, "%s\n", __func__);
#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 7, 0)
tty_unregister_device(xr_usb_serial_tty_driver, xr_usb_serial->minor);
#endif
#ifdef CONFIG_GPIOLIB
if (xr_usb_serial->rv_gpio_created == 0)
gpiochip_remove(&xr_usb_serial->xr_gpio);
#endif
xr_usb_serial_release_minor(xr_usb_serial);
usb_put_intf(xr_usb_serial->control);
kfree(xr_usb_serial->country_codes);
kfree(xr_usb_serial);
}
static void xr_usb_serial_port_shutdown(struct tty_port *port)
{
struct xr_usb_serial *xr_usb_serial = container_of(port, struct xr_usb_serial, port);
int i;
dev_dbg(&xr_usb_serial->control->dev, "%s\n", __func__);
mutex_lock(&xr_usb_serial->mutex);
if (!xr_usb_serial->disconnected) {
usb_autopm_get_interface(xr_usb_serial->control);
xr_usb_serial_set_control(xr_usb_serial, xr_usb_serial->ctrlout = 0);
usb_kill_urb(xr_usb_serial->ctrlurb);
for (i = 0; i < XR_USB_SERIAL_NW; i++)
usb_kill_urb(xr_usb_serial->wb[i].urb);
for (i = 0; i < xr_usb_serial->rx_buflimit; i++)
usb_kill_urb(xr_usb_serial->read_urbs[i]);
xr_usb_serial->control->needs_remote_wakeup = 0;
usb_autopm_put_interface(xr_usb_serial->control);
}
mutex_unlock(&xr_usb_serial->mutex);
}
static void xr_usb_serial_tty_cleanup(struct tty_struct *tty)
{
struct xr_usb_serial *xr_usb_serial = tty->driver_data;
dev_dbg(&xr_usb_serial->control->dev, "%s\n", __func__);
tty_port_put(&xr_usb_serial->port);
}
static void xr_usb_serial_tty_hangup(struct tty_struct *tty)
{
struct xr_usb_serial *xr_usb_serial = tty->driver_data;
dev_dbg(&xr_usb_serial->control->dev, "%s\n", __func__);
tty_port_hangup(&xr_usb_serial->port);
}
static void xr_usb_serial_tty_close(struct tty_struct *tty, struct file *filp)
{
struct xr_usb_serial *xr_usb_serial = tty->driver_data;
dev_dbg(&xr_usb_serial->control->dev, "%s\n", __func__);
tty_port_close(&xr_usb_serial->port, tty, filp);
}
static int xr_usb_serial_tty_write(struct tty_struct *tty,
const unsigned char *buf, int count)
{
struct xr_usb_serial *xr_usb_serial = tty->driver_data;
int stat;
unsigned long flags;
int wbn;
struct xr_usb_serial_wb *wb;
if (!count)
return 0;
//dev_vdbg(&xr_usb_serial->data->dev, "%s - count %d\n", __func__, count);
spin_lock_irqsave(&xr_usb_serial->write_lock, flags);
wbn = xr_usb_serial_wb_alloc(xr_usb_serial);
if (wbn < 0) {
spin_unlock_irqrestore(&xr_usb_serial->write_lock, flags);
return 0;
}
wb = &xr_usb_serial->wb[wbn];
if (!xr_usb_serial->dev) {
wb->use = 0;
spin_unlock_irqrestore(&xr_usb_serial->write_lock, flags);
return -ENODEV;
}
count = (count > xr_usb_serial->writesize) ? xr_usb_serial->writesize : count;
//dev_vdbg(&xr_usb_serial->data->dev, "%s - write %d\n", __func__, count);
memcpy(wb->buf, buf, count);
wb->len = count;
usb_autopm_get_interface_async(xr_usb_serial->control);
if (xr_usb_serial->susp_count) {
if (!xr_usb_serial->delayed_wb)
xr_usb_serial->delayed_wb = wb;
else
usb_autopm_put_interface_async(xr_usb_serial->control);
spin_unlock_irqrestore(&xr_usb_serial->write_lock, flags);
return count; /* A white lie */
}
usb_mark_last_busy(xr_usb_serial->dev);
stat = xr_usb_serial_start_wb(xr_usb_serial, wb);
spin_unlock_irqrestore(&xr_usb_serial->write_lock, flags);
if (stat < 0)
return stat;
return count;
}
static int xr_usb_serial_tty_write_room(struct tty_struct *tty)
{
struct xr_usb_serial *xr_usb_serial = tty->driver_data;
/*
* Do not let the line discipline to know that we have a reserve,
* or it might get too enthusiastic.
*/
return xr_usb_serial_wb_is_avail(xr_usb_serial) ? xr_usb_serial->writesize : 0;
}
static int xr_usb_serial_tty_chars_in_buffer(struct tty_struct *tty)
{
struct xr_usb_serial *xr_usb_serial = tty->driver_data;
/*
* if the device was unplugged then any remaining characters fell out
* of the connector ;)
*/
if (xr_usb_serial->disconnected)
return 0;
/*
* This is inaccurate (overcounts), but it works.
*/
return (XR_USB_SERIAL_NW - xr_usb_serial_wb_is_avail(xr_usb_serial)) * xr_usb_serial->writesize;
}
static void xr_usb_serial_tty_throttle(struct tty_struct *tty)
{
struct xr_usb_serial *xr_usb_serial = tty->driver_data;
spin_lock_irq(&xr_usb_serial->read_lock);
xr_usb_serial->throttle_req = 1;
spin_unlock_irq(&xr_usb_serial->read_lock);
}
static void xr_usb_serial_tty_unthrottle(struct tty_struct *tty)
{
struct xr_usb_serial *xr_usb_serial = tty->driver_data;
unsigned int was_throttled;
spin_lock_irq(&xr_usb_serial->read_lock);
was_throttled = xr_usb_serial->throttled;
xr_usb_serial->throttled = 0;
xr_usb_serial->throttle_req = 0;
spin_unlock_irq(&xr_usb_serial->read_lock);
if (was_throttled)
xr_usb_serial_submit_read_urbs(xr_usb_serial, GFP_KERNEL);
}
static int xr_usb_serial_tty_break_ctl(struct tty_struct *tty, int state)
{
struct xr_usb_serial *xr_usb_serial = tty->driver_data;
int retval;
retval = xr_usb_serial_send_break(xr_usb_serial, state ? 0xffff : 0);
if (retval < 0)
dev_err(&xr_usb_serial->control->dev, "%s - send break failed\n",
__func__);
return retval;
}
static int xr_usb_serial_tty_tiocmget(struct tty_struct *tty)
{
struct xr_usb_serial *xr_usb_serial = tty->driver_data;
//dev_dbg(&xr_usb_serial->control->dev, "xr_usb_serial_tty_tiocmget\n");
return xr_usb_serial_tiocmget(xr_usb_serial);
}
static int xr_usb_serial_tty_tiocmset(struct tty_struct *tty,
unsigned int set, unsigned int clear)
{
struct xr_usb_serial *xr_usb_serial = tty->driver_data;
//dev_dbg(&xr_usb_serial->control->dev, "xr_usb_serial_tty_tiocmset set=0x%x clear=0x%x\n",set,clear);
return xr_usb_serial_tiocmset(xr_usb_serial,set,clear);
}
static int get_serial_info(struct xr_usb_serial *xr_usb_serial, struct serial_struct __user *info)
{
struct serial_struct tmp;
if (!info)
return -EINVAL;
memset(&tmp, 0, sizeof(tmp));
tmp.flags = ASYNC_LOW_LATENCY;
tmp.xmit_fifo_size = xr_usb_serial->writesize;
tmp.baud_base = le32_to_cpu(xr_usb_serial->line.dwDTERate);
tmp.close_delay = xr_usb_serial->port.close_delay / 10;
tmp.closing_wait = xr_usb_serial->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
ASYNC_CLOSING_WAIT_NONE :
xr_usb_serial->port.closing_wait / 10;
if (copy_to_user(info, &tmp, sizeof(tmp)))
return -EFAULT;
else
return 0;
}
static int set_serial_info(struct xr_usb_serial *xr_usb_serial,
struct serial_struct __user *newinfo)
{
struct serial_struct new_serial;
unsigned int closing_wait, close_delay;
int retval = 0;
if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
return -EFAULT;
close_delay = new_serial.close_delay * 10;
closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
mutex_lock(&xr_usb_serial->port.mutex);
if (!capable(CAP_SYS_ADMIN)) {
if ((close_delay != xr_usb_serial->port.close_delay) ||
(closing_wait != xr_usb_serial->port.closing_wait))
retval = -EPERM;
else
retval = -EOPNOTSUPP;
} else {
xr_usb_serial->port.close_delay = close_delay;
xr_usb_serial->port.closing_wait = closing_wait;
}
mutex_unlock(&xr_usb_serial->port.mutex);
return retval;
}
static int xr_usb_serial_tty_ioctl(struct tty_struct *tty,
unsigned int cmd, unsigned long arg)
{
struct xr_usb_serial *xr_usb_serial = tty->driver_data;
int rv = -ENOIOCTLCMD;
unsigned int channel, reg, val,preciseflags;
int baud_rate = 0;
struct usb_cdc_line_coding newline;
short *data;
switch (cmd) {
case TIOCGSERIAL: /* gets serial port data */
rv = get_serial_info(xr_usb_serial, (struct serial_struct __user *) arg);
break;
case TIOCSSERIAL:
rv = set_serial_info(xr_usb_serial, (struct serial_struct __user *) arg);
break;
case XR_USB_SERIAL_GET_REG:
if (get_user(channel, (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(&xr_usb_serial->control->dev, "%s - Cannot allocate USB buffer.\n", __func__);
return -ENOMEM;
}
if (channel == -1)
{
rv = xr_usb_serial_get_reg(xr_usb_serial,reg, data);
}
else
{
rv = xr_usb_serial_get_reg_ext(xr_usb_serial,channel,reg, data);
}
if (rv < 0)
{
dev_err(&xr_usb_serial->control->dev, "Cannot get register (%d)\n", rv);
kfree(data);
return -EFAULT;
}
if (put_user(le16_to_cpu(*data), (int __user *)(arg + 2 * sizeof(int))))
{
dev_err(&xr_usb_serial->control->dev, "Cannot put user result\n");
kfree(data);
return -EFAULT;
}
rv = 0;
kfree(data);
break;
case XR_USB_SERIAL_SET_REG:
if (get_user(channel, (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 (channel == -1)
{
rv = xr_usb_serial_set_reg(xr_usb_serial,reg, val);
}
else
{
rv = xr_usb_serial_set_reg_ext(xr_usb_serial,channel,reg, val);
}
if (rv < 0)
return -EFAULT;
rv = 0;
break;
case XR_USB_SERIAL_LOOPBACK:
if (get_user(channel, (int __user *)arg))
return -EFAULT;
if (channel == -1)
channel = xr_usb_serial->channel;
rv = xr_usb_serial_set_loopback(xr_usb_serial,channel);
if (rv < 0)
return -EFAULT;
rv = 0;
break;
case XR_USB_SERIAL_SET_GPIO_MODE_REG:
xr_usb_serial_disable(xr_usb_serial);
if (get_user(channel, (int __user *)arg))
return -EFAULT;
if (get_user(val, (int __user *)(arg + sizeof(int))))
return -EFAULT;
if (channel == -1)
{
//block = portdata->block;
rv = xr_usb_serial_set_reg(xr_usb_serial,xr_usb_serial->reg_map.uart_gpio_mode_addr, val);
}
else
{
rv = xr_usb_serial_set_reg_ext(xr_usb_serial,channel,xr_usb_serial->reg_map.uart_gpio_mode_addr, val);
}
dev_dbg(&xr_usb_serial->control->dev, "XR_USB_SERIAL_SET_GPIO_MODE_REG 0x%x val:0x%x \n", xr_usb_serial->reg_map.uart_gpio_mode_addr,val);
xr_usb_serial_enable(xr_usb_serial);
if (rv < 0)