Commit 20fffee818ec43b64f58ab25c42705b7dcae16e5

Authored by Nicolas Kaiser
Committed by Dominik Brodowski
1 parent 006839f12e

pcmcia/cm4000: fix error code

I'm assuming it's not intended to instantly change the error code
from -ENODEV to -EIO, is it?

Signed-off-by: Nicolas Kaiser <nikai@nikai.net>
Acked-by: Harald Welte <laforge@gnumonks.org>
Signed-off-by: Dominik Brodowski <linux@dominikbrodowski.net>

Showing 1 changed file with 2 additions and 1 deletions Inline Diff

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