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 /* #define PCMCIA_DEBUG 6 */
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/slab.h>
25 #include <linux/init.h>
27 #include <linux/delay.h>
28 #include <linux/poll.h>
29 #include <linux/smp_lock.h>
30 #include <linux/wait.h>
31 #include <asm/uaccess.h>
34 #include <pcmcia/cs_types.h>
35 #include <pcmcia/cs.h>
36 #include <pcmcia/cistpl.h>
37 #include <pcmcia/cisreg.h>
38 #include <pcmcia/ciscode.h>
39 #include <pcmcia/ds.h>
41 #include "cm4040_cs.h"
45 #define reader_to_dev(x) (&handle_to_dev(x->p_dev))
46 static int pc_debug
= PCMCIA_DEBUG
;
47 module_param(pc_debug
, int, 0600);
48 #define DEBUGP(n, rdr, x, args...) do { \
49 if (pc_debug >= (n)) \
50 dev_printk(KERN_DEBUG, reader_to_dev(rdr), "%s:" x, \
54 #define DEBUGP(n, rdr, x, args...)
57 static char *version
=
58 "OMNIKEY CardMan 4040 v1.1.0gm5 - All bugs added by Harald Welte";
60 #define CCID_DRIVER_BULK_DEFAULT_TIMEOUT (150*HZ)
61 #define CCID_DRIVER_ASYNC_POWERUP_TIMEOUT (35*HZ)
62 #define CCID_DRIVER_MINIMUM_TIMEOUT (3*HZ)
63 #define READ_WRITE_BUFFER_SIZE 512
64 #define POLL_LOOP_COUNT 1000
66 /* how often to poll for fifo status change */
67 #define POLL_PERIOD msecs_to_jiffies(10)
69 static void reader_release(struct pcmcia_device
*link
);
72 static struct class *cmx_class
;
74 #define BS_READABLE 0x01
75 #define BS_WRITABLE 0x02
78 struct pcmcia_device
*p_dev
;
80 wait_queue_head_t devq
;
81 wait_queue_head_t poll_wait
;
82 wait_queue_head_t read_wait
;
83 wait_queue_head_t write_wait
;
84 unsigned long buffer_status
;
85 unsigned long timeout
;
86 unsigned char s_buf
[READ_WRITE_BUFFER_SIZE
];
87 unsigned char r_buf
[READ_WRITE_BUFFER_SIZE
];
88 struct timer_list poll_timer
;
91 static struct pcmcia_device
*dev_table
[CM_MAX_DEV
];
97 static inline void xoutb(unsigned char val
, unsigned short port
)
100 printk(KERN_DEBUG
"outb(val=%.2x,port=%.4x)\n", val
, port
);
104 static inline unsigned char xinb(unsigned short port
)
110 printk(KERN_DEBUG
"%.2x=inb(%.4x)\n", val
, port
);
115 /* poll the device fifo status register. not to be confused with
116 * the poll syscall. */
117 static void cm4040_do_poll(unsigned long dummy
)
119 struct reader_dev
*dev
= (struct reader_dev
*) dummy
;
120 unsigned int obs
= xinb(dev
->p_dev
->io
.BasePort1
121 + REG_OFFSET_BUFFER_STATUS
);
123 if ((obs
& BSR_BULK_IN_FULL
)) {
124 set_bit(BS_READABLE
, &dev
->buffer_status
);
125 DEBUGP(4, dev
, "waking up read_wait\n");
126 wake_up_interruptible(&dev
->read_wait
);
128 clear_bit(BS_READABLE
, &dev
->buffer_status
);
130 if (!(obs
& BSR_BULK_OUT_FULL
)) {
131 set_bit(BS_WRITABLE
, &dev
->buffer_status
);
132 DEBUGP(4, dev
, "waking up write_wait\n");
133 wake_up_interruptible(&dev
->write_wait
);
135 clear_bit(BS_WRITABLE
, &dev
->buffer_status
);
137 if (dev
->buffer_status
)
138 wake_up_interruptible(&dev
->poll_wait
);
140 mod_timer(&dev
->poll_timer
, jiffies
+ POLL_PERIOD
);
143 static void cm4040_stop_poll(struct reader_dev
*dev
)
145 del_timer_sync(&dev
->poll_timer
);
148 static int wait_for_bulk_out_ready(struct reader_dev
*dev
)
151 int iobase
= dev
->p_dev
->io
.BasePort1
;
153 for (i
= 0; i
< POLL_LOOP_COUNT
; i
++) {
154 if ((xinb(iobase
+ REG_OFFSET_BUFFER_STATUS
)
155 & BSR_BULK_OUT_FULL
) == 0) {
156 DEBUGP(4, dev
, "BulkOut empty (i=%d)\n", i
);
161 DEBUGP(4, dev
, "wait_event_interruptible_timeout(timeout=%ld\n",
163 rc
= wait_event_interruptible_timeout(dev
->write_wait
,
164 test_and_clear_bit(BS_WRITABLE
,
165 &dev
->buffer_status
),
169 DEBUGP(4, dev
, "woke up: BulkOut empty\n");
171 DEBUGP(4, dev
, "woke up: BulkOut full, returning 0 :(\n");
173 DEBUGP(4, dev
, "woke up: signal arrived\n");
178 /* Write to Sync Control Register */
179 static int write_sync_reg(unsigned char val
, struct reader_dev
*dev
)
181 int iobase
= dev
->p_dev
->io
.BasePort1
;
184 rc
= wait_for_bulk_out_ready(dev
);
188 xoutb(val
, iobase
+ REG_OFFSET_SYNC_CONTROL
);
189 rc
= wait_for_bulk_out_ready(dev
);
196 static int wait_for_bulk_in_ready(struct reader_dev
*dev
)
199 int iobase
= dev
->p_dev
->io
.BasePort1
;
201 for (i
= 0; i
< POLL_LOOP_COUNT
; i
++) {
202 if ((xinb(iobase
+ REG_OFFSET_BUFFER_STATUS
)
203 & BSR_BULK_IN_FULL
) == BSR_BULK_IN_FULL
) {
204 DEBUGP(3, dev
, "BulkIn full (i=%d)\n", i
);
209 DEBUGP(4, dev
, "wait_event_interruptible_timeout(timeout=%ld\n",
211 rc
= wait_event_interruptible_timeout(dev
->read_wait
,
212 test_and_clear_bit(BS_READABLE
,
213 &dev
->buffer_status
),
216 DEBUGP(4, dev
, "woke up: BulkIn full\n");
218 DEBUGP(4, dev
, "woke up: BulkIn not full, returning 0 :(\n");
220 DEBUGP(4, dev
, "woke up: signal arrived\n");
225 static ssize_t
cm4040_read(struct file
*filp
, char __user
*buf
,
226 size_t count
, loff_t
*ppos
)
228 struct reader_dev
*dev
= filp
->private_data
;
229 int iobase
= dev
->p_dev
->io
.BasePort1
;
230 size_t bytes_to_read
;
232 size_t min_bytes_to_read
;
236 DEBUGP(2, dev
, "-> cm4040_read(%s,%d)\n", current
->comm
, current
->pid
);
244 if (filp
->f_flags
& O_NONBLOCK
) {
245 DEBUGP(4, dev
, "filep->f_flags O_NONBLOCK set\n");
246 DEBUGP(2, dev
, "<- cm4040_read (failure)\n");
250 if (!pcmcia_dev_present(dev
->p_dev
))
253 for (i
= 0; i
< 5; i
++) {
254 rc
= wait_for_bulk_in_ready(dev
);
256 DEBUGP(5, dev
, "wait_for_bulk_in_ready rc=%.2x\n", rc
);
257 DEBUGP(2, dev
, "<- cm4040_read (failed)\n");
258 if (rc
== -ERESTARTSYS
)
262 dev
->r_buf
[i
] = xinb(iobase
+ REG_OFFSET_BULK_IN
);
265 printk(KERN_DEBUG
"%lu:%2x ", i
, dev
->r_buf
[i
]);
272 bytes_to_read
= 5 + le32_to_cpu(*(__le32
*)&dev
->r_buf
[1]);
274 DEBUGP(6, dev
, "BytesToRead=%lu\n", bytes_to_read
);
276 min_bytes_to_read
= min(count
, bytes_to_read
+ 5);
277 min_bytes_to_read
= min_t(size_t, min_bytes_to_read
, READ_WRITE_BUFFER_SIZE
);
279 DEBUGP(6, dev
, "Min=%lu\n", min_bytes_to_read
);
281 for (i
= 0; i
< (min_bytes_to_read
-5); i
++) {
282 rc
= wait_for_bulk_in_ready(dev
);
284 DEBUGP(5, dev
, "wait_for_bulk_in_ready rc=%.2x\n", rc
);
285 DEBUGP(2, dev
, "<- cm4040_read (failed)\n");
286 if (rc
== -ERESTARTSYS
)
290 dev
->r_buf
[i
+5] = xinb(iobase
+ REG_OFFSET_BULK_IN
);
293 printk(KERN_DEBUG
"%lu:%2x ", i
, dev
->r_buf
[i
]);
300 *ppos
= min_bytes_to_read
;
301 if (copy_to_user(buf
, dev
->r_buf
, min_bytes_to_read
))
304 rc
= wait_for_bulk_in_ready(dev
);
306 DEBUGP(5, dev
, "wait_for_bulk_in_ready rc=%.2x\n", rc
);
307 DEBUGP(2, dev
, "<- cm4040_read (failed)\n");
308 if (rc
== -ERESTARTSYS
)
313 rc
= write_sync_reg(SCR_READER_TO_HOST_DONE
, dev
);
315 DEBUGP(5, dev
, "write_sync_reg c=%.2x\n", rc
);
316 DEBUGP(2, dev
, "<- cm4040_read (failed)\n");
317 if (rc
== -ERESTARTSYS
)
323 uc
= xinb(iobase
+ REG_OFFSET_BULK_IN
);
325 DEBUGP(2, dev
, "<- cm4040_read (successfully)\n");
326 return min_bytes_to_read
;
329 static ssize_t
cm4040_write(struct file
*filp
, const char __user
*buf
,
330 size_t count
, loff_t
*ppos
)
332 struct reader_dev
*dev
= filp
->private_data
;
333 int iobase
= dev
->p_dev
->io
.BasePort1
;
336 unsigned int bytes_to_write
;
338 DEBUGP(2, dev
, "-> cm4040_write(%s,%d)\n", current
->comm
, current
->pid
);
341 DEBUGP(2, dev
, "<- cm4040_write empty read (successfully)\n");
345 if ((count
< 5) || (count
> READ_WRITE_BUFFER_SIZE
)) {
346 DEBUGP(2, dev
, "<- cm4040_write buffersize=%Zd < 5\n", count
);
350 if (filp
->f_flags
& O_NONBLOCK
) {
351 DEBUGP(4, dev
, "filep->f_flags O_NONBLOCK set\n");
352 DEBUGP(4, dev
, "<- cm4040_write (failure)\n");
356 if (!pcmcia_dev_present(dev
->p_dev
))
359 bytes_to_write
= count
;
360 if (copy_from_user(dev
->s_buf
, buf
, bytes_to_write
))
363 switch (dev
->s_buf
[0]) {
364 case CMD_PC_TO_RDR_XFRBLOCK
:
365 case CMD_PC_TO_RDR_SECURE
:
366 case CMD_PC_TO_RDR_TEST_SECURE
:
367 case CMD_PC_TO_RDR_OK_SECURE
:
368 dev
->timeout
= CCID_DRIVER_BULK_DEFAULT_TIMEOUT
;
371 case CMD_PC_TO_RDR_ICCPOWERON
:
372 dev
->timeout
= CCID_DRIVER_ASYNC_POWERUP_TIMEOUT
;
375 case CMD_PC_TO_RDR_GETSLOTSTATUS
:
376 case CMD_PC_TO_RDR_ICCPOWEROFF
:
377 case CMD_PC_TO_RDR_GETPARAMETERS
:
378 case CMD_PC_TO_RDR_RESETPARAMETERS
:
379 case CMD_PC_TO_RDR_SETPARAMETERS
:
380 case CMD_PC_TO_RDR_ESCAPE
:
381 case CMD_PC_TO_RDR_ICCCLOCK
:
383 dev
->timeout
= CCID_DRIVER_MINIMUM_TIMEOUT
;
387 rc
= write_sync_reg(SCR_HOST_TO_READER_START
, dev
);
389 DEBUGP(5, dev
, "write_sync_reg c=%.2Zx\n", rc
);
390 DEBUGP(2, dev
, "<- cm4040_write (failed)\n");
391 if (rc
== -ERESTARTSYS
)
397 DEBUGP(4, dev
, "start \n");
399 for (i
= 0; i
< bytes_to_write
; i
++) {
400 rc
= wait_for_bulk_out_ready(dev
);
402 DEBUGP(5, dev
, "wait_for_bulk_out_ready rc=%.2Zx\n",
404 DEBUGP(2, dev
, "<- cm4040_write (failed)\n");
405 if (rc
== -ERESTARTSYS
)
411 xoutb(dev
->s_buf
[i
],iobase
+ REG_OFFSET_BULK_OUT
);
413 DEBUGP(4, dev
, "end\n");
415 rc
= write_sync_reg(SCR_HOST_TO_READER_DONE
, dev
);
418 DEBUGP(5, dev
, "write_sync_reg c=%.2Zx\n", rc
);
419 DEBUGP(2, dev
, "<- cm4040_write (failed)\n");
420 if (rc
== -ERESTARTSYS
)
426 DEBUGP(2, dev
, "<- cm4040_write (successfully)\n");
430 static unsigned int cm4040_poll(struct file
*filp
, poll_table
*wait
)
432 struct reader_dev
*dev
= filp
->private_data
;
433 unsigned int mask
= 0;
435 poll_wait(filp
, &dev
->poll_wait
, wait
);
437 if (test_and_clear_bit(BS_READABLE
, &dev
->buffer_status
))
438 mask
|= POLLIN
| POLLRDNORM
;
439 if (test_and_clear_bit(BS_WRITABLE
, &dev
->buffer_status
))
440 mask
|= POLLOUT
| POLLWRNORM
;
442 DEBUGP(2, dev
, "<- cm4040_poll(%u)\n", mask
);
447 static int cm4040_open(struct inode
*inode
, struct file
*filp
)
449 struct reader_dev
*dev
;
450 struct pcmcia_device
*link
;
451 int minor
= iminor(inode
);
454 if (minor
>= CM_MAX_DEV
)
458 link
= dev_table
[minor
];
459 if (link
== NULL
|| !pcmcia_dev_present(link
)) {
470 filp
->private_data
= dev
;
472 if (filp
->f_flags
& O_NONBLOCK
) {
473 DEBUGP(4, dev
, "filep->f_flags O_NONBLOCK set\n");
480 dev
->poll_timer
.data
= (unsigned long) dev
;
481 mod_timer(&dev
->poll_timer
, jiffies
+ POLL_PERIOD
);
483 DEBUGP(2, dev
, "<- cm4040_open (successfully)\n");
484 ret
= nonseekable_open(inode
, filp
);
490 static int cm4040_close(struct inode
*inode
, struct file
*filp
)
492 struct reader_dev
*dev
= filp
->private_data
;
493 struct pcmcia_device
*link
;
494 int minor
= iminor(inode
);
496 DEBUGP(2, dev
, "-> cm4040_close(maj/min=%d.%d)\n", imajor(inode
),
499 if (minor
>= CM_MAX_DEV
)
502 link
= dev_table
[minor
];
506 cm4040_stop_poll(dev
);
511 DEBUGP(2, dev
, "<- cm4040_close\n");
515 static void cm4040_reader_release(struct pcmcia_device
*link
)
517 struct reader_dev
*dev
= link
->priv
;
519 DEBUGP(3, dev
, "-> cm4040_reader_release\n");
521 DEBUGP(3, dev
, KERN_INFO MODULE_NAME
": delaying release "
522 "until process has terminated\n");
523 wait_event(dev
->devq
, (link
->open
== 0));
525 DEBUGP(3, dev
, "<- cm4040_reader_release\n");
529 static int reader_config(struct pcmcia_device
*link
, int devno
)
531 struct reader_dev
*dev
;
535 int fail_fn
, fail_rc
;
538 tuple
.Attributes
= 0;
539 tuple
.TupleData
= buf
;
540 tuple
.TupleDataMax
= sizeof(buf
);
541 tuple
.TupleOffset
= 0;
543 link
->io
.BasePort2
= 0;
544 link
->io
.NumPorts2
= 0;
545 link
->io
.Attributes2
= 0;
546 tuple
.DesiredTuple
= CISTPL_CFTABLE_ENTRY
;
547 for (rc
= pcmcia_get_first_tuple(link
, &tuple
);
549 rc
= pcmcia_get_next_tuple(link
, &tuple
)) {
550 rc
= pcmcia_get_tuple_data(link
, &tuple
);
551 if (rc
!= CS_SUCCESS
)
553 rc
= pcmcia_parse_tuple(link
, &tuple
, &parse
);
554 if (rc
!= CS_SUCCESS
)
557 link
->conf
.ConfigIndex
= parse
.cftable_entry
.index
;
559 if (!parse
.cftable_entry
.io
.nwin
)
562 link
->io
.BasePort1
= parse
.cftable_entry
.io
.win
[0].base
;
563 link
->io
.NumPorts1
= parse
.cftable_entry
.io
.win
[0].len
;
564 link
->io
.Attributes1
= IO_DATA_PATH_WIDTH_AUTO
;
565 if (!(parse
.cftable_entry
.io
.flags
& CISTPL_IO_8BIT
))
566 link
->io
.Attributes1
= IO_DATA_PATH_WIDTH_16
;
567 if (!(parse
.cftable_entry
.io
.flags
& CISTPL_IO_16BIT
))
568 link
->io
.Attributes1
= IO_DATA_PATH_WIDTH_8
;
569 link
->io
.IOAddrLines
= parse
.cftable_entry
.io
.flags
570 & CISTPL_IO_LINES_MASK
;
571 rc
= pcmcia_request_io(link
, &link
->io
);
573 dev_printk(KERN_INFO
, &handle_to_dev(link
), "foo");
574 if (rc
== CS_SUCCESS
)
577 dev_printk(KERN_INFO
, &handle_to_dev(link
),
578 "pcmcia_request_io failed 0x%x\n", rc
);
580 if (rc
!= CS_SUCCESS
)
583 link
->conf
.IntType
= 00000002;
585 if ((fail_rc
= pcmcia_request_configuration(link
,&link
->conf
))
587 fail_fn
= RequestConfiguration
;
588 dev_printk(KERN_INFO
, &handle_to_dev(link
),
589 "pcmcia_request_configuration failed 0x%x\n",
595 sprintf(dev
->node
.dev_name
, DEVICE_NAME
"%d", devno
);
596 dev
->node
.major
= major
;
597 dev
->node
.minor
= devno
;
598 dev
->node
.next
= &dev
->node
;
600 DEBUGP(2, dev
, "device " DEVICE_NAME
"%d at 0x%.4x-0x%.4x\n", devno
,
601 link
->io
.BasePort1
, link
->io
.BasePort1
+link
->io
.NumPorts1
);
602 DEBUGP(2, dev
, "<- reader_config (succ)\n");
607 reader_release(link
);
611 static void reader_release(struct pcmcia_device
*link
)
613 cm4040_reader_release(link
);
614 pcmcia_disable_device(link
);
617 static int reader_probe(struct pcmcia_device
*link
)
619 struct reader_dev
*dev
;
622 for (i
= 0; i
< CM_MAX_DEV
; i
++) {
623 if (dev_table
[i
] == NULL
)
630 dev
= kzalloc(sizeof(struct reader_dev
), GFP_KERNEL
);
634 dev
->timeout
= CCID_DRIVER_MINIMUM_TIMEOUT
;
635 dev
->buffer_status
= 0;
640 link
->conf
.IntType
= INT_MEMORY_AND_IO
;
643 init_waitqueue_head(&dev
->devq
);
644 init_waitqueue_head(&dev
->poll_wait
);
645 init_waitqueue_head(&dev
->read_wait
);
646 init_waitqueue_head(&dev
->write_wait
);
647 setup_timer(&dev
->poll_timer
, cm4040_do_poll
, 0);
649 ret
= reader_config(link
, i
);
656 device_create_drvdata(cmx_class
, NULL
, MKDEV(major
, i
), NULL
,
662 static void reader_detach(struct pcmcia_device
*link
)
664 struct reader_dev
*dev
= link
->priv
;
668 for (devno
= 0; devno
< CM_MAX_DEV
; devno
++) {
669 if (dev_table
[devno
] == link
)
672 if (devno
== CM_MAX_DEV
)
675 reader_release(link
);
677 dev_table
[devno
] = NULL
;
680 device_destroy(cmx_class
, MKDEV(major
, devno
));
685 static const struct file_operations reader_fops
= {
686 .owner
= THIS_MODULE
,
688 .write
= cm4040_write
,
690 .release
= cm4040_close
,
694 static struct pcmcia_device_id cm4040_ids
[] = {
695 PCMCIA_DEVICE_MANF_CARD(0x0223, 0x0200),
696 PCMCIA_DEVICE_PROD_ID12("OMNIKEY", "CardMan 4040",
697 0xE32CDD8C, 0x8F23318B),
700 MODULE_DEVICE_TABLE(pcmcia
, cm4040_ids
);
702 static struct pcmcia_driver reader_driver
= {
703 .owner
= THIS_MODULE
,
707 .probe
= reader_probe
,
708 .remove
= reader_detach
,
709 .id_table
= cm4040_ids
,
712 static int __init
cm4040_init(void)
716 printk(KERN_INFO
"%s\n", version
);
717 cmx_class
= class_create(THIS_MODULE
, "cardman_4040");
718 if (IS_ERR(cmx_class
))
719 return PTR_ERR(cmx_class
);
721 major
= register_chrdev(0, DEVICE_NAME
, &reader_fops
);
723 printk(KERN_WARNING MODULE_NAME
724 ": could not get major number\n");
725 class_destroy(cmx_class
);
729 rc
= pcmcia_register_driver(&reader_driver
);
731 unregister_chrdev(major
, DEVICE_NAME
);
732 class_destroy(cmx_class
);
739 static void __exit
cm4040_exit(void)
741 printk(KERN_INFO MODULE_NAME
": unloading\n");
742 pcmcia_unregister_driver(&reader_driver
);
743 unregister_chrdev(major
, DEVICE_NAME
);
744 class_destroy(cmx_class
);
747 module_init(cm4040_init
);
748 module_exit(cm4040_exit
);
749 MODULE_LICENSE("Dual BSD/GPL");