PageRenderTime 65ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/drivers/char/pcmcia/cm4000_cs.c

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