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>
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 fields.
120 * initialising to zero depends on it - see ZERO_DEV below. */
121 unsigned char atr_csum;
122 unsigned char atr_len_retry;
123 unsigned short atr_len;
124 unsigned short rlen; /* bytes avail. after write */
125 unsigned short rpos; /* latest read pos. write zeroes */
126 unsigned char procbyte; /* T=0 procedure byte */
127 unsigned char mstate; /* state of card monitor */
128 unsigned char cwarn; /* slow down warning */
129 unsigned char flags0; /* cardman IO-flags 0 */
130 unsigned char flags1; /* cardman IO-flags 1 */
131 unsigned int mdelay; /* variable monitor speeds, in jiffies */
133 unsigned int baudv; /* baud value for speed */
135 unsigned char proto; /* T=0, T=1, ... */
136 unsigned long flags; /* lock+flags (MONITOR,IO,ATR) * for concurrent
139 unsigned char pts[4];
141 struct timer_list timer; /* used to keep monitor running */
145 #define ZERO_DEV(dev) \
146 memset(&dev->atr_csum,0, \
147 sizeof(struct cm4000_dev) - \
148 offsetof(struct cm4000_dev, atr_csum))
150 static struct pcmcia_device *dev_table[CM4000_MAX_DEV];
151 static struct class *cmm_class;
153 /* This table doesn't use spaces after the comma between fields and thus
154 * violates CodingStyle. However, I don't really think wrapping it around will
155 * make it any clearer to read -HW */
156 static unsigned char fi_di_table[10][14] = {
157 /*FI 00 01 02 03 04 05 06 07 08 09 10 11 12 13 */
159 /* 0 */ {0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11},
160 /* 1 */ {0x01,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x91,0x11,0x11,0x11,0x11},
161 /* 2 */ {0x02,0x12,0x22,0x32,0x11,0x11,0x11,0x11,0x11,0x92,0xA2,0xB2,0x11,0x11},
162 /* 3 */ {0x03,0x13,0x23,0x33,0x43,0x53,0x63,0x11,0x11,0x93,0xA3,0xB3,0xC3,0xD3},
163 /* 4 */ {0x04,0x14,0x24,0x34,0x44,0x54,0x64,0x11,0x11,0x94,0xA4,0xB4,0xC4,0xD4},
164 /* 5 */ {0x00,0x15,0x25,0x35,0x45,0x55,0x65,0x11,0x11,0x95,0xA5,0xB5,0xC5,0xD5},
165 /* 6 */ {0x06,0x16,0x26,0x36,0x46,0x56,0x66,0x11,0x11,0x96,0xA6,0xB6,0xC6,0xD6},
166 /* 7 */ {0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11},
167 /* 8 */ {0x08,0x11,0x28,0x38,0x48,0x58,0x68,0x11,0x11,0x98,0xA8,0xB8,0xC8,0xD8},
168 /* 9 */ {0x09,0x19,0x29,0x39,0x49,0x59,0x69,0x11,0x11,0x99,0xA9,0xB9,0xC9,0xD9}
175 static inline void xoutb(unsigned char val, unsigned short port)
177 pr_debug("outb(val=%.2x,port=%.4x)\n", val, port);
180 static inline unsigned char xinb(unsigned short port)
185 pr_debug("%.2x=inb(%.4x)\n", val, port);
191 static inline unsigned char invert_revert(unsigned char ch)
196 static void str_invert_revert(unsigned char *b, int len)
200 for (i = 0; i < len; i++)
201 b[i] = invert_revert(b[i]);
204 #define ATRLENCK(dev,pos) \
205 if (pos>=dev->atr_len || pos>=MAX_ATR) \
208 static unsigned int calc_baudv(unsigned char fidi)
210 unsigned int wcrcf, wbrcf, fi_rfu, di_rfu;
216 switch ((fidi >> 4) & 0x0F) {
265 switch (fidi & 0x0F) {
301 return (wcrcf / wbrcf);
304 static unsigned short io_read_num_rec_bytes(unsigned int iobase,
312 tmp = inb(REG_NUM_BYTES(iobase)) |
313 (inb(REG_FLAGS0(iobase)) & 4 ? 0x100 : 0);
319 static int parse_atr(struct cm4000_dev *dev)
321 unsigned char any_t1, any_t0;
322 unsigned char ch, ifno;
325 DEBUGP(3, dev, "-> parse_atr: dev->atr_len = %i\n", dev->atr_len);
327 if (dev->atr_len < 3) {
328 DEBUGP(5, dev, "parse_atr: atr_len < 3\n");
332 if (dev->atr[0] == 0x3f)
333 set_bit(IS_INVREV, &dev->flags);
335 clear_bit(IS_INVREV, &dev->flags);
339 dev->proto = 0; /* XXX PROTO */
340 any_t1 = any_t0 = done = 0;
341 dev->ta1 = 0x11; /* defaults to 9600 baud */
343 if (ifno == 1 && (ch & 0x10)) {
344 /* read first interface byte and TA1 is present */
345 dev->ta1 = dev->atr[2];
346 DEBUGP(5, dev, "Card says FiDi is 0x%.2x\n", dev->ta1);
348 } else if ((ifno == 2) && (ch & 0x10)) { /* TA(2) */
353 DEBUGP(5, dev, "Yi=%.2x\n", ch & 0xf0);
354 ix += ((ch & 0x10) >> 4) /* no of int.face chars */
357 + ((ch & 0x80) >> 7);
358 /* ATRLENCK(dev,ix); */
359 if (ch & 0x80) { /* TDi */
363 DEBUGP(5, dev, "card is capable of T=1\n");
366 DEBUGP(5, dev, "card is capable of T=0\n");
372 DEBUGP(5, dev, "ix=%d noHist=%d any_t1=%d\n",
373 ix, dev->atr[1] & 15, any_t1);
374 if (ix + 1 + (dev->atr[1] & 0x0f) + any_t1 != dev->atr_len) {
375 DEBUGP(5, dev, "length error\n");
379 set_bit(IS_ANY_T0, &dev->flags);
381 if (any_t1) { /* compute csum */
384 for (i = 1; i < dev->atr_len; i++)
385 dev->atr_csum ^= dev->atr[i];
387 set_bit(IS_BAD_CSUM, &dev->flags);
388 DEBUGP(5, dev, "bad checksum\n");
393 dev->proto = 1; /* XXX PROTO */
394 set_bit(IS_ANY_T1, &dev->flags);
406 static struct card_fixup card_fixups[] = {
408 .atr = { 0x3b, 0xb3, 0x11, 0x00, 0x00, 0x41, 0x01 },
413 .atr = {0x3b, 0x76, 0x13, 0x00, 0x00, 0x80, 0x62, 0x07,
420 static void set_cardparameter(struct cm4000_dev *dev)
423 unsigned int iobase = dev->p_dev->resource[0]->start;
424 u_int8_t stopbits = 0x02; /* ISO default */
426 DEBUGP(3, dev, "-> set_cardparameter\n");
428 dev->flags1 = dev->flags1 | (((dev->baudv - 1) & 0x0100) >> 8);
429 xoutb(dev->flags1, REG_FLAGS1(iobase));
430 DEBUGP(5, dev, "flags1 = 0x%02x\n", dev->flags1);
433 xoutb((unsigned char)((dev->baudv - 1) & 0xFF), REG_BAUDRATE(iobase));
435 DEBUGP(5, dev, "baudv = %i -> write 0x%02x\n", dev->baudv,
436 ((dev->baudv - 1) & 0xFF));
439 for (i = 0; i < ARRAY_SIZE(card_fixups); i++) {
440 if (!memcmp(dev->atr, card_fixups[i].atr,
441 card_fixups[i].atr_len))
442 stopbits = card_fixups[i].stopbits;
444 xoutb(stopbits, REG_STOPBITS(iobase));
446 DEBUGP(3, dev, "<- set_cardparameter\n");
449 static int set_protocol(struct cm4000_dev *dev, struct ptsreq *ptsreq)
452 unsigned long tmp, i;
453 unsigned short num_bytes_read;
454 unsigned char pts_reply[4];
456 unsigned int iobase = dev->p_dev->resource[0]->start;
460 DEBUGP(3, dev, "-> set_protocol\n");
461 DEBUGP(5, dev, "ptsreq->Protocol = 0x%.8x, ptsreq->Flags=0x%.8x, "
462 "ptsreq->pts1=0x%.2x, ptsreq->pts2=0x%.2x, "
463 "ptsreq->pts3=0x%.2x\n", (unsigned int)ptsreq->protocol,
464 (unsigned int)ptsreq->flags, ptsreq->pts1, ptsreq->pts2,
467 /* Fill PTS structure */
470 tmp = ptsreq->protocol;
471 while ((tmp = (tmp >> 1)) > 0)
473 dev->proto = dev->pts[1]; /* Set new protocol */
474 dev->pts[1] = (0x01 << 4) | (dev->pts[1]);
476 /* Correct Fi/Di according to CM4000 Fi/Di table */
477 DEBUGP(5, dev, "Ta(1) from ATR is 0x%.2x\n", dev->ta1);
478 /* set Fi/Di according to ATR TA(1) */
479 dev->pts[2] = fi_di_table[dev->ta1 & 0x0F][(dev->ta1 >> 4) & 0x0F];
481 /* Calculate PCK character */
482 dev->pts[3] = dev->pts[0] ^ dev->pts[1] ^ dev->pts[2];
484 DEBUGP(5, dev, "pts0=%.2x, pts1=%.2x, pts2=%.2x, pts3=%.2x\n",
485 dev->pts[0], dev->pts[1], dev->pts[2], dev->pts[3]);
487 /* check card convention */
488 if (test_bit(IS_INVREV, &dev->flags))
489 str_invert_revert(dev->pts, 4);
492 xoutb(0x80, REG_FLAGS0(iobase));
494 /* Enable access to the message buffer */
495 DEBUGP(5, dev, "Enable access to the messages buffer\n");
496 dev->flags1 = 0x20 /* T_Active */
497 | (test_bit(IS_INVREV, &dev->flags) ? 0x02 : 0x00) /* inv parity */
498 | ((dev->baudv >> 8) & 0x01); /* MSB-baud */
499 xoutb(dev->flags1, REG_FLAGS1(iobase));
501 DEBUGP(5, dev, "Enable message buffer -> flags1 = 0x%.2x\n",
504 /* write challenge to the buffer */
505 DEBUGP(5, dev, "Write challenge to buffer: ");
506 for (i = 0; i < 4; i++) {
507 xoutb(i, REG_BUF_ADDR(iobase));
508 xoutb(dev->pts[i], REG_BUF_DATA(iobase)); /* buf data */
510 pr_debug("0x%.2x ", dev->pts[i]);
517 /* set number of bytes to write */
518 DEBUGP(5, dev, "Set number of bytes to write\n");
519 xoutb(0x04, REG_NUM_SEND(iobase));
521 /* Trigger CARDMAN CONTROLLER */
522 xoutb(0x50, REG_FLAGS0(iobase));
524 /* Monitor progress */
525 /* wait for xmit done */
526 DEBUGP(5, dev, "Waiting for NumRecBytes getting valid\n");
528 for (i = 0; i < 100; i++) {
529 if (inb(REG_FLAGS0(iobase)) & 0x08) {
530 DEBUGP(5, dev, "NumRecBytes is valid\n");
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;
556 /* check whether it is a short PTS reply? */
557 if (num_bytes_read == 3)
561 DEBUGP(5, dev, "Timeout reading num_bytes_read\n");
563 goto exit_setprotocol;
566 DEBUGP(5, dev, "Reset the CARDMAN CONTROLLER\n");
567 xoutb(0x80, REG_FLAGS0(iobase));
570 DEBUGP(5, dev, "Read PPS reply\n");
571 for (i = 0; i < num_bytes_read; i++) {
572 xoutb(i, REG_BUF_ADDR(iobase));
573 pts_reply[i] = inb(REG_BUF_DATA(iobase));
577 DEBUGP(2, dev, "PTSreply: ");
578 for (i = 0; i < num_bytes_read; i++) {
579 pr_debug("0x%.2x ", pts_reply[i]);
582 #endif /* CM4000_DEBUG */
584 DEBUGP(5, dev, "Clear Tactive in Flags1\n");
585 xoutb(0x20, REG_FLAGS1(iobase));
587 /* Compare ptsreq and ptsreply */
588 if ((dev->pts[0] == pts_reply[0]) &&
589 (dev->pts[1] == pts_reply[1]) &&
590 (dev->pts[2] == pts_reply[2]) && (dev->pts[3] == pts_reply[3])) {
591 /* setcardparameter according to PPS */
592 dev->baudv = calc_baudv(dev->pts[2]);
593 set_cardparameter(dev);
594 } else if ((dev->pts[0] == pts_reply[0]) &&
595 ((dev->pts[1] & 0xef) == pts_reply[1]) &&
596 ((pts_reply[0] ^ pts_reply[1]) == pts_reply[2])) {
597 /* short PTS reply, set card parameter to default values */
598 dev->baudv = calc_baudv(0x11);
599 set_cardparameter(dev);
604 DEBUGP(3, dev, "<- set_protocol\n");
608 static int io_detect_cm4000(unsigned int iobase, struct cm4000_dev *dev)
611 /* note: statemachine is assumed to be reset */
612 if (inb(REG_FLAGS0(iobase)) & 8) {
613 clear_bit(IS_ATR_VALID, &dev->flags);
614 set_bit(IS_CMM_ABSENT, &dev->flags);
615 return 0; /* detect CMM = 1 -> failure */
617 /* xoutb(0x40, REG_FLAGS1(iobase)); detectCMM */
618 xoutb(dev->flags1 | 0x40, REG_FLAGS1(iobase));
619 if ((inb(REG_FLAGS0(iobase)) & 8) == 0) {
620 clear_bit(IS_ATR_VALID, &dev->flags);
621 set_bit(IS_CMM_ABSENT, &dev->flags);
622 return 0; /* detect CMM=0 -> failure */
624 /* clear detectCMM again by restoring original flags1 */
625 xoutb(dev->flags1, REG_FLAGS1(iobase));
629 static void terminate_monitor(struct cm4000_dev *dev)
632 /* tell the monitor to stop and wait until
635 DEBUGP(3, dev, "-> terminate_monitor\n");
636 wait_event_interruptible(dev->devq,
637 test_and_set_bit(LOCK_MONITOR,
638 (void *)&dev->flags));
640 /* now, LOCK_MONITOR has been set.
641 * allow a last cycle in the monitor.
642 * the monitor will indicate that it has
643 * finished by clearing this bit.
645 DEBUGP(5, dev, "Now allow last cycle of monitor!\n");
646 while (test_bit(LOCK_MONITOR, (void *)&dev->flags))
649 DEBUGP(5, dev, "Delete timer\n");
650 del_timer_sync(&dev->timer);
652 dev->monitor_running = 0;
655 DEBUGP(3, dev, "<- terminate_monitor\n");
659 * monitor the card every 50msec. as a side-effect, retrieve the
660 * atr once a card is inserted. another side-effect of retrieving the
661 * atr is that the card will be powered on, so there is no need to
662 * power on the card explictely from the application: the driver
663 * is already doing that for you.
666 static void monitor_card(unsigned long p)
668 struct cm4000_dev *dev = (struct cm4000_dev *) p;
669 unsigned int iobase = dev->p_dev->resource[0]->start;
671 struct ptsreq ptsreq;
674 DEBUGP(7, dev, "-> monitor_card\n");
676 /* if someone has set the lock for us: we're done! */
677 if (test_and_set_bit(LOCK_MONITOR, &dev->flags)) {
678 DEBUGP(4, dev, "About to stop monitor\n");
682 dev->atr_csum = dev->atr_len_retry = dev->cwarn = 0;
683 dev->mstate = M_FETCH_ATR;
684 clear_bit(LOCK_MONITOR, &dev->flags);
685 /* close et al. are sleeping on devq, so wake it */
686 wake_up_interruptible(&dev->devq);
687 DEBUGP(2, dev, "<- monitor_card (we are done now)\n");
691 /* try to lock io: if it is already locked, just add another timer */
692 if (test_and_set_bit(LOCK_IO, (void *)&dev->flags)) {
693 DEBUGP(4, dev, "Couldn't get IO lock\n");
694 goto return_with_timer;
697 /* is a card/a reader inserted at all ? */
698 dev->flags0 = xinb(REG_FLAGS0(iobase));
699 DEBUGP(7, dev, "dev->flags0 = 0x%2x\n", dev->flags0);
700 DEBUGP(7, dev, "smartcard present: %s\n",
701 dev->flags0 & 1 ? "yes" : "no");
702 DEBUGP(7, dev, "cardman present: %s\n",
703 dev->flags0 == 0xff ? "no" : "yes");
705 if ((dev->flags0 & 1) == 0 /* no smartcard inserted */
706 || dev->flags0 == 0xff) { /* no cardman inserted */
710 dev->atr_csum = dev->atr_len_retry = dev->cwarn = 0;
711 dev->mstate = M_FETCH_ATR;
713 dev->flags &= 0x000000ff; /* only keep IO and MONITOR locks */
715 if (dev->flags0 == 0xff) {
716 DEBUGP(4, dev, "set IS_CMM_ABSENT bit\n");
717 set_bit(IS_CMM_ABSENT, &dev->flags);
718 } else if (test_bit(IS_CMM_ABSENT, &dev->flags)) {
719 DEBUGP(4, dev, "clear IS_CMM_ABSENT bit "
720 "(card is removed)\n");
721 clear_bit(IS_CMM_ABSENT, &dev->flags);
725 } else if ((dev->flags0 & 1) && test_bit(IS_CMM_ABSENT, &dev->flags)) {
726 /* cardman and card present but cardman was absent before
727 * (after suspend with inserted card) */
728 DEBUGP(4, dev, "clear IS_CMM_ABSENT bit (card is inserted)\n");
729 clear_bit(IS_CMM_ABSENT, &dev->flags);
732 if (test_bit(IS_ATR_VALID, &dev->flags) == 1) {
733 DEBUGP(7, dev, "believe ATR is already valid (do nothing)\n");
737 switch (dev->mstate) {
738 unsigned char flags0;
740 DEBUGP(4, dev, "M_CARDOFF\n");
741 flags0 = inb(REG_FLAGS0(iobase));
743 /* wait until Flags0 indicate power is off */
744 dev->mdelay = T_10MSEC;
746 /* Flags0 indicate power off and no card inserted now;
747 * Reset CARDMAN CONTROLLER */
748 xoutb(0x80, REG_FLAGS0(iobase));
750 /* prepare for fetching ATR again: after card off ATR
751 * is read again automatically */
755 dev->atr_len_retry = dev->cwarn = 0;
756 dev->mstate = M_FETCH_ATR;
758 /* minimal gap between CARDOFF and read ATR is 50msec */
759 dev->mdelay = T_50MSEC;
763 DEBUGP(4, dev, "M_FETCH_ATR\n");
764 xoutb(0x80, REG_FLAGS0(iobase));
765 DEBUGP(4, dev, "Reset BAUDV to 9600\n");
766 dev->baudv = 0x173; /* 9600 */
767 xoutb(0x02, REG_STOPBITS(iobase)); /* stopbits=2 */
768 xoutb(0x73, REG_BAUDRATE(iobase)); /* baud value */
769 xoutb(0x21, REG_FLAGS1(iobase)); /* T_Active=1, baud
771 /* warm start vs. power on: */
772 xoutb(dev->flags0 & 2 ? 0x46 : 0x44, REG_FLAGS0(iobase));
773 dev->mdelay = T_40MSEC;
774 dev->mstate = M_TIMEOUT_WAIT;
777 DEBUGP(4, dev, "M_TIMEOUT_WAIT\n");
779 io_read_num_rec_bytes(iobase, &dev->atr_len);
780 dev->mdelay = T_10MSEC;
781 dev->mstate = M_READ_ATR_LEN;
784 DEBUGP(4, dev, "M_READ_ATR_LEN\n");
785 /* infinite loop possible, since there is no timeout */
787 #define MAX_ATR_LEN_RETRY 100
789 if (dev->atr_len == io_read_num_rec_bytes(iobase, &s)) {
790 if (dev->atr_len_retry++ >= MAX_ATR_LEN_RETRY) { /* + XX msec */
791 dev->mdelay = T_10MSEC;
792 dev->mstate = M_READ_ATR;
796 dev->atr_len_retry = 0; /* set new timeout */
799 DEBUGP(4, dev, "Current ATR_LEN = %i\n", dev->atr_len);
802 DEBUGP(4, dev, "M_READ_ATR\n");
803 xoutb(0x80, REG_FLAGS0(iobase)); /* reset SM */
804 for (i = 0; i < dev->atr_len; i++) {
805 xoutb(i, REG_BUF_ADDR(iobase));
806 dev->atr[i] = inb(REG_BUF_DATA(iobase));
808 /* Deactivate T_Active flags */
809 DEBUGP(4, dev, "Deactivate T_Active flags\n");
811 xoutb(dev->flags1, REG_FLAGS1(iobase));
813 /* atr is present (which doesn't mean it's valid) */
814 set_bit(IS_ATR_PRESENT, &dev->flags);
815 if (dev->atr[0] == 0x03)
816 str_invert_revert(dev->atr, dev->atr_len);
817 atrc = parse_atr(dev);
818 if (atrc == 0) { /* atr invalid */
820 dev->mstate = M_BAD_CARD;
822 dev->mdelay = T_50MSEC;
823 dev->mstate = M_ATR_PRESENT;
824 set_bit(IS_ATR_VALID, &dev->flags);
827 if (test_bit(IS_ATR_VALID, &dev->flags) == 1) {
828 DEBUGP(4, dev, "monitor_card: ATR valid\n");
829 /* if ta1 == 0x11, no PPS necessary (default values) */
830 /* do not do PPS with multi protocol cards */
831 if ((test_bit(IS_AUTOPPS_ACT, &dev->flags) == 0) &&
832 (dev->ta1 != 0x11) &&
833 !(test_bit(IS_ANY_T0, &dev->flags) &&
834 test_bit(IS_ANY_T1, &dev->flags))) {
835 DEBUGP(4, dev, "Perform AUTOPPS\n");
836 set_bit(IS_AUTOPPS_ACT, &dev->flags);
837 ptsreq.protocol = (0x01 << dev->proto);
842 if (set_protocol(dev, &ptsreq) == 0) {
843 DEBUGP(4, dev, "AUTOPPS ret SUCC\n");
844 clear_bit(IS_AUTOPPS_ACT, &dev->flags);
845 wake_up_interruptible(&dev->atrq);
847 DEBUGP(4, dev, "AUTOPPS failed: "
848 "repower using defaults\n");
849 /* prepare for repowering */
850 clear_bit(IS_ATR_PRESENT, &dev->flags);
851 clear_bit(IS_ATR_VALID, &dev->flags);
855 dev->atr_len_retry = dev->cwarn = 0;
856 dev->mstate = M_FETCH_ATR;
858 dev->mdelay = T_50MSEC;
861 /* for cards which use slightly different
862 * params (extra guard time) */
863 set_cardparameter(dev);
864 if (test_bit(IS_AUTOPPS_ACT, &dev->flags) == 1)
865 DEBUGP(4, dev, "AUTOPPS already active "
866 "2nd try:use default values\n");
867 if (dev->ta1 == 0x11)
868 DEBUGP(4, dev, "No AUTOPPS necessary "
870 if (test_bit(IS_ANY_T0, &dev->flags)
871 && test_bit(IS_ANY_T1, &dev->flags))
872 DEBUGP(4, dev, "Do NOT perform AUTOPPS "
873 "with multiprotocol cards\n");
874 clear_bit(IS_AUTOPPS_ACT, &dev->flags);
875 wake_up_interruptible(&dev->atrq);
878 DEBUGP(4, dev, "ATR invalid\n");
879 wake_up_interruptible(&dev->atrq);
883 DEBUGP(4, dev, "M_BAD_CARD\n");
884 /* slow down warning, but prompt immediately after insertion */
885 if (dev->cwarn == 0 || dev->cwarn == 10) {
886 set_bit(IS_BAD_CARD, &dev->flags);
887 dev_warn(&dev->p_dev->dev, MODULE_NAME ": ");
888 if (test_bit(IS_BAD_CSUM, &dev->flags)) {
889 DEBUGP(4, dev, "ATR checksum (0x%.2x, should "
890 "be zero) failed\n", dev->atr_csum);
893 else if (test_bit(IS_BAD_LENGTH, &dev->flags)) {
894 DEBUGP(4, dev, "ATR length error\n");
896 DEBUGP(4, dev, "card damaged or wrong way "
901 wake_up_interruptible(&dev->atrq); /* wake open */
904 dev->mdelay = T_100MSEC;
905 dev->mstate = M_FETCH_ATR;
908 DEBUGP(7, dev, "Unknown action\n");
913 DEBUGP(7, dev, "release_io\n");
914 clear_bit(LOCK_IO, &dev->flags);
915 wake_up_interruptible(&dev->ioq); /* whoever needs IO */
918 DEBUGP(7, dev, "<- monitor_card (returns with timer)\n");
919 mod_timer(&dev->timer, jiffies + dev->mdelay);
920 clear_bit(LOCK_MONITOR, &dev->flags);
923 /* Interface to userland (file_operations) */
925 static ssize_t cmm_read(struct file *filp, __user char *buf, size_t count,
928 struct cm4000_dev *dev = filp->private_data;
929 unsigned int iobase = dev->p_dev->resource[0]->start;
933 DEBUGP(2, dev, "-> cmm_read(%s,%d)\n", current->comm, current->pid);
935 if (count == 0) /* according to manpage */
938 if (!pcmcia_dev_present(dev->p_dev) || /* device removed */
939 test_bit(IS_CMM_ABSENT, &dev->flags))
942 if (test_bit(IS_BAD_CSUM, &dev->flags))
945 /* also see the note about this in cmm_write */
946 if (wait_event_interruptible
948 ((filp->f_flags & O_NONBLOCK)
949 || (test_bit(IS_ATR_PRESENT, (void *)&dev->flags) != 0)))) {
950 if (filp->f_flags & O_NONBLOCK)
955 if (test_bit(IS_ATR_VALID, &dev->flags) == 0)
958 /* this one implements blocking IO */
959 if (wait_event_interruptible
961 ((filp->f_flags & O_NONBLOCK) || (dev->rpos < dev->rlen)))) {
962 if (filp->f_flags & O_NONBLOCK)
968 if (wait_event_interruptible
970 ((filp->f_flags & O_NONBLOCK)
971 || (test_and_set_bit(LOCK_IO, (void *)&dev->flags) == 0)))) {
972 if (filp->f_flags & O_NONBLOCK)
978 dev->flags0 = inb(REG_FLAGS0(iobase));
979 if ((dev->flags0 & 1) == 0 /* no smartcard inserted */
980 || dev->flags0 == 0xff) { /* no cardman inserted */
981 clear_bit(IS_ATR_VALID, &dev->flags);
982 if (dev->flags0 & 1) {
983 set_bit(IS_CMM_ABSENT, &dev->flags);
991 DEBUGP(4, dev, "begin read answer\n");
992 j = min(count, (size_t)(dev->rlen - dev->rpos));
996 DEBUGP(4, dev, "read1 j=%d\n", j);
997 for (i = 0; i < j; i++) {
998 xoutb(k++, REG_BUF_ADDR(iobase));
999 dev->rbuf[i] = xinb(REG_BUF_DATA(iobase));
1001 j = min(count, (size_t)(dev->rlen - dev->rpos));
1003 DEBUGP(4, dev, "read2 j=%d\n", j);
1004 dev->flags1 |= 0x10; /* MSB buf addr set */
1005 xoutb(dev->flags1, REG_FLAGS1(iobase));
1006 for (; i < j; i++) {
1007 xoutb(k++, REG_BUF_ADDR(iobase));
1008 dev->rbuf[i] = xinb(REG_BUF_DATA(iobase));
1012 if (dev->proto == 0 && count > dev->rlen - dev->rpos && i) {
1013 DEBUGP(4, dev, "T=0 and count > buffer\n");
1014 dev->rbuf[i] = dev->rbuf[i - 1];
1015 dev->rbuf[i - 1] = dev->procbyte;
1020 dev->rpos = dev->rlen + 1;
1022 /* Clear T1Active */
1023 DEBUGP(4, dev, "Clear T1Active\n");
1024 dev->flags1 &= 0xdf;
1025 xoutb(dev->flags1, REG_FLAGS1(iobase));
1027 xoutb(0, REG_FLAGS1(iobase)); /* clear detectCMM */
1028 /* last check before exit */
1029 if (!io_detect_cm4000(iobase, dev)) {
1034 if (test_bit(IS_INVREV, &dev->flags) && count > 0)
1035 str_invert_revert(dev->rbuf, count);
1037 if (copy_to_user(buf, dev->rbuf, count))
1041 clear_bit(LOCK_IO, &dev->flags);
1042 wake_up_interruptible(&dev->ioq);
1044 DEBUGP(2, dev, "<- cmm_read returns: rc = %Zi\n",
1045 (rc < 0 ? rc : count));
1046 return rc < 0 ? rc : count;
1049 static ssize_t cmm_write(struct file *filp, const char __user *buf,
1050 size_t count, loff_t *ppos)
1052 struct cm4000_dev *dev = filp->private_data;
1053 unsigned int iobase = dev->p_dev->resource[0]->start;
1056 unsigned char infolen;
1057 unsigned char sendT0;
1058 unsigned short nsend;
1063 DEBUGP(2, dev, "-> cmm_write(%s,%d)\n", current->comm, current->pid);
1065 if (count == 0) /* according to manpage */
1068 if (dev->proto == 0 && count < 4) {
1069 /* T0 must have at least 4 bytes */
1070 DEBUGP(4, dev, "T0 short write\n");
1074 nr = count & 0x1ff; /* max bytes to write */
1076 sendT0 = dev->proto ? 0 : nr > 5 ? 0x08 : 0;
1078 if (!pcmcia_dev_present(dev->p_dev) || /* device removed */
1079 test_bit(IS_CMM_ABSENT, &dev->flags))
1082 if (test_bit(IS_BAD_CSUM, &dev->flags)) {
1083 DEBUGP(4, dev, "bad csum\n");
1088 * wait for atr to become valid.
1089 * note: it is important to lock this code. if we dont, the monitor
1090 * could be run between test_bit and the call to sleep on the
1091 * atr-queue. if *then* the monitor detects atr valid, it will wake up
1092 * any process on the atr-queue, *but* since we have been interrupted,
1093 * we do not yet sleep on this queue. this would result in a missed
1094 * wake_up and the calling process would sleep forever (until
1095 * interrupted). also, do *not* restore_flags before sleep_on, because
1096 * this could result in the same situation!
1098 if (wait_event_interruptible
1100 ((filp->f_flags & O_NONBLOCK)
1101 || (test_bit(IS_ATR_PRESENT, (void *)&dev->flags) != 0)))) {
1102 if (filp->f_flags & O_NONBLOCK)
1104 return -ERESTARTSYS;
1107 if (test_bit(IS_ATR_VALID, &dev->flags) == 0) { /* invalid atr */
1108 DEBUGP(4, dev, "invalid ATR\n");
1113 if (wait_event_interruptible
1115 ((filp->f_flags & O_NONBLOCK)
1116 || (test_and_set_bit(LOCK_IO, (void *)&dev->flags) == 0)))) {
1117 if (filp->f_flags & O_NONBLOCK)
1119 return -ERESTARTSYS;
1122 if (copy_from_user(dev->sbuf, buf, ((count > 512) ? 512 : count)))
1126 dev->flags0 = inb(REG_FLAGS0(iobase));
1127 if ((dev->flags0 & 1) == 0 /* no smartcard inserted */
1128 || dev->flags0 == 0xff) { /* no cardman inserted */
1129 clear_bit(IS_ATR_VALID, &dev->flags);
1130 if (dev->flags0 & 1) {
1131 set_bit(IS_CMM_ABSENT, &dev->flags);
1134 DEBUGP(4, dev, "IO error\n");
1140 xoutb(0x80, REG_FLAGS0(iobase)); /* reset SM */
1142 if (!io_detect_cm4000(iobase, dev)) {
1147 /* reflect T=0 send/read mode in flags1 */
1148 dev->flags1 |= (sendT0);
1150 set_cardparameter(dev);
1152 /* dummy read, reset flag procedure received */
1153 tmp = inb(REG_FLAGS1(iobase));
1155 dev->flags1 = 0x20 /* T_Active */
1157 | (test_bit(IS_INVREV, &dev->flags) ? 2 : 0)/* inverse parity */
1158 | (((dev->baudv - 1) & 0x0100) >> 8); /* MSB-Baud */
1159 DEBUGP(1, dev, "set dev->flags1 = 0x%.2x\n", dev->flags1);
1160 xoutb(dev->flags1, REG_FLAGS1(iobase));
1163 DEBUGP(4, dev, "Xmit data\n");
1164 for (i = 0; i < nr; i++) {
1166 dev->flags1 = 0x20 /* T_Active */
1167 | (sendT0) /* SendT0 */
1168 /* inverse parity: */
1169 | (test_bit(IS_INVREV, &dev->flags) ? 2 : 0)
1170 | (((dev->baudv - 1) & 0x0100) >> 8) /* MSB-Baud */
1171 | 0x10; /* set address high */
1172 DEBUGP(4, dev, "dev->flags = 0x%.2x - set address "
1173 "high\n", dev->flags1);
1174 xoutb(dev->flags1, REG_FLAGS1(iobase));
1176 if (test_bit(IS_INVREV, &dev->flags)) {
1177 DEBUGP(4, dev, "Apply inverse convention for 0x%.2x "
1178 "-> 0x%.2x\n", (unsigned char)dev->sbuf[i],
1179 invert_revert(dev->sbuf[i]));
1180 xoutb(i, REG_BUF_ADDR(iobase));
1181 xoutb(invert_revert(dev->sbuf[i]),
1182 REG_BUF_DATA(iobase));
1184 xoutb(i, REG_BUF_ADDR(iobase));
1185 xoutb(dev->sbuf[i], REG_BUF_DATA(iobase));
1188 DEBUGP(4, dev, "Xmit done\n");
1190 if (dev->proto == 0) {
1191 /* T=0 proto: 0 byte reply */
1193 DEBUGP(4, dev, "T=0 assumes 0 byte reply\n");
1194 xoutb(i, REG_BUF_ADDR(iobase));
1195 if (test_bit(IS_INVREV, &dev->flags))
1196 xoutb(0xff, REG_BUF_DATA(iobase));
1198 xoutb(0x00, REG_BUF_DATA(iobase));
1208 nsend = 5 + (unsigned char)dev->sbuf[4];
1209 if (dev->sbuf[4] == 0)
1216 /* T0: output procedure byte */
1217 if (test_bit(IS_INVREV, &dev->flags)) {
1218 DEBUGP(4, dev, "T=0 set Procedure byte (inverse-reverse) "
1219 "0x%.2x\n", invert_revert(dev->sbuf[1]));
1220 xoutb(invert_revert(dev->sbuf[1]), REG_NUM_BYTES(iobase));
1222 DEBUGP(4, dev, "T=0 set Procedure byte 0x%.2x\n", dev->sbuf[1]);
1223 xoutb(dev->sbuf[1], REG_NUM_BYTES(iobase));
1226 DEBUGP(1, dev, "set NumSendBytes = 0x%.2x\n",
1227 (unsigned char)(nsend & 0xff));
1228 xoutb((unsigned char)(nsend & 0xff), REG_NUM_SEND(iobase));
1230 DEBUGP(1, dev, "Trigger CARDMAN CONTROLLER (0x%.2x)\n",
1231 0x40 /* SM_Active */
1232 | (dev->flags0 & 2 ? 0 : 4) /* power on if needed */
1233 |(dev->proto ? 0x10 : 0x08) /* T=1/T=0 */
1234 |(nsend & 0x100) >> 8 /* MSB numSendBytes */ );
1235 xoutb(0x40 /* SM_Active */
1236 | (dev->flags0 & 2 ? 0 : 4) /* power on if needed */
1237 |(dev->proto ? 0x10 : 0x08) /* T=1/T=0 */
1238 |(nsend & 0x100) >> 8, /* MSB numSendBytes */
1239 REG_FLAGS0(iobase));
1241 /* wait for xmit done */
1242 if (dev->proto == 1) {
1243 DEBUGP(4, dev, "Wait for xmit done\n");
1244 for (i = 0; i < 1000; i++) {
1245 if (inb(REG_FLAGS0(iobase)) & 0x08)
1247 msleep_interruptible(10);
1250 DEBUGP(4, dev, "timeout waiting for xmit done\n");
1256 /* T=1: wait for infoLen */
1260 /* wait until infoLen is valid */
1261 for (i = 0; i < 6000; i++) { /* max waiting time of 1 min */
1262 io_read_num_rec_bytes(iobase, &s);
1264 infolen = inb(REG_FLAGS1(iobase));
1265 DEBUGP(4, dev, "infolen=%d\n", infolen);
1268 msleep_interruptible(10);
1271 DEBUGP(4, dev, "timeout waiting for infoLen\n");
1276 clear_bit(IS_PROCBYTE_PRESENT, &dev->flags);
1278 /* numRecBytes | bit9 of numRecytes */
1279 io_read_num_rec_bytes(iobase, &dev->rlen);
1280 for (i = 0; i < 600; i++) { /* max waiting time of 2 sec */
1282 if (dev->rlen >= infolen + 4)
1285 msleep_interruptible(10);
1286 /* numRecBytes | bit9 of numRecytes */
1287 io_read_num_rec_bytes(iobase, &s);
1288 if (s > dev->rlen) {
1289 DEBUGP(1, dev, "NumRecBytes inc (reset timeout)\n");
1290 i = 0; /* reset timeout */
1293 /* T=0: we are done when numRecBytes doesn't
1294 * increment any more and NoProcedureByte
1295 * is set and numRecBytes == bytes sent + 6
1296 * (header bytes + data + 1 for sw2)
1297 * except when the card replies an error
1298 * which means, no data will be sent back.
1300 else if (dev->proto == 0) {
1301 if ((inb(REG_BUF_ADDR(iobase)) & 0x80)) {
1302 /* no procedure byte received since last read */
1303 DEBUGP(1, dev, "NoProcedure byte set\n");
1306 /* procedure byte received since last read */
1307 DEBUGP(1, dev, "NoProcedure byte unset "
1308 "(reset timeout)\n");
1309 dev->procbyte = inb(REG_FLAGS1(iobase));
1310 DEBUGP(1, dev, "Read procedure byte 0x%.2x\n",
1312 i = 0; /* resettimeout */
1314 if (inb(REG_FLAGS0(iobase)) & 0x08) {
1315 DEBUGP(1, dev, "T0Done flag (read reply)\n");
1320 infolen = inb(REG_FLAGS1(iobase));
1323 DEBUGP(1, dev, "timeout waiting for numRecBytes\n");
1327 if (dev->proto == 0) {
1328 DEBUGP(1, dev, "Wait for T0Done bit to be set\n");
1329 for (i = 0; i < 1000; i++) {
1330 if (inb(REG_FLAGS0(iobase)) & 0x08)
1332 msleep_interruptible(10);
1335 DEBUGP(1, dev, "timeout waiting for T0Done\n");
1340 dev->procbyte = inb(REG_FLAGS1(iobase));
1341 DEBUGP(4, dev, "Read procedure byte 0x%.2x\n",
1344 io_read_num_rec_bytes(iobase, &dev->rlen);
1345 DEBUGP(4, dev, "Read NumRecBytes = %i\n", dev->rlen);
1349 /* T=1: read offset=zero, T=0: read offset=after challenge */
1350 dev->rpos = dev->proto ? 0 : nr == 4 ? 5 : nr > dev->rlen ? 5 : nr;
1351 DEBUGP(4, dev, "dev->rlen = %i, dev->rpos = %i, nr = %i\n",
1352 dev->rlen, dev->rpos, nr);
1355 DEBUGP(4, dev, "Reset SM\n");
1356 xoutb(0x80, REG_FLAGS0(iobase)); /* reset SM */
1359 DEBUGP(4, dev, "Write failed but clear T_Active\n");
1360 dev->flags1 &= 0xdf;
1361 xoutb(dev->flags1, REG_FLAGS1(iobase));
1364 clear_bit(LOCK_IO, &dev->flags);
1365 wake_up_interruptible(&dev->ioq);
1366 wake_up_interruptible(&dev->readq); /* tell read we have data */
1368 /* ITSEC E2: clear write buffer */
1369 memset((char *)dev->sbuf, 0, 512);
1371 /* return error or actually written bytes */
1372 DEBUGP(2, dev, "<- cmm_write\n");
1373 return rc < 0 ? rc : nr;
1376 static void start_monitor(struct cm4000_dev *dev)
1378 DEBUGP(3, dev, "-> start_monitor\n");
1379 if (!dev->monitor_running) {
1380 DEBUGP(5, dev, "create, init and add timer\n");
1381 setup_timer(&dev->timer, monitor_card, (unsigned long)dev);
1382 dev->monitor_running = 1;
1383 mod_timer(&dev->timer, jiffies);
1385 DEBUGP(5, dev, "monitor already running\n");
1386 DEBUGP(3, dev, "<- start_monitor\n");
1389 static void stop_monitor(struct cm4000_dev *dev)
1391 DEBUGP(3, dev, "-> stop_monitor\n");
1392 if (dev->monitor_running) {
1393 DEBUGP(5, dev, "stopping monitor\n");
1394 terminate_monitor(dev);
1395 /* reset monitor SM */
1396 clear_bit(IS_ATR_VALID, &dev->flags);
1397 clear_bit(IS_ATR_PRESENT, &dev->flags);
1399 DEBUGP(5, dev, "monitor already stopped\n");
1400 DEBUGP(3, dev, "<- stop_monitor\n");
1403 static long cmm_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1405 struct cm4000_dev *dev = filp->private_data;
1406 unsigned int iobase = dev->p_dev->resource[0]->start;
1407 struct inode *inode = file_inode(filp);
1408 struct pcmcia_device *link;
1411 void __user *argp = (void __user *)arg;
1413 char *ioctl_names[CM_IOC_MAXNR + 1] = {
1414 [_IOC_NR(CM_IOCGSTATUS)] "CM_IOCGSTATUS",
1415 [_IOC_NR(CM_IOCGATR)] "CM_IOCGATR",
1416 [_IOC_NR(CM_IOCARDOFF)] "CM_IOCARDOFF",
1417 [_IOC_NR(CM_IOCSPTS)] "CM_IOCSPTS",
1418 [_IOC_NR(CM_IOSDBGLVL)] "CM4000_DBGLVL",
1420 DEBUGP(3, dev, "cmm_ioctl(device=%d.%d) %s\n", imajor(inode),
1421 iminor(inode), ioctl_names[_IOC_NR(cmd)]);
1424 mutex_lock(&cmm_mutex);
1426 link = dev_table[iminor(inode)];
1427 if (!pcmcia_dev_present(link)) {
1428 DEBUGP(4, dev, "DEV_OK false\n");
1432 if (test_bit(IS_CMM_ABSENT, &dev->flags)) {
1433 DEBUGP(4, dev, "CMM_ABSENT flag set\n");
1438 if (_IOC_TYPE(cmd) != CM_IOC_MAGIC) {
1439 DEBUGP(4, dev, "ioctype mismatch\n");
1442 if (_IOC_NR(cmd) > CM_IOC_MAXNR) {
1443 DEBUGP(4, dev, "iocnr mismatch\n");
1446 size = _IOC_SIZE(cmd);
1448 DEBUGP(4, dev, "iocdir=%.4x iocr=%.4x iocw=%.4x iocsize=%d cmd=%.4x\n",
1449 _IOC_DIR(cmd), _IOC_READ, _IOC_WRITE, size, cmd);
1451 if (_IOC_DIR(cmd) & _IOC_READ) {
1452 if (!access_ok(VERIFY_WRITE, argp, size))
1455 if (_IOC_DIR(cmd) & _IOC_WRITE) {
1456 if (!access_ok(VERIFY_READ, argp, size))
1463 DEBUGP(4, dev, " ... in CM_IOCGSTATUS\n");
1467 /* clear other bits, but leave inserted & powered as
1469 status = dev->flags0 & 3;
1470 if (test_bit(IS_ATR_PRESENT, &dev->flags))
1471 status |= CM_ATR_PRESENT;
1472 if (test_bit(IS_ATR_VALID, &dev->flags))
1473 status |= CM_ATR_VALID;
1474 if (test_bit(IS_CMM_ABSENT, &dev->flags))
1475 status |= CM_NO_READER;
1476 if (test_bit(IS_BAD_CARD, &dev->flags))
1477 status |= CM_BAD_CARD;
1478 if (copy_to_user(argp, &status, sizeof(int)))
1483 DEBUGP(4, dev, "... in CM_IOCGATR\n");
1485 struct atreq __user *atreq = argp;
1487 /* allow nonblocking io and being interrupted */
1488 if (wait_event_interruptible
1490 ((filp->f_flags & O_NONBLOCK)
1491 || (test_bit(IS_ATR_PRESENT, (void *)&dev->flags)
1493 if (filp->f_flags & O_NONBLOCK)
1501 if (test_bit(IS_ATR_VALID, &dev->flags) == 0) {
1503 if (copy_to_user(&(atreq->atr_len), &tmp,
1507 if (copy_to_user(atreq->atr, dev->atr,
1512 if (copy_to_user(&(atreq->atr_len), &tmp, sizeof(int)))
1521 DEBUGP(4, dev, "... in CM_IOCARDOFF\n");
1522 if (dev->flags0 & 0x01) {
1523 DEBUGP(4, dev, " Card inserted\n");
1525 DEBUGP(2, dev, " No card inserted\n");
1527 if (dev->flags0 & 0x02) {
1528 DEBUGP(4, dev, " Card powered\n");
1530 DEBUGP(2, dev, " Card not powered\n");
1534 /* is a card inserted and powered? */
1535 if ((dev->flags0 & 0x01) && (dev->flags0 & 0x02)) {
1538 if (wait_event_interruptible
1540 ((filp->f_flags & O_NONBLOCK)
1541 || (test_and_set_bit(LOCK_IO, (void *)&dev->flags)
1543 if (filp->f_flags & O_NONBLOCK)
1549 /* Set Flags0 = 0x42 */
1550 DEBUGP(4, dev, "Set Flags0=0x42 \n");
1551 xoutb(0x42, REG_FLAGS0(iobase));
1552 clear_bit(IS_ATR_PRESENT, &dev->flags);
1553 clear_bit(IS_ATR_VALID, &dev->flags);
1554 dev->mstate = M_CARDOFF;
1555 clear_bit(LOCK_IO, &dev->flags);
1556 if (wait_event_interruptible
1558 ((filp->f_flags & O_NONBLOCK)
1559 || (test_bit(IS_ATR_VALID, (void *)&dev->flags) !=
1561 if (filp->f_flags & O_NONBLOCK)
1569 clear_bit(LOCK_IO, &dev->flags);
1570 wake_up_interruptible(&dev->ioq);
1576 struct ptsreq krnptsreq;
1578 if (copy_from_user(&krnptsreq, argp,
1579 sizeof(struct ptsreq))) {
1585 DEBUGP(4, dev, "... in CM_IOCSPTS\n");
1586 /* wait for ATR to get valid */
1587 if (wait_event_interruptible
1589 ((filp->f_flags & O_NONBLOCK)
1590 || (test_bit(IS_ATR_PRESENT, (void *)&dev->flags)
1592 if (filp->f_flags & O_NONBLOCK)
1599 if (wait_event_interruptible
1601 ((filp->f_flags & O_NONBLOCK)
1602 || (test_and_set_bit(LOCK_IO, (void *)&dev->flags)
1604 if (filp->f_flags & O_NONBLOCK)
1611 if ((rc = set_protocol(dev, &krnptsreq)) != 0) {
1612 /* auto power_on again */
1613 dev->mstate = M_FETCH_ATR;
1614 clear_bit(IS_ATR_VALID, &dev->flags);
1617 clear_bit(LOCK_IO, &dev->flags);
1618 wake_up_interruptible(&dev->ioq);
1628 DEBUGP(4, dev, "... in default (unknown IOCTL code)\n");
1632 mutex_unlock(&cmm_mutex);
1636 static int cmm_open(struct inode *inode, struct file *filp)
1638 struct cm4000_dev *dev;
1639 struct pcmcia_device *link;
1640 int minor = iminor(inode);
1643 if (minor >= CM4000_MAX_DEV)
1646 mutex_lock(&cmm_mutex);
1647 link = dev_table[minor];
1648 if (link == NULL || !pcmcia_dev_present(link)) {
1659 filp->private_data = dev;
1661 DEBUGP(2, dev, "-> cmm_open(device=%d.%d process=%s,%d)\n",
1662 imajor(inode), minor, current->comm, current->pid);
1664 /* init device variables, they may be "polluted" after close
1665 * or, the device may never have been closed (i.e. open failed)
1670 /* opening will always block since the
1671 * monitor will be started by open, which
1672 * means we have to wait for ATR becoming
1673 * valid = block until valid (or card
1676 if (filp->f_flags & O_NONBLOCK) {
1681 dev->mdelay = T_50MSEC;
1683 /* start monitoring the cardstatus */
1686 link->open = 1; /* only one open per device */
1688 DEBUGP(2, dev, "<- cmm_open\n");
1689 ret = nonseekable_open(inode, filp);
1691 mutex_unlock(&cmm_mutex);
1695 static int cmm_close(struct inode *inode, struct file *filp)
1697 struct cm4000_dev *dev;
1698 struct pcmcia_device *link;
1699 int minor = iminor(inode);
1701 if (minor >= CM4000_MAX_DEV)
1704 link = dev_table[minor];
1710 DEBUGP(2, dev, "-> cmm_close(maj/min=%d.%d)\n",
1711 imajor(inode), minor);
1717 link->open = 0; /* only one open per device */
1718 wake_up(&dev->devq); /* socket removed? */
1720 DEBUGP(2, dev, "cmm_close\n");
1724 static void cmm_cm4000_release(struct pcmcia_device * link)
1726 struct cm4000_dev *dev = link->priv;
1728 /* dont terminate the monitor, rather rely on
1729 * close doing that for us.
1731 DEBUGP(3, dev, "-> cmm_cm4000_release\n");
1732 while (link->open) {
1733 printk(KERN_INFO MODULE_NAME ": delaying release until "
1734 "process has terminated\n");
1735 /* note: don't interrupt us:
1736 * close the applications which own
1737 * the devices _first_ !
1739 wait_event(dev->devq, (link->open == 0));
1741 /* dev->devq=NULL; this cannot be zeroed earlier */
1742 DEBUGP(3, dev, "<- cmm_cm4000_release\n");
1746 /*==== Interface to PCMCIA Layer =======================================*/
1748 static int cm4000_config_check(struct pcmcia_device *p_dev, void *priv_data)
1750 return pcmcia_request_io(p_dev);
1753 static int cm4000_config(struct pcmcia_device * link, int devno)
1755 struct cm4000_dev *dev;
1757 link->config_flags |= CONF_AUTO_SET_IO;
1759 /* read the config-tuples */
1760 if (pcmcia_loop_config(link, cm4000_config_check, NULL))
1763 if (pcmcia_enable_device(link))
1771 cm4000_release(link);
1775 static int cm4000_suspend(struct pcmcia_device *link)
1777 struct cm4000_dev *dev;
1785 static int cm4000_resume(struct pcmcia_device *link)
1787 struct cm4000_dev *dev;
1796 static void cm4000_release(struct pcmcia_device *link)
1798 cmm_cm4000_release(link); /* delay release until device closed */
1799 pcmcia_disable_device(link);
1802 static int cm4000_probe(struct pcmcia_device *link)
1804 struct cm4000_dev *dev;
1807 for (i = 0; i < CM4000_MAX_DEV; i++)
1808 if (dev_table[i] == NULL)
1811 if (i == CM4000_MAX_DEV) {
1812 printk(KERN_NOTICE MODULE_NAME ": all devices in use\n");
1816 /* create a new cm4000_cs device */
1817 dev = kzalloc(sizeof(struct cm4000_dev), GFP_KERNEL);
1823 dev_table[i] = link;
1825 init_waitqueue_head(&dev->devq);
1826 init_waitqueue_head(&dev->ioq);
1827 init_waitqueue_head(&dev->atrq);
1828 init_waitqueue_head(&dev->readq);
1830 ret = cm4000_config(link, i);
1832 dev_table[i] = NULL;
1837 device_create(cmm_class, NULL, MKDEV(major, i), NULL, "cmm%d", i);
1842 static void cm4000_detach(struct pcmcia_device *link)
1844 struct cm4000_dev *dev = link->priv;
1848 for (devno = 0; devno < CM4000_MAX_DEV; devno++)
1849 if (dev_table[devno] == link)
1851 if (devno == CM4000_MAX_DEV)
1856 cm4000_release(link);
1858 dev_table[devno] = NULL;
1861 device_destroy(cmm_class, MKDEV(major, devno));
1866 static const struct file_operations cm4000_fops = {
1867 .owner = THIS_MODULE,
1870 .unlocked_ioctl = cmm_ioctl,
1872 .release= cmm_close,
1873 .llseek = no_llseek,
1876 static const struct pcmcia_device_id cm4000_ids[] = {
1877 PCMCIA_DEVICE_MANF_CARD(0x0223, 0x0002),
1878 PCMCIA_DEVICE_PROD_ID12("CardMan", "4000", 0x2FB368CA, 0xA2BD8C39),
1881 MODULE_DEVICE_TABLE(pcmcia, cm4000_ids);
1883 static struct pcmcia_driver cm4000_driver = {
1884 .owner = THIS_MODULE,
1885 .name = "cm4000_cs",
1886 .probe = cm4000_probe,
1887 .remove = cm4000_detach,
1888 .suspend = cm4000_suspend,
1889 .resume = cm4000_resume,
1890 .id_table = cm4000_ids,
1893 static int __init cmm_init(void)
1897 cmm_class = class_create(THIS_MODULE, "cardman_4000");
1898 if (IS_ERR(cmm_class))
1899 return PTR_ERR(cmm_class);
1901 major = register_chrdev(0, DEVICE_NAME, &cm4000_fops);
1903 printk(KERN_WARNING MODULE_NAME
1904 ": could not get major number\n");
1905 class_destroy(cmm_class);
1909 rc = pcmcia_register_driver(&cm4000_driver);
1911 unregister_chrdev(major, DEVICE_NAME);
1912 class_destroy(cmm_class);
1919 static void __exit cmm_exit(void)
1921 pcmcia_unregister_driver(&cm4000_driver);
1922 unregister_chrdev(major, DEVICE_NAME);
1923 class_destroy(cmm_class);
1926 module_init(cmm_init);
1927 module_exit(cmm_exit);
1928 MODULE_LICENSE("Dual BSD/GPL");