-
Notifications
You must be signed in to change notification settings - Fork 1
/
ch34x.c
1630 lines (1344 loc) · 38.8 KB
/
ch34x.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
/*
* ch34x.c - V1.11
*
* Copyright (c) 2020 SoldierJazz <zhangj@wch.cn>
*
* USB driver for USB to serial chip ch34x
*
* Sponsored by SuSE
*
* System required:
* Kernel version beyond 3.4.x
* Update Log:
* V1.0 - initial version
* V1.01 - added supports for high baudrates
* V1.10 - added supports for modem signal, flow control, etc.
* V1.11 - fixed usb pid supports
*
* 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
*/
#define DEBUG
#define VERBOSE_DEBUG
#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/idr.h>
#include <linux/list.h>
#include <linux/version.h>
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0))
#include <linux/sched/signal.h>
#endif
#include "ch34x.h"
#define DRIVER_AUTHOR "SoldierJazz"
#define DRIVER_DESC "USB driver for USB to serial chip ch340, ch341, ch9341, etc."
#define VERSION_DESC "V1.11 On 2020.03.02"
static struct usb_driver ch34x_driver;
static struct tty_driver *ch34x_tty_driver;
static DEFINE_IDR(ch34x_minors);
static DEFINE_MUTEX(ch34x_minors_lock);
static void ch34x_tty_set_termios(struct tty_struct *tty,
struct ktermios *termios_old);
/*
* ch34x_minors accessors
*/
/*
* Look up an ch34x structure by minor. If found and not disconnected, increment
* its refcount and return it with its mutex held.
*/
static struct ch34x *ch34x_get_by_minor(unsigned int minor)
{
struct ch34x *ch34x;
mutex_lock(&ch34x_minors_lock);
ch34x = idr_find(&ch34x_minors, minor);
if (ch34x) {
mutex_lock(&ch34x->mutex);
if (ch34x->disconnected) {
mutex_unlock(&ch34x->mutex);
ch34x = NULL;
} else {
tty_port_get(&ch34x->port);
mutex_unlock(&ch34x->mutex);
}
}
mutex_unlock(&ch34x_minors_lock);
return ch34x;
}
/*
* Try to find an available minor number and if found, associate it with 'ch34x'.
*/
static int ch34x_alloc_minor(struct ch34x *ch34x)
{
int minor;
mutex_lock(&ch34x_minors_lock);
minor = idr_alloc(&ch34x_minors, ch34x, 0, CH34X_TTY_MINORS, GFP_KERNEL);
mutex_unlock(&ch34x_minors_lock);
return minor;
}
/* Release the minor number associated with 'ch34x'. */
static void ch34x_release_minor(struct ch34x *ch34x)
{
mutex_lock(&ch34x_minors_lock);
idr_remove(&ch34x_minors, ch34x->minor);
mutex_unlock(&ch34x_minors_lock);
}
/*
* Functions for CH34X control messages.
*/
static int ch34x_control_out(struct ch34x *ch34x, u8 request,
u16 value, u16 index)
{
int retval;
retval = usb_autopm_get_interface(ch34x->data);
if (retval)
return retval;
retval = usb_control_msg(ch34x->dev, usb_sndctrlpipe(ch34x->dev, 0),
request, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
value, index, NULL, 0, DEFAULT_TIMEOUT);
dev_vdbg(&ch34x->data->dev,
"ch34x_control_out(%02x,%02x,%04x,%04x)\n",
USB_DIR_OUT|0x40, request, value, index);
usb_autopm_put_interface(ch34x->data);
return retval < 0 ? retval : 0;
}
static int ch34x_control_in(struct ch34x *ch34x,
u8 request, u16 value, u16 index,
char *buf, unsigned bufsize)
{
int retval;
int i;
retval = usb_autopm_get_interface(ch34x->data);
if (retval)
return retval;
retval = usb_control_msg(ch34x->dev, usb_rcvctrlpipe(ch34x->dev, 0), request,
USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
value, index, buf, bufsize, DEFAULT_TIMEOUT);
dev_vdbg(&ch34x->data->dev,
"ch34x_control_in(%02x,%02x,%04x,%04x,%p,%u)\n",
USB_DIR_IN | 0x40, (u8)request, (u16)value, (u16)index, buf,
(int)bufsize);
dev_vdbg(&ch34x->data->dev,
"ch34x_control_in result:");
for (i = 0; i < retval; i++) {
dev_vdbg(&ch34x->data->dev,
"0x%.2x ", (u8)buf[i]);
}
usb_autopm_put_interface(ch34x->data);
return retval;
}
static inline int ch34x_set_control(struct ch34x *ch34x, int control)
{
return ch34x_control_out(ch34x, VENDOR_MODEM_OUT,
~control, 0x0000);
}
static inline int ch34x_set_line(struct ch34x *ch34x, struct usb_cdc_line_coding *line)
{
return 0;
}
static int ch34x_get_status(struct ch34x *ch34x)
{
char *buffer;
int retval;
const unsigned size = 2;
unsigned long flags;
buffer = kmalloc(size, GFP_KERNEL);
if (!buffer)
return -ENOMEM;
retval = ch34x_control_in(ch34x, VENDOR_READ, 0x0706, 0, buffer, size);
if (retval < 0)
goto out;
/* setup the private status if available */
if (retval > 0) {
spin_lock_irqsave(&ch34x->read_lock, flags);
ch34x->ctrlin = (~(*buffer)) & CH34X_MODEM_STAT;
spin_unlock_irqrestore(&ch34x->read_lock, flags);
} else
retval = -EPROTO;
out:
kfree(buffer);
return retval;
}
/* -------------------------------------------------------------------------- */
static int ch34x_configure(struct ch34x *ch34x)
{
char *buffer;
int r;
const unsigned size = 2;
buffer = kmalloc(size, GFP_KERNEL);
if (!buffer)
return -ENOMEM;
/* expect two bytes 0x27 0x00 */
r = ch34x_control_in(ch34x, VENDOR_VERSION, 0, 0, buffer, size);
if (r < 0)
goto out;
r = ch34x_control_out(ch34x, VENDOR_SERIAL_INIT, 0, 0);
if (r < 0)
goto out;
r = ch34x_control_out(ch34x, VENDOR_WRITE, 0x1312, 0xd982);
if (r < 0)
goto out;
r = ch34x_control_out(ch34x, VENDOR_WRITE, 0x0f2c, 0x0007);
if (r < 0)
goto out;
r = ch34x_control_in(ch34x, VENDOR_READ, 0x2518, 0, buffer, size);
if (r < 0)
goto out;
r = ch34x_get_status(ch34x);
if (r < 0)
goto out;
r = ch34x_control_out(ch34x, VENDOR_WRITE, 0x2727, 0);
if (r < 0)
goto out;
/*
r = ch34x_control_out(ch34x, VENDOR_MODEM_OUT, 0x00ff, 0);
if (r < 0)
goto out;
*/
out:
kfree(buffer);
return r;
}
/*
* Write buffer management.
* All of these assume proper locks taken by the caller.
*/
static int ch34x_wb_alloc(struct ch34x *ch34x)
{
int i, wbn;
struct ch34x_wb *wb;
wbn = 0;
i = 0;
for (;;) {
wb = &ch34x->wb[wbn];
if (!wb->use) {
wb->use = 1;
return wbn;
}
wbn = (wbn + 1) % CH34X_NW;
if (++i >= CH34X_NW)
return -1;
}
}
static int ch34x_wb_is_avail(struct ch34x *ch34x)
{
int i, n;
unsigned long flags;
n = CH34X_NW;
spin_lock_irqsave(&ch34x->write_lock, flags);
for (i = 0; i < CH34X_NW; i++)
n -= ch34x->wb[i].use;
spin_unlock_irqrestore(&ch34x->write_lock, flags);
return n;
}
/*
* Finish write. Caller must hold ch34x->write_lock
*/
static void ch34x_write_done(struct ch34x *ch34x, struct ch34x_wb *wb)
{
wb->use = 0;
ch34x->transmitting--;
usb_autopm_put_interface_async(ch34x->data);
}
/*
* Poke write.
*
* the caller is responsible for locking
*/
static int ch34x_start_wb(struct ch34x *ch34x, struct ch34x_wb *wb)
{
int rc;
ch34x->transmitting++;
wb->urb->transfer_buffer = wb->buf;
wb->urb->transfer_dma = wb->dmah;
wb->urb->transfer_buffer_length = wb->len;
wb->urb->dev = ch34x->dev;
rc = usb_submit_urb(wb->urb, GFP_ATOMIC);
if (rc < 0) {
dev_err(&ch34x->data->dev,
"%s - usb_submit_urb(write bulk) failed: %d\n",
__func__, rc);
ch34x_write_done(ch34x, wb);
}
return rc;
}
static void ch34x_update_status(struct ch34x *ch34x,
unsigned char *data, size_t len)
{
unsigned long flags;
u8 status;
u8 difference;
if (len < 4)
return;
status = ~data[2] & CH34X_MODEM_STAT;
if (!ch34x->clocal && (ch34x->ctrlin & status & CH34X_CTRL_DCD)) {
dev_dbg(&ch34x->data->dev, "%s - calling hangup\n",
__func__);
tty_port_tty_hangup(&ch34x->port, false);
}
spin_lock_irqsave(&ch34x->read_lock, flags);
difference = status ^ ch34x->ctrlin;
ch34x->ctrlin = status;
if (data[1] & CH34X_MULT_STAT)
dev_vdbg(&ch34x->data->dev, "%s - multiple status change\n", __func__);
if (!difference) {
spin_unlock_irqrestore(&ch34x->read_lock, flags);
return;
}
if (difference & CH34X_CTRL_CTS) {
dev_vdbg(&ch34x->data->dev, "%s - cts change\n", __func__);
ch34x->iocount.cts++;
}
if (difference & CH34X_CTRL_DSR) {
dev_vdbg(&ch34x->data->dev, "%s - dsr change\n", __func__);
ch34x->iocount.dsr++;
}
if (difference & CH34X_CTRL_RI) {
dev_vdbg(&ch34x->data->dev, "%s - rng change\n", __func__);
ch34x->iocount.rng++;
}
if (difference & CH34X_CTRL_DCD) {
dev_vdbg(&ch34x->data->dev, "%s - dcd change\n", __func__);
ch34x->iocount.dcd++;
}
spin_unlock_irqrestore(&ch34x->read_lock, flags);
wake_up_interruptible(&ch34x->wioctl);
}
/*
* Interrupt handlers for various CH34X device responses
*/
/* control interface reports status changes with "interrupt" transfers */
static void ch34x_ctrl_irq(struct urb *urb)
{
struct ch34x *ch34x = urb->context;
unsigned char *data = urb->transfer_buffer;
unsigned int len = urb->actual_length;
int status = urb->status;
int retval;
switch (status) {
case 0:
/* success */
break;
case -ECONNRESET:
case -ENOENT:
case -ESHUTDOWN:
/* this urb is terminated, clean up */
dev_dbg(&ch34x->data->dev,
"%s - urb shutting down with status: %d\n",
__func__, status);
return;
default:
dev_dbg(&ch34x->data->dev,
"%s - nonzero urb status received: %d\n",
__func__, status);
goto exit;
}
usb_mark_last_busy(ch34x->dev);
ch34x_update_status(ch34x, data, len);
exit:
retval = usb_submit_urb(urb, GFP_ATOMIC);
if (retval && retval != -EPERM)
dev_err(&ch34x->data->dev, "%s - usb_submit_urb failed: %d\n",
__func__, retval);
}
static int ch34x_submit_read_urb(struct ch34x *ch34x, int index, gfp_t mem_flags)
{
int res;
if (!test_and_clear_bit(index, &ch34x->read_urbs_free))
return 0;
dev_vdbg(&ch34x->data->dev, "%s - urb %d\n", __func__, index);
res = usb_submit_urb(ch34x->read_urbs[index], mem_flags);
if (res) {
if (res != -EPERM) {
dev_err(&ch34x->data->dev,
"%s - usb_submit_urb failed: %d\n",
__func__, res);
}
set_bit(index, &ch34x->read_urbs_free);
return res;
}
return 0;
}
static int ch34x_submit_read_urbs(struct ch34x *ch34x, gfp_t mem_flags)
{
int res;
int i;
for (i = 0; i < ch34x->rx_buflimit; ++i) {
res = ch34x_submit_read_urb(ch34x, i, mem_flags);
if (res)
return res;
}
return 0;
}
static void ch34x_process_read_urb(struct ch34x *ch34x, struct urb *urb)
{
if (!urb->actual_length)
return;
tty_insert_flip_string(&ch34x->port, urb->transfer_buffer,
urb->actual_length);
tty_flip_buffer_push(&ch34x->port);
}
static void ch34x_read_bulk_callback(struct urb *urb)
{
struct ch34x_rb *rb = urb->context;
struct ch34x *ch34x = rb->instance;
unsigned long flags;
int status = urb->status;
dev_vdbg(&ch34x->data->dev, "%s - urb %d, len %d\n", __func__,
rb->index, urb->actual_length);
if (!ch34x->dev) {
set_bit(rb->index, &ch34x->read_urbs_free);
dev_dbg(&ch34x->data->dev, "%s - disconnected\n", __func__);
return;
}
if (status) {
set_bit(rb->index, &ch34x->read_urbs_free);
dev_dbg(&ch34x->data->dev, "%s - non-zero urb status: %d\n",
__func__, status);
return;
}
usb_mark_last_busy(ch34x->dev);
ch34x_process_read_urb(ch34x, urb);
/*
* Unthrottle may run on another CPU which needs to see events
* in the same order. Submission has an implict barrier
*/
smp_mb__before_atomic();
set_bit(rb->index, &ch34x->read_urbs_free);
/* throttle device if requested by tty */
spin_lock_irqsave(&ch34x->read_lock, flags);
ch34x->throttled = ch34x->throttle_req;
if (!ch34x->throttled) {
spin_unlock_irqrestore(&ch34x->read_lock, flags);
ch34x_submit_read_urb(ch34x, rb->index, GFP_ATOMIC);
} else {
spin_unlock_irqrestore(&ch34x->read_lock, flags);
}
}
/* data interface wrote those outgoing bytes */
static void ch34x_write_bulk(struct urb *urb)
{
struct ch34x_wb *wb = urb->context;
struct ch34x *ch34x = wb->instance;
unsigned long flags;
int status = urb->status;
if (status || (urb->actual_length != urb->transfer_buffer_length))
dev_vdbg(&ch34x->data->dev, "%s - len %d/%d, status %d\n",
__func__,
urb->actual_length,
urb->transfer_buffer_length,
status);
spin_lock_irqsave(&ch34x->write_lock, flags);
ch34x_write_done(ch34x, wb);
spin_unlock_irqrestore(&ch34x->write_lock, flags);
schedule_work(&ch34x->work);
}
static void ch34x_softint(struct work_struct *work)
{
struct ch34x *ch34x = container_of(work, struct ch34x, work);
dev_vdbg(&ch34x->data->dev, "%s\n", __func__);
tty_port_tty_wakeup(&ch34x->port);
}
/*
* TTY handlers
*/
static int ch34x_tty_install(struct tty_driver *driver, struct tty_struct *tty)
{
struct ch34x *ch34x;
int retval;
dev_dbg(tty->dev, "%s\n", __func__);
ch34x = ch34x_get_by_minor(tty->index);
if (!ch34x)
return -ENODEV;
retval = tty_standard_install(driver, tty);
if (retval)
goto error_init_termios;
tty->driver_data = ch34x;
return 0;
error_init_termios:
tty_port_put(&ch34x->port);
return retval;
}
static int ch34x_tty_open(struct tty_struct *tty, struct file *filp)
{
struct ch34x *ch34x = tty->driver_data;
dev_dbg(tty->dev, "%s\n", __func__);
return tty_port_open(&ch34x->port, tty, filp);
}
static void ch34x_port_dtr_rts(struct tty_port *port, int raise)
{
struct ch34x *ch34x = container_of(port, struct ch34x, port);
int res;
dev_dbg(&ch34x->data->dev, "%s\n", __func__);
return;
if (raise)
ch34x->ctrlout |= CH34X_CTRL_DTR | CH34X_CTRL_RTS;
else
ch34x->ctrlout &= ~(CH34X_CTRL_DTR | CH34X_CTRL_RTS);
res = ch34x_set_control(ch34x, ch34x->ctrlout);
if (res)
dev_err(&ch34x->data->dev, "failed to set dtr/rts\n");
}
static int ch34x_port_activate(struct tty_port *port, struct tty_struct *tty)
{
struct ch34x *ch34x = container_of(port, struct ch34x, port);
int retval = -ENODEV;
dev_dbg(&ch34x->data->dev, "%s\n", __func__);
mutex_lock(&ch34x->mutex);
if (ch34x->disconnected)
goto disconnected;
retval = usb_autopm_get_interface(ch34x->data);
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);
ch34x->data->needs_remote_wakeup = 1;
retval = ch34x_configure(ch34x);
if (retval)
goto error_configure;
ch34x_tty_set_termios(tty, NULL);
/*
* Unthrottle device in case the TTY was closed while throttled.
*/
spin_lock_irq(&ch34x->read_lock);
ch34x->throttled = 0;
ch34x->throttle_req = 0;
spin_unlock_irq(&ch34x->read_lock);
usb_autopm_put_interface(ch34x->data);
mutex_unlock(&ch34x->mutex);
return 0;
error_configure:
usb_autopm_put_interface(ch34x->data);
error_get_interface:
disconnected:
mutex_unlock(&ch34x->mutex);
return usb_translate_errors(retval);
}
static void ch34x_port_destruct(struct tty_port *port)
{
struct ch34x *ch34x = container_of(port, struct ch34x, port);
dev_dbg(&ch34x->data->dev, "%s\n", __func__);
ch34x_release_minor(ch34x);
usb_put_intf(ch34x->data);
kfree(ch34x);
}
static void ch34x_port_shutdown(struct tty_port *port)
{
struct ch34x *ch34x = container_of(port, struct ch34x, port);
dev_dbg(&ch34x->data->dev, "%s\n", __func__);
}
static void ch34x_tty_cleanup(struct tty_struct *tty)
{
struct ch34x *ch34x = tty->driver_data;
dev_dbg(&ch34x->data->dev, "%s\n", __func__);
tty_port_put(&ch34x->port);
}
static void ch34x_tty_hangup(struct tty_struct *tty)
{
struct ch34x *ch34x = tty->driver_data;
dev_dbg(&ch34x->data->dev, "%s\n", __func__);
tty_port_hangup(&ch34x->port);
}
static void ch34x_tty_close(struct tty_struct *tty, struct file *filp)
{
struct ch34x *ch34x = tty->driver_data;
dev_dbg(&ch34x->data->dev, "%s\n", __func__);
tty_port_close(&ch34x->port, tty, filp);
}
static int ch34x_tty_write(struct tty_struct *tty,
const unsigned char *buf, int count)
{
struct ch34x *ch34x = tty->driver_data;
int stat;
unsigned long flags;
int wbn;
struct ch34x_wb *wb;
if (!count)
return 0;
dev_vdbg(&ch34x->data->dev, "%s - count %d\n", __func__, count);
spin_lock_irqsave(&ch34x->write_lock, flags);
wbn = ch34x_wb_alloc(ch34x);
if (wbn < 0) {
spin_unlock_irqrestore(&ch34x->write_lock, flags);
return 0;
}
wb = &ch34x->wb[wbn];
if (!ch34x->dev) {
wb->use = 0;
spin_unlock_irqrestore(&ch34x->write_lock, flags);
return -ENODEV;
}
count = (count > ch34x->writesize) ? ch34x->writesize : count;
memcpy(wb->buf, buf, count);
wb->len = count;
stat = usb_autopm_get_interface_async(ch34x->data);
if (stat) {
wb->use = 0;
spin_unlock_irqrestore(&ch34x->write_lock, flags);
return stat;
}
if (ch34x->susp_count) {
usb_anchor_urb(wb->urb, &ch34x->delayed);
spin_unlock_irqrestore(&ch34x->write_lock, flags);
return count;
}
stat = ch34x_start_wb(ch34x, wb);
spin_unlock_irqrestore(&ch34x->write_lock, flags);
if (stat < 0)
return stat;
return count;
}
static int ch34x_tty_write_room(struct tty_struct *tty)
{
struct ch34x *ch34x = tty->driver_data;
/*
* Do not let the line discipline to know that we have a reserve,
* or it might get too enthusiastic.
*/
return ch34x_wb_is_avail(ch34x) ? ch34x->writesize : 0;
}
static int ch34x_tty_chars_in_buffer(struct tty_struct *tty)
{
struct ch34x *ch34x = tty->driver_data;
/*
* if the device was unplugged then any remaining characters fell out
* of the connector ;)
*/
if (ch34x->disconnected)
return 0;
/*
* This is inaccurate (overcounts), but it works.
*/
return (CH34X_NW - ch34x_wb_is_avail(ch34x)) * ch34x->writesize;
}
static void ch34x_tty_throttle(struct tty_struct *tty)
{
struct ch34x *ch34x = tty->driver_data;
dev_dbg(&ch34x->data->dev, "%s\n", __func__);
spin_lock_irq(&ch34x->read_lock);
ch34x->throttle_req = 1;
spin_unlock_irq(&ch34x->read_lock);
}
static void ch34x_tty_unthrottle(struct tty_struct *tty)
{
struct ch34x *ch34x = tty->driver_data;
unsigned int was_throttled;
dev_dbg(&ch34x->data->dev, "%s\n", __func__);
spin_lock_irq(&ch34x->read_lock);
was_throttled = ch34x->throttled;
ch34x->throttled = 0;
ch34x->throttle_req = 0;
spin_unlock_irq(&ch34x->read_lock);
if (was_throttled)
ch34x_submit_read_urbs(ch34x, GFP_KERNEL);
}
static int ch34x_tty_break_ctl(struct tty_struct *tty, int state)
{
struct ch34x *ch34x = tty->driver_data;
int retval;
uint16_t reg_contents;
uint8_t *break_reg;
const u16 ch34x_break_reg =
((u16) CH34X_REG_LCR << 8) | CH34X_REG_BREAK;
dev_dbg(&ch34x->data->dev, "%s\n", __func__);
break_reg = kmalloc(2, GFP_KERNEL);
if (!break_reg)
return -1;
retval = ch34x_control_in(ch34x, VENDOR_READ, ch34x_break_reg, 0, break_reg, 2);
if (retval < 0) {
dev_err(&ch34x->data->dev, "%s - ch34x_control_in error (%d)\n",
__func__, retval);
goto out;
}
dev_dbg(&ch34x->data->dev, "%s - initial ch34x break register contents - reg1: %x, reg2: %x\n",
__func__, break_reg[0], break_reg[1]);
if (state != 0) {
dev_dbg(&ch34x->data->dev, "%s - Enter break state requested\n", __func__);
break_reg[0] &= ~CH34X_NBREAK_BITS;
break_reg[1] &= ~CH34X_LCR_ENABLE_TX;
} else {
dev_dbg(&ch34x->data->dev, "%s - Leave break state requested\n", __func__);
break_reg[0] |= CH34X_NBREAK_BITS;
break_reg[1] |= CH34X_LCR_ENABLE_TX;
}
dev_dbg(&ch34x->data->dev, "%s - New ch341 break register contents - reg1: %x, reg2: %x\n",
__func__, break_reg[0], break_reg[1]);
reg_contents = get_unaligned_le16(break_reg);
retval = ch34x_control_out(ch34x, VENDOR_WRITE, ch34x_break_reg,
reg_contents);
if (retval < 0)
dev_err(&ch34x->data->dev, "%s - USB control write error (%d)\n",
__func__, retval);
out:
kfree(break_reg);
return retval;
}
static int ch34x_tty_tiocmget(struct tty_struct *tty)
{
struct ch34x *ch34x = tty->driver_data;
unsigned long flags;
unsigned int result;
dev_dbg(&ch34x->data->dev, "%s\n", __func__);
spin_lock_irqsave(&ch34x->read_lock, flags);
result = (ch34x->ctrlout & CH34X_CTRL_DTR ? TIOCM_DTR : 0) |
(ch34x->ctrlout & CH34X_CTRL_RTS ? TIOCM_RTS : 0) |
(ch34x->ctrlin & CH34X_CTRL_CTS ? TIOCM_CTS : 0) |
(ch34x->ctrlin & CH34X_CTRL_DSR ? TIOCM_DSR : 0) |
(ch34x->ctrlin & CH34X_CTRL_RI ? TIOCM_RI : 0) |
(ch34x->ctrlin & CH34X_CTRL_DCD ? TIOCM_CD : 0);
spin_unlock_irqrestore(&ch34x->read_lock, flags);
dev_dbg(&ch34x->data->dev, "%s - result = %x\n", __func__, result);
return result;
}
static int ch34x_tty_tiocmset(struct tty_struct *tty,
unsigned int set, unsigned int clear)
{
struct ch34x *ch34x = tty->driver_data;
unsigned int newctrl;
dev_dbg(&ch34x->data->dev, "%s\n", __func__);
newctrl = ch34x->ctrlout;
set = (set & TIOCM_DTR ? CH34X_CTRL_DTR : 0) |
(set & TIOCM_RTS ? CH34X_CTRL_RTS : 0);
clear = (clear & TIOCM_DTR ? CH34X_CTRL_DTR : 0) |
(clear & TIOCM_RTS ? CH34X_CTRL_RTS : 0);
newctrl = (newctrl & ~clear) | set;
if (ch34x->ctrlout == newctrl)
return 0;
return ch34x_set_control(ch34x, ch34x->ctrlout = newctrl);
}
static int ch34x_tty_ioctl(struct tty_struct *tty,
unsigned int cmd, unsigned long arg)
{
// struct ch34x *ch34x = tty->driver_data;
int rv = -ENOIOCTLCMD;
switch (cmd) {
case TIOCGSERIAL: /* gets serial port data */
break;
case TIOCSSERIAL:
break;
case TIOCMIWAIT:
break;
case TIOCGICOUNT:
break;
}
return rv;
}
static int ch34x_get_baud_rate(unsigned int baud_rate,
unsigned char *factor, unsigned char *divisor)
{
unsigned char a;
unsigned char b;
unsigned long c;
switch (baud_rate) {
case 4000000:
case 800000:
if (baud_rate > 32000000 / 8 / 256) {
c = 32000000 / 8;
b = 0x4a;
} else {
c = 32000000 / 8 / 256;
b = 0x4b;
}
a = (unsigned char)(c / baud_rate);
if (a == 0 || a == 0xFF) return -EINVAL;
if ((c / a - baud_rate) > (baud_rate - c / (a + 1)))
a ++;
a = 256 - a;
break;
case 921600:
a = 0xf3;
b = 7;
break;
case 307200:
a = 0xd9;
b = 7;
break;
default:
if (baud_rate > 6000000/255) {
b = 3;
c = 6000000;
} else if (baud_rate > 750000/255) {
b = 2;
c = 750000;
} else if (baud_rate > 93750/255) {
b = 1;
c = 93750;
} else {
b = 0;
c = 11719;
}
a = (unsigned char)(c / baud_rate);
if (a == 0 || a == 0xFF) return -EINVAL;
if ((c / a - baud_rate) > (baud_rate - c / (a + 1)))
a ++;
a = 256 - a;
break;
}
*factor = a;
*divisor = b;
return 0;
}
static void ch34x_tty_set_termios(struct tty_struct *tty,
struct ktermios *termios_old)
{
struct ch34x *ch34x = tty->driver_data;
struct ktermios *termios = &tty->termios;
struct usb_cdc_line_coding newline;
int newctrl = ch34x->ctrlout;
unsigned char divisor = 0;
unsigned char reg_count = 0;
unsigned char factor = 0;
unsigned char reg_value = 0;
unsigned short value = 0;
unsigned short index = 0;
dev_dbg(tty->dev, "%s\n", __func__);
if (termios_old &&
!tty_termios_hw_change(&tty->termios, termios_old)) {
dev_dbg(tty->dev, "%s - nothing to change\n", __func__);
return;
}
newline.dwDTERate = tty_get_baud_rate(tty);