blob: a6a70e476beaf4babb7446643775d92ed5866635 [file] [log] [blame]
Harald Welte77c44ab2005-11-13 16:06:26 -08001/*
2 * A driver for the Omnikey PCMCIA smartcard reader CardMan 4040
3 *
4 * (c) 2000-2004 Omnikey AG (http://www.omnikey.com/)
5 *
Harald Welte67bc6202006-02-14 09:21:26 +01006 * (C) 2005-2006 Harald Welte <laforge@gnumonks.org>
Harald Welte77c44ab2005-11-13 16:06:26 -08007 * - add support for poll()
8 * - driver cleanup
9 * - add waitqueues
10 * - adhere to linux kernel coding style and policies
11 * - support 2.6.13 "new style" pcmcia interface
Harald Welte67bc6202006-02-14 09:21:26 +010012 * - add class interface for udev device creation
Harald Welte77c44ab2005-11-13 16:06:26 -080013 *
14 * The device basically is a USB CCID compliant device that has been
15 * attached to an I/O-Mapped FIFO.
16 *
17 * All rights reserved, Dual BSD/GPL Licensed.
18 */
19
Harald Welte77c44ab2005-11-13 16:06:26 -080020#include <linux/kernel.h>
21#include <linux/module.h>
22#include <linux/slab.h>
23#include <linux/init.h>
24#include <linux/fs.h>
25#include <linux/delay.h>
26#include <linux/poll.h>
Jonathan Corbet8b5332f2008-06-19 14:34:41 -060027#include <linux/smp_lock.h>
Harald Welte77c44ab2005-11-13 16:06:26 -080028#include <linux/wait.h>
29#include <asm/uaccess.h>
30#include <asm/io.h>
31
32#include <pcmcia/cs_types.h>
33#include <pcmcia/cs.h>
34#include <pcmcia/cistpl.h>
35#include <pcmcia/cisreg.h>
36#include <pcmcia/ciscode.h>
37#include <pcmcia/ds.h>
38
39#include "cm4040_cs.h"
40
41
Dominik Brodowskidd2e5a12009-11-03 10:27:34 +010042#define reader_to_dev(x) (&x->p_dev->dev)
Dominik Brodowskicbf624f2009-10-24 15:47:29 +020043
44/* n (debug level) is ignored */
45/* additional debug output may be enabled by re-compiling with
46 * CM4040_DEBUG set */
47/* #define CM4040_DEBUG */
48#define DEBUGP(n, rdr, x, args...) do { \
49 dev_dbg(reader_to_dev(rdr), "%s:" x, \
50 __func__ , ## args); \
Harald Welte77c44ab2005-11-13 16:06:26 -080051 } while (0)
Harald Welte77c44ab2005-11-13 16:06:26 -080052
53static char *version =
Harald Welte67bc6202006-02-14 09:21:26 +010054"OMNIKEY CardMan 4040 v1.1.0gm5 - All bugs added by Harald Welte";
Harald Welte77c44ab2005-11-13 16:06:26 -080055
56#define CCID_DRIVER_BULK_DEFAULT_TIMEOUT (150*HZ)
57#define CCID_DRIVER_ASYNC_POWERUP_TIMEOUT (35*HZ)
58#define CCID_DRIVER_MINIMUM_TIMEOUT (3*HZ)
59#define READ_WRITE_BUFFER_SIZE 512
60#define POLL_LOOP_COUNT 1000
61
62/* how often to poll for fifo status change */
63#define POLL_PERIOD msecs_to_jiffies(10)
64
Dominik Brodowskifba395e2006-03-31 17:21:06 +020065static void reader_release(struct pcmcia_device *link);
Harald Welte77c44ab2005-11-13 16:06:26 -080066
67static int major;
Harald Welte67bc6202006-02-14 09:21:26 +010068static struct class *cmx_class;
Harald Welte77c44ab2005-11-13 16:06:26 -080069
70#define BS_READABLE 0x01
71#define BS_WRITABLE 0x02
72
73struct reader_dev {
Dominik Brodowskifd238232006-03-05 10:45:09 +010074 struct pcmcia_device *p_dev;
Harald Welte77c44ab2005-11-13 16:06:26 -080075 dev_node_t node;
76 wait_queue_head_t devq;
77 wait_queue_head_t poll_wait;
78 wait_queue_head_t read_wait;
79 wait_queue_head_t write_wait;
80 unsigned long buffer_status;
81 unsigned long timeout;
82 unsigned char s_buf[READ_WRITE_BUFFER_SIZE];
83 unsigned char r_buf[READ_WRITE_BUFFER_SIZE];
84 struct timer_list poll_timer;
85};
86
Dominik Brodowskifba395e2006-03-31 17:21:06 +020087static struct pcmcia_device *dev_table[CM_MAX_DEV];
Harald Welte77c44ab2005-11-13 16:06:26 -080088
Dominik Brodowskicbf624f2009-10-24 15:47:29 +020089#ifndef CM4040_DEBUG
Harald Welte77c44ab2005-11-13 16:06:26 -080090#define xoutb outb
91#define xinb inb
92#else
93static inline void xoutb(unsigned char val, unsigned short port)
94{
Dominik Brodowskicbf624f2009-10-24 15:47:29 +020095 pr_debug("outb(val=%.2x,port=%.4x)\n", val, port);
Harald Welte77c44ab2005-11-13 16:06:26 -080096 outb(val, port);
97}
98
99static inline unsigned char xinb(unsigned short port)
100{
101 unsigned char val;
102
103 val = inb(port);
Dominik Brodowskicbf624f2009-10-24 15:47:29 +0200104 pr_debug("%.2x=inb(%.4x)\n", val, port);
Harald Welte77c44ab2005-11-13 16:06:26 -0800105 return val;
106}
107#endif
108
109/* poll the device fifo status register. not to be confused with
110 * the poll syscall. */
111static void cm4040_do_poll(unsigned long dummy)
112{
113 struct reader_dev *dev = (struct reader_dev *) dummy;
Dominik Brodowskifd238232006-03-05 10:45:09 +0100114 unsigned int obs = xinb(dev->p_dev->io.BasePort1
Harald Welte77c44ab2005-11-13 16:06:26 -0800115 + REG_OFFSET_BUFFER_STATUS);
116
117 if ((obs & BSR_BULK_IN_FULL)) {
118 set_bit(BS_READABLE, &dev->buffer_status);
119 DEBUGP(4, dev, "waking up read_wait\n");
120 wake_up_interruptible(&dev->read_wait);
121 } else
122 clear_bit(BS_READABLE, &dev->buffer_status);
123
124 if (!(obs & BSR_BULK_OUT_FULL)) {
125 set_bit(BS_WRITABLE, &dev->buffer_status);
126 DEBUGP(4, dev, "waking up write_wait\n");
127 wake_up_interruptible(&dev->write_wait);
128 } else
129 clear_bit(BS_WRITABLE, &dev->buffer_status);
130
131 if (dev->buffer_status)
132 wake_up_interruptible(&dev->poll_wait);
133
134 mod_timer(&dev->poll_timer, jiffies + POLL_PERIOD);
135}
136
137static void cm4040_stop_poll(struct reader_dev *dev)
138{
139 del_timer_sync(&dev->poll_timer);
140}
141
142static int wait_for_bulk_out_ready(struct reader_dev *dev)
143{
144 int i, rc;
Dominik Brodowskifd238232006-03-05 10:45:09 +0100145 int iobase = dev->p_dev->io.BasePort1;
Harald Welte77c44ab2005-11-13 16:06:26 -0800146
147 for (i = 0; i < POLL_LOOP_COUNT; i++) {
148 if ((xinb(iobase + REG_OFFSET_BUFFER_STATUS)
149 & BSR_BULK_OUT_FULL) == 0) {
150 DEBUGP(4, dev, "BulkOut empty (i=%d)\n", i);
151 return 1;
152 }
153 }
154
155 DEBUGP(4, dev, "wait_event_interruptible_timeout(timeout=%ld\n",
156 dev->timeout);
157 rc = wait_event_interruptible_timeout(dev->write_wait,
158 test_and_clear_bit(BS_WRITABLE,
159 &dev->buffer_status),
160 dev->timeout);
161
162 if (rc > 0)
163 DEBUGP(4, dev, "woke up: BulkOut empty\n");
164 else if (rc == 0)
165 DEBUGP(4, dev, "woke up: BulkOut full, returning 0 :(\n");
166 else if (rc < 0)
167 DEBUGP(4, dev, "woke up: signal arrived\n");
168
169 return rc;
170}
171
172/* Write to Sync Control Register */
173static int write_sync_reg(unsigned char val, struct reader_dev *dev)
174{
Dominik Brodowskifd238232006-03-05 10:45:09 +0100175 int iobase = dev->p_dev->io.BasePort1;
Harald Welte77c44ab2005-11-13 16:06:26 -0800176 int rc;
177
178 rc = wait_for_bulk_out_ready(dev);
179 if (rc <= 0)
180 return rc;
181
182 xoutb(val, iobase + REG_OFFSET_SYNC_CONTROL);
183 rc = wait_for_bulk_out_ready(dev);
184 if (rc <= 0)
185 return rc;
186
187 return 1;
188}
189
190static int wait_for_bulk_in_ready(struct reader_dev *dev)
191{
192 int i, rc;
Dominik Brodowskifd238232006-03-05 10:45:09 +0100193 int iobase = dev->p_dev->io.BasePort1;
Harald Welte77c44ab2005-11-13 16:06:26 -0800194
195 for (i = 0; i < POLL_LOOP_COUNT; i++) {
196 if ((xinb(iobase + REG_OFFSET_BUFFER_STATUS)
197 & BSR_BULK_IN_FULL) == BSR_BULK_IN_FULL) {
198 DEBUGP(3, dev, "BulkIn full (i=%d)\n", i);
199 return 1;
200 }
201 }
202
203 DEBUGP(4, dev, "wait_event_interruptible_timeout(timeout=%ld\n",
204 dev->timeout);
205 rc = wait_event_interruptible_timeout(dev->read_wait,
206 test_and_clear_bit(BS_READABLE,
207 &dev->buffer_status),
208 dev->timeout);
209 if (rc > 0)
210 DEBUGP(4, dev, "woke up: BulkIn full\n");
211 else if (rc == 0)
212 DEBUGP(4, dev, "woke up: BulkIn not full, returning 0 :(\n");
213 else if (rc < 0)
214 DEBUGP(4, dev, "woke up: signal arrived\n");
215
216 return rc;
217}
218
219static ssize_t cm4040_read(struct file *filp, char __user *buf,
220 size_t count, loff_t *ppos)
221{
222 struct reader_dev *dev = filp->private_data;
Dominik Brodowskifd238232006-03-05 10:45:09 +0100223 int iobase = dev->p_dev->io.BasePort1;
Harald Welte77c44ab2005-11-13 16:06:26 -0800224 size_t bytes_to_read;
225 unsigned long i;
226 size_t min_bytes_to_read;
227 int rc;
228 unsigned char uc;
229
230 DEBUGP(2, dev, "-> cm4040_read(%s,%d)\n", current->comm, current->pid);
231
232 if (count == 0)
233 return 0;
234
235 if (count < 10)
236 return -EFAULT;
237
238 if (filp->f_flags & O_NONBLOCK) {
239 DEBUGP(4, dev, "filep->f_flags O_NONBLOCK set\n");
240 DEBUGP(2, dev, "<- cm4040_read (failure)\n");
241 return -EAGAIN;
242 }
243
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100244 if (!pcmcia_dev_present(dev->p_dev))
Harald Welte77c44ab2005-11-13 16:06:26 -0800245 return -ENODEV;
246
247 for (i = 0; i < 5; i++) {
248 rc = wait_for_bulk_in_ready(dev);
249 if (rc <= 0) {
250 DEBUGP(5, dev, "wait_for_bulk_in_ready rc=%.2x\n", rc);
251 DEBUGP(2, dev, "<- cm4040_read (failed)\n");
252 if (rc == -ERESTARTSYS)
253 return rc;
254 return -EIO;
255 }
256 dev->r_buf[i] = xinb(iobase + REG_OFFSET_BULK_IN);
Dominik Brodowskicbf624f2009-10-24 15:47:29 +0200257#ifdef CM4040_DEBUG
258 pr_debug("%lu:%2x ", i, dev->r_buf[i]);
Harald Welte77c44ab2005-11-13 16:06:26 -0800259 }
Dominik Brodowskicbf624f2009-10-24 15:47:29 +0200260 pr_debug("\n");
Harald Welte77c44ab2005-11-13 16:06:26 -0800261#else
262 }
263#endif
264
265 bytes_to_read = 5 + le32_to_cpu(*(__le32 *)&dev->r_buf[1]);
266
Randy Dunlape657ea12009-11-11 09:31:07 -0800267 DEBUGP(6, dev, "BytesToRead=%zu\n", bytes_to_read);
Harald Welte77c44ab2005-11-13 16:06:26 -0800268
269 min_bytes_to_read = min(count, bytes_to_read + 5);
Marcel Holtmann059819a2007-03-06 22:12:00 +0100270 min_bytes_to_read = min_t(size_t, min_bytes_to_read, READ_WRITE_BUFFER_SIZE);
Harald Welte77c44ab2005-11-13 16:06:26 -0800271
Randy Dunlape657ea12009-11-11 09:31:07 -0800272 DEBUGP(6, dev, "Min=%zu\n", min_bytes_to_read);
Harald Welte77c44ab2005-11-13 16:06:26 -0800273
274 for (i = 0; i < (min_bytes_to_read-5); i++) {
275 rc = wait_for_bulk_in_ready(dev);
276 if (rc <= 0) {
277 DEBUGP(5, dev, "wait_for_bulk_in_ready rc=%.2x\n", rc);
278 DEBUGP(2, dev, "<- cm4040_read (failed)\n");
279 if (rc == -ERESTARTSYS)
280 return rc;
281 return -EIO;
282 }
283 dev->r_buf[i+5] = xinb(iobase + REG_OFFSET_BULK_IN);
Dominik Brodowskicbf624f2009-10-24 15:47:29 +0200284#ifdef CM4040_DEBUG
285 pr_debug("%lu:%2x ", i, dev->r_buf[i]);
Harald Welte77c44ab2005-11-13 16:06:26 -0800286 }
Dominik Brodowskicbf624f2009-10-24 15:47:29 +0200287 pr_debug("\n");
Harald Welte77c44ab2005-11-13 16:06:26 -0800288#else
289 }
290#endif
291
292 *ppos = min_bytes_to_read;
293 if (copy_to_user(buf, dev->r_buf, min_bytes_to_read))
294 return -EFAULT;
295
296 rc = wait_for_bulk_in_ready(dev);
297 if (rc <= 0) {
298 DEBUGP(5, dev, "wait_for_bulk_in_ready rc=%.2x\n", rc);
299 DEBUGP(2, dev, "<- cm4040_read (failed)\n");
300 if (rc == -ERESTARTSYS)
301 return rc;
302 return -EIO;
303 }
304
305 rc = write_sync_reg(SCR_READER_TO_HOST_DONE, dev);
306 if (rc <= 0) {
307 DEBUGP(5, dev, "write_sync_reg c=%.2x\n", rc);
308 DEBUGP(2, dev, "<- cm4040_read (failed)\n");
309 if (rc == -ERESTARTSYS)
310 return rc;
311 else
312 return -EIO;
313 }
314
315 uc = xinb(iobase + REG_OFFSET_BULK_IN);
316
317 DEBUGP(2, dev, "<- cm4040_read (successfully)\n");
318 return min_bytes_to_read;
319}
320
321static ssize_t cm4040_write(struct file *filp, const char __user *buf,
322 size_t count, loff_t *ppos)
323{
324 struct reader_dev *dev = filp->private_data;
Dominik Brodowskifd238232006-03-05 10:45:09 +0100325 int iobase = dev->p_dev->io.BasePort1;
Harald Welte77c44ab2005-11-13 16:06:26 -0800326 ssize_t rc;
327 int i;
328 unsigned int bytes_to_write;
329
330 DEBUGP(2, dev, "-> cm4040_write(%s,%d)\n", current->comm, current->pid);
331
332 if (count == 0) {
333 DEBUGP(2, dev, "<- cm4040_write empty read (successfully)\n");
334 return 0;
335 }
336
Marcel Holtmann059819a2007-03-06 22:12:00 +0100337 if ((count < 5) || (count > READ_WRITE_BUFFER_SIZE)) {
Harald Welte77c44ab2005-11-13 16:06:26 -0800338 DEBUGP(2, dev, "<- cm4040_write buffersize=%Zd < 5\n", count);
339 return -EIO;
340 }
341
342 if (filp->f_flags & O_NONBLOCK) {
343 DEBUGP(4, dev, "filep->f_flags O_NONBLOCK set\n");
344 DEBUGP(4, dev, "<- cm4040_write (failure)\n");
345 return -EAGAIN;
346 }
347
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100348 if (!pcmcia_dev_present(dev->p_dev))
Harald Welte77c44ab2005-11-13 16:06:26 -0800349 return -ENODEV;
350
351 bytes_to_write = count;
352 if (copy_from_user(dev->s_buf, buf, bytes_to_write))
353 return -EFAULT;
354
355 switch (dev->s_buf[0]) {
356 case CMD_PC_TO_RDR_XFRBLOCK:
357 case CMD_PC_TO_RDR_SECURE:
358 case CMD_PC_TO_RDR_TEST_SECURE:
359 case CMD_PC_TO_RDR_OK_SECURE:
360 dev->timeout = CCID_DRIVER_BULK_DEFAULT_TIMEOUT;
361 break;
362
363 case CMD_PC_TO_RDR_ICCPOWERON:
364 dev->timeout = CCID_DRIVER_ASYNC_POWERUP_TIMEOUT;
365 break;
366
367 case CMD_PC_TO_RDR_GETSLOTSTATUS:
368 case CMD_PC_TO_RDR_ICCPOWEROFF:
369 case CMD_PC_TO_RDR_GETPARAMETERS:
370 case CMD_PC_TO_RDR_RESETPARAMETERS:
371 case CMD_PC_TO_RDR_SETPARAMETERS:
372 case CMD_PC_TO_RDR_ESCAPE:
373 case CMD_PC_TO_RDR_ICCCLOCK:
374 default:
375 dev->timeout = CCID_DRIVER_MINIMUM_TIMEOUT;
376 break;
377 }
378
379 rc = write_sync_reg(SCR_HOST_TO_READER_START, dev);
380 if (rc <= 0) {
381 DEBUGP(5, dev, "write_sync_reg c=%.2Zx\n", rc);
382 DEBUGP(2, dev, "<- cm4040_write (failed)\n");
383 if (rc == -ERESTARTSYS)
384 return rc;
385 else
386 return -EIO;
387 }
388
389 DEBUGP(4, dev, "start \n");
390
391 for (i = 0; i < bytes_to_write; i++) {
392 rc = wait_for_bulk_out_ready(dev);
393 if (rc <= 0) {
394 DEBUGP(5, dev, "wait_for_bulk_out_ready rc=%.2Zx\n",
395 rc);
396 DEBUGP(2, dev, "<- cm4040_write (failed)\n");
397 if (rc == -ERESTARTSYS)
398 return rc;
399 else
400 return -EIO;
401 }
402
403 xoutb(dev->s_buf[i],iobase + REG_OFFSET_BULK_OUT);
404 }
405 DEBUGP(4, dev, "end\n");
406
407 rc = write_sync_reg(SCR_HOST_TO_READER_DONE, dev);
408
409 if (rc <= 0) {
410 DEBUGP(5, dev, "write_sync_reg c=%.2Zx\n", rc);
411 DEBUGP(2, dev, "<- cm4040_write (failed)\n");
412 if (rc == -ERESTARTSYS)
413 return rc;
414 else
415 return -EIO;
416 }
417
418 DEBUGP(2, dev, "<- cm4040_write (successfully)\n");
419 return count;
420}
421
422static unsigned int cm4040_poll(struct file *filp, poll_table *wait)
423{
424 struct reader_dev *dev = filp->private_data;
425 unsigned int mask = 0;
426
427 poll_wait(filp, &dev->poll_wait, wait);
428
429 if (test_and_clear_bit(BS_READABLE, &dev->buffer_status))
430 mask |= POLLIN | POLLRDNORM;
431 if (test_and_clear_bit(BS_WRITABLE, &dev->buffer_status))
432 mask |= POLLOUT | POLLWRNORM;
433
434 DEBUGP(2, dev, "<- cm4040_poll(%u)\n", mask);
435
436 return mask;
437}
438
439static int cm4040_open(struct inode *inode, struct file *filp)
440{
441 struct reader_dev *dev;
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200442 struct pcmcia_device *link;
Harald Welte77c44ab2005-11-13 16:06:26 -0800443 int minor = iminor(inode);
Jonathan Corbet8b5332f2008-06-19 14:34:41 -0600444 int ret;
Harald Welte77c44ab2005-11-13 16:06:26 -0800445
446 if (minor >= CM_MAX_DEV)
447 return -ENODEV;
448
Jonathan Corbet8b5332f2008-06-19 14:34:41 -0600449 lock_kernel();
Harald Welte77c44ab2005-11-13 16:06:26 -0800450 link = dev_table[minor];
Jonathan Corbet8b5332f2008-06-19 14:34:41 -0600451 if (link == NULL || !pcmcia_dev_present(link)) {
452 ret = -ENODEV;
453 goto out;
454 }
Harald Welte77c44ab2005-11-13 16:06:26 -0800455
Jonathan Corbet8b5332f2008-06-19 14:34:41 -0600456 if (link->open) {
457 ret = -EBUSY;
458 goto out;
459 }
Harald Welte77c44ab2005-11-13 16:06:26 -0800460
461 dev = link->priv;
462 filp->private_data = dev;
463
464 if (filp->f_flags & O_NONBLOCK) {
465 DEBUGP(4, dev, "filep->f_flags O_NONBLOCK set\n");
Jonathan Corbet8b5332f2008-06-19 14:34:41 -0600466 ret = -EAGAIN;
467 goto out;
Harald Welte77c44ab2005-11-13 16:06:26 -0800468 }
469
470 link->open = 1;
471
472 dev->poll_timer.data = (unsigned long) dev;
473 mod_timer(&dev->poll_timer, jiffies + POLL_PERIOD);
474
475 DEBUGP(2, dev, "<- cm4040_open (successfully)\n");
Jonathan Corbet8b5332f2008-06-19 14:34:41 -0600476 ret = nonseekable_open(inode, filp);
477out:
478 unlock_kernel();
479 return ret;
Harald Welte77c44ab2005-11-13 16:06:26 -0800480}
481
482static int cm4040_close(struct inode *inode, struct file *filp)
483{
484 struct reader_dev *dev = filp->private_data;
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200485 struct pcmcia_device *link;
Harald Welte77c44ab2005-11-13 16:06:26 -0800486 int minor = iminor(inode);
487
488 DEBUGP(2, dev, "-> cm4040_close(maj/min=%d.%d)\n", imajor(inode),
489 iminor(inode));
490
491 if (minor >= CM_MAX_DEV)
492 return -ENODEV;
493
494 link = dev_table[minor];
495 if (link == NULL)
496 return -ENODEV;
497
498 cm4040_stop_poll(dev);
499
500 link->open = 0;
501 wake_up(&dev->devq);
502
503 DEBUGP(2, dev, "<- cm4040_close\n");
504 return 0;
505}
506
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200507static void cm4040_reader_release(struct pcmcia_device *link)
Harald Welte77c44ab2005-11-13 16:06:26 -0800508{
509 struct reader_dev *dev = link->priv;
510
511 DEBUGP(3, dev, "-> cm4040_reader_release\n");
512 while (link->open) {
513 DEBUGP(3, dev, KERN_INFO MODULE_NAME ": delaying release "
514 "until process has terminated\n");
515 wait_event(dev->devq, (link->open == 0));
516 }
517 DEBUGP(3, dev, "<- cm4040_reader_release\n");
518 return;
519}
520
Dominik Brodowski84e2d342008-07-29 08:38:55 +0200521static int cm4040_config_check(struct pcmcia_device *p_dev,
522 cistpl_cftable_entry_t *cfg,
Dominik Brodowski8e2fc392008-08-02 15:30:31 +0200523 cistpl_cftable_entry_t *dflt,
Dominik Brodowskiad913c12008-08-02 16:12:00 +0200524 unsigned int vcc,
Dominik Brodowski84e2d342008-07-29 08:38:55 +0200525 void *priv_data)
526{
527 int rc;
Dominik Brodowski84e2d342008-07-29 08:38:55 +0200528 if (!cfg->io.nwin)
529 return -ENODEV;
530
531 /* Get the IOaddr */
532 p_dev->io.BasePort1 = cfg->io.win[0].base;
533 p_dev->io.NumPorts1 = cfg->io.win[0].len;
534 p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
535 if (!(cfg->io.flags & CISTPL_IO_8BIT))
536 p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_16;
537 if (!(cfg->io.flags & CISTPL_IO_16BIT))
538 p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
539 p_dev->io.IOAddrLines = cfg->io.flags & CISTPL_IO_LINES_MASK;
540
541 rc = pcmcia_request_io(p_dev, &p_dev->io);
Dominik Brodowskidd2e5a12009-11-03 10:27:34 +0100542 dev_printk(KERN_INFO, &p_dev->dev,
Dominik Brodowski84e2d342008-07-29 08:38:55 +0200543 "pcmcia_request_io returned 0x%x\n", rc);
544 return rc;
545}
546
547
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200548static int reader_config(struct pcmcia_device *link, int devno)
Harald Welte77c44ab2005-11-13 16:06:26 -0800549{
Harald Welte77c44ab2005-11-13 16:06:26 -0800550 struct reader_dev *dev;
Dominik Brodowski84e2d342008-07-29 08:38:55 +0200551 int fail_rc;
Harald Welte77c44ab2005-11-13 16:06:26 -0800552
Harald Welte77c44ab2005-11-13 16:06:26 -0800553 link->io.BasePort2 = 0;
554 link->io.NumPorts2 = 0;
555 link->io.Attributes2 = 0;
Harald Welte77c44ab2005-11-13 16:06:26 -0800556
Dominik Brodowski84e2d342008-07-29 08:38:55 +0200557 if (pcmcia_loop_config(link, cm4040_config_check, NULL))
Harald Welte77c44ab2005-11-13 16:06:26 -0800558 goto cs_release;
559
560 link->conf.IntType = 00000002;
561
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200562 fail_rc = pcmcia_request_configuration(link, &link->conf);
563 if (fail_rc != 0) {
Dominik Brodowskidd2e5a12009-11-03 10:27:34 +0100564 dev_printk(KERN_INFO, &link->dev,
Harald Welte77c44ab2005-11-13 16:06:26 -0800565 "pcmcia_request_configuration failed 0x%x\n",
566 fail_rc);
567 goto cs_release;
568 }
569
570 dev = link->priv;
571 sprintf(dev->node.dev_name, DEVICE_NAME "%d", devno);
572 dev->node.major = major;
573 dev->node.minor = devno;
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100574 dev->node.next = &dev->node;
Harald Welte77c44ab2005-11-13 16:06:26 -0800575
576 DEBUGP(2, dev, "device " DEVICE_NAME "%d at 0x%.4x-0x%.4x\n", devno,
577 link->io.BasePort1, link->io.BasePort1+link->io.NumPorts1);
578 DEBUGP(2, dev, "<- reader_config (succ)\n");
579
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200580 return 0;
Harald Welte77c44ab2005-11-13 16:06:26 -0800581
Harald Welte77c44ab2005-11-13 16:06:26 -0800582cs_release:
583 reader_release(link);
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200584 return -ENODEV;
Harald Welte77c44ab2005-11-13 16:06:26 -0800585}
586
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200587static void reader_release(struct pcmcia_device *link)
Harald Welte77c44ab2005-11-13 16:06:26 -0800588{
Daniel Ritz925796e2007-08-10 13:00:58 -0700589 cm4040_reader_release(link);
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200590 pcmcia_disable_device(link);
Harald Welte77c44ab2005-11-13 16:06:26 -0800591}
592
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200593static int reader_probe(struct pcmcia_device *link)
Harald Welte77c44ab2005-11-13 16:06:26 -0800594{
595 struct reader_dev *dev;
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200596 int i, ret;
Harald Welte77c44ab2005-11-13 16:06:26 -0800597
598 for (i = 0; i < CM_MAX_DEV; i++) {
599 if (dev_table[i] == NULL)
600 break;
601 }
602
603 if (i == CM_MAX_DEV)
Dominik Brodowskif8cfa612005-11-14 21:25:51 +0100604 return -ENODEV;
Harald Welte77c44ab2005-11-13 16:06:26 -0800605
606 dev = kzalloc(sizeof(struct reader_dev), GFP_KERNEL);
607 if (dev == NULL)
Dominik Brodowskif8cfa612005-11-14 21:25:51 +0100608 return -ENOMEM;
Harald Welte77c44ab2005-11-13 16:06:26 -0800609
610 dev->timeout = CCID_DRIVER_MINIMUM_TIMEOUT;
611 dev->buffer_status = 0;
612
Harald Welte77c44ab2005-11-13 16:06:26 -0800613 link->priv = dev;
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200614 dev->p_dev = link;
Harald Welte77c44ab2005-11-13 16:06:26 -0800615
616 link->conf.IntType = INT_MEMORY_AND_IO;
617 dev_table[i] = link;
618
Harald Welte77c44ab2005-11-13 16:06:26 -0800619 init_waitqueue_head(&dev->devq);
620 init_waitqueue_head(&dev->poll_wait);
621 init_waitqueue_head(&dev->read_wait);
622 init_waitqueue_head(&dev->write_wait);
Jiri Slaby40565f12007-02-12 00:52:31 -0800623 setup_timer(&dev->poll_timer, cm4040_do_poll, 0);
Harald Welte77c44ab2005-11-13 16:06:26 -0800624
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200625 ret = reader_config(link, i);
Akinobu Mita54493c12007-05-09 02:33:31 -0700626 if (ret) {
627 dev_table[i] = NULL;
628 kfree(dev);
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200629 return ret;
Akinobu Mita54493c12007-05-09 02:33:31 -0700630 }
Dominik Brodowskif8cfa612005-11-14 21:25:51 +0100631
Greg Kroah-Hartman03457cd2008-07-21 20:03:34 -0700632 device_create(cmx_class, NULL, MKDEV(major, i), NULL, "cmx%d", i);
Harald Welte67bc6202006-02-14 09:21:26 +0100633
Dominik Brodowskif8cfa612005-11-14 21:25:51 +0100634 return 0;
Harald Welte77c44ab2005-11-13 16:06:26 -0800635}
636
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200637static void reader_detach(struct pcmcia_device *link)
Harald Welte77c44ab2005-11-13 16:06:26 -0800638{
639 struct reader_dev *dev = link->priv;
Dominik Brodowskicc3b4862005-11-14 21:23:14 +0100640 int devno;
Harald Welte77c44ab2005-11-13 16:06:26 -0800641
642 /* find device */
Dominik Brodowskicc3b4862005-11-14 21:23:14 +0100643 for (devno = 0; devno < CM_MAX_DEV; devno++) {
644 if (dev_table[devno] == link)
Harald Welte77c44ab2005-11-13 16:06:26 -0800645 break;
646 }
Dominik Brodowskicc3b4862005-11-14 21:23:14 +0100647 if (devno == CM_MAX_DEV)
Harald Welte77c44ab2005-11-13 16:06:26 -0800648 return;
649
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100650 reader_release(link);
Dominik Brodowskicc3b4862005-11-14 21:23:14 +0100651
652 dev_table[devno] = NULL;
653 kfree(dev);
654
tonyj@suse.de07c015e2007-08-07 22:28:44 -0700655 device_destroy(cmx_class, MKDEV(major, devno));
Harald Welte67bc6202006-02-14 09:21:26 +0100656
Harald Welte77c44ab2005-11-13 16:06:26 -0800657 return;
658}
659
Arjan van de Ven62322d22006-07-03 00:24:21 -0700660static const struct file_operations reader_fops = {
Harald Welte77c44ab2005-11-13 16:06:26 -0800661 .owner = THIS_MODULE,
662 .read = cm4040_read,
663 .write = cm4040_write,
664 .open = cm4040_open,
665 .release = cm4040_close,
666 .poll = cm4040_poll,
667};
668
669static struct pcmcia_device_id cm4040_ids[] = {
670 PCMCIA_DEVICE_MANF_CARD(0x0223, 0x0200),
671 PCMCIA_DEVICE_PROD_ID12("OMNIKEY", "CardMan 4040",
672 0xE32CDD8C, 0x8F23318B),
673 PCMCIA_DEVICE_NULL,
674};
675MODULE_DEVICE_TABLE(pcmcia, cm4040_ids);
676
677static struct pcmcia_driver reader_driver = {
678 .owner = THIS_MODULE,
679 .drv = {
680 .name = "cm4040_cs",
681 },
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200682 .probe = reader_probe,
Dominik Brodowskicc3b4862005-11-14 21:23:14 +0100683 .remove = reader_detach,
Harald Welte77c44ab2005-11-13 16:06:26 -0800684 .id_table = cm4040_ids,
685};
686
687static int __init cm4040_init(void)
688{
Harald Welte67bc6202006-02-14 09:21:26 +0100689 int rc;
690
Harald Welte77c44ab2005-11-13 16:06:26 -0800691 printk(KERN_INFO "%s\n", version);
Harald Welte67bc6202006-02-14 09:21:26 +0100692 cmx_class = class_create(THIS_MODULE, "cardman_4040");
Akinobu Mita5eb5fc92006-10-17 15:25:59 +0900693 if (IS_ERR(cmx_class))
694 return PTR_ERR(cmx_class);
Harald Welte67bc6202006-02-14 09:21:26 +0100695
Harald Welte77c44ab2005-11-13 16:06:26 -0800696 major = register_chrdev(0, DEVICE_NAME, &reader_fops);
697 if (major < 0) {
698 printk(KERN_WARNING MODULE_NAME
699 ": could not get major number\n");
Akinobu Mita54493c12007-05-09 02:33:31 -0700700 class_destroy(cmx_class);
Akinobu Mita5eb5fc92006-10-17 15:25:59 +0900701 return major;
Harald Welte77c44ab2005-11-13 16:06:26 -0800702 }
Harald Welte7fc5b1e2006-05-10 13:28:52 +0200703
704 rc = pcmcia_register_driver(&reader_driver);
705 if (rc < 0) {
706 unregister_chrdev(major, DEVICE_NAME);
Akinobu Mita54493c12007-05-09 02:33:31 -0700707 class_destroy(cmx_class);
Harald Welte7fc5b1e2006-05-10 13:28:52 +0200708 return rc;
709 }
710
Harald Welte77c44ab2005-11-13 16:06:26 -0800711 return 0;
712}
713
714static void __exit cm4040_exit(void)
715{
Harald Welte77c44ab2005-11-13 16:06:26 -0800716 printk(KERN_INFO MODULE_NAME ": unloading\n");
717 pcmcia_unregister_driver(&reader_driver);
Harald Welte77c44ab2005-11-13 16:06:26 -0800718 unregister_chrdev(major, DEVICE_NAME);
Harald Welte67bc6202006-02-14 09:21:26 +0100719 class_destroy(cmx_class);
Harald Welte77c44ab2005-11-13 16:06:26 -0800720}
721
722module_init(cm4040_init);
723module_exit(cm4040_exit);
724MODULE_LICENSE("Dual BSD/GPL");