blob: 18816bf96a4d6152da19a062c5f21d3247eaca60 [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 */
Eric Smith11e82732006-12-19 13:15:25 -080022 { USB_DEVICE(0x1410, 0x1130) }, /* Novatel Wireless S720 CDMA/EV-DO */
23 { USB_DEVICE(0x1410, 0x2110) }, /* Novatel Wireless U720 CDMA/EV-DO */
daniel@centurion.net.nzad0327d2006-11-11 15:47:52 +130024 { USB_DEVICE(0x1410, 0x1430) }, /* Novatel Merlin XU870 HSDPA/3G */
Luiz Fernando N. Capitulino931b0412006-10-03 10:31:36 -030025 { USB_DEVICE(0x1410, 0x1100) }, /* ExpressCard34 Qualcomm 3G CDMA */
Eagle Jones87f28bd2006-11-24 16:40:04 -080026 { USB_DEVICE(0x413c, 0x8115) }, /* Dell Wireless HSDPA 5500 */
Greg KH3b86b202005-04-25 21:46:29 -070027 { },
28};
29MODULE_DEVICE_TABLE(usb, id_table);
30
Andy Gay5dda1712006-07-03 18:43:01 -040031#define URB_TRANSFER_BUFFER_SIZE 4096
32#define NUM_READ_URBS 4
33#define NUM_WRITE_URBS 4
34#define NUM_BULK_EPS 3
35#define MAX_BULK_EPS 6
36
37/* if overridden by the user, then use their value for the size of the
38 * read and write urbs, and the number of endpoints */
39static int buffer_size = URB_TRANSFER_BUFFER_SIZE;
40static int endpoints = NUM_BULK_EPS;
41static int debug;
42struct airprime_private {
43 spinlock_t lock;
44 int outstanding_urbs;
45 int throttled;
46 struct urb *read_urbp[NUM_READ_URBS];
47};
48
David Howells7d12e782006-10-05 14:55:46 +010049static void airprime_read_bulk_callback(struct urb *urb)
Andy Gay5dda1712006-07-03 18:43:01 -040050{
51 struct usb_serial_port *port = urb->context;
52 unsigned char *data = urb->transfer_buffer;
53 struct tty_struct *tty;
54 int result;
55
56 dbg("%s - port %d", __FUNCTION__, port->number);
57
58 if (urb->status) {
59 dbg("%s - nonzero read bulk status received: %d",
60 __FUNCTION__, urb->status);
Andy Gay5dda1712006-07-03 18:43:01 -040061 return;
62 }
63 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
64
65 tty = port->tty;
66 if (tty && urb->actual_length) {
67 tty_insert_flip_string (tty, data, urb->actual_length);
68 tty_flip_buffer_push (tty);
69 }
70
71 result = usb_submit_urb (urb, GFP_ATOMIC);
72 if (result)
73 dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n",
74 __FUNCTION__, result);
75 return;
76}
77
David Howells7d12e782006-10-05 14:55:46 +010078static void airprime_write_bulk_callback(struct urb *urb)
Andy Gay5dda1712006-07-03 18:43:01 -040079{
80 struct usb_serial_port *port = urb->context;
81 struct airprime_private *priv = usb_get_serial_port_data(port);
82 unsigned long flags;
83
84 dbg("%s - port %d", __FUNCTION__, port->number);
85
86 /* free up the transfer buffer, as usb_free_urb() does not do this */
87 kfree (urb->transfer_buffer);
88
89 if (urb->status)
90 dbg("%s - nonzero write bulk status received: %d",
91 __FUNCTION__, urb->status);
92 spin_lock_irqsave(&priv->lock, flags);
93 --priv->outstanding_urbs;
94 spin_unlock_irqrestore(&priv->lock, flags);
95
96 usb_serial_port_softint(port);
97}
98
99static int airprime_open(struct usb_serial_port *port, struct file *filp)
100{
101 struct airprime_private *priv = usb_get_serial_port_data(port);
102 struct usb_serial *serial = port->serial;
103 struct urb *urb;
104 char *buffer = NULL;
105 int i;
106 int result = 0;
107
108 dbg("%s - port %d", __FUNCTION__, port->number);
109
110 /* initialize our private data structure if it isn't already created */
111 if (!priv) {
112 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
113 if (!priv) {
114 result = -ENOMEM;
115 goto out;
116 }
117 spin_lock_init(&priv->lock);
118 usb_set_serial_port_data(port, priv);
119 }
120
121 for (i = 0; i < NUM_READ_URBS; ++i) {
122 buffer = kmalloc(buffer_size, GFP_KERNEL);
123 if (!buffer) {
124 dev_err(&port->dev, "%s - out of memory.\n",
125 __FUNCTION__);
126 result = -ENOMEM;
127 goto errout;
128 }
129 urb = usb_alloc_urb(0, GFP_KERNEL);
130 if (!urb) {
Eric Sesterhenn0e185b72006-10-10 14:42:50 -0700131 kfree(buffer);
Andy Gay5dda1712006-07-03 18:43:01 -0400132 dev_err(&port->dev, "%s - no more urbs?\n",
133 __FUNCTION__);
134 result = -ENOMEM;
135 goto errout;
136 }
137 usb_fill_bulk_urb(urb, serial->dev,
138 usb_rcvbulkpipe(serial->dev,
139 port->bulk_out_endpointAddress),
140 buffer, buffer_size,
141 airprime_read_bulk_callback, port);
142 result = usb_submit_urb(urb, GFP_KERNEL);
143 if (result) {
Pete Zaitcevaf59cf42007-02-02 23:13:14 -0800144 usb_free_urb(urb);
145 kfree(buffer);
Andy Gay5dda1712006-07-03 18:43:01 -0400146 dev_err(&port->dev,
147 "%s - failed submitting read urb %d for port %d, error %d\n",
148 __FUNCTION__, i, port->number, result);
149 goto errout;
150 }
151 /* remember this urb so we can kill it when the port is closed */
152 priv->read_urbp[i] = urb;
153 }
154 goto out;
155
156 errout:
157 /* some error happened, cancel any submitted urbs and clean up anything that
158 got allocated successfully */
159
Pete Zaitcevaf59cf42007-02-02 23:13:14 -0800160 while (i-- != 0) {
Andy Gay5dda1712006-07-03 18:43:01 -0400161 urb = priv->read_urbp[i];
Pete Zaitcevaf59cf42007-02-02 23:13:14 -0800162 buffer = urb->transfer_buffer;
163 usb_kill_urb (urb);
164 usb_free_urb (urb);
165 kfree (buffer);
Andy Gay5dda1712006-07-03 18:43:01 -0400166 }
167
168 out:
169 return result;
170}
171
172static void airprime_close(struct usb_serial_port *port, struct file * filp)
173{
174 struct airprime_private *priv = usb_get_serial_port_data(port);
175 int i;
176
177 dbg("%s - port %d", __FUNCTION__, port->number);
178
Andy Gay5dda1712006-07-03 18:43:01 -0400179 for (i = 0; i < NUM_READ_URBS; ++i) {
180 usb_kill_urb (priv->read_urbp[i]);
Pete Zaitcevaf59cf42007-02-02 23:13:14 -0800181 kfree (priv->read_urbp[i]->transfer_buffer);
Andy Gay5dda1712006-07-03 18:43:01 -0400182 usb_free_urb (priv->read_urbp[i]);
183 }
184
185 /* free up private structure */
186 kfree (priv);
187 usb_set_serial_port_data(port, NULL);
188}
189
190static int airprime_write(struct usb_serial_port *port,
191 const unsigned char *buf, int count)
192{
193 struct airprime_private *priv = usb_get_serial_port_data(port);
194 struct usb_serial *serial = port->serial;
195 struct urb *urb;
196 unsigned char *buffer;
197 unsigned long flags;
198 int status;
199 dbg("%s - port %d", __FUNCTION__, port->number);
200
201 spin_lock_irqsave(&priv->lock, flags);
202 if (priv->outstanding_urbs > NUM_WRITE_URBS) {
203 spin_unlock_irqrestore(&priv->lock, flags);
204 dbg("%s - write limit hit\n", __FUNCTION__);
205 return 0;
206 }
207 spin_unlock_irqrestore(&priv->lock, flags);
208 buffer = kmalloc(count, GFP_ATOMIC);
209 if (!buffer) {
210 dev_err(&port->dev, "out of memory\n");
211 return -ENOMEM;
212 }
213 urb = usb_alloc_urb(0, GFP_ATOMIC);
214 if (!urb) {
215 dev_err(&port->dev, "no more free urbs\n");
216 kfree (buffer);
217 return -ENOMEM;
218 }
219 memcpy (buffer, buf, count);
220
221 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, buffer);
222
223 usb_fill_bulk_urb(urb, serial->dev,
224 usb_sndbulkpipe(serial->dev,
225 port->bulk_out_endpointAddress),
226 buffer, count,
227 airprime_write_bulk_callback, port);
228
229 /* send it down the pipe */
230 status = usb_submit_urb(urb, GFP_ATOMIC);
231 if (status) {
232 dev_err(&port->dev,
233 "%s - usb_submit_urb(write bulk) failed with status = %d\n",
234 __FUNCTION__, status);
235 count = status;
236 kfree (buffer);
237 } else {
238 spin_lock_irqsave(&priv->lock, flags);
239 ++priv->outstanding_urbs;
240 spin_unlock_irqrestore(&priv->lock, flags);
241 }
242 /* we are done with this urb, so let the host driver
243 * really free it when it is finished with it */
244 usb_free_urb (urb);
245 return count;
246}
247
Greg KH3b86b202005-04-25 21:46:29 -0700248static struct usb_driver airprime_driver = {
Greg KH3b86b202005-04-25 21:46:29 -0700249 .name = "airprime",
250 .probe = usb_serial_probe,
251 .disconnect = usb_serial_disconnect,
252 .id_table = id_table,
Andy Gay5dda1712006-07-03 18:43:01 -0400253 .no_dynamic_id = 1,
Greg KH3b86b202005-04-25 21:46:29 -0700254};
255
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700256static struct usb_serial_driver airprime_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700257 .driver = {
258 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700259 .name = "airprime",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700260 },
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100261 .usb_driver = &airprime_driver,
Greg KH3b86b202005-04-25 21:46:29 -0700262 .id_table = id_table,
263 .num_interrupt_in = NUM_DONT_CARE,
264 .num_bulk_in = NUM_DONT_CARE,
265 .num_bulk_out = NUM_DONT_CARE,
Andy Gay5dda1712006-07-03 18:43:01 -0400266 .open = airprime_open,
267 .close = airprime_close,
268 .write = airprime_write,
Greg KH3b86b202005-04-25 21:46:29 -0700269};
270
271static int __init airprime_init(void)
272{
273 int retval;
274
Andy Gay5dda1712006-07-03 18:43:01 -0400275 airprime_device.num_ports =
276 (endpoints > 0 && endpoints <= MAX_BULK_EPS) ? endpoints : NUM_BULK_EPS;
Greg KH3b86b202005-04-25 21:46:29 -0700277 retval = usb_serial_register(&airprime_device);
278 if (retval)
279 return retval;
280 retval = usb_register(&airprime_driver);
281 if (retval)
282 usb_serial_deregister(&airprime_device);
283 return retval;
284}
285
286static void __exit airprime_exit(void)
287{
Andy Gay5dda1712006-07-03 18:43:01 -0400288 dbg("%s", __FUNCTION__);
289
Greg KH3b86b202005-04-25 21:46:29 -0700290 usb_deregister(&airprime_driver);
291 usb_serial_deregister(&airprime_device);
292}
293
294module_init(airprime_init);
295module_exit(airprime_exit);
296MODULE_LICENSE("GPL");
Andy Gay5dda1712006-07-03 18:43:01 -0400297
298module_param(debug, bool, S_IRUGO | S_IWUSR);
299MODULE_PARM_DESC(debug, "Debug enabled");
300module_param(buffer_size, int, 0);
301MODULE_PARM_DESC(buffer_size, "Size of the transfer buffers in bytes (default 4096)");
302module_param(endpoints, int, 0);
303MODULE_PARM_DESC(endpoints, "Number of bulk EPs to configure (default 3)");