Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 1 | /* |
| 2 | * A driver for the Omnikey PCMCIA smartcard reader CardMan 4040 |
| 3 | * |
| 4 | * (c) 2000-2004 Omnikey AG (http://www.omnikey.com/) |
| 5 | * |
Harald Welte | 67bc620 | 2006-02-14 09:21:26 +0100 | [diff] [blame] | 6 | * (C) 2005-2006 Harald Welte <laforge@gnumonks.org> |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 7 | * - 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 Welte | 67bc620 | 2006-02-14 09:21:26 +0100 | [diff] [blame] | 12 | * - add class interface for udev device creation |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 13 | * |
| 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 Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 20 | #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 Corbet | 8b5332f | 2008-06-19 14:34:41 -0600 | [diff] [blame] | 27 | #include <linux/smp_lock.h> |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 28 | #include <linux/wait.h> |
| 29 | #include <asm/uaccess.h> |
| 30 | #include <asm/io.h> |
| 31 | |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 32 | #include <pcmcia/cistpl.h> |
| 33 | #include <pcmcia/cisreg.h> |
| 34 | #include <pcmcia/ciscode.h> |
| 35 | #include <pcmcia/ds.h> |
| 36 | |
| 37 | #include "cm4040_cs.h" |
| 38 | |
| 39 | |
Dominik Brodowski | dd2e5a1 | 2009-11-03 10:27:34 +0100 | [diff] [blame] | 40 | #define reader_to_dev(x) (&x->p_dev->dev) |
Dominik Brodowski | cbf624f | 2009-10-24 15:47:29 +0200 | [diff] [blame] | 41 | |
| 42 | /* n (debug level) is ignored */ |
| 43 | /* additional debug output may be enabled by re-compiling with |
| 44 | * CM4040_DEBUG set */ |
| 45 | /* #define CM4040_DEBUG */ |
| 46 | #define DEBUGP(n, rdr, x, args...) do { \ |
| 47 | dev_dbg(reader_to_dev(rdr), "%s:" x, \ |
| 48 | __func__ , ## args); \ |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 49 | } while (0) |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 50 | |
| 51 | static char *version = |
Harald Welte | 67bc620 | 2006-02-14 09:21:26 +0100 | [diff] [blame] | 52 | "OMNIKEY CardMan 4040 v1.1.0gm5 - All bugs added by Harald Welte"; |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 53 | |
| 54 | #define CCID_DRIVER_BULK_DEFAULT_TIMEOUT (150*HZ) |
| 55 | #define CCID_DRIVER_ASYNC_POWERUP_TIMEOUT (35*HZ) |
| 56 | #define CCID_DRIVER_MINIMUM_TIMEOUT (3*HZ) |
| 57 | #define READ_WRITE_BUFFER_SIZE 512 |
| 58 | #define POLL_LOOP_COUNT 1000 |
| 59 | |
| 60 | /* how often to poll for fifo status change */ |
| 61 | #define POLL_PERIOD msecs_to_jiffies(10) |
| 62 | |
Dominik Brodowski | fba395e | 2006-03-31 17:21:06 +0200 | [diff] [blame] | 63 | static void reader_release(struct pcmcia_device *link); |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 64 | |
| 65 | static int major; |
Harald Welte | 67bc620 | 2006-02-14 09:21:26 +0100 | [diff] [blame] | 66 | static struct class *cmx_class; |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 67 | |
| 68 | #define BS_READABLE 0x01 |
| 69 | #define BS_WRITABLE 0x02 |
| 70 | |
| 71 | struct reader_dev { |
Dominik Brodowski | fd23823 | 2006-03-05 10:45:09 +0100 | [diff] [blame] | 72 | struct pcmcia_device *p_dev; |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 73 | wait_queue_head_t devq; |
| 74 | wait_queue_head_t poll_wait; |
| 75 | wait_queue_head_t read_wait; |
| 76 | wait_queue_head_t write_wait; |
| 77 | unsigned long buffer_status; |
| 78 | unsigned long timeout; |
| 79 | unsigned char s_buf[READ_WRITE_BUFFER_SIZE]; |
| 80 | unsigned char r_buf[READ_WRITE_BUFFER_SIZE]; |
| 81 | struct timer_list poll_timer; |
| 82 | }; |
| 83 | |
Dominik Brodowski | fba395e | 2006-03-31 17:21:06 +0200 | [diff] [blame] | 84 | static struct pcmcia_device *dev_table[CM_MAX_DEV]; |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 85 | |
Dominik Brodowski | cbf624f | 2009-10-24 15:47:29 +0200 | [diff] [blame] | 86 | #ifndef CM4040_DEBUG |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 87 | #define xoutb outb |
| 88 | #define xinb inb |
| 89 | #else |
| 90 | static inline void xoutb(unsigned char val, unsigned short port) |
| 91 | { |
Dominik Brodowski | cbf624f | 2009-10-24 15:47:29 +0200 | [diff] [blame] | 92 | pr_debug("outb(val=%.2x,port=%.4x)\n", val, port); |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 93 | outb(val, port); |
| 94 | } |
| 95 | |
| 96 | static inline unsigned char xinb(unsigned short port) |
| 97 | { |
| 98 | unsigned char val; |
| 99 | |
| 100 | val = inb(port); |
Dominik Brodowski | cbf624f | 2009-10-24 15:47:29 +0200 | [diff] [blame] | 101 | pr_debug("%.2x=inb(%.4x)\n", val, port); |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 102 | return val; |
| 103 | } |
| 104 | #endif |
| 105 | |
| 106 | /* poll the device fifo status register. not to be confused with |
| 107 | * the poll syscall. */ |
| 108 | static void cm4040_do_poll(unsigned long dummy) |
| 109 | { |
| 110 | struct reader_dev *dev = (struct reader_dev *) dummy; |
Dominik Brodowski | 9a017a9 | 2010-07-24 15:58:54 +0200 | [diff] [blame] | 111 | unsigned int obs = xinb(dev->p_dev->resource[0]->start |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 112 | + REG_OFFSET_BUFFER_STATUS); |
| 113 | |
| 114 | if ((obs & BSR_BULK_IN_FULL)) { |
| 115 | set_bit(BS_READABLE, &dev->buffer_status); |
| 116 | DEBUGP(4, dev, "waking up read_wait\n"); |
| 117 | wake_up_interruptible(&dev->read_wait); |
| 118 | } else |
| 119 | clear_bit(BS_READABLE, &dev->buffer_status); |
| 120 | |
| 121 | if (!(obs & BSR_BULK_OUT_FULL)) { |
| 122 | set_bit(BS_WRITABLE, &dev->buffer_status); |
| 123 | DEBUGP(4, dev, "waking up write_wait\n"); |
| 124 | wake_up_interruptible(&dev->write_wait); |
| 125 | } else |
| 126 | clear_bit(BS_WRITABLE, &dev->buffer_status); |
| 127 | |
| 128 | if (dev->buffer_status) |
| 129 | wake_up_interruptible(&dev->poll_wait); |
| 130 | |
| 131 | mod_timer(&dev->poll_timer, jiffies + POLL_PERIOD); |
| 132 | } |
| 133 | |
| 134 | static void cm4040_stop_poll(struct reader_dev *dev) |
| 135 | { |
| 136 | del_timer_sync(&dev->poll_timer); |
| 137 | } |
| 138 | |
| 139 | static int wait_for_bulk_out_ready(struct reader_dev *dev) |
| 140 | { |
| 141 | int i, rc; |
Dominik Brodowski | 9a017a9 | 2010-07-24 15:58:54 +0200 | [diff] [blame] | 142 | int iobase = dev->p_dev->resource[0]->start; |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 143 | |
| 144 | for (i = 0; i < POLL_LOOP_COUNT; i++) { |
| 145 | if ((xinb(iobase + REG_OFFSET_BUFFER_STATUS) |
| 146 | & BSR_BULK_OUT_FULL) == 0) { |
| 147 | DEBUGP(4, dev, "BulkOut empty (i=%d)\n", i); |
| 148 | return 1; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | DEBUGP(4, dev, "wait_event_interruptible_timeout(timeout=%ld\n", |
| 153 | dev->timeout); |
| 154 | rc = wait_event_interruptible_timeout(dev->write_wait, |
| 155 | test_and_clear_bit(BS_WRITABLE, |
| 156 | &dev->buffer_status), |
| 157 | dev->timeout); |
| 158 | |
| 159 | if (rc > 0) |
| 160 | DEBUGP(4, dev, "woke up: BulkOut empty\n"); |
| 161 | else if (rc == 0) |
| 162 | DEBUGP(4, dev, "woke up: BulkOut full, returning 0 :(\n"); |
| 163 | else if (rc < 0) |
| 164 | DEBUGP(4, dev, "woke up: signal arrived\n"); |
| 165 | |
| 166 | return rc; |
| 167 | } |
| 168 | |
| 169 | /* Write to Sync Control Register */ |
| 170 | static int write_sync_reg(unsigned char val, struct reader_dev *dev) |
| 171 | { |
Dominik Brodowski | 9a017a9 | 2010-07-24 15:58:54 +0200 | [diff] [blame] | 172 | int iobase = dev->p_dev->resource[0]->start; |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 173 | int rc; |
| 174 | |
| 175 | rc = wait_for_bulk_out_ready(dev); |
| 176 | if (rc <= 0) |
| 177 | return rc; |
| 178 | |
| 179 | xoutb(val, iobase + REG_OFFSET_SYNC_CONTROL); |
| 180 | rc = wait_for_bulk_out_ready(dev); |
| 181 | if (rc <= 0) |
| 182 | return rc; |
| 183 | |
| 184 | return 1; |
| 185 | } |
| 186 | |
| 187 | static int wait_for_bulk_in_ready(struct reader_dev *dev) |
| 188 | { |
| 189 | int i, rc; |
Dominik Brodowski | 9a017a9 | 2010-07-24 15:58:54 +0200 | [diff] [blame] | 190 | int iobase = dev->p_dev->resource[0]->start; |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 191 | |
| 192 | for (i = 0; i < POLL_LOOP_COUNT; i++) { |
| 193 | if ((xinb(iobase + REG_OFFSET_BUFFER_STATUS) |
| 194 | & BSR_BULK_IN_FULL) == BSR_BULK_IN_FULL) { |
| 195 | DEBUGP(3, dev, "BulkIn full (i=%d)\n", i); |
| 196 | return 1; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | DEBUGP(4, dev, "wait_event_interruptible_timeout(timeout=%ld\n", |
| 201 | dev->timeout); |
| 202 | rc = wait_event_interruptible_timeout(dev->read_wait, |
| 203 | test_and_clear_bit(BS_READABLE, |
| 204 | &dev->buffer_status), |
| 205 | dev->timeout); |
| 206 | if (rc > 0) |
| 207 | DEBUGP(4, dev, "woke up: BulkIn full\n"); |
| 208 | else if (rc == 0) |
| 209 | DEBUGP(4, dev, "woke up: BulkIn not full, returning 0 :(\n"); |
| 210 | else if (rc < 0) |
| 211 | DEBUGP(4, dev, "woke up: signal arrived\n"); |
| 212 | |
| 213 | return rc; |
| 214 | } |
| 215 | |
| 216 | static ssize_t cm4040_read(struct file *filp, char __user *buf, |
| 217 | size_t count, loff_t *ppos) |
| 218 | { |
| 219 | struct reader_dev *dev = filp->private_data; |
Dominik Brodowski | 9a017a9 | 2010-07-24 15:58:54 +0200 | [diff] [blame] | 220 | int iobase = dev->p_dev->resource[0]->start; |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 221 | size_t bytes_to_read; |
| 222 | unsigned long i; |
| 223 | size_t min_bytes_to_read; |
| 224 | int rc; |
| 225 | unsigned char uc; |
| 226 | |
| 227 | DEBUGP(2, dev, "-> cm4040_read(%s,%d)\n", current->comm, current->pid); |
| 228 | |
| 229 | if (count == 0) |
| 230 | return 0; |
| 231 | |
| 232 | if (count < 10) |
| 233 | return -EFAULT; |
| 234 | |
| 235 | if (filp->f_flags & O_NONBLOCK) { |
| 236 | DEBUGP(4, dev, "filep->f_flags O_NONBLOCK set\n"); |
| 237 | DEBUGP(2, dev, "<- cm4040_read (failure)\n"); |
| 238 | return -EAGAIN; |
| 239 | } |
| 240 | |
Dominik Brodowski | e2d4096 | 2006-03-02 00:09:29 +0100 | [diff] [blame] | 241 | if (!pcmcia_dev_present(dev->p_dev)) |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 242 | return -ENODEV; |
| 243 | |
| 244 | for (i = 0; i < 5; i++) { |
| 245 | rc = wait_for_bulk_in_ready(dev); |
| 246 | if (rc <= 0) { |
| 247 | DEBUGP(5, dev, "wait_for_bulk_in_ready rc=%.2x\n", rc); |
| 248 | DEBUGP(2, dev, "<- cm4040_read (failed)\n"); |
| 249 | if (rc == -ERESTARTSYS) |
| 250 | return rc; |
| 251 | return -EIO; |
| 252 | } |
| 253 | dev->r_buf[i] = xinb(iobase + REG_OFFSET_BULK_IN); |
Dominik Brodowski | cbf624f | 2009-10-24 15:47:29 +0200 | [diff] [blame] | 254 | #ifdef CM4040_DEBUG |
| 255 | pr_debug("%lu:%2x ", i, dev->r_buf[i]); |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 256 | } |
Dominik Brodowski | cbf624f | 2009-10-24 15:47:29 +0200 | [diff] [blame] | 257 | pr_debug("\n"); |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 258 | #else |
| 259 | } |
| 260 | #endif |
| 261 | |
| 262 | bytes_to_read = 5 + le32_to_cpu(*(__le32 *)&dev->r_buf[1]); |
| 263 | |
Randy Dunlap | e657ea1 | 2009-11-11 09:31:07 -0800 | [diff] [blame] | 264 | DEBUGP(6, dev, "BytesToRead=%zu\n", bytes_to_read); |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 265 | |
| 266 | min_bytes_to_read = min(count, bytes_to_read + 5); |
Marcel Holtmann | 059819a | 2007-03-06 22:12:00 +0100 | [diff] [blame] | 267 | min_bytes_to_read = min_t(size_t, min_bytes_to_read, READ_WRITE_BUFFER_SIZE); |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 268 | |
Randy Dunlap | e657ea1 | 2009-11-11 09:31:07 -0800 | [diff] [blame] | 269 | DEBUGP(6, dev, "Min=%zu\n", min_bytes_to_read); |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 270 | |
| 271 | for (i = 0; i < (min_bytes_to_read-5); i++) { |
| 272 | rc = wait_for_bulk_in_ready(dev); |
| 273 | if (rc <= 0) { |
| 274 | DEBUGP(5, dev, "wait_for_bulk_in_ready rc=%.2x\n", rc); |
| 275 | DEBUGP(2, dev, "<- cm4040_read (failed)\n"); |
| 276 | if (rc == -ERESTARTSYS) |
| 277 | return rc; |
| 278 | return -EIO; |
| 279 | } |
| 280 | dev->r_buf[i+5] = xinb(iobase + REG_OFFSET_BULK_IN); |
Dominik Brodowski | cbf624f | 2009-10-24 15:47:29 +0200 | [diff] [blame] | 281 | #ifdef CM4040_DEBUG |
| 282 | pr_debug("%lu:%2x ", i, dev->r_buf[i]); |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 283 | } |
Dominik Brodowski | cbf624f | 2009-10-24 15:47:29 +0200 | [diff] [blame] | 284 | pr_debug("\n"); |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 285 | #else |
| 286 | } |
| 287 | #endif |
| 288 | |
| 289 | *ppos = min_bytes_to_read; |
| 290 | if (copy_to_user(buf, dev->r_buf, min_bytes_to_read)) |
| 291 | return -EFAULT; |
| 292 | |
| 293 | rc = wait_for_bulk_in_ready(dev); |
| 294 | if (rc <= 0) { |
| 295 | DEBUGP(5, dev, "wait_for_bulk_in_ready rc=%.2x\n", rc); |
| 296 | DEBUGP(2, dev, "<- cm4040_read (failed)\n"); |
| 297 | if (rc == -ERESTARTSYS) |
| 298 | return rc; |
| 299 | return -EIO; |
| 300 | } |
| 301 | |
| 302 | rc = write_sync_reg(SCR_READER_TO_HOST_DONE, dev); |
| 303 | if (rc <= 0) { |
| 304 | DEBUGP(5, dev, "write_sync_reg c=%.2x\n", rc); |
| 305 | DEBUGP(2, dev, "<- cm4040_read (failed)\n"); |
| 306 | if (rc == -ERESTARTSYS) |
| 307 | return rc; |
| 308 | else |
| 309 | return -EIO; |
| 310 | } |
| 311 | |
| 312 | uc = xinb(iobase + REG_OFFSET_BULK_IN); |
| 313 | |
| 314 | DEBUGP(2, dev, "<- cm4040_read (successfully)\n"); |
| 315 | return min_bytes_to_read; |
| 316 | } |
| 317 | |
| 318 | static ssize_t cm4040_write(struct file *filp, const char __user *buf, |
| 319 | size_t count, loff_t *ppos) |
| 320 | { |
| 321 | struct reader_dev *dev = filp->private_data; |
Dominik Brodowski | 9a017a9 | 2010-07-24 15:58:54 +0200 | [diff] [blame] | 322 | int iobase = dev->p_dev->resource[0]->start; |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 323 | ssize_t rc; |
| 324 | int i; |
| 325 | unsigned int bytes_to_write; |
| 326 | |
| 327 | DEBUGP(2, dev, "-> cm4040_write(%s,%d)\n", current->comm, current->pid); |
| 328 | |
| 329 | if (count == 0) { |
| 330 | DEBUGP(2, dev, "<- cm4040_write empty read (successfully)\n"); |
| 331 | return 0; |
| 332 | } |
| 333 | |
Marcel Holtmann | 059819a | 2007-03-06 22:12:00 +0100 | [diff] [blame] | 334 | if ((count < 5) || (count > READ_WRITE_BUFFER_SIZE)) { |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 335 | DEBUGP(2, dev, "<- cm4040_write buffersize=%Zd < 5\n", count); |
| 336 | return -EIO; |
| 337 | } |
| 338 | |
| 339 | if (filp->f_flags & O_NONBLOCK) { |
| 340 | DEBUGP(4, dev, "filep->f_flags O_NONBLOCK set\n"); |
| 341 | DEBUGP(4, dev, "<- cm4040_write (failure)\n"); |
| 342 | return -EAGAIN; |
| 343 | } |
| 344 | |
Dominik Brodowski | e2d4096 | 2006-03-02 00:09:29 +0100 | [diff] [blame] | 345 | if (!pcmcia_dev_present(dev->p_dev)) |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 346 | return -ENODEV; |
| 347 | |
| 348 | bytes_to_write = count; |
| 349 | if (copy_from_user(dev->s_buf, buf, bytes_to_write)) |
| 350 | return -EFAULT; |
| 351 | |
| 352 | switch (dev->s_buf[0]) { |
| 353 | case CMD_PC_TO_RDR_XFRBLOCK: |
| 354 | case CMD_PC_TO_RDR_SECURE: |
| 355 | case CMD_PC_TO_RDR_TEST_SECURE: |
| 356 | case CMD_PC_TO_RDR_OK_SECURE: |
| 357 | dev->timeout = CCID_DRIVER_BULK_DEFAULT_TIMEOUT; |
| 358 | break; |
| 359 | |
| 360 | case CMD_PC_TO_RDR_ICCPOWERON: |
| 361 | dev->timeout = CCID_DRIVER_ASYNC_POWERUP_TIMEOUT; |
| 362 | break; |
| 363 | |
| 364 | case CMD_PC_TO_RDR_GETSLOTSTATUS: |
| 365 | case CMD_PC_TO_RDR_ICCPOWEROFF: |
| 366 | case CMD_PC_TO_RDR_GETPARAMETERS: |
| 367 | case CMD_PC_TO_RDR_RESETPARAMETERS: |
| 368 | case CMD_PC_TO_RDR_SETPARAMETERS: |
| 369 | case CMD_PC_TO_RDR_ESCAPE: |
| 370 | case CMD_PC_TO_RDR_ICCCLOCK: |
| 371 | default: |
| 372 | dev->timeout = CCID_DRIVER_MINIMUM_TIMEOUT; |
| 373 | break; |
| 374 | } |
| 375 | |
| 376 | rc = write_sync_reg(SCR_HOST_TO_READER_START, dev); |
| 377 | if (rc <= 0) { |
| 378 | DEBUGP(5, dev, "write_sync_reg c=%.2Zx\n", rc); |
| 379 | DEBUGP(2, dev, "<- cm4040_write (failed)\n"); |
| 380 | if (rc == -ERESTARTSYS) |
| 381 | return rc; |
| 382 | else |
| 383 | return -EIO; |
| 384 | } |
| 385 | |
| 386 | DEBUGP(4, dev, "start \n"); |
| 387 | |
| 388 | for (i = 0; i < bytes_to_write; i++) { |
| 389 | rc = wait_for_bulk_out_ready(dev); |
| 390 | if (rc <= 0) { |
| 391 | DEBUGP(5, dev, "wait_for_bulk_out_ready rc=%.2Zx\n", |
| 392 | rc); |
| 393 | DEBUGP(2, dev, "<- cm4040_write (failed)\n"); |
| 394 | if (rc == -ERESTARTSYS) |
| 395 | return rc; |
| 396 | else |
| 397 | return -EIO; |
| 398 | } |
| 399 | |
| 400 | xoutb(dev->s_buf[i],iobase + REG_OFFSET_BULK_OUT); |
| 401 | } |
| 402 | DEBUGP(4, dev, "end\n"); |
| 403 | |
| 404 | rc = write_sync_reg(SCR_HOST_TO_READER_DONE, dev); |
| 405 | |
| 406 | if (rc <= 0) { |
| 407 | DEBUGP(5, dev, "write_sync_reg c=%.2Zx\n", rc); |
| 408 | DEBUGP(2, dev, "<- cm4040_write (failed)\n"); |
| 409 | if (rc == -ERESTARTSYS) |
| 410 | return rc; |
| 411 | else |
| 412 | return -EIO; |
| 413 | } |
| 414 | |
| 415 | DEBUGP(2, dev, "<- cm4040_write (successfully)\n"); |
| 416 | return count; |
| 417 | } |
| 418 | |
| 419 | static unsigned int cm4040_poll(struct file *filp, poll_table *wait) |
| 420 | { |
| 421 | struct reader_dev *dev = filp->private_data; |
| 422 | unsigned int mask = 0; |
| 423 | |
| 424 | poll_wait(filp, &dev->poll_wait, wait); |
| 425 | |
| 426 | if (test_and_clear_bit(BS_READABLE, &dev->buffer_status)) |
| 427 | mask |= POLLIN | POLLRDNORM; |
| 428 | if (test_and_clear_bit(BS_WRITABLE, &dev->buffer_status)) |
| 429 | mask |= POLLOUT | POLLWRNORM; |
| 430 | |
| 431 | DEBUGP(2, dev, "<- cm4040_poll(%u)\n", mask); |
| 432 | |
| 433 | return mask; |
| 434 | } |
| 435 | |
| 436 | static int cm4040_open(struct inode *inode, struct file *filp) |
| 437 | { |
| 438 | struct reader_dev *dev; |
Dominik Brodowski | fba395e | 2006-03-31 17:21:06 +0200 | [diff] [blame] | 439 | struct pcmcia_device *link; |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 440 | int minor = iminor(inode); |
Jonathan Corbet | 8b5332f | 2008-06-19 14:34:41 -0600 | [diff] [blame] | 441 | int ret; |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 442 | |
| 443 | if (minor >= CM_MAX_DEV) |
| 444 | return -ENODEV; |
| 445 | |
Jonathan Corbet | 8b5332f | 2008-06-19 14:34:41 -0600 | [diff] [blame] | 446 | lock_kernel(); |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 447 | link = dev_table[minor]; |
Jonathan Corbet | 8b5332f | 2008-06-19 14:34:41 -0600 | [diff] [blame] | 448 | if (link == NULL || !pcmcia_dev_present(link)) { |
| 449 | ret = -ENODEV; |
| 450 | goto out; |
| 451 | } |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 452 | |
Jonathan Corbet | 8b5332f | 2008-06-19 14:34:41 -0600 | [diff] [blame] | 453 | if (link->open) { |
| 454 | ret = -EBUSY; |
| 455 | goto out; |
| 456 | } |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 457 | |
| 458 | dev = link->priv; |
| 459 | filp->private_data = dev; |
| 460 | |
| 461 | if (filp->f_flags & O_NONBLOCK) { |
| 462 | DEBUGP(4, dev, "filep->f_flags O_NONBLOCK set\n"); |
Jonathan Corbet | 8b5332f | 2008-06-19 14:34:41 -0600 | [diff] [blame] | 463 | ret = -EAGAIN; |
| 464 | goto out; |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | link->open = 1; |
| 468 | |
| 469 | dev->poll_timer.data = (unsigned long) dev; |
| 470 | mod_timer(&dev->poll_timer, jiffies + POLL_PERIOD); |
| 471 | |
| 472 | DEBUGP(2, dev, "<- cm4040_open (successfully)\n"); |
Jonathan Corbet | 8b5332f | 2008-06-19 14:34:41 -0600 | [diff] [blame] | 473 | ret = nonseekable_open(inode, filp); |
| 474 | out: |
| 475 | unlock_kernel(); |
| 476 | return ret; |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | static int cm4040_close(struct inode *inode, struct file *filp) |
| 480 | { |
| 481 | struct reader_dev *dev = filp->private_data; |
Dominik Brodowski | fba395e | 2006-03-31 17:21:06 +0200 | [diff] [blame] | 482 | struct pcmcia_device *link; |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 483 | int minor = iminor(inode); |
| 484 | |
| 485 | DEBUGP(2, dev, "-> cm4040_close(maj/min=%d.%d)\n", imajor(inode), |
| 486 | iminor(inode)); |
| 487 | |
| 488 | if (minor >= CM_MAX_DEV) |
| 489 | return -ENODEV; |
| 490 | |
| 491 | link = dev_table[minor]; |
| 492 | if (link == NULL) |
| 493 | return -ENODEV; |
| 494 | |
| 495 | cm4040_stop_poll(dev); |
| 496 | |
| 497 | link->open = 0; |
| 498 | wake_up(&dev->devq); |
| 499 | |
| 500 | DEBUGP(2, dev, "<- cm4040_close\n"); |
| 501 | return 0; |
| 502 | } |
| 503 | |
Dominik Brodowski | fba395e | 2006-03-31 17:21:06 +0200 | [diff] [blame] | 504 | static void cm4040_reader_release(struct pcmcia_device *link) |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 505 | { |
| 506 | struct reader_dev *dev = link->priv; |
| 507 | |
| 508 | DEBUGP(3, dev, "-> cm4040_reader_release\n"); |
| 509 | while (link->open) { |
| 510 | DEBUGP(3, dev, KERN_INFO MODULE_NAME ": delaying release " |
| 511 | "until process has terminated\n"); |
| 512 | wait_event(dev->devq, (link->open == 0)); |
| 513 | } |
| 514 | DEBUGP(3, dev, "<- cm4040_reader_release\n"); |
| 515 | return; |
| 516 | } |
| 517 | |
Dominik Brodowski | 00990e7 | 2010-07-30 13:13:46 +0200 | [diff] [blame] | 518 | static int cm4040_config_check(struct pcmcia_device *p_dev, void *priv_data) |
Dominik Brodowski | 84e2d34 | 2008-07-29 08:38:55 +0200 | [diff] [blame] | 519 | { |
Dominik Brodowski | 00990e7 | 2010-07-30 13:13:46 +0200 | [diff] [blame] | 520 | return pcmcia_request_io(p_dev); |
Dominik Brodowski | 84e2d34 | 2008-07-29 08:38:55 +0200 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | |
Dominik Brodowski | 15b99ac | 2006-03-31 17:26:06 +0200 | [diff] [blame] | 524 | static int reader_config(struct pcmcia_device *link, int devno) |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 525 | { |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 526 | struct reader_dev *dev; |
Dominik Brodowski | 84e2d34 | 2008-07-29 08:38:55 +0200 | [diff] [blame] | 527 | int fail_rc; |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 528 | |
Dominik Brodowski | 00990e7 | 2010-07-30 13:13:46 +0200 | [diff] [blame] | 529 | link->config_flags |= CONF_AUTO_SET_IO; |
| 530 | |
Dominik Brodowski | 84e2d34 | 2008-07-29 08:38:55 +0200 | [diff] [blame] | 531 | if (pcmcia_loop_config(link, cm4040_config_check, NULL)) |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 532 | goto cs_release; |
| 533 | |
Dominik Brodowski | 1ac71e5 | 2010-07-29 19:27:09 +0200 | [diff] [blame] | 534 | fail_rc = pcmcia_enable_device(link); |
Dominik Brodowski | 4c89e88 | 2008-08-03 10:07:45 +0200 | [diff] [blame] | 535 | if (fail_rc != 0) { |
Dominik Brodowski | dd2e5a1 | 2009-11-03 10:27:34 +0100 | [diff] [blame] | 536 | dev_printk(KERN_INFO, &link->dev, |
Dominik Brodowski | 1ac71e5 | 2010-07-29 19:27:09 +0200 | [diff] [blame] | 537 | "pcmcia_enable_device failed 0x%x\n", |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 538 | fail_rc); |
| 539 | goto cs_release; |
| 540 | } |
| 541 | |
| 542 | dev = link->priv; |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 543 | |
Dominik Brodowski | 9a017a9 | 2010-07-24 15:58:54 +0200 | [diff] [blame] | 544 | DEBUGP(2, dev, "device " DEVICE_NAME "%d at %pR\n", devno, |
| 545 | link->resource[0]); |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 546 | DEBUGP(2, dev, "<- reader_config (succ)\n"); |
| 547 | |
Dominik Brodowski | 15b99ac | 2006-03-31 17:26:06 +0200 | [diff] [blame] | 548 | return 0; |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 549 | |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 550 | cs_release: |
| 551 | reader_release(link); |
Dominik Brodowski | 15b99ac | 2006-03-31 17:26:06 +0200 | [diff] [blame] | 552 | return -ENODEV; |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 553 | } |
| 554 | |
Dominik Brodowski | fba395e | 2006-03-31 17:21:06 +0200 | [diff] [blame] | 555 | static void reader_release(struct pcmcia_device *link) |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 556 | { |
Daniel Ritz | 925796e | 2007-08-10 13:00:58 -0700 | [diff] [blame] | 557 | cm4040_reader_release(link); |
Dominik Brodowski | fba395e | 2006-03-31 17:21:06 +0200 | [diff] [blame] | 558 | pcmcia_disable_device(link); |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 559 | } |
| 560 | |
Dominik Brodowski | 15b99ac | 2006-03-31 17:26:06 +0200 | [diff] [blame] | 561 | static int reader_probe(struct pcmcia_device *link) |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 562 | { |
| 563 | struct reader_dev *dev; |
Dominik Brodowski | 15b99ac | 2006-03-31 17:26:06 +0200 | [diff] [blame] | 564 | int i, ret; |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 565 | |
| 566 | for (i = 0; i < CM_MAX_DEV; i++) { |
| 567 | if (dev_table[i] == NULL) |
| 568 | break; |
| 569 | } |
| 570 | |
| 571 | if (i == CM_MAX_DEV) |
Dominik Brodowski | f8cfa61 | 2005-11-14 21:25:51 +0100 | [diff] [blame] | 572 | return -ENODEV; |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 573 | |
| 574 | dev = kzalloc(sizeof(struct reader_dev), GFP_KERNEL); |
| 575 | if (dev == NULL) |
Dominik Brodowski | f8cfa61 | 2005-11-14 21:25:51 +0100 | [diff] [blame] | 576 | return -ENOMEM; |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 577 | |
| 578 | dev->timeout = CCID_DRIVER_MINIMUM_TIMEOUT; |
| 579 | dev->buffer_status = 0; |
| 580 | |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 581 | link->priv = dev; |
Dominik Brodowski | fba395e | 2006-03-31 17:21:06 +0200 | [diff] [blame] | 582 | dev->p_dev = link; |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 583 | |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 584 | dev_table[i] = link; |
| 585 | |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 586 | init_waitqueue_head(&dev->devq); |
| 587 | init_waitqueue_head(&dev->poll_wait); |
| 588 | init_waitqueue_head(&dev->read_wait); |
| 589 | init_waitqueue_head(&dev->write_wait); |
Jiri Slaby | 40565f1 | 2007-02-12 00:52:31 -0800 | [diff] [blame] | 590 | setup_timer(&dev->poll_timer, cm4040_do_poll, 0); |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 591 | |
Dominik Brodowski | 15b99ac | 2006-03-31 17:26:06 +0200 | [diff] [blame] | 592 | ret = reader_config(link, i); |
Akinobu Mita | 54493c1 | 2007-05-09 02:33:31 -0700 | [diff] [blame] | 593 | if (ret) { |
| 594 | dev_table[i] = NULL; |
| 595 | kfree(dev); |
Dominik Brodowski | 15b99ac | 2006-03-31 17:26:06 +0200 | [diff] [blame] | 596 | return ret; |
Akinobu Mita | 54493c1 | 2007-05-09 02:33:31 -0700 | [diff] [blame] | 597 | } |
Dominik Brodowski | f8cfa61 | 2005-11-14 21:25:51 +0100 | [diff] [blame] | 598 | |
Greg Kroah-Hartman | 03457cd | 2008-07-21 20:03:34 -0700 | [diff] [blame] | 599 | device_create(cmx_class, NULL, MKDEV(major, i), NULL, "cmx%d", i); |
Harald Welte | 67bc620 | 2006-02-14 09:21:26 +0100 | [diff] [blame] | 600 | |
Dominik Brodowski | f8cfa61 | 2005-11-14 21:25:51 +0100 | [diff] [blame] | 601 | return 0; |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 602 | } |
| 603 | |
Dominik Brodowski | fba395e | 2006-03-31 17:21:06 +0200 | [diff] [blame] | 604 | static void reader_detach(struct pcmcia_device *link) |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 605 | { |
| 606 | struct reader_dev *dev = link->priv; |
Dominik Brodowski | cc3b486 | 2005-11-14 21:23:14 +0100 | [diff] [blame] | 607 | int devno; |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 608 | |
| 609 | /* find device */ |
Dominik Brodowski | cc3b486 | 2005-11-14 21:23:14 +0100 | [diff] [blame] | 610 | for (devno = 0; devno < CM_MAX_DEV; devno++) { |
| 611 | if (dev_table[devno] == link) |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 612 | break; |
| 613 | } |
Dominik Brodowski | cc3b486 | 2005-11-14 21:23:14 +0100 | [diff] [blame] | 614 | if (devno == CM_MAX_DEV) |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 615 | return; |
| 616 | |
Dominik Brodowski | e2d4096 | 2006-03-02 00:09:29 +0100 | [diff] [blame] | 617 | reader_release(link); |
Dominik Brodowski | cc3b486 | 2005-11-14 21:23:14 +0100 | [diff] [blame] | 618 | |
| 619 | dev_table[devno] = NULL; |
| 620 | kfree(dev); |
| 621 | |
tonyj@suse.de | 07c015e | 2007-08-07 22:28:44 -0700 | [diff] [blame] | 622 | device_destroy(cmx_class, MKDEV(major, devno)); |
Harald Welte | 67bc620 | 2006-02-14 09:21:26 +0100 | [diff] [blame] | 623 | |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 624 | return; |
| 625 | } |
| 626 | |
Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 627 | static const struct file_operations reader_fops = { |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 628 | .owner = THIS_MODULE, |
| 629 | .read = cm4040_read, |
| 630 | .write = cm4040_write, |
| 631 | .open = cm4040_open, |
| 632 | .release = cm4040_close, |
| 633 | .poll = cm4040_poll, |
| 634 | }; |
| 635 | |
| 636 | static struct pcmcia_device_id cm4040_ids[] = { |
| 637 | PCMCIA_DEVICE_MANF_CARD(0x0223, 0x0200), |
| 638 | PCMCIA_DEVICE_PROD_ID12("OMNIKEY", "CardMan 4040", |
| 639 | 0xE32CDD8C, 0x8F23318B), |
| 640 | PCMCIA_DEVICE_NULL, |
| 641 | }; |
| 642 | MODULE_DEVICE_TABLE(pcmcia, cm4040_ids); |
| 643 | |
| 644 | static struct pcmcia_driver reader_driver = { |
| 645 | .owner = THIS_MODULE, |
Dominik Brodowski | 2e9b981 | 2010-08-08 11:36:26 +0200 | [diff] [blame^] | 646 | .name = "cm4040_cs", |
Dominik Brodowski | 15b99ac | 2006-03-31 17:26:06 +0200 | [diff] [blame] | 647 | .probe = reader_probe, |
Dominik Brodowski | cc3b486 | 2005-11-14 21:23:14 +0100 | [diff] [blame] | 648 | .remove = reader_detach, |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 649 | .id_table = cm4040_ids, |
| 650 | }; |
| 651 | |
| 652 | static int __init cm4040_init(void) |
| 653 | { |
Harald Welte | 67bc620 | 2006-02-14 09:21:26 +0100 | [diff] [blame] | 654 | int rc; |
| 655 | |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 656 | printk(KERN_INFO "%s\n", version); |
Harald Welte | 67bc620 | 2006-02-14 09:21:26 +0100 | [diff] [blame] | 657 | cmx_class = class_create(THIS_MODULE, "cardman_4040"); |
Akinobu Mita | 5eb5fc9 | 2006-10-17 15:25:59 +0900 | [diff] [blame] | 658 | if (IS_ERR(cmx_class)) |
| 659 | return PTR_ERR(cmx_class); |
Harald Welte | 67bc620 | 2006-02-14 09:21:26 +0100 | [diff] [blame] | 660 | |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 661 | major = register_chrdev(0, DEVICE_NAME, &reader_fops); |
| 662 | if (major < 0) { |
| 663 | printk(KERN_WARNING MODULE_NAME |
| 664 | ": could not get major number\n"); |
Akinobu Mita | 54493c1 | 2007-05-09 02:33:31 -0700 | [diff] [blame] | 665 | class_destroy(cmx_class); |
Akinobu Mita | 5eb5fc9 | 2006-10-17 15:25:59 +0900 | [diff] [blame] | 666 | return major; |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 667 | } |
Harald Welte | 7fc5b1e | 2006-05-10 13:28:52 +0200 | [diff] [blame] | 668 | |
| 669 | rc = pcmcia_register_driver(&reader_driver); |
| 670 | if (rc < 0) { |
| 671 | unregister_chrdev(major, DEVICE_NAME); |
Akinobu Mita | 54493c1 | 2007-05-09 02:33:31 -0700 | [diff] [blame] | 672 | class_destroy(cmx_class); |
Harald Welte | 7fc5b1e | 2006-05-10 13:28:52 +0200 | [diff] [blame] | 673 | return rc; |
| 674 | } |
| 675 | |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 676 | return 0; |
| 677 | } |
| 678 | |
| 679 | static void __exit cm4040_exit(void) |
| 680 | { |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 681 | printk(KERN_INFO MODULE_NAME ": unloading\n"); |
| 682 | pcmcia_unregister_driver(&reader_driver); |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 683 | unregister_chrdev(major, DEVICE_NAME); |
Harald Welte | 67bc620 | 2006-02-14 09:21:26 +0100 | [diff] [blame] | 684 | class_destroy(cmx_class); |
Harald Welte | 77c44ab | 2005-11-13 16:06:26 -0800 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | module_init(cm4040_init); |
| 688 | module_exit(cm4040_exit); |
| 689 | MODULE_LICENSE("Dual BSD/GPL"); |