Back to home page

OSCL-LXR

 
 

    


0001  /*
0002   * A driver for the PCMCIA Smartcard Reader "Omnikey CardMan Mobile 4000"
0003   *
0004   * cm4000_cs.c support.linux@omnikey.com
0005   *
0006   * Tue Oct 23 11:32:43 GMT 2001 herp - cleaned up header files
0007   * Sun Jan 20 10:11:15 MET 2002 herp - added modversion header files
0008   * Thu Nov 14 16:34:11 GMT 2002 mh   - added PPS functionality
0009   * Tue Nov 19 16:36:27 GMT 2002 mh   - added SUSPEND/RESUME functionailty
0010   * Wed Jul 28 12:55:01 CEST 2004 mh  - kernel 2.6 adjustments
0011   *
0012   * current version: 2.4.0gm4
0013   *
0014   * (C) 2000,2001,2002,2003,2004 Omnikey AG
0015   *
0016   * (C) 2005-2006 Harald Welte <laforge@gnumonks.org>
0017   *     - Adhere to Kernel process/coding-style.rst
0018   *     - Port to 2.6.13 "new" style PCMCIA
0019   *     - Check for copy_{from,to}_user return values
0020   *     - Use nonseekable_open()
0021   *     - add class interface for udev device creation
0022   *
0023   * All rights reserved. Licensed under dual BSD/GPL license.
0024   */
0025 
0026 #include <linux/kernel.h>
0027 #include <linux/module.h>
0028 #include <linux/slab.h>
0029 #include <linux/init.h>
0030 #include <linux/fs.h>
0031 #include <linux/delay.h>
0032 #include <linux/bitrev.h>
0033 #include <linux/mutex.h>
0034 #include <linux/uaccess.h>
0035 #include <linux/io.h>
0036 
0037 #include <pcmcia/cistpl.h>
0038 #include <pcmcia/cisreg.h>
0039 #include <pcmcia/ciscode.h>
0040 #include <pcmcia/ds.h>
0041 
0042 #include <linux/cm4000_cs.h>
0043 
0044 /* #define ATR_CSUM */
0045 
0046 #define reader_to_dev(x)    (&x->p_dev->dev)
0047 
0048 /* n (debug level) is ignored */
0049 /* additional debug output may be enabled by re-compiling with
0050  * CM4000_DEBUG set */
0051 /* #define CM4000_DEBUG */
0052 #define DEBUGP(n, rdr, x, args...) do {         \
0053         dev_dbg(reader_to_dev(rdr), "%s:" x,    \
0054                __func__ , ## args);     \
0055     } while (0)
0056 
0057 static DEFINE_MUTEX(cmm_mutex);
0058 
0059 #define T_1SEC      (HZ)
0060 #define T_10MSEC    msecs_to_jiffies(10)
0061 #define T_20MSEC    msecs_to_jiffies(20)
0062 #define T_40MSEC    msecs_to_jiffies(40)
0063 #define T_50MSEC    msecs_to_jiffies(50)
0064 #define T_100MSEC   msecs_to_jiffies(100)
0065 #define T_500MSEC   msecs_to_jiffies(500)
0066 
0067 static void cm4000_release(struct pcmcia_device *link);
0068 
0069 static int major;       /* major number we get from the kernel */
0070 
0071 /* note: the first state has to have number 0 always */
0072 
0073 #define M_FETCH_ATR 0
0074 #define M_TIMEOUT_WAIT  1
0075 #define M_READ_ATR_LEN  2
0076 #define M_READ_ATR  3
0077 #define M_ATR_PRESENT   4
0078 #define M_BAD_CARD  5
0079 #define M_CARDOFF   6
0080 
0081 #define LOCK_IO         0
0082 #define LOCK_MONITOR        1
0083 
0084 #define IS_AUTOPPS_ACT       6
0085 #define IS_PROCBYTE_PRESENT  7
0086 #define IS_INVREV        8
0087 #define IS_ANY_T0        9
0088 #define IS_ANY_T1       10
0089 #define IS_ATR_PRESENT      11
0090 #define IS_ATR_VALID        12
0091 #define IS_CMM_ABSENT       13
0092 #define IS_BAD_LENGTH       14
0093 #define IS_BAD_CSUM     15
0094 #define IS_BAD_CARD     16
0095 
0096 #define REG_FLAGS0(x)       (x + 0)
0097 #define REG_FLAGS1(x)       (x + 1)
0098 #define REG_NUM_BYTES(x)    (x + 2)
0099 #define REG_BUF_ADDR(x)     (x + 3)
0100 #define REG_BUF_DATA(x)     (x + 4)
0101 #define REG_NUM_SEND(x)     (x + 5)
0102 #define REG_BAUDRATE(x)     (x + 6)
0103 #define REG_STOPBITS(x)     (x + 7)
0104 
0105 struct cm4000_dev {
0106     struct pcmcia_device *p_dev;
0107 
0108     unsigned char atr[MAX_ATR];
0109     unsigned char rbuf[512];
0110     unsigned char sbuf[512];
0111 
0112     wait_queue_head_t devq;     /* when removing cardman must not be
0113                        zeroed! */
0114 
0115     wait_queue_head_t ioq;      /* if IO is locked, wait on this Q */
0116     wait_queue_head_t atrq;     /* wait for ATR valid */
0117     wait_queue_head_t readq;    /* used by write to wake blk.read */
0118 
0119     /* warning: do not move this struct group.
0120      * initialising to zero depends on it - see ZERO_DEV below.  */
0121     struct_group(init,
0122     unsigned char atr_csum;
0123     unsigned char atr_len_retry;
0124     unsigned short atr_len;
0125     unsigned short rlen;    /* bytes avail. after write */
0126     unsigned short rpos;    /* latest read pos. write zeroes */
0127     unsigned char procbyte; /* T=0 procedure byte */
0128     unsigned char mstate;   /* state of card monitor */
0129     unsigned char cwarn;    /* slow down warning */
0130     unsigned char flags0;   /* cardman IO-flags 0 */
0131     unsigned char flags1;   /* cardman IO-flags 1 */
0132     unsigned int mdelay;    /* variable monitor speeds, in jiffies */
0133 
0134     unsigned int baudv; /* baud value for speed */
0135     unsigned char ta1;
0136     unsigned char proto;    /* T=0, T=1, ... */
0137     unsigned long flags;    /* lock+flags (MONITOR,IO,ATR) * for concurrent
0138                    access */
0139 
0140     unsigned char pts[4];
0141 
0142     struct timer_list timer;    /* used to keep monitor running */
0143     int monitor_running;
0144     );
0145 };
0146 
0147 #define ZERO_DEV(dev)   memset(&((dev)->init), 0, sizeof((dev)->init))
0148 
0149 static struct pcmcia_device *dev_table[CM4000_MAX_DEV];
0150 static struct class *cmm_class;
0151 
0152 /* This table doesn't use spaces after the comma between fields and thus
0153  * violates process/coding-style.rst.  However, I don't really think wrapping it around will
0154  * make it any clearer to read -HW */
0155 static unsigned char fi_di_table[10][14] = {
0156 /*FI     00   01   02   03   04   05   06   07   08   09   10   11   12   13 */
0157 /*DI */
0158 /* 0 */ {0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11},
0159 /* 1 */ {0x01,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x91,0x11,0x11,0x11,0x11},
0160 /* 2 */ {0x02,0x12,0x22,0x32,0x11,0x11,0x11,0x11,0x11,0x92,0xA2,0xB2,0x11,0x11},
0161 /* 3 */ {0x03,0x13,0x23,0x33,0x43,0x53,0x63,0x11,0x11,0x93,0xA3,0xB3,0xC3,0xD3},
0162 /* 4 */ {0x04,0x14,0x24,0x34,0x44,0x54,0x64,0x11,0x11,0x94,0xA4,0xB4,0xC4,0xD4},
0163 /* 5 */ {0x00,0x15,0x25,0x35,0x45,0x55,0x65,0x11,0x11,0x95,0xA5,0xB5,0xC5,0xD5},
0164 /* 6 */ {0x06,0x16,0x26,0x36,0x46,0x56,0x66,0x11,0x11,0x96,0xA6,0xB6,0xC6,0xD6},
0165 /* 7 */ {0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11},
0166 /* 8 */ {0x08,0x11,0x28,0x38,0x48,0x58,0x68,0x11,0x11,0x98,0xA8,0xB8,0xC8,0xD8},
0167 /* 9 */ {0x09,0x19,0x29,0x39,0x49,0x59,0x69,0x11,0x11,0x99,0xA9,0xB9,0xC9,0xD9}
0168 };
0169 
0170 #ifndef CM4000_DEBUG
0171 #define xoutb   outb
0172 #define xinb    inb
0173 #else
0174 static inline void xoutb(unsigned char val, unsigned short port)
0175 {
0176     pr_debug("outb(val=%.2x,port=%.4x)\n", val, port);
0177     outb(val, port);
0178 }
0179 static inline unsigned char xinb(unsigned short port)
0180 {
0181     unsigned char val;
0182 
0183     val = inb(port);
0184     pr_debug("%.2x=inb(%.4x)\n", val, port);
0185 
0186     return val;
0187 }
0188 #endif
0189 
0190 static inline unsigned char invert_revert(unsigned char ch)
0191 {
0192     return bitrev8(~ch);
0193 }
0194 
0195 static void str_invert_revert(unsigned char *b, int len)
0196 {
0197     int i;
0198 
0199     for (i = 0; i < len; i++)
0200         b[i] = invert_revert(b[i]);
0201 }
0202 
0203 #define ATRLENCK(dev,pos) \
0204     if (pos>=dev->atr_len || pos>=MAX_ATR) \
0205         goto return_0;
0206 
0207 static unsigned int calc_baudv(unsigned char fidi)
0208 {
0209     unsigned int wcrcf, wbrcf, fi_rfu, di_rfu;
0210 
0211     fi_rfu = 372;
0212     di_rfu = 1;
0213 
0214     /* FI */
0215     switch ((fidi >> 4) & 0x0F) {
0216     case 0x00:
0217         wcrcf = 372;
0218         break;
0219     case 0x01:
0220         wcrcf = 372;
0221         break;
0222     case 0x02:
0223         wcrcf = 558;
0224         break;
0225     case 0x03:
0226         wcrcf = 744;
0227         break;
0228     case 0x04:
0229         wcrcf = 1116;
0230         break;
0231     case 0x05:
0232         wcrcf = 1488;
0233         break;
0234     case 0x06:
0235         wcrcf = 1860;
0236         break;
0237     case 0x07:
0238         wcrcf = fi_rfu;
0239         break;
0240     case 0x08:
0241         wcrcf = fi_rfu;
0242         break;
0243     case 0x09:
0244         wcrcf = 512;
0245         break;
0246     case 0x0A:
0247         wcrcf = 768;
0248         break;
0249     case 0x0B:
0250         wcrcf = 1024;
0251         break;
0252     case 0x0C:
0253         wcrcf = 1536;
0254         break;
0255     case 0x0D:
0256         wcrcf = 2048;
0257         break;
0258     default:
0259         wcrcf = fi_rfu;
0260         break;
0261     }
0262 
0263     /* DI */
0264     switch (fidi & 0x0F) {
0265     case 0x00:
0266         wbrcf = di_rfu;
0267         break;
0268     case 0x01:
0269         wbrcf = 1;
0270         break;
0271     case 0x02:
0272         wbrcf = 2;
0273         break;
0274     case 0x03:
0275         wbrcf = 4;
0276         break;
0277     case 0x04:
0278         wbrcf = 8;
0279         break;
0280     case 0x05:
0281         wbrcf = 16;
0282         break;
0283     case 0x06:
0284         wbrcf = 32;
0285         break;
0286     case 0x07:
0287         wbrcf = di_rfu;
0288         break;
0289     case 0x08:
0290         wbrcf = 12;
0291         break;
0292     case 0x09:
0293         wbrcf = 20;
0294         break;
0295     default:
0296         wbrcf = di_rfu;
0297         break;
0298     }
0299 
0300     return (wcrcf / wbrcf);
0301 }
0302 
0303 static unsigned short io_read_num_rec_bytes(unsigned int iobase,
0304                         unsigned short *s)
0305 {
0306     unsigned short tmp;
0307 
0308     tmp = *s = 0;
0309     do {
0310         *s = tmp;
0311         tmp = inb(REG_NUM_BYTES(iobase)) |
0312                 (inb(REG_FLAGS0(iobase)) & 4 ? 0x100 : 0);
0313     } while (tmp != *s);
0314 
0315     return *s;
0316 }
0317 
0318 static int parse_atr(struct cm4000_dev *dev)
0319 {
0320     unsigned char any_t1, any_t0;
0321     unsigned char ch, ifno;
0322     int ix, done;
0323 
0324     DEBUGP(3, dev, "-> parse_atr: dev->atr_len = %i\n", dev->atr_len);
0325 
0326     if (dev->atr_len < 3) {
0327         DEBUGP(5, dev, "parse_atr: atr_len < 3\n");
0328         return 0;
0329     }
0330 
0331     if (dev->atr[0] == 0x3f)
0332         set_bit(IS_INVREV, &dev->flags);
0333     else
0334         clear_bit(IS_INVREV, &dev->flags);
0335     ix = 1;
0336     ifno = 1;
0337     ch = dev->atr[1];
0338     dev->proto = 0;     /* XXX PROTO */
0339     any_t1 = any_t0 = done = 0;
0340     dev->ta1 = 0x11;    /* defaults to 9600 baud */
0341     do {
0342         if (ifno == 1 && (ch & 0x10)) {
0343             /* read first interface byte and TA1 is present */
0344             dev->ta1 = dev->atr[2];
0345             DEBUGP(5, dev, "Card says FiDi is 0x%.2x\n", dev->ta1);
0346             ifno++;
0347         } else if ((ifno == 2) && (ch & 0x10)) { /* TA(2) */
0348             dev->ta1 = 0x11;
0349             ifno++;
0350         }
0351 
0352         DEBUGP(5, dev, "Yi=%.2x\n", ch & 0xf0);
0353         ix += ((ch & 0x10) >> 4)    /* no of int.face chars */
0354             +((ch & 0x20) >> 5)
0355             + ((ch & 0x40) >> 6)
0356             + ((ch & 0x80) >> 7);
0357         /* ATRLENCK(dev,ix); */
0358         if (ch & 0x80) {    /* TDi */
0359             ch = dev->atr[ix];
0360             if ((ch & 0x0f)) {
0361                 any_t1 = 1;
0362                 DEBUGP(5, dev, "card is capable of T=1\n");
0363             } else {
0364                 any_t0 = 1;
0365                 DEBUGP(5, dev, "card is capable of T=0\n");
0366             }
0367         } else
0368             done = 1;
0369     } while (!done);
0370 
0371     DEBUGP(5, dev, "ix=%d noHist=%d any_t1=%d\n",
0372           ix, dev->atr[1] & 15, any_t1);
0373     if (ix + 1 + (dev->atr[1] & 0x0f) + any_t1 != dev->atr_len) {
0374         DEBUGP(5, dev, "length error\n");
0375         return 0;
0376     }
0377     if (any_t0)
0378         set_bit(IS_ANY_T0, &dev->flags);
0379 
0380     if (any_t1) {       /* compute csum */
0381         dev->atr_csum = 0;
0382 #ifdef ATR_CSUM
0383         for (i = 1; i < dev->atr_len; i++)
0384             dev->atr_csum ^= dev->atr[i];
0385         if (dev->atr_csum) {
0386             set_bit(IS_BAD_CSUM, &dev->flags);
0387             DEBUGP(5, dev, "bad checksum\n");
0388             goto return_0;
0389         }
0390 #endif
0391         if (any_t0 == 0)
0392             dev->proto = 1; /* XXX PROTO */
0393         set_bit(IS_ANY_T1, &dev->flags);
0394     }
0395 
0396     return 1;
0397 }
0398 
0399 struct card_fixup {
0400     char atr[12];
0401     u_int8_t atr_len;
0402     u_int8_t stopbits;
0403 };
0404 
0405 static struct card_fixup card_fixups[] = {
0406     {   /* ACOS */
0407         .atr = { 0x3b, 0xb3, 0x11, 0x00, 0x00, 0x41, 0x01 },
0408         .atr_len = 7,
0409         .stopbits = 0x03,
0410     },
0411     {   /* Motorola */
0412         .atr = {0x3b, 0x76, 0x13, 0x00, 0x00, 0x80, 0x62, 0x07,
0413             0x41, 0x81, 0x81 },
0414         .atr_len = 11,
0415         .stopbits = 0x04,
0416     },
0417 };
0418 
0419 static void set_cardparameter(struct cm4000_dev *dev)
0420 {
0421     int i;
0422     unsigned int iobase = dev->p_dev->resource[0]->start;
0423     u_int8_t stopbits = 0x02; /* ISO default */
0424 
0425     DEBUGP(3, dev, "-> set_cardparameter\n");
0426 
0427     dev->flags1 = dev->flags1 | (((dev->baudv - 1) & 0x0100) >> 8);
0428     xoutb(dev->flags1, REG_FLAGS1(iobase));
0429     DEBUGP(5, dev, "flags1 = 0x%02x\n", dev->flags1);
0430 
0431     /* set baudrate */
0432     xoutb((unsigned char)((dev->baudv - 1) & 0xFF), REG_BAUDRATE(iobase));
0433 
0434     DEBUGP(5, dev, "baudv = %i -> write 0x%02x\n", dev->baudv,
0435           ((dev->baudv - 1) & 0xFF));
0436 
0437     /* set stopbits */
0438     for (i = 0; i < ARRAY_SIZE(card_fixups); i++) {
0439         if (!memcmp(dev->atr, card_fixups[i].atr,
0440                 card_fixups[i].atr_len))
0441             stopbits = card_fixups[i].stopbits;
0442     }
0443     xoutb(stopbits, REG_STOPBITS(iobase));
0444 
0445     DEBUGP(3, dev, "<- set_cardparameter\n");
0446 }
0447 
0448 static int set_protocol(struct cm4000_dev *dev, struct ptsreq *ptsreq)
0449 {
0450 
0451     unsigned long tmp, i;
0452     unsigned short num_bytes_read;
0453     unsigned char pts_reply[4];
0454     ssize_t rc;
0455     unsigned int iobase = dev->p_dev->resource[0]->start;
0456 
0457     rc = 0;
0458 
0459     DEBUGP(3, dev, "-> set_protocol\n");
0460     DEBUGP(5, dev, "ptsreq->Protocol = 0x%.8x, ptsreq->Flags=0x%.8x, "
0461          "ptsreq->pts1=0x%.2x, ptsreq->pts2=0x%.2x, "
0462          "ptsreq->pts3=0x%.2x\n", (unsigned int)ptsreq->protocol,
0463          (unsigned int)ptsreq->flags, ptsreq->pts1, ptsreq->pts2,
0464          ptsreq->pts3);
0465 
0466     /* Fill PTS structure */
0467     dev->pts[0] = 0xff;
0468     dev->pts[1] = 0x00;
0469     tmp = ptsreq->protocol;
0470     while ((tmp = (tmp >> 1)) > 0)
0471         dev->pts[1]++;
0472     dev->proto = dev->pts[1];   /* Set new protocol */
0473     dev->pts[1] = (0x01 << 4) | (dev->pts[1]);
0474 
0475     /* Correct Fi/Di according to CM4000 Fi/Di table */
0476     DEBUGP(5, dev, "Ta(1) from ATR is 0x%.2x\n", dev->ta1);
0477     /* set Fi/Di according to ATR TA(1) */
0478     dev->pts[2] = fi_di_table[dev->ta1 & 0x0F][(dev->ta1 >> 4) & 0x0F];
0479 
0480     /* Calculate PCK character */
0481     dev->pts[3] = dev->pts[0] ^ dev->pts[1] ^ dev->pts[2];
0482 
0483     DEBUGP(5, dev, "pts0=%.2x, pts1=%.2x, pts2=%.2x, pts3=%.2x\n",
0484            dev->pts[0], dev->pts[1], dev->pts[2], dev->pts[3]);
0485 
0486     /* check card convention */
0487     if (test_bit(IS_INVREV, &dev->flags))
0488         str_invert_revert(dev->pts, 4);
0489 
0490     /* reset SM */
0491     xoutb(0x80, REG_FLAGS0(iobase));
0492 
0493     /* Enable access to the message buffer */
0494     DEBUGP(5, dev, "Enable access to the messages buffer\n");
0495     dev->flags1 = 0x20  /* T_Active */
0496         | (test_bit(IS_INVREV, &dev->flags) ? 0x02 : 0x00) /* inv parity */
0497         | ((dev->baudv >> 8) & 0x01);   /* MSB-baud */
0498     xoutb(dev->flags1, REG_FLAGS1(iobase));
0499 
0500     DEBUGP(5, dev, "Enable message buffer -> flags1 = 0x%.2x\n",
0501            dev->flags1);
0502 
0503     /* write challenge to the buffer */
0504     DEBUGP(5, dev, "Write challenge to buffer: ");
0505     for (i = 0; i < 4; i++) {
0506         xoutb(i, REG_BUF_ADDR(iobase));
0507         xoutb(dev->pts[i], REG_BUF_DATA(iobase));   /* buf data */
0508 #ifdef CM4000_DEBUG
0509         pr_debug("0x%.2x ", dev->pts[i]);
0510     }
0511     pr_debug("\n");
0512 #else
0513     }
0514 #endif
0515 
0516     /* set number of bytes to write */
0517     DEBUGP(5, dev, "Set number of bytes to write\n");
0518     xoutb(0x04, REG_NUM_SEND(iobase));
0519 
0520     /* Trigger CARDMAN CONTROLLER */
0521     xoutb(0x50, REG_FLAGS0(iobase));
0522 
0523     /* Monitor progress */
0524     /* wait for xmit done */
0525     DEBUGP(5, dev, "Waiting for NumRecBytes getting valid\n");
0526 
0527     for (i = 0; i < 100; i++) {
0528         if (inb(REG_FLAGS0(iobase)) & 0x08) {
0529             DEBUGP(5, dev, "NumRecBytes is valid\n");
0530             break;
0531         }
0532         usleep_range(10000, 11000);
0533     }
0534     if (i == 100) {
0535         DEBUGP(5, dev, "Timeout waiting for NumRecBytes getting "
0536                "valid\n");
0537         rc = -EIO;
0538         goto exit_setprotocol;
0539     }
0540 
0541     DEBUGP(5, dev, "Reading NumRecBytes\n");
0542     for (i = 0; i < 100; i++) {
0543         io_read_num_rec_bytes(iobase, &num_bytes_read);
0544         if (num_bytes_read >= 4) {
0545             DEBUGP(2, dev, "NumRecBytes = %i\n", num_bytes_read);
0546             if (num_bytes_read > 4) {
0547                 rc = -EIO;
0548                 goto exit_setprotocol;
0549             }
0550             break;
0551         }
0552         usleep_range(10000, 11000);
0553     }
0554 
0555     /* check whether it is a short PTS reply? */
0556     if (num_bytes_read == 3)
0557         i = 0;
0558 
0559     if (i == 100) {
0560         DEBUGP(5, dev, "Timeout reading num_bytes_read\n");
0561         rc = -EIO;
0562         goto exit_setprotocol;
0563     }
0564 
0565     DEBUGP(5, dev, "Reset the CARDMAN CONTROLLER\n");
0566     xoutb(0x80, REG_FLAGS0(iobase));
0567 
0568     /* Read PPS reply */
0569     DEBUGP(5, dev, "Read PPS reply\n");
0570     for (i = 0; i < num_bytes_read; i++) {
0571         xoutb(i, REG_BUF_ADDR(iobase));
0572         pts_reply[i] = inb(REG_BUF_DATA(iobase));
0573     }
0574 
0575 #ifdef CM4000_DEBUG
0576     DEBUGP(2, dev, "PTSreply: ");
0577     for (i = 0; i < num_bytes_read; i++) {
0578         pr_debug("0x%.2x ", pts_reply[i]);
0579     }
0580     pr_debug("\n");
0581 #endif  /* CM4000_DEBUG */
0582 
0583     DEBUGP(5, dev, "Clear Tactive in Flags1\n");
0584     xoutb(0x20, REG_FLAGS1(iobase));
0585 
0586     /* Compare ptsreq and ptsreply */
0587     if ((dev->pts[0] == pts_reply[0]) &&
0588         (dev->pts[1] == pts_reply[1]) &&
0589         (dev->pts[2] == pts_reply[2]) && (dev->pts[3] == pts_reply[3])) {
0590         /* setcardparameter according to PPS */
0591         dev->baudv = calc_baudv(dev->pts[2]);
0592         set_cardparameter(dev);
0593     } else if ((dev->pts[0] == pts_reply[0]) &&
0594            ((dev->pts[1] & 0xef) == pts_reply[1]) &&
0595            ((pts_reply[0] ^ pts_reply[1]) == pts_reply[2])) {
0596         /* short PTS reply, set card parameter to default values */
0597         dev->baudv = calc_baudv(0x11);
0598         set_cardparameter(dev);
0599     } else
0600         rc = -EIO;
0601 
0602 exit_setprotocol:
0603     DEBUGP(3, dev, "<- set_protocol\n");
0604     return rc;
0605 }
0606 
0607 static int io_detect_cm4000(unsigned int iobase, struct cm4000_dev *dev)
0608 {
0609 
0610     /* note: statemachine is assumed to be reset */
0611     if (inb(REG_FLAGS0(iobase)) & 8) {
0612         clear_bit(IS_ATR_VALID, &dev->flags);
0613         set_bit(IS_CMM_ABSENT, &dev->flags);
0614         return 0;   /* detect CMM = 1 -> failure */
0615     }
0616     /* xoutb(0x40, REG_FLAGS1(iobase)); detectCMM */
0617     xoutb(dev->flags1 | 0x40, REG_FLAGS1(iobase));
0618     if ((inb(REG_FLAGS0(iobase)) & 8) == 0) {
0619         clear_bit(IS_ATR_VALID, &dev->flags);
0620         set_bit(IS_CMM_ABSENT, &dev->flags);
0621         return 0;   /* detect CMM=0 -> failure */
0622     }
0623     /* clear detectCMM again by restoring original flags1 */
0624     xoutb(dev->flags1, REG_FLAGS1(iobase));
0625     return 1;
0626 }
0627 
0628 static void terminate_monitor(struct cm4000_dev *dev)
0629 {
0630 
0631     /* tell the monitor to stop and wait until
0632      * it terminates.
0633      */
0634     DEBUGP(3, dev, "-> terminate_monitor\n");
0635     wait_event_interruptible(dev->devq,
0636                  test_and_set_bit(LOCK_MONITOR,
0637                           (void *)&dev->flags));
0638 
0639     /* now, LOCK_MONITOR has been set.
0640      * allow a last cycle in the monitor.
0641      * the monitor will indicate that it has
0642      * finished by clearing this bit.
0643      */
0644     DEBUGP(5, dev, "Now allow last cycle of monitor!\n");
0645     while (test_bit(LOCK_MONITOR, (void *)&dev->flags))
0646         msleep(25);
0647 
0648     DEBUGP(5, dev, "Delete timer\n");
0649     del_timer_sync(&dev->timer);
0650 #ifdef CM4000_DEBUG
0651     dev->monitor_running = 0;
0652 #endif
0653 
0654     DEBUGP(3, dev, "<- terminate_monitor\n");
0655 }
0656 
0657 /*
0658  * monitor the card every 50msec. as a side-effect, retrieve the
0659  * atr once a card is inserted. another side-effect of retrieving the
0660  * atr is that the card will be powered on, so there is no need to
0661  * power on the card explicitly from the application: the driver
0662  * is already doing that for you.
0663  */
0664 
0665 static void monitor_card(struct timer_list *t)
0666 {
0667     struct cm4000_dev *dev = from_timer(dev, t, timer);
0668     unsigned int iobase = dev->p_dev->resource[0]->start;
0669     unsigned short s;
0670     struct ptsreq ptsreq;
0671     int i, atrc;
0672 
0673     DEBUGP(7, dev, "->  monitor_card\n");
0674 
0675     /* if someone has set the lock for us: we're done! */
0676     if (test_and_set_bit(LOCK_MONITOR, &dev->flags)) {
0677         DEBUGP(4, dev, "About to stop monitor\n");
0678         /* no */
0679         dev->rlen =
0680             dev->rpos =
0681             dev->atr_csum = dev->atr_len_retry = dev->cwarn = 0;
0682         dev->mstate = M_FETCH_ATR;
0683         clear_bit(LOCK_MONITOR, &dev->flags);
0684         /* close et al. are sleeping on devq, so wake it */
0685         wake_up_interruptible(&dev->devq);
0686         DEBUGP(2, dev, "<- monitor_card (we are done now)\n");
0687         return;
0688     }
0689 
0690     /* try to lock io: if it is already locked, just add another timer */
0691     if (test_and_set_bit(LOCK_IO, (void *)&dev->flags)) {
0692         DEBUGP(4, dev, "Couldn't get IO lock\n");
0693         goto return_with_timer;
0694     }
0695 
0696     /* is a card/a reader inserted at all ? */
0697     dev->flags0 = xinb(REG_FLAGS0(iobase));
0698     DEBUGP(7, dev, "dev->flags0 = 0x%2x\n", dev->flags0);
0699     DEBUGP(7, dev, "smartcard present: %s\n",
0700            dev->flags0 & 1 ? "yes" : "no");
0701     DEBUGP(7, dev, "cardman present: %s\n",
0702            dev->flags0 == 0xff ? "no" : "yes");
0703 
0704     if ((dev->flags0 & 1) == 0  /* no smartcard inserted */
0705         || dev->flags0 == 0xff) {   /* no cardman inserted */
0706         /* no */
0707         dev->rlen =
0708             dev->rpos =
0709             dev->atr_csum = dev->atr_len_retry = dev->cwarn = 0;
0710         dev->mstate = M_FETCH_ATR;
0711 
0712         dev->flags &= 0x000000ff; /* only keep IO and MONITOR locks */
0713 
0714         if (dev->flags0 == 0xff) {
0715             DEBUGP(4, dev, "set IS_CMM_ABSENT bit\n");
0716             set_bit(IS_CMM_ABSENT, &dev->flags);
0717         } else if (test_bit(IS_CMM_ABSENT, &dev->flags)) {
0718             DEBUGP(4, dev, "clear IS_CMM_ABSENT bit "
0719                    "(card is removed)\n");
0720             clear_bit(IS_CMM_ABSENT, &dev->flags);
0721         }
0722 
0723         goto release_io;
0724     } else if ((dev->flags0 & 1) && test_bit(IS_CMM_ABSENT, &dev->flags)) {
0725         /* cardman and card present but cardman was absent before
0726          * (after suspend with inserted card) */
0727         DEBUGP(4, dev, "clear IS_CMM_ABSENT bit (card is inserted)\n");
0728         clear_bit(IS_CMM_ABSENT, &dev->flags);
0729     }
0730 
0731     if (test_bit(IS_ATR_VALID, &dev->flags) == 1) {
0732         DEBUGP(7, dev, "believe ATR is already valid (do nothing)\n");
0733         goto release_io;
0734     }
0735 
0736     switch (dev->mstate) {
0737     case M_CARDOFF: {
0738         unsigned char flags0;
0739 
0740         DEBUGP(4, dev, "M_CARDOFF\n");
0741         flags0 = inb(REG_FLAGS0(iobase));
0742         if (flags0 & 0x02) {
0743             /* wait until Flags0 indicate power is off */
0744             dev->mdelay = T_10MSEC;
0745         } else {
0746             /* Flags0 indicate power off and no card inserted now;
0747              * Reset CARDMAN CONTROLLER */
0748             xoutb(0x80, REG_FLAGS0(iobase));
0749 
0750             /* prepare for fetching ATR again: after card off ATR
0751              * is read again automatically */
0752             dev->rlen =
0753                 dev->rpos =
0754                 dev->atr_csum =
0755                 dev->atr_len_retry = dev->cwarn = 0;
0756             dev->mstate = M_FETCH_ATR;
0757 
0758             /* minimal gap between CARDOFF and read ATR is 50msec */
0759             dev->mdelay = T_50MSEC;
0760         }
0761         break;
0762     }
0763     case M_FETCH_ATR:
0764         DEBUGP(4, dev, "M_FETCH_ATR\n");
0765         xoutb(0x80, REG_FLAGS0(iobase));
0766         DEBUGP(4, dev, "Reset BAUDV to 9600\n");
0767         dev->baudv = 0x173; /* 9600 */
0768         xoutb(0x02, REG_STOPBITS(iobase));  /* stopbits=2 */
0769         xoutb(0x73, REG_BAUDRATE(iobase));  /* baud value */
0770         xoutb(0x21, REG_FLAGS1(iobase));    /* T_Active=1, baud
0771                                value */
0772         /* warm start vs. power on: */
0773         xoutb(dev->flags0 & 2 ? 0x46 : 0x44, REG_FLAGS0(iobase));
0774         dev->mdelay = T_40MSEC;
0775         dev->mstate = M_TIMEOUT_WAIT;
0776         break;
0777     case M_TIMEOUT_WAIT:
0778         DEBUGP(4, dev, "M_TIMEOUT_WAIT\n");
0779         /* numRecBytes */
0780         io_read_num_rec_bytes(iobase, &dev->atr_len);
0781         dev->mdelay = T_10MSEC;
0782         dev->mstate = M_READ_ATR_LEN;
0783         break;
0784     case M_READ_ATR_LEN:
0785         DEBUGP(4, dev, "M_READ_ATR_LEN\n");
0786         /* infinite loop possible, since there is no timeout */
0787 
0788 #define MAX_ATR_LEN_RETRY   100
0789 
0790         if (dev->atr_len == io_read_num_rec_bytes(iobase, &s)) {
0791             if (dev->atr_len_retry++ >= MAX_ATR_LEN_RETRY) {                    /* + XX msec */
0792                 dev->mdelay = T_10MSEC;
0793                 dev->mstate = M_READ_ATR;
0794             }
0795         } else {
0796             dev->atr_len = s;
0797             dev->atr_len_retry = 0; /* set new timeout */
0798         }
0799 
0800         DEBUGP(4, dev, "Current ATR_LEN = %i\n", dev->atr_len);
0801         break;
0802     case M_READ_ATR:
0803         DEBUGP(4, dev, "M_READ_ATR\n");
0804         xoutb(0x80, REG_FLAGS0(iobase));    /* reset SM */
0805         for (i = 0; i < dev->atr_len; i++) {
0806             xoutb(i, REG_BUF_ADDR(iobase));
0807             dev->atr[i] = inb(REG_BUF_DATA(iobase));
0808         }
0809         /* Deactivate T_Active flags */
0810         DEBUGP(4, dev, "Deactivate T_Active flags\n");
0811         dev->flags1 = 0x01;
0812         xoutb(dev->flags1, REG_FLAGS1(iobase));
0813 
0814         /* atr is present (which doesn't mean it's valid) */
0815         set_bit(IS_ATR_PRESENT, &dev->flags);
0816         if (dev->atr[0] == 0x03)
0817             str_invert_revert(dev->atr, dev->atr_len);
0818         atrc = parse_atr(dev);
0819         if (atrc == 0) {    /* atr invalid */
0820             dev->mdelay = 0;
0821             dev->mstate = M_BAD_CARD;
0822         } else {
0823             dev->mdelay = T_50MSEC;
0824             dev->mstate = M_ATR_PRESENT;
0825             set_bit(IS_ATR_VALID, &dev->flags);
0826         }
0827 
0828         if (test_bit(IS_ATR_VALID, &dev->flags) == 1) {
0829             DEBUGP(4, dev, "monitor_card: ATR valid\n");
0830             /* if ta1 == 0x11, no PPS necessary (default values) */
0831             /* do not do PPS with multi protocol cards */
0832             if ((test_bit(IS_AUTOPPS_ACT, &dev->flags) == 0) &&
0833                 (dev->ta1 != 0x11) &&
0834                 !(test_bit(IS_ANY_T0, &dev->flags) &&
0835                 test_bit(IS_ANY_T1, &dev->flags))) {
0836                 DEBUGP(4, dev, "Perform AUTOPPS\n");
0837                 set_bit(IS_AUTOPPS_ACT, &dev->flags);
0838                 ptsreq.protocol = (0x01 << dev->proto);
0839                 ptsreq.flags = 0x01;
0840                 ptsreq.pts1 = 0x00;
0841                 ptsreq.pts2 = 0x00;
0842                 ptsreq.pts3 = 0x00;
0843                 if (set_protocol(dev, &ptsreq) == 0) {
0844                     DEBUGP(4, dev, "AUTOPPS ret SUCC\n");
0845                     clear_bit(IS_AUTOPPS_ACT, &dev->flags);
0846                     wake_up_interruptible(&dev->atrq);
0847                 } else {
0848                     DEBUGP(4, dev, "AUTOPPS failed: "
0849                            "repower using defaults\n");
0850                     /* prepare for repowering  */
0851                     clear_bit(IS_ATR_PRESENT, &dev->flags);
0852                     clear_bit(IS_ATR_VALID, &dev->flags);
0853                     dev->rlen =
0854                         dev->rpos =
0855                         dev->atr_csum =
0856                         dev->atr_len_retry = dev->cwarn = 0;
0857                     dev->mstate = M_FETCH_ATR;
0858 
0859                     dev->mdelay = T_50MSEC;
0860                 }
0861             } else {
0862                 /* for cards which use slightly different
0863                  * params (extra guard time) */
0864                 set_cardparameter(dev);
0865                 if (test_bit(IS_AUTOPPS_ACT, &dev->flags) == 1)
0866                     DEBUGP(4, dev, "AUTOPPS already active "
0867                            "2nd try:use default values\n");
0868                 if (dev->ta1 == 0x11)
0869                     DEBUGP(4, dev, "No AUTOPPS necessary "
0870                            "TA(1)==0x11\n");
0871                 if (test_bit(IS_ANY_T0, &dev->flags)
0872                     && test_bit(IS_ANY_T1, &dev->flags))
0873                     DEBUGP(4, dev, "Do NOT perform AUTOPPS "
0874                            "with multiprotocol cards\n");
0875                 clear_bit(IS_AUTOPPS_ACT, &dev->flags);
0876                 wake_up_interruptible(&dev->atrq);
0877             }
0878         } else {
0879             DEBUGP(4, dev, "ATR invalid\n");
0880             wake_up_interruptible(&dev->atrq);
0881         }
0882         break;
0883     case M_BAD_CARD:
0884         DEBUGP(4, dev, "M_BAD_CARD\n");
0885         /* slow down warning, but prompt immediately after insertion */
0886         if (dev->cwarn == 0 || dev->cwarn == 10) {
0887             set_bit(IS_BAD_CARD, &dev->flags);
0888             dev_warn(&dev->p_dev->dev, MODULE_NAME ": ");
0889             if (test_bit(IS_BAD_CSUM, &dev->flags)) {
0890                 DEBUGP(4, dev, "ATR checksum (0x%.2x, should "
0891                        "be zero) failed\n", dev->atr_csum);
0892             }
0893 #ifdef CM4000_DEBUG
0894             else if (test_bit(IS_BAD_LENGTH, &dev->flags)) {
0895                 DEBUGP(4, dev, "ATR length error\n");
0896             } else {
0897                 DEBUGP(4, dev, "card damaged or wrong way "
0898                     "inserted\n");
0899             }
0900 #endif
0901             dev->cwarn = 0;
0902             wake_up_interruptible(&dev->atrq);  /* wake open */
0903         }
0904         dev->cwarn++;
0905         dev->mdelay = T_100MSEC;
0906         dev->mstate = M_FETCH_ATR;
0907         break;
0908     default:
0909         DEBUGP(7, dev, "Unknown action\n");
0910         break;      /* nothing */
0911     }
0912 
0913 release_io:
0914     DEBUGP(7, dev, "release_io\n");
0915     clear_bit(LOCK_IO, &dev->flags);
0916     wake_up_interruptible(&dev->ioq);   /* whoever needs IO */
0917 
0918 return_with_timer:
0919     DEBUGP(7, dev, "<- monitor_card (returns with timer)\n");
0920     mod_timer(&dev->timer, jiffies + dev->mdelay);
0921     clear_bit(LOCK_MONITOR, &dev->flags);
0922 }
0923 
0924 /* Interface to userland (file_operations) */
0925 
0926 static ssize_t cmm_read(struct file *filp, __user char *buf, size_t count,
0927             loff_t *ppos)
0928 {
0929     struct cm4000_dev *dev = filp->private_data;
0930     unsigned int iobase = dev->p_dev->resource[0]->start;
0931     ssize_t rc;
0932     int i, j, k;
0933 
0934     DEBUGP(2, dev, "-> cmm_read(%s,%d)\n", current->comm, current->pid);
0935 
0936     if (count == 0)     /* according to manpage */
0937         return 0;
0938 
0939     if (!pcmcia_dev_present(dev->p_dev) || /* device removed */
0940         test_bit(IS_CMM_ABSENT, &dev->flags))
0941         return -ENODEV;
0942 
0943     if (test_bit(IS_BAD_CSUM, &dev->flags))
0944         return -EIO;
0945 
0946     /* also see the note about this in cmm_write */
0947     if (wait_event_interruptible
0948         (dev->atrq,
0949          ((filp->f_flags & O_NONBLOCK)
0950           || (test_bit(IS_ATR_PRESENT, (void *)&dev->flags) != 0)))) {
0951         if (filp->f_flags & O_NONBLOCK)
0952             return -EAGAIN;
0953         return -ERESTARTSYS;
0954     }
0955 
0956     if (test_bit(IS_ATR_VALID, &dev->flags) == 0)
0957         return -EIO;
0958 
0959     /* this one implements blocking IO */
0960     if (wait_event_interruptible
0961         (dev->readq,
0962          ((filp->f_flags & O_NONBLOCK) || (dev->rpos < dev->rlen)))) {
0963         if (filp->f_flags & O_NONBLOCK)
0964             return -EAGAIN;
0965         return -ERESTARTSYS;
0966     }
0967 
0968     /* lock io */
0969     if (wait_event_interruptible
0970         (dev->ioq,
0971          ((filp->f_flags & O_NONBLOCK)
0972           || (test_and_set_bit(LOCK_IO, (void *)&dev->flags) == 0)))) {
0973         if (filp->f_flags & O_NONBLOCK)
0974             return -EAGAIN;
0975         return -ERESTARTSYS;
0976     }
0977 
0978     rc = 0;
0979     dev->flags0 = inb(REG_FLAGS0(iobase));
0980     if ((dev->flags0 & 1) == 0  /* no smartcard inserted */
0981         || dev->flags0 == 0xff) {   /* no cardman inserted */
0982         clear_bit(IS_ATR_VALID, &dev->flags);
0983         if (dev->flags0 & 1) {
0984             set_bit(IS_CMM_ABSENT, &dev->flags);
0985             rc = -ENODEV;
0986         } else {
0987             rc = -EIO;
0988         }
0989         goto release_io;
0990     }
0991 
0992     DEBUGP(4, dev, "begin read answer\n");
0993     j = min(count, (size_t)(dev->rlen - dev->rpos));
0994     k = dev->rpos;
0995     if (k + j > 255)
0996         j = 256 - k;
0997     DEBUGP(4, dev, "read1 j=%d\n", j);
0998     for (i = 0; i < j; i++) {
0999         xoutb(k++, REG_BUF_ADDR(iobase));
1000         dev->rbuf[i] = xinb(REG_BUF_DATA(iobase));
1001     }
1002     j = min(count, (size_t)(dev->rlen - dev->rpos));
1003     if (k + j > 255) {
1004         DEBUGP(4, dev, "read2 j=%d\n", j);
1005         dev->flags1 |= 0x10;    /* MSB buf addr set */
1006         xoutb(dev->flags1, REG_FLAGS1(iobase));
1007         for (; i < j; i++) {
1008             xoutb(k++, REG_BUF_ADDR(iobase));
1009             dev->rbuf[i] = xinb(REG_BUF_DATA(iobase));
1010         }
1011     }
1012 
1013     if (dev->proto == 0 && count > dev->rlen - dev->rpos && i) {
1014         DEBUGP(4, dev, "T=0 and count > buffer\n");
1015         dev->rbuf[i] = dev->rbuf[i - 1];
1016         dev->rbuf[i - 1] = dev->procbyte;
1017         j++;
1018     }
1019     count = j;
1020 
1021     dev->rpos = dev->rlen + 1;
1022 
1023     /* Clear T1Active */
1024     DEBUGP(4, dev, "Clear T1Active\n");
1025     dev->flags1 &= 0xdf;
1026     xoutb(dev->flags1, REG_FLAGS1(iobase));
1027 
1028     xoutb(0, REG_FLAGS1(iobase));   /* clear detectCMM */
1029     /* last check before exit */
1030     if (!io_detect_cm4000(iobase, dev)) {
1031         rc = -ENODEV;
1032         goto release_io;
1033     }
1034 
1035     if (test_bit(IS_INVREV, &dev->flags) && count > 0)
1036         str_invert_revert(dev->rbuf, count);
1037 
1038     if (copy_to_user(buf, dev->rbuf, count))
1039         rc = -EFAULT;
1040 
1041 release_io:
1042     clear_bit(LOCK_IO, &dev->flags);
1043     wake_up_interruptible(&dev->ioq);
1044 
1045     DEBUGP(2, dev, "<- cmm_read returns: rc = %zi\n",
1046            (rc < 0 ? rc : count));
1047     return rc < 0 ? rc : count;
1048 }
1049 
1050 static ssize_t cmm_write(struct file *filp, const char __user *buf,
1051              size_t count, loff_t *ppos)
1052 {
1053     struct cm4000_dev *dev = filp->private_data;
1054     unsigned int iobase = dev->p_dev->resource[0]->start;
1055     unsigned short s;
1056     unsigned char infolen;
1057     unsigned char sendT0;
1058     unsigned short nsend;
1059     unsigned short nr;
1060     ssize_t rc;
1061     int i;
1062 
1063     DEBUGP(2, dev, "-> cmm_write(%s,%d)\n", current->comm, current->pid);
1064 
1065     if (count == 0)     /* according to manpage */
1066         return 0;
1067 
1068     if (dev->proto == 0 && count < 4) {
1069         /* T0 must have at least 4 bytes */
1070         DEBUGP(4, dev, "T0 short write\n");
1071         return -EIO;
1072     }
1073 
1074     nr = count & 0x1ff; /* max bytes to write */
1075 
1076     sendT0 = dev->proto ? 0 : nr > 5 ? 0x08 : 0;
1077 
1078     if (!pcmcia_dev_present(dev->p_dev) || /* device removed */
1079         test_bit(IS_CMM_ABSENT, &dev->flags))
1080         return -ENODEV;
1081 
1082     if (test_bit(IS_BAD_CSUM, &dev->flags)) {
1083         DEBUGP(4, dev, "bad csum\n");
1084         return -EIO;
1085     }
1086 
1087     /*
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!
1097      */
1098     if (wait_event_interruptible
1099         (dev->atrq,
1100          ((filp->f_flags & O_NONBLOCK)
1101           || (test_bit(IS_ATR_PRESENT, (void *)&dev->flags) != 0)))) {
1102         if (filp->f_flags & O_NONBLOCK)
1103             return -EAGAIN;
1104         return -ERESTARTSYS;
1105     }
1106 
1107     if (test_bit(IS_ATR_VALID, &dev->flags) == 0) { /* invalid atr */
1108         DEBUGP(4, dev, "invalid ATR\n");
1109         return -EIO;
1110     }
1111 
1112     /* lock io */
1113     if (wait_event_interruptible
1114         (dev->ioq,
1115          ((filp->f_flags & O_NONBLOCK)
1116           || (test_and_set_bit(LOCK_IO, (void *)&dev->flags) == 0)))) {
1117         if (filp->f_flags & O_NONBLOCK)
1118             return -EAGAIN;
1119         return -ERESTARTSYS;
1120     }
1121 
1122     if (copy_from_user(dev->sbuf, buf, ((count > 512) ? 512 : count)))
1123         return -EFAULT;
1124 
1125     rc = 0;
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);
1132             rc = -ENODEV;
1133         } else {
1134             DEBUGP(4, dev, "IO error\n");
1135             rc = -EIO;
1136         }
1137         goto release_io;
1138     }
1139 
1140     xoutb(0x80, REG_FLAGS0(iobase));    /* reset SM  */
1141 
1142     if (!io_detect_cm4000(iobase, dev)) {
1143         rc = -ENODEV;
1144         goto release_io;
1145     }
1146 
1147     /* reflect T=0 send/read mode in flags1 */
1148     dev->flags1 |= (sendT0);
1149 
1150     set_cardparameter(dev);
1151 
1152     /* dummy read, reset flag procedure received */
1153     inb(REG_FLAGS1(iobase));
1154 
1155     dev->flags1 = 0x20  /* T_Active */
1156         | (sendT0)
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));
1161 
1162     /* xmit data */
1163     DEBUGP(4, dev, "Xmit data\n");
1164     for (i = 0; i < nr; i++) {
1165         if (i >= 256) {
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));
1175         }
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));
1183         } else {
1184             xoutb(i, REG_BUF_ADDR(iobase));
1185             xoutb(dev->sbuf[i], REG_BUF_DATA(iobase));
1186         }
1187     }
1188     DEBUGP(4, dev, "Xmit done\n");
1189 
1190     if (dev->proto == 0) {
1191         /* T=0 proto: 0 byte reply  */
1192         if (nr == 4) {
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));
1197             else
1198                 xoutb(0x00, REG_BUF_DATA(iobase));
1199         }
1200 
1201         /* numSendBytes */
1202         if (sendT0)
1203             nsend = nr;
1204         else {
1205             if (nr == 4)
1206                 nsend = 5;
1207             else {
1208                 nsend = 5 + (unsigned char)dev->sbuf[4];
1209                 if (dev->sbuf[4] == 0)
1210                     nsend += 0x100;
1211             }
1212         }
1213     } else
1214         nsend = nr;
1215 
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));
1221     } else {
1222         DEBUGP(4, dev, "T=0 set Procedure byte 0x%.2x\n", dev->sbuf[1]);
1223         xoutb(dev->sbuf[1], REG_NUM_BYTES(iobase));
1224     }
1225 
1226     DEBUGP(1, dev, "set NumSendBytes = 0x%.2x\n",
1227            (unsigned char)(nsend & 0xff));
1228     xoutb((unsigned char)(nsend & 0xff), REG_NUM_SEND(iobase));
1229 
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));
1240 
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)
1246                 break;
1247             msleep_interruptible(10);
1248         }
1249         if (i == 1000) {
1250             DEBUGP(4, dev, "timeout waiting for xmit done\n");
1251             rc = -EIO;
1252             goto release_io;
1253         }
1254     }
1255 
1256     /* T=1: wait for infoLen */
1257 
1258     infolen = 0;
1259     if (dev->proto) {
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);
1263             if (s >= 3) {
1264                 infolen = inb(REG_FLAGS1(iobase));
1265                 DEBUGP(4, dev, "infolen=%d\n", infolen);
1266                 break;
1267             }
1268             msleep_interruptible(10);
1269         }
1270         if (i == 6000) {
1271             DEBUGP(4, dev, "timeout waiting for infoLen\n");
1272             rc = -EIO;
1273             goto release_io;
1274         }
1275     } else
1276         clear_bit(IS_PROCBYTE_PRESENT, &dev->flags);
1277 
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 */
1281         if (dev->proto) {
1282             if (dev->rlen >= infolen + 4)
1283                 break;
1284         }
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 */
1291             dev->rlen = s;
1292         }
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.
1299          */
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");
1304                 /* i=0; */
1305             } else {
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",
1311                       dev->procbyte);
1312                 i = 0;  /* resettimeout */
1313             }
1314             if (inb(REG_FLAGS0(iobase)) & 0x08) {
1315                 DEBUGP(1, dev, "T0Done flag (read reply)\n");
1316                 break;
1317             }
1318         }
1319         if (dev->proto)
1320             infolen = inb(REG_FLAGS1(iobase));
1321     }
1322     if (i == 600) {
1323         DEBUGP(1, dev, "timeout waiting for numRecBytes\n");
1324         rc = -EIO;
1325         goto release_io;
1326     } else {
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)
1331                     break;
1332                 msleep_interruptible(10);
1333             }
1334             if (i == 1000) {
1335                 DEBUGP(1, dev, "timeout waiting for T0Done\n");
1336                 rc = -EIO;
1337                 goto release_io;
1338             }
1339 
1340             dev->procbyte = inb(REG_FLAGS1(iobase));
1341             DEBUGP(4, dev, "Read procedure byte 0x%.2x\n",
1342                   dev->procbyte);
1343 
1344             io_read_num_rec_bytes(iobase, &dev->rlen);
1345             DEBUGP(4, dev, "Read NumRecBytes = %i\n", dev->rlen);
1346 
1347         }
1348     }
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);
1353 
1354 release_io:
1355     DEBUGP(4, dev, "Reset SM\n");
1356     xoutb(0x80, REG_FLAGS0(iobase));    /* reset SM */
1357 
1358     if (rc < 0) {
1359         DEBUGP(4, dev, "Write failed but clear T_Active\n");
1360         dev->flags1 &= 0xdf;
1361         xoutb(dev->flags1, REG_FLAGS1(iobase));
1362     }
1363 
1364     clear_bit(LOCK_IO, &dev->flags);
1365     wake_up_interruptible(&dev->ioq);
1366     wake_up_interruptible(&dev->readq); /* tell read we have data */
1367 
1368     /* ITSEC E2: clear write buffer */
1369     memset((char *)dev->sbuf, 0, 512);
1370 
1371     /* return error or actually written bytes */
1372     DEBUGP(2, dev, "<- cmm_write\n");
1373     return rc < 0 ? rc : nr;
1374 }
1375 
1376 static void start_monitor(struct cm4000_dev *dev)
1377 {
1378     DEBUGP(3, dev, "-> start_monitor\n");
1379     if (!dev->monitor_running) {
1380         DEBUGP(5, dev, "create, init and add timer\n");
1381         timer_setup(&dev->timer, monitor_card, 0);
1382         dev->monitor_running = 1;
1383         mod_timer(&dev->timer, jiffies);
1384     } else
1385         DEBUGP(5, dev, "monitor already running\n");
1386     DEBUGP(3, dev, "<- start_monitor\n");
1387 }
1388 
1389 static void stop_monitor(struct cm4000_dev *dev)
1390 {
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);
1398     } else
1399         DEBUGP(5, dev, "monitor already stopped\n");
1400     DEBUGP(3, dev, "<- stop_monitor\n");
1401 }
1402 
1403 static long cmm_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1404 {
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;
1409     int rc;
1410     void __user *argp = (void __user *)arg;
1411 #ifdef CM4000_DEBUG
1412     char *ioctl_names[CM_IOC_MAXNR + 1] = {
1413         [_IOC_NR(CM_IOCGSTATUS)] "CM_IOCGSTATUS",
1414         [_IOC_NR(CM_IOCGATR)] "CM_IOCGATR",
1415         [_IOC_NR(CM_IOCARDOFF)] "CM_IOCARDOFF",
1416         [_IOC_NR(CM_IOCSPTS)] "CM_IOCSPTS",
1417         [_IOC_NR(CM_IOSDBGLVL)] "CM4000_DBGLVL",
1418     };
1419     DEBUGP(3, dev, "cmm_ioctl(device=%d.%d) %s\n", imajor(inode),
1420            iminor(inode), ioctl_names[_IOC_NR(cmd)]);
1421 #endif
1422 
1423     mutex_lock(&cmm_mutex);
1424     rc = -ENODEV;
1425     link = dev_table[iminor(inode)];
1426     if (!pcmcia_dev_present(link)) {
1427         DEBUGP(4, dev, "DEV_OK false\n");
1428         goto out;
1429     }
1430 
1431     if (test_bit(IS_CMM_ABSENT, &dev->flags)) {
1432         DEBUGP(4, dev, "CMM_ABSENT flag set\n");
1433         goto out;
1434     }
1435     rc = -EINVAL;
1436 
1437     if (_IOC_TYPE(cmd) != CM_IOC_MAGIC) {
1438         DEBUGP(4, dev, "ioctype mismatch\n");
1439         goto out;
1440     }
1441     if (_IOC_NR(cmd) > CM_IOC_MAXNR) {
1442         DEBUGP(4, dev, "iocnr mismatch\n");
1443         goto out;
1444     }
1445     rc = 0;
1446 
1447     switch (cmd) {
1448     case CM_IOCGSTATUS:
1449         DEBUGP(4, dev, " ... in CM_IOCGSTATUS\n");
1450         {
1451             int status;
1452 
1453             /* clear other bits, but leave inserted & powered as
1454              * they are */
1455             status = dev->flags0 & 3;
1456             if (test_bit(IS_ATR_PRESENT, &dev->flags))
1457                 status |= CM_ATR_PRESENT;
1458             if (test_bit(IS_ATR_VALID, &dev->flags))
1459                 status |= CM_ATR_VALID;
1460             if (test_bit(IS_CMM_ABSENT, &dev->flags))
1461                 status |= CM_NO_READER;
1462             if (test_bit(IS_BAD_CARD, &dev->flags))
1463                 status |= CM_BAD_CARD;
1464             if (copy_to_user(argp, &status, sizeof(int)))
1465                 rc = -EFAULT;
1466         }
1467         break;
1468     case CM_IOCGATR:
1469         DEBUGP(4, dev, "... in CM_IOCGATR\n");
1470         {
1471             struct atreq __user *atreq = argp;
1472             int tmp;
1473             /* allow nonblocking io and being interrupted */
1474             if (wait_event_interruptible
1475                 (dev->atrq,
1476                  ((filp->f_flags & O_NONBLOCK)
1477                   || (test_bit(IS_ATR_PRESENT, (void *)&dev->flags)
1478                   != 0)))) {
1479                 if (filp->f_flags & O_NONBLOCK)
1480                     rc = -EAGAIN;
1481                 else
1482                     rc = -ERESTARTSYS;
1483                 break;
1484             }
1485 
1486             rc = -EFAULT;
1487             if (test_bit(IS_ATR_VALID, &dev->flags) == 0) {
1488                 tmp = -1;
1489                 if (copy_to_user(&(atreq->atr_len), &tmp,
1490                          sizeof(int)))
1491                     break;
1492             } else {
1493                 if (copy_to_user(atreq->atr, dev->atr,
1494                          dev->atr_len))
1495                     break;
1496 
1497                 tmp = dev->atr_len;
1498                 if (copy_to_user(&(atreq->atr_len), &tmp, sizeof(int)))
1499                     break;
1500             }
1501             rc = 0;
1502             break;
1503         }
1504     case CM_IOCARDOFF:
1505 
1506 #ifdef CM4000_DEBUG
1507         DEBUGP(4, dev, "... in CM_IOCARDOFF\n");
1508         if (dev->flags0 & 0x01) {
1509             DEBUGP(4, dev, "    Card inserted\n");
1510         } else {
1511             DEBUGP(2, dev, "    No card inserted\n");
1512         }
1513         if (dev->flags0 & 0x02) {
1514             DEBUGP(4, dev, "    Card powered\n");
1515         } else {
1516             DEBUGP(2, dev, "    Card not powered\n");
1517         }
1518 #endif
1519 
1520         /* is a card inserted and powered? */
1521         if ((dev->flags0 & 0x01) && (dev->flags0 & 0x02)) {
1522 
1523             /* get IO lock */
1524             if (wait_event_interruptible
1525                 (dev->ioq,
1526                  ((filp->f_flags & O_NONBLOCK)
1527                   || (test_and_set_bit(LOCK_IO, (void *)&dev->flags)
1528                   == 0)))) {
1529                 if (filp->f_flags & O_NONBLOCK)
1530                     rc = -EAGAIN;
1531                 else
1532                     rc = -ERESTARTSYS;
1533                 break;
1534             }
1535             /* Set Flags0 = 0x42 */
1536             DEBUGP(4, dev, "Set Flags0=0x42 \n");
1537             xoutb(0x42, REG_FLAGS0(iobase));
1538             clear_bit(IS_ATR_PRESENT, &dev->flags);
1539             clear_bit(IS_ATR_VALID, &dev->flags);
1540             dev->mstate = M_CARDOFF;
1541             clear_bit(LOCK_IO, &dev->flags);
1542             if (wait_event_interruptible
1543                 (dev->atrq,
1544                  ((filp->f_flags & O_NONBLOCK)
1545                   || (test_bit(IS_ATR_VALID, (void *)&dev->flags) !=
1546                   0)))) {
1547                 if (filp->f_flags & O_NONBLOCK)
1548                     rc = -EAGAIN;
1549                 else
1550                     rc = -ERESTARTSYS;
1551                 break;
1552             }
1553         }
1554         /* release lock */
1555         clear_bit(LOCK_IO, &dev->flags);
1556         wake_up_interruptible(&dev->ioq);
1557 
1558         rc = 0;
1559         break;
1560     case CM_IOCSPTS:
1561         {
1562             struct ptsreq krnptsreq;
1563 
1564             if (copy_from_user(&krnptsreq, argp,
1565                        sizeof(struct ptsreq))) {
1566                 rc = -EFAULT;
1567                 break;
1568             }
1569 
1570             rc = 0;
1571             DEBUGP(4, dev, "... in CM_IOCSPTS\n");
1572             /* wait for ATR to get valid */
1573             if (wait_event_interruptible
1574                 (dev->atrq,
1575                  ((filp->f_flags & O_NONBLOCK)
1576                   || (test_bit(IS_ATR_PRESENT, (void *)&dev->flags)
1577                   != 0)))) {
1578                 if (filp->f_flags & O_NONBLOCK)
1579                     rc = -EAGAIN;
1580                 else
1581                     rc = -ERESTARTSYS;
1582                 break;
1583             }
1584             /* get IO lock */
1585             if (wait_event_interruptible
1586                 (dev->ioq,
1587                  ((filp->f_flags & O_NONBLOCK)
1588                   || (test_and_set_bit(LOCK_IO, (void *)&dev->flags)
1589                   == 0)))) {
1590                 if (filp->f_flags & O_NONBLOCK)
1591                     rc = -EAGAIN;
1592                 else
1593                     rc = -ERESTARTSYS;
1594                 break;
1595             }
1596 
1597             if ((rc = set_protocol(dev, &krnptsreq)) != 0) {
1598                 /* auto power_on again */
1599                 dev->mstate = M_FETCH_ATR;
1600                 clear_bit(IS_ATR_VALID, &dev->flags);
1601             }
1602             /* release lock */
1603             clear_bit(LOCK_IO, &dev->flags);
1604             wake_up_interruptible(&dev->ioq);
1605 
1606         }
1607         break;
1608 #ifdef CM4000_DEBUG
1609     case CM_IOSDBGLVL:
1610         rc = -ENOTTY;
1611         break;
1612 #endif
1613     default:
1614         DEBUGP(4, dev, "... in default (unknown IOCTL code)\n");
1615         rc = -ENOTTY;
1616     }
1617 out:
1618     mutex_unlock(&cmm_mutex);
1619     return rc;
1620 }
1621 
1622 static int cmm_open(struct inode *inode, struct file *filp)
1623 {
1624     struct cm4000_dev *dev;
1625     struct pcmcia_device *link;
1626     int minor = iminor(inode);
1627     int ret;
1628 
1629     if (minor >= CM4000_MAX_DEV)
1630         return -ENODEV;
1631 
1632     mutex_lock(&cmm_mutex);
1633     link = dev_table[minor];
1634     if (link == NULL || !pcmcia_dev_present(link)) {
1635         ret = -ENODEV;
1636         goto out;
1637     }
1638 
1639     if (link->open) {
1640         ret = -EBUSY;
1641         goto out;
1642     }
1643 
1644     dev = link->priv;
1645     filp->private_data = dev;
1646 
1647     DEBUGP(2, dev, "-> cmm_open(device=%d.%d process=%s,%d)\n",
1648           imajor(inode), minor, current->comm, current->pid);
1649 
1650     /* init device variables, they may be "polluted" after close
1651      * or, the device may never have been closed (i.e. open failed)
1652      */
1653 
1654     ZERO_DEV(dev);
1655 
1656     /* opening will always block since the
1657      * monitor will be started by open, which
1658      * means we have to wait for ATR becoming
1659      * valid = block until valid (or card
1660      * inserted)
1661      */
1662     if (filp->f_flags & O_NONBLOCK) {
1663         ret = -EAGAIN;
1664         goto out;
1665     }
1666 
1667     dev->mdelay = T_50MSEC;
1668 
1669     /* start monitoring the cardstatus */
1670     start_monitor(dev);
1671 
1672     link->open = 1;     /* only one open per device */
1673 
1674     DEBUGP(2, dev, "<- cmm_open\n");
1675     ret = stream_open(inode, filp);
1676 out:
1677     mutex_unlock(&cmm_mutex);
1678     return ret;
1679 }
1680 
1681 static int cmm_close(struct inode *inode, struct file *filp)
1682 {
1683     struct cm4000_dev *dev;
1684     struct pcmcia_device *link;
1685     int minor = iminor(inode);
1686 
1687     if (minor >= CM4000_MAX_DEV)
1688         return -ENODEV;
1689 
1690     link = dev_table[minor];
1691     if (link == NULL)
1692         return -ENODEV;
1693 
1694     dev = link->priv;
1695 
1696     DEBUGP(2, dev, "-> cmm_close(maj/min=%d.%d)\n",
1697            imajor(inode), minor);
1698 
1699     stop_monitor(dev);
1700 
1701     ZERO_DEV(dev);
1702 
1703     link->open = 0;     /* only one open per device */
1704     wake_up(&dev->devq);    /* socket removed? */
1705 
1706     DEBUGP(2, dev, "cmm_close\n");
1707     return 0;
1708 }
1709 
1710 static void cmm_cm4000_release(struct pcmcia_device * link)
1711 {
1712     struct cm4000_dev *dev = link->priv;
1713 
1714     /* dont terminate the monitor, rather rely on
1715      * close doing that for us.
1716      */
1717     DEBUGP(3, dev, "-> cmm_cm4000_release\n");
1718     while (link->open) {
1719         printk(KERN_INFO MODULE_NAME ": delaying release until "
1720                "process has terminated\n");
1721         /* note: don't interrupt us:
1722          * close the applications which own
1723          * the devices _first_ !
1724          */
1725         wait_event(dev->devq, (link->open == 0));
1726     }
1727     /* dev->devq=NULL;  this cannot be zeroed earlier */
1728     DEBUGP(3, dev, "<- cmm_cm4000_release\n");
1729     return;
1730 }
1731 
1732 /*==== Interface to PCMCIA Layer =======================================*/
1733 
1734 static int cm4000_config_check(struct pcmcia_device *p_dev, void *priv_data)
1735 {
1736     return pcmcia_request_io(p_dev);
1737 }
1738 
1739 static int cm4000_config(struct pcmcia_device * link, int devno)
1740 {
1741     link->config_flags |= CONF_AUTO_SET_IO;
1742 
1743     /* read the config-tuples */
1744     if (pcmcia_loop_config(link, cm4000_config_check, NULL))
1745         goto cs_release;
1746 
1747     if (pcmcia_enable_device(link))
1748         goto cs_release;
1749 
1750     return 0;
1751 
1752 cs_release:
1753     cm4000_release(link);
1754     return -ENODEV;
1755 }
1756 
1757 static int cm4000_suspend(struct pcmcia_device *link)
1758 {
1759     struct cm4000_dev *dev;
1760 
1761     dev = link->priv;
1762     stop_monitor(dev);
1763 
1764     return 0;
1765 }
1766 
1767 static int cm4000_resume(struct pcmcia_device *link)
1768 {
1769     struct cm4000_dev *dev;
1770 
1771     dev = link->priv;
1772     if (link->open)
1773         start_monitor(dev);
1774 
1775     return 0;
1776 }
1777 
1778 static void cm4000_release(struct pcmcia_device *link)
1779 {
1780     cmm_cm4000_release(link);   /* delay release until device closed */
1781     pcmcia_disable_device(link);
1782 }
1783 
1784 static int cm4000_probe(struct pcmcia_device *link)
1785 {
1786     struct cm4000_dev *dev;
1787     int i, ret;
1788 
1789     for (i = 0; i < CM4000_MAX_DEV; i++)
1790         if (dev_table[i] == NULL)
1791             break;
1792 
1793     if (i == CM4000_MAX_DEV) {
1794         printk(KERN_NOTICE MODULE_NAME ": all devices in use\n");
1795         return -ENODEV;
1796     }
1797 
1798     /* create a new cm4000_cs device */
1799     dev = kzalloc(sizeof(struct cm4000_dev), GFP_KERNEL);
1800     if (dev == NULL)
1801         return -ENOMEM;
1802 
1803     dev->p_dev = link;
1804     link->priv = dev;
1805     dev_table[i] = link;
1806 
1807     init_waitqueue_head(&dev->devq);
1808     init_waitqueue_head(&dev->ioq);
1809     init_waitqueue_head(&dev->atrq);
1810     init_waitqueue_head(&dev->readq);
1811 
1812     ret = cm4000_config(link, i);
1813     if (ret) {
1814         dev_table[i] = NULL;
1815         kfree(dev);
1816         return ret;
1817     }
1818 
1819     device_create(cmm_class, NULL, MKDEV(major, i), NULL, "cmm%d", i);
1820 
1821     return 0;
1822 }
1823 
1824 static void cm4000_detach(struct pcmcia_device *link)
1825 {
1826     struct cm4000_dev *dev = link->priv;
1827     int devno;
1828 
1829     /* find device */
1830     for (devno = 0; devno < CM4000_MAX_DEV; devno++)
1831         if (dev_table[devno] == link)
1832             break;
1833     if (devno == CM4000_MAX_DEV)
1834         return;
1835 
1836     stop_monitor(dev);
1837 
1838     cm4000_release(link);
1839 
1840     dev_table[devno] = NULL;
1841     kfree(dev);
1842 
1843     device_destroy(cmm_class, MKDEV(major, devno));
1844 
1845     return;
1846 }
1847 
1848 static const struct file_operations cm4000_fops = {
1849     .owner  = THIS_MODULE,
1850     .read   = cmm_read,
1851     .write  = cmm_write,
1852     .unlocked_ioctl = cmm_ioctl,
1853     .open   = cmm_open,
1854     .release= cmm_close,
1855     .llseek = no_llseek,
1856 };
1857 
1858 static const struct pcmcia_device_id cm4000_ids[] = {
1859     PCMCIA_DEVICE_MANF_CARD(0x0223, 0x0002),
1860     PCMCIA_DEVICE_PROD_ID12("CardMan", "4000", 0x2FB368CA, 0xA2BD8C39),
1861     PCMCIA_DEVICE_NULL,
1862 };
1863 MODULE_DEVICE_TABLE(pcmcia, cm4000_ids);
1864 
1865 static struct pcmcia_driver cm4000_driver = {
1866     .owner    = THIS_MODULE,
1867     .name     = "cm4000_cs",
1868     .probe    = cm4000_probe,
1869     .remove   = cm4000_detach,
1870     .suspend  = cm4000_suspend,
1871     .resume   = cm4000_resume,
1872     .id_table = cm4000_ids,
1873 };
1874 
1875 static int __init cmm_init(void)
1876 {
1877     int rc;
1878 
1879     cmm_class = class_create(THIS_MODULE, "cardman_4000");
1880     if (IS_ERR(cmm_class))
1881         return PTR_ERR(cmm_class);
1882 
1883     major = register_chrdev(0, DEVICE_NAME, &cm4000_fops);
1884     if (major < 0) {
1885         printk(KERN_WARNING MODULE_NAME
1886             ": could not get major number\n");
1887         class_destroy(cmm_class);
1888         return major;
1889     }
1890 
1891     rc = pcmcia_register_driver(&cm4000_driver);
1892     if (rc < 0) {
1893         unregister_chrdev(major, DEVICE_NAME);
1894         class_destroy(cmm_class);
1895         return rc;
1896     }
1897 
1898     return 0;
1899 }
1900 
1901 static void __exit cmm_exit(void)
1902 {
1903     pcmcia_unregister_driver(&cm4000_driver);
1904     unregister_chrdev(major, DEVICE_NAME);
1905     class_destroy(cmm_class);
1906 };
1907 
1908 module_init(cmm_init);
1909 module_exit(cmm_exit);
1910 MODULE_LICENSE("Dual BSD/GPL");