metro-usb.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. { USB_DEVICE_INTERFACE_CLASS(0x0c2e, 0x0730, 0xff) }, /* MS7820 */
  42. { }, /* Terminating entry. */
  43. };
  44. MODULE_DEVICE_TABLE(usb, id_table);
  45. /* UNI-Directional mode commands for device configure */
  46. #define UNI_CMD_OPEN 0x80
  47. #define UNI_CMD_CLOSE 0xFF
  48. static inline int metrousb_is_unidirectional_mode(struct usb_serial_port *port)
  49. {
  50. __u16 product_id = le16_to_cpu(
  51. port->serial->dev->descriptor.idProduct);
  52. return product_id == FOCUS_PRODUCT_ID_UNI;
  53. }
  54. static int metrousb_send_unidirectional_cmd(u8 cmd, struct usb_serial_port *port)
  55. {
  56. int ret;
  57. int actual_len;
  58. u8 *buffer_cmd = NULL;
  59. if (!metrousb_is_unidirectional_mode(port))
  60. return 0;
  61. buffer_cmd = kzalloc(sizeof(cmd), GFP_KERNEL);
  62. if (!buffer_cmd)
  63. return -ENOMEM;
  64. *buffer_cmd = cmd;
  65. ret = usb_interrupt_msg(port->serial->dev,
  66. usb_sndintpipe(port->serial->dev, port->interrupt_out_endpointAddress),
  67. buffer_cmd, sizeof(cmd),
  68. &actual_len, USB_CTRL_SET_TIMEOUT);
  69. kfree(buffer_cmd);
  70. if (ret < 0)
  71. return ret;
  72. else if (actual_len != sizeof(cmd))
  73. return -EIO;
  74. return 0;
  75. }
  76. static void metrousb_read_int_callback(struct urb *urb)
  77. {
  78. struct usb_serial_port *port = urb->context;
  79. struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
  80. unsigned char *data = urb->transfer_buffer;
  81. int throttled = 0;
  82. int result = 0;
  83. unsigned long flags = 0;
  84. dev_dbg(&port->dev, "%s\n", __func__);
  85. switch (urb->status) {
  86. case 0:
  87. /* Success status, read from the port. */
  88. break;
  89. case -ECONNRESET:
  90. case -ENOENT:
  91. case -ESHUTDOWN:
  92. /* urb has been terminated. */
  93. dev_dbg(&port->dev,
  94. "%s - urb shutting down, error code=%d\n",
  95. __func__, urb->status);
  96. return;
  97. default:
  98. dev_dbg(&port->dev,
  99. "%s - non-zero urb received, error code=%d\n",
  100. __func__, urb->status);
  101. goto exit;
  102. }
  103. /* Set the data read from the usb port into the serial port buffer. */
  104. if (urb->actual_length) {
  105. /* Loop through the data copying each byte to the tty layer. */
  106. tty_insert_flip_string(&port->port, data, urb->actual_length);
  107. /* Force the data to the tty layer. */
  108. tty_flip_buffer_push(&port->port);
  109. }
  110. /* Set any port variables. */
  111. spin_lock_irqsave(&metro_priv->lock, flags);
  112. throttled = metro_priv->throttled;
  113. spin_unlock_irqrestore(&metro_priv->lock, flags);
  114. /* Continue trying to read if set. */
  115. if (!throttled) {
  116. usb_fill_int_urb(port->interrupt_in_urb, port->serial->dev,
  117. usb_rcvintpipe(port->serial->dev, port->interrupt_in_endpointAddress),
  118. port->interrupt_in_urb->transfer_buffer,
  119. port->interrupt_in_urb->transfer_buffer_length,
  120. metrousb_read_int_callback, port, 1);
  121. result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
  122. if (result)
  123. dev_err(&port->dev,
  124. "%s - failed submitting interrupt in urb, error code=%d\n",
  125. __func__, result);
  126. }
  127. return;
  128. exit:
  129. /* Try to resubmit the urb. */
  130. result = usb_submit_urb(urb, GFP_ATOMIC);
  131. if (result)
  132. dev_err(&port->dev,
  133. "%s - failed submitting interrupt in urb, error code=%d\n",
  134. __func__, result);
  135. }
  136. static void metrousb_write_int_callback(struct urb *urb)
  137. {
  138. struct usb_serial_port *port = urb->context;
  139. dev_warn(&port->dev, "%s not implemented yet.\n",
  140. __func__);
  141. }
  142. static void metrousb_cleanup(struct usb_serial_port *port)
  143. {
  144. dev_dbg(&port->dev, "%s\n", __func__);
  145. usb_unlink_urb(port->interrupt_in_urb);
  146. usb_kill_urb(port->interrupt_in_urb);
  147. metrousb_send_unidirectional_cmd(UNI_CMD_CLOSE, port);
  148. }
  149. static int metrousb_open(struct tty_struct *tty, struct usb_serial_port *port)
  150. {
  151. struct usb_serial *serial = port->serial;
  152. struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
  153. unsigned long flags = 0;
  154. int result = 0;
  155. dev_dbg(&port->dev, "%s\n", __func__);
  156. /* Make sure the urb is initialized. */
  157. if (!port->interrupt_in_urb) {
  158. dev_err(&port->dev, "%s - interrupt urb not initialized\n",
  159. __func__);
  160. return -ENODEV;
  161. }
  162. /* Set the private data information for the port. */
  163. spin_lock_irqsave(&metro_priv->lock, flags);
  164. metro_priv->control_state = 0;
  165. metro_priv->throttled = 0;
  166. spin_unlock_irqrestore(&metro_priv->lock, flags);
  167. /* Clear the urb pipe. */
  168. usb_clear_halt(serial->dev, port->interrupt_in_urb->pipe);
  169. /* Start reading from the device */
  170. usb_fill_int_urb(port->interrupt_in_urb, serial->dev,
  171. usb_rcvintpipe(serial->dev, port->interrupt_in_endpointAddress),
  172. port->interrupt_in_urb->transfer_buffer,
  173. port->interrupt_in_urb->transfer_buffer_length,
  174. metrousb_read_int_callback, port, 1);
  175. result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  176. if (result) {
  177. dev_err(&port->dev,
  178. "%s - failed submitting interrupt in urb, error code=%d\n",
  179. __func__, result);
  180. goto exit;
  181. }
  182. /* Send activate cmd to device */
  183. result = metrousb_send_unidirectional_cmd(UNI_CMD_OPEN, port);
  184. if (result) {
  185. dev_err(&port->dev,
  186. "%s - failed to configure device, error code=%d\n",
  187. __func__, result);
  188. goto exit;
  189. }
  190. dev_dbg(&port->dev, "%s - port open\n", __func__);
  191. exit:
  192. return result;
  193. }
  194. static int metrousb_set_modem_ctrl(struct usb_serial *serial, unsigned int control_state)
  195. {
  196. int retval = 0;
  197. unsigned char mcr = METROUSB_MCR_NONE;
  198. dev_dbg(&serial->dev->dev, "%s - control state = %d\n",
  199. __func__, control_state);
  200. /* Set the modem control value. */
  201. if (control_state & TIOCM_DTR)
  202. mcr |= METROUSB_MCR_DTR;
  203. if (control_state & TIOCM_RTS)
  204. mcr |= METROUSB_MCR_RTS;
  205. /* Send the command to the usb port. */
  206. retval = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  207. METROUSB_SET_REQUEST_TYPE, METROUSB_SET_MODEM_CTRL_REQUEST,
  208. control_state, 0, NULL, 0, WDR_TIMEOUT);
  209. if (retval < 0)
  210. dev_err(&serial->dev->dev,
  211. "%s - set modem ctrl=0x%x failed, error code=%d\n",
  212. __func__, mcr, retval);
  213. return retval;
  214. }
  215. static int metrousb_port_probe(struct usb_serial_port *port)
  216. {
  217. struct metrousb_private *metro_priv;
  218. metro_priv = kzalloc(sizeof(*metro_priv), GFP_KERNEL);
  219. if (!metro_priv)
  220. return -ENOMEM;
  221. spin_lock_init(&metro_priv->lock);
  222. usb_set_serial_port_data(port, metro_priv);
  223. return 0;
  224. }
  225. static int metrousb_port_remove(struct usb_serial_port *port)
  226. {
  227. struct metrousb_private *metro_priv;
  228. metro_priv = usb_get_serial_port_data(port);
  229. kfree(metro_priv);
  230. return 0;
  231. }
  232. static void metrousb_throttle(struct tty_struct *tty)
  233. {
  234. struct usb_serial_port *port = tty->driver_data;
  235. struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
  236. unsigned long flags = 0;
  237. dev_dbg(tty->dev, "%s\n", __func__);
  238. /* Set the private information for the port to stop reading data. */
  239. spin_lock_irqsave(&metro_priv->lock, flags);
  240. metro_priv->throttled = 1;
  241. spin_unlock_irqrestore(&metro_priv->lock, flags);
  242. }
  243. static int metrousb_tiocmget(struct tty_struct *tty)
  244. {
  245. unsigned long control_state = 0;
  246. struct usb_serial_port *port = tty->driver_data;
  247. struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
  248. unsigned long flags = 0;
  249. dev_dbg(tty->dev, "%s\n", __func__);
  250. spin_lock_irqsave(&metro_priv->lock, flags);
  251. control_state = metro_priv->control_state;
  252. spin_unlock_irqrestore(&metro_priv->lock, flags);
  253. return control_state;
  254. }
  255. static int metrousb_tiocmset(struct tty_struct *tty,
  256. unsigned int set, unsigned int clear)
  257. {
  258. struct usb_serial_port *port = tty->driver_data;
  259. struct usb_serial *serial = port->serial;
  260. struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
  261. unsigned long flags = 0;
  262. unsigned long control_state = 0;
  263. dev_dbg(tty->dev, "%s - set=%d, clear=%d\n", __func__, set, clear);
  264. spin_lock_irqsave(&metro_priv->lock, flags);
  265. control_state = metro_priv->control_state;
  266. /* Set the RTS and DTR values. */
  267. if (set & TIOCM_RTS)
  268. control_state |= TIOCM_RTS;
  269. if (set & TIOCM_DTR)
  270. control_state |= TIOCM_DTR;
  271. if (clear & TIOCM_RTS)
  272. control_state &= ~TIOCM_RTS;
  273. if (clear & TIOCM_DTR)
  274. control_state &= ~TIOCM_DTR;
  275. metro_priv->control_state = control_state;
  276. spin_unlock_irqrestore(&metro_priv->lock, flags);
  277. return metrousb_set_modem_ctrl(serial, control_state);
  278. }
  279. static void metrousb_unthrottle(struct tty_struct *tty)
  280. {
  281. struct usb_serial_port *port = tty->driver_data;
  282. struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
  283. unsigned long flags = 0;
  284. int result = 0;
  285. dev_dbg(tty->dev, "%s\n", __func__);
  286. /* Set the private information for the port to resume reading data. */
  287. spin_lock_irqsave(&metro_priv->lock, flags);
  288. metro_priv->throttled = 0;
  289. spin_unlock_irqrestore(&metro_priv->lock, flags);
  290. /* Submit the urb to read from the port. */
  291. port->interrupt_in_urb->dev = port->serial->dev;
  292. result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
  293. if (result)
  294. dev_err(tty->dev,
  295. "failed submitting interrupt in urb error code=%d\n",
  296. result);
  297. }
  298. static struct usb_serial_driver metrousb_device = {
  299. .driver = {
  300. .owner = THIS_MODULE,
  301. .name = "metro-usb",
  302. },
  303. .description = "Metrologic USB to Serial",
  304. .id_table = id_table,
  305. .num_ports = 1,
  306. .open = metrousb_open,
  307. .close = metrousb_cleanup,
  308. .read_int_callback = metrousb_read_int_callback,
  309. .write_int_callback = metrousb_write_int_callback,
  310. .port_probe = metrousb_port_probe,
  311. .port_remove = metrousb_port_remove,
  312. .throttle = metrousb_throttle,
  313. .unthrottle = metrousb_unthrottle,
  314. .tiocmget = metrousb_tiocmget,
  315. .tiocmset = metrousb_tiocmset,
  316. };
  317. static struct usb_serial_driver * const serial_drivers[] = {
  318. &metrousb_device,
  319. NULL,
  320. };
  321. module_usb_serial_driver(serial_drivers, id_table);
  322. MODULE_LICENSE("GPL");
  323. MODULE_AUTHOR("Philip Nicastro");
  324. MODULE_AUTHOR("Aleksey Babahin <tamerlan311@gmail.com>");
  325. MODULE_DESCRIPTION(DRIVER_DESC);