Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

[PATCH] New Omnikey Cardman 4000 driver

Add new Omnikey Cardman 4000 smartcard reader driver

Signed-off-by: Harald Welte <laforge@gnumonks.org>
Cc: Dominik Brodowski <linux@dominikbrodowski.net>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>

authored by

Harald Welte and committed by
Linus Torvalds
c1986ee9 77c44ab1

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