cm4040_cs.c (15786B)
1/* 2 * A driver for the Omnikey PCMCIA smartcard reader CardMan 4040 3 * 4 * (c) 2000-2004 Omnikey AG (http://www.omnikey.com/) 5 * 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 13 * 14 * The device basically is a USB CCID compliant device that has been 15 * attached to an I/O-Mapped FIFO. 16 * 17 * All rights reserved, Dual BSD/GPL Licensed. 18 */ 19 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/mutex.h> 28#include <linux/wait.h> 29#include <linux/uaccess.h> 30#include <asm/io.h> 31 32#include <pcmcia/cistpl.h> 33#include <pcmcia/cisreg.h> 34#include <pcmcia/ciscode.h> 35#include <pcmcia/ds.h> 36 37#include "cm4040_cs.h" 38 39 40#define reader_to_dev(x) (&x->p_dev->dev) 41 42/* n (debug level) is ignored */ 43/* additional debug output may be enabled by re-compiling with 44 * CM4040_DEBUG set */ 45/* #define CM4040_DEBUG */ 46#define DEBUGP(n, rdr, x, args...) do { \ 47 dev_dbg(reader_to_dev(rdr), "%s:" x, \ 48 __func__ , ## args); \ 49 } while (0) 50 51static DEFINE_MUTEX(cm4040_mutex); 52 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 58 59/* how often to poll for fifo status change */ 60#define POLL_PERIOD msecs_to_jiffies(10) 61 62static void reader_release(struct pcmcia_device *link); 63 64static int major; 65static struct class *cmx_class; 66 67#define BS_READABLE 0x01 68#define BS_WRITABLE 0x02 69 70struct reader_dev { 71 struct pcmcia_device *p_dev; 72 wait_queue_head_t devq; 73 wait_queue_head_t poll_wait; 74 wait_queue_head_t read_wait; 75 wait_queue_head_t write_wait; 76 unsigned long buffer_status; 77 unsigned long timeout; 78 unsigned char s_buf[READ_WRITE_BUFFER_SIZE]; 79 unsigned char r_buf[READ_WRITE_BUFFER_SIZE]; 80 struct timer_list poll_timer; 81}; 82 83static struct pcmcia_device *dev_table[CM_MAX_DEV]; 84 85#ifndef CM4040_DEBUG 86#define xoutb outb 87#define xinb inb 88#else 89static inline void xoutb(unsigned char val, unsigned short port) 90{ 91 pr_debug("outb(val=%.2x,port=%.4x)\n", val, port); 92 outb(val, port); 93} 94 95static inline unsigned char xinb(unsigned short port) 96{ 97 unsigned char val; 98 99 val = inb(port); 100 pr_debug("%.2x=inb(%.4x)\n", val, port); 101 return val; 102} 103#endif 104 105/* poll the device fifo status register. not to be confused with 106 * the poll syscall. */ 107static void cm4040_do_poll(struct timer_list *t) 108{ 109 struct reader_dev *dev = from_timer(dev, t, poll_timer); 110 unsigned int obs = xinb(dev->p_dev->resource[0]->start 111 + REG_OFFSET_BUFFER_STATUS); 112 113 if ((obs & BSR_BULK_IN_FULL)) { 114 set_bit(BS_READABLE, &dev->buffer_status); 115 DEBUGP(4, dev, "waking up read_wait\n"); 116 wake_up_interruptible(&dev->read_wait); 117 } else 118 clear_bit(BS_READABLE, &dev->buffer_status); 119 120 if (!(obs & BSR_BULK_OUT_FULL)) { 121 set_bit(BS_WRITABLE, &dev->buffer_status); 122 DEBUGP(4, dev, "waking up write_wait\n"); 123 wake_up_interruptible(&dev->write_wait); 124 } else 125 clear_bit(BS_WRITABLE, &dev->buffer_status); 126 127 if (dev->buffer_status) 128 wake_up_interruptible(&dev->poll_wait); 129 130 mod_timer(&dev->poll_timer, jiffies + POLL_PERIOD); 131} 132 133static void cm4040_stop_poll(struct reader_dev *dev) 134{ 135 del_timer_sync(&dev->poll_timer); 136} 137 138static int wait_for_bulk_out_ready(struct reader_dev *dev) 139{ 140 int i, rc; 141 int iobase = dev->p_dev->resource[0]->start; 142 143 for (i = 0; i < POLL_LOOP_COUNT; i++) { 144 if ((xinb(iobase + REG_OFFSET_BUFFER_STATUS) 145 & BSR_BULK_OUT_FULL) == 0) { 146 DEBUGP(4, dev, "BulkOut empty (i=%d)\n", i); 147 return 1; 148 } 149 } 150 151 DEBUGP(4, dev, "wait_event_interruptible_timeout(timeout=%ld\n", 152 dev->timeout); 153 rc = wait_event_interruptible_timeout(dev->write_wait, 154 test_and_clear_bit(BS_WRITABLE, 155 &dev->buffer_status), 156 dev->timeout); 157 158 if (rc > 0) 159 DEBUGP(4, dev, "woke up: BulkOut empty\n"); 160 else if (rc == 0) 161 DEBUGP(4, dev, "woke up: BulkOut full, returning 0 :(\n"); 162 else if (rc < 0) 163 DEBUGP(4, dev, "woke up: signal arrived\n"); 164 165 return rc; 166} 167 168/* Write to Sync Control Register */ 169static int write_sync_reg(unsigned char val, struct reader_dev *dev) 170{ 171 int iobase = dev->p_dev->resource[0]->start; 172 int rc; 173 174 rc = wait_for_bulk_out_ready(dev); 175 if (rc <= 0) 176 return rc; 177 178 xoutb(val, iobase + REG_OFFSET_SYNC_CONTROL); 179 rc = wait_for_bulk_out_ready(dev); 180 if (rc <= 0) 181 return rc; 182 183 return 1; 184} 185 186static int wait_for_bulk_in_ready(struct reader_dev *dev) 187{ 188 int i, rc; 189 int iobase = dev->p_dev->resource[0]->start; 190 191 for (i = 0; i < POLL_LOOP_COUNT; i++) { 192 if ((xinb(iobase + REG_OFFSET_BUFFER_STATUS) 193 & BSR_BULK_IN_FULL) == BSR_BULK_IN_FULL) { 194 DEBUGP(3, dev, "BulkIn full (i=%d)\n", i); 195 return 1; 196 } 197 } 198 199 DEBUGP(4, dev, "wait_event_interruptible_timeout(timeout=%ld\n", 200 dev->timeout); 201 rc = wait_event_interruptible_timeout(dev->read_wait, 202 test_and_clear_bit(BS_READABLE, 203 &dev->buffer_status), 204 dev->timeout); 205 if (rc > 0) 206 DEBUGP(4, dev, "woke up: BulkIn full\n"); 207 else if (rc == 0) 208 DEBUGP(4, dev, "woke up: BulkIn not full, returning 0 :(\n"); 209 else if (rc < 0) 210 DEBUGP(4, dev, "woke up: signal arrived\n"); 211 212 return rc; 213} 214 215static ssize_t cm4040_read(struct file *filp, char __user *buf, 216 size_t count, loff_t *ppos) 217{ 218 struct reader_dev *dev = filp->private_data; 219 int iobase = dev->p_dev->resource[0]->start; 220 size_t bytes_to_read; 221 unsigned long i; 222 size_t min_bytes_to_read; 223 int rc; 224 225 DEBUGP(2, dev, "-> cm4040_read(%s,%d)\n", current->comm, current->pid); 226 227 if (count == 0) 228 return 0; 229 230 if (count < 10) 231 return -EFAULT; 232 233 if (filp->f_flags & O_NONBLOCK) { 234 DEBUGP(4, dev, "filep->f_flags O_NONBLOCK set\n"); 235 DEBUGP(2, dev, "<- cm4040_read (failure)\n"); 236 return -EAGAIN; 237 } 238 239 if (!pcmcia_dev_present(dev->p_dev)) 240 return -ENODEV; 241 242 for (i = 0; i < 5; i++) { 243 rc = wait_for_bulk_in_ready(dev); 244 if (rc <= 0) { 245 DEBUGP(5, dev, "wait_for_bulk_in_ready rc=%.2x\n", rc); 246 DEBUGP(2, dev, "<- cm4040_read (failed)\n"); 247 if (rc == -ERESTARTSYS) 248 return rc; 249 return -EIO; 250 } 251 dev->r_buf[i] = xinb(iobase + REG_OFFSET_BULK_IN); 252#ifdef CM4040_DEBUG 253 pr_debug("%lu:%2x ", i, dev->r_buf[i]); 254 } 255 pr_debug("\n"); 256#else 257 } 258#endif 259 260 bytes_to_read = 5 + le32_to_cpu(*(__le32 *)&dev->r_buf[1]); 261 262 DEBUGP(6, dev, "BytesToRead=%zu\n", bytes_to_read); 263 264 min_bytes_to_read = min(count, bytes_to_read + 5); 265 min_bytes_to_read = min_t(size_t, min_bytes_to_read, READ_WRITE_BUFFER_SIZE); 266 267 DEBUGP(6, dev, "Min=%zu\n", min_bytes_to_read); 268 269 for (i = 0; i < (min_bytes_to_read-5); i++) { 270 rc = wait_for_bulk_in_ready(dev); 271 if (rc <= 0) { 272 DEBUGP(5, dev, "wait_for_bulk_in_ready rc=%.2x\n", rc); 273 DEBUGP(2, dev, "<- cm4040_read (failed)\n"); 274 if (rc == -ERESTARTSYS) 275 return rc; 276 return -EIO; 277 } 278 dev->r_buf[i+5] = xinb(iobase + REG_OFFSET_BULK_IN); 279#ifdef CM4040_DEBUG 280 pr_debug("%lu:%2x ", i, dev->r_buf[i]); 281 } 282 pr_debug("\n"); 283#else 284 } 285#endif 286 287 *ppos = min_bytes_to_read; 288 if (copy_to_user(buf, dev->r_buf, min_bytes_to_read)) 289 return -EFAULT; 290 291 rc = wait_for_bulk_in_ready(dev); 292 if (rc <= 0) { 293 DEBUGP(5, dev, "wait_for_bulk_in_ready rc=%.2x\n", rc); 294 DEBUGP(2, dev, "<- cm4040_read (failed)\n"); 295 if (rc == -ERESTARTSYS) 296 return rc; 297 return -EIO; 298 } 299 300 rc = write_sync_reg(SCR_READER_TO_HOST_DONE, dev); 301 if (rc <= 0) { 302 DEBUGP(5, dev, "write_sync_reg c=%.2x\n", rc); 303 DEBUGP(2, dev, "<- cm4040_read (failed)\n"); 304 if (rc == -ERESTARTSYS) 305 return rc; 306 else 307 return -EIO; 308 } 309 310 xinb(iobase + REG_OFFSET_BULK_IN); 311 312 DEBUGP(2, dev, "<- cm4040_read (successfully)\n"); 313 return min_bytes_to_read; 314} 315 316static ssize_t cm4040_write(struct file *filp, const char __user *buf, 317 size_t count, loff_t *ppos) 318{ 319 struct reader_dev *dev = filp->private_data; 320 int iobase = dev->p_dev->resource[0]->start; 321 ssize_t rc; 322 int i; 323 unsigned int bytes_to_write; 324 325 DEBUGP(2, dev, "-> cm4040_write(%s,%d)\n", current->comm, current->pid); 326 327 if (count == 0) { 328 DEBUGP(2, dev, "<- cm4040_write empty read (successfully)\n"); 329 return 0; 330 } 331 332 if ((count < 5) || (count > READ_WRITE_BUFFER_SIZE)) { 333 DEBUGP(2, dev, "<- cm4040_write buffersize=%zd < 5\n", count); 334 return -EIO; 335 } 336 337 if (filp->f_flags & O_NONBLOCK) { 338 DEBUGP(4, dev, "filep->f_flags O_NONBLOCK set\n"); 339 DEBUGP(4, dev, "<- cm4040_write (failure)\n"); 340 return -EAGAIN; 341 } 342 343 if (!pcmcia_dev_present(dev->p_dev)) 344 return -ENODEV; 345 346 bytes_to_write = count; 347 if (copy_from_user(dev->s_buf, buf, bytes_to_write)) 348 return -EFAULT; 349 350 switch (dev->s_buf[0]) { 351 case CMD_PC_TO_RDR_XFRBLOCK: 352 case CMD_PC_TO_RDR_SECURE: 353 case CMD_PC_TO_RDR_TEST_SECURE: 354 case CMD_PC_TO_RDR_OK_SECURE: 355 dev->timeout = CCID_DRIVER_BULK_DEFAULT_TIMEOUT; 356 break; 357 358 case CMD_PC_TO_RDR_ICCPOWERON: 359 dev->timeout = CCID_DRIVER_ASYNC_POWERUP_TIMEOUT; 360 break; 361 362 case CMD_PC_TO_RDR_GETSLOTSTATUS: 363 case CMD_PC_TO_RDR_ICCPOWEROFF: 364 case CMD_PC_TO_RDR_GETPARAMETERS: 365 case CMD_PC_TO_RDR_RESETPARAMETERS: 366 case CMD_PC_TO_RDR_SETPARAMETERS: 367 case CMD_PC_TO_RDR_ESCAPE: 368 case CMD_PC_TO_RDR_ICCCLOCK: 369 default: 370 dev->timeout = CCID_DRIVER_MINIMUM_TIMEOUT; 371 break; 372 } 373 374 rc = write_sync_reg(SCR_HOST_TO_READER_START, dev); 375 if (rc <= 0) { 376 DEBUGP(5, dev, "write_sync_reg c=%.2zx\n", rc); 377 DEBUGP(2, dev, "<- cm4040_write (failed)\n"); 378 if (rc == -ERESTARTSYS) 379 return rc; 380 else 381 return -EIO; 382 } 383 384 DEBUGP(4, dev, "start \n"); 385 386 for (i = 0; i < bytes_to_write; i++) { 387 rc = wait_for_bulk_out_ready(dev); 388 if (rc <= 0) { 389 DEBUGP(5, dev, "wait_for_bulk_out_ready rc=%.2zx\n", 390 rc); 391 DEBUGP(2, dev, "<- cm4040_write (failed)\n"); 392 if (rc == -ERESTARTSYS) 393 return rc; 394 else 395 return -EIO; 396 } 397 398 xoutb(dev->s_buf[i],iobase + REG_OFFSET_BULK_OUT); 399 } 400 DEBUGP(4, dev, "end\n"); 401 402 rc = write_sync_reg(SCR_HOST_TO_READER_DONE, dev); 403 404 if (rc <= 0) { 405 DEBUGP(5, dev, "write_sync_reg c=%.2zx\n", rc); 406 DEBUGP(2, dev, "<- cm4040_write (failed)\n"); 407 if (rc == -ERESTARTSYS) 408 return rc; 409 else 410 return -EIO; 411 } 412 413 DEBUGP(2, dev, "<- cm4040_write (successfully)\n"); 414 return count; 415} 416 417static __poll_t cm4040_poll(struct file *filp, poll_table *wait) 418{ 419 struct reader_dev *dev = filp->private_data; 420 __poll_t mask = 0; 421 422 poll_wait(filp, &dev->poll_wait, wait); 423 424 if (test_and_clear_bit(BS_READABLE, &dev->buffer_status)) 425 mask |= EPOLLIN | EPOLLRDNORM; 426 if (test_and_clear_bit(BS_WRITABLE, &dev->buffer_status)) 427 mask |= EPOLLOUT | EPOLLWRNORM; 428 429 DEBUGP(2, dev, "<- cm4040_poll(%u)\n", mask); 430 431 return mask; 432} 433 434static int cm4040_open(struct inode *inode, struct file *filp) 435{ 436 struct reader_dev *dev; 437 struct pcmcia_device *link; 438 int minor = iminor(inode); 439 int ret; 440 441 if (minor >= CM_MAX_DEV) 442 return -ENODEV; 443 444 mutex_lock(&cm4040_mutex); 445 link = dev_table[minor]; 446 if (link == NULL || !pcmcia_dev_present(link)) { 447 ret = -ENODEV; 448 goto out; 449 } 450 451 if (link->open) { 452 ret = -EBUSY; 453 goto out; 454 } 455 456 dev = link->priv; 457 filp->private_data = dev; 458 459 if (filp->f_flags & O_NONBLOCK) { 460 DEBUGP(4, dev, "filep->f_flags O_NONBLOCK set\n"); 461 ret = -EAGAIN; 462 goto out; 463 } 464 465 link->open = 1; 466 467 mod_timer(&dev->poll_timer, jiffies + POLL_PERIOD); 468 469 DEBUGP(2, dev, "<- cm4040_open (successfully)\n"); 470 ret = nonseekable_open(inode, filp); 471out: 472 mutex_unlock(&cm4040_mutex); 473 return ret; 474} 475 476static int cm4040_close(struct inode *inode, struct file *filp) 477{ 478 struct reader_dev *dev = filp->private_data; 479 struct pcmcia_device *link; 480 int minor = iminor(inode); 481 482 DEBUGP(2, dev, "-> cm4040_close(maj/min=%d.%d)\n", imajor(inode), 483 iminor(inode)); 484 485 if (minor >= CM_MAX_DEV) 486 return -ENODEV; 487 488 link = dev_table[minor]; 489 if (link == NULL) 490 return -ENODEV; 491 492 cm4040_stop_poll(dev); 493 494 link->open = 0; 495 wake_up(&dev->devq); 496 497 DEBUGP(2, dev, "<- cm4040_close\n"); 498 return 0; 499} 500 501static void cm4040_reader_release(struct pcmcia_device *link) 502{ 503 struct reader_dev *dev = link->priv; 504 505 DEBUGP(3, dev, "-> cm4040_reader_release\n"); 506 while (link->open) { 507 DEBUGP(3, dev, MODULE_NAME ": delaying release " 508 "until process has terminated\n"); 509 wait_event(dev->devq, (link->open == 0)); 510 } 511 DEBUGP(3, dev, "<- cm4040_reader_release\n"); 512 return; 513} 514 515static int cm4040_config_check(struct pcmcia_device *p_dev, void *priv_data) 516{ 517 return pcmcia_request_io(p_dev); 518} 519 520 521static int reader_config(struct pcmcia_device *link, int devno) 522{ 523 struct reader_dev *dev; 524 int fail_rc; 525 526 link->config_flags |= CONF_AUTO_SET_IO; 527 528 if (pcmcia_loop_config(link, cm4040_config_check, NULL)) 529 goto cs_release; 530 531 fail_rc = pcmcia_enable_device(link); 532 if (fail_rc != 0) { 533 dev_info(&link->dev, "pcmcia_enable_device failed 0x%x\n", 534 fail_rc); 535 goto cs_release; 536 } 537 538 dev = link->priv; 539 540 DEBUGP(2, dev, "device " DEVICE_NAME "%d at %pR\n", devno, 541 link->resource[0]); 542 DEBUGP(2, dev, "<- reader_config (succ)\n"); 543 544 return 0; 545 546cs_release: 547 reader_release(link); 548 return -ENODEV; 549} 550 551static void reader_release(struct pcmcia_device *link) 552{ 553 cm4040_reader_release(link); 554 pcmcia_disable_device(link); 555} 556 557static int reader_probe(struct pcmcia_device *link) 558{ 559 struct reader_dev *dev; 560 int i, ret; 561 562 for (i = 0; i < CM_MAX_DEV; i++) { 563 if (dev_table[i] == NULL) 564 break; 565 } 566 567 if (i == CM_MAX_DEV) 568 return -ENODEV; 569 570 dev = kzalloc(sizeof(struct reader_dev), GFP_KERNEL); 571 if (dev == NULL) 572 return -ENOMEM; 573 574 dev->timeout = CCID_DRIVER_MINIMUM_TIMEOUT; 575 dev->buffer_status = 0; 576 577 link->priv = dev; 578 dev->p_dev = link; 579 580 dev_table[i] = link; 581 582 init_waitqueue_head(&dev->devq); 583 init_waitqueue_head(&dev->poll_wait); 584 init_waitqueue_head(&dev->read_wait); 585 init_waitqueue_head(&dev->write_wait); 586 timer_setup(&dev->poll_timer, cm4040_do_poll, 0); 587 588 ret = reader_config(link, i); 589 if (ret) { 590 dev_table[i] = NULL; 591 kfree(dev); 592 return ret; 593 } 594 595 device_create(cmx_class, NULL, MKDEV(major, i), NULL, "cmx%d", i); 596 597 return 0; 598} 599 600static void reader_detach(struct pcmcia_device *link) 601{ 602 struct reader_dev *dev = link->priv; 603 int devno; 604 605 /* find device */ 606 for (devno = 0; devno < CM_MAX_DEV; devno++) { 607 if (dev_table[devno] == link) 608 break; 609 } 610 if (devno == CM_MAX_DEV) 611 return; 612 613 reader_release(link); 614 615 dev_table[devno] = NULL; 616 kfree(dev); 617 618 device_destroy(cmx_class, MKDEV(major, devno)); 619 620 return; 621} 622 623static const struct file_operations reader_fops = { 624 .owner = THIS_MODULE, 625 .read = cm4040_read, 626 .write = cm4040_write, 627 .open = cm4040_open, 628 .release = cm4040_close, 629 .poll = cm4040_poll, 630 .llseek = no_llseek, 631}; 632 633static const struct pcmcia_device_id cm4040_ids[] = { 634 PCMCIA_DEVICE_MANF_CARD(0x0223, 0x0200), 635 PCMCIA_DEVICE_PROD_ID12("OMNIKEY", "CardMan 4040", 636 0xE32CDD8C, 0x8F23318B), 637 PCMCIA_DEVICE_NULL, 638}; 639MODULE_DEVICE_TABLE(pcmcia, cm4040_ids); 640 641static struct pcmcia_driver reader_driver = { 642 .owner = THIS_MODULE, 643 .name = "cm4040_cs", 644 .probe = reader_probe, 645 .remove = reader_detach, 646 .id_table = cm4040_ids, 647}; 648 649static int __init cm4040_init(void) 650{ 651 int rc; 652 653 cmx_class = class_create(THIS_MODULE, "cardman_4040"); 654 if (IS_ERR(cmx_class)) 655 return PTR_ERR(cmx_class); 656 657 major = register_chrdev(0, DEVICE_NAME, &reader_fops); 658 if (major < 0) { 659 printk(KERN_WARNING MODULE_NAME 660 ": could not get major number\n"); 661 class_destroy(cmx_class); 662 return major; 663 } 664 665 rc = pcmcia_register_driver(&reader_driver); 666 if (rc < 0) { 667 unregister_chrdev(major, DEVICE_NAME); 668 class_destroy(cmx_class); 669 return rc; 670 } 671 672 return 0; 673} 674 675static void __exit cm4040_exit(void) 676{ 677 pcmcia_unregister_driver(&reader_driver); 678 unregister_chrdev(major, DEVICE_NAME); 679 class_destroy(cmx_class); 680} 681 682module_init(cm4040_init); 683module_exit(cm4040_exit); 684MODULE_LICENSE("Dual BSD/GPL");