linux/drivers/char/pcmcia/cm4040_cs.c
<<
>>
Prefs
   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/* #define PCMCIA_DEBUG 6 */
  21
  22#include <linux/kernel.h>
  23#include <linux/module.h>
  24#include <linux/slab.h>
  25#include <linux/init.h>
  26#include <linux/fs.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>
  32#include <asm/io.h>
  33
  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>
  40
  41#include "cm4040_cs.h"
  42
  43
  44#ifdef PCMCIA_DEBUG
  45#define reader_to_dev(x)        (&handle_to_dev(x->p_dev))
  46static int pc_debug = PCMCIA_DEBUG;
  47module_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,     \
  51                           __func__ , ##args);                  \
  52        } while (0)
  53#else
  54#define DEBUGP(n, rdr, x, args...)
  55#endif
  56
  57static char *version =
  58"OMNIKEY CardMan 4040 v1.1.0gm5 - All bugs added by Harald Welte";
  59
  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
  65
  66/* how often to poll for fifo status change */
  67#define POLL_PERIOD                             msecs_to_jiffies(10)
  68
  69static void reader_release(struct pcmcia_device *link);
  70
  71static int major;
  72static struct class *cmx_class;
  73
  74#define         BS_READABLE     0x01
  75#define         BS_WRITABLE     0x02
  76
  77struct reader_dev {
  78        struct pcmcia_device    *p_dev;
  79        dev_node_t              node;
  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;
  89};
  90
  91static struct pcmcia_device *dev_table[CM_MAX_DEV];
  92
  93#ifndef PCMCIA_DEBUG
  94#define xoutb   outb
  95#define xinb    inb
  96#else
  97static inline void xoutb(unsigned char val, unsigned short port)
  98{
  99        if (pc_debug >= 7)
 100                printk(KERN_DEBUG "outb(val=%.2x,port=%.4x)\n", val, port);
 101        outb(val, port);
 102}
 103
 104static inline unsigned char xinb(unsigned short port)
 105{
 106        unsigned char val;
 107
 108        val = inb(port);
 109        if (pc_debug >= 7)
 110                printk(KERN_DEBUG "%.2x=inb(%.4x)\n", val, port);
 111        return val;
 112}
 113#endif
 114
 115/* poll the device fifo status register.  not to be confused with
 116 * the poll syscall. */
 117static void cm4040_do_poll(unsigned long dummy)
 118{
 119        struct reader_dev *dev = (struct reader_dev *) dummy;
 120        unsigned int obs = xinb(dev->p_dev->io.BasePort1
 121                                + REG_OFFSET_BUFFER_STATUS);
 122
 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);
 127        } else
 128                clear_bit(BS_READABLE, &dev->buffer_status);
 129
 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);
 134        } else
 135                clear_bit(BS_WRITABLE, &dev->buffer_status);
 136
 137        if (dev->buffer_status)
 138                wake_up_interruptible(&dev->poll_wait);
 139
 140        mod_timer(&dev->poll_timer, jiffies + POLL_PERIOD);
 141}
 142
 143static void cm4040_stop_poll(struct reader_dev *dev)
 144{
 145        del_timer_sync(&dev->poll_timer);
 146}
 147
 148static int wait_for_bulk_out_ready(struct reader_dev *dev)
 149{
 150        int i, rc;
 151        int iobase = dev->p_dev->io.BasePort1;
 152
 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);
 157                        return 1;
 158                }
 159        }
 160
 161        DEBUGP(4, dev, "wait_event_interruptible_timeout(timeout=%ld\n",
 162                dev->timeout);
 163        rc = wait_event_interruptible_timeout(dev->write_wait,
 164                                              test_and_clear_bit(BS_WRITABLE,
 165                                                       &dev->buffer_status),
 166                                              dev->timeout);
 167
 168        if (rc > 0)
 169                DEBUGP(4, dev, "woke up: BulkOut empty\n");
 170        else if (rc == 0)
 171                DEBUGP(4, dev, "woke up: BulkOut full, returning 0 :(\n");
 172        else if (rc < 0)
 173                DEBUGP(4, dev, "woke up: signal arrived\n");
 174
 175        return rc;
 176}
 177
 178/* Write to Sync Control Register */
 179static int write_sync_reg(unsigned char val, struct reader_dev *dev)
 180{
 181        int iobase = dev->p_dev->io.BasePort1;
 182        int rc;
 183
 184        rc = wait_for_bulk_out_ready(dev);
 185        if (rc <= 0)
 186                return rc;
 187
 188        xoutb(val, iobase + REG_OFFSET_SYNC_CONTROL);
 189        rc = wait_for_bulk_out_ready(dev);
 190        if (rc <= 0)
 191                return rc;
 192
 193        return 1;
 194}
 195
 196static int wait_for_bulk_in_ready(struct reader_dev *dev)
 197{
 198        int i, rc;
 199        int iobase = dev->p_dev->io.BasePort1;
 200
 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);
 205                        return 1;
 206                }
 207        }
 208
 209        DEBUGP(4, dev, "wait_event_interruptible_timeout(timeout=%ld\n",
 210                dev->timeout);
 211        rc = wait_event_interruptible_timeout(dev->read_wait,
 212                                              test_and_clear_bit(BS_READABLE,
 213                                                        &dev->buffer_status),
 214                                              dev->timeout);
 215        if (rc > 0)
 216                DEBUGP(4, dev, "woke up: BulkIn full\n");
 217        else if (rc == 0)
 218                DEBUGP(4, dev, "woke up: BulkIn not full, returning 0 :(\n");
 219        else if (rc < 0)
 220                DEBUGP(4, dev, "woke up: signal arrived\n");
 221
 222        return rc;
 223}
 224
 225static ssize_t cm4040_read(struct file *filp, char __user *buf,
 226                        size_t count, loff_t *ppos)
 227{
 228        struct reader_dev *dev = filp->private_data;
 229        int iobase = dev->p_dev->io.BasePort1;
 230        size_t bytes_to_read;
 231        unsigned long i;
 232        size_t min_bytes_to_read;
 233        int rc;
 234        unsigned char uc;
 235
 236        DEBUGP(2, dev, "-> cm4040_read(%s,%d)\n", current->comm, current->pid);
 237
 238        if (count == 0)
 239                return 0;
 240
 241        if (count < 10)
 242                return -EFAULT;
 243
 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");
 247                return -EAGAIN;
 248        }
 249
 250        if (!pcmcia_dev_present(dev->p_dev))
 251                return -ENODEV;
 252
 253        for (i = 0; i < 5; i++) {
 254                rc = wait_for_bulk_in_ready(dev);
 255                if (rc <= 0) {
 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)
 259                                return rc;
 260                        return -EIO;
 261                }
 262                dev->r_buf[i] = xinb(iobase + REG_OFFSET_BULK_IN);
 263#ifdef PCMCIA_DEBUG
 264                if (pc_debug >= 6)
 265                        printk(KERN_DEBUG "%lu:%2x ", i, dev->r_buf[i]);
 266        }
 267        printk("\n");
 268#else
 269        }
 270#endif
 271
 272        bytes_to_read = 5 + le32_to_cpu(*(__le32 *)&dev->r_buf[1]);
 273
 274        DEBUGP(6, dev, "BytesToRead=%lu\n", bytes_to_read);
 275
 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);
 278
 279        DEBUGP(6, dev, "Min=%lu\n", min_bytes_to_read);
 280
 281        for (i = 0; i < (min_bytes_to_read-5); i++) {
 282                rc = wait_for_bulk_in_ready(dev);
 283                if (rc <= 0) {
 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)
 287                                return rc;
 288                        return -EIO;
 289                }
 290                dev->r_buf[i+5] = xinb(iobase + REG_OFFSET_BULK_IN);
 291#ifdef PCMCIA_DEBUG
 292                if (pc_debug >= 6)
 293                        printk(KERN_DEBUG "%lu:%2x ", i, dev->r_buf[i]);
 294        }
 295        printk("\n");
 296#else
 297        }
 298#endif
 299
 300        *ppos = min_bytes_to_read;
 301        if (copy_to_user(buf, dev->r_buf, min_bytes_to_read))
 302                return -EFAULT;
 303
 304        rc = wait_for_bulk_in_ready(dev);
 305        if (rc <= 0) {
 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)
 309                        return rc;
 310                return -EIO;
 311        }
 312
 313        rc = write_sync_reg(SCR_READER_TO_HOST_DONE, dev);
 314        if (rc <= 0) {
 315                DEBUGP(5, dev, "write_sync_reg c=%.2x\n", rc);
 316                DEBUGP(2, dev, "<- cm4040_read (failed)\n");
 317                if (rc == -ERESTARTSYS)
 318                        return rc;
 319                else
 320                        return -EIO;
 321        }
 322
 323        uc = xinb(iobase + REG_OFFSET_BULK_IN);
 324
 325        DEBUGP(2, dev, "<- cm4040_read (successfully)\n");
 326        return min_bytes_to_read;
 327}
 328
 329static ssize_t cm4040_write(struct file *filp, const char __user *buf,
 330                         size_t count, loff_t *ppos)
 331{
 332        struct reader_dev *dev = filp->private_data;
 333        int iobase = dev->p_dev->io.BasePort1;
 334        ssize_t rc;
 335        int i;
 336        unsigned int bytes_to_write;
 337
 338        DEBUGP(2, dev, "-> cm4040_write(%s,%d)\n", current->comm, current->pid);
 339
 340        if (count == 0) {
 341                DEBUGP(2, dev, "<- cm4040_write empty read (successfully)\n");
 342                return 0;
 343        }
 344
 345        if ((count < 5) || (count > READ_WRITE_BUFFER_SIZE)) {
 346                DEBUGP(2, dev, "<- cm4040_write buffersize=%Zd < 5\n", count);
 347                return -EIO;
 348        }
 349
 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");
 353                return -EAGAIN;
 354        }
 355
 356        if (!pcmcia_dev_present(dev->p_dev))
 357                return -ENODEV;
 358
 359        bytes_to_write = count;
 360        if (copy_from_user(dev->s_buf, buf, bytes_to_write))
 361                return -EFAULT;
 362
 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;
 369                        break;
 370
 371                case CMD_PC_TO_RDR_ICCPOWERON:
 372                        dev->timeout = CCID_DRIVER_ASYNC_POWERUP_TIMEOUT;
 373                        break;
 374
 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:
 382                default:
 383                        dev->timeout = CCID_DRIVER_MINIMUM_TIMEOUT;
 384                        break;
 385        }
 386
 387        rc = write_sync_reg(SCR_HOST_TO_READER_START, dev);
 388        if (rc <= 0) {
 389                DEBUGP(5, dev, "write_sync_reg c=%.2Zx\n", rc);
 390                DEBUGP(2, dev, "<- cm4040_write (failed)\n");
 391                if (rc == -ERESTARTSYS)
 392                        return rc;
 393                else
 394                        return -EIO;
 395        }
 396
 397        DEBUGP(4, dev, "start \n");
 398
 399        for (i = 0; i < bytes_to_write; i++) {
 400                rc = wait_for_bulk_out_ready(dev);
 401                if (rc <= 0) {
 402                        DEBUGP(5, dev, "wait_for_bulk_out_ready rc=%.2Zx\n",
 403                               rc);
 404                        DEBUGP(2, dev, "<- cm4040_write (failed)\n");
 405                        if (rc == -ERESTARTSYS)
 406                                return rc;
 407                        else
 408                                return -EIO;
 409                }
 410
 411                xoutb(dev->s_buf[i],iobase + REG_OFFSET_BULK_OUT);
 412        }
 413        DEBUGP(4, dev, "end\n");
 414
 415        rc = write_sync_reg(SCR_HOST_TO_READER_DONE, dev);
 416
 417        if (rc <= 0) {
 418                DEBUGP(5, dev, "write_sync_reg c=%.2Zx\n", rc);
 419                DEBUGP(2, dev, "<- cm4040_write (failed)\n");
 420                if (rc == -ERESTARTSYS)
 421                        return rc;
 422                else
 423                        return -EIO;
 424        }
 425
 426        DEBUGP(2, dev, "<- cm4040_write (successfully)\n");
 427        return count;
 428}
 429
 430static unsigned int cm4040_poll(struct file *filp, poll_table *wait)
 431{
 432        struct reader_dev *dev = filp->private_data;
 433        unsigned int mask = 0;
 434
 435        poll_wait(filp, &dev->poll_wait, wait);
 436
 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;
 441
 442        DEBUGP(2, dev, "<- cm4040_poll(%u)\n", mask);
 443
 444        return mask;
 445}
 446
 447static int cm4040_open(struct inode *inode, struct file *filp)
 448{
 449        struct reader_dev *dev;
 450        struct pcmcia_device *link;
 451        int minor = iminor(inode);
 452        int ret;
 453
 454        if (minor >= CM_MAX_DEV)
 455                return -ENODEV;
 456
 457        lock_kernel();
 458        link = dev_table[minor];
 459        if (link == NULL || !pcmcia_dev_present(link)) {
 460                ret = -ENODEV;
 461                goto out;
 462        }
 463
 464        if (link->open) {
 465                ret = -EBUSY;
 466                goto out;
 467        }
 468
 469        dev = link->priv;
 470        filp->private_data = dev;
 471
 472        if (filp->f_flags & O_NONBLOCK) {
 473                DEBUGP(4, dev, "filep->f_flags O_NONBLOCK set\n");
 474                ret = -EAGAIN;
 475                goto out;
 476        }
 477
 478        link->open = 1;
 479
 480        dev->poll_timer.data = (unsigned long) dev;
 481        mod_timer(&dev->poll_timer, jiffies + POLL_PERIOD);
 482
 483        DEBUGP(2, dev, "<- cm4040_open (successfully)\n");
 484        ret = nonseekable_open(inode, filp);
 485out:
 486        unlock_kernel();
 487        return ret;
 488}
 489
 490static int cm4040_close(struct inode *inode, struct file *filp)
 491{
 492        struct reader_dev *dev = filp->private_data;
 493        struct pcmcia_device *link;
 494        int minor = iminor(inode);
 495
 496        DEBUGP(2, dev, "-> cm4040_close(maj/min=%d.%d)\n", imajor(inode),
 497              iminor(inode));
 498
 499        if (minor >= CM_MAX_DEV)
 500                return -ENODEV;
 501
 502        link = dev_table[minor];
 503        if (link == NULL)
 504                return -ENODEV;
 505
 506        cm4040_stop_poll(dev);
 507
 508        link->open = 0;
 509        wake_up(&dev->devq);
 510
 511        DEBUGP(2, dev, "<- cm4040_close\n");
 512        return 0;
 513}
 514
 515static void cm4040_reader_release(struct pcmcia_device *link)
 516{
 517        struct reader_dev *dev = link->priv;
 518
 519        DEBUGP(3, dev, "-> cm4040_reader_release\n");
 520        while (link->open) {
 521                DEBUGP(3, dev, KERN_INFO MODULE_NAME ": delaying release "
 522                       "until process has terminated\n");
 523                wait_event(dev->devq, (link->open == 0));
 524        }
 525        DEBUGP(3, dev, "<- cm4040_reader_release\n");
 526        return;
 527}
 528
 529static int cm4040_config_check(struct pcmcia_device *p_dev,
 530                               cistpl_cftable_entry_t *cfg,
 531                               cistpl_cftable_entry_t *dflt,
 532                               unsigned int vcc,
 533                               void *priv_data)
 534{
 535        int rc;
 536        if (!cfg->io.nwin)
 537                return -ENODEV;
 538
 539        /* Get the IOaddr */
 540        p_dev->io.BasePort1 = cfg->io.win[0].base;
 541        p_dev->io.NumPorts1 = cfg->io.win[0].len;
 542        p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
 543        if (!(cfg->io.flags & CISTPL_IO_8BIT))
 544                p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_16;
 545        if (!(cfg->io.flags & CISTPL_IO_16BIT))
 546                p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
 547        p_dev->io.IOAddrLines = cfg->io.flags & CISTPL_IO_LINES_MASK;
 548
 549        rc = pcmcia_request_io(p_dev, &p_dev->io);
 550        dev_printk(KERN_INFO, &handle_to_dev(p_dev),
 551                   "pcmcia_request_io returned 0x%x\n", rc);
 552        return rc;
 553}
 554
 555
 556static int reader_config(struct pcmcia_device *link, int devno)
 557{
 558        struct reader_dev *dev;
 559        int fail_rc;
 560
 561        link->io.BasePort2 = 0;
 562        link->io.NumPorts2 = 0;
 563        link->io.Attributes2 = 0;
 564
 565        if (pcmcia_loop_config(link, cm4040_config_check, NULL))
 566                goto cs_release;
 567
 568        link->conf.IntType = 00000002;
 569
 570        fail_rc = pcmcia_request_configuration(link, &link->conf);
 571        if (fail_rc != 0) {
 572                dev_printk(KERN_INFO, &handle_to_dev(link),
 573                           "pcmcia_request_configuration failed 0x%x\n",
 574                           fail_rc);
 575                goto cs_release;
 576        }
 577
 578        dev = link->priv;
 579        sprintf(dev->node.dev_name, DEVICE_NAME "%d", devno);
 580        dev->node.major = major;
 581        dev->node.minor = devno;
 582        dev->node.next = &dev->node;
 583
 584        DEBUGP(2, dev, "device " DEVICE_NAME "%d at 0x%.4x-0x%.4x\n", devno,
 585              link->io.BasePort1, link->io.BasePort1+link->io.NumPorts1);
 586        DEBUGP(2, dev, "<- reader_config (succ)\n");
 587
 588        return 0;
 589
 590cs_release:
 591        reader_release(link);
 592        return -ENODEV;
 593}
 594
 595static void reader_release(struct pcmcia_device *link)
 596{
 597        cm4040_reader_release(link);
 598        pcmcia_disable_device(link);
 599}
 600
 601static int reader_probe(struct pcmcia_device *link)
 602{
 603        struct reader_dev *dev;
 604        int i, ret;
 605
 606        for (i = 0; i < CM_MAX_DEV; i++) {
 607                if (dev_table[i] == NULL)
 608                        break;
 609        }
 610
 611        if (i == CM_MAX_DEV)
 612                return -ENODEV;
 613
 614        dev = kzalloc(sizeof(struct reader_dev), GFP_KERNEL);
 615        if (dev == NULL)
 616                return -ENOMEM;
 617
 618        dev->timeout = CCID_DRIVER_MINIMUM_TIMEOUT;
 619        dev->buffer_status = 0;
 620
 621        link->priv = dev;
 622        dev->p_dev = link;
 623
 624        link->conf.IntType = INT_MEMORY_AND_IO;
 625        dev_table[i] = link;
 626
 627        init_waitqueue_head(&dev->devq);
 628        init_waitqueue_head(&dev->poll_wait);
 629        init_waitqueue_head(&dev->read_wait);
 630        init_waitqueue_head(&dev->write_wait);
 631        setup_timer(&dev->poll_timer, cm4040_do_poll, 0);
 632
 633        ret = reader_config(link, i);
 634        if (ret) {
 635                dev_table[i] = NULL;
 636                kfree(dev);
 637                return ret;
 638        }
 639
 640        device_create(cmx_class, NULL, MKDEV(major, i), NULL, "cmx%d", i);
 641
 642        return 0;
 643}
 644
 645static void reader_detach(struct pcmcia_device *link)
 646{
 647        struct reader_dev *dev = link->priv;
 648        int devno;
 649
 650        /* find device */
 651        for (devno = 0; devno < CM_MAX_DEV; devno++) {
 652                if (dev_table[devno] == link)
 653                        break;
 654        }
 655        if (devno == CM_MAX_DEV)
 656                return;
 657
 658        reader_release(link);
 659
 660        dev_table[devno] = NULL;
 661        kfree(dev);
 662
 663        device_destroy(cmx_class, MKDEV(major, devno));
 664
 665        return;
 666}
 667
 668static const struct file_operations reader_fops = {
 669        .owner          = THIS_MODULE,
 670        .read           = cm4040_read,
 671        .write          = cm4040_write,
 672        .open           = cm4040_open,
 673        .release        = cm4040_close,
 674        .poll           = cm4040_poll,
 675};
 676
 677static struct pcmcia_device_id cm4040_ids[] = {
 678        PCMCIA_DEVICE_MANF_CARD(0x0223, 0x0200),
 679        PCMCIA_DEVICE_PROD_ID12("OMNIKEY", "CardMan 4040",
 680                                0xE32CDD8C, 0x8F23318B),
 681        PCMCIA_DEVICE_NULL,
 682};
 683MODULE_DEVICE_TABLE(pcmcia, cm4040_ids);
 684
 685static struct pcmcia_driver reader_driver = {
 686        .owner          = THIS_MODULE,
 687        .drv            = {
 688                .name   = "cm4040_cs",
 689        },
 690        .probe          = reader_probe,
 691        .remove         = reader_detach,
 692        .id_table       = cm4040_ids,
 693};
 694
 695static int __init cm4040_init(void)
 696{
 697        int rc;
 698
 699        printk(KERN_INFO "%s\n", version);
 700        cmx_class = class_create(THIS_MODULE, "cardman_4040");
 701        if (IS_ERR(cmx_class))
 702                return PTR_ERR(cmx_class);
 703
 704        major = register_chrdev(0, DEVICE_NAME, &reader_fops);
 705        if (major < 0) {
 706                printk(KERN_WARNING MODULE_NAME
 707                        ": could not get major number\n");
 708                class_destroy(cmx_class);
 709                return major;
 710        }
 711
 712        rc = pcmcia_register_driver(&reader_driver);
 713        if (rc < 0) {
 714                unregister_chrdev(major, DEVICE_NAME);
 715                class_destroy(cmx_class);
 716                return rc;
 717        }
 718
 719        return 0;
 720}
 721
 722static void __exit cm4040_exit(void)
 723{
 724        printk(KERN_INFO MODULE_NAME ": unloading\n");
 725        pcmcia_unregister_driver(&reader_driver);
 726        unregister_chrdev(major, DEVICE_NAME);
 727        class_destroy(cmx_class);
 728}
 729
 730module_init(cm4040_init);
 731module_exit(cm4040_exit);
 732MODULE_LICENSE("Dual BSD/GPL");
 733