forked from MathewKing/mcp2210-linux
-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathmcp2210-lib.c
2123 lines (1841 loc) · 58.1 KB
/
mcp2210-lib.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
/*
* MCP2210 kernel-/user-space library
*
* Copyright (c) 2013-2017 Daniel Santos <daniel.santos@pobox.com>
*
* 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.
*/
#ifdef __KERNEL__
# include <linux/kernel.h>
# include <linux/errno.h>
# include <linux/module.h>
# include <linux/ctype.h>
#else
# include <stddef.h>
# include <stdlib.h>
# include <stdio.h>
# include <string.h>
# include <errno.h>
# include <ctype.h>
#endif /* __KERNEL__ */
#include "mcp2210.h"
#include "mcp2210-debug.h"
#ifdef CONFIG_MCP2210_CREEK
# include "mcp2210-creek.h"
#endif
static void copy_board_config_string(const char *strings, size_t strings_size,
size_t *pos, const char **dest,
const char *src)
{
ssize_t buf_size = strings_size - *pos;
if (!src)
return;
BUG_ON(buf_size < 0);
*dest = (char *)&strings[*pos];
strncpy((char *)*dest, src, buf_size);
*pos += strlen(src) + 1;
}
/**
* copy_board_config - copy a struct mcp2210_board_config object
*
* These things are pesky because of the strings and I wasn't in the mood to
* make static sized strings (maybe I should have?). The main thing that this
* function does is to collect the strings (regardless of where they are stored
* in the source struct), copy them into the blob at the end of the struct and
* correctly set the pointers for each.
*/
struct mcp2210_board_config *copy_board_config(
struct mcp2210_board_config *dest,
const struct mcp2210_board_config *src, gfp_t gfp_flags)
{
struct mcp2210_board_config *ret;
size_t str_size = 0;
size_t str_buffer_size;
size_t pos = 0;
uint i;
for (i = 0; i < MCP2210_NUM_PINS; ++i) {
const struct mcp2210_pin_config *pin = &src->pins[i];
printk(KERN_DEBUG "src->pins[%u].name = %p (\"%s\")\n",
i, pin->name, pin->name ? pin->name : "");
if (pin->name)
str_size += strlen(pin->name) + 1;
printk(KERN_DEBUG "src->pins[%u].modalias = %p (\"%s\")\n",
i, pin->modalias, pin->modalias ? pin->modalias : "");
if (pin->modalias)
str_size += strlen(pin->modalias) + 1;
}
if (dest) {
if (dest->strings_size < str_size) {
printk(KERN_ERR "need %u bytes, got %u\n",
(uint)str_size, (uint)dest->strings_size);
return ERR_PTR(-EOVERFLOW);
}
str_buffer_size = dest->strings_size;
} else {
if (!(dest = kzalloc(sizeof(*ret) + str_size, gfp_flags)))
return NULL;
str_buffer_size = str_size;
}
memcpy(dest, src, sizeof(*dest));
/* Newly allocated buffer will be exactly the size needed, supplied
* buffer may be larger. Either way, we ignore the src buffer size */
dest->strings_size = str_buffer_size;
for (i = 0; i < MCP2210_NUM_PINS; ++i) {
const struct mcp2210_pin_config *src_pin = &src->pins[i];
struct mcp2210_pin_config *dest_pin = &dest->pins[i];
copy_board_config_string(dest->strings, str_size, &pos,
&dest_pin->name,
src_pin->name);
copy_board_config_string(dest->strings, str_size, &pos,
&dest_pin->modalias,
src_pin->modalias);
}
BUG_ON(pos != str_size);
return dest;
}
int validate_board_config(const struct mcp2210_board_config *src,
const struct mcp2210_chip_settings *chip)
{
uint i;
uint max_irq = 0;
uint nr_irqs = 0;
u8 irq_has_src[8];
memset(irq_has_src, 0, sizeof(irq_has_src));
/* validate settings & write irq data */
for (i = 0; i < MCP2210_NUM_PINS; ++i) {
const struct mcp2210_pin_config *pin = &src->pins[i];
/* a few sanity checks on the chip_settings */
if (chip->pin_mode[i] > MCP2210_PIN_DEDICATED) {
printk(KERN_ERR "Invalid pin mode in chip_settings\n");
return -EINVAL;
}
if (pin->mode != chip->pin_mode[i]) {
printk(KERN_ERR "chip_settings don't match "
"board_config.\n");
return -EINVAL;
}
if (pin->has_irq) {
/* determine the highest irq used */
++nr_irqs;
if (pin->irq > max_irq)
max_irq = pin->irq;
if (pin->mode == MCP2210_PIN_DEDICATED && i != 6) {
printk(KERN_ERR "Invalid: IRQ on dedicated "
"pin other than 6.");
return -EINVAL;
}
}
/* should get compiled-out, but leave it here in case we change
* the storage of the u8 irq:3 field */
if (pin->irq >= 7) {
printk(KERN_ERR "Invalid: irq > 7.");
return -EINVAL;
}
}
if (nr_irqs > 8) {
printk(KERN_ERR "Invalid: nr_irqs > 8 (unsupported).");
return -EINVAL;
}
#if 0
/* validate no unused IRQs in allocated range */
for (i = 0; i < nr_irqs; ++i) {
uint j;
for (j = 0; j < MCP2210_NUM_PINS; ++j) {
const struct mcp2210_pin_config *pin = &src->pins[j];
if (pin->has_irq && pin->irq == i)
break;
}
if (j != MCP2210_NUM_PINS) {
printk(KERN_ERR "allocated but unused irq %u", i);
return -EINVAL;
}
}
#endif
/* validate IRQ consumers match producers */
for (i = 0; i < MCP2210_NUM_PINS; ++i) {
const struct mcp2210_pin_config *pin_a = &src->pins[i];
const struct mcp2210_pin_config *pin_b;
uint j;
if (!pin_a->has_irq)
continue;
/* verify that we don't have multiple pins producing the same
* IRQ */
switch (pin_a->mode) {
case MCP2210_PIN_DEDICATED:
BUG_ON(i != 6);
/* intentional fall-through */
case MCP2210_PIN_GPIO:
if (irq_has_src[pin_a->irq]) {
printk(KERN_ERR "multiple pins configured to "
"produce irq %u", pin_a->irq);
return -EINVAL;
}
irq_has_src[pin_a->irq] = 1;
continue;
case MCP2210_PIN_SPI:
break;
}
for (j = 0; j < MCP2210_NUM_PINS; ++j) {
pin_b = &src->pins[j];
if (pin_b->mode == MCP2210_PIN_SPI)
continue;
if (pin_b->has_irq && pin_b->irq == pin_a->irq)
break;
}
if (j == MCP2210_NUM_PINS) {
printk(KERN_ERR "Invalid: spi pin %u consumes IRQ "
"offset %u, but no other pin produces "
"it.", i, pin_a->irq);
return -EINVAL;
}
}
/* validate spi cs_gpio */
for (i = 0; i < MCP2210_NUM_PINS; ++i) {
const struct mcp2210_pin_config *pin = &src->pins[i];
u8 cs_gpio = pin->spi.cs_gpio;
if (pin->mode != MCP2210_PIN_SPI || !pin->spi.use_cs_gpio)
continue;
if (cs_gpio > MCP2210_NUM_PINS || src->pins[cs_gpio].mode
!= MCP2210_PIN_GPIO) {
printk(KERN_ERR "Invalid: spi pin %u uses gpio for "
"chip select, but the specified pin (%hhu) is "
"not valid or not a gpio", i, cs_gpio);
return -EINVAL;
}
}
if (!(src->pins[6].has_irq && src->pins[6].mode == MCP2210_PIN_DEDICATED)
&& (src->poll_intr_usecs || src->stale_intr_usecs)) {
printk(KERN_ERR "Invalid: poll_intr_usecs and stale_intr_usecs should be "
"zero unless pin 6 is set to dedicated with .has_irq=1\n");
return -EINVAL;
}
return 0;
}
/******************************************************************************
* Creek configuration scheme functions
*/
#ifdef CONFIG_MCP2210_CREEK
static const uint pow10[] = {
1,
10,
100,
1000,
10000,
100000,
1000000,
10000000,
100000000,
1000000000,
};
static uint _pack_uint(uint value, uint value_bits, uint scale_bits)
{
const uint num_end = (1 << value_bits);
const uint scale_end = (1 << scale_bits);
uint num;
uint scale;
uint mult;
uint ret;
for (scale = 0, mult = 1; scale < scale_end; ++scale, mult *= 10) {
num = value / mult;
if (num < num_end) {
ret = num | scale << value_bits;
/* some sanity checks until we know that this is correct */
BUG_ON(num & ~((1 << value_bits) - 1));
BUG_ON(scale & ~((1 << scale_bits) - 1));
BUG_ON(ret & ~((1 << (value_bits + scale_bits)) - 1));
return ret;
}
}
BUG();
return (uint)-1;
}
static uint _unpack_uint(uint packed, uint value_bits, uint scale_bits)
{
const uint value_mask = (1 << value_bits) - 1;
const uint scale_mask = (1 << scale_bits) - 1;
const uint value = packed & value_mask;
const uint scale = (packed >> value_bits) & scale_mask;
#if 0
uint i, mult;
for (i = 0, mult = 1; i != scale; ++i, mult *= 10);
return value * mult;
#else
if (scale <= 9)
return value * pow10[scale];
else
return (uint)-1;
#endif
}
static uint _unpack_uint_opt(struct bit_creek *src, uint value_bits,
uint scale_bits, uint def)
{
if (creek_get_bits(src, 1)) {
uint data = creek_get_bits(src, value_bits + scale_bits);
return _unpack_uint(data, value_bits, scale_bits);
} else
return def;
}
/**
* pack_uint_opt - Write an optional packed uint value to a bit stream
*
* If the value matches def, then only a single zero bit is written. Otherwise,
* a one bit is written followed by the valued converted to a packed uint as
* specified by value_ and scale_ bits.
*/
static void _pack_uint_opt(struct bit_creek *dest, uint value, uint value_bits,
uint scale_bits, uint def)
{
bool is_not_default = (value != def);
creek_put_bits(dest, is_not_default, 1);
if (is_not_default) {
uint data = _pack_uint(value, value_bits, scale_bits);
creek_put_bits(dest, data, value_bits + scale_bits);
}
}
static __always_inline void validate_packed(uint value_bits, uint scale_bits)
{
BUILD_BUG_ON(scale_bits + value_bits >= sizeof(uint) * 8);
}
/**
* pack_uint - Pack a positive integral value into a smaller space by
* tossing out precision.
*
* @value: The (normal) value
* @value_bits: Size (in bits) of the value portion of the result (currently
* requires a compile-time constant)
* @scale_bits: Size (in bits) of the scale portion of the result (currently
* requires a compile-time constant)
*
* This mechanism for packing numbers allows for a wide range of values with a
* limited precision. A "10:2" packed value has 10 bits of value and 2 bits of
* scale allowing for the values 987, 9870, 98700 or 987000, but not 9879,
* 98799, etc. In other words, the least significant portion of the value is
* zeroed in the conversion process.
*
* @returns Upon error, (uint)-1. Otherwise, the result which is
* guaranteed to fit into value_bits + scale_bits bits.
*/
static __always_inline uint pack_uint(uint value, uint value_bits,
uint scale_bits)
{
validate_packed(value_bits, scale_bits);
return _pack_uint(value, value_bits, scale_bits);
}
/**
* unpack_uint - Unpack a packed positive integral number.
*
* @packed: The packed value
* @value_bits: Size (in bits) of the value portion of packed (currently
* requires a compile-time constant)
* @scale_bits: Size (in bits) of the scale portion of packed (currently
* requires a compile-time constant)
*
* @see pack_uint
*
* @returns The unpacked value.
*/
static __always_inline uint unpack_uint(uint packed, uint value_bits,
uint scale_bits)
{
validate_packed(value_bits, scale_bits);
return _unpack_uint(packed, value_bits, scale_bits);
}
/**
* unpack_uint_opt - Read and unpack an optional packed uint value from a
* bit stream
*/
static __always_inline uint unpack_uint_opt(struct bit_creek *src,
uint value_bits, uint scale_bits,
uint def)
{
validate_packed(value_bits, scale_bits);
return _unpack_uint_opt(src, value_bits, scale_bits, def);
}
/**
* pack_uint_opt - Write an optional packed uint value to a bit stream
*
* If the value matches def, then only a single zero bit is written. Otherwise,
* a one bit is written followed by the valued converted to a packed uint as
* specified by value_ and scale_ bits.
*/
static __always_inline void pack_uint_opt(struct bit_creek *dest, uint value,
uint value_bits, uint scale_bits,
uint def)
{
validate_packed(value_bits, scale_bits);
_pack_uint_opt(dest, value, value_bits, scale_bits, def);
}
/**
* creek_get_bits -- helper function to treat our buffer like a stream of bits
*/
uint creek_get_bits(struct bit_creek *bs, uint num_bits) {
const uint MAX_BITS = sizeof(uint) * 8;
// const uint bit_mask = num_bits == sizeof(uint) * 8 ? 0xffffffff : ((1 << num_bits) - 1);
const u8 *p = &bs->start[bs->pos / 8];
uint skip_bits = bs->pos % 8;
uint start_bits = 8 - skip_bits;
uint ret;
BUG_ON(!num_bits);
BUG_ON(num_bits > MAX_BITS);
if ((bs->pos + num_bits - 1) / 8 >= bs->size) {
bs->overflow = 1;
return (uint)-EOVERFLOW;
}
bs->pos += num_bits;
ret = *p++ >> skip_bits;
if (num_bits <= start_bits)
return ret & ((1 << num_bits) - 1);
num_bits -= start_bits;
for (;num_bits >= 8; num_bits -= 8)
ret = ret << 8 | *p++;
/* less than 8 bits needed, but more than zero */
if (num_bits > 0)
ret = ret << num_bits | (*p & ((1 << num_bits) - 1));
return ret;
}
int creek_put_bits(struct bit_creek *bs, uint value, uint num_bits) {
const uint MAX_BITS = sizeof(uint) * 8;
const uint bit_mask = num_bits == sizeof(uint) * 8
? (uint)-1
: (uint)((1 << num_bits) - 1);
u8 *p = &bs->start[bs->pos / 8];
uint skip_bits = bs->pos % 8; /* count of bits existing at byte pos
* points to and need to be skipped */
BUG_ON(!num_bits);
BUG_ON(num_bits > MAX_BITS);
BUG_ON(value & ~bit_mask);
#if 0
if (num_bits == 8) {
printk("0x%02x %c\n", value, value & 0x7f);
printk("bs->pos = %u (pos %% 8 = %u)\n", (uint)bs->pos, (uint)bs->pos % 8);
}
#endif
if ((bs->pos + num_bits - 1) / 8 >= bs->size) {
bs->overflow = 1;
return -EOVERFLOW;
}
bs->pos += num_bits;
if (skip_bits) {
uint start_bits = 8 - skip_bits;
u8 bits = num_bits < start_bits ? num_bits
: start_bits;
*p++ |= (value >> (num_bits - bits)) << skip_bits;
num_bits -= bits;
}
while(num_bits > 0) {
if (num_bits >= 8) {
*p++ = (value >> (num_bits - 8));
num_bits -= 8;
} else {
*p = value & ((1 << num_bits) - 1);
return 0;
}
}
return 0;
}
/**
* decode_strings - this could be done far more efficiently with arch-specific
* code but nobody cares.
*/
static const char **decode_strings(char *dest, size_t dest_size,
struct bit_creek *src, uint num_strings)
{
const char **index = kzalloc(sizeof(char *) * num_strings, GFP_KERNEL);
const char * const dend = &dest[dest_size];
const char ** const iend = &index[num_strings];
char *d;
const char **i;
if (!index)
return ERR_PTR(-ENOMEM);
//printk("index = %p\n", index);
for (index[0] = d = dest, i = &index[1]; d != dend;) {
u8 c = creek_get_bits(src, 8);
*d++ = (char)(c & 0x7f);
//printk("c = 0x%02hhx, d = '%c'\n", c, *(d - 1));
/* bit 7 high terminates the string */
if (c & 0x80) {
if (unlikely(d == dend))
goto error;
/* terminate string */
*d++ = 0;
/* record start of next string */
if (i != iend) {
//printk("i = %p\n", i);
*i++ = d;
} else
break;
}
}
/* make sure we're exactly where we expected to be */
if (d == dend && i == iend)
return index;
error:
printk(KERN_ERR "strings not properly read\n");
kfree(index);
return ERR_PTR(-EPROTO);
}
#ifdef CONFIG_MCP2210_DEBUG
static void creek_debug(const char *str, struct bit_creek *bs)
{
printk(KERN_DEBUG "%s - %u\n", str, (uint)bs->pos);
}
#else
# define creek_debug(a,b)
#endif
/**
* mcp2210_board_config - read in and decode settings
*
* Reads in encoded settings from source and writes them to dest
*/
struct mcp2210_board_config *creek_decode(
const struct mcp2210_chip_settings *chip_settings,
const u8* src, size_t size, gfp_t gfp_flags)
{
struct mcp2210_board_config *board_config = NULL;
struct mcp2210_board_config *tmp;
struct creek_data dec;
struct bit_creek bs;
int ret = 0;
uint strings_start;
uint i;
memset(&dec, 0, sizeof(dec));
bs.start = (u8 *)src;
bs.size = size;
bs.pos = 0;
bs.overflow = 0;
creek_debug("-------- creek_decode --------", &bs);
/* magic */
dec.magic[0] = creek_get_bits(&bs, 8);
dec.magic[1] = creek_get_bits(&bs, 8);
dec.magic[2] = creek_get_bits(&bs, 8);
dec.magic[3] = creek_get_bits(&bs, 8);
creek_debug("magic", &bs);
if (memcmp(dec.magic, CREEK_CONFIG_MAGIC, 4)) {
printk(KERN_WARNING "EEPROM magic doesn't match");
return ERR_PTR(-ENODEV);
}
/* version */
dec.ver = creek_get_bits(&bs, 4);
creek_debug("version", &bs);
if (dec.ver > 2) {
printk(KERN_ERR "Creek version %hhu unsupported", dec.ver);
return ERR_PTR(-EPROTO);
}
tmp = kzalloc(sizeof(*tmp), gfp_flags);
if (!tmp)
return ERR_PTR(-ENOMEM);
/* read has_strings, validate & populate pin modes, count SPI pins */
for (i = 0; i < MCP2210_NUM_PINS; ++i) {
struct mcp2210_pin_config *pin = &tmp->pins[i];
u8 str_flags = creek_get_bits(&bs, 2);
dec.name_index[i] = str_flags & 1 ? ++dec.str_count : 0;
dec.modalias_index[i] = str_flags & 2 ? ++dec.str_count : 0;
switch ((pin->mode = chip_settings->pin_mode[i])) {
case MCP2210_PIN_GPIO:
case MCP2210_PIN_DEDICATED:
break;
case MCP2210_PIN_SPI:
dec.spi_pin_num[dec.spi_count++] = i;
break;
default:
printk(KERN_ERR "invalid pin_mode\n");
return ERR_PTR(-EPROTO);
};
}
creek_debug("strings present, count SPI", &bs);
/* 3-wire capability */
tmp->_3wire_capable = creek_get_bits(&bs, 1);
if (tmp->_3wire_capable) {
tmp->_3wire_tx_enable_active_high = creek_get_bits(&bs, 1);
tmp->_3wire_tx_enable_pin = creek_get_bits(&bs, 3);
}
creek_debug("3wire", &bs);
/* read IRQs */
for (i = 0; i < MCP2210_NUM_PINS; ++i) {
struct mcp2210_pin_config *pin = &tmp->pins[i];
if (pin->mode == MCP2210_PIN_DEDICATED && i != 6)
/* skip IRQ for dedicated pins other than 6 */
continue;
if ((pin->has_irq = creek_get_bits(&bs, 1))) {
pin->irq = creek_get_bits(&bs, 3);
if (dec.ver >= 1)
pin->irq_threaded = creek_get_bits(&bs, 1);
if (pin->mode == MCP2210_PIN_GPIO) {
dec.have_gpio_irqs = 1;
if (dec.ver == 0)
pin->irq_type = creek_get_bits(&bs, 3);
else
pin->irq_type = creek_get_bits(&bs, 4);
}
}
}
creek_debug("IRQs", &bs);
/* read gpio polling settings */
if (dec.have_gpio_irqs) {
uint data = creek_get_bits(&bs, 13);
u32 interval = unpack_uint(data, 10, 3);
tmp->poll_gpio_usecs = interval;
tmp->stale_gpio_usecs = unpack_uint_opt(&bs, 10, 3, interval);
}
creek_debug("gpio polling done", &bs);
/* read interrupt polling settings */
if (tmp->pins[6].mode == MCP2210_PIN_DEDICATED && tmp->pins[6].has_irq) {
/* if no gpio irqs, don't read the feature bit */
uint same_as_gpio = dec.have_gpio_irqs && creek_get_bits(&bs, 1);
if (same_as_gpio) {
tmp->poll_intr_usecs = tmp->poll_gpio_usecs;
tmp->stale_intr_usecs = tmp->stale_gpio_usecs;
} else {
uint data = creek_get_bits(&bs, 13);
u32 interval = unpack_uint(data, 10, 3);
tmp->poll_intr_usecs = interval;
tmp->stale_intr_usecs = unpack_uint_opt(&bs, 10, 3,
interval);
}
}
creek_debug("intr polling done", &bs);
for (i = 0; i < dec.spi_count; ++i) {
u8 pin = dec.spi_pin_num[i];
struct mcp2210_pin_config_spi *spi = &tmp->pins[pin].spi;
uint speed_mag = dec.ver < 2 ? 2 : 3;
spi->max_speed_hz = unpack_uint_opt(&bs, 10, speed_mag,
MCP2210_MAX_SPEED);
spi->min_speed_hz = unpack_uint_opt(&bs, 10, speed_mag,
MCP2210_MIN_SPEED);
spi->mode = creek_get_bits(&bs, 8);
spi->bits_per_word = 8;
spi->use_cs_gpio = creek_get_bits(&bs, 1);
if (spi->use_cs_gpio)
spi->cs_gpio = creek_get_bits(&bs, 3);
spi->cs_to_data_delay = unpack_uint_opt(&bs, 7, 2, 0);
spi->last_byte_to_cs_delay = unpack_uint_opt(&bs, 7, 2, 0);
spi->delay_between_bytes = unpack_uint_opt(&bs, 7, 2, 0);
spi->delay_between_xfers = unpack_uint_opt(&bs, 7, 2, 0);
creek_debug("spi data", &bs);
}
/* record start of strings and find out how many bytes we need */
strings_start = bs.pos;
for (i = 0; i < dec.str_count; ) {
u8 c = creek_get_bits(&bs, 8);
if (c & 0x80) {
++i;
dec.str_size += 2;
} else
dec.str_size += 1;
c &= 0x7f;
if (unlikely(c < ' ' || c == 0x7f)) {
ret = -EPROTO;
goto exit_free;
}
}
creek_debug("strings", &bs);
if (bs.overflow) {
ret = -EPROTO;
goto exit_free;
}
/* now that we know how much room we need for strings we can allocate &
* init our return value */
if (!(board_config = kzalloc(sizeof(struct mcp2210_board_config)
+ dec.str_size, GFP_KERNEL)))
return ERR_PTR(-ENOMEM);
/* copy temporary data and then free tmp */
memcpy(board_config, tmp, sizeof(*board_config));
kfree(tmp);
board_config->strings_size = dec.str_size;
/* rewind bit stream and read strings */
bs.pos = strings_start;
dec.string_index = decode_strings((char *)board_config->strings,
board_config->strings_size, &bs,
dec.str_count);
if (IS_ERR(dec.string_index)) {
ret = PTR_ERR(dec.string_index);
dec.string_index = NULL;
goto exit_free;
}
/* now populate our mcp2210_board_config with the string pointers */
board_config->strings_size = dec.str_size;
for (i = 0; i < MCP2210_NUM_PINS; ++i) {
if (dec.name_index[i])
board_config->pins[i].name = dec.string_index[
dec.name_index[i] - 1];
if (dec.modalias_index[i])
board_config->pins[i].modalias = dec.string_index[
dec.modalias_index[i] - 1];
}
// printk(KERN_DEBUG "creek_encode: used %u bits\n", bs.pos);
exit_free:
kfree(dec.string_index);
if (ret) {
kfree(board_config);
return ERR_PTR(ret);
}
return board_config;
}
/* we could restrict strings to one case and such and get them smaller, but
* 7-bit terminated should be good enough */
static void put_encoded_string(struct bit_creek *dest, const char *str)
{
if (!str || !*str) {
creek_put_bits(dest, 0x80, 8);
return;
}
while (str[1])
creek_put_bits(dest, *str++, 8);
creek_put_bits(dest, *str | 0x80, 8);
}
int creek_encode(const struct mcp2210_board_config *src,
const struct mcp2210_chip_settings *chip, u8* dest,
size_t size, u8 ver)
{
struct creek_data data;
struct bit_creek bs;
uint i;
int ret;
ret = validate_board_config(src, chip);
if (ret) {
printk(KERN_ERR "Invalid configuration\n");
return ret;
}
if (ver > 2) {
printk(KERN_ERR "Invalid encoding version: %u. Supported "
"versions are 0 and 1.\n", ver);
return ret;
}
memset(&data, 0, sizeof(data));
bs.start = (u8 *)dest;
bs.size = size;
bs.pos = 0;
bs.overflow = 0;
creek_debug("-------- creek_encode --------", &bs);
/* magic */
creek_put_bits(&bs, CREEK_CONFIG_MAGIC[0], 8);
creek_put_bits(&bs, CREEK_CONFIG_MAGIC[1], 8);
creek_put_bits(&bs, CREEK_CONFIG_MAGIC[2], 8);
creek_put_bits(&bs, CREEK_CONFIG_MAGIC[3], 8);
creek_debug("magic", &bs);
/* version */
creek_put_bits(&bs, ver, 4);
creek_debug("version", &bs);
/* write has_string header */
for (i = 0; i < MCP2210_NUM_PINS; ++i) {
const struct mcp2210_pin_config *pin = &src->pins[i];
u8 value = (!!pin->name) | (!!pin->modalias) << 1;
creek_put_bits(&bs, value, 2);
}
creek_debug("strings present", &bs);
/* 3-wire capability (a gpio that functions as a tx enable) */
creek_put_bits(&bs, src->_3wire_capable, 1);
if (src->_3wire_capable) {
creek_put_bits(&bs, src->_3wire_tx_enable_active_high, 1);
creek_put_bits(&bs, src->_3wire_tx_enable_pin, 3);
}
creek_debug("3wire", &bs);
/* write irq data & count spi devices */
for (i = 0; i < MCP2210_NUM_PINS; ++i) {
const struct mcp2210_pin_config *pin = &src->pins[i];
switch (pin->mode) {
case MCP2210_PIN_SPI:
data.spi_pin_num[data.spi_count++] = i;
break;
case MCP2210_PIN_DEDICATED:
/* skip IRQ for dedicated pins other than 6 */
if (i != 6)
continue;
printk(KERN_DEBUG "dedicated pin...\n");
};
creek_put_bits(&bs, pin->has_irq, 1);
printk(KERN_DEBUG "pin %u %s IRQ (pos = %u)\n", i, pin->has_irq
? "has" : "doesn't have", (uint)bs.pos);
if (pin->has_irq) {
/* write the relative IRQ value */
creek_put_bits(&bs, pin->irq, 3);
if (ver >= 1)
creek_put_bits(&bs, pin->irq_threaded, 1);
/* only gpio have an irq_type, the dedicated line's
* type is configured via the USB interface */
if (pin->mode == MCP2210_PIN_GPIO) {
data.have_gpio_irqs = 1;
if (ver == 0)
creek_put_bits(&bs, pin->irq_type, 3);
else
creek_put_bits(&bs, pin->irq_type, 4);
}
}
}
creek_debug("IRQs", &bs);
/* write gpio polling settings */
if (data.have_gpio_irqs) {
u32 interval = src->poll_gpio_usecs;
creek_put_bits(&bs, pack_uint(interval, 10, 3), 13);
pack_uint_opt(&bs, src->stale_gpio_usecs, 10, 3, interval);
}
creek_debug("gpio polling done", &bs);
/* write interrupt polling settings */
if (src->pins[6].has_irq && src->pins[6].mode
== MCP2210_PIN_DEDICATED) {
uint same_as_gpio = data.have_gpio_irqs
&& src->poll_gpio_usecs == src->poll_intr_usecs
&& src->stale_intr_usecs == src->stale_gpio_usecs;
/* if we don't have any gpio irqs we should skip this bit */
if (data.have_gpio_irqs)
creek_put_bits(&bs, same_as_gpio, 1);
if (!same_as_gpio) {
u32 interval = src->poll_intr_usecs;
creek_put_bits(&bs, pack_uint(interval, 10, 3), 13);
pack_uint_opt(&bs, src->stale_intr_usecs, 10, 3,
interval);
}
}
creek_debug("intr polling done", &bs);
/* write spi setting data */
for (i = 0; i < data.spi_count; ++i) {
u8 pin = data.spi_pin_num[i];
const struct mcp2210_pin_config_spi *spi = &src->pins[pin].spi;
uint speed_mag = ver < 2 ? 2 : 3;
pack_uint_opt(&bs, spi->max_speed_hz, 10, speed_mag,
MCP2210_MAX_SPEED);
pack_uint_opt(&bs, spi->min_speed_hz, 10, speed_mag,
MCP2210_MIN_SPEED);
creek_put_bits(&bs, spi->mode, 8);
/* spi->bits_per_word should always be 8 since this chip
* doesn't support any other value */
creek_put_bits(&bs, spi->use_cs_gpio, 1);
if (spi->use_cs_gpio)
creek_put_bits(&bs, spi->cs_gpio, 3);
pack_uint_opt(&bs, spi->cs_to_data_delay, 7, 2, 0);
pack_uint_opt(&bs, spi->last_byte_to_cs_delay, 7, 2, 0);
pack_uint_opt(&bs, spi->delay_between_bytes, 7, 2, 0);
pack_uint_opt(&bs, spi->delay_between_xfers, 7, 2, 0);
creek_debug("spi data", &bs);
}
for (i = 0; i < MCP2210_NUM_PINS; ++i) {
const struct mcp2210_pin_config *pin = &src->pins[i];
if (pin->name) {
put_encoded_string(&bs, pin->name);
creek_debug("name", &bs);
}
if (pin->modalias) {
put_encoded_string(&bs, pin->modalias);
creek_debug("modalias", &bs);
}
}
creek_debug("strings", &bs);
printk(KERN_DEBUG "creek_encode: used %u bits\n", (uint)bs.pos);
if (bs.overflow)
return -EOVERFLOW;
return bs.pos;
}
#ifdef __KERNEL__
struct mcp2210_board_config *mcp2210_creek_probe(struct mcp2210_device *dev,