cyberjack.c 13 KB

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