metro-usb.c 12 KB

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