metro-usb.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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. /* UNI-Directional mode commands for device configure */
  51. #define UNI_CMD_OPEN 0x80
  52. #define UNI_CMD_CLOSE 0xFF
  53. inline int metrousb_is_unidirectional_mode(struct usb_serial_port *port)
  54. {
  55. __u16 product_id = le16_to_cpu(
  56. port->serial->dev->descriptor.idProduct);
  57. return product_id == FOCUS_PRODUCT_ID_UNI;
  58. }
  59. static int metrousb_send_unidirectional_cmd(u8 cmd, struct usb_serial_port *port)
  60. {
  61. int ret;
  62. int actual_len;
  63. u8 *buffer_cmd = NULL;
  64. if (!metrousb_is_unidirectional_mode(port))
  65. return 0;
  66. buffer_cmd = kzalloc(sizeof(cmd), GFP_KERNEL);
  67. if (!buffer_cmd)
  68. return -ENOMEM;
  69. *buffer_cmd = cmd;
  70. ret = usb_interrupt_msg(port->serial->dev,
  71. usb_sndintpipe(port->serial->dev, port->interrupt_out_endpointAddress),
  72. buffer_cmd, sizeof(cmd),
  73. &actual_len, USB_CTRL_SET_TIMEOUT);
  74. kfree(buffer_cmd);
  75. if (ret < 0)
  76. return ret;
  77. else if (actual_len != sizeof(cmd))
  78. return -EIO;
  79. return 0;
  80. }
  81. static void metrousb_read_int_callback(struct urb *urb)
  82. {
  83. struct usb_serial_port *port = urb->context;
  84. struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
  85. struct tty_struct *tty;
  86. unsigned char *data = urb->transfer_buffer;
  87. int throttled = 0;
  88. int result = 0;
  89. unsigned long flags = 0;
  90. dev_dbg(&port->dev, "%s\n", __func__);
  91. switch (urb->status) {
  92. case 0:
  93. /* Success status, read from the port. */
  94. break;
  95. case -ECONNRESET:
  96. case -ENOENT:
  97. case -ESHUTDOWN:
  98. /* urb has been terminated. */
  99. dev_dbg(&port->dev,
  100. "%s - urb shutting down, error code=%d\n",
  101. __func__, result);
  102. return;
  103. default:
  104. dev_dbg(&port->dev,
  105. "%s - non-zero urb received, error code=%d\n",
  106. __func__, result);
  107. goto exit;
  108. }
  109. /* Set the data read from the usb port into the serial port buffer. */
  110. tty = tty_port_tty_get(&port->port);
  111. if (!tty) {
  112. dev_dbg(&port->dev, "%s - bad tty pointer - exiting\n",
  113. __func__);
  114. return;
  115. }
  116. if (tty && urb->actual_length) {
  117. /* Loop through the data copying each byte to the tty layer. */
  118. tty_insert_flip_string(tty, data, urb->actual_length);
  119. /* Force the data to the tty layer. */
  120. tty_flip_buffer_push(tty);
  121. }
  122. tty_kref_put(tty);
  123. /* Set any port variables. */
  124. spin_lock_irqsave(&metro_priv->lock, flags);
  125. throttled = metro_priv->throttled;
  126. spin_unlock_irqrestore(&metro_priv->lock, flags);
  127. /* Continue trying to read if set. */
  128. if (!throttled) {
  129. usb_fill_int_urb(port->interrupt_in_urb, port->serial->dev,
  130. usb_rcvintpipe(port->serial->dev, port->interrupt_in_endpointAddress),
  131. port->interrupt_in_urb->transfer_buffer,
  132. port->interrupt_in_urb->transfer_buffer_length,
  133. metrousb_read_int_callback, port, 1);
  134. result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
  135. if (result)
  136. dev_dbg(&port->dev,
  137. "%s - failed submitting interrupt in urb, error code=%d\n",
  138. __func__, result);
  139. }
  140. return;
  141. exit:
  142. /* Try to resubmit the urb. */
  143. result = usb_submit_urb(urb, GFP_ATOMIC);
  144. if (result)
  145. dev_dbg(&port->dev,
  146. "%s - failed submitting interrupt in urb, error code=%d\n",
  147. __func__, result);
  148. }
  149. static void metrousb_write_int_callback(struct urb *urb)
  150. {
  151. struct usb_serial_port *port = urb->context;
  152. dev_warn(&port->dev, "%s not implemented yet.\n",
  153. __func__);
  154. }
  155. static void metrousb_cleanup(struct usb_serial_port *port)
  156. {
  157. dev_dbg(&port->dev, "%s\n", __func__);
  158. if (port->serial->dev) {
  159. /* Shutdown any interrupt in urbs. */
  160. if (port->interrupt_in_urb) {
  161. usb_unlink_urb(port->interrupt_in_urb);
  162. usb_kill_urb(port->interrupt_in_urb);
  163. }
  164. /* Send deactivate cmd to device */
  165. metrousb_send_unidirectional_cmd(UNI_CMD_CLOSE, port);
  166. }
  167. }
  168. static int metrousb_open(struct tty_struct *tty, struct usb_serial_port *port)
  169. {
  170. struct usb_serial *serial = port->serial;
  171. struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
  172. unsigned long flags = 0;
  173. int result = 0;
  174. dev_dbg(&port->dev, "%s\n", __func__);
  175. /* Make sure the urb is initialized. */
  176. if (!port->interrupt_in_urb) {
  177. dev_dbg(&port->dev, "%s - interrupt urb not initialized\n",
  178. __func__);
  179. return -ENODEV;
  180. }
  181. /* Set the private data information for the port. */
  182. spin_lock_irqsave(&metro_priv->lock, flags);
  183. metro_priv->control_state = 0;
  184. metro_priv->throttled = 0;
  185. spin_unlock_irqrestore(&metro_priv->lock, flags);
  186. /*
  187. * Force low_latency on so that our tty_push actually forces the data
  188. * through, otherwise it is scheduled, and with high data rates (like
  189. * with OHCI) data can get lost.
  190. */
  191. if (tty)
  192. tty->low_latency = 1;
  193. /* Clear the urb pipe. */
  194. usb_clear_halt(serial->dev, port->interrupt_in_urb->pipe);
  195. /* Start reading from the device */
  196. usb_fill_int_urb(port->interrupt_in_urb, serial->dev,
  197. usb_rcvintpipe(serial->dev, port->interrupt_in_endpointAddress),
  198. port->interrupt_in_urb->transfer_buffer,
  199. port->interrupt_in_urb->transfer_buffer_length,
  200. metrousb_read_int_callback, port, 1);
  201. result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  202. if (result) {
  203. dev_dbg(&port->dev,
  204. "%s - failed submitting interrupt in urb, error code=%d\n",
  205. __func__, result);
  206. goto exit;
  207. }
  208. /* Send activate cmd to device */
  209. result = metrousb_send_unidirectional_cmd(UNI_CMD_OPEN, port);
  210. if (result) {
  211. dev_err(&port->dev,
  212. "%s - failed to configure device for port number=%d, error code=%d\n",
  213. __func__, port->number, result);
  214. goto exit;
  215. }
  216. dev_dbg(&port->dev, "%s - port open\n", __func__);
  217. exit:
  218. return result;
  219. }
  220. static int metrousb_set_modem_ctrl(struct usb_serial *serial, unsigned int control_state)
  221. {
  222. int retval = 0;
  223. unsigned char mcr = METROUSB_MCR_NONE;
  224. dev_dbg(&serial->dev->dev, "%s - control state = %d\n",
  225. __func__, control_state);
  226. /* Set the modem control value. */
  227. if (control_state & TIOCM_DTR)
  228. mcr |= METROUSB_MCR_DTR;
  229. if (control_state & TIOCM_RTS)
  230. mcr |= METROUSB_MCR_RTS;
  231. /* Send the command to the usb port. */
  232. retval = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  233. METROUSB_SET_REQUEST_TYPE, METROUSB_SET_MODEM_CTRL_REQUEST,
  234. control_state, 0, NULL, 0, WDR_TIMEOUT);
  235. if (retval < 0)
  236. dev_dbg(&serial->dev->dev,
  237. "%s - set modem ctrl=0x%x failed, error code=%d\n",
  238. __func__, mcr, retval);
  239. return retval;
  240. }
  241. static void metrousb_shutdown(struct usb_serial *serial)
  242. {
  243. int i = 0;
  244. dev_dbg(&serial->dev->dev, "%s\n", __func__);
  245. /* Stop reading and writing on all ports. */
  246. for (i = 0; i < serial->num_ports; ++i) {
  247. /* Close any open urbs. */
  248. metrousb_cleanup(serial->port[i]);
  249. /* Free memory. */
  250. kfree(usb_get_serial_port_data(serial->port[i]));
  251. usb_set_serial_port_data(serial->port[i], NULL);
  252. dev_dbg(&serial->dev->dev, "%s - freed port number=%d\n",
  253. __func__, serial->port[i]->number);
  254. }
  255. }
  256. static int metrousb_startup(struct usb_serial *serial)
  257. {
  258. struct metrousb_private *metro_priv;
  259. struct usb_serial_port *port;
  260. int i = 0;
  261. dev_dbg(&serial->dev->dev, "%s\n", __func__);
  262. /* Loop through the serial ports setting up the private structures.
  263. * Currently we only use one port. */
  264. for (i = 0; i < serial->num_ports; ++i) {
  265. port = serial->port[i];
  266. /* Declare memory. */
  267. metro_priv = kzalloc(sizeof(struct metrousb_private), GFP_KERNEL);
  268. if (!metro_priv)
  269. return -ENOMEM;
  270. /* Initialize memory. */
  271. spin_lock_init(&metro_priv->lock);
  272. usb_set_serial_port_data(port, metro_priv);
  273. dev_dbg(&serial->dev->dev, "%s - port number=%d\n ",
  274. __func__, port->number);
  275. }
  276. return 0;
  277. }
  278. static void metrousb_throttle(struct tty_struct *tty)
  279. {
  280. struct usb_serial_port *port = tty->driver_data;
  281. struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
  282. unsigned long flags = 0;
  283. dev_dbg(tty->dev, "%s\n", __func__);
  284. /* Set the private information for the port to stop reading data. */
  285. spin_lock_irqsave(&metro_priv->lock, flags);
  286. metro_priv->throttled = 1;
  287. spin_unlock_irqrestore(&metro_priv->lock, flags);
  288. }
  289. static int metrousb_tiocmget(struct tty_struct *tty)
  290. {
  291. unsigned long control_state = 0;
  292. struct usb_serial_port *port = tty->driver_data;
  293. struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
  294. unsigned long flags = 0;
  295. dev_dbg(tty->dev, "%s\n", __func__);
  296. spin_lock_irqsave(&metro_priv->lock, flags);
  297. control_state = metro_priv->control_state;
  298. spin_unlock_irqrestore(&metro_priv->lock, flags);
  299. return control_state;
  300. }
  301. static int metrousb_tiocmset(struct tty_struct *tty,
  302. unsigned int set, unsigned int clear)
  303. {
  304. struct usb_serial_port *port = tty->driver_data;
  305. struct usb_serial *serial = port->serial;
  306. struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
  307. unsigned long flags = 0;
  308. unsigned long control_state = 0;
  309. dev_dbg(tty->dev, "%s - set=%d, clear=%d\n", __func__, set, clear);
  310. spin_lock_irqsave(&metro_priv->lock, flags);
  311. control_state = metro_priv->control_state;
  312. /* Set the RTS and DTR values. */
  313. if (set & TIOCM_RTS)
  314. control_state |= TIOCM_RTS;
  315. if (set & TIOCM_DTR)
  316. control_state |= TIOCM_DTR;
  317. if (clear & TIOCM_RTS)
  318. control_state &= ~TIOCM_RTS;
  319. if (clear & TIOCM_DTR)
  320. control_state &= ~TIOCM_DTR;
  321. metro_priv->control_state = control_state;
  322. spin_unlock_irqrestore(&metro_priv->lock, flags);
  323. return metrousb_set_modem_ctrl(serial, control_state);
  324. }
  325. static void metrousb_unthrottle(struct tty_struct *tty)
  326. {
  327. struct usb_serial_port *port = tty->driver_data;
  328. struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
  329. unsigned long flags = 0;
  330. int result = 0;
  331. dev_dbg(tty->dev, "%s\n", __func__);
  332. /* Set the private information for the port to resume reading data. */
  333. spin_lock_irqsave(&metro_priv->lock, flags);
  334. metro_priv->throttled = 0;
  335. spin_unlock_irqrestore(&metro_priv->lock, flags);
  336. /* Submit the urb to read from the port. */
  337. port->interrupt_in_urb->dev = port->serial->dev;
  338. result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
  339. if (result)
  340. dev_dbg(tty->dev,
  341. "failed submitting interrupt in urb error code=%d\n",
  342. result);
  343. }
  344. static struct usb_driver metrousb_driver = {
  345. .name = "metro-usb",
  346. .probe = usb_serial_probe,
  347. .disconnect = usb_serial_disconnect,
  348. .id_table = id_table
  349. };
  350. static struct usb_serial_driver metrousb_device = {
  351. .driver = {
  352. .owner = THIS_MODULE,
  353. .name = "metro-usb",
  354. },
  355. .description = "Metrologic USB to serial converter.",
  356. .id_table = id_table,
  357. .num_ports = 1,
  358. .open = metrousb_open,
  359. .close = metrousb_cleanup,
  360. .read_int_callback = metrousb_read_int_callback,
  361. .write_int_callback = metrousb_write_int_callback,
  362. .attach = metrousb_startup,
  363. .release = metrousb_shutdown,
  364. .throttle = metrousb_throttle,
  365. .unthrottle = metrousb_unthrottle,
  366. .tiocmget = metrousb_tiocmget,
  367. .tiocmset = metrousb_tiocmset,
  368. };
  369. static struct usb_serial_driver * const serial_drivers[] = {
  370. &metrousb_device,
  371. NULL,
  372. };
  373. module_usb_serial_driver(metrousb_driver, serial_drivers);
  374. MODULE_LICENSE("GPL");
  375. MODULE_AUTHOR("Philip Nicastro");
  376. MODULE_AUTHOR("Aleksey Babahin <tamerlan311@gmail.com>");
  377. MODULE_DESCRIPTION(DRIVER_DESC);
  378. /* Module input parameters */
  379. module_param(debug, bool, S_IRUGO | S_IWUSR);
  380. MODULE_PARM_DESC(debug, "Print debug info (bool 1=on, 0=off)");