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