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 /* #define PCMCIA_DEBUG 6 */
28 #include <linux/kernel.h>
29 #include <linux/module.h>
30 #include <linux/slab.h>
31 #include <linux/init.h>
33 #include <linux/delay.h>
34 #include <linux/bitrev.h>
35 #include <linux/smp_lock.h>
36 #include <linux/uaccess.h>
39 #include <pcmcia/cs_types.h>
40 #include <pcmcia/cs.h>
41 #include <pcmcia/cistpl.h>
42 #include <pcmcia/cisreg.h>
43 #include <pcmcia/ciscode.h>
44 #include <pcmcia/ds.h>
46 #include <linux/cm4000_cs.h>
48 /* #define ATR_CSUM */
51 #define reader_to_dev(x) (&handle_to_dev(x->p_dev))
52 static int pc_debug = PCMCIA_DEBUG;
53 module_param(pc_debug, int, 0600);
54 #define DEBUGP(n, rdr, x, args...) do { \
55 if (pc_debug >= (n)) \
56 dev_printk(KERN_DEBUG, reader_to_dev(rdr), "%s:" x, \
57 __func__ , ## args); \
60 #define DEBUGP(n, rdr, x, args...)
62 static char *version = "cm4000_cs.c v2.4.0gm6 - All bugs added by Harald Welte";
65 #define T_10MSEC msecs_to_jiffies(10)
66 #define T_20MSEC msecs_to_jiffies(20)
67 #define T_40MSEC msecs_to_jiffies(40)
68 #define T_50MSEC msecs_to_jiffies(50)
69 #define T_100MSEC msecs_to_jiffies(100)
70 #define T_500MSEC msecs_to_jiffies(500)
72 static void cm4000_release(struct pcmcia_device *link);
74 static int major; /* major number we get from the kernel */
76 /* note: the first state has to have number 0 always */
79 #define M_TIMEOUT_WAIT 1
80 #define M_READ_ATR_LEN 2
82 #define M_ATR_PRESENT 4
87 #define LOCK_MONITOR 1
89 #define IS_AUTOPPS_ACT 6
90 #define IS_PROCBYTE_PRESENT 7
94 #define IS_ATR_PRESENT 11
95 #define IS_ATR_VALID 12
96 #define IS_CMM_ABSENT 13
97 #define IS_BAD_LENGTH 14
98 #define IS_BAD_CSUM 15
99 #define IS_BAD_CARD 16
101 #define REG_FLAGS0(x) (x + 0)
102 #define REG_FLAGS1(x) (x + 1)
103 #define REG_NUM_BYTES(x) (x + 2)
104 #define REG_BUF_ADDR(x) (x + 3)
105 #define REG_BUF_DATA(x) (x + 4)
106 #define REG_NUM_SEND(x) (x + 5)
107 #define REG_BAUDRATE(x) (x + 6)
108 #define REG_STOPBITS(x) (x + 7)
111 struct pcmcia_device *p_dev;
112 dev_node_t node; /* OS node (major,minor) */
114 unsigned char atr[MAX_ATR];
115 unsigned char rbuf[512];
116 unsigned char sbuf[512];
118 wait_queue_head_t devq; /* when removing cardman must not be
121 wait_queue_head_t ioq; /* if IO is locked, wait on this Q */
122 wait_queue_head_t atrq; /* wait for ATR valid */
123 wait_queue_head_t readq; /* used by write to wake blk.read */
125 /* warning: do not move this fields.
126 * initialising to zero depends on it - see ZERO_DEV below. */
127 unsigned char atr_csum;
128 unsigned char atr_len_retry;
129 unsigned short atr_len;
130 unsigned short rlen; /* bytes avail. after write */
131 unsigned short rpos; /* latest read pos. write zeroes */
132 unsigned char procbyte; /* T=0 procedure byte */
133 unsigned char mstate; /* state of card monitor */
134 unsigned char cwarn; /* slow down warning */
135 unsigned char flags0; /* cardman IO-flags 0 */
136 unsigned char flags1; /* cardman IO-flags 1 */
137 unsigned int mdelay; /* variable monitor speeds, in jiffies */
139 unsigned int baudv; /* baud value for speed */
141 unsigned char proto; /* T=0, T=1, ... */
142 unsigned long flags; /* lock+flags (MONITOR,IO,ATR) * for concurrent
145 unsigned char pts[4];
147 struct timer_list timer; /* used to keep monitor running */
151 #define ZERO_DEV(dev) \
152 memset(&dev->atr_csum,0, \
153 sizeof(struct cm4000_dev) - \
154 offsetof(struct cm4000_dev, atr_csum))
156 static struct pcmcia_device *dev_table[CM4000_MAX_DEV];
157 static struct class *cmm_class;
159 /* This table doesn't use spaces after the comma between fields and thus
160 * violates CodingStyle. However, I don't really think wrapping it around will
161 * make it any clearer to read -HW */
162 static unsigned char fi_di_table[10][14] = {
163 /*FI 00 01 02 03 04 05 06 07 08 09 10 11 12 13 */
165 /* 0 */ {0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11},
166 /* 1 */ {0x01,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x91,0x11,0x11,0x11,0x11},
167 /* 2 */ {0x02,0x12,0x22,0x32,0x11,0x11,0x11,0x11,0x11,0x92,0xA2,0xB2,0x11,0x11},
168 /* 3 */ {0x03,0x13,0x23,0x33,0x43,0x53,0x63,0x11,0x11,0x93,0xA3,0xB3,0xC3,0xD3},
169 /* 4 */ {0x04,0x14,0x24,0x34,0x44,0x54,0x64,0x11,0x11,0x94,0xA4,0xB4,0xC4,0xD4},
170 /* 5 */ {0x00,0x15,0x25,0x35,0x45,0x55,0x65,0x11,0x11,0x95,0xA5,0xB5,0xC5,0xD5},
171 /* 6 */ {0x06,0x16,0x26,0x36,0x46,0x56,0x66,0x11,0x11,0x96,0xA6,0xB6,0xC6,0xD6},
172 /* 7 */ {0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11},
173 /* 8 */ {0x08,0x11,0x28,0x38,0x48,0x58,0x68,0x11,0x11,0x98,0xA8,0xB8,0xC8,0xD8},
174 /* 9 */ {0x09,0x19,0x29,0x39,0x49,0x59,0x69,0x11,0x11,0x99,0xA9,0xB9,0xC9,0xD9}
181 static inline void xoutb(unsigned char val, unsigned short port)
184 printk(KERN_DEBUG "outb(val=%.2x,port=%.4x)\n", val, port);
187 static inline unsigned char xinb(unsigned short port)
193 printk(KERN_DEBUG "%.2x=inb(%.4x)\n", val, port);
199 static inline unsigned char invert_revert(unsigned char ch)
204 static void str_invert_revert(unsigned char *b, int len)
208 for (i = 0; i < len; i++)
209 b[i] = invert_revert(b[i]);
212 #define ATRLENCK(dev,pos) \
213 if (pos>=dev->atr_len || pos>=MAX_ATR) \
216 static unsigned int calc_baudv(unsigned char fidi)
218 unsigned int wcrcf, wbrcf, fi_rfu, di_rfu;
224 switch ((fidi >> 4) & 0x0F) {
273 switch (fidi & 0x0F) {
309 return (wcrcf / wbrcf);
312 static unsigned short io_read_num_rec_bytes(unsigned int iobase,
320 tmp = inb(REG_NUM_BYTES(iobase)) |
321 (inb(REG_FLAGS0(iobase)) & 4 ? 0x100 : 0);
327 static int parse_atr(struct cm4000_dev *dev)
329 unsigned char any_t1, any_t0;
330 unsigned char ch, ifno;
333 DEBUGP(3, dev, "-> parse_atr: dev->atr_len = %i\n", dev->atr_len);
335 if (dev->atr_len < 3) {
336 DEBUGP(5, dev, "parse_atr: atr_len < 3\n");
340 if (dev->atr[0] == 0x3f)
341 set_bit(IS_INVREV, &dev->flags);
343 clear_bit(IS_INVREV, &dev->flags);
347 dev->proto = 0; /* XXX PROTO */
348 any_t1 = any_t0 = done = 0;
349 dev->ta1 = 0x11; /* defaults to 9600 baud */
351 if (ifno == 1 && (ch & 0x10)) {
352 /* read first interface byte and TA1 is present */
353 dev->ta1 = dev->atr[2];
354 DEBUGP(5, dev, "Card says FiDi is 0x%.2x\n", dev->ta1);
356 } else if ((ifno == 2) && (ch & 0x10)) { /* TA(2) */
361 DEBUGP(5, dev, "Yi=%.2x\n", ch & 0xf0);
362 ix += ((ch & 0x10) >> 4) /* no of int.face chars */
365 + ((ch & 0x80) >> 7);
366 /* ATRLENCK(dev,ix); */
367 if (ch & 0x80) { /* TDi */
371 DEBUGP(5, dev, "card is capable of T=1\n");
374 DEBUGP(5, dev, "card is capable of T=0\n");
380 DEBUGP(5, dev, "ix=%d noHist=%d any_t1=%d\n",
381 ix, dev->atr[1] & 15, any_t1);
382 if (ix + 1 + (dev->atr[1] & 0x0f) + any_t1 != dev->atr_len) {
383 DEBUGP(5, dev, "length error\n");
387 set_bit(IS_ANY_T0, &dev->flags);
389 if (any_t1) { /* compute csum */
392 for (i = 1; i < dev->atr_len; i++)
393 dev->atr_csum ^= dev->atr[i];
395 set_bit(IS_BAD_CSUM, &dev->flags);
396 DEBUGP(5, dev, "bad checksum\n");
401 dev->proto = 1; /* XXX PROTO */
402 set_bit(IS_ANY_T1, &dev->flags);
414 static struct card_fixup card_fixups[] = {
416 .atr = { 0x3b, 0xb3, 0x11, 0x00, 0x00, 0x41, 0x01 },
421 .atr = {0x3b, 0x76, 0x13, 0x00, 0x00, 0x80, 0x62, 0x07,
428 static void set_cardparameter(struct cm4000_dev *dev)
431 unsigned int iobase = dev->p_dev->io.BasePort1;
432 u_int8_t stopbits = 0x02; /* ISO default */
434 DEBUGP(3, dev, "-> set_cardparameter\n");
436 dev->flags1 = dev->flags1 | (((dev->baudv - 1) & 0x0100) >> 8);
437 xoutb(dev->flags1, REG_FLAGS1(iobase));
438 DEBUGP(5, dev, "flags1 = 0x%02x\n", dev->flags1);
441 xoutb((unsigned char)((dev->baudv - 1) & 0xFF), REG_BAUDRATE(iobase));
443 DEBUGP(5, dev, "baudv = %i -> write 0x%02x\n", dev->baudv,
444 ((dev->baudv - 1) & 0xFF));
447 for (i = 0; i < ARRAY_SIZE(card_fixups); i++) {
448 if (!memcmp(dev->atr, card_fixups[i].atr,
449 card_fixups[i].atr_len))
450 stopbits = card_fixups[i].stopbits;
452 xoutb(stopbits, REG_STOPBITS(iobase));
454 DEBUGP(3, dev, "<- set_cardparameter\n");
457 static int set_protocol(struct cm4000_dev *dev, struct ptsreq *ptsreq)
460 unsigned long tmp, i;
461 unsigned short num_bytes_read;
462 unsigned char pts_reply[4];
464 unsigned int iobase = dev->p_dev->io.BasePort1;
468 DEBUGP(3, dev, "-> set_protocol\n");
469 DEBUGP(5, dev, "ptsreq->Protocol = 0x%.8x, ptsreq->Flags=0x%.8x, "
470 "ptsreq->pts1=0x%.2x, ptsreq->pts2=0x%.2x, "
471 "ptsreq->pts3=0x%.2x\n", (unsigned int)ptsreq->protocol,
472 (unsigned int)ptsreq->flags, ptsreq->pts1, ptsreq->pts2,
475 /* Fill PTS structure */
478 tmp = ptsreq->protocol;
479 while ((tmp = (tmp >> 1)) > 0)
481 dev->proto = dev->pts[1]; /* Set new protocol */
482 dev->pts[1] = (0x01 << 4) | (dev->pts[1]);
484 /* Correct Fi/Di according to CM4000 Fi/Di table */
485 DEBUGP(5, dev, "Ta(1) from ATR is 0x%.2x\n", dev->ta1);
486 /* set Fi/Di according to ATR TA(1) */
487 dev->pts[2] = fi_di_table[dev->ta1 & 0x0F][(dev->ta1 >> 4) & 0x0F];
489 /* Calculate PCK character */
490 dev->pts[3] = dev->pts[0] ^ dev->pts[1] ^ dev->pts[2];
492 DEBUGP(5, dev, "pts0=%.2x, pts1=%.2x, pts2=%.2x, pts3=%.2x\n",
493 dev->pts[0], dev->pts[1], dev->pts[2], dev->pts[3]);
495 /* check card convention */
496 if (test_bit(IS_INVREV, &dev->flags))
497 str_invert_revert(dev->pts, 4);
500 xoutb(0x80, REG_FLAGS0(iobase));
502 /* Enable access to the message buffer */
503 DEBUGP(5, dev, "Enable access to the messages buffer\n");
504 dev->flags1 = 0x20 /* T_Active */
505 | (test_bit(IS_INVREV, &dev->flags) ? 0x02 : 0x00) /* inv parity */
506 | ((dev->baudv >> 8) & 0x01); /* MSB-baud */
507 xoutb(dev->flags1, REG_FLAGS1(iobase));
509 DEBUGP(5, dev, "Enable message buffer -> flags1 = 0x%.2x\n",
512 /* write challenge to the buffer */
513 DEBUGP(5, dev, "Write challenge to buffer: ");
514 for (i = 0; i < 4; i++) {
515 xoutb(i, REG_BUF_ADDR(iobase));
516 xoutb(dev->pts[i], REG_BUF_DATA(iobase)); /* buf data */
519 printk("0x%.2x ", dev->pts[i]);
527 /* set number of bytes to write */
528 DEBUGP(5, dev, "Set number of bytes to write\n");
529 xoutb(0x04, REG_NUM_SEND(iobase));
531 /* Trigger CARDMAN CONTROLLER */
532 xoutb(0x50, REG_FLAGS0(iobase));
534 /* Monitor progress */
535 /* wait for xmit done */
536 DEBUGP(5, dev, "Waiting for NumRecBytes getting valid\n");
538 for (i = 0; i < 100; i++) {
539 if (inb(REG_FLAGS0(iobase)) & 0x08) {
540 DEBUGP(5, dev, "NumRecBytes is valid\n");
546 DEBUGP(5, dev, "Timeout waiting for NumRecBytes getting "
549 goto exit_setprotocol;
552 DEBUGP(5, dev, "Reading NumRecBytes\n");
553 for (i = 0; i < 100; i++) {
554 io_read_num_rec_bytes(iobase, &num_bytes_read);
555 if (num_bytes_read >= 4) {
556 DEBUGP(2, dev, "NumRecBytes = %i\n", num_bytes_read);
562 /* check whether it is a short PTS reply? */
563 if (num_bytes_read == 3)
567 DEBUGP(5, dev, "Timeout reading num_bytes_read\n");
569 goto exit_setprotocol;
572 DEBUGP(5, dev, "Reset the CARDMAN CONTROLLER\n");
573 xoutb(0x80, REG_FLAGS0(iobase));
576 DEBUGP(5, dev, "Read PPS reply\n");
577 for (i = 0; i < num_bytes_read; i++) {
578 xoutb(i, REG_BUF_ADDR(iobase));
579 pts_reply[i] = inb(REG_BUF_DATA(iobase));
583 DEBUGP(2, dev, "PTSreply: ");
584 for (i = 0; i < num_bytes_read; i++) {
586 printk("0x%.2x ", pts_reply[i]);
589 #endif /* PCMCIA_DEBUG */
591 DEBUGP(5, dev, "Clear Tactive in Flags1\n");
592 xoutb(0x20, REG_FLAGS1(iobase));
594 /* Compare ptsreq and ptsreply */
595 if ((dev->pts[0] == pts_reply[0]) &&
596 (dev->pts[1] == pts_reply[1]) &&
597 (dev->pts[2] == pts_reply[2]) && (dev->pts[3] == pts_reply[3])) {
598 /* setcardparameter according to PPS */
599 dev->baudv = calc_baudv(dev->pts[2]);
600 set_cardparameter(dev);
601 } else if ((dev->pts[0] == pts_reply[0]) &&
602 ((dev->pts[1] & 0xef) == pts_reply[1]) &&
603 ((pts_reply[0] ^ pts_reply[1]) == pts_reply[2])) {
604 /* short PTS reply, set card parameter to default values */
605 dev->baudv = calc_baudv(0x11);
606 set_cardparameter(dev);
611 DEBUGP(3, dev, "<- set_protocol\n");
615 static int io_detect_cm4000(unsigned int iobase, struct cm4000_dev *dev)
618 /* note: statemachine is assumed to be reset */
619 if (inb(REG_FLAGS0(iobase)) & 8) {
620 clear_bit(IS_ATR_VALID, &dev->flags);
621 set_bit(IS_CMM_ABSENT, &dev->flags);
622 return 0; /* detect CMM = 1 -> failure */
624 /* xoutb(0x40, REG_FLAGS1(iobase)); detectCMM */
625 xoutb(dev->flags1 | 0x40, REG_FLAGS1(iobase));
626 if ((inb(REG_FLAGS0(iobase)) & 8) == 0) {
627 clear_bit(IS_ATR_VALID, &dev->flags);
628 set_bit(IS_CMM_ABSENT, &dev->flags);
629 return 0; /* detect CMM=0 -> failure */
631 /* clear detectCMM again by restoring original flags1 */
632 xoutb(dev->flags1, REG_FLAGS1(iobase));
636 static void terminate_monitor(struct cm4000_dev *dev)
639 /* tell the monitor to stop and wait until
642 DEBUGP(3, dev, "-> terminate_monitor\n");
643 wait_event_interruptible(dev->devq,
644 test_and_set_bit(LOCK_MONITOR,
645 (void *)&dev->flags));
647 /* now, LOCK_MONITOR has been set.
648 * allow a last cycle in the monitor.
649 * the monitor will indicate that it has
650 * finished by clearing this bit.
652 DEBUGP(5, dev, "Now allow last cycle of monitor!\n");
653 while (test_bit(LOCK_MONITOR, (void *)&dev->flags))
656 DEBUGP(5, dev, "Delete timer\n");
657 del_timer_sync(&dev->timer);
659 dev->monitor_running = 0;
662 DEBUGP(3, dev, "<- terminate_monitor\n");
666 * monitor the card every 50msec. as a side-effect, retrieve the
667 * atr once a card is inserted. another side-effect of retrieving the
668 * atr is that the card will be powered on, so there is no need to
669 * power on the card explictely from the application: the driver
670 * is already doing that for you.
673 static void monitor_card(unsigned long p)
675 struct cm4000_dev *dev = (struct cm4000_dev *) p;
676 unsigned int iobase = dev->p_dev->io.BasePort1;
678 struct ptsreq ptsreq;
681 DEBUGP(7, dev, "-> monitor_card\n");
683 /* if someone has set the lock for us: we're done! */
684 if (test_and_set_bit(LOCK_MONITOR, &dev->flags)) {
685 DEBUGP(4, dev, "About to stop monitor\n");
689 dev->atr_csum = dev->atr_len_retry = dev->cwarn = 0;
690 dev->mstate = M_FETCH_ATR;
691 clear_bit(LOCK_MONITOR, &dev->flags);
692 /* close et al. are sleeping on devq, so wake it */
693 wake_up_interruptible(&dev->devq);
694 DEBUGP(2, dev, "<- monitor_card (we are done now)\n");
698 /* try to lock io: if it is already locked, just add another timer */
699 if (test_and_set_bit(LOCK_IO, (void *)&dev->flags)) {
700 DEBUGP(4, dev, "Couldn't get IO lock\n");
701 goto return_with_timer;
704 /* is a card/a reader inserted at all ? */
705 dev->flags0 = xinb(REG_FLAGS0(iobase));
706 DEBUGP(7, dev, "dev->flags0 = 0x%2x\n", dev->flags0);
707 DEBUGP(7, dev, "smartcard present: %s\n",
708 dev->flags0 & 1 ? "yes" : "no");
709 DEBUGP(7, dev, "cardman present: %s\n",
710 dev->flags0 == 0xff ? "no" : "yes");
712 if ((dev->flags0 & 1) == 0 /* no smartcard inserted */
713 || dev->flags0 == 0xff) { /* no cardman inserted */
717 dev->atr_csum = dev->atr_len_retry = dev->cwarn = 0;
718 dev->mstate = M_FETCH_ATR;
720 dev->flags &= 0x000000ff; /* only keep IO and MONITOR locks */
722 if (dev->flags0 == 0xff) {
723 DEBUGP(4, dev, "set IS_CMM_ABSENT bit\n");
724 set_bit(IS_CMM_ABSENT, &dev->flags);
725 } else if (test_bit(IS_CMM_ABSENT, &dev->flags)) {
726 DEBUGP(4, dev, "clear IS_CMM_ABSENT bit "
727 "(card is removed)\n");
728 clear_bit(IS_CMM_ABSENT, &dev->flags);
732 } else if ((dev->flags0 & 1) && test_bit(IS_CMM_ABSENT, &dev->flags)) {
733 /* cardman and card present but cardman was absent before
734 * (after suspend with inserted card) */
735 DEBUGP(4, dev, "clear IS_CMM_ABSENT bit (card is inserted)\n");
736 clear_bit(IS_CMM_ABSENT, &dev->flags);
739 if (test_bit(IS_ATR_VALID, &dev->flags) == 1) {
740 DEBUGP(7, dev, "believe ATR is already valid (do nothing)\n");
744 switch (dev->mstate) {
745 unsigned char flags0;
747 DEBUGP(4, dev, "M_CARDOFF\n");
748 flags0 = inb(REG_FLAGS0(iobase));
750 /* wait until Flags0 indicate power is off */
751 dev->mdelay = T_10MSEC;
753 /* Flags0 indicate power off and no card inserted now;
754 * Reset CARDMAN CONTROLLER */
755 xoutb(0x80, REG_FLAGS0(iobase));
757 /* prepare for fetching ATR again: after card off ATR
758 * is read again automatically */
762 dev->atr_len_retry = dev->cwarn = 0;
763 dev->mstate = M_FETCH_ATR;
765 /* minimal gap between CARDOFF and read ATR is 50msec */
766 dev->mdelay = T_50MSEC;
770 DEBUGP(4, dev, "M_FETCH_ATR\n");
771 xoutb(0x80, REG_FLAGS0(iobase));
772 DEBUGP(4, dev, "Reset BAUDV to 9600\n");
773 dev->baudv = 0x173; /* 9600 */
774 xoutb(0x02, REG_STOPBITS(iobase)); /* stopbits=2 */
775 xoutb(0x73, REG_BAUDRATE(iobase)); /* baud value */
776 xoutb(0x21, REG_FLAGS1(iobase)); /* T_Active=1, baud
778 /* warm start vs. power on: */
779 xoutb(dev->flags0 & 2 ? 0x46 : 0x44, REG_FLAGS0(iobase));
780 dev->mdelay = T_40MSEC;
781 dev->mstate = M_TIMEOUT_WAIT;
784 DEBUGP(4, dev, "M_TIMEOUT_WAIT\n");
786 io_read_num_rec_bytes(iobase, &dev->atr_len);
787 dev->mdelay = T_10MSEC;
788 dev->mstate = M_READ_ATR_LEN;
791 DEBUGP(4, dev, "M_READ_ATR_LEN\n");
792 /* infinite loop possible, since there is no timeout */
794 #define MAX_ATR_LEN_RETRY 100
796 if (dev->atr_len == io_read_num_rec_bytes(iobase, &s)) {
797 if (dev->atr_len_retry++ >= MAX_ATR_LEN_RETRY) { /* + XX msec */
798 dev->mdelay = T_10MSEC;
799 dev->mstate = M_READ_ATR;
803 dev->atr_len_retry = 0; /* set new timeout */
806 DEBUGP(4, dev, "Current ATR_LEN = %i\n", dev->atr_len);
809 DEBUGP(4, dev, "M_READ_ATR\n");
810 xoutb(0x80, REG_FLAGS0(iobase)); /* reset SM */
811 for (i = 0; i < dev->atr_len; i++) {
812 xoutb(i, REG_BUF_ADDR(iobase));
813 dev->atr[i] = inb(REG_BUF_DATA(iobase));
815 /* Deactivate T_Active flags */
816 DEBUGP(4, dev, "Deactivate T_Active flags\n");
818 xoutb(dev->flags1, REG_FLAGS1(iobase));
820 /* atr is present (which doesnt mean it's valid) */
821 set_bit(IS_ATR_PRESENT, &dev->flags);
822 if (dev->atr[0] == 0x03)
823 str_invert_revert(dev->atr, dev->atr_len);
824 atrc = parse_atr(dev);
825 if (atrc == 0) { /* atr invalid */
827 dev->mstate = M_BAD_CARD;
829 dev->mdelay = T_50MSEC;
830 dev->mstate = M_ATR_PRESENT;
831 set_bit(IS_ATR_VALID, &dev->flags);
834 if (test_bit(IS_ATR_VALID, &dev->flags) == 1) {
835 DEBUGP(4, dev, "monitor_card: ATR valid\n");
836 /* if ta1 == 0x11, no PPS necessary (default values) */
837 /* do not do PPS with multi protocol cards */
838 if ((test_bit(IS_AUTOPPS_ACT, &dev->flags) == 0) &&
839 (dev->ta1 != 0x11) &&
840 !(test_bit(IS_ANY_T0, &dev->flags) &&
841 test_bit(IS_ANY_T1, &dev->flags))) {
842 DEBUGP(4, dev, "Perform AUTOPPS\n");
843 set_bit(IS_AUTOPPS_ACT, &dev->flags);
844 ptsreq.protocol = ptsreq.protocol =
845 (0x01 << dev->proto);
850 if (set_protocol(dev, &ptsreq) == 0) {
851 DEBUGP(4, dev, "AUTOPPS ret SUCC\n");
852 clear_bit(IS_AUTOPPS_ACT, &dev->flags);
853 wake_up_interruptible(&dev->atrq);
855 DEBUGP(4, dev, "AUTOPPS failed: "
856 "repower using defaults\n");
857 /* prepare for repowering */
858 clear_bit(IS_ATR_PRESENT, &dev->flags);
859 clear_bit(IS_ATR_VALID, &dev->flags);
863 dev->atr_len_retry = dev->cwarn = 0;
864 dev->mstate = M_FETCH_ATR;
866 dev->mdelay = T_50MSEC;
869 /* for cards which use slightly different
870 * params (extra guard time) */
871 set_cardparameter(dev);
872 if (test_bit(IS_AUTOPPS_ACT, &dev->flags) == 1)
873 DEBUGP(4, dev, "AUTOPPS already active "
874 "2nd try:use default values\n");
875 if (dev->ta1 == 0x11)
876 DEBUGP(4, dev, "No AUTOPPS necessary "
878 if (test_bit(IS_ANY_T0, &dev->flags)
879 && test_bit(IS_ANY_T1, &dev->flags))
880 DEBUGP(4, dev, "Do NOT perform AUTOPPS "
881 "with multiprotocol cards\n");
882 clear_bit(IS_AUTOPPS_ACT, &dev->flags);
883 wake_up_interruptible(&dev->atrq);
886 DEBUGP(4, dev, "ATR invalid\n");
887 wake_up_interruptible(&dev->atrq);
891 DEBUGP(4, dev, "M_BAD_CARD\n");
892 /* slow down warning, but prompt immediately after insertion */
893 if (dev->cwarn == 0 || dev->cwarn == 10) {
894 set_bit(IS_BAD_CARD, &dev->flags);
895 printk(KERN_WARNING MODULE_NAME ": device %s: ",
897 if (test_bit(IS_BAD_CSUM, &dev->flags)) {
898 DEBUGP(4, dev, "ATR checksum (0x%.2x, should "
899 "be zero) failed\n", dev->atr_csum);
902 else if (test_bit(IS_BAD_LENGTH, &dev->flags)) {
903 DEBUGP(4, dev, "ATR length error\n");
905 DEBUGP(4, dev, "card damaged or wrong way "
910 wake_up_interruptible(&dev->atrq); /* wake open */
913 dev->mdelay = T_100MSEC;
914 dev->mstate = M_FETCH_ATR;
917 DEBUGP(7, dev, "Unknown action\n");
922 DEBUGP(7, dev, "release_io\n");
923 clear_bit(LOCK_IO, &dev->flags);
924 wake_up_interruptible(&dev->ioq); /* whoever needs IO */
927 DEBUGP(7, dev, "<- monitor_card (returns with timer)\n");
928 mod_timer(&dev->timer, jiffies + dev->mdelay);
929 clear_bit(LOCK_MONITOR, &dev->flags);
932 /* Interface to userland (file_operations) */
934 static ssize_t cmm_read(struct file *filp, __user char *buf, size_t count,
937 struct cm4000_dev *dev = filp->private_data;
938 unsigned int iobase = dev->p_dev->io.BasePort1;
942 DEBUGP(2, dev, "-> cmm_read(%s,%d)\n", current->comm, current->pid);
944 if (count == 0) /* according to manpage */
947 if (!pcmcia_dev_present(dev->p_dev) || /* device removed */
948 test_bit(IS_CMM_ABSENT, &dev->flags))
951 if (test_bit(IS_BAD_CSUM, &dev->flags))
954 /* also see the note about this in cmm_write */
955 if (wait_event_interruptible
957 ((filp->f_flags & O_NONBLOCK)
958 || (test_bit(IS_ATR_PRESENT, (void *)&dev->flags) != 0)))) {
959 if (filp->f_flags & O_NONBLOCK)
964 if (test_bit(IS_ATR_VALID, &dev->flags) == 0)
967 /* this one implements blocking IO */
968 if (wait_event_interruptible
970 ((filp->f_flags & O_NONBLOCK) || (dev->rpos < dev->rlen)))) {
971 if (filp->f_flags & O_NONBLOCK)
977 if (wait_event_interruptible
979 ((filp->f_flags & O_NONBLOCK)
980 || (test_and_set_bit(LOCK_IO, (void *)&dev->flags) == 0)))) {
981 if (filp->f_flags & O_NONBLOCK)
987 dev->flags0 = inb(REG_FLAGS0(iobase));
988 if ((dev->flags0 & 1) == 0 /* no smartcard inserted */
989 || dev->flags0 == 0xff) { /* no cardman inserted */
990 clear_bit(IS_ATR_VALID, &dev->flags);
991 if (dev->flags0 & 1) {
992 set_bit(IS_CMM_ABSENT, &dev->flags);
999 DEBUGP(4, dev, "begin read answer\n");
1000 j = min(count, (size_t)(dev->rlen - dev->rpos));
1004 DEBUGP(4, dev, "read1 j=%d\n", j);
1005 for (i = 0; i < j; i++) {
1006 xoutb(k++, REG_BUF_ADDR(iobase));
1007 dev->rbuf[i] = xinb(REG_BUF_DATA(iobase));
1009 j = min(count, (size_t)(dev->rlen - dev->rpos));
1011 DEBUGP(4, dev, "read2 j=%d\n", j);
1012 dev->flags1 |= 0x10; /* MSB buf addr set */
1013 xoutb(dev->flags1, REG_FLAGS1(iobase));
1014 for (; i < j; i++) {
1015 xoutb(k++, REG_BUF_ADDR(iobase));
1016 dev->rbuf[i] = xinb(REG_BUF_DATA(iobase));
1020 if (dev->proto == 0 && count > dev->rlen - dev->rpos) {
1021 DEBUGP(4, dev, "T=0 and count > buffer\n");
1022 dev->rbuf[i] = dev->rbuf[i - 1];
1023 dev->rbuf[i - 1] = dev->procbyte;
1028 dev->rpos = dev->rlen + 1;
1030 /* Clear T1Active */
1031 DEBUGP(4, dev, "Clear T1Active\n");
1032 dev->flags1 &= 0xdf;
1033 xoutb(dev->flags1, REG_FLAGS1(iobase));
1035 xoutb(0, REG_FLAGS1(iobase)); /* clear detectCMM */
1036 /* last check before exit */
1037 if (!io_detect_cm4000(iobase, dev))
1040 if (test_bit(IS_INVREV, &dev->flags) && count > 0)
1041 str_invert_revert(dev->rbuf, count);
1043 if (copy_to_user(buf, dev->rbuf, count))
1047 clear_bit(LOCK_IO, &dev->flags);
1048 wake_up_interruptible(&dev->ioq);
1050 DEBUGP(2, dev, "<- cmm_read returns: rc = %Zi\n",
1051 (rc < 0 ? rc : count));
1052 return rc < 0 ? rc : count;
1055 static ssize_t cmm_write(struct file *filp, const char __user *buf,
1056 size_t count, loff_t *ppos)
1058 struct cm4000_dev *dev = (struct cm4000_dev *) filp->private_data;
1059 unsigned int iobase = dev->p_dev->io.BasePort1;
1062 unsigned char infolen;
1063 unsigned char sendT0;
1064 unsigned short nsend;
1069 DEBUGP(2, dev, "-> cmm_write(%s,%d)\n", current->comm, current->pid);
1071 if (count == 0) /* according to manpage */
1074 if (dev->proto == 0 && count < 4) {
1075 /* T0 must have at least 4 bytes */
1076 DEBUGP(4, dev, "T0 short write\n");
1080 nr = count & 0x1ff; /* max bytes to write */
1082 sendT0 = dev->proto ? 0 : nr > 5 ? 0x08 : 0;
1084 if (!pcmcia_dev_present(dev->p_dev) || /* device removed */
1085 test_bit(IS_CMM_ABSENT, &dev->flags))
1088 if (test_bit(IS_BAD_CSUM, &dev->flags)) {
1089 DEBUGP(4, dev, "bad csum\n");
1094 * wait for atr to become valid.
1095 * note: it is important to lock this code. if we dont, the monitor
1096 * could be run between test_bit and the call to sleep on the
1097 * atr-queue. if *then* the monitor detects atr valid, it will wake up
1098 * any process on the atr-queue, *but* since we have been interrupted,
1099 * we do not yet sleep on this queue. this would result in a missed
1100 * wake_up and the calling process would sleep forever (until
1101 * interrupted). also, do *not* restore_flags before sleep_on, because
1102 * this could result in the same situation!
1104 if (wait_event_interruptible
1106 ((filp->f_flags & O_NONBLOCK)
1107 || (test_bit(IS_ATR_PRESENT, (void *)&dev->flags) != 0)))) {
1108 if (filp->f_flags & O_NONBLOCK)
1110 return -ERESTARTSYS;
1113 if (test_bit(IS_ATR_VALID, &dev->flags) == 0) { /* invalid atr */
1114 DEBUGP(4, dev, "invalid ATR\n");
1119 if (wait_event_interruptible
1121 ((filp->f_flags & O_NONBLOCK)
1122 || (test_and_set_bit(LOCK_IO, (void *)&dev->flags) == 0)))) {
1123 if (filp->f_flags & O_NONBLOCK)
1125 return -ERESTARTSYS;
1128 if (copy_from_user(dev->sbuf, buf, ((count > 512) ? 512 : count)))
1132 dev->flags0 = inb(REG_FLAGS0(iobase));
1133 if ((dev->flags0 & 1) == 0 /* no smartcard inserted */
1134 || dev->flags0 == 0xff) { /* no cardman inserted */
1135 clear_bit(IS_ATR_VALID, &dev->flags);
1136 if (dev->flags0 & 1) {
1137 set_bit(IS_CMM_ABSENT, &dev->flags);
1140 DEBUGP(4, dev, "IO error\n");
1146 xoutb(0x80, REG_FLAGS0(iobase)); /* reset SM */
1148 if (!io_detect_cm4000(iobase, dev)) {
1153 /* reflect T=0 send/read mode in flags1 */
1154 dev->flags1 |= (sendT0);
1156 set_cardparameter(dev);
1158 /* dummy read, reset flag procedure received */
1159 tmp = inb(REG_FLAGS1(iobase));
1161 dev->flags1 = 0x20 /* T_Active */
1163 | (test_bit(IS_INVREV, &dev->flags) ? 2 : 0)/* inverse parity */
1164 | (((dev->baudv - 1) & 0x0100) >> 8); /* MSB-Baud */
1165 DEBUGP(1, dev, "set dev->flags1 = 0x%.2x\n", dev->flags1);
1166 xoutb(dev->flags1, REG_FLAGS1(iobase));
1169 DEBUGP(4, dev, "Xmit data\n");
1170 for (i = 0; i < nr; i++) {
1172 dev->flags1 = 0x20 /* T_Active */
1173 | (sendT0) /* SendT0 */
1174 /* inverse parity: */
1175 | (test_bit(IS_INVREV, &dev->flags) ? 2 : 0)
1176 | (((dev->baudv - 1) & 0x0100) >> 8) /* MSB-Baud */
1177 | 0x10; /* set address high */
1178 DEBUGP(4, dev, "dev->flags = 0x%.2x - set address "
1179 "high\n", dev->flags1);
1180 xoutb(dev->flags1, REG_FLAGS1(iobase));
1182 if (test_bit(IS_INVREV, &dev->flags)) {
1183 DEBUGP(4, dev, "Apply inverse convention for 0x%.2x "
1184 "-> 0x%.2x\n", (unsigned char)dev->sbuf[i],
1185 invert_revert(dev->sbuf[i]));
1186 xoutb(i, REG_BUF_ADDR(iobase));
1187 xoutb(invert_revert(dev->sbuf[i]),
1188 REG_BUF_DATA(iobase));
1190 xoutb(i, REG_BUF_ADDR(iobase));
1191 xoutb(dev->sbuf[i], REG_BUF_DATA(iobase));
1194 DEBUGP(4, dev, "Xmit done\n");
1196 if (dev->proto == 0) {
1197 /* T=0 proto: 0 byte reply */
1199 DEBUGP(4, dev, "T=0 assumes 0 byte reply\n");
1200 xoutb(i, REG_BUF_ADDR(iobase));
1201 if (test_bit(IS_INVREV, &dev->flags))
1202 xoutb(0xff, REG_BUF_DATA(iobase));
1204 xoutb(0x00, REG_BUF_DATA(iobase));
1214 nsend = 5 + (unsigned char)dev->sbuf[4];
1215 if (dev->sbuf[4] == 0)
1222 /* T0: output procedure byte */
1223 if (test_bit(IS_INVREV, &dev->flags)) {
1224 DEBUGP(4, dev, "T=0 set Procedure byte (inverse-reverse) "
1225 "0x%.2x\n", invert_revert(dev->sbuf[1]));
1226 xoutb(invert_revert(dev->sbuf[1]), REG_NUM_BYTES(iobase));
1228 DEBUGP(4, dev, "T=0 set Procedure byte 0x%.2x\n", dev->sbuf[1]);
1229 xoutb(dev->sbuf[1], REG_NUM_BYTES(iobase));
1232 DEBUGP(1, dev, "set NumSendBytes = 0x%.2x\n",
1233 (unsigned char)(nsend & 0xff));
1234 xoutb((unsigned char)(nsend & 0xff), REG_NUM_SEND(iobase));
1236 DEBUGP(1, dev, "Trigger CARDMAN CONTROLLER (0x%.2x)\n",
1237 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 xoutb(0x40 /* SM_Active */
1242 | (dev->flags0 & 2 ? 0 : 4) /* power on if needed */
1243 |(dev->proto ? 0x10 : 0x08) /* T=1/T=0 */
1244 |(nsend & 0x100) >> 8, /* MSB numSendBytes */
1245 REG_FLAGS0(iobase));
1247 /* wait for xmit done */
1248 if (dev->proto == 1) {
1249 DEBUGP(4, dev, "Wait for xmit done\n");
1250 for (i = 0; i < 1000; i++) {
1251 if (inb(REG_FLAGS0(iobase)) & 0x08)
1253 msleep_interruptible(10);
1256 DEBUGP(4, dev, "timeout waiting for xmit done\n");
1262 /* T=1: wait for infoLen */
1266 /* wait until infoLen is valid */
1267 for (i = 0; i < 6000; i++) { /* max waiting time of 1 min */
1268 io_read_num_rec_bytes(iobase, &s);
1270 infolen = inb(REG_FLAGS1(iobase));
1271 DEBUGP(4, dev, "infolen=%d\n", infolen);
1274 msleep_interruptible(10);
1277 DEBUGP(4, dev, "timeout waiting for infoLen\n");
1282 clear_bit(IS_PROCBYTE_PRESENT, &dev->flags);
1284 /* numRecBytes | bit9 of numRecytes */
1285 io_read_num_rec_bytes(iobase, &dev->rlen);
1286 for (i = 0; i < 600; i++) { /* max waiting time of 2 sec */
1288 if (dev->rlen >= infolen + 4)
1291 msleep_interruptible(10);
1292 /* numRecBytes | bit9 of numRecytes */
1293 io_read_num_rec_bytes(iobase, &s);
1294 if (s > dev->rlen) {
1295 DEBUGP(1, dev, "NumRecBytes inc (reset timeout)\n");
1296 i = 0; /* reset timeout */
1299 /* T=0: we are done when numRecBytes doesn't
1300 * increment any more and NoProcedureByte
1301 * is set and numRecBytes == bytes sent + 6
1302 * (header bytes + data + 1 for sw2)
1303 * except when the card replies an error
1304 * which means, no data will be sent back.
1306 else if (dev->proto == 0) {
1307 if ((inb(REG_BUF_ADDR(iobase)) & 0x80)) {
1308 /* no procedure byte received since last read */
1309 DEBUGP(1, dev, "NoProcedure byte set\n");
1312 /* procedure byte received since last read */
1313 DEBUGP(1, dev, "NoProcedure byte unset "
1314 "(reset timeout)\n");
1315 dev->procbyte = inb(REG_FLAGS1(iobase));
1316 DEBUGP(1, dev, "Read procedure byte 0x%.2x\n",
1318 i = 0; /* resettimeout */
1320 if (inb(REG_FLAGS0(iobase)) & 0x08) {
1321 DEBUGP(1, dev, "T0Done flag (read reply)\n");
1326 infolen = inb(REG_FLAGS1(iobase));
1329 DEBUGP(1, dev, "timeout waiting for numRecBytes\n");
1333 if (dev->proto == 0) {
1334 DEBUGP(1, dev, "Wait for T0Done bit to be set\n");
1335 for (i = 0; i < 1000; i++) {
1336 if (inb(REG_FLAGS0(iobase)) & 0x08)
1338 msleep_interruptible(10);
1341 DEBUGP(1, dev, "timeout waiting for T0Done\n");
1346 dev->procbyte = inb(REG_FLAGS1(iobase));
1347 DEBUGP(4, dev, "Read procedure byte 0x%.2x\n",
1350 io_read_num_rec_bytes(iobase, &dev->rlen);
1351 DEBUGP(4, dev, "Read NumRecBytes = %i\n", dev->rlen);
1355 /* T=1: read offset=zero, T=0: read offset=after challenge */
1356 dev->rpos = dev->proto ? 0 : nr == 4 ? 5 : nr > dev->rlen ? 5 : nr;
1357 DEBUGP(4, dev, "dev->rlen = %i, dev->rpos = %i, nr = %i\n",
1358 dev->rlen, dev->rpos, nr);
1361 DEBUGP(4, dev, "Reset SM\n");
1362 xoutb(0x80, REG_FLAGS0(iobase)); /* reset SM */
1365 DEBUGP(4, dev, "Write failed but clear T_Active\n");
1366 dev->flags1 &= 0xdf;
1367 xoutb(dev->flags1, REG_FLAGS1(iobase));
1370 clear_bit(LOCK_IO, &dev->flags);
1371 wake_up_interruptible(&dev->ioq);
1372 wake_up_interruptible(&dev->readq); /* tell read we have data */
1374 /* ITSEC E2: clear write buffer */
1375 memset((char *)dev->sbuf, 0, 512);
1377 /* return error or actually written bytes */
1378 DEBUGP(2, dev, "<- cmm_write\n");
1379 return rc < 0 ? rc : nr;
1382 static void start_monitor(struct cm4000_dev *dev)
1384 DEBUGP(3, dev, "-> start_monitor\n");
1385 if (!dev->monitor_running) {
1386 DEBUGP(5, dev, "create, init and add timer\n");
1387 setup_timer(&dev->timer, monitor_card, (unsigned long)dev);
1388 dev->monitor_running = 1;
1389 mod_timer(&dev->timer, jiffies);
1391 DEBUGP(5, dev, "monitor already running\n");
1392 DEBUGP(3, dev, "<- start_monitor\n");
1395 static void stop_monitor(struct cm4000_dev *dev)
1397 DEBUGP(3, dev, "-> stop_monitor\n");
1398 if (dev->monitor_running) {
1399 DEBUGP(5, dev, "stopping monitor\n");
1400 terminate_monitor(dev);
1401 /* reset monitor SM */
1402 clear_bit(IS_ATR_VALID, &dev->flags);
1403 clear_bit(IS_ATR_PRESENT, &dev->flags);
1405 DEBUGP(5, dev, "monitor already stopped\n");
1406 DEBUGP(3, dev, "<- stop_monitor\n");
1409 static long cmm_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1411 struct cm4000_dev *dev = filp->private_data;
1412 unsigned int iobase = dev->p_dev->io.BasePort1;
1413 struct inode *inode = filp->f_path.dentry->d_inode;
1414 struct pcmcia_device *link;
1417 void __user *argp = (void __user *)arg;
1419 char *ioctl_names[CM_IOC_MAXNR + 1] = {
1420 [_IOC_NR(CM_IOCGSTATUS)] "CM_IOCGSTATUS",
1421 [_IOC_NR(CM_IOCGATR)] "CM_IOCGATR",
1422 [_IOC_NR(CM_IOCARDOFF)] "CM_IOCARDOFF",
1423 [_IOC_NR(CM_IOCSPTS)] "CM_IOCSPTS",
1424 [_IOC_NR(CM_IOSDBGLVL)] "CM4000_DBGLVL",
1427 DEBUGP(3, dev, "cmm_ioctl(device=%d.%d) %s\n", imajor(inode),
1428 iminor(inode), ioctl_names[_IOC_NR(cmd)]);
1432 link = dev_table[iminor(inode)];
1433 if (!pcmcia_dev_present(link)) {
1434 DEBUGP(4, dev, "DEV_OK false\n");
1438 if (test_bit(IS_CMM_ABSENT, &dev->flags)) {
1439 DEBUGP(4, dev, "CMM_ABSENT flag set\n");
1444 if (_IOC_TYPE(cmd) != CM_IOC_MAGIC) {
1445 DEBUGP(4, dev, "ioctype mismatch\n");
1448 if (_IOC_NR(cmd) > CM_IOC_MAXNR) {
1449 DEBUGP(4, dev, "iocnr mismatch\n");
1452 size = _IOC_SIZE(cmd);
1454 DEBUGP(4, dev, "iocdir=%.4x iocr=%.4x iocw=%.4x iocsize=%d cmd=%.4x\n",
1455 _IOC_DIR(cmd), _IOC_READ, _IOC_WRITE, size, cmd);
1457 if (_IOC_DIR(cmd) & _IOC_READ) {
1458 if (!access_ok(VERIFY_WRITE, argp, size))
1461 if (_IOC_DIR(cmd) & _IOC_WRITE) {
1462 if (!access_ok(VERIFY_READ, argp, size))
1469 DEBUGP(4, dev, " ... in CM_IOCGSTATUS\n");
1473 /* clear other bits, but leave inserted & powered as
1475 status = dev->flags0 & 3;
1476 if (test_bit(IS_ATR_PRESENT, &dev->flags))
1477 status |= CM_ATR_PRESENT;
1478 if (test_bit(IS_ATR_VALID, &dev->flags))
1479 status |= CM_ATR_VALID;
1480 if (test_bit(IS_CMM_ABSENT, &dev->flags))
1481 status |= CM_NO_READER;
1482 if (test_bit(IS_BAD_CARD, &dev->flags))
1483 status |= CM_BAD_CARD;
1484 if (copy_to_user(argp, &status, sizeof(int)))
1489 DEBUGP(4, dev, "... in CM_IOCGATR\n");
1491 struct atreq __user *atreq = argp;
1493 /* allow nonblocking io and being interrupted */
1494 if (wait_event_interruptible
1496 ((filp->f_flags & O_NONBLOCK)
1497 || (test_bit(IS_ATR_PRESENT, (void *)&dev->flags)
1499 if (filp->f_flags & O_NONBLOCK)
1507 if (test_bit(IS_ATR_VALID, &dev->flags) == 0) {
1509 if (copy_to_user(&(atreq->atr_len), &tmp,
1513 if (copy_to_user(atreq->atr, dev->atr,
1518 if (copy_to_user(&(atreq->atr_len), &tmp, sizeof(int)))
1527 DEBUGP(4, dev, "... in CM_IOCARDOFF\n");
1528 if (dev->flags0 & 0x01) {
1529 DEBUGP(4, dev, " Card inserted\n");
1531 DEBUGP(2, dev, " No card inserted\n");
1533 if (dev->flags0 & 0x02) {
1534 DEBUGP(4, dev, " Card powered\n");
1536 DEBUGP(2, dev, " Card not powered\n");
1540 /* is a card inserted and powered? */
1541 if ((dev->flags0 & 0x01) && (dev->flags0 & 0x02)) {
1544 if (wait_event_interruptible
1546 ((filp->f_flags & O_NONBLOCK)
1547 || (test_and_set_bit(LOCK_IO, (void *)&dev->flags)
1549 if (filp->f_flags & O_NONBLOCK)
1555 /* Set Flags0 = 0x42 */
1556 DEBUGP(4, dev, "Set Flags0=0x42 \n");
1557 xoutb(0x42, REG_FLAGS0(iobase));
1558 clear_bit(IS_ATR_PRESENT, &dev->flags);
1559 clear_bit(IS_ATR_VALID, &dev->flags);
1560 dev->mstate = M_CARDOFF;
1561 clear_bit(LOCK_IO, &dev->flags);
1562 if (wait_event_interruptible
1564 ((filp->f_flags & O_NONBLOCK)
1565 || (test_bit(IS_ATR_VALID, (void *)&dev->flags) !=
1567 if (filp->f_flags & O_NONBLOCK)
1575 clear_bit(LOCK_IO, &dev->flags);
1576 wake_up_interruptible(&dev->ioq);
1581 struct ptsreq krnptsreq;
1583 if (copy_from_user(&krnptsreq, argp,
1584 sizeof(struct ptsreq))) {
1590 DEBUGP(4, dev, "... in CM_IOCSPTS\n");
1591 /* wait for ATR to get valid */
1592 if (wait_event_interruptible
1594 ((filp->f_flags & O_NONBLOCK)
1595 || (test_bit(IS_ATR_PRESENT, (void *)&dev->flags)
1597 if (filp->f_flags & O_NONBLOCK)
1604 if (wait_event_interruptible
1606 ((filp->f_flags & O_NONBLOCK)
1607 || (test_and_set_bit(LOCK_IO, (void *)&dev->flags)
1609 if (filp->f_flags & O_NONBLOCK)
1616 if ((rc = set_protocol(dev, &krnptsreq)) != 0) {
1617 /* auto power_on again */
1618 dev->mstate = M_FETCH_ATR;
1619 clear_bit(IS_ATR_VALID, &dev->flags);
1622 clear_bit(LOCK_IO, &dev->flags);
1623 wake_up_interruptible(&dev->ioq);
1628 case CM_IOSDBGLVL: /* set debug log level */
1630 int old_pc_debug = 0;
1632 old_pc_debug = pc_debug;
1633 if (copy_from_user(&pc_debug, argp, sizeof(int)))
1635 else if (old_pc_debug != pc_debug)
1636 DEBUGP(0, dev, "Changed debug log level "
1637 "to %i\n", pc_debug);
1642 DEBUGP(4, dev, "... in default (unknown IOCTL code)\n");
1650 static int cmm_open(struct inode *inode, struct file *filp)
1652 struct cm4000_dev *dev;
1653 struct pcmcia_device *link;
1654 int minor = iminor(inode);
1657 if (minor >= CM4000_MAX_DEV)
1661 link = dev_table[minor];
1662 if (link == NULL || !pcmcia_dev_present(link)) {
1673 filp->private_data = dev;
1675 DEBUGP(2, dev, "-> cmm_open(device=%d.%d process=%s,%d)\n",
1676 imajor(inode), minor, current->comm, current->pid);
1678 /* init device variables, they may be "polluted" after close
1679 * or, the device may never have been closed (i.e. open failed)
1684 /* opening will always block since the
1685 * monitor will be started by open, which
1686 * means we have to wait for ATR becoming
1687 * vaild = block until valid (or card
1690 if (filp->f_flags & O_NONBLOCK) {
1695 dev->mdelay = T_50MSEC;
1697 /* start monitoring the cardstatus */
1700 link->open = 1; /* only one open per device */
1702 DEBUGP(2, dev, "<- cmm_open\n");
1703 ret = nonseekable_open(inode, filp);
1709 static int cmm_close(struct inode *inode, struct file *filp)
1711 struct cm4000_dev *dev;
1712 struct pcmcia_device *link;
1713 int minor = iminor(inode);
1715 if (minor >= CM4000_MAX_DEV)
1718 link = dev_table[minor];
1724 DEBUGP(2, dev, "-> cmm_close(maj/min=%d.%d)\n",
1725 imajor(inode), minor);
1731 link->open = 0; /* only one open per device */
1732 wake_up(&dev->devq); /* socket removed? */
1734 DEBUGP(2, dev, "cmm_close\n");
1738 static void cmm_cm4000_release(struct pcmcia_device * link)
1740 struct cm4000_dev *dev = link->priv;
1742 /* dont terminate the monitor, rather rely on
1743 * close doing that for us.
1745 DEBUGP(3, dev, "-> cmm_cm4000_release\n");
1746 while (link->open) {
1747 printk(KERN_INFO MODULE_NAME ": delaying release until "
1748 "process has terminated\n");
1749 /* note: don't interrupt us:
1750 * close the applications which own
1751 * the devices _first_ !
1753 wait_event(dev->devq, (link->open == 0));
1755 /* dev->devq=NULL; this cannot be zeroed earlier */
1756 DEBUGP(3, dev, "<- cmm_cm4000_release\n");
1760 /*==== Interface to PCMCIA Layer =======================================*/
1762 static int cm4000_config(struct pcmcia_device * link, int devno)
1764 struct cm4000_dev *dev;
1768 int fail_fn, fail_rc;
1771 /* read the config-tuples */
1772 tuple.Attributes = 0;
1773 tuple.TupleData = buf;
1774 tuple.TupleDataMax = sizeof(buf);
1775 tuple.TupleOffset = 0;
1777 link->io.BasePort2 = 0;
1778 link->io.NumPorts2 = 0;
1779 link->io.Attributes2 = 0;
1780 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
1781 for (rc = pcmcia_get_first_tuple(link, &tuple);
1782 rc == CS_SUCCESS; rc = pcmcia_get_next_tuple(link, &tuple)) {
1784 rc = pcmcia_get_tuple_data(link, &tuple);
1785 if (rc != CS_SUCCESS)
1787 rc = pcmcia_parse_tuple(link, &tuple, &parse);
1788 if (rc != CS_SUCCESS)
1791 link->conf.ConfigIndex = parse.cftable_entry.index;
1793 if (!parse.cftable_entry.io.nwin)
1796 /* Get the IOaddr */
1797 link->io.BasePort1 = parse.cftable_entry.io.win[0].base;
1798 link->io.NumPorts1 = parse.cftable_entry.io.win[0].len;
1799 link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
1800 if (!(parse.cftable_entry.io.flags & CISTPL_IO_8BIT))
1801 link->io.Attributes1 = IO_DATA_PATH_WIDTH_16;
1802 if (!(parse.cftable_entry.io.flags & CISTPL_IO_16BIT))
1803 link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
1804 link->io.IOAddrLines = parse.cftable_entry.io.flags
1805 & CISTPL_IO_LINES_MASK;
1807 rc = pcmcia_request_io(link, &link->io);
1808 if (rc == CS_SUCCESS)
1809 break; /* we are done */
1811 if (rc != CS_SUCCESS)
1814 link->conf.IntType = 00000002;
1817 pcmcia_request_configuration(link, &link->conf)) != CS_SUCCESS) {
1818 fail_fn = RequestConfiguration;
1823 sprintf(dev->node.dev_name, DEVICE_NAME "%d", devno);
1824 dev->node.major = major;
1825 dev->node.minor = devno;
1826 dev->node.next = NULL;
1827 link->dev_node = &dev->node;
1832 cm4000_release(link);
1836 static int cm4000_suspend(struct pcmcia_device *link)
1838 struct cm4000_dev *dev;
1846 static int cm4000_resume(struct pcmcia_device *link)
1848 struct cm4000_dev *dev;
1857 static void cm4000_release(struct pcmcia_device *link)
1859 cmm_cm4000_release(link); /* delay release until device closed */
1860 pcmcia_disable_device(link);
1863 static int cm4000_probe(struct pcmcia_device *link)
1865 struct cm4000_dev *dev;
1868 for (i = 0; i < CM4000_MAX_DEV; i++)
1869 if (dev_table[i] == NULL)
1872 if (i == CM4000_MAX_DEV) {
1873 printk(KERN_NOTICE MODULE_NAME ": all devices in use\n");
1877 /* create a new cm4000_cs device */
1878 dev = kzalloc(sizeof(struct cm4000_dev), GFP_KERNEL);
1884 link->conf.IntType = INT_MEMORY_AND_IO;
1885 dev_table[i] = link;
1887 init_waitqueue_head(&dev->devq);
1888 init_waitqueue_head(&dev->ioq);
1889 init_waitqueue_head(&dev->atrq);
1890 init_waitqueue_head(&dev->readq);
1892 ret = cm4000_config(link, i);
1894 dev_table[i] = NULL;
1899 device_create(cmm_class, NULL, MKDEV(major, i), "cmm%d", i);
1904 static void cm4000_detach(struct pcmcia_device *link)
1906 struct cm4000_dev *dev = link->priv;
1910 for (devno = 0; devno < CM4000_MAX_DEV; devno++)
1911 if (dev_table[devno] == link)
1913 if (devno == CM4000_MAX_DEV)
1918 cm4000_release(link);
1920 dev_table[devno] = NULL;
1923 device_destroy(cmm_class, MKDEV(major, devno));
1928 static const struct file_operations cm4000_fops = {
1929 .owner = THIS_MODULE,
1932 .unlocked_ioctl = cmm_ioctl,
1934 .release= cmm_close,
1937 static struct pcmcia_device_id cm4000_ids[] = {
1938 PCMCIA_DEVICE_MANF_CARD(0x0223, 0x0002),
1939 PCMCIA_DEVICE_PROD_ID12("CardMan", "4000", 0x2FB368CA, 0xA2BD8C39),
1942 MODULE_DEVICE_TABLE(pcmcia, cm4000_ids);
1944 static struct pcmcia_driver cm4000_driver = {
1945 .owner = THIS_MODULE,
1947 .name = "cm4000_cs",
1949 .probe = cm4000_probe,
1950 .remove = cm4000_detach,
1951 .suspend = cm4000_suspend,
1952 .resume = cm4000_resume,
1953 .id_table = cm4000_ids,
1956 static int __init cmm_init(void)
1960 printk(KERN_INFO "%s\n", version);
1962 cmm_class = class_create(THIS_MODULE, "cardman_4000");
1963 if (IS_ERR(cmm_class))
1964 return PTR_ERR(cmm_class);
1966 major = register_chrdev(0, DEVICE_NAME, &cm4000_fops);
1968 printk(KERN_WARNING MODULE_NAME
1969 ": could not get major number\n");
1970 class_destroy(cmm_class);
1974 rc = pcmcia_register_driver(&cm4000_driver);
1976 unregister_chrdev(major, DEVICE_NAME);
1977 class_destroy(cmm_class);
1984 static void __exit cmm_exit(void)
1986 printk(KERN_INFO MODULE_NAME ": unloading\n");
1987 pcmcia_unregister_driver(&cm4000_driver);
1988 unregister_chrdev(major, DEVICE_NAME);
1989 class_destroy(cmm_class);
1992 module_init(cmm_init);
1993 module_exit(cmm_exit);
1994 MODULE_LICENSE("Dual BSD/GPL");