cyberjack.c 12 KB

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