Linux 2.6.34-rc3
[pohmelfs.git] / drivers / char / pcmcia / cm4040_cs.c
bloba6a70e476beaf4babb7446643775d92ed5866635
1 /*
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()
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
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>
24 #include <linux/fs.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>
30 #include <asm/io.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"
42 #define reader_to_dev(x) (&x->p_dev->dev)
44 /* n (debug level) is ignored */
45 /* additional debug output may be enabled by re-compiling with
46 * CM4040_DEBUG set */
47 /* #define CM4040_DEBUG */
48 #define DEBUGP(n, rdr, x, args...) do { \
49 dev_dbg(reader_to_dev(rdr), "%s:" x, \
50 __func__ , ## args); \
51 } while (0)
53 static char *version =
54 "OMNIKEY CardMan 4040 v1.1.0gm5 - All bugs added by Harald Welte";
56 #define CCID_DRIVER_BULK_DEFAULT_TIMEOUT (150*HZ)
57 #define CCID_DRIVER_ASYNC_POWERUP_TIMEOUT (35*HZ)
58 #define CCID_DRIVER_MINIMUM_TIMEOUT (3*HZ)
59 #define READ_WRITE_BUFFER_SIZE 512
60 #define POLL_LOOP_COUNT 1000
62 /* how often to poll for fifo status change */
63 #define POLL_PERIOD msecs_to_jiffies(10)
65 static void reader_release(struct pcmcia_device *link);
67 static int major;
68 static struct class *cmx_class;
70 #define BS_READABLE 0x01
71 #define BS_WRITABLE 0x02
73 struct reader_dev {
74 struct pcmcia_device *p_dev;
75 dev_node_t node;
76 wait_queue_head_t devq;
77 wait_queue_head_t poll_wait;
78 wait_queue_head_t read_wait;
79 wait_queue_head_t write_wait;
80 unsigned long buffer_status;
81 unsigned long timeout;
82 unsigned char s_buf[READ_WRITE_BUFFER_SIZE];
83 unsigned char r_buf[READ_WRITE_BUFFER_SIZE];
84 struct timer_list poll_timer;
87 static struct pcmcia_device *dev_table[CM_MAX_DEV];
89 #ifndef CM4040_DEBUG
90 #define xoutb outb
91 #define xinb inb
92 #else
93 static inline void xoutb(unsigned char val, unsigned short port)
95 pr_debug("outb(val=%.2x,port=%.4x)\n", val, port);
96 outb(val, port);
99 static inline unsigned char xinb(unsigned short port)
101 unsigned char val;
103 val = inb(port);
104 pr_debug("%.2x=inb(%.4x)\n", val, port);
105 return val;
107 #endif
109 /* poll the device fifo status register. not to be confused with
110 * the poll syscall. */
111 static void cm4040_do_poll(unsigned long dummy)
113 struct reader_dev *dev = (struct reader_dev *) dummy;
114 unsigned int obs = xinb(dev->p_dev->io.BasePort1
115 + REG_OFFSET_BUFFER_STATUS);
117 if ((obs & BSR_BULK_IN_FULL)) {
118 set_bit(BS_READABLE, &dev->buffer_status);
119 DEBUGP(4, dev, "waking up read_wait\n");
120 wake_up_interruptible(&dev->read_wait);
121 } else
122 clear_bit(BS_READABLE, &dev->buffer_status);
124 if (!(obs & BSR_BULK_OUT_FULL)) {
125 set_bit(BS_WRITABLE, &dev->buffer_status);
126 DEBUGP(4, dev, "waking up write_wait\n");
127 wake_up_interruptible(&dev->write_wait);
128 } else
129 clear_bit(BS_WRITABLE, &dev->buffer_status);
131 if (dev->buffer_status)
132 wake_up_interruptible(&dev->poll_wait);
134 mod_timer(&dev->poll_timer, jiffies + POLL_PERIOD);
137 static void cm4040_stop_poll(struct reader_dev *dev)
139 del_timer_sync(&dev->poll_timer);
142 static int wait_for_bulk_out_ready(struct reader_dev *dev)
144 int i, rc;
145 int iobase = dev->p_dev->io.BasePort1;
147 for (i = 0; i < POLL_LOOP_COUNT; i++) {
148 if ((xinb(iobase + REG_OFFSET_BUFFER_STATUS)
149 & BSR_BULK_OUT_FULL) == 0) {
150 DEBUGP(4, dev, "BulkOut empty (i=%d)\n", i);
151 return 1;
155 DEBUGP(4, dev, "wait_event_interruptible_timeout(timeout=%ld\n",
156 dev->timeout);
157 rc = wait_event_interruptible_timeout(dev->write_wait,
158 test_and_clear_bit(BS_WRITABLE,
159 &dev->buffer_status),
160 dev->timeout);
162 if (rc > 0)
163 DEBUGP(4, dev, "woke up: BulkOut empty\n");
164 else if (rc == 0)
165 DEBUGP(4, dev, "woke up: BulkOut full, returning 0 :(\n");
166 else if (rc < 0)
167 DEBUGP(4, dev, "woke up: signal arrived\n");
169 return rc;
172 /* Write to Sync Control Register */
173 static int write_sync_reg(unsigned char val, struct reader_dev *dev)
175 int iobase = dev->p_dev->io.BasePort1;
176 int rc;
178 rc = wait_for_bulk_out_ready(dev);
179 if (rc <= 0)
180 return rc;
182 xoutb(val, iobase + REG_OFFSET_SYNC_CONTROL);
183 rc = wait_for_bulk_out_ready(dev);
184 if (rc <= 0)
185 return rc;
187 return 1;
190 static int wait_for_bulk_in_ready(struct reader_dev *dev)
192 int i, rc;
193 int iobase = dev->p_dev->io.BasePort1;
195 for (i = 0; i < POLL_LOOP_COUNT; i++) {
196 if ((xinb(iobase + REG_OFFSET_BUFFER_STATUS)
197 & BSR_BULK_IN_FULL) == BSR_BULK_IN_FULL) {
198 DEBUGP(3, dev, "BulkIn full (i=%d)\n", i);
199 return 1;
203 DEBUGP(4, dev, "wait_event_interruptible_timeout(timeout=%ld\n",
204 dev->timeout);
205 rc = wait_event_interruptible_timeout(dev->read_wait,
206 test_and_clear_bit(BS_READABLE,
207 &dev->buffer_status),
208 dev->timeout);
209 if (rc > 0)
210 DEBUGP(4, dev, "woke up: BulkIn full\n");
211 else if (rc == 0)
212 DEBUGP(4, dev, "woke up: BulkIn not full, returning 0 :(\n");
213 else if (rc < 0)
214 DEBUGP(4, dev, "woke up: signal arrived\n");
216 return rc;
219 static ssize_t cm4040_read(struct file *filp, char __user *buf,
220 size_t count, loff_t *ppos)
222 struct reader_dev *dev = filp->private_data;
223 int iobase = dev->p_dev->io.BasePort1;
224 size_t bytes_to_read;
225 unsigned long i;
226 size_t min_bytes_to_read;
227 int rc;
228 unsigned char uc;
230 DEBUGP(2, dev, "-> cm4040_read(%s,%d)\n", current->comm, current->pid);
232 if (count == 0)
233 return 0;
235 if (count < 10)
236 return -EFAULT;
238 if (filp->f_flags & O_NONBLOCK) {
239 DEBUGP(4, dev, "filep->f_flags O_NONBLOCK set\n");
240 DEBUGP(2, dev, "<- cm4040_read (failure)\n");
241 return -EAGAIN;
244 if (!pcmcia_dev_present(dev->p_dev))
245 return -ENODEV;
247 for (i = 0; i < 5; i++) {
248 rc = wait_for_bulk_in_ready(dev);
249 if (rc <= 0) {
250 DEBUGP(5, dev, "wait_for_bulk_in_ready rc=%.2x\n", rc);
251 DEBUGP(2, dev, "<- cm4040_read (failed)\n");
252 if (rc == -ERESTARTSYS)
253 return rc;
254 return -EIO;
256 dev->r_buf[i] = xinb(iobase + REG_OFFSET_BULK_IN);
257 #ifdef CM4040_DEBUG
258 pr_debug("%lu:%2x ", i, dev->r_buf[i]);
260 pr_debug("\n");
261 #else
263 #endif
265 bytes_to_read = 5 + le32_to_cpu(*(__le32 *)&dev->r_buf[1]);
267 DEBUGP(6, dev, "BytesToRead=%zu\n", bytes_to_read);
269 min_bytes_to_read = min(count, bytes_to_read + 5);
270 min_bytes_to_read = min_t(size_t, min_bytes_to_read, READ_WRITE_BUFFER_SIZE);
272 DEBUGP(6, dev, "Min=%zu\n", min_bytes_to_read);
274 for (i = 0; i < (min_bytes_to_read-5); i++) {
275 rc = wait_for_bulk_in_ready(dev);
276 if (rc <= 0) {
277 DEBUGP(5, dev, "wait_for_bulk_in_ready rc=%.2x\n", rc);
278 DEBUGP(2, dev, "<- cm4040_read (failed)\n");
279 if (rc == -ERESTARTSYS)
280 return rc;
281 return -EIO;
283 dev->r_buf[i+5] = xinb(iobase + REG_OFFSET_BULK_IN);
284 #ifdef CM4040_DEBUG
285 pr_debug("%lu:%2x ", i, dev->r_buf[i]);
287 pr_debug("\n");
288 #else
290 #endif
292 *ppos = min_bytes_to_read;
293 if (copy_to_user(buf, dev->r_buf, min_bytes_to_read))
294 return -EFAULT;
296 rc = wait_for_bulk_in_ready(dev);
297 if (rc <= 0) {
298 DEBUGP(5, dev, "wait_for_bulk_in_ready rc=%.2x\n", rc);
299 DEBUGP(2, dev, "<- cm4040_read (failed)\n");
300 if (rc == -ERESTARTSYS)
301 return rc;
302 return -EIO;
305 rc = write_sync_reg(SCR_READER_TO_HOST_DONE, dev);
306 if (rc <= 0) {
307 DEBUGP(5, dev, "write_sync_reg c=%.2x\n", rc);
308 DEBUGP(2, dev, "<- cm4040_read (failed)\n");
309 if (rc == -ERESTARTSYS)
310 return rc;
311 else
312 return -EIO;
315 uc = xinb(iobase + REG_OFFSET_BULK_IN);
317 DEBUGP(2, dev, "<- cm4040_read (successfully)\n");
318 return min_bytes_to_read;
321 static ssize_t cm4040_write(struct file *filp, const char __user *buf,
322 size_t count, loff_t *ppos)
324 struct reader_dev *dev = filp->private_data;
325 int iobase = dev->p_dev->io.BasePort1;
326 ssize_t rc;
327 int i;
328 unsigned int bytes_to_write;
330 DEBUGP(2, dev, "-> cm4040_write(%s,%d)\n", current->comm, current->pid);
332 if (count == 0) {
333 DEBUGP(2, dev, "<- cm4040_write empty read (successfully)\n");
334 return 0;
337 if ((count < 5) || (count > READ_WRITE_BUFFER_SIZE)) {
338 DEBUGP(2, dev, "<- cm4040_write buffersize=%Zd < 5\n", count);
339 return -EIO;
342 if (filp->f_flags & O_NONBLOCK) {
343 DEBUGP(4, dev, "filep->f_flags O_NONBLOCK set\n");
344 DEBUGP(4, dev, "<- cm4040_write (failure)\n");
345 return -EAGAIN;
348 if (!pcmcia_dev_present(dev->p_dev))
349 return -ENODEV;
351 bytes_to_write = count;
352 if (copy_from_user(dev->s_buf, buf, bytes_to_write))
353 return -EFAULT;
355 switch (dev->s_buf[0]) {
356 case CMD_PC_TO_RDR_XFRBLOCK:
357 case CMD_PC_TO_RDR_SECURE:
358 case CMD_PC_TO_RDR_TEST_SECURE:
359 case CMD_PC_TO_RDR_OK_SECURE:
360 dev->timeout = CCID_DRIVER_BULK_DEFAULT_TIMEOUT;
361 break;
363 case CMD_PC_TO_RDR_ICCPOWERON:
364 dev->timeout = CCID_DRIVER_ASYNC_POWERUP_TIMEOUT;
365 break;
367 case CMD_PC_TO_RDR_GETSLOTSTATUS:
368 case CMD_PC_TO_RDR_ICCPOWEROFF:
369 case CMD_PC_TO_RDR_GETPARAMETERS:
370 case CMD_PC_TO_RDR_RESETPARAMETERS:
371 case CMD_PC_TO_RDR_SETPARAMETERS:
372 case CMD_PC_TO_RDR_ESCAPE:
373 case CMD_PC_TO_RDR_ICCCLOCK:
374 default:
375 dev->timeout = CCID_DRIVER_MINIMUM_TIMEOUT;
376 break;
379 rc = write_sync_reg(SCR_HOST_TO_READER_START, dev);
380 if (rc <= 0) {
381 DEBUGP(5, dev, "write_sync_reg c=%.2Zx\n", rc);
382 DEBUGP(2, dev, "<- cm4040_write (failed)\n");
383 if (rc == -ERESTARTSYS)
384 return rc;
385 else
386 return -EIO;
389 DEBUGP(4, dev, "start \n");
391 for (i = 0; i < bytes_to_write; i++) {
392 rc = wait_for_bulk_out_ready(dev);
393 if (rc <= 0) {
394 DEBUGP(5, dev, "wait_for_bulk_out_ready rc=%.2Zx\n",
395 rc);
396 DEBUGP(2, dev, "<- cm4040_write (failed)\n");
397 if (rc == -ERESTARTSYS)
398 return rc;
399 else
400 return -EIO;
403 xoutb(dev->s_buf[i],iobase + REG_OFFSET_BULK_OUT);
405 DEBUGP(4, dev, "end\n");
407 rc = write_sync_reg(SCR_HOST_TO_READER_DONE, dev);
409 if (rc <= 0) {
410 DEBUGP(5, dev, "write_sync_reg c=%.2Zx\n", rc);
411 DEBUGP(2, dev, "<- cm4040_write (failed)\n");
412 if (rc == -ERESTARTSYS)
413 return rc;
414 else
415 return -EIO;
418 DEBUGP(2, dev, "<- cm4040_write (successfully)\n");
419 return count;
422 static unsigned int cm4040_poll(struct file *filp, poll_table *wait)
424 struct reader_dev *dev = filp->private_data;
425 unsigned int mask = 0;
427 poll_wait(filp, &dev->poll_wait, wait);
429 if (test_and_clear_bit(BS_READABLE, &dev->buffer_status))
430 mask |= POLLIN | POLLRDNORM;
431 if (test_and_clear_bit(BS_WRITABLE, &dev->buffer_status))
432 mask |= POLLOUT | POLLWRNORM;
434 DEBUGP(2, dev, "<- cm4040_poll(%u)\n", mask);
436 return mask;
439 static int cm4040_open(struct inode *inode, struct file *filp)
441 struct reader_dev *dev;
442 struct pcmcia_device *link;
443 int minor = iminor(inode);
444 int ret;
446 if (minor >= CM_MAX_DEV)
447 return -ENODEV;
449 lock_kernel();
450 link = dev_table[minor];
451 if (link == NULL || !pcmcia_dev_present(link)) {
452 ret = -ENODEV;
453 goto out;
456 if (link->open) {
457 ret = -EBUSY;
458 goto out;
461 dev = link->priv;
462 filp->private_data = dev;
464 if (filp->f_flags & O_NONBLOCK) {
465 DEBUGP(4, dev, "filep->f_flags O_NONBLOCK set\n");
466 ret = -EAGAIN;
467 goto out;
470 link->open = 1;
472 dev->poll_timer.data = (unsigned long) dev;
473 mod_timer(&dev->poll_timer, jiffies + POLL_PERIOD);
475 DEBUGP(2, dev, "<- cm4040_open (successfully)\n");
476 ret = nonseekable_open(inode, filp);
477 out:
478 unlock_kernel();
479 return ret;
482 static int cm4040_close(struct inode *inode, struct file *filp)
484 struct reader_dev *dev = filp->private_data;
485 struct pcmcia_device *link;
486 int minor = iminor(inode);
488 DEBUGP(2, dev, "-> cm4040_close(maj/min=%d.%d)\n", imajor(inode),
489 iminor(inode));
491 if (minor >= CM_MAX_DEV)
492 return -ENODEV;
494 link = dev_table[minor];
495 if (link == NULL)
496 return -ENODEV;
498 cm4040_stop_poll(dev);
500 link->open = 0;
501 wake_up(&dev->devq);
503 DEBUGP(2, dev, "<- cm4040_close\n");
504 return 0;
507 static void cm4040_reader_release(struct pcmcia_device *link)
509 struct reader_dev *dev = link->priv;
511 DEBUGP(3, dev, "-> cm4040_reader_release\n");
512 while (link->open) {
513 DEBUGP(3, dev, KERN_INFO MODULE_NAME ": delaying release "
514 "until process has terminated\n");
515 wait_event(dev->devq, (link->open == 0));
517 DEBUGP(3, dev, "<- cm4040_reader_release\n");
518 return;
521 static int cm4040_config_check(struct pcmcia_device *p_dev,
522 cistpl_cftable_entry_t *cfg,
523 cistpl_cftable_entry_t *dflt,
524 unsigned int vcc,
525 void *priv_data)
527 int rc;
528 if (!cfg->io.nwin)
529 return -ENODEV;
531 /* Get the IOaddr */
532 p_dev->io.BasePort1 = cfg->io.win[0].base;
533 p_dev->io.NumPorts1 = cfg->io.win[0].len;
534 p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
535 if (!(cfg->io.flags & CISTPL_IO_8BIT))
536 p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_16;
537 if (!(cfg->io.flags & CISTPL_IO_16BIT))
538 p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
539 p_dev->io.IOAddrLines = cfg->io.flags & CISTPL_IO_LINES_MASK;
541 rc = pcmcia_request_io(p_dev, &p_dev->io);
542 dev_printk(KERN_INFO, &p_dev->dev,
543 "pcmcia_request_io returned 0x%x\n", rc);
544 return rc;
548 static int reader_config(struct pcmcia_device *link, int devno)
550 struct reader_dev *dev;
551 int fail_rc;
553 link->io.BasePort2 = 0;
554 link->io.NumPorts2 = 0;
555 link->io.Attributes2 = 0;
557 if (pcmcia_loop_config(link, cm4040_config_check, NULL))
558 goto cs_release;
560 link->conf.IntType = 00000002;
562 fail_rc = pcmcia_request_configuration(link, &link->conf);
563 if (fail_rc != 0) {
564 dev_printk(KERN_INFO, &link->dev,
565 "pcmcia_request_configuration failed 0x%x\n",
566 fail_rc);
567 goto cs_release;
570 dev = link->priv;
571 sprintf(dev->node.dev_name, DEVICE_NAME "%d", devno);
572 dev->node.major = major;
573 dev->node.minor = devno;
574 dev->node.next = &dev->node;
576 DEBUGP(2, dev, "device " DEVICE_NAME "%d at 0x%.4x-0x%.4x\n", devno,
577 link->io.BasePort1, link->io.BasePort1+link->io.NumPorts1);
578 DEBUGP(2, dev, "<- reader_config (succ)\n");
580 return 0;
582 cs_release:
583 reader_release(link);
584 return -ENODEV;
587 static void reader_release(struct pcmcia_device *link)
589 cm4040_reader_release(link);
590 pcmcia_disable_device(link);
593 static int reader_probe(struct pcmcia_device *link)
595 struct reader_dev *dev;
596 int i, ret;
598 for (i = 0; i < CM_MAX_DEV; i++) {
599 if (dev_table[i] == NULL)
600 break;
603 if (i == CM_MAX_DEV)
604 return -ENODEV;
606 dev = kzalloc(sizeof(struct reader_dev), GFP_KERNEL);
607 if (dev == NULL)
608 return -ENOMEM;
610 dev->timeout = CCID_DRIVER_MINIMUM_TIMEOUT;
611 dev->buffer_status = 0;
613 link->priv = dev;
614 dev->p_dev = link;
616 link->conf.IntType = INT_MEMORY_AND_IO;
617 dev_table[i] = link;
619 init_waitqueue_head(&dev->devq);
620 init_waitqueue_head(&dev->poll_wait);
621 init_waitqueue_head(&dev->read_wait);
622 init_waitqueue_head(&dev->write_wait);
623 setup_timer(&dev->poll_timer, cm4040_do_poll, 0);
625 ret = reader_config(link, i);
626 if (ret) {
627 dev_table[i] = NULL;
628 kfree(dev);
629 return ret;
632 device_create(cmx_class, NULL, MKDEV(major, i), NULL, "cmx%d", i);
634 return 0;
637 static void reader_detach(struct pcmcia_device *link)
639 struct reader_dev *dev = link->priv;
640 int devno;
642 /* find device */
643 for (devno = 0; devno < CM_MAX_DEV; devno++) {
644 if (dev_table[devno] == link)
645 break;
647 if (devno == CM_MAX_DEV)
648 return;
650 reader_release(link);
652 dev_table[devno] = NULL;
653 kfree(dev);
655 device_destroy(cmx_class, MKDEV(major, devno));
657 return;
660 static const struct file_operations reader_fops = {
661 .owner = THIS_MODULE,
662 .read = cm4040_read,
663 .write = cm4040_write,
664 .open = cm4040_open,
665 .release = cm4040_close,
666 .poll = cm4040_poll,
669 static struct pcmcia_device_id cm4040_ids[] = {
670 PCMCIA_DEVICE_MANF_CARD(0x0223, 0x0200),
671 PCMCIA_DEVICE_PROD_ID12("OMNIKEY", "CardMan 4040",
672 0xE32CDD8C, 0x8F23318B),
673 PCMCIA_DEVICE_NULL,
675 MODULE_DEVICE_TABLE(pcmcia, cm4040_ids);
677 static struct pcmcia_driver reader_driver = {
678 .owner = THIS_MODULE,
679 .drv = {
680 .name = "cm4040_cs",
682 .probe = reader_probe,
683 .remove = reader_detach,
684 .id_table = cm4040_ids,
687 static int __init cm4040_init(void)
689 int rc;
691 printk(KERN_INFO "%s\n", version);
692 cmx_class = class_create(THIS_MODULE, "cardman_4040");
693 if (IS_ERR(cmx_class))
694 return PTR_ERR(cmx_class);
696 major = register_chrdev(0, DEVICE_NAME, &reader_fops);
697 if (major < 0) {
698 printk(KERN_WARNING MODULE_NAME
699 ": could not get major number\n");
700 class_destroy(cmx_class);
701 return major;
704 rc = pcmcia_register_driver(&reader_driver);
705 if (rc < 0) {
706 unregister_chrdev(major, DEVICE_NAME);
707 class_destroy(cmx_class);
708 return rc;
711 return 0;
714 static void __exit cm4040_exit(void)
716 printk(KERN_INFO MODULE_NAME ": unloading\n");
717 pcmcia_unregister_driver(&reader_driver);
718 unregister_chrdev(major, DEVICE_NAME);
719 class_destroy(cmx_class);
722 module_init(cm4040_init);
723 module_exit(cm4040_exit);
724 MODULE_LICENSE("Dual BSD/GPL");