blob: 7f5d546da39af963b57123b9b35e58e714a88cd0 [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 */
Luiz Fernando N. Capitulino931b0412006-10-03 10:31:36 -030022 { USB_DEVICE(0x1410, 0x1100) }, /* ExpressCard34 Qualcomm 3G CDMA */
Greg KH3b86b202005-04-25 21:46:29 -070023 { },
24};
25MODULE_DEVICE_TABLE(usb, id_table);
26
Andy Gay5dda1712006-07-03 18:43:01 -040027#define URB_TRANSFER_BUFFER_SIZE 4096
28#define NUM_READ_URBS 4
29#define NUM_WRITE_URBS 4
30#define NUM_BULK_EPS 3
31#define MAX_BULK_EPS 6
32
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 */
35static int buffer_size = URB_TRANSFER_BUFFER_SIZE;
36static int endpoints = NUM_BULK_EPS;
37static int debug;
38struct airprime_private {
39 spinlock_t lock;
40 int outstanding_urbs;
41 int throttled;
42 struct urb *read_urbp[NUM_READ_URBS];
43};
44
David Howells7d12e782006-10-05 14:55:46 +010045static void airprime_read_bulk_callback(struct urb *urb)
Andy Gay5dda1712006-07-03 18:43:01 -040046{
47 struct usb_serial_port *port = urb->context;
48 unsigned char *data = urb->transfer_buffer;
49 struct tty_struct *tty;
50 int result;
51
52 dbg("%s - port %d", __FUNCTION__, port->number);
53
54 if (urb->status) {
55 dbg("%s - nonzero read bulk status received: %d",
56 __FUNCTION__, urb->status);
57 /* something happened, so free up the memory for this urb */
58 if (urb->transfer_buffer) {
59 kfree (urb->transfer_buffer);
60 urb->transfer_buffer = NULL;
61 }
62 return;
63 }
64 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
65
66 tty = port->tty;
67 if (tty && urb->actual_length) {
68 tty_insert_flip_string (tty, data, urb->actual_length);
69 tty_flip_buffer_push (tty);
70 }
71
72 result = usb_submit_urb (urb, GFP_ATOMIC);
73 if (result)
74 dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n",
75 __FUNCTION__, result);
76 return;
77}
78
David Howells7d12e782006-10-05 14:55:46 +010079static void airprime_write_bulk_callback(struct urb *urb)
Andy Gay5dda1712006-07-03 18:43:01 -040080{
81 struct usb_serial_port *port = urb->context;
82 struct airprime_private *priv = usb_get_serial_port_data(port);
83 unsigned long flags;
84
85 dbg("%s - port %d", __FUNCTION__, port->number);
86
87 /* free up the transfer buffer, as usb_free_urb() does not do this */
88 kfree (urb->transfer_buffer);
89
90 if (urb->status)
91 dbg("%s - nonzero write bulk status received: %d",
92 __FUNCTION__, urb->status);
93 spin_lock_irqsave(&priv->lock, flags);
94 --priv->outstanding_urbs;
95 spin_unlock_irqrestore(&priv->lock, flags);
96
97 usb_serial_port_softint(port);
98}
99
100static int airprime_open(struct usb_serial_port *port, struct file *filp)
101{
102 struct airprime_private *priv = usb_get_serial_port_data(port);
103 struct usb_serial *serial = port->serial;
104 struct urb *urb;
105 char *buffer = NULL;
106 int i;
107 int result = 0;
108
109 dbg("%s - port %d", __FUNCTION__, port->number);
110
111 /* initialize our private data structure if it isn't already created */
112 if (!priv) {
113 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
114 if (!priv) {
115 result = -ENOMEM;
116 goto out;
117 }
118 spin_lock_init(&priv->lock);
119 usb_set_serial_port_data(port, priv);
120 }
121
122 for (i = 0; i < NUM_READ_URBS; ++i) {
123 buffer = kmalloc(buffer_size, GFP_KERNEL);
124 if (!buffer) {
125 dev_err(&port->dev, "%s - out of memory.\n",
126 __FUNCTION__);
127 result = -ENOMEM;
128 goto errout;
129 }
130 urb = usb_alloc_urb(0, GFP_KERNEL);
131 if (!urb) {
Eric Sesterhenn0e185b72006-10-10 14:42:50 -0700132 kfree(buffer);
Andy Gay5dda1712006-07-03 18:43:01 -0400133 dev_err(&port->dev, "%s - no more urbs?\n",
134 __FUNCTION__);
135 result = -ENOMEM;
136 goto errout;
137 }
138 usb_fill_bulk_urb(urb, serial->dev,
139 usb_rcvbulkpipe(serial->dev,
140 port->bulk_out_endpointAddress),
141 buffer, buffer_size,
142 airprime_read_bulk_callback, port);
143 result = usb_submit_urb(urb, GFP_KERNEL);
144 if (result) {
145 dev_err(&port->dev,
146 "%s - failed submitting read urb %d for port %d, error %d\n",
147 __FUNCTION__, i, port->number, result);
148 goto errout;
149 }
150 /* remember this urb so we can kill it when the port is closed */
151 priv->read_urbp[i] = urb;
152 }
153 goto out;
154
155 errout:
156 /* some error happened, cancel any submitted urbs and clean up anything that
157 got allocated successfully */
158
159 for ( ; i >= 0; --i) {
160 urb = priv->read_urbp[i];
161 if (urb) {
162 /* This urb was submitted successfully. So we have to
163 cancel it.
164 Unlinking the urb will invoke read_bulk_callback()
165 with an error status, so its transfer buffer will
166 be freed there */
167 if (usb_unlink_urb (urb) != -EINPROGRESS) {
168 /* comments in drivers/usb/core/urb.c say this
169 can only happen if the urb was never submitted,
170 or has completed already.
171 Either way we may have to free the transfer
172 buffer here. */
173 if (urb->transfer_buffer) {
174 kfree (urb->transfer_buffer);
175 urb->transfer_buffer = NULL;
176 }
177 }
178 usb_free_urb (urb);
179 }
180 }
181
182 out:
183 return result;
184}
185
186static void airprime_close(struct usb_serial_port *port, struct file * filp)
187{
188 struct airprime_private *priv = usb_get_serial_port_data(port);
189 int i;
190
191 dbg("%s - port %d", __FUNCTION__, port->number);
192
193 /* killing the urb will invoke read_bulk_callback() with an error status,
194 so the transfer buffer will be freed there */
195 for (i = 0; i < NUM_READ_URBS; ++i) {
196 usb_kill_urb (priv->read_urbp[i]);
197 usb_free_urb (priv->read_urbp[i]);
198 }
199
200 /* free up private structure */
201 kfree (priv);
202 usb_set_serial_port_data(port, NULL);
203}
204
205static int airprime_write(struct usb_serial_port *port,
206 const unsigned char *buf, int count)
207{
208 struct airprime_private *priv = usb_get_serial_port_data(port);
209 struct usb_serial *serial = port->serial;
210 struct urb *urb;
211 unsigned char *buffer;
212 unsigned long flags;
213 int status;
214 dbg("%s - port %d", __FUNCTION__, port->number);
215
216 spin_lock_irqsave(&priv->lock, flags);
217 if (priv->outstanding_urbs > NUM_WRITE_URBS) {
218 spin_unlock_irqrestore(&priv->lock, flags);
219 dbg("%s - write limit hit\n", __FUNCTION__);
220 return 0;
221 }
222 spin_unlock_irqrestore(&priv->lock, flags);
223 buffer = kmalloc(count, GFP_ATOMIC);
224 if (!buffer) {
225 dev_err(&port->dev, "out of memory\n");
226 return -ENOMEM;
227 }
228 urb = usb_alloc_urb(0, GFP_ATOMIC);
229 if (!urb) {
230 dev_err(&port->dev, "no more free urbs\n");
231 kfree (buffer);
232 return -ENOMEM;
233 }
234 memcpy (buffer, buf, count);
235
236 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, buffer);
237
238 usb_fill_bulk_urb(urb, serial->dev,
239 usb_sndbulkpipe(serial->dev,
240 port->bulk_out_endpointAddress),
241 buffer, count,
242 airprime_write_bulk_callback, port);
243
244 /* send it down the pipe */
245 status = usb_submit_urb(urb, GFP_ATOMIC);
246 if (status) {
247 dev_err(&port->dev,
248 "%s - usb_submit_urb(write bulk) failed with status = %d\n",
249 __FUNCTION__, status);
250 count = status;
251 kfree (buffer);
252 } else {
253 spin_lock_irqsave(&priv->lock, flags);
254 ++priv->outstanding_urbs;
255 spin_unlock_irqrestore(&priv->lock, flags);
256 }
257 /* we are done with this urb, so let the host driver
258 * really free it when it is finished with it */
259 usb_free_urb (urb);
260 return count;
261}
262
Greg KH3b86b202005-04-25 21:46:29 -0700263static struct usb_driver airprime_driver = {
Greg KH3b86b202005-04-25 21:46:29 -0700264 .name = "airprime",
265 .probe = usb_serial_probe,
266 .disconnect = usb_serial_disconnect,
267 .id_table = id_table,
Andy Gay5dda1712006-07-03 18:43:01 -0400268 .no_dynamic_id = 1,
Greg KH3b86b202005-04-25 21:46:29 -0700269};
270
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700271static struct usb_serial_driver airprime_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700272 .driver = {
273 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700274 .name = "airprime",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700275 },
Greg KH3b86b202005-04-25 21:46:29 -0700276 .id_table = id_table,
277 .num_interrupt_in = NUM_DONT_CARE,
278 .num_bulk_in = NUM_DONT_CARE,
279 .num_bulk_out = NUM_DONT_CARE,
Andy Gay5dda1712006-07-03 18:43:01 -0400280 .open = airprime_open,
281 .close = airprime_close,
282 .write = airprime_write,
Greg KH3b86b202005-04-25 21:46:29 -0700283};
284
285static int __init airprime_init(void)
286{
287 int retval;
288
Andy Gay5dda1712006-07-03 18:43:01 -0400289 airprime_device.num_ports =
290 (endpoints > 0 && endpoints <= MAX_BULK_EPS) ? endpoints : NUM_BULK_EPS;
Greg KH3b86b202005-04-25 21:46:29 -0700291 retval = usb_serial_register(&airprime_device);
292 if (retval)
293 return retval;
294 retval = usb_register(&airprime_driver);
295 if (retval)
296 usb_serial_deregister(&airprime_device);
297 return retval;
298}
299
300static void __exit airprime_exit(void)
301{
Andy Gay5dda1712006-07-03 18:43:01 -0400302 dbg("%s", __FUNCTION__);
303
Greg KH3b86b202005-04-25 21:46:29 -0700304 usb_deregister(&airprime_driver);
305 usb_serial_deregister(&airprime_device);
306}
307
308module_init(airprime_init);
309module_exit(airprime_exit);
310MODULE_LICENSE("GPL");
Andy Gay5dda1712006-07-03 18:43:01 -0400311
312module_param(debug, bool, S_IRUGO | S_IWUSR);
313MODULE_PARM_DESC(debug, "Debug enabled");
314module_param(buffer_size, int, 0);
315MODULE_PARM_DESC(buffer_size, "Size of the transfer buffers in bytes (default 4096)");
316module_param(endpoints, int, 0);
317MODULE_PARM_DESC(endpoints, "Number of bulk EPs to configure (default 3)");