-
Notifications
You must be signed in to change notification settings - Fork 6
/
hid-ft260.c
2381 lines (1980 loc) · 61.4 KB
/
hid-ft260.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
// SPDX-License-Identifier: GPL-2.0-only
/*
* FTDI FT260 USB HID to I2C/UART host bridge
*
* Copyright (c) 2021, Michael Zaidman <michaelz@xsightlabs.com>
*
* Data Sheet:
* https://www.ftdichip.com/Support/Documents/DataSheets/ICs/DS_FT260.pdf
*/
#include "hid-ids.h"
#include <linux/hidraw.h>
#include <linux/i2c.h>
#include <linux/module.h>
#include <linux/usb.h>
#include <linux/serial.h>
#include <linux/serial_core.h>
#include <linux/kfifo.h>
#include <linux/tty_flip.h>
#include <linux/minmax.h>
#include <asm/unaligned.h>
#include <linux/gpio/driver.h>
#ifdef DEBUG
static int ft260_debug = 1;
#else
static int ft260_debug;
#endif
module_param_named(debug, ft260_debug, int, 0600);
MODULE_PARM_DESC(debug, "Toggle FT260 debugging messages");
#define ft260_dbg(format, arg...) \
do { \
if (ft260_debug) \
pr_info("%s: " format, __func__, ##arg); \
} while (0)
#define FT260_REPORT_MAX_LEN (64)
#define FT260_DATA_REPORT_ID(min, len) (min + (len - 1) / 4)
#define FT260_I2C_DATA_REPORT_ID(len) \
FT260_DATA_REPORT_ID(FT260_I2C_REPORT_MIN, len)
#define FT260_UART_DATA_REPORT_ID(len) \
FT260_DATA_REPORT_ID(FT260_UART_REPORT_MIN, len)
#define FT260_WAKEUP_NEEDED_AFTER_MS (4800) /* 5s minus 200ms margin */
/*
* The ft260 input report format defines 62 bytes for the data payload, but
* when requested 62 bytes, the controller returns 60 and 2 in separate input
* reports. To achieve better performance with the multi-report read data
* transfers, we set the maximum read payload length to a multiple of 60.
* With a 100 kHz I2C clock, one 240 bytes read takes about 1/27 second,
* which is excessive; On the other hand, some higher layer drivers like at24
* or optoe limit the i2c reads to 128 bytes. To not block other drivers out
* of I2C for potentially troublesome amounts of time, we select the maximum
* read payload length to be 180 bytes.
*/
#define FT260_RD_DATA_MAX (180)
/* Time in ms to wait for a single report read data transfer completion */
#define FT260_RD_ONE_REPORT_TO (25)
/* Time in ms to wait for a multi-report read data transfer completion */
#define FT260_RD_MULTI_REPORT_TO (FT260_RD_ONE_REPORT_TO * FT260_RD_DATA_MAX / 60)
#define FT260_WR_I2C_DATA_MAX (60)
#define FT260_WR_UART_DATA_MAX (62)
#define FT260_GPIOCHIP "ft260_gpio"
#define FT260_GPIO_MAX (6)
#define FT260_GPIO_EX_MAX (8)
#define FT260_GPIO_TOTAL (FT260_GPIO_MAX + FT260_GPIO_EX_MAX)
#define FT260_GPIO_MASK (~(0xffff << FT260_GPIO_TOTAL))
/*
* Device interface configuration.
* The FT260 has 2 interfaces that are controlled by DCNF0 and DCNF1 pins.
* First implementes USB HID to I2C bridge function and
* second - USB HID to UART bridge function.
*/
enum {
FT260_MODE_ALL = 0x00,
FT260_MODE_I2C = 0x01,
FT260_MODE_UART = 0x02,
FT260_MODE_BOTH = 0x03,
};
/* Control pipe */
enum {
FT260_GET_RQST_TYPE = 0xA1,
FT260_GET_REPORT = 0x01,
FT260_SET_RQST_TYPE = 0x21,
FT260_SET_REPORT = 0x09,
FT260_FEATURE = 0x03,
};
/* Report IDs / Feature In */
enum {
FT260_CHIP_VERSION = 0xA0,
FT260_SYSTEM_SETTINGS = 0xA1,
FT260_I2C_STATUS = 0xC0,
FT260_I2C_READ_REQ = 0xC2,
FT260_I2C_REPORT_MIN = 0xD0,
FT260_I2C_REPORT_MAX = 0xDE,
FT260_GPIO = 0xB0,
FT260_UART_INTERRUPT_STATUS = 0xB1,
FT260_UART_SETTINGS = 0xE0,
FT260_UART_RI_DCD_STATUS = 0xE1,
FT260_UART_REPORT_MIN = 0xF0,
FT260_UART_REPORT_MAX = 0xFE,
};
/* Feature Out */
enum {
FT260_SET_CLOCK = 0x01,
FT260_SET_I2C_MODE = 0x02,
FT260_SET_UART_MODE = 0x03,
FT260_ENABLE_INTERRUPT = 0x05,
FT260_SELECT_GPIO2_FUNC = 0x06,
FT260_ENABLE_UART_DCD_RI = 0x07,
FT260_SELECT_GPIOA_FUNC = 0x08,
FT260_SELECT_GPIOG_FUNC = 0x09,
FT260_SET_INTERRUPT_TRIGGER = 0x0A,
FT260_SET_SUSPEND_OUT_POLAR = 0x0B,
FT260_ENABLE_UART_RI_WAKEUP = 0x0C,
FT260_SET_UART_RI_WAKEUP_CFG = 0x0D,
FT260_SET_I2C_RESET = 0x20,
FT260_SET_I2C_CLOCK_SPEED = 0x22,
FT260_SET_UART_RESET = 0x40,
FT260_SET_UART_CONFIG = 0x41,
FT260_SET_UART_BAUD_RATE = 0x42,
FT260_SET_UART_DATA_BIT = 0x43,
FT260_SET_UART_PARITY = 0x44,
FT260_SET_UART_STOP_BIT = 0x45,
FT260_SET_UART_BREAKING = 0x46,
FT260_SET_UART_XON_XOFF = 0x49,
};
/* Response codes in I2C status report */
enum {
FT260_I2C_STATUS_SUCCESS = 0x00,
FT260_I2C_STATUS_CTRL_BUSY = 0x01,
FT260_I2C_STATUS_ERROR = 0x02,
FT260_I2C_STATUS_ADDR_NO_ACK = 0x04,
FT260_I2C_STATUS_DATA_NO_ACK = 0x08,
FT260_I2C_STATUS_ARBITR_LOST = 0x10,
FT260_I2C_STATUS_CTRL_IDLE = 0x20,
FT260_I2C_STATUS_BUS_BUSY = 0x40,
};
/* I2C Conditions flags */
enum {
FT260_FLAG_NONE = 0x00,
FT260_FLAG_START = 0x02,
FT260_FLAG_START_REPEATED = 0x03,
FT260_FLAG_STOP = 0x04,
FT260_FLAG_START_STOP = 0x06,
FT260_FLAG_START_STOP_REPEATED = 0x07,
};
/* USB interface type values */
enum {
FT260_IFACE_NONE,
FT260_IFACE_I2C,
FT260_IFACE_UART
};
/* Multi-function pin functions */
enum {
FT260_MFPIN_GPIO = 0x00,
FT260_MFPIN_SUSPOUT = 0x01,
FT260_MFPIN_PWREN = 0x02,
FT260_MFPIN_TX_ACTIVE = 0x03,
FT260_MFPIN_TX_LED = 0x04,
FT260_MFPIN_RX_LED = 0x05,
FT260_MFPIN_BCD_DET = 0x06,
};
enum {
FT260_GPIO_VALUE = 0x00,
FT260_GPIO_DIRECTION = 0x01,
FT260_GPIO_DIR_INPUT = 0x00,
FT260_GPIO_DIR_OUTPUT = 0x01,
};
/* GPIO offsets */
enum {
FT260_GPIO_0 = (1 << 0),
FT260_GPIO_1 = (1 << 1),
FT260_GPIO_2 = (1 << 2),
FT260_GPIO_3 = (1 << 3),
FT260_GPIO_4 = (1 << 4),
FT260_GPIO_5 = (1 << 5),
FT260_GPIO_A = (1 << (FT260_GPIO_MAX + 0)),
FT260_GPIO_B = (1 << (FT260_GPIO_MAX + 1)),
FT260_GPIO_C = (1 << (FT260_GPIO_MAX + 2)),
FT260_GPIO_D = (1 << (FT260_GPIO_MAX + 3)),
FT260_GPIO_E = (1 << (FT260_GPIO_MAX + 4)),
FT260_GPIO_F = (1 << (FT260_GPIO_MAX + 5)),
FT260_GPIO_G = (1 << (FT260_GPIO_MAX + 6)),
FT260_GPIO_H = (1 << (FT260_GPIO_MAX + 7)),
};
/* GPIO groups */
enum {
FT260_GPIO_WAKEUP = (FT260_GPIO_3),
FT260_GPIO_I2C_DEFAULT = (FT260_GPIO_0 | FT260_GPIO_1),
FT260_GPIO_UART_RX_TX = (FT260_GPIO_C | FT260_GPIO_D),
FT260_GPIO_UART_DCD_RI = (FT260_GPIO_4 | FT260_GPIO_5),
FT260_GPIO_UART_RTS_CTS = (FT260_GPIO_B | FT260_GPIO_E),
FT260_GPIO_UART_DTR_DSR = (FT260_GPIO_F | FT260_GPIO_H),
FT260_GPIO_UART_MODE_0_SET = (FT260_GPIO_UART_RX_TX |
FT260_GPIO_UART_DCD_RI |
FT260_GPIO_UART_RTS_CTS |
FT260_GPIO_UART_DTR_DSR),
FT260_GPIO_UART_MODE_1_SET = (FT260_GPIO_UART_DTR_DSR),
FT260_GPIO_UART_MODE_2_SET = (FT260_GPIO_UART_RTS_CTS),
FT260_GPIO_UART_MODE_3_SET = (FT260_GPIO_UART_RTS_CTS |
FT260_GPIO_UART_DTR_DSR),
FT260_GPIO_UART_MODE_4_SET = (FT260_GPIO_UART_MODE_3_SET),
FT260_GPIO_UART_DEFAULT = (FT260_GPIO_UART_MODE_0_SET),
FT260_GPIO_UART_MODE_1_CLR = (FT260_GPIO_UART_RX_TX |
FT260_GPIO_UART_RTS_CTS),
FT260_GPIO_UART_MODE_2_CLR = (FT260_GPIO_UART_RX_TX |
FT260_GPIO_UART_DTR_DSR),
FT260_GPIO_UART_MODE_3_CLR = (FT260_GPIO_UART_RX_TX),
FT260_GPIO_UART_MODES = (5),
};
#define FT260_SET_REQUEST_VALUE(report_id) ((FT260_FEATURE << 8) | (report_id))
/* Feature In reports */
struct ft260_get_chip_version_report {
u8 report; /* FT260_CHIP_VERSION */
u8 chip_code[4]; /* FTDI chip identification code */
u8 reserved[8];
} __packed;
struct ft260_get_system_status_report {
u8 report; /* FT260_SYSTEM_SETTINGS */
u8 chip_mode; /* DCNF0 and DCNF1 status, bits 0-1 */
u8 clock_ctl; /* 0 - 12MHz, 1 - 24MHz, 2 - 48MHz */
u8 suspend_status; /* 0 - not suspended, 1 - suspended */
u8 pwren_status; /* 0 - FT260 is not ready, 1 - ready */
u8 i2c_enable; /* 0 - disabled, 1 - enabled */
u8 uart_mode; /* 0 - OFF; 1 - RTS_CTS, 2 - DTR_DSR, */
/* 3 - XON_XOFF, 4 - No flow control */
u8 hid_over_i2c_en; /* 0 - disabled, 1 - enabled */
u8 gpio2_func; /* 0 - GPIO, 1 - SUSPOUT, */
/* 2 - PWREN, 4 - TX_LED */
u8 gpioa_func; /* 0 - GPIO, 3 - TX_ACTIVE, 4 - TX_LED */
u8 gpiog_func; /* 0 - GPIO, 2 - PWREN, */
/* 5 - RX_LED, 6 - BCD_DET */
u8 suspend_out_pol; /* 0 - active-high, 1 - active-low */
u8 enable_wakeup_int; /* 0 - disabled, 1 - enabled */
u8 intr_cond; /* Interrupt trigger conditions */
u8 power_saving_en; /* 0 - disabled, 1 - enabled */
u8 reserved[10];
} __packed;
struct ft260_get_i2c_status_report {
u8 report; /* FT260_I2C_STATUS */
u8 bus_status; /* I2C bus status */
__le16 clock; /* I2C bus clock in range 60-3400 KHz */
u8 reserved;
} __packed;
struct ft260_get_uart_settings_report {
u8 report; /* FT260_UART_SETTINGS */
u8 flow_ctrl; /* 0 - OFF; 1 - RTS_CTS, 2 - DTR_DSR, */
/* 3 - XON_XOFF, 4 - No flow control */
/* The baudrate field is unaligned */
__le32 baudrate; /* little endian, 9600 = 0x2580, 19200 = 0x4B00 */
u8 data_bit; /* 7 or 8 */
u8 parity; /* 0: no parity, 1: odd, 2: even, 3: high, 4: low */
u8 stop_bit; /* 0: one stop bit, 2: 2 stop bits */
u8 breaking; /* 0: no break */
} __packed;
struct ft260_gpio_state {
u8 vals; /* GPIO[0-5] values in bits 0 - 5 */
u8 dirs; /* GPIO[0-5] directions, 0 - in, 1 - out */
u8 ex_vals; /* GPIO[A-H] values in bits 0 - 7 */
u8 ex_dirs; /* GPIO[A-H] directions, 0 - in, 1 - out */
} __packed;
struct ft260_gpio_read_request_report {
u8 report; /* FT260_GPIO */
struct ft260_gpio_state gpio;
} __packed;
/* Feature Out reports */
struct ft260_set_system_clock_report {
u8 report; /* FT260_SYSTEM_SETTINGS */
u8 request; /* FT260_SET_CLOCK */
u8 clock_ctl; /* 0 - 12MHz, 1 - 24MHz, 2 - 48MHz */
} __packed;
struct ft260_set_i2c_mode_report {
u8 report; /* FT260_SYSTEM_SETTINGS */
u8 request; /* FT260_SET_I2C_MODE */
u8 i2c_enable; /* 0 - disabled, 1 - enabled */
} __packed;
struct ft260_set_uart_mode_report {
u8 report; /* FT260_SYSTEM_SETTINGS */
u8 request; /* FT260_SET_UART_MODE */
u8 uart_mode; /* 0 - OFF; 1 - RTS_CTS, 2 - DTR_DSR, */
/* 3 - XON_XOFF, 4 - No flow control */
} __packed;
struct ft260_set_uart_dcd_ri_report {
u8 report; /* FT260_SYSTEM_SETTINGS */
u8 request; /* FT260_ENABLE_UART_DCD_RI */
u8 uart_dcd_ri; /* Pins func: 0 - GPIO4,GPIO5, 1 - DCD,RI */
} __packed;
struct ft260_set_i2c_reset_report {
u8 report; /* FT260_SYSTEM_SETTINGS */
u8 request; /* FT260_SET_I2C_RESET */
} __packed;
struct ft260_set_i2c_speed_report {
u8 report; /* FT260_SYSTEM_SETTINGS */
u8 request; /* FT260_SET_I2C_CLOCK_SPEED */
__le16 clock; /* I2C bus clock in range 60-3400 KHz */
} __packed;
struct ft260_set_gpio2_func_report {
u8 report; /* FT260_SYSTEM_SETTINGS */
u8 request; /* FT260_SELECT_GPIO2_FUNC */
u8 gpio2_func; /* Pin func: 0 - GPIO, 1 - SUSPOUT, */
/* 2 - PWREN# (active-low), 4 - TX_LED */
} __packed;
struct ft260_set_gpioa_func_report {
u8 report; /* FT260_SYSTEM_SETTINGS */
u8 request; /* FT260_SELECT_GPIOA_FUNC */
u8 gpioa_func; /* Pin func: 0 - GPIO, */
/* 3 - TX_ACTIVE, 4 - TX_LED */
} __packed;
struct ft260_set_gpiog_func_report {
u8 report; /* FT260_SYSTEM_SETTINGS */
u8 request; /* FT260_SELECT_GPIOG_FUNC */
u8 gpiog_func; /* Pin func: 0 - GPIO, */
/* 2 - PWREN# (active-low), */
/* 5 - RX_LED, 6 - BCD_DET */
} __packed;
struct ft260_gpio_write_request_report {
u8 report; /* FT260_GPIO */
struct ft260_gpio_state gpio;
} __packed;
/* Data transfer reports */
struct ft260_i2c_write_request_report {
u8 report; /* FT260_I2C_REPORT */
u8 address; /* 7-bit I2C address */
u8 flag; /* I2C transaction condition */
u8 length; /* data payload length */
u8 data[FT260_WR_I2C_DATA_MAX]; /* data payload */
} __packed;
struct ft260_i2c_read_request_report {
u8 report; /* FT260_I2C_READ_REQ */
u8 address; /* 7-bit I2C address */
u8 flag; /* I2C transaction condition */
__le16 length; /* data payload length */
} __packed;
struct ft260_input_report {
u8 report; /* FT260_I2C_REPORT or FT260_UART_REPORT */
u8 length; /* data payload length */
u8 data[2]; /* data payload */
} __packed;
/* UART reports */
struct ft260_uart_write_request_report {
u8 report; /* FT260_UART_REPORT */
u8 length; /* data payload length */
u8 data[FT260_WR_UART_DATA_MAX]; /* data payload */
} __packed;
struct ft260_configure_uart_request_report {
u8 report; /* FT260_SYSTEM_SETTINGS */
u8 request; /* FT260_SET_UART_CONFIG */
u8 flow_ctrl; /* 0: OFF, 1: RTS_CTS, 2: DTR_DSR */
/* 3: XON_XOFF, 4: No flow ctrl */
/* The baudrate field is unaligned */
__le32 baudrate; /* little endian, 9600 = 0x2580, 19200 = 0x4B00 */
u8 data_bit; /* 7 or 8 */
u8 parity; /* 0: no parity, 1: odd, 2: even, 3: high, 4: low */
u8 stop_bit; /* 0: one stop bit, 2: 2 stop bits */
u8 breaking; /* 0: no break */
} __packed;
/* UART interface configuration */
enum {
FT260_UART_CFG_FLOW_CTRL_OFF = 0x00,
FT260_UART_CFG_FLOW_CTRL_RTS_CTS = 0x01,
FT260_UART_CFG_FLOW_CTRL_DTR_DSR = 0x02,
FT260_UART_CFG_FLOW_CTRL_XON_XOFF = 0x03,
FT260_UART_CFG_FLOW_CTRL_NONE = 0x04,
FT260_UART_CFG_DATA_BITS_7 = 0x07,
FT260_UART_CFG_DATA_BITS_8 = 0x08,
FT260_UART_CFG_PAR_NO = 0x00,
FT260_UART_CFG_PAR_ODD = 0x01,
FT260_UART_CFG_PAR_EVEN = 0x02,
FT260_UART_CFG_PAR_HIGH = 0x03,
FT260_UART_CFG_PAR_LOW = 0x04,
FT260_UART_CFG_STOP_ONE_BIT = 0x00,
FT260_UART_CFG_STOP_TWO_BIT = 0x02,
FT260_UART_CFG_BREAKING_NO = 0x00,
FT260_UART_CFG_BEAKING_YES = 0x01,
FT260_UART_CFG_BAUD_MIN = 1200,
FT260_UART_CFG_BAUD_MAX = 12000000,
};
#define FT260_UART_EN_PW_SAVE_BAUD (4800)
#define UART_COUNT_MAX (4) /* Number of supported UARTs */
#define XMIT_FIFO_SIZE (PAGE_SIZE)
static const struct hid_device_id ft260_devices[] = {
{ HID_USB_DEVICE(USB_VENDOR_ID_FUTURE_TECHNOLOGY,
USB_DEVICE_ID_FT260) },
{ /* END OF LIST */ }
};
MODULE_DEVICE_TABLE(hid, ft260_devices);
struct ft260_device {
struct i2c_adapter adap;
struct hid_device *hdev;
int iface_type;
int iface_id;
struct list_head device_list;
struct tty_port port;
/* tty port index */
unsigned int index;
struct kfifo xmit_fifo;
spinlock_t xmit_fifo_lock;
struct uart_icount icount;
struct timer_list wakeup_timer;
struct work_struct wakeup_work;
bool reschedule_work;
bool power_saving_en;
struct completion wait;
struct mutex lock;
u8 i2c_wr_buf[FT260_REPORT_MAX_LEN];
u8 uart_wr_buf[FT260_REPORT_MAX_LEN];
unsigned long need_wakeup_at;
u8 *read_buf;
u16 read_idx;
u16 read_len;
u16 clock;
u16 gpio_en;
struct gpio_chip *gc;
struct ft260_gpio_state gpio;
u16 gpio_uart_mode[FT260_GPIO_UART_MODES];
};
static int ft260_hid_feature_report_get(struct hid_device *hdev,
u8 report_id, u8 *data, size_t len)
{
u8 *buf;
int ret;
buf = kmalloc(len, GFP_KERNEL);
if (!buf)
return -ENOMEM;
ret = hid_hw_raw_request(hdev, report_id, buf, len, HID_FEATURE_REPORT,
HID_REQ_GET_REPORT);
if (likely(ret == len))
memcpy(data, buf, len);
else if (ret >= 0)
ret = -EIO;
kfree(buf);
return ret;
}
static int ft260_hid_feature_report_set(struct hid_device *hdev, u8 *data,
size_t len)
{
u8 *buf;
int ret;
buf = kmemdup(data, len, GFP_KERNEL);
if (!buf)
return -ENOMEM;
ret = hid_hw_raw_request(hdev, buf[0], buf, len, HID_FEATURE_REPORT,
HID_REQ_SET_REPORT);
kfree(buf);
return ret;
}
static int ft260_i2c_reset(struct hid_device *hdev)
{
struct ft260_set_i2c_reset_report report;
int ret;
report.report = FT260_SYSTEM_SETTINGS;
report.request = FT260_SET_I2C_RESET;
ret = ft260_hid_feature_report_set(hdev, (u8 *)&report, sizeof(report));
if (ret < 0) {
hid_err(hdev, "failed to reset I2C controller: %d\n", ret);
return ret;
}
ft260_dbg("done\n");
return ret;
}
static int ft260_xfer_status(struct ft260_device *dev, u8 bus_busy)
{
struct hid_device *hdev = dev->hdev;
struct ft260_get_i2c_status_report report;
int ret;
if (time_is_before_jiffies(dev->need_wakeup_at)) {
ret = ft260_hid_feature_report_get(hdev, FT260_I2C_STATUS,
(u8 *)&report, sizeof(report));
if (unlikely(ret < 0)) {
hid_err(hdev, "failed to retrieve status: %d, no wakeup\n",
ret);
} else {
dev->need_wakeup_at = jiffies +
msecs_to_jiffies(FT260_WAKEUP_NEEDED_AFTER_MS);
ft260_dbg("bus_status %#02x, wakeup\n",
report.bus_status);
}
}
ret = ft260_hid_feature_report_get(hdev, FT260_I2C_STATUS,
(u8 *)&report, sizeof(report));
if (unlikely(ret < 0)) {
hid_err(hdev, "failed to retrieve status: %d\n", ret);
return ret;
}
dev->clock = le16_to_cpu(report.clock);
ft260_dbg("bus_status %#02x, clock %u\n", report.bus_status,
dev->clock);
if (report.bus_status & (FT260_I2C_STATUS_CTRL_BUSY | bus_busy))
return -EAGAIN;
/*
* The error condition (bit 1) is a status bit reflecting any
* error conditions. When any of the bits 2, 3, or 4 are raised
* to 1, bit 1 is also set to 1.
*/
if (report.bus_status & FT260_I2C_STATUS_ERROR) {
hid_err(hdev, "i2c bus error: %#02x\n", report.bus_status);
return -EIO;
}
return 0;
}
static int ft260_hid_output_report(struct hid_device *hdev, u8 *data,
size_t len)
{
u8 *buf;
int ret;
buf = kmemdup(data, len, GFP_KERNEL);
if (!buf)
return -ENOMEM;
ret = hid_hw_output_report(hdev, buf, len);
kfree(buf);
return ret;
}
static int ft260_hid_output_report_check_status(struct ft260_device *dev,
u8 *data, int len)
{
u8 bus_busy;
int ret, usec, try = 100;
struct hid_device *hdev = dev->hdev;
struct ft260_i2c_write_request_report *rep =
(struct ft260_i2c_write_request_report *)data;
ret = ft260_hid_output_report(hdev, data, len);
if (ret < 0) {
ft260_i2c_reset(hdev);
return ret;
}
/* transfer time = 1 / clock(KHz) * 9 bits * bytes */
usec = len * 9000 / dev->clock;
if (usec > 2000) {
usec -= 1500;
usleep_range(usec, usec + 100);
ft260_dbg("wait %d usec, len %d\n", usec, len);
}
/*
* Do not check the busy bit for combined transactions
* since the controller keeps the bus busy between writing
* and reading IOs to ensure an atomic operation.
*/
if (rep->flag == FT260_FLAG_START)
bus_busy = 0;
else
bus_busy = FT260_I2C_STATUS_BUS_BUSY;
do {
ret = ft260_xfer_status(dev, bus_busy);
if (ret != -EAGAIN)
break;
} while (--try);
if (ret == 0)
return 0;
ft260_i2c_reset(hdev);
return -EIO;
}
static int ft260_i2c_write(struct ft260_device *dev, u8 addr, u8 *data,
int len, u8 flag)
{
int ret, wr_len, idx = 0;
struct hid_device *hdev = dev->hdev;
struct ft260_i2c_write_request_report *rep =
(struct ft260_i2c_write_request_report *)dev->i2c_wr_buf;
if (len < 1)
return -EINVAL;
rep->flag = FT260_FLAG_START;
do {
if (len <= FT260_WR_I2C_DATA_MAX) {
wr_len = len;
if (flag == FT260_FLAG_START_STOP)
rep->flag |= FT260_FLAG_STOP;
} else {
wr_len = FT260_WR_I2C_DATA_MAX;
}
rep->report = FT260_I2C_DATA_REPORT_ID(wr_len);
rep->address = addr;
rep->length = wr_len;
memcpy(rep->data, &data[idx], wr_len);
ft260_dbg("rep %#02x addr %#02x off %d len %d wlen %d flag %#x d[0] %#02x\n",
rep->report, addr, idx, len, wr_len,
rep->flag, data[0]);
ret = ft260_hid_output_report_check_status(dev, (u8 *)rep,
wr_len + 4);
if (ret < 0) {
hid_err(hdev, "%s: failed with %d\n", __func__, ret);
return ret;
}
len -= wr_len;
idx += wr_len;
rep->flag = 0;
} while (len > 0);
return 0;
}
static int ft260_smbus_write(struct ft260_device *dev, u8 addr, u8 cmd,
u8 *data, u8 data_len, u8 flag)
{
int ret = 0;
int len = 4;
struct ft260_i2c_write_request_report *rep =
(struct ft260_i2c_write_request_report *)dev->i2c_wr_buf;
if (data_len >= sizeof(rep->data))
return -EINVAL;
rep->address = addr;
rep->data[0] = cmd;
rep->length = data_len + 1;
rep->flag = flag;
len += rep->length;
rep->report = FT260_I2C_DATA_REPORT_ID(len);
if (data_len > 0)
memcpy(&rep->data[1], data, data_len);
ft260_dbg("rep %#02x addr %#02x cmd %#02x datlen %d replen %d\n",
rep->report, addr, cmd, rep->length, len);
ret = ft260_hid_output_report_check_status(dev, (u8 *)rep, len);
if (ret < 0)
hid_err(dev->hdev, "%s: failed with %d\n", __func__, ret);
return ret;
}
static int ft260_i2c_read(struct ft260_device *dev, u8 addr, u8 *data,
u16 len, u8 flag)
{
u16 rd_len;
u16 rd_data_max = 60;
int timeout, jiffies, ret = 0;
struct ft260_i2c_read_request_report rep;
struct hid_device *hdev = dev->hdev;
u8 bus_busy = 0;
if ((flag & FT260_FLAG_START_REPEATED) == FT260_FLAG_START_REPEATED)
flag = FT260_FLAG_START_REPEATED;
else
flag = FT260_FLAG_START;
do {
if (len <= rd_data_max) {
timeout = FT260_RD_ONE_REPORT_TO;
rd_len = len;
flag |= FT260_FLAG_STOP;
} else {
timeout = FT260_RD_MULTI_REPORT_TO;
rd_len = rd_data_max;
}
rd_data_max = FT260_RD_DATA_MAX;
rep.report = FT260_I2C_READ_REQ;
rep.length = cpu_to_le16(rd_len);
rep.address = addr;
rep.flag = flag;
ft260_dbg("rep %#02x addr %#02x len %d rlen %d flag %#x\n",
rep.report, rep.address, len, rd_len, flag);
reinit_completion(&dev->wait);
dev->read_idx = 0;
dev->read_buf = data;
dev->read_len = rd_len;
ret = ft260_hid_output_report(hdev, (u8 *)&rep, sizeof(rep));
if (ret < 0) {
hid_err(hdev, "%s: failed with %d\n", __func__, ret);
goto ft260_i2c_read_exit;
}
jiffies = msecs_to_jiffies(timeout);
if (!wait_for_completion_timeout(&dev->wait, jiffies)) {
ret = -ETIMEDOUT;
ft260_i2c_reset(hdev);
goto ft260_i2c_read_exit;
}
dev->read_buf = NULL;
if (flag & FT260_FLAG_STOP)
bus_busy = FT260_I2C_STATUS_BUS_BUSY;
ret = ft260_xfer_status(dev, bus_busy);
if (ret < 0) {
ret = -EIO;
ft260_i2c_reset(hdev);
goto ft260_i2c_read_exit;
}
len -= rd_len;
data += rd_len;
flag = 0;
} while (len > 0);
ft260_i2c_read_exit:
dev->read_buf = NULL;
return ret;
}
/*
* A random read operation is implemented as a dummy write operation, followed
* by a current address read operation. The dummy write operation is used to
* load the target byte address into the current byte address counter, from
* which the subsequent current address read operation then reads.
*/
static int ft260_i2c_write_read(struct ft260_device *dev, struct i2c_msg *msgs)
{
int ret;
int wr_len = msgs[0].len;
int rd_len = msgs[1].len;
struct hid_device *hdev = dev->hdev;
u8 addr = msgs[0].addr;
u16 read_off = 0;
if (wr_len > 2) {
hid_err(hdev, "%s: invalid wr_len: %d\n", __func__, wr_len);
return -EOPNOTSUPP;
}
if (ft260_debug) {
if (wr_len == 2)
read_off = be16_to_cpu(*(__be16 *)msgs[0].buf);
else
read_off = *msgs[0].buf;
ft260_dbg("off %#x rlen %d wlen %d\n", read_off, rd_len, wr_len);
}
ret = ft260_i2c_write(dev, addr, msgs[0].buf, wr_len,
FT260_FLAG_START);
if (ret < 0)
return ret;
ret = ft260_i2c_read(dev, addr, msgs[1].buf, rd_len,
FT260_FLAG_START_STOP_REPEATED);
if (ret < 0)
return ret;
return 0;
}
static int ft260_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs,
int num)
{
int ret;
struct ft260_device *dev = i2c_get_adapdata(adapter);
struct hid_device *hdev = dev->hdev;
mutex_lock(&dev->lock);
ret = hid_hw_power(hdev, PM_HINT_FULLON);
if (ret < 0) {
hid_err(hdev, "failed to enter FULLON power mode: %d\n", ret);
mutex_unlock(&dev->lock);
return ret;
}
if (num == 1) {
if (msgs->flags & I2C_M_RD)
ret = ft260_i2c_read(dev, msgs->addr, msgs->buf,
msgs->len, FT260_FLAG_START_STOP);
else
ret = ft260_i2c_write(dev, msgs->addr, msgs->buf,
msgs->len, FT260_FLAG_START_STOP);
if (ret < 0)
goto i2c_exit;
} else {
/* Combined write then read message */
ret = ft260_i2c_write_read(dev, msgs);
if (ret < 0)
goto i2c_exit;
}
ret = num;
i2c_exit:
hid_hw_power(hdev, PM_HINT_NORMAL);
mutex_unlock(&dev->lock);
return ret;
}
static int ft260_smbus_xfer(struct i2c_adapter *adapter, u16 addr, u16 flags,
char read_write, u8 cmd, int size,
union i2c_smbus_data *data)
{
int ret;
struct ft260_device *dev = i2c_get_adapdata(adapter);
struct hid_device *hdev = dev->hdev;
ft260_dbg("smbus size %d\n", size);
mutex_lock(&dev->lock);
ret = hid_hw_power(hdev, PM_HINT_FULLON);
if (ret < 0) {
hid_err(hdev, "power management error: %d\n", ret);
mutex_unlock(&dev->lock);
return ret;
}
switch (size) {
case I2C_SMBUS_BYTE:
if (read_write == I2C_SMBUS_READ)
ret = ft260_i2c_read(dev, addr, &data->byte, 1,
FT260_FLAG_START_STOP);
else
ret = ft260_smbus_write(dev, addr, cmd, NULL, 0,
FT260_FLAG_START_STOP);
break;
case I2C_SMBUS_BYTE_DATA:
if (read_write == I2C_SMBUS_READ) {
ret = ft260_smbus_write(dev, addr, cmd, NULL, 0,
FT260_FLAG_START);
if (ret)
goto smbus_exit;
ret = ft260_i2c_read(dev, addr, &data->byte, 1,
FT260_FLAG_START_STOP_REPEATED);
} else {
ret = ft260_smbus_write(dev, addr, cmd, &data->byte, 1,
FT260_FLAG_START_STOP);
}
break;
case I2C_SMBUS_WORD_DATA:
if (read_write == I2C_SMBUS_READ) {
ret = ft260_smbus_write(dev, addr, cmd, NULL, 0,
FT260_FLAG_START);
if (ret)
goto smbus_exit;
ret = ft260_i2c_read(dev, addr, (u8 *)&data->word, 2,
FT260_FLAG_START_STOP_REPEATED);
} else {
ret = ft260_smbus_write(dev, addr, cmd,
(u8 *)&data->word, 2,
FT260_FLAG_START_STOP);
}
break;
case I2C_SMBUS_BLOCK_DATA:
if (read_write == I2C_SMBUS_READ) {
ret = ft260_smbus_write(dev, addr, cmd, NULL, 0,
FT260_FLAG_START);
if (ret)
goto smbus_exit;
ret = ft260_i2c_read(dev, addr, data->block,
data->block[0] + 1,
FT260_FLAG_START_STOP_REPEATED);
} else {
ret = ft260_smbus_write(dev, addr, cmd, data->block,
data->block[0] + 1,
FT260_FLAG_START_STOP);
}
break;
case I2C_SMBUS_I2C_BLOCK_DATA:
if (read_write == I2C_SMBUS_READ) {
ret = ft260_smbus_write(dev, addr, cmd, NULL, 0,
FT260_FLAG_START);
if (ret)
goto smbus_exit;
ret = ft260_i2c_read(dev, addr, data->block + 1,
data->block[0],
FT260_FLAG_START_STOP_REPEATED);
} else {
ret = ft260_smbus_write(dev, addr, cmd, data->block + 1,
data->block[0],
FT260_FLAG_START_STOP);
}
break;
default:
hid_err(hdev, "unsupported smbus transaction size %d\n", size);
ret = -EOPNOTSUPP;
}
smbus_exit:
hid_hw_power(hdev, PM_HINT_NORMAL);
mutex_unlock(&dev->lock);
return ret;
}
static u32 ft260_functionality(struct i2c_adapter *adap)
{
return I2C_FUNC_I2C | I2C_FUNC_SMBUS_BYTE |
I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
I2C_FUNC_SMBUS_BLOCK_DATA | I2C_FUNC_SMBUS_I2C_BLOCK;
}
static const struct i2c_adapter_quirks ft260_i2c_quirks = {
.flags = I2C_AQ_COMB_WRITE_THEN_READ,
.max_comb_1st_msg_len = 2,
};
static const struct i2c_algorithm ft260_i2c_algo = {
.master_xfer = ft260_i2c_xfer,
.smbus_xfer = ft260_smbus_xfer,
.functionality = ft260_functionality,
};
static void ft260_gpio_en_set(struct ft260_device *dev, u16 bitmap)
{
dev->gpio_en |= bitmap & FT260_GPIO_MASK;
}
static void ft260_gpio_en_clr(struct ft260_device *dev, u16 bitmap)
{
dev->gpio_en &= ~bitmap & FT260_GPIO_MASK;
}