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/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 char *version
=
52 "OMNIKEY CardMan 4040 v1.1.0gm5 - All bugs added by Harald Welte";
54 #define CCID_DRIVER_BULK_DEFAULT_TIMEOUT (150*HZ)
55 #define CCID_DRIVER_ASYNC_POWERUP_TIMEOUT (35*HZ)
56 #define CCID_DRIVER_MINIMUM_TIMEOUT (3*HZ)
57 #define READ_WRITE_BUFFER_SIZE 512
58 #define POLL_LOOP_COUNT 1000
60 /* how often to poll for fifo status change */
61 #define POLL_PERIOD msecs_to_jiffies(10)
63 static void reader_release(struct pcmcia_device
*link
);
66 static struct class *cmx_class
;
68 #define BS_READABLE 0x01
69 #define BS_WRITABLE 0x02
72 struct pcmcia_device
*p_dev
;
73 wait_queue_head_t devq
;
74 wait_queue_head_t poll_wait
;
75 wait_queue_head_t read_wait
;
76 wait_queue_head_t write_wait
;
77 unsigned long buffer_status
;
78 unsigned long timeout
;
79 unsigned char s_buf
[READ_WRITE_BUFFER_SIZE
];
80 unsigned char r_buf
[READ_WRITE_BUFFER_SIZE
];
81 struct timer_list poll_timer
;
84 static struct pcmcia_device
*dev_table
[CM_MAX_DEV
];
90 static inline void xoutb(unsigned char val
, unsigned short port
)
92 pr_debug("outb(val=%.2x,port=%.4x)\n", val
, port
);
96 static inline unsigned char xinb(unsigned short port
)
101 pr_debug("%.2x=inb(%.4x)\n", val
, port
);
106 /* poll the device fifo status register. not to be confused with
107 * the poll syscall. */
108 static void cm4040_do_poll(unsigned long dummy
)
110 struct reader_dev
*dev
= (struct reader_dev
*) dummy
;
111 unsigned int obs
= xinb(dev
->p_dev
->resource
[0]->start
112 + REG_OFFSET_BUFFER_STATUS
);
114 if ((obs
& BSR_BULK_IN_FULL
)) {
115 set_bit(BS_READABLE
, &dev
->buffer_status
);
116 DEBUGP(4, dev
, "waking up read_wait\n");
117 wake_up_interruptible(&dev
->read_wait
);
119 clear_bit(BS_READABLE
, &dev
->buffer_status
);
121 if (!(obs
& BSR_BULK_OUT_FULL
)) {
122 set_bit(BS_WRITABLE
, &dev
->buffer_status
);
123 DEBUGP(4, dev
, "waking up write_wait\n");
124 wake_up_interruptible(&dev
->write_wait
);
126 clear_bit(BS_WRITABLE
, &dev
->buffer_status
);
128 if (dev
->buffer_status
)
129 wake_up_interruptible(&dev
->poll_wait
);
131 mod_timer(&dev
->poll_timer
, jiffies
+ POLL_PERIOD
);
134 static void cm4040_stop_poll(struct reader_dev
*dev
)
136 del_timer_sync(&dev
->poll_timer
);
139 static int wait_for_bulk_out_ready(struct reader_dev
*dev
)
142 int iobase
= dev
->p_dev
->resource
[0]->start
;
144 for (i
= 0; i
< POLL_LOOP_COUNT
; i
++) {
145 if ((xinb(iobase
+ REG_OFFSET_BUFFER_STATUS
)
146 & BSR_BULK_OUT_FULL
) == 0) {
147 DEBUGP(4, dev
, "BulkOut empty (i=%d)\n", i
);
152 DEBUGP(4, dev
, "wait_event_interruptible_timeout(timeout=%ld\n",
154 rc
= wait_event_interruptible_timeout(dev
->write_wait
,
155 test_and_clear_bit(BS_WRITABLE
,
156 &dev
->buffer_status
),
160 DEBUGP(4, dev
, "woke up: BulkOut empty\n");
162 DEBUGP(4, dev
, "woke up: BulkOut full, returning 0 :(\n");
164 DEBUGP(4, dev
, "woke up: signal arrived\n");
169 /* Write to Sync Control Register */
170 static int write_sync_reg(unsigned char val
, struct reader_dev
*dev
)
172 int iobase
= dev
->p_dev
->resource
[0]->start
;
175 rc
= wait_for_bulk_out_ready(dev
);
179 xoutb(val
, iobase
+ REG_OFFSET_SYNC_CONTROL
);
180 rc
= wait_for_bulk_out_ready(dev
);
187 static int wait_for_bulk_in_ready(struct reader_dev
*dev
)
190 int iobase
= dev
->p_dev
->resource
[0]->start
;
192 for (i
= 0; i
< POLL_LOOP_COUNT
; i
++) {
193 if ((xinb(iobase
+ REG_OFFSET_BUFFER_STATUS
)
194 & BSR_BULK_IN_FULL
) == BSR_BULK_IN_FULL
) {
195 DEBUGP(3, dev
, "BulkIn full (i=%d)\n", i
);
200 DEBUGP(4, dev
, "wait_event_interruptible_timeout(timeout=%ld\n",
202 rc
= wait_event_interruptible_timeout(dev
->read_wait
,
203 test_and_clear_bit(BS_READABLE
,
204 &dev
->buffer_status
),
207 DEBUGP(4, dev
, "woke up: BulkIn full\n");
209 DEBUGP(4, dev
, "woke up: BulkIn not full, returning 0 :(\n");
211 DEBUGP(4, dev
, "woke up: signal arrived\n");
216 static ssize_t
cm4040_read(struct file
*filp
, char __user
*buf
,
217 size_t count
, loff_t
*ppos
)
219 struct reader_dev
*dev
= filp
->private_data
;
220 int iobase
= dev
->p_dev
->resource
[0]->start
;
221 size_t bytes_to_read
;
223 size_t min_bytes_to_read
;
227 DEBUGP(2, dev
, "-> cm4040_read(%s,%d)\n", current
->comm
, current
->pid
);
235 if (filp
->f_flags
& O_NONBLOCK
) {
236 DEBUGP(4, dev
, "filep->f_flags O_NONBLOCK set\n");
237 DEBUGP(2, dev
, "<- cm4040_read (failure)\n");
241 if (!pcmcia_dev_present(dev
->p_dev
))
244 for (i
= 0; i
< 5; i
++) {
245 rc
= wait_for_bulk_in_ready(dev
);
247 DEBUGP(5, dev
, "wait_for_bulk_in_ready rc=%.2x\n", rc
);
248 DEBUGP(2, dev
, "<- cm4040_read (failed)\n");
249 if (rc
== -ERESTARTSYS
)
253 dev
->r_buf
[i
] = xinb(iobase
+ REG_OFFSET_BULK_IN
);
255 pr_debug("%lu:%2x ", i
, dev
->r_buf
[i
]);
262 bytes_to_read
= 5 + le32_to_cpu(*(__le32
*)&dev
->r_buf
[1]);
264 DEBUGP(6, dev
, "BytesToRead=%zu\n", bytes_to_read
);
266 min_bytes_to_read
= min(count
, bytes_to_read
+ 5);
267 min_bytes_to_read
= min_t(size_t, min_bytes_to_read
, READ_WRITE_BUFFER_SIZE
);
269 DEBUGP(6, dev
, "Min=%zu\n", min_bytes_to_read
);
271 for (i
= 0; i
< (min_bytes_to_read
-5); i
++) {
272 rc
= wait_for_bulk_in_ready(dev
);
274 DEBUGP(5, dev
, "wait_for_bulk_in_ready rc=%.2x\n", rc
);
275 DEBUGP(2, dev
, "<- cm4040_read (failed)\n");
276 if (rc
== -ERESTARTSYS
)
280 dev
->r_buf
[i
+5] = xinb(iobase
+ REG_OFFSET_BULK_IN
);
282 pr_debug("%lu:%2x ", i
, dev
->r_buf
[i
]);
289 *ppos
= min_bytes_to_read
;
290 if (copy_to_user(buf
, dev
->r_buf
, min_bytes_to_read
))
293 rc
= wait_for_bulk_in_ready(dev
);
295 DEBUGP(5, dev
, "wait_for_bulk_in_ready rc=%.2x\n", rc
);
296 DEBUGP(2, dev
, "<- cm4040_read (failed)\n");
297 if (rc
== -ERESTARTSYS
)
302 rc
= write_sync_reg(SCR_READER_TO_HOST_DONE
, dev
);
304 DEBUGP(5, dev
, "write_sync_reg c=%.2x\n", rc
);
305 DEBUGP(2, dev
, "<- cm4040_read (failed)\n");
306 if (rc
== -ERESTARTSYS
)
312 uc
= xinb(iobase
+ REG_OFFSET_BULK_IN
);
314 DEBUGP(2, dev
, "<- cm4040_read (successfully)\n");
315 return min_bytes_to_read
;
318 static ssize_t
cm4040_write(struct file
*filp
, const char __user
*buf
,
319 size_t count
, loff_t
*ppos
)
321 struct reader_dev
*dev
= filp
->private_data
;
322 int iobase
= dev
->p_dev
->resource
[0]->start
;
325 unsigned int bytes_to_write
;
327 DEBUGP(2, dev
, "-> cm4040_write(%s,%d)\n", current
->comm
, current
->pid
);
330 DEBUGP(2, dev
, "<- cm4040_write empty read (successfully)\n");
334 if ((count
< 5) || (count
> READ_WRITE_BUFFER_SIZE
)) {
335 DEBUGP(2, dev
, "<- cm4040_write buffersize=%Zd < 5\n", count
);
339 if (filp
->f_flags
& O_NONBLOCK
) {
340 DEBUGP(4, dev
, "filep->f_flags O_NONBLOCK set\n");
341 DEBUGP(4, dev
, "<- cm4040_write (failure)\n");
345 if (!pcmcia_dev_present(dev
->p_dev
))
348 bytes_to_write
= count
;
349 if (copy_from_user(dev
->s_buf
, buf
, bytes_to_write
))
352 switch (dev
->s_buf
[0]) {
353 case CMD_PC_TO_RDR_XFRBLOCK
:
354 case CMD_PC_TO_RDR_SECURE
:
355 case CMD_PC_TO_RDR_TEST_SECURE
:
356 case CMD_PC_TO_RDR_OK_SECURE
:
357 dev
->timeout
= CCID_DRIVER_BULK_DEFAULT_TIMEOUT
;
360 case CMD_PC_TO_RDR_ICCPOWERON
:
361 dev
->timeout
= CCID_DRIVER_ASYNC_POWERUP_TIMEOUT
;
364 case CMD_PC_TO_RDR_GETSLOTSTATUS
:
365 case CMD_PC_TO_RDR_ICCPOWEROFF
:
366 case CMD_PC_TO_RDR_GETPARAMETERS
:
367 case CMD_PC_TO_RDR_RESETPARAMETERS
:
368 case CMD_PC_TO_RDR_SETPARAMETERS
:
369 case CMD_PC_TO_RDR_ESCAPE
:
370 case CMD_PC_TO_RDR_ICCCLOCK
:
372 dev
->timeout
= CCID_DRIVER_MINIMUM_TIMEOUT
;
376 rc
= write_sync_reg(SCR_HOST_TO_READER_START
, dev
);
378 DEBUGP(5, dev
, "write_sync_reg c=%.2Zx\n", rc
);
379 DEBUGP(2, dev
, "<- cm4040_write (failed)\n");
380 if (rc
== -ERESTARTSYS
)
386 DEBUGP(4, dev
, "start \n");
388 for (i
= 0; i
< bytes_to_write
; i
++) {
389 rc
= wait_for_bulk_out_ready(dev
);
391 DEBUGP(5, dev
, "wait_for_bulk_out_ready rc=%.2Zx\n",
393 DEBUGP(2, dev
, "<- cm4040_write (failed)\n");
394 if (rc
== -ERESTARTSYS
)
400 xoutb(dev
->s_buf
[i
],iobase
+ REG_OFFSET_BULK_OUT
);
402 DEBUGP(4, dev
, "end\n");
404 rc
= write_sync_reg(SCR_HOST_TO_READER_DONE
, dev
);
407 DEBUGP(5, dev
, "write_sync_reg c=%.2Zx\n", rc
);
408 DEBUGP(2, dev
, "<- cm4040_write (failed)\n");
409 if (rc
== -ERESTARTSYS
)
415 DEBUGP(2, dev
, "<- cm4040_write (successfully)\n");
419 static unsigned int cm4040_poll(struct file
*filp
, poll_table
*wait
)
421 struct reader_dev
*dev
= filp
->private_data
;
422 unsigned int mask
= 0;
424 poll_wait(filp
, &dev
->poll_wait
, wait
);
426 if (test_and_clear_bit(BS_READABLE
, &dev
->buffer_status
))
427 mask
|= POLLIN
| POLLRDNORM
;
428 if (test_and_clear_bit(BS_WRITABLE
, &dev
->buffer_status
))
429 mask
|= POLLOUT
| POLLWRNORM
;
431 DEBUGP(2, dev
, "<- cm4040_poll(%u)\n", mask
);
436 static int cm4040_open(struct inode
*inode
, struct file
*filp
)
438 struct reader_dev
*dev
;
439 struct pcmcia_device
*link
;
440 int minor
= iminor(inode
);
443 if (minor
>= CM_MAX_DEV
)
447 link
= dev_table
[minor
];
448 if (link
== NULL
|| !pcmcia_dev_present(link
)) {
459 filp
->private_data
= dev
;
461 if (filp
->f_flags
& O_NONBLOCK
) {
462 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 ret
= nonseekable_open(inode
, filp
);
479 static int cm4040_close(struct inode
*inode
, struct file
*filp
)
481 struct reader_dev
*dev
= filp
->private_data
;
482 struct pcmcia_device
*link
;
483 int minor
= iminor(inode
);
485 DEBUGP(2, dev
, "-> cm4040_close(maj/min=%d.%d)\n", imajor(inode
),
488 if (minor
>= CM_MAX_DEV
)
491 link
= dev_table
[minor
];
495 cm4040_stop_poll(dev
);
500 DEBUGP(2, dev
, "<- cm4040_close\n");
504 static void cm4040_reader_release(struct pcmcia_device
*link
)
506 struct reader_dev
*dev
= link
->priv
;
508 DEBUGP(3, dev
, "-> cm4040_reader_release\n");
510 DEBUGP(3, dev
, KERN_INFO MODULE_NAME
": delaying release "
511 "until process has terminated\n");
512 wait_event(dev
->devq
, (link
->open
== 0));
514 DEBUGP(3, dev
, "<- cm4040_reader_release\n");
518 static int cm4040_config_check(struct pcmcia_device
*p_dev
, void *priv_data
)
520 return pcmcia_request_io(p_dev
);
524 static int reader_config(struct pcmcia_device
*link
, int devno
)
526 struct reader_dev
*dev
;
529 link
->config_flags
|= CONF_AUTO_SET_IO
;
531 if (pcmcia_loop_config(link
, cm4040_config_check
, NULL
))
534 fail_rc
= pcmcia_enable_device(link
);
536 dev_printk(KERN_INFO
, &link
->dev
,
537 "pcmcia_enable_device failed 0x%x\n",
544 DEBUGP(2, dev
, "device " DEVICE_NAME
"%d at %pR\n", devno
,
546 DEBUGP(2, dev
, "<- reader_config (succ)\n");
551 reader_release(link
);
555 static void reader_release(struct pcmcia_device
*link
)
557 cm4040_reader_release(link
);
558 pcmcia_disable_device(link
);
561 static int reader_probe(struct pcmcia_device
*link
)
563 struct reader_dev
*dev
;
566 for (i
= 0; i
< CM_MAX_DEV
; i
++) {
567 if (dev_table
[i
] == NULL
)
574 dev
= kzalloc(sizeof(struct reader_dev
), GFP_KERNEL
);
578 dev
->timeout
= CCID_DRIVER_MINIMUM_TIMEOUT
;
579 dev
->buffer_status
= 0;
586 init_waitqueue_head(&dev
->devq
);
587 init_waitqueue_head(&dev
->poll_wait
);
588 init_waitqueue_head(&dev
->read_wait
);
589 init_waitqueue_head(&dev
->write_wait
);
590 setup_timer(&dev
->poll_timer
, cm4040_do_poll
, 0);
592 ret
= reader_config(link
, i
);
599 device_create(cmx_class
, NULL
, MKDEV(major
, i
), NULL
, "cmx%d", i
);
604 static void reader_detach(struct pcmcia_device
*link
)
606 struct reader_dev
*dev
= link
->priv
;
610 for (devno
= 0; devno
< CM_MAX_DEV
; devno
++) {
611 if (dev_table
[devno
] == link
)
614 if (devno
== CM_MAX_DEV
)
617 reader_release(link
);
619 dev_table
[devno
] = NULL
;
622 device_destroy(cmx_class
, MKDEV(major
, devno
));
627 static const struct file_operations reader_fops
= {
628 .owner
= THIS_MODULE
,
630 .write
= cm4040_write
,
632 .release
= cm4040_close
,
636 static struct pcmcia_device_id cm4040_ids
[] = {
637 PCMCIA_DEVICE_MANF_CARD(0x0223, 0x0200),
638 PCMCIA_DEVICE_PROD_ID12("OMNIKEY", "CardMan 4040",
639 0xE32CDD8C, 0x8F23318B),
642 MODULE_DEVICE_TABLE(pcmcia
, cm4040_ids
);
644 static struct pcmcia_driver reader_driver
= {
645 .owner
= THIS_MODULE
,
649 .probe
= reader_probe
,
650 .remove
= reader_detach
,
651 .id_table
= cm4040_ids
,
654 static int __init
cm4040_init(void)
658 printk(KERN_INFO
"%s\n", version
);
659 cmx_class
= class_create(THIS_MODULE
, "cardman_4040");
660 if (IS_ERR(cmx_class
))
661 return PTR_ERR(cmx_class
);
663 major
= register_chrdev(0, DEVICE_NAME
, &reader_fops
);
665 printk(KERN_WARNING MODULE_NAME
666 ": could not get major number\n");
667 class_destroy(cmx_class
);
671 rc
= pcmcia_register_driver(&reader_driver
);
673 unregister_chrdev(major
, DEVICE_NAME
);
674 class_destroy(cmx_class
);
681 static void __exit
cm4040_exit(void)
683 printk(KERN_INFO MODULE_NAME
": unloading\n");
684 pcmcia_unregister_driver(&reader_driver
);
685 unregister_chrdev(major
, DEVICE_NAME
);
686 class_destroy(cmx_class
);
689 module_init(cm4040_init
);
690 module_exit(cm4040_exit
);
691 MODULE_LICENSE("Dual BSD/GPL");