generic.c 8.6 KB

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