metro-usb.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /*
  2. Some of this code is credited to Linux USB open source files that are
  3. distributed with Linux.
  4. Copyright: 2007 Metrologic Instruments. All rights reserved.
  5. Copyright: 2011 Azimut Ltd. <http://azimutrzn.ru/>
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/init.h>
  9. #include <linux/tty.h>
  10. #include <linux/module.h>
  11. #include <linux/usb.h>
  12. #include <linux/errno.h>
  13. #include <linux/slab.h>
  14. #include <linux/tty_driver.h>
  15. #include <linux/tty_flip.h>
  16. #include <linux/moduleparam.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/errno.h>
  19. #include <linux/uaccess.h>
  20. #include <linux/usb/serial.h>
  21. /* Version Information */
  22. #define DRIVER_VERSION "v1.2.0.0"
  23. #define DRIVER_DESC "Metrologic Instruments Inc. - USB-POS driver"
  24. /* Product information. */
  25. #define FOCUS_VENDOR_ID 0x0C2E
  26. #define FOCUS_PRODUCT_ID 0x0720
  27. #define FOCUS_PRODUCT_ID_UNI 0x0710
  28. #define METROUSB_SET_REQUEST_TYPE 0x40
  29. #define METROUSB_SET_MODEM_CTRL_REQUEST 10
  30. #define METROUSB_SET_BREAK_REQUEST 0x40
  31. #define METROUSB_MCR_NONE 0x08 /* Deactivate DTR and RTS. */
  32. #define METROUSB_MCR_RTS 0x0a /* Activate RTS. */
  33. #define METROUSB_MCR_DTR 0x09 /* Activate DTR. */
  34. #define WDR_TIMEOUT 5000 /* default urb timeout. */
  35. /* Private data structure. */
  36. struct metrousb_private {
  37. spinlock_t lock;
  38. int throttled;
  39. unsigned long control_state;
  40. };
  41. /* Device table list. */
  42. static struct usb_device_id id_table[] = {
  43. { USB_DEVICE(FOCUS_VENDOR_ID, FOCUS_PRODUCT_ID) },
  44. { USB_DEVICE(FOCUS_VENDOR_ID, FOCUS_PRODUCT_ID_UNI) },
  45. { }, /* Terminating entry. */
  46. };
  47. MODULE_DEVICE_TABLE(usb, id_table);
  48. /* Input parameter constants. */
  49. static bool debug;
  50. static void metrousb_read_int_callback(struct urb *urb)
  51. {
  52. struct usb_serial_port *port = urb->context;
  53. struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
  54. struct tty_struct *tty;
  55. unsigned char *data = urb->transfer_buffer;
  56. int throttled = 0;
  57. int result = 0;
  58. unsigned long flags = 0;
  59. dev_dbg(&port->dev, "%s\n", __func__);
  60. switch (urb->status) {
  61. case 0:
  62. /* Success status, read from the port. */
  63. break;
  64. case -ECONNRESET:
  65. case -ENOENT:
  66. case -ESHUTDOWN:
  67. /* urb has been terminated. */
  68. dev_dbg(&port->dev,
  69. "%s - urb shutting down, error code=%d\n",
  70. __func__, result);
  71. return;
  72. default:
  73. dev_dbg(&port->dev,
  74. "%s - non-zero urb received, error code=%d\n",
  75. __func__, result);
  76. goto exit;
  77. }
  78. /* Set the data read from the usb port into the serial port buffer. */
  79. tty = tty_port_tty_get(&port->port);
  80. if (!tty) {
  81. dev_dbg(&port->dev, "%s - bad tty pointer - exiting\n",
  82. __func__);
  83. return;
  84. }
  85. if (tty && urb->actual_length) {
  86. /* Loop through the data copying each byte to the tty layer. */
  87. tty_insert_flip_string(tty, data, urb->actual_length);
  88. /* Force the data to the tty layer. */
  89. tty_flip_buffer_push(tty);
  90. }
  91. tty_kref_put(tty);
  92. /* Set any port variables. */
  93. spin_lock_irqsave(&metro_priv->lock, flags);
  94. throttled = metro_priv->throttled;
  95. spin_unlock_irqrestore(&metro_priv->lock, flags);
  96. /* Continue trying to read if set. */
  97. if (!throttled) {
  98. usb_fill_int_urb(port->interrupt_in_urb, port->serial->dev,
  99. usb_rcvintpipe(port->serial->dev, port->interrupt_in_endpointAddress),
  100. port->interrupt_in_urb->transfer_buffer,
  101. port->interrupt_in_urb->transfer_buffer_length,
  102. metrousb_read_int_callback, port, 1);
  103. result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
  104. if (result)
  105. dev_dbg(&port->dev,
  106. "%s - failed submitting interrupt in urb, error code=%d\n",
  107. __func__, result);
  108. }
  109. return;
  110. exit:
  111. /* Try to resubmit the urb. */
  112. result = usb_submit_urb(urb, GFP_ATOMIC);
  113. if (result)
  114. dev_dbg(&port->dev,
  115. "%s - failed submitting interrupt in urb, error code=%d\n",
  116. __func__, result);
  117. }
  118. static void metrousb_cleanup(struct usb_serial_port *port)
  119. {
  120. dev_dbg(&port->dev, "%s\n", __func__);
  121. if (port->serial->dev) {
  122. /* Shutdown any interrupt in urbs. */
  123. if (port->interrupt_in_urb) {
  124. usb_unlink_urb(port->interrupt_in_urb);
  125. usb_kill_urb(port->interrupt_in_urb);
  126. }
  127. }
  128. }
  129. static int metrousb_open(struct tty_struct *tty, struct usb_serial_port *port)
  130. {
  131. struct usb_serial *serial = port->serial;
  132. struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
  133. unsigned long flags = 0;
  134. int result = 0;
  135. dev_dbg(&port->dev, "%s\n", __func__);
  136. /* Make sure the urb is initialized. */
  137. if (!port->interrupt_in_urb) {
  138. dev_dbg(&port->dev, "%s - interrupt urb not initialized\n",
  139. __func__);
  140. return -ENODEV;
  141. }
  142. /* Set the private data information for the port. */
  143. spin_lock_irqsave(&metro_priv->lock, flags);
  144. metro_priv->control_state = 0;
  145. metro_priv->throttled = 0;
  146. spin_unlock_irqrestore(&metro_priv->lock, flags);
  147. /*
  148. * Force low_latency on so that our tty_push actually forces the data
  149. * through, otherwise it is scheduled, and with high data rates (like
  150. * with OHCI) data can get lost.
  151. */
  152. if (tty)
  153. tty->low_latency = 1;
  154. /* Clear the urb pipe. */
  155. usb_clear_halt(serial->dev, port->interrupt_in_urb->pipe);
  156. /* Start reading from the device */
  157. usb_fill_int_urb(port->interrupt_in_urb, serial->dev,
  158. usb_rcvintpipe(serial->dev, port->interrupt_in_endpointAddress),
  159. port->interrupt_in_urb->transfer_buffer,
  160. port->interrupt_in_urb->transfer_buffer_length,
  161. metrousb_read_int_callback, port, 1);
  162. result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  163. if (result) {
  164. dev_dbg(&port->dev,
  165. "%s - failed submitting interrupt in urb, error code=%d\n",
  166. __func__, result);
  167. goto exit;
  168. }
  169. dev_dbg(&port->dev, "%s - port open\n", __func__);
  170. exit:
  171. return result;
  172. }
  173. static int metrousb_set_modem_ctrl(struct usb_serial *serial, unsigned int control_state)
  174. {
  175. int retval = 0;
  176. unsigned char mcr = METROUSB_MCR_NONE;
  177. dev_dbg(&serial->dev->dev, "%s - control state = %d\n",
  178. __func__, control_state);
  179. /* Set the modem control value. */
  180. if (control_state & TIOCM_DTR)
  181. mcr |= METROUSB_MCR_DTR;
  182. if (control_state & TIOCM_RTS)
  183. mcr |= METROUSB_MCR_RTS;
  184. /* Send the command to the usb port. */
  185. retval = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  186. METROUSB_SET_REQUEST_TYPE, METROUSB_SET_MODEM_CTRL_REQUEST,
  187. control_state, 0, NULL, 0, WDR_TIMEOUT);
  188. if (retval < 0)
  189. dev_dbg(&serial->dev->dev,
  190. "%s - set modem ctrl=0x%x failed, error code=%d\n",
  191. __func__, mcr, retval);
  192. return retval;
  193. }
  194. static void metrousb_shutdown(struct usb_serial *serial)
  195. {
  196. int i = 0;
  197. dev_dbg(&serial->dev->dev, "%s\n", __func__);
  198. /* Stop reading and writing on all ports. */
  199. for (i = 0; i < serial->num_ports; ++i) {
  200. /* Close any open urbs. */
  201. metrousb_cleanup(serial->port[i]);
  202. /* Free memory. */
  203. kfree(usb_get_serial_port_data(serial->port[i]));
  204. usb_set_serial_port_data(serial->port[i], NULL);
  205. dev_dbg(&serial->dev->dev, "%s - freed port number=%d\n",
  206. __func__, serial->port[i]->number);
  207. }
  208. }
  209. static int metrousb_startup(struct usb_serial *serial)
  210. {
  211. struct metrousb_private *metro_priv;
  212. struct usb_serial_port *port;
  213. int i = 0;
  214. dev_dbg(&serial->dev->dev, "%s\n", __func__);
  215. /* Loop through the serial ports setting up the private structures.
  216. * Currently we only use one port. */
  217. for (i = 0; i < serial->num_ports; ++i) {
  218. port = serial->port[i];
  219. /* Declare memory. */
  220. metro_priv = kzalloc(sizeof(struct metrousb_private), GFP_KERNEL);
  221. if (!metro_priv)
  222. return -ENOMEM;
  223. /* Initialize memory. */
  224. spin_lock_init(&metro_priv->lock);
  225. usb_set_serial_port_data(port, metro_priv);
  226. dev_dbg(&serial->dev->dev, "%s - port number=%d\n ",
  227. __func__, port->number);
  228. }
  229. return 0;
  230. }
  231. static void metrousb_throttle(struct tty_struct *tty)
  232. {
  233. struct usb_serial_port *port = tty->driver_data;
  234. struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
  235. unsigned long flags = 0;
  236. dev_dbg(tty->dev, "%s\n", __func__);
  237. /* Set the private information for the port to stop reading data. */
  238. spin_lock_irqsave(&metro_priv->lock, flags);
  239. metro_priv->throttled = 1;
  240. spin_unlock_irqrestore(&metro_priv->lock, flags);
  241. }
  242. static int metrousb_tiocmget(struct tty_struct *tty)
  243. {
  244. unsigned long control_state = 0;
  245. struct usb_serial_port *port = tty->driver_data;
  246. struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
  247. unsigned long flags = 0;
  248. dev_dbg(tty->dev, "%s\n", __func__);
  249. spin_lock_irqsave(&metro_priv->lock, flags);
  250. control_state = metro_priv->control_state;
  251. spin_unlock_irqrestore(&metro_priv->lock, flags);
  252. return control_state;
  253. }
  254. static int metrousb_tiocmset(struct tty_struct *tty,
  255. unsigned int set, unsigned int clear)
  256. {
  257. struct usb_serial_port *port = tty->driver_data;
  258. struct usb_serial *serial = port->serial;
  259. struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
  260. unsigned long flags = 0;
  261. unsigned long control_state = 0;
  262. dev_dbg(tty->dev, "%s - set=%d, clear=%d\n", __func__, set, clear);
  263. spin_lock_irqsave(&metro_priv->lock, flags);
  264. control_state = metro_priv->control_state;
  265. /* Set the RTS and DTR values. */
  266. if (set & TIOCM_RTS)
  267. control_state |= TIOCM_RTS;
  268. if (set & TIOCM_DTR)
  269. control_state |= TIOCM_DTR;
  270. if (clear & TIOCM_RTS)
  271. control_state &= ~TIOCM_RTS;
  272. if (clear & TIOCM_DTR)
  273. control_state &= ~TIOCM_DTR;
  274. metro_priv->control_state = control_state;
  275. spin_unlock_irqrestore(&metro_priv->lock, flags);
  276. return metrousb_set_modem_ctrl(serial, control_state);
  277. }
  278. static void metrousb_unthrottle(struct tty_struct *tty)
  279. {
  280. struct usb_serial_port *port = tty->driver_data;
  281. struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
  282. unsigned long flags = 0;
  283. int result = 0;
  284. dev_dbg(tty->dev, "%s\n", __func__);
  285. /* Set the private information for the port to resume reading data. */
  286. spin_lock_irqsave(&metro_priv->lock, flags);
  287. metro_priv->throttled = 0;
  288. spin_unlock_irqrestore(&metro_priv->lock, flags);
  289. /* Submit the urb to read from the port. */
  290. port->interrupt_in_urb->dev = port->serial->dev;
  291. result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
  292. if (result)
  293. dev_dbg(tty->dev,
  294. "failed submitting interrupt in urb error code=%d\n",
  295. result);
  296. }
  297. static struct usb_driver metrousb_driver = {
  298. .name = "metro-usb",
  299. .probe = usb_serial_probe,
  300. .disconnect = usb_serial_disconnect,
  301. .id_table = id_table
  302. };
  303. static struct usb_serial_driver metrousb_device = {
  304. .driver = {
  305. .owner = THIS_MODULE,
  306. .name = "metro-usb",
  307. },
  308. .description = "Metrologic USB to serial converter.",
  309. .id_table = id_table,
  310. .num_ports = 1,
  311. .open = metrousb_open,
  312. .close = metrousb_cleanup,
  313. .read_int_callback = metrousb_read_int_callback,
  314. .attach = metrousb_startup,
  315. .release = metrousb_shutdown,
  316. .throttle = metrousb_throttle,
  317. .unthrottle = metrousb_unthrottle,
  318. .tiocmget = metrousb_tiocmget,
  319. .tiocmset = metrousb_tiocmset,
  320. };
  321. static struct usb_serial_driver * const serial_drivers[] = {
  322. &metrousb_device,
  323. NULL,
  324. };
  325. module_usb_serial_driver(metrousb_driver, serial_drivers);
  326. MODULE_LICENSE("GPL");
  327. MODULE_AUTHOR("Philip Nicastro");
  328. MODULE_AUTHOR("Aleksey Babahin <tamerlan311@gmail.com>");
  329. MODULE_DESCRIPTION(DRIVER_DESC);
  330. /* Module input parameters */
  331. module_param(debug, bool, S_IRUGO | S_IWUSR);
  332. MODULE_PARM_DESC(debug, "Print debug info (bool 1=on, 0=off)");