metro-usb.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. if (throttled)
  114. return;
  115. exit:
  116. /* Try to resubmit the urb. */
  117. result = usb_submit_urb(urb, GFP_ATOMIC);
  118. if (result)
  119. dev_err(&port->dev,
  120. "%s - failed submitting interrupt in urb, error code=%d\n",
  121. __func__, result);
  122. }
  123. static void metrousb_cleanup(struct usb_serial_port *port)
  124. {
  125. usb_kill_urb(port->interrupt_in_urb);
  126. metrousb_send_unidirectional_cmd(UNI_CMD_CLOSE, port);
  127. }
  128. static int metrousb_open(struct tty_struct *tty, struct usb_serial_port *port)
  129. {
  130. struct usb_serial *serial = port->serial;
  131. struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
  132. unsigned long flags = 0;
  133. int result = 0;
  134. /* Make sure the urb is initialized. */
  135. if (!port->interrupt_in_urb) {
  136. dev_err(&port->dev, "%s - interrupt urb not initialized\n",
  137. __func__);
  138. return -ENODEV;
  139. }
  140. /* Set the private data information for the port. */
  141. spin_lock_irqsave(&metro_priv->lock, flags);
  142. metro_priv->control_state = 0;
  143. metro_priv->throttled = 0;
  144. spin_unlock_irqrestore(&metro_priv->lock, flags);
  145. /* Clear the urb pipe. */
  146. usb_clear_halt(serial->dev, port->interrupt_in_urb->pipe);
  147. /* Start reading from the device */
  148. usb_fill_int_urb(port->interrupt_in_urb, serial->dev,
  149. usb_rcvintpipe(serial->dev, port->interrupt_in_endpointAddress),
  150. port->interrupt_in_urb->transfer_buffer,
  151. port->interrupt_in_urb->transfer_buffer_length,
  152. metrousb_read_int_callback, port, 1);
  153. result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  154. if (result) {
  155. dev_err(&port->dev,
  156. "%s - failed submitting interrupt in urb, error code=%d\n",
  157. __func__, result);
  158. goto exit;
  159. }
  160. /* Send activate cmd to device */
  161. result = metrousb_send_unidirectional_cmd(UNI_CMD_OPEN, port);
  162. if (result) {
  163. dev_err(&port->dev,
  164. "%s - failed to configure device, error code=%d\n",
  165. __func__, result);
  166. goto exit;
  167. }
  168. exit:
  169. return result;
  170. }
  171. static int metrousb_set_modem_ctrl(struct usb_serial *serial, unsigned int control_state)
  172. {
  173. int retval = 0;
  174. unsigned char mcr = METROUSB_MCR_NONE;
  175. dev_dbg(&serial->dev->dev, "%s - control state = %d\n",
  176. __func__, control_state);
  177. /* Set the modem control value. */
  178. if (control_state & TIOCM_DTR)
  179. mcr |= METROUSB_MCR_DTR;
  180. if (control_state & TIOCM_RTS)
  181. mcr |= METROUSB_MCR_RTS;
  182. /* Send the command to the usb port. */
  183. retval = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  184. METROUSB_SET_REQUEST_TYPE, METROUSB_SET_MODEM_CTRL_REQUEST,
  185. control_state, 0, NULL, 0, WDR_TIMEOUT);
  186. if (retval < 0)
  187. dev_err(&serial->dev->dev,
  188. "%s - set modem ctrl=0x%x failed, error code=%d\n",
  189. __func__, mcr, retval);
  190. return retval;
  191. }
  192. static int metrousb_port_probe(struct usb_serial_port *port)
  193. {
  194. struct metrousb_private *metro_priv;
  195. metro_priv = kzalloc(sizeof(*metro_priv), GFP_KERNEL);
  196. if (!metro_priv)
  197. return -ENOMEM;
  198. spin_lock_init(&metro_priv->lock);
  199. usb_set_serial_port_data(port, metro_priv);
  200. return 0;
  201. }
  202. static int metrousb_port_remove(struct usb_serial_port *port)
  203. {
  204. struct metrousb_private *metro_priv;
  205. metro_priv = usb_get_serial_port_data(port);
  206. kfree(metro_priv);
  207. return 0;
  208. }
  209. static void metrousb_throttle(struct tty_struct *tty)
  210. {
  211. struct usb_serial_port *port = tty->driver_data;
  212. struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
  213. unsigned long flags = 0;
  214. /* Set the private information for the port to stop reading data. */
  215. spin_lock_irqsave(&metro_priv->lock, flags);
  216. metro_priv->throttled = 1;
  217. spin_unlock_irqrestore(&metro_priv->lock, flags);
  218. }
  219. static int metrousb_tiocmget(struct tty_struct *tty)
  220. {
  221. unsigned long control_state = 0;
  222. struct usb_serial_port *port = tty->driver_data;
  223. struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
  224. unsigned long flags = 0;
  225. spin_lock_irqsave(&metro_priv->lock, flags);
  226. control_state = metro_priv->control_state;
  227. spin_unlock_irqrestore(&metro_priv->lock, flags);
  228. return control_state;
  229. }
  230. static int metrousb_tiocmset(struct tty_struct *tty,
  231. unsigned int set, unsigned int clear)
  232. {
  233. struct usb_serial_port *port = tty->driver_data;
  234. struct usb_serial *serial = port->serial;
  235. struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
  236. unsigned long flags = 0;
  237. unsigned long control_state = 0;
  238. dev_dbg(tty->dev, "%s - set=%d, clear=%d\n", __func__, set, clear);
  239. spin_lock_irqsave(&metro_priv->lock, flags);
  240. control_state = metro_priv->control_state;
  241. /* Set the RTS and DTR values. */
  242. if (set & TIOCM_RTS)
  243. control_state |= TIOCM_RTS;
  244. if (set & TIOCM_DTR)
  245. control_state |= TIOCM_DTR;
  246. if (clear & TIOCM_RTS)
  247. control_state &= ~TIOCM_RTS;
  248. if (clear & TIOCM_DTR)
  249. control_state &= ~TIOCM_DTR;
  250. metro_priv->control_state = control_state;
  251. spin_unlock_irqrestore(&metro_priv->lock, flags);
  252. return metrousb_set_modem_ctrl(serial, control_state);
  253. }
  254. static void metrousb_unthrottle(struct tty_struct *tty)
  255. {
  256. struct usb_serial_port *port = tty->driver_data;
  257. struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
  258. unsigned long flags = 0;
  259. int result = 0;
  260. /* Set the private information for the port to resume reading data. */
  261. spin_lock_irqsave(&metro_priv->lock, flags);
  262. metro_priv->throttled = 0;
  263. spin_unlock_irqrestore(&metro_priv->lock, flags);
  264. /* Submit the urb to read from the port. */
  265. result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
  266. if (result)
  267. dev_err(tty->dev,
  268. "failed submitting interrupt in urb error code=%d\n",
  269. result);
  270. }
  271. static struct usb_serial_driver metrousb_device = {
  272. .driver = {
  273. .owner = THIS_MODULE,
  274. .name = "metro-usb",
  275. },
  276. .description = "Metrologic USB to Serial",
  277. .id_table = id_table,
  278. .num_ports = 1,
  279. .open = metrousb_open,
  280. .close = metrousb_cleanup,
  281. .read_int_callback = metrousb_read_int_callback,
  282. .port_probe = metrousb_port_probe,
  283. .port_remove = metrousb_port_remove,
  284. .throttle = metrousb_throttle,
  285. .unthrottle = metrousb_unthrottle,
  286. .tiocmget = metrousb_tiocmget,
  287. .tiocmset = metrousb_tiocmset,
  288. };
  289. static struct usb_serial_driver * const serial_drivers[] = {
  290. &metrousb_device,
  291. NULL,
  292. };
  293. module_usb_serial_driver(serial_drivers, id_table);
  294. MODULE_LICENSE("GPL");
  295. MODULE_AUTHOR("Philip Nicastro");
  296. MODULE_AUTHOR("Aleksey Babahin <tamerlan311@gmail.com>");
  297. MODULE_DESCRIPTION(DRIVER_DESC);