metro-usb.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. /*
  2. Date Created: 9/15/2006
  3. File Name: metro-usb.c
  4. Description: metro-usb.c is the drivers main source file. The driver is a USB to Serial converter.
  5. The driver takes USB data and sends it to a virtual ttyUSB# serial port.
  6. The driver interfaces with the usbserial.ko driver supplied by Linux.
  7. NOTES:
  8. To install the driver:
  9. 1. Install the usbserial.ko module supplied by Linux with: # insmod usbserial.ko
  10. 2. Install the metro-usb.ko module with: # insmod metro-usb.ko
  11. Some of this code is credited to Linux USB open source files that are distributed with Linux.
  12. Copyright: 2007 Metrologic Instruments. All rights reserved.
  13. Copyright: 2011 Azimut Ltd. <http://azimutrzn.ru/>
  14. Requirements: gedit.exe, notepad.exe
  15. Revision History:
  16. Date: Developer: Revisions:
  17. ------------------------------------------------------------------------------
  18. 1/30/2007 Philip Nicastro Initial release. (v1.0.0.0)
  19. 2/27/2007 Philip Nicastro Changed the metrousb_read_int_callback function to use a loop with the tty_insert_flip_char function to copy each byte to the tty layer. Removed the tty_buffer_request_room and the tty_insert_flip_string function calls. These calls were not supported on Fedora.
  20. 2/27/2007 Philip Nicastro Released. (v1.1.0.0)
  21. 10/07/2011 Aleksey Babahin Update for new kernel (tested on 2.6.38)
  22. Add unidirection mode support
  23. */
  24. #include <linux/kernel.h>
  25. #include <linux/init.h>
  26. #include <linux/tty.h>
  27. #include <linux/module.h>
  28. #include <linux/usb.h>
  29. #include <linux/errno.h>
  30. #include <linux/slab.h>
  31. #include <linux/tty_driver.h>
  32. #include <linux/tty_flip.h>
  33. #include <linux/moduleparam.h>
  34. #include <linux/spinlock.h>
  35. #include <asm/uaccess.h>
  36. #include <linux/errno.h>
  37. #include "metro-usb.h"
  38. #include <linux/usb/serial.h>
  39. /* Version Information */
  40. #define DRIVER_VERSION "v1.2.0.0"
  41. #define DRIVER_DESC "Metrologic Instruments Inc. - USB-POS driver"
  42. /* Device table list. */
  43. static struct usb_device_id id_table [] = {
  44. { USB_DEVICE(FOCUS_VENDOR_ID, FOCUS_PRODUCT_ID) },
  45. { USB_DEVICE(FOCUS_VENDOR_ID, FOCUS_PRODUCT_ID_UNI) },
  46. { }, /* Terminating entry. */
  47. };
  48. MODULE_DEVICE_TABLE(usb, id_table);
  49. /* Input parameter constants. */
  50. static bool debug;
  51. /* Function prototypes. */
  52. static void metrousb_cleanup (struct usb_serial_port *port);
  53. static void metrousb_close (struct usb_serial_port *port);
  54. static int metrousb_open (struct tty_struct *tty, struct usb_serial_port *port);
  55. static void metrousb_read_int_callback (struct urb *urb);
  56. static void metrousb_shutdown (struct usb_serial *serial);
  57. static int metrousb_startup (struct usb_serial *serial);
  58. static void metrousb_throttle(struct tty_struct *tty);
  59. static int metrousb_tiocmget(struct tty_struct *tty);
  60. static int metrousb_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear);
  61. static void metrousb_unthrottle(struct tty_struct *tty);
  62. /* Driver structure. */
  63. static struct usb_driver metrousb_driver = {
  64. .name = "metro-usb",
  65. .probe = usb_serial_probe,
  66. .disconnect = usb_serial_disconnect,
  67. .id_table = id_table
  68. };
  69. /* Device structure. */
  70. static struct usb_serial_driver metrousb_device = {
  71. .driver = {
  72. .owner = THIS_MODULE,
  73. .name = "metro-usb",
  74. },
  75. .description = "Metrologic USB to serial converter.",
  76. .id_table = id_table,
  77. .num_ports = 1,
  78. .open = metrousb_open,
  79. .close = metrousb_close,
  80. .read_int_callback = metrousb_read_int_callback,
  81. .attach = metrousb_startup,
  82. .release = metrousb_shutdown,
  83. .throttle = metrousb_throttle,
  84. .unthrottle = metrousb_unthrottle,
  85. .tiocmget = metrousb_tiocmget,
  86. .tiocmset = metrousb_tiocmset,
  87. };
  88. static struct usb_serial_driver * const serial_drivers[] = {
  89. &metrousb_device,
  90. NULL,
  91. };
  92. /* ----------------------------------------------------------------------------------------------
  93. Description:
  94. Clean up any urbs and port information.
  95. Input:
  96. struct usb_serial_port *: pointer to a usb_serial_port structure.
  97. Output:
  98. int: Returns true (0) if successful, false otherwise.
  99. */
  100. static void metrousb_cleanup (struct usb_serial_port *port)
  101. {
  102. dbg("METRO-USB - %s - port number=%d", __FUNCTION__, port->number);
  103. if (port->serial->dev) {
  104. /* Shutdown any interrupt in urbs. */
  105. if (port->interrupt_in_urb) {
  106. usb_unlink_urb(port->interrupt_in_urb);
  107. usb_kill_urb(port->interrupt_in_urb);
  108. }
  109. // temp
  110. // this will be needed for the write urb
  111. /* Shutdown any interrupt_out_urbs. */
  112. //if (serial->num_bulk_in)
  113. // usb_kill_urb(port->read_urb);
  114. }
  115. }
  116. /* ----------------------------------------------------------------------------------------------
  117. Description:
  118. Close the open serial port. Cleanup any open serial port information.
  119. Input:
  120. struct usb_serial_port *: pointer to a usb_serial_port structure.
  121. struct file *: pointer to a file structure.
  122. Output:
  123. int: Returns true (0) if successful, false otherwise.
  124. */
  125. static void metrousb_close (struct usb_serial_port *port)
  126. {
  127. dbg("METRO-USB - %s - port number=%d", __FUNCTION__, port->number);
  128. metrousb_cleanup(port);
  129. }
  130. /* ----------------------------------------------------------------------------------------------
  131. Description:
  132. Open the drivers serial port.
  133. Input:
  134. struct usb_serial_port *: pointer to a usb_serial_port structure.
  135. struct file *: pointer to a file structure.
  136. Output:
  137. int: Returns true (0) if successful, false otherwise.
  138. */
  139. static int metrousb_open (struct tty_struct *tty, struct usb_serial_port *port)
  140. {
  141. struct usb_serial *serial = port->serial;
  142. struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
  143. unsigned long flags = 0;
  144. int result = 0;
  145. dbg("METRO-USB - %s - port number=%d", __FUNCTION__, port->number);
  146. /* Make sure the urb is initialized. */
  147. if (!port->interrupt_in_urb) {
  148. dbg("METRO-USB - %s - interrupt urb not initialized for port number=%d", __FUNCTION__, port->number);
  149. return -ENODEV;
  150. }
  151. /* Set the private data information for the port. */
  152. spin_lock_irqsave(&metro_priv->lock, flags);
  153. metro_priv->control_state = 0;
  154. metro_priv->throttled = 0;
  155. spin_unlock_irqrestore(&metro_priv->lock, flags);
  156. /*
  157. * Force low_latency on so that our tty_push actually forces the data
  158. * through, otherwise it is scheduled, and with high data rates (like
  159. * with OHCI) data can get lost.
  160. */
  161. if (tty) {
  162. tty->low_latency = 1;
  163. }
  164. /* Clear the urb pipe. */
  165. usb_clear_halt(serial->dev, port->interrupt_in_urb->pipe);
  166. /* Start reading from the device */
  167. usb_fill_int_urb (port->interrupt_in_urb, serial->dev,
  168. usb_rcvintpipe (serial->dev, port->interrupt_in_endpointAddress),
  169. port->interrupt_in_urb->transfer_buffer,
  170. port->interrupt_in_urb->transfer_buffer_length,
  171. metrousb_read_int_callback, port, 1);
  172. result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  173. if (result) {
  174. dbg("METRO-USB - %s - failed submitting interrupt in urb for port number=%d, error code=%d"
  175. , __FUNCTION__, port->number, result);
  176. goto exit;
  177. }
  178. dbg("METRO-USB - %s - port open for port number=%d", __FUNCTION__, port->number);
  179. exit:
  180. return result;
  181. }
  182. /* ----------------------------------------------------------------------------------------------
  183. Description:
  184. Read the port from the read interrupt.
  185. Input:
  186. struct urb *: urb structure to get data.
  187. struct pt_regs *: pt_regs structure.
  188. Output:
  189. None:
  190. */
  191. static void metrousb_read_int_callback (struct urb *urb)
  192. {
  193. struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
  194. struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
  195. struct tty_struct *tty;
  196. unsigned char *data = urb->transfer_buffer;
  197. int throttled = 0;
  198. int result = 0;
  199. unsigned long flags = 0;
  200. dbg("METRO-USB - %s - port number=%d", __FUNCTION__, port->number);
  201. switch (urb->status) {
  202. case 0:
  203. /* Success status, read from the port. */
  204. break;
  205. case -ECONNRESET:
  206. case -ENOENT:
  207. case -ESHUTDOWN:
  208. /* urb has been terminated. */
  209. dbg("METRO-USB - %s - urb shutting down, port number=%d, error code=%d",
  210. __FUNCTION__, port->number, result);
  211. return;
  212. default:
  213. dbg("METRO-USB - %s - non-zero urb received, port number=%d, error code=%d",
  214. __FUNCTION__, port->number, result);
  215. goto exit;
  216. }
  217. /* Set the data read from the usb port into the serial port buffer. */
  218. tty = tty_port_tty_get(&port->port);
  219. if (!tty) {
  220. dbg("%s - bad tty pointer - exiting", __func__);
  221. return;
  222. }
  223. if (tty && urb->actual_length) {
  224. // Loop through the data copying each byte to the tty layer.
  225. tty_insert_flip_string(tty, data, urb->actual_length);
  226. // Force the data to the tty layer.
  227. tty_flip_buffer_push(tty);
  228. }
  229. tty_kref_put(tty);
  230. /* Set any port variables. */
  231. spin_lock_irqsave(&metro_priv->lock, flags);
  232. throttled = metro_priv->throttled;
  233. spin_unlock_irqrestore(&metro_priv->lock, flags);
  234. /* Continue trying to read if set. */
  235. if (!throttled) {
  236. usb_fill_int_urb (port->interrupt_in_urb, port->serial->dev,
  237. usb_rcvintpipe (port->serial->dev, port->interrupt_in_endpointAddress),
  238. port->interrupt_in_urb->transfer_buffer,
  239. port->interrupt_in_urb->transfer_buffer_length,
  240. metrousb_read_int_callback, port, 1);
  241. result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
  242. if (result) {
  243. dbg("METRO-USB - %s - failed submitting interrupt in urb for port number=%d, error code=%d",
  244. __FUNCTION__, port->number, result);
  245. }
  246. }
  247. return;
  248. exit:
  249. /* Try to resubmit the urb. */
  250. result = usb_submit_urb (urb, GFP_ATOMIC);
  251. if (result) {
  252. dbg("METRO-USB - %s - failed submitting interrupt in urb for port number=%d, error code=%d",
  253. __FUNCTION__, port->number, result);
  254. }
  255. }
  256. /* ----------------------------------------------------------------------------------------------
  257. Description:
  258. Set the modem control state for the entered serial port.
  259. Input:
  260. struct usb_serial_port *: pointer to a usb_serial_port structure.
  261. unsigned int: control state value to set.
  262. Output:
  263. int: Returns true (0) if successful, false otherwise.
  264. */
  265. static int metrousb_set_modem_ctrl(struct usb_serial *serial, unsigned int control_state)
  266. {
  267. int retval = 0;
  268. unsigned char mcr = METROUSB_MCR_NONE;
  269. dbg("METRO-USB - %s - control state=%d", __FUNCTION__, control_state);
  270. /* Set the modem control value. */
  271. if (control_state & TIOCM_DTR)
  272. mcr |= METROUSB_MCR_DTR;
  273. if (control_state & TIOCM_RTS)
  274. mcr |= METROUSB_MCR_RTS;
  275. /* Send the command to the usb port. */
  276. retval = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  277. METROUSB_SET_REQUEST_TYPE, METROUSB_SET_MODEM_CTRL_REQUEST,
  278. control_state, 0, NULL, 0, WDR_TIMEOUT);
  279. if (retval < 0)
  280. dbg("METRO-USB - %s - set modem ctrl=0x%x failed, error code=%d", __FUNCTION__, mcr, retval);
  281. return retval;
  282. }
  283. /* ----------------------------------------------------------------------------------------------
  284. Description:
  285. Shutdown the driver.
  286. Input:
  287. struct usb_serial *: pointer to a usb-serial structure.
  288. Output:
  289. int: Returns true (0) if successful, false otherwise.
  290. */
  291. static void metrousb_shutdown (struct usb_serial *serial)
  292. {
  293. int i = 0;
  294. dbg("METRO-USB - %s", __FUNCTION__);
  295. /* Stop reading and writing on all ports. */
  296. for (i=0; i < serial->num_ports; ++i) {
  297. /* Close any open urbs. */
  298. metrousb_cleanup(serial->port[i]);
  299. /* Free memory. */
  300. kfree(usb_get_serial_port_data(serial->port[i]));
  301. usb_set_serial_port_data(serial->port[i], NULL);
  302. dbg("METRO-USB - %s - freed port number=%d", __FUNCTION__, serial->port[i]->number);
  303. }
  304. }
  305. /* ----------------------------------------------------------------------------------------------
  306. Description:
  307. Startup the driver.
  308. Input:
  309. struct usb_serial *: pointer to a usb-serial structure.
  310. Output:
  311. int: Returns true (0) if successful, false otherwise.
  312. */
  313. static int metrousb_startup(struct usb_serial *serial)
  314. {
  315. struct metrousb_private *metro_priv;
  316. struct usb_serial_port *port;
  317. int i = 0;
  318. dbg("METRO-USB - %s", __FUNCTION__);
  319. /* Loop through the serial ports setting up the private structures.
  320. * Currently we only use one port. */
  321. for (i = 0; i < serial->num_ports; ++i) {
  322. port = serial->port[i];
  323. /* Declare memory. */
  324. metro_priv = (struct metrousb_private *) kmalloc (sizeof(struct metrousb_private), GFP_KERNEL);
  325. if (!metro_priv)
  326. return -ENOMEM;
  327. /* Clear memory. */
  328. memset (metro_priv, 0x00, sizeof(struct metrousb_private));
  329. /* Initialize memory. */
  330. spin_lock_init(&metro_priv->lock);
  331. usb_set_serial_port_data(port, metro_priv);
  332. dbg("METRO-USB - %s - port number=%d.", __FUNCTION__, port->number);
  333. }
  334. return 0;
  335. }
  336. /* ----------------------------------------------------------------------------------------------
  337. Description:
  338. Set the serial port throttle to stop reading from the port.
  339. Input:
  340. struct usb_serial_port *: pointer to a usb_serial_port structure.
  341. Output:
  342. None:
  343. */
  344. static void metrousb_throttle (struct tty_struct *tty)
  345. {
  346. struct usb_serial_port *port = tty->driver_data;
  347. struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
  348. unsigned long flags = 0;
  349. dbg("METRO-USB - %s - port number=%d", __FUNCTION__, port->number);
  350. /* Set the private information for the port to stop reading data. */
  351. spin_lock_irqsave(&metro_priv->lock, flags);
  352. metro_priv->throttled = 1;
  353. spin_unlock_irqrestore(&metro_priv->lock, flags);
  354. }
  355. /* ----------------------------------------------------------------------------------------------
  356. Description:
  357. Get the serial port control line states.
  358. Input:
  359. struct usb_serial_port *: pointer to a usb_serial_port structure.
  360. struct file *: pointer to a file structure.
  361. Output:
  362. int: Returns the state of the control lines.
  363. */
  364. static int metrousb_tiocmget (struct tty_struct *tty)
  365. {
  366. unsigned long control_state = 0;
  367. struct usb_serial_port *port = tty->driver_data;
  368. struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
  369. unsigned long flags = 0;
  370. dbg("METRO-USB - %s - port number=%d", __FUNCTION__, port->number);
  371. spin_lock_irqsave(&metro_priv->lock, flags);
  372. control_state = metro_priv->control_state;
  373. spin_unlock_irqrestore(&metro_priv->lock, flags);
  374. return control_state;
  375. }
  376. /* ----------------------------------------------------------------------------------------------
  377. Description:
  378. Set the serial port control line states.
  379. Input:
  380. struct usb_serial_port *: pointer to a usb_serial_port structure.
  381. struct file *: pointer to a file structure.
  382. unsigned int: line state to set.
  383. unsigned int: line state to clear.
  384. Output:
  385. int: Returns the state of the control lines.
  386. */
  387. static int metrousb_tiocmset (struct tty_struct *tty,
  388. unsigned int set, unsigned int clear)
  389. {
  390. struct usb_serial_port *port = tty->driver_data;
  391. struct usb_serial *serial = port->serial;
  392. struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
  393. unsigned long flags = 0;
  394. unsigned long control_state = 0;
  395. dbg("METRO-USB - %s - port number=%d, set=%d, clear=%d", __FUNCTION__, port->number, set, clear);
  396. spin_lock_irqsave(&metro_priv->lock, flags);
  397. control_state = metro_priv->control_state;
  398. // Set the RTS and DTR values.
  399. if (set & TIOCM_RTS)
  400. control_state |= TIOCM_RTS;
  401. if (set & TIOCM_DTR)
  402. control_state |= TIOCM_DTR;
  403. if (clear & TIOCM_RTS)
  404. control_state &= ~TIOCM_RTS;
  405. if (clear & TIOCM_DTR)
  406. control_state &= ~TIOCM_DTR;
  407. metro_priv->control_state = control_state;
  408. spin_unlock_irqrestore(&metro_priv->lock, flags);
  409. return metrousb_set_modem_ctrl(serial, control_state);
  410. }
  411. /* ----------------------------------------------------------------------------------------------
  412. Description:
  413. Set the serial port unthrottle to resume reading from the port.
  414. Input:
  415. struct usb_serial_port *: pointer to a usb_serial_port structure.
  416. Output:
  417. None:
  418. */
  419. static void metrousb_unthrottle (struct tty_struct *tty)
  420. {
  421. struct usb_serial_port *port = tty->driver_data;
  422. struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
  423. unsigned long flags = 0;
  424. int result = 0;
  425. dbg("METRO-USB - %s - port number=%d", __FUNCTION__, port->number);
  426. /* Set the private information for the port to resume reading data. */
  427. spin_lock_irqsave(&metro_priv->lock, flags);
  428. metro_priv->throttled = 0;
  429. spin_unlock_irqrestore(&metro_priv->lock, flags);
  430. /* Submit the urb to read from the port. */
  431. port->interrupt_in_urb->dev = port->serial->dev;
  432. result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
  433. if (result) {
  434. dbg("METRO-USB - %s - failed submitting interrupt in urb for port number=%d, error code=%d",
  435. __FUNCTION__, port->number, result);
  436. }
  437. }
  438. module_usb_serial_driver(metrousb_driver, serial_drivers);
  439. MODULE_LICENSE("GPL");
  440. MODULE_AUTHOR( "Philip Nicastro" );
  441. MODULE_AUTHOR( "Aleksey Babahin <tamerlan311@gmail.com>" );
  442. MODULE_DESCRIPTION( DRIVER_DESC );
  443. /* Module input parameters */
  444. module_param(debug, bool, S_IRUGO | S_IWUSR);
  445. MODULE_PARM_DESC(debug, "Print debug info (bool 1=on, 0=off)");