metro-usb.c 14 KB

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