airprime.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. * AirPrime CDMA Wireless Serial USB driver
  3. *
  4. * Copyright (C) 2005-2006 Greg Kroah-Hartman <gregkh@suse.de>
  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. #include <linux/kernel.h>
  11. #include <linux/init.h>
  12. #include <linux/tty.h>
  13. #include <linux/tty_flip.h>
  14. #include <linux/module.h>
  15. #include <linux/usb.h>
  16. #include <linux/usb/serial.h>
  17. static struct usb_device_id id_table [] = {
  18. { USB_DEVICE(0x0c88, 0x17da) }, /* Kyocera Wireless KPC650/Passport */
  19. { USB_DEVICE(0x1410, 0x1110) }, /* Novatel Wireless Merlin CDMA */
  20. { USB_DEVICE(0x1410, 0x1130) }, /* Novatel Wireless S720 CDMA/EV-DO */
  21. { USB_DEVICE(0x1410, 0x2110) }, /* Novatel Wireless U720 CDMA/EV-DO */
  22. { USB_DEVICE(0x1410, 0x1430) }, /* Novatel Merlin XU870 HSDPA/3G */
  23. { USB_DEVICE(0x1410, 0x1100) }, /* ExpressCard34 Qualcomm 3G CDMA */
  24. { USB_DEVICE(0x413c, 0x8115) }, /* Dell Wireless HSDPA 5500 */
  25. { },
  26. };
  27. MODULE_DEVICE_TABLE(usb, id_table);
  28. #define URB_TRANSFER_BUFFER_SIZE 4096
  29. #define NUM_READ_URBS 4
  30. #define NUM_WRITE_URBS 4
  31. #define NUM_BULK_EPS 3
  32. #define MAX_BULK_EPS 6
  33. /* if overridden by the user, then use their value for the size of the
  34. * read and write urbs, and the number of endpoints */
  35. static int buffer_size = URB_TRANSFER_BUFFER_SIZE;
  36. static int endpoints = NUM_BULK_EPS;
  37. static int debug;
  38. struct airprime_private {
  39. spinlock_t lock;
  40. int outstanding_urbs;
  41. int throttled;
  42. struct urb *read_urbp[NUM_READ_URBS];
  43. };
  44. static void airprime_read_bulk_callback(struct urb *urb)
  45. {
  46. struct usb_serial_port *port = urb->context;
  47. unsigned char *data = urb->transfer_buffer;
  48. struct tty_struct *tty;
  49. int result;
  50. dbg("%s - port %d", __FUNCTION__, port->number);
  51. if (urb->status) {
  52. dbg("%s - nonzero read bulk status received: %d",
  53. __FUNCTION__, urb->status);
  54. return;
  55. }
  56. usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
  57. tty = port->tty;
  58. if (tty && urb->actual_length) {
  59. tty_insert_flip_string (tty, data, urb->actual_length);
  60. tty_flip_buffer_push (tty);
  61. }
  62. result = usb_submit_urb (urb, GFP_ATOMIC);
  63. if (result)
  64. dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n",
  65. __FUNCTION__, result);
  66. return;
  67. }
  68. static void airprime_write_bulk_callback(struct urb *urb)
  69. {
  70. struct usb_serial_port *port = urb->context;
  71. struct airprime_private *priv = usb_get_serial_port_data(port);
  72. unsigned long flags;
  73. dbg("%s - port %d", __FUNCTION__, port->number);
  74. /* free up the transfer buffer, as usb_free_urb() does not do this */
  75. kfree (urb->transfer_buffer);
  76. if (urb->status)
  77. dbg("%s - nonzero write bulk status received: %d",
  78. __FUNCTION__, urb->status);
  79. spin_lock_irqsave(&priv->lock, flags);
  80. --priv->outstanding_urbs;
  81. spin_unlock_irqrestore(&priv->lock, flags);
  82. usb_serial_port_softint(port);
  83. }
  84. static int airprime_open(struct usb_serial_port *port, struct file *filp)
  85. {
  86. struct airprime_private *priv = usb_get_serial_port_data(port);
  87. struct usb_serial *serial = port->serial;
  88. struct urb *urb;
  89. char *buffer = NULL;
  90. int i;
  91. int result = 0;
  92. dbg("%s - port %d", __FUNCTION__, port->number);
  93. /* initialize our private data structure if it isn't already created */
  94. if (!priv) {
  95. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  96. if (!priv) {
  97. result = -ENOMEM;
  98. goto out;
  99. }
  100. spin_lock_init(&priv->lock);
  101. usb_set_serial_port_data(port, priv);
  102. }
  103. for (i = 0; i < NUM_READ_URBS; ++i) {
  104. buffer = kmalloc(buffer_size, GFP_KERNEL);
  105. if (!buffer) {
  106. dev_err(&port->dev, "%s - out of memory.\n",
  107. __FUNCTION__);
  108. result = -ENOMEM;
  109. goto errout;
  110. }
  111. urb = usb_alloc_urb(0, GFP_KERNEL);
  112. if (!urb) {
  113. kfree(buffer);
  114. dev_err(&port->dev, "%s - no more urbs?\n",
  115. __FUNCTION__);
  116. result = -ENOMEM;
  117. goto errout;
  118. }
  119. usb_fill_bulk_urb(urb, serial->dev,
  120. usb_rcvbulkpipe(serial->dev,
  121. port->bulk_out_endpointAddress),
  122. buffer, buffer_size,
  123. airprime_read_bulk_callback, port);
  124. result = usb_submit_urb(urb, GFP_KERNEL);
  125. if (result) {
  126. usb_free_urb(urb);
  127. kfree(buffer);
  128. dev_err(&port->dev,
  129. "%s - failed submitting read urb %d for port %d, error %d\n",
  130. __FUNCTION__, i, port->number, result);
  131. goto errout;
  132. }
  133. /* remember this urb so we can kill it when the port is closed */
  134. priv->read_urbp[i] = urb;
  135. }
  136. goto out;
  137. errout:
  138. /* some error happened, cancel any submitted urbs and clean up anything that
  139. got allocated successfully */
  140. while (i-- != 0) {
  141. urb = priv->read_urbp[i];
  142. buffer = urb->transfer_buffer;
  143. usb_kill_urb (urb);
  144. usb_free_urb (urb);
  145. kfree (buffer);
  146. }
  147. out:
  148. return result;
  149. }
  150. static void airprime_close(struct usb_serial_port *port, struct file * filp)
  151. {
  152. struct airprime_private *priv = usb_get_serial_port_data(port);
  153. int i;
  154. dbg("%s - port %d", __FUNCTION__, port->number);
  155. for (i = 0; i < NUM_READ_URBS; ++i) {
  156. usb_kill_urb (priv->read_urbp[i]);
  157. kfree (priv->read_urbp[i]->transfer_buffer);
  158. usb_free_urb (priv->read_urbp[i]);
  159. }
  160. /* free up private structure */
  161. kfree (priv);
  162. usb_set_serial_port_data(port, NULL);
  163. }
  164. static int airprime_write(struct usb_serial_port *port,
  165. const unsigned char *buf, int count)
  166. {
  167. struct airprime_private *priv = usb_get_serial_port_data(port);
  168. struct usb_serial *serial = port->serial;
  169. struct urb *urb;
  170. unsigned char *buffer;
  171. unsigned long flags;
  172. int status;
  173. dbg("%s - port %d", __FUNCTION__, port->number);
  174. spin_lock_irqsave(&priv->lock, flags);
  175. if (priv->outstanding_urbs > NUM_WRITE_URBS) {
  176. spin_unlock_irqrestore(&priv->lock, flags);
  177. dbg("%s - write limit hit\n", __FUNCTION__);
  178. return 0;
  179. }
  180. spin_unlock_irqrestore(&priv->lock, flags);
  181. buffer = kmalloc(count, GFP_ATOMIC);
  182. if (!buffer) {
  183. dev_err(&port->dev, "out of memory\n");
  184. return -ENOMEM;
  185. }
  186. urb = usb_alloc_urb(0, GFP_ATOMIC);
  187. if (!urb) {
  188. dev_err(&port->dev, "no more free urbs\n");
  189. kfree (buffer);
  190. return -ENOMEM;
  191. }
  192. memcpy (buffer, buf, count);
  193. usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, buffer);
  194. usb_fill_bulk_urb(urb, serial->dev,
  195. usb_sndbulkpipe(serial->dev,
  196. port->bulk_out_endpointAddress),
  197. buffer, count,
  198. airprime_write_bulk_callback, port);
  199. /* send it down the pipe */
  200. status = usb_submit_urb(urb, GFP_ATOMIC);
  201. if (status) {
  202. dev_err(&port->dev,
  203. "%s - usb_submit_urb(write bulk) failed with status = %d\n",
  204. __FUNCTION__, status);
  205. count = status;
  206. kfree (buffer);
  207. } else {
  208. spin_lock_irqsave(&priv->lock, flags);
  209. ++priv->outstanding_urbs;
  210. spin_unlock_irqrestore(&priv->lock, flags);
  211. }
  212. /* we are done with this urb, so let the host driver
  213. * really free it when it is finished with it */
  214. usb_free_urb (urb);
  215. return count;
  216. }
  217. static struct usb_driver airprime_driver = {
  218. .name = "airprime",
  219. .probe = usb_serial_probe,
  220. .disconnect = usb_serial_disconnect,
  221. .id_table = id_table,
  222. .no_dynamic_id = 1,
  223. };
  224. static struct usb_serial_driver airprime_device = {
  225. .driver = {
  226. .owner = THIS_MODULE,
  227. .name = "airprime",
  228. },
  229. .usb_driver = &airprime_driver,
  230. .id_table = id_table,
  231. .num_interrupt_in = NUM_DONT_CARE,
  232. .num_bulk_in = NUM_DONT_CARE,
  233. .num_bulk_out = NUM_DONT_CARE,
  234. .open = airprime_open,
  235. .close = airprime_close,
  236. .write = airprime_write,
  237. };
  238. static int __init airprime_init(void)
  239. {
  240. int retval;
  241. airprime_device.num_ports =
  242. (endpoints > 0 && endpoints <= MAX_BULK_EPS) ? endpoints : NUM_BULK_EPS;
  243. retval = usb_serial_register(&airprime_device);
  244. if (retval)
  245. return retval;
  246. retval = usb_register(&airprime_driver);
  247. if (retval)
  248. usb_serial_deregister(&airprime_device);
  249. return retval;
  250. }
  251. static void __exit airprime_exit(void)
  252. {
  253. dbg("%s", __FUNCTION__);
  254. usb_deregister(&airprime_driver);
  255. usb_serial_deregister(&airprime_device);
  256. }
  257. module_init(airprime_init);
  258. module_exit(airprime_exit);
  259. MODULE_LICENSE("GPL");
  260. module_param(debug, bool, S_IRUGO | S_IWUSR);
  261. MODULE_PARM_DESC(debug, "Debug enabled");
  262. module_param(buffer_size, int, 0);
  263. MODULE_PARM_DESC(buffer_size, "Size of the transfer buffers in bytes (default 4096)");
  264. module_param(endpoints, int, 0);
  265. MODULE_PARM_DESC(endpoints, "Number of bulk EPs to configure (default 3)");