cyberjack.c 12 KB

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