cyberjack.c 14 KB

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