generic.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * USB Serial Converter Generic functions
  3. *
  4. * Copyright (C) 1999 - 2002 Greg Kroah-Hartman (greg@kroah.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License version
  8. * 2 as published by the Free Software Foundation.
  9. *
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/errno.h>
  13. #include <linux/slab.h>
  14. #include <linux/tty.h>
  15. #include <linux/tty_flip.h>
  16. #include <linux/module.h>
  17. #include <linux/moduleparam.h>
  18. #include <linux/usb.h>
  19. #include <linux/usb/serial.h>
  20. #include <asm/uaccess.h>
  21. static int generic_probe(struct usb_interface *interface,
  22. const struct usb_device_id *id);
  23. static int debug;
  24. #ifdef CONFIG_USB_SERIAL_GENERIC
  25. static __u16 vendor = 0x05f9;
  26. static __u16 product = 0xffff;
  27. module_param(vendor, ushort, 0);
  28. MODULE_PARM_DESC(vendor, "User specified USB idVendor");
  29. module_param(product, ushort, 0);
  30. MODULE_PARM_DESC(product, "User specified USB idProduct");
  31. static struct usb_device_id generic_device_ids[2]; /* Initially all zeroes. */
  32. /* we want to look at all devices, as the vendor/product id can change
  33. * depending on the command line argument */
  34. static struct usb_device_id generic_serial_ids[] = {
  35. {.driver_info = 42},
  36. {}
  37. };
  38. static struct usb_driver generic_driver = {
  39. .name = "usbserial_generic",
  40. .probe = generic_probe,
  41. .disconnect = usb_serial_disconnect,
  42. .id_table = generic_serial_ids,
  43. .no_dynamic_id = 1,
  44. };
  45. /* All of the device info needed for the Generic Serial Converter */
  46. struct usb_serial_driver usb_serial_generic_device = {
  47. .driver = {
  48. .owner = THIS_MODULE,
  49. .name = "generic",
  50. },
  51. .id_table = generic_device_ids,
  52. .usb_driver = &generic_driver,
  53. .num_interrupt_in = NUM_DONT_CARE,
  54. .num_bulk_in = NUM_DONT_CARE,
  55. .num_bulk_out = NUM_DONT_CARE,
  56. .num_ports = 1,
  57. .shutdown = usb_serial_generic_shutdown,
  58. };
  59. static int generic_probe(struct usb_interface *interface,
  60. const struct usb_device_id *id)
  61. {
  62. const struct usb_device_id *id_pattern;
  63. id_pattern = usb_match_id(interface, generic_device_ids);
  64. if (id_pattern != NULL)
  65. return usb_serial_probe(interface, id);
  66. return -ENODEV;
  67. }
  68. #endif
  69. int usb_serial_generic_register (int _debug)
  70. {
  71. int retval = 0;
  72. debug = _debug;
  73. #ifdef CONFIG_USB_SERIAL_GENERIC
  74. generic_device_ids[0].idVendor = vendor;
  75. generic_device_ids[0].idProduct = product;
  76. generic_device_ids[0].match_flags = USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT;
  77. /* register our generic driver with ourselves */
  78. retval = usb_serial_register (&usb_serial_generic_device);
  79. if (retval)
  80. goto exit;
  81. retval = usb_register(&generic_driver);
  82. if (retval)
  83. usb_serial_deregister(&usb_serial_generic_device);
  84. exit:
  85. #endif
  86. return retval;
  87. }
  88. void usb_serial_generic_deregister (void)
  89. {
  90. #ifdef CONFIG_USB_SERIAL_GENERIC
  91. /* remove our generic driver */
  92. usb_deregister(&generic_driver);
  93. usb_serial_deregister (&usb_serial_generic_device);
  94. #endif
  95. }
  96. int usb_serial_generic_open (struct usb_serial_port *port, struct file *filp)
  97. {
  98. struct usb_serial *serial = port->serial;
  99. int result = 0;
  100. dbg("%s - port %d", __FUNCTION__, port->number);
  101. /* force low_latency on so that our tty_push actually forces the data through,
  102. otherwise it is scheduled, and with high data rates (like with OHCI) data
  103. can get lost. */
  104. if (port->tty)
  105. port->tty->low_latency = 1;
  106. /* if we have a bulk interrupt, start reading from it */
  107. if (serial->num_bulk_in) {
  108. /* Start reading from the device */
  109. usb_fill_bulk_urb (port->read_urb, serial->dev,
  110. usb_rcvbulkpipe(serial->dev, port->bulk_in_endpointAddress),
  111. port->read_urb->transfer_buffer,
  112. port->read_urb->transfer_buffer_length,
  113. ((serial->type->read_bulk_callback) ?
  114. serial->type->read_bulk_callback :
  115. usb_serial_generic_read_bulk_callback),
  116. port);
  117. result = usb_submit_urb(port->read_urb, GFP_KERNEL);
  118. if (result)
  119. dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
  120. }
  121. return result;
  122. }
  123. EXPORT_SYMBOL_GPL(usb_serial_generic_open);
  124. static void generic_cleanup (struct usb_serial_port *port)
  125. {
  126. struct usb_serial *serial = port->serial;
  127. dbg("%s - port %d", __FUNCTION__, port->number);
  128. if (serial->dev) {
  129. /* shutdown any bulk reads that might be going on */
  130. if (serial->num_bulk_out)
  131. usb_kill_urb(port->write_urb);
  132. if (serial->num_bulk_in)
  133. usb_kill_urb(port->read_urb);
  134. }
  135. }
  136. void usb_serial_generic_close (struct usb_serial_port *port, struct file * filp)
  137. {
  138. dbg("%s - port %d", __FUNCTION__, port->number);
  139. generic_cleanup (port);
  140. }
  141. int usb_serial_generic_write(struct usb_serial_port *port, const unsigned char *buf, int count)
  142. {
  143. struct usb_serial *serial = port->serial;
  144. int result;
  145. unsigned char *data;
  146. dbg("%s - port %d", __FUNCTION__, port->number);
  147. if (count == 0) {
  148. dbg("%s - write request of 0 bytes", __FUNCTION__);
  149. return (0);
  150. }
  151. /* only do something if we have a bulk out endpoint */
  152. if (serial->num_bulk_out) {
  153. spin_lock_bh(&port->lock);
  154. if (port->write_urb_busy) {
  155. spin_unlock_bh(&port->lock);
  156. dbg("%s - already writing", __FUNCTION__);
  157. return 0;
  158. }
  159. port->write_urb_busy = 1;
  160. spin_unlock_bh(&port->lock);
  161. count = (count > port->bulk_out_size) ? port->bulk_out_size : count;
  162. memcpy (port->write_urb->transfer_buffer, buf, count);
  163. data = port->write_urb->transfer_buffer;
  164. usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, data);
  165. /* set up our urb */
  166. usb_fill_bulk_urb (port->write_urb, serial->dev,
  167. usb_sndbulkpipe (serial->dev,
  168. port->bulk_out_endpointAddress),
  169. port->write_urb->transfer_buffer, count,
  170. ((serial->type->write_bulk_callback) ?
  171. serial->type->write_bulk_callback :
  172. usb_serial_generic_write_bulk_callback), port);
  173. /* send the data out the bulk port */
  174. port->write_urb_busy = 1;
  175. result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
  176. if (result) {
  177. dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __FUNCTION__, result);
  178. /* don't have to grab the lock here, as we will retry if != 0 */
  179. port->write_urb_busy = 0;
  180. } else
  181. result = count;
  182. return result;
  183. }
  184. /* no bulk out, so return 0 bytes written */
  185. return 0;
  186. }
  187. int usb_serial_generic_write_room (struct usb_serial_port *port)
  188. {
  189. struct usb_serial *serial = port->serial;
  190. int room = 0;
  191. dbg("%s - port %d", __FUNCTION__, port->number);
  192. if (serial->num_bulk_out) {
  193. if (!(port->write_urb_busy))
  194. room = port->bulk_out_size;
  195. }
  196. dbg("%s - returns %d", __FUNCTION__, room);
  197. return (room);
  198. }
  199. int usb_serial_generic_chars_in_buffer (struct usb_serial_port *port)
  200. {
  201. struct usb_serial *serial = port->serial;
  202. int chars = 0;
  203. dbg("%s - port %d", __FUNCTION__, port->number);
  204. if (serial->num_bulk_out) {
  205. if (port->write_urb_busy)
  206. chars = port->write_urb->transfer_buffer_length;
  207. }
  208. dbg("%s - returns %d", __FUNCTION__, chars);
  209. return (chars);
  210. }
  211. void usb_serial_generic_read_bulk_callback (struct urb *urb)
  212. {
  213. struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
  214. struct usb_serial *serial = port->serial;
  215. struct tty_struct *tty;
  216. unsigned char *data = urb->transfer_buffer;
  217. int result;
  218. dbg("%s - port %d", __FUNCTION__, port->number);
  219. if (urb->status) {
  220. dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
  221. return;
  222. }
  223. usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
  224. tty = port->tty;
  225. if (tty && urb->actual_length) {
  226. tty_buffer_request_room(tty, urb->actual_length);
  227. tty_insert_flip_string(tty, data, urb->actual_length);
  228. tty_flip_buffer_push(tty);
  229. }
  230. /* Continue trying to always read */
  231. usb_fill_bulk_urb (port->read_urb, serial->dev,
  232. usb_rcvbulkpipe (serial->dev,
  233. port->bulk_in_endpointAddress),
  234. port->read_urb->transfer_buffer,
  235. port->read_urb->transfer_buffer_length,
  236. ((serial->type->read_bulk_callback) ?
  237. serial->type->read_bulk_callback :
  238. usb_serial_generic_read_bulk_callback), port);
  239. result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  240. if (result)
  241. dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
  242. }
  243. EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback);
  244. void usb_serial_generic_write_bulk_callback (struct urb *urb)
  245. {
  246. struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
  247. dbg("%s - port %d", __FUNCTION__, port->number);
  248. port->write_urb_busy = 0;
  249. if (urb->status) {
  250. dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
  251. return;
  252. }
  253. usb_serial_port_softint(port);
  254. }
  255. EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
  256. void usb_serial_generic_shutdown (struct usb_serial *serial)
  257. {
  258. int i;
  259. dbg("%s", __FUNCTION__);
  260. /* stop reads and writes on all ports */
  261. for (i=0; i < serial->num_ports; ++i) {
  262. generic_cleanup(serial->port[i]);
  263. }
  264. }