linux/drivers/char/pcmcia/cm4000_cs.c
<<
>>
Prefs
   1 /*
   2  * A driver for the PCMCIA Smartcard Reader "Omnikey CardMan Mobile 4000"
   3  *
   4  * cm4000_cs.c support.linux@omnikey.com
   5  *
   6  * Tue Oct 23 11:32:43 GMT 2001 herp - cleaned up header files
   7  * Sun Jan 20 10:11:15 MET 2002 herp - added modversion header files
   8  * Thu Nov 14 16:34:11 GMT 2002 mh   - added PPS functionality
   9  * Tue Nov 19 16:36:27 GMT 2002 mh   - added SUSPEND/RESUME functionailty
  10  * Wed Jul 28 12:55:01 CEST 2004 mh  - kernel 2.6 adjustments
  11  *
  12  * current version: 2.4.0gm4
  13  *
  14  * (C) 2000,2001,2002,2003,2004 Omnikey AG
  15  *
  16  * (C) 2005-2006 Harald Welte <laforge@gnumonks.org>
  17  *     - Adhere to Kernel process/coding-style.rst
  18  *     - Port to 2.6.13 "new" style PCMCIA
  19  *     - Check for copy_{from,to}_user return values
  20  *     - Use nonseekable_open()
  21  *     - add class interface for udev device creation
  22  *
  23  * All rights reserved. Licensed under dual BSD/GPL license.
  24  */
  25
  26#include <linux/kernel.h>
  27#include <linux/module.h>
  28#include <linux/slab.h>
  29#include <linux/init.h>
  30#include <linux/fs.h>
  31#include <linux/delay.h>
  32#include <linux/bitrev.h>
  33#include <linux/mutex.h>
  34#include <linux/uaccess.h>
  35#include <linux/io.h>
  36
  37#include <pcmcia/cistpl.h>
  38#include <pcmcia/cisreg.h>
  39#include <pcmcia/ciscode.h>
  40#include <pcmcia/ds.h>
  41
  42#include <linux/cm4000_cs.h>
  43
  44/* #define ATR_CSUM */
  45
  46#define reader_to_dev(x)        (&x->p_dev->dev)
  47
  48/* n (debug level) is ignored */
  49/* additional debug output may be enabled by re-compiling with
  50 * CM4000_DEBUG set */
  51/* #define CM4000_DEBUG */
  52#define DEBUGP(n, rdr, x, args...) do {                 \
  53                dev_dbg(reader_to_dev(rdr), "%s:" x,    \
  54                           __func__ , ## args);         \
  55        } while (0)
  56
  57static DEFINE_MUTEX(cmm_mutex);
  58
  59#define T_1SEC          (HZ)
  60#define T_10MSEC        msecs_to_jiffies(10)
  61#define T_20MSEC        msecs_to_jiffies(20)
  62#define T_40MSEC        msecs_to_jiffies(40)
  63#define T_50MSEC        msecs_to_jiffies(50)
  64#define T_100MSEC       msecs_to_jiffies(100)
  65#define T_500MSEC       msecs_to_jiffies(500)
  66
  67static void cm4000_release(struct pcmcia_device *link);
  68
  69static int major;               /* major number we get from the kernel */
  70
  71/* note: the first state has to have number 0 always */
  72
  73#define M_FETCH_ATR     0
  74#define M_TIMEOUT_WAIT  1
  75#define M_READ_ATR_LEN  2
  76#define M_READ_ATR      3
  77#define M_ATR_PRESENT   4
  78#define M_BAD_CARD      5
  79#define M_CARDOFF       6
  80
  81#define LOCK_IO                 0
  82#define LOCK_MONITOR            1
  83
  84#define IS_AUTOPPS_ACT           6
  85#define IS_PROCBYTE_PRESENT      7
  86#define IS_INVREV                8
  87#define IS_ANY_T0                9
  88#define IS_ANY_T1               10
  89#define IS_ATR_PRESENT          11
  90#define IS_ATR_VALID            12
  91#define IS_CMM_ABSENT           13
  92#define IS_BAD_LENGTH           14
  93#define IS_BAD_CSUM             15
  94#define IS_BAD_CARD             16
  95
  96#define REG_FLAGS0(x)           (x + 0)
  97#define REG_FLAGS1(x)           (x + 1)
  98#define REG_NUM_BYTES(x)        (x + 2)
  99#define REG_BUF_ADDR(x)         (x + 3)
 100#define REG_BUF_DATA(x)         (x + 4)
 101#define REG_NUM_SEND(x)         (x + 5)
 102#define REG_BAUDRATE(x)         (x + 6)
 103#define REG_STOPBITS(x)         (x + 7)
 104
 105struct cm4000_dev {
 106        struct pcmcia_device *p_dev;
 107
 108        unsigned char atr[MAX_ATR];
 109        unsigned char rbuf[512];
 110        unsigned char sbuf[512];
 111
 112        wait_queue_head_t devq;         /* when removing cardman must not be
 113                                           zeroed! */
 114
 115        wait_queue_head_t ioq;          /* if IO is locked, wait on this Q */
 116        wait_queue_head_t atrq;         /* wait for ATR valid */
 117        wait_queue_head_t readq;        /* used by write to wake blk.read */
 118
 119        /* warning: do not move this fields.
 120         * initialising to zero depends on it - see ZERO_DEV below.  */
 121        unsigned char atr_csum;
 122        unsigned char atr_len_retry;
 123        unsigned short atr_len;
 124        unsigned short rlen;    /* bytes avail. after write */
 125        unsigned short rpos;    /* latest read pos. write zeroes */
 126        unsigned char procbyte; /* T=0 procedure byte */
 127        unsigned char mstate;   /* state of card monitor */
 128        unsigned char cwarn;    /* slow down warning */
 129        unsigned char flags0;   /* cardman IO-flags 0 */
 130        unsigned char flags1;   /* cardman IO-flags 1 */
 131        unsigned int mdelay;    /* variable monitor speeds, in jiffies */
 132
 133        unsigned int baudv;     /* baud value for speed */
 134        unsigned char ta1;
 135        unsigned char proto;    /* T=0, T=1, ... */
 136        unsigned long flags;    /* lock+flags (MONITOR,IO,ATR) * for concurrent
 137                                   access */
 138
 139        unsigned char pts[4];
 140
 141        struct timer_list timer;        /* used to keep monitor running */
 142        int monitor_running;
 143};
 144
 145#define ZERO_DEV(dev)                                           \
 146        memset(&dev->atr_csum,0,                                \
 147                sizeof(struct cm4000_dev) -                     \
 148                offsetof(struct cm4000_dev, atr_csum))
 149
 150static struct pcmcia_device *dev_table[CM4000_MAX_DEV];
 151static struct class *cmm_class;
 152
 153/* This table doesn't use spaces after the comma between fields and thus
 154 * violates process/coding-style.rst.  However, I don't really think wrapping it around will
 155 * make it any clearer to read -HW */
 156static unsigned char fi_di_table[10][14] = {
 157/*FI     00   01   02   03   04   05   06   07   08   09   10   11   12   13 */
 158/*DI */
 159/* 0 */ {0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11},
 160/* 1 */ {0x01,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x91,0x11,0x11,0x11,0x11},
 161/* 2 */ {0x02,0x12,0x22,0x32,0x11,0x11,0x11,0x11,0x11,0x92,0xA2,0xB2,0x11,0x11},
 162/* 3 */ {0x03,0x13,0x23,0x33,0x43,0x53,0x63,0x11,0x11,0x93,0xA3,0xB3,0xC3,0xD3},
 163/* 4 */ {0x04,0x14,0x24,0x34,0x44,0x54,0x64,0x11,0x11,0x94,0xA4,0xB4,0xC4,0xD4},
 164/* 5 */ {0x00,0x15,0x25,0x35,0x45,0x55,0x65,0x11,0x11,0x95,0xA5,0xB5,0xC5,0xD5},
 165/* 6 */ {0x06,0x16,0x26,0x36,0x46,0x56,0x66,0x11,0x11,0x96,0xA6,0xB6,0xC6,0xD6},
 166/* 7 */ {0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11},
 167/* 8 */ {0x08,0x11,0x28,0x38,0x48,0x58,0x68,0x11,0x11,0x98,0xA8,0xB8,0xC8,0xD8},
 168/* 9 */ {0x09,0x19,0x29,0x39,0x49,0x59,0x69,0x11,0x11,0x99,0xA9,0xB9,0xC9,0xD9}
 169};
 170
 171#ifndef CM4000_DEBUG
 172#define xoutb   outb
 173#define xinb    inb
 174#else
 175static inline void xoutb(unsigned char val, unsigned short port)
 176{
 177        pr_debug("outb(val=%.2x,port=%.4x)\n", val, port);
 178        outb(val, port);
 179}
 180static inline unsigned char xinb(unsigned short port)
 181{
 182        unsigned char val;
 183
 184        val = inb(port);
 185        pr_debug("%.2x=inb(%.4x)\n", val, port);
 186
 187        return val;
 188}
 189#endif
 190
 191static inline unsigned char invert_revert(unsigned char ch)
 192{
 193        return bitrev8(~ch);
 194}
 195
 196static void str_invert_revert(unsigned char *b, int len)
 197{
 198        int i;
 199
 200        for (i = 0; i < len; i++)
 201                b[i] = invert_revert(b[i]);
 202}
 203
 204#define ATRLENCK(dev,pos) \
 205        if (pos>=dev->atr_len || pos>=MAX_ATR) \
 206                goto return_0;
 207
 208static unsigned int calc_baudv(unsigned char fidi)
 209{
 210        unsigned int wcrcf, wbrcf, fi_rfu, di_rfu;
 211
 212        fi_rfu = 372;
 213        di_rfu = 1;
 214
 215        /* FI */
 216        switch ((fidi >> 4) & 0x0F) {
 217        case 0x00:
 218                wcrcf = 372;
 219                break;
 220        case 0x01:
 221                wcrcf = 372;
 222                break;
 223        case 0x02:
 224                wcrcf = 558;
 225                break;
 226        case 0x03:
 227                wcrcf = 744;
 228                break;
 229        case 0x04:
 230                wcrcf = 1116;
 231                break;
 232        case 0x05:
 233                wcrcf = 1488;
 234                break;
 235        case 0x06:
 236                wcrcf = 1860;
 237                break;
 238        case 0x07:
 239                wcrcf = fi_rfu;
 240                break;
 241        case 0x08:
 242                wcrcf = fi_rfu;
 243                break;
 244        case 0x09:
 245                wcrcf = 512;
 246                break;
 247        case 0x0A:
 248                wcrcf = 768;
 249                break;
 250        case 0x0B:
 251                wcrcf = 1024;
 252                break;
 253        case 0x0C:
 254                wcrcf = 1536;
 255                break;
 256        case 0x0D:
 257                wcrcf = 2048;
 258                break;
 259        default:
 260                wcrcf = fi_rfu;
 261                break;
 262        }
 263
 264        /* DI */
 265        switch (fidi & 0x0F) {
 266        case 0x00:
 267                wbrcf = di_rfu;
 268                break;
 269        case 0x01:
 270                wbrcf = 1;
 271                break;
 272        case 0x02:
 273                wbrcf = 2;
 274                break;
 275        case 0x03:
 276                wbrcf = 4;
 277                break;
 278        case 0x04:
 279                wbrcf = 8;
 280                break;
 281        case 0x05:
 282                wbrcf = 16;
 283                break;
 284        case 0x06:
 285                wbrcf = 32;
 286                break;
 287        case 0x07:
 288                wbrcf = di_rfu;
 289                break;
 290        case 0x08:
 291                wbrcf = 12;
 292                break;
 293        case 0x09:
 294                wbrcf = 20;
 295                break;
 296        default:
 297                wbrcf = di_rfu;
 298                break;
 299        }
 300
 301        return (wcrcf / wbrcf);
 302}
 303
 304static unsigned short io_read_num_rec_bytes(unsigned int iobase,
 305                                            unsigned short *s)
 306{
 307        unsigned short tmp;
 308
 309        tmp = *s = 0;
 310        do {
 311                *s = tmp;
 312                tmp = inb(REG_NUM_BYTES(iobase)) |
 313                                (inb(REG_FLAGS0(iobase)) & 4 ? 0x100 : 0);
 314        } while (tmp != *s);
 315
 316        return *s;
 317}
 318
 319static int parse_atr(struct cm4000_dev *dev)
 320{
 321        unsigned char any_t1, any_t0;
 322        unsigned char ch, ifno;
 323        int ix, done;
 324
 325        DEBUGP(3, dev, "-> parse_atr: dev->atr_len = %i\n", dev->atr_len);
 326
 327        if (dev->atr_len < 3) {
 328                DEBUGP(5, dev, "parse_atr: atr_len < 3\n");
 329                return 0;
 330        }
 331
 332        if (dev->atr[0] == 0x3f)
 333                set_bit(IS_INVREV, &dev->flags);
 334        else
 335                clear_bit(IS_INVREV, &dev->flags);
 336        ix = 1;
 337        ifno = 1;
 338        ch = dev->atr[1];
 339        dev->proto = 0;         /* XXX PROTO */
 340        any_t1 = any_t0 = done = 0;
 341        dev->ta1 = 0x11;        /* defaults to 9600 baud */
 342        do {
 343                if (ifno == 1 && (ch & 0x10)) {
 344                        /* read first interface byte and TA1 is present */
 345                        dev->ta1 = dev->atr[2];
 346                        DEBUGP(5, dev, "Card says FiDi is 0x%.2x\n", dev->ta1);
 347                        ifno++;
 348                } else if ((ifno == 2) && (ch & 0x10)) { /* TA(2) */
 349                        dev->ta1 = 0x11;
 350                        ifno++;
 351                }
 352
 353                DEBUGP(5, dev, "Yi=%.2x\n", ch & 0xf0);
 354                ix += ((ch & 0x10) >> 4)        /* no of int.face chars */
 355                    +((ch & 0x20) >> 5)
 356                    + ((ch & 0x40) >> 6)
 357                    + ((ch & 0x80) >> 7);
 358                /* ATRLENCK(dev,ix); */
 359                if (ch & 0x80) {        /* TDi */
 360                        ch = dev->atr[ix];
 361                        if ((ch & 0x0f)) {
 362                                any_t1 = 1;
 363                                DEBUGP(5, dev, "card is capable of T=1\n");
 364                        } else {
 365                                any_t0 = 1;
 366                                DEBUGP(5, dev, "card is capable of T=0\n");
 367                        }
 368                } else
 369                        done = 1;
 370        } while (!done);
 371
 372        DEBUGP(5, dev, "ix=%d noHist=%d any_t1=%d\n",
 373              ix, dev->atr[1] & 15, any_t1);
 374        if (ix + 1 + (dev->atr[1] & 0x0f) + any_t1 != dev->atr_len) {
 375                DEBUGP(5, dev, "length error\n");
 376                return 0;
 377        }
 378        if (any_t0)
 379                set_bit(IS_ANY_T0, &dev->flags);
 380
 381        if (any_t1) {           /* compute csum */
 382                dev->atr_csum = 0;
 383#ifdef ATR_CSUM
 384                for (i = 1; i < dev->atr_len; i++)
 385                        dev->atr_csum ^= dev->atr[i];
 386                if (dev->atr_csum) {
 387                        set_bit(IS_BAD_CSUM, &dev->flags);
 388                        DEBUGP(5, dev, "bad checksum\n");
 389                        goto return_0;
 390                }
 391#endif
 392                if (any_t0 == 0)
 393                        dev->proto = 1; /* XXX PROTO */
 394                set_bit(IS_ANY_T1, &dev->flags);
 395        }
 396
 397        return 1;
 398}
 399
 400struct card_fixup {
 401        char atr[12];
 402        u_int8_t atr_len;
 403        u_int8_t stopbits;
 404};
 405
 406static struct card_fixup card_fixups[] = {
 407        {       /* ACOS */
 408                .atr = { 0x3b, 0xb3, 0x11, 0x00, 0x00, 0x41, 0x01 },
 409                .atr_len = 7,
 410                .stopbits = 0x03,
 411        },
 412        {       /* Motorola */
 413                .atr = {0x3b, 0x76, 0x13, 0x00, 0x00, 0x80, 0x62, 0x07,
 414                        0x41, 0x81, 0x81 },
 415                .atr_len = 11,
 416                .stopbits = 0x04,
 417        },
 418};
 419
 420static void set_cardparameter(struct cm4000_dev *dev)
 421{
 422        int i;
 423        unsigned int iobase = dev->p_dev->resource[0]->start;
 424        u_int8_t stopbits = 0x02; /* ISO default */
 425
 426        DEBUGP(3, dev, "-> set_cardparameter\n");
 427
 428        dev->flags1 = dev->flags1 | (((dev->baudv - 1) & 0x0100) >> 8);
 429        xoutb(dev->flags1, REG_FLAGS1(iobase));
 430        DEBUGP(5, dev, "flags1 = 0x%02x\n", dev->flags1);
 431
 432        /* set baudrate */
 433        xoutb((unsigned char)((dev->baudv - 1) & 0xFF), REG_BAUDRATE(iobase));
 434
 435        DEBUGP(5, dev, "baudv = %i -> write 0x%02x\n", dev->baudv,
 436              ((dev->baudv - 1) & 0xFF));
 437
 438        /* set stopbits */
 439        for (i = 0; i < ARRAY_SIZE(card_fixups); i++) {
 440                if (!memcmp(dev->atr, card_fixups[i].atr,
 441                            card_fixups[i].atr_len))
 442                        stopbits = card_fixups[i].stopbits;
 443        }
 444        xoutb(stopbits, REG_STOPBITS(iobase));
 445
 446        DEBUGP(3, dev, "<- set_cardparameter\n");
 447}
 448
 449static int set_protocol(struct cm4000_dev *dev, struct ptsreq *ptsreq)
 450{
 451
 452        unsigned long tmp, i;
 453        unsigned short num_bytes_read;
 454        unsigned char pts_reply[4];
 455        ssize_t rc;
 456        unsigned int iobase = dev->p_dev->resource[0]->start;
 457
 458        rc = 0;
 459
 460        DEBUGP(3, dev, "-> set_protocol\n");
 461        DEBUGP(5, dev, "ptsreq->Protocol = 0x%.8x, ptsreq->Flags=0x%.8x, "
 462                 "ptsreq->pts1=0x%.2x, ptsreq->pts2=0x%.2x, "
 463                 "ptsreq->pts3=0x%.2x\n", (unsigned int)ptsreq->protocol,
 464                 (unsigned int)ptsreq->flags, ptsreq->pts1, ptsreq->pts2,
 465                 ptsreq->pts3);
 466
 467        /* Fill PTS structure */
 468        dev->pts[0] = 0xff;
 469        dev->pts[1] = 0x00;
 470        tmp = ptsreq->protocol;
 471        while ((tmp = (tmp >> 1)) > 0)
 472                dev->pts[1]++;
 473        dev->proto = dev->pts[1];       /* Set new protocol */
 474        dev->pts[1] = (0x01 << 4) | (dev->pts[1]);
 475
 476        /* Correct Fi/Di according to CM4000 Fi/Di table */
 477        DEBUGP(5, dev, "Ta(1) from ATR is 0x%.2x\n", dev->ta1);
 478        /* set Fi/Di according to ATR TA(1) */
 479        dev->pts[2] = fi_di_table[dev->ta1 & 0x0F][(dev->ta1 >> 4) & 0x0F];
 480
 481        /* Calculate PCK character */
 482        dev->pts[3] = dev->pts[0] ^ dev->pts[1] ^ dev->pts[2];
 483
 484        DEBUGP(5, dev, "pts0=%.2x, pts1=%.2x, pts2=%.2x, pts3=%.2x\n",
 485               dev->pts[0], dev->pts[1], dev->pts[2], dev->pts[3]);
 486
 487        /* check card convention */
 488        if (test_bit(IS_INVREV, &dev->flags))
 489                str_invert_revert(dev->pts, 4);
 490
 491        /* reset SM */
 492        xoutb(0x80, REG_FLAGS0(iobase));
 493
 494        /* Enable access to the message buffer */
 495        DEBUGP(5, dev, "Enable access to the messages buffer\n");
 496        dev->flags1 = 0x20      /* T_Active */
 497            | (test_bit(IS_INVREV, &dev->flags) ? 0x02 : 0x00) /* inv parity */
 498            | ((dev->baudv >> 8) & 0x01);       /* MSB-baud */
 499        xoutb(dev->flags1, REG_FLAGS1(iobase));
 500
 501        DEBUGP(5, dev, "Enable message buffer -> flags1 = 0x%.2x\n",
 502               dev->flags1);
 503
 504        /* write challenge to the buffer */
 505        DEBUGP(5, dev, "Write challenge to buffer: ");
 506        for (i = 0; i < 4; i++) {
 507                xoutb(i, REG_BUF_ADDR(iobase));
 508                xoutb(dev->pts[i], REG_BUF_DATA(iobase));       /* buf data */
 509#ifdef CM4000_DEBUG
 510                pr_debug("0x%.2x ", dev->pts[i]);
 511        }
 512        pr_debug("\n");
 513#else
 514        }
 515#endif
 516
 517        /* set number of bytes to write */
 518        DEBUGP(5, dev, "Set number of bytes to write\n");
 519        xoutb(0x04, REG_NUM_SEND(iobase));
 520
 521        /* Trigger CARDMAN CONTROLLER */
 522        xoutb(0x50, REG_FLAGS0(iobase));
 523
 524        /* Monitor progress */
 525        /* wait for xmit done */
 526        DEBUGP(5, dev, "Waiting for NumRecBytes getting valid\n");
 527
 528        for (i = 0; i < 100; i++) {
 529                if (inb(REG_FLAGS0(iobase)) & 0x08) {
 530                        DEBUGP(5, dev, "NumRecBytes is valid\n");
 531                        break;
 532                }
 533                usleep_range(10000, 11000);
 534        }
 535        if (i == 100) {
 536                DEBUGP(5, dev, "Timeout waiting for NumRecBytes getting "
 537                       "valid\n");
 538                rc = -EIO;
 539                goto exit_setprotocol;
 540        }
 541
 542        DEBUGP(5, dev, "Reading NumRecBytes\n");
 543        for (i = 0; i < 100; i++) {
 544                io_read_num_rec_bytes(iobase, &num_bytes_read);
 545                if (num_bytes_read >= 4) {
 546                        DEBUGP(2, dev, "NumRecBytes = %i\n", num_bytes_read);
 547                        if (num_bytes_read > 4) {
 548                                rc = -EIO;
 549                                goto exit_setprotocol;
 550                        }
 551                        break;
 552                }
 553                usleep_range(10000, 11000);
 554        }
 555
 556        /* check whether it is a short PTS reply? */
 557        if (num_bytes_read == 3)
 558                i = 0;
 559
 560        if (i == 100) {
 561                DEBUGP(5, dev, "Timeout reading num_bytes_read\n");
 562                rc = -EIO;
 563                goto exit_setprotocol;
 564        }
 565
 566        DEBUGP(5, dev, "Reset the CARDMAN CONTROLLER\n");
 567        xoutb(0x80, REG_FLAGS0(iobase));
 568
 569        /* Read PPS reply */
 570        DEBUGP(5, dev, "Read PPS reply\n");
 571        for (i = 0; i < num_bytes_read; i++) {
 572                xoutb(i, REG_BUF_ADDR(iobase));
 573                pts_reply[i] = inb(REG_BUF_DATA(iobase));
 574        }
 575
 576#ifdef CM4000_DEBUG
 577        DEBUGP(2, dev, "PTSreply: ");
 578        for (i = 0; i < num_bytes_read; i++) {
 579                pr_debug("0x%.2x ", pts_reply[i]);
 580        }
 581        pr_debug("\n");
 582#endif  /* CM4000_DEBUG */
 583
 584        DEBUGP(5, dev, "Clear Tactive in Flags1\n");
 585        xoutb(0x20, REG_FLAGS1(iobase));
 586
 587        /* Compare ptsreq and ptsreply */
 588        if ((dev->pts[0] == pts_reply[0]) &&
 589            (dev->pts[1] == pts_reply[1]) &&
 590            (dev->pts[2] == pts_reply[2]) && (dev->pts[3] == pts_reply[3])) {
 591                /* setcardparameter according to PPS */
 592                dev->baudv = calc_baudv(dev->pts[2]);
 593                set_cardparameter(dev);
 594        } else if ((dev->pts[0] == pts_reply[0]) &&
 595                   ((dev->pts[1] & 0xef) == pts_reply[1]) &&
 596                   ((pts_reply[0] ^ pts_reply[1]) == pts_reply[2])) {
 597                /* short PTS reply, set card parameter to default values */
 598                dev->baudv = calc_baudv(0x11);
 599                set_cardparameter(dev);
 600        } else
 601                rc = -EIO;
 602
 603exit_setprotocol:
 604        DEBUGP(3, dev, "<- set_protocol\n");
 605        return rc;
 606}
 607
 608static int io_detect_cm4000(unsigned int iobase, struct cm4000_dev *dev)
 609{
 610
 611        /* note: statemachine is assumed to be reset */
 612        if (inb(REG_FLAGS0(iobase)) & 8) {
 613                clear_bit(IS_ATR_VALID, &dev->flags);
 614                set_bit(IS_CMM_ABSENT, &dev->flags);
 615                return 0;       /* detect CMM = 1 -> failure */
 616        }
 617        /* xoutb(0x40, REG_FLAGS1(iobase)); detectCMM */
 618        xoutb(dev->flags1 | 0x40, REG_FLAGS1(iobase));
 619        if ((inb(REG_FLAGS0(iobase)) & 8) == 0) {
 620                clear_bit(IS_ATR_VALID, &dev->flags);
 621                set_bit(IS_CMM_ABSENT, &dev->flags);
 622                return 0;       /* detect CMM=0 -> failure */
 623        }
 624        /* clear detectCMM again by restoring original flags1 */
 625        xoutb(dev->flags1, REG_FLAGS1(iobase));
 626        return 1;
 627}
 628
 629static void terminate_monitor(struct cm4000_dev *dev)
 630{
 631
 632        /* tell the monitor to stop and wait until
 633         * it terminates.
 634         */
 635        DEBUGP(3, dev, "-> terminate_monitor\n");
 636        wait_event_interruptible(dev->devq,
 637                                 test_and_set_bit(LOCK_MONITOR,
 638                                                  (void *)&dev->flags));
 639
 640        /* now, LOCK_MONITOR has been set.
 641         * allow a last cycle in the monitor.
 642         * the monitor will indicate that it has
 643         * finished by clearing this bit.
 644         */
 645        DEBUGP(5, dev, "Now allow last cycle of monitor!\n");
 646        while (test_bit(LOCK_MONITOR, (void *)&dev->flags))
 647                msleep(25);
 648
 649        DEBUGP(5, dev, "Delete timer\n");
 650        del_timer_sync(&dev->timer);
 651#ifdef CM4000_DEBUG
 652        dev->monitor_running = 0;
 653#endif
 654
 655        DEBUGP(3, dev, "<- terminate_monitor\n");
 656}
 657
 658/*
 659 * monitor the card every 50msec. as a side-effect, retrieve the
 660 * atr once a card is inserted. another side-effect of retrieving the
 661 * atr is that the card will be powered on, so there is no need to
 662 * power on the card explicitly from the application: the driver
 663 * is already doing that for you.
 664 */
 665
 666static void monitor_card(struct timer_list *t)
 667{
 668        struct cm4000_dev *dev = from_timer(dev, t, timer);
 669        unsigned int iobase = dev->p_dev->resource[0]->start;
 670        unsigned short s;
 671        struct ptsreq ptsreq;
 672        int i, atrc;
 673
 674        DEBUGP(7, dev, "->  monitor_card\n");
 675
 676        /* if someone has set the lock for us: we're done! */
 677        if (test_and_set_bit(LOCK_MONITOR, &dev->flags)) {
 678                DEBUGP(4, dev, "About to stop monitor\n");
 679                /* no */
 680                dev->rlen =
 681                    dev->rpos =
 682                    dev->atr_csum = dev->atr_len_retry = dev->cwarn = 0;
 683                dev->mstate = M_FETCH_ATR;
 684                clear_bit(LOCK_MONITOR, &dev->flags);
 685                /* close et al. are sleeping on devq, so wake it */
 686                wake_up_interruptible(&dev->devq);
 687                DEBUGP(2, dev, "<- monitor_card (we are done now)\n");
 688                return;
 689        }
 690
 691        /* try to lock io: if it is already locked, just add another timer */
 692        if (test_and_set_bit(LOCK_IO, (void *)&dev->flags)) {
 693                DEBUGP(4, dev, "Couldn't get IO lock\n");
 694                goto return_with_timer;
 695        }
 696
 697        /* is a card/a reader inserted at all ? */
 698        dev->flags0 = xinb(REG_FLAGS0(iobase));
 699        DEBUGP(7, dev, "dev->flags0 = 0x%2x\n", dev->flags0);
 700        DEBUGP(7, dev, "smartcard present: %s\n",
 701               dev->flags0 & 1 ? "yes" : "no");
 702        DEBUGP(7, dev, "cardman present: %s\n",
 703               dev->flags0 == 0xff ? "no" : "yes");
 704
 705        if ((dev->flags0 & 1) == 0      /* no smartcard inserted */
 706            || dev->flags0 == 0xff) {   /* no cardman inserted */
 707                /* no */
 708                dev->rlen =
 709                    dev->rpos =
 710                    dev->atr_csum = dev->atr_len_retry = dev->cwarn = 0;
 711                dev->mstate = M_FETCH_ATR;
 712
 713                dev->flags &= 0x000000ff; /* only keep IO and MONITOR locks */
 714
 715                if (dev->flags0 == 0xff) {
 716                        DEBUGP(4, dev, "set IS_CMM_ABSENT bit\n");
 717                        set_bit(IS_CMM_ABSENT, &dev->flags);
 718                } else if (test_bit(IS_CMM_ABSENT, &dev->flags)) {
 719                        DEBUGP(4, dev, "clear IS_CMM_ABSENT bit "
 720                               "(card is removed)\n");
 721                        clear_bit(IS_CMM_ABSENT, &dev->flags);
 722                }
 723
 724                goto release_io;
 725        } else if ((dev->flags0 & 1) && test_bit(IS_CMM_ABSENT, &dev->flags)) {
 726                /* cardman and card present but cardman was absent before
 727                 * (after suspend with inserted card) */
 728                DEBUGP(4, dev, "clear IS_CMM_ABSENT bit (card is inserted)\n");
 729                clear_bit(IS_CMM_ABSENT, &dev->flags);
 730        }
 731
 732        if (test_bit(IS_ATR_VALID, &dev->flags) == 1) {
 733                DEBUGP(7, dev, "believe ATR is already valid (do nothing)\n");
 734                goto release_io;
 735        }
 736
 737        switch (dev->mstate) {
 738        case M_CARDOFF: {
 739                unsigned char flags0;
 740
 741                DEBUGP(4, dev, "M_CARDOFF\n");
 742                flags0 = inb(REG_FLAGS0(iobase));
 743                if (flags0 & 0x02) {
 744                        /* wait until Flags0 indicate power is off */
 745                        dev->mdelay = T_10MSEC;
 746                } else {
 747                        /* Flags0 indicate power off and no card inserted now;
 748                         * Reset CARDMAN CONTROLLER */
 749                        xoutb(0x80, REG_FLAGS0(iobase));
 750
 751                        /* prepare for fetching ATR again: after card off ATR
 752                         * is read again automatically */
 753                        dev->rlen =
 754                            dev->rpos =
 755                            dev->atr_csum =
 756                            dev->atr_len_retry = dev->cwarn = 0;
 757                        dev->mstate = M_FETCH_ATR;
 758
 759                        /* minimal gap between CARDOFF and read ATR is 50msec */
 760                        dev->mdelay = T_50MSEC;
 761                }
 762                break;
 763        }
 764        case M_FETCH_ATR:
 765                DEBUGP(4, dev, "M_FETCH_ATR\n");
 766                xoutb(0x80, REG_FLAGS0(iobase));
 767                DEBUGP(4, dev, "Reset BAUDV to 9600\n");
 768                dev->baudv = 0x173;     /* 9600 */
 769                xoutb(0x02, REG_STOPBITS(iobase));      /* stopbits=2 */
 770                xoutb(0x73, REG_BAUDRATE(iobase));      /* baud value */
 771                xoutb(0x21, REG_FLAGS1(iobase));        /* T_Active=1, baud
 772                                                           value */
 773                /* warm start vs. power on: */
 774                xoutb(dev->flags0 & 2 ? 0x46 : 0x44, REG_FLAGS0(iobase));
 775                dev->mdelay = T_40MSEC;
 776                dev->mstate = M_TIMEOUT_WAIT;
 777                break;
 778        case M_TIMEOUT_WAIT:
 779                DEBUGP(4, dev, "M_TIMEOUT_WAIT\n");
 780                /* numRecBytes */
 781                io_read_num_rec_bytes(iobase, &dev->atr_len);
 782                dev->mdelay = T_10MSEC;
 783                dev->mstate = M_READ_ATR_LEN;
 784                break;
 785        case M_READ_ATR_LEN:
 786                DEBUGP(4, dev, "M_READ_ATR_LEN\n");
 787                /* infinite loop possible, since there is no timeout */
 788
 789#define MAX_ATR_LEN_RETRY       100
 790
 791                if (dev->atr_len == io_read_num_rec_bytes(iobase, &s)) {
 792                        if (dev->atr_len_retry++ >= MAX_ATR_LEN_RETRY) {                                        /* + XX msec */
 793                                dev->mdelay = T_10MSEC;
 794                                dev->mstate = M_READ_ATR;
 795                        }
 796                } else {
 797                        dev->atr_len = s;
 798                        dev->atr_len_retry = 0; /* set new timeout */
 799                }
 800
 801                DEBUGP(4, dev, "Current ATR_LEN = %i\n", dev->atr_len);
 802                break;
 803        case M_READ_ATR:
 804                DEBUGP(4, dev, "M_READ_ATR\n");
 805                xoutb(0x80, REG_FLAGS0(iobase));        /* reset SM */
 806                for (i = 0; i < dev->atr_len; i++) {
 807                        xoutb(i, REG_BUF_ADDR(iobase));
 808                        dev->atr[i] = inb(REG_BUF_DATA(iobase));
 809                }
 810                /* Deactivate T_Active flags */
 811                DEBUGP(4, dev, "Deactivate T_Active flags\n");
 812                dev->flags1 = 0x01;
 813                xoutb(dev->flags1, REG_FLAGS1(iobase));
 814
 815                /* atr is present (which doesn't mean it's valid) */
 816                set_bit(IS_ATR_PRESENT, &dev->flags);
 817                if (dev->atr[0] == 0x03)
 818                        str_invert_revert(dev->atr, dev->atr_len);
 819                atrc = parse_atr(dev);
 820                if (atrc == 0) {        /* atr invalid */
 821                        dev->mdelay = 0;
 822                        dev->mstate = M_BAD_CARD;
 823                } else {
 824                        dev->mdelay = T_50MSEC;
 825                        dev->mstate = M_ATR_PRESENT;
 826                        set_bit(IS_ATR_VALID, &dev->flags);
 827                }
 828
 829                if (test_bit(IS_ATR_VALID, &dev->flags) == 1) {
 830                        DEBUGP(4, dev, "monitor_card: ATR valid\n");
 831                        /* if ta1 == 0x11, no PPS necessary (default values) */
 832                        /* do not do PPS with multi protocol cards */
 833                        if ((test_bit(IS_AUTOPPS_ACT, &dev->flags) == 0) &&
 834                            (dev->ta1 != 0x11) &&
 835                            !(test_bit(IS_ANY_T0, &dev->flags) &&
 836                            test_bit(IS_ANY_T1, &dev->flags))) {
 837                                DEBUGP(4, dev, "Perform AUTOPPS\n");
 838                                set_bit(IS_AUTOPPS_ACT, &dev->flags);
 839                                ptsreq.protocol = (0x01 << dev->proto);
 840                                ptsreq.flags = 0x01;
 841                                ptsreq.pts1 = 0x00;
 842                                ptsreq.pts2 = 0x00;
 843                                ptsreq.pts3 = 0x00;
 844                                if (set_protocol(dev, &ptsreq) == 0) {
 845                                        DEBUGP(4, dev, "AUTOPPS ret SUCC\n");
 846                                        clear_bit(IS_AUTOPPS_ACT, &dev->flags);
 847                                        wake_up_interruptible(&dev->atrq);
 848                                } else {
 849                                        DEBUGP(4, dev, "AUTOPPS failed: "
 850                                               "repower using defaults\n");
 851                                        /* prepare for repowering  */
 852                                        clear_bit(IS_ATR_PRESENT, &dev->flags);
 853                                        clear_bit(IS_ATR_VALID, &dev->flags);
 854                                        dev->rlen =
 855                                            dev->rpos =
 856                                            dev->atr_csum =
 857                                            dev->atr_len_retry = dev->cwarn = 0;
 858                                        dev->mstate = M_FETCH_ATR;
 859
 860                                        dev->mdelay = T_50MSEC;
 861                                }
 862                        } else {
 863                                /* for cards which use slightly different
 864                                 * params (extra guard time) */
 865                                set_cardparameter(dev);
 866                                if (test_bit(IS_AUTOPPS_ACT, &dev->flags) == 1)
 867                                        DEBUGP(4, dev, "AUTOPPS already active "
 868                                               "2nd try:use default values\n");
 869                                if (dev->ta1 == 0x11)
 870                                        DEBUGP(4, dev, "No AUTOPPS necessary "
 871                                               "TA(1)==0x11\n");
 872                                if (test_bit(IS_ANY_T0, &dev->flags)
 873                                    && test_bit(IS_ANY_T1, &dev->flags))
 874                                        DEBUGP(4, dev, "Do NOT perform AUTOPPS "
 875                                               "with multiprotocol cards\n");
 876                                clear_bit(IS_AUTOPPS_ACT, &dev->flags);
 877                                wake_up_interruptible(&dev->atrq);
 878                        }
 879                } else {
 880                        DEBUGP(4, dev, "ATR invalid\n");
 881                        wake_up_interruptible(&dev->atrq);
 882                }
 883                break;
 884        case M_BAD_CARD:
 885                DEBUGP(4, dev, "M_BAD_CARD\n");
 886                /* slow down warning, but prompt immediately after insertion */
 887                if (dev->cwarn == 0 || dev->cwarn == 10) {
 888                        set_bit(IS_BAD_CARD, &dev->flags);
 889                        dev_warn(&dev->p_dev->dev, MODULE_NAME ": ");
 890                        if (test_bit(IS_BAD_CSUM, &dev->flags)) {
 891                                DEBUGP(4, dev, "ATR checksum (0x%.2x, should "
 892                                       "be zero) failed\n", dev->atr_csum);
 893                        }
 894#ifdef CM4000_DEBUG
 895                        else if (test_bit(IS_BAD_LENGTH, &dev->flags)) {
 896                                DEBUGP(4, dev, "ATR length error\n");
 897                        } else {
 898                                DEBUGP(4, dev, "card damaged or wrong way "
 899                                        "inserted\n");
 900                        }
 901#endif
 902                        dev->cwarn = 0;
 903                        wake_up_interruptible(&dev->atrq);      /* wake open */
 904                }
 905                dev->cwarn++;
 906                dev->mdelay = T_100MSEC;
 907                dev->mstate = M_FETCH_ATR;
 908                break;
 909        default:
 910                DEBUGP(7, dev, "Unknown action\n");
 911                break;          /* nothing */
 912        }
 913
 914release_io:
 915        DEBUGP(7, dev, "release_io\n");
 916        clear_bit(LOCK_IO, &dev->flags);
 917        wake_up_interruptible(&dev->ioq);       /* whoever needs IO */
 918
 919return_with_timer:
 920        DEBUGP(7, dev, "<- monitor_card (returns with timer)\n");
 921        mod_timer(&dev->timer, jiffies + dev->mdelay);
 922        clear_bit(LOCK_MONITOR, &dev->flags);
 923}
 924
 925/* Interface to userland (file_operations) */
 926
 927static ssize_t cmm_read(struct file *filp, __user char *buf, size_t count,
 928                        loff_t *ppos)
 929{
 930        struct cm4000_dev *dev = filp->private_data;
 931        unsigned int iobase = dev->p_dev->resource[0]->start;
 932        ssize_t rc;
 933        int i, j, k;
 934
 935        DEBUGP(2, dev, "-> cmm_read(%s,%d)\n", current->comm, current->pid);
 936
 937        if (count == 0)         /* according to manpage */
 938                return 0;
 939
 940        if (!pcmcia_dev_present(dev->p_dev) || /* device removed */
 941            test_bit(IS_CMM_ABSENT, &dev->flags))
 942                return -ENODEV;
 943
 944        if (test_bit(IS_BAD_CSUM, &dev->flags))
 945                return -EIO;
 946
 947        /* also see the note about this in cmm_write */
 948        if (wait_event_interruptible
 949            (dev->atrq,
 950             ((filp->f_flags & O_NONBLOCK)
 951              || (test_bit(IS_ATR_PRESENT, (void *)&dev->flags) != 0)))) {
 952                if (filp->f_flags & O_NONBLOCK)
 953                        return -EAGAIN;
 954                return -ERESTARTSYS;
 955        }
 956
 957        if (test_bit(IS_ATR_VALID, &dev->flags) == 0)
 958                return -EIO;
 959
 960        /* this one implements blocking IO */
 961        if (wait_event_interruptible
 962            (dev->readq,
 963             ((filp->f_flags & O_NONBLOCK) || (dev->rpos < dev->rlen)))) {
 964                if (filp->f_flags & O_NONBLOCK)
 965                        return -EAGAIN;
 966                return -ERESTARTSYS;
 967        }
 968
 969        /* lock io */
 970        if (wait_event_interruptible
 971            (dev->ioq,
 972             ((filp->f_flags & O_NONBLOCK)
 973              || (test_and_set_bit(LOCK_IO, (void *)&dev->flags) == 0)))) {
 974                if (filp->f_flags & O_NONBLOCK)
 975                        return -EAGAIN;
 976                return -ERESTARTSYS;
 977        }
 978
 979        rc = 0;
 980        dev->flags0 = inb(REG_FLAGS0(iobase));
 981        if ((dev->flags0 & 1) == 0      /* no smartcard inserted */
 982            || dev->flags0 == 0xff) {   /* no cardman inserted */
 983                clear_bit(IS_ATR_VALID, &dev->flags);
 984                if (dev->flags0 & 1) {
 985                        set_bit(IS_CMM_ABSENT, &dev->flags);
 986                        rc = -ENODEV;
 987                } else {
 988                        rc = -EIO;
 989                }
 990                goto release_io;
 991        }
 992
 993        DEBUGP(4, dev, "begin read answer\n");
 994        j = min(count, (size_t)(dev->rlen - dev->rpos));
 995        k = dev->rpos;
 996        if (k + j > 255)
 997                j = 256 - k;
 998        DEBUGP(4, dev, "read1 j=%d\n", j);
 999        for (i = 0; i < j; i++) {
1000                xoutb(k++, REG_BUF_ADDR(iobase));
1001                dev->rbuf[i] = xinb(REG_BUF_DATA(iobase));
1002        }
1003        j = min(count, (size_t)(dev->rlen - dev->rpos));
1004        if (k + j > 255) {
1005                DEBUGP(4, dev, "read2 j=%d\n", j);
1006                dev->flags1 |= 0x10;    /* MSB buf addr set */
1007                xoutb(dev->flags1, REG_FLAGS1(iobase));
1008                for (; i < j; i++) {
1009                        xoutb(k++, REG_BUF_ADDR(iobase));
1010                        dev->rbuf[i] = xinb(REG_BUF_DATA(iobase));
1011                }
1012        }
1013
1014        if (dev->proto == 0 && count > dev->rlen - dev->rpos && i) {
1015                DEBUGP(4, dev, "T=0 and count > buffer\n");
1016                dev->rbuf[i] = dev->rbuf[i - 1];
1017                dev->rbuf[i - 1] = dev->procbyte;
1018                j++;
1019        }
1020        count = j;
1021
1022        dev->rpos = dev->rlen + 1;
1023
1024        /* Clear T1Active */
1025        DEBUGP(4, dev, "Clear T1Active\n");
1026        dev->flags1 &= 0xdf;
1027        xoutb(dev->flags1, REG_FLAGS1(iobase));
1028
1029        xoutb(0, REG_FLAGS1(iobase));   /* clear detectCMM */
1030        /* last check before exit */
1031        if (!io_detect_cm4000(iobase, dev)) {
1032                rc = -ENODEV;
1033                goto release_io;
1034        }
1035
1036        if (test_bit(IS_INVREV, &dev->flags) && count > 0)
1037                str_invert_revert(dev->rbuf, count);
1038
1039        if (copy_to_user(buf, dev->rbuf, count))
1040                rc = -EFAULT;
1041
1042release_io:
1043        clear_bit(LOCK_IO, &dev->flags);
1044        wake_up_interruptible(&dev->ioq);
1045
1046        DEBUGP(2, dev, "<- cmm_read returns: rc = %zi\n",
1047               (rc < 0 ? rc : count));
1048        return rc < 0 ? rc : count;
1049}
1050
1051static ssize_t cmm_write(struct file *filp, const char __user *buf,
1052                         size_t count, loff_t *ppos)
1053{
1054        struct cm4000_dev *dev = filp->private_data;
1055        unsigned int iobase = dev->p_dev->resource[0]->start;
1056        unsigned short s;
1057        unsigned char infolen;
1058        unsigned char sendT0;
1059        unsigned short nsend;
1060        unsigned short nr;
1061        ssize_t rc;
1062        int i;
1063
1064        DEBUGP(2, dev, "-> cmm_write(%s,%d)\n", current->comm, current->pid);
1065
1066        if (count == 0)         /* according to manpage */
1067                return 0;
1068
1069        if (dev->proto == 0 && count < 4) {
1070                /* T0 must have at least 4 bytes */
1071                DEBUGP(4, dev, "T0 short write\n");
1072                return -EIO;
1073        }
1074
1075        nr = count & 0x1ff;     /* max bytes to write */
1076
1077        sendT0 = dev->proto ? 0 : nr > 5 ? 0x08 : 0;
1078
1079        if (!pcmcia_dev_present(dev->p_dev) || /* device removed */
1080            test_bit(IS_CMM_ABSENT, &dev->flags))
1081                return -ENODEV;
1082
1083        if (test_bit(IS_BAD_CSUM, &dev->flags)) {
1084                DEBUGP(4, dev, "bad csum\n");
1085                return -EIO;
1086        }
1087
1088        /*
1089         * wait for atr to become valid.
1090         * note: it is important to lock this code. if we dont, the monitor
1091         * could be run between test_bit and the call to sleep on the
1092         * atr-queue.  if *then* the monitor detects atr valid, it will wake up
1093         * any process on the atr-queue, *but* since we have been interrupted,
1094         * we do not yet sleep on this queue. this would result in a missed
1095         * wake_up and the calling process would sleep forever (until
1096         * interrupted).  also, do *not* restore_flags before sleep_on, because
1097         * this could result in the same situation!
1098         */
1099        if (wait_event_interruptible
1100            (dev->atrq,
1101             ((filp->f_flags & O_NONBLOCK)
1102              || (test_bit(IS_ATR_PRESENT, (void *)&dev->flags) != 0)))) {
1103                if (filp->f_flags & O_NONBLOCK)
1104                        return -EAGAIN;
1105                return -ERESTARTSYS;
1106        }
1107
1108        if (test_bit(IS_ATR_VALID, &dev->flags) == 0) { /* invalid atr */
1109                DEBUGP(4, dev, "invalid ATR\n");
1110                return -EIO;
1111        }
1112
1113        /* lock io */
1114        if (wait_event_interruptible
1115            (dev->ioq,
1116             ((filp->f_flags & O_NONBLOCK)
1117              || (test_and_set_bit(LOCK_IO, (void *)&dev->flags) == 0)))) {
1118                if (filp->f_flags & O_NONBLOCK)
1119                        return -EAGAIN;
1120                return -ERESTARTSYS;
1121        }
1122
1123        if (copy_from_user(dev->sbuf, buf, ((count > 512) ? 512 : count)))
1124                return -EFAULT;
1125
1126        rc = 0;
1127        dev->flags0 = inb(REG_FLAGS0(iobase));
1128        if ((dev->flags0 & 1) == 0      /* no smartcard inserted */
1129            || dev->flags0 == 0xff) {   /* no cardman inserted */
1130                clear_bit(IS_ATR_VALID, &dev->flags);
1131                if (dev->flags0 & 1) {
1132                        set_bit(IS_CMM_ABSENT, &dev->flags);
1133                        rc = -ENODEV;
1134                } else {
1135                        DEBUGP(4, dev, "IO error\n");
1136                        rc = -EIO;
1137                }
1138                goto release_io;
1139        }
1140
1141        xoutb(0x80, REG_FLAGS0(iobase));        /* reset SM  */
1142
1143        if (!io_detect_cm4000(iobase, dev)) {
1144                rc = -ENODEV;
1145                goto release_io;
1146        }
1147
1148        /* reflect T=0 send/read mode in flags1 */
1149        dev->flags1 |= (sendT0);
1150
1151        set_cardparameter(dev);
1152
1153        /* dummy read, reset flag procedure received */
1154        inb(REG_FLAGS1(iobase));
1155
1156        dev->flags1 = 0x20      /* T_Active */
1157            | (sendT0)
1158            | (test_bit(IS_INVREV, &dev->flags) ? 2 : 0)/* inverse parity  */
1159            | (((dev->baudv - 1) & 0x0100) >> 8);       /* MSB-Baud */
1160        DEBUGP(1, dev, "set dev->flags1 = 0x%.2x\n", dev->flags1);
1161        xoutb(dev->flags1, REG_FLAGS1(iobase));
1162
1163        /* xmit data */
1164        DEBUGP(4, dev, "Xmit data\n");
1165        for (i = 0; i < nr; i++) {
1166                if (i >= 256) {
1167                        dev->flags1 = 0x20      /* T_Active */
1168                            | (sendT0)  /* SendT0 */
1169                                /* inverse parity: */
1170                            | (test_bit(IS_INVREV, &dev->flags) ? 2 : 0)
1171                            | (((dev->baudv - 1) & 0x0100) >> 8) /* MSB-Baud */
1172                            | 0x10;     /* set address high */
1173                        DEBUGP(4, dev, "dev->flags = 0x%.2x - set address "
1174                               "high\n", dev->flags1);
1175                        xoutb(dev->flags1, REG_FLAGS1(iobase));
1176                }
1177                if (test_bit(IS_INVREV, &dev->flags)) {
1178                        DEBUGP(4, dev, "Apply inverse convention for 0x%.2x "
1179                                "-> 0x%.2x\n", (unsigned char)dev->sbuf[i],
1180                              invert_revert(dev->sbuf[i]));
1181                        xoutb(i, REG_BUF_ADDR(iobase));
1182                        xoutb(invert_revert(dev->sbuf[i]),
1183                              REG_BUF_DATA(iobase));
1184                } else {
1185                        xoutb(i, REG_BUF_ADDR(iobase));
1186                        xoutb(dev->sbuf[i], REG_BUF_DATA(iobase));
1187                }
1188        }
1189        DEBUGP(4, dev, "Xmit done\n");
1190
1191        if (dev->proto == 0) {
1192                /* T=0 proto: 0 byte reply  */
1193                if (nr == 4) {
1194                        DEBUGP(4, dev, "T=0 assumes 0 byte reply\n");
1195                        xoutb(i, REG_BUF_ADDR(iobase));
1196                        if (test_bit(IS_INVREV, &dev->flags))
1197                                xoutb(0xff, REG_BUF_DATA(iobase));
1198                        else
1199                                xoutb(0x00, REG_BUF_DATA(iobase));
1200                }
1201
1202                /* numSendBytes */
1203                if (sendT0)
1204                        nsend = nr;
1205                else {
1206                        if (nr == 4)
1207                                nsend = 5;
1208                        else {
1209                                nsend = 5 + (unsigned char)dev->sbuf[4];
1210                                if (dev->sbuf[4] == 0)
1211                                        nsend += 0x100;
1212                        }
1213                }
1214        } else
1215                nsend = nr;
1216
1217        /* T0: output procedure byte */
1218        if (test_bit(IS_INVREV, &dev->flags)) {
1219                DEBUGP(4, dev, "T=0 set Procedure byte (inverse-reverse) "
1220                       "0x%.2x\n", invert_revert(dev->sbuf[1]));
1221                xoutb(invert_revert(dev->sbuf[1]), REG_NUM_BYTES(iobase));
1222        } else {
1223                DEBUGP(4, dev, "T=0 set Procedure byte 0x%.2x\n", dev->sbuf[1]);
1224                xoutb(dev->sbuf[1], REG_NUM_BYTES(iobase));
1225        }
1226
1227        DEBUGP(1, dev, "set NumSendBytes = 0x%.2x\n",
1228               (unsigned char)(nsend & 0xff));
1229        xoutb((unsigned char)(nsend & 0xff), REG_NUM_SEND(iobase));
1230
1231        DEBUGP(1, dev, "Trigger CARDMAN CONTROLLER (0x%.2x)\n",
1232               0x40     /* SM_Active */
1233              | (dev->flags0 & 2 ? 0 : 4)       /* power on if needed */
1234              |(dev->proto ? 0x10 : 0x08)       /* T=1/T=0 */
1235              |(nsend & 0x100) >> 8 /* MSB numSendBytes */ );
1236        xoutb(0x40              /* SM_Active */
1237              | (dev->flags0 & 2 ? 0 : 4)       /* power on if needed */
1238              |(dev->proto ? 0x10 : 0x08)       /* T=1/T=0 */
1239              |(nsend & 0x100) >> 8,    /* MSB numSendBytes */
1240              REG_FLAGS0(iobase));
1241
1242        /* wait for xmit done */
1243        if (dev->proto == 1) {
1244                DEBUGP(4, dev, "Wait for xmit done\n");
1245                for (i = 0; i < 1000; i++) {
1246                        if (inb(REG_FLAGS0(iobase)) & 0x08)
1247                                break;
1248                        msleep_interruptible(10);
1249                }
1250                if (i == 1000) {
1251                        DEBUGP(4, dev, "timeout waiting for xmit done\n");
1252                        rc = -EIO;
1253                        goto release_io;
1254                }
1255        }
1256
1257        /* T=1: wait for infoLen */
1258
1259        infolen = 0;
1260        if (dev->proto) {
1261                /* wait until infoLen is valid */
1262                for (i = 0; i < 6000; i++) {    /* max waiting time of 1 min */
1263                        io_read_num_rec_bytes(iobase, &s);
1264                        if (s >= 3) {
1265                                infolen = inb(REG_FLAGS1(iobase));
1266                                DEBUGP(4, dev, "infolen=%d\n", infolen);
1267                                break;
1268                        }
1269                        msleep_interruptible(10);
1270                }
1271                if (i == 6000) {
1272                        DEBUGP(4, dev, "timeout waiting for infoLen\n");
1273                        rc = -EIO;
1274                        goto release_io;
1275                }
1276        } else
1277                clear_bit(IS_PROCBYTE_PRESENT, &dev->flags);
1278
1279        /* numRecBytes | bit9 of numRecytes */
1280        io_read_num_rec_bytes(iobase, &dev->rlen);
1281        for (i = 0; i < 600; i++) {     /* max waiting time of 2 sec */
1282                if (dev->proto) {
1283                        if (dev->rlen >= infolen + 4)
1284                                break;
1285                }
1286                msleep_interruptible(10);
1287                /* numRecBytes | bit9 of numRecytes */
1288                io_read_num_rec_bytes(iobase, &s);
1289                if (s > dev->rlen) {
1290                        DEBUGP(1, dev, "NumRecBytes inc (reset timeout)\n");
1291                        i = 0;  /* reset timeout */
1292                        dev->rlen = s;
1293                }
1294                /* T=0: we are done when numRecBytes doesn't
1295                 *      increment any more and NoProcedureByte
1296                 *      is set and numRecBytes == bytes sent + 6
1297                 *      (header bytes + data + 1 for sw2)
1298                 *      except when the card replies an error
1299                 *      which means, no data will be sent back.
1300                 */
1301                else if (dev->proto == 0) {
1302                        if ((inb(REG_BUF_ADDR(iobase)) & 0x80)) {
1303                                /* no procedure byte received since last read */
1304                                DEBUGP(1, dev, "NoProcedure byte set\n");
1305                                /* i=0; */
1306                        } else {
1307                                /* procedure byte received since last read */
1308                                DEBUGP(1, dev, "NoProcedure byte unset "
1309                                        "(reset timeout)\n");
1310                                dev->procbyte = inb(REG_FLAGS1(iobase));
1311                                DEBUGP(1, dev, "Read procedure byte 0x%.2x\n",
1312                                      dev->procbyte);
1313                                i = 0;  /* resettimeout */
1314                        }
1315                        if (inb(REG_FLAGS0(iobase)) & 0x08) {
1316                                DEBUGP(1, dev, "T0Done flag (read reply)\n");
1317                                break;
1318                        }
1319                }
1320                if (dev->proto)
1321                        infolen = inb(REG_FLAGS1(iobase));
1322        }
1323        if (i == 600) {
1324                DEBUGP(1, dev, "timeout waiting for numRecBytes\n");
1325                rc = -EIO;
1326                goto release_io;
1327        } else {
1328                if (dev->proto == 0) {
1329                        DEBUGP(1, dev, "Wait for T0Done bit to be  set\n");
1330                        for (i = 0; i < 1000; i++) {
1331                                if (inb(REG_FLAGS0(iobase)) & 0x08)
1332                                        break;
1333                                msleep_interruptible(10);
1334                        }
1335                        if (i == 1000) {
1336                                DEBUGP(1, dev, "timeout waiting for T0Done\n");
1337                                rc = -EIO;
1338                                goto release_io;
1339                        }
1340
1341                        dev->procbyte = inb(REG_FLAGS1(iobase));
1342                        DEBUGP(4, dev, "Read procedure byte 0x%.2x\n",
1343                              dev->procbyte);
1344
1345                        io_read_num_rec_bytes(iobase, &dev->rlen);
1346                        DEBUGP(4, dev, "Read NumRecBytes = %i\n", dev->rlen);
1347
1348                }
1349        }
1350        /* T=1: read offset=zero, T=0: read offset=after challenge */
1351        dev->rpos = dev->proto ? 0 : nr == 4 ? 5 : nr > dev->rlen ? 5 : nr;
1352        DEBUGP(4, dev, "dev->rlen = %i,  dev->rpos = %i, nr = %i\n",
1353              dev->rlen, dev->rpos, nr);
1354
1355release_io:
1356        DEBUGP(4, dev, "Reset SM\n");
1357        xoutb(0x80, REG_FLAGS0(iobase));        /* reset SM */
1358
1359        if (rc < 0) {
1360                DEBUGP(4, dev, "Write failed but clear T_Active\n");
1361                dev->flags1 &= 0xdf;
1362                xoutb(dev->flags1, REG_FLAGS1(iobase));
1363        }
1364
1365        clear_bit(LOCK_IO, &dev->flags);
1366        wake_up_interruptible(&dev->ioq);
1367        wake_up_interruptible(&dev->readq);     /* tell read we have data */
1368
1369        /* ITSEC E2: clear write buffer */
1370        memset((char *)dev->sbuf, 0, 512);
1371
1372        /* return error or actually written bytes */
1373        DEBUGP(2, dev, "<- cmm_write\n");
1374        return rc < 0 ? rc : nr;
1375}
1376
1377static void start_monitor(struct cm4000_dev *dev)
1378{
1379        DEBUGP(3, dev, "-> start_monitor\n");
1380        if (!dev->monitor_running) {
1381                DEBUGP(5, dev, "create, init and add timer\n");
1382                timer_setup(&dev->timer, monitor_card, 0);
1383                dev->monitor_running = 1;
1384                mod_timer(&dev->timer, jiffies);
1385        } else
1386                DEBUGP(5, dev, "monitor already running\n");
1387        DEBUGP(3, dev, "<- start_monitor\n");
1388}
1389
1390static void stop_monitor(struct cm4000_dev *dev)
1391{
1392        DEBUGP(3, dev, "-> stop_monitor\n");
1393        if (dev->monitor_running) {
1394                DEBUGP(5, dev, "stopping monitor\n");
1395                terminate_monitor(dev);
1396                /* reset monitor SM */
1397                clear_bit(IS_ATR_VALID, &dev->flags);
1398                clear_bit(IS_ATR_PRESENT, &dev->flags);
1399        } else
1400                DEBUGP(5, dev, "monitor already stopped\n");
1401        DEBUGP(3, dev, "<- stop_monitor\n");
1402}
1403
1404static long cmm_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1405{
1406        struct cm4000_dev *dev = filp->private_data;
1407        unsigned int iobase = dev->p_dev->resource[0]->start;
1408        struct inode *inode = file_inode(filp);
1409        struct pcmcia_device *link;
1410        int rc;
1411        void __user *argp = (void __user *)arg;
1412#ifdef CM4000_DEBUG
1413        char *ioctl_names[CM_IOC_MAXNR + 1] = {
1414                [_IOC_NR(CM_IOCGSTATUS)] "CM_IOCGSTATUS",
1415                [_IOC_NR(CM_IOCGATR)] "CM_IOCGATR",
1416                [_IOC_NR(CM_IOCARDOFF)] "CM_IOCARDOFF",
1417                [_IOC_NR(CM_IOCSPTS)] "CM_IOCSPTS",
1418                [_IOC_NR(CM_IOSDBGLVL)] "CM4000_DBGLVL",
1419        };
1420        DEBUGP(3, dev, "cmm_ioctl(device=%d.%d) %s\n", imajor(inode),
1421               iminor(inode), ioctl_names[_IOC_NR(cmd)]);
1422#endif
1423
1424        mutex_lock(&cmm_mutex);
1425        rc = -ENODEV;
1426        link = dev_table[iminor(inode)];
1427        if (!pcmcia_dev_present(link)) {
1428                DEBUGP(4, dev, "DEV_OK false\n");
1429                goto out;
1430        }
1431
1432        if (test_bit(IS_CMM_ABSENT, &dev->flags)) {
1433                DEBUGP(4, dev, "CMM_ABSENT flag set\n");
1434                goto out;
1435        }
1436        rc = -EINVAL;
1437
1438        if (_IOC_TYPE(cmd) != CM_IOC_MAGIC) {
1439                DEBUGP(4, dev, "ioctype mismatch\n");
1440                goto out;
1441        }
1442        if (_IOC_NR(cmd) > CM_IOC_MAXNR) {
1443                DEBUGP(4, dev, "iocnr mismatch\n");
1444                goto out;
1445        }
1446        rc = 0;
1447
1448        switch (cmd) {
1449        case CM_IOCGSTATUS:
1450                DEBUGP(4, dev, " ... in CM_IOCGSTATUS\n");
1451                {
1452                        int status;
1453
1454                        /* clear other bits, but leave inserted & powered as
1455                         * they are */
1456                        status = dev->flags0 & 3;
1457                        if (test_bit(IS_ATR_PRESENT, &dev->flags))
1458                                status |= CM_ATR_PRESENT;
1459                        if (test_bit(IS_ATR_VALID, &dev->flags))
1460                                status |= CM_ATR_VALID;
1461                        if (test_bit(IS_CMM_ABSENT, &dev->flags))
1462                                status |= CM_NO_READER;
1463                        if (test_bit(IS_BAD_CARD, &dev->flags))
1464                                status |= CM_BAD_CARD;
1465                        if (copy_to_user(argp, &status, sizeof(int)))
1466                                rc = -EFAULT;
1467                }
1468                break;
1469        case CM_IOCGATR:
1470                DEBUGP(4, dev, "... in CM_IOCGATR\n");
1471                {
1472                        struct atreq __user *atreq = argp;
1473                        int tmp;
1474                        /* allow nonblocking io and being interrupted */
1475                        if (wait_event_interruptible
1476                            (dev->atrq,
1477                             ((filp->f_flags & O_NONBLOCK)
1478                              || (test_bit(IS_ATR_PRESENT, (void *)&dev->flags)
1479                                  != 0)))) {
1480                                if (filp->f_flags & O_NONBLOCK)
1481                                        rc = -EAGAIN;
1482                                else
1483                                        rc = -ERESTARTSYS;
1484                                break;
1485                        }
1486
1487                        rc = -EFAULT;
1488                        if (test_bit(IS_ATR_VALID, &dev->flags) == 0) {
1489                                tmp = -1;
1490                                if (copy_to_user(&(atreq->atr_len), &tmp,
1491                                                 sizeof(int)))
1492                                        break;
1493                        } else {
1494                                if (copy_to_user(atreq->atr, dev->atr,
1495                                                 dev->atr_len))
1496                                        break;
1497
1498                                tmp = dev->atr_len;
1499                                if (copy_to_user(&(atreq->atr_len), &tmp, sizeof(int)))
1500                                        break;
1501                        }
1502                        rc = 0;
1503                        break;
1504                }
1505        case CM_IOCARDOFF:
1506
1507#ifdef CM4000_DEBUG
1508                DEBUGP(4, dev, "... in CM_IOCARDOFF\n");
1509                if (dev->flags0 & 0x01) {
1510                        DEBUGP(4, dev, "    Card inserted\n");
1511                } else {
1512                        DEBUGP(2, dev, "    No card inserted\n");
1513                }
1514                if (dev->flags0 & 0x02) {
1515                        DEBUGP(4, dev, "    Card powered\n");
1516                } else {
1517                        DEBUGP(2, dev, "    Card not powered\n");
1518                }
1519#endif
1520
1521                /* is a card inserted and powered? */
1522                if ((dev->flags0 & 0x01) && (dev->flags0 & 0x02)) {
1523
1524                        /* get IO lock */
1525                        if (wait_event_interruptible
1526                            (dev->ioq,
1527                             ((filp->f_flags & O_NONBLOCK)
1528                              || (test_and_set_bit(LOCK_IO, (void *)&dev->flags)
1529                                  == 0)))) {
1530                                if (filp->f_flags & O_NONBLOCK)
1531                                        rc = -EAGAIN;
1532                                else
1533                                        rc = -ERESTARTSYS;
1534                                break;
1535                        }
1536                        /* Set Flags0 = 0x42 */
1537                        DEBUGP(4, dev, "Set Flags0=0x42 \n");
1538                        xoutb(0x42, REG_FLAGS0(iobase));
1539                        clear_bit(IS_ATR_PRESENT, &dev->flags);
1540                        clear_bit(IS_ATR_VALID, &dev->flags);
1541                        dev->mstate = M_CARDOFF;
1542                        clear_bit(LOCK_IO, &dev->flags);
1543                        if (wait_event_interruptible
1544                            (dev->atrq,
1545                             ((filp->f_flags & O_NONBLOCK)
1546                              || (test_bit(IS_ATR_VALID, (void *)&dev->flags) !=
1547                                  0)))) {
1548                                if (filp->f_flags & O_NONBLOCK)
1549                                        rc = -EAGAIN;
1550                                else
1551                                        rc = -ERESTARTSYS;
1552                                break;
1553                        }
1554                }
1555                /* release lock */
1556                clear_bit(LOCK_IO, &dev->flags);
1557                wake_up_interruptible(&dev->ioq);
1558
1559                rc = 0;
1560                break;
1561        case CM_IOCSPTS:
1562                {
1563                        struct ptsreq krnptsreq;
1564
1565                        if (copy_from_user(&krnptsreq, argp,
1566                                           sizeof(struct ptsreq))) {
1567                                rc = -EFAULT;
1568                                break;
1569                        }
1570
1571                        rc = 0;
1572                        DEBUGP(4, dev, "... in CM_IOCSPTS\n");
1573                        /* wait for ATR to get valid */
1574                        if (wait_event_interruptible
1575                            (dev->atrq,
1576                             ((filp->f_flags & O_NONBLOCK)
1577                              || (test_bit(IS_ATR_PRESENT, (void *)&dev->flags)
1578                                  != 0)))) {
1579                                if (filp->f_flags & O_NONBLOCK)
1580                                        rc = -EAGAIN;
1581                                else
1582                                        rc = -ERESTARTSYS;
1583                                break;
1584                        }
1585                        /* get IO lock */
1586                        if (wait_event_interruptible
1587                            (dev->ioq,
1588                             ((filp->f_flags & O_NONBLOCK)
1589                              || (test_and_set_bit(LOCK_IO, (void *)&dev->flags)
1590                                  == 0)))) {
1591                                if (filp->f_flags & O_NONBLOCK)
1592                                        rc = -EAGAIN;
1593                                else
1594                                        rc = -ERESTARTSYS;
1595                                break;
1596                        }
1597
1598                        if ((rc = set_protocol(dev, &krnptsreq)) != 0) {
1599                                /* auto power_on again */
1600                                dev->mstate = M_FETCH_ATR;
1601                                clear_bit(IS_ATR_VALID, &dev->flags);
1602                        }
1603                        /* release lock */
1604                        clear_bit(LOCK_IO, &dev->flags);
1605                        wake_up_interruptible(&dev->ioq);
1606
1607                }
1608                break;
1609#ifdef CM4000_DEBUG
1610        case CM_IOSDBGLVL:
1611                rc = -ENOTTY;
1612                break;
1613#endif
1614        default:
1615                DEBUGP(4, dev, "... in default (unknown IOCTL code)\n");
1616                rc = -ENOTTY;
1617        }
1618out:
1619        mutex_unlock(&cmm_mutex);
1620        return rc;
1621}
1622
1623static int cmm_open(struct inode *inode, struct file *filp)
1624{
1625        struct cm4000_dev *dev;
1626        struct pcmcia_device *link;
1627        int minor = iminor(inode);
1628        int ret;
1629
1630        if (minor >= CM4000_MAX_DEV)
1631                return -ENODEV;
1632
1633        mutex_lock(&cmm_mutex);
1634        link = dev_table[minor];
1635        if (link == NULL || !pcmcia_dev_present(link)) {
1636                ret = -ENODEV;
1637                goto out;
1638        }
1639
1640        if (link->open) {
1641                ret = -EBUSY;
1642                goto out;
1643        }
1644
1645        dev = link->priv;
1646        filp->private_data = dev;
1647
1648        DEBUGP(2, dev, "-> cmm_open(device=%d.%d process=%s,%d)\n",
1649              imajor(inode), minor, current->comm, current->pid);
1650
1651        /* init device variables, they may be "polluted" after close
1652         * or, the device may never have been closed (i.e. open failed)
1653         */
1654
1655        ZERO_DEV(dev);
1656
1657        /* opening will always block since the
1658         * monitor will be started by open, which
1659         * means we have to wait for ATR becoming
1660         * valid = block until valid (or card
1661         * inserted)
1662         */
1663        if (filp->f_flags & O_NONBLOCK) {
1664                ret = -EAGAIN;
1665                goto out;
1666        }
1667
1668        dev->mdelay = T_50MSEC;
1669
1670        /* start monitoring the cardstatus */
1671        start_monitor(dev);
1672
1673        link->open = 1;         /* only one open per device */
1674
1675        DEBUGP(2, dev, "<- cmm_open\n");
1676        ret = stream_open(inode, filp);
1677out:
1678        mutex_unlock(&cmm_mutex);
1679        return ret;
1680}
1681
1682static int cmm_close(struct inode *inode, struct file *filp)
1683{
1684        struct cm4000_dev *dev;
1685        struct pcmcia_device *link;
1686        int minor = iminor(inode);
1687
1688        if (minor >= CM4000_MAX_DEV)
1689                return -ENODEV;
1690
1691        link = dev_table[minor];
1692        if (link == NULL)
1693                return -ENODEV;
1694
1695        dev = link->priv;
1696
1697        DEBUGP(2, dev, "-> cmm_close(maj/min=%d.%d)\n",
1698               imajor(inode), minor);
1699
1700        stop_monitor(dev);
1701
1702        ZERO_DEV(dev);
1703
1704        link->open = 0;         /* only one open per device */
1705        wake_up(&dev->devq);    /* socket removed? */
1706
1707        DEBUGP(2, dev, "cmm_close\n");
1708        return 0;
1709}
1710
1711static void cmm_cm4000_release(struct pcmcia_device * link)
1712{
1713        struct cm4000_dev *dev = link->priv;
1714
1715        /* dont terminate the monitor, rather rely on
1716         * close doing that for us.
1717         */
1718        DEBUGP(3, dev, "-> cmm_cm4000_release\n");
1719        while (link->open) {
1720                printk(KERN_INFO MODULE_NAME ": delaying release until "
1721                       "process has terminated\n");
1722                /* note: don't interrupt us:
1723                 * close the applications which own
1724                 * the devices _first_ !
1725                 */
1726                wait_event(dev->devq, (link->open == 0));
1727        }
1728        /* dev->devq=NULL;      this cannot be zeroed earlier */
1729        DEBUGP(3, dev, "<- cmm_cm4000_release\n");
1730        return;
1731}
1732
1733/*==== Interface to PCMCIA Layer =======================================*/
1734
1735static int cm4000_config_check(struct pcmcia_device *p_dev, void *priv_data)
1736{
1737        return pcmcia_request_io(p_dev);
1738}
1739
1740static int cm4000_config(struct pcmcia_device * link, int devno)
1741{
1742        link->config_flags |= CONF_AUTO_SET_IO;
1743
1744        /* read the config-tuples */
1745        if (pcmcia_loop_config(link, cm4000_config_check, NULL))
1746                goto cs_release;
1747
1748        if (pcmcia_enable_device(link))
1749                goto cs_release;
1750
1751        return 0;
1752
1753cs_release:
1754        cm4000_release(link);
1755        return -ENODEV;
1756}
1757
1758static int cm4000_suspend(struct pcmcia_device *link)
1759{
1760        struct cm4000_dev *dev;
1761
1762        dev = link->priv;
1763        stop_monitor(dev);
1764
1765        return 0;
1766}
1767
1768static int cm4000_resume(struct pcmcia_device *link)
1769{
1770        struct cm4000_dev *dev;
1771
1772        dev = link->priv;
1773        if (link->open)
1774                start_monitor(dev);
1775
1776        return 0;
1777}
1778
1779static void cm4000_release(struct pcmcia_device *link)
1780{
1781        cmm_cm4000_release(link);       /* delay release until device closed */
1782        pcmcia_disable_device(link);
1783}
1784
1785static int cm4000_probe(struct pcmcia_device *link)
1786{
1787        struct cm4000_dev *dev;
1788        int i, ret;
1789
1790        for (i = 0; i < CM4000_MAX_DEV; i++)
1791                if (dev_table[i] == NULL)
1792                        break;
1793
1794        if (i == CM4000_MAX_DEV) {
1795                printk(KERN_NOTICE MODULE_NAME ": all devices in use\n");
1796                return -ENODEV;
1797        }
1798
1799        /* create a new cm4000_cs device */
1800        dev = kzalloc(sizeof(struct cm4000_dev), GFP_KERNEL);
1801        if (dev == NULL)
1802                return -ENOMEM;
1803
1804        dev->p_dev = link;
1805        link->priv = dev;
1806        dev_table[i] = link;
1807
1808        init_waitqueue_head(&dev->devq);
1809        init_waitqueue_head(&dev->ioq);
1810        init_waitqueue_head(&dev->atrq);
1811        init_waitqueue_head(&dev->readq);
1812
1813        ret = cm4000_config(link, i);
1814        if (ret) {
1815                dev_table[i] = NULL;
1816                kfree(dev);
1817                return ret;
1818        }
1819
1820        device_create(cmm_class, NULL, MKDEV(major, i), NULL, "cmm%d", i);
1821
1822        return 0;
1823}
1824
1825static void cm4000_detach(struct pcmcia_device *link)
1826{
1827        struct cm4000_dev *dev = link->priv;
1828        int devno;
1829
1830        /* find device */
1831        for (devno = 0; devno < CM4000_MAX_DEV; devno++)
1832                if (dev_table[devno] == link)
1833                        break;
1834        if (devno == CM4000_MAX_DEV)
1835                return;
1836
1837        stop_monitor(dev);
1838
1839        cm4000_release(link);
1840
1841        dev_table[devno] = NULL;
1842        kfree(dev);
1843
1844        device_destroy(cmm_class, MKDEV(major, devno));
1845
1846        return;
1847}
1848
1849static const struct file_operations cm4000_fops = {
1850        .owner  = THIS_MODULE,
1851        .read   = cmm_read,
1852        .write  = cmm_write,
1853        .unlocked_ioctl = cmm_ioctl,
1854        .open   = cmm_open,
1855        .release= cmm_close,
1856        .llseek = no_llseek,
1857};
1858
1859static const struct pcmcia_device_id cm4000_ids[] = {
1860        PCMCIA_DEVICE_MANF_CARD(0x0223, 0x0002),
1861        PCMCIA_DEVICE_PROD_ID12("CardMan", "4000", 0x2FB368CA, 0xA2BD8C39),
1862        PCMCIA_DEVICE_NULL,
1863};
1864MODULE_DEVICE_TABLE(pcmcia, cm4000_ids);
1865
1866static struct pcmcia_driver cm4000_driver = {
1867        .owner    = THIS_MODULE,
1868        .name     = "cm4000_cs",
1869        .probe    = cm4000_probe,
1870        .remove   = cm4000_detach,
1871        .suspend  = cm4000_suspend,
1872        .resume   = cm4000_resume,
1873        .id_table = cm4000_ids,
1874};
1875
1876static int __init cmm_init(void)
1877{
1878        int rc;
1879
1880        cmm_class = class_create(THIS_MODULE, "cardman_4000");
1881        if (IS_ERR(cmm_class))
1882                return PTR_ERR(cmm_class);
1883
1884        major = register_chrdev(0, DEVICE_NAME, &cm4000_fops);
1885        if (major < 0) {
1886                printk(KERN_WARNING MODULE_NAME
1887                        ": could not get major number\n");
1888                class_destroy(cmm_class);
1889                return major;
1890        }
1891
1892        rc = pcmcia_register_driver(&cm4000_driver);
1893        if (rc < 0) {
1894                unregister_chrdev(major, DEVICE_NAME);
1895                class_destroy(cmm_class);
1896                return rc;
1897        }
1898
1899        return 0;
1900}
1901
1902static void __exit cmm_exit(void)
1903{
1904        pcmcia_unregister_driver(&cm4000_driver);
1905        unregister_chrdev(major, DEVICE_NAME);
1906        class_destroy(cmm_class);
1907};
1908
1909module_init(cmm_init);
1910module_exit(cmm_exit);
1911MODULE_LICENSE("Dual BSD/GPL");
1912