cyberjack.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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 <linux/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 tty_struct *tty,
  55. struct usb_serial_port *port, struct file *filp);
  56. static void cyberjack_close(struct tty_struct *tty,
  57. struct usb_serial_port *port, struct file *filp);
  58. static int cyberjack_write(struct tty_struct *tty,
  59. struct usb_serial_port *port, const unsigned char *buf, int count);
  60. static int cyberjack_write_room(struct tty_struct *tty);
  61. static void cyberjack_read_int_callback(struct urb *urb);
  62. static void cyberjack_read_bulk_callback(struct urb *urb);
  63. static void cyberjack_write_bulk_callback(struct urb *urb);
  64. static struct usb_device_id id_table [] = {
  65. { USB_DEVICE(CYBERJACK_VENDOR_ID, CYBERJACK_PRODUCT_ID) },
  66. { } /* Terminating entry */
  67. };
  68. MODULE_DEVICE_TABLE(usb, id_table);
  69. static struct usb_driver cyberjack_driver = {
  70. .name = "cyberjack",
  71. .probe = usb_serial_probe,
  72. .disconnect = usb_serial_disconnect,
  73. .id_table = id_table,
  74. .no_dynamic_id = 1,
  75. };
  76. static struct usb_serial_driver cyberjack_device = {
  77. .driver = {
  78. .owner = THIS_MODULE,
  79. .name = "cyberjack",
  80. },
  81. .description = "Reiner SCT Cyberjack USB card reader",
  82. .usb_driver = &cyberjack_driver,
  83. .id_table = id_table,
  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", __func__);
  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)", __func__);
  127. }
  128. return 0;
  129. }
  130. static void cyberjack_shutdown(struct usb_serial *serial)
  131. {
  132. int i;
  133. dbg("%s", __func__);
  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 tty_struct *tty,
  142. struct usb_serial_port *port, struct file *filp)
  143. {
  144. struct cyberjack_private *priv;
  145. unsigned long flags;
  146. int result = 0;
  147. dbg("%s - port %d", __func__, port->number);
  148. dbg("%s - usb_clear_halt", __func__);
  149. usb_clear_halt(port->serial->dev, port->write_urb->pipe);
  150. /* force low_latency on so that our tty_push actually forces
  151. * the data through, otherwise it is scheduled, and with high
  152. * data rates (like with OHCI) data can get lost.
  153. */
  154. if (tty)
  155. tty->low_latency = 1;
  156. priv = usb_get_serial_port_data(port);
  157. spin_lock_irqsave(&priv->lock, flags);
  158. priv->rdtodo = 0;
  159. priv->wrfilled = 0;
  160. priv->wrsent = 0;
  161. spin_unlock_irqrestore(&priv->lock, flags);
  162. return result;
  163. }
  164. static void cyberjack_close(struct tty_struct *tty,
  165. struct usb_serial_port *port, struct file *filp)
  166. {
  167. dbg("%s - port %d", __func__, port->number);
  168. if (port->serial->dev) {
  169. /* shutdown any bulk reads that might be going on */
  170. usb_kill_urb(port->write_urb);
  171. usb_kill_urb(port->read_urb);
  172. }
  173. }
  174. static int cyberjack_write(struct tty_struct *tty,
  175. struct usb_serial_port *port, const unsigned char *buf, int count)
  176. {
  177. struct usb_serial *serial = port->serial;
  178. struct cyberjack_private *priv = usb_get_serial_port_data(port);
  179. unsigned long flags;
  180. int result;
  181. int wrexpected;
  182. dbg("%s - port %d", __func__, port->number);
  183. if (count == 0) {
  184. dbg("%s - write request of 0 bytes", __func__);
  185. return 0;
  186. }
  187. spin_lock_bh(&port->lock);
  188. if (port->write_urb_busy) {
  189. spin_unlock_bh(&port->lock);
  190. dbg("%s - already writing", __func__);
  191. return 0;
  192. }
  193. port->write_urb_busy = 1;
  194. spin_unlock_bh(&port->lock);
  195. spin_lock_irqsave(&priv->lock, flags);
  196. if (count+priv->wrfilled > sizeof(priv->wrbuf)) {
  197. /* To much data for buffer. Reset buffer. */
  198. priv->wrfilled = 0;
  199. port->write_urb_busy = 0;
  200. spin_unlock_irqrestore(&priv->lock, flags);
  201. return 0;
  202. }
  203. /* Copy data */
  204. memcpy(priv->wrbuf + priv->wrfilled, buf, count);
  205. usb_serial_debug_data(debug, &port->dev, __func__, count,
  206. priv->wrbuf + priv->wrfilled);
  207. priv->wrfilled += count;
  208. if (priv->wrfilled >= 3) {
  209. wrexpected = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
  210. dbg("%s - expected data: %d", __func__, wrexpected);
  211. } else
  212. wrexpected = sizeof(priv->wrbuf);
  213. if (priv->wrfilled >= wrexpected) {
  214. /* We have enough data to begin transmission */
  215. int length;
  216. dbg("%s - transmitting data (frame 1)", __func__);
  217. length = (wrexpected > port->bulk_out_size) ?
  218. port->bulk_out_size : wrexpected;
  219. memcpy(port->write_urb->transfer_buffer, priv->wrbuf, length);
  220. priv->wrsent = length;
  221. /* set up our urb */
  222. usb_fill_bulk_urb(port->write_urb, serial->dev,
  223. usb_sndbulkpipe(serial->dev, port->bulk_out_endpointAddress),
  224. port->write_urb->transfer_buffer, length,
  225. ((serial->type->write_bulk_callback) ?
  226. serial->type->write_bulk_callback :
  227. cyberjack_write_bulk_callback),
  228. port);
  229. /* send the data out the bulk port */
  230. result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
  231. if (result) {
  232. err("%s - failed submitting write urb, error %d",
  233. __func__, result);
  234. /* Throw away data. No better idea what to do with it. */
  235. priv->wrfilled = 0;
  236. priv->wrsent = 0;
  237. spin_unlock_irqrestore(&priv->lock, flags);
  238. port->write_urb_busy = 0;
  239. return 0;
  240. }
  241. dbg("%s - priv->wrsent=%d", __func__, priv->wrsent);
  242. dbg("%s - priv->wrfilled=%d", __func__, priv->wrfilled);
  243. if (priv->wrsent >= priv->wrfilled) {
  244. dbg("%s - buffer cleaned", __func__);
  245. memset(priv->wrbuf, 0, sizeof(priv->wrbuf));
  246. priv->wrfilled = 0;
  247. priv->wrsent = 0;
  248. }
  249. }
  250. spin_unlock_irqrestore(&priv->lock, flags);
  251. return count;
  252. }
  253. static int cyberjack_write_room(struct tty_struct *tty)
  254. {
  255. /* FIXME: .... */
  256. return CYBERJACK_LOCAL_BUF_SIZE;
  257. }
  258. static void cyberjack_read_int_callback(struct urb *urb)
  259. {
  260. struct usb_serial_port *port = urb->context;
  261. struct cyberjack_private *priv = usb_get_serial_port_data(port);
  262. unsigned char *data = urb->transfer_buffer;
  263. int status = urb->status;
  264. int result;
  265. dbg("%s - port %d", __func__, port->number);
  266. /* the urb might have been killed. */
  267. if (status)
  268. return;
  269. usb_serial_debug_data(debug, &port->dev, __func__,
  270. urb->actual_length, data);
  271. /* React only to interrupts signaling a bulk_in transfer */
  272. if (urb->actual_length == 4 && data[0] == 0x01) {
  273. short old_rdtodo;
  274. /* This is a announcement of coming bulk_ins. */
  275. unsigned short size = ((unsigned short)data[3]<<8)+data[2]+3;
  276. spin_lock(&priv->lock);
  277. old_rdtodo = priv->rdtodo;
  278. if (old_rdtodo + size < old_rdtodo) {
  279. dbg("To many bulk_in urbs to do.");
  280. spin_unlock(&priv->lock);
  281. goto resubmit;
  282. }
  283. /* "+=" is probably more fault tollerant than "=" */
  284. priv->rdtodo += size;
  285. dbg("%s - rdtodo: %d", __func__, priv->rdtodo);
  286. spin_unlock(&priv->lock);
  287. if (!old_rdtodo) {
  288. port->read_urb->dev = port->serial->dev;
  289. result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  290. if (result)
  291. err("%s - failed resubmitting read urb, error %d", __func__, result);
  292. dbg("%s - usb_submit_urb(read urb)", __func__);
  293. }
  294. }
  295. resubmit:
  296. port->interrupt_in_urb->dev = port->serial->dev;
  297. result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
  298. if (result)
  299. err(" usb_submit_urb(read int) failed");
  300. dbg("%s - usb_submit_urb(int urb)", __func__);
  301. }
  302. static void cyberjack_read_bulk_callback(struct urb *urb)
  303. {
  304. struct usb_serial_port *port = urb->context;
  305. struct cyberjack_private *priv = usb_get_serial_port_data(port);
  306. struct tty_struct *tty;
  307. unsigned char *data = urb->transfer_buffer;
  308. short todo;
  309. int result;
  310. int status = urb->status;
  311. dbg("%s - port %d", __func__, port->number);
  312. usb_serial_debug_data(debug, &port->dev, __func__,
  313. urb->actual_length, data);
  314. if (status) {
  315. dbg("%s - nonzero read bulk status received: %d",
  316. __func__, status);
  317. return;
  318. }
  319. tty = port->port.tty;
  320. if (!tty) {
  321. dbg("%s - ignoring since device not open\n", __func__);
  322. return;
  323. }
  324. if (urb->actual_length) {
  325. tty_buffer_request_room(tty, urb->actual_length);
  326. tty_insert_flip_string(tty, data, urb->actual_length);
  327. tty_flip_buffer_push(tty);
  328. }
  329. spin_lock(&priv->lock);
  330. /* Reduce urbs to do by one. */
  331. priv->rdtodo -= urb->actual_length;
  332. /* Just to be sure */
  333. if (priv->rdtodo < 0)
  334. priv->rdtodo = 0;
  335. todo = priv->rdtodo;
  336. spin_unlock(&priv->lock);
  337. dbg("%s - rdtodo: %d", __func__, todo);
  338. /* Continue to read if we have still urbs to do. */
  339. if (todo /* || (urb->actual_length==port->bulk_in_endpointAddress)*/) {
  340. port->read_urb->dev = port->serial->dev;
  341. result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  342. if (result)
  343. err("%s - failed resubmitting read urb, error %d",
  344. __func__, result);
  345. dbg("%s - usb_submit_urb(read urb)", __func__);
  346. }
  347. }
  348. static void cyberjack_write_bulk_callback(struct urb *urb)
  349. {
  350. struct usb_serial_port *port = urb->context;
  351. struct cyberjack_private *priv = usb_get_serial_port_data(port);
  352. int status = urb->status;
  353. dbg("%s - port %d", __func__, port->number);
  354. port->write_urb_busy = 0;
  355. if (status) {
  356. dbg("%s - nonzero write bulk status received: %d",
  357. __func__, status);
  358. return;
  359. }
  360. spin_lock(&priv->lock);
  361. /* only do something if we have more data to send */
  362. if (priv->wrfilled) {
  363. int length, blksize, result;
  364. dbg("%s - transmitting data (frame n)", __func__);
  365. length = ((priv->wrfilled - priv->wrsent) > port->bulk_out_size) ?
  366. port->bulk_out_size : (priv->wrfilled - priv->wrsent);
  367. memcpy(port->write_urb->transfer_buffer,
  368. priv->wrbuf + priv->wrsent, length);
  369. priv->wrsent += length;
  370. /* set up our urb */
  371. usb_fill_bulk_urb(port->write_urb, port->serial->dev,
  372. usb_sndbulkpipe(port->serial->dev, port->bulk_out_endpointAddress),
  373. port->write_urb->transfer_buffer, length,
  374. ((port->serial->type->write_bulk_callback) ?
  375. port->serial->type->write_bulk_callback :
  376. cyberjack_write_bulk_callback),
  377. port);
  378. /* send the data out the bulk port */
  379. result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
  380. if (result) {
  381. err("%s - failed submitting write urb, error %d",
  382. __func__, result);
  383. /* Throw away data. No better idea what to do with it. */
  384. priv->wrfilled = 0;
  385. priv->wrsent = 0;
  386. goto exit;
  387. }
  388. dbg("%s - priv->wrsent=%d", __func__, priv->wrsent);
  389. dbg("%s - priv->wrfilled=%d", __func__, priv->wrfilled);
  390. blksize = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
  391. if (priv->wrsent >= priv->wrfilled ||
  392. priv->wrsent >= blksize) {
  393. dbg("%s - buffer cleaned", __func__);
  394. memset(priv->wrbuf, 0, sizeof(priv->wrbuf));
  395. priv->wrfilled = 0;
  396. priv->wrsent = 0;
  397. }
  398. }
  399. exit:
  400. spin_unlock(&priv->lock);
  401. usb_serial_port_softint(port);
  402. }
  403. static int __init cyberjack_init(void)
  404. {
  405. int retval;
  406. retval = usb_serial_register(&cyberjack_device);
  407. if (retval)
  408. goto failed_usb_serial_register;
  409. retval = usb_register(&cyberjack_driver);
  410. if (retval)
  411. goto failed_usb_register;
  412. info(DRIVER_VERSION " " DRIVER_AUTHOR);
  413. info(DRIVER_DESC);
  414. return 0;
  415. failed_usb_register:
  416. usb_serial_deregister(&cyberjack_device);
  417. failed_usb_serial_register:
  418. return retval;
  419. }
  420. static void __exit cyberjack_exit(void)
  421. {
  422. usb_deregister(&cyberjack_driver);
  423. usb_serial_deregister(&cyberjack_device);
  424. }
  425. module_init(cyberjack_init);
  426. module_exit(cyberjack_exit);
  427. MODULE_AUTHOR(DRIVER_AUTHOR);
  428. MODULE_DESCRIPTION(DRIVER_DESC);
  429. MODULE_VERSION(DRIVER_VERSION);
  430. MODULE_LICENSE("GPL");
  431. module_param(debug, bool, S_IRUGO | S_IWUSR);
  432. MODULE_PARM_DESC(debug, "Debug enabled or not");