1 /* 2 * REINER SCT cyberJack pinpad/e-com USB Chipcard Reader Driver 3 * 4 * Copyright (C) 2001 REINER SCT 5 * Author: Matthias Bruestle 6 * 7 * Contact: support@reiner-sct.com (see MAINTAINERS) 8 * 9 * This program is largely derived from work by the linux-usb group 10 * and associated source files. Please see the usb/serial files for 11 * individual credits and copyrights. 12 * 13 * This program is free software; you can redistribute it and/or modify 14 * it under the terms of the GNU General Public License as published by 15 * the Free Software Foundation; either version 2 of the License, or 16 * (at your option) any later version. 17 * 18 * Thanks to Greg Kroah-Hartman (greg@kroah.com) for his help and 19 * patience. 20 * 21 * In case of problems, please write to the contact e-mail address 22 * mentioned above. 23 * 24 * Please note that later models of the cyberjack reader family are 25 * supported by a libusb-based userspace device driver. 26 * 27 * Homepage: http://www.reiner-sct.de/support/treiber_cyberjack.php#linux 28 */ 29 30 31 #include <linux/kernel.h> 32 #include <linux/errno.h> 33 #include <linux/init.h> 34 #include <linux/slab.h> 35 #include <linux/tty.h> 36 #include <linux/tty_driver.h> 37 #include <linux/tty_flip.h> 38 #include <linux/module.h> 39 #include <linux/spinlock.h> 40 #include <linux/uaccess.h> 41 #include <linux/usb.h> 42 #include <linux/usb/serial.h> 43 44 #define CYBERJACK_LOCAL_BUF_SIZE 32 45 46 /* 47 * Version Information 48 */ 49 #define DRIVER_VERSION "v1.01" 50 #define DRIVER_AUTHOR "Matthias Bruestle" 51 #define DRIVER_DESC "REINER SCT cyberJack pinpad/e-com USB Chipcard Reader Driver" 52 53 54 #define CYBERJACK_VENDOR_ID 0x0C4B 55 #define CYBERJACK_PRODUCT_ID 0x0100 56 57 /* Function prototypes */ 58 static int cyberjack_startup(struct usb_serial *serial); 59 static void cyberjack_disconnect(struct usb_serial *serial); 60 static void cyberjack_release(struct usb_serial *serial); 61 static int cyberjack_open(struct tty_struct *tty, 62 struct usb_serial_port *port); 63 static void cyberjack_close(struct usb_serial_port *port); 64 static int cyberjack_write(struct tty_struct *tty, 65 struct usb_serial_port *port, const unsigned char *buf, int count); 66 static int cyberjack_write_room(struct tty_struct *tty); 67 static void cyberjack_read_int_callback(struct urb *urb); 68 static void cyberjack_read_bulk_callback(struct urb *urb); 69 static void cyberjack_write_bulk_callback(struct urb *urb); 70 71 static const struct usb_device_id id_table[] = { 72 { USB_DEVICE(CYBERJACK_VENDOR_ID, CYBERJACK_PRODUCT_ID) }, 73 { } /* Terminating entry */ 74 }; 75 76 MODULE_DEVICE_TABLE(usb, id_table); 77 78 static struct usb_serial_driver cyberjack_device = { 79 .driver = { 80 .owner = THIS_MODULE, 81 .name = "cyberjack", 82 }, 83 .description = "Reiner SCT Cyberjack USB card reader", 84 .id_table = id_table, 85 .num_ports = 1, 86 .attach = cyberjack_startup, 87 .disconnect = cyberjack_disconnect, 88 .release = cyberjack_release, 89 .open = cyberjack_open, 90 .close = cyberjack_close, 91 .write = cyberjack_write, 92 .write_room = cyberjack_write_room, 93 .read_int_callback = cyberjack_read_int_callback, 94 .read_bulk_callback = cyberjack_read_bulk_callback, 95 .write_bulk_callback = cyberjack_write_bulk_callback, 96 }; 97 98 static struct usb_serial_driver * const serial_drivers[] = { 99 &cyberjack_device, NULL 100 }; 101 102 struct cyberjack_private { 103 spinlock_t lock; /* Lock for SMP */ 104 short rdtodo; /* Bytes still to read */ 105 unsigned char wrbuf[5*64]; /* Buffer for collecting data to write */ 106 short wrfilled; /* Overall data size we already got */ 107 short wrsent; /* Data already sent */ 108 }; 109 110 /* do some startup allocations not currently performed by usb_serial_probe() */ 111 static int cyberjack_startup(struct usb_serial *serial) 112 { 113 struct cyberjack_private *priv; 114 int i; 115 116 /* allocate the private data structure */ 117 priv = kmalloc(sizeof(struct cyberjack_private), GFP_KERNEL); 118 if (!priv) 119 return -ENOMEM; 120 121 /* set initial values */ 122 spin_lock_init(&priv->lock); 123 priv->rdtodo = 0; 124 priv->wrfilled = 0; 125 priv->wrsent = 0; 126 usb_set_serial_port_data(serial->port[0], priv); 127 128 init_waitqueue_head(&serial->port[0]->write_wait); 129 130 for (i = 0; i < serial->num_ports; ++i) { 131 int result; 132 result = usb_submit_urb(serial->port[i]->interrupt_in_urb, 133 GFP_KERNEL); 134 if (result) 135 dev_err(&serial->dev->dev, 136 "usb_submit_urb(read int) failed\n"); 137 dev_dbg(&serial->dev->dev, "%s - usb_submit_urb(int urb)\n", 138 __func__); 139 } 140 141 return 0; 142 } 143 144 static void cyberjack_disconnect(struct usb_serial *serial) 145 { 146 int i; 147 148 for (i = 0; i < serial->num_ports; ++i) 149 usb_kill_urb(serial->port[i]->interrupt_in_urb); 150 } 151 152 static void cyberjack_release(struct usb_serial *serial) 153 { 154 int i; 155 156 for (i = 0; i < serial->num_ports; ++i) { 157 /* My special items, the standard routines free my urbs */ 158 kfree(usb_get_serial_port_data(serial->port[i])); 159 } 160 } 161 162 static int cyberjack_open(struct tty_struct *tty, 163 struct usb_serial_port *port) 164 { 165 struct cyberjack_private *priv; 166 unsigned long flags; 167 int result = 0; 168 169 dev_dbg(&port->dev, "%s - usb_clear_halt\n", __func__); 170 usb_clear_halt(port->serial->dev, port->write_urb->pipe); 171 172 priv = usb_get_serial_port_data(port); 173 spin_lock_irqsave(&priv->lock, flags); 174 priv->rdtodo = 0; 175 priv->wrfilled = 0; 176 priv->wrsent = 0; 177 spin_unlock_irqrestore(&priv->lock, flags); 178 179 return result; 180 } 181 182 static void cyberjack_close(struct usb_serial_port *port) 183 { 184 if (port->serial->dev) { 185 /* shutdown any bulk reads that might be going on */ 186 usb_kill_urb(port->write_urb); 187 usb_kill_urb(port->read_urb); 188 } 189 } 190 191 static int cyberjack_write(struct tty_struct *tty, 192 struct usb_serial_port *port, const unsigned char *buf, int count) 193 { 194 struct device *dev = &port->dev; 195 struct cyberjack_private *priv = usb_get_serial_port_data(port); 196 unsigned long flags; 197 int result; 198 int wrexpected; 199 200 if (count == 0) { 201 dev_dbg(dev, "%s - write request of 0 bytes\n", __func__); 202 return 0; 203 } 204 205 if (!test_and_clear_bit(0, &port->write_urbs_free)) { 206 dev_dbg(dev, "%s - already writing\n", __func__); 207 return 0; 208 } 209 210 spin_lock_irqsave(&priv->lock, flags); 211 212 if (count+priv->wrfilled > sizeof(priv->wrbuf)) { 213 /* To much data for buffer. Reset buffer. */ 214 priv->wrfilled = 0; 215 spin_unlock_irqrestore(&priv->lock, flags); 216 set_bit(0, &port->write_urbs_free); 217 return 0; 218 } 219 220 /* Copy data */ 221 memcpy(priv->wrbuf + priv->wrfilled, buf, count); 222 223 usb_serial_debug_data(dev, __func__, count, priv->wrbuf + priv->wrfilled); 224 priv->wrfilled += count; 225 226 if (priv->wrfilled >= 3) { 227 wrexpected = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3; 228 dev_dbg(dev, "%s - expected data: %d\n", __func__, wrexpected); 229 } else 230 wrexpected = sizeof(priv->wrbuf); 231 232 if (priv->wrfilled >= wrexpected) { 233 /* We have enough data to begin transmission */ 234 int length; 235 236 dev_dbg(dev, "%s - transmitting data (frame 1)\n", __func__); 237 length = (wrexpected > port->bulk_out_size) ? 238 port->bulk_out_size : wrexpected; 239 240 memcpy(port->write_urb->transfer_buffer, priv->wrbuf, length); 241 priv->wrsent = length; 242 243 /* set up our urb */ 244 port->write_urb->transfer_buffer_length = length; 245 246 /* send the data out the bulk port */ 247 result = usb_submit_urb(port->write_urb, GFP_ATOMIC); 248 if (result) { 249 dev_err(&port->dev, 250 "%s - failed submitting write urb, error %d", 251 __func__, result); 252 /* Throw away data. No better idea what to do with it. */ 253 priv->wrfilled = 0; 254 priv->wrsent = 0; 255 spin_unlock_irqrestore(&priv->lock, flags); 256 set_bit(0, &port->write_urbs_free); 257 return 0; 258 } 259 260 dev_dbg(dev, "%s - priv->wrsent=%d\n", __func__, priv->wrsent); 261 dev_dbg(dev, "%s - priv->wrfilled=%d\n", __func__, priv->wrfilled); 262 263 if (priv->wrsent >= priv->wrfilled) { 264 dev_dbg(dev, "%s - buffer cleaned\n", __func__); 265 memset(priv->wrbuf, 0, sizeof(priv->wrbuf)); 266 priv->wrfilled = 0; 267 priv->wrsent = 0; 268 } 269 } 270 271 spin_unlock_irqrestore(&priv->lock, flags); 272 273 return count; 274 } 275 276 static int cyberjack_write_room(struct tty_struct *tty) 277 { 278 /* FIXME: .... */ 279 return CYBERJACK_LOCAL_BUF_SIZE; 280 } 281 282 static void cyberjack_read_int_callback(struct urb *urb) 283 { 284 struct usb_serial_port *port = urb->context; 285 struct cyberjack_private *priv = usb_get_serial_port_data(port); 286 struct device *dev = &port->dev; 287 unsigned char *data = urb->transfer_buffer; 288 int status = urb->status; 289 int result; 290 291 /* the urb might have been killed. */ 292 if (status) 293 return; 294 295 usb_serial_debug_data(dev, __func__, urb->actual_length, data); 296 297 /* React only to interrupts signaling a bulk_in transfer */ 298 if (urb->actual_length == 4 && data[0] == 0x01) { 299 short old_rdtodo; 300 301 /* This is a announcement of coming bulk_ins. */ 302 unsigned short size = ((unsigned short)data[3]<<8)+data[2]+3; 303 304 spin_lock(&priv->lock); 305 306 old_rdtodo = priv->rdtodo; 307 308 if (old_rdtodo + size < old_rdtodo) { 309 dev_dbg(dev, "To many bulk_in urbs to do.\n"); 310 spin_unlock(&priv->lock); 311 goto resubmit; 312 } 313 314 /* "+=" is probably more fault tollerant than "=" */ 315 priv->rdtodo += size; 316 317 dev_dbg(dev, "%s - rdtodo: %d\n", __func__, priv->rdtodo); 318 319 spin_unlock(&priv->lock); 320 321 if (!old_rdtodo) { 322 result = usb_submit_urb(port->read_urb, GFP_ATOMIC); 323 if (result) 324 dev_err(dev, "%s - failed resubmitting read urb, error %d\n", 325 __func__, result); 326 dev_dbg(dev, "%s - usb_submit_urb(read urb)\n", __func__); 327 } 328 } 329 330 resubmit: 331 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC); 332 if (result) 333 dev_err(&port->dev, "usb_submit_urb(read int) failed\n"); 334 dev_dbg(dev, "%s - usb_submit_urb(int urb)\n", __func__); 335 } 336 337 static void cyberjack_read_bulk_callback(struct urb *urb) 338 { 339 struct usb_serial_port *port = urb->context; 340 struct cyberjack_private *priv = usb_get_serial_port_data(port); 341 struct device *dev = &port->dev; 342 struct tty_struct *tty; 343 unsigned char *data = urb->transfer_buffer; 344 short todo; 345 int result; 346 int status = urb->status; 347 348 usb_serial_debug_data(dev, __func__, urb->actual_length, data); 349 if (status) { 350 dev_dbg(dev, "%s - nonzero read bulk status received: %d\n", 351 __func__, status); 352 return; 353 } 354 355 tty = tty_port_tty_get(&port->port); 356 if (!tty) { 357 dev_dbg(dev, "%s - ignoring since device not open\n", __func__); 358 return; 359 } 360 if (urb->actual_length) { 361 tty_insert_flip_string(tty, data, urb->actual_length); 362 tty_flip_buffer_push(tty); 363 } 364 tty_kref_put(tty); 365 366 spin_lock(&priv->lock); 367 368 /* Reduce urbs to do by one. */ 369 priv->rdtodo -= urb->actual_length; 370 /* Just to be sure */ 371 if (priv->rdtodo < 0) 372 priv->rdtodo = 0; 373 todo = priv->rdtodo; 374 375 spin_unlock(&priv->lock); 376 377 dev_dbg(dev, "%s - rdtodo: %d\n", __func__, todo); 378 379 /* Continue to read if we have still urbs to do. */ 380 if (todo /* || (urb->actual_length==port->bulk_in_endpointAddress)*/) { 381 result = usb_submit_urb(port->read_urb, GFP_ATOMIC); 382 if (result) 383 dev_err(dev, "%s - failed resubmitting read urb, error %d\n", 384 __func__, result); 385 dev_dbg(dev, "%s - usb_submit_urb(read urb)\n", __func__); 386 } 387 } 388 389 static void cyberjack_write_bulk_callback(struct urb *urb) 390 { 391 struct usb_serial_port *port = urb->context; 392 struct cyberjack_private *priv = usb_get_serial_port_data(port); 393 struct device *dev = &port->dev; 394 int status = urb->status; 395 396 set_bit(0, &port->write_urbs_free); 397 if (status) { 398 dev_dbg(dev, "%s - nonzero write bulk status received: %d\n", 399 __func__, status); 400 return; 401 } 402 403 spin_lock(&priv->lock); 404 405 /* only do something if we have more data to send */ 406 if (priv->wrfilled) { 407 int length, blksize, result; 408 409 dev_dbg(dev, "%s - transmitting data (frame n)\n", __func__); 410 411 length = ((priv->wrfilled - priv->wrsent) > port->bulk_out_size) ? 412 port->bulk_out_size : (priv->wrfilled - priv->wrsent); 413 414 memcpy(port->write_urb->transfer_buffer, 415 priv->wrbuf + priv->wrsent, length); 416 priv->wrsent += length; 417 418 /* set up our urb */ 419 port->write_urb->transfer_buffer_length = length; 420 421 /* send the data out the bulk port */ 422 result = usb_submit_urb(port->write_urb, GFP_ATOMIC); 423 if (result) { 424 dev_err(dev, "%s - failed submitting write urb, error %d\n", 425 __func__, result); 426 /* Throw away data. No better idea what to do with it. */ 427 priv->wrfilled = 0; 428 priv->wrsent = 0; 429 goto exit; 430 } 431 432 dev_dbg(dev, "%s - priv->wrsent=%d\n", __func__, priv->wrsent); 433 dev_dbg(dev, "%s - priv->wrfilled=%d\n", __func__, priv->wrfilled); 434 435 blksize = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3; 436 437 if (priv->wrsent >= priv->wrfilled || 438 priv->wrsent >= blksize) { 439 dev_dbg(dev, "%s - buffer cleaned\n", __func__); 440 memset(priv->wrbuf, 0, sizeof(priv->wrbuf)); 441 priv->wrfilled = 0; 442 priv->wrsent = 0; 443 } 444 } 445 446 exit: 447 spin_unlock(&priv->lock); 448 usb_serial_port_softint(port); 449 } 450 451 module_usb_serial_driver(serial_drivers, id_table); 452 453 MODULE_AUTHOR(DRIVER_AUTHOR); 454 MODULE_DESCRIPTION(DRIVER_DESC); 455 MODULE_VERSION(DRIVER_VERSION); 456 MODULE_LICENSE("GPL"); 457