cyberjack.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * REINER SCT cyberJack pinpad/e-com USB Chipcard Reader Driver
  4. *
  5. * Copyright (C) 2001 REINER SCT
  6. * Author: Matthias Bruestle
  7. *
  8. * Contact: support@reiner-sct.com (see MAINTAINERS)
  9. *
  10. * This program is largely derived from work by the linux-usb group
  11. * and associated source files. Please see the usb/serial files for
  12. * individual credits and copyrights.
  13. *
  14. * Thanks to Greg Kroah-Hartman (greg@kroah.com) for his help and
  15. * patience.
  16. *
  17. * In case of problems, please write to the contact e-mail address
  18. * mentioned above.
  19. *
  20. * Please note that later models of the cyberjack reader family are
  21. * supported by a libusb-based userspace device driver.
  22. *
  23. * Homepage: http://www.reiner-sct.de/support/treiber_cyberjack.php#linux
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/errno.h>
  27. #include <linux/slab.h>
  28. #include <linux/tty.h>
  29. #include <linux/tty_driver.h>
  30. #include <linux/tty_flip.h>
  31. #include <linux/module.h>
  32. #include <linux/spinlock.h>
  33. #include <linux/uaccess.h>
  34. #include <linux/usb.h>
  35. #include <linux/usb/serial.h>
  36. #define CYBERJACK_LOCAL_BUF_SIZE 32
  37. #define DRIVER_AUTHOR "Matthias Bruestle"
  38. #define DRIVER_DESC "REINER SCT cyberJack pinpad/e-com USB Chipcard Reader Driver"
  39. #define CYBERJACK_VENDOR_ID 0x0C4B
  40. #define CYBERJACK_PRODUCT_ID 0x0100
  41. /* Function prototypes */
  42. static int cyberjack_port_probe(struct usb_serial_port *port);
  43. static void cyberjack_port_remove(struct usb_serial_port *port);
  44. static int cyberjack_open(struct tty_struct *tty,
  45. struct usb_serial_port *port);
  46. static void cyberjack_close(struct usb_serial_port *port);
  47. static int cyberjack_write(struct tty_struct *tty,
  48. struct usb_serial_port *port, const unsigned char *buf, int count);
  49. static unsigned int cyberjack_write_room(struct tty_struct *tty);
  50. static void cyberjack_read_int_callback(struct urb *urb);
  51. static void cyberjack_read_bulk_callback(struct urb *urb);
  52. static void cyberjack_write_bulk_callback(struct urb *urb);
  53. static const struct usb_device_id id_table[] = {
  54. { USB_DEVICE(CYBERJACK_VENDOR_ID, CYBERJACK_PRODUCT_ID) },
  55. { } /* Terminating entry */
  56. };
  57. MODULE_DEVICE_TABLE(usb, id_table);
  58. static struct usb_serial_driver cyberjack_device = {
  59. .driver = {
  60. .name = "cyberjack",
  61. },
  62. .description = "Reiner SCT Cyberjack USB card reader",
  63. .id_table = id_table,
  64. .num_ports = 1,
  65. .num_bulk_out = 1,
  66. .port_probe = cyberjack_port_probe,
  67. .port_remove = cyberjack_port_remove,
  68. .open = cyberjack_open,
  69. .close = cyberjack_close,
  70. .write = cyberjack_write,
  71. .write_room = cyberjack_write_room,
  72. .read_int_callback = cyberjack_read_int_callback,
  73. .read_bulk_callback = cyberjack_read_bulk_callback,
  74. .write_bulk_callback = cyberjack_write_bulk_callback,
  75. };
  76. static struct usb_serial_driver * const serial_drivers[] = {
  77. &cyberjack_device, NULL
  78. };
  79. struct cyberjack_private {
  80. spinlock_t lock; /* Lock for SMP */
  81. short rdtodo; /* Bytes still to read */
  82. unsigned char wrbuf[5*64]; /* Buffer for collecting data to write */
  83. short wrfilled; /* Overall data size we already got */
  84. short wrsent; /* Data already sent */
  85. };
  86. static int cyberjack_port_probe(struct usb_serial_port *port)
  87. {
  88. struct cyberjack_private *priv;
  89. int result;
  90. priv = kmalloc(sizeof(struct cyberjack_private), GFP_KERNEL);
  91. if (!priv)
  92. return -ENOMEM;
  93. spin_lock_init(&priv->lock);
  94. priv->rdtodo = 0;
  95. priv->wrfilled = 0;
  96. priv->wrsent = 0;
  97. usb_set_serial_port_data(port, priv);
  98. result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  99. if (result)
  100. dev_err(&port->dev, "usb_submit_urb(read int) failed\n");
  101. return 0;
  102. }
  103. static void cyberjack_port_remove(struct usb_serial_port *port)
  104. {
  105. struct cyberjack_private *priv;
  106. usb_kill_urb(port->interrupt_in_urb);
  107. priv = usb_get_serial_port_data(port);
  108. kfree(priv);
  109. }
  110. static int cyberjack_open(struct tty_struct *tty,
  111. struct usb_serial_port *port)
  112. {
  113. struct cyberjack_private *priv;
  114. unsigned long flags;
  115. dev_dbg(&port->dev, "%s - usb_clear_halt\n", __func__);
  116. usb_clear_halt(port->serial->dev, port->write_urb->pipe);
  117. priv = usb_get_serial_port_data(port);
  118. spin_lock_irqsave(&priv->lock, flags);
  119. priv->rdtodo = 0;
  120. priv->wrfilled = 0;
  121. priv->wrsent = 0;
  122. spin_unlock_irqrestore(&priv->lock, flags);
  123. return 0;
  124. }
  125. static void cyberjack_close(struct usb_serial_port *port)
  126. {
  127. usb_kill_urb(port->write_urb);
  128. usb_kill_urb(port->read_urb);
  129. }
  130. static int cyberjack_write(struct tty_struct *tty,
  131. struct usb_serial_port *port, const unsigned char *buf, int count)
  132. {
  133. struct device *dev = &port->dev;
  134. struct cyberjack_private *priv = usb_get_serial_port_data(port);
  135. unsigned long flags;
  136. int result;
  137. int wrexpected;
  138. if (count == 0) {
  139. dev_dbg(dev, "%s - write request of 0 bytes\n", __func__);
  140. return 0;
  141. }
  142. if (!test_and_clear_bit(0, &port->write_urbs_free)) {
  143. dev_dbg(dev, "%s - already writing\n", __func__);
  144. return 0;
  145. }
  146. spin_lock_irqsave(&priv->lock, flags);
  147. if (count+priv->wrfilled > sizeof(priv->wrbuf)) {
  148. /* To much data for buffer. Reset buffer. */
  149. priv->wrfilled = 0;
  150. spin_unlock_irqrestore(&priv->lock, flags);
  151. set_bit(0, &port->write_urbs_free);
  152. return 0;
  153. }
  154. /* Copy data */
  155. memcpy(priv->wrbuf + priv->wrfilled, buf, count);
  156. usb_serial_debug_data(dev, __func__, count, priv->wrbuf + priv->wrfilled);
  157. priv->wrfilled += count;
  158. if (priv->wrfilled >= 3) {
  159. wrexpected = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
  160. dev_dbg(dev, "%s - expected data: %d\n", __func__, wrexpected);
  161. } else
  162. wrexpected = sizeof(priv->wrbuf);
  163. if (priv->wrfilled >= wrexpected) {
  164. /* We have enough data to begin transmission */
  165. int length;
  166. dev_dbg(dev, "%s - transmitting data (frame 1)\n", __func__);
  167. length = (wrexpected > port->bulk_out_size) ?
  168. port->bulk_out_size : wrexpected;
  169. memcpy(port->write_urb->transfer_buffer, priv->wrbuf, length);
  170. priv->wrsent = length;
  171. /* set up our urb */
  172. port->write_urb->transfer_buffer_length = length;
  173. /* send the data out the bulk port */
  174. result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
  175. if (result) {
  176. dev_err(&port->dev,
  177. "%s - failed submitting write urb, error %d\n",
  178. __func__, result);
  179. /* Throw away data. No better idea what to do with it. */
  180. priv->wrfilled = 0;
  181. priv->wrsent = 0;
  182. spin_unlock_irqrestore(&priv->lock, flags);
  183. set_bit(0, &port->write_urbs_free);
  184. return 0;
  185. }
  186. dev_dbg(dev, "%s - priv->wrsent=%d\n", __func__, priv->wrsent);
  187. dev_dbg(dev, "%s - priv->wrfilled=%d\n", __func__, priv->wrfilled);
  188. if (priv->wrsent >= priv->wrfilled) {
  189. dev_dbg(dev, "%s - buffer cleaned\n", __func__);
  190. memset(priv->wrbuf, 0, sizeof(priv->wrbuf));
  191. priv->wrfilled = 0;
  192. priv->wrsent = 0;
  193. }
  194. }
  195. spin_unlock_irqrestore(&priv->lock, flags);
  196. return count;
  197. }
  198. static unsigned int cyberjack_write_room(struct tty_struct *tty)
  199. {
  200. /* FIXME: .... */
  201. return CYBERJACK_LOCAL_BUF_SIZE;
  202. }
  203. static void cyberjack_read_int_callback(struct urb *urb)
  204. {
  205. struct usb_serial_port *port = urb->context;
  206. struct cyberjack_private *priv = usb_get_serial_port_data(port);
  207. struct device *dev = &port->dev;
  208. unsigned char *data = urb->transfer_buffer;
  209. int status = urb->status;
  210. unsigned long flags;
  211. int result;
  212. /* the urb might have been killed. */
  213. if (status)
  214. return;
  215. usb_serial_debug_data(dev, __func__, urb->actual_length, data);
  216. /* React only to interrupts signaling a bulk_in transfer */
  217. if (urb->actual_length == 4 && data[0] == 0x01) {
  218. short old_rdtodo;
  219. /* This is a announcement of coming bulk_ins. */
  220. unsigned short size = ((unsigned short)data[3]<<8)+data[2]+3;
  221. spin_lock_irqsave(&priv->lock, flags);
  222. old_rdtodo = priv->rdtodo;
  223. if (old_rdtodo > SHRT_MAX - size) {
  224. dev_dbg(dev, "Too many bulk_in urbs to do.\n");
  225. spin_unlock_irqrestore(&priv->lock, flags);
  226. goto resubmit;
  227. }
  228. /* "+=" is probably more fault tolerant than "=" */
  229. priv->rdtodo += size;
  230. dev_dbg(dev, "%s - rdtodo: %d\n", __func__, priv->rdtodo);
  231. spin_unlock_irqrestore(&priv->lock, flags);
  232. if (!old_rdtodo) {
  233. result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  234. if (result)
  235. dev_err(dev, "%s - failed resubmitting read urb, error %d\n",
  236. __func__, result);
  237. dev_dbg(dev, "%s - usb_submit_urb(read urb)\n", __func__);
  238. }
  239. }
  240. resubmit:
  241. result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
  242. if (result)
  243. dev_err(&port->dev, "usb_submit_urb(read int) failed\n");
  244. dev_dbg(dev, "%s - usb_submit_urb(int urb)\n", __func__);
  245. }
  246. static void cyberjack_read_bulk_callback(struct urb *urb)
  247. {
  248. struct usb_serial_port *port = urb->context;
  249. struct cyberjack_private *priv = usb_get_serial_port_data(port);
  250. struct device *dev = &port->dev;
  251. unsigned char *data = urb->transfer_buffer;
  252. unsigned long flags;
  253. short todo;
  254. int result;
  255. int status = urb->status;
  256. usb_serial_debug_data(dev, __func__, urb->actual_length, data);
  257. if (status) {
  258. dev_dbg(dev, "%s - nonzero read bulk status received: %d\n",
  259. __func__, status);
  260. return;
  261. }
  262. if (urb->actual_length) {
  263. tty_insert_flip_string(&port->port, data, urb->actual_length);
  264. tty_flip_buffer_push(&port->port);
  265. }
  266. spin_lock_irqsave(&priv->lock, flags);
  267. /* Reduce urbs to do by one. */
  268. priv->rdtodo -= urb->actual_length;
  269. /* Just to be sure */
  270. if (priv->rdtodo < 0)
  271. priv->rdtodo = 0;
  272. todo = priv->rdtodo;
  273. spin_unlock_irqrestore(&priv->lock, flags);
  274. dev_dbg(dev, "%s - rdtodo: %d\n", __func__, todo);
  275. /* Continue to read if we have still urbs to do. */
  276. if (todo /* || (urb->actual_length==port->bulk_in_endpointAddress)*/) {
  277. result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  278. if (result)
  279. dev_err(dev, "%s - failed resubmitting read urb, error %d\n",
  280. __func__, result);
  281. dev_dbg(dev, "%s - usb_submit_urb(read urb)\n", __func__);
  282. }
  283. }
  284. static void cyberjack_write_bulk_callback(struct urb *urb)
  285. {
  286. struct usb_serial_port *port = urb->context;
  287. struct cyberjack_private *priv = usb_get_serial_port_data(port);
  288. struct device *dev = &port->dev;
  289. int status = urb->status;
  290. unsigned long flags;
  291. bool resubmitted = false;
  292. if (status) {
  293. dev_dbg(dev, "%s - nonzero write bulk status received: %d\n",
  294. __func__, status);
  295. set_bit(0, &port->write_urbs_free);
  296. return;
  297. }
  298. spin_lock_irqsave(&priv->lock, flags);
  299. /* only do something if we have more data to send */
  300. if (priv->wrfilled) {
  301. int length, blksize, result;
  302. dev_dbg(dev, "%s - transmitting data (frame n)\n", __func__);
  303. length = ((priv->wrfilled - priv->wrsent) > port->bulk_out_size) ?
  304. port->bulk_out_size : (priv->wrfilled - priv->wrsent);
  305. memcpy(port->write_urb->transfer_buffer,
  306. priv->wrbuf + priv->wrsent, length);
  307. priv->wrsent += length;
  308. /* set up our urb */
  309. port->write_urb->transfer_buffer_length = length;
  310. /* send the data out the bulk port */
  311. result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
  312. if (result) {
  313. dev_err(dev, "%s - failed submitting write urb, error %d\n",
  314. __func__, result);
  315. /* Throw away data. No better idea what to do with it. */
  316. priv->wrfilled = 0;
  317. priv->wrsent = 0;
  318. goto exit;
  319. }
  320. resubmitted = true;
  321. dev_dbg(dev, "%s - priv->wrsent=%d\n", __func__, priv->wrsent);
  322. dev_dbg(dev, "%s - priv->wrfilled=%d\n", __func__, priv->wrfilled);
  323. blksize = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
  324. if (priv->wrsent >= priv->wrfilled ||
  325. priv->wrsent >= blksize) {
  326. dev_dbg(dev, "%s - buffer cleaned\n", __func__);
  327. memset(priv->wrbuf, 0, sizeof(priv->wrbuf));
  328. priv->wrfilled = 0;
  329. priv->wrsent = 0;
  330. }
  331. }
  332. exit:
  333. spin_unlock_irqrestore(&priv->lock, flags);
  334. if (!resubmitted)
  335. set_bit(0, &port->write_urbs_free);
  336. usb_serial_port_softint(port);
  337. }
  338. module_usb_serial_driver(serial_drivers, id_table);
  339. MODULE_AUTHOR(DRIVER_AUTHOR);
  340. MODULE_DESCRIPTION(DRIVER_DESC);
  341. MODULE_LICENSE("GPL");