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_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
47 /* #define CM4040_DEBUG */
48 #define DEBUGP(n, rdr, x, args...) do { \
49 dev_dbg(reader_to_dev(rdr), "%s:" x, \
50 __func__ , ## args); \
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
);
68 static struct class *cmx_class
;
70 #define BS_READABLE 0x01
71 #define BS_WRITABLE 0x02
74 struct pcmcia_device
*p_dev
;
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
];
93 static inline void xoutb(unsigned char val
, unsigned short port
)
95 pr_debug("outb(val=%.2x,port=%.4x)\n", val
, port
);
99 static inline unsigned char xinb(unsigned short port
)
104 pr_debug("%.2x=inb(%.4x)\n", val
, port
);
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
);
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
);
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
)
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
);
155 DEBUGP(4, dev
, "wait_event_interruptible_timeout(timeout=%ld\n",
157 rc
= wait_event_interruptible_timeout(dev
->write_wait
,
158 test_and_clear_bit(BS_WRITABLE
,
159 &dev
->buffer_status
),
163 DEBUGP(4, dev
, "woke up: BulkOut empty\n");
165 DEBUGP(4, dev
, "woke up: BulkOut full, returning 0 :(\n");
167 DEBUGP(4, dev
, "woke up: signal arrived\n");
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
;
178 rc
= wait_for_bulk_out_ready(dev
);
182 xoutb(val
, iobase
+ REG_OFFSET_SYNC_CONTROL
);
183 rc
= wait_for_bulk_out_ready(dev
);
190 static int wait_for_bulk_in_ready(struct reader_dev
*dev
)
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
);
203 DEBUGP(4, dev
, "wait_event_interruptible_timeout(timeout=%ld\n",
205 rc
= wait_event_interruptible_timeout(dev
->read_wait
,
206 test_and_clear_bit(BS_READABLE
,
207 &dev
->buffer_status
),
210 DEBUGP(4, dev
, "woke up: BulkIn full\n");
212 DEBUGP(4, dev
, "woke up: BulkIn not full, returning 0 :(\n");
214 DEBUGP(4, dev
, "woke up: signal arrived\n");
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
;
226 size_t min_bytes_to_read
;
230 DEBUGP(2, dev
, "-> cm4040_read(%s,%d)\n", current
->comm
, current
->pid
);
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");
244 if (!pcmcia_dev_present(dev
->p_dev
))
247 for (i
= 0; i
< 5; i
++) {
248 rc
= wait_for_bulk_in_ready(dev
);
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
)
256 dev
->r_buf
[i
] = xinb(iobase
+ REG_OFFSET_BULK_IN
);
258 pr_debug("%lu:%2x ", i
, dev
->r_buf
[i
]);
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
);
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
)
283 dev
->r_buf
[i
+5] = xinb(iobase
+ REG_OFFSET_BULK_IN
);
285 pr_debug("%lu:%2x ", i
, dev
->r_buf
[i
]);
292 *ppos
= min_bytes_to_read
;
293 if (copy_to_user(buf
, dev
->r_buf
, min_bytes_to_read
))
296 rc
= wait_for_bulk_in_ready(dev
);
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
)
305 rc
= write_sync_reg(SCR_READER_TO_HOST_DONE
, dev
);
307 DEBUGP(5, dev
, "write_sync_reg c=%.2x\n", rc
);
308 DEBUGP(2, dev
, "<- cm4040_read (failed)\n");
309 if (rc
== -ERESTARTSYS
)
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
;
328 unsigned int bytes_to_write
;
330 DEBUGP(2, dev
, "-> cm4040_write(%s,%d)\n", current
->comm
, current
->pid
);
333 DEBUGP(2, dev
, "<- cm4040_write empty read (successfully)\n");
337 if ((count
< 5) || (count
> READ_WRITE_BUFFER_SIZE
)) {
338 DEBUGP(2, dev
, "<- cm4040_write buffersize=%Zd < 5\n", count
);
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");
348 if (!pcmcia_dev_present(dev
->p_dev
))
351 bytes_to_write
= count
;
352 if (copy_from_user(dev
->s_buf
, buf
, bytes_to_write
))
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
;
363 case CMD_PC_TO_RDR_ICCPOWERON
:
364 dev
->timeout
= CCID_DRIVER_ASYNC_POWERUP_TIMEOUT
;
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
:
375 dev
->timeout
= CCID_DRIVER_MINIMUM_TIMEOUT
;
379 rc
= write_sync_reg(SCR_HOST_TO_READER_START
, dev
);
381 DEBUGP(5, dev
, "write_sync_reg c=%.2Zx\n", rc
);
382 DEBUGP(2, dev
, "<- cm4040_write (failed)\n");
383 if (rc
== -ERESTARTSYS
)
389 DEBUGP(4, dev
, "start \n");
391 for (i
= 0; i
< bytes_to_write
; i
++) {
392 rc
= wait_for_bulk_out_ready(dev
);
394 DEBUGP(5, dev
, "wait_for_bulk_out_ready rc=%.2Zx\n",
396 DEBUGP(2, dev
, "<- cm4040_write (failed)\n");
397 if (rc
== -ERESTARTSYS
)
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
);
410 DEBUGP(5, dev
, "write_sync_reg c=%.2Zx\n", rc
);
411 DEBUGP(2, dev
, "<- cm4040_write (failed)\n");
412 if (rc
== -ERESTARTSYS
)
418 DEBUGP(2, dev
, "<- cm4040_write (successfully)\n");
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
);
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
);
446 if (minor
>= CM_MAX_DEV
)
450 link
= dev_table
[minor
];
451 if (link
== NULL
|| !pcmcia_dev_present(link
)) {
462 filp
->private_data
= dev
;
464 if (filp
->f_flags
& O_NONBLOCK
) {
465 DEBUGP(4, dev
, "filep->f_flags O_NONBLOCK set\n");
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
);
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
),
491 if (minor
>= CM_MAX_DEV
)
494 link
= dev_table
[minor
];
498 cm4040_stop_poll(dev
);
503 DEBUGP(2, dev
, "<- cm4040_close\n");
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");
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");
521 static int cm4040_config_check(struct pcmcia_device
*p_dev
,
522 cistpl_cftable_entry_t
*cfg
,
523 cistpl_cftable_entry_t
*dflt
,
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
);
548 static int reader_config(struct pcmcia_device
*link
, int devno
)
550 struct reader_dev
*dev
;
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
))
560 link
->conf
.IntType
= 00000002;
562 fail_rc
= pcmcia_request_configuration(link
, &link
->conf
);
564 dev_printk(KERN_INFO
, &link
->dev
,
565 "pcmcia_request_configuration failed 0x%x\n",
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");
583 reader_release(link
);
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
;
598 for (i
= 0; i
< CM_MAX_DEV
; i
++) {
599 if (dev_table
[i
] == NULL
)
606 dev
= kzalloc(sizeof(struct reader_dev
), GFP_KERNEL
);
610 dev
->timeout
= CCID_DRIVER_MINIMUM_TIMEOUT
;
611 dev
->buffer_status
= 0;
616 link
->conf
.IntType
= INT_MEMORY_AND_IO
;
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
);
632 device_create(cmx_class
, NULL
, MKDEV(major
, i
), NULL
, "cmx%d", i
);
637 static void reader_detach(struct pcmcia_device
*link
)
639 struct reader_dev
*dev
= link
->priv
;
643 for (devno
= 0; devno
< CM_MAX_DEV
; devno
++) {
644 if (dev_table
[devno
] == link
)
647 if (devno
== CM_MAX_DEV
)
650 reader_release(link
);
652 dev_table
[devno
] = NULL
;
655 device_destroy(cmx_class
, MKDEV(major
, devno
));
660 static const struct file_operations reader_fops
= {
661 .owner
= THIS_MODULE
,
663 .write
= cm4040_write
,
665 .release
= cm4040_close
,
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),
675 MODULE_DEVICE_TABLE(pcmcia
, cm4040_ids
);
677 static struct pcmcia_driver reader_driver
= {
678 .owner
= THIS_MODULE
,
682 .probe
= reader_probe
,
683 .remove
= reader_detach
,
684 .id_table
= cm4040_ids
,
687 static int __init
cm4040_init(void)
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
);
698 printk(KERN_WARNING MODULE_NAME
699 ": could not get major number\n");
700 class_destroy(cmx_class
);
704 rc
= pcmcia_register_driver(&reader_driver
);
706 unregister_chrdev(major
, DEVICE_NAME
);
707 class_destroy(cmx_class
);
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");