metro-usb.c 12 KB

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