20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/slab.h>
26 #include <linux/poll.h>
28 #include <linux/wait.h>
29 #include <asm/uaccess.h>
40 #define reader_to_dev(x) (&x->p_dev->dev)
46 #define DEBUGP(n, rdr, x, args...) do { \
47 dev_dbg(reader_to_dev(rdr), "%s:" x, \
48 __func__ , ## args); \
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
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
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);
107 static void cm4040_do_poll(
unsigned long dummy)
110 unsigned int obs =
xinb(dev->
p_dev->resource[0]->start
115 DEBUGP(4, dev,
"waking up read_wait\n");
122 DEBUGP(4, dev,
"waking up write_wait\n");
133 static void cm4040_stop_poll(
struct reader_dev *dev)
138 static int wait_for_bulk_out_ready(
struct reader_dev *dev)
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",
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");
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);
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;
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",
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");
216 size_t count, loff_t *ppos)
219 int iobase = dev->
p_dev->resource[0]->start;
220 size_t bytes_to_read;
222 size_t min_bytes_to_read;
235 DEBUGP(4, dev,
"filep->f_flags O_NONBLOCK set\n");
236 DEBUGP(2, dev,
"<- cm4040_read (failure)\n");
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");
263 DEBUGP(6, dev,
"BytesToRead=%zu\n", bytes_to_read);
265 min_bytes_to_read =
min(count, bytes_to_read + 5);
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");
288 *ppos = 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");
303 DEBUGP(5, dev,
"write_sync_reg c=%.2x\n", rc);
304 DEBUGP(2, dev,
"<- cm4040_read (failed)\n");
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)
321 int iobase = dev->
p_dev->resource[0]->start;
324 unsigned int bytes_to_write;
329 DEBUGP(2, dev,
"<- cm4040_write empty read (successfully)\n");
334 DEBUGP(2, dev,
"<- cm4040_write buffersize=%Zd < 5\n", count);
339 DEBUGP(4, dev,
"filep->f_flags O_NONBLOCK set\n");
340 DEBUGP(4, dev,
"<- cm4040_write (failure)\n");
347 bytes_to_write =
count;
351 switch (dev->
s_buf[0]) {
377 DEBUGP(5, dev,
"write_sync_reg c=%.2Zx\n", rc);
378 DEBUGP(2, dev,
"<- cm4040_write (failed)\n");
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");
406 DEBUGP(5, dev,
"write_sync_reg c=%.2Zx\n", rc);
407 DEBUGP(2, dev,
"<- cm4040_write (failed)\n");
414 DEBUGP(2, dev,
"<- cm4040_write (successfully)\n");
421 unsigned int mask = 0;
430 DEBUGP(2, dev,
"<- cm4040_poll(%u)\n", mask);
438 struct pcmcia_device *
link;
439 int minor = iminor(inode);
446 link = dev_table[minor];
461 DEBUGP(4, dev,
"filep->f_flags O_NONBLOCK set\n");
471 DEBUGP(2, dev,
"<- cm4040_open (successfully)\n");
478 static int cm4040_close(
struct inode *inode,
struct file *filp)
481 struct pcmcia_device *
link;
482 int minor = iminor(inode);
484 DEBUGP(2, dev,
"-> cm4040_close(maj/min=%d.%d)\n", imajor(inode),
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)
507 DEBUGP(3, dev,
"-> cm4040_reader_release\n");
510 "until process has terminated\n");
513 DEBUGP(3, dev,
"<- cm4040_reader_release\n");
517 static int cm4040_config_check(
struct pcmcia_device *
p_dev,
void *priv_data)
523 static int reader_config(
struct pcmcia_device *link,
int devno)
528 link->config_flags |= CONF_AUTO_SET_IO;
530 if (pcmcia_loop_config(link, cm4040_config_check,
NULL))
536 "pcmcia_enable_device failed 0x%x\n",
545 DEBUGP(2, dev,
"<- reader_config (succ)\n");
550 reader_release(link);
554 static void reader_release(
struct pcmcia_device *link)
556 cm4040_reader_release(link);
560 static int reader_probe(
struct pcmcia_device *link)
566 if (dev_table[i] ==
NULL)
591 ret = reader_config(link, i);
603 static void reader_detach(
struct pcmcia_device *link)
609 for (devno = 0; devno <
CM_MAX_DEV; devno++) {
610 if (dev_table[devno] == link)
613 if (devno == CM_MAX_DEV)
616 reader_release(link);
629 .write = cm4040_write,
631 .release = cm4040_close,
637 PCMCIA_DEVICE_MANF_CARD(0x0223, 0x0200),
638 PCMCIA_DEVICE_PROD_ID12(
"OMNIKEY",
"CardMan 4040",
639 0xE32CDD8C, 0x8F23318B),
644 static struct pcmcia_driver reader_driver = {
647 .probe = reader_probe,
648 .remove = reader_detach,
649 .id_table = cm4040_ids,
652 static int __init cm4040_init(
void)
657 if (IS_ERR(cmx_class))
658 return PTR_ERR(cmx_class);
663 ": could not get major number\n");
678 static void __exit cm4040_exit(
void)