cyberjack.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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. #include <linux/kernel.h>
  30. #include <linux/errno.h>
  31. #include <linux/init.h>
  32. #include <linux/slab.h>
  33. #include <linux/tty.h>
  34. #include <linux/tty_driver.h>
  35. #include <linux/tty_flip.h>
  36. #include <linux/module.h>
  37. #include <linux/spinlock.h>
  38. #include <asm/uaccess.h>
  39. #include <linux/usb.h>
  40. #include <linux/usb/serial.h>
  41. #define CYBERJACK_LOCAL_BUF_SIZE 32
  42. static int debug;
  43. /*
  44. * Version Information
  45. */
  46. #define DRIVER_VERSION "v1.01"
  47. #define DRIVER_AUTHOR "Matthias Bruestle"
  48. #define DRIVER_DESC "REINER SCT cyberJack pinpad/e-com USB Chipcard Reader Driver"
  49. #define CYBERJACK_VENDOR_ID 0x0C4B
  50. #define CYBERJACK_PRODUCT_ID 0x0100
  51. /* Function prototypes */
  52. static int cyberjack_startup (struct usb_serial *serial);
  53. static void cyberjack_shutdown (struct usb_serial *serial);
  54. static int cyberjack_open (struct usb_serial_port *port, struct file *filp);
  55. static void cyberjack_close (struct usb_serial_port *port, struct file *filp);
  56. static int cyberjack_write (struct usb_serial_port *port, const unsigned char *buf, int count);
  57. static int cyberjack_write_room( struct usb_serial_port *port );
  58. static void cyberjack_read_int_callback (struct urb *urb);
  59. static void cyberjack_read_bulk_callback (struct urb *urb);
  60. static void cyberjack_write_bulk_callback (struct urb *urb);
  61. static struct usb_device_id id_table [] = {
  62. { USB_DEVICE(CYBERJACK_VENDOR_ID, CYBERJACK_PRODUCT_ID) },
  63. { } /* Terminating entry */
  64. };
  65. MODULE_DEVICE_TABLE (usb, id_table);
  66. static struct usb_driver cyberjack_driver = {
  67. .name = "cyberjack",
  68. .probe = usb_serial_probe,
  69. .disconnect = usb_serial_disconnect,
  70. .id_table = id_table,
  71. .no_dynamic_id = 1,
  72. };
  73. static struct usb_serial_driver cyberjack_device = {
  74. .driver = {
  75. .owner = THIS_MODULE,
  76. .name = "cyberjack",
  77. },
  78. .description = "Reiner SCT Cyberjack USB card reader",
  79. .id_table = id_table,
  80. .num_interrupt_in = 1,
  81. .num_bulk_in = 1,
  82. .num_bulk_out = 1,
  83. .num_ports = 1,
  84. .attach = cyberjack_startup,
  85. .shutdown = cyberjack_shutdown,
  86. .open = cyberjack_open,
  87. .close = cyberjack_close,
  88. .write = cyberjack_write,
  89. .write_room = cyberjack_write_room,
  90. .read_int_callback = cyberjack_read_int_callback,
  91. .read_bulk_callback = cyberjack_read_bulk_callback,
  92. .write_bulk_callback = cyberjack_write_bulk_callback,
  93. };
  94. struct cyberjack_private {
  95. spinlock_t lock; /* Lock for SMP */
  96. short rdtodo; /* Bytes still to read */
  97. unsigned char wrbuf[5*64]; /* Buffer for collecting data to write */
  98. short wrfilled; /* Overall data size we already got */
  99. short wrsent; /* Data already sent */
  100. };
  101. /* do some startup allocations not currently performed by usb_serial_probe() */
  102. static int cyberjack_startup (struct usb_serial *serial)
  103. {
  104. struct cyberjack_private *priv;
  105. int i;
  106. dbg("%s", __FUNCTION__);
  107. /* allocate the private data structure */
  108. priv = kmalloc(sizeof(struct cyberjack_private), GFP_KERNEL);
  109. if (!priv)
  110. return -ENOMEM;
  111. /* set initial values */
  112. spin_lock_init(&priv->lock);
  113. priv->rdtodo = 0;
  114. priv->wrfilled = 0;
  115. priv->wrsent = 0;
  116. usb_set_serial_port_data(serial->port[0], priv);
  117. init_waitqueue_head(&serial->port[0]->write_wait);
  118. for (i = 0; i < serial->num_ports; ++i) {
  119. int result;
  120. serial->port[i]->interrupt_in_urb->dev = serial->dev;
  121. result = usb_submit_urb(serial->port[i]->interrupt_in_urb,
  122. GFP_KERNEL);
  123. if (result)
  124. err(" usb_submit_urb(read int) failed");
  125. dbg("%s - usb_submit_urb(int urb)", __FUNCTION__);
  126. }
  127. return( 0 );
  128. }
  129. static void cyberjack_shutdown (struct usb_serial *serial)
  130. {
  131. int i;
  132. dbg("%s", __FUNCTION__);
  133. for (i=0; i < serial->num_ports; ++i) {
  134. usb_kill_urb(serial->port[i]->interrupt_in_urb);
  135. /* My special items, the standard routines free my urbs */
  136. kfree(usb_get_serial_port_data(serial->port[i]));
  137. usb_set_serial_port_data(serial->port[i], NULL);
  138. }
  139. }
  140. static int cyberjack_open (struct usb_serial_port *port, struct file *filp)
  141. {
  142. struct cyberjack_private *priv;
  143. unsigned long flags;
  144. int result = 0;
  145. dbg("%s - port %d", __FUNCTION__, port->number);
  146. dbg("%s - usb_clear_halt", __FUNCTION__ );
  147. usb_clear_halt(port->serial->dev, port->write_urb->pipe);
  148. /* force low_latency on so that our tty_push actually forces
  149. * the data through, otherwise it is scheduled, and with high
  150. * data rates (like with OHCI) data can get lost.
  151. */
  152. port->tty->low_latency = 1;
  153. priv = usb_get_serial_port_data(port);
  154. spin_lock_irqsave(&priv->lock, flags);
  155. priv->rdtodo = 0;
  156. priv->wrfilled = 0;
  157. priv->wrsent = 0;
  158. spin_unlock_irqrestore(&priv->lock, flags);
  159. return result;
  160. }
  161. static void cyberjack_close (struct usb_serial_port *port, struct file *filp)
  162. {
  163. dbg("%s - port %d", __FUNCTION__, port->number);
  164. if (port->serial->dev) {
  165. /* shutdown any bulk reads that might be going on */
  166. usb_kill_urb(port->write_urb);
  167. usb_kill_urb(port->read_urb);
  168. }
  169. }
  170. static int cyberjack_write (struct usb_serial_port *port, const unsigned char *buf, int count)
  171. {
  172. struct usb_serial *serial = port->serial;
  173. struct cyberjack_private *priv = usb_get_serial_port_data(port);
  174. unsigned long flags;
  175. int result;
  176. int wrexpected;
  177. dbg("%s - port %d", __FUNCTION__, port->number);
  178. if (count == 0) {
  179. dbg("%s - write request of 0 bytes", __FUNCTION__);
  180. return (0);
  181. }
  182. spin_lock_bh(&port->lock);
  183. if (port->write_urb_busy) {
  184. spin_unlock_bh(&port->lock);
  185. dbg("%s - already writing", __FUNCTION__);
  186. return 0;
  187. }
  188. port->write_urb_busy = 1;
  189. spin_unlock_bh(&port->lock);
  190. spin_lock_irqsave(&priv->lock, flags);
  191. if( (count+priv->wrfilled)>sizeof(priv->wrbuf) ) {
  192. /* To much data for buffer. Reset buffer. */
  193. priv->wrfilled=0;
  194. spin_unlock_irqrestore(&priv->lock, flags);
  195. port->write_urb_busy = 0;
  196. return (0);
  197. }
  198. /* Copy data */
  199. memcpy (priv->wrbuf+priv->wrfilled, buf, count);
  200. usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count,
  201. priv->wrbuf+priv->wrfilled);
  202. priv->wrfilled += count;
  203. if( priv->wrfilled >= 3 ) {
  204. wrexpected = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
  205. dbg("%s - expected data: %d", __FUNCTION__, wrexpected);
  206. } else {
  207. wrexpected = sizeof(priv->wrbuf);
  208. }
  209. if( priv->wrfilled >= wrexpected ) {
  210. /* We have enough data to begin transmission */
  211. int length;
  212. dbg("%s - transmitting data (frame 1)", __FUNCTION__);
  213. length = (wrexpected > port->bulk_out_size) ? port->bulk_out_size : wrexpected;
  214. memcpy (port->write_urb->transfer_buffer, priv->wrbuf, length );
  215. priv->wrsent=length;
  216. /* set up our urb */
  217. usb_fill_bulk_urb(port->write_urb, serial->dev,
  218. usb_sndbulkpipe(serial->dev, port->bulk_out_endpointAddress),
  219. port->write_urb->transfer_buffer, length,
  220. ((serial->type->write_bulk_callback) ?
  221. serial->type->write_bulk_callback :
  222. cyberjack_write_bulk_callback),
  223. port);
  224. /* send the data out the bulk port */
  225. result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
  226. if (result) {
  227. err("%s - failed submitting write urb, error %d", __FUNCTION__, result);
  228. /* Throw away data. No better idea what to do with it. */
  229. priv->wrfilled=0;
  230. priv->wrsent=0;
  231. spin_unlock_irqrestore(&priv->lock, flags);
  232. port->write_urb_busy = 0;
  233. return 0;
  234. }
  235. dbg("%s - priv->wrsent=%d", __FUNCTION__,priv->wrsent);
  236. dbg("%s - priv->wrfilled=%d", __FUNCTION__,priv->wrfilled);
  237. if( priv->wrsent>=priv->wrfilled ) {
  238. dbg("%s - buffer cleaned", __FUNCTION__);
  239. memset( priv->wrbuf, 0, sizeof(priv->wrbuf) );
  240. priv->wrfilled=0;
  241. priv->wrsent=0;
  242. }
  243. }
  244. spin_unlock_irqrestore(&priv->lock, flags);
  245. return (count);
  246. }
  247. static int cyberjack_write_room( struct usb_serial_port *port )
  248. {
  249. return CYBERJACK_LOCAL_BUF_SIZE;
  250. }
  251. static void cyberjack_read_int_callback( struct urb *urb )
  252. {
  253. struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
  254. struct cyberjack_private *priv = usb_get_serial_port_data(port);
  255. unsigned char *data = urb->transfer_buffer;
  256. int result;
  257. dbg("%s - port %d", __FUNCTION__, port->number);
  258. /* the urb might have been killed. */
  259. if (urb->status)
  260. return;
  261. usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
  262. /* React only to interrupts signaling a bulk_in transfer */
  263. if( (urb->actual_length==4) && (data[0]==0x01) ) {
  264. short old_rdtodo;
  265. int result;
  266. /* This is a announcement of coming bulk_ins. */
  267. unsigned short size = ((unsigned short)data[3]<<8)+data[2]+3;
  268. spin_lock(&priv->lock);
  269. old_rdtodo = priv->rdtodo;
  270. if( (old_rdtodo+size)<(old_rdtodo) ) {
  271. dbg( "To many bulk_in urbs to do." );
  272. spin_unlock(&priv->lock);
  273. goto resubmit;
  274. }
  275. /* "+=" is probably more fault tollerant than "=" */
  276. priv->rdtodo += size;
  277. dbg("%s - rdtodo: %d", __FUNCTION__, priv->rdtodo);
  278. spin_unlock(&priv->lock);
  279. if( !old_rdtodo ) {
  280. port->read_urb->dev = port->serial->dev;
  281. result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  282. if( result )
  283. err("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);
  284. dbg("%s - usb_submit_urb(read urb)", __FUNCTION__);
  285. }
  286. }
  287. resubmit:
  288. port->interrupt_in_urb->dev = port->serial->dev;
  289. result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
  290. if (result)
  291. err(" usb_submit_urb(read int) failed");
  292. dbg("%s - usb_submit_urb(int urb)", __FUNCTION__);
  293. }
  294. static void cyberjack_read_bulk_callback (struct urb *urb)
  295. {
  296. struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
  297. struct cyberjack_private *priv = usb_get_serial_port_data(port);
  298. struct tty_struct *tty;
  299. unsigned char *data = urb->transfer_buffer;
  300. short todo;
  301. int result;
  302. dbg("%s - port %d", __FUNCTION__, port->number);
  303. usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
  304. if (urb->status) {
  305. dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
  306. return;
  307. }
  308. tty = port->tty;
  309. if (!tty) {
  310. dbg("%s - ignoring since device not open\n", __FUNCTION__);
  311. return;
  312. }
  313. if (urb->actual_length) {
  314. tty_buffer_request_room(tty, urb->actual_length);
  315. tty_insert_flip_string(tty, data, urb->actual_length);
  316. tty_flip_buffer_push(tty);
  317. }
  318. spin_lock(&priv->lock);
  319. /* Reduce urbs to do by one. */
  320. priv->rdtodo-=urb->actual_length;
  321. /* Just to be sure */
  322. if ( priv->rdtodo<0 ) priv->rdtodo = 0;
  323. todo = priv->rdtodo;
  324. spin_unlock(&priv->lock);
  325. dbg("%s - rdtodo: %d", __FUNCTION__, todo);
  326. /* Continue to read if we have still urbs to do. */
  327. if( todo /* || (urb->actual_length==port->bulk_in_endpointAddress)*/ ) {
  328. port->read_urb->dev = port->serial->dev;
  329. result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  330. if (result)
  331. err("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);
  332. dbg("%s - usb_submit_urb(read urb)", __FUNCTION__);
  333. }
  334. }
  335. static void cyberjack_write_bulk_callback (struct urb *urb)
  336. {
  337. struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
  338. struct cyberjack_private *priv = usb_get_serial_port_data(port);
  339. dbg("%s - port %d", __FUNCTION__, port->number);
  340. port->write_urb_busy = 0;
  341. if (urb->status) {
  342. dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
  343. return;
  344. }
  345. spin_lock(&priv->lock);
  346. /* only do something if we have more data to send */
  347. if( priv->wrfilled ) {
  348. int length, blksize, result;
  349. dbg("%s - transmitting data (frame n)", __FUNCTION__);
  350. length = ((priv->wrfilled - priv->wrsent) > port->bulk_out_size) ?
  351. port->bulk_out_size : (priv->wrfilled - priv->wrsent);
  352. memcpy (port->write_urb->transfer_buffer, priv->wrbuf + priv->wrsent,
  353. length );
  354. priv->wrsent+=length;
  355. /* set up our urb */
  356. usb_fill_bulk_urb(port->write_urb, port->serial->dev,
  357. usb_sndbulkpipe(port->serial->dev, port->bulk_out_endpointAddress),
  358. port->write_urb->transfer_buffer, length,
  359. ((port->serial->type->write_bulk_callback) ?
  360. port->serial->type->write_bulk_callback :
  361. cyberjack_write_bulk_callback),
  362. port);
  363. /* send the data out the bulk port */
  364. result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
  365. if (result) {
  366. err("%s - failed submitting write urb, error %d", __FUNCTION__, result);
  367. /* Throw away data. No better idea what to do with it. */
  368. priv->wrfilled=0;
  369. priv->wrsent=0;
  370. goto exit;
  371. }
  372. dbg("%s - priv->wrsent=%d", __FUNCTION__,priv->wrsent);
  373. dbg("%s - priv->wrfilled=%d", __FUNCTION__,priv->wrfilled);
  374. blksize = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
  375. if( (priv->wrsent>=priv->wrfilled) || (priv->wrsent>=blksize) ) {
  376. dbg("%s - buffer cleaned", __FUNCTION__);
  377. memset( priv->wrbuf, 0, sizeof(priv->wrbuf) );
  378. priv->wrfilled=0;
  379. priv->wrsent=0;
  380. }
  381. }
  382. exit:
  383. spin_unlock(&priv->lock);
  384. usb_serial_port_softint(port);
  385. }
  386. static int __init cyberjack_init (void)
  387. {
  388. int retval;
  389. retval = usb_serial_register(&cyberjack_device);
  390. if (retval)
  391. goto failed_usb_serial_register;
  392. retval = usb_register(&cyberjack_driver);
  393. if (retval)
  394. goto failed_usb_register;
  395. info(DRIVER_VERSION " " DRIVER_AUTHOR);
  396. info(DRIVER_DESC);
  397. return 0;
  398. failed_usb_register:
  399. usb_serial_deregister(&cyberjack_device);
  400. failed_usb_serial_register:
  401. return retval;
  402. }
  403. static void __exit cyberjack_exit (void)
  404. {
  405. usb_deregister (&cyberjack_driver);
  406. usb_serial_deregister (&cyberjack_device);
  407. }
  408. module_init(cyberjack_init);
  409. module_exit(cyberjack_exit);
  410. MODULE_AUTHOR( DRIVER_AUTHOR );
  411. MODULE_DESCRIPTION( DRIVER_DESC );
  412. MODULE_VERSION( DRIVER_VERSION );
  413. MODULE_LICENSE("GPL");
  414. module_param(debug, bool, S_IRUGO | S_IWUSR);
  415. MODULE_PARM_DESC(debug, "Debug enabled or not");