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