Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 | /* * REINER SCT cyberJack pinpad/e-com USB Chipcard Reader Driver * * Copyright (C) 2001 REINER SCT * Author: Matthias Bruestle * * Contact: linux-usb@sii.li (see MAINTAINERS) * * This program is largely derived from work by the linux-usb group * and associated source files. Please see the usb/serial files for * individual credits and copyrights. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Thanks to Greg Kroah-Hartman (greg@kroah.com) for his help and * patience. * * In case of problems, please write to the contact e-mail address * mentioned above. */ #include <linux/config.h> #include <linux/kernel.h> #include <linux/sched.h> #include <linux/signal.h> #include <linux/errno.h> #include <linux/poll.h> #include <linux/init.h> #include <linux/slab.h> #include <linux/fcntl.h> #include <linux/tty.h> #include <linux/tty_driver.h> #include <linux/tty_flip.h> #include <linux/module.h> #include <linux/spinlock.h> #include <linux/usb.h> #ifdef CONFIG_USB_SERIAL_DEBUG static int debug = 1; #else static int debug; #endif #include "usb-serial.h" /* * Version Information */ #define DRIVER_VERSION "v1.0" #define DRIVER_AUTHOR "Matthias Bruestle" #define DRIVER_DESC "REINER SCT cyberJack pinpad/e-com USB Chipcard Reader Driver" #define CYBERJACK_VENDOR_ID 0x0C4B #define CYBERJACK_PRODUCT_ID 0x0100 /* Function prototypes */ static int cyberjack_startup (struct usb_serial *serial); static void cyberjack_shutdown (struct usb_serial *serial); static int cyberjack_open (struct usb_serial_port *port, struct file *filp); static void cyberjack_close (struct usb_serial_port *port, struct file *filp); static int cyberjack_write (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count); static void cyberjack_read_int_callback( struct urb *urb ); static void cyberjack_read_bulk_callback (struct urb *urb); static void cyberjack_write_bulk_callback (struct urb *urb); static __devinitdata struct usb_device_id id_table [] = { { USB_DEVICE(CYBERJACK_VENDOR_ID, CYBERJACK_PRODUCT_ID) }, { } /* Terminating entry */ }; MODULE_DEVICE_TABLE (usb, id_table); static struct usb_serial_device_type cyberjack_device = { owner: THIS_MODULE, name: "Reiner SCT Cyberjack USB card reader", id_table: id_table, num_interrupt_in: 1, num_bulk_in: 1, num_bulk_out: 1, num_ports: 1, startup: cyberjack_startup, shutdown: cyberjack_shutdown, open: cyberjack_open, close: cyberjack_close, write: cyberjack_write, read_int_callback: cyberjack_read_int_callback, read_bulk_callback: cyberjack_read_bulk_callback, write_bulk_callback: cyberjack_write_bulk_callback, }; struct cyberjack_private { short rdtodo; /* Bytes still to read */ unsigned char wrbuf[5*64]; /* Buffer for collecting data to write */ short wrfilled; /* Overall data size we already got */ short wrsent; /* Data akready sent */ }; /* do some startup allocations not currently performed by usb_serial_probe() */ static int cyberjack_startup (struct usb_serial *serial) { struct cyberjack_private *priv; dbg (__FUNCTION__); /* allocate the private data structure */ serial->port->private = kmalloc(sizeof(struct cyberjack_private), GFP_KERNEL); if (!serial->port->private) return (-1); /* error */ /* set initial values */ priv = (struct cyberjack_private *)serial->port->private; priv->rdtodo = 0; priv->wrfilled = 0; priv->wrsent = 0; init_waitqueue_head(&serial->port->write_wait); return( 0 ); } static void cyberjack_shutdown (struct usb_serial *serial) { int i; dbg (__FUNCTION__); for (i=0; i < serial->num_ports; ++i) { /* My special items, the standard routines free my urbs */ if (serial->port[i].private) kfree(serial->port[i].private); } } static int cyberjack_open (struct usb_serial_port *port, struct file *filp) { struct cyberjack_private *priv; int result = 0; if (port_paranoia_check (port, __FUNCTION__)) return -ENODEV; dbg(__FUNCTION__ " - port %d", port->number); /* force low_latency on so that our tty_push actually forces * the data through, otherwise it is scheduled, and with high * data rates (like with OHCI) data can get lost. */ port->tty->low_latency = 1; priv = (struct cyberjack_private *)port->private; priv->rdtodo = 0; priv->wrfilled = 0; priv->wrsent = 0; /* shutdown any bulk reads that might be going on */ usb_unlink_urb (port->write_urb); usb_unlink_urb (port->read_urb); usb_unlink_urb (port->interrupt_in_urb); port->interrupt_in_urb->dev = port->serial->dev; result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL); if (result) err(" usb_submit_urb(read int) failed"); dbg(__FUNCTION__ " - usb_submit_urb(int urb)"); return result; } static void cyberjack_close (struct usb_serial_port *port, struct file *filp) { dbg(__FUNCTION__ " - port %d", port->number); if (port->serial->dev) { /* shutdown any bulk reads that might be going on */ usb_unlink_urb (port->write_urb); usb_unlink_urb (port->read_urb); usb_unlink_urb (port->interrupt_in_urb); } } static int cyberjack_write (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count) { struct usb_serial *serial = port->serial; struct cyberjack_private *priv = (struct cyberjack_private *)port->private; int result; int wrexpected; dbg(__FUNCTION__ " - port %d", port->number); dbg(__FUNCTION__ " - from_user %d", from_user); if (count == 0) { dbg(__FUNCTION__ " - write request of 0 bytes"); return (0); } if (port->write_urb->status == -EINPROGRESS) { dbg (__FUNCTION__ " - already writing"); return (0); } if( (count+priv->wrfilled)>sizeof(priv->wrbuf) ) { /* To much data for buffer. Reset buffer. */ priv->wrfilled=0; return (0); } /* Copy data */ if (from_user) { if (copy_from_user(priv->wrbuf+priv->wrfilled, buf, count)) { return -EFAULT; } } else { memcpy (priv->wrbuf+priv->wrfilled, buf, count); } usb_serial_debug_data (__FILE__, __FUNCTION__, count, priv->wrbuf+priv->wrfilled); priv->wrfilled += count; if( priv->wrfilled >= 3 ) { wrexpected = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3; dbg(__FUNCTION__ " - expected data: %d", wrexpected); } else { wrexpected = sizeof(priv->wrbuf); } if( priv->wrfilled >= wrexpected ) { /* We have enough data to begin transmission */ int length; dbg(__FUNCTION__ " - transmitting data (frame 1)"); length = (wrexpected > port->bulk_out_size) ? port->bulk_out_size : wrexpected; memcpy (port->write_urb->transfer_buffer, priv->wrbuf, length ); priv->wrsent=length; /* set up our urb */ FILL_BULK_URB(port->write_urb, serial->dev, usb_sndbulkpipe(serial->dev, port->bulk_out_endpointAddress), port->write_urb->transfer_buffer, length, ((serial->type->write_bulk_callback) ? serial->type->write_bulk_callback : cyberjack_write_bulk_callback), port); /* send the data out the bulk port */ result = usb_submit_urb(port->write_urb, GFP_ATOMIC); if (result) { err(__FUNCTION__ " - failed submitting write urb, error %d", result); /* Throw away data. No better idea what to do with it. */ priv->wrfilled=0; priv->wrsent=0; return 0; } dbg(__FUNCTION__ " - priv->wrsent=%d",priv->wrsent); dbg(__FUNCTION__ " - priv->wrfilled=%d",priv->wrfilled); if( priv->wrsent>=priv->wrfilled ) { dbg(__FUNCTION__ " - buffer cleaned"); memset( priv->wrbuf, 0, sizeof(priv->wrbuf) ); priv->wrfilled=0; priv->wrsent=0; } } return (count); } static void cyberjack_read_int_callback( struct urb *urb ) { struct usb_serial_port *port = (struct usb_serial_port *)urb->context; struct cyberjack_private *priv = (struct cyberjack_private *)port->private; struct usb_serial *serial; unsigned char *data = urb->transfer_buffer; if (port_paranoia_check (port, __FUNCTION__)) return; dbg(__FUNCTION__ " - port %d", port->number); /* the urb might have been killed. */ if (urb->status) return; serial = port->serial; if (serial_paranoia_check (serial, __FUNCTION__)) return; usb_serial_debug_data (__FILE__, __FUNCTION__, urb->actual_length, data); /* React only to interrupts signaling a bulk_in transfer */ if( (urb->actual_length==4) && (data[0]==0x01) ) { short old_rdtodo = priv->rdtodo; int result; /* This is a announcement of comming bulk_ins. */ unsigned short size = ((unsigned short)data[3]<<8)+data[2]+3; if( (size>259) || (size==0) ) { dbg( "Bad announced bulk_in data length: %d", size ); /* Dunno what is most reliable to do here. */ /* return; */ } if( (old_rdtodo+size)<(old_rdtodo) ) { dbg( "To many bulk_in urbs to do." ); return; } /* "+=" is probably more fault tollerant than "=" */ priv->rdtodo += size; dbg(__FUNCTION__ " - rdtodo: %d", priv->rdtodo); if( !old_rdtodo ) { port->read_urb->dev = port->serial->dev; result = usb_submit_urb(port->read_urb, GFP_ATOMIC); if( result ) err(__FUNCTION__ " - failed resubmitting read urb, error %d", result); dbg(__FUNCTION__ " - usb_submit_urb(read urb)"); } } } static void cyberjack_read_bulk_callback (struct urb *urb) { struct usb_serial_port *port = (struct usb_serial_port *)urb->context; struct cyberjack_private *priv = (struct cyberjack_private *)port->private; struct usb_serial *serial = get_usb_serial (port, __FUNCTION__); struct tty_struct *tty; unsigned char *data = urb->transfer_buffer; int i; int result; dbg(__FUNCTION__ " - port %d", port->number); if (!serial) { dbg(__FUNCTION__ " - bad serial pointer, exiting"); return; } if (urb->status) { usb_serial_debug_data (__FILE__, __FUNCTION__, urb->actual_length, urb->transfer_buffer); dbg(__FUNCTION__ " - nonzero read bulk status received: %d", urb->status); return; } usb_serial_debug_data (__FILE__, __FUNCTION__, urb->actual_length, data); tty = port->tty; if (urb->actual_length) { for (i = 0; i < urb->actual_length ; ++i) { /* if we insert more than TTY_FLIPBUF_SIZE characters, we drop them. */ if(tty->flip.count >= TTY_FLIPBUF_SIZE) { tty_flip_buffer_push(tty); } /* this doesn't actually push the data through unless tty->low_latency is set */ tty_insert_flip_char(tty, data[i], 0); } tty_flip_buffer_push(tty); } /* Reduce urbs to do by one. */ priv->rdtodo-=urb->actual_length; /* Just to be sure */ if( priv->rdtodo<0 ) priv->rdtodo=0; dbg(__FUNCTION__ " - rdtodo: %d", priv->rdtodo); /* Continue to read if we have still urbs to do. */ if( priv->rdtodo /* || (urb->actual_length==port->bulk_in_endpointAddress)*/ ) { port->read_urb->dev = port->serial->dev; result = usb_submit_urb(port->read_urb, GFP_ATOMIC); if (result) err(__FUNCTION__ " - failed resubmitting read urb, error %d", result); dbg(__FUNCTION__ " - usb_submit_urb(read urb)"); } } static void cyberjack_write_bulk_callback (struct urb *urb) { struct usb_serial_port *port = (struct usb_serial_port *)urb->context; struct cyberjack_private *priv = (struct cyberjack_private *)port->private; struct usb_serial *serial = get_usb_serial (port, __FUNCTION__); dbg(__FUNCTION__ " - port %d", port->number); if (!serial) { dbg(__FUNCTION__ " - bad serial pointer, exiting"); return; } if (urb->status) { dbg(__FUNCTION__ " - nonzero write bulk status received: %d", urb->status); return; } /* only do something if we have more data to send */ if( priv->wrfilled ) { int length, blksize, result; if (port->write_urb->status == -EINPROGRESS) { dbg (__FUNCTION__ " - already writing"); return; } dbg(__FUNCTION__ " - transmitting data (frame n)"); length = ((priv->wrfilled - priv->wrsent) > port->bulk_out_size) ? port->bulk_out_size : (priv->wrfilled - priv->wrsent); memcpy (port->write_urb->transfer_buffer, priv->wrbuf + priv->wrsent, length ); priv->wrsent+=length; /* set up our urb */ FILL_BULK_URB(port->write_urb, serial->dev, usb_sndbulkpipe(serial->dev, port->bulk_out_endpointAddress), port->write_urb->transfer_buffer, length, ((serial->type->write_bulk_callback) ? serial->type->write_bulk_callback : cyberjack_write_bulk_callback), port); /* send the data out the bulk port */ result = usb_submit_urb(port->write_urb, GFP_ATOMIC); if (result) { err(__FUNCTION__ " - failed submitting write urb, error %d", result); /* Throw away data. No better idea what to do with it. */ priv->wrfilled=0; priv->wrsent=0; queue_task(&port->tqueue, &tq_immediate); mark_bh(IMMEDIATE_BH); return; } dbg(__FUNCTION__ " - priv->wrsent=%d",priv->wrsent); dbg(__FUNCTION__ " - priv->wrfilled=%d",priv->wrfilled); blksize = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3; if( (priv->wrsent>=priv->wrfilled) || (priv->wrsent>=blksize) ) { dbg(__FUNCTION__ " - buffer cleaned"); memset( priv->wrbuf, 0, sizeof(priv->wrbuf) ); priv->wrfilled=0; priv->wrsent=0; } queue_task(&port->tqueue, &tq_immediate); mark_bh(IMMEDIATE_BH); return; } queue_task(&port->tqueue, &tq_immediate); mark_bh(IMMEDIATE_BH); return; } static int __init cyberjack_init (void) { usb_serial_register (&cyberjack_device); info(DRIVER_VERSION " " DRIVER_AUTHOR); info(DRIVER_DESC); return 0; } static void __exit cyberjack_exit (void) { usb_serial_deregister (&cyberjack_device); } module_init(cyberjack_init); module_exit(cyberjack_exit); MODULE_AUTHOR( DRIVER_AUTHOR ); MODULE_DESCRIPTION( DRIVER_DESC ); MODULE_LICENSE("GPL"); MODULE_PARM(debug, "i"); MODULE_PARM_DESC(debug, "Debug enabled or not"); |