metro-usb.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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/errno.h>
  19. #include <linux/uaccess.h>
  20. #include <linux/usb/serial.h>
  21. /* Version Information */
  22. #define DRIVER_VERSION "v1.2.0.0"
  23. #define DRIVER_DESC "Metrologic Instruments Inc. - USB-POS driver"
  24. /* Product information. */
  25. #define FOCUS_VENDOR_ID 0x0C2E
  26. #define FOCUS_PRODUCT_ID_BI 0x0720
  27. #define FOCUS_PRODUCT_ID_UNI 0x0700
  28. #define METROUSB_SET_REQUEST_TYPE 0x40
  29. #define METROUSB_SET_MODEM_CTRL_REQUEST 10
  30. #define METROUSB_SET_BREAK_REQUEST 0x40
  31. #define METROUSB_MCR_NONE 0x08 /* Deactivate DTR and RTS. */
  32. #define METROUSB_MCR_RTS 0x0a /* Activate RTS. */
  33. #define METROUSB_MCR_DTR 0x09 /* Activate DTR. */
  34. #define WDR_TIMEOUT 5000 /* default urb timeout. */
  35. /* Private data structure. */
  36. struct metrousb_private {
  37. spinlock_t lock;
  38. int throttled;
  39. unsigned long control_state;
  40. };
  41. /* Device table list. */
  42. static struct usb_device_id id_table[] = {
  43. { USB_DEVICE(FOCUS_VENDOR_ID, FOCUS_PRODUCT_ID_BI) },
  44. { USB_DEVICE(FOCUS_VENDOR_ID, FOCUS_PRODUCT_ID_UNI) },
  45. { }, /* Terminating entry. */
  46. };
  47. MODULE_DEVICE_TABLE(usb, id_table);
  48. /* Input parameter constants. */
  49. static bool debug;
  50. static void metrousb_read_int_callback(struct urb *urb)
  51. {
  52. struct usb_serial_port *port = urb->context;
  53. struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
  54. struct tty_struct *tty;
  55. unsigned char *data = urb->transfer_buffer;
  56. int throttled = 0;
  57. int result = 0;
  58. unsigned long flags = 0;
  59. dev_dbg(&port->dev, "%s\n", __func__);
  60. switch (urb->status) {
  61. case 0:
  62. /* Success status, read from the port. */
  63. break;
  64. case -ECONNRESET:
  65. case -ENOENT:
  66. case -ESHUTDOWN:
  67. /* urb has been terminated. */
  68. dev_dbg(&port->dev,
  69. "%s - urb shutting down, error code=%d\n",
  70. __func__, result);
  71. return;
  72. default:
  73. dev_dbg(&port->dev,
  74. "%s - non-zero urb received, error code=%d\n",
  75. __func__, result);
  76. goto exit;
  77. }
  78. /* Set the data read from the usb port into the serial port buffer. */
  79. tty = tty_port_tty_get(&port->port);
  80. if (!tty) {
  81. dev_dbg(&port->dev, "%s - bad tty pointer - exiting\n",
  82. __func__);
  83. return;
  84. }
  85. if (tty && urb->actual_length) {
  86. /* Loop through the data copying each byte to the tty layer. */
  87. tty_insert_flip_string(tty, data, urb->actual_length);
  88. /* Force the data to the tty layer. */
  89. tty_flip_buffer_push(tty);
  90. }
  91. tty_kref_put(tty);
  92. /* Set any port variables. */
  93. spin_lock_irqsave(&metro_priv->lock, flags);
  94. throttled = metro_priv->throttled;
  95. spin_unlock_irqrestore(&metro_priv->lock, flags);
  96. /* Continue trying to read if set. */
  97. if (!throttled) {
  98. usb_fill_int_urb(port->interrupt_in_urb, port->serial->dev,
  99. usb_rcvintpipe(port->serial->dev, port->interrupt_in_endpointAddress),
  100. port->interrupt_in_urb->transfer_buffer,
  101. port->interrupt_in_urb->transfer_buffer_length,
  102. metrousb_read_int_callback, port, 1);
  103. result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
  104. if (result)
  105. dev_dbg(&port->dev,
  106. "%s - failed submitting interrupt in urb, error code=%d\n",
  107. __func__, result);
  108. }
  109. return;
  110. exit:
  111. /* Try to resubmit the urb. */
  112. result = usb_submit_urb(urb, GFP_ATOMIC);
  113. if (result)
  114. dev_dbg(&port->dev,
  115. "%s - failed submitting interrupt in urb, error code=%d\n",
  116. __func__, result);
  117. }
  118. static void metrousb_cleanup(struct usb_serial_port *port)
  119. {
  120. dev_dbg(&port->dev, "%s\n", __func__);
  121. if (port->serial->dev) {
  122. /* Shutdown any interrupt in urbs. */
  123. if (port->interrupt_in_urb) {
  124. usb_unlink_urb(port->interrupt_in_urb);
  125. usb_kill_urb(port->interrupt_in_urb);
  126. }
  127. }
  128. }
  129. static int metrousb_open(struct tty_struct *tty, struct usb_serial_port *port)
  130. {
  131. struct usb_serial *serial = port->serial;
  132. struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
  133. unsigned long flags = 0;
  134. int result = 0;
  135. dev_dbg(&port->dev, "%s\n", __func__);
  136. /* Make sure the urb is initialized. */
  137. if (!port->interrupt_in_urb) {
  138. dev_dbg(&port->dev, "%s - interrupt urb not initialized\n",
  139. __func__);
  140. return -ENODEV;
  141. }
  142. /* Set the private data information for the port. */
  143. spin_lock_irqsave(&metro_priv->lock, flags);
  144. metro_priv->control_state = 0;
  145. metro_priv->throttled = 0;
  146. spin_unlock_irqrestore(&metro_priv->lock, flags);
  147. /* Clear the urb pipe. */
  148. usb_clear_halt(serial->dev, port->interrupt_in_urb->pipe);
  149. /* Start reading from the device */
  150. usb_fill_int_urb(port->interrupt_in_urb, serial->dev,
  151. usb_rcvintpipe(serial->dev, port->interrupt_in_endpointAddress),
  152. port->interrupt_in_urb->transfer_buffer,
  153. port->interrupt_in_urb->transfer_buffer_length,
  154. metrousb_read_int_callback, port, 1);
  155. result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  156. if (result) {
  157. dev_dbg(&port->dev,
  158. "%s - failed submitting interrupt in urb, error code=%d\n",
  159. __func__, result);
  160. goto exit;
  161. }
  162. dev_dbg(&port->dev, "%s - port open\n", __func__);
  163. exit:
  164. return result;
  165. }
  166. static int metrousb_set_modem_ctrl(struct usb_serial *serial, unsigned int control_state)
  167. {
  168. int retval = 0;
  169. unsigned char mcr = METROUSB_MCR_NONE;
  170. dev_dbg(&serial->dev->dev, "%s - control state = %d\n",
  171. __func__, control_state);
  172. /* Set the modem control value. */
  173. if (control_state & TIOCM_DTR)
  174. mcr |= METROUSB_MCR_DTR;
  175. if (control_state & TIOCM_RTS)
  176. mcr |= METROUSB_MCR_RTS;
  177. /* Send the command to the usb port. */
  178. retval = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  179. METROUSB_SET_REQUEST_TYPE, METROUSB_SET_MODEM_CTRL_REQUEST,
  180. control_state, 0, NULL, 0, WDR_TIMEOUT);
  181. if (retval < 0)
  182. dev_dbg(&serial->dev->dev,
  183. "%s - set modem ctrl=0x%x failed, error code=%d\n",
  184. __func__, mcr, retval);
  185. return retval;
  186. }
  187. static void metrousb_shutdown(struct usb_serial *serial)
  188. {
  189. int i = 0;
  190. dev_dbg(&serial->dev->dev, "%s\n", __func__);
  191. /* Stop reading and writing on all ports. */
  192. for (i = 0; i < serial->num_ports; ++i) {
  193. /* Close any open urbs. */
  194. metrousb_cleanup(serial->port[i]);
  195. /* Free memory. */
  196. kfree(usb_get_serial_port_data(serial->port[i]));
  197. usb_set_serial_port_data(serial->port[i], NULL);
  198. dev_dbg(&serial->dev->dev, "%s - freed port number=%d\n",
  199. __func__, serial->port[i]->number);
  200. }
  201. }
  202. static int metrousb_startup(struct usb_serial *serial)
  203. {
  204. struct metrousb_private *metro_priv;
  205. struct usb_serial_port *port;
  206. int i = 0;
  207. dev_dbg(&serial->dev->dev, "%s\n", __func__);
  208. /* Loop through the serial ports setting up the private structures.
  209. * Currently we only use one port. */
  210. for (i = 0; i < serial->num_ports; ++i) {
  211. port = serial->port[i];
  212. /* Declare memory. */
  213. metro_priv = kzalloc(sizeof(struct metrousb_private), GFP_KERNEL);
  214. if (!metro_priv)
  215. return -ENOMEM;
  216. /* Initialize memory. */
  217. spin_lock_init(&metro_priv->lock);
  218. usb_set_serial_port_data(port, metro_priv);
  219. dev_dbg(&serial->dev->dev, "%s - port number=%d\n ",
  220. __func__, port->number);
  221. }
  222. return 0;
  223. }
  224. static void metrousb_throttle(struct tty_struct *tty)
  225. {
  226. struct usb_serial_port *port = tty->driver_data;
  227. struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
  228. unsigned long flags = 0;
  229. dev_dbg(tty->dev, "%s\n", __func__);
  230. /* Set the private information for the port to stop reading data. */
  231. spin_lock_irqsave(&metro_priv->lock, flags);
  232. metro_priv->throttled = 1;
  233. spin_unlock_irqrestore(&metro_priv->lock, flags);
  234. }
  235. static int metrousb_tiocmget(struct tty_struct *tty)
  236. {
  237. unsigned long control_state = 0;
  238. struct usb_serial_port *port = tty->driver_data;
  239. struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
  240. unsigned long flags = 0;
  241. dev_dbg(tty->dev, "%s\n", __func__);
  242. spin_lock_irqsave(&metro_priv->lock, flags);
  243. control_state = metro_priv->control_state;
  244. spin_unlock_irqrestore(&metro_priv->lock, flags);
  245. return control_state;
  246. }
  247. static int metrousb_tiocmset(struct tty_struct *tty,
  248. unsigned int set, unsigned int clear)
  249. {
  250. struct usb_serial_port *port = tty->driver_data;
  251. struct usb_serial *serial = port->serial;
  252. struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
  253. unsigned long flags = 0;
  254. unsigned long control_state = 0;
  255. dev_dbg(tty->dev, "%s - set=%d, clear=%d\n", __func__, set, clear);
  256. spin_lock_irqsave(&metro_priv->lock, flags);
  257. control_state = metro_priv->control_state;
  258. /* Set the RTS and DTR values. */
  259. if (set & TIOCM_RTS)
  260. control_state |= TIOCM_RTS;
  261. if (set & TIOCM_DTR)
  262. control_state |= TIOCM_DTR;
  263. if (clear & TIOCM_RTS)
  264. control_state &= ~TIOCM_RTS;
  265. if (clear & TIOCM_DTR)
  266. control_state &= ~TIOCM_DTR;
  267. metro_priv->control_state = control_state;
  268. spin_unlock_irqrestore(&metro_priv->lock, flags);
  269. return metrousb_set_modem_ctrl(serial, control_state);
  270. }
  271. static void metrousb_unthrottle(struct tty_struct *tty)
  272. {
  273. struct usb_serial_port *port = tty->driver_data;
  274. struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
  275. unsigned long flags = 0;
  276. int result = 0;
  277. dev_dbg(tty->dev, "%s\n", __func__);
  278. /* Set the private information for the port to resume reading data. */
  279. spin_lock_irqsave(&metro_priv->lock, flags);
  280. metro_priv->throttled = 0;
  281. spin_unlock_irqrestore(&metro_priv->lock, flags);
  282. /* Submit the urb to read from the port. */
  283. port->interrupt_in_urb->dev = port->serial->dev;
  284. result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
  285. if (result)
  286. dev_dbg(tty->dev,
  287. "failed submitting interrupt in urb error code=%d\n",
  288. result);
  289. }
  290. static struct usb_driver metrousb_driver = {
  291. .name = "metro-usb",
  292. .probe = usb_serial_probe,
  293. .disconnect = usb_serial_disconnect,
  294. .id_table = id_table
  295. };
  296. static struct usb_serial_driver metrousb_device = {
  297. .driver = {
  298. .owner = THIS_MODULE,
  299. .name = "metro-usb",
  300. },
  301. .description = "Metrologic USB to serial converter.",
  302. .id_table = id_table,
  303. .num_ports = 1,
  304. .open = metrousb_open,
  305. .close = metrousb_cleanup,
  306. .read_int_callback = metrousb_read_int_callback,
  307. .attach = metrousb_startup,
  308. .release = metrousb_shutdown,
  309. .throttle = metrousb_throttle,
  310. .unthrottle = metrousb_unthrottle,
  311. .tiocmget = metrousb_tiocmget,
  312. .tiocmset = metrousb_tiocmset,
  313. };
  314. static struct usb_serial_driver * const serial_drivers[] = {
  315. &metrousb_device,
  316. NULL,
  317. };
  318. module_usb_serial_driver(metrousb_driver, serial_drivers);
  319. MODULE_LICENSE("GPL");
  320. MODULE_AUTHOR("Philip Nicastro");
  321. MODULE_AUTHOR("Aleksey Babahin <tamerlan311@gmail.com>");
  322. MODULE_DESCRIPTION(DRIVER_DESC);
  323. /* Module input parameters */
  324. module_param(debug, bool, S_IRUGO | S_IWUSR);
  325. MODULE_PARM_DESC(debug, "Print debug info (bool 1=on, 0=off)");