metro-usb.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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/tty.h>
  9. #include <linux/module.h>
  10. #include <linux/usb.h>
  11. #include <linux/errno.h>
  12. #include <linux/slab.h>
  13. #include <linux/tty_driver.h>
  14. #include <linux/tty_flip.h>
  15. #include <linux/moduleparam.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/uaccess.h>
  18. #include <linux/usb/serial.h>
  19. #define DRIVER_DESC "Metrologic Instruments Inc. - USB-POS driver"
  20. /* Product information. */
  21. #define FOCUS_VENDOR_ID 0x0C2E
  22. #define FOCUS_PRODUCT_ID_BI 0x0720
  23. #define FOCUS_PRODUCT_ID_UNI 0x0700
  24. #define METROUSB_SET_REQUEST_TYPE 0x40
  25. #define METROUSB_SET_MODEM_CTRL_REQUEST 10
  26. #define METROUSB_SET_BREAK_REQUEST 0x40
  27. #define METROUSB_MCR_NONE 0x08 /* Deactivate DTR and RTS. */
  28. #define METROUSB_MCR_RTS 0x0a /* Activate RTS. */
  29. #define METROUSB_MCR_DTR 0x09 /* Activate DTR. */
  30. #define WDR_TIMEOUT 5000 /* default urb timeout. */
  31. /* Private data structure. */
  32. struct metrousb_private {
  33. spinlock_t lock;
  34. int throttled;
  35. unsigned long control_state;
  36. };
  37. /* Device table list. */
  38. static const struct usb_device_id id_table[] = {
  39. { USB_DEVICE(FOCUS_VENDOR_ID, FOCUS_PRODUCT_ID_BI) },
  40. { USB_DEVICE(FOCUS_VENDOR_ID, FOCUS_PRODUCT_ID_UNI) },
  41. { }, /* Terminating entry. */
  42. };
  43. MODULE_DEVICE_TABLE(usb, id_table);
  44. /* UNI-Directional mode commands for device configure */
  45. #define UNI_CMD_OPEN 0x80
  46. #define UNI_CMD_CLOSE 0xFF
  47. static inline int metrousb_is_unidirectional_mode(struct usb_serial_port *port)
  48. {
  49. __u16 product_id = le16_to_cpu(
  50. port->serial->dev->descriptor.idProduct);
  51. return product_id == FOCUS_PRODUCT_ID_UNI;
  52. }
  53. static int metrousb_send_unidirectional_cmd(u8 cmd, struct usb_serial_port *port)
  54. {
  55. int ret;
  56. int actual_len;
  57. u8 *buffer_cmd = NULL;
  58. if (!metrousb_is_unidirectional_mode(port))
  59. return 0;
  60. buffer_cmd = kzalloc(sizeof(cmd), GFP_KERNEL);
  61. if (!buffer_cmd)
  62. return -ENOMEM;
  63. *buffer_cmd = cmd;
  64. ret = usb_interrupt_msg(port->serial->dev,
  65. usb_sndintpipe(port->serial->dev, port->interrupt_out_endpointAddress),
  66. buffer_cmd, sizeof(cmd),
  67. &actual_len, USB_CTRL_SET_TIMEOUT);
  68. kfree(buffer_cmd);
  69. if (ret < 0)
  70. return ret;
  71. else if (actual_len != sizeof(cmd))
  72. return -EIO;
  73. return 0;
  74. }
  75. static void metrousb_read_int_callback(struct urb *urb)
  76. {
  77. struct usb_serial_port *port = urb->context;
  78. struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
  79. unsigned char *data = urb->transfer_buffer;
  80. int throttled = 0;
  81. int result = 0;
  82. unsigned long flags = 0;
  83. dev_dbg(&port->dev, "%s\n", __func__);
  84. switch (urb->status) {
  85. case 0:
  86. /* Success status, read from the port. */
  87. break;
  88. case -ECONNRESET:
  89. case -ENOENT:
  90. case -ESHUTDOWN:
  91. /* urb has been terminated. */
  92. dev_dbg(&port->dev,
  93. "%s - urb shutting down, error code=%d\n",
  94. __func__, urb->status);
  95. return;
  96. default:
  97. dev_dbg(&port->dev,
  98. "%s - non-zero urb received, error code=%d\n",
  99. __func__, urb->status);
  100. goto exit;
  101. }
  102. /* Set the data read from the usb port into the serial port buffer. */
  103. if (urb->actual_length) {
  104. /* Loop through the data copying each byte to the tty layer. */
  105. tty_insert_flip_string(&port->port, data, urb->actual_length);
  106. /* Force the data to the tty layer. */
  107. tty_flip_buffer_push(&port->port);
  108. }
  109. /* Set any port variables. */
  110. spin_lock_irqsave(&metro_priv->lock, flags);
  111. throttled = metro_priv->throttled;
  112. spin_unlock_irqrestore(&metro_priv->lock, flags);
  113. /* Continue trying to read if set. */
  114. if (!throttled) {
  115. usb_fill_int_urb(port->interrupt_in_urb, port->serial->dev,
  116. usb_rcvintpipe(port->serial->dev, port->interrupt_in_endpointAddress),
  117. port->interrupt_in_urb->transfer_buffer,
  118. port->interrupt_in_urb->transfer_buffer_length,
  119. metrousb_read_int_callback, port, 1);
  120. result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
  121. if (result)
  122. dev_err(&port->dev,
  123. "%s - failed submitting interrupt in urb, error code=%d\n",
  124. __func__, result);
  125. }
  126. return;
  127. exit:
  128. /* Try to resubmit the urb. */
  129. result = usb_submit_urb(urb, GFP_ATOMIC);
  130. if (result)
  131. dev_err(&port->dev,
  132. "%s - failed submitting interrupt in urb, error code=%d\n",
  133. __func__, result);
  134. }
  135. static void metrousb_write_int_callback(struct urb *urb)
  136. {
  137. struct usb_serial_port *port = urb->context;
  138. dev_warn(&port->dev, "%s not implemented yet.\n",
  139. __func__);
  140. }
  141. static void metrousb_cleanup(struct usb_serial_port *port)
  142. {
  143. dev_dbg(&port->dev, "%s\n", __func__);
  144. usb_unlink_urb(port->interrupt_in_urb);
  145. usb_kill_urb(port->interrupt_in_urb);
  146. metrousb_send_unidirectional_cmd(UNI_CMD_CLOSE, port);
  147. }
  148. static int metrousb_open(struct tty_struct *tty, struct usb_serial_port *port)
  149. {
  150. struct usb_serial *serial = port->serial;
  151. struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
  152. unsigned long flags = 0;
  153. int result = 0;
  154. dev_dbg(&port->dev, "%s\n", __func__);
  155. /* Make sure the urb is initialized. */
  156. if (!port->interrupt_in_urb) {
  157. dev_err(&port->dev, "%s - interrupt urb not initialized\n",
  158. __func__);
  159. return -ENODEV;
  160. }
  161. /* Set the private data information for the port. */
  162. spin_lock_irqsave(&metro_priv->lock, flags);
  163. metro_priv->control_state = 0;
  164. metro_priv->throttled = 0;
  165. spin_unlock_irqrestore(&metro_priv->lock, flags);
  166. /* Clear the urb pipe. */
  167. usb_clear_halt(serial->dev, port->interrupt_in_urb->pipe);
  168. /* Start reading from the device */
  169. usb_fill_int_urb(port->interrupt_in_urb, serial->dev,
  170. usb_rcvintpipe(serial->dev, port->interrupt_in_endpointAddress),
  171. port->interrupt_in_urb->transfer_buffer,
  172. port->interrupt_in_urb->transfer_buffer_length,
  173. metrousb_read_int_callback, port, 1);
  174. result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  175. if (result) {
  176. dev_err(&port->dev,
  177. "%s - failed submitting interrupt in urb, error code=%d\n",
  178. __func__, result);
  179. goto exit;
  180. }
  181. /* Send activate cmd to device */
  182. result = metrousb_send_unidirectional_cmd(UNI_CMD_OPEN, port);
  183. if (result) {
  184. dev_err(&port->dev,
  185. "%s - failed to configure device, error code=%d\n",
  186. __func__, result);
  187. goto exit;
  188. }
  189. dev_dbg(&port->dev, "%s - port open\n", __func__);
  190. exit:
  191. return result;
  192. }
  193. static int metrousb_set_modem_ctrl(struct usb_serial *serial, unsigned int control_state)
  194. {
  195. int retval = 0;
  196. unsigned char mcr = METROUSB_MCR_NONE;
  197. dev_dbg(&serial->dev->dev, "%s - control state = %d\n",
  198. __func__, control_state);
  199. /* Set the modem control value. */
  200. if (control_state & TIOCM_DTR)
  201. mcr |= METROUSB_MCR_DTR;
  202. if (control_state & TIOCM_RTS)
  203. mcr |= METROUSB_MCR_RTS;
  204. /* Send the command to the usb port. */
  205. retval = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  206. METROUSB_SET_REQUEST_TYPE, METROUSB_SET_MODEM_CTRL_REQUEST,
  207. control_state, 0, NULL, 0, WDR_TIMEOUT);
  208. if (retval < 0)
  209. dev_err(&serial->dev->dev,
  210. "%s - set modem ctrl=0x%x failed, error code=%d\n",
  211. __func__, mcr, retval);
  212. return retval;
  213. }
  214. static int metrousb_port_probe(struct usb_serial_port *port)
  215. {
  216. struct metrousb_private *metro_priv;
  217. metro_priv = kzalloc(sizeof(*metro_priv), GFP_KERNEL);
  218. if (!metro_priv)
  219. return -ENOMEM;
  220. spin_lock_init(&metro_priv->lock);
  221. usb_set_serial_port_data(port, metro_priv);
  222. return 0;
  223. }
  224. static int metrousb_port_remove(struct usb_serial_port *port)
  225. {
  226. struct metrousb_private *metro_priv;
  227. metro_priv = usb_get_serial_port_data(port);
  228. kfree(metro_priv);
  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_err(tty->dev,
  294. "failed submitting interrupt in urb error code=%d\n",
  295. result);
  296. }
  297. static struct usb_serial_driver metrousb_device = {
  298. .driver = {
  299. .owner = THIS_MODULE,
  300. .name = "metro-usb",
  301. },
  302. .description = "Metrologic USB to Serial",
  303. .id_table = id_table,
  304. .num_ports = 1,
  305. .open = metrousb_open,
  306. .close = metrousb_cleanup,
  307. .read_int_callback = metrousb_read_int_callback,
  308. .write_int_callback = metrousb_write_int_callback,
  309. .port_probe = metrousb_port_probe,
  310. .port_remove = metrousb_port_remove,
  311. .throttle = metrousb_throttle,
  312. .unthrottle = metrousb_unthrottle,
  313. .tiocmget = metrousb_tiocmget,
  314. .tiocmset = metrousb_tiocmset,
  315. };
  316. static struct usb_serial_driver * const serial_drivers[] = {
  317. &metrousb_device,
  318. NULL,
  319. };
  320. module_usb_serial_driver(serial_drivers, id_table);
  321. MODULE_LICENSE("GPL");
  322. MODULE_AUTHOR("Philip Nicastro");
  323. MODULE_AUTHOR("Aleksey Babahin <tamerlan311@gmail.com>");
  324. MODULE_DESCRIPTION(DRIVER_DESC);