blob: 62a0df2db539b5427d7175e094e7c0909ce7975d [file] [log] [blame]
Greg KH3b86b202005-04-25 21:46:29 -07001/*
2 * AirPrime CDMA Wireless Serial USB driver
3 *
Andy Gay5dda1712006-07-03 18:43:01 -04004 * Copyright (C) 2005-2006 Greg Kroah-Hartman <gregkh@suse.de>
Greg KH3b86b202005-04-25 21:46:29 -07005 *
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/init.h>
13#include <linux/tty.h>
Andy Gay5dda1712006-07-03 18:43:01 -040014#include <linux/tty_flip.h>
Greg KH3b86b202005-04-25 21:46:29 -070015#include <linux/module.h>
16#include <linux/usb.h>
Greg Kroah-Hartmana9698882006-07-11 21:22:58 -070017#include <linux/usb/serial.h>
Greg KH3b86b202005-04-25 21:46:29 -070018
19static struct usb_device_id id_table [] = {
Timothy Sipples34ab86e2006-06-16 20:42:59 +090020 { USB_DEVICE(0x0c88, 0x17da) }, /* Kyocera Wireless KPC650/Passport */
Andy Gay5dda1712006-07-03 18:43:01 -040021 { USB_DEVICE(0x1410, 0x1110) }, /* Novatel Wireless Merlin CDMA */
daniel@centurion.net.nzad0327d2006-11-11 15:47:52 +130022 { USB_DEVICE(0x1410, 0x1430) }, /* Novatel Merlin XU870 HSDPA/3G */
Luiz Fernando N. Capitulino931b0412006-10-03 10:31:36 -030023 { USB_DEVICE(0x1410, 0x1100) }, /* ExpressCard34 Qualcomm 3G CDMA */
Eagle Jones87f28bd2006-11-24 16:40:04 -080024 { USB_DEVICE(0x413c, 0x8115) }, /* Dell Wireless HSDPA 5500 */
Greg KH3b86b202005-04-25 21:46:29 -070025 { },
26};
27MODULE_DEVICE_TABLE(usb, id_table);
28
Andy Gay5dda1712006-07-03 18:43:01 -040029#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
35/* if overridden by the user, then use their value for the size of the
36 * read and write urbs, and the number of endpoints */
37static int buffer_size = URB_TRANSFER_BUFFER_SIZE;
38static int endpoints = NUM_BULK_EPS;
39static int debug;
40struct airprime_private {
41 spinlock_t lock;
42 int outstanding_urbs;
43 int throttled;
44 struct urb *read_urbp[NUM_READ_URBS];
45};
46
David Howells7d12e782006-10-05 14:55:46 +010047static void airprime_read_bulk_callback(struct urb *urb)
Andy Gay5dda1712006-07-03 18:43:01 -040048{
49 struct usb_serial_port *port = urb->context;
50 unsigned char *data = urb->transfer_buffer;
51 struct tty_struct *tty;
52 int result;
53
54 dbg("%s - port %d", __FUNCTION__, port->number);
55
56 if (urb->status) {
57 dbg("%s - nonzero read bulk status received: %d",
58 __FUNCTION__, urb->status);
59 /* something happened, so free up the memory for this urb */
60 if (urb->transfer_buffer) {
61 kfree (urb->transfer_buffer);
62 urb->transfer_buffer = NULL;
63 }
64 return;
65 }
66 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
67
68 tty = port->tty;
69 if (tty && urb->actual_length) {
70 tty_insert_flip_string (tty, data, urb->actual_length);
71 tty_flip_buffer_push (tty);
72 }
73
74 result = usb_submit_urb (urb, GFP_ATOMIC);
75 if (result)
76 dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n",
77 __FUNCTION__, result);
78 return;
79}
80
David Howells7d12e782006-10-05 14:55:46 +010081static void airprime_write_bulk_callback(struct urb *urb)
Andy Gay5dda1712006-07-03 18:43:01 -040082{
83 struct usb_serial_port *port = urb->context;
84 struct airprime_private *priv = usb_get_serial_port_data(port);
85 unsigned long flags;
86
87 dbg("%s - port %d", __FUNCTION__, port->number);
88
89 /* free up the transfer buffer, as usb_free_urb() does not do this */
90 kfree (urb->transfer_buffer);
91
92 if (urb->status)
93 dbg("%s - nonzero write bulk status received: %d",
94 __FUNCTION__, urb->status);
95 spin_lock_irqsave(&priv->lock, flags);
96 --priv->outstanding_urbs;
97 spin_unlock_irqrestore(&priv->lock, flags);
98
99 usb_serial_port_softint(port);
100}
101
102static int airprime_open(struct usb_serial_port *port, struct file *filp)
103{
104 struct airprime_private *priv = usb_get_serial_port_data(port);
105 struct usb_serial *serial = port->serial;
106 struct urb *urb;
107 char *buffer = NULL;
108 int i;
109 int result = 0;
110
111 dbg("%s - port %d", __FUNCTION__, port->number);
112
113 /* initialize our private data structure if it isn't already created */
114 if (!priv) {
115 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
116 if (!priv) {
117 result = -ENOMEM;
118 goto out;
119 }
120 spin_lock_init(&priv->lock);
121 usb_set_serial_port_data(port, priv);
122 }
123
124 for (i = 0; i < NUM_READ_URBS; ++i) {
125 buffer = kmalloc(buffer_size, GFP_KERNEL);
126 if (!buffer) {
127 dev_err(&port->dev, "%s - out of memory.\n",
128 __FUNCTION__);
129 result = -ENOMEM;
130 goto errout;
131 }
132 urb = usb_alloc_urb(0, GFP_KERNEL);
133 if (!urb) {
Eric Sesterhenn0e185b72006-10-10 14:42:50 -0700134 kfree(buffer);
Andy Gay5dda1712006-07-03 18:43:01 -0400135 dev_err(&port->dev, "%s - no more urbs?\n",
136 __FUNCTION__);
137 result = -ENOMEM;
138 goto errout;
139 }
140 usb_fill_bulk_urb(urb, serial->dev,
141 usb_rcvbulkpipe(serial->dev,
142 port->bulk_out_endpointAddress),
143 buffer, buffer_size,
144 airprime_read_bulk_callback, port);
145 result = usb_submit_urb(urb, GFP_KERNEL);
146 if (result) {
147 dev_err(&port->dev,
148 "%s - failed submitting read urb %d for port %d, error %d\n",
149 __FUNCTION__, i, port->number, result);
150 goto errout;
151 }
152 /* remember this urb so we can kill it when the port is closed */
153 priv->read_urbp[i] = urb;
154 }
155 goto out;
156
157 errout:
158 /* some error happened, cancel any submitted urbs and clean up anything that
159 got allocated successfully */
160
161 for ( ; i >= 0; --i) {
162 urb = priv->read_urbp[i];
163 if (urb) {
164 /* This urb was submitted successfully. So we have to
165 cancel it.
166 Unlinking the urb will invoke read_bulk_callback()
167 with an error status, so its transfer buffer will
168 be freed there */
169 if (usb_unlink_urb (urb) != -EINPROGRESS) {
170 /* comments in drivers/usb/core/urb.c say this
171 can only happen if the urb was never submitted,
172 or has completed already.
173 Either way we may have to free the transfer
174 buffer here. */
175 if (urb->transfer_buffer) {
176 kfree (urb->transfer_buffer);
177 urb->transfer_buffer = NULL;
178 }
179 }
180 usb_free_urb (urb);
181 }
182 }
183
184 out:
185 return result;
186}
187
188static void airprime_close(struct usb_serial_port *port, struct file * filp)
189{
190 struct airprime_private *priv = usb_get_serial_port_data(port);
191 int i;
192
193 dbg("%s - port %d", __FUNCTION__, port->number);
194
195 /* killing the urb will invoke read_bulk_callback() with an error status,
196 so the transfer buffer will be freed there */
197 for (i = 0; i < NUM_READ_URBS; ++i) {
198 usb_kill_urb (priv->read_urbp[i]);
199 usb_free_urb (priv->read_urbp[i]);
200 }
201
202 /* free up private structure */
203 kfree (priv);
204 usb_set_serial_port_data(port, NULL);
205}
206
207static int airprime_write(struct usb_serial_port *port,
208 const unsigned char *buf, int count)
209{
210 struct airprime_private *priv = usb_get_serial_port_data(port);
211 struct usb_serial *serial = port->serial;
212 struct urb *urb;
213 unsigned char *buffer;
214 unsigned long flags;
215 int status;
216 dbg("%s - port %d", __FUNCTION__, port->number);
217
218 spin_lock_irqsave(&priv->lock, flags);
219 if (priv->outstanding_urbs > NUM_WRITE_URBS) {
220 spin_unlock_irqrestore(&priv->lock, flags);
221 dbg("%s - write limit hit\n", __FUNCTION__);
222 return 0;
223 }
224 spin_unlock_irqrestore(&priv->lock, flags);
225 buffer = kmalloc(count, GFP_ATOMIC);
226 if (!buffer) {
227 dev_err(&port->dev, "out of memory\n");
228 return -ENOMEM;
229 }
230 urb = usb_alloc_urb(0, GFP_ATOMIC);
231 if (!urb) {
232 dev_err(&port->dev, "no more free urbs\n");
233 kfree (buffer);
234 return -ENOMEM;
235 }
236 memcpy (buffer, buf, count);
237
238 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, buffer);
239
240 usb_fill_bulk_urb(urb, serial->dev,
241 usb_sndbulkpipe(serial->dev,
242 port->bulk_out_endpointAddress),
243 buffer, count,
244 airprime_write_bulk_callback, port);
245
246 /* send it down the pipe */
247 status = usb_submit_urb(urb, GFP_ATOMIC);
248 if (status) {
249 dev_err(&port->dev,
250 "%s - usb_submit_urb(write bulk) failed with status = %d\n",
251 __FUNCTION__, status);
252 count = status;
253 kfree (buffer);
254 } else {
255 spin_lock_irqsave(&priv->lock, flags);
256 ++priv->outstanding_urbs;
257 spin_unlock_irqrestore(&priv->lock, flags);
258 }
259 /* we are done with this urb, so let the host driver
260 * really free it when it is finished with it */
261 usb_free_urb (urb);
262 return count;
263}
264
Greg KH3b86b202005-04-25 21:46:29 -0700265static struct usb_driver airprime_driver = {
Greg KH3b86b202005-04-25 21:46:29 -0700266 .name = "airprime",
267 .probe = usb_serial_probe,
268 .disconnect = usb_serial_disconnect,
269 .id_table = id_table,
Andy Gay5dda1712006-07-03 18:43:01 -0400270 .no_dynamic_id = 1,
Greg KH3b86b202005-04-25 21:46:29 -0700271};
272
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700273static struct usb_serial_driver airprime_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700274 .driver = {
275 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700276 .name = "airprime",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700277 },
Greg KH3b86b202005-04-25 21:46:29 -0700278 .id_table = id_table,
279 .num_interrupt_in = NUM_DONT_CARE,
280 .num_bulk_in = NUM_DONT_CARE,
281 .num_bulk_out = NUM_DONT_CARE,
Andy Gay5dda1712006-07-03 18:43:01 -0400282 .open = airprime_open,
283 .close = airprime_close,
284 .write = airprime_write,
Greg KH3b86b202005-04-25 21:46:29 -0700285};
286
287static int __init airprime_init(void)
288{
289 int retval;
290
Andy Gay5dda1712006-07-03 18:43:01 -0400291 airprime_device.num_ports =
292 (endpoints > 0 && endpoints <= MAX_BULK_EPS) ? endpoints : NUM_BULK_EPS;
Greg KH3b86b202005-04-25 21:46:29 -0700293 retval = usb_serial_register(&airprime_device);
294 if (retval)
295 return retval;
296 retval = usb_register(&airprime_driver);
297 if (retval)
298 usb_serial_deregister(&airprime_device);
299 return retval;
300}
301
302static void __exit airprime_exit(void)
303{
Andy Gay5dda1712006-07-03 18:43:01 -0400304 dbg("%s", __FUNCTION__);
305
Greg KH3b86b202005-04-25 21:46:29 -0700306 usb_deregister(&airprime_driver);
307 usb_serial_deregister(&airprime_device);
308}
309
310module_init(airprime_init);
311module_exit(airprime_exit);
312MODULE_LICENSE("GPL");
Andy Gay5dda1712006-07-03 18:43:01 -0400313
314module_param(debug, bool, S_IRUGO | S_IWUSR);
315MODULE_PARM_DESC(debug, "Debug enabled");
316module_param(buffer_size, int, 0);
317MODULE_PARM_DESC(buffer_size, "Size of the transfer buffers in bytes (default 4096)");
318module_param(endpoints, int, 0);
319MODULE_PARM_DESC(endpoints, "Number of bulk EPs to configure (default 3)");