forked from daniel-santos/mcp2210-linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmcp2210.h
1092 lines (973 loc) · 30.8 KB
/
mcp2210.h
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
/*
* MCP2210 driver for linux
*
* Copyright (c) 2013 Daniel Santos <daniel.santos@pobox.com>
* 2013 Mathew King <mking@trilithic.com> for Trilithic, Inc
*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#ifndef _MCP2210_H
#define _MCP2210_H
#ifndef __GNUC__
# error mcp2210 driver only supports gcc as compiler
#endif
#ifndef __KERNEL__
# include "out-of-tree-autoconf.h"
#endif
#ifdef __KERNEL__
# include <linux/kernel.h>
# include <linux/version.h>
# include <linux/types.h>
# include <linux/device.h>
# include <linux/mutex.h>
# include <linux/module.h>
# include <linux/bug.h>
# include <linux/usb.h>
# include <linux/spi/spi.h>
# include <linux/gpio.h>
# include <linux/timer.h>
# include <linux/jiffies.h>
# if LINUX_VERSION_CODE >= KERNEL_VERSION(3,1,0)
# include <linux/kconfig.h>
# endif
/* BUILD_BUG_ON broken somewhere prior to 3.0 */
# if LINUX_VERSION_CODE < KERNEL_VERSION(3,0,0) && defined(BUILD_BUG_ON)
# undef BUILD_BUG_ON
# endif
/* spin_is_locked broken somewhere prior to 2.6.34 */
# include <linux/spinlock.h>
# if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,34) && defined(spin_is_locked)
# undef spin_is_locked
# endif
#else
# include <stdint.h>
# include <stddef.h>
# include <stdlib.h>
# include <stdio.h>
# include <string.h>
# include <errno.h>
# include <sys/types.h>
# include <sys/ioctl.h>
# include <assert.h>
#endif /* __KERNEL__ */
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
/* hack for IS_ENABLED macro for both userspace and pre 3.1 kernels */
#ifndef IS_ENABLED
# define __ARG_PLACEHOLDER_1 0,
# define config_enabled(cfg) _config_enabled(cfg)
# define _config_enabled(value) __config_enabled(__ARG_PLACEHOLDER_##value)
# define __config_enabled(arg1_or_junk) ___config_enabled(arg1_or_junk 1, 0)
# define ___config_enabled(__ignored, val, ...) val
# define IS_ENABLED(option) \
(config_enabled(option) || config_enabled(option##_MODULE))
#endif /* IS_ENABLED */
/* if no good BUILD_BUG_ON() then just use BUG_BUG */
#ifndef BUILD_BUG_ON
# define BUILD_BUG_ON(cond) BUG_ON(cond)
#endif
#ifndef spin_is_locked
# define spin_is_locked(spinlock) 0
#endif
#ifndef GCC_VERSION
# define GCC_VERSION (__GNUC__ * 10000 \
+ __GNUC_MINOR__ * 100 \
+ __GNUC_PATCHLEVEL__)
#endif
#ifndef __compiletime_warning
# if GCC_VERSION >= 40300
# define __compiletime_warning(msg) __attribute__((warning(msg)))
# else
# define __compiletime_warning(msg)
# endif
#endif
#ifndef __KERNEL__
/* macros, typedefs & hacks for userspace compliation */
# define BUG() assert(0)
# define BUG_ON(cond) assert(!(cond))
# define EXPORT_SYMBOL_GPL(symbol)
# define le32_to_cpu(v) (v)
# define cpu_to_le32(v) (v)
# define printk(fmt, ...) fprintf(stderr, fmt, ##__VA_ARGS__)
# define KERN_ERR ""
# define KERN_WARNING ""
# define KERN_INFO ""
# define KERN_DEBUG ""
# define GFP_KERNEL 0
# define likely(cond) (cond)
# define unlikely(cond) (cond)
# define IS_ERR_VALUE(x) unlikely((unsigned long)(x) >= (unsigned long)-200)
# ifndef __always_inline
# define __always_inline inline __attribute__((always_inline))
# endif
/* userspace programs need a few typedefs */
typedef int8_t s8;
typedef int16_t s16;
typedef int32_t s32;
typedef int64_t s64;
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
#ifndef __cplusplus
typedef _Bool bool;
#endif
typedef unsigned gfp_t;
/* more userspace hacks */
static inline void *kzalloc(size_t size, unsigned flags) {
void *ret = malloc(size);
if (ret)
memset(ret, 0, size);
return ret;
}
static inline void *kmalloc(size_t size, unsigned flags){return malloc(size);}
static inline void kfree(void *ptr) {free(ptr);}
static inline void *ERR_PTR(long error) {return (void *) error;}
static inline long PTR_ERR(const void *ptr) {return (long) ptr;}
static inline long IS_ERR(const void *ptr) {return IS_ERR_VALUE((unsigned long)ptr);}
static inline long IS_ERR_OR_NULL(const void *ptr) {return !ptr || IS_ERR_VALUE((unsigned long)ptr);}
#endif /* __KERNEL__ */
#define USB_VENDOR_ID_MICROCHIP 0x04d8
#define USB_DEVICE_ID_MCP2210 0x00de
#define MCP2210_BUFFER_SIZE 64
#define MCP2210_MAX_SPEED (12 * 1000 * 1000)
#define MCP2210_MIN_SPEED 1500
#define MCP2210_EEPROM_SIZE 256
#define MCP2210_NUM_PINS 9
static const char * const urb_dir_str[] = {"out", "in"};
/****************************************************************************
* enums
*/
/**
* enum mcp2210_cmd_code - valid command codes for MCP2210
*
* Listed in order of their appearance in the datasheet with section numbers in
* comments.
*/
enum mcp2210_cmd_code {
MCP2210_CMD_SET_NVRAM = 0x60, /* 3.1.1 - 3.1.5 */
MCP2210_CMD_GET_NVRAM = 0x61, /* 3.1.6 - 3.1.10 */
MCP2210_CMD_SEND_PASSWD = 0x70, /* 3.1.11 */
MCP2210_CMD_GET_SPI_CONFIG = 0x41, /* 3.2.1 */
MCP2210_CMD_SET_SPI_CONFIG = 0x40, /* 3.2.2 */
MCP2210_CMD_GET_CHIP_CONFIG = 0x20, /* 3.2.3 */
MCP2210_CMD_SET_CHIP_CONFIG = 0x21, /* 3.2.4 */
MCP2210_CMD_GET_PIN_DIR = 0x33, /* 3.2.5 */
MCP2210_CMD_SET_PIN_DIR = 0x32, /* 3.2.6 */
MCP2210_CMD_GET_PIN_VALUE = 0x31, /* 3.2.7 */
MCP2210_CMD_SET_PIN_VALUE = 0x30, /* 3.2.8 */
MCP2210_CMD_READ_EEPROM = 0x50, /* 3.3.1 */
MCP2210_CMD_WRITE_EEPROM = 0x51, /* 3.3.2 */
MCP2210_CMD_GET_INTERRUPTS = 0x12, /* 3.4.1 */
MCP2210_CMD_SPI_TRANSFER = 0x42, /* 3.5.1 */
MCP2210_CMD_SPI_CANCEL = 0x11, /* 3.5.2 */
MCP2210_CMD_SPI_RELEASE = 0x80, /* 3.5.3 */
MCP2210_CMD_GET_STATUS = 0x10 /* 3.6.1 */
};
/**
* enum mcp2210_nvram_subcmd_code - valid subcommand codes for
*
*/
enum mcp2210_nvram_subcmd_code {
MCP2210_NVRAM_CHIP = 0x20,
MCP2210_NVRAM_SPI = 0x10,
MCP2210_NVRAM_KEY_PARAMS = 0x30,
MCP2210_NVRAM_MFG = 0x50,
MCP2210_NVRAM_PROD = 0x40
};
/**
* enum mcp2210_status - status codes retruned by MCP2210
*
* @MCP2210_STATUS_SUCCESS: No error
* @MCP2210_STATUS_SPI_NOT_OWNED: The SPI bus is not currently owned
* @MCP2210_STATUS_BUSY: Either the SPI bus or USB interface is busy.
* @MCP2210_STATUS_UNKNOWN_CMD: You sent an invalid command code.
* @MCP2210_STATUS_WRITE_FAIL: Write to EEPROM failed (uh-oh)
* @MCP2210_STATUS_NO_ACCESS: Either the device is permenantly locked or is
* password-protected and you haven't supplied
* the magic word. If you get this with
* MCP2210_CMD_SEND_PASSWD it just means that
* you've tried too many times and it wont let
* you try again until you power cycle the
* device.
* @MCP2210_STATUS_PERM_LOCKED: The device is permemently locked.
* @MCP2210_STATUS_BAD_PASSWD: You've given a bad password, but can try
* again.
*/
enum mcp2210_status {
MCP2210_STATUS_SUCCESS = 0x00,
MCP2210_STATUS_SPI_NOT_OWNED = 0xf7,
MCP2210_STATUS_BUSY = 0xf8,
MCP2210_STATUS_UNKNOWN_CMD = 0xf9,
MCP2210_STATUS_WRITE_FAIL = 0xfa,
MCP2210_STATUS_NO_ACCESS = 0xfb,
MCP2210_STATUS_PERM_LOCKED = 0xfc,
MCP2210_STATUS_BAD_PASSWD = 0xfd,
};
enum mcp2210_pin_mode {
MCP2210_PIN_GPIO = 0,
MCP2210_PIN_SPI = 1,
MCP2210_PIN_DEDICATED = 2,
};
enum mcp2210_gpio_direction {
MCP2210_GPIO_NO_CHANGE = -1,
MCP2210_GPIO_OUTPUT = 0,
MCP2210_GPIO_INPUT = 1
};
enum mcp2210_eeprom_status {
MCP2210_EEPROM_UNREAD = 0,
MCP2210_EEPROM_READ_PENDING = 1,
MCP2210_EEPROM_READ = 2,
MCP2210_EEPROM_DIRTY = 3
};
enum mcp2210_cmd_type_id {
MCP2210_CMD_TYPE_CTL,
MCP2210_CMD_TYPE_SPI,
MCP2210_CMD_TYPE_EEPROM,
MCP2210_CMD_TYPE_MAX
};
/**
* enum mcp2210_state - represents the state of both URBs and commands
*/
enum mcp2210_urb_cmd_state {
MCP2210_STATE_NEW,
MCP2210_STATE_SUBMITTED,
MCP2210_STATE_COMPLETE,
MCP2210_STATE_DEAD
};
enum mcp2210_endpoints {
EP_OUT = 0,
EP_IN = 1
};
/******************************************************************************
* Packed structs
*
* These structs represent portions of the MCP2210's wire protocol. Endianness
* is set to little prior to transmission in ctl_submit_prepare() and back to
* the native endianess in ctl_complete_urb().
*/
#if 0
#define STRUCT_PREFIX mcp2210
#include "mcp2210-structs.h"
#undef STRUCT_PREFIX
#define STRUCT_PREFIX packed_mcp2210
#pragma pack(1)
#include "mcp2210-structs.h"
#pragma pack()
#endif
/**
* mcp2210_msg - A USB message sent to or received from an MCP2210
*
* @body.chip Body for "Chip Settings" (3.1.7, 3.1.1, 3.2.3, and 3.2.4).
* @body.gpio Body for "GPIO Current Pin Direction" (3.2.5, 3.2.6) and "GIPO
* Current Pin Value" (3.2.7, 3.2.8)
* @body.spi Body for "SPI Transfer Settings" (3.1.6, 3.1.2, 3.1.2, and
* 3.2.2).
* @body.get_usb_params Body for "USB Power-Up Key Parameters" (3.1.8, 3.1.3)
* @body.set_usb_params Body for "USB Power-Up Key Parameters" (3.1.8, 3.1.3)
* @body.usb_string Body for "USB Manufacturer Name" and "USB Product Name" (3.1.9,
* 3.1.4, 3.1.10, 3.1.5)
* @body.password Body for "Send Access Password" (3.1.11)
* @body.data Body for SPI data tx & rx
*
* The entire mcp2210_msg should be zeroed prior to populating fields. Prior to
* submitting endianness of all fields (over one byte in size) should be'
* converted from CPU to little endian and after recieving, the reverse.
*
* @see http://ww1.microchip.com/downloads/en/DeviceDoc/22288A.pdf
*
*/
#pragma pack(1)
struct mcp2210_msg {
u8 cmd;
union {
/* Request headers */
union {
/* Request header for all "normal" set and get commands
* (excepting MCP2210_CMD_GET_INTERRUPTS and
* MCP2210_CMD_GET_STATUS) */
struct {
u8 sub_cmd;
u16 reserved;
} xet;
/* Request header for reads & writes to EEPROM */
struct {
u8 addr;
u8 value;
u8 reserved;
} eeprom;
/* Request header for SPI operations */
struct {
u8 size;
u16 reserved;
} spi;
/* External Interrupt Pin (GP6) Event Status */
struct {
u8 reset;
u16 reserved;
} intr;
} req;
/* Response headers */
union {
u8 status;
/* Request header for all "normal" set and get commands
* (excepting MCP2210_CMD_GET_INTERRUPTS and
* MCP2210_CMD_GET_STATUS) */
struct {
u8 status;
u8 sub_cmd;
u8 reserved;
} xet;
/* Response header for reads & writes to EEPROM */
struct {
u8 status;
u8 addr;
u8 value;
} eeprom;
/* Response header for SPI messages */
struct {
u8 status;
u8 size;
u8 spi_status;
} spi;
/* Response header for SPI cancel & status */
struct {
u8 status;
u8 release_external_req_status;
u8 current_bus_owner;
} spi_status;
} rep;
u8 raw[3];
} head;
union mcp2210_msg_body {
struct mcp2210_chip_settings {
u8 pin_mode[MCP2210_NUM_PINS];
u16 gpio_value;
u16 gpio_direction;
u8 other_settings;
u8 nvram_access_control;
u8 password[8];
/* u8 reserved[37]; */
} chip;
u16 gpio;
/**
* struct mcp2210_spi_xfer_settings
* @bitrate: Bits per second. A valid value is between 1500
* (0x5dc) and 12,000,000 (0xb71b00).
* @idle_cs: The idle chip select values for pins 0-8:
* x x x x x x x 8 7 6 5 4 3 2 1 0
* @active_cs: The active chip select values for pins 0-8:
* x x x x x x x 8 7 6 5 4 3 2 1 0
* @delay_cs_to_data: Delay between CS assertion and first data byte in
* multiples of 100 uS (from zero to 6.5536 seconds).
* @delay_last_byte_to_cs: Delay between last byte and CS de-assertion in
* multiples of 100 uS.
* @delay_between_bytes: Delay between data bytes in multiples of 100 uS.
* @bytes_per_trans: Number of bytes to transfer per-transaction.
* @mode: SPI Mode 0-3.
*
* When representing the values for SPI communications (either sending or
* receiving the SPI transfer settings), the idle_cs and active_cs are as
* advertised -- what the values of each pin should be when innactive and
* active.
*/
struct mcp2210_spi_xfer_settings {
u32 bitrate;
u16 idle_cs;
u16 active_cs;
u16 cs_to_data_delay;
u16 last_byte_to_cs_delay;
u16 delay_between_bytes;
u16 bytes_per_trans;
u8 mode;
/* u8 reserved[43]; */
} spi;
struct mcp2210_usb_key_params {
u16 vid;
u16 pid;
u8 chip_power_option;
u8 requested_power;
/* u8 reserved[54]; */
} set_usb_params;
u8 password[8];
u16 interrupt_event_counter;
u8 raw[60];
struct {
u8 reserved0[8];
u16 vid;
u16 pid;
u8 reserved1[13];
u8 chip_power_option;
u8 requested_power;
/* u8 reserved2[33]; */
} get_usb_params;
struct {
u8 length;
u8 id;
u8 data[58];
} usb_string;
struct {
u8 num_pwd_guesses;
u8 pwd_guessed;
} spi_status;
} body;
};
#pragma pack()
/**
* mcp2210_pin_config - Configuration for a single multi-purpose pin output
*
* @mode: How this pin will be used. Should be one of
* enum mcp2210_pin_mode
* @device_pn: SPI: The part number of the device connected to this PIN.
* @device_name: SPI: A more descriptive name of the device
*
* The MCP2210 has 9 pins that can be used for either General Purpose I/O
* (GPIO), SPI Chip Select (CS) or some dedicated function specific to the pin.
* This struct represnts both the hardware configuration of that pin as well as
* the values for how SPI communications to devices connected to that pin are
* done.
*/
struct mcp2210_pin_config {
u8 mode:2;
u8 has_irq:1;
u8 irq:3;
u8 irq_type:2;
/**
* @max_speed_hz: SPI: Max bits per second (must be 1500-12000000)
* @max_speed_hz: SPI: Max bits per second (must be 1500-12000000)
* @mode: the struct spi_device mode
* @manual_cs: chip select will be managed vi gpio
* @bits_per_word: better be 8
* @use_cs_gpio: Use a gpio for chip select insetad of the device's
* built-in mechanism
* @cs_gpio: Only when use_cs_gpio is set. The gpio (offset on this
* MCP2210) to use for chip select.
* @cs_to_data_delay SPI: Chip select to data start delay as a quanta of
* 100 us (i.e., in hundreds of microseconds)
* @last_byte_to_cs_delay SPI: Delay bewteen last byte of data and CS
* de-activation (x 100us)
* @delay_between_bytes SPI: Delay bewteen data bytes (x 100us)
* @delay_between_xfers Delay between transfers (x 100us)
*/
struct mcp2210_pin_config_spi {
u32 max_speed_hz;
u32 min_speed_hz;
u8 mode;
u8 bits_per_word;
u8 use_cs_gpio:1;
u8 cs_gpio;
u16 cs_to_data_delay;
u16 last_byte_to_cs_delay;
u16 delay_between_bytes;
u16 delay_between_xfers;
} spi;
const char *name;
const char *modalias;
/*const char *desc; */
};
struct mcp2210_board_config {
const char *dev_name;
struct mcp2210_pin_config pins[MCP2210_NUM_PINS];
u32 poll_gpio_usecs;
u32 stale_gpio_usecs;
u32 poll_intr_usecs;
u32 stale_intr_usecs;
u8 _3wire_capable:1;
u8 _3wire_tx_enable_active_high:1;
u8 _3wire_tx_enable_pin;
size_t strings_size;
#ifndef __cplusplus
/* const invokes gcc bug #57977 */
const
#endif
char strings[0];
};
/*
* @spi_delay_per_kb: usecs / get_timing_quanta()
*/
struct mcp2210_state {
u8 have_chip_settings:1;
u8 have_power_up_chip_settings:1;
u8 have_spi_settings:1;
u8 have_power_up_spi_settings:1;
u8 have_usb_key_params:1;
u8 have_config:1;
u8 is_spi_probed:1;
u8 is_gpio_probed:1;
u8 is_irq_probed:1;
u8 poll_intr:1;
u8 poll_gpio:1;
// u8 chip_settings_dirty:1;
struct mcp2210_chip_settings chip_settings;
struct mcp2210_chip_settings power_up_chip_settings;
struct mcp2210_spi_xfer_settings spi_settings;
struct mcp2210_spi_xfer_settings power_up_spi_settings;
struct mcp2210_usb_key_params usb_key_params;
int cur_spi_config;
u16 idle_cs;
u16 active_cs;
unsigned long spi_delay_per_kb;
unsigned long last_poll_gpio;
unsigned long last_poll_intr;
//u16 gpio;
u16 interrupt_event_counter;
};
#ifdef __KERNEL__
/* module parameters */
extern int debug_level;
extern int creek_enabled;
extern int dump_urbs;
extern int dump_commands;
extern uint pending_bytes_wait_threshold;
struct mcp2210_cmd;
struct mcp2210_cmd_type {
enum mcp2210_cmd_type_id id;
int (*submit_prepare)(struct mcp2210_cmd *cmd_head);
int (*complete_urb)(struct mcp2210_cmd *cmd_head);
int (*mcp_error)(struct mcp2210_cmd *cmd_head);
void (*dump)(const char *level, unsigned indent, const char *start,
const struct mcp2210_cmd *cmd_head);
const char *desc;
};
typedef int (*mcp2210_complete_t)(struct mcp2210_cmd *cmd, void *context);
/** struct mcp2210_cmd
* @dev:
* @type: The type of command or NULL if this is a general-
* purpose (non-USB transaction) command that will execute
* when it complete() is called.
* @node: private
* @time_queued: Time (in jiffies) comand was queued
* @time_started: Time (in jiffies) comand processing began
* @delay_until: Time (in jiffies) this command should be executed
* (ignored unless delayed is set)
* @status: Status code (zero or a negative error)
* @mcp_status: The MCP status code (initially zero)
* @state: State of the command (enum mcp2210_urb_cmd_state)
* @kill: True if this command is being killed
* @can_retry: True if this command has no side-effects (like querying
* the chip's settings) and can be retried if the USB
* transaction fails due to IO errors or shitty drivers.
* @delayed: True if this delay_until should be respected.
* @nonatomic: True if this command must be executed in a non-atomic
* context.
* @repeat_count: Number of times this command has been repeated.
* @complete: A complete function
* @context: The complete function's context
*
*/
struct mcp2210_cmd {
struct mcp2210_device *dev;
const struct mcp2210_cmd_type *type;
struct list_head node;
unsigned long time_queued;
unsigned long time_started;
unsigned long delay_until;
int status;
u8 mcp_status;
u8 state:2;
u8 can_retry:1;
u8 delayed:1;
u8 nonatomic:1;
u8 non_preemptable:1;
uint repeat_count;
mcp2210_complete_t complete;
void *context;
u8 data[0];
};
struct mcp2210_cmd_ctl {
struct mcp2210_cmd head;
struct mcp2210_msg req;
u8 pin;
u8 is_mcp_endianness:1;
};
/**
* struct mcp2210_cmd_spi_msg -
* @spi: The spi_device
* @msg: The spi_message
* @xfer: The current spi_transfer
* @pos: The number of bytes sucessfully xfered to/from the
* peripheral.
* @pending_bytes: The number of bytes sent to the MCP2210 (acked and
* unacked), but not yet transferred to the SPI device.
* @pending_unacked: The number of un-acked bytes sent to the MCP2210.
* @busy_count: The number of times the device has given us 0xf8
* sending this message
* @spi_in_flight: Set if this command is doing an SPI transfer (not a
* dupliate of from dev->spi_in_flight!)
* @ctl_cmd: A pointer to dev->ctl_cmd if we are either changing the
* SPI transfer settings for or cancling a previously
* failed SPI message.
*/
struct mcp2210_cmd_spi_msg {
struct mcp2210_cmd head;
struct spi_device *spi;
struct spi_message *msg;
struct spi_transfer *xfer;
uint pos;
u16 pending_bytes;
u16 pending_unacked;
uint busy_count;
u8 spi_in_flight:1;
struct mcp2210_cmd_ctl *ctl_cmd;
};
struct mcp2210_cmd_eeprom {
struct mcp2210_cmd head;
u8 op;
u8 addr;
u8 zero_tail;
u16 size;
};
union mcp2210_cmd_any {
struct mcp2210_cmd *head;
struct mcp2210_cmd_ctl *ctl;
struct mcp2210_cmd_spi_msg *spi;
struct mcp2210_cmd_eeprom *eeprom;
};
/**
* struct mcp2210_endpoint - a struct for comm with an endpoint
*
* @ep: endpoint struct
* @urb: The URB that is re-used for each request.
* @buffer: Coherent DMA-able buffer
* @submit_time: time URB was submitted (in jiffies)
* @state:
* @kill: non-zero if URB is being killed
* @retry_count: used only with CONFIG_MCP2210_USB_QUIRKS
* @is_dir_in: The direction of this endpoint
*/
struct mcp2210_endpoint {
const struct usb_host_endpoint *ep;
struct urb *urb;
struct mcp2210_msg *buffer;
unsigned long submit_time;
atomic_t unlink_in_process;
u8 state;
u8 is_mcp_endianness:1;
u8 kill:1;
u8 is_dir_in:1;
u8 retry_count;
};
/**
* struct mcp2210_device -
*
* @intf:
* @udev:
* @int_in_ep:
* @int_out_ep:
*
* @io_mutex: Lock for current USB I/O operations. Lock for access to cur_cmd and reqest_buffer.
* @cur_cmd: The current command in-progress
* @requeust_buffer: rx/tx buffer
*
* @cmd_queue: FIFO command queue
* @cmd_queue_mutex: mutex for manipulating cmd_queue
*
* @dead: We've encountered an error and we're just giving up on everything (for now)....
* @pi_in_flight: An SPI message is in process (or even hung)
* @ctl_cmd: A re-usable control command struct (can be used by cur_cmd only)
* @have_chip_settings: @chip_settings accurately relfects the state of the device
* @have_spi_settings: @spi_settings accurately relfects the state of the device
* @chip_settings: Current chip settings (native endianness)
* @spi_settings: The current SPI transfer settings with values stored in native endianness
* @config: (known) pin configuration data for this device
* @cur_spi_config: The current SPI device that we are configured to communicate with. Should be 0-8 or -1 for none.
* @idle_cs: calculated value for when all chips are idle
*
* @eeprom_state: A 2 bit map of the state of each byte in eeprom_cache. 00 = unread, 01 = read pending (unread), 10 = read, 11 = dirty (write pending).
* @eeprom_cache: Cached EEPROM values.
*
* Unfortunately, since reads & writes of the 256 bytes of user EEPROM are a
* byte at a time, we have to track the state of each byte separately in
* eeprom_state.
*
* Lock order:
* dev_spinlock -> queue_spinlock
*/
struct mcp2210_device {
struct usb_device *udev;
struct usb_interface *intf;
spinlock_t dev_spinlock;
spinlock_t queue_spinlock;
#ifdef CONFIG_MCP2210_IOCTL
struct mutex io_mutex;
#endif
struct kref kref;
#ifdef CONFIG_MCP2210_DEBUG
atomic_t manager_running;
#endif
struct list_head cmd_queue;
struct mcp2210_cmd *cur_cmd;
struct list_head delayed_list;
struct mcp2210_cmd *delayed_cmd;
struct timer_list timer;
struct mcp2210_endpoint eps[2];
struct delayed_work delayed_work;
int dead;
u8 debug_chatter_count;
u8 spi_in_flight;
struct mcp2210_state s;
struct mcp2210_board_config *config;
struct mcp2210_cmd_ctl ctl_cmd;
#ifdef CONFIG_MCP2210_EEPROM
spinlock_t eeprom_spinlock;
u8 eeprom_state[64];
u8 eeprom_cache[256];
#endif
const char *names[MCP2210_NUM_PINS];
#ifdef CONFIG_MCP2210_GPIO
struct gpio_chip gpio;
#endif
#ifdef CONFIG_MCP2210_SPI
struct spi_master *spi_master;
struct spi_device *chips[MCP2210_NUM_PINS];
#else
char spi_master; /* tmp hack */
#endif
#ifdef CONFIG_MCP2210_IRQ
struct mutex irq_lock;
u16 irq_mask;
u16 irq_stat;
u16 irq_trig_raise;
u16 irq_trig_fall;
struct irq_domain *irq_domain;
u8 nr_irqs;
int irq_base;
# ifdef CONFIG_MCP2210_GPIO
struct mcp2210_cmd_ctl cmd_poll_gpio;
# endif
struct mcp2210_cmd_ctl cmd_poll_intr;
#endif /* CONFIG_MCP2210_IRQ */
};
/* exported functions */
/*****************************************************************************
* mcp2210-core.c
*/
int mcp2210_update_settings(struct mcp2210_device *dev);
#define mcp2210_alloc_cmd_type(dev, type, fns, gfp_flags) \
((type*) mcp2210_alloc_cmd((dev), fns, sizeof(type), gfp_flags))
struct mcp2210_cmd *mcp2210_alloc_cmd(struct mcp2210_device *dev,
const struct mcp2210_cmd_type *type,
size_t size, gfp_t gfp_flags);
int mcp2210_add_cmd(struct mcp2210_cmd *cmd, bool free_if_dead);
int process_commands(struct mcp2210_device *dev, const bool lock_held, const bool can_sleep);
int mcp2210_set_pin_config(struct mcp2210_device *dev, unsigned pin,
const struct mcp2210_pin_config *config);
int mcp2210_configure(struct mcp2210_device *dev,
struct mcp2210_board_config *new_config);
/*****************************************************************************
* mcp2210-ctl.c
*/
void ctl_cmd_init(struct mcp2210_device *dev, struct mcp2210_cmd_ctl *cmd,
u8 cmd_code, u8 subcmd_code, void *body, size_t body_size,
u8 is_mcp_endianness);
struct mcp2210_cmd_ctl *mcp2210_alloc_ctl_cmd(struct mcp2210_device *dev,
u8 cmd_code, u8 subcmd_code, void *body, size_t body_size,
u8 is_mcp_endianness, gfp_t gfp_flags);
void calculate_spi_settings(struct mcp2210_spi_xfer_settings *dest,
const struct mcp2210_device *dev,
const struct spi_device *spi,
const struct spi_message *msg,
const struct spi_transfer *xfer, u8 pin);
/*****************************************************************************
* mcp2210-ioctl.c
*/
#ifdef CONFIG_MCP2210_IOCTL
long mcp2210_ioctl(struct file *file, unsigned int request, unsigned long arg);
#endif
/*****************************************************************************
* mcp2210-spi.c
*/
#ifdef CONFIG_MCP2210_SPI
int mcp2210_spi_probe (struct mcp2210_device *dev);
void mcp2210_spi_remove(struct mcp2210_device *dev);
#else
static inline int mcp2210_spi_probe (struct mcp2210_device *dev) {return 0;}
static inline void mcp2210_spi_remove(struct mcp2210_device *dev) {}
#endif /* CONFIG_MCP2210_SPI */
/*****************************************************************************
* mcp2210-gpio.c
*/
#ifdef CONFIG_MCP2210_GPIO
int mcp2210_gpio_probe (struct mcp2210_device *dev);
void mcp2210_gpio_remove(struct mcp2210_device *dev);
#else
static inline int mcp2210_gpio_probe (struct mcp2210_device *dev) {return 0;}
static inline void mcp2210_gpio_remove(struct mcp2210_device *dev) {}
#endif /* CONFIG_MCP2210_GPIO */
/*****************************************************************************
* mcp2210-eeprom.c
*/
#ifdef CONFIG_MCP2210_EEPROM
/* locks dev->eeprom_spinlock */
int
mcp2210_eeprom_read(struct mcp2210_device *dev, u8 *dest, u8 addr, u16 size,
mcp2210_complete_t complete, void *context,
gfp_t gfp_flags);
int
mcp2210_eeprom_write(struct mcp2210_device *dev, const u8 *src, u8 addr,
u16 size, mcp2210_complete_t complete, void *context,
gfp_t gfp_flags);
#else
static inline int
mcp2210_eeprom_read(struct mcp2210_device *dev, u8 *dest, u8 addr, u16 size,
mcp2210_complete_t complete, void *context,
gfp_t gfp_flags) {return 0;}
static inline int
mcp2210_eeprom_write(struct mcp2210_device *dev, const u8 *src, u8 addr,
u16 size, mcp2210_complete_t complete, void *context,
gfp_t gfp_flags) {return 0;}
#endif /* CONFIG_MCP2210_EEPROM */
/*****************************************************************************
* mcp2210-irq.c
*/
#ifdef CONFIG_MCP2210_IRQ
int mcp2210_irq_probe (struct mcp2210_device *dev);
void mcp2210_irq_remove(struct mcp2210_device *dev);
int mcp2210_gpio_to_irq(struct gpio_chip *chip, unsigned offset);
void _mcp2210_irq_do_gpio(struct mcp2210_device *dev, u16 old_val, u16 new_val);
void _mcp2210_irq_do_intr_counter(struct mcp2210_device *dev, u16 count);
static inline void mcp2210_irq_do_gpio(struct mcp2210_device *dev,
u16 old_val, u16 new_val)
{
if (old_val != new_val)
_mcp2210_irq_do_gpio(dev, old_val, new_val);
}
static inline void mcp2210_irq_do_intr_counter(struct mcp2210_device *dev,
u16 count)
{
if (count)
_mcp2210_irq_do_intr_counter(dev, count);
}
#else
static inline int mcp2210_irq_probe (struct mcp2210_device *dev) {return 0;}
static inline void mcp2210_irq_remove(struct mcp2210_device *dev) {}
static inline void mcp2210_irq_do_gpio(struct mcp2210_device *dev,
u16 old_val, u16 new_val) {}
static inline void mcp2210_irq_do_intr_counter(struct mcp2210_device *dev,
u16 count) {}
#endif /* CONFIG_MCP2210_IRQ */
/*****************************************************************************
* inlines
*/
static inline long jiffdiff(unsigned long a, unsigned long b)
{
return (long)a - (long)b;
}
/* mcp2210_init_msg - initialize an MCP2210 message
*
* As long as body_size and zero_tail are compile-time constants, this is a
* more effient mechanism to cleanly init a message than zeroing the whole
* thing and then overwriting the values you need.
*/
static inline void mcp2210_init_msg(struct mcp2210_msg *msg, u8 cmd,
u8 sub_cmd_addr_size, u8 value,
const void *body, size_t body_size,
int zero_tail) {
/* init header */
msg->cmd = cmd;
msg->head.raw[0] = sub_cmd_addr_size;
msg->head.raw[1] = value;
msg->head.raw[2] = 0;
/* init body */
if(__builtin_constant_p(body_size))
BUILD_BUG_ON(body_size > sizeof(msg->body.raw));
else
BUG_ON(body_size > sizeof(msg->body.raw));
if (body)
memcpy(msg->body.raw, body, body_size);
else if (body_size)