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/mutex.h>
28 #include <linux/wait.h>
29 #include <asm/uaccess.h>
32 #include <pcmcia/cistpl.h>
33 #include <pcmcia/cisreg.h>
34 #include <pcmcia/ciscode.h>
35 #include <pcmcia/ds.h>
37 #include "cm4040_cs.h"
40 #define reader_to_dev(x) (&x->p_dev->dev)
42 /* n (debug level) is ignored */
43 /* additional debug output may be enabled by re-compiling with
45 /* #define CM4040_DEBUG */
46 #define DEBUGP(n, rdr, x, args...) do { \
47 dev_dbg(reader_to_dev(rdr), "%s:" x, \
48 __func__ , ## args); \
51 static DEFINE_MUTEX(cm4040_mutex);
53 #define CCID_DRIVER_BULK_DEFAULT_TIMEOUT (150*HZ)
54 #define CCID_DRIVER_ASYNC_POWERUP_TIMEOUT (35*HZ)
55 #define CCID_DRIVER_MINIMUM_TIMEOUT (3*HZ)
56 #define READ_WRITE_BUFFER_SIZE 512
57 #define POLL_LOOP_COUNT 1000
59 /* how often to poll for fifo status change */
60 #define POLL_PERIOD msecs_to_jiffies(10)
62 static void reader_release(struct pcmcia_device *link);
65 static struct class *cmx_class;
67 #define BS_READABLE 0x01
68 #define BS_WRITABLE 0x02
71 struct pcmcia_device *p_dev;
72 wait_queue_head_t devq;
73 wait_queue_head_t poll_wait;
74 wait_queue_head_t read_wait;
75 wait_queue_head_t write_wait;
76 unsigned long buffer_status;
77 unsigned long timeout;
78 unsigned char s_buf[READ_WRITE_BUFFER_SIZE];
79 unsigned char r_buf[READ_WRITE_BUFFER_SIZE];
80 struct timer_list poll_timer;
83 static struct pcmcia_device *dev_table[CM_MAX_DEV];
89 static inline void xoutb(unsigned char val, unsigned short port)
91 pr_debug("outb(val=%.2x,port=%.4x)\n", val, port);
95 static inline unsigned char xinb(unsigned short port)
100 pr_debug("%.2x=inb(%.4x)\n", val, port);
105 /* poll the device fifo status register. not to be confused with
106 * the poll syscall. */
107 static void cm4040_do_poll(unsigned long dummy)
109 struct reader_dev *dev = (struct reader_dev *) dummy;
110 unsigned int obs = xinb(dev->p_dev->resource[0]->start
111 + REG_OFFSET_BUFFER_STATUS);
113 if ((obs & BSR_BULK_IN_FULL)) {
114 set_bit(BS_READABLE, &dev->buffer_status);
115 DEBUGP(4, dev, "waking up read_wait\n");
116 wake_up_interruptible(&dev->read_wait);
118 clear_bit(BS_READABLE, &dev->buffer_status);
120 if (!(obs & BSR_BULK_OUT_FULL)) {
121 set_bit(BS_WRITABLE, &dev->buffer_status);
122 DEBUGP(4, dev, "waking up write_wait\n");
123 wake_up_interruptible(&dev->write_wait);
125 clear_bit(BS_WRITABLE, &dev->buffer_status);
127 if (dev->buffer_status)
128 wake_up_interruptible(&dev->poll_wait);
130 mod_timer(&dev->poll_timer, jiffies + POLL_PERIOD);
133 static void cm4040_stop_poll(struct reader_dev *dev)
135 del_timer_sync(&dev->poll_timer);
138 static int wait_for_bulk_out_ready(struct reader_dev *dev)
141 int iobase = dev->p_dev->resource[0]->start;
143 for (i = 0; i < POLL_LOOP_COUNT; i++) {
144 if ((xinb(iobase + REG_OFFSET_BUFFER_STATUS)
145 & BSR_BULK_OUT_FULL) == 0) {
146 DEBUGP(4, dev, "BulkOut empty (i=%d)\n", i);
151 DEBUGP(4, dev, "wait_event_interruptible_timeout(timeout=%ld\n",
153 rc = wait_event_interruptible_timeout(dev->write_wait,
154 test_and_clear_bit(BS_WRITABLE,
155 &dev->buffer_status),
159 DEBUGP(4, dev, "woke up: BulkOut empty\n");
161 DEBUGP(4, dev, "woke up: BulkOut full, returning 0 :(\n");
163 DEBUGP(4, dev, "woke up: signal arrived\n");
168 /* Write to Sync Control Register */
169 static int write_sync_reg(unsigned char val, struct reader_dev *dev)
171 int iobase = dev->p_dev->resource[0]->start;
174 rc = wait_for_bulk_out_ready(dev);
178 xoutb(val, iobase + REG_OFFSET_SYNC_CONTROL);
179 rc = wait_for_bulk_out_ready(dev);
186 static int wait_for_bulk_in_ready(struct reader_dev *dev)
189 int iobase = dev->p_dev->resource[0]->start;
191 for (i = 0; i < POLL_LOOP_COUNT; i++) {
192 if ((xinb(iobase + REG_OFFSET_BUFFER_STATUS)
193 & BSR_BULK_IN_FULL) == BSR_BULK_IN_FULL) {
194 DEBUGP(3, dev, "BulkIn full (i=%d)\n", i);
199 DEBUGP(4, dev, "wait_event_interruptible_timeout(timeout=%ld\n",
201 rc = wait_event_interruptible_timeout(dev->read_wait,
202 test_and_clear_bit(BS_READABLE,
203 &dev->buffer_status),
206 DEBUGP(4, dev, "woke up: BulkIn full\n");
208 DEBUGP(4, dev, "woke up: BulkIn not full, returning 0 :(\n");
210 DEBUGP(4, dev, "woke up: signal arrived\n");
215 static ssize_t cm4040_read(struct file *filp, char __user *buf,
216 size_t count, loff_t *ppos)
218 struct reader_dev *dev = filp->private_data;
219 int iobase = dev->p_dev->resource[0]->start;
220 size_t bytes_to_read;
222 size_t min_bytes_to_read;
226 DEBUGP(2, dev, "-> cm4040_read(%s,%d)\n", current->comm, current->pid);
234 if (filp->f_flags & O_NONBLOCK) {
235 DEBUGP(4, dev, "filep->f_flags O_NONBLOCK set\n");
236 DEBUGP(2, dev, "<- cm4040_read (failure)\n");
240 if (!pcmcia_dev_present(dev->p_dev))
243 for (i = 0; i < 5; i++) {
244 rc = wait_for_bulk_in_ready(dev);
246 DEBUGP(5, dev, "wait_for_bulk_in_ready rc=%.2x\n", rc);
247 DEBUGP(2, dev, "<- cm4040_read (failed)\n");
248 if (rc == -ERESTARTSYS)
252 dev->r_buf[i] = xinb(iobase + REG_OFFSET_BULK_IN);
254 pr_debug("%lu:%2x ", i, dev->r_buf[i]);
261 bytes_to_read = 5 + le32_to_cpu(*(__le32 *)&dev->r_buf[1]);
263 DEBUGP(6, dev, "BytesToRead=%zu\n", bytes_to_read);
265 min_bytes_to_read = min(count, bytes_to_read + 5);
266 min_bytes_to_read = min_t(size_t, min_bytes_to_read, READ_WRITE_BUFFER_SIZE);
268 DEBUGP(6, dev, "Min=%zu\n", min_bytes_to_read);
270 for (i = 0; i < (min_bytes_to_read-5); i++) {
271 rc = wait_for_bulk_in_ready(dev);
273 DEBUGP(5, dev, "wait_for_bulk_in_ready rc=%.2x\n", rc);
274 DEBUGP(2, dev, "<- cm4040_read (failed)\n");
275 if (rc == -ERESTARTSYS)
279 dev->r_buf[i+5] = xinb(iobase + REG_OFFSET_BULK_IN);
281 pr_debug("%lu:%2x ", i, dev->r_buf[i]);
288 *ppos = min_bytes_to_read;
289 if (copy_to_user(buf, dev->r_buf, min_bytes_to_read))
292 rc = wait_for_bulk_in_ready(dev);
294 DEBUGP(5, dev, "wait_for_bulk_in_ready rc=%.2x\n", rc);
295 DEBUGP(2, dev, "<- cm4040_read (failed)\n");
296 if (rc == -ERESTARTSYS)
301 rc = write_sync_reg(SCR_READER_TO_HOST_DONE, dev);
303 DEBUGP(5, dev, "write_sync_reg c=%.2x\n", rc);
304 DEBUGP(2, dev, "<- cm4040_read (failed)\n");
305 if (rc == -ERESTARTSYS)
311 uc = xinb(iobase + REG_OFFSET_BULK_IN);
313 DEBUGP(2, dev, "<- cm4040_read (successfully)\n");
314 return min_bytes_to_read;
317 static ssize_t cm4040_write(struct file *filp, const char __user *buf,
318 size_t count, loff_t *ppos)
320 struct reader_dev *dev = filp->private_data;
321 int iobase = dev->p_dev->resource[0]->start;
324 unsigned int bytes_to_write;
326 DEBUGP(2, dev, "-> cm4040_write(%s,%d)\n", current->comm, current->pid);
329 DEBUGP(2, dev, "<- cm4040_write empty read (successfully)\n");
333 if ((count < 5) || (count > READ_WRITE_BUFFER_SIZE)) {
334 DEBUGP(2, dev, "<- cm4040_write buffersize=%Zd < 5\n", count);
338 if (filp->f_flags & O_NONBLOCK) {
339 DEBUGP(4, dev, "filep->f_flags O_NONBLOCK set\n");
340 DEBUGP(4, dev, "<- cm4040_write (failure)\n");
344 if (!pcmcia_dev_present(dev->p_dev))
347 bytes_to_write = count;
348 if (copy_from_user(dev->s_buf, buf, bytes_to_write))
351 switch (dev->s_buf[0]) {
352 case CMD_PC_TO_RDR_XFRBLOCK:
353 case CMD_PC_TO_RDR_SECURE:
354 case CMD_PC_TO_RDR_TEST_SECURE:
355 case CMD_PC_TO_RDR_OK_SECURE:
356 dev->timeout = CCID_DRIVER_BULK_DEFAULT_TIMEOUT;
359 case CMD_PC_TO_RDR_ICCPOWERON:
360 dev->timeout = CCID_DRIVER_ASYNC_POWERUP_TIMEOUT;
363 case CMD_PC_TO_RDR_GETSLOTSTATUS:
364 case CMD_PC_TO_RDR_ICCPOWEROFF:
365 case CMD_PC_TO_RDR_GETPARAMETERS:
366 case CMD_PC_TO_RDR_RESETPARAMETERS:
367 case CMD_PC_TO_RDR_SETPARAMETERS:
368 case CMD_PC_TO_RDR_ESCAPE:
369 case CMD_PC_TO_RDR_ICCCLOCK:
371 dev->timeout = CCID_DRIVER_MINIMUM_TIMEOUT;
375 rc = write_sync_reg(SCR_HOST_TO_READER_START, dev);
377 DEBUGP(5, dev, "write_sync_reg c=%.2Zx\n", rc);
378 DEBUGP(2, dev, "<- cm4040_write (failed)\n");
379 if (rc == -ERESTARTSYS)
385 DEBUGP(4, dev, "start \n");
387 for (i = 0; i < bytes_to_write; i++) {
388 rc = wait_for_bulk_out_ready(dev);
390 DEBUGP(5, dev, "wait_for_bulk_out_ready rc=%.2Zx\n",
392 DEBUGP(2, dev, "<- cm4040_write (failed)\n");
393 if (rc == -ERESTARTSYS)
399 xoutb(dev->s_buf[i],iobase + REG_OFFSET_BULK_OUT);
401 DEBUGP(4, dev, "end\n");
403 rc = write_sync_reg(SCR_HOST_TO_READER_DONE, dev);
406 DEBUGP(5, dev, "write_sync_reg c=%.2Zx\n", rc);
407 DEBUGP(2, dev, "<- cm4040_write (failed)\n");
408 if (rc == -ERESTARTSYS)
414 DEBUGP(2, dev, "<- cm4040_write (successfully)\n");
418 static unsigned int cm4040_poll(struct file *filp, poll_table *wait)
420 struct reader_dev *dev = filp->private_data;
421 unsigned int mask = 0;
423 poll_wait(filp, &dev->poll_wait, wait);
425 if (test_and_clear_bit(BS_READABLE, &dev->buffer_status))
426 mask |= POLLIN | POLLRDNORM;
427 if (test_and_clear_bit(BS_WRITABLE, &dev->buffer_status))
428 mask |= POLLOUT | POLLWRNORM;
430 DEBUGP(2, dev, "<- cm4040_poll(%u)\n", mask);
435 static int cm4040_open(struct inode *inode, struct file *filp)
437 struct reader_dev *dev;
438 struct pcmcia_device *link;
439 int minor = iminor(inode);
442 if (minor >= CM_MAX_DEV)
445 mutex_lock(&cm4040_mutex);
446 link = dev_table[minor];
447 if (link == NULL || !pcmcia_dev_present(link)) {
458 filp->private_data = dev;
460 if (filp->f_flags & O_NONBLOCK) {
461 DEBUGP(4, dev, "filep->f_flags O_NONBLOCK set\n");
468 dev->poll_timer.data = (unsigned long) dev;
469 mod_timer(&dev->poll_timer, jiffies + POLL_PERIOD);
471 DEBUGP(2, dev, "<- cm4040_open (successfully)\n");
472 ret = nonseekable_open(inode, filp);
474 mutex_unlock(&cm4040_mutex);
478 static int cm4040_close(struct inode *inode, struct file *filp)
480 struct reader_dev *dev = filp->private_data;
481 struct pcmcia_device *link;
482 int minor = iminor(inode);
484 DEBUGP(2, dev, "-> cm4040_close(maj/min=%d.%d)\n", imajor(inode),
487 if (minor >= CM_MAX_DEV)
490 link = dev_table[minor];
494 cm4040_stop_poll(dev);
499 DEBUGP(2, dev, "<- cm4040_close\n");
503 static void cm4040_reader_release(struct pcmcia_device *link)
505 struct reader_dev *dev = link->priv;
507 DEBUGP(3, dev, "-> cm4040_reader_release\n");
509 DEBUGP(3, dev, KERN_INFO MODULE_NAME ": delaying release "
510 "until process has terminated\n");
511 wait_event(dev->devq, (link->open == 0));
513 DEBUGP(3, dev, "<- cm4040_reader_release\n");
517 static int cm4040_config_check(struct pcmcia_device *p_dev, void *priv_data)
519 return pcmcia_request_io(p_dev);
523 static int reader_config(struct pcmcia_device *link, int devno)
525 struct reader_dev *dev;
528 link->config_flags |= CONF_AUTO_SET_IO;
530 if (pcmcia_loop_config(link, cm4040_config_check, NULL))
533 fail_rc = pcmcia_enable_device(link);
535 dev_info(&link->dev, "pcmcia_enable_device failed 0x%x\n",
542 DEBUGP(2, dev, "device " DEVICE_NAME "%d at %pR\n", devno,
544 DEBUGP(2, dev, "<- reader_config (succ)\n");
549 reader_release(link);
553 static void reader_release(struct pcmcia_device *link)
555 cm4040_reader_release(link);
556 pcmcia_disable_device(link);
559 static int reader_probe(struct pcmcia_device *link)
561 struct reader_dev *dev;
564 for (i = 0; i < CM_MAX_DEV; i++) {
565 if (dev_table[i] == NULL)
572 dev = kzalloc(sizeof(struct reader_dev), GFP_KERNEL);
576 dev->timeout = CCID_DRIVER_MINIMUM_TIMEOUT;
577 dev->buffer_status = 0;
584 init_waitqueue_head(&dev->devq);
585 init_waitqueue_head(&dev->poll_wait);
586 init_waitqueue_head(&dev->read_wait);
587 init_waitqueue_head(&dev->write_wait);
588 setup_timer(&dev->poll_timer, cm4040_do_poll, 0);
590 ret = reader_config(link, i);
597 device_create(cmx_class, NULL, MKDEV(major, i), NULL, "cmx%d", i);
602 static void reader_detach(struct pcmcia_device *link)
604 struct reader_dev *dev = link->priv;
608 for (devno = 0; devno < CM_MAX_DEV; devno++) {
609 if (dev_table[devno] == link)
612 if (devno == CM_MAX_DEV)
615 reader_release(link);
617 dev_table[devno] = NULL;
620 device_destroy(cmx_class, MKDEV(major, devno));
625 static const struct file_operations reader_fops = {
626 .owner = THIS_MODULE,
628 .write = cm4040_write,
630 .release = cm4040_close,
635 static const struct pcmcia_device_id cm4040_ids[] = {
636 PCMCIA_DEVICE_MANF_CARD(0x0223, 0x0200),
637 PCMCIA_DEVICE_PROD_ID12("OMNIKEY", "CardMan 4040",
638 0xE32CDD8C, 0x8F23318B),
641 MODULE_DEVICE_TABLE(pcmcia, cm4040_ids);
643 static struct pcmcia_driver reader_driver = {
644 .owner = THIS_MODULE,
646 .probe = reader_probe,
647 .remove = reader_detach,
648 .id_table = cm4040_ids,
651 static int __init cm4040_init(void)
655 cmx_class = class_create(THIS_MODULE, "cardman_4040");
656 if (IS_ERR(cmx_class))
657 return PTR_ERR(cmx_class);
659 major = register_chrdev(0, DEVICE_NAME, &reader_fops);
661 printk(KERN_WARNING MODULE_NAME
662 ": could not get major number\n");
663 class_destroy(cmx_class);
667 rc = pcmcia_register_driver(&reader_driver);
669 unregister_chrdev(major, DEVICE_NAME);
670 class_destroy(cmx_class);
677 static void __exit cm4040_exit(void)
679 pcmcia_unregister_driver(&reader_driver);
680 unregister_chrdev(major, DEVICE_NAME);
681 class_destroy(cmx_class);
684 module_init(cm4040_init);
685 module_exit(cm4040_exit);
686 MODULE_LICENSE("Dual BSD/GPL");