2 * A driver for the Omnikey PCMCIA smartcard reader CardMan 4040
4 * (c) 2000-2004 Omnikey AG (http://www.omnikey.com/)
6 * (C) 2005 Harald Welte <laforge@gnumonks.org>
7 * - add support for poll()
10 * - adhere to linux kernel coding style and policies
11 * - support 2.6.13 "new style" pcmcia interface
13 * The device basically is a USB CCID compliant device that has been
14 * attached to an I/O-Mapped FIFO.
16 * All rights reserved, Dual BSD/GPL Licensed.
19 /* #define PCMCIA_DEBUG 6 */
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/slab.h>
24 #include <linux/init.h>
26 #include <linux/delay.h>
27 #include <linux/poll.h>
28 #include <linux/wait.h>
29 #include <asm/uaccess.h>
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>
39 #include "cm4040_cs.h"
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); \
52 #define DEBUGP(n, rdr, x, args...)
55 static char *version =
56 "OMNIKEY CardMan 4040 v1.1.0gm4 - All bugs added by Harald Welte";
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
64 /* how often to poll for fifo status change */
65 #define POLL_PERIOD msecs_to_jiffies(10)
67 static void reader_release(dev_link_t *link);
71 #define BS_READABLE 0x01
72 #define BS_WRITABLE 0x02
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;
88 static dev_link_t *dev_table[CM_MAX_DEV];
94 static inline void xoutb(unsigned char val, unsigned short port)
97 printk(KERN_DEBUG "outb(val=%.2x,port=%.4x)\n", val, port);
101 static inline unsigned char xinb(unsigned short port)
107 printk(KERN_DEBUG "%.2x=inb(%.4x)\n", val, port);
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)
116 struct reader_dev *dev = (struct reader_dev *) dummy;
117 unsigned int obs = xinb(dev->link.io.BasePort1
118 + REG_OFFSET_BUFFER_STATUS);
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);
125 clear_bit(BS_READABLE, &dev->buffer_status);
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);
132 clear_bit(BS_WRITABLE, &dev->buffer_status);
134 if (dev->buffer_status)
135 wake_up_interruptible(&dev->poll_wait);
137 mod_timer(&dev->poll_timer, jiffies + POLL_PERIOD);
140 static void cm4040_stop_poll(struct reader_dev *dev)
142 del_timer_sync(&dev->poll_timer);
145 static int wait_for_bulk_out_ready(struct reader_dev *dev)
148 int iobase = dev->link.io.BasePort1;
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);
158 DEBUGP(4, dev, "wait_event_interruptible_timeout(timeout=%ld\n",
160 rc = wait_event_interruptible_timeout(dev->write_wait,
161 test_and_clear_bit(BS_WRITABLE,
162 &dev->buffer_status),
166 DEBUGP(4, dev, "woke up: BulkOut empty\n");
168 DEBUGP(4, dev, "woke up: BulkOut full, returning 0 :(\n");
170 DEBUGP(4, dev, "woke up: signal arrived\n");
175 /* Write to Sync Control Register */
176 static int write_sync_reg(unsigned char val, struct reader_dev *dev)
178 int iobase = dev->link.io.BasePort1;
181 rc = wait_for_bulk_out_ready(dev);
185 xoutb(val, iobase + REG_OFFSET_SYNC_CONTROL);
186 rc = wait_for_bulk_out_ready(dev);
193 static int wait_for_bulk_in_ready(struct reader_dev *dev)
196 int iobase = dev->link.io.BasePort1;
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);
206 DEBUGP(4, dev, "wait_event_interruptible_timeout(timeout=%ld\n",
208 rc = wait_event_interruptible_timeout(dev->read_wait,
209 test_and_clear_bit(BS_READABLE,
210 &dev->buffer_status),
213 DEBUGP(4, dev, "woke up: BulkIn full\n");
215 DEBUGP(4, dev, "woke up: BulkIn not full, returning 0 :(\n");
217 DEBUGP(4, dev, "woke up: signal arrived\n");
222 static ssize_t cm4040_read(struct file *filp, char __user *buf,
223 size_t count, loff_t *ppos)
225 struct reader_dev *dev = filp->private_data;
226 int iobase = dev->link.io.BasePort1;
227 size_t bytes_to_read;
229 size_t min_bytes_to_read;
233 DEBUGP(2, dev, "-> cm4040_read(%s,%d)\n", current->comm, current->pid);
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");
247 if ((dev->link.state & DEV_PRESENT)==0)
250 for (i = 0; i < 5; i++) {
251 rc = wait_for_bulk_in_ready(dev);
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)
259 dev->r_buf[i] = xinb(iobase + REG_OFFSET_BULK_IN);
262 printk(KERN_DEBUG "%lu:%2x ", i, dev->r_buf[i]);
269 bytes_to_read = 5 + le32_to_cpu(*(__le32 *)&dev->r_buf[1]);
271 DEBUGP(6, dev, "BytesToRead=%lu\n", bytes_to_read);
273 min_bytes_to_read = min(count, bytes_to_read + 5);
275 DEBUGP(6, dev, "Min=%lu\n", min_bytes_to_read);
277 for (i = 0; i < (min_bytes_to_read-5); i++) {
278 rc = wait_for_bulk_in_ready(dev);
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)
286 dev->r_buf[i+5] = xinb(iobase + REG_OFFSET_BULK_IN);
289 printk(KERN_DEBUG "%lu:%2x ", i, dev->r_buf[i]);
296 *ppos = min_bytes_to_read;
297 if (copy_to_user(buf, dev->r_buf, min_bytes_to_read))
300 rc = wait_for_bulk_in_ready(dev);
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)
309 rc = write_sync_reg(SCR_READER_TO_HOST_DONE, dev);
311 DEBUGP(5, dev, "write_sync_reg c=%.2x\n", rc);
312 DEBUGP(2, dev, "<- cm4040_read (failed)\n");
313 if (rc == -ERESTARTSYS)
319 uc = xinb(iobase + REG_OFFSET_BULK_IN);
321 DEBUGP(2, dev, "<- cm4040_read (successfully)\n");
322 return min_bytes_to_read;
325 static ssize_t cm4040_write(struct file *filp, const char __user *buf,
326 size_t count, loff_t *ppos)
328 struct reader_dev *dev = filp->private_data;
329 int iobase = dev->link.io.BasePort1;
332 unsigned int bytes_to_write;
334 DEBUGP(2, dev, "-> cm4040_write(%s,%d)\n", current->comm, current->pid);
337 DEBUGP(2, dev, "<- cm4040_write empty read (successfully)\n");
342 DEBUGP(2, dev, "<- cm4040_write buffersize=%Zd < 5\n", count);
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");
352 if ((dev->link.state & DEV_PRESENT) == 0)
355 bytes_to_write = count;
356 if (copy_from_user(dev->s_buf, buf, bytes_to_write))
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;
367 case CMD_PC_TO_RDR_ICCPOWERON:
368 dev->timeout = CCID_DRIVER_ASYNC_POWERUP_TIMEOUT;
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:
379 dev->timeout = CCID_DRIVER_MINIMUM_TIMEOUT;
383 rc = write_sync_reg(SCR_HOST_TO_READER_START, dev);
385 DEBUGP(5, dev, "write_sync_reg c=%.2Zx\n", rc);
386 DEBUGP(2, dev, "<- cm4040_write (failed)\n");
387 if (rc == -ERESTARTSYS)
393 DEBUGP(4, dev, "start \n");
395 for (i = 0; i < bytes_to_write; i++) {
396 rc = wait_for_bulk_out_ready(dev);
398 DEBUGP(5, dev, "wait_for_bulk_out_ready rc=%.2Zx\n",
400 DEBUGP(2, dev, "<- cm4040_write (failed)\n");
401 if (rc == -ERESTARTSYS)
407 xoutb(dev->s_buf[i],iobase + REG_OFFSET_BULK_OUT);
409 DEBUGP(4, dev, "end\n");
411 rc = write_sync_reg(SCR_HOST_TO_READER_DONE, dev);
414 DEBUGP(5, dev, "write_sync_reg c=%.2Zx\n", rc);
415 DEBUGP(2, dev, "<- cm4040_write (failed)\n");
416 if (rc == -ERESTARTSYS)
422 DEBUGP(2, dev, "<- cm4040_write (successfully)\n");
426 static unsigned int cm4040_poll(struct file *filp, poll_table *wait)
428 struct reader_dev *dev = filp->private_data;
429 unsigned int mask = 0;
431 poll_wait(filp, &dev->poll_wait, wait);
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;
438 DEBUGP(2, dev, "<- cm4040_poll(%u)\n", mask);
443 static int cm4040_open(struct inode *inode, struct file *filp)
445 struct reader_dev *dev;
447 int minor = iminor(inode);
449 if (minor >= CM_MAX_DEV)
452 link = dev_table[minor];
453 if (link == NULL || !(DEV_OK(link)))
460 filp->private_data = dev;
462 if (filp->f_flags & O_NONBLOCK) {
463 DEBUGP(4, dev, "filep->f_flags O_NONBLOCK set\n");
469 dev->poll_timer.data = (unsigned long) dev;
470 mod_timer(&dev->poll_timer, jiffies + POLL_PERIOD);
472 DEBUGP(2, dev, "<- cm4040_open (successfully)\n");
473 return nonseekable_open(inode, filp);
476 static int cm4040_close(struct inode *inode, struct file *filp)
478 struct reader_dev *dev = filp->private_data;
480 int minor = iminor(inode);
482 DEBUGP(2, dev, "-> cm4040_close(maj/min=%d.%d)\n", imajor(inode),
485 if (minor >= CM_MAX_DEV)
488 link = dev_table[minor];
492 cm4040_stop_poll(dev);
497 DEBUGP(2, dev, "<- cm4040_close\n");
501 static void cm4040_reader_release(dev_link_t *link)
503 struct reader_dev *dev = link->priv;
505 DEBUGP(3, dev, "-> cm4040_reader_release\n");
507 DEBUGP(3, dev, KERN_INFO MODULE_NAME ": delaying release "
508 "until process has terminated\n");
509 wait_event(dev->devq, (link->open == 0));
511 DEBUGP(3, dev, "<- cm4040_reader_release\n");
515 static void reader_config(dev_link_t *link, int devno)
517 client_handle_t handle;
518 struct reader_dev *dev;
523 int fail_fn, fail_rc;
526 handle = link->handle;
528 tuple.DesiredTuple = CISTPL_CONFIG;
529 tuple.Attributes = 0;
530 tuple.TupleData = buf;
531 tuple.TupleDataMax = sizeof(buf);
532 tuple.TupleOffset = 0;
534 if ((fail_rc = pcmcia_get_first_tuple(handle, &tuple)) != CS_SUCCESS) {
535 fail_fn = GetFirstTuple;
538 if ((fail_rc = pcmcia_get_tuple_data(handle, &tuple)) != CS_SUCCESS) {
539 fail_fn = GetTupleData;
542 if ((fail_rc = pcmcia_parse_tuple(handle, &tuple, &parse))
544 fail_fn = ParseTuple;
547 if ((fail_rc = pcmcia_get_configuration_info(handle, &conf))
549 fail_fn = GetConfigurationInfo;
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;
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);
564 rc = pcmcia_get_next_tuple(handle, &tuple)) {
565 rc = pcmcia_get_tuple_data(handle, &tuple);
566 if (rc != CS_SUCCESS)
568 rc = pcmcia_parse_tuple(handle, &tuple, &parse);
569 if (rc != CS_SUCCESS)
572 link->conf.ConfigIndex = parse.cftable_entry.index;
574 if (!parse.cftable_entry.io.nwin)
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);
588 dev_printk(KERN_INFO, &handle_to_dev(handle), "foo");
589 if (rc == CS_SUCCESS)
592 dev_printk(KERN_INFO, &handle_to_dev(handle),
593 "pcmcia_request_io failed 0x%x\n", rc);
595 if (rc != CS_SUCCESS)
598 link->conf.IntType = 00000002;
600 if ((fail_rc = pcmcia_request_configuration(handle,&link->conf))
602 fail_fn = RequestConfiguration;
603 dev_printk(KERN_INFO, &handle_to_dev(handle),
604 "pcmcia_request_configuration failed 0x%x\n",
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;
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");
624 cs_error(handle, fail_fn, fail_rc);
626 reader_release(link);
627 link->state &= ~DEV_CONFIG_PENDING;
630 static int reader_suspend(struct pcmcia_device *p_dev)
632 dev_link_t *link = dev_to_instance(p_dev);
634 link->state |= DEV_SUSPEND;
635 if (link->state & DEV_CONFIG)
636 pcmcia_release_configuration(link->handle);
641 static int reader_resume(struct pcmcia_device *p_dev)
643 dev_link_t *link = dev_to_instance(p_dev);
645 link->state &= ~DEV_SUSPEND;
646 if (link->state & DEV_CONFIG)
647 pcmcia_request_configuration(link->handle, &link->conf);
652 static void reader_release(dev_link_t *link)
654 cm4040_reader_release(link->priv);
655 pcmcia_release_configuration(link->handle);
656 pcmcia_release_io(link->handle, &link->io);
659 static int reader_attach(struct pcmcia_device *p_dev)
661 struct reader_dev *dev;
665 for (i = 0; i < CM_MAX_DEV; i++) {
666 if (dev_table[i] == NULL)
673 dev = kzalloc(sizeof(struct reader_dev), GFP_KERNEL);
677 dev->timeout = CCID_DRIVER_MINIMUM_TIMEOUT;
678 dev->buffer_status = 0;
683 link->conf.IntType = INT_MEMORY_AND_IO;
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;
693 link->handle = p_dev;
694 p_dev->instance = link;
696 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
697 reader_config(link, i);
702 static void reader_detach(struct pcmcia_device *p_dev)
704 dev_link_t *link = dev_to_instance(p_dev);
705 struct reader_dev *dev = link->priv;
709 for (devno = 0; devno < CM_MAX_DEV; devno++) {
710 if (dev_table[devno] == link)
713 if (devno == CM_MAX_DEV)
716 link->state &= ~DEV_PRESENT;
718 if (link->state & DEV_CONFIG)
719 reader_release(link);
721 dev_table[devno] = NULL;
727 static struct file_operations reader_fops = {
728 .owner = THIS_MODULE,
730 .write = cm4040_write,
732 .release = cm4040_close,
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),
742 MODULE_DEVICE_TABLE(pcmcia, cm4040_ids);
744 static struct pcmcia_driver reader_driver = {
745 .owner = THIS_MODULE,
749 .probe = reader_attach,
750 .remove = reader_detach,
751 .suspend = reader_suspend,
752 .resume = reader_resume,
753 .id_table = cm4040_ids,
756 static int __init cm4040_init(void)
758 printk(KERN_INFO "%s\n", version);
759 pcmcia_register_driver(&reader_driver);
760 major = register_chrdev(0, DEVICE_NAME, &reader_fops);
762 printk(KERN_WARNING MODULE_NAME
763 ": could not get major number\n");
769 static void __exit cm4040_exit(void)
771 printk(KERN_INFO MODULE_NAME ": unloading\n");
772 pcmcia_unregister_driver(&reader_driver);
773 unregister_chrdev(major, DEVICE_NAME);
776 module_init(cm4040_init);
777 module_exit(cm4040_exit);
778 MODULE_LICENSE("Dual BSD/GPL");