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