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