forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcm4000_cs.c
1924 lines (1669 loc) · 48.2 KB
/
cm4000_cs.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
/*
* A driver for the PCMCIA Smartcard Reader "Omnikey CardMan Mobile 4000"
*
* cm4000_cs.c support.linux@omnikey.com
*
* Tue Oct 23 11:32:43 GMT 2001 herp - cleaned up header files
* Sun Jan 20 10:11:15 MET 2002 herp - added modversion header files
* Thu Nov 14 16:34:11 GMT 2002 mh - added PPS functionality
* Tue Nov 19 16:36:27 GMT 2002 mh - added SUSPEND/RESUME functionailty
* Wed Jul 28 12:55:01 CEST 2004 mh - kernel 2.6 adjustments
*
* current version: 2.4.0gm4
*
* (C) 2000,2001,2002,2003,2004 Omnikey AG
*
* (C) 2005-2006 Harald Welte <laforge@gnumonks.org>
* - Adhere to Kernel CodingStyle
* - Port to 2.6.13 "new" style PCMCIA
* - Check for copy_{from,to}_user return values
* - Use nonseekable_open()
* - add class interface for udev device creation
*
* All rights reserved. Licensed under dual BSD/GPL license.
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/delay.h>
#include <linux/bitrev.h>
#include <linux/mutex.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include <pcmcia/cistpl.h>
#include <pcmcia/cisreg.h>
#include <pcmcia/ciscode.h>
#include <pcmcia/ds.h>
#include <linux/cm4000_cs.h>
/* #define ATR_CSUM */
#define reader_to_dev(x) (&x->p_dev->dev)
/* n (debug level) is ignored */
/* additional debug output may be enabled by re-compiling with
* CM4000_DEBUG set */
/* #define CM4000_DEBUG */
#define DEBUGP(n, rdr, x, args...) do { \
dev_dbg(reader_to_dev(rdr), "%s:" x, \
__func__ , ## args); \
} while (0)
static DEFINE_MUTEX(cmm_mutex);
#define T_1SEC (HZ)
#define T_10MSEC msecs_to_jiffies(10)
#define T_20MSEC msecs_to_jiffies(20)
#define T_40MSEC msecs_to_jiffies(40)
#define T_50MSEC msecs_to_jiffies(50)
#define T_100MSEC msecs_to_jiffies(100)
#define T_500MSEC msecs_to_jiffies(500)
static void cm4000_release(struct pcmcia_device *link);
static int major; /* major number we get from the kernel */
/* note: the first state has to have number 0 always */
#define M_FETCH_ATR 0
#define M_TIMEOUT_WAIT 1
#define M_READ_ATR_LEN 2
#define M_READ_ATR 3
#define M_ATR_PRESENT 4
#define M_BAD_CARD 5
#define M_CARDOFF 6
#define LOCK_IO 0
#define LOCK_MONITOR 1
#define IS_AUTOPPS_ACT 6
#define IS_PROCBYTE_PRESENT 7
#define IS_INVREV 8
#define IS_ANY_T0 9
#define IS_ANY_T1 10
#define IS_ATR_PRESENT 11
#define IS_ATR_VALID 12
#define IS_CMM_ABSENT 13
#define IS_BAD_LENGTH 14
#define IS_BAD_CSUM 15
#define IS_BAD_CARD 16
#define REG_FLAGS0(x) (x + 0)
#define REG_FLAGS1(x) (x + 1)
#define REG_NUM_BYTES(x) (x + 2)
#define REG_BUF_ADDR(x) (x + 3)
#define REG_BUF_DATA(x) (x + 4)
#define REG_NUM_SEND(x) (x + 5)
#define REG_BAUDRATE(x) (x + 6)
#define REG_STOPBITS(x) (x + 7)
struct cm4000_dev {
struct pcmcia_device *p_dev;
unsigned char atr[MAX_ATR];
unsigned char rbuf[512];
unsigned char sbuf[512];
wait_queue_head_t devq; /* when removing cardman must not be
zeroed! */
wait_queue_head_t ioq; /* if IO is locked, wait on this Q */
wait_queue_head_t atrq; /* wait for ATR valid */
wait_queue_head_t readq; /* used by write to wake blk.read */
/* warning: do not move this fields.
* initialising to zero depends on it - see ZERO_DEV below. */
unsigned char atr_csum;
unsigned char atr_len_retry;
unsigned short atr_len;
unsigned short rlen; /* bytes avail. after write */
unsigned short rpos; /* latest read pos. write zeroes */
unsigned char procbyte; /* T=0 procedure byte */
unsigned char mstate; /* state of card monitor */
unsigned char cwarn; /* slow down warning */
unsigned char flags0; /* cardman IO-flags 0 */
unsigned char flags1; /* cardman IO-flags 1 */
unsigned int mdelay; /* variable monitor speeds, in jiffies */
unsigned int baudv; /* baud value for speed */
unsigned char ta1;
unsigned char proto; /* T=0, T=1, ... */
unsigned long flags; /* lock+flags (MONITOR,IO,ATR) * for concurrent
access */
unsigned char pts[4];
struct timer_list timer; /* used to keep monitor running */
int monitor_running;
};
#define ZERO_DEV(dev) \
memset(&dev->atr_csum,0, \
sizeof(struct cm4000_dev) - \
offsetof(struct cm4000_dev, atr_csum))
static struct pcmcia_device *dev_table[CM4000_MAX_DEV];
static struct class *cmm_class;
/* This table doesn't use spaces after the comma between fields and thus
* violates CodingStyle. However, I don't really think wrapping it around will
* make it any clearer to read -HW */
static unsigned char fi_di_table[10][14] = {
/*FI 00 01 02 03 04 05 06 07 08 09 10 11 12 13 */
/*DI */
/* 0 */ {0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11},
/* 1 */ {0x01,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x91,0x11,0x11,0x11,0x11},
/* 2 */ {0x02,0x12,0x22,0x32,0x11,0x11,0x11,0x11,0x11,0x92,0xA2,0xB2,0x11,0x11},
/* 3 */ {0x03,0x13,0x23,0x33,0x43,0x53,0x63,0x11,0x11,0x93,0xA3,0xB3,0xC3,0xD3},
/* 4 */ {0x04,0x14,0x24,0x34,0x44,0x54,0x64,0x11,0x11,0x94,0xA4,0xB4,0xC4,0xD4},
/* 5 */ {0x00,0x15,0x25,0x35,0x45,0x55,0x65,0x11,0x11,0x95,0xA5,0xB5,0xC5,0xD5},
/* 6 */ {0x06,0x16,0x26,0x36,0x46,0x56,0x66,0x11,0x11,0x96,0xA6,0xB6,0xC6,0xD6},
/* 7 */ {0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11},
/* 8 */ {0x08,0x11,0x28,0x38,0x48,0x58,0x68,0x11,0x11,0x98,0xA8,0xB8,0xC8,0xD8},
/* 9 */ {0x09,0x19,0x29,0x39,0x49,0x59,0x69,0x11,0x11,0x99,0xA9,0xB9,0xC9,0xD9}
};
#ifndef CM4000_DEBUG
#define xoutb outb
#define xinb inb
#else
static inline void xoutb(unsigned char val, unsigned short port)
{
pr_debug("outb(val=%.2x,port=%.4x)\n", val, port);
outb(val, port);
}
static inline unsigned char xinb(unsigned short port)
{
unsigned char val;
val = inb(port);
pr_debug("%.2x=inb(%.4x)\n", val, port);
return val;
}
#endif
static inline unsigned char invert_revert(unsigned char ch)
{
return bitrev8(~ch);
}
static void str_invert_revert(unsigned char *b, int len)
{
int i;
for (i = 0; i < len; i++)
b[i] = invert_revert(b[i]);
}
#define ATRLENCK(dev,pos) \
if (pos>=dev->atr_len || pos>=MAX_ATR) \
goto return_0;
static unsigned int calc_baudv(unsigned char fidi)
{
unsigned int wcrcf, wbrcf, fi_rfu, di_rfu;
fi_rfu = 372;
di_rfu = 1;
/* FI */
switch ((fidi >> 4) & 0x0F) {
case 0x00:
wcrcf = 372;
break;
case 0x01:
wcrcf = 372;
break;
case 0x02:
wcrcf = 558;
break;
case 0x03:
wcrcf = 744;
break;
case 0x04:
wcrcf = 1116;
break;
case 0x05:
wcrcf = 1488;
break;
case 0x06:
wcrcf = 1860;
break;
case 0x07:
wcrcf = fi_rfu;
break;
case 0x08:
wcrcf = fi_rfu;
break;
case 0x09:
wcrcf = 512;
break;
case 0x0A:
wcrcf = 768;
break;
case 0x0B:
wcrcf = 1024;
break;
case 0x0C:
wcrcf = 1536;
break;
case 0x0D:
wcrcf = 2048;
break;
default:
wcrcf = fi_rfu;
break;
}
/* DI */
switch (fidi & 0x0F) {
case 0x00:
wbrcf = di_rfu;
break;
case 0x01:
wbrcf = 1;
break;
case 0x02:
wbrcf = 2;
break;
case 0x03:
wbrcf = 4;
break;
case 0x04:
wbrcf = 8;
break;
case 0x05:
wbrcf = 16;
break;
case 0x06:
wbrcf = 32;
break;
case 0x07:
wbrcf = di_rfu;
break;
case 0x08:
wbrcf = 12;
break;
case 0x09:
wbrcf = 20;
break;
default:
wbrcf = di_rfu;
break;
}
return (wcrcf / wbrcf);
}
static unsigned short io_read_num_rec_bytes(unsigned int iobase,
unsigned short *s)
{
unsigned short tmp;
tmp = *s = 0;
do {
*s = tmp;
tmp = inb(REG_NUM_BYTES(iobase)) |
(inb(REG_FLAGS0(iobase)) & 4 ? 0x100 : 0);
} while (tmp != *s);
return *s;
}
static int parse_atr(struct cm4000_dev *dev)
{
unsigned char any_t1, any_t0;
unsigned char ch, ifno;
int ix, done;
DEBUGP(3, dev, "-> parse_atr: dev->atr_len = %i\n", dev->atr_len);
if (dev->atr_len < 3) {
DEBUGP(5, dev, "parse_atr: atr_len < 3\n");
return 0;
}
if (dev->atr[0] == 0x3f)
set_bit(IS_INVREV, &dev->flags);
else
clear_bit(IS_INVREV, &dev->flags);
ix = 1;
ifno = 1;
ch = dev->atr[1];
dev->proto = 0; /* XXX PROTO */
any_t1 = any_t0 = done = 0;
dev->ta1 = 0x11; /* defaults to 9600 baud */
do {
if (ifno == 1 && (ch & 0x10)) {
/* read first interface byte and TA1 is present */
dev->ta1 = dev->atr[2];
DEBUGP(5, dev, "Card says FiDi is 0x%.2x\n", dev->ta1);
ifno++;
} else if ((ifno == 2) && (ch & 0x10)) { /* TA(2) */
dev->ta1 = 0x11;
ifno++;
}
DEBUGP(5, dev, "Yi=%.2x\n", ch & 0xf0);
ix += ((ch & 0x10) >> 4) /* no of int.face chars */
+((ch & 0x20) >> 5)
+ ((ch & 0x40) >> 6)
+ ((ch & 0x80) >> 7);
/* ATRLENCK(dev,ix); */
if (ch & 0x80) { /* TDi */
ch = dev->atr[ix];
if ((ch & 0x0f)) {
any_t1 = 1;
DEBUGP(5, dev, "card is capable of T=1\n");
} else {
any_t0 = 1;
DEBUGP(5, dev, "card is capable of T=0\n");
}
} else
done = 1;
} while (!done);
DEBUGP(5, dev, "ix=%d noHist=%d any_t1=%d\n",
ix, dev->atr[1] & 15, any_t1);
if (ix + 1 + (dev->atr[1] & 0x0f) + any_t1 != dev->atr_len) {
DEBUGP(5, dev, "length error\n");
return 0;
}
if (any_t0)
set_bit(IS_ANY_T0, &dev->flags);
if (any_t1) { /* compute csum */
dev->atr_csum = 0;
#ifdef ATR_CSUM
for (i = 1; i < dev->atr_len; i++)
dev->atr_csum ^= dev->atr[i];
if (dev->atr_csum) {
set_bit(IS_BAD_CSUM, &dev->flags);
DEBUGP(5, dev, "bad checksum\n");
goto return_0;
}
#endif
if (any_t0 == 0)
dev->proto = 1; /* XXX PROTO */
set_bit(IS_ANY_T1, &dev->flags);
}
return 1;
}
struct card_fixup {
char atr[12];
u_int8_t atr_len;
u_int8_t stopbits;
};
static struct card_fixup card_fixups[] = {
{ /* ACOS */
.atr = { 0x3b, 0xb3, 0x11, 0x00, 0x00, 0x41, 0x01 },
.atr_len = 7,
.stopbits = 0x03,
},
{ /* Motorola */
.atr = {0x3b, 0x76, 0x13, 0x00, 0x00, 0x80, 0x62, 0x07,
0x41, 0x81, 0x81 },
.atr_len = 11,
.stopbits = 0x04,
},
};
static void set_cardparameter(struct cm4000_dev *dev)
{
int i;
unsigned int iobase = dev->p_dev->resource[0]->start;
u_int8_t stopbits = 0x02; /* ISO default */
DEBUGP(3, dev, "-> set_cardparameter\n");
dev->flags1 = dev->flags1 | (((dev->baudv - 1) & 0x0100) >> 8);
xoutb(dev->flags1, REG_FLAGS1(iobase));
DEBUGP(5, dev, "flags1 = 0x%02x\n", dev->flags1);
/* set baudrate */
xoutb((unsigned char)((dev->baudv - 1) & 0xFF), REG_BAUDRATE(iobase));
DEBUGP(5, dev, "baudv = %i -> write 0x%02x\n", dev->baudv,
((dev->baudv - 1) & 0xFF));
/* set stopbits */
for (i = 0; i < ARRAY_SIZE(card_fixups); i++) {
if (!memcmp(dev->atr, card_fixups[i].atr,
card_fixups[i].atr_len))
stopbits = card_fixups[i].stopbits;
}
xoutb(stopbits, REG_STOPBITS(iobase));
DEBUGP(3, dev, "<- set_cardparameter\n");
}
static int set_protocol(struct cm4000_dev *dev, struct ptsreq *ptsreq)
{
unsigned long tmp, i;
unsigned short num_bytes_read;
unsigned char pts_reply[4];
ssize_t rc;
unsigned int iobase = dev->p_dev->resource[0]->start;
rc = 0;
DEBUGP(3, dev, "-> set_protocol\n");
DEBUGP(5, dev, "ptsreq->Protocol = 0x%.8x, ptsreq->Flags=0x%.8x, "
"ptsreq->pts1=0x%.2x, ptsreq->pts2=0x%.2x, "
"ptsreq->pts3=0x%.2x\n", (unsigned int)ptsreq->protocol,
(unsigned int)ptsreq->flags, ptsreq->pts1, ptsreq->pts2,
ptsreq->pts3);
/* Fill PTS structure */
dev->pts[0] = 0xff;
dev->pts[1] = 0x00;
tmp = ptsreq->protocol;
while ((tmp = (tmp >> 1)) > 0)
dev->pts[1]++;
dev->proto = dev->pts[1]; /* Set new protocol */
dev->pts[1] = (0x01 << 4) | (dev->pts[1]);
/* Correct Fi/Di according to CM4000 Fi/Di table */
DEBUGP(5, dev, "Ta(1) from ATR is 0x%.2x\n", dev->ta1);
/* set Fi/Di according to ATR TA(1) */
dev->pts[2] = fi_di_table[dev->ta1 & 0x0F][(dev->ta1 >> 4) & 0x0F];
/* Calculate PCK character */
dev->pts[3] = dev->pts[0] ^ dev->pts[1] ^ dev->pts[2];
DEBUGP(5, dev, "pts0=%.2x, pts1=%.2x, pts2=%.2x, pts3=%.2x\n",
dev->pts[0], dev->pts[1], dev->pts[2], dev->pts[3]);
/* check card convention */
if (test_bit(IS_INVREV, &dev->flags))
str_invert_revert(dev->pts, 4);
/* reset SM */
xoutb(0x80, REG_FLAGS0(iobase));
/* Enable access to the message buffer */
DEBUGP(5, dev, "Enable access to the messages buffer\n");
dev->flags1 = 0x20 /* T_Active */
| (test_bit(IS_INVREV, &dev->flags) ? 0x02 : 0x00) /* inv parity */
| ((dev->baudv >> 8) & 0x01); /* MSB-baud */
xoutb(dev->flags1, REG_FLAGS1(iobase));
DEBUGP(5, dev, "Enable message buffer -> flags1 = 0x%.2x\n",
dev->flags1);
/* write challenge to the buffer */
DEBUGP(5, dev, "Write challenge to buffer: ");
for (i = 0; i < 4; i++) {
xoutb(i, REG_BUF_ADDR(iobase));
xoutb(dev->pts[i], REG_BUF_DATA(iobase)); /* buf data */
#ifdef CM4000_DEBUG
pr_debug("0x%.2x ", dev->pts[i]);
}
pr_debug("\n");
#else
}
#endif
/* set number of bytes to write */
DEBUGP(5, dev, "Set number of bytes to write\n");
xoutb(0x04, REG_NUM_SEND(iobase));
/* Trigger CARDMAN CONTROLLER */
xoutb(0x50, REG_FLAGS0(iobase));
/* Monitor progress */
/* wait for xmit done */
DEBUGP(5, dev, "Waiting for NumRecBytes getting valid\n");
for (i = 0; i < 100; i++) {
if (inb(REG_FLAGS0(iobase)) & 0x08) {
DEBUGP(5, dev, "NumRecBytes is valid\n");
break;
}
mdelay(10);
}
if (i == 100) {
DEBUGP(5, dev, "Timeout waiting for NumRecBytes getting "
"valid\n");
rc = -EIO;
goto exit_setprotocol;
}
DEBUGP(5, dev, "Reading NumRecBytes\n");
for (i = 0; i < 100; i++) {
io_read_num_rec_bytes(iobase, &num_bytes_read);
if (num_bytes_read >= 4) {
DEBUGP(2, dev, "NumRecBytes = %i\n", num_bytes_read);
break;
}
mdelay(10);
}
/* check whether it is a short PTS reply? */
if (num_bytes_read == 3)
i = 0;
if (i == 100) {
DEBUGP(5, dev, "Timeout reading num_bytes_read\n");
rc = -EIO;
goto exit_setprotocol;
}
DEBUGP(5, dev, "Reset the CARDMAN CONTROLLER\n");
xoutb(0x80, REG_FLAGS0(iobase));
/* Read PPS reply */
DEBUGP(5, dev, "Read PPS reply\n");
for (i = 0; i < num_bytes_read; i++) {
xoutb(i, REG_BUF_ADDR(iobase));
pts_reply[i] = inb(REG_BUF_DATA(iobase));
}
#ifdef CM4000_DEBUG
DEBUGP(2, dev, "PTSreply: ");
for (i = 0; i < num_bytes_read; i++) {
pr_debug("0x%.2x ", pts_reply[i]);
}
pr_debug("\n");
#endif /* CM4000_DEBUG */
DEBUGP(5, dev, "Clear Tactive in Flags1\n");
xoutb(0x20, REG_FLAGS1(iobase));
/* Compare ptsreq and ptsreply */
if ((dev->pts[0] == pts_reply[0]) &&
(dev->pts[1] == pts_reply[1]) &&
(dev->pts[2] == pts_reply[2]) && (dev->pts[3] == pts_reply[3])) {
/* setcardparameter according to PPS */
dev->baudv = calc_baudv(dev->pts[2]);
set_cardparameter(dev);
} else if ((dev->pts[0] == pts_reply[0]) &&
((dev->pts[1] & 0xef) == pts_reply[1]) &&
((pts_reply[0] ^ pts_reply[1]) == pts_reply[2])) {
/* short PTS reply, set card parameter to default values */
dev->baudv = calc_baudv(0x11);
set_cardparameter(dev);
} else
rc = -EIO;
exit_setprotocol:
DEBUGP(3, dev, "<- set_protocol\n");
return rc;
}
static int io_detect_cm4000(unsigned int iobase, struct cm4000_dev *dev)
{
/* note: statemachine is assumed to be reset */
if (inb(REG_FLAGS0(iobase)) & 8) {
clear_bit(IS_ATR_VALID, &dev->flags);
set_bit(IS_CMM_ABSENT, &dev->flags);
return 0; /* detect CMM = 1 -> failure */
}
/* xoutb(0x40, REG_FLAGS1(iobase)); detectCMM */
xoutb(dev->flags1 | 0x40, REG_FLAGS1(iobase));
if ((inb(REG_FLAGS0(iobase)) & 8) == 0) {
clear_bit(IS_ATR_VALID, &dev->flags);
set_bit(IS_CMM_ABSENT, &dev->flags);
return 0; /* detect CMM=0 -> failure */
}
/* clear detectCMM again by restoring original flags1 */
xoutb(dev->flags1, REG_FLAGS1(iobase));
return 1;
}
static void terminate_monitor(struct cm4000_dev *dev)
{
/* tell the monitor to stop and wait until
* it terminates.
*/
DEBUGP(3, dev, "-> terminate_monitor\n");
wait_event_interruptible(dev->devq,
test_and_set_bit(LOCK_MONITOR,
(void *)&dev->flags));
/* now, LOCK_MONITOR has been set.
* allow a last cycle in the monitor.
* the monitor will indicate that it has
* finished by clearing this bit.
*/
DEBUGP(5, dev, "Now allow last cycle of monitor!\n");
while (test_bit(LOCK_MONITOR, (void *)&dev->flags))
msleep(25);
DEBUGP(5, dev, "Delete timer\n");
del_timer_sync(&dev->timer);
#ifdef CM4000_DEBUG
dev->monitor_running = 0;
#endif
DEBUGP(3, dev, "<- terminate_monitor\n");
}
/*
* monitor the card every 50msec. as a side-effect, retrieve the
* atr once a card is inserted. another side-effect of retrieving the
* atr is that the card will be powered on, so there is no need to
* power on the card explictely from the application: the driver
* is already doing that for you.
*/
static void monitor_card(unsigned long p)
{
struct cm4000_dev *dev = (struct cm4000_dev *) p;
unsigned int iobase = dev->p_dev->resource[0]->start;
unsigned short s;
struct ptsreq ptsreq;
int i, atrc;
DEBUGP(7, dev, "-> monitor_card\n");
/* if someone has set the lock for us: we're done! */
if (test_and_set_bit(LOCK_MONITOR, &dev->flags)) {
DEBUGP(4, dev, "About to stop monitor\n");
/* no */
dev->rlen =
dev->rpos =
dev->atr_csum = dev->atr_len_retry = dev->cwarn = 0;
dev->mstate = M_FETCH_ATR;
clear_bit(LOCK_MONITOR, &dev->flags);
/* close et al. are sleeping on devq, so wake it */
wake_up_interruptible(&dev->devq);
DEBUGP(2, dev, "<- monitor_card (we are done now)\n");
return;
}
/* try to lock io: if it is already locked, just add another timer */
if (test_and_set_bit(LOCK_IO, (void *)&dev->flags)) {
DEBUGP(4, dev, "Couldn't get IO lock\n");
goto return_with_timer;
}
/* is a card/a reader inserted at all ? */
dev->flags0 = xinb(REG_FLAGS0(iobase));
DEBUGP(7, dev, "dev->flags0 = 0x%2x\n", dev->flags0);
DEBUGP(7, dev, "smartcard present: %s\n",
dev->flags0 & 1 ? "yes" : "no");
DEBUGP(7, dev, "cardman present: %s\n",
dev->flags0 == 0xff ? "no" : "yes");
if ((dev->flags0 & 1) == 0 /* no smartcard inserted */
|| dev->flags0 == 0xff) { /* no cardman inserted */
/* no */
dev->rlen =
dev->rpos =
dev->atr_csum = dev->atr_len_retry = dev->cwarn = 0;
dev->mstate = M_FETCH_ATR;
dev->flags &= 0x000000ff; /* only keep IO and MONITOR locks */
if (dev->flags0 == 0xff) {
DEBUGP(4, dev, "set IS_CMM_ABSENT bit\n");
set_bit(IS_CMM_ABSENT, &dev->flags);
} else if (test_bit(IS_CMM_ABSENT, &dev->flags)) {
DEBUGP(4, dev, "clear IS_CMM_ABSENT bit "
"(card is removed)\n");
clear_bit(IS_CMM_ABSENT, &dev->flags);
}
goto release_io;
} else if ((dev->flags0 & 1) && test_bit(IS_CMM_ABSENT, &dev->flags)) {
/* cardman and card present but cardman was absent before
* (after suspend with inserted card) */
DEBUGP(4, dev, "clear IS_CMM_ABSENT bit (card is inserted)\n");
clear_bit(IS_CMM_ABSENT, &dev->flags);
}
if (test_bit(IS_ATR_VALID, &dev->flags) == 1) {
DEBUGP(7, dev, "believe ATR is already valid (do nothing)\n");
goto release_io;
}
switch (dev->mstate) {
unsigned char flags0;
case M_CARDOFF:
DEBUGP(4, dev, "M_CARDOFF\n");
flags0 = inb(REG_FLAGS0(iobase));
if (flags0 & 0x02) {
/* wait until Flags0 indicate power is off */
dev->mdelay = T_10MSEC;
} else {
/* Flags0 indicate power off and no card inserted now;
* Reset CARDMAN CONTROLLER */
xoutb(0x80, REG_FLAGS0(iobase));
/* prepare for fetching ATR again: after card off ATR
* is read again automatically */
dev->rlen =
dev->rpos =
dev->atr_csum =
dev->atr_len_retry = dev->cwarn = 0;
dev->mstate = M_FETCH_ATR;
/* minimal gap between CARDOFF and read ATR is 50msec */
dev->mdelay = T_50MSEC;
}
break;
case M_FETCH_ATR:
DEBUGP(4, dev, "M_FETCH_ATR\n");
xoutb(0x80, REG_FLAGS0(iobase));
DEBUGP(4, dev, "Reset BAUDV to 9600\n");
dev->baudv = 0x173; /* 9600 */
xoutb(0x02, REG_STOPBITS(iobase)); /* stopbits=2 */
xoutb(0x73, REG_BAUDRATE(iobase)); /* baud value */
xoutb(0x21, REG_FLAGS1(iobase)); /* T_Active=1, baud
value */
/* warm start vs. power on: */
xoutb(dev->flags0 & 2 ? 0x46 : 0x44, REG_FLAGS0(iobase));
dev->mdelay = T_40MSEC;
dev->mstate = M_TIMEOUT_WAIT;
break;
case M_TIMEOUT_WAIT:
DEBUGP(4, dev, "M_TIMEOUT_WAIT\n");
/* numRecBytes */
io_read_num_rec_bytes(iobase, &dev->atr_len);
dev->mdelay = T_10MSEC;
dev->mstate = M_READ_ATR_LEN;
break;
case M_READ_ATR_LEN:
DEBUGP(4, dev, "M_READ_ATR_LEN\n");
/* infinite loop possible, since there is no timeout */
#define MAX_ATR_LEN_RETRY 100
if (dev->atr_len == io_read_num_rec_bytes(iobase, &s)) {
if (dev->atr_len_retry++ >= MAX_ATR_LEN_RETRY) { /* + XX msec */
dev->mdelay = T_10MSEC;
dev->mstate = M_READ_ATR;
}
} else {
dev->atr_len = s;
dev->atr_len_retry = 0; /* set new timeout */
}
DEBUGP(4, dev, "Current ATR_LEN = %i\n", dev->atr_len);
break;
case M_READ_ATR:
DEBUGP(4, dev, "M_READ_ATR\n");
xoutb(0x80, REG_FLAGS0(iobase)); /* reset SM */
for (i = 0; i < dev->atr_len; i++) {
xoutb(i, REG_BUF_ADDR(iobase));
dev->atr[i] = inb(REG_BUF_DATA(iobase));
}
/* Deactivate T_Active flags */
DEBUGP(4, dev, "Deactivate T_Active flags\n");
dev->flags1 = 0x01;
xoutb(dev->flags1, REG_FLAGS1(iobase));
/* atr is present (which doesn't mean it's valid) */
set_bit(IS_ATR_PRESENT, &dev->flags);
if (dev->atr[0] == 0x03)
str_invert_revert(dev->atr, dev->atr_len);
atrc = parse_atr(dev);
if (atrc == 0) { /* atr invalid */
dev->mdelay = 0;
dev->mstate = M_BAD_CARD;
} else {
dev->mdelay = T_50MSEC;
dev->mstate = M_ATR_PRESENT;
set_bit(IS_ATR_VALID, &dev->flags);
}
if (test_bit(IS_ATR_VALID, &dev->flags) == 1) {
DEBUGP(4, dev, "monitor_card: ATR valid\n");
/* if ta1 == 0x11, no PPS necessary (default values) */
/* do not do PPS with multi protocol cards */
if ((test_bit(IS_AUTOPPS_ACT, &dev->flags) == 0) &&
(dev->ta1 != 0x11) &&
!(test_bit(IS_ANY_T0, &dev->flags) &&
test_bit(IS_ANY_T1, &dev->flags))) {
DEBUGP(4, dev, "Perform AUTOPPS\n");
set_bit(IS_AUTOPPS_ACT, &dev->flags);
ptsreq.protocol = (0x01 << dev->proto);
ptsreq.flags = 0x01;
ptsreq.pts1 = 0x00;
ptsreq.pts2 = 0x00;
ptsreq.pts3 = 0x00;
if (set_protocol(dev, &ptsreq) == 0) {
DEBUGP(4, dev, "AUTOPPS ret SUCC\n");
clear_bit(IS_AUTOPPS_ACT, &dev->flags);
wake_up_interruptible(&dev->atrq);
} else {
DEBUGP(4, dev, "AUTOPPS failed: "
"repower using defaults\n");
/* prepare for repowering */
clear_bit(IS_ATR_PRESENT, &dev->flags);
clear_bit(IS_ATR_VALID, &dev->flags);
dev->rlen =
dev->rpos =
dev->atr_csum =
dev->atr_len_retry = dev->cwarn = 0;
dev->mstate = M_FETCH_ATR;
dev->mdelay = T_50MSEC;
}
} else {
/* for cards which use slightly different
* params (extra guard time) */
set_cardparameter(dev);
if (test_bit(IS_AUTOPPS_ACT, &dev->flags) == 1)
DEBUGP(4, dev, "AUTOPPS already active "
"2nd try:use default values\n");
if (dev->ta1 == 0x11)
DEBUGP(4, dev, "No AUTOPPS necessary "
"TA(1)==0x11\n");
if (test_bit(IS_ANY_T0, &dev->flags)
&& test_bit(IS_ANY_T1, &dev->flags))
DEBUGP(4, dev, "Do NOT perform AUTOPPS "
"with multiprotocol cards\n");
clear_bit(IS_AUTOPPS_ACT, &dev->flags);
wake_up_interruptible(&dev->atrq);
}
} else {
DEBUGP(4, dev, "ATR invalid\n");
wake_up_interruptible(&dev->atrq);
}
break;
case M_BAD_CARD:
DEBUGP(4, dev, "M_BAD_CARD\n");
/* slow down warning, but prompt immediately after insertion */
if (dev->cwarn == 0 || dev->cwarn == 10) {
set_bit(IS_BAD_CARD, &dev->flags);
dev_warn(&dev->p_dev->dev, MODULE_NAME ": ");
if (test_bit(IS_BAD_CSUM, &dev->flags)) {
DEBUGP(4, dev, "ATR checksum (0x%.2x, should "
"be zero) failed\n", dev->atr_csum);
}
#ifdef CM4000_DEBUG
else if (test_bit(IS_BAD_LENGTH, &dev->flags)) {
DEBUGP(4, dev, "ATR length error\n");
} else {
DEBUGP(4, dev, "card damaged or wrong way "
"inserted\n");
}
#endif
dev->cwarn = 0;
wake_up_interruptible(&dev->atrq); /* wake open */
}
dev->cwarn++;
dev->mdelay = T_100MSEC;
dev->mstate = M_FETCH_ATR;
break;
default:
DEBUGP(7, dev, "Unknown action\n");
break; /* nothing */
}
release_io:
DEBUGP(7, dev, "release_io\n");
clear_bit(LOCK_IO, &dev->flags);
wake_up_interruptible(&dev->ioq); /* whoever needs IO */
return_with_timer:
DEBUGP(7, dev, "<- monitor_card (returns with timer)\n");
mod_timer(&dev->timer, jiffies + dev->mdelay);
clear_bit(LOCK_MONITOR, &dev->flags);
}
/* Interface to userland (file_operations) */
static ssize_t cmm_read(struct file *filp, __user char *buf, size_t count,
loff_t *ppos)
{
struct cm4000_dev *dev = filp->private_data;
unsigned int iobase = dev->p_dev->resource[0]->start;
ssize_t rc;
int i, j, k;
DEBUGP(2, dev, "-> cmm_read(%s,%d)\n", current->comm, current->pid);
if (count == 0) /* according to manpage */
return 0;
if (!pcmcia_dev_present(dev->p_dev) || /* device removed */
test_bit(IS_CMM_ABSENT, &dev->flags))
return -ENODEV;
if (test_bit(IS_BAD_CSUM, &dev->flags))
return -EIO;
/* also see the note about this in cmm_write */
if (wait_event_interruptible
(dev->atrq,
((filp->f_flags & O_NONBLOCK)
|| (test_bit(IS_ATR_PRESENT, (void *)&dev->flags) != 0)))) {
if (filp->f_flags & O_NONBLOCK)
return -EAGAIN;
return -ERESTARTSYS;
}
if (test_bit(IS_ATR_VALID, &dev->flags) == 0)
return -EIO;
/* this one implements blocking IO */
if (wait_event_interruptible
(dev->readq,
((filp->f_flags & O_NONBLOCK) || (dev->rpos < dev->rlen)))) {
if (filp->f_flags & O_NONBLOCK)
return -EAGAIN;
return -ERESTARTSYS;
}
/* lock io */
if (wait_event_interruptible
(dev->ioq,
((filp->f_flags & O_NONBLOCK)
|| (test_and_set_bit(LOCK_IO, (void *)&dev->flags) == 0)))) {
if (filp->f_flags & O_NONBLOCK)
return -EAGAIN;
return -ERESTARTSYS;
}
rc = 0;
dev->flags0 = inb(REG_FLAGS0(iobase));
if ((dev->flags0 & 1) == 0 /* no smartcard inserted */
|| dev->flags0 == 0xff) { /* no cardman inserted */
clear_bit(IS_ATR_VALID, &dev->flags);
if (dev->flags0 & 1) {
set_bit(IS_CMM_ABSENT, &dev->flags);
rc = -ENODEV;
} else {
rc = -EIO;
}
goto release_io;
}
DEBUGP(4, dev, "begin read answer\n");
j = min(count, (size_t)(dev->rlen - dev->rpos));
k = dev->rpos;
if (k + j > 255)
j = 256 - k;
DEBUGP(4, dev, "read1 j=%d\n", j);
for (i = 0; i < j; i++) {
xoutb(k++, REG_BUF_ADDR(iobase));
dev->rbuf[i] = xinb(REG_BUF_DATA(iobase));
}
j = min(count, (size_t)(dev->rlen - dev->rpos));
if (k + j > 255) {
DEBUGP(4, dev, "read2 j=%d\n", j);
dev->flags1 |= 0x10; /* MSB buf addr set */