blob: d2accd64b3f5e1facba69b43e0ac38eaa3ec42bf [file] [log] [blame]
Harald Weltec1986ee2005-11-13 16:06:29 -08001 /*
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 *
Harald Welte67bc6202006-02-14 09:21:26 +010016 * (C) 2005-2006 Harald Welte <laforge@gnumonks.org>
Harald Weltec1986ee2005-11-13 16:06:29 -080017 * - 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()
Harald Welte67bc6202006-02-14 09:21:26 +010021 * - add class interface for udev device creation
Harald Weltec1986ee2005-11-13 16:06:29 -080022 *
23 * All rights reserved. Licensed under dual BSD/GPL license.
24 */
25
Harald Weltec1986ee2005-11-13 16:06:29 -080026#include <linux/kernel.h>
27#include <linux/module.h>
28#include <linux/slab.h>
29#include <linux/init.h>
30#include <linux/fs.h>
31#include <linux/delay.h>
Akinobu Mita884c3d72007-05-09 02:33:32 -070032#include <linux/bitrev.h>
Alan Cox4cf974c2008-06-14 16:01:57 +020033#include <linux/smp_lock.h>
34#include <linux/uaccess.h>
35#include <linux/io.h>
Harald Weltec1986ee2005-11-13 16:06:29 -080036
Harald Weltec1986ee2005-11-13 16:06:29 -080037#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
Dominik Brodowskidd2e5a12009-11-03 10:27:34 +010047#define reader_to_dev(x) (&x->p_dev->dev)
Dominik Brodowskicbf624f2009-10-24 15:47:29 +020048
49/* n (debug level) is ignored */
50/* additional debug output may be enabled by re-compiling with
51 * CM4000_DEBUG set */
52/* #define CM4000_DEBUG */
53#define DEBUGP(n, rdr, x, args...) do { \
54 dev_dbg(reader_to_dev(rdr), "%s:" x, \
55 __func__ , ## args); \
Harald Weltec1986ee2005-11-13 16:06:29 -080056 } while (0)
Dominik Brodowskicbf624f2009-10-24 15:47:29 +020057
Harald Welte67bc6202006-02-14 09:21:26 +010058static char *version = "cm4000_cs.c v2.4.0gm6 - All bugs added by Harald Welte";
Harald Weltec1986ee2005-11-13 16:06:29 -080059
60#define T_1SEC (HZ)
61#define T_10MSEC msecs_to_jiffies(10)
62#define T_20MSEC msecs_to_jiffies(20)
63#define T_40MSEC msecs_to_jiffies(40)
64#define T_50MSEC msecs_to_jiffies(50)
65#define T_100MSEC msecs_to_jiffies(100)
66#define T_500MSEC msecs_to_jiffies(500)
67
Dominik Brodowskifba395e2006-03-31 17:21:06 +020068static void cm4000_release(struct pcmcia_device *link);
Harald Weltec1986ee2005-11-13 16:06:29 -080069
70static int major; /* major number we get from the kernel */
71
72/* note: the first state has to have number 0 always */
73
74#define M_FETCH_ATR 0
75#define M_TIMEOUT_WAIT 1
76#define M_READ_ATR_LEN 2
77#define M_READ_ATR 3
78#define M_ATR_PRESENT 4
79#define M_BAD_CARD 5
80#define M_CARDOFF 6
81
82#define LOCK_IO 0
83#define LOCK_MONITOR 1
84
85#define IS_AUTOPPS_ACT 6
86#define IS_PROCBYTE_PRESENT 7
87#define IS_INVREV 8
88#define IS_ANY_T0 9
89#define IS_ANY_T1 10
90#define IS_ATR_PRESENT 11
91#define IS_ATR_VALID 12
92#define IS_CMM_ABSENT 13
93#define IS_BAD_LENGTH 14
94#define IS_BAD_CSUM 15
95#define IS_BAD_CARD 16
96
97#define REG_FLAGS0(x) (x + 0)
98#define REG_FLAGS1(x) (x + 1)
99#define REG_NUM_BYTES(x) (x + 2)
100#define REG_BUF_ADDR(x) (x + 3)
101#define REG_BUF_DATA(x) (x + 4)
102#define REG_NUM_SEND(x) (x + 5)
103#define REG_BAUDRATE(x) (x + 6)
104#define REG_STOPBITS(x) (x + 7)
105
106struct cm4000_dev {
Dominik Brodowskifd238232006-03-05 10:45:09 +0100107 struct pcmcia_device *p_dev;
Harald Weltec1986ee2005-11-13 16:06:29 -0800108
109 unsigned char atr[MAX_ATR];
110 unsigned char rbuf[512];
111 unsigned char sbuf[512];
112
113 wait_queue_head_t devq; /* when removing cardman must not be
114 zeroed! */
115
116 wait_queue_head_t ioq; /* if IO is locked, wait on this Q */
117 wait_queue_head_t atrq; /* wait for ATR valid */
118 wait_queue_head_t readq; /* used by write to wake blk.read */
119
120 /* warning: do not move this fields.
121 * initialising to zero depends on it - see ZERO_DEV below. */
122 unsigned char atr_csum;
123 unsigned char atr_len_retry;
124 unsigned short atr_len;
125 unsigned short rlen; /* bytes avail. after write */
126 unsigned short rpos; /* latest read pos. write zeroes */
127 unsigned char procbyte; /* T=0 procedure byte */
128 unsigned char mstate; /* state of card monitor */
129 unsigned char cwarn; /* slow down warning */
130 unsigned char flags0; /* cardman IO-flags 0 */
131 unsigned char flags1; /* cardman IO-flags 1 */
132 unsigned int mdelay; /* variable monitor speeds, in jiffies */
133
134 unsigned int baudv; /* baud value for speed */
135 unsigned char ta1;
136 unsigned char proto; /* T=0, T=1, ... */
137 unsigned long flags; /* lock+flags (MONITOR,IO,ATR) * for concurrent
138 access */
139
140 unsigned char pts[4];
141
142 struct timer_list timer; /* used to keep monitor running */
143 int monitor_running;
144};
145
146#define ZERO_DEV(dev) \
147 memset(&dev->atr_csum,0, \
148 sizeof(struct cm4000_dev) - \
Al Viroa2bcce82006-06-15 12:26:55 +0100149 offsetof(struct cm4000_dev, atr_csum))
Harald Weltec1986ee2005-11-13 16:06:29 -0800150
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200151static struct pcmcia_device *dev_table[CM4000_MAX_DEV];
Harald Welte67bc6202006-02-14 09:21:26 +0100152static struct class *cmm_class;
Harald Weltec1986ee2005-11-13 16:06:29 -0800153
154/* This table doesn't use spaces after the comma between fields and thus
155 * violates CodingStyle. However, I don't really think wrapping it around will
156 * make it any clearer to read -HW */
157static unsigned char fi_di_table[10][14] = {
158/*FI 00 01 02 03 04 05 06 07 08 09 10 11 12 13 */
159/*DI */
160/* 0 */ {0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11},
161/* 1 */ {0x01,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x91,0x11,0x11,0x11,0x11},
162/* 2 */ {0x02,0x12,0x22,0x32,0x11,0x11,0x11,0x11,0x11,0x92,0xA2,0xB2,0x11,0x11},
163/* 3 */ {0x03,0x13,0x23,0x33,0x43,0x53,0x63,0x11,0x11,0x93,0xA3,0xB3,0xC3,0xD3},
164/* 4 */ {0x04,0x14,0x24,0x34,0x44,0x54,0x64,0x11,0x11,0x94,0xA4,0xB4,0xC4,0xD4},
165/* 5 */ {0x00,0x15,0x25,0x35,0x45,0x55,0x65,0x11,0x11,0x95,0xA5,0xB5,0xC5,0xD5},
166/* 6 */ {0x06,0x16,0x26,0x36,0x46,0x56,0x66,0x11,0x11,0x96,0xA6,0xB6,0xC6,0xD6},
167/* 7 */ {0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11},
168/* 8 */ {0x08,0x11,0x28,0x38,0x48,0x58,0x68,0x11,0x11,0x98,0xA8,0xB8,0xC8,0xD8},
169/* 9 */ {0x09,0x19,0x29,0x39,0x49,0x59,0x69,0x11,0x11,0x99,0xA9,0xB9,0xC9,0xD9}
170};
171
Dominik Brodowskicbf624f2009-10-24 15:47:29 +0200172#ifndef CM4000_DEBUG
Harald Weltec1986ee2005-11-13 16:06:29 -0800173#define xoutb outb
174#define xinb inb
175#else
176static inline void xoutb(unsigned char val, unsigned short port)
177{
Dominik Brodowskicbf624f2009-10-24 15:47:29 +0200178 pr_debug("outb(val=%.2x,port=%.4x)\n", val, port);
Harald Weltec1986ee2005-11-13 16:06:29 -0800179 outb(val, port);
180}
181static inline unsigned char xinb(unsigned short port)
182{
183 unsigned char val;
184
185 val = inb(port);
Dominik Brodowskicbf624f2009-10-24 15:47:29 +0200186 pr_debug("%.2x=inb(%.4x)\n", val, port);
Harald Weltec1986ee2005-11-13 16:06:29 -0800187
188 return val;
189}
190#endif
191
Akinobu Mita884c3d72007-05-09 02:33:32 -0700192static inline unsigned char invert_revert(unsigned char ch)
193{
194 return bitrev8(~ch);
195}
Harald Weltec1986ee2005-11-13 16:06:29 -0800196
197static void str_invert_revert(unsigned char *b, int len)
198{
199 int i;
200
201 for (i = 0; i < len; i++)
Akinobu Mita884c3d72007-05-09 02:33:32 -0700202 b[i] = invert_revert(b[i]);
Harald Weltec1986ee2005-11-13 16:06:29 -0800203}
204
205#define ATRLENCK(dev,pos) \
206 if (pos>=dev->atr_len || pos>=MAX_ATR) \
207 goto return_0;
208
209static unsigned int calc_baudv(unsigned char fidi)
210{
211 unsigned int wcrcf, wbrcf, fi_rfu, di_rfu;
212
213 fi_rfu = 372;
214 di_rfu = 1;
215
216 /* FI */
217 switch ((fidi >> 4) & 0x0F) {
218 case 0x00:
219 wcrcf = 372;
220 break;
221 case 0x01:
222 wcrcf = 372;
223 break;
224 case 0x02:
225 wcrcf = 558;
226 break;
227 case 0x03:
228 wcrcf = 744;
229 break;
230 case 0x04:
231 wcrcf = 1116;
232 break;
233 case 0x05:
234 wcrcf = 1488;
235 break;
236 case 0x06:
237 wcrcf = 1860;
238 break;
239 case 0x07:
240 wcrcf = fi_rfu;
241 break;
242 case 0x08:
243 wcrcf = fi_rfu;
244 break;
245 case 0x09:
246 wcrcf = 512;
247 break;
248 case 0x0A:
249 wcrcf = 768;
250 break;
251 case 0x0B:
252 wcrcf = 1024;
253 break;
254 case 0x0C:
255 wcrcf = 1536;
256 break;
257 case 0x0D:
258 wcrcf = 2048;
259 break;
260 default:
261 wcrcf = fi_rfu;
262 break;
263 }
264
265 /* DI */
266 switch (fidi & 0x0F) {
267 case 0x00:
268 wbrcf = di_rfu;
269 break;
270 case 0x01:
271 wbrcf = 1;
272 break;
273 case 0x02:
274 wbrcf = 2;
275 break;
276 case 0x03:
277 wbrcf = 4;
278 break;
279 case 0x04:
280 wbrcf = 8;
281 break;
282 case 0x05:
283 wbrcf = 16;
284 break;
285 case 0x06:
286 wbrcf = 32;
287 break;
288 case 0x07:
289 wbrcf = di_rfu;
290 break;
291 case 0x08:
292 wbrcf = 12;
293 break;
294 case 0x09:
295 wbrcf = 20;
296 break;
297 default:
298 wbrcf = di_rfu;
299 break;
300 }
301
302 return (wcrcf / wbrcf);
303}
304
Olof Johanssonecb8a842008-02-04 22:27:34 -0800305static unsigned short io_read_num_rec_bytes(unsigned int iobase,
306 unsigned short *s)
Harald Weltec1986ee2005-11-13 16:06:29 -0800307{
308 unsigned short tmp;
309
310 tmp = *s = 0;
311 do {
312 *s = tmp;
313 tmp = inb(REG_NUM_BYTES(iobase)) |
314 (inb(REG_FLAGS0(iobase)) & 4 ? 0x100 : 0);
315 } while (tmp != *s);
316
317 return *s;
318}
319
320static int parse_atr(struct cm4000_dev *dev)
321{
322 unsigned char any_t1, any_t0;
323 unsigned char ch, ifno;
324 int ix, done;
325
326 DEBUGP(3, dev, "-> parse_atr: dev->atr_len = %i\n", dev->atr_len);
327
328 if (dev->atr_len < 3) {
329 DEBUGP(5, dev, "parse_atr: atr_len < 3\n");
330 return 0;
331 }
332
333 if (dev->atr[0] == 0x3f)
334 set_bit(IS_INVREV, &dev->flags);
335 else
336 clear_bit(IS_INVREV, &dev->flags);
337 ix = 1;
338 ifno = 1;
339 ch = dev->atr[1];
340 dev->proto = 0; /* XXX PROTO */
341 any_t1 = any_t0 = done = 0;
342 dev->ta1 = 0x11; /* defaults to 9600 baud */
343 do {
344 if (ifno == 1 && (ch & 0x10)) {
345 /* read first interface byte and TA1 is present */
346 dev->ta1 = dev->atr[2];
347 DEBUGP(5, dev, "Card says FiDi is 0x%.2x\n", dev->ta1);
348 ifno++;
349 } else if ((ifno == 2) && (ch & 0x10)) { /* TA(2) */
350 dev->ta1 = 0x11;
351 ifno++;
352 }
353
354 DEBUGP(5, dev, "Yi=%.2x\n", ch & 0xf0);
355 ix += ((ch & 0x10) >> 4) /* no of int.face chars */
356 +((ch & 0x20) >> 5)
357 + ((ch & 0x40) >> 6)
358 + ((ch & 0x80) >> 7);
359 /* ATRLENCK(dev,ix); */
360 if (ch & 0x80) { /* TDi */
361 ch = dev->atr[ix];
362 if ((ch & 0x0f)) {
363 any_t1 = 1;
364 DEBUGP(5, dev, "card is capable of T=1\n");
365 } else {
366 any_t0 = 1;
367 DEBUGP(5, dev, "card is capable of T=0\n");
368 }
369 } else
370 done = 1;
371 } while (!done);
372
373 DEBUGP(5, dev, "ix=%d noHist=%d any_t1=%d\n",
374 ix, dev->atr[1] & 15, any_t1);
375 if (ix + 1 + (dev->atr[1] & 0x0f) + any_t1 != dev->atr_len) {
376 DEBUGP(5, dev, "length error\n");
377 return 0;
378 }
379 if (any_t0)
380 set_bit(IS_ANY_T0, &dev->flags);
381
382 if (any_t1) { /* compute csum */
383 dev->atr_csum = 0;
384#ifdef ATR_CSUM
385 for (i = 1; i < dev->atr_len; i++)
386 dev->atr_csum ^= dev->atr[i];
387 if (dev->atr_csum) {
388 set_bit(IS_BAD_CSUM, &dev->flags);
389 DEBUGP(5, dev, "bad checksum\n");
390 goto return_0;
391 }
392#endif
393 if (any_t0 == 0)
394 dev->proto = 1; /* XXX PROTO */
395 set_bit(IS_ANY_T1, &dev->flags);
396 }
397
398 return 1;
399}
400
401struct card_fixup {
402 char atr[12];
403 u_int8_t atr_len;
404 u_int8_t stopbits;
405};
406
407static struct card_fixup card_fixups[] = {
408 { /* ACOS */
409 .atr = { 0x3b, 0xb3, 0x11, 0x00, 0x00, 0x41, 0x01 },
410 .atr_len = 7,
411 .stopbits = 0x03,
412 },
413 { /* Motorola */
414 .atr = {0x3b, 0x76, 0x13, 0x00, 0x00, 0x80, 0x62, 0x07,
415 0x41, 0x81, 0x81 },
416 .atr_len = 11,
417 .stopbits = 0x04,
418 },
419};
420
421static void set_cardparameter(struct cm4000_dev *dev)
422{
423 int i;
Dominik Brodowski9a017a92010-07-24 15:58:54 +0200424 unsigned int iobase = dev->p_dev->resource[0]->start;
Harald Weltec1986ee2005-11-13 16:06:29 -0800425 u_int8_t stopbits = 0x02; /* ISO default */
426
427 DEBUGP(3, dev, "-> set_cardparameter\n");
428
429 dev->flags1 = dev->flags1 | (((dev->baudv - 1) & 0x0100) >> 8);
430 xoutb(dev->flags1, REG_FLAGS1(iobase));
431 DEBUGP(5, dev, "flags1 = 0x%02x\n", dev->flags1);
432
433 /* set baudrate */
434 xoutb((unsigned char)((dev->baudv - 1) & 0xFF), REG_BAUDRATE(iobase));
435
436 DEBUGP(5, dev, "baudv = %i -> write 0x%02x\n", dev->baudv,
437 ((dev->baudv - 1) & 0xFF));
438
439 /* set stopbits */
440 for (i = 0; i < ARRAY_SIZE(card_fixups); i++) {
441 if (!memcmp(dev->atr, card_fixups[i].atr,
442 card_fixups[i].atr_len))
443 stopbits = card_fixups[i].stopbits;
444 }
445 xoutb(stopbits, REG_STOPBITS(iobase));
446
447 DEBUGP(3, dev, "<- set_cardparameter\n");
448}
449
450static int set_protocol(struct cm4000_dev *dev, struct ptsreq *ptsreq)
451{
452
453 unsigned long tmp, i;
454 unsigned short num_bytes_read;
455 unsigned char pts_reply[4];
456 ssize_t rc;
Dominik Brodowski9a017a92010-07-24 15:58:54 +0200457 unsigned int iobase = dev->p_dev->resource[0]->start;
Harald Weltec1986ee2005-11-13 16:06:29 -0800458
459 rc = 0;
460
461 DEBUGP(3, dev, "-> set_protocol\n");
462 DEBUGP(5, dev, "ptsreq->Protocol = 0x%.8x, ptsreq->Flags=0x%.8x, "
463 "ptsreq->pts1=0x%.2x, ptsreq->pts2=0x%.2x, "
464 "ptsreq->pts3=0x%.2x\n", (unsigned int)ptsreq->protocol,
465 (unsigned int)ptsreq->flags, ptsreq->pts1, ptsreq->pts2,
466 ptsreq->pts3);
467
468 /* Fill PTS structure */
469 dev->pts[0] = 0xff;
470 dev->pts[1] = 0x00;
471 tmp = ptsreq->protocol;
472 while ((tmp = (tmp >> 1)) > 0)
473 dev->pts[1]++;
474 dev->proto = dev->pts[1]; /* Set new protocol */
475 dev->pts[1] = (0x01 << 4) | (dev->pts[1]);
476
477 /* Correct Fi/Di according to CM4000 Fi/Di table */
478 DEBUGP(5, dev, "Ta(1) from ATR is 0x%.2x\n", dev->ta1);
479 /* set Fi/Di according to ATR TA(1) */
480 dev->pts[2] = fi_di_table[dev->ta1 & 0x0F][(dev->ta1 >> 4) & 0x0F];
481
482 /* Calculate PCK character */
483 dev->pts[3] = dev->pts[0] ^ dev->pts[1] ^ dev->pts[2];
484
485 DEBUGP(5, dev, "pts0=%.2x, pts1=%.2x, pts2=%.2x, pts3=%.2x\n",
486 dev->pts[0], dev->pts[1], dev->pts[2], dev->pts[3]);
487
488 /* check card convention */
489 if (test_bit(IS_INVREV, &dev->flags))
490 str_invert_revert(dev->pts, 4);
491
492 /* reset SM */
493 xoutb(0x80, REG_FLAGS0(iobase));
494
495 /* Enable access to the message buffer */
496 DEBUGP(5, dev, "Enable access to the messages buffer\n");
497 dev->flags1 = 0x20 /* T_Active */
498 | (test_bit(IS_INVREV, &dev->flags) ? 0x02 : 0x00) /* inv parity */
499 | ((dev->baudv >> 8) & 0x01); /* MSB-baud */
500 xoutb(dev->flags1, REG_FLAGS1(iobase));
501
502 DEBUGP(5, dev, "Enable message buffer -> flags1 = 0x%.2x\n",
503 dev->flags1);
504
505 /* write challenge to the buffer */
506 DEBUGP(5, dev, "Write challenge to buffer: ");
507 for (i = 0; i < 4; i++) {
508 xoutb(i, REG_BUF_ADDR(iobase));
509 xoutb(dev->pts[i], REG_BUF_DATA(iobase)); /* buf data */
Dominik Brodowskicbf624f2009-10-24 15:47:29 +0200510#ifdef CM4000_DEBUG
511 pr_debug("0x%.2x ", dev->pts[i]);
Harald Weltec1986ee2005-11-13 16:06:29 -0800512 }
Dominik Brodowskicbf624f2009-10-24 15:47:29 +0200513 pr_debug("\n");
Harald Weltec1986ee2005-11-13 16:06:29 -0800514#else
515 }
516#endif
517
518 /* set number of bytes to write */
519 DEBUGP(5, dev, "Set number of bytes to write\n");
520 xoutb(0x04, REG_NUM_SEND(iobase));
521
522 /* Trigger CARDMAN CONTROLLER */
523 xoutb(0x50, REG_FLAGS0(iobase));
524
525 /* Monitor progress */
526 /* wait for xmit done */
527 DEBUGP(5, dev, "Waiting for NumRecBytes getting valid\n");
528
529 for (i = 0; i < 100; i++) {
530 if (inb(REG_FLAGS0(iobase)) & 0x08) {
531 DEBUGP(5, dev, "NumRecBytes is valid\n");
532 break;
533 }
534 mdelay(10);
535 }
536 if (i == 100) {
537 DEBUGP(5, dev, "Timeout waiting for NumRecBytes getting "
538 "valid\n");
539 rc = -EIO;
540 goto exit_setprotocol;
541 }
542
543 DEBUGP(5, dev, "Reading NumRecBytes\n");
544 for (i = 0; i < 100; i++) {
545 io_read_num_rec_bytes(iobase, &num_bytes_read);
546 if (num_bytes_read >= 4) {
547 DEBUGP(2, dev, "NumRecBytes = %i\n", num_bytes_read);
548 break;
549 }
550 mdelay(10);
551 }
552
553 /* check whether it is a short PTS reply? */
554 if (num_bytes_read == 3)
555 i = 0;
556
557 if (i == 100) {
558 DEBUGP(5, dev, "Timeout reading num_bytes_read\n");
559 rc = -EIO;
560 goto exit_setprotocol;
561 }
562
563 DEBUGP(5, dev, "Reset the CARDMAN CONTROLLER\n");
564 xoutb(0x80, REG_FLAGS0(iobase));
565
566 /* Read PPS reply */
567 DEBUGP(5, dev, "Read PPS reply\n");
568 for (i = 0; i < num_bytes_read; i++) {
569 xoutb(i, REG_BUF_ADDR(iobase));
570 pts_reply[i] = inb(REG_BUF_DATA(iobase));
571 }
572
Dominik Brodowskicbf624f2009-10-24 15:47:29 +0200573#ifdef CM4000_DEBUG
Harald Weltec1986ee2005-11-13 16:06:29 -0800574 DEBUGP(2, dev, "PTSreply: ");
575 for (i = 0; i < num_bytes_read; i++) {
Dominik Brodowskicbf624f2009-10-24 15:47:29 +0200576 pr_debug("0x%.2x ", pts_reply[i]);
Harald Weltec1986ee2005-11-13 16:06:29 -0800577 }
Dominik Brodowskicbf624f2009-10-24 15:47:29 +0200578 pr_debug("\n");
579#endif /* CM4000_DEBUG */
Harald Weltec1986ee2005-11-13 16:06:29 -0800580
581 DEBUGP(5, dev, "Clear Tactive in Flags1\n");
582 xoutb(0x20, REG_FLAGS1(iobase));
583
584 /* Compare ptsreq and ptsreply */
585 if ((dev->pts[0] == pts_reply[0]) &&
586 (dev->pts[1] == pts_reply[1]) &&
587 (dev->pts[2] == pts_reply[2]) && (dev->pts[3] == pts_reply[3])) {
588 /* setcardparameter according to PPS */
589 dev->baudv = calc_baudv(dev->pts[2]);
590 set_cardparameter(dev);
591 } else if ((dev->pts[0] == pts_reply[0]) &&
592 ((dev->pts[1] & 0xef) == pts_reply[1]) &&
593 ((pts_reply[0] ^ pts_reply[1]) == pts_reply[2])) {
594 /* short PTS reply, set card parameter to default values */
595 dev->baudv = calc_baudv(0x11);
596 set_cardparameter(dev);
597 } else
598 rc = -EIO;
599
600exit_setprotocol:
601 DEBUGP(3, dev, "<- set_protocol\n");
602 return rc;
603}
604
Olof Johanssonecb8a842008-02-04 22:27:34 -0800605static int io_detect_cm4000(unsigned int iobase, struct cm4000_dev *dev)
Harald Weltec1986ee2005-11-13 16:06:29 -0800606{
607
608 /* note: statemachine is assumed to be reset */
609 if (inb(REG_FLAGS0(iobase)) & 8) {
610 clear_bit(IS_ATR_VALID, &dev->flags);
611 set_bit(IS_CMM_ABSENT, &dev->flags);
612 return 0; /* detect CMM = 1 -> failure */
613 }
614 /* xoutb(0x40, REG_FLAGS1(iobase)); detectCMM */
615 xoutb(dev->flags1 | 0x40, REG_FLAGS1(iobase));
616 if ((inb(REG_FLAGS0(iobase)) & 8) == 0) {
617 clear_bit(IS_ATR_VALID, &dev->flags);
618 set_bit(IS_CMM_ABSENT, &dev->flags);
619 return 0; /* detect CMM=0 -> failure */
620 }
621 /* clear detectCMM again by restoring original flags1 */
622 xoutb(dev->flags1, REG_FLAGS1(iobase));
623 return 1;
624}
625
626static void terminate_monitor(struct cm4000_dev *dev)
627{
628
629 /* tell the monitor to stop and wait until
630 * it terminates.
631 */
632 DEBUGP(3, dev, "-> terminate_monitor\n");
633 wait_event_interruptible(dev->devq,
634 test_and_set_bit(LOCK_MONITOR,
635 (void *)&dev->flags));
636
637 /* now, LOCK_MONITOR has been set.
638 * allow a last cycle in the monitor.
639 * the monitor will indicate that it has
640 * finished by clearing this bit.
641 */
642 DEBUGP(5, dev, "Now allow last cycle of monitor!\n");
643 while (test_bit(LOCK_MONITOR, (void *)&dev->flags))
644 msleep(25);
645
646 DEBUGP(5, dev, "Delete timer\n");
647 del_timer_sync(&dev->timer);
Dominik Brodowskicbf624f2009-10-24 15:47:29 +0200648#ifdef CM4000_DEBUG
Harald Weltec1986ee2005-11-13 16:06:29 -0800649 dev->monitor_running = 0;
650#endif
651
652 DEBUGP(3, dev, "<- terminate_monitor\n");
653}
654
655/*
656 * monitor the card every 50msec. as a side-effect, retrieve the
657 * atr once a card is inserted. another side-effect of retrieving the
658 * atr is that the card will be powered on, so there is no need to
659 * power on the card explictely from the application: the driver
660 * is already doing that for you.
661 */
662
663static void monitor_card(unsigned long p)
664{
665 struct cm4000_dev *dev = (struct cm4000_dev *) p;
Dominik Brodowski9a017a92010-07-24 15:58:54 +0200666 unsigned int iobase = dev->p_dev->resource[0]->start;
Harald Weltec1986ee2005-11-13 16:06:29 -0800667 unsigned short s;
668 struct ptsreq ptsreq;
669 int i, atrc;
670
671 DEBUGP(7, dev, "-> monitor_card\n");
672
673 /* if someone has set the lock for us: we're done! */
674 if (test_and_set_bit(LOCK_MONITOR, &dev->flags)) {
675 DEBUGP(4, dev, "About to stop monitor\n");
676 /* no */
677 dev->rlen =
678 dev->rpos =
679 dev->atr_csum = dev->atr_len_retry = dev->cwarn = 0;
680 dev->mstate = M_FETCH_ATR;
681 clear_bit(LOCK_MONITOR, &dev->flags);
682 /* close et al. are sleeping on devq, so wake it */
683 wake_up_interruptible(&dev->devq);
684 DEBUGP(2, dev, "<- monitor_card (we are done now)\n");
685 return;
686 }
687
688 /* try to lock io: if it is already locked, just add another timer */
689 if (test_and_set_bit(LOCK_IO, (void *)&dev->flags)) {
690 DEBUGP(4, dev, "Couldn't get IO lock\n");
691 goto return_with_timer;
692 }
693
694 /* is a card/a reader inserted at all ? */
695 dev->flags0 = xinb(REG_FLAGS0(iobase));
696 DEBUGP(7, dev, "dev->flags0 = 0x%2x\n", dev->flags0);
697 DEBUGP(7, dev, "smartcard present: %s\n",
698 dev->flags0 & 1 ? "yes" : "no");
699 DEBUGP(7, dev, "cardman present: %s\n",
700 dev->flags0 == 0xff ? "no" : "yes");
701
702 if ((dev->flags0 & 1) == 0 /* no smartcard inserted */
703 || dev->flags0 == 0xff) { /* no cardman inserted */
704 /* no */
705 dev->rlen =
706 dev->rpos =
707 dev->atr_csum = dev->atr_len_retry = dev->cwarn = 0;
708 dev->mstate = M_FETCH_ATR;
709
710 dev->flags &= 0x000000ff; /* only keep IO and MONITOR locks */
711
712 if (dev->flags0 == 0xff) {
713 DEBUGP(4, dev, "set IS_CMM_ABSENT bit\n");
714 set_bit(IS_CMM_ABSENT, &dev->flags);
715 } else if (test_bit(IS_CMM_ABSENT, &dev->flags)) {
716 DEBUGP(4, dev, "clear IS_CMM_ABSENT bit "
717 "(card is removed)\n");
718 clear_bit(IS_CMM_ABSENT, &dev->flags);
719 }
720
721 goto release_io;
722 } else if ((dev->flags0 & 1) && test_bit(IS_CMM_ABSENT, &dev->flags)) {
723 /* cardman and card present but cardman was absent before
724 * (after suspend with inserted card) */
725 DEBUGP(4, dev, "clear IS_CMM_ABSENT bit (card is inserted)\n");
726 clear_bit(IS_CMM_ABSENT, &dev->flags);
727 }
728
729 if (test_bit(IS_ATR_VALID, &dev->flags) == 1) {
730 DEBUGP(7, dev, "believe ATR is already valid (do nothing)\n");
731 goto release_io;
732 }
733
734 switch (dev->mstate) {
735 unsigned char flags0;
736 case M_CARDOFF:
737 DEBUGP(4, dev, "M_CARDOFF\n");
738 flags0 = inb(REG_FLAGS0(iobase));
739 if (flags0 & 0x02) {
740 /* wait until Flags0 indicate power is off */
741 dev->mdelay = T_10MSEC;
742 } else {
743 /* Flags0 indicate power off and no card inserted now;
744 * Reset CARDMAN CONTROLLER */
745 xoutb(0x80, REG_FLAGS0(iobase));
746
747 /* prepare for fetching ATR again: after card off ATR
748 * is read again automatically */
749 dev->rlen =
750 dev->rpos =
751 dev->atr_csum =
752 dev->atr_len_retry = dev->cwarn = 0;
753 dev->mstate = M_FETCH_ATR;
754
755 /* minimal gap between CARDOFF and read ATR is 50msec */
756 dev->mdelay = T_50MSEC;
757 }
758 break;
759 case M_FETCH_ATR:
760 DEBUGP(4, dev, "M_FETCH_ATR\n");
761 xoutb(0x80, REG_FLAGS0(iobase));
762 DEBUGP(4, dev, "Reset BAUDV to 9600\n");
763 dev->baudv = 0x173; /* 9600 */
764 xoutb(0x02, REG_STOPBITS(iobase)); /* stopbits=2 */
765 xoutb(0x73, REG_BAUDRATE(iobase)); /* baud value */
766 xoutb(0x21, REG_FLAGS1(iobase)); /* T_Active=1, baud
767 value */
768 /* warm start vs. power on: */
769 xoutb(dev->flags0 & 2 ? 0x46 : 0x44, REG_FLAGS0(iobase));
770 dev->mdelay = T_40MSEC;
771 dev->mstate = M_TIMEOUT_WAIT;
772 break;
773 case M_TIMEOUT_WAIT:
774 DEBUGP(4, dev, "M_TIMEOUT_WAIT\n");
775 /* numRecBytes */
776 io_read_num_rec_bytes(iobase, &dev->atr_len);
777 dev->mdelay = T_10MSEC;
778 dev->mstate = M_READ_ATR_LEN;
779 break;
780 case M_READ_ATR_LEN:
781 DEBUGP(4, dev, "M_READ_ATR_LEN\n");
782 /* infinite loop possible, since there is no timeout */
783
784#define MAX_ATR_LEN_RETRY 100
785
786 if (dev->atr_len == io_read_num_rec_bytes(iobase, &s)) {
787 if (dev->atr_len_retry++ >= MAX_ATR_LEN_RETRY) { /* + XX msec */
788 dev->mdelay = T_10MSEC;
789 dev->mstate = M_READ_ATR;
790 }
791 } else {
792 dev->atr_len = s;
793 dev->atr_len_retry = 0; /* set new timeout */
794 }
795
796 DEBUGP(4, dev, "Current ATR_LEN = %i\n", dev->atr_len);
797 break;
798 case M_READ_ATR:
799 DEBUGP(4, dev, "M_READ_ATR\n");
800 xoutb(0x80, REG_FLAGS0(iobase)); /* reset SM */
801 for (i = 0; i < dev->atr_len; i++) {
802 xoutb(i, REG_BUF_ADDR(iobase));
803 dev->atr[i] = inb(REG_BUF_DATA(iobase));
804 }
805 /* Deactivate T_Active flags */
806 DEBUGP(4, dev, "Deactivate T_Active flags\n");
807 dev->flags1 = 0x01;
808 xoutb(dev->flags1, REG_FLAGS1(iobase));
809
810 /* atr is present (which doesnt mean it's valid) */
811 set_bit(IS_ATR_PRESENT, &dev->flags);
812 if (dev->atr[0] == 0x03)
813 str_invert_revert(dev->atr, dev->atr_len);
814 atrc = parse_atr(dev);
815 if (atrc == 0) { /* atr invalid */
816 dev->mdelay = 0;
817 dev->mstate = M_BAD_CARD;
818 } else {
819 dev->mdelay = T_50MSEC;
820 dev->mstate = M_ATR_PRESENT;
821 set_bit(IS_ATR_VALID, &dev->flags);
822 }
823
824 if (test_bit(IS_ATR_VALID, &dev->flags) == 1) {
825 DEBUGP(4, dev, "monitor_card: ATR valid\n");
826 /* if ta1 == 0x11, no PPS necessary (default values) */
827 /* do not do PPS with multi protocol cards */
828 if ((test_bit(IS_AUTOPPS_ACT, &dev->flags) == 0) &&
829 (dev->ta1 != 0x11) &&
830 !(test_bit(IS_ANY_T0, &dev->flags) &&
831 test_bit(IS_ANY_T1, &dev->flags))) {
832 DEBUGP(4, dev, "Perform AUTOPPS\n");
833 set_bit(IS_AUTOPPS_ACT, &dev->flags);
834 ptsreq.protocol = ptsreq.protocol =
835 (0x01 << dev->proto);
836 ptsreq.flags = 0x01;
837 ptsreq.pts1 = 0x00;
838 ptsreq.pts2 = 0x00;
839 ptsreq.pts3 = 0x00;
840 if (set_protocol(dev, &ptsreq) == 0) {
841 DEBUGP(4, dev, "AUTOPPS ret SUCC\n");
842 clear_bit(IS_AUTOPPS_ACT, &dev->flags);
843 wake_up_interruptible(&dev->atrq);
844 } else {
845 DEBUGP(4, dev, "AUTOPPS failed: "
846 "repower using defaults\n");
847 /* prepare for repowering */
848 clear_bit(IS_ATR_PRESENT, &dev->flags);
849 clear_bit(IS_ATR_VALID, &dev->flags);
850 dev->rlen =
851 dev->rpos =
852 dev->atr_csum =
853 dev->atr_len_retry = dev->cwarn = 0;
854 dev->mstate = M_FETCH_ATR;
855
856 dev->mdelay = T_50MSEC;
857 }
858 } else {
859 /* for cards which use slightly different
860 * params (extra guard time) */
861 set_cardparameter(dev);
862 if (test_bit(IS_AUTOPPS_ACT, &dev->flags) == 1)
863 DEBUGP(4, dev, "AUTOPPS already active "
864 "2nd try:use default values\n");
865 if (dev->ta1 == 0x11)
866 DEBUGP(4, dev, "No AUTOPPS necessary "
867 "TA(1)==0x11\n");
868 if (test_bit(IS_ANY_T0, &dev->flags)
869 && test_bit(IS_ANY_T1, &dev->flags))
870 DEBUGP(4, dev, "Do NOT perform AUTOPPS "
871 "with multiprotocol cards\n");
872 clear_bit(IS_AUTOPPS_ACT, &dev->flags);
873 wake_up_interruptible(&dev->atrq);
874 }
875 } else {
876 DEBUGP(4, dev, "ATR invalid\n");
877 wake_up_interruptible(&dev->atrq);
878 }
879 break;
880 case M_BAD_CARD:
881 DEBUGP(4, dev, "M_BAD_CARD\n");
882 /* slow down warning, but prompt immediately after insertion */
883 if (dev->cwarn == 0 || dev->cwarn == 10) {
884 set_bit(IS_BAD_CARD, &dev->flags);
Dominik Brodowskided6a1a2010-03-20 19:35:12 +0100885 dev_warn(&dev->p_dev->dev, MODULE_NAME ": ");
Harald Weltec1986ee2005-11-13 16:06:29 -0800886 if (test_bit(IS_BAD_CSUM, &dev->flags)) {
887 DEBUGP(4, dev, "ATR checksum (0x%.2x, should "
888 "be zero) failed\n", dev->atr_csum);
889 }
Dominik Brodowskicbf624f2009-10-24 15:47:29 +0200890#ifdef CM4000_DEBUG
Harald Weltec1986ee2005-11-13 16:06:29 -0800891 else if (test_bit(IS_BAD_LENGTH, &dev->flags)) {
892 DEBUGP(4, dev, "ATR length error\n");
893 } else {
894 DEBUGP(4, dev, "card damaged or wrong way "
895 "inserted\n");
896 }
897#endif
898 dev->cwarn = 0;
899 wake_up_interruptible(&dev->atrq); /* wake open */
900 }
901 dev->cwarn++;
902 dev->mdelay = T_100MSEC;
903 dev->mstate = M_FETCH_ATR;
904 break;
905 default:
906 DEBUGP(7, dev, "Unknown action\n");
907 break; /* nothing */
908 }
909
910release_io:
911 DEBUGP(7, dev, "release_io\n");
912 clear_bit(LOCK_IO, &dev->flags);
913 wake_up_interruptible(&dev->ioq); /* whoever needs IO */
914
915return_with_timer:
916 DEBUGP(7, dev, "<- monitor_card (returns with timer)\n");
Jiri Slaby40565f12007-02-12 00:52:31 -0800917 mod_timer(&dev->timer, jiffies + dev->mdelay);
Harald Weltec1986ee2005-11-13 16:06:29 -0800918 clear_bit(LOCK_MONITOR, &dev->flags);
919}
920
921/* Interface to userland (file_operations) */
922
923static ssize_t cmm_read(struct file *filp, __user char *buf, size_t count,
924 loff_t *ppos)
925{
926 struct cm4000_dev *dev = filp->private_data;
Dominik Brodowski9a017a92010-07-24 15:58:54 +0200927 unsigned int iobase = dev->p_dev->resource[0]->start;
Harald Weltec1986ee2005-11-13 16:06:29 -0800928 ssize_t rc;
929 int i, j, k;
930
931 DEBUGP(2, dev, "-> cmm_read(%s,%d)\n", current->comm, current->pid);
932
933 if (count == 0) /* according to manpage */
934 return 0;
935
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100936 if (!pcmcia_dev_present(dev->p_dev) || /* device removed */
Harald Weltec1986ee2005-11-13 16:06:29 -0800937 test_bit(IS_CMM_ABSENT, &dev->flags))
938 return -ENODEV;
939
940 if (test_bit(IS_BAD_CSUM, &dev->flags))
941 return -EIO;
942
943 /* also see the note about this in cmm_write */
944 if (wait_event_interruptible
945 (dev->atrq,
946 ((filp->f_flags & O_NONBLOCK)
947 || (test_bit(IS_ATR_PRESENT, (void *)&dev->flags) != 0)))) {
948 if (filp->f_flags & O_NONBLOCK)
949 return -EAGAIN;
950 return -ERESTARTSYS;
951 }
952
953 if (test_bit(IS_ATR_VALID, &dev->flags) == 0)
954 return -EIO;
955
956 /* this one implements blocking IO */
957 if (wait_event_interruptible
958 (dev->readq,
959 ((filp->f_flags & O_NONBLOCK) || (dev->rpos < dev->rlen)))) {
960 if (filp->f_flags & O_NONBLOCK)
961 return -EAGAIN;
962 return -ERESTARTSYS;
963 }
964
965 /* lock io */
966 if (wait_event_interruptible
967 (dev->ioq,
968 ((filp->f_flags & O_NONBLOCK)
969 || (test_and_set_bit(LOCK_IO, (void *)&dev->flags) == 0)))) {
970 if (filp->f_flags & O_NONBLOCK)
971 return -EAGAIN;
972 return -ERESTARTSYS;
973 }
974
975 rc = 0;
976 dev->flags0 = inb(REG_FLAGS0(iobase));
977 if ((dev->flags0 & 1) == 0 /* no smartcard inserted */
978 || dev->flags0 == 0xff) { /* no cardman inserted */
979 clear_bit(IS_ATR_VALID, &dev->flags);
980 if (dev->flags0 & 1) {
981 set_bit(IS_CMM_ABSENT, &dev->flags);
982 rc = -ENODEV;
983 }
984 rc = -EIO;
985 goto release_io;
986 }
987
988 DEBUGP(4, dev, "begin read answer\n");
989 j = min(count, (size_t)(dev->rlen - dev->rpos));
990 k = dev->rpos;
991 if (k + j > 255)
992 j = 256 - k;
993 DEBUGP(4, dev, "read1 j=%d\n", j);
994 for (i = 0; i < j; i++) {
995 xoutb(k++, REG_BUF_ADDR(iobase));
996 dev->rbuf[i] = xinb(REG_BUF_DATA(iobase));
997 }
998 j = min(count, (size_t)(dev->rlen - dev->rpos));
999 if (k + j > 255) {
1000 DEBUGP(4, dev, "read2 j=%d\n", j);
1001 dev->flags1 |= 0x10; /* MSB buf addr set */
1002 xoutb(dev->flags1, REG_FLAGS1(iobase));
1003 for (; i < j; i++) {
1004 xoutb(k++, REG_BUF_ADDR(iobase));
1005 dev->rbuf[i] = xinb(REG_BUF_DATA(iobase));
1006 }
1007 }
1008
Roel Kluin470967d2009-09-21 17:03:54 -07001009 if (dev->proto == 0 && count > dev->rlen - dev->rpos && i) {
Harald Weltec1986ee2005-11-13 16:06:29 -08001010 DEBUGP(4, dev, "T=0 and count > buffer\n");
1011 dev->rbuf[i] = dev->rbuf[i - 1];
1012 dev->rbuf[i - 1] = dev->procbyte;
1013 j++;
1014 }
1015 count = j;
1016
1017 dev->rpos = dev->rlen + 1;
1018
1019 /* Clear T1Active */
1020 DEBUGP(4, dev, "Clear T1Active\n");
1021 dev->flags1 &= 0xdf;
1022 xoutb(dev->flags1, REG_FLAGS1(iobase));
1023
1024 xoutb(0, REG_FLAGS1(iobase)); /* clear detectCMM */
1025 /* last check before exit */
Dan Carpenter07a71412010-04-18 22:07:33 +03001026 if (!io_detect_cm4000(iobase, dev)) {
1027 rc = -ENODEV;
1028 goto release_io;
1029 }
Harald Weltec1986ee2005-11-13 16:06:29 -08001030
1031 if (test_bit(IS_INVREV, &dev->flags) && count > 0)
1032 str_invert_revert(dev->rbuf, count);
1033
1034 if (copy_to_user(buf, dev->rbuf, count))
Dan Carpenter07a71412010-04-18 22:07:33 +03001035 rc = -EFAULT;
Harald Weltec1986ee2005-11-13 16:06:29 -08001036
1037release_io:
1038 clear_bit(LOCK_IO, &dev->flags);
1039 wake_up_interruptible(&dev->ioq);
1040
1041 DEBUGP(2, dev, "<- cmm_read returns: rc = %Zi\n",
1042 (rc < 0 ? rc : count));
1043 return rc < 0 ? rc : count;
1044}
1045
1046static ssize_t cmm_write(struct file *filp, const char __user *buf,
1047 size_t count, loff_t *ppos)
1048{
H Hartley Sweetenc6be9c52010-01-08 11:30:13 -07001049 struct cm4000_dev *dev = filp->private_data;
Dominik Brodowski9a017a92010-07-24 15:58:54 +02001050 unsigned int iobase = dev->p_dev->resource[0]->start;
Harald Weltec1986ee2005-11-13 16:06:29 -08001051 unsigned short s;
1052 unsigned char tmp;
1053 unsigned char infolen;
1054 unsigned char sendT0;
1055 unsigned short nsend;
1056 unsigned short nr;
1057 ssize_t rc;
1058 int i;
1059
1060 DEBUGP(2, dev, "-> cmm_write(%s,%d)\n", current->comm, current->pid);
1061
1062 if (count == 0) /* according to manpage */
1063 return 0;
1064
1065 if (dev->proto == 0 && count < 4) {
1066 /* T0 must have at least 4 bytes */
1067 DEBUGP(4, dev, "T0 short write\n");
1068 return -EIO;
1069 }
1070
1071 nr = count & 0x1ff; /* max bytes to write */
1072
1073 sendT0 = dev->proto ? 0 : nr > 5 ? 0x08 : 0;
1074
Dominik Brodowskie2d40962006-03-02 00:09:29 +01001075 if (!pcmcia_dev_present(dev->p_dev) || /* device removed */
Harald Weltec1986ee2005-11-13 16:06:29 -08001076 test_bit(IS_CMM_ABSENT, &dev->flags))
1077 return -ENODEV;
1078
1079 if (test_bit(IS_BAD_CSUM, &dev->flags)) {
1080 DEBUGP(4, dev, "bad csum\n");
1081 return -EIO;
1082 }
1083
1084 /*
1085 * wait for atr to become valid.
1086 * note: it is important to lock this code. if we dont, the monitor
Michael Opdenacker59c51592007-05-09 08:57:56 +02001087 * could be run between test_bit and the call to sleep on the
Harald Weltec1986ee2005-11-13 16:06:29 -08001088 * atr-queue. if *then* the monitor detects atr valid, it will wake up
1089 * any process on the atr-queue, *but* since we have been interrupted,
1090 * we do not yet sleep on this queue. this would result in a missed
1091 * wake_up and the calling process would sleep forever (until
1092 * interrupted). also, do *not* restore_flags before sleep_on, because
1093 * this could result in the same situation!
1094 */
1095 if (wait_event_interruptible
1096 (dev->atrq,
1097 ((filp->f_flags & O_NONBLOCK)
1098 || (test_bit(IS_ATR_PRESENT, (void *)&dev->flags) != 0)))) {
1099 if (filp->f_flags & O_NONBLOCK)
1100 return -EAGAIN;
1101 return -ERESTARTSYS;
1102 }
1103
1104 if (test_bit(IS_ATR_VALID, &dev->flags) == 0) { /* invalid atr */
1105 DEBUGP(4, dev, "invalid ATR\n");
1106 return -EIO;
1107 }
1108
1109 /* lock io */
1110 if (wait_event_interruptible
1111 (dev->ioq,
1112 ((filp->f_flags & O_NONBLOCK)
1113 || (test_and_set_bit(LOCK_IO, (void *)&dev->flags) == 0)))) {
1114 if (filp->f_flags & O_NONBLOCK)
1115 return -EAGAIN;
1116 return -ERESTARTSYS;
1117 }
1118
1119 if (copy_from_user(dev->sbuf, buf, ((count > 512) ? 512 : count)))
1120 return -EFAULT;
1121
1122 rc = 0;
1123 dev->flags0 = inb(REG_FLAGS0(iobase));
1124 if ((dev->flags0 & 1) == 0 /* no smartcard inserted */
1125 || dev->flags0 == 0xff) { /* no cardman inserted */
1126 clear_bit(IS_ATR_VALID, &dev->flags);
1127 if (dev->flags0 & 1) {
1128 set_bit(IS_CMM_ABSENT, &dev->flags);
1129 rc = -ENODEV;
1130 } else {
1131 DEBUGP(4, dev, "IO error\n");
1132 rc = -EIO;
1133 }
1134 goto release_io;
1135 }
1136
1137 xoutb(0x80, REG_FLAGS0(iobase)); /* reset SM */
1138
1139 if (!io_detect_cm4000(iobase, dev)) {
1140 rc = -ENODEV;
1141 goto release_io;
1142 }
1143
1144 /* reflect T=0 send/read mode in flags1 */
1145 dev->flags1 |= (sendT0);
1146
1147 set_cardparameter(dev);
1148
1149 /* dummy read, reset flag procedure received */
1150 tmp = inb(REG_FLAGS1(iobase));
1151
1152 dev->flags1 = 0x20 /* T_Active */
1153 | (sendT0)
1154 | (test_bit(IS_INVREV, &dev->flags) ? 2 : 0)/* inverse parity */
1155 | (((dev->baudv - 1) & 0x0100) >> 8); /* MSB-Baud */
1156 DEBUGP(1, dev, "set dev->flags1 = 0x%.2x\n", dev->flags1);
1157 xoutb(dev->flags1, REG_FLAGS1(iobase));
1158
1159 /* xmit data */
1160 DEBUGP(4, dev, "Xmit data\n");
1161 for (i = 0; i < nr; i++) {
1162 if (i >= 256) {
1163 dev->flags1 = 0x20 /* T_Active */
1164 | (sendT0) /* SendT0 */
1165 /* inverse parity: */
1166 | (test_bit(IS_INVREV, &dev->flags) ? 2 : 0)
1167 | (((dev->baudv - 1) & 0x0100) >> 8) /* MSB-Baud */
1168 | 0x10; /* set address high */
1169 DEBUGP(4, dev, "dev->flags = 0x%.2x - set address "
1170 "high\n", dev->flags1);
1171 xoutb(dev->flags1, REG_FLAGS1(iobase));
1172 }
1173 if (test_bit(IS_INVREV, &dev->flags)) {
1174 DEBUGP(4, dev, "Apply inverse convention for 0x%.2x "
1175 "-> 0x%.2x\n", (unsigned char)dev->sbuf[i],
1176 invert_revert(dev->sbuf[i]));
1177 xoutb(i, REG_BUF_ADDR(iobase));
1178 xoutb(invert_revert(dev->sbuf[i]),
1179 REG_BUF_DATA(iobase));
1180 } else {
1181 xoutb(i, REG_BUF_ADDR(iobase));
1182 xoutb(dev->sbuf[i], REG_BUF_DATA(iobase));
1183 }
1184 }
1185 DEBUGP(4, dev, "Xmit done\n");
1186
1187 if (dev->proto == 0) {
1188 /* T=0 proto: 0 byte reply */
1189 if (nr == 4) {
1190 DEBUGP(4, dev, "T=0 assumes 0 byte reply\n");
1191 xoutb(i, REG_BUF_ADDR(iobase));
1192 if (test_bit(IS_INVREV, &dev->flags))
1193 xoutb(0xff, REG_BUF_DATA(iobase));
1194 else
1195 xoutb(0x00, REG_BUF_DATA(iobase));
1196 }
1197
1198 /* numSendBytes */
1199 if (sendT0)
1200 nsend = nr;
1201 else {
1202 if (nr == 4)
1203 nsend = 5;
1204 else {
1205 nsend = 5 + (unsigned char)dev->sbuf[4];
1206 if (dev->sbuf[4] == 0)
1207 nsend += 0x100;
1208 }
1209 }
1210 } else
1211 nsend = nr;
1212
1213 /* T0: output procedure byte */
1214 if (test_bit(IS_INVREV, &dev->flags)) {
1215 DEBUGP(4, dev, "T=0 set Procedure byte (inverse-reverse) "
1216 "0x%.2x\n", invert_revert(dev->sbuf[1]));
1217 xoutb(invert_revert(dev->sbuf[1]), REG_NUM_BYTES(iobase));
1218 } else {
1219 DEBUGP(4, dev, "T=0 set Procedure byte 0x%.2x\n", dev->sbuf[1]);
1220 xoutb(dev->sbuf[1], REG_NUM_BYTES(iobase));
1221 }
1222
1223 DEBUGP(1, dev, "set NumSendBytes = 0x%.2x\n",
1224 (unsigned char)(nsend & 0xff));
1225 xoutb((unsigned char)(nsend & 0xff), REG_NUM_SEND(iobase));
1226
1227 DEBUGP(1, dev, "Trigger CARDMAN CONTROLLER (0x%.2x)\n",
1228 0x40 /* SM_Active */
1229 | (dev->flags0 & 2 ? 0 : 4) /* power on if needed */
1230 |(dev->proto ? 0x10 : 0x08) /* T=1/T=0 */
1231 |(nsend & 0x100) >> 8 /* MSB numSendBytes */ );
1232 xoutb(0x40 /* SM_Active */
1233 | (dev->flags0 & 2 ? 0 : 4) /* power on if needed */
1234 |(dev->proto ? 0x10 : 0x08) /* T=1/T=0 */
1235 |(nsend & 0x100) >> 8, /* MSB numSendBytes */
1236 REG_FLAGS0(iobase));
1237
1238 /* wait for xmit done */
1239 if (dev->proto == 1) {
1240 DEBUGP(4, dev, "Wait for xmit done\n");
1241 for (i = 0; i < 1000; i++) {
1242 if (inb(REG_FLAGS0(iobase)) & 0x08)
1243 break;
1244 msleep_interruptible(10);
1245 }
1246 if (i == 1000) {
1247 DEBUGP(4, dev, "timeout waiting for xmit done\n");
1248 rc = -EIO;
1249 goto release_io;
1250 }
1251 }
1252
1253 /* T=1: wait for infoLen */
1254
1255 infolen = 0;
1256 if (dev->proto) {
1257 /* wait until infoLen is valid */
1258 for (i = 0; i < 6000; i++) { /* max waiting time of 1 min */
1259 io_read_num_rec_bytes(iobase, &s);
1260 if (s >= 3) {
1261 infolen = inb(REG_FLAGS1(iobase));
1262 DEBUGP(4, dev, "infolen=%d\n", infolen);
1263 break;
1264 }
1265 msleep_interruptible(10);
1266 }
1267 if (i == 6000) {
1268 DEBUGP(4, dev, "timeout waiting for infoLen\n");
1269 rc = -EIO;
1270 goto release_io;
1271 }
1272 } else
1273 clear_bit(IS_PROCBYTE_PRESENT, &dev->flags);
1274
1275 /* numRecBytes | bit9 of numRecytes */
1276 io_read_num_rec_bytes(iobase, &dev->rlen);
1277 for (i = 0; i < 600; i++) { /* max waiting time of 2 sec */
1278 if (dev->proto) {
1279 if (dev->rlen >= infolen + 4)
1280 break;
1281 }
1282 msleep_interruptible(10);
1283 /* numRecBytes | bit9 of numRecytes */
1284 io_read_num_rec_bytes(iobase, &s);
1285 if (s > dev->rlen) {
1286 DEBUGP(1, dev, "NumRecBytes inc (reset timeout)\n");
1287 i = 0; /* reset timeout */
1288 dev->rlen = s;
1289 }
1290 /* T=0: we are done when numRecBytes doesn't
1291 * increment any more and NoProcedureByte
1292 * is set and numRecBytes == bytes sent + 6
1293 * (header bytes + data + 1 for sw2)
1294 * except when the card replies an error
1295 * which means, no data will be sent back.
1296 */
1297 else if (dev->proto == 0) {
1298 if ((inb(REG_BUF_ADDR(iobase)) & 0x80)) {
1299 /* no procedure byte received since last read */
1300 DEBUGP(1, dev, "NoProcedure byte set\n");
1301 /* i=0; */
1302 } else {
1303 /* procedure byte received since last read */
1304 DEBUGP(1, dev, "NoProcedure byte unset "
1305 "(reset timeout)\n");
1306 dev->procbyte = inb(REG_FLAGS1(iobase));
1307 DEBUGP(1, dev, "Read procedure byte 0x%.2x\n",
1308 dev->procbyte);
1309 i = 0; /* resettimeout */
1310 }
1311 if (inb(REG_FLAGS0(iobase)) & 0x08) {
1312 DEBUGP(1, dev, "T0Done flag (read reply)\n");
1313 break;
1314 }
1315 }
1316 if (dev->proto)
1317 infolen = inb(REG_FLAGS1(iobase));
1318 }
1319 if (i == 600) {
1320 DEBUGP(1, dev, "timeout waiting for numRecBytes\n");
1321 rc = -EIO;
1322 goto release_io;
1323 } else {
1324 if (dev->proto == 0) {
1325 DEBUGP(1, dev, "Wait for T0Done bit to be set\n");
1326 for (i = 0; i < 1000; i++) {
1327 if (inb(REG_FLAGS0(iobase)) & 0x08)
1328 break;
1329 msleep_interruptible(10);
1330 }
1331 if (i == 1000) {
1332 DEBUGP(1, dev, "timeout waiting for T0Done\n");
1333 rc = -EIO;
1334 goto release_io;
1335 }
1336
1337 dev->procbyte = inb(REG_FLAGS1(iobase));
1338 DEBUGP(4, dev, "Read procedure byte 0x%.2x\n",
1339 dev->procbyte);
1340
1341 io_read_num_rec_bytes(iobase, &dev->rlen);
1342 DEBUGP(4, dev, "Read NumRecBytes = %i\n", dev->rlen);
1343
1344 }
1345 }
1346 /* T=1: read offset=zero, T=0: read offset=after challenge */
1347 dev->rpos = dev->proto ? 0 : nr == 4 ? 5 : nr > dev->rlen ? 5 : nr;
1348 DEBUGP(4, dev, "dev->rlen = %i, dev->rpos = %i, nr = %i\n",
1349 dev->rlen, dev->rpos, nr);
1350
1351release_io:
1352 DEBUGP(4, dev, "Reset SM\n");
1353 xoutb(0x80, REG_FLAGS0(iobase)); /* reset SM */
1354
1355 if (rc < 0) {
1356 DEBUGP(4, dev, "Write failed but clear T_Active\n");
1357 dev->flags1 &= 0xdf;
1358 xoutb(dev->flags1, REG_FLAGS1(iobase));
1359 }
1360
1361 clear_bit(LOCK_IO, &dev->flags);
1362 wake_up_interruptible(&dev->ioq);
1363 wake_up_interruptible(&dev->readq); /* tell read we have data */
1364
1365 /* ITSEC E2: clear write buffer */
1366 memset((char *)dev->sbuf, 0, 512);
1367
1368 /* return error or actually written bytes */
1369 DEBUGP(2, dev, "<- cmm_write\n");
1370 return rc < 0 ? rc : nr;
1371}
1372
1373static void start_monitor(struct cm4000_dev *dev)
1374{
1375 DEBUGP(3, dev, "-> start_monitor\n");
1376 if (!dev->monitor_running) {
1377 DEBUGP(5, dev, "create, init and add timer\n");
Jiri Slaby40565f12007-02-12 00:52:31 -08001378 setup_timer(&dev->timer, monitor_card, (unsigned long)dev);
Harald Weltec1986ee2005-11-13 16:06:29 -08001379 dev->monitor_running = 1;
Jiri Slaby40565f12007-02-12 00:52:31 -08001380 mod_timer(&dev->timer, jiffies);
Harald Weltec1986ee2005-11-13 16:06:29 -08001381 } else
1382 DEBUGP(5, dev, "monitor already running\n");
1383 DEBUGP(3, dev, "<- start_monitor\n");
1384}
1385
1386static void stop_monitor(struct cm4000_dev *dev)
1387{
1388 DEBUGP(3, dev, "-> stop_monitor\n");
1389 if (dev->monitor_running) {
1390 DEBUGP(5, dev, "stopping monitor\n");
1391 terminate_monitor(dev);
1392 /* reset monitor SM */
1393 clear_bit(IS_ATR_VALID, &dev->flags);
1394 clear_bit(IS_ATR_PRESENT, &dev->flags);
1395 } else
1396 DEBUGP(5, dev, "monitor already stopped\n");
1397 DEBUGP(3, dev, "<- stop_monitor\n");
1398}
1399
Alan Cox4cf974c2008-06-14 16:01:57 +02001400static long cmm_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Harald Weltec1986ee2005-11-13 16:06:29 -08001401{
1402 struct cm4000_dev *dev = filp->private_data;
Dominik Brodowski9a017a92010-07-24 15:58:54 +02001403 unsigned int iobase = dev->p_dev->resource[0]->start;
Alan Cox4cf974c2008-06-14 16:01:57 +02001404 struct inode *inode = filp->f_path.dentry->d_inode;
Dominik Brodowskifba395e2006-03-31 17:21:06 +02001405 struct pcmcia_device *link;
Harald Weltec1986ee2005-11-13 16:06:29 -08001406 int size;
1407 int rc;
Al Viroc4aa02e2005-12-15 09:17:55 +00001408 void __user *argp = (void __user *)arg;
Dominik Brodowskicbf624f2009-10-24 15:47:29 +02001409#ifdef CM4000_DEBUG
Harald Weltec1986ee2005-11-13 16:06:29 -08001410 char *ioctl_names[CM_IOC_MAXNR + 1] = {
1411 [_IOC_NR(CM_IOCGSTATUS)] "CM_IOCGSTATUS",
1412 [_IOC_NR(CM_IOCGATR)] "CM_IOCGATR",
1413 [_IOC_NR(CM_IOCARDOFF)] "CM_IOCARDOFF",
1414 [_IOC_NR(CM_IOCSPTS)] "CM_IOCSPTS",
1415 [_IOC_NR(CM_IOSDBGLVL)] "CM4000_DBGLVL",
1416 };
Harald Weltec1986ee2005-11-13 16:06:29 -08001417 DEBUGP(3, dev, "cmm_ioctl(device=%d.%d) %s\n", imajor(inode),
1418 iminor(inode), ioctl_names[_IOC_NR(cmd)]);
Dominik Brodowskicbf624f2009-10-24 15:47:29 +02001419#endif
Harald Weltec1986ee2005-11-13 16:06:29 -08001420
Alan Cox4cf974c2008-06-14 16:01:57 +02001421 lock_kernel();
1422 rc = -ENODEV;
Harald Weltec1986ee2005-11-13 16:06:29 -08001423 link = dev_table[iminor(inode)];
Dominik Brodowski9940ec32006-03-05 11:04:33 +01001424 if (!pcmcia_dev_present(link)) {
Harald Weltec1986ee2005-11-13 16:06:29 -08001425 DEBUGP(4, dev, "DEV_OK false\n");
Alan Cox4cf974c2008-06-14 16:01:57 +02001426 goto out;
Harald Weltec1986ee2005-11-13 16:06:29 -08001427 }
1428
1429 if (test_bit(IS_CMM_ABSENT, &dev->flags)) {
1430 DEBUGP(4, dev, "CMM_ABSENT flag set\n");
Alan Cox4cf974c2008-06-14 16:01:57 +02001431 goto out;
Harald Weltec1986ee2005-11-13 16:06:29 -08001432 }
Dominik Brodowskid93c7682008-07-15 20:11:21 +02001433 rc = -EINVAL;
Harald Weltec1986ee2005-11-13 16:06:29 -08001434
1435 if (_IOC_TYPE(cmd) != CM_IOC_MAGIC) {
1436 DEBUGP(4, dev, "ioctype mismatch\n");
Alan Cox4cf974c2008-06-14 16:01:57 +02001437 goto out;
Harald Weltec1986ee2005-11-13 16:06:29 -08001438 }
1439 if (_IOC_NR(cmd) > CM_IOC_MAXNR) {
1440 DEBUGP(4, dev, "iocnr mismatch\n");
Alan Cox4cf974c2008-06-14 16:01:57 +02001441 goto out;
Harald Weltec1986ee2005-11-13 16:06:29 -08001442 }
1443 size = _IOC_SIZE(cmd);
Alan Cox4cf974c2008-06-14 16:01:57 +02001444 rc = -EFAULT;
Harald Weltec1986ee2005-11-13 16:06:29 -08001445 DEBUGP(4, dev, "iocdir=%.4x iocr=%.4x iocw=%.4x iocsize=%d cmd=%.4x\n",
1446 _IOC_DIR(cmd), _IOC_READ, _IOC_WRITE, size, cmd);
1447
1448 if (_IOC_DIR(cmd) & _IOC_READ) {
Al Viroc4aa02e2005-12-15 09:17:55 +00001449 if (!access_ok(VERIFY_WRITE, argp, size))
Alan Cox4cf974c2008-06-14 16:01:57 +02001450 goto out;
Harald Weltec1986ee2005-11-13 16:06:29 -08001451 }
1452 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Al Viroc4aa02e2005-12-15 09:17:55 +00001453 if (!access_ok(VERIFY_READ, argp, size))
Alan Cox4cf974c2008-06-14 16:01:57 +02001454 goto out;
Harald Weltec1986ee2005-11-13 16:06:29 -08001455 }
Alan Cox4cf974c2008-06-14 16:01:57 +02001456 rc = 0;
Harald Weltec1986ee2005-11-13 16:06:29 -08001457
1458 switch (cmd) {
1459 case CM_IOCGSTATUS:
1460 DEBUGP(4, dev, " ... in CM_IOCGSTATUS\n");
1461 {
1462 int status;
1463
1464 /* clear other bits, but leave inserted & powered as
1465 * they are */
1466 status = dev->flags0 & 3;
1467 if (test_bit(IS_ATR_PRESENT, &dev->flags))
1468 status |= CM_ATR_PRESENT;
1469 if (test_bit(IS_ATR_VALID, &dev->flags))
1470 status |= CM_ATR_VALID;
1471 if (test_bit(IS_CMM_ABSENT, &dev->flags))
1472 status |= CM_NO_READER;
1473 if (test_bit(IS_BAD_CARD, &dev->flags))
1474 status |= CM_BAD_CARD;
Al Viroc4aa02e2005-12-15 09:17:55 +00001475 if (copy_to_user(argp, &status, sizeof(int)))
Alan Cox4cf974c2008-06-14 16:01:57 +02001476 rc = -EFAULT;
Harald Weltec1986ee2005-11-13 16:06:29 -08001477 }
Alan Cox4cf974c2008-06-14 16:01:57 +02001478 break;
Harald Weltec1986ee2005-11-13 16:06:29 -08001479 case CM_IOCGATR:
1480 DEBUGP(4, dev, "... in CM_IOCGATR\n");
1481 {
Al Viroc4aa02e2005-12-15 09:17:55 +00001482 struct atreq __user *atreq = argp;
Harald Weltec1986ee2005-11-13 16:06:29 -08001483 int tmp;
1484 /* allow nonblocking io and being interrupted */
1485 if (wait_event_interruptible
1486 (dev->atrq,
1487 ((filp->f_flags & O_NONBLOCK)
1488 || (test_bit(IS_ATR_PRESENT, (void *)&dev->flags)
1489 != 0)))) {
1490 if (filp->f_flags & O_NONBLOCK)
Alan Cox4cf974c2008-06-14 16:01:57 +02001491 rc = -EAGAIN;
1492 else
1493 rc = -ERESTARTSYS;
1494 break;
Harald Weltec1986ee2005-11-13 16:06:29 -08001495 }
1496
Alan Cox4cf974c2008-06-14 16:01:57 +02001497 rc = -EFAULT;
Harald Weltec1986ee2005-11-13 16:06:29 -08001498 if (test_bit(IS_ATR_VALID, &dev->flags) == 0) {
1499 tmp = -1;
1500 if (copy_to_user(&(atreq->atr_len), &tmp,
1501 sizeof(int)))
Alan Cox4cf974c2008-06-14 16:01:57 +02001502 break;
Harald Weltec1986ee2005-11-13 16:06:29 -08001503 } else {
1504 if (copy_to_user(atreq->atr, dev->atr,
1505 dev->atr_len))
Alan Cox4cf974c2008-06-14 16:01:57 +02001506 break;
Harald Weltec1986ee2005-11-13 16:06:29 -08001507
1508 tmp = dev->atr_len;
1509 if (copy_to_user(&(atreq->atr_len), &tmp, sizeof(int)))
Alan Cox4cf974c2008-06-14 16:01:57 +02001510 break;
Harald Weltec1986ee2005-11-13 16:06:29 -08001511 }
Alan Cox4cf974c2008-06-14 16:01:57 +02001512 rc = 0;
1513 break;
Harald Weltec1986ee2005-11-13 16:06:29 -08001514 }
1515 case CM_IOCARDOFF:
1516
Dominik Brodowskicbf624f2009-10-24 15:47:29 +02001517#ifdef CM4000_DEBUG
Harald Weltec1986ee2005-11-13 16:06:29 -08001518 DEBUGP(4, dev, "... in CM_IOCARDOFF\n");
1519 if (dev->flags0 & 0x01) {
1520 DEBUGP(4, dev, " Card inserted\n");
1521 } else {
1522 DEBUGP(2, dev, " No card inserted\n");
1523 }
1524 if (dev->flags0 & 0x02) {
1525 DEBUGP(4, dev, " Card powered\n");
1526 } else {
1527 DEBUGP(2, dev, " Card not powered\n");
1528 }
1529#endif
1530
1531 /* is a card inserted and powered? */
1532 if ((dev->flags0 & 0x01) && (dev->flags0 & 0x02)) {
1533
1534 /* get IO lock */
1535 if (wait_event_interruptible
1536 (dev->ioq,
1537 ((filp->f_flags & O_NONBLOCK)
1538 || (test_and_set_bit(LOCK_IO, (void *)&dev->flags)
1539 == 0)))) {
1540 if (filp->f_flags & O_NONBLOCK)
Alan Cox4cf974c2008-06-14 16:01:57 +02001541 rc = -EAGAIN;
1542 else
1543 rc = -ERESTARTSYS;
1544 break;
Harald Weltec1986ee2005-11-13 16:06:29 -08001545 }
1546 /* Set Flags0 = 0x42 */
1547 DEBUGP(4, dev, "Set Flags0=0x42 \n");
1548 xoutb(0x42, REG_FLAGS0(iobase));
1549 clear_bit(IS_ATR_PRESENT, &dev->flags);
1550 clear_bit(IS_ATR_VALID, &dev->flags);
1551 dev->mstate = M_CARDOFF;
1552 clear_bit(LOCK_IO, &dev->flags);
1553 if (wait_event_interruptible
1554 (dev->atrq,
1555 ((filp->f_flags & O_NONBLOCK)
1556 || (test_bit(IS_ATR_VALID, (void *)&dev->flags) !=
1557 0)))) {
1558 if (filp->f_flags & O_NONBLOCK)
Alan Cox4cf974c2008-06-14 16:01:57 +02001559 rc = -EAGAIN;
1560 else
1561 rc = -ERESTARTSYS;
1562 break;
Harald Weltec1986ee2005-11-13 16:06:29 -08001563 }
1564 }
1565 /* release lock */
1566 clear_bit(LOCK_IO, &dev->flags);
1567 wake_up_interruptible(&dev->ioq);
1568
Jiri Slaby69ae59d2009-06-22 18:42:10 +01001569 rc = 0;
1570 break;
Harald Weltec1986ee2005-11-13 16:06:29 -08001571 case CM_IOCSPTS:
1572 {
1573 struct ptsreq krnptsreq;
1574
Al Viroc4aa02e2005-12-15 09:17:55 +00001575 if (copy_from_user(&krnptsreq, argp,
Alan Cox4cf974c2008-06-14 16:01:57 +02001576 sizeof(struct ptsreq))) {
1577 rc = -EFAULT;
1578 break;
1579 }
Harald Weltec1986ee2005-11-13 16:06:29 -08001580
1581 rc = 0;
1582 DEBUGP(4, dev, "... in CM_IOCSPTS\n");
1583 /* wait for ATR to get valid */
1584 if (wait_event_interruptible
1585 (dev->atrq,
1586 ((filp->f_flags & O_NONBLOCK)
1587 || (test_bit(IS_ATR_PRESENT, (void *)&dev->flags)
1588 != 0)))) {
1589 if (filp->f_flags & O_NONBLOCK)
Alan Cox4cf974c2008-06-14 16:01:57 +02001590 rc = -EAGAIN;
1591 else
1592 rc = -ERESTARTSYS;
1593 break;
Harald Weltec1986ee2005-11-13 16:06:29 -08001594 }
1595 /* get IO lock */
1596 if (wait_event_interruptible
1597 (dev->ioq,
1598 ((filp->f_flags & O_NONBLOCK)
1599 || (test_and_set_bit(LOCK_IO, (void *)&dev->flags)
1600 == 0)))) {
1601 if (filp->f_flags & O_NONBLOCK)
Alan Cox4cf974c2008-06-14 16:01:57 +02001602 rc = -EAGAIN;
1603 else
1604 rc = -ERESTARTSYS;
1605 break;
Harald Weltec1986ee2005-11-13 16:06:29 -08001606 }
1607
1608 if ((rc = set_protocol(dev, &krnptsreq)) != 0) {
1609 /* auto power_on again */
1610 dev->mstate = M_FETCH_ATR;
1611 clear_bit(IS_ATR_VALID, &dev->flags);
1612 }
1613 /* release lock */
1614 clear_bit(LOCK_IO, &dev->flags);
1615 wake_up_interruptible(&dev->ioq);
1616
1617 }
Alan Cox4cf974c2008-06-14 16:01:57 +02001618 break;
Dominik Brodowskicbf624f2009-10-24 15:47:29 +02001619#ifdef CM4000_DEBUG
1620 case CM_IOSDBGLVL:
1621 rc = -ENOTTY;
Alan Cox4cf974c2008-06-14 16:01:57 +02001622 break;
Harald Weltec1986ee2005-11-13 16:06:29 -08001623#endif
1624 default:
1625 DEBUGP(4, dev, "... in default (unknown IOCTL code)\n");
Alan Cox4cf974c2008-06-14 16:01:57 +02001626 rc = -ENOTTY;
Harald Weltec1986ee2005-11-13 16:06:29 -08001627 }
Alan Cox4cf974c2008-06-14 16:01:57 +02001628out:
1629 unlock_kernel();
1630 return rc;
Harald Weltec1986ee2005-11-13 16:06:29 -08001631}
1632
1633static int cmm_open(struct inode *inode, struct file *filp)
1634{
1635 struct cm4000_dev *dev;
Dominik Brodowskifba395e2006-03-31 17:21:06 +02001636 struct pcmcia_device *link;
Daniel Ritz925796e2007-08-10 13:00:58 -07001637 int minor = iminor(inode);
Jonathan Corbet8b5332f2008-06-19 14:34:41 -06001638 int ret;
Harald Weltec1986ee2005-11-13 16:06:29 -08001639
1640 if (minor >= CM4000_MAX_DEV)
1641 return -ENODEV;
1642
Jonathan Corbet8b5332f2008-06-19 14:34:41 -06001643 lock_kernel();
Harald Weltec1986ee2005-11-13 16:06:29 -08001644 link = dev_table[minor];
Jonathan Corbet8b5332f2008-06-19 14:34:41 -06001645 if (link == NULL || !pcmcia_dev_present(link)) {
1646 ret = -ENODEV;
1647 goto out;
1648 }
Harald Weltec1986ee2005-11-13 16:06:29 -08001649
Jonathan Corbet8b5332f2008-06-19 14:34:41 -06001650 if (link->open) {
1651 ret = -EBUSY;
1652 goto out;
1653 }
Harald Weltec1986ee2005-11-13 16:06:29 -08001654
1655 dev = link->priv;
1656 filp->private_data = dev;
1657
1658 DEBUGP(2, dev, "-> cmm_open(device=%d.%d process=%s,%d)\n",
1659 imajor(inode), minor, current->comm, current->pid);
1660
1661 /* init device variables, they may be "polluted" after close
1662 * or, the device may never have been closed (i.e. open failed)
1663 */
1664
1665 ZERO_DEV(dev);
1666
1667 /* opening will always block since the
1668 * monitor will be started by open, which
1669 * means we have to wait for ATR becoming
1670 * vaild = block until valid (or card
1671 * inserted)
1672 */
Jonathan Corbet8b5332f2008-06-19 14:34:41 -06001673 if (filp->f_flags & O_NONBLOCK) {
1674 ret = -EAGAIN;
1675 goto out;
1676 }
Harald Weltec1986ee2005-11-13 16:06:29 -08001677
1678 dev->mdelay = T_50MSEC;
1679
1680 /* start monitoring the cardstatus */
1681 start_monitor(dev);
1682
1683 link->open = 1; /* only one open per device */
Harald Weltec1986ee2005-11-13 16:06:29 -08001684
1685 DEBUGP(2, dev, "<- cmm_open\n");
Jonathan Corbet8b5332f2008-06-19 14:34:41 -06001686 ret = nonseekable_open(inode, filp);
1687out:
1688 unlock_kernel();
1689 return ret;
Harald Weltec1986ee2005-11-13 16:06:29 -08001690}
1691
1692static int cmm_close(struct inode *inode, struct file *filp)
1693{
1694 struct cm4000_dev *dev;
Dominik Brodowskifba395e2006-03-31 17:21:06 +02001695 struct pcmcia_device *link;
Harald Weltec1986ee2005-11-13 16:06:29 -08001696 int minor = iminor(inode);
1697
1698 if (minor >= CM4000_MAX_DEV)
1699 return -ENODEV;
1700
1701 link = dev_table[minor];
1702 if (link == NULL)
1703 return -ENODEV;
1704
1705 dev = link->priv;
1706
1707 DEBUGP(2, dev, "-> cmm_close(maj/min=%d.%d)\n",
1708 imajor(inode), minor);
1709
1710 stop_monitor(dev);
1711
1712 ZERO_DEV(dev);
1713
1714 link->open = 0; /* only one open per device */
1715 wake_up(&dev->devq); /* socket removed? */
1716
1717 DEBUGP(2, dev, "cmm_close\n");
1718 return 0;
1719}
1720
Dominik Brodowskifba395e2006-03-31 17:21:06 +02001721static void cmm_cm4000_release(struct pcmcia_device * link)
Harald Weltec1986ee2005-11-13 16:06:29 -08001722{
1723 struct cm4000_dev *dev = link->priv;
1724
1725 /* dont terminate the monitor, rather rely on
1726 * close doing that for us.
1727 */
1728 DEBUGP(3, dev, "-> cmm_cm4000_release\n");
1729 while (link->open) {
1730 printk(KERN_INFO MODULE_NAME ": delaying release until "
1731 "process has terminated\n");
1732 /* note: don't interrupt us:
1733 * close the applications which own
1734 * the devices _first_ !
1735 */
1736 wait_event(dev->devq, (link->open == 0));
1737 }
1738 /* dev->devq=NULL; this cannot be zeroed earlier */
1739 DEBUGP(3, dev, "<- cmm_cm4000_release\n");
1740 return;
1741}
1742
1743/*==== Interface to PCMCIA Layer =======================================*/
1744
Dominik Brodowski84e2d342008-07-29 08:38:55 +02001745static int cm4000_config_check(struct pcmcia_device *p_dev,
1746 cistpl_cftable_entry_t *cfg,
Dominik Brodowski8e2fc392008-08-02 15:30:31 +02001747 cistpl_cftable_entry_t *dflt,
Dominik Brodowskiad913c12008-08-02 16:12:00 +02001748 unsigned int vcc,
Dominik Brodowski84e2d342008-07-29 08:38:55 +02001749 void *priv_data)
1750{
Dominik Brodowski84e2d342008-07-29 08:38:55 +02001751 if (!cfg->io.nwin)
1752 return -ENODEV;
1753
Dominik Brodowski90abdc32010-07-24 17:23:51 +02001754 p_dev->resource[0]->start = cfg->io.win[0].base;
1755 p_dev->resource[0]->end = cfg->io.win[0].len;
1756 p_dev->resource[0]->flags |= pcmcia_io_cfg_data_width(cfg->io.flags);
1757 p_dev->io_lines = cfg->io.flags & CISTPL_IO_LINES_MASK;
Dominik Brodowski84e2d342008-07-29 08:38:55 +02001758
Dominik Brodowski90abdc32010-07-24 17:23:51 +02001759 return pcmcia_request_io(p_dev);
Dominik Brodowski84e2d342008-07-29 08:38:55 +02001760}
1761
Dominik Brodowski15b99ac2006-03-31 17:26:06 +02001762static int cm4000_config(struct pcmcia_device * link, int devno)
Harald Weltec1986ee2005-11-13 16:06:29 -08001763{
Harald Weltec1986ee2005-11-13 16:06:29 -08001764 struct cm4000_dev *dev;
Harald Weltec1986ee2005-11-13 16:06:29 -08001765
1766 /* read the config-tuples */
Dominik Brodowski84e2d342008-07-29 08:38:55 +02001767 if (pcmcia_loop_config(link, cm4000_config_check, NULL))
Harald Weltec1986ee2005-11-13 16:06:29 -08001768 goto cs_release;
1769
Dominik Brodowski84e2d342008-07-29 08:38:55 +02001770 if (pcmcia_request_configuration(link, &link->conf))
Harald Weltec1986ee2005-11-13 16:06:29 -08001771 goto cs_release;
Harald Weltec1986ee2005-11-13 16:06:29 -08001772
1773 dev = link->priv;
Harald Weltec1986ee2005-11-13 16:06:29 -08001774
Dominik Brodowski15b99ac2006-03-31 17:26:06 +02001775 return 0;
Harald Weltec1986ee2005-11-13 16:06:29 -08001776
Harald Weltec1986ee2005-11-13 16:06:29 -08001777cs_release:
1778 cm4000_release(link);
Dominik Brodowski15b99ac2006-03-31 17:26:06 +02001779 return -ENODEV;
Harald Weltec1986ee2005-11-13 16:06:29 -08001780}
1781
Dominik Brodowskifba395e2006-03-31 17:21:06 +02001782static int cm4000_suspend(struct pcmcia_device *link)
Dominik Brodowski98e4c282005-11-14 21:21:18 +01001783{
Dominik Brodowski98e4c282005-11-14 21:21:18 +01001784 struct cm4000_dev *dev;
1785
1786 dev = link->priv;
Dominik Brodowski98e4c282005-11-14 21:21:18 +01001787 stop_monitor(dev);
1788
1789 return 0;
1790}
1791
Dominik Brodowskifba395e2006-03-31 17:21:06 +02001792static int cm4000_resume(struct pcmcia_device *link)
Dominik Brodowski98e4c282005-11-14 21:21:18 +01001793{
Dominik Brodowski98e4c282005-11-14 21:21:18 +01001794 struct cm4000_dev *dev;
1795
1796 dev = link->priv;
Dominik Brodowski98e4c282005-11-14 21:21:18 +01001797 if (link->open)
1798 start_monitor(dev);
1799
1800 return 0;
1801}
1802
Dominik Brodowskifba395e2006-03-31 17:21:06 +02001803static void cm4000_release(struct pcmcia_device *link)
Harald Weltec1986ee2005-11-13 16:06:29 -08001804{
Daniel Ritz925796e2007-08-10 13:00:58 -07001805 cmm_cm4000_release(link); /* delay release until device closed */
Dominik Brodowskifba395e2006-03-31 17:21:06 +02001806 pcmcia_disable_device(link);
Harald Weltec1986ee2005-11-13 16:06:29 -08001807}
1808
Dominik Brodowski15b99ac2006-03-31 17:26:06 +02001809static int cm4000_probe(struct pcmcia_device *link)
Harald Weltec1986ee2005-11-13 16:06:29 -08001810{
1811 struct cm4000_dev *dev;
Dominik Brodowski15b99ac2006-03-31 17:26:06 +02001812 int i, ret;
Harald Weltec1986ee2005-11-13 16:06:29 -08001813
1814 for (i = 0; i < CM4000_MAX_DEV; i++)
1815 if (dev_table[i] == NULL)
1816 break;
1817
1818 if (i == CM4000_MAX_DEV) {
1819 printk(KERN_NOTICE MODULE_NAME ": all devices in use\n");
Dominik Brodowskif8cfa612005-11-14 21:25:51 +01001820 return -ENODEV;
Harald Weltec1986ee2005-11-13 16:06:29 -08001821 }
1822
1823 /* create a new cm4000_cs device */
1824 dev = kzalloc(sizeof(struct cm4000_dev), GFP_KERNEL);
1825 if (dev == NULL)
Dominik Brodowskif8cfa612005-11-14 21:25:51 +01001826 return -ENOMEM;
Harald Weltec1986ee2005-11-13 16:06:29 -08001827
Dominik Brodowskifba395e2006-03-31 17:21:06 +02001828 dev->p_dev = link;
Harald Weltec1986ee2005-11-13 16:06:29 -08001829 link->priv = dev;
Harald Weltec1986ee2005-11-13 16:06:29 -08001830 dev_table[i] = link;
1831
Harald Weltec1986ee2005-11-13 16:06:29 -08001832 init_waitqueue_head(&dev->devq);
1833 init_waitqueue_head(&dev->ioq);
1834 init_waitqueue_head(&dev->atrq);
1835 init_waitqueue_head(&dev->readq);
1836
Dominik Brodowski15b99ac2006-03-31 17:26:06 +02001837 ret = cm4000_config(link, i);
Akinobu Mita54493c12007-05-09 02:33:31 -07001838 if (ret) {
1839 dev_table[i] = NULL;
1840 kfree(dev);
Dominik Brodowski15b99ac2006-03-31 17:26:06 +02001841 return ret;
Akinobu Mita54493c12007-05-09 02:33:31 -07001842 }
Dominik Brodowskif8cfa612005-11-14 21:25:51 +01001843
Greg Kroah-Hartman03457cd2008-07-21 20:03:34 -07001844 device_create(cmm_class, NULL, MKDEV(major, i), NULL, "cmm%d", i);
Harald Welte67bc6202006-02-14 09:21:26 +01001845
Dominik Brodowskif8cfa612005-11-14 21:25:51 +01001846 return 0;
Harald Weltec1986ee2005-11-13 16:06:29 -08001847}
1848
Dominik Brodowskifba395e2006-03-31 17:21:06 +02001849static void cm4000_detach(struct pcmcia_device *link)
Harald Weltec1986ee2005-11-13 16:06:29 -08001850{
1851 struct cm4000_dev *dev = link->priv;
Dominik Brodowskicc3b4862005-11-14 21:23:14 +01001852 int devno;
Harald Weltec1986ee2005-11-13 16:06:29 -08001853
1854 /* find device */
Dominik Brodowskicc3b4862005-11-14 21:23:14 +01001855 for (devno = 0; devno < CM4000_MAX_DEV; devno++)
1856 if (dev_table[devno] == link)
Harald Weltec1986ee2005-11-13 16:06:29 -08001857 break;
Dominik Brodowskicc3b4862005-11-14 21:23:14 +01001858 if (devno == CM4000_MAX_DEV)
Harald Weltec1986ee2005-11-13 16:06:29 -08001859 return;
1860
Dominik Brodowskicc3b4862005-11-14 21:23:14 +01001861 stop_monitor(dev);
1862
Dominik Brodowskie2d40962006-03-02 00:09:29 +01001863 cm4000_release(link);
Dominik Brodowskicc3b4862005-11-14 21:23:14 +01001864
1865 dev_table[devno] = NULL;
Akinobu Mita54493c12007-05-09 02:33:31 -07001866 kfree(dev);
Dominik Brodowskicc3b4862005-11-14 21:23:14 +01001867
tonyj@suse.de07c015e2007-08-07 22:28:44 -07001868 device_destroy(cmm_class, MKDEV(major, devno));
Harald Welte67bc6202006-02-14 09:21:26 +01001869
Harald Weltec1986ee2005-11-13 16:06:29 -08001870 return;
1871}
1872
Arjan van de Ven62322d22006-07-03 00:24:21 -07001873static const struct file_operations cm4000_fops = {
Harald Weltec1986ee2005-11-13 16:06:29 -08001874 .owner = THIS_MODULE,
1875 .read = cmm_read,
1876 .write = cmm_write,
Alan Cox4cf974c2008-06-14 16:01:57 +02001877 .unlocked_ioctl = cmm_ioctl,
Harald Weltec1986ee2005-11-13 16:06:29 -08001878 .open = cmm_open,
1879 .release= cmm_close,
1880};
1881
1882static struct pcmcia_device_id cm4000_ids[] = {
1883 PCMCIA_DEVICE_MANF_CARD(0x0223, 0x0002),
1884 PCMCIA_DEVICE_PROD_ID12("CardMan", "4000", 0x2FB368CA, 0xA2BD8C39),
1885 PCMCIA_DEVICE_NULL,
1886};
1887MODULE_DEVICE_TABLE(pcmcia, cm4000_ids);
1888
1889static struct pcmcia_driver cm4000_driver = {
1890 .owner = THIS_MODULE,
1891 .drv = {
1892 .name = "cm4000_cs",
1893 },
Dominik Brodowski15b99ac2006-03-31 17:26:06 +02001894 .probe = cm4000_probe,
Dominik Brodowskicc3b4862005-11-14 21:23:14 +01001895 .remove = cm4000_detach,
Dominik Brodowski98e4c282005-11-14 21:21:18 +01001896 .suspend = cm4000_suspend,
1897 .resume = cm4000_resume,
Harald Weltec1986ee2005-11-13 16:06:29 -08001898 .id_table = cm4000_ids,
1899};
1900
1901static int __init cmm_init(void)
1902{
Harald Welte67bc6202006-02-14 09:21:26 +01001903 int rc;
1904
Harald Weltec1986ee2005-11-13 16:06:29 -08001905 printk(KERN_INFO "%s\n", version);
Harald Welte67bc6202006-02-14 09:21:26 +01001906
1907 cmm_class = class_create(THIS_MODULE, "cardman_4000");
Akinobu Mita5eb5fc92006-10-17 15:25:59 +09001908 if (IS_ERR(cmm_class))
1909 return PTR_ERR(cmm_class);
Harald Welte67bc6202006-02-14 09:21:26 +01001910
Harald Weltec1986ee2005-11-13 16:06:29 -08001911 major = register_chrdev(0, DEVICE_NAME, &cm4000_fops);
1912 if (major < 0) {
1913 printk(KERN_WARNING MODULE_NAME
1914 ": could not get major number\n");
Akinobu Mita54493c12007-05-09 02:33:31 -07001915 class_destroy(cmm_class);
Akinobu Mita5eb5fc92006-10-17 15:25:59 +09001916 return major;
Harald Weltec1986ee2005-11-13 16:06:29 -08001917 }
1918
Harald Welte7fc5b1e2006-05-10 13:28:52 +02001919 rc = pcmcia_register_driver(&cm4000_driver);
1920 if (rc < 0) {
1921 unregister_chrdev(major, DEVICE_NAME);
Akinobu Mita54493c12007-05-09 02:33:31 -07001922 class_destroy(cmm_class);
Harald Welte7fc5b1e2006-05-10 13:28:52 +02001923 return rc;
1924 }
1925
Harald Weltec1986ee2005-11-13 16:06:29 -08001926 return 0;
1927}
1928
1929static void __exit cmm_exit(void)
1930{
Harald Weltec1986ee2005-11-13 16:06:29 -08001931 printk(KERN_INFO MODULE_NAME ": unloading\n");
1932 pcmcia_unregister_driver(&cm4000_driver);
Harald Weltec1986ee2005-11-13 16:06:29 -08001933 unregister_chrdev(major, DEVICE_NAME);
Harald Welte67bc6202006-02-14 09:21:26 +01001934 class_destroy(cmm_class);
Harald Weltec1986ee2005-11-13 16:06:29 -08001935};
1936
1937module_init(cmm_init);
1938module_exit(cmm_exit);
1939MODULE_LICENSE("Dual BSD/GPL");