cyberjack.c 12 KB

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