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