pcmcia: do not use io_req_t after call to pcmcia_request_io()
[linux-2.6.git] / drivers / char / pcmcia / cm4040_cs.c
1 /*
2  * A driver for the Omnikey PCMCIA smartcard reader CardMan 4040
3  *
4  * (c) 2000-2004 Omnikey AG (http://www.omnikey.com/)
5  *
6  * (C) 2005-2006 Harald Welte <laforge@gnumonks.org>
7  *      - add support for poll()
8  *      - driver cleanup
9  *      - add waitqueues
10  *      - adhere to linux kernel coding style and policies
11  *      - support 2.6.13 "new style" pcmcia interface
12  *      - add class interface for udev device creation
13  *
14  * The device basically is a USB CCID compliant device that has been
15  * attached to an I/O-Mapped FIFO.
16  *
17  * All rights reserved, Dual BSD/GPL Licensed.
18  */
19
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/slab.h>
23 #include <linux/init.h>
24 #include <linux/fs.h>
25 #include <linux/delay.h>
26 #include <linux/poll.h>
27 #include <linux/smp_lock.h>
28 #include <linux/wait.h>
29 #include <asm/uaccess.h>
30 #include <asm/io.h>
31
32 #include <pcmcia/cs.h>
33 #include <pcmcia/cistpl.h>
34 #include <pcmcia/cisreg.h>
35 #include <pcmcia/ciscode.h>
36 #include <pcmcia/ds.h>
37
38 #include "cm4040_cs.h"
39
40
41 #define reader_to_dev(x)        (&x->p_dev->dev)
42
43 /* n (debug level) is ignored */
44 /* additional debug output may be enabled by re-compiling with
45  * CM4040_DEBUG set */
46 /* #define CM4040_DEBUG */
47 #define DEBUGP(n, rdr, x, args...) do {                 \
48                 dev_dbg(reader_to_dev(rdr), "%s:" x,    \
49                            __func__ , ## args);         \
50         } while (0)
51
52 static char *version =
53 "OMNIKEY CardMan 4040 v1.1.0gm5 - All bugs added by Harald Welte";
54
55 #define CCID_DRIVER_BULK_DEFAULT_TIMEOUT        (150*HZ)
56 #define CCID_DRIVER_ASYNC_POWERUP_TIMEOUT       (35*HZ)
57 #define CCID_DRIVER_MINIMUM_TIMEOUT             (3*HZ)
58 #define READ_WRITE_BUFFER_SIZE 512
59 #define POLL_LOOP_COUNT                         1000
60
61 /* how often to poll for fifo status change */
62 #define POLL_PERIOD                             msecs_to_jiffies(10)
63
64 static void reader_release(struct pcmcia_device *link);
65
66 static int major;
67 static struct class *cmx_class;
68
69 #define         BS_READABLE     0x01
70 #define         BS_WRITABLE     0x02
71
72 struct reader_dev {
73         struct pcmcia_device    *p_dev;
74         wait_queue_head_t       devq;
75         wait_queue_head_t       poll_wait;
76         wait_queue_head_t       read_wait;
77         wait_queue_head_t       write_wait;
78         unsigned long           buffer_status;
79         unsigned long           timeout;
80         unsigned char           s_buf[READ_WRITE_BUFFER_SIZE];
81         unsigned char           r_buf[READ_WRITE_BUFFER_SIZE];
82         struct timer_list       poll_timer;
83 };
84
85 static struct pcmcia_device *dev_table[CM_MAX_DEV];
86
87 #ifndef CM4040_DEBUG
88 #define xoutb   outb
89 #define xinb    inb
90 #else
91 static inline void xoutb(unsigned char val, unsigned short port)
92 {
93         pr_debug("outb(val=%.2x,port=%.4x)\n", val, port);
94         outb(val, port);
95 }
96
97 static inline unsigned char xinb(unsigned short port)
98 {
99         unsigned char val;
100
101         val = inb(port);
102         pr_debug("%.2x=inb(%.4x)\n", val, port);
103         return val;
104 }
105 #endif
106
107 /* poll the device fifo status register.  not to be confused with
108  * the poll syscall. */
109 static void cm4040_do_poll(unsigned long dummy)
110 {
111         struct reader_dev *dev = (struct reader_dev *) dummy;
112         unsigned int obs = xinb(dev->p_dev->resource[0]->start
113                                 + REG_OFFSET_BUFFER_STATUS);
114
115         if ((obs & BSR_BULK_IN_FULL)) {
116                 set_bit(BS_READABLE, &dev->buffer_status);
117                 DEBUGP(4, dev, "waking up read_wait\n");
118                 wake_up_interruptible(&dev->read_wait);
119         } else
120                 clear_bit(BS_READABLE, &dev->buffer_status);
121
122         if (!(obs & BSR_BULK_OUT_FULL)) {
123                 set_bit(BS_WRITABLE, &dev->buffer_status);
124                 DEBUGP(4, dev, "waking up write_wait\n");
125                 wake_up_interruptible(&dev->write_wait);
126         } else
127                 clear_bit(BS_WRITABLE, &dev->buffer_status);
128
129         if (dev->buffer_status)
130                 wake_up_interruptible(&dev->poll_wait);
131
132         mod_timer(&dev->poll_timer, jiffies + POLL_PERIOD);
133 }
134
135 static void cm4040_stop_poll(struct reader_dev *dev)
136 {
137         del_timer_sync(&dev->poll_timer);
138 }
139
140 static int wait_for_bulk_out_ready(struct reader_dev *dev)
141 {
142         int i, rc;
143         int iobase = dev->p_dev->resource[0]->start;
144
145         for (i = 0; i < POLL_LOOP_COUNT; i++) {
146                 if ((xinb(iobase + REG_OFFSET_BUFFER_STATUS)
147                     & BSR_BULK_OUT_FULL) == 0) {
148                         DEBUGP(4, dev, "BulkOut empty (i=%d)\n", i);
149                         return 1;
150                 }
151         }
152
153         DEBUGP(4, dev, "wait_event_interruptible_timeout(timeout=%ld\n",
154                 dev->timeout);
155         rc = wait_event_interruptible_timeout(dev->write_wait,
156                                               test_and_clear_bit(BS_WRITABLE,
157                                                        &dev->buffer_status),
158                                               dev->timeout);
159
160         if (rc > 0)
161                 DEBUGP(4, dev, "woke up: BulkOut empty\n");
162         else if (rc == 0)
163                 DEBUGP(4, dev, "woke up: BulkOut full, returning 0 :(\n");
164         else if (rc < 0)
165                 DEBUGP(4, dev, "woke up: signal arrived\n");
166
167         return rc;
168 }
169
170 /* Write to Sync Control Register */
171 static int write_sync_reg(unsigned char val, struct reader_dev *dev)
172 {
173         int iobase = dev->p_dev->resource[0]->start;
174         int rc;
175
176         rc = wait_for_bulk_out_ready(dev);
177         if (rc <= 0)
178                 return rc;
179
180         xoutb(val, iobase + REG_OFFSET_SYNC_CONTROL);
181         rc = wait_for_bulk_out_ready(dev);
182         if (rc <= 0)
183                 return rc;
184
185         return 1;
186 }
187
188 static int wait_for_bulk_in_ready(struct reader_dev *dev)
189 {
190         int i, rc;
191         int iobase = dev->p_dev->resource[0]->start;
192
193         for (i = 0; i < POLL_LOOP_COUNT; i++) {
194                 if ((xinb(iobase + REG_OFFSET_BUFFER_STATUS)
195                     & BSR_BULK_IN_FULL) == BSR_BULK_IN_FULL) {
196                         DEBUGP(3, dev, "BulkIn full (i=%d)\n", i);
197                         return 1;
198                 }
199         }
200
201         DEBUGP(4, dev, "wait_event_interruptible_timeout(timeout=%ld\n",
202                 dev->timeout);
203         rc = wait_event_interruptible_timeout(dev->read_wait,
204                                               test_and_clear_bit(BS_READABLE,
205                                                         &dev->buffer_status),
206                                               dev->timeout);
207         if (rc > 0)
208                 DEBUGP(4, dev, "woke up: BulkIn full\n");
209         else if (rc == 0)
210                 DEBUGP(4, dev, "woke up: BulkIn not full, returning 0 :(\n");
211         else if (rc < 0)
212                 DEBUGP(4, dev, "woke up: signal arrived\n");
213
214         return rc;
215 }
216
217 static ssize_t cm4040_read(struct file *filp, char __user *buf,
218                         size_t count, loff_t *ppos)
219 {
220         struct reader_dev *dev = filp->private_data;
221         int iobase = dev->p_dev->resource[0]->start;
222         size_t bytes_to_read;
223         unsigned long i;
224         size_t min_bytes_to_read;
225         int rc;
226         unsigned char uc;
227
228         DEBUGP(2, dev, "-> cm4040_read(%s,%d)\n", current->comm, current->pid);
229
230         if (count == 0)
231                 return 0;
232
233         if (count < 10)
234                 return -EFAULT;
235
236         if (filp->f_flags & O_NONBLOCK) {
237                 DEBUGP(4, dev, "filep->f_flags O_NONBLOCK set\n");
238                 DEBUGP(2, dev, "<- cm4040_read (failure)\n");
239                 return -EAGAIN;
240         }
241
242         if (!pcmcia_dev_present(dev->p_dev))
243                 return -ENODEV;
244
245         for (i = 0; i < 5; i++) {
246                 rc = wait_for_bulk_in_ready(dev);
247                 if (rc <= 0) {
248                         DEBUGP(5, dev, "wait_for_bulk_in_ready rc=%.2x\n", rc);
249                         DEBUGP(2, dev, "<- cm4040_read (failed)\n");
250                         if (rc == -ERESTARTSYS)
251                                 return rc;
252                         return -EIO;
253                 }
254                 dev->r_buf[i] = xinb(iobase + REG_OFFSET_BULK_IN);
255 #ifdef CM4040_DEBUG
256                 pr_debug("%lu:%2x ", i, dev->r_buf[i]);
257         }
258         pr_debug("\n");
259 #else
260         }
261 #endif
262
263         bytes_to_read = 5 + le32_to_cpu(*(__le32 *)&dev->r_buf[1]);
264
265         DEBUGP(6, dev, "BytesToRead=%zu\n", bytes_to_read);
266
267         min_bytes_to_read = min(count, bytes_to_read + 5);
268         min_bytes_to_read = min_t(size_t, min_bytes_to_read, READ_WRITE_BUFFER_SIZE);
269
270         DEBUGP(6, dev, "Min=%zu\n", min_bytes_to_read);
271
272         for (i = 0; i < (min_bytes_to_read-5); i++) {
273                 rc = wait_for_bulk_in_ready(dev);
274                 if (rc <= 0) {
275                         DEBUGP(5, dev, "wait_for_bulk_in_ready rc=%.2x\n", rc);
276                         DEBUGP(2, dev, "<- cm4040_read (failed)\n");
277                         if (rc == -ERESTARTSYS)
278                                 return rc;
279                         return -EIO;
280                 }
281                 dev->r_buf[i+5] = xinb(iobase + REG_OFFSET_BULK_IN);
282 #ifdef CM4040_DEBUG
283                 pr_debug("%lu:%2x ", i, dev->r_buf[i]);
284         }
285         pr_debug("\n");
286 #else
287         }
288 #endif
289
290         *ppos = min_bytes_to_read;
291         if (copy_to_user(buf, dev->r_buf, min_bytes_to_read))
292                 return -EFAULT;
293
294         rc = wait_for_bulk_in_ready(dev);
295         if (rc <= 0) {
296                 DEBUGP(5, dev, "wait_for_bulk_in_ready rc=%.2x\n", rc);
297                 DEBUGP(2, dev, "<- cm4040_read (failed)\n");
298                 if (rc == -ERESTARTSYS)
299                         return rc;
300                 return -EIO;
301         }
302
303         rc = write_sync_reg(SCR_READER_TO_HOST_DONE, dev);
304         if (rc <= 0) {
305                 DEBUGP(5, dev, "write_sync_reg c=%.2x\n", rc);
306                 DEBUGP(2, dev, "<- cm4040_read (failed)\n");
307                 if (rc == -ERESTARTSYS)
308                         return rc;
309                 else
310                         return -EIO;
311         }
312
313         uc = xinb(iobase + REG_OFFSET_BULK_IN);
314
315         DEBUGP(2, dev, "<- cm4040_read (successfully)\n");
316         return min_bytes_to_read;
317 }
318
319 static ssize_t cm4040_write(struct file *filp, const char __user *buf,
320                          size_t count, loff_t *ppos)
321 {
322         struct reader_dev *dev = filp->private_data;
323         int iobase = dev->p_dev->resource[0]->start;
324         ssize_t rc;
325         int i;
326         unsigned int bytes_to_write;
327
328         DEBUGP(2, dev, "-> cm4040_write(%s,%d)\n", current->comm, current->pid);
329
330         if (count == 0) {
331                 DEBUGP(2, dev, "<- cm4040_write empty read (successfully)\n");
332                 return 0;
333         }
334
335         if ((count < 5) || (count > READ_WRITE_BUFFER_SIZE)) {
336                 DEBUGP(2, dev, "<- cm4040_write buffersize=%Zd < 5\n", count);
337                 return -EIO;
338         }
339
340         if (filp->f_flags & O_NONBLOCK) {
341                 DEBUGP(4, dev, "filep->f_flags O_NONBLOCK set\n");
342                 DEBUGP(4, dev, "<- cm4040_write (failure)\n");
343                 return -EAGAIN;
344         }
345
346         if (!pcmcia_dev_present(dev->p_dev))
347                 return -ENODEV;
348
349         bytes_to_write = count;
350         if (copy_from_user(dev->s_buf, buf, bytes_to_write))
351                 return -EFAULT;
352
353         switch (dev->s_buf[0]) {
354                 case CMD_PC_TO_RDR_XFRBLOCK:
355                 case CMD_PC_TO_RDR_SECURE:
356                 case CMD_PC_TO_RDR_TEST_SECURE:
357                 case CMD_PC_TO_RDR_OK_SECURE:
358                         dev->timeout = CCID_DRIVER_BULK_DEFAULT_TIMEOUT;
359                         break;
360
361                 case CMD_PC_TO_RDR_ICCPOWERON:
362                         dev->timeout = CCID_DRIVER_ASYNC_POWERUP_TIMEOUT;
363                         break;
364
365                 case CMD_PC_TO_RDR_GETSLOTSTATUS:
366                 case CMD_PC_TO_RDR_ICCPOWEROFF:
367                 case CMD_PC_TO_RDR_GETPARAMETERS:
368                 case CMD_PC_TO_RDR_RESETPARAMETERS:
369                 case CMD_PC_TO_RDR_SETPARAMETERS:
370                 case CMD_PC_TO_RDR_ESCAPE:
371                 case CMD_PC_TO_RDR_ICCCLOCK:
372                 default:
373                         dev->timeout = CCID_DRIVER_MINIMUM_TIMEOUT;
374                         break;
375         }
376
377         rc = write_sync_reg(SCR_HOST_TO_READER_START, dev);
378         if (rc <= 0) {
379                 DEBUGP(5, dev, "write_sync_reg c=%.2Zx\n", rc);
380                 DEBUGP(2, dev, "<- cm4040_write (failed)\n");
381                 if (rc == -ERESTARTSYS)
382                         return rc;
383                 else
384                         return -EIO;
385         }
386
387         DEBUGP(4, dev, "start \n");
388
389         for (i = 0; i < bytes_to_write; i++) {
390                 rc = wait_for_bulk_out_ready(dev);
391                 if (rc <= 0) {
392                         DEBUGP(5, dev, "wait_for_bulk_out_ready rc=%.2Zx\n",
393                                rc);
394                         DEBUGP(2, dev, "<- cm4040_write (failed)\n");
395                         if (rc == -ERESTARTSYS)
396                                 return rc;
397                         else
398                                 return -EIO;
399                 }
400
401                 xoutb(dev->s_buf[i],iobase + REG_OFFSET_BULK_OUT);
402         }
403         DEBUGP(4, dev, "end\n");
404
405         rc = write_sync_reg(SCR_HOST_TO_READER_DONE, dev);
406
407         if (rc <= 0) {
408                 DEBUGP(5, dev, "write_sync_reg c=%.2Zx\n", rc);
409                 DEBUGP(2, dev, "<- cm4040_write (failed)\n");
410                 if (rc == -ERESTARTSYS)
411                         return rc;
412                 else
413                         return -EIO;
414         }
415
416         DEBUGP(2, dev, "<- cm4040_write (successfully)\n");
417         return count;
418 }
419
420 static unsigned int cm4040_poll(struct file *filp, poll_table *wait)
421 {
422         struct reader_dev *dev = filp->private_data;
423         unsigned int mask = 0;
424
425         poll_wait(filp, &dev->poll_wait, wait);
426
427         if (test_and_clear_bit(BS_READABLE, &dev->buffer_status))
428                 mask |= POLLIN | POLLRDNORM;
429         if (test_and_clear_bit(BS_WRITABLE, &dev->buffer_status))
430                 mask |= POLLOUT | POLLWRNORM;
431
432         DEBUGP(2, dev, "<- cm4040_poll(%u)\n", mask);
433
434         return mask;
435 }
436
437 static int cm4040_open(struct inode *inode, struct file *filp)
438 {
439         struct reader_dev *dev;
440         struct pcmcia_device *link;
441         int minor = iminor(inode);
442         int ret;
443
444         if (minor >= CM_MAX_DEV)
445                 return -ENODEV;
446
447         lock_kernel();
448         link = dev_table[minor];
449         if (link == NULL || !pcmcia_dev_present(link)) {
450                 ret = -ENODEV;
451                 goto out;
452         }
453
454         if (link->open) {
455                 ret = -EBUSY;
456                 goto out;
457         }
458
459         dev = link->priv;
460         filp->private_data = dev;
461
462         if (filp->f_flags & O_NONBLOCK) {
463                 DEBUGP(4, dev, "filep->f_flags O_NONBLOCK set\n");
464                 ret = -EAGAIN;
465                 goto out;
466         }
467
468         link->open = 1;
469
470         dev->poll_timer.data = (unsigned long) dev;
471         mod_timer(&dev->poll_timer, jiffies + POLL_PERIOD);
472
473         DEBUGP(2, dev, "<- cm4040_open (successfully)\n");
474         ret = nonseekable_open(inode, filp);
475 out:
476         unlock_kernel();
477         return ret;
478 }
479
480 static int cm4040_close(struct inode *inode, struct file *filp)
481 {
482         struct reader_dev *dev = filp->private_data;
483         struct pcmcia_device *link;
484         int minor = iminor(inode);
485
486         DEBUGP(2, dev, "-> cm4040_close(maj/min=%d.%d)\n", imajor(inode),
487               iminor(inode));
488
489         if (minor >= CM_MAX_DEV)
490                 return -ENODEV;
491
492         link = dev_table[minor];
493         if (link == NULL)
494                 return -ENODEV;
495
496         cm4040_stop_poll(dev);
497
498         link->open = 0;
499         wake_up(&dev->devq);
500
501         DEBUGP(2, dev, "<- cm4040_close\n");
502         return 0;
503 }
504
505 static void cm4040_reader_release(struct pcmcia_device *link)
506 {
507         struct reader_dev *dev = link->priv;
508
509         DEBUGP(3, dev, "-> cm4040_reader_release\n");
510         while (link->open) {
511                 DEBUGP(3, dev, KERN_INFO MODULE_NAME ": delaying release "
512                        "until process has terminated\n");
513                 wait_event(dev->devq, (link->open == 0));
514         }
515         DEBUGP(3, dev, "<- cm4040_reader_release\n");
516         return;
517 }
518
519 static int cm4040_config_check(struct pcmcia_device *p_dev,
520                                cistpl_cftable_entry_t *cfg,
521                                cistpl_cftable_entry_t *dflt,
522                                unsigned int vcc,
523                                void *priv_data)
524 {
525         int rc;
526         if (!cfg->io.nwin)
527                 return -ENODEV;
528
529         /* Get the IOaddr */
530         p_dev->io.BasePort1 = cfg->io.win[0].base;
531         p_dev->io.NumPorts1 = cfg->io.win[0].len;
532         p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
533         if (!(cfg->io.flags & CISTPL_IO_8BIT))
534                 p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_16;
535         if (!(cfg->io.flags & CISTPL_IO_16BIT))
536                 p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
537         p_dev->io.IOAddrLines = cfg->io.flags & CISTPL_IO_LINES_MASK;
538
539         rc = pcmcia_request_io(p_dev, &p_dev->io);
540         dev_printk(KERN_INFO, &p_dev->dev,
541                    "pcmcia_request_io returned 0x%x\n", rc);
542         return rc;
543 }
544
545
546 static int reader_config(struct pcmcia_device *link, int devno)
547 {
548         struct reader_dev *dev;
549         int fail_rc;
550
551         link->io.BasePort2 = 0;
552         link->io.NumPorts2 = 0;
553         link->io.Attributes2 = 0;
554
555         if (pcmcia_loop_config(link, cm4040_config_check, NULL))
556                 goto cs_release;
557
558         link->conf.IntType = 00000002;
559
560         fail_rc = pcmcia_request_configuration(link, &link->conf);
561         if (fail_rc != 0) {
562                 dev_printk(KERN_INFO, &link->dev,
563                            "pcmcia_request_configuration failed 0x%x\n",
564                            fail_rc);
565                 goto cs_release;
566         }
567
568         dev = link->priv;
569
570         DEBUGP(2, dev, "device " DEVICE_NAME "%d at %pR\n", devno,
571               link->resource[0]);
572         DEBUGP(2, dev, "<- reader_config (succ)\n");
573
574         return 0;
575
576 cs_release:
577         reader_release(link);
578         return -ENODEV;
579 }
580
581 static void reader_release(struct pcmcia_device *link)
582 {
583         cm4040_reader_release(link);
584         pcmcia_disable_device(link);
585 }
586
587 static int reader_probe(struct pcmcia_device *link)
588 {
589         struct reader_dev *dev;
590         int i, ret;
591
592         for (i = 0; i < CM_MAX_DEV; i++) {
593                 if (dev_table[i] == NULL)
594                         break;
595         }
596
597         if (i == CM_MAX_DEV)
598                 return -ENODEV;
599
600         dev = kzalloc(sizeof(struct reader_dev), GFP_KERNEL);
601         if (dev == NULL)
602                 return -ENOMEM;
603
604         dev->timeout = CCID_DRIVER_MINIMUM_TIMEOUT;
605         dev->buffer_status = 0;
606
607         link->priv = dev;
608         dev->p_dev = link;
609
610         link->conf.IntType = INT_MEMORY_AND_IO;
611         dev_table[i] = link;
612
613         init_waitqueue_head(&dev->devq);
614         init_waitqueue_head(&dev->poll_wait);
615         init_waitqueue_head(&dev->read_wait);
616         init_waitqueue_head(&dev->write_wait);
617         setup_timer(&dev->poll_timer, cm4040_do_poll, 0);
618
619         ret = reader_config(link, i);
620         if (ret) {
621                 dev_table[i] = NULL;
622                 kfree(dev);
623                 return ret;
624         }
625
626         device_create(cmx_class, NULL, MKDEV(major, i), NULL, "cmx%d", i);
627
628         return 0;
629 }
630
631 static void reader_detach(struct pcmcia_device *link)
632 {
633         struct reader_dev *dev = link->priv;
634         int devno;
635
636         /* find device */
637         for (devno = 0; devno < CM_MAX_DEV; devno++) {
638                 if (dev_table[devno] == link)
639                         break;
640         }
641         if (devno == CM_MAX_DEV)
642                 return;
643
644         reader_release(link);
645
646         dev_table[devno] = NULL;
647         kfree(dev);
648
649         device_destroy(cmx_class, MKDEV(major, devno));
650
651         return;
652 }
653
654 static const struct file_operations reader_fops = {
655         .owner          = THIS_MODULE,
656         .read           = cm4040_read,
657         .write          = cm4040_write,
658         .open           = cm4040_open,
659         .release        = cm4040_close,
660         .poll           = cm4040_poll,
661 };
662
663 static struct pcmcia_device_id cm4040_ids[] = {
664         PCMCIA_DEVICE_MANF_CARD(0x0223, 0x0200),
665         PCMCIA_DEVICE_PROD_ID12("OMNIKEY", "CardMan 4040",
666                                 0xE32CDD8C, 0x8F23318B),
667         PCMCIA_DEVICE_NULL,
668 };
669 MODULE_DEVICE_TABLE(pcmcia, cm4040_ids);
670
671 static struct pcmcia_driver reader_driver = {
672         .owner          = THIS_MODULE,
673         .drv            = {
674                 .name   = "cm4040_cs",
675         },
676         .probe          = reader_probe,
677         .remove         = reader_detach,
678         .id_table       = cm4040_ids,
679 };
680
681 static int __init cm4040_init(void)
682 {
683         int rc;
684
685         printk(KERN_INFO "%s\n", version);
686         cmx_class = class_create(THIS_MODULE, "cardman_4040");
687         if (IS_ERR(cmx_class))
688                 return PTR_ERR(cmx_class);
689
690         major = register_chrdev(0, DEVICE_NAME, &reader_fops);
691         if (major < 0) {
692                 printk(KERN_WARNING MODULE_NAME
693                         ": could not get major number\n");
694                 class_destroy(cmx_class);
695                 return major;
696         }
697
698         rc = pcmcia_register_driver(&reader_driver);
699         if (rc < 0) {
700                 unregister_chrdev(major, DEVICE_NAME);
701                 class_destroy(cmx_class);
702                 return rc;
703         }
704
705         return 0;
706 }
707
708 static void __exit cm4040_exit(void)
709 {
710         printk(KERN_INFO MODULE_NAME ": unloading\n");
711         pcmcia_unregister_driver(&reader_driver);
712         unregister_chrdev(major, DEVICE_NAME);
713         class_destroy(cmx_class);
714 }
715
716 module_init(cm4040_init);
717 module_exit(cm4040_exit);
718 MODULE_LICENSE("Dual BSD/GPL");