drivers: autoconvert trivial BKL users to private mutex
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / char / pcmcia / cm4000_cs.c
blob7d091b68d247aa27429f0431963e2a0b744a2003
1 /*
2 * A driver for the PCMCIA Smartcard Reader "Omnikey CardMan Mobile 4000"
4 * cm4000_cs.c support.linux@omnikey.com
6 * Tue Oct 23 11:32:43 GMT 2001 herp - cleaned up header files
7 * Sun Jan 20 10:11:15 MET 2002 herp - added modversion header files
8 * Thu Nov 14 16:34:11 GMT 2002 mh - added PPS functionality
9 * Tue Nov 19 16:36:27 GMT 2002 mh - added SUSPEND/RESUME functionailty
10 * Wed Jul 28 12:55:01 CEST 2004 mh - kernel 2.6 adjustments
12 * current version: 2.4.0gm4
14 * (C) 2000,2001,2002,2003,2004 Omnikey AG
16 * (C) 2005-2006 Harald Welte <laforge@gnumonks.org>
17 * - Adhere to Kernel CodingStyle
18 * - Port to 2.6.13 "new" style PCMCIA
19 * - Check for copy_{from,to}_user return values
20 * - Use nonseekable_open()
21 * - add class interface for udev device creation
23 * All rights reserved. Licensed under dual BSD/GPL license.
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/slab.h>
29 #include <linux/init.h>
30 #include <linux/fs.h>
31 #include <linux/delay.h>
32 #include <linux/bitrev.h>
33 #include <linux/mutex.h>
34 #include <linux/uaccess.h>
35 #include <linux/io.h>
37 #include <pcmcia/cs.h>
38 #include <pcmcia/cistpl.h>
39 #include <pcmcia/cisreg.h>
40 #include <pcmcia/ciscode.h>
41 #include <pcmcia/ds.h>
43 #include <linux/cm4000_cs.h>
45 /* #define ATR_CSUM */
47 #define reader_to_dev(x) (&x->p_dev->dev)
49 /* n (debug level) is ignored */
50 /* additional debug output may be enabled by re-compiling with
51 * CM4000_DEBUG set */
52 /* #define CM4000_DEBUG */
53 #define DEBUGP(n, rdr, x, args...) do { \
54 dev_dbg(reader_to_dev(rdr), "%s:" x, \
55 __func__ , ## args); \
56 } while (0)
58 static DEFINE_MUTEX(cmm_mutex);
59 static char *version = "cm4000_cs.c v2.4.0gm6 - All bugs added by Harald Welte";
61 #define T_1SEC (HZ)
62 #define T_10MSEC msecs_to_jiffies(10)
63 #define T_20MSEC msecs_to_jiffies(20)
64 #define T_40MSEC msecs_to_jiffies(40)
65 #define T_50MSEC msecs_to_jiffies(50)
66 #define T_100MSEC msecs_to_jiffies(100)
67 #define T_500MSEC msecs_to_jiffies(500)
69 static void cm4000_release(struct pcmcia_device *link);
71 static int major; /* major number we get from the kernel */
73 /* note: the first state has to have number 0 always */
75 #define M_FETCH_ATR 0
76 #define M_TIMEOUT_WAIT 1
77 #define M_READ_ATR_LEN 2
78 #define M_READ_ATR 3
79 #define M_ATR_PRESENT 4
80 #define M_BAD_CARD 5
81 #define M_CARDOFF 6
83 #define LOCK_IO 0
84 #define LOCK_MONITOR 1
86 #define IS_AUTOPPS_ACT 6
87 #define IS_PROCBYTE_PRESENT 7
88 #define IS_INVREV 8
89 #define IS_ANY_T0 9
90 #define IS_ANY_T1 10
91 #define IS_ATR_PRESENT 11
92 #define IS_ATR_VALID 12
93 #define IS_CMM_ABSENT 13
94 #define IS_BAD_LENGTH 14
95 #define IS_BAD_CSUM 15
96 #define IS_BAD_CARD 16
98 #define REG_FLAGS0(x) (x + 0)
99 #define REG_FLAGS1(x) (x + 1)
100 #define REG_NUM_BYTES(x) (x + 2)
101 #define REG_BUF_ADDR(x) (x + 3)
102 #define REG_BUF_DATA(x) (x + 4)
103 #define REG_NUM_SEND(x) (x + 5)
104 #define REG_BAUDRATE(x) (x + 6)
105 #define REG_STOPBITS(x) (x + 7)
107 struct cm4000_dev {
108 struct pcmcia_device *p_dev;
110 unsigned char atr[MAX_ATR];
111 unsigned char rbuf[512];
112 unsigned char sbuf[512];
114 wait_queue_head_t devq; /* when removing cardman must not be
115 zeroed! */
117 wait_queue_head_t ioq; /* if IO is locked, wait on this Q */
118 wait_queue_head_t atrq; /* wait for ATR valid */
119 wait_queue_head_t readq; /* used by write to wake blk.read */
121 /* warning: do not move this fields.
122 * initialising to zero depends on it - see ZERO_DEV below. */
123 unsigned char atr_csum;
124 unsigned char atr_len_retry;
125 unsigned short atr_len;
126 unsigned short rlen; /* bytes avail. after write */
127 unsigned short rpos; /* latest read pos. write zeroes */
128 unsigned char procbyte; /* T=0 procedure byte */
129 unsigned char mstate; /* state of card monitor */
130 unsigned char cwarn; /* slow down warning */
131 unsigned char flags0; /* cardman IO-flags 0 */
132 unsigned char flags1; /* cardman IO-flags 1 */
133 unsigned int mdelay; /* variable monitor speeds, in jiffies */
135 unsigned int baudv; /* baud value for speed */
136 unsigned char ta1;
137 unsigned char proto; /* T=0, T=1, ... */
138 unsigned long flags; /* lock+flags (MONITOR,IO,ATR) * for concurrent
139 access */
141 unsigned char pts[4];
143 struct timer_list timer; /* used to keep monitor running */
144 int monitor_running;
147 #define ZERO_DEV(dev) \
148 memset(&dev->atr_csum,0, \
149 sizeof(struct cm4000_dev) - \
150 offsetof(struct cm4000_dev, atr_csum))
152 static struct pcmcia_device *dev_table[CM4000_MAX_DEV];
153 static struct class *cmm_class;
155 /* This table doesn't use spaces after the comma between fields and thus
156 * violates CodingStyle. However, I don't really think wrapping it around will
157 * make it any clearer to read -HW */
158 static unsigned char fi_di_table[10][14] = {
159 /*FI 00 01 02 03 04 05 06 07 08 09 10 11 12 13 */
160 /*DI */
161 /* 0 */ {0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11},
162 /* 1 */ {0x01,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x91,0x11,0x11,0x11,0x11},
163 /* 2 */ {0x02,0x12,0x22,0x32,0x11,0x11,0x11,0x11,0x11,0x92,0xA2,0xB2,0x11,0x11},
164 /* 3 */ {0x03,0x13,0x23,0x33,0x43,0x53,0x63,0x11,0x11,0x93,0xA3,0xB3,0xC3,0xD3},
165 /* 4 */ {0x04,0x14,0x24,0x34,0x44,0x54,0x64,0x11,0x11,0x94,0xA4,0xB4,0xC4,0xD4},
166 /* 5 */ {0x00,0x15,0x25,0x35,0x45,0x55,0x65,0x11,0x11,0x95,0xA5,0xB5,0xC5,0xD5},
167 /* 6 */ {0x06,0x16,0x26,0x36,0x46,0x56,0x66,0x11,0x11,0x96,0xA6,0xB6,0xC6,0xD6},
168 /* 7 */ {0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11},
169 /* 8 */ {0x08,0x11,0x28,0x38,0x48,0x58,0x68,0x11,0x11,0x98,0xA8,0xB8,0xC8,0xD8},
170 /* 9 */ {0x09,0x19,0x29,0x39,0x49,0x59,0x69,0x11,0x11,0x99,0xA9,0xB9,0xC9,0xD9}
173 #ifndef CM4000_DEBUG
174 #define xoutb outb
175 #define xinb inb
176 #else
177 static inline void xoutb(unsigned char val, unsigned short port)
179 pr_debug("outb(val=%.2x,port=%.4x)\n", val, port);
180 outb(val, port);
182 static inline unsigned char xinb(unsigned short port)
184 unsigned char val;
186 val = inb(port);
187 pr_debug("%.2x=inb(%.4x)\n", val, port);
189 return val;
191 #endif
193 static inline unsigned char invert_revert(unsigned char ch)
195 return bitrev8(~ch);
198 static void str_invert_revert(unsigned char *b, int len)
200 int i;
202 for (i = 0; i < len; i++)
203 b[i] = invert_revert(b[i]);
206 #define ATRLENCK(dev,pos) \
207 if (pos>=dev->atr_len || pos>=MAX_ATR) \
208 goto return_0;
210 static unsigned int calc_baudv(unsigned char fidi)
212 unsigned int wcrcf, wbrcf, fi_rfu, di_rfu;
214 fi_rfu = 372;
215 di_rfu = 1;
217 /* FI */
218 switch ((fidi >> 4) & 0x0F) {
219 case 0x00:
220 wcrcf = 372;
221 break;
222 case 0x01:
223 wcrcf = 372;
224 break;
225 case 0x02:
226 wcrcf = 558;
227 break;
228 case 0x03:
229 wcrcf = 744;
230 break;
231 case 0x04:
232 wcrcf = 1116;
233 break;
234 case 0x05:
235 wcrcf = 1488;
236 break;
237 case 0x06:
238 wcrcf = 1860;
239 break;
240 case 0x07:
241 wcrcf = fi_rfu;
242 break;
243 case 0x08:
244 wcrcf = fi_rfu;
245 break;
246 case 0x09:
247 wcrcf = 512;
248 break;
249 case 0x0A:
250 wcrcf = 768;
251 break;
252 case 0x0B:
253 wcrcf = 1024;
254 break;
255 case 0x0C:
256 wcrcf = 1536;
257 break;
258 case 0x0D:
259 wcrcf = 2048;
260 break;
261 default:
262 wcrcf = fi_rfu;
263 break;
266 /* DI */
267 switch (fidi & 0x0F) {
268 case 0x00:
269 wbrcf = di_rfu;
270 break;
271 case 0x01:
272 wbrcf = 1;
273 break;
274 case 0x02:
275 wbrcf = 2;
276 break;
277 case 0x03:
278 wbrcf = 4;
279 break;
280 case 0x04:
281 wbrcf = 8;
282 break;
283 case 0x05:
284 wbrcf = 16;
285 break;
286 case 0x06:
287 wbrcf = 32;
288 break;
289 case 0x07:
290 wbrcf = di_rfu;
291 break;
292 case 0x08:
293 wbrcf = 12;
294 break;
295 case 0x09:
296 wbrcf = 20;
297 break;
298 default:
299 wbrcf = di_rfu;
300 break;
303 return (wcrcf / wbrcf);
306 static unsigned short io_read_num_rec_bytes(unsigned int iobase,
307 unsigned short *s)
309 unsigned short tmp;
311 tmp = *s = 0;
312 do {
313 *s = tmp;
314 tmp = inb(REG_NUM_BYTES(iobase)) |
315 (inb(REG_FLAGS0(iobase)) & 4 ? 0x100 : 0);
316 } while (tmp != *s);
318 return *s;
321 static int parse_atr(struct cm4000_dev *dev)
323 unsigned char any_t1, any_t0;
324 unsigned char ch, ifno;
325 int ix, done;
327 DEBUGP(3, dev, "-> parse_atr: dev->atr_len = %i\n", dev->atr_len);
329 if (dev->atr_len < 3) {
330 DEBUGP(5, dev, "parse_atr: atr_len < 3\n");
331 return 0;
334 if (dev->atr[0] == 0x3f)
335 set_bit(IS_INVREV, &dev->flags);
336 else
337 clear_bit(IS_INVREV, &dev->flags);
338 ix = 1;
339 ifno = 1;
340 ch = dev->atr[1];
341 dev->proto = 0; /* XXX PROTO */
342 any_t1 = any_t0 = done = 0;
343 dev->ta1 = 0x11; /* defaults to 9600 baud */
344 do {
345 if (ifno == 1 && (ch & 0x10)) {
346 /* read first interface byte and TA1 is present */
347 dev->ta1 = dev->atr[2];
348 DEBUGP(5, dev, "Card says FiDi is 0x%.2x\n", dev->ta1);
349 ifno++;
350 } else if ((ifno == 2) && (ch & 0x10)) { /* TA(2) */
351 dev->ta1 = 0x11;
352 ifno++;
355 DEBUGP(5, dev, "Yi=%.2x\n", ch & 0xf0);
356 ix += ((ch & 0x10) >> 4) /* no of int.face chars */
357 +((ch & 0x20) >> 5)
358 + ((ch & 0x40) >> 6)
359 + ((ch & 0x80) >> 7);
360 /* ATRLENCK(dev,ix); */
361 if (ch & 0x80) { /* TDi */
362 ch = dev->atr[ix];
363 if ((ch & 0x0f)) {
364 any_t1 = 1;
365 DEBUGP(5, dev, "card is capable of T=1\n");
366 } else {
367 any_t0 = 1;
368 DEBUGP(5, dev, "card is capable of T=0\n");
370 } else
371 done = 1;
372 } while (!done);
374 DEBUGP(5, dev, "ix=%d noHist=%d any_t1=%d\n",
375 ix, dev->atr[1] & 15, any_t1);
376 if (ix + 1 + (dev->atr[1] & 0x0f) + any_t1 != dev->atr_len) {
377 DEBUGP(5, dev, "length error\n");
378 return 0;
380 if (any_t0)
381 set_bit(IS_ANY_T0, &dev->flags);
383 if (any_t1) { /* compute csum */
384 dev->atr_csum = 0;
385 #ifdef ATR_CSUM
386 for (i = 1; i < dev->atr_len; i++)
387 dev->atr_csum ^= dev->atr[i];
388 if (dev->atr_csum) {
389 set_bit(IS_BAD_CSUM, &dev->flags);
390 DEBUGP(5, dev, "bad checksum\n");
391 goto return_0;
393 #endif
394 if (any_t0 == 0)
395 dev->proto = 1; /* XXX PROTO */
396 set_bit(IS_ANY_T1, &dev->flags);
399 return 1;
402 struct card_fixup {
403 char atr[12];
404 u_int8_t atr_len;
405 u_int8_t stopbits;
408 static struct card_fixup card_fixups[] = {
409 { /* ACOS */
410 .atr = { 0x3b, 0xb3, 0x11, 0x00, 0x00, 0x41, 0x01 },
411 .atr_len = 7,
412 .stopbits = 0x03,
414 { /* Motorola */
415 .atr = {0x3b, 0x76, 0x13, 0x00, 0x00, 0x80, 0x62, 0x07,
416 0x41, 0x81, 0x81 },
417 .atr_len = 11,
418 .stopbits = 0x04,
422 static void set_cardparameter(struct cm4000_dev *dev)
424 int i;
425 unsigned int iobase = dev->p_dev->resource[0]->start;
426 u_int8_t stopbits = 0x02; /* ISO default */
428 DEBUGP(3, dev, "-> set_cardparameter\n");
430 dev->flags1 = dev->flags1 | (((dev->baudv - 1) & 0x0100) >> 8);
431 xoutb(dev->flags1, REG_FLAGS1(iobase));
432 DEBUGP(5, dev, "flags1 = 0x%02x\n", dev->flags1);
434 /* set baudrate */
435 xoutb((unsigned char)((dev->baudv - 1) & 0xFF), REG_BAUDRATE(iobase));
437 DEBUGP(5, dev, "baudv = %i -> write 0x%02x\n", dev->baudv,
438 ((dev->baudv - 1) & 0xFF));
440 /* set stopbits */
441 for (i = 0; i < ARRAY_SIZE(card_fixups); i++) {
442 if (!memcmp(dev->atr, card_fixups[i].atr,
443 card_fixups[i].atr_len))
444 stopbits = card_fixups[i].stopbits;
446 xoutb(stopbits, REG_STOPBITS(iobase));
448 DEBUGP(3, dev, "<- set_cardparameter\n");
451 static int set_protocol(struct cm4000_dev *dev, struct ptsreq *ptsreq)
454 unsigned long tmp, i;
455 unsigned short num_bytes_read;
456 unsigned char pts_reply[4];
457 ssize_t rc;
458 unsigned int iobase = dev->p_dev->resource[0]->start;
460 rc = 0;
462 DEBUGP(3, dev, "-> set_protocol\n");
463 DEBUGP(5, dev, "ptsreq->Protocol = 0x%.8x, ptsreq->Flags=0x%.8x, "
464 "ptsreq->pts1=0x%.2x, ptsreq->pts2=0x%.2x, "
465 "ptsreq->pts3=0x%.2x\n", (unsigned int)ptsreq->protocol,
466 (unsigned int)ptsreq->flags, ptsreq->pts1, ptsreq->pts2,
467 ptsreq->pts3);
469 /* Fill PTS structure */
470 dev->pts[0] = 0xff;
471 dev->pts[1] = 0x00;
472 tmp = ptsreq->protocol;
473 while ((tmp = (tmp >> 1)) > 0)
474 dev->pts[1]++;
475 dev->proto = dev->pts[1]; /* Set new protocol */
476 dev->pts[1] = (0x01 << 4) | (dev->pts[1]);
478 /* Correct Fi/Di according to CM4000 Fi/Di table */
479 DEBUGP(5, dev, "Ta(1) from ATR is 0x%.2x\n", dev->ta1);
480 /* set Fi/Di according to ATR TA(1) */
481 dev->pts[2] = fi_di_table[dev->ta1 & 0x0F][(dev->ta1 >> 4) & 0x0F];
483 /* Calculate PCK character */
484 dev->pts[3] = dev->pts[0] ^ dev->pts[1] ^ dev->pts[2];
486 DEBUGP(5, dev, "pts0=%.2x, pts1=%.2x, pts2=%.2x, pts3=%.2x\n",
487 dev->pts[0], dev->pts[1], dev->pts[2], dev->pts[3]);
489 /* check card convention */
490 if (test_bit(IS_INVREV, &dev->flags))
491 str_invert_revert(dev->pts, 4);
493 /* reset SM */
494 xoutb(0x80, REG_FLAGS0(iobase));
496 /* Enable access to the message buffer */
497 DEBUGP(5, dev, "Enable access to the messages buffer\n");
498 dev->flags1 = 0x20 /* T_Active */
499 | (test_bit(IS_INVREV, &dev->flags) ? 0x02 : 0x00) /* inv parity */
500 | ((dev->baudv >> 8) & 0x01); /* MSB-baud */
501 xoutb(dev->flags1, REG_FLAGS1(iobase));
503 DEBUGP(5, dev, "Enable message buffer -> flags1 = 0x%.2x\n",
504 dev->flags1);
506 /* write challenge to the buffer */
507 DEBUGP(5, dev, "Write challenge to buffer: ");
508 for (i = 0; i < 4; i++) {
509 xoutb(i, REG_BUF_ADDR(iobase));
510 xoutb(dev->pts[i], REG_BUF_DATA(iobase)); /* buf data */
511 #ifdef CM4000_DEBUG
512 pr_debug("0x%.2x ", dev->pts[i]);
514 pr_debug("\n");
515 #else
517 #endif
519 /* set number of bytes to write */
520 DEBUGP(5, dev, "Set number of bytes to write\n");
521 xoutb(0x04, REG_NUM_SEND(iobase));
523 /* Trigger CARDMAN CONTROLLER */
524 xoutb(0x50, REG_FLAGS0(iobase));
526 /* Monitor progress */
527 /* wait for xmit done */
528 DEBUGP(5, dev, "Waiting for NumRecBytes getting valid\n");
530 for (i = 0; i < 100; i++) {
531 if (inb(REG_FLAGS0(iobase)) & 0x08) {
532 DEBUGP(5, dev, "NumRecBytes is valid\n");
533 break;
535 mdelay(10);
537 if (i == 100) {
538 DEBUGP(5, dev, "Timeout waiting for NumRecBytes getting "
539 "valid\n");
540 rc = -EIO;
541 goto exit_setprotocol;
544 DEBUGP(5, dev, "Reading NumRecBytes\n");
545 for (i = 0; i < 100; i++) {
546 io_read_num_rec_bytes(iobase, &num_bytes_read);
547 if (num_bytes_read >= 4) {
548 DEBUGP(2, dev, "NumRecBytes = %i\n", num_bytes_read);
549 break;
551 mdelay(10);
554 /* check whether it is a short PTS reply? */
555 if (num_bytes_read == 3)
556 i = 0;
558 if (i == 100) {
559 DEBUGP(5, dev, "Timeout reading num_bytes_read\n");
560 rc = -EIO;
561 goto exit_setprotocol;
564 DEBUGP(5, dev, "Reset the CARDMAN CONTROLLER\n");
565 xoutb(0x80, REG_FLAGS0(iobase));
567 /* Read PPS reply */
568 DEBUGP(5, dev, "Read PPS reply\n");
569 for (i = 0; i < num_bytes_read; i++) {
570 xoutb(i, REG_BUF_ADDR(iobase));
571 pts_reply[i] = inb(REG_BUF_DATA(iobase));
574 #ifdef CM4000_DEBUG
575 DEBUGP(2, dev, "PTSreply: ");
576 for (i = 0; i < num_bytes_read; i++) {
577 pr_debug("0x%.2x ", pts_reply[i]);
579 pr_debug("\n");
580 #endif /* CM4000_DEBUG */
582 DEBUGP(5, dev, "Clear Tactive in Flags1\n");
583 xoutb(0x20, REG_FLAGS1(iobase));
585 /* Compare ptsreq and ptsreply */
586 if ((dev->pts[0] == pts_reply[0]) &&
587 (dev->pts[1] == pts_reply[1]) &&
588 (dev->pts[2] == pts_reply[2]) && (dev->pts[3] == pts_reply[3])) {
589 /* setcardparameter according to PPS */
590 dev->baudv = calc_baudv(dev->pts[2]);
591 set_cardparameter(dev);
592 } else if ((dev->pts[0] == pts_reply[0]) &&
593 ((dev->pts[1] & 0xef) == pts_reply[1]) &&
594 ((pts_reply[0] ^ pts_reply[1]) == pts_reply[2])) {
595 /* short PTS reply, set card parameter to default values */
596 dev->baudv = calc_baudv(0x11);
597 set_cardparameter(dev);
598 } else
599 rc = -EIO;
601 exit_setprotocol:
602 DEBUGP(3, dev, "<- set_protocol\n");
603 return rc;
606 static int io_detect_cm4000(unsigned int iobase, struct cm4000_dev *dev)
609 /* note: statemachine is assumed to be reset */
610 if (inb(REG_FLAGS0(iobase)) & 8) {
611 clear_bit(IS_ATR_VALID, &dev->flags);
612 set_bit(IS_CMM_ABSENT, &dev->flags);
613 return 0; /* detect CMM = 1 -> failure */
615 /* xoutb(0x40, REG_FLAGS1(iobase)); detectCMM */
616 xoutb(dev->flags1 | 0x40, REG_FLAGS1(iobase));
617 if ((inb(REG_FLAGS0(iobase)) & 8) == 0) {
618 clear_bit(IS_ATR_VALID, &dev->flags);
619 set_bit(IS_CMM_ABSENT, &dev->flags);
620 return 0; /* detect CMM=0 -> failure */
622 /* clear detectCMM again by restoring original flags1 */
623 xoutb(dev->flags1, REG_FLAGS1(iobase));
624 return 1;
627 static void terminate_monitor(struct cm4000_dev *dev)
630 /* tell the monitor to stop and wait until
631 * it terminates.
633 DEBUGP(3, dev, "-> terminate_monitor\n");
634 wait_event_interruptible(dev->devq,
635 test_and_set_bit(LOCK_MONITOR,
636 (void *)&dev->flags));
638 /* now, LOCK_MONITOR has been set.
639 * allow a last cycle in the monitor.
640 * the monitor will indicate that it has
641 * finished by clearing this bit.
643 DEBUGP(5, dev, "Now allow last cycle of monitor!\n");
644 while (test_bit(LOCK_MONITOR, (void *)&dev->flags))
645 msleep(25);
647 DEBUGP(5, dev, "Delete timer\n");
648 del_timer_sync(&dev->timer);
649 #ifdef CM4000_DEBUG
650 dev->monitor_running = 0;
651 #endif
653 DEBUGP(3, dev, "<- terminate_monitor\n");
657 * monitor the card every 50msec. as a side-effect, retrieve the
658 * atr once a card is inserted. another side-effect of retrieving the
659 * atr is that the card will be powered on, so there is no need to
660 * power on the card explictely from the application: the driver
661 * is already doing that for you.
664 static void monitor_card(unsigned long p)
666 struct cm4000_dev *dev = (struct cm4000_dev *) p;
667 unsigned int iobase = dev->p_dev->resource[0]->start;
668 unsigned short s;
669 struct ptsreq ptsreq;
670 int i, atrc;
672 DEBUGP(7, dev, "-> monitor_card\n");
674 /* if someone has set the lock for us: we're done! */
675 if (test_and_set_bit(LOCK_MONITOR, &dev->flags)) {
676 DEBUGP(4, dev, "About to stop monitor\n");
677 /* no */
678 dev->rlen =
679 dev->rpos =
680 dev->atr_csum = dev->atr_len_retry = dev->cwarn = 0;
681 dev->mstate = M_FETCH_ATR;
682 clear_bit(LOCK_MONITOR, &dev->flags);
683 /* close et al. are sleeping on devq, so wake it */
684 wake_up_interruptible(&dev->devq);
685 DEBUGP(2, dev, "<- monitor_card (we are done now)\n");
686 return;
689 /* try to lock io: if it is already locked, just add another timer */
690 if (test_and_set_bit(LOCK_IO, (void *)&dev->flags)) {
691 DEBUGP(4, dev, "Couldn't get IO lock\n");
692 goto return_with_timer;
695 /* is a card/a reader inserted at all ? */
696 dev->flags0 = xinb(REG_FLAGS0(iobase));
697 DEBUGP(7, dev, "dev->flags0 = 0x%2x\n", dev->flags0);
698 DEBUGP(7, dev, "smartcard present: %s\n",
699 dev->flags0 & 1 ? "yes" : "no");
700 DEBUGP(7, dev, "cardman present: %s\n",
701 dev->flags0 == 0xff ? "no" : "yes");
703 if ((dev->flags0 & 1) == 0 /* no smartcard inserted */
704 || dev->flags0 == 0xff) { /* no cardman inserted */
705 /* no */
706 dev->rlen =
707 dev->rpos =
708 dev->atr_csum = dev->atr_len_retry = dev->cwarn = 0;
709 dev->mstate = M_FETCH_ATR;
711 dev->flags &= 0x000000ff; /* only keep IO and MONITOR locks */
713 if (dev->flags0 == 0xff) {
714 DEBUGP(4, dev, "set IS_CMM_ABSENT bit\n");
715 set_bit(IS_CMM_ABSENT, &dev->flags);
716 } else if (test_bit(IS_CMM_ABSENT, &dev->flags)) {
717 DEBUGP(4, dev, "clear IS_CMM_ABSENT bit "
718 "(card is removed)\n");
719 clear_bit(IS_CMM_ABSENT, &dev->flags);
722 goto release_io;
723 } else if ((dev->flags0 & 1) && test_bit(IS_CMM_ABSENT, &dev->flags)) {
724 /* cardman and card present but cardman was absent before
725 * (after suspend with inserted card) */
726 DEBUGP(4, dev, "clear IS_CMM_ABSENT bit (card is inserted)\n");
727 clear_bit(IS_CMM_ABSENT, &dev->flags);
730 if (test_bit(IS_ATR_VALID, &dev->flags) == 1) {
731 DEBUGP(7, dev, "believe ATR is already valid (do nothing)\n");
732 goto release_io;
735 switch (dev->mstate) {
736 unsigned char flags0;
737 case M_CARDOFF:
738 DEBUGP(4, dev, "M_CARDOFF\n");
739 flags0 = inb(REG_FLAGS0(iobase));
740 if (flags0 & 0x02) {
741 /* wait until Flags0 indicate power is off */
742 dev->mdelay = T_10MSEC;
743 } else {
744 /* Flags0 indicate power off and no card inserted now;
745 * Reset CARDMAN CONTROLLER */
746 xoutb(0x80, REG_FLAGS0(iobase));
748 /* prepare for fetching ATR again: after card off ATR
749 * is read again automatically */
750 dev->rlen =
751 dev->rpos =
752 dev->atr_csum =
753 dev->atr_len_retry = dev->cwarn = 0;
754 dev->mstate = M_FETCH_ATR;
756 /* minimal gap between CARDOFF and read ATR is 50msec */
757 dev->mdelay = T_50MSEC;
759 break;
760 case M_FETCH_ATR:
761 DEBUGP(4, dev, "M_FETCH_ATR\n");
762 xoutb(0x80, REG_FLAGS0(iobase));
763 DEBUGP(4, dev, "Reset BAUDV to 9600\n");
764 dev->baudv = 0x173; /* 9600 */
765 xoutb(0x02, REG_STOPBITS(iobase)); /* stopbits=2 */
766 xoutb(0x73, REG_BAUDRATE(iobase)); /* baud value */
767 xoutb(0x21, REG_FLAGS1(iobase)); /* T_Active=1, baud
768 value */
769 /* warm start vs. power on: */
770 xoutb(dev->flags0 & 2 ? 0x46 : 0x44, REG_FLAGS0(iobase));
771 dev->mdelay = T_40MSEC;
772 dev->mstate = M_TIMEOUT_WAIT;
773 break;
774 case M_TIMEOUT_WAIT:
775 DEBUGP(4, dev, "M_TIMEOUT_WAIT\n");
776 /* numRecBytes */
777 io_read_num_rec_bytes(iobase, &dev->atr_len);
778 dev->mdelay = T_10MSEC;
779 dev->mstate = M_READ_ATR_LEN;
780 break;
781 case M_READ_ATR_LEN:
782 DEBUGP(4, dev, "M_READ_ATR_LEN\n");
783 /* infinite loop possible, since there is no timeout */
785 #define MAX_ATR_LEN_RETRY 100
787 if (dev->atr_len == io_read_num_rec_bytes(iobase, &s)) {
788 if (dev->atr_len_retry++ >= MAX_ATR_LEN_RETRY) { /* + XX msec */
789 dev->mdelay = T_10MSEC;
790 dev->mstate = M_READ_ATR;
792 } else {
793 dev->atr_len = s;
794 dev->atr_len_retry = 0; /* set new timeout */
797 DEBUGP(4, dev, "Current ATR_LEN = %i\n", dev->atr_len);
798 break;
799 case M_READ_ATR:
800 DEBUGP(4, dev, "M_READ_ATR\n");
801 xoutb(0x80, REG_FLAGS0(iobase)); /* reset SM */
802 for (i = 0; i < dev->atr_len; i++) {
803 xoutb(i, REG_BUF_ADDR(iobase));
804 dev->atr[i] = inb(REG_BUF_DATA(iobase));
806 /* Deactivate T_Active flags */
807 DEBUGP(4, dev, "Deactivate T_Active flags\n");
808 dev->flags1 = 0x01;
809 xoutb(dev->flags1, REG_FLAGS1(iobase));
811 /* atr is present (which doesnt mean it's valid) */
812 set_bit(IS_ATR_PRESENT, &dev->flags);
813 if (dev->atr[0] == 0x03)
814 str_invert_revert(dev->atr, dev->atr_len);
815 atrc = parse_atr(dev);
816 if (atrc == 0) { /* atr invalid */
817 dev->mdelay = 0;
818 dev->mstate = M_BAD_CARD;
819 } else {
820 dev->mdelay = T_50MSEC;
821 dev->mstate = M_ATR_PRESENT;
822 set_bit(IS_ATR_VALID, &dev->flags);
825 if (test_bit(IS_ATR_VALID, &dev->flags) == 1) {
826 DEBUGP(4, dev, "monitor_card: ATR valid\n");
827 /* if ta1 == 0x11, no PPS necessary (default values) */
828 /* do not do PPS with multi protocol cards */
829 if ((test_bit(IS_AUTOPPS_ACT, &dev->flags) == 0) &&
830 (dev->ta1 != 0x11) &&
831 !(test_bit(IS_ANY_T0, &dev->flags) &&
832 test_bit(IS_ANY_T1, &dev->flags))) {
833 DEBUGP(4, dev, "Perform AUTOPPS\n");
834 set_bit(IS_AUTOPPS_ACT, &dev->flags);
835 ptsreq.protocol = ptsreq.protocol =
836 (0x01 << dev->proto);
837 ptsreq.flags = 0x01;
838 ptsreq.pts1 = 0x00;
839 ptsreq.pts2 = 0x00;
840 ptsreq.pts3 = 0x00;
841 if (set_protocol(dev, &ptsreq) == 0) {
842 DEBUGP(4, dev, "AUTOPPS ret SUCC\n");
843 clear_bit(IS_AUTOPPS_ACT, &dev->flags);
844 wake_up_interruptible(&dev->atrq);
845 } else {
846 DEBUGP(4, dev, "AUTOPPS failed: "
847 "repower using defaults\n");
848 /* prepare for repowering */
849 clear_bit(IS_ATR_PRESENT, &dev->flags);
850 clear_bit(IS_ATR_VALID, &dev->flags);
851 dev->rlen =
852 dev->rpos =
853 dev->atr_csum =
854 dev->atr_len_retry = dev->cwarn = 0;
855 dev->mstate = M_FETCH_ATR;
857 dev->mdelay = T_50MSEC;
859 } else {
860 /* for cards which use slightly different
861 * params (extra guard time) */
862 set_cardparameter(dev);
863 if (test_bit(IS_AUTOPPS_ACT, &dev->flags) == 1)
864 DEBUGP(4, dev, "AUTOPPS already active "
865 "2nd try:use default values\n");
866 if (dev->ta1 == 0x11)
867 DEBUGP(4, dev, "No AUTOPPS necessary "
868 "TA(1)==0x11\n");
869 if (test_bit(IS_ANY_T0, &dev->flags)
870 && test_bit(IS_ANY_T1, &dev->flags))
871 DEBUGP(4, dev, "Do NOT perform AUTOPPS "
872 "with multiprotocol cards\n");
873 clear_bit(IS_AUTOPPS_ACT, &dev->flags);
874 wake_up_interruptible(&dev->atrq);
876 } else {
877 DEBUGP(4, dev, "ATR invalid\n");
878 wake_up_interruptible(&dev->atrq);
880 break;
881 case M_BAD_CARD:
882 DEBUGP(4, dev, "M_BAD_CARD\n");
883 /* slow down warning, but prompt immediately after insertion */
884 if (dev->cwarn == 0 || dev->cwarn == 10) {
885 set_bit(IS_BAD_CARD, &dev->flags);
886 dev_warn(&dev->p_dev->dev, MODULE_NAME ": ");
887 if (test_bit(IS_BAD_CSUM, &dev->flags)) {
888 DEBUGP(4, dev, "ATR checksum (0x%.2x, should "
889 "be zero) failed\n", dev->atr_csum);
891 #ifdef CM4000_DEBUG
892 else if (test_bit(IS_BAD_LENGTH, &dev->flags)) {
893 DEBUGP(4, dev, "ATR length error\n");
894 } else {
895 DEBUGP(4, dev, "card damaged or wrong way "
896 "inserted\n");
898 #endif
899 dev->cwarn = 0;
900 wake_up_interruptible(&dev->atrq); /* wake open */
902 dev->cwarn++;
903 dev->mdelay = T_100MSEC;
904 dev->mstate = M_FETCH_ATR;
905 break;
906 default:
907 DEBUGP(7, dev, "Unknown action\n");
908 break; /* nothing */
911 release_io:
912 DEBUGP(7, dev, "release_io\n");
913 clear_bit(LOCK_IO, &dev->flags);
914 wake_up_interruptible(&dev->ioq); /* whoever needs IO */
916 return_with_timer:
917 DEBUGP(7, dev, "<- monitor_card (returns with timer)\n");
918 mod_timer(&dev->timer, jiffies + dev->mdelay);
919 clear_bit(LOCK_MONITOR, &dev->flags);
922 /* Interface to userland (file_operations) */
924 static ssize_t cmm_read(struct file *filp, __user char *buf, size_t count,
925 loff_t *ppos)
927 struct cm4000_dev *dev = filp->private_data;
928 unsigned int iobase = dev->p_dev->resource[0]->start;
929 ssize_t rc;
930 int i, j, k;
932 DEBUGP(2, dev, "-> cmm_read(%s,%d)\n", current->comm, current->pid);
934 if (count == 0) /* according to manpage */
935 return 0;
937 if (!pcmcia_dev_present(dev->p_dev) || /* device removed */
938 test_bit(IS_CMM_ABSENT, &dev->flags))
939 return -ENODEV;
941 if (test_bit(IS_BAD_CSUM, &dev->flags))
942 return -EIO;
944 /* also see the note about this in cmm_write */
945 if (wait_event_interruptible
946 (dev->atrq,
947 ((filp->f_flags & O_NONBLOCK)
948 || (test_bit(IS_ATR_PRESENT, (void *)&dev->flags) != 0)))) {
949 if (filp->f_flags & O_NONBLOCK)
950 return -EAGAIN;
951 return -ERESTARTSYS;
954 if (test_bit(IS_ATR_VALID, &dev->flags) == 0)
955 return -EIO;
957 /* this one implements blocking IO */
958 if (wait_event_interruptible
959 (dev->readq,
960 ((filp->f_flags & O_NONBLOCK) || (dev->rpos < dev->rlen)))) {
961 if (filp->f_flags & O_NONBLOCK)
962 return -EAGAIN;
963 return -ERESTARTSYS;
966 /* lock io */
967 if (wait_event_interruptible
968 (dev->ioq,
969 ((filp->f_flags & O_NONBLOCK)
970 || (test_and_set_bit(LOCK_IO, (void *)&dev->flags) == 0)))) {
971 if (filp->f_flags & O_NONBLOCK)
972 return -EAGAIN;
973 return -ERESTARTSYS;
976 rc = 0;
977 dev->flags0 = inb(REG_FLAGS0(iobase));
978 if ((dev->flags0 & 1) == 0 /* no smartcard inserted */
979 || dev->flags0 == 0xff) { /* no cardman inserted */
980 clear_bit(IS_ATR_VALID, &dev->flags);
981 if (dev->flags0 & 1) {
982 set_bit(IS_CMM_ABSENT, &dev->flags);
983 rc = -ENODEV;
985 rc = -EIO;
986 goto release_io;
989 DEBUGP(4, dev, "begin read answer\n");
990 j = min(count, (size_t)(dev->rlen - dev->rpos));
991 k = dev->rpos;
992 if (k + j > 255)
993 j = 256 - k;
994 DEBUGP(4, dev, "read1 j=%d\n", j);
995 for (i = 0; i < j; i++) {
996 xoutb(k++, REG_BUF_ADDR(iobase));
997 dev->rbuf[i] = xinb(REG_BUF_DATA(iobase));
999 j = min(count, (size_t)(dev->rlen - dev->rpos));
1000 if (k + j > 255) {
1001 DEBUGP(4, dev, "read2 j=%d\n", j);
1002 dev->flags1 |= 0x10; /* MSB buf addr set */
1003 xoutb(dev->flags1, REG_FLAGS1(iobase));
1004 for (; i < j; i++) {
1005 xoutb(k++, REG_BUF_ADDR(iobase));
1006 dev->rbuf[i] = xinb(REG_BUF_DATA(iobase));
1010 if (dev->proto == 0 && count > dev->rlen - dev->rpos && i) {
1011 DEBUGP(4, dev, "T=0 and count > buffer\n");
1012 dev->rbuf[i] = dev->rbuf[i - 1];
1013 dev->rbuf[i - 1] = dev->procbyte;
1014 j++;
1016 count = j;
1018 dev->rpos = dev->rlen + 1;
1020 /* Clear T1Active */
1021 DEBUGP(4, dev, "Clear T1Active\n");
1022 dev->flags1 &= 0xdf;
1023 xoutb(dev->flags1, REG_FLAGS1(iobase));
1025 xoutb(0, REG_FLAGS1(iobase)); /* clear detectCMM */
1026 /* last check before exit */
1027 if (!io_detect_cm4000(iobase, dev)) {
1028 rc = -ENODEV;
1029 goto release_io;
1032 if (test_bit(IS_INVREV, &dev->flags) && count > 0)
1033 str_invert_revert(dev->rbuf, count);
1035 if (copy_to_user(buf, dev->rbuf, count))
1036 rc = -EFAULT;
1038 release_io:
1039 clear_bit(LOCK_IO, &dev->flags);
1040 wake_up_interruptible(&dev->ioq);
1042 DEBUGP(2, dev, "<- cmm_read returns: rc = %Zi\n",
1043 (rc < 0 ? rc : count));
1044 return rc < 0 ? rc : count;
1047 static ssize_t cmm_write(struct file *filp, const char __user *buf,
1048 size_t count, loff_t *ppos)
1050 struct cm4000_dev *dev = filp->private_data;
1051 unsigned int iobase = dev->p_dev->resource[0]->start;
1052 unsigned short s;
1053 unsigned char tmp;
1054 unsigned char infolen;
1055 unsigned char sendT0;
1056 unsigned short nsend;
1057 unsigned short nr;
1058 ssize_t rc;
1059 int i;
1061 DEBUGP(2, dev, "-> cmm_write(%s,%d)\n", current->comm, current->pid);
1063 if (count == 0) /* according to manpage */
1064 return 0;
1066 if (dev->proto == 0 && count < 4) {
1067 /* T0 must have at least 4 bytes */
1068 DEBUGP(4, dev, "T0 short write\n");
1069 return -EIO;
1072 nr = count & 0x1ff; /* max bytes to write */
1074 sendT0 = dev->proto ? 0 : nr > 5 ? 0x08 : 0;
1076 if (!pcmcia_dev_present(dev->p_dev) || /* device removed */
1077 test_bit(IS_CMM_ABSENT, &dev->flags))
1078 return -ENODEV;
1080 if (test_bit(IS_BAD_CSUM, &dev->flags)) {
1081 DEBUGP(4, dev, "bad csum\n");
1082 return -EIO;
1086 * wait for atr to become valid.
1087 * note: it is important to lock this code. if we dont, the monitor
1088 * could be run between test_bit and the call to sleep on the
1089 * atr-queue. if *then* the monitor detects atr valid, it will wake up
1090 * any process on the atr-queue, *but* since we have been interrupted,
1091 * we do not yet sleep on this queue. this would result in a missed
1092 * wake_up and the calling process would sleep forever (until
1093 * interrupted). also, do *not* restore_flags before sleep_on, because
1094 * this could result in the same situation!
1096 if (wait_event_interruptible
1097 (dev->atrq,
1098 ((filp->f_flags & O_NONBLOCK)
1099 || (test_bit(IS_ATR_PRESENT, (void *)&dev->flags) != 0)))) {
1100 if (filp->f_flags & O_NONBLOCK)
1101 return -EAGAIN;
1102 return -ERESTARTSYS;
1105 if (test_bit(IS_ATR_VALID, &dev->flags) == 0) { /* invalid atr */
1106 DEBUGP(4, dev, "invalid ATR\n");
1107 return -EIO;
1110 /* lock io */
1111 if (wait_event_interruptible
1112 (dev->ioq,
1113 ((filp->f_flags & O_NONBLOCK)
1114 || (test_and_set_bit(LOCK_IO, (void *)&dev->flags) == 0)))) {
1115 if (filp->f_flags & O_NONBLOCK)
1116 return -EAGAIN;
1117 return -ERESTARTSYS;
1120 if (copy_from_user(dev->sbuf, buf, ((count > 512) ? 512 : count)))
1121 return -EFAULT;
1123 rc = 0;
1124 dev->flags0 = inb(REG_FLAGS0(iobase));
1125 if ((dev->flags0 & 1) == 0 /* no smartcard inserted */
1126 || dev->flags0 == 0xff) { /* no cardman inserted */
1127 clear_bit(IS_ATR_VALID, &dev->flags);
1128 if (dev->flags0 & 1) {
1129 set_bit(IS_CMM_ABSENT, &dev->flags);
1130 rc = -ENODEV;
1131 } else {
1132 DEBUGP(4, dev, "IO error\n");
1133 rc = -EIO;
1135 goto release_io;
1138 xoutb(0x80, REG_FLAGS0(iobase)); /* reset SM */
1140 if (!io_detect_cm4000(iobase, dev)) {
1141 rc = -ENODEV;
1142 goto release_io;
1145 /* reflect T=0 send/read mode in flags1 */
1146 dev->flags1 |= (sendT0);
1148 set_cardparameter(dev);
1150 /* dummy read, reset flag procedure received */
1151 tmp = inb(REG_FLAGS1(iobase));
1153 dev->flags1 = 0x20 /* T_Active */
1154 | (sendT0)
1155 | (test_bit(IS_INVREV, &dev->flags) ? 2 : 0)/* inverse parity */
1156 | (((dev->baudv - 1) & 0x0100) >> 8); /* MSB-Baud */
1157 DEBUGP(1, dev, "set dev->flags1 = 0x%.2x\n", dev->flags1);
1158 xoutb(dev->flags1, REG_FLAGS1(iobase));
1160 /* xmit data */
1161 DEBUGP(4, dev, "Xmit data\n");
1162 for (i = 0; i < nr; i++) {
1163 if (i >= 256) {
1164 dev->flags1 = 0x20 /* T_Active */
1165 | (sendT0) /* SendT0 */
1166 /* inverse parity: */
1167 | (test_bit(IS_INVREV, &dev->flags) ? 2 : 0)
1168 | (((dev->baudv - 1) & 0x0100) >> 8) /* MSB-Baud */
1169 | 0x10; /* set address high */
1170 DEBUGP(4, dev, "dev->flags = 0x%.2x - set address "
1171 "high\n", dev->flags1);
1172 xoutb(dev->flags1, REG_FLAGS1(iobase));
1174 if (test_bit(IS_INVREV, &dev->flags)) {
1175 DEBUGP(4, dev, "Apply inverse convention for 0x%.2x "
1176 "-> 0x%.2x\n", (unsigned char)dev->sbuf[i],
1177 invert_revert(dev->sbuf[i]));
1178 xoutb(i, REG_BUF_ADDR(iobase));
1179 xoutb(invert_revert(dev->sbuf[i]),
1180 REG_BUF_DATA(iobase));
1181 } else {
1182 xoutb(i, REG_BUF_ADDR(iobase));
1183 xoutb(dev->sbuf[i], REG_BUF_DATA(iobase));
1186 DEBUGP(4, dev, "Xmit done\n");
1188 if (dev->proto == 0) {
1189 /* T=0 proto: 0 byte reply */
1190 if (nr == 4) {
1191 DEBUGP(4, dev, "T=0 assumes 0 byte reply\n");
1192 xoutb(i, REG_BUF_ADDR(iobase));
1193 if (test_bit(IS_INVREV, &dev->flags))
1194 xoutb(0xff, REG_BUF_DATA(iobase));
1195 else
1196 xoutb(0x00, REG_BUF_DATA(iobase));
1199 /* numSendBytes */
1200 if (sendT0)
1201 nsend = nr;
1202 else {
1203 if (nr == 4)
1204 nsend = 5;
1205 else {
1206 nsend = 5 + (unsigned char)dev->sbuf[4];
1207 if (dev->sbuf[4] == 0)
1208 nsend += 0x100;
1211 } else
1212 nsend = nr;
1214 /* T0: output procedure byte */
1215 if (test_bit(IS_INVREV, &dev->flags)) {
1216 DEBUGP(4, dev, "T=0 set Procedure byte (inverse-reverse) "
1217 "0x%.2x\n", invert_revert(dev->sbuf[1]));
1218 xoutb(invert_revert(dev->sbuf[1]), REG_NUM_BYTES(iobase));
1219 } else {
1220 DEBUGP(4, dev, "T=0 set Procedure byte 0x%.2x\n", dev->sbuf[1]);
1221 xoutb(dev->sbuf[1], REG_NUM_BYTES(iobase));
1224 DEBUGP(1, dev, "set NumSendBytes = 0x%.2x\n",
1225 (unsigned char)(nsend & 0xff));
1226 xoutb((unsigned char)(nsend & 0xff), REG_NUM_SEND(iobase));
1228 DEBUGP(1, dev, "Trigger CARDMAN CONTROLLER (0x%.2x)\n",
1229 0x40 /* SM_Active */
1230 | (dev->flags0 & 2 ? 0 : 4) /* power on if needed */
1231 |(dev->proto ? 0x10 : 0x08) /* T=1/T=0 */
1232 |(nsend & 0x100) >> 8 /* MSB numSendBytes */ );
1233 xoutb(0x40 /* SM_Active */
1234 | (dev->flags0 & 2 ? 0 : 4) /* power on if needed */
1235 |(dev->proto ? 0x10 : 0x08) /* T=1/T=0 */
1236 |(nsend & 0x100) >> 8, /* MSB numSendBytes */
1237 REG_FLAGS0(iobase));
1239 /* wait for xmit done */
1240 if (dev->proto == 1) {
1241 DEBUGP(4, dev, "Wait for xmit done\n");
1242 for (i = 0; i < 1000; i++) {
1243 if (inb(REG_FLAGS0(iobase)) & 0x08)
1244 break;
1245 msleep_interruptible(10);
1247 if (i == 1000) {
1248 DEBUGP(4, dev, "timeout waiting for xmit done\n");
1249 rc = -EIO;
1250 goto release_io;
1254 /* T=1: wait for infoLen */
1256 infolen = 0;
1257 if (dev->proto) {
1258 /* wait until infoLen is valid */
1259 for (i = 0; i < 6000; i++) { /* max waiting time of 1 min */
1260 io_read_num_rec_bytes(iobase, &s);
1261 if (s >= 3) {
1262 infolen = inb(REG_FLAGS1(iobase));
1263 DEBUGP(4, dev, "infolen=%d\n", infolen);
1264 break;
1266 msleep_interruptible(10);
1268 if (i == 6000) {
1269 DEBUGP(4, dev, "timeout waiting for infoLen\n");
1270 rc = -EIO;
1271 goto release_io;
1273 } else
1274 clear_bit(IS_PROCBYTE_PRESENT, &dev->flags);
1276 /* numRecBytes | bit9 of numRecytes */
1277 io_read_num_rec_bytes(iobase, &dev->rlen);
1278 for (i = 0; i < 600; i++) { /* max waiting time of 2 sec */
1279 if (dev->proto) {
1280 if (dev->rlen >= infolen + 4)
1281 break;
1283 msleep_interruptible(10);
1284 /* numRecBytes | bit9 of numRecytes */
1285 io_read_num_rec_bytes(iobase, &s);
1286 if (s > dev->rlen) {
1287 DEBUGP(1, dev, "NumRecBytes inc (reset timeout)\n");
1288 i = 0; /* reset timeout */
1289 dev->rlen = s;
1291 /* T=0: we are done when numRecBytes doesn't
1292 * increment any more and NoProcedureByte
1293 * is set and numRecBytes == bytes sent + 6
1294 * (header bytes + data + 1 for sw2)
1295 * except when the card replies an error
1296 * which means, no data will be sent back.
1298 else if (dev->proto == 0) {
1299 if ((inb(REG_BUF_ADDR(iobase)) & 0x80)) {
1300 /* no procedure byte received since last read */
1301 DEBUGP(1, dev, "NoProcedure byte set\n");
1302 /* i=0; */
1303 } else {
1304 /* procedure byte received since last read */
1305 DEBUGP(1, dev, "NoProcedure byte unset "
1306 "(reset timeout)\n");
1307 dev->procbyte = inb(REG_FLAGS1(iobase));
1308 DEBUGP(1, dev, "Read procedure byte 0x%.2x\n",
1309 dev->procbyte);
1310 i = 0; /* resettimeout */
1312 if (inb(REG_FLAGS0(iobase)) & 0x08) {
1313 DEBUGP(1, dev, "T0Done flag (read reply)\n");
1314 break;
1317 if (dev->proto)
1318 infolen = inb(REG_FLAGS1(iobase));
1320 if (i == 600) {
1321 DEBUGP(1, dev, "timeout waiting for numRecBytes\n");
1322 rc = -EIO;
1323 goto release_io;
1324 } else {
1325 if (dev->proto == 0) {
1326 DEBUGP(1, dev, "Wait for T0Done bit to be set\n");
1327 for (i = 0; i < 1000; i++) {
1328 if (inb(REG_FLAGS0(iobase)) & 0x08)
1329 break;
1330 msleep_interruptible(10);
1332 if (i == 1000) {
1333 DEBUGP(1, dev, "timeout waiting for T0Done\n");
1334 rc = -EIO;
1335 goto release_io;
1338 dev->procbyte = inb(REG_FLAGS1(iobase));
1339 DEBUGP(4, dev, "Read procedure byte 0x%.2x\n",
1340 dev->procbyte);
1342 io_read_num_rec_bytes(iobase, &dev->rlen);
1343 DEBUGP(4, dev, "Read NumRecBytes = %i\n", dev->rlen);
1347 /* T=1: read offset=zero, T=0: read offset=after challenge */
1348 dev->rpos = dev->proto ? 0 : nr == 4 ? 5 : nr > dev->rlen ? 5 : nr;
1349 DEBUGP(4, dev, "dev->rlen = %i, dev->rpos = %i, nr = %i\n",
1350 dev->rlen, dev->rpos, nr);
1352 release_io:
1353 DEBUGP(4, dev, "Reset SM\n");
1354 xoutb(0x80, REG_FLAGS0(iobase)); /* reset SM */
1356 if (rc < 0) {
1357 DEBUGP(4, dev, "Write failed but clear T_Active\n");
1358 dev->flags1 &= 0xdf;
1359 xoutb(dev->flags1, REG_FLAGS1(iobase));
1362 clear_bit(LOCK_IO, &dev->flags);
1363 wake_up_interruptible(&dev->ioq);
1364 wake_up_interruptible(&dev->readq); /* tell read we have data */
1366 /* ITSEC E2: clear write buffer */
1367 memset((char *)dev->sbuf, 0, 512);
1369 /* return error or actually written bytes */
1370 DEBUGP(2, dev, "<- cmm_write\n");
1371 return rc < 0 ? rc : nr;
1374 static void start_monitor(struct cm4000_dev *dev)
1376 DEBUGP(3, dev, "-> start_monitor\n");
1377 if (!dev->monitor_running) {
1378 DEBUGP(5, dev, "create, init and add timer\n");
1379 setup_timer(&dev->timer, monitor_card, (unsigned long)dev);
1380 dev->monitor_running = 1;
1381 mod_timer(&dev->timer, jiffies);
1382 } else
1383 DEBUGP(5, dev, "monitor already running\n");
1384 DEBUGP(3, dev, "<- start_monitor\n");
1387 static void stop_monitor(struct cm4000_dev *dev)
1389 DEBUGP(3, dev, "-> stop_monitor\n");
1390 if (dev->monitor_running) {
1391 DEBUGP(5, dev, "stopping monitor\n");
1392 terminate_monitor(dev);
1393 /* reset monitor SM */
1394 clear_bit(IS_ATR_VALID, &dev->flags);
1395 clear_bit(IS_ATR_PRESENT, &dev->flags);
1396 } else
1397 DEBUGP(5, dev, "monitor already stopped\n");
1398 DEBUGP(3, dev, "<- stop_monitor\n");
1401 static long cmm_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1403 struct cm4000_dev *dev = filp->private_data;
1404 unsigned int iobase = dev->p_dev->resource[0]->start;
1405 struct inode *inode = filp->f_path.dentry->d_inode;
1406 struct pcmcia_device *link;
1407 int size;
1408 int rc;
1409 void __user *argp = (void __user *)arg;
1410 #ifdef CM4000_DEBUG
1411 char *ioctl_names[CM_IOC_MAXNR + 1] = {
1412 [_IOC_NR(CM_IOCGSTATUS)] "CM_IOCGSTATUS",
1413 [_IOC_NR(CM_IOCGATR)] "CM_IOCGATR",
1414 [_IOC_NR(CM_IOCARDOFF)] "CM_IOCARDOFF",
1415 [_IOC_NR(CM_IOCSPTS)] "CM_IOCSPTS",
1416 [_IOC_NR(CM_IOSDBGLVL)] "CM4000_DBGLVL",
1418 DEBUGP(3, dev, "cmm_ioctl(device=%d.%d) %s\n", imajor(inode),
1419 iminor(inode), ioctl_names[_IOC_NR(cmd)]);
1420 #endif
1422 mutex_lock(&cmm_mutex);
1423 rc = -ENODEV;
1424 link = dev_table[iminor(inode)];
1425 if (!pcmcia_dev_present(link)) {
1426 DEBUGP(4, dev, "DEV_OK false\n");
1427 goto out;
1430 if (test_bit(IS_CMM_ABSENT, &dev->flags)) {
1431 DEBUGP(4, dev, "CMM_ABSENT flag set\n");
1432 goto out;
1434 rc = -EINVAL;
1436 if (_IOC_TYPE(cmd) != CM_IOC_MAGIC) {
1437 DEBUGP(4, dev, "ioctype mismatch\n");
1438 goto out;
1440 if (_IOC_NR(cmd) > CM_IOC_MAXNR) {
1441 DEBUGP(4, dev, "iocnr mismatch\n");
1442 goto out;
1444 size = _IOC_SIZE(cmd);
1445 rc = -EFAULT;
1446 DEBUGP(4, dev, "iocdir=%.4x iocr=%.4x iocw=%.4x iocsize=%d cmd=%.4x\n",
1447 _IOC_DIR(cmd), _IOC_READ, _IOC_WRITE, size, cmd);
1449 if (_IOC_DIR(cmd) & _IOC_READ) {
1450 if (!access_ok(VERIFY_WRITE, argp, size))
1451 goto out;
1453 if (_IOC_DIR(cmd) & _IOC_WRITE) {
1454 if (!access_ok(VERIFY_READ, argp, size))
1455 goto out;
1457 rc = 0;
1459 switch (cmd) {
1460 case CM_IOCGSTATUS:
1461 DEBUGP(4, dev, " ... in CM_IOCGSTATUS\n");
1463 int status;
1465 /* clear other bits, but leave inserted & powered as
1466 * they are */
1467 status = dev->flags0 & 3;
1468 if (test_bit(IS_ATR_PRESENT, &dev->flags))
1469 status |= CM_ATR_PRESENT;
1470 if (test_bit(IS_ATR_VALID, &dev->flags))
1471 status |= CM_ATR_VALID;
1472 if (test_bit(IS_CMM_ABSENT, &dev->flags))
1473 status |= CM_NO_READER;
1474 if (test_bit(IS_BAD_CARD, &dev->flags))
1475 status |= CM_BAD_CARD;
1476 if (copy_to_user(argp, &status, sizeof(int)))
1477 rc = -EFAULT;
1479 break;
1480 case CM_IOCGATR:
1481 DEBUGP(4, dev, "... in CM_IOCGATR\n");
1483 struct atreq __user *atreq = argp;
1484 int tmp;
1485 /* allow nonblocking io and being interrupted */
1486 if (wait_event_interruptible
1487 (dev->atrq,
1488 ((filp->f_flags & O_NONBLOCK)
1489 || (test_bit(IS_ATR_PRESENT, (void *)&dev->flags)
1490 != 0)))) {
1491 if (filp->f_flags & O_NONBLOCK)
1492 rc = -EAGAIN;
1493 else
1494 rc = -ERESTARTSYS;
1495 break;
1498 rc = -EFAULT;
1499 if (test_bit(IS_ATR_VALID, &dev->flags) == 0) {
1500 tmp = -1;
1501 if (copy_to_user(&(atreq->atr_len), &tmp,
1502 sizeof(int)))
1503 break;
1504 } else {
1505 if (copy_to_user(atreq->atr, dev->atr,
1506 dev->atr_len))
1507 break;
1509 tmp = dev->atr_len;
1510 if (copy_to_user(&(atreq->atr_len), &tmp, sizeof(int)))
1511 break;
1513 rc = 0;
1514 break;
1516 case CM_IOCARDOFF:
1518 #ifdef CM4000_DEBUG
1519 DEBUGP(4, dev, "... in CM_IOCARDOFF\n");
1520 if (dev->flags0 & 0x01) {
1521 DEBUGP(4, dev, " Card inserted\n");
1522 } else {
1523 DEBUGP(2, dev, " No card inserted\n");
1525 if (dev->flags0 & 0x02) {
1526 DEBUGP(4, dev, " Card powered\n");
1527 } else {
1528 DEBUGP(2, dev, " Card not powered\n");
1530 #endif
1532 /* is a card inserted and powered? */
1533 if ((dev->flags0 & 0x01) && (dev->flags0 & 0x02)) {
1535 /* get IO lock */
1536 if (wait_event_interruptible
1537 (dev->ioq,
1538 ((filp->f_flags & O_NONBLOCK)
1539 || (test_and_set_bit(LOCK_IO, (void *)&dev->flags)
1540 == 0)))) {
1541 if (filp->f_flags & O_NONBLOCK)
1542 rc = -EAGAIN;
1543 else
1544 rc = -ERESTARTSYS;
1545 break;
1547 /* Set Flags0 = 0x42 */
1548 DEBUGP(4, dev, "Set Flags0=0x42 \n");
1549 xoutb(0x42, REG_FLAGS0(iobase));
1550 clear_bit(IS_ATR_PRESENT, &dev->flags);
1551 clear_bit(IS_ATR_VALID, &dev->flags);
1552 dev->mstate = M_CARDOFF;
1553 clear_bit(LOCK_IO, &dev->flags);
1554 if (wait_event_interruptible
1555 (dev->atrq,
1556 ((filp->f_flags & O_NONBLOCK)
1557 || (test_bit(IS_ATR_VALID, (void *)&dev->flags) !=
1558 0)))) {
1559 if (filp->f_flags & O_NONBLOCK)
1560 rc = -EAGAIN;
1561 else
1562 rc = -ERESTARTSYS;
1563 break;
1566 /* release lock */
1567 clear_bit(LOCK_IO, &dev->flags);
1568 wake_up_interruptible(&dev->ioq);
1570 rc = 0;
1571 break;
1572 case CM_IOCSPTS:
1574 struct ptsreq krnptsreq;
1576 if (copy_from_user(&krnptsreq, argp,
1577 sizeof(struct ptsreq))) {
1578 rc = -EFAULT;
1579 break;
1582 rc = 0;
1583 DEBUGP(4, dev, "... in CM_IOCSPTS\n");
1584 /* wait for ATR to get valid */
1585 if (wait_event_interruptible
1586 (dev->atrq,
1587 ((filp->f_flags & O_NONBLOCK)
1588 || (test_bit(IS_ATR_PRESENT, (void *)&dev->flags)
1589 != 0)))) {
1590 if (filp->f_flags & O_NONBLOCK)
1591 rc = -EAGAIN;
1592 else
1593 rc = -ERESTARTSYS;
1594 break;
1596 /* get IO lock */
1597 if (wait_event_interruptible
1598 (dev->ioq,
1599 ((filp->f_flags & O_NONBLOCK)
1600 || (test_and_set_bit(LOCK_IO, (void *)&dev->flags)
1601 == 0)))) {
1602 if (filp->f_flags & O_NONBLOCK)
1603 rc = -EAGAIN;
1604 else
1605 rc = -ERESTARTSYS;
1606 break;
1609 if ((rc = set_protocol(dev, &krnptsreq)) != 0) {
1610 /* auto power_on again */
1611 dev->mstate = M_FETCH_ATR;
1612 clear_bit(IS_ATR_VALID, &dev->flags);
1614 /* release lock */
1615 clear_bit(LOCK_IO, &dev->flags);
1616 wake_up_interruptible(&dev->ioq);
1619 break;
1620 #ifdef CM4000_DEBUG
1621 case CM_IOSDBGLVL:
1622 rc = -ENOTTY;
1623 break;
1624 #endif
1625 default:
1626 DEBUGP(4, dev, "... in default (unknown IOCTL code)\n");
1627 rc = -ENOTTY;
1629 out:
1630 mutex_unlock(&cmm_mutex);
1631 return rc;
1634 static int cmm_open(struct inode *inode, struct file *filp)
1636 struct cm4000_dev *dev;
1637 struct pcmcia_device *link;
1638 int minor = iminor(inode);
1639 int ret;
1641 if (minor >= CM4000_MAX_DEV)
1642 return -ENODEV;
1644 mutex_lock(&cmm_mutex);
1645 link = dev_table[minor];
1646 if (link == NULL || !pcmcia_dev_present(link)) {
1647 ret = -ENODEV;
1648 goto out;
1651 if (link->open) {
1652 ret = -EBUSY;
1653 goto out;
1656 dev = link->priv;
1657 filp->private_data = dev;
1659 DEBUGP(2, dev, "-> cmm_open(device=%d.%d process=%s,%d)\n",
1660 imajor(inode), minor, current->comm, current->pid);
1662 /* init device variables, they may be "polluted" after close
1663 * or, the device may never have been closed (i.e. open failed)
1666 ZERO_DEV(dev);
1668 /* opening will always block since the
1669 * monitor will be started by open, which
1670 * means we have to wait for ATR becoming
1671 * vaild = block until valid (or card
1672 * inserted)
1674 if (filp->f_flags & O_NONBLOCK) {
1675 ret = -EAGAIN;
1676 goto out;
1679 dev->mdelay = T_50MSEC;
1681 /* start monitoring the cardstatus */
1682 start_monitor(dev);
1684 link->open = 1; /* only one open per device */
1686 DEBUGP(2, dev, "<- cmm_open\n");
1687 ret = nonseekable_open(inode, filp);
1688 out:
1689 mutex_unlock(&cmm_mutex);
1690 return ret;
1693 static int cmm_close(struct inode *inode, struct file *filp)
1695 struct cm4000_dev *dev;
1696 struct pcmcia_device *link;
1697 int minor = iminor(inode);
1699 if (minor >= CM4000_MAX_DEV)
1700 return -ENODEV;
1702 link = dev_table[minor];
1703 if (link == NULL)
1704 return -ENODEV;
1706 dev = link->priv;
1708 DEBUGP(2, dev, "-> cmm_close(maj/min=%d.%d)\n",
1709 imajor(inode), minor);
1711 stop_monitor(dev);
1713 ZERO_DEV(dev);
1715 link->open = 0; /* only one open per device */
1716 wake_up(&dev->devq); /* socket removed? */
1718 DEBUGP(2, dev, "cmm_close\n");
1719 return 0;
1722 static void cmm_cm4000_release(struct pcmcia_device * link)
1724 struct cm4000_dev *dev = link->priv;
1726 /* dont terminate the monitor, rather rely on
1727 * close doing that for us.
1729 DEBUGP(3, dev, "-> cmm_cm4000_release\n");
1730 while (link->open) {
1731 printk(KERN_INFO MODULE_NAME ": delaying release until "
1732 "process has terminated\n");
1733 /* note: don't interrupt us:
1734 * close the applications which own
1735 * the devices _first_ !
1737 wait_event(dev->devq, (link->open == 0));
1739 /* dev->devq=NULL; this cannot be zeroed earlier */
1740 DEBUGP(3, dev, "<- cmm_cm4000_release\n");
1741 return;
1744 /*==== Interface to PCMCIA Layer =======================================*/
1746 static int cm4000_config_check(struct pcmcia_device *p_dev,
1747 cistpl_cftable_entry_t *cfg,
1748 cistpl_cftable_entry_t *dflt,
1749 unsigned int vcc,
1750 void *priv_data)
1752 if (!cfg->io.nwin)
1753 return -ENODEV;
1755 p_dev->resource[0]->start = cfg->io.win[0].base;
1756 p_dev->resource[0]->end = cfg->io.win[0].len;
1757 p_dev->resource[0]->flags |= pcmcia_io_cfg_data_width(cfg->io.flags);
1758 p_dev->io_lines = cfg->io.flags & CISTPL_IO_LINES_MASK;
1760 return pcmcia_request_io(p_dev);
1763 static int cm4000_config(struct pcmcia_device * link, int devno)
1765 struct cm4000_dev *dev;
1767 /* read the config-tuples */
1768 if (pcmcia_loop_config(link, cm4000_config_check, NULL))
1769 goto cs_release;
1771 link->conf.IntType = 00000002;
1773 if (pcmcia_request_configuration(link, &link->conf))
1774 goto cs_release;
1776 dev = link->priv;
1778 return 0;
1780 cs_release:
1781 cm4000_release(link);
1782 return -ENODEV;
1785 static int cm4000_suspend(struct pcmcia_device *link)
1787 struct cm4000_dev *dev;
1789 dev = link->priv;
1790 stop_monitor(dev);
1792 return 0;
1795 static int cm4000_resume(struct pcmcia_device *link)
1797 struct cm4000_dev *dev;
1799 dev = link->priv;
1800 if (link->open)
1801 start_monitor(dev);
1803 return 0;
1806 static void cm4000_release(struct pcmcia_device *link)
1808 cmm_cm4000_release(link); /* delay release until device closed */
1809 pcmcia_disable_device(link);
1812 static int cm4000_probe(struct pcmcia_device *link)
1814 struct cm4000_dev *dev;
1815 int i, ret;
1817 for (i = 0; i < CM4000_MAX_DEV; i++)
1818 if (dev_table[i] == NULL)
1819 break;
1821 if (i == CM4000_MAX_DEV) {
1822 printk(KERN_NOTICE MODULE_NAME ": all devices in use\n");
1823 return -ENODEV;
1826 /* create a new cm4000_cs device */
1827 dev = kzalloc(sizeof(struct cm4000_dev), GFP_KERNEL);
1828 if (dev == NULL)
1829 return -ENOMEM;
1831 dev->p_dev = link;
1832 link->priv = dev;
1833 link->conf.IntType = INT_MEMORY_AND_IO;
1834 dev_table[i] = link;
1836 init_waitqueue_head(&dev->devq);
1837 init_waitqueue_head(&dev->ioq);
1838 init_waitqueue_head(&dev->atrq);
1839 init_waitqueue_head(&dev->readq);
1841 ret = cm4000_config(link, i);
1842 if (ret) {
1843 dev_table[i] = NULL;
1844 kfree(dev);
1845 return ret;
1848 device_create(cmm_class, NULL, MKDEV(major, i), NULL, "cmm%d", i);
1850 return 0;
1853 static void cm4000_detach(struct pcmcia_device *link)
1855 struct cm4000_dev *dev = link->priv;
1856 int devno;
1858 /* find device */
1859 for (devno = 0; devno < CM4000_MAX_DEV; devno++)
1860 if (dev_table[devno] == link)
1861 break;
1862 if (devno == CM4000_MAX_DEV)
1863 return;
1865 stop_monitor(dev);
1867 cm4000_release(link);
1869 dev_table[devno] = NULL;
1870 kfree(dev);
1872 device_destroy(cmm_class, MKDEV(major, devno));
1874 return;
1877 static const struct file_operations cm4000_fops = {
1878 .owner = THIS_MODULE,
1879 .read = cmm_read,
1880 .write = cmm_write,
1881 .unlocked_ioctl = cmm_ioctl,
1882 .open = cmm_open,
1883 .release= cmm_close,
1886 static struct pcmcia_device_id cm4000_ids[] = {
1887 PCMCIA_DEVICE_MANF_CARD(0x0223, 0x0002),
1888 PCMCIA_DEVICE_PROD_ID12("CardMan", "4000", 0x2FB368CA, 0xA2BD8C39),
1889 PCMCIA_DEVICE_NULL,
1891 MODULE_DEVICE_TABLE(pcmcia, cm4000_ids);
1893 static struct pcmcia_driver cm4000_driver = {
1894 .owner = THIS_MODULE,
1895 .drv = {
1896 .name = "cm4000_cs",
1898 .probe = cm4000_probe,
1899 .remove = cm4000_detach,
1900 .suspend = cm4000_suspend,
1901 .resume = cm4000_resume,
1902 .id_table = cm4000_ids,
1905 static int __init cmm_init(void)
1907 int rc;
1909 printk(KERN_INFO "%s\n", version);
1911 cmm_class = class_create(THIS_MODULE, "cardman_4000");
1912 if (IS_ERR(cmm_class))
1913 return PTR_ERR(cmm_class);
1915 major = register_chrdev(0, DEVICE_NAME, &cm4000_fops);
1916 if (major < 0) {
1917 printk(KERN_WARNING MODULE_NAME
1918 ": could not get major number\n");
1919 class_destroy(cmm_class);
1920 return major;
1923 rc = pcmcia_register_driver(&cm4000_driver);
1924 if (rc < 0) {
1925 unregister_chrdev(major, DEVICE_NAME);
1926 class_destroy(cmm_class);
1927 return rc;
1930 return 0;
1933 static void __exit cmm_exit(void)
1935 printk(KERN_INFO MODULE_NAME ": unloading\n");
1936 pcmcia_unregister_driver(&cm4000_driver);
1937 unregister_chrdev(major, DEVICE_NAME);
1938 class_destroy(cmm_class);
1941 module_init(cmm_init);
1942 module_exit(cmm_exit);
1943 MODULE_LICENSE("Dual BSD/GPL");