-
Notifications
You must be signed in to change notification settings - Fork 0
/
xr_usb_serial_hal.c
821 lines (757 loc) · 30.9 KB
/
xr_usb_serial_hal.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
/*
* 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 XR_SET_MAP_XR2280X 5
#define XR_GET_MAP_XR2280X 5
#define XR_SET_MAP_XR21B142X 0
#define XR_GET_MAP_XR21B142X 0
#define XR_SET_MAP_XR21V141X 0
#define XR_GET_MAP_XR21V141X 1
#define XR_SET_MAP_XR21B1411 0
#define XR_GET_MAP_XR21B1411 1
int xr_usb_serial_set_reg(struct xr_usb_serial *xr_usb_serial,int regnum, int value)
{
int result;
int channel = 0;
//dev_info(&xr_usb_serial->control->dev, "%s Channel:%d 0x%02x = 0x%02x\n", __func__,channel,regnum, value);
if((xr_usb_serial->DeviceProduct&0xfff0) == 0x1400)
{
int XR2280xaddr = XR2280x_FUNC_MGR_OFFSET + regnum;
result = usb_control_msg(xr_usb_serial->dev, /* usb device */
usb_sndctrlpipe(xr_usb_serial->dev, 0), /* endpoint pipe */
XR_SET_MAP_XR2280X, /* request */
USB_DIR_OUT | USB_TYPE_VENDOR, /* request_type */
value, /* request value */
XR2280xaddr, /* index */
NULL, /* data */
0, /* size */
5000); /* timeout */
}
else if((xr_usb_serial->DeviceProduct == 0x1410) ||
(xr_usb_serial->DeviceProduct == 0x1412) ||
(xr_usb_serial->DeviceProduct == 0x1414))
{
if(xr_usb_serial->channel)
channel = xr_usb_serial->channel - 1;
result = usb_control_msg(xr_usb_serial->dev, /* usb device */
usb_sndctrlpipe(xr_usb_serial->dev, 0), /* endpoint pipe */
XR_SET_MAP_XR21V141X, /* request */
USB_DIR_OUT | USB_TYPE_VENDOR, /* request_type */
value, /* request value */
regnum | (channel << 8), /* index */
NULL, /* data */
0, /* size */
5000); /* timeout */
}
else if(xr_usb_serial->DeviceProduct == 0x1411)
{
result = usb_control_msg(xr_usb_serial->dev, /* usb device */
usb_sndctrlpipe(xr_usb_serial->dev, 0), /* endpoint pipe */
XR_SET_MAP_XR21B1411, /* request */
USB_DIR_OUT | USB_TYPE_VENDOR, /* request_type */
value, /* request value */
regnum , /* index */
NULL, /* data */
0, /* size */
5000); /* timeout */
}
else if((xr_usb_serial->DeviceProduct == 0x1420)||
(xr_usb_serial->DeviceProduct == 0x1422)||
(xr_usb_serial->DeviceProduct == 0x1424))
{
channel = (xr_usb_serial->channel - 4)*2;
result = usb_control_msg(xr_usb_serial->dev, /* usb device */
usb_sndctrlpipe(xr_usb_serial->dev, 0), /* endpoint pipe */
XR_SET_MAP_XR21B142X, /* request */
USB_DIR_OUT | USB_TYPE_VENDOR | 1, /* request_type */
value, /* request value */
regnum | (channel << 8), /* index */
NULL, /* data */
0, /* size */
5000); /* timeout */
}
else
{
result = -1;
}
if(result < 0)
dev_err(&xr_usb_serial->control->dev, "%s Error:%d\n", __func__,result);
return result;
}
int xr_usb_serial_set_reg_ext(struct xr_usb_serial *xr_usb_serial,int channel,int regnum, int value)
{
int result;
int XR2280xaddr = XR2280x_FUNC_MGR_OFFSET + regnum;
//dev_info(&xr_usb_serial->control->dev, "%s channel:%d 0x%02x = 0x%02x\n", __func__,channel,regnum, value);
if((xr_usb_serial->DeviceProduct&0xfff0) == 0x1400)
{
result = usb_control_msg(xr_usb_serial->dev, /* usb device */
usb_sndctrlpipe(xr_usb_serial->dev, 0), /* endpoint pipe */
XR_SET_MAP_XR2280X, /* request */
USB_DIR_OUT | USB_TYPE_VENDOR, /* request_type */
value, /* request value */
XR2280xaddr, /* index */
NULL, /* data */
0, /* size */
5000); /* timeout */
}
else if((xr_usb_serial->DeviceProduct == 0x1410) ||
(xr_usb_serial->DeviceProduct == 0x1412) ||
(xr_usb_serial->DeviceProduct == 0x1414))
{
result = usb_control_msg(xr_usb_serial->dev, /* usb device */
usb_sndctrlpipe(xr_usb_serial->dev, 0), /* endpoint pipe */
XR_SET_MAP_XR21V141X, /* request */
USB_DIR_OUT | USB_TYPE_VENDOR, /* request_type */
value, /* request value */
regnum | (channel << 8), /* index */
NULL, /* data */
0, /* size */
5000); /* timeout */
}
else if(xr_usb_serial->DeviceProduct == 0x1411)
{
result = usb_control_msg(xr_usb_serial->dev, /* usb device */
usb_sndctrlpipe(xr_usb_serial->dev, 0), /* endpoint pipe */
XR_SET_MAP_XR21B1411, /* request */
USB_DIR_OUT | USB_TYPE_VENDOR , /* request_type */
value, /* request value */
regnum , /* index */
NULL, /* data */
0, /* size */
5000); /* timeout */
}
else if((xr_usb_serial->DeviceProduct == 0x1420)||
(xr_usb_serial->DeviceProduct == 0x1422)||
(xr_usb_serial->DeviceProduct == 0x1424))
{
result = usb_control_msg(xr_usb_serial->dev, /* usb device */
usb_sndctrlpipe(xr_usb_serial->dev, 0), /* endpoint pipe */
XR_SET_MAP_XR21B142X, /* request */
USB_DIR_OUT | USB_TYPE_VENDOR | 1, /* request_type */
value, /* request value */
regnum | (channel << 8), /* index */
NULL, /* data */
0, /* size */
5000); /* timeout */
}
else
{
result = -1;
}
if(result < 0)
dev_err(&xr_usb_serial->control->dev, "%s Error:%d\n", __func__,result);
return result;
}
int xr_usb_serial_get_reg(struct xr_usb_serial *xr_usb_serial,int regnum, short *value)
{
int result;
int channel = 0;
short *dmadata;
/* Use dynamic memory instead of statically allocated buffer
This is to avoid Error -11 (EAGAIN) for Kernel no longer accept static allocated buffer after 4.9 */
dmadata = kmalloc(2, GFP_KERNEL);
if (!dmadata)
{
dev_err(&xr_usb_serial->control->dev, "[func:%s,line:%d] error no memory allocated!! \n", __func__,__LINE__);
return -ENOMEM;
}
if((xr_usb_serial->DeviceProduct&0xfff0) == 0x1400)
{
int XR2280xaddr = XR2280x_FUNC_MGR_OFFSET + regnum;
result = usb_control_msg(xr_usb_serial->dev, /* usb device */
usb_rcvctrlpipe(xr_usb_serial->dev, 0), /* endpoint pipe */
XR_GET_MAP_XR2280X, /* request */
USB_DIR_IN | USB_TYPE_VENDOR , /* request_type */
0, /* request value */
XR2280xaddr, /* index */
dmadata, /* data */
2, /* size */
5000); /* timeout */
memcpy(value, dmadata, 2);
}
else if((xr_usb_serial->DeviceProduct == 0x1410) ||
(xr_usb_serial->DeviceProduct == 0x1412) ||
(xr_usb_serial->DeviceProduct == 0x1414))
{
if(xr_usb_serial->channel)
channel = xr_usb_serial->channel -1;
result = usb_control_msg(xr_usb_serial->dev, /* usb device */
usb_rcvctrlpipe(xr_usb_serial->dev, 0), /* endpoint pipe */
XR_GET_MAP_XR21V141X, /* request */
USB_DIR_IN | USB_TYPE_VENDOR, /* request_type */
0, /* request value */
regnum | (channel << 8), /* index */
dmadata, /* data */
1, /* size */
5000); /* timeout */
memcpy(value, dmadata, 1);
}
else if(xr_usb_serial->DeviceProduct == 0x1411)
{
result = usb_control_msg(xr_usb_serial->dev, /* usb device */
usb_rcvctrlpipe(xr_usb_serial->dev, 0), /* endpoint pipe */
XR_GET_MAP_XR21B1411, /* request */
USB_DIR_IN | USB_TYPE_VENDOR, /* request_type */
0, /* request value */
regnum, /* index */
dmadata, /* data */
2, /* size */
5000); /* timeout */
memcpy(value, dmadata, 2);
}
else if((xr_usb_serial->DeviceProduct == 0x1420)||
(xr_usb_serial->DeviceProduct == 0x1422)||
(xr_usb_serial->DeviceProduct == 0x1424))
{
channel = (xr_usb_serial->channel -4)*2;
result = usb_control_msg(xr_usb_serial->dev, /* usb device */
usb_rcvctrlpipe(xr_usb_serial->dev, 0), /* endpoint pipe */
XR_GET_MAP_XR21B142X, /* request */
USB_DIR_IN | USB_TYPE_VENDOR | 1, /* request_type */
0, /* request value */
regnum | (channel << 8), /* index */
dmadata, /* data */
2, /* size */
5000); /* timeout */
memcpy(value, dmadata, 2);
}
else
{
result = -1;
}
if(result < 0)
dev_err(&xr_usb_serial->control->dev, "%s channel:%d Reg 0x%x Error:%d\n", __func__,channel,regnum,result);
//else
//dev_info(&xr_usb_serial->control->dev, "%s channel:%d 0x%x = 0x%04x\n", __func__,channel,regnum, *value);
kfree(dmadata);
return result;
}
int xr_usb_serial_get_reg_ext(struct xr_usb_serial *xr_usb_serial,int channel,int regnum, short *value)
{
int result;
int XR2280xaddr = XR2280x_FUNC_MGR_OFFSET + regnum;
short *dmadata;
/* Use dynamic memory instead of statically allocated buffer
This is to avoid Error -11 (EAGAIN) for Kernel no longer accept static allocated buffer after 4.9 */
dmadata = kmalloc(2, GFP_KERNEL);
if (!dmadata)
{
dev_err(&xr_usb_serial->control->dev, "[func:%s,line:%d] error no memory allocated!! \n", __func__,__LINE__);
return -ENOMEM;
}
if((xr_usb_serial->DeviceProduct&0xfff0) == 0x1400)
{
result = usb_control_msg(xr_usb_serial->dev, /* usb device */
usb_rcvctrlpipe(xr_usb_serial->dev, 0), /* endpoint pipe */
XR_GET_MAP_XR2280X, /* request */
USB_DIR_IN | USB_TYPE_VENDOR , /* request_type */
0, /* request value */
XR2280xaddr, /* index */
dmadata, /* data */
2, /* size */
5000); /* timeout */
memcpy(value, dmadata, 2);
}
else if((xr_usb_serial->DeviceProduct == 0x1410) ||
(xr_usb_serial->DeviceProduct == 0x1412) ||
(xr_usb_serial->DeviceProduct == 0x1414))
{
result = usb_control_msg(xr_usb_serial->dev, /* usb device */
usb_rcvctrlpipe(xr_usb_serial->dev, 0), /* endpoint pipe */
XR_GET_MAP_XR21V141X, /* request */
USB_DIR_IN | USB_TYPE_VENDOR, /* request_type */
0, /* request value */
regnum | (channel << 8), /* index */
dmadata, /* data */
1, /* size */
5000); /* timeout */
memcpy(value, dmadata, 1);
}
else if(xr_usb_serial->DeviceProduct == 0x1411)
{
result = usb_control_msg(xr_usb_serial->dev, /* usb device */
usb_rcvctrlpipe(xr_usb_serial->dev, 0), /* endpoint pipe */
XR_GET_MAP_XR21B1411, /* request */
USB_DIR_IN | USB_TYPE_VENDOR , /* request_type */
0, /* request value */
regnum | (channel << 8), /* index */
dmadata, /* data */
2, /* size */
5000); /* timeout */
memcpy(value, dmadata, 2);
}
else if((xr_usb_serial->DeviceProduct == 0x1420)||
(xr_usb_serial->DeviceProduct == 0x1422)||
(xr_usb_serial->DeviceProduct == 0x1424))
{
result = usb_control_msg(xr_usb_serial->dev, /* usb device */
usb_rcvctrlpipe(xr_usb_serial->dev, 0), /* endpoint pipe */
XR_GET_MAP_XR21B142X, /* request */
USB_DIR_IN | USB_TYPE_VENDOR | 1, /* request_type */
0, /* request value */
regnum | (channel << 8), /* index */
dmadata, /* data */
2, /* size */
5000); /* timeout */
memcpy(value, dmadata, 2);
}
else
{
result = -1;
}
if(result < 0)
dev_err(&xr_usb_serial->control->dev, "%s Error:%d\n", __func__,result);
//else
//dev_info(&xr_usb_serial->control->dev, "%s channel:%d 0x%x = 0x%04x\n", __func__,channel,regnum, *value);
kfree(dmadata);
return result;
}
struct xr21v141x_baud_rate
{
unsigned int tx;
unsigned int rx0;
unsigned int rx1;
};
static struct xr21v141x_baud_rate xr21v141x_baud_rates[] = {
{ 0x000, 0x000, 0x000 },
{ 0x000, 0x000, 0x000 },
{ 0x100, 0x000, 0x100 },
{ 0x020, 0x400, 0x020 },
{ 0x010, 0x100, 0x010 },
{ 0x208, 0x040, 0x208 },
{ 0x104, 0x820, 0x108 },
{ 0x844, 0x210, 0x884 },
{ 0x444, 0x110, 0x444 },
{ 0x122, 0x888, 0x224 },
{ 0x912, 0x448, 0x924 },
{ 0x492, 0x248, 0x492 },
{ 0x252, 0x928, 0x292 },
{ 0X94A, 0X4A4, 0XA52 },
{ 0X52A, 0XAA4, 0X54A },
{ 0XAAA, 0x954, 0X4AA },
{ 0XAAA, 0x554, 0XAAA },
{ 0x555, 0XAD4, 0X5AA },
{ 0XB55, 0XAB4, 0X55A },
{ 0X6B5, 0X5AC, 0XB56 },
{ 0X5B5, 0XD6C, 0X6D6 },
{ 0XB6D, 0XB6A, 0XDB6 },
{ 0X76D, 0X6DA, 0XBB6 },
{ 0XEDD, 0XDDA, 0X76E },
{ 0XDDD, 0XBBA, 0XEEE },
{ 0X7BB, 0XF7A, 0XDDE },
{ 0XF7B, 0XEF6, 0X7DE },
{ 0XDF7, 0XBF6, 0XF7E },
{ 0X7F7, 0XFEE, 0XEFE },
{ 0XFDF, 0XFBE, 0X7FE },
{ 0XF7F, 0XEFE, 0XFFE },
{ 0XFFF, 0XFFE, 0XFFD },
};
#define UART_CLOCK_DIVISOR_0 0x004
#define UART_CLOCK_DIVISOR_1 0x005
#define UART_CLOCK_DIVISOR_2 0x006
#define UART_TX_CLOCK_MASK_0 0x007
#define UART_TX_CLOCK_MASK_1 0x008
#define UART_RX_CLOCK_MASK_0 0x009
#define UART_RX_CLOCK_MASK_1 0x00a
static int xr21v141x_set_baud_rate(struct xr_usb_serial *xr_usb_serial, unsigned int rate)
{
unsigned int divisor = 48000000 / rate;
unsigned int i = ((32 * 48000000) / rate) & 0x1f;
unsigned int tx_mask = xr21v141x_baud_rates[i].tx;
unsigned int rx_mask = (divisor & 1) ? xr21v141x_baud_rates[i].rx1 : xr21v141x_baud_rates[i].rx0;
//dev_info(&xr_usb_serial->control->dev, "Setting baud rate to %d: i=%u div=%u tx=%03x rx=%03x\n", rate, i, divisor, tx_mask, rx_mask);
xr_usb_serial_set_reg(xr_usb_serial,UART_CLOCK_DIVISOR_0, (divisor >> 0) & 0xff);
xr_usb_serial_set_reg(xr_usb_serial,UART_CLOCK_DIVISOR_1, (divisor >> 8) & 0xff);
xr_usb_serial_set_reg(xr_usb_serial,UART_CLOCK_DIVISOR_2, (divisor >> 16) & 0xff);
xr_usb_serial_set_reg(xr_usb_serial,UART_TX_CLOCK_MASK_0, (tx_mask >> 0) & 0xff);
xr_usb_serial_set_reg(xr_usb_serial,UART_TX_CLOCK_MASK_1, (tx_mask >> 8) & 0xff);
xr_usb_serial_set_reg(xr_usb_serial,UART_RX_CLOCK_MASK_0, (rx_mask >> 0) & 0xff);
xr_usb_serial_set_reg(xr_usb_serial,UART_RX_CLOCK_MASK_1, (rx_mask >> 8) & 0xff);
return 0;
}
/* devices aren't required to support these requests.
* the cdc xr_usb_serial descriptor tells whether they do...
*/
int xr_usb_serial_set_control(struct xr_usb_serial *xr_usb_serial, unsigned int control)
{
int ret = 0;
if((xr_usb_serial->DeviceProduct == 0x1410) ||
(xr_usb_serial->DeviceProduct == 0x1412) ||
(xr_usb_serial->DeviceProduct == 0x1414))
{
if (control & XR_USB_SERIAL_CTRL_DTR)
xr_usb_serial_set_reg(xr_usb_serial,xr_usb_serial->reg_map.uart_gpio_clr_addr, 0x08);
else
xr_usb_serial_set_reg(xr_usb_serial,xr_usb_serial->reg_map.uart_gpio_set_addr, 0x08);
if (control & XR_USB_SERIAL_CTRL_RTS)
xr_usb_serial_set_reg(xr_usb_serial,xr_usb_serial->reg_map.uart_gpio_clr_addr, 0x20);
else
xr_usb_serial_set_reg(xr_usb_serial,xr_usb_serial->reg_map.uart_gpio_set_addr, 0x20);
}
else
{
ret = xr_usb_serial_ctrl_msg(xr_usb_serial, USB_CDC_REQ_SET_CONTROL_LINE_STATE, control, NULL, 0);
}
return ret;
}
int xr_usb_serial_set_line(struct xr_usb_serial *xr_usb_serial, struct usb_cdc_line_coding* line)
{
int ret = 0;
unsigned int format_size;
unsigned int format_parity;
unsigned int format_stop;
if((xr_usb_serial->DeviceProduct == 0x1410) ||
(xr_usb_serial->DeviceProduct == 0x1412) ||
(xr_usb_serial->DeviceProduct == 0x1414))
{
xr21v141x_set_baud_rate(xr_usb_serial,le32_to_cpu(line->dwDTERate));
format_size = line->bDataBits;
format_parity = line->bParityType;
format_stop = line->bCharFormat;
xr_usb_serial_set_reg(xr_usb_serial,
xr_usb_serial->reg_map.uart_format_addr,
(format_size << 0) | (format_parity << 4) | (format_stop << 7) );
}
else
{
ret = xr_usb_serial_ctrl_msg(xr_usb_serial, USB_CDC_REQ_SET_LINE_CODING, 0, line, sizeof *(line));
}
return ret;
}
int xr_usb_serial_set_flow_mode(struct xr_usb_serial *xr_usb_serial, struct tty_struct *tty, unsigned int cflag)
{
unsigned int flow;
unsigned int gpio_mode;
if (cflag & CRTSCTS)
{
//dev_dbg(&xr_usb_serial->control->dev, "xr_usb_serial_set_flow_mode:hardware\n");
flow = UART_FLOW_MODE_HW;
gpio_mode = UART_GPIO_MODE_SEL_RTS_CTS;
}
else if (I_IXOFF(tty) || I_IXON(tty))
{
unsigned char start_char = START_CHAR(tty);
unsigned char stop_char = STOP_CHAR(tty);
//dev_dbg(&xr_usb_serial->control->dev, "xr_usb_serial_set_flow_mode:software\n");
flow = UART_FLOW_MODE_SW;
gpio_mode = UART_GPIO_MODE_SEL_GPIO;
xr_usb_serial_set_reg(xr_usb_serial, xr_usb_serial->reg_map.uart_xon_char_addr, start_char);
xr_usb_serial_set_reg(xr_usb_serial, xr_usb_serial->reg_map.uart_xoff_char_addr, stop_char);
}
else
{
//dev_dbg(&xr_usb_serial->control->dev, "xr_usb_serial_set_flow_mode:none\n");
flow = UART_FLOW_MODE_NONE;
gpio_mode = UART_GPIO_MODE_SEL_GPIO;
}
if((xr_usb_serial->DeviceProduct == 0x1420)||
(xr_usb_serial->DeviceProduct == 0x1422)||
(xr_usb_serial->DeviceProduct == 0x1424))
{
//Add support for the TXT and RXT function for 0x1420, 0x1422, 0x1424, by setting GPIO_MODE [9:8] = '11'
gpio_mode |= 0x300;
}
xr_usb_serial_set_reg(xr_usb_serial, xr_usb_serial->reg_map.uart_flow_addr, flow);
xr_usb_serial_set_reg(xr_usb_serial, xr_usb_serial->reg_map.uart_gpio_mode_addr, gpio_mode);
return 0;
}
int xr_usb_serial_send_break(struct xr_usb_serial *xr_usb_serial, int state)
{
int ret = 0;
if((xr_usb_serial->DeviceProduct == 0x1410)||
(xr_usb_serial->DeviceProduct == 0x1412)||
(xr_usb_serial->DeviceProduct == 0x1414))
{
if(state)
ret = xr_usb_serial_set_reg(xr_usb_serial,xr_usb_serial->reg_map.tx_break_addr,0xffff);
else
ret = xr_usb_serial_set_reg(xr_usb_serial,xr_usb_serial->reg_map.tx_break_addr,0);
}
else
{
ret = xr_usb_serial_ctrl_msg(xr_usb_serial, USB_CDC_REQ_SEND_BREAK, state, NULL, 0);
}
return ret;
}
#define URM_REG_BLOCK 4
#define URM_ENABLE_BASE 0x010
#define URM_ENABLE_0_TX 0x001
#define URM_ENABLE_0_RX 0x002
#define URM_RESET_RX_FIFO_BASE 0x018
#define URM_RESET_TX_FIFO_BASE 0x01C
int xr_usb_serial_enable(struct xr_usb_serial *xr_usb_serial)
{
int ret = 0;
int channel = xr_usb_serial->channel;
//dev_info(&xr_usb_serial->control->dev, "xr_usb_serial_enable channel=%d\n",channel);
if(channel)
channel--;
if((xr_usb_serial->DeviceProduct == 0x1410)||
(xr_usb_serial->DeviceProduct == 0x1412)||
(xr_usb_serial->DeviceProduct == 0x1414))
{
ret = xr_usb_serial_set_reg_ext(xr_usb_serial,URM_REG_BLOCK,URM_ENABLE_BASE + channel,URM_ENABLE_0_TX);
ret = xr_usb_serial_set_reg(xr_usb_serial,xr_usb_serial->reg_map.uart_enable_addr,UART_ENABLE_TX | UART_ENABLE_RX);
ret = xr_usb_serial_set_reg_ext(xr_usb_serial,URM_REG_BLOCK,URM_ENABLE_BASE + channel,URM_ENABLE_0_TX | URM_ENABLE_0_RX);
}
else
{
ret = xr_usb_serial_set_reg(xr_usb_serial,xr_usb_serial->reg_map.uart_enable_addr,UART_ENABLE_TX | UART_ENABLE_RX);
}
return ret;
}
int xr_usb_serial_fifo_reset(struct xr_usb_serial *xr_usb_serial)
{
int ret = 0;
int channel = xr_usb_serial->channel;
if(channel) channel--;
if((xr_usb_serial->DeviceProduct == 0x1410)||
(xr_usb_serial->DeviceProduct == 0x1412)||
(xr_usb_serial->DeviceProduct == 0x1414))
{
ret = xr_usb_serial_set_reg_ext(xr_usb_serial,URM_REG_BLOCK,URM_RESET_RX_FIFO_BASE + channel,0xff);
ret |= xr_usb_serial_set_reg_ext(xr_usb_serial,URM_REG_BLOCK,URM_RESET_TX_FIFO_BASE + channel,0xff);
}
return ret;
}
int xr_usb_serial_disable(struct xr_usb_serial *xr_usb_serial)
{
int ret = 0;
int channel = xr_usb_serial->channel;
//dev_info(&xr_usb_serial->control->dev, "xr_usb_serial_disable channel=%d\n",channel);
if(channel) channel--;
ret = xr_usb_serial_set_reg(xr_usb_serial,xr_usb_serial->reg_map.uart_enable_addr,0);
if((xr_usb_serial->DeviceProduct == 0x1410)||
(xr_usb_serial->DeviceProduct == 0x1412)||
(xr_usb_serial->DeviceProduct == 0x1414))
{
ret = xr_usb_serial_set_reg_ext(xr_usb_serial,URM_REG_BLOCK,URM_ENABLE_BASE + channel,URM_ENABLE_0_TX);
}
return ret;
}
int xr_usb_serial_set_loopback(struct xr_usb_serial *xr_usb_serial, int channel)
{
int ret = 0;
xr_usb_serial_disable(xr_usb_serial);
if((xr_usb_serial->DeviceProduct == 0x1410) ||
(xr_usb_serial->DeviceProduct == 0x1412) ||
(xr_usb_serial->DeviceProduct == 0x1414))
{
switch (channel)
{
case 0:
ret = xr_usb_serial_set_reg_ext(xr_usb_serial,channel,
xr_usb_serial->reg_map.uart_loopback_addr,0x40);
break;
case 1:
ret = xr_usb_serial_set_reg_ext(xr_usb_serial,channel,
xr_usb_serial->reg_map.uart_loopback_addr,0x41);
break;
case 2:
ret = xr_usb_serial_set_reg_ext(xr_usb_serial,channel,
xr_usb_serial->reg_map.uart_loopback_addr,0x42);
break;
case 3:
ret = xr_usb_serial_set_reg_ext(xr_usb_serial,channel,
xr_usb_serial->reg_map.uart_loopback_addr,0x43);
break;
default:
break;
}
}
else if((xr_usb_serial->DeviceProduct == 0x1420)||
(xr_usb_serial->DeviceProduct == 0x1422)||
(xr_usb_serial->DeviceProduct == 0x1424))
{
ret = xr_usb_serial_set_reg_ext(xr_usb_serial,channel,
xr_usb_serial->reg_map.uart_loopback_addr,0x07);
}
xr_usb_serial_enable(xr_usb_serial);
return ret;
}
#define XR21V1414_WIDE_MODE_OFFSET 3
#define XR21B142X_WIDE_MODE_TX_OFFSET 0x42
#define XR21B142X_WIDE_MODE_RX_OFFSET 0x45
/* XR2280x_FUNC_MGR_OFFSET will be included in xr_usb_serial_set_reg */
#define XR21B140X_WIDE_MODE_TX_OFFSET 0x22
#define XR21B140X_WIDE_MODE_RX_OFFSET 0x25
int xr_usb_serial_set_wide_mode(struct xr_usb_serial *xr_usb_serial, int preciseflags)
{
int ret = 0;
int channel = xr_usb_serial->channel;
xr_usb_serial_disable(xr_usb_serial);
if((xr_usb_serial->DeviceProduct&0xfff0) == 0x1400)
{
xr_usb_serial_set_reg(xr_usb_serial, XR21B140X_WIDE_MODE_TX_OFFSET, preciseflags);
xr_usb_serial_set_reg(xr_usb_serial, XR21B140X_WIDE_MODE_RX_OFFSET, preciseflags);
}
else if((xr_usb_serial->DeviceProduct == 0x1410)||
(xr_usb_serial->DeviceProduct == 0x1412)||
(xr_usb_serial->DeviceProduct == 0x1414))
{
if(channel) channel--;
xr_usb_serial_set_reg_ext(xr_usb_serial, 0x66, channel*8 + XR21V1414_WIDE_MODE_OFFSET, preciseflags);
}
else if(xr_usb_serial->DeviceProduct == 0x1411)
{
xr_usb_serial_set_reg(xr_usb_serial,0xd02, preciseflags);
}
else if((xr_usb_serial->DeviceProduct == 0x1420)||
(xr_usb_serial->DeviceProduct == 0x1422)||
(xr_usb_serial->DeviceProduct == 0x1424))
{
xr_usb_serial_set_reg(xr_usb_serial, XR21B142X_WIDE_MODE_TX_OFFSET, preciseflags);
xr_usb_serial_set_reg(xr_usb_serial, XR21B142X_WIDE_MODE_RX_OFFSET, preciseflags);
}
xr_usb_serial_enable(xr_usb_serial);
return ret;
}
static int xr_usb_serial_tiocmget(struct xr_usb_serial *xr_usb_serial)
{
short data;
int result;
result = xr_usb_serial_get_reg(xr_usb_serial,xr_usb_serial->reg_map.uart_gpio_status_addr, &data);
//dev_info(&xr_usb_serial->control->dev, "xr_usb_serial_tiocmget uart_gpio_status_addr:0x%04x\n",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;
}
static int xr_usb_serial_tiocmset(struct xr_usb_serial *xr_usb_serial,
unsigned int set, unsigned int clear)
{
unsigned int newctrl = 0;
newctrl = xr_usb_serial->ctrlout;
set = (set & TIOCM_DTR ? XR_USB_SERIAL_CTRL_DTR : 0) | (set & TIOCM_RTS ? XR_USB_SERIAL_CTRL_RTS : 0);
clear = (clear & TIOCM_DTR ? XR_USB_SERIAL_CTRL_DTR : 0) | (clear & TIOCM_RTS ? XR_USB_SERIAL_CTRL_RTS : 0);
newctrl = (newctrl & ~clear) | set;
if (xr_usb_serial->ctrlout == newctrl)
return 0;
xr_usb_serial->ctrlout = newctrl;
if (newctrl & XR_USB_SERIAL_CTRL_DTR)
xr_usb_serial_set_reg(xr_usb_serial, xr_usb_serial->reg_map.uart_gpio_clr_addr, 0x08);
else
xr_usb_serial_set_reg(xr_usb_serial, xr_usb_serial->reg_map.uart_gpio_set_addr, 0x08);
if (newctrl & XR_USB_SERIAL_CTRL_RTS)
xr_usb_serial_set_reg(xr_usb_serial, xr_usb_serial->reg_map.uart_gpio_clr_addr, 0x20);
else
xr_usb_serial_set_reg(xr_usb_serial, xr_usb_serial->reg_map.uart_gpio_set_addr, 0x20);
return 0;
}
static struct reg_addr_map xr21b140x_reg_map;
static struct reg_addr_map xr21b1411_reg_map;
static struct reg_addr_map xr21v141x_reg_map;
static struct reg_addr_map xr21b142x_reg_map;
static void init_xr21b140x_reg_map(void)
{
xr21b140x_reg_map.uart_enable_addr = 0x00;
xr21b140x_reg_map.uart_format_addr = 0x05;
xr21b140x_reg_map.uart_flow_addr = 0x06;
xr21b140x_reg_map.uart_loopback_addr = 0x16;
xr21b140x_reg_map.uart_xon_char_addr = 0x07;
xr21b140x_reg_map.uart_xoff_char_addr = 0x08;
xr21b140x_reg_map.uart_gpio_mode_addr = 0x0c;
xr21b140x_reg_map.uart_gpio_dir_addr = 0x0d;
xr21b140x_reg_map.uart_gpio_set_addr = 0x0e;
xr21b140x_reg_map.uart_gpio_clr_addr = 0x0f;
xr21b140x_reg_map.uart_gpio_status_addr = 0x10;
xr21b140x_reg_map.tx_break_addr = 0x0a;
xr21b140x_reg_map.uart_custom_driver = 0x41;
}
static void init_xr21b1411_reg_map(void)
{
xr21b1411_reg_map.uart_enable_addr = 0xc00;
xr21b1411_reg_map.uart_flow_addr = 0xc06;
xr21b1411_reg_map.uart_loopback_addr = 0xc16;
xr21b1411_reg_map.uart_xon_char_addr = 0xc07;
xr21b1411_reg_map.uart_xoff_char_addr = 0xc08;
xr21b1411_reg_map.uart_gpio_mode_addr = 0xc0c;
xr21b1411_reg_map.uart_gpio_dir_addr = 0xc0d;
xr21b1411_reg_map.uart_gpio_set_addr = 0xc0e;
xr21b1411_reg_map.uart_gpio_clr_addr = 0xc0f;
xr21b1411_reg_map.uart_gpio_status_addr = 0xc10;
xr21b1411_reg_map.tx_break_addr = 0xc0a;
xr21b1411_reg_map.uart_custom_driver = 0x20d;
}
static void init_xr21v141x_reg_map(void)
{
xr21v141x_reg_map.uart_enable_addr = 0x03;
xr21v141x_reg_map.uart_format_addr = 0x0b;
xr21v141x_reg_map.uart_flow_addr = 0x0c;
xr21v141x_reg_map.uart_loopback_addr = 0x12;
xr21v141x_reg_map.uart_xon_char_addr = 0x10;
xr21v141x_reg_map.uart_xoff_char_addr = 0x11;
xr21v141x_reg_map.uart_gpio_mode_addr = 0x1a;
xr21v141x_reg_map.uart_gpio_dir_addr = 0x1b;
xr21v141x_reg_map.uart_gpio_set_addr = 0x1d;
xr21v141x_reg_map.uart_gpio_clr_addr = 0x1e;
xr21v141x_reg_map.uart_gpio_status_addr = 0x1f;
xr21v141x_reg_map.tx_break_addr = 0x14;
}
static void init_xr21b142x_reg_map(void)
{
xr21b142x_reg_map.uart_enable_addr = 0x00;
xr21b142x_reg_map.uart_flow_addr = 0x06;
xr21b142x_reg_map.uart_loopback_addr = 0x16;
xr21b142x_reg_map.uart_xon_char_addr = 0x07;
xr21b142x_reg_map.uart_xoff_char_addr = 0x08;
xr21b142x_reg_map.uart_gpio_mode_addr = 0x0c;
xr21b142x_reg_map.uart_gpio_dir_addr = 0x0d;
xr21b142x_reg_map.uart_gpio_set_addr = 0x0e;
xr21b142x_reg_map.uart_gpio_clr_addr = 0x0f;
xr21b142x_reg_map.uart_gpio_status_addr = 0x10;
xr21b142x_reg_map.tx_break_addr = 0x0a;
xr21b142x_reg_map.uart_custom_driver = 0x60;
xr21b142x_reg_map.uart_low_latency = 0x46;
}
int xr_usb_serial_pre_setup(struct xr_usb_serial *xr_usb_serial)
{
int ret = 0;
init_xr21b140x_reg_map();
init_xr21b1411_reg_map();
init_xr21v141x_reg_map();
init_xr21b142x_reg_map();
if((xr_usb_serial->DeviceProduct&0xfff0) == 0x1400)
{
memcpy(&(xr_usb_serial->reg_map),&xr21b140x_reg_map,sizeof(struct reg_addr_map));
}
else if(xr_usb_serial->DeviceProduct == 0x1411)
{
memcpy(&(xr_usb_serial->reg_map),&xr21b1411_reg_map,sizeof(struct reg_addr_map));
}
else if((xr_usb_serial->DeviceProduct == 0x1410)||
(xr_usb_serial->DeviceProduct == 0x1412)||
(xr_usb_serial->DeviceProduct == 0x1414))
{
memcpy(&(xr_usb_serial->reg_map),&xr21v141x_reg_map,sizeof(struct reg_addr_map));
}
else if((xr_usb_serial->DeviceProduct == 0x1420)||
(xr_usb_serial->DeviceProduct == 0x1422)||
(xr_usb_serial->DeviceProduct == 0x1424))
{
memcpy(&(xr_usb_serial->reg_map),&xr21b142x_reg_map,sizeof(struct reg_addr_map));
}
else
{
ret = -1;
}
if(xr_usb_serial->reg_map.uart_custom_driver)
xr_usb_serial_set_reg(xr_usb_serial, xr_usb_serial->reg_map.uart_custom_driver, 1);
xr_usb_serial_set_reg(xr_usb_serial, xr_usb_serial->reg_map.uart_gpio_mode_addr, 0);
xr_usb_serial_set_reg(xr_usb_serial, xr_usb_serial->reg_map.uart_gpio_dir_addr, 0x28);
xr_usb_serial_set_reg(xr_usb_serial, xr_usb_serial->reg_map.uart_gpio_set_addr, UART_GPIO_SET_DTR | UART_GPIO_SET_RTS);
return ret;
}