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