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 process/coding-style.rst
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>
31 #include <linux/delay.h>
32 #include <linux/bitrev.h>
33 #include <linux/mutex.h>
34 #include <linux/uaccess.h>
37 #include <pcmcia/cistpl.h>
38 #include <pcmcia/cisreg.h>
39 #include <pcmcia/ciscode.h>
40 #include <pcmcia/ds.h>
42 #include <linux/cm4000_cs.h>
44 /* #define ATR_CSUM */
46 #define reader_to_dev(x) (&x->p_dev->dev)
48 /* n (debug level) is ignored */
49 /* additional debug output may be enabled by re-compiling with
51 /* #define CM4000_DEBUG */
52 #define DEBUGP(n, rdr, x, args...) do { \
53 dev_dbg(reader_to_dev(rdr), "%s:" x, \
54 __func__ , ## args); \
57 static DEFINE_MUTEX(cmm_mutex);
60 #define T_10MSEC msecs_to_jiffies(10)
61 #define T_20MSEC msecs_to_jiffies(20)
62 #define T_40MSEC msecs_to_jiffies(40)
63 #define T_50MSEC msecs_to_jiffies(50)
64 #define T_100MSEC msecs_to_jiffies(100)
65 #define T_500MSEC msecs_to_jiffies(500)
67 static void cm4000_release(struct pcmcia_device *link);
69 static int major; /* major number we get from the kernel */
71 /* note: the first state has to have number 0 always */
74 #define M_TIMEOUT_WAIT 1
75 #define M_READ_ATR_LEN 2
77 #define M_ATR_PRESENT 4
82 #define LOCK_MONITOR 1
84 #define IS_AUTOPPS_ACT 6
85 #define IS_PROCBYTE_PRESENT 7
89 #define IS_ATR_PRESENT 11
90 #define IS_ATR_VALID 12
91 #define IS_CMM_ABSENT 13
92 #define IS_BAD_LENGTH 14
93 #define IS_BAD_CSUM 15
94 #define IS_BAD_CARD 16
96 #define REG_FLAGS0(x) (x + 0)
97 #define REG_FLAGS1(x) (x + 1)
98 #define REG_NUM_BYTES(x) (x + 2)
99 #define REG_BUF_ADDR(x) (x + 3)
100 #define REG_BUF_DATA(x) (x + 4)
101 #define REG_NUM_SEND(x) (x + 5)
102 #define REG_BAUDRATE(x) (x + 6)
103 #define REG_STOPBITS(x) (x + 7)
106 struct pcmcia_device *p_dev;
108 unsigned char atr[MAX_ATR];
109 unsigned char rbuf[512];
110 unsigned char sbuf[512];
112 wait_queue_head_t devq; /* when removing cardman must not be
115 wait_queue_head_t ioq; /* if IO is locked, wait on this Q */
116 wait_queue_head_t atrq; /* wait for ATR valid */
117 wait_queue_head_t readq; /* used by write to wake blk.read */
119 /* warning: do not move this struct group.
120 * initialising to zero depends on it - see ZERO_DEV below. */
122 unsigned char atr_csum;
123 unsigned char atr_len_retry;
124 unsigned short atr_len;
125 unsigned short rlen; /* bytes avail. after write */
126 unsigned short rpos; /* latest read pos. write zeroes */
127 unsigned char procbyte; /* T=0 procedure byte */
128 unsigned char mstate; /* state of card monitor */
129 unsigned char cwarn; /* slow down warning */
130 unsigned char flags0; /* cardman IO-flags 0 */
131 unsigned char flags1; /* cardman IO-flags 1 */
132 unsigned int mdelay; /* variable monitor speeds, in jiffies */
134 unsigned int baudv; /* baud value for speed */
136 unsigned char proto; /* T=0, T=1, ... */
137 unsigned long flags; /* lock+flags (MONITOR,IO,ATR) * for concurrent
140 unsigned char pts[4];
142 struct timer_list timer; /* used to keep monitor running */
147 #define ZERO_DEV(dev) memset(&((dev)->init), 0, sizeof((dev)->init))
149 static struct pcmcia_device *dev_table[CM4000_MAX_DEV];
150 static struct class *cmm_class;
152 /* This table doesn't use spaces after the comma between fields and thus
153 * violates process/coding-style.rst. However, I don't really think wrapping it around will
154 * make it any clearer to read -HW */
155 static unsigned char fi_di_table[10][14] = {
156 /*FI 00 01 02 03 04 05 06 07 08 09 10 11 12 13 */
158 /* 0 */ {0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11},
159 /* 1 */ {0x01,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x91,0x11,0x11,0x11,0x11},
160 /* 2 */ {0x02,0x12,0x22,0x32,0x11,0x11,0x11,0x11,0x11,0x92,0xA2,0xB2,0x11,0x11},
161 /* 3 */ {0x03,0x13,0x23,0x33,0x43,0x53,0x63,0x11,0x11,0x93,0xA3,0xB3,0xC3,0xD3},
162 /* 4 */ {0x04,0x14,0x24,0x34,0x44,0x54,0x64,0x11,0x11,0x94,0xA4,0xB4,0xC4,0xD4},
163 /* 5 */ {0x00,0x15,0x25,0x35,0x45,0x55,0x65,0x11,0x11,0x95,0xA5,0xB5,0xC5,0xD5},
164 /* 6 */ {0x06,0x16,0x26,0x36,0x46,0x56,0x66,0x11,0x11,0x96,0xA6,0xB6,0xC6,0xD6},
165 /* 7 */ {0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11},
166 /* 8 */ {0x08,0x11,0x28,0x38,0x48,0x58,0x68,0x11,0x11,0x98,0xA8,0xB8,0xC8,0xD8},
167 /* 9 */ {0x09,0x19,0x29,0x39,0x49,0x59,0x69,0x11,0x11,0x99,0xA9,0xB9,0xC9,0xD9}
174 static inline void xoutb(unsigned char val, unsigned short port)
176 pr_debug("outb(val=%.2x,port=%.4x)\n", val, port);
179 static inline unsigned char xinb(unsigned short port)
184 pr_debug("%.2x=inb(%.4x)\n", val, port);
190 static inline unsigned char invert_revert(unsigned char ch)
195 static void str_invert_revert(unsigned char *b, int len)
199 for (i = 0; i < len; i++)
200 b[i] = invert_revert(b[i]);
203 #define ATRLENCK(dev,pos) \
204 if (pos>=dev->atr_len || pos>=MAX_ATR) \
207 static unsigned int calc_baudv(unsigned char fidi)
209 unsigned int wcrcf, wbrcf, fi_rfu, di_rfu;
215 switch ((fidi >> 4) & 0x0F) {
264 switch (fidi & 0x0F) {
300 return (wcrcf / wbrcf);
303 static unsigned short io_read_num_rec_bytes(unsigned int iobase,
311 tmp = inb(REG_NUM_BYTES(iobase)) |
312 (inb(REG_FLAGS0(iobase)) & 4 ? 0x100 : 0);
318 static int parse_atr(struct cm4000_dev *dev)
320 unsigned char any_t1, any_t0;
321 unsigned char ch, ifno;
324 DEBUGP(3, dev, "-> parse_atr: dev->atr_len = %i\n", dev->atr_len);
326 if (dev->atr_len < 3) {
327 DEBUGP(5, dev, "parse_atr: atr_len < 3\n");
331 if (dev->atr[0] == 0x3f)
332 set_bit(IS_INVREV, &dev->flags);
334 clear_bit(IS_INVREV, &dev->flags);
338 dev->proto = 0; /* XXX PROTO */
339 any_t1 = any_t0 = done = 0;
340 dev->ta1 = 0x11; /* defaults to 9600 baud */
342 if (ifno == 1 && (ch & 0x10)) {
343 /* read first interface byte and TA1 is present */
344 dev->ta1 = dev->atr[2];
345 DEBUGP(5, dev, "Card says FiDi is 0x%.2x\n", dev->ta1);
347 } else if ((ifno == 2) && (ch & 0x10)) { /* TA(2) */
352 DEBUGP(5, dev, "Yi=%.2x\n", ch & 0xf0);
353 ix += ((ch & 0x10) >> 4) /* no of int.face chars */
356 + ((ch & 0x80) >> 7);
357 /* ATRLENCK(dev,ix); */
358 if (ch & 0x80) { /* TDi */
362 DEBUGP(5, dev, "card is capable of T=1\n");
365 DEBUGP(5, dev, "card is capable of T=0\n");
371 DEBUGP(5, dev, "ix=%d noHist=%d any_t1=%d\n",
372 ix, dev->atr[1] & 15, any_t1);
373 if (ix + 1 + (dev->atr[1] & 0x0f) + any_t1 != dev->atr_len) {
374 DEBUGP(5, dev, "length error\n");
378 set_bit(IS_ANY_T0, &dev->flags);
380 if (any_t1) { /* compute csum */
383 for (i = 1; i < dev->atr_len; i++)
384 dev->atr_csum ^= dev->atr[i];
386 set_bit(IS_BAD_CSUM, &dev->flags);
387 DEBUGP(5, dev, "bad checksum\n");
392 dev->proto = 1; /* XXX PROTO */
393 set_bit(IS_ANY_T1, &dev->flags);
405 static struct card_fixup card_fixups[] = {
407 .atr = { 0x3b, 0xb3, 0x11, 0x00, 0x00, 0x41, 0x01 },
412 .atr = {0x3b, 0x76, 0x13, 0x00, 0x00, 0x80, 0x62, 0x07,
419 static void set_cardparameter(struct cm4000_dev *dev)
422 unsigned int iobase = dev->p_dev->resource[0]->start;
423 u_int8_t stopbits = 0x02; /* ISO default */
425 DEBUGP(3, dev, "-> set_cardparameter\n");
427 dev->flags1 = dev->flags1 | (((dev->baudv - 1) & 0x0100) >> 8);
428 xoutb(dev->flags1, REG_FLAGS1(iobase));
429 DEBUGP(5, dev, "flags1 = 0x%02x\n", dev->flags1);
432 xoutb((unsigned char)((dev->baudv - 1) & 0xFF), REG_BAUDRATE(iobase));
434 DEBUGP(5, dev, "baudv = %i -> write 0x%02x\n", dev->baudv,
435 ((dev->baudv - 1) & 0xFF));
438 for (i = 0; i < ARRAY_SIZE(card_fixups); i++) {
439 if (!memcmp(dev->atr, card_fixups[i].atr,
440 card_fixups[i].atr_len))
441 stopbits = card_fixups[i].stopbits;
443 xoutb(stopbits, REG_STOPBITS(iobase));
445 DEBUGP(3, dev, "<- set_cardparameter\n");
448 static int set_protocol(struct cm4000_dev *dev, struct ptsreq *ptsreq)
451 unsigned long tmp, i;
452 unsigned short num_bytes_read;
453 unsigned char pts_reply[4];
455 unsigned int iobase = dev->p_dev->resource[0]->start;
459 DEBUGP(3, dev, "-> set_protocol\n");
460 DEBUGP(5, dev, "ptsreq->Protocol = 0x%.8x, ptsreq->Flags=0x%.8x, "
461 "ptsreq->pts1=0x%.2x, ptsreq->pts2=0x%.2x, "
462 "ptsreq->pts3=0x%.2x\n", (unsigned int)ptsreq->protocol,
463 (unsigned int)ptsreq->flags, ptsreq->pts1, ptsreq->pts2,
466 /* Fill PTS structure */
469 tmp = ptsreq->protocol;
470 while ((tmp = (tmp >> 1)) > 0)
472 dev->proto = dev->pts[1]; /* Set new protocol */
473 dev->pts[1] = (0x01 << 4) | (dev->pts[1]);
475 /* Correct Fi/Di according to CM4000 Fi/Di table */
476 DEBUGP(5, dev, "Ta(1) from ATR is 0x%.2x\n", dev->ta1);
477 /* set Fi/Di according to ATR TA(1) */
478 dev->pts[2] = fi_di_table[dev->ta1 & 0x0F][(dev->ta1 >> 4) & 0x0F];
480 /* Calculate PCK character */
481 dev->pts[3] = dev->pts[0] ^ dev->pts[1] ^ dev->pts[2];
483 DEBUGP(5, dev, "pts0=%.2x, pts1=%.2x, pts2=%.2x, pts3=%.2x\n",
484 dev->pts[0], dev->pts[1], dev->pts[2], dev->pts[3]);
486 /* check card convention */
487 if (test_bit(IS_INVREV, &dev->flags))
488 str_invert_revert(dev->pts, 4);
491 xoutb(0x80, REG_FLAGS0(iobase));
493 /* Enable access to the message buffer */
494 DEBUGP(5, dev, "Enable access to the messages buffer\n");
495 dev->flags1 = 0x20 /* T_Active */
496 | (test_bit(IS_INVREV, &dev->flags) ? 0x02 : 0x00) /* inv parity */
497 | ((dev->baudv >> 8) & 0x01); /* MSB-baud */
498 xoutb(dev->flags1, REG_FLAGS1(iobase));
500 DEBUGP(5, dev, "Enable message buffer -> flags1 = 0x%.2x\n",
503 /* write challenge to the buffer */
504 DEBUGP(5, dev, "Write challenge to buffer: ");
505 for (i = 0; i < 4; i++) {
506 xoutb(i, REG_BUF_ADDR(iobase));
507 xoutb(dev->pts[i], REG_BUF_DATA(iobase)); /* buf data */
509 pr_debug("0x%.2x ", dev->pts[i]);
516 /* set number of bytes to write */
517 DEBUGP(5, dev, "Set number of bytes to write\n");
518 xoutb(0x04, REG_NUM_SEND(iobase));
520 /* Trigger CARDMAN CONTROLLER */
521 xoutb(0x50, REG_FLAGS0(iobase));
523 /* Monitor progress */
524 /* wait for xmit done */
525 DEBUGP(5, dev, "Waiting for NumRecBytes getting valid\n");
527 for (i = 0; i < 100; i++) {
528 if (inb(REG_FLAGS0(iobase)) & 0x08) {
529 DEBUGP(5, dev, "NumRecBytes is valid\n");
532 /* can not sleep as this is in atomic context */
536 DEBUGP(5, dev, "Timeout waiting for NumRecBytes getting "
539 goto exit_setprotocol;
542 DEBUGP(5, dev, "Reading NumRecBytes\n");
543 for (i = 0; i < 100; i++) {
544 io_read_num_rec_bytes(iobase, &num_bytes_read);
545 if (num_bytes_read >= 4) {
546 DEBUGP(2, dev, "NumRecBytes = %i\n", num_bytes_read);
547 if (num_bytes_read > 4) {
549 goto exit_setprotocol;
553 /* can not sleep as this is in atomic context */
557 /* check whether it is a short PTS reply? */
558 if (num_bytes_read == 3)
562 DEBUGP(5, dev, "Timeout reading num_bytes_read\n");
564 goto exit_setprotocol;
567 DEBUGP(5, dev, "Reset the CARDMAN CONTROLLER\n");
568 xoutb(0x80, REG_FLAGS0(iobase));
571 DEBUGP(5, dev, "Read PPS reply\n");
572 for (i = 0; i < num_bytes_read; i++) {
573 xoutb(i, REG_BUF_ADDR(iobase));
574 pts_reply[i] = inb(REG_BUF_DATA(iobase));
578 DEBUGP(2, dev, "PTSreply: ");
579 for (i = 0; i < num_bytes_read; i++) {
580 pr_debug("0x%.2x ", pts_reply[i]);
583 #endif /* CM4000_DEBUG */
585 DEBUGP(5, dev, "Clear Tactive in Flags1\n");
586 xoutb(0x20, REG_FLAGS1(iobase));
588 /* Compare ptsreq and ptsreply */
589 if ((dev->pts[0] == pts_reply[0]) &&
590 (dev->pts[1] == pts_reply[1]) &&
591 (dev->pts[2] == pts_reply[2]) && (dev->pts[3] == pts_reply[3])) {
592 /* setcardparameter according to PPS */
593 dev->baudv = calc_baudv(dev->pts[2]);
594 set_cardparameter(dev);
595 } else if ((dev->pts[0] == pts_reply[0]) &&
596 ((dev->pts[1] & 0xef) == pts_reply[1]) &&
597 ((pts_reply[0] ^ pts_reply[1]) == pts_reply[2])) {
598 /* short PTS reply, set card parameter to default values */
599 dev->baudv = calc_baudv(0x11);
600 set_cardparameter(dev);
605 DEBUGP(3, dev, "<- set_protocol\n");
609 static int io_detect_cm4000(unsigned int iobase, struct cm4000_dev *dev)
612 /* note: statemachine is assumed to be reset */
613 if (inb(REG_FLAGS0(iobase)) & 8) {
614 clear_bit(IS_ATR_VALID, &dev->flags);
615 set_bit(IS_CMM_ABSENT, &dev->flags);
616 return 0; /* detect CMM = 1 -> failure */
618 /* xoutb(0x40, REG_FLAGS1(iobase)); detectCMM */
619 xoutb(dev->flags1 | 0x40, REG_FLAGS1(iobase));
620 if ((inb(REG_FLAGS0(iobase)) & 8) == 0) {
621 clear_bit(IS_ATR_VALID, &dev->flags);
622 set_bit(IS_CMM_ABSENT, &dev->flags);
623 return 0; /* detect CMM=0 -> failure */
625 /* clear detectCMM again by restoring original flags1 */
626 xoutb(dev->flags1, REG_FLAGS1(iobase));
630 static void terminate_monitor(struct cm4000_dev *dev)
633 /* tell the monitor to stop and wait until
636 DEBUGP(3, dev, "-> terminate_monitor\n");
637 wait_event_interruptible(dev->devq,
638 test_and_set_bit(LOCK_MONITOR,
639 (void *)&dev->flags));
641 /* now, LOCK_MONITOR has been set.
642 * allow a last cycle in the monitor.
643 * the monitor will indicate that it has
644 * finished by clearing this bit.
646 DEBUGP(5, dev, "Now allow last cycle of monitor!\n");
647 while (test_bit(LOCK_MONITOR, (void *)&dev->flags))
650 DEBUGP(5, dev, "Delete timer\n");
651 del_timer_sync(&dev->timer);
653 dev->monitor_running = 0;
656 DEBUGP(3, dev, "<- terminate_monitor\n");
660 * monitor the card every 50msec. as a side-effect, retrieve the
661 * atr once a card is inserted. another side-effect of retrieving the
662 * atr is that the card will be powered on, so there is no need to
663 * power on the card explicitly from the application: the driver
664 * is already doing that for you.
667 static void monitor_card(struct timer_list *t)
669 struct cm4000_dev *dev = from_timer(dev, t, timer);
670 unsigned int iobase = dev->p_dev->resource[0]->start;
672 struct ptsreq ptsreq;
675 DEBUGP(7, dev, "-> monitor_card\n");
677 /* if someone has set the lock for us: we're done! */
678 if (test_and_set_bit(LOCK_MONITOR, &dev->flags)) {
679 DEBUGP(4, dev, "About to stop monitor\n");
683 dev->atr_csum = dev->atr_len_retry = dev->cwarn = 0;
684 dev->mstate = M_FETCH_ATR;
685 clear_bit(LOCK_MONITOR, &dev->flags);
686 /* close et al. are sleeping on devq, so wake it */
687 wake_up_interruptible(&dev->devq);
688 DEBUGP(2, dev, "<- monitor_card (we are done now)\n");
692 /* try to lock io: if it is already locked, just add another timer */
693 if (test_and_set_bit(LOCK_IO, (void *)&dev->flags)) {
694 DEBUGP(4, dev, "Couldn't get IO lock\n");
695 goto return_with_timer;
698 /* is a card/a reader inserted at all ? */
699 dev->flags0 = xinb(REG_FLAGS0(iobase));
700 DEBUGP(7, dev, "dev->flags0 = 0x%2x\n", dev->flags0);
701 DEBUGP(7, dev, "smartcard present: %s\n",
702 dev->flags0 & 1 ? "yes" : "no");
703 DEBUGP(7, dev, "cardman present: %s\n",
704 dev->flags0 == 0xff ? "no" : "yes");
706 if ((dev->flags0 & 1) == 0 /* no smartcard inserted */
707 || dev->flags0 == 0xff) { /* no cardman inserted */
711 dev->atr_csum = dev->atr_len_retry = dev->cwarn = 0;
712 dev->mstate = M_FETCH_ATR;
714 dev->flags &= 0x000000ff; /* only keep IO and MONITOR locks */
716 if (dev->flags0 == 0xff) {
717 DEBUGP(4, dev, "set IS_CMM_ABSENT bit\n");
718 set_bit(IS_CMM_ABSENT, &dev->flags);
719 } else if (test_bit(IS_CMM_ABSENT, &dev->flags)) {
720 DEBUGP(4, dev, "clear IS_CMM_ABSENT bit "
721 "(card is removed)\n");
722 clear_bit(IS_CMM_ABSENT, &dev->flags);
726 } else if ((dev->flags0 & 1) && test_bit(IS_CMM_ABSENT, &dev->flags)) {
727 /* cardman and card present but cardman was absent before
728 * (after suspend with inserted card) */
729 DEBUGP(4, dev, "clear IS_CMM_ABSENT bit (card is inserted)\n");
730 clear_bit(IS_CMM_ABSENT, &dev->flags);
733 if (test_bit(IS_ATR_VALID, &dev->flags) == 1) {
734 DEBUGP(7, dev, "believe ATR is already valid (do nothing)\n");
738 switch (dev->mstate) {
740 unsigned char flags0;
742 DEBUGP(4, dev, "M_CARDOFF\n");
743 flags0 = inb(REG_FLAGS0(iobase));
745 /* wait until Flags0 indicate power is off */
746 dev->mdelay = T_10MSEC;
748 /* Flags0 indicate power off and no card inserted now;
749 * Reset CARDMAN CONTROLLER */
750 xoutb(0x80, REG_FLAGS0(iobase));
752 /* prepare for fetching ATR again: after card off ATR
753 * is read again automatically */
757 dev->atr_len_retry = dev->cwarn = 0;
758 dev->mstate = M_FETCH_ATR;
760 /* minimal gap between CARDOFF and read ATR is 50msec */
761 dev->mdelay = T_50MSEC;
766 DEBUGP(4, dev, "M_FETCH_ATR\n");
767 xoutb(0x80, REG_FLAGS0(iobase));
768 DEBUGP(4, dev, "Reset BAUDV to 9600\n");
769 dev->baudv = 0x173; /* 9600 */
770 xoutb(0x02, REG_STOPBITS(iobase)); /* stopbits=2 */
771 xoutb(0x73, REG_BAUDRATE(iobase)); /* baud value */
772 xoutb(0x21, REG_FLAGS1(iobase)); /* T_Active=1, baud
774 /* warm start vs. power on: */
775 xoutb(dev->flags0 & 2 ? 0x46 : 0x44, REG_FLAGS0(iobase));
776 dev->mdelay = T_40MSEC;
777 dev->mstate = M_TIMEOUT_WAIT;
780 DEBUGP(4, dev, "M_TIMEOUT_WAIT\n");
782 io_read_num_rec_bytes(iobase, &dev->atr_len);
783 dev->mdelay = T_10MSEC;
784 dev->mstate = M_READ_ATR_LEN;
787 DEBUGP(4, dev, "M_READ_ATR_LEN\n");
788 /* infinite loop possible, since there is no timeout */
790 #define MAX_ATR_LEN_RETRY 100
792 if (dev->atr_len == io_read_num_rec_bytes(iobase, &s)) {
793 if (dev->atr_len_retry++ >= MAX_ATR_LEN_RETRY) { /* + XX msec */
794 dev->mdelay = T_10MSEC;
795 dev->mstate = M_READ_ATR;
799 dev->atr_len_retry = 0; /* set new timeout */
802 DEBUGP(4, dev, "Current ATR_LEN = %i\n", dev->atr_len);
805 DEBUGP(4, dev, "M_READ_ATR\n");
806 xoutb(0x80, REG_FLAGS0(iobase)); /* reset SM */
807 for (i = 0; i < dev->atr_len; i++) {
808 xoutb(i, REG_BUF_ADDR(iobase));
809 dev->atr[i] = inb(REG_BUF_DATA(iobase));
811 /* Deactivate T_Active flags */
812 DEBUGP(4, dev, "Deactivate T_Active flags\n");
814 xoutb(dev->flags1, REG_FLAGS1(iobase));
816 /* atr is present (which doesn't mean it's valid) */
817 set_bit(IS_ATR_PRESENT, &dev->flags);
818 if (dev->atr[0] == 0x03)
819 str_invert_revert(dev->atr, dev->atr_len);
820 atrc = parse_atr(dev);
821 if (atrc == 0) { /* atr invalid */
823 dev->mstate = M_BAD_CARD;
825 dev->mdelay = T_50MSEC;
826 dev->mstate = M_ATR_PRESENT;
827 set_bit(IS_ATR_VALID, &dev->flags);
830 if (test_bit(IS_ATR_VALID, &dev->flags) == 1) {
831 DEBUGP(4, dev, "monitor_card: ATR valid\n");
832 /* if ta1 == 0x11, no PPS necessary (default values) */
833 /* do not do PPS with multi protocol cards */
834 if ((test_bit(IS_AUTOPPS_ACT, &dev->flags) == 0) &&
835 (dev->ta1 != 0x11) &&
836 !(test_bit(IS_ANY_T0, &dev->flags) &&
837 test_bit(IS_ANY_T1, &dev->flags))) {
838 DEBUGP(4, dev, "Perform AUTOPPS\n");
839 set_bit(IS_AUTOPPS_ACT, &dev->flags);
840 ptsreq.protocol = (0x01 << dev->proto);
845 if (set_protocol(dev, &ptsreq) == 0) {
846 DEBUGP(4, dev, "AUTOPPS ret SUCC\n");
847 clear_bit(IS_AUTOPPS_ACT, &dev->flags);
848 wake_up_interruptible(&dev->atrq);
850 DEBUGP(4, dev, "AUTOPPS failed: "
851 "repower using defaults\n");
852 /* prepare for repowering */
853 clear_bit(IS_ATR_PRESENT, &dev->flags);
854 clear_bit(IS_ATR_VALID, &dev->flags);
858 dev->atr_len_retry = dev->cwarn = 0;
859 dev->mstate = M_FETCH_ATR;
861 dev->mdelay = T_50MSEC;
864 /* for cards which use slightly different
865 * params (extra guard time) */
866 set_cardparameter(dev);
867 if (test_bit(IS_AUTOPPS_ACT, &dev->flags) == 1)
868 DEBUGP(4, dev, "AUTOPPS already active "
869 "2nd try:use default values\n");
870 if (dev->ta1 == 0x11)
871 DEBUGP(4, dev, "No AUTOPPS necessary "
873 if (test_bit(IS_ANY_T0, &dev->flags)
874 && test_bit(IS_ANY_T1, &dev->flags))
875 DEBUGP(4, dev, "Do NOT perform AUTOPPS "
876 "with multiprotocol cards\n");
877 clear_bit(IS_AUTOPPS_ACT, &dev->flags);
878 wake_up_interruptible(&dev->atrq);
881 DEBUGP(4, dev, "ATR invalid\n");
882 wake_up_interruptible(&dev->atrq);
886 DEBUGP(4, dev, "M_BAD_CARD\n");
887 /* slow down warning, but prompt immediately after insertion */
888 if (dev->cwarn == 0 || dev->cwarn == 10) {
889 set_bit(IS_BAD_CARD, &dev->flags);
890 dev_warn(&dev->p_dev->dev, MODULE_NAME ": ");
891 if (test_bit(IS_BAD_CSUM, &dev->flags)) {
892 DEBUGP(4, dev, "ATR checksum (0x%.2x, should "
893 "be zero) failed\n", dev->atr_csum);
896 else if (test_bit(IS_BAD_LENGTH, &dev->flags)) {
897 DEBUGP(4, dev, "ATR length error\n");
899 DEBUGP(4, dev, "card damaged or wrong way "
904 wake_up_interruptible(&dev->atrq); /* wake open */
907 dev->mdelay = T_100MSEC;
908 dev->mstate = M_FETCH_ATR;
911 DEBUGP(7, dev, "Unknown action\n");
916 DEBUGP(7, dev, "release_io\n");
917 clear_bit(LOCK_IO, &dev->flags);
918 wake_up_interruptible(&dev->ioq); /* whoever needs IO */
921 DEBUGP(7, dev, "<- monitor_card (returns with timer)\n");
922 mod_timer(&dev->timer, jiffies + dev->mdelay);
923 clear_bit(LOCK_MONITOR, &dev->flags);
926 /* Interface to userland (file_operations) */
928 static ssize_t cmm_read(struct file *filp, __user char *buf, size_t count,
931 struct cm4000_dev *dev = filp->private_data;
932 unsigned int iobase = dev->p_dev->resource[0]->start;
936 DEBUGP(2, dev, "-> cmm_read(%s,%d)\n", current->comm, current->pid);
938 if (count == 0) /* according to manpage */
941 if (!pcmcia_dev_present(dev->p_dev) || /* device removed */
942 test_bit(IS_CMM_ABSENT, &dev->flags))
945 if (test_bit(IS_BAD_CSUM, &dev->flags))
948 /* also see the note about this in cmm_write */
949 if (wait_event_interruptible
951 ((filp->f_flags & O_NONBLOCK)
952 || (test_bit(IS_ATR_PRESENT, (void *)&dev->flags) != 0)))) {
953 if (filp->f_flags & O_NONBLOCK)
958 if (test_bit(IS_ATR_VALID, &dev->flags) == 0)
961 /* this one implements blocking IO */
962 if (wait_event_interruptible
964 ((filp->f_flags & O_NONBLOCK) || (dev->rpos < dev->rlen)))) {
965 if (filp->f_flags & O_NONBLOCK)
971 if (wait_event_interruptible
973 ((filp->f_flags & O_NONBLOCK)
974 || (test_and_set_bit(LOCK_IO, (void *)&dev->flags) == 0)))) {
975 if (filp->f_flags & O_NONBLOCK)
981 dev->flags0 = inb(REG_FLAGS0(iobase));
982 if ((dev->flags0 & 1) == 0 /* no smartcard inserted */
983 || dev->flags0 == 0xff) { /* no cardman inserted */
984 clear_bit(IS_ATR_VALID, &dev->flags);
985 if (dev->flags0 & 1) {
986 set_bit(IS_CMM_ABSENT, &dev->flags);
994 DEBUGP(4, dev, "begin read answer\n");
995 j = min(count, (size_t)(dev->rlen - dev->rpos));
999 DEBUGP(4, dev, "read1 j=%d\n", j);
1000 for (i = 0; i < j; i++) {
1001 xoutb(k++, REG_BUF_ADDR(iobase));
1002 dev->rbuf[i] = xinb(REG_BUF_DATA(iobase));
1004 j = min(count, (size_t)(dev->rlen - dev->rpos));
1006 DEBUGP(4, dev, "read2 j=%d\n", j);
1007 dev->flags1 |= 0x10; /* MSB buf addr set */
1008 xoutb(dev->flags1, REG_FLAGS1(iobase));
1009 for (; i < j; i++) {
1010 xoutb(k++, REG_BUF_ADDR(iobase));
1011 dev->rbuf[i] = xinb(REG_BUF_DATA(iobase));
1015 if (dev->proto == 0 && count > dev->rlen - dev->rpos && i) {
1016 DEBUGP(4, dev, "T=0 and count > buffer\n");
1017 dev->rbuf[i] = dev->rbuf[i - 1];
1018 dev->rbuf[i - 1] = dev->procbyte;
1023 dev->rpos = dev->rlen + 1;
1025 /* Clear T1Active */
1026 DEBUGP(4, dev, "Clear T1Active\n");
1027 dev->flags1 &= 0xdf;
1028 xoutb(dev->flags1, REG_FLAGS1(iobase));
1030 xoutb(0, REG_FLAGS1(iobase)); /* clear detectCMM */
1031 /* last check before exit */
1032 if (!io_detect_cm4000(iobase, dev)) {
1037 if (test_bit(IS_INVREV, &dev->flags) && count > 0)
1038 str_invert_revert(dev->rbuf, count);
1040 if (copy_to_user(buf, dev->rbuf, count))
1044 clear_bit(LOCK_IO, &dev->flags);
1045 wake_up_interruptible(&dev->ioq);
1047 DEBUGP(2, dev, "<- cmm_read returns: rc = %zi\n",
1048 (rc < 0 ? rc : count));
1049 return rc < 0 ? rc : count;
1052 static ssize_t cmm_write(struct file *filp, const char __user *buf,
1053 size_t count, loff_t *ppos)
1055 struct cm4000_dev *dev = filp->private_data;
1056 unsigned int iobase = dev->p_dev->resource[0]->start;
1058 unsigned char infolen;
1059 unsigned char sendT0;
1060 unsigned short nsend;
1065 DEBUGP(2, dev, "-> cmm_write(%s,%d)\n", current->comm, current->pid);
1067 if (count == 0) /* according to manpage */
1070 if (dev->proto == 0 && count < 4) {
1071 /* T0 must have at least 4 bytes */
1072 DEBUGP(4, dev, "T0 short write\n");
1076 nr = count & 0x1ff; /* max bytes to write */
1078 sendT0 = dev->proto ? 0 : nr > 5 ? 0x08 : 0;
1080 if (!pcmcia_dev_present(dev->p_dev) || /* device removed */
1081 test_bit(IS_CMM_ABSENT, &dev->flags))
1084 if (test_bit(IS_BAD_CSUM, &dev->flags)) {
1085 DEBUGP(4, dev, "bad csum\n");
1090 * wait for atr to become valid.
1091 * note: it is important to lock this code. if we dont, the monitor
1092 * could be run between test_bit and the call to sleep on the
1093 * atr-queue. if *then* the monitor detects atr valid, it will wake up
1094 * any process on the atr-queue, *but* since we have been interrupted,
1095 * we do not yet sleep on this queue. this would result in a missed
1096 * wake_up and the calling process would sleep forever (until
1097 * interrupted). also, do *not* restore_flags before sleep_on, because
1098 * this could result in the same situation!
1100 if (wait_event_interruptible
1102 ((filp->f_flags & O_NONBLOCK)
1103 || (test_bit(IS_ATR_PRESENT, (void *)&dev->flags) != 0)))) {
1104 if (filp->f_flags & O_NONBLOCK)
1106 return -ERESTARTSYS;
1109 if (test_bit(IS_ATR_VALID, &dev->flags) == 0) { /* invalid atr */
1110 DEBUGP(4, dev, "invalid ATR\n");
1115 if (wait_event_interruptible
1117 ((filp->f_flags & O_NONBLOCK)
1118 || (test_and_set_bit(LOCK_IO, (void *)&dev->flags) == 0)))) {
1119 if (filp->f_flags & O_NONBLOCK)
1121 return -ERESTARTSYS;
1124 if (copy_from_user(dev->sbuf, buf, ((count > 512) ? 512 : count)))
1128 dev->flags0 = inb(REG_FLAGS0(iobase));
1129 if ((dev->flags0 & 1) == 0 /* no smartcard inserted */
1130 || dev->flags0 == 0xff) { /* no cardman inserted */
1131 clear_bit(IS_ATR_VALID, &dev->flags);
1132 if (dev->flags0 & 1) {
1133 set_bit(IS_CMM_ABSENT, &dev->flags);
1136 DEBUGP(4, dev, "IO error\n");
1142 xoutb(0x80, REG_FLAGS0(iobase)); /* reset SM */
1144 if (!io_detect_cm4000(iobase, dev)) {
1149 /* reflect T=0 send/read mode in flags1 */
1150 dev->flags1 |= (sendT0);
1152 set_cardparameter(dev);
1154 /* dummy read, reset flag procedure received */
1155 inb(REG_FLAGS1(iobase));
1157 dev->flags1 = 0x20 /* T_Active */
1159 | (test_bit(IS_INVREV, &dev->flags) ? 2 : 0)/* inverse parity */
1160 | (((dev->baudv - 1) & 0x0100) >> 8); /* MSB-Baud */
1161 DEBUGP(1, dev, "set dev->flags1 = 0x%.2x\n", dev->flags1);
1162 xoutb(dev->flags1, REG_FLAGS1(iobase));
1165 DEBUGP(4, dev, "Xmit data\n");
1166 for (i = 0; i < nr; i++) {
1168 dev->flags1 = 0x20 /* T_Active */
1169 | (sendT0) /* SendT0 */
1170 /* inverse parity: */
1171 | (test_bit(IS_INVREV, &dev->flags) ? 2 : 0)
1172 | (((dev->baudv - 1) & 0x0100) >> 8) /* MSB-Baud */
1173 | 0x10; /* set address high */
1174 DEBUGP(4, dev, "dev->flags = 0x%.2x - set address "
1175 "high\n", dev->flags1);
1176 xoutb(dev->flags1, REG_FLAGS1(iobase));
1178 if (test_bit(IS_INVREV, &dev->flags)) {
1179 DEBUGP(4, dev, "Apply inverse convention for 0x%.2x "
1180 "-> 0x%.2x\n", (unsigned char)dev->sbuf[i],
1181 invert_revert(dev->sbuf[i]));
1182 xoutb(i, REG_BUF_ADDR(iobase));
1183 xoutb(invert_revert(dev->sbuf[i]),
1184 REG_BUF_DATA(iobase));
1186 xoutb(i, REG_BUF_ADDR(iobase));
1187 xoutb(dev->sbuf[i], REG_BUF_DATA(iobase));
1190 DEBUGP(4, dev, "Xmit done\n");
1192 if (dev->proto == 0) {
1193 /* T=0 proto: 0 byte reply */
1195 DEBUGP(4, dev, "T=0 assumes 0 byte reply\n");
1196 xoutb(i, REG_BUF_ADDR(iobase));
1197 if (test_bit(IS_INVREV, &dev->flags))
1198 xoutb(0xff, REG_BUF_DATA(iobase));
1200 xoutb(0x00, REG_BUF_DATA(iobase));
1210 nsend = 5 + (unsigned char)dev->sbuf[4];
1211 if (dev->sbuf[4] == 0)
1218 /* T0: output procedure byte */
1219 if (test_bit(IS_INVREV, &dev->flags)) {
1220 DEBUGP(4, dev, "T=0 set Procedure byte (inverse-reverse) "
1221 "0x%.2x\n", invert_revert(dev->sbuf[1]));
1222 xoutb(invert_revert(dev->sbuf[1]), REG_NUM_BYTES(iobase));
1224 DEBUGP(4, dev, "T=0 set Procedure byte 0x%.2x\n", dev->sbuf[1]);
1225 xoutb(dev->sbuf[1], REG_NUM_BYTES(iobase));
1228 DEBUGP(1, dev, "set NumSendBytes = 0x%.2x\n",
1229 (unsigned char)(nsend & 0xff));
1230 xoutb((unsigned char)(nsend & 0xff), REG_NUM_SEND(iobase));
1232 DEBUGP(1, dev, "Trigger CARDMAN CONTROLLER (0x%.2x)\n",
1233 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 xoutb(0x40 /* SM_Active */
1238 | (dev->flags0 & 2 ? 0 : 4) /* power on if needed */
1239 |(dev->proto ? 0x10 : 0x08) /* T=1/T=0 */
1240 |(nsend & 0x100) >> 8, /* MSB numSendBytes */
1241 REG_FLAGS0(iobase));
1243 /* wait for xmit done */
1244 if (dev->proto == 1) {
1245 DEBUGP(4, dev, "Wait for xmit done\n");
1246 for (i = 0; i < 1000; i++) {
1247 if (inb(REG_FLAGS0(iobase)) & 0x08)
1249 msleep_interruptible(10);
1252 DEBUGP(4, dev, "timeout waiting for xmit done\n");
1258 /* T=1: wait for infoLen */
1262 /* wait until infoLen is valid */
1263 for (i = 0; i < 6000; i++) { /* max waiting time of 1 min */
1264 io_read_num_rec_bytes(iobase, &s);
1266 infolen = inb(REG_FLAGS1(iobase));
1267 DEBUGP(4, dev, "infolen=%d\n", infolen);
1270 msleep_interruptible(10);
1273 DEBUGP(4, dev, "timeout waiting for infoLen\n");
1278 clear_bit(IS_PROCBYTE_PRESENT, &dev->flags);
1280 /* numRecBytes | bit9 of numRecytes */
1281 io_read_num_rec_bytes(iobase, &dev->rlen);
1282 for (i = 0; i < 600; i++) { /* max waiting time of 2 sec */
1284 if (dev->rlen >= infolen + 4)
1287 msleep_interruptible(10);
1288 /* numRecBytes | bit9 of numRecytes */
1289 io_read_num_rec_bytes(iobase, &s);
1290 if (s > dev->rlen) {
1291 DEBUGP(1, dev, "NumRecBytes inc (reset timeout)\n");
1292 i = 0; /* reset timeout */
1295 /* T=0: we are done when numRecBytes doesn't
1296 * increment any more and NoProcedureByte
1297 * is set and numRecBytes == bytes sent + 6
1298 * (header bytes + data + 1 for sw2)
1299 * except when the card replies an error
1300 * which means, no data will be sent back.
1302 else if (dev->proto == 0) {
1303 if ((inb(REG_BUF_ADDR(iobase)) & 0x80)) {
1304 /* no procedure byte received since last read */
1305 DEBUGP(1, dev, "NoProcedure byte set\n");
1308 /* procedure byte received since last read */
1309 DEBUGP(1, dev, "NoProcedure byte unset "
1310 "(reset timeout)\n");
1311 dev->procbyte = inb(REG_FLAGS1(iobase));
1312 DEBUGP(1, dev, "Read procedure byte 0x%.2x\n",
1314 i = 0; /* resettimeout */
1316 if (inb(REG_FLAGS0(iobase)) & 0x08) {
1317 DEBUGP(1, dev, "T0Done flag (read reply)\n");
1322 infolen = inb(REG_FLAGS1(iobase));
1325 DEBUGP(1, dev, "timeout waiting for numRecBytes\n");
1329 if (dev->proto == 0) {
1330 DEBUGP(1, dev, "Wait for T0Done bit to be set\n");
1331 for (i = 0; i < 1000; i++) {
1332 if (inb(REG_FLAGS0(iobase)) & 0x08)
1334 msleep_interruptible(10);
1337 DEBUGP(1, dev, "timeout waiting for T0Done\n");
1342 dev->procbyte = inb(REG_FLAGS1(iobase));
1343 DEBUGP(4, dev, "Read procedure byte 0x%.2x\n",
1346 io_read_num_rec_bytes(iobase, &dev->rlen);
1347 DEBUGP(4, dev, "Read NumRecBytes = %i\n", dev->rlen);
1351 /* T=1: read offset=zero, T=0: read offset=after challenge */
1352 dev->rpos = dev->proto ? 0 : nr == 4 ? 5 : nr > dev->rlen ? 5 : nr;
1353 DEBUGP(4, dev, "dev->rlen = %i, dev->rpos = %i, nr = %i\n",
1354 dev->rlen, dev->rpos, nr);
1357 DEBUGP(4, dev, "Reset SM\n");
1358 xoutb(0x80, REG_FLAGS0(iobase)); /* reset SM */
1361 DEBUGP(4, dev, "Write failed but clear T_Active\n");
1362 dev->flags1 &= 0xdf;
1363 xoutb(dev->flags1, REG_FLAGS1(iobase));
1366 clear_bit(LOCK_IO, &dev->flags);
1367 wake_up_interruptible(&dev->ioq);
1368 wake_up_interruptible(&dev->readq); /* tell read we have data */
1370 /* ITSEC E2: clear write buffer */
1371 memset((char *)dev->sbuf, 0, 512);
1373 /* return error or actually written bytes */
1374 DEBUGP(2, dev, "<- cmm_write\n");
1375 return rc < 0 ? rc : nr;
1378 static void start_monitor(struct cm4000_dev *dev)
1380 DEBUGP(3, dev, "-> start_monitor\n");
1381 if (!dev->monitor_running) {
1382 DEBUGP(5, dev, "create, init and add timer\n");
1383 timer_setup(&dev->timer, monitor_card, 0);
1384 dev->monitor_running = 1;
1385 mod_timer(&dev->timer, jiffies);
1387 DEBUGP(5, dev, "monitor already running\n");
1388 DEBUGP(3, dev, "<- start_monitor\n");
1391 static void stop_monitor(struct cm4000_dev *dev)
1393 DEBUGP(3, dev, "-> stop_monitor\n");
1394 if (dev->monitor_running) {
1395 DEBUGP(5, dev, "stopping monitor\n");
1396 terminate_monitor(dev);
1397 /* reset monitor SM */
1398 clear_bit(IS_ATR_VALID, &dev->flags);
1399 clear_bit(IS_ATR_PRESENT, &dev->flags);
1401 DEBUGP(5, dev, "monitor already stopped\n");
1402 DEBUGP(3, dev, "<- stop_monitor\n");
1405 static long cmm_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1407 struct cm4000_dev *dev = filp->private_data;
1408 unsigned int iobase = dev->p_dev->resource[0]->start;
1409 struct inode *inode = file_inode(filp);
1410 struct pcmcia_device *link;
1412 void __user *argp = (void __user *)arg;
1414 char *ioctl_names[CM_IOC_MAXNR + 1] = {
1415 [_IOC_NR(CM_IOCGSTATUS)] "CM_IOCGSTATUS",
1416 [_IOC_NR(CM_IOCGATR)] "CM_IOCGATR",
1417 [_IOC_NR(CM_IOCARDOFF)] "CM_IOCARDOFF",
1418 [_IOC_NR(CM_IOCSPTS)] "CM_IOCSPTS",
1419 [_IOC_NR(CM_IOSDBGLVL)] "CM4000_DBGLVL",
1421 DEBUGP(3, dev, "cmm_ioctl(device=%d.%d) %s\n", imajor(inode),
1422 iminor(inode), ioctl_names[_IOC_NR(cmd)]);
1425 mutex_lock(&cmm_mutex);
1427 link = dev_table[iminor(inode)];
1428 if (!pcmcia_dev_present(link)) {
1429 DEBUGP(4, dev, "DEV_OK false\n");
1433 if (test_bit(IS_CMM_ABSENT, &dev->flags)) {
1434 DEBUGP(4, dev, "CMM_ABSENT flag set\n");
1439 if (_IOC_TYPE(cmd) != CM_IOC_MAGIC) {
1440 DEBUGP(4, dev, "ioctype mismatch\n");
1443 if (_IOC_NR(cmd) > CM_IOC_MAXNR) {
1444 DEBUGP(4, dev, "iocnr mismatch\n");
1451 DEBUGP(4, dev, " ... in CM_IOCGSTATUS\n");
1455 /* clear other bits, but leave inserted & powered as
1457 status = dev->flags0 & 3;
1458 if (test_bit(IS_ATR_PRESENT, &dev->flags))
1459 status |= CM_ATR_PRESENT;
1460 if (test_bit(IS_ATR_VALID, &dev->flags))
1461 status |= CM_ATR_VALID;
1462 if (test_bit(IS_CMM_ABSENT, &dev->flags))
1463 status |= CM_NO_READER;
1464 if (test_bit(IS_BAD_CARD, &dev->flags))
1465 status |= CM_BAD_CARD;
1466 if (copy_to_user(argp, &status, sizeof(int)))
1471 DEBUGP(4, dev, "... in CM_IOCGATR\n");
1473 struct atreq __user *atreq = argp;
1475 /* allow nonblocking io and being interrupted */
1476 if (wait_event_interruptible
1478 ((filp->f_flags & O_NONBLOCK)
1479 || (test_bit(IS_ATR_PRESENT, (void *)&dev->flags)
1481 if (filp->f_flags & O_NONBLOCK)
1489 if (test_bit(IS_ATR_VALID, &dev->flags) == 0) {
1491 if (copy_to_user(&(atreq->atr_len), &tmp,
1495 if (copy_to_user(atreq->atr, dev->atr,
1500 if (copy_to_user(&(atreq->atr_len), &tmp, sizeof(int)))
1509 DEBUGP(4, dev, "... in CM_IOCARDOFF\n");
1510 if (dev->flags0 & 0x01) {
1511 DEBUGP(4, dev, " Card inserted\n");
1513 DEBUGP(2, dev, " No card inserted\n");
1515 if (dev->flags0 & 0x02) {
1516 DEBUGP(4, dev, " Card powered\n");
1518 DEBUGP(2, dev, " Card not powered\n");
1522 /* is a card inserted and powered? */
1523 if ((dev->flags0 & 0x01) && (dev->flags0 & 0x02)) {
1526 if (wait_event_interruptible
1528 ((filp->f_flags & O_NONBLOCK)
1529 || (test_and_set_bit(LOCK_IO, (void *)&dev->flags)
1531 if (filp->f_flags & O_NONBLOCK)
1537 /* Set Flags0 = 0x42 */
1538 DEBUGP(4, dev, "Set Flags0=0x42 \n");
1539 xoutb(0x42, REG_FLAGS0(iobase));
1540 clear_bit(IS_ATR_PRESENT, &dev->flags);
1541 clear_bit(IS_ATR_VALID, &dev->flags);
1542 dev->mstate = M_CARDOFF;
1543 clear_bit(LOCK_IO, &dev->flags);
1544 if (wait_event_interruptible
1546 ((filp->f_flags & O_NONBLOCK)
1547 || (test_bit(IS_ATR_VALID, (void *)&dev->flags) !=
1549 if (filp->f_flags & O_NONBLOCK)
1557 clear_bit(LOCK_IO, &dev->flags);
1558 wake_up_interruptible(&dev->ioq);
1564 struct ptsreq krnptsreq;
1566 if (copy_from_user(&krnptsreq, argp,
1567 sizeof(struct ptsreq))) {
1573 DEBUGP(4, dev, "... in CM_IOCSPTS\n");
1574 /* wait for ATR to get valid */
1575 if (wait_event_interruptible
1577 ((filp->f_flags & O_NONBLOCK)
1578 || (test_bit(IS_ATR_PRESENT, (void *)&dev->flags)
1580 if (filp->f_flags & O_NONBLOCK)
1587 if (wait_event_interruptible
1589 ((filp->f_flags & O_NONBLOCK)
1590 || (test_and_set_bit(LOCK_IO, (void *)&dev->flags)
1592 if (filp->f_flags & O_NONBLOCK)
1599 if ((rc = set_protocol(dev, &krnptsreq)) != 0) {
1600 /* auto power_on again */
1601 dev->mstate = M_FETCH_ATR;
1602 clear_bit(IS_ATR_VALID, &dev->flags);
1605 clear_bit(LOCK_IO, &dev->flags);
1606 wake_up_interruptible(&dev->ioq);
1616 DEBUGP(4, dev, "... in default (unknown IOCTL code)\n");
1620 mutex_unlock(&cmm_mutex);
1624 static int cmm_open(struct inode *inode, struct file *filp)
1626 struct cm4000_dev *dev;
1627 struct pcmcia_device *link;
1628 int minor = iminor(inode);
1631 if (minor >= CM4000_MAX_DEV)
1634 mutex_lock(&cmm_mutex);
1635 link = dev_table[minor];
1636 if (link == NULL || !pcmcia_dev_present(link)) {
1647 filp->private_data = dev;
1649 DEBUGP(2, dev, "-> cmm_open(device=%d.%d process=%s,%d)\n",
1650 imajor(inode), minor, current->comm, current->pid);
1652 /* init device variables, they may be "polluted" after close
1653 * or, the device may never have been closed (i.e. open failed)
1658 /* opening will always block since the
1659 * monitor will be started by open, which
1660 * means we have to wait for ATR becoming
1661 * valid = block until valid (or card
1664 if (filp->f_flags & O_NONBLOCK) {
1669 dev->mdelay = T_50MSEC;
1671 /* start monitoring the cardstatus */
1674 link->open = 1; /* only one open per device */
1676 DEBUGP(2, dev, "<- cmm_open\n");
1677 ret = stream_open(inode, filp);
1679 mutex_unlock(&cmm_mutex);
1683 static int cmm_close(struct inode *inode, struct file *filp)
1685 struct cm4000_dev *dev;
1686 struct pcmcia_device *link;
1687 int minor = iminor(inode);
1689 if (minor >= CM4000_MAX_DEV)
1692 link = dev_table[minor];
1698 DEBUGP(2, dev, "-> cmm_close(maj/min=%d.%d)\n",
1699 imajor(inode), minor);
1705 link->open = 0; /* only one open per device */
1706 wake_up(&dev->devq); /* socket removed? */
1708 DEBUGP(2, dev, "cmm_close\n");
1712 static void cmm_cm4000_release(struct pcmcia_device * link)
1714 struct cm4000_dev *dev = link->priv;
1716 /* dont terminate the monitor, rather rely on
1717 * close doing that for us.
1719 DEBUGP(3, dev, "-> cmm_cm4000_release\n");
1720 while (link->open) {
1721 printk(KERN_INFO MODULE_NAME ": delaying release until "
1722 "process has terminated\n");
1723 /* note: don't interrupt us:
1724 * close the applications which own
1725 * the devices _first_ !
1727 wait_event(dev->devq, (link->open == 0));
1729 /* dev->devq=NULL; this cannot be zeroed earlier */
1730 DEBUGP(3, dev, "<- cmm_cm4000_release\n");
1734 /*==== Interface to PCMCIA Layer =======================================*/
1736 static int cm4000_config_check(struct pcmcia_device *p_dev, void *priv_data)
1738 return pcmcia_request_io(p_dev);
1741 static int cm4000_config(struct pcmcia_device * link, int devno)
1743 link->config_flags |= CONF_AUTO_SET_IO;
1745 /* read the config-tuples */
1746 if (pcmcia_loop_config(link, cm4000_config_check, NULL))
1749 if (pcmcia_enable_device(link))
1755 cm4000_release(link);
1759 static int cm4000_suspend(struct pcmcia_device *link)
1761 struct cm4000_dev *dev;
1769 static int cm4000_resume(struct pcmcia_device *link)
1771 struct cm4000_dev *dev;
1780 static void cm4000_release(struct pcmcia_device *link)
1782 cmm_cm4000_release(link); /* delay release until device closed */
1783 pcmcia_disable_device(link);
1786 static int cm4000_probe(struct pcmcia_device *link)
1788 struct cm4000_dev *dev;
1791 for (i = 0; i < CM4000_MAX_DEV; i++)
1792 if (dev_table[i] == NULL)
1795 if (i == CM4000_MAX_DEV) {
1796 printk(KERN_NOTICE MODULE_NAME ": all devices in use\n");
1800 /* create a new cm4000_cs device */
1801 dev = kzalloc(sizeof(struct cm4000_dev), GFP_KERNEL);
1807 dev_table[i] = link;
1809 init_waitqueue_head(&dev->devq);
1810 init_waitqueue_head(&dev->ioq);
1811 init_waitqueue_head(&dev->atrq);
1812 init_waitqueue_head(&dev->readq);
1814 ret = cm4000_config(link, i);
1816 dev_table[i] = NULL;
1821 device_create(cmm_class, NULL, MKDEV(major, i), NULL, "cmm%d", i);
1826 static void cm4000_detach(struct pcmcia_device *link)
1828 struct cm4000_dev *dev = link->priv;
1832 for (devno = 0; devno < CM4000_MAX_DEV; devno++)
1833 if (dev_table[devno] == link)
1835 if (devno == CM4000_MAX_DEV)
1840 cm4000_release(link);
1842 dev_table[devno] = NULL;
1845 device_destroy(cmm_class, MKDEV(major, devno));
1850 static const struct file_operations cm4000_fops = {
1851 .owner = THIS_MODULE,
1854 .unlocked_ioctl = cmm_ioctl,
1856 .release= cmm_close,
1857 .llseek = no_llseek,
1860 static const struct pcmcia_device_id cm4000_ids[] = {
1861 PCMCIA_DEVICE_MANF_CARD(0x0223, 0x0002),
1862 PCMCIA_DEVICE_PROD_ID12("CardMan", "4000", 0x2FB368CA, 0xA2BD8C39),
1865 MODULE_DEVICE_TABLE(pcmcia, cm4000_ids);
1867 static struct pcmcia_driver cm4000_driver = {
1868 .owner = THIS_MODULE,
1869 .name = "cm4000_cs",
1870 .probe = cm4000_probe,
1871 .remove = cm4000_detach,
1872 .suspend = cm4000_suspend,
1873 .resume = cm4000_resume,
1874 .id_table = cm4000_ids,
1877 static int __init cmm_init(void)
1881 cmm_class = class_create("cardman_4000");
1882 if (IS_ERR(cmm_class))
1883 return PTR_ERR(cmm_class);
1885 major = register_chrdev(0, DEVICE_NAME, &cm4000_fops);
1887 printk(KERN_WARNING MODULE_NAME
1888 ": could not get major number\n");
1889 class_destroy(cmm_class);
1893 rc = pcmcia_register_driver(&cm4000_driver);
1895 unregister_chrdev(major, DEVICE_NAME);
1896 class_destroy(cmm_class);
1903 static void __exit cmm_exit(void)
1905 pcmcia_unregister_driver(&cm4000_driver);
1906 unregister_chrdev(major, DEVICE_NAME);
1907 class_destroy(cmm_class);
1910 module_init(cmm_init);
1911 module_exit(cmm_exit);
1912 MODULE_LICENSE("Dual BSD/GPL");