metro-usb.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. Some of this code is credited to Linux USB open source files that are
  4. distributed with Linux.
  5. Copyright: 2007 Metrologic Instruments. All rights reserved.
  6. Copyright: 2011 Azimut Ltd. <http://azimutrzn.ru/>
  7. */
  8. #include <linux/kernel.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/uaccess.h>
  19. #include <linux/usb/serial.h>
  20. #define DRIVER_DESC "Metrologic Instruments Inc. - USB-POS driver"
  21. /* Product information. */
  22. #define FOCUS_VENDOR_ID 0x0C2E
  23. #define FOCUS_PRODUCT_ID_BI 0x0720
  24. #define FOCUS_PRODUCT_ID_UNI 0x0700
  25. #define METROUSB_SET_REQUEST_TYPE 0x40
  26. #define METROUSB_SET_MODEM_CTRL_REQUEST 10
  27. #define METROUSB_SET_BREAK_REQUEST 0x40
  28. #define METROUSB_MCR_NONE 0x08 /* Deactivate DTR and RTS. */
  29. #define METROUSB_MCR_RTS 0x0a /* Activate RTS. */
  30. #define METROUSB_MCR_DTR 0x09 /* Activate DTR. */
  31. #define WDR_TIMEOUT 5000 /* default urb timeout. */
  32. /* Private data structure. */
  33. struct metrousb_private {
  34. spinlock_t lock;
  35. int throttled;
  36. unsigned long control_state;
  37. };
  38. /* Device table list. */
  39. static const struct usb_device_id id_table[] = {
  40. { USB_DEVICE(FOCUS_VENDOR_ID, FOCUS_PRODUCT_ID_BI) },
  41. { USB_DEVICE(FOCUS_VENDOR_ID, FOCUS_PRODUCT_ID_UNI) },
  42. { USB_DEVICE_INTERFACE_CLASS(0x0c2e, 0x0730, 0xff) }, /* MS7820 */
  43. { }, /* Terminating entry. */
  44. };
  45. MODULE_DEVICE_TABLE(usb, id_table);
  46. /* UNI-Directional mode commands for device configure */
  47. #define UNI_CMD_OPEN 0x80
  48. #define UNI_CMD_CLOSE 0xFF
  49. static int metrousb_is_unidirectional_mode(struct usb_serial *serial)
  50. {
  51. u16 product_id = le16_to_cpu(serial->dev->descriptor.idProduct);
  52. return product_id == FOCUS_PRODUCT_ID_UNI;
  53. }
  54. static int metrousb_calc_num_ports(struct usb_serial *serial,
  55. struct usb_serial_endpoints *epds)
  56. {
  57. if (metrousb_is_unidirectional_mode(serial)) {
  58. if (epds->num_interrupt_out == 0) {
  59. dev_err(&serial->interface->dev, "interrupt-out endpoint missing\n");
  60. return -ENODEV;
  61. }
  62. }
  63. return 1;
  64. }
  65. static int metrousb_send_unidirectional_cmd(u8 cmd, struct usb_serial_port *port)
  66. {
  67. int ret;
  68. int actual_len;
  69. u8 *buffer_cmd = NULL;
  70. if (!metrousb_is_unidirectional_mode(port->serial))
  71. return 0;
  72. buffer_cmd = kzalloc(sizeof(cmd), GFP_KERNEL);
  73. if (!buffer_cmd)
  74. return -ENOMEM;
  75. *buffer_cmd = cmd;
  76. ret = usb_interrupt_msg(port->serial->dev,
  77. usb_sndintpipe(port->serial->dev, port->interrupt_out_endpointAddress),
  78. buffer_cmd, sizeof(cmd),
  79. &actual_len, USB_CTRL_SET_TIMEOUT);
  80. kfree(buffer_cmd);
  81. if (ret < 0)
  82. return ret;
  83. else if (actual_len != sizeof(cmd))
  84. return -EIO;
  85. return 0;
  86. }
  87. static void metrousb_read_int_callback(struct urb *urb)
  88. {
  89. struct usb_serial_port *port = urb->context;
  90. struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
  91. unsigned char *data = urb->transfer_buffer;
  92. int throttled = 0;
  93. int result = 0;
  94. unsigned long flags = 0;
  95. dev_dbg(&port->dev, "%s\n", __func__);
  96. switch (urb->status) {
  97. case 0:
  98. /* Success status, read from the port. */
  99. break;
  100. case -ECONNRESET:
  101. case -ENOENT:
  102. case -ESHUTDOWN:
  103. /* urb has been terminated. */
  104. dev_dbg(&port->dev,
  105. "%s - urb shutting down, error code=%d\n",
  106. __func__, urb->status);
  107. return;
  108. default:
  109. dev_dbg(&port->dev,
  110. "%s - non-zero urb received, error code=%d\n",
  111. __func__, urb->status);
  112. goto exit;
  113. }
  114. /* Set the data read from the usb port into the serial port buffer. */
  115. if (urb->actual_length) {
  116. /* Loop through the data copying each byte to the tty layer. */
  117. tty_insert_flip_string(&port->port, data, urb->actual_length);
  118. /* Force the data to the tty layer. */
  119. tty_flip_buffer_push(&port->port);
  120. }
  121. /* Set any port variables. */
  122. spin_lock_irqsave(&metro_priv->lock, flags);
  123. throttled = metro_priv->throttled;
  124. spin_unlock_irqrestore(&metro_priv->lock, flags);
  125. if (throttled)
  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_cleanup(struct usb_serial_port *port)
  136. {
  137. usb_kill_urb(port->interrupt_in_urb);
  138. metrousb_send_unidirectional_cmd(UNI_CMD_CLOSE, port);
  139. }
  140. static int metrousb_open(struct tty_struct *tty, struct usb_serial_port *port)
  141. {
  142. struct usb_serial *serial = port->serial;
  143. struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
  144. unsigned long flags = 0;
  145. int result = 0;
  146. /* Set the private data information for the port. */
  147. spin_lock_irqsave(&metro_priv->lock, flags);
  148. metro_priv->control_state = 0;
  149. metro_priv->throttled = 0;
  150. spin_unlock_irqrestore(&metro_priv->lock, flags);
  151. /* Clear the urb pipe. */
  152. usb_clear_halt(serial->dev, port->interrupt_in_urb->pipe);
  153. /* Start reading from the device */
  154. usb_fill_int_urb(port->interrupt_in_urb, serial->dev,
  155. usb_rcvintpipe(serial->dev, port->interrupt_in_endpointAddress),
  156. port->interrupt_in_urb->transfer_buffer,
  157. port->interrupt_in_urb->transfer_buffer_length,
  158. metrousb_read_int_callback, port, 1);
  159. result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  160. if (result) {
  161. dev_err(&port->dev,
  162. "%s - failed submitting interrupt in urb, error code=%d\n",
  163. __func__, result);
  164. return result;
  165. }
  166. /* Send activate cmd to device */
  167. result = metrousb_send_unidirectional_cmd(UNI_CMD_OPEN, port);
  168. if (result) {
  169. dev_err(&port->dev,
  170. "%s - failed to configure device, error code=%d\n",
  171. __func__, result);
  172. goto err_kill_urb;
  173. }
  174. return 0;
  175. err_kill_urb:
  176. usb_kill_urb(port->interrupt_in_urb);
  177. return result;
  178. }
  179. static int metrousb_set_modem_ctrl(struct usb_serial *serial, unsigned int control_state)
  180. {
  181. int retval = 0;
  182. unsigned char mcr = METROUSB_MCR_NONE;
  183. dev_dbg(&serial->dev->dev, "%s - control state = %d\n",
  184. __func__, control_state);
  185. /* Set the modem control value. */
  186. if (control_state & TIOCM_DTR)
  187. mcr |= METROUSB_MCR_DTR;
  188. if (control_state & TIOCM_RTS)
  189. mcr |= METROUSB_MCR_RTS;
  190. /* Send the command to the usb port. */
  191. retval = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  192. METROUSB_SET_REQUEST_TYPE, METROUSB_SET_MODEM_CTRL_REQUEST,
  193. control_state, 0, NULL, 0, WDR_TIMEOUT);
  194. if (retval < 0)
  195. dev_err(&serial->dev->dev,
  196. "%s - set modem ctrl=0x%x failed, error code=%d\n",
  197. __func__, mcr, retval);
  198. return retval;
  199. }
  200. static int metrousb_port_probe(struct usb_serial_port *port)
  201. {
  202. struct metrousb_private *metro_priv;
  203. metro_priv = kzalloc(sizeof(*metro_priv), GFP_KERNEL);
  204. if (!metro_priv)
  205. return -ENOMEM;
  206. spin_lock_init(&metro_priv->lock);
  207. usb_set_serial_port_data(port, metro_priv);
  208. return 0;
  209. }
  210. static int metrousb_port_remove(struct usb_serial_port *port)
  211. {
  212. struct metrousb_private *metro_priv;
  213. metro_priv = usb_get_serial_port_data(port);
  214. kfree(metro_priv);
  215. return 0;
  216. }
  217. static void metrousb_throttle(struct tty_struct *tty)
  218. {
  219. struct usb_serial_port *port = tty->driver_data;
  220. struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
  221. unsigned long flags = 0;
  222. /* Set the private information for the port to stop reading data. */
  223. spin_lock_irqsave(&metro_priv->lock, flags);
  224. metro_priv->throttled = 1;
  225. spin_unlock_irqrestore(&metro_priv->lock, flags);
  226. }
  227. static int metrousb_tiocmget(struct tty_struct *tty)
  228. {
  229. unsigned long control_state = 0;
  230. struct usb_serial_port *port = tty->driver_data;
  231. struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
  232. unsigned long flags = 0;
  233. spin_lock_irqsave(&metro_priv->lock, flags);
  234. control_state = metro_priv->control_state;
  235. spin_unlock_irqrestore(&metro_priv->lock, flags);
  236. return control_state;
  237. }
  238. static int metrousb_tiocmset(struct tty_struct *tty,
  239. unsigned int set, unsigned int clear)
  240. {
  241. struct usb_serial_port *port = tty->driver_data;
  242. struct usb_serial *serial = port->serial;
  243. struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
  244. unsigned long flags = 0;
  245. unsigned long control_state = 0;
  246. dev_dbg(tty->dev, "%s - set=%d, clear=%d\n", __func__, set, clear);
  247. spin_lock_irqsave(&metro_priv->lock, flags);
  248. control_state = metro_priv->control_state;
  249. /* Set the RTS and DTR values. */
  250. if (set & TIOCM_RTS)
  251. control_state |= TIOCM_RTS;
  252. if (set & TIOCM_DTR)
  253. control_state |= TIOCM_DTR;
  254. if (clear & TIOCM_RTS)
  255. control_state &= ~TIOCM_RTS;
  256. if (clear & TIOCM_DTR)
  257. control_state &= ~TIOCM_DTR;
  258. metro_priv->control_state = control_state;
  259. spin_unlock_irqrestore(&metro_priv->lock, flags);
  260. return metrousb_set_modem_ctrl(serial, control_state);
  261. }
  262. static void metrousb_unthrottle(struct tty_struct *tty)
  263. {
  264. struct usb_serial_port *port = tty->driver_data;
  265. struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
  266. unsigned long flags = 0;
  267. int result = 0;
  268. /* Set the private information for the port to resume reading data. */
  269. spin_lock_irqsave(&metro_priv->lock, flags);
  270. metro_priv->throttled = 0;
  271. spin_unlock_irqrestore(&metro_priv->lock, flags);
  272. /* Submit the urb to read from the port. */
  273. result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
  274. if (result)
  275. dev_err(tty->dev,
  276. "failed submitting interrupt in urb error code=%d\n",
  277. result);
  278. }
  279. static struct usb_serial_driver metrousb_device = {
  280. .driver = {
  281. .owner = THIS_MODULE,
  282. .name = "metro-usb",
  283. },
  284. .description = "Metrologic USB to Serial",
  285. .id_table = id_table,
  286. .num_interrupt_in = 1,
  287. .calc_num_ports = metrousb_calc_num_ports,
  288. .open = metrousb_open,
  289. .close = metrousb_cleanup,
  290. .read_int_callback = metrousb_read_int_callback,
  291. .port_probe = metrousb_port_probe,
  292. .port_remove = metrousb_port_remove,
  293. .throttle = metrousb_throttle,
  294. .unthrottle = metrousb_unthrottle,
  295. .tiocmget = metrousb_tiocmget,
  296. .tiocmset = metrousb_tiocmset,
  297. };
  298. static struct usb_serial_driver * const serial_drivers[] = {
  299. &metrousb_device,
  300. NULL,
  301. };
  302. module_usb_serial_driver(serial_drivers, id_table);
  303. MODULE_LICENSE("GPL v2");
  304. MODULE_AUTHOR("Philip Nicastro");
  305. MODULE_AUTHOR("Aleksey Babahin <tamerlan311@gmail.com>");
  306. MODULE_DESCRIPTION(DRIVER_DESC);