airprime.c 8.9 KB

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