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