cyberjack.c 14 KB

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