cyberjack.c 13 KB

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