metro-usb.c 11 KB

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