forked from juliagoda/CH341SER
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ch34x.c
1361 lines (1175 loc) · 34.6 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
// 2013.7
//********************************************
//** Copyright (C) WCH 2002-2013 ******
//** Web: http://www.winchiphead.com ******
//********************************************
//** Driver for USB to serial adaptor CH34X**
//** GCC **
//********************************************
// Support linux kernel version 2.6.25 and later
//
#include <linux/version.h>
#ifndef KERNEL_VERSION
#define KERNEL_VERSION(ver, rel, seq) ((ver << 16) | (rel << 8) | (seq))
#endif
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/tty.h>
#include <linux/tty_driver.h>
#include <linux/tty_flip.h>
#include <linux/serial.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/spinlock.h>
#include <asm/uaccess.h>
//#include <linux/uaccess.h>
#include <linux/usb.h>
#include <linux/usb/serial.h>
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,11,0)
#include <linux/signal.h>
#else
#include <linux/sched/signal.h>
#endif
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("WCH CH34x USB to serial adaptor driver");
MODULE_AUTHOR("<[email protected]>");
#define DRIVER_DESC "WCH CH34x USB to serial adaptor driver"
#define DRIVER_AUTHOR "<[email protected]>"
#define CH34x_VENDOR_ID 0x1A86
#define CH340_PRODUCT_ID 0x7523
#define CH341_PRODUCT_ID 0x5523
#define CH34x_CLOSING_WAIT (30 * HZ)
#define CH34x_BUF_SIZE 1024
#define CH34x_TMP_BUF_SIZE 1024
//Vendor define
#define VENDOR_WRITE_TYPE 0x40
#define VENDOR_READ_TYPE 0xC0
#define VENDOR_READ 0x95
#define VENDOR_WRITE 0x9A
#define VENDOR_SERIAL_INIT 0xA1
#define VENDOR_MODEM_OUT 0xA4
#define VENDOR_VERSION 0x5F
//For CMD 0xA4
#define UART_CTS 0x01
#define UART_DSR 0x02
#define UART_RING 0x04
#define UART_DCD 0x08
#define CONTROL_OUT 0x10
#define CONTROL_DTR 0x20
#define CONTROL_RTS 0x40
//Uart state
#define UART_STATE 0x00
#define UART_OVERRUN_ERROR 0x01
#define UART_BREAK_ERROR //no define
#define UART_PARITY_ERROR 0x02
#define UART_FRAME_ERROR 0x06
#define UART_RECV_ERROR 0x02
#define UART_STATE_TRANSIENT_MASK 0x07
//Port state
#define PORTA_STATE 0x01
#define PORTB_STATE 0x02
#define PORTC_STATE 0x03
//CH34x Baud Rate
#define CH34x_BAUDRATE_FACTOR 1532620800
#define CH34x_BAUDRATE_DIVMAX 3
//#define DEBUG_CH34x
#undef DEBUG_CH34x
#ifdef DEBUG_CH34x
#define dbg_ch34x( format, arg... ) \
printk( KERN_DEBUG "%d: " format "\n", __LINE__, ##arg )
#else
#define dbg_ch34x( format, arg... ) \
do{ \
if(0) \
printk(KERN_DEBUG "%d: " format "\n", __LINE__, ##arg); \
} while (0)
#endif
#ifdef DEBUG_CH34x
#define err_ch34x( format, arg... ) \
printk(KERN_ERR KBUILD_MODNAME ": " format "\n", ##arg)
#else
#define err_ch34x( format, arg... ) \
do{ \
if(0) \
printk( KERN_ERR KBUILD_MODNAME ": " format "\n", ##arg)\
}while(0)
#endif
// For debug
#if (LINUX_VERSION_CODE < KERNEL_VERSION(3,7,1))
static int debug = 1;
#endif
static DECLARE_WAIT_QUEUE_HEAD(wq);
static int wait_flag = 0;
struct ch34x_buf {
unsigned int buf_size;
char *buf_buf;
char *buf_get;
char *buf_put;
};
struct ch34x_private {
spinlock_t lock; //access lock
struct ch34x_buf *buf;
int write_urb_in_use;
unsigned baud_rate;
wait_queue_head_t delta_msr_wait;
u8 line_control;
u8 line_status;
u8 termios_initialized;
};
static struct usb_device_id id_table [] = {
{ USB_DEVICE(CH34x_VENDOR_ID, CH340_PRODUCT_ID) },
{ USB_DEVICE(CH34x_VENDOR_ID, CH341_PRODUCT_ID) },
{ } //End
};
MODULE_DEVICE_TABLE( usb, id_table );
#if (LINUX_VERSION_CODE < KERNEL_VERSION(3,5,2))
static struct usb_driver ch34x_driver = {
.name = "ch34x",
.probe = usb_serial_probe,
.disconnect = usb_serial_disconnect,
.id_table = id_table,
.suspend = usb_serial_suspend,
.resume = usb_serial_resume,
.no_dynamic_id = 1,
.supports_autosuspend = 1,
};
#endif
// ch34x_buf_alloc
// Allocate a circular buffer and all associated memory
static struct ch34x_buf *ch34x_buf_alloc( unsigned int size )
{
struct ch34x_buf *pb;
if( size == 0 )
return NULL;
pb = kmalloc( sizeof(struct ch34x_buf), GFP_KERNEL );
if( pb == NULL )
return NULL;
pb->buf_buf = kmalloc( size, GFP_KERNEL );
if( pb->buf_buf == NULL )
{
kfree(pb);
return NULL;
}
pb->buf_size = size;
pb->buf_get = pb->buf_put = pb->buf_buf;
return pb;
}
// ch34x_buf_free
// Free the buffer and all associated memory
static void ch34x_buf_free( struct ch34x_buf *pb )
{
if( pb ) {
kfree( pb->buf_buf );
kfree( pb );
}
}
// ch34x_buf_clear
// Clear out all data in the circular buffer
static void ch34x_buf_clear( struct ch34x_buf *pb )
{
if( pb != NULL )
pb->buf_get = pb->buf_put;
// equivalent to a get of all data available
}
// ch34x_buf_data_avail
// Return the number of bytes of data available in he circular buffer
static unsigned int ch34x_buf_data_avail( struct ch34x_buf *pb )
{
if( pb == NULL )
return 0;
return ((pb->buf_size + pb->buf_put - pb->buf_get) % pb->buf_size );
}
// ch34x_buf_space_avail
// Return the number of bytes of space available in the circular
static unsigned int ch34x_buf_space_avail( struct ch34x_buf *pb )
{
if( pb == NULL )
return 0;
return ((pb->buf_size + pb->buf_get - pb->buf_put - 1) % pb->buf_size );
}
// ch34x_buf_put
// Copy data from a user buffer and put it into the circular buffer.
// Restrict to the amount of space available
// Return the number of bytes copied
static unsigned int ch34x_buf_put( struct ch34x_buf *pb,
const char *buf, unsigned int count )
{
unsigned int len;
if( pb == NULL )
return 0;
len = ch34x_buf_space_avail(pb);
if( count > len ) count = len;
else if( count == 0 ) return 0;
len = pb->buf_buf + pb->buf_size - pb->buf_put;
if( count > len )
{
memcpy( pb->buf_put, buf, len );
memcpy( pb->buf_buf, buf+len, count - len );
pb->buf_put = pb->buf_buf + count - len;
}
else
{
memcpy( pb->buf_put, buf, count );
if( count < len )
pb->buf_put += count;
else if( count == len )
pb->buf_put = pb->buf_buf;
}
return count;
}
static unsigned int ch34x_buf_get( struct ch34x_buf *pb,
char *buf, unsigned int count )
{
unsigned int len;
if( pb == NULL )
return 0;
len = ch34x_buf_data_avail(pb);
if( count > len )
count = len;
else if( count == 0 )
return 0;
len = pb->buf_buf + pb->buf_size - pb->buf_get;
if( count > len )
{
memcpy( buf, pb->buf_get, len );
memcpy( buf+len, pb->buf_buf, count - len );
pb->buf_get = pb->buf_buf + count - len;
}
else
{
memcpy( buf, pb->buf_get, count );
if( count < len )
pb->buf_get += count;
else if( count == len )
pb->buf_get = pb->buf_buf;
}
return count;
}
static int ch34x_vendor_read( __u8 request,
__u16 value,
__u16 index,
struct usb_serial *serial,
unsigned char *buf,
__u16 len )
{
int retval;
retval = usb_control_msg( serial->dev, usb_rcvctrlpipe(serial->dev, 0),
request, VENDOR_READ_TYPE, value, index, buf, len, 1000 );
dbg_ch34x("0x%x:0x%x:0x%x:0x%x %d - %d",
VENDOR_READ_TYPE, request, value, index, retval, len );
return retval;
}
static int ch34x_vendor_write( __u8 request,
__u16 value,
__u16 index,
struct usb_serial *serial,
unsigned char *buf,
__u16 len )
{
int retval;
retval = usb_control_msg( serial->dev,
usb_sndctrlpipe(serial->dev, 0),
request,
VENDOR_WRITE_TYPE,
value, index, buf, len, 1000 );
return retval;
}
static int set_control_lines( struct usb_serial *serial,
u8 value )
{
int retval;
retval = ch34x_vendor_write( VENDOR_MODEM_OUT, (unsigned short)~value,
0x0000, serial, NULL, 0x00 );
dbg_ch34x("%s - value=%d, retval=%d", __func__, value, retval );
return retval;
}
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 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;
}
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27))
static void ch34x_set_termios( struct tty_struct *tty,
struct usb_serial_port *port, struct ktermios *old_termios )
{
#else
static void ch34x_set_termios( struct usb_serial_port *port,
struct ktermios *old_termios )
{
struct tty_struct *tty = port->tty;
#endif
struct usb_serial *serial = port->serial;
struct ch34x_private *priv = usb_get_serial_port_data(port);
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,1))
struct ktermios *termios = &tty->termios;
#else
struct ktermios *termios = tty->termios;
#endif
unsigned int baud_rate;
unsigned int cflag;
unsigned long flags;
u8 control;
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;
#if(LINUX_VERSION_CODE < KERNEL_VERSION(3, 11, 0))
dbg_ch34x("%s - port:%d", __func__, port->number);
#else
dbg_ch34x("%s - port:%d", __func__, port->port_number);
#endif
spin_lock_irqsave( &priv->lock, flags );
if( !priv->termios_initialized ) {
*(termios) = tty_std_termios;
termios->c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
termios->c_ispeed = 9600;
termios->c_ospeed = 9600;
priv->termios_initialized = 1;
}
spin_unlock_irqrestore( &priv->lock, flags );
/*
* The ch34x is reported to lose bytes if you change serial setting
* even to the same vaules as before. Thus we actually need to filter
* in this specific case.
*/
if( !tty_termios_hw_change(termios, old_termios) )
return;
cflag = termios->c_cflag;
#if(LINUX_VERSION_CODE < KERNEL_VERSION(3, 11, 0))
dbg_ch34x("%s (%d) cflag=0x%x\n", __func__, port->number, cflag);
#else
dbg_ch34x("%s (%d) cflag=0x%x\n", __func__, port->port_number, cflag);
#endif
// Get the byte size
switch( cflag & CSIZE )
{
case CS5:
reg_value |= 0x00;
break;
case CS6:
reg_value |= 0x01;
break;
case CS7:
reg_value |= 0x02;
break;
case CS8:
reg_value |= 0x03;
break;
default:
reg_value |= 0x03;
break;
}
dbg_ch34x("%s - data bits = %d", __func__, reg_value + 0x05 );
// Figure out the stop bits
if( cflag & CSTOPB ) {
reg_value |= 0x04;
dbg_ch34x("%s - stop bits = 2", __func__);
}
else
dbg_ch34x("%s - stop bits = 1", __func__);
// Determine the parity
if (cflag & PARENB) {
if (cflag & CMSPAR) {
if (cflag & PARODD) {
reg_value |= (0x28 | 0x00);
dbg_ch34x("%s - parity = mark", __func__);
} else {
reg_value |= (0x38 | 0x10);
dbg_ch34x("%s - parity = space", __func__);
}
} else {
if (cflag & PARODD) {
reg_value |= (0x08 | 0x00);
dbg_ch34x("%s - parity = odd", __func__);
} else {
reg_value |= (0x18 | 0x10);
dbg_ch34x("%s - parity = even", __func__);
}
}
}
else
dbg_ch34x("%s - parity = none", __func__);
// Determine the baud rate
baud_rate = tty_get_baud_rate( tty );
dbg_ch34x("%s = baud_rate = %d", __func__, baud_rate);
ch34x_get_baud_rate( baud_rate, &factor, &divisor );
dbg_ch34x("----->>>> baud_rate = %d, factor:0x%x, divisor:0x%x",
baud_rate, factor, divisor );
//enable SFR_UART RX and TX
reg_value |= 0xc0;
//enable SFR_UART Control register and timer
reg_count |= 0x9c;
value |= reg_count;
value |= (unsigned short)reg_value << 8;
index |= 0x80 | divisor;
index |= (unsigned short)factor << 8;
ch34x_vendor_write( VENDOR_SERIAL_INIT, value, index, serial, NULL, 0 );
// change control lines if we are switching to or from B0
spin_lock_irqsave( &priv->lock, flags );
control = priv->line_control;
if( (cflag & CBAUD) == B0 )
priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
else
priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
if( control != priv->line_control ) {
control = priv->line_control;
spin_unlock_irqrestore( &priv->lock, flags );
set_control_lines( serial, control );
}
else
spin_unlock_irqrestore( &priv->lock, flags );
if( cflag & CRTSCTS )
ch34x_vendor_write( VENDOR_WRITE, 0x2727, 0x0101, serial, NULL, 0);
// FIXME: Need to read back resulting baud rate
if( baud_rate )
tty_encode_baud_rate(tty, baud_rate, baud_rate);
}
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,3,3))
static int ch34x_tiocmget( struct tty_struct *tty )
{
struct usb_serial_port *port = tty->driver_data;
#elif (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27))
static int ch34x_tiocmget( struct tty_struct *tty,
struct file *filp )
{
struct usb_serial_port *port = tty->driver_data;
#else
static int ch34x_tiocmget( struct usb_serial_port *port,
struct file *filp )
{
#endif
struct ch34x_private *priv = usb_get_serial_port_data(port);
unsigned long flags;
unsigned int mcr;
unsigned int retval;
#if(LINUX_VERSION_CODE < KERNEL_VERSION(3, 11, 0))
dbg_ch34x("%s - port:%d", __func__, port->number);
#else
dbg_ch34x("%s - port:%d", __func__, port->port_number);
#endif
if( !usb_get_intfdata( port->serial->interface) )
return -ENODEV;
spin_lock_irqsave( &priv->lock, flags );
mcr = priv->line_control;
spin_unlock_irqrestore( &priv->lock, flags );
retval = ((mcr & CONTROL_DTR) ? TIOCM_DTR : 0) |
((mcr & CONTROL_RTS) ? TIOCM_RTS : 0) |
((mcr & UART_CTS) ? TIOCM_CTS : 0) |
((mcr & UART_DSR) ? TIOCM_DSR : 0) |
((mcr & UART_RING) ? TIOCM_RI : 0) |
((mcr & UART_DCD) ? TIOCM_CD : 0);
dbg_ch34x("%s - retval=0x%x", __func__, retval);
return retval;
}
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,32) && \
LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27) )
static void ch34x_close( struct tty_struct *tty,
struct usb_serial_port *port, struct file *filp )
{
#elif (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,32) )
static void ch34x_close( struct usb_serial_port *port )
{
struct tty_struct *tty = port->port.tty;
#elif (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,27))
static void ch34x_close( struct usb_serial_port *port,
struct file *filp )
{
struct tty_struct *tty = port->tty;
#endif
struct ch34x_private *priv = usb_get_serial_port_data(port);
unsigned long flags;
unsigned int c_cflag;
#if(LINUX_VERSION_CODE < KERNEL_VERSION(3, 11, 0))
dbg_ch34x("%s - port:%d", __func__, port->number);
#else
dbg_ch34x("%s - port:%d", __func__, port->port_number);
#endif
spin_lock_irqsave( &priv->lock, flags );
// clear out any remaining data in the buffer
ch34x_buf_clear( priv->buf );
spin_unlock_irqrestore( &priv->lock, flags );
// shutdown our urbs
usb_kill_urb(port->interrupt_in_urb);
usb_kill_urb(port->read_urb);
usb_kill_urb(port->write_urb);
if( tty ) {
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,1))
c_cflag = tty->termios.c_cflag;
#else
c_cflag = tty->termios->c_cflag;
#endif
if( c_cflag & HUPCL ) {
// drop DTR and RTS
spin_lock_irqsave( &priv->lock, flags );
priv->line_control = 0;
spin_unlock_irqrestore( &priv->lock, flags );
set_control_lines( port->serial, 0 );
}
}
}
// kernel version
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,32) \
&& LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27))
static int ch34x_open( struct tty_struct *tty,
struct usb_serial_port *port, struct file *filp )
{
#elif (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,32))
static int ch34x_open( struct tty_struct *tty,
struct usb_serial_port *port )
{
#elif (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,27))
static int ch34x_open( struct usb_serial_port *port,
struct file *filp )
{
struct tty_struct *tty = port->tty;
#endif
struct ktermios tmp_termios;
struct usb_serial *serial = port->serial;
int retval;
#if(LINUX_VERSION_CODE < KERNEL_VERSION(3, 11, 0))
dbg_ch34x("%s - port:%d", __func__, port->number );
#else
dbg_ch34x("%s - port:%d", __func__, port->port_number );
#endif
usb_clear_halt( serial->dev, port->write_urb->pipe );
usb_clear_halt( serial->dev, port->read_urb->pipe );
if( tty ) {
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27))
ch34x_set_termios( tty, port, &tmp_termios );
#else
ch34x_set_termios( port, &tmp_termios );
#endif
}
dbg_ch34x("%s - submit read urb", __func__);
port->read_urb->dev = serial->dev;
retval = usb_submit_urb( port->read_urb, GFP_KERNEL );
if(retval) {
dev_err( &port->dev, "%s - failed submit read urb,error %d\n",
__func__, retval );
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,32) && \
LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27) )
ch34x_close(tty, port, NULL);
#elif (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,32))
ch34x_close(port);
#elif (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,27))
ch34x_close(port, filp);
#endif
goto err_out;
}
dbg_ch34x("%s - submit interrupt urb", __func__ );
port->interrupt_in_urb->dev = serial->dev;
retval = usb_submit_urb( port->interrupt_in_urb, GFP_KERNEL );
if(retval) {
dev_err( &port->dev, "%s - failed submit interrupt urb,error %d\n",
__func__, retval );
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,32) && \
LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27) )
ch34x_close(tty, port, NULL);
#elif (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,32))
ch34x_close(port);
#elif (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,27))
ch34x_close(port, filp);
#endif
goto err_out;
}
err_out:
return retval;
}
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,3,3))
static int ch34x_tiocmset( struct tty_struct *tty,
unsigned int set, unsigned int clear )
{
struct usb_serial_port *port = tty->driver_data;
#elif (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27))
static int ch34x_tiocmset( struct tty_struct *tty,
struct file *filp, unsigned int set, unsigned int clear )
{
struct usb_serial_port *port = tty->driver_data;
#else
static int ch34x_tiocmset( struct usb_serial_port *port,
struct file *filp, unsigned int set, unsigned int clear )
{
#endif
struct ch34x_private *priv = usb_get_serial_port_data(port);
unsigned long flags;
/*unsigned int mcr = priv->line_control;*/
u8 control;
#if(LINUX_VERSION_CODE < KERNEL_VERSION(3, 11, 0))
dbg_ch34x("%s - port:%d", __func__, port->number);
#else
dbg_ch34x("%s - port:%d", __func__, port->port_number);
#endif
if( !usb_get_intfdata(port->serial->interface) )
return -ENODEV;
spin_lock_irqsave( &priv->lock, flags );
if( set & TIOCM_RTS )
priv->line_control |= CONTROL_RTS;
if( set & TIOCM_DTR )
priv->line_control |= CONTROL_DTR;
if( clear & TIOCM_RTS )
priv->line_control &= ~CONTROL_RTS;
if( clear & TIOCM_DTR )
priv->line_control &= ~CONTROL_DTR;
control = priv->line_control;
spin_unlock_irqrestore( &priv->lock, flags );
return set_control_lines( port->serial, control );
}
static int wait_modem_info( struct usb_serial_port *port,
unsigned int arg )
{
struct ch34x_private *priv = usb_get_serial_port_data(port);
unsigned long flags;
unsigned int prevstatus;
unsigned int status;
unsigned int changed;
#if(LINUX_VERSION_CODE < KERNEL_VERSION(3, 11, 0))
dbg_ch34x("%s -port:%d", __func__, port->number);
#else
dbg_ch34x("%s -port:%d", __func__, port->port_number);
#endif
spin_lock_irqsave( &priv->lock, flags );
prevstatus = priv->line_status;
spin_unlock_irqrestore( &priv->lock, flags );
while(1) {
wait_event_interruptible( wq, wait_flag != 0 );
wait_flag = 0;
// see if a signal did it
if( signal_pending(current) )
return -ERESTARTSYS;
spin_lock_irqsave( &priv->lock, flags );
status = priv->line_status;
spin_unlock_irqrestore( &priv->lock, flags );
changed = prevstatus ^ status;
if( ((arg & TIOCM_RNG) && (changed & UART_RING)) ||
((arg & TIOCM_DSR) && (changed & UART_DSR)) ||
((arg & TIOCM_CD) && (changed & UART_DCD)) ||
((arg & TIOCM_CTS) && (changed & UART_CTS)) )
return 0;
prevstatus = status;
}
// Not reatched
return 0;
}
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,3,3))
static int ch34x_ioctl( struct tty_struct *tty,
unsigned int cmd, unsigned long arg )
{
struct usb_serial_port *port = tty->driver_data;
#elif (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27))
static int ch34x_ioctl( struct tty_struct *tty,
struct file *filp, unsigned int cmd, unsigned long arg )
{
struct usb_serial_port *port = tty->driver_data;
#else
static int ch34x_ioctl( struct usb_serial_port *port,
struct file *filp, unsigned int cmd, unsigned long arg )
{
//struct usb_serial_port *port = tty->driver_data;
#endif
#if(LINUX_VERSION_CODE < KERNEL_VERSION(3, 11, 0))
dbg_ch34x("%s - port:%d, cmd=0x%04x", __func__, port->number, cmd);
#else
dbg_ch34x("%s - port:%d, cmd=0x%04x", __func__, port->port_number, cmd);
#endif
switch(cmd)
{
// Note here
case TIOCMIWAIT:
#if(LINUX_VERSION_CODE < KERNEL_VERSION(3, 11, 0))
dbg_ch34x("%s - port:%d TIOCMIWAIT", __func__, port->number);
#else
dbg_ch34x("%s - port:%d TIOCMIWAIT", __func__, port->port_number);
#endif
return wait_modem_info(port, arg);
default:
dbg_ch34x("%s not supported=0x%04x", __func__, cmd);
break;
}
return -ENOIOCTLCMD;
}
static void ch34x_send( struct usb_serial_port *port )
{
int count;
int retval;
struct ch34x_private *priv = usb_get_serial_port_data( port );
unsigned long flags;
#if(LINUX_VERSION_CODE < KERNEL_VERSION(3, 11, 0))
dbg_ch34x("%s - port:%d", __func__, port->number);
#else
dbg_ch34x("%s - port:%d", __func__, port->port_number);
#endif
spin_lock_irqsave( &priv->lock, flags );
if( priv->write_urb_in_use ) {
spin_unlock_irqrestore( &priv->lock, flags );
return;
}
count = ch34x_buf_get( priv->buf, port->write_urb->transfer_buffer,
port->bulk_out_size );
if( count == 0 ) {
spin_unlock_irqrestore( &priv->lock, flags );
return;
}
priv->write_urb_in_use = 1;
spin_unlock_irqrestore( &priv->lock, flags );
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,1))
usb_serial_debug_data( &port->dev, __func__, count,
port->write_urb->transfer_buffer );
#else
usb_serial_debug_data( debug, &port->dev, __func__, count,
port->write_urb->transfer_buffer );
#endif
port->write_urb->transfer_buffer_length = count;
port->write_urb->dev = port->serial->dev;
retval = usb_submit_urb( port->write_urb, GFP_ATOMIC );
if( retval ) {
dev_err( &port->dev, "%s - failed submitting write urb,error %d\n"
, __func__, retval );
priv->write_urb_in_use = 0;
// reschedule ch34x_send
}
usb_serial_port_softint( port );
}
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27))
static int ch34x_write( struct tty_struct *tty,
struct usb_serial_port *port, const unsigned char *buf, int count )
#else
static int ch34x_write( struct usb_serial_port *port,
const unsigned char *buf, int count )
#endif
{
struct ch34x_private *priv = usb_get_serial_port_data(port);
unsigned long flags;
#if(LINUX_VERSION_CODE < KERNEL_VERSION(3, 11, 0))
dbg_ch34x("%s - port:%d, %d bytes", __func__, port->number, count);
#else
dbg_ch34x("%s - port:%d, %d bytes", __func__, port->port_number, count);
#endif
if( !count )
return count;
spin_lock_irqsave( &priv->lock, flags );
count = ch34x_buf_put( priv->buf, buf, count );
spin_unlock_irqrestore( &priv->lock, flags );
ch34x_send(port);
return count;
}
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(5,14,0)) && GCC_VERSION >= 100100
static unsigned int ch34x_write_room( struct tty_struct *tty )
{
struct usb_serial_port *port = tty->driver_data;
#elif (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27))
static int ch34x_write_room( struct tty_struct *tty )
{
struct usb_serial_port *port = tty->driver_data;
#else
static int ch34x_write_room( struct usb_serial_port *port )
{
#endif
struct ch34x_private *priv = usb_get_serial_port_data( port );
unsigned int room = 0;
unsigned long flags;
#if(LINUX_VERSION_CODE < KERNEL_VERSION(3, 11, 0))
dbg_ch34x("%s - port:%d", __func__, port->number);
#else
dbg_ch34x("%s - port:%d", __func__, port->port_number);
#endif
spin_lock_irqsave( &priv->lock, flags );
room = ch34x_buf_space_avail( priv->buf );
spin_unlock_irqrestore( &priv->lock, flags );
dbg_ch34x("%s - room:%u", __func__, room );
return room;
}
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(5,14,0)) && GCC_VERSION >= 100100
static unsigned int ch34x_chars_in_buffer( struct tty_struct *tty )
{
struct usb_serial_port *port = tty->driver_data;
#elif (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27))
static int ch34x_chars_in_buffer( struct tty_struct *tty )
{
struct usb_serial_port *port = tty->driver_data;
#else
static int ch34x_chars_in_buffer( struct usb_serial_port *port )
{
#endif
struct ch34x_private *priv = usb_get_serial_port_data(port);
unsigned int chars = 0;
unsigned long flags;
#if(LINUX_VERSION_CODE < KERNEL_VERSION(3, 11, 0))
dbg_ch34x("%s - port:%d", __func__, port->number);
#else
dbg_ch34x("%s - port:%d", __func__, port->port_number);
#endif
spin_lock_irqsave( &priv->lock, flags );
chars = ch34x_buf_data_avail( priv->buf );
spin_unlock_irqrestore( &priv->lock, flags );
dbg_ch34x("%s - chars:%u", __func__, chars );
return chars;
}
static int ch34x_attach( struct usb_serial *serial )
{
struct ch34x_private *priv;
int i;
char buf[8];
dbg_ch34x("%s", __func__);