metro-usb.c 18 KB

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