cyberjack.c 12 KB

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