cyberjack.c 14 KB

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