metro-usb.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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. /* Clear the urb pipe. */
  186. usb_clear_halt(serial->dev, port->interrupt_in_urb->pipe);
  187. /* Start reading from the device */
  188. usb_fill_int_urb(port->interrupt_in_urb, serial->dev,
  189. usb_rcvintpipe(serial->dev, port->interrupt_in_endpointAddress),
  190. port->interrupt_in_urb->transfer_buffer,
  191. port->interrupt_in_urb->transfer_buffer_length,
  192. metrousb_read_int_callback, port, 1);
  193. result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  194. if (result) {
  195. dev_err(&port->dev,
  196. "%s - failed submitting interrupt in urb, error code=%d\n",
  197. __func__, result);
  198. goto exit;
  199. }
  200. /* Send activate cmd to device */
  201. result = metrousb_send_unidirectional_cmd(UNI_CMD_OPEN, port);
  202. if (result) {
  203. dev_err(&port->dev,
  204. "%s - failed to configure device for port number=%d, error code=%d\n",
  205. __func__, port->number, result);
  206. goto exit;
  207. }
  208. dev_dbg(&port->dev, "%s - port open\n", __func__);
  209. exit:
  210. return result;
  211. }
  212. static int metrousb_set_modem_ctrl(struct usb_serial *serial, unsigned int control_state)
  213. {
  214. int retval = 0;
  215. unsigned char mcr = METROUSB_MCR_NONE;
  216. dev_dbg(&serial->dev->dev, "%s - control state = %d\n",
  217. __func__, control_state);
  218. /* Set the modem control value. */
  219. if (control_state & TIOCM_DTR)
  220. mcr |= METROUSB_MCR_DTR;
  221. if (control_state & TIOCM_RTS)
  222. mcr |= METROUSB_MCR_RTS;
  223. /* Send the command to the usb port. */
  224. retval = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  225. METROUSB_SET_REQUEST_TYPE, METROUSB_SET_MODEM_CTRL_REQUEST,
  226. control_state, 0, NULL, 0, WDR_TIMEOUT);
  227. if (retval < 0)
  228. dev_err(&serial->dev->dev,
  229. "%s - set modem ctrl=0x%x failed, error code=%d\n",
  230. __func__, mcr, retval);
  231. return retval;
  232. }
  233. static void metrousb_shutdown(struct usb_serial *serial)
  234. {
  235. int i = 0;
  236. dev_dbg(&serial->dev->dev, "%s\n", __func__);
  237. /* Stop reading and writing on all ports. */
  238. for (i = 0; i < serial->num_ports; ++i) {
  239. /* Close any open urbs. */
  240. metrousb_cleanup(serial->port[i]);
  241. /* Free memory. */
  242. kfree(usb_get_serial_port_data(serial->port[i]));
  243. usb_set_serial_port_data(serial->port[i], NULL);
  244. dev_dbg(&serial->dev->dev, "%s - freed port number=%d\n",
  245. __func__, serial->port[i]->number);
  246. }
  247. }
  248. static int metrousb_startup(struct usb_serial *serial)
  249. {
  250. struct metrousb_private *metro_priv;
  251. struct usb_serial_port *port;
  252. int i = 0;
  253. dev_dbg(&serial->dev->dev, "%s\n", __func__);
  254. /* Loop through the serial ports setting up the private structures.
  255. * Currently we only use one port. */
  256. for (i = 0; i < serial->num_ports; ++i) {
  257. port = serial->port[i];
  258. /* Declare memory. */
  259. metro_priv = kzalloc(sizeof(struct metrousb_private), GFP_KERNEL);
  260. if (!metro_priv)
  261. return -ENOMEM;
  262. /* Initialize memory. */
  263. spin_lock_init(&metro_priv->lock);
  264. usb_set_serial_port_data(port, metro_priv);
  265. dev_dbg(&serial->dev->dev, "%s - port number=%d\n ",
  266. __func__, port->number);
  267. }
  268. return 0;
  269. }
  270. static void metrousb_throttle(struct tty_struct *tty)
  271. {
  272. struct usb_serial_port *port = tty->driver_data;
  273. struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
  274. unsigned long flags = 0;
  275. dev_dbg(tty->dev, "%s\n", __func__);
  276. /* Set the private information for the port to stop reading data. */
  277. spin_lock_irqsave(&metro_priv->lock, flags);
  278. metro_priv->throttled = 1;
  279. spin_unlock_irqrestore(&metro_priv->lock, flags);
  280. }
  281. static int metrousb_tiocmget(struct tty_struct *tty)
  282. {
  283. unsigned long control_state = 0;
  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. dev_dbg(tty->dev, "%s\n", __func__);
  288. spin_lock_irqsave(&metro_priv->lock, flags);
  289. control_state = metro_priv->control_state;
  290. spin_unlock_irqrestore(&metro_priv->lock, flags);
  291. return control_state;
  292. }
  293. static int metrousb_tiocmset(struct tty_struct *tty,
  294. unsigned int set, unsigned int clear)
  295. {
  296. struct usb_serial_port *port = tty->driver_data;
  297. struct usb_serial *serial = port->serial;
  298. struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
  299. unsigned long flags = 0;
  300. unsigned long control_state = 0;
  301. dev_dbg(tty->dev, "%s - set=%d, clear=%d\n", __func__, set, clear);
  302. spin_lock_irqsave(&metro_priv->lock, flags);
  303. control_state = metro_priv->control_state;
  304. /* Set the RTS and DTR values. */
  305. if (set & TIOCM_RTS)
  306. control_state |= TIOCM_RTS;
  307. if (set & TIOCM_DTR)
  308. control_state |= TIOCM_DTR;
  309. if (clear & TIOCM_RTS)
  310. control_state &= ~TIOCM_RTS;
  311. if (clear & TIOCM_DTR)
  312. control_state &= ~TIOCM_DTR;
  313. metro_priv->control_state = control_state;
  314. spin_unlock_irqrestore(&metro_priv->lock, flags);
  315. return metrousb_set_modem_ctrl(serial, control_state);
  316. }
  317. static void metrousb_unthrottle(struct tty_struct *tty)
  318. {
  319. struct usb_serial_port *port = tty->driver_data;
  320. struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
  321. unsigned long flags = 0;
  322. int result = 0;
  323. dev_dbg(tty->dev, "%s\n", __func__);
  324. /* Set the private information for the port to resume reading data. */
  325. spin_lock_irqsave(&metro_priv->lock, flags);
  326. metro_priv->throttled = 0;
  327. spin_unlock_irqrestore(&metro_priv->lock, flags);
  328. /* Submit the urb to read from the port. */
  329. port->interrupt_in_urb->dev = port->serial->dev;
  330. result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
  331. if (result)
  332. dev_err(tty->dev,
  333. "failed submitting interrupt in urb error code=%d\n",
  334. result);
  335. }
  336. static struct usb_serial_driver metrousb_device = {
  337. .driver = {
  338. .owner = THIS_MODULE,
  339. .name = "metro-usb",
  340. },
  341. .description = "Metrologic USB to Serial",
  342. .id_table = id_table,
  343. .num_ports = 1,
  344. .open = metrousb_open,
  345. .close = metrousb_cleanup,
  346. .read_int_callback = metrousb_read_int_callback,
  347. .write_int_callback = metrousb_write_int_callback,
  348. .attach = metrousb_startup,
  349. .release = metrousb_shutdown,
  350. .throttle = metrousb_throttle,
  351. .unthrottle = metrousb_unthrottle,
  352. .tiocmget = metrousb_tiocmget,
  353. .tiocmset = metrousb_tiocmset,
  354. };
  355. static struct usb_serial_driver * const serial_drivers[] = {
  356. &metrousb_device,
  357. NULL,
  358. };
  359. module_usb_serial_driver(serial_drivers, id_table);
  360. MODULE_LICENSE("GPL");
  361. MODULE_AUTHOR("Philip Nicastro");
  362. MODULE_AUTHOR("Aleksey Babahin <tamerlan311@gmail.com>");
  363. MODULE_DESCRIPTION(DRIVER_DESC);
  364. /* Module input parameters */
  365. module_param(debug, bool, S_IRUGO | S_IWUSR);
  366. MODULE_PARM_DESC(debug, "Print debug info (bool 1=on, 0=off)");