2 * A driver for the Omnikey PCMCIA smartcard reader CardMan 4040
4 * (c) 2000-2004 Omnikey AG (http://www.omnikey.com/)
6 * (C) 2005-2006 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
12 * - add class interface for udev device creation
14 * The device basically is a USB CCID compliant device that has been
15 * attached to an I/O-Mapped FIFO.
17 * All rights reserved, Dual BSD/GPL Licensed.
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/slab.h>
23 #include <linux/init.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>
32 #include <pcmcia/cs.h>
33 #include <pcmcia/cistpl.h>
34 #include <pcmcia/cisreg.h>
35 #include <pcmcia/ciscode.h>
36 #include <pcmcia/ds.h>
38 #include "cm4040_cs.h"
41 #define reader_to_dev(x) (&x->p_dev->dev)
43 /* n (debug level) is ignored */
44 /* additional debug output may be enabled by re-compiling with
46 /* #define CM4040_DEBUG */
47 #define DEBUGP(n, rdr, x, args...) do { \
48 dev_dbg(reader_to_dev(rdr), "%s:" x, \
49 __func__ , ## args); \
52 static char *version =
53 "OMNIKEY CardMan 4040 v1.1.0gm5 - All bugs added by Harald Welte";
55 #define CCID_DRIVER_BULK_DEFAULT_TIMEOUT (150*HZ)
56 #define CCID_DRIVER_ASYNC_POWERUP_TIMEOUT (35*HZ)
57 #define CCID_DRIVER_MINIMUM_TIMEOUT (3*HZ)
58 #define READ_WRITE_BUFFER_SIZE 512
59 #define POLL_LOOP_COUNT 1000
61 /* how often to poll for fifo status change */
62 #define POLL_PERIOD msecs_to_jiffies(10)
64 static void reader_release(struct pcmcia_device *link);
67 static struct class *cmx_class;
69 #define BS_READABLE 0x01
70 #define BS_WRITABLE 0x02
73 struct pcmcia_device *p_dev;
74 wait_queue_head_t devq;
75 wait_queue_head_t poll_wait;
76 wait_queue_head_t read_wait;
77 wait_queue_head_t write_wait;
78 unsigned long buffer_status;
79 unsigned long timeout;
80 unsigned char s_buf[READ_WRITE_BUFFER_SIZE];
81 unsigned char r_buf[READ_WRITE_BUFFER_SIZE];
82 struct timer_list poll_timer;
85 static struct pcmcia_device *dev_table[CM_MAX_DEV];
91 static inline void xoutb(unsigned char val, unsigned short port)
93 pr_debug("outb(val=%.2x,port=%.4x)\n", val, port);
97 static inline unsigned char xinb(unsigned short port)
102 pr_debug("%.2x=inb(%.4x)\n", val, port);
107 /* poll the device fifo status register. not to be confused with
108 * the poll syscall. */
109 static void cm4040_do_poll(unsigned long dummy)
111 struct reader_dev *dev = (struct reader_dev *) dummy;
112 unsigned int obs = xinb(dev->p_dev->resource[0]->start
113 + REG_OFFSET_BUFFER_STATUS);
115 if ((obs & BSR_BULK_IN_FULL)) {
116 set_bit(BS_READABLE, &dev->buffer_status);
117 DEBUGP(4, dev, "waking up read_wait\n");
118 wake_up_interruptible(&dev->read_wait);
120 clear_bit(BS_READABLE, &dev->buffer_status);
122 if (!(obs & BSR_BULK_OUT_FULL)) {
123 set_bit(BS_WRITABLE, &dev->buffer_status);
124 DEBUGP(4, dev, "waking up write_wait\n");
125 wake_up_interruptible(&dev->write_wait);
127 clear_bit(BS_WRITABLE, &dev->buffer_status);
129 if (dev->buffer_status)
130 wake_up_interruptible(&dev->poll_wait);
132 mod_timer(&dev->poll_timer, jiffies + POLL_PERIOD);
135 static void cm4040_stop_poll(struct reader_dev *dev)
137 del_timer_sync(&dev->poll_timer);
140 static int wait_for_bulk_out_ready(struct reader_dev *dev)
143 int iobase = dev->p_dev->resource[0]->start;
145 for (i = 0; i < POLL_LOOP_COUNT; i++) {
146 if ((xinb(iobase + REG_OFFSET_BUFFER_STATUS)
147 & BSR_BULK_OUT_FULL) == 0) {
148 DEBUGP(4, dev, "BulkOut empty (i=%d)\n", i);
153 DEBUGP(4, dev, "wait_event_interruptible_timeout(timeout=%ld\n",
155 rc = wait_event_interruptible_timeout(dev->write_wait,
156 test_and_clear_bit(BS_WRITABLE,
157 &dev->buffer_status),
161 DEBUGP(4, dev, "woke up: BulkOut empty\n");
163 DEBUGP(4, dev, "woke up: BulkOut full, returning 0 :(\n");
165 DEBUGP(4, dev, "woke up: signal arrived\n");
170 /* Write to Sync Control Register */
171 static int write_sync_reg(unsigned char val, struct reader_dev *dev)
173 int iobase = dev->p_dev->resource[0]->start;
176 rc = wait_for_bulk_out_ready(dev);
180 xoutb(val, iobase + REG_OFFSET_SYNC_CONTROL);
181 rc = wait_for_bulk_out_ready(dev);
188 static int wait_for_bulk_in_ready(struct reader_dev *dev)
191 int iobase = dev->p_dev->resource[0]->start;
193 for (i = 0; i < POLL_LOOP_COUNT; i++) {
194 if ((xinb(iobase + REG_OFFSET_BUFFER_STATUS)
195 & BSR_BULK_IN_FULL) == BSR_BULK_IN_FULL) {
196 DEBUGP(3, dev, "BulkIn full (i=%d)\n", i);
201 DEBUGP(4, dev, "wait_event_interruptible_timeout(timeout=%ld\n",
203 rc = wait_event_interruptible_timeout(dev->read_wait,
204 test_and_clear_bit(BS_READABLE,
205 &dev->buffer_status),
208 DEBUGP(4, dev, "woke up: BulkIn full\n");
210 DEBUGP(4, dev, "woke up: BulkIn not full, returning 0 :(\n");
212 DEBUGP(4, dev, "woke up: signal arrived\n");
217 static ssize_t cm4040_read(struct file *filp, char __user *buf,
218 size_t count, loff_t *ppos)
220 struct reader_dev *dev = filp->private_data;
221 int iobase = dev->p_dev->resource[0]->start;
222 size_t bytes_to_read;
224 size_t min_bytes_to_read;
228 DEBUGP(2, dev, "-> cm4040_read(%s,%d)\n", current->comm, current->pid);
236 if (filp->f_flags & O_NONBLOCK) {
237 DEBUGP(4, dev, "filep->f_flags O_NONBLOCK set\n");
238 DEBUGP(2, dev, "<- cm4040_read (failure)\n");
242 if (!pcmcia_dev_present(dev->p_dev))
245 for (i = 0; i < 5; i++) {
246 rc = wait_for_bulk_in_ready(dev);
248 DEBUGP(5, dev, "wait_for_bulk_in_ready rc=%.2x\n", rc);
249 DEBUGP(2, dev, "<- cm4040_read (failed)\n");
250 if (rc == -ERESTARTSYS)
254 dev->r_buf[i] = xinb(iobase + REG_OFFSET_BULK_IN);
256 pr_debug("%lu:%2x ", i, dev->r_buf[i]);
263 bytes_to_read = 5 + le32_to_cpu(*(__le32 *)&dev->r_buf[1]);
265 DEBUGP(6, dev, "BytesToRead=%zu\n", bytes_to_read);
267 min_bytes_to_read = min(count, bytes_to_read + 5);
268 min_bytes_to_read = min_t(size_t, min_bytes_to_read, READ_WRITE_BUFFER_SIZE);
270 DEBUGP(6, dev, "Min=%zu\n", min_bytes_to_read);
272 for (i = 0; i < (min_bytes_to_read-5); i++) {
273 rc = wait_for_bulk_in_ready(dev);
275 DEBUGP(5, dev, "wait_for_bulk_in_ready rc=%.2x\n", rc);
276 DEBUGP(2, dev, "<- cm4040_read (failed)\n");
277 if (rc == -ERESTARTSYS)
281 dev->r_buf[i+5] = xinb(iobase + REG_OFFSET_BULK_IN);
283 pr_debug("%lu:%2x ", i, dev->r_buf[i]);
290 *ppos = min_bytes_to_read;
291 if (copy_to_user(buf, dev->r_buf, min_bytes_to_read))
294 rc = wait_for_bulk_in_ready(dev);
296 DEBUGP(5, dev, "wait_for_bulk_in_ready rc=%.2x\n", rc);
297 DEBUGP(2, dev, "<- cm4040_read (failed)\n");
298 if (rc == -ERESTARTSYS)
303 rc = write_sync_reg(SCR_READER_TO_HOST_DONE, dev);
305 DEBUGP(5, dev, "write_sync_reg c=%.2x\n", rc);
306 DEBUGP(2, dev, "<- cm4040_read (failed)\n");
307 if (rc == -ERESTARTSYS)
313 uc = xinb(iobase + REG_OFFSET_BULK_IN);
315 DEBUGP(2, dev, "<- cm4040_read (successfully)\n");
316 return min_bytes_to_read;
319 static ssize_t cm4040_write(struct file *filp, const char __user *buf,
320 size_t count, loff_t *ppos)
322 struct reader_dev *dev = filp->private_data;
323 int iobase = dev->p_dev->resource[0]->start;
326 unsigned int bytes_to_write;
328 DEBUGP(2, dev, "-> cm4040_write(%s,%d)\n", current->comm, current->pid);
331 DEBUGP(2, dev, "<- cm4040_write empty read (successfully)\n");
335 if ((count < 5) || (count > READ_WRITE_BUFFER_SIZE)) {
336 DEBUGP(2, dev, "<- cm4040_write buffersize=%Zd < 5\n", count);
340 if (filp->f_flags & O_NONBLOCK) {
341 DEBUGP(4, dev, "filep->f_flags O_NONBLOCK set\n");
342 DEBUGP(4, dev, "<- cm4040_write (failure)\n");
346 if (!pcmcia_dev_present(dev->p_dev))
349 bytes_to_write = count;
350 if (copy_from_user(dev->s_buf, buf, bytes_to_write))
353 switch (dev->s_buf[0]) {
354 case CMD_PC_TO_RDR_XFRBLOCK:
355 case CMD_PC_TO_RDR_SECURE:
356 case CMD_PC_TO_RDR_TEST_SECURE:
357 case CMD_PC_TO_RDR_OK_SECURE:
358 dev->timeout = CCID_DRIVER_BULK_DEFAULT_TIMEOUT;
361 case CMD_PC_TO_RDR_ICCPOWERON:
362 dev->timeout = CCID_DRIVER_ASYNC_POWERUP_TIMEOUT;
365 case CMD_PC_TO_RDR_GETSLOTSTATUS:
366 case CMD_PC_TO_RDR_ICCPOWEROFF:
367 case CMD_PC_TO_RDR_GETPARAMETERS:
368 case CMD_PC_TO_RDR_RESETPARAMETERS:
369 case CMD_PC_TO_RDR_SETPARAMETERS:
370 case CMD_PC_TO_RDR_ESCAPE:
371 case CMD_PC_TO_RDR_ICCCLOCK:
373 dev->timeout = CCID_DRIVER_MINIMUM_TIMEOUT;
377 rc = write_sync_reg(SCR_HOST_TO_READER_START, dev);
379 DEBUGP(5, dev, "write_sync_reg c=%.2Zx\n", rc);
380 DEBUGP(2, dev, "<- cm4040_write (failed)\n");
381 if (rc == -ERESTARTSYS)
387 DEBUGP(4, dev, "start \n");
389 for (i = 0; i < bytes_to_write; i++) {
390 rc = wait_for_bulk_out_ready(dev);
392 DEBUGP(5, dev, "wait_for_bulk_out_ready rc=%.2Zx\n",
394 DEBUGP(2, dev, "<- cm4040_write (failed)\n");
395 if (rc == -ERESTARTSYS)
401 xoutb(dev->s_buf[i],iobase + REG_OFFSET_BULK_OUT);
403 DEBUGP(4, dev, "end\n");
405 rc = write_sync_reg(SCR_HOST_TO_READER_DONE, dev);
408 DEBUGP(5, dev, "write_sync_reg c=%.2Zx\n", rc);
409 DEBUGP(2, dev, "<- cm4040_write (failed)\n");
410 if (rc == -ERESTARTSYS)
416 DEBUGP(2, dev, "<- cm4040_write (successfully)\n");
420 static unsigned int cm4040_poll(struct file *filp, poll_table *wait)
422 struct reader_dev *dev = filp->private_data;
423 unsigned int mask = 0;
425 poll_wait(filp, &dev->poll_wait, wait);
427 if (test_and_clear_bit(BS_READABLE, &dev->buffer_status))
428 mask |= POLLIN | POLLRDNORM;
429 if (test_and_clear_bit(BS_WRITABLE, &dev->buffer_status))
430 mask |= POLLOUT | POLLWRNORM;
432 DEBUGP(2, dev, "<- cm4040_poll(%u)\n", mask);
437 static int cm4040_open(struct inode *inode, struct file *filp)
439 struct reader_dev *dev;
440 struct pcmcia_device *link;
441 int minor = iminor(inode);
444 if (minor >= CM_MAX_DEV)
448 link = dev_table[minor];
449 if (link == NULL || !pcmcia_dev_present(link)) {
460 filp->private_data = dev;
462 if (filp->f_flags & O_NONBLOCK) {
463 DEBUGP(4, dev, "filep->f_flags O_NONBLOCK set\n");
470 dev->poll_timer.data = (unsigned long) dev;
471 mod_timer(&dev->poll_timer, jiffies + POLL_PERIOD);
473 DEBUGP(2, dev, "<- cm4040_open (successfully)\n");
474 ret = nonseekable_open(inode, filp);
480 static int cm4040_close(struct inode *inode, struct file *filp)
482 struct reader_dev *dev = filp->private_data;
483 struct pcmcia_device *link;
484 int minor = iminor(inode);
486 DEBUGP(2, dev, "-> cm4040_close(maj/min=%d.%d)\n", imajor(inode),
489 if (minor >= CM_MAX_DEV)
492 link = dev_table[minor];
496 cm4040_stop_poll(dev);
501 DEBUGP(2, dev, "<- cm4040_close\n");
505 static void cm4040_reader_release(struct pcmcia_device *link)
507 struct reader_dev *dev = link->priv;
509 DEBUGP(3, dev, "-> cm4040_reader_release\n");
511 DEBUGP(3, dev, KERN_INFO MODULE_NAME ": delaying release "
512 "until process has terminated\n");
513 wait_event(dev->devq, (link->open == 0));
515 DEBUGP(3, dev, "<- cm4040_reader_release\n");
519 static int cm4040_config_check(struct pcmcia_device *p_dev,
520 cistpl_cftable_entry_t *cfg,
521 cistpl_cftable_entry_t *dflt,
530 p_dev->resource[0]->start = cfg->io.win[0].base;
531 p_dev->resource[0]->end = cfg->io.win[0].len;
532 p_dev->resource[0]->flags |= pcmcia_io_cfg_data_width(cfg->io.flags);
533 p_dev->io_lines = cfg->io.flags & CISTPL_IO_LINES_MASK;
534 rc = pcmcia_request_io(p_dev);
536 dev_printk(KERN_INFO, &p_dev->dev,
537 "pcmcia_request_io returned 0x%x\n", rc);
542 static int reader_config(struct pcmcia_device *link, int devno)
544 struct reader_dev *dev;
547 if (pcmcia_loop_config(link, cm4040_config_check, NULL))
550 link->conf.IntType = 00000002;
552 fail_rc = pcmcia_request_configuration(link, &link->conf);
554 dev_printk(KERN_INFO, &link->dev,
555 "pcmcia_request_configuration failed 0x%x\n",
562 DEBUGP(2, dev, "device " DEVICE_NAME "%d at %pR\n", devno,
564 DEBUGP(2, dev, "<- reader_config (succ)\n");
569 reader_release(link);
573 static void reader_release(struct pcmcia_device *link)
575 cm4040_reader_release(link);
576 pcmcia_disable_device(link);
579 static int reader_probe(struct pcmcia_device *link)
581 struct reader_dev *dev;
584 for (i = 0; i < CM_MAX_DEV; i++) {
585 if (dev_table[i] == NULL)
592 dev = kzalloc(sizeof(struct reader_dev), GFP_KERNEL);
596 dev->timeout = CCID_DRIVER_MINIMUM_TIMEOUT;
597 dev->buffer_status = 0;
602 link->conf.IntType = INT_MEMORY_AND_IO;
605 init_waitqueue_head(&dev->devq);
606 init_waitqueue_head(&dev->poll_wait);
607 init_waitqueue_head(&dev->read_wait);
608 init_waitqueue_head(&dev->write_wait);
609 setup_timer(&dev->poll_timer, cm4040_do_poll, 0);
611 ret = reader_config(link, i);
618 device_create(cmx_class, NULL, MKDEV(major, i), NULL, "cmx%d", i);
623 static void reader_detach(struct pcmcia_device *link)
625 struct reader_dev *dev = link->priv;
629 for (devno = 0; devno < CM_MAX_DEV; devno++) {
630 if (dev_table[devno] == link)
633 if (devno == CM_MAX_DEV)
636 reader_release(link);
638 dev_table[devno] = NULL;
641 device_destroy(cmx_class, MKDEV(major, devno));
646 static const struct file_operations reader_fops = {
647 .owner = THIS_MODULE,
649 .write = cm4040_write,
651 .release = cm4040_close,
656 static struct pcmcia_device_id cm4040_ids[] = {
657 PCMCIA_DEVICE_MANF_CARD(0x0223, 0x0200),
658 PCMCIA_DEVICE_PROD_ID12("OMNIKEY", "CardMan 4040",
659 0xE32CDD8C, 0x8F23318B),
662 MODULE_DEVICE_TABLE(pcmcia, cm4040_ids);
664 static struct pcmcia_driver reader_driver = {
665 .owner = THIS_MODULE,
669 .probe = reader_probe,
670 .remove = reader_detach,
671 .id_table = cm4040_ids,
674 static int __init cm4040_init(void)
678 printk(KERN_INFO "%s\n", version);
679 cmx_class = class_create(THIS_MODULE, "cardman_4040");
680 if (IS_ERR(cmx_class))
681 return PTR_ERR(cmx_class);
683 major = register_chrdev(0, DEVICE_NAME, &reader_fops);
685 printk(KERN_WARNING MODULE_NAME
686 ": could not get major number\n");
687 class_destroy(cmx_class);
691 rc = pcmcia_register_driver(&reader_driver);
693 unregister_chrdev(major, DEVICE_NAME);
694 class_destroy(cmx_class);
701 static void __exit cm4040_exit(void)
703 printk(KERN_INFO MODULE_NAME ": unloading\n");
704 pcmcia_unregister_driver(&reader_driver);
705 unregister_chrdev(major, DEVICE_NAME);
706 class_destroy(cmx_class);
709 module_init(cm4040_init);
710 module_exit(cm4040_exit);
711 MODULE_LICENSE("Dual BSD/GPL");