cyberjack.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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. spin_lock(&port->lock);
  182. if (port->write_urb_busy) {
  183. spin_unlock(&port->lock);
  184. dbg("%s - already writing", __FUNCTION__);
  185. return 0;
  186. }
  187. port->write_urb_busy = 1;
  188. spin_unlock(&port->lock);
  189. spin_lock_irqsave(&priv->lock, flags);
  190. if( (count+priv->wrfilled)>sizeof(priv->wrbuf) ) {
  191. /* To much data for buffer. Reset buffer. */
  192. priv->wrfilled=0;
  193. spin_unlock_irqrestore(&priv->lock, flags);
  194. port->write_urb_busy = 0;
  195. return (0);
  196. }
  197. /* Copy data */
  198. memcpy (priv->wrbuf+priv->wrfilled, buf, count);
  199. usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count,
  200. priv->wrbuf+priv->wrfilled);
  201. priv->wrfilled += count;
  202. if( priv->wrfilled >= 3 ) {
  203. wrexpected = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
  204. dbg("%s - expected data: %d", __FUNCTION__, wrexpected);
  205. } else {
  206. wrexpected = sizeof(priv->wrbuf);
  207. }
  208. if( priv->wrfilled >= wrexpected ) {
  209. /* We have enough data to begin transmission */
  210. int length;
  211. dbg("%s - transmitting data (frame 1)", __FUNCTION__);
  212. length = (wrexpected > port->bulk_out_size) ? port->bulk_out_size : wrexpected;
  213. memcpy (port->write_urb->transfer_buffer, priv->wrbuf, length );
  214. priv->wrsent=length;
  215. /* set up our urb */
  216. usb_fill_bulk_urb(port->write_urb, serial->dev,
  217. usb_sndbulkpipe(serial->dev, port->bulk_out_endpointAddress),
  218. port->write_urb->transfer_buffer, length,
  219. ((serial->type->write_bulk_callback) ?
  220. serial->type->write_bulk_callback :
  221. cyberjack_write_bulk_callback),
  222. port);
  223. /* send the data out the bulk port */
  224. result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
  225. if (result) {
  226. err("%s - failed submitting write urb, error %d", __FUNCTION__, result);
  227. /* Throw away data. No better idea what to do with it. */
  228. priv->wrfilled=0;
  229. priv->wrsent=0;
  230. spin_unlock_irqrestore(&priv->lock, flags);
  231. port->write_urb_busy = 0;
  232. return 0;
  233. }
  234. dbg("%s - priv->wrsent=%d", __FUNCTION__,priv->wrsent);
  235. dbg("%s - priv->wrfilled=%d", __FUNCTION__,priv->wrfilled);
  236. if( priv->wrsent>=priv->wrfilled ) {
  237. dbg("%s - buffer cleaned", __FUNCTION__);
  238. memset( priv->wrbuf, 0, sizeof(priv->wrbuf) );
  239. priv->wrfilled=0;
  240. priv->wrsent=0;
  241. }
  242. }
  243. spin_unlock_irqrestore(&priv->lock, flags);
  244. return (count);
  245. }
  246. static int cyberjack_write_room( struct usb_serial_port *port )
  247. {
  248. return CYBERJACK_LOCAL_BUF_SIZE;
  249. }
  250. static void cyberjack_read_int_callback( struct urb *urb, struct pt_regs *regs )
  251. {
  252. struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
  253. struct cyberjack_private *priv = usb_get_serial_port_data(port);
  254. unsigned char *data = urb->transfer_buffer;
  255. int result;
  256. dbg("%s - port %d", __FUNCTION__, port->number);
  257. /* the urb might have been killed. */
  258. if (urb->status)
  259. return;
  260. usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
  261. /* React only to interrupts signaling a bulk_in transfer */
  262. if( (urb->actual_length==4) && (data[0]==0x01) ) {
  263. short old_rdtodo;
  264. int result;
  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", __FUNCTION__, 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", __FUNCTION__, result);
  283. dbg("%s - usb_submit_urb(read urb)", __FUNCTION__);
  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)", __FUNCTION__);
  292. }
  293. static void cyberjack_read_bulk_callback (struct urb *urb, struct pt_regs *regs)
  294. {
  295. struct usb_serial_port *port = (struct usb_serial_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 i;
  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. for (i = 0; i < urb->actual_length ; ++i) {
  315. /* if we insert more than TTY_FLIPBUF_SIZE characters, we drop them. */
  316. if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
  317. tty_flip_buffer_push(tty);
  318. }
  319. /* this doesn't actually push the data through unless tty->low_latency is set */
  320. tty_insert_flip_char(tty, data[i], 0);
  321. }
  322. tty_flip_buffer_push(tty);
  323. }
  324. spin_lock(&priv->lock);
  325. /* Reduce urbs to do by one. */
  326. priv->rdtodo-=urb->actual_length;
  327. /* Just to be sure */
  328. if ( priv->rdtodo<0 ) priv->rdtodo = 0;
  329. todo = priv->rdtodo;
  330. spin_unlock(&priv->lock);
  331. dbg("%s - rdtodo: %d", __FUNCTION__, todo);
  332. /* Continue to read if we have still urbs to do. */
  333. if( todo /* || (urb->actual_length==port->bulk_in_endpointAddress)*/ ) {
  334. port->read_urb->dev = port->serial->dev;
  335. result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  336. if (result)
  337. err("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);
  338. dbg("%s - usb_submit_urb(read urb)", __FUNCTION__);
  339. }
  340. }
  341. static void cyberjack_write_bulk_callback (struct urb *urb, struct pt_regs *regs)
  342. {
  343. struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
  344. struct cyberjack_private *priv = usb_get_serial_port_data(port);
  345. dbg("%s - port %d", __FUNCTION__, port->number);
  346. port->write_urb_busy = 0;
  347. if (urb->status) {
  348. dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
  349. return;
  350. }
  351. spin_lock(&priv->lock);
  352. /* only do something if we have more data to send */
  353. if( priv->wrfilled ) {
  354. int length, blksize, result;
  355. dbg("%s - transmitting data (frame n)", __FUNCTION__);
  356. length = ((priv->wrfilled - priv->wrsent) > port->bulk_out_size) ?
  357. port->bulk_out_size : (priv->wrfilled - priv->wrsent);
  358. memcpy (port->write_urb->transfer_buffer, priv->wrbuf + priv->wrsent,
  359. length );
  360. priv->wrsent+=length;
  361. /* set up our urb */
  362. usb_fill_bulk_urb(port->write_urb, port->serial->dev,
  363. usb_sndbulkpipe(port->serial->dev, port->bulk_out_endpointAddress),
  364. port->write_urb->transfer_buffer, length,
  365. ((port->serial->type->write_bulk_callback) ?
  366. port->serial->type->write_bulk_callback :
  367. cyberjack_write_bulk_callback),
  368. port);
  369. /* send the data out the bulk port */
  370. result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
  371. if (result) {
  372. err("%s - failed submitting write urb, error %d", __FUNCTION__, result);
  373. /* Throw away data. No better idea what to do with it. */
  374. priv->wrfilled=0;
  375. priv->wrsent=0;
  376. goto exit;
  377. }
  378. dbg("%s - priv->wrsent=%d", __FUNCTION__,priv->wrsent);
  379. dbg("%s - priv->wrfilled=%d", __FUNCTION__,priv->wrfilled);
  380. blksize = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
  381. if( (priv->wrsent>=priv->wrfilled) || (priv->wrsent>=blksize) ) {
  382. dbg("%s - buffer cleaned", __FUNCTION__);
  383. memset( priv->wrbuf, 0, sizeof(priv->wrbuf) );
  384. priv->wrfilled=0;
  385. priv->wrsent=0;
  386. }
  387. }
  388. exit:
  389. spin_unlock(&priv->lock);
  390. schedule_work(&port->work);
  391. }
  392. static int __init cyberjack_init (void)
  393. {
  394. int retval;
  395. retval = usb_serial_register(&cyberjack_device);
  396. if (retval)
  397. goto failed_usb_serial_register;
  398. retval = usb_register(&cyberjack_driver);
  399. if (retval)
  400. goto failed_usb_register;
  401. info(DRIVER_VERSION " " DRIVER_AUTHOR);
  402. info(DRIVER_DESC);
  403. return 0;
  404. failed_usb_register:
  405. usb_serial_deregister(&cyberjack_device);
  406. failed_usb_serial_register:
  407. return retval;
  408. }
  409. static void __exit cyberjack_exit (void)
  410. {
  411. usb_deregister (&cyberjack_driver);
  412. usb_serial_deregister (&cyberjack_device);
  413. }
  414. module_init(cyberjack_init);
  415. module_exit(cyberjack_exit);
  416. MODULE_AUTHOR( DRIVER_AUTHOR );
  417. MODULE_DESCRIPTION( DRIVER_DESC );
  418. MODULE_VERSION( DRIVER_VERSION );
  419. MODULE_LICENSE("GPL");
  420. module_param(debug, bool, S_IRUGO | S_IWUSR);
  421. MODULE_PARM_DESC(debug, "Debug enabled or not");