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