blob: 310a8b5f5906a6f54b226b5e2dddab2453d3f48f [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];
Martin Schillercf0cb1a2007-03-01 13:49:48 +010047
48 /* Settings for the port */
49 int rts_state; /* Handshaking pins (outputs) */
50 int dtr_state;
51 int cts_state; /* Handshaking pins (inputs) */
52 int dsr_state;
53 int dcd_state;
54 int ri_state;
Andy Gay5dda1712006-07-03 18:43:01 -040055};
56
Martin Schillercf0cb1a2007-03-01 13:49:48 +010057static int airprime_send_setup(struct usb_serial_port *port)
58{
59 struct usb_serial *serial = port->serial;
60 struct airprime_private *priv;
61
62 dbg("%s", __FUNCTION__);
63
64 if (port->number != 0)
65 return 0;
66
67 priv = usb_get_serial_port_data(port);
68
69 if (port->tty) {
70 int val = 0;
71 if (priv->dtr_state)
72 val |= 0x01;
73 if (priv->rts_state)
74 val |= 0x02;
75
76 return usb_control_msg(serial->dev,
77 usb_rcvctrlpipe(serial->dev, 0),
78 0x22,0x21,val,0,NULL,0,USB_CTRL_SET_TIMEOUT);
79 }
80
81 return 0;
82}
83
David Howells7d12e782006-10-05 14:55:46 +010084static void airprime_read_bulk_callback(struct urb *urb)
Andy Gay5dda1712006-07-03 18:43:01 -040085{
86 struct usb_serial_port *port = urb->context;
87 unsigned char *data = urb->transfer_buffer;
88 struct tty_struct *tty;
89 int result;
90
91 dbg("%s - port %d", __FUNCTION__, port->number);
92
93 if (urb->status) {
94 dbg("%s - nonzero read bulk status received: %d",
95 __FUNCTION__, urb->status);
Andy Gay5dda1712006-07-03 18:43:01 -040096 return;
97 }
98 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
99
100 tty = port->tty;
101 if (tty && urb->actual_length) {
102 tty_insert_flip_string (tty, data, urb->actual_length);
103 tty_flip_buffer_push (tty);
104 }
105
106 result = usb_submit_urb (urb, GFP_ATOMIC);
107 if (result)
108 dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n",
109 __FUNCTION__, result);
110 return;
111}
112
David Howells7d12e782006-10-05 14:55:46 +0100113static void airprime_write_bulk_callback(struct urb *urb)
Andy Gay5dda1712006-07-03 18:43:01 -0400114{
115 struct usb_serial_port *port = urb->context;
116 struct airprime_private *priv = usb_get_serial_port_data(port);
117 unsigned long flags;
118
119 dbg("%s - port %d", __FUNCTION__, port->number);
120
121 /* free up the transfer buffer, as usb_free_urb() does not do this */
122 kfree (urb->transfer_buffer);
123
124 if (urb->status)
125 dbg("%s - nonzero write bulk status received: %d",
126 __FUNCTION__, urb->status);
127 spin_lock_irqsave(&priv->lock, flags);
128 --priv->outstanding_urbs;
129 spin_unlock_irqrestore(&priv->lock, flags);
130
131 usb_serial_port_softint(port);
132}
133
134static int airprime_open(struct usb_serial_port *port, struct file *filp)
135{
136 struct airprime_private *priv = usb_get_serial_port_data(port);
137 struct usb_serial *serial = port->serial;
138 struct urb *urb;
139 char *buffer = NULL;
140 int i;
141 int result = 0;
142
143 dbg("%s - port %d", __FUNCTION__, port->number);
144
145 /* initialize our private data structure if it isn't already created */
146 if (!priv) {
147 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
148 if (!priv) {
149 result = -ENOMEM;
150 goto out;
151 }
152 spin_lock_init(&priv->lock);
153 usb_set_serial_port_data(port, priv);
154 }
155
Martin Schillercf0cb1a2007-03-01 13:49:48 +0100156 /* Set some sane defaults */
157 priv->rts_state = 1;
158 priv->dtr_state = 1;
159
Andy Gay5dda1712006-07-03 18:43:01 -0400160 for (i = 0; i < NUM_READ_URBS; ++i) {
161 buffer = kmalloc(buffer_size, GFP_KERNEL);
162 if (!buffer) {
163 dev_err(&port->dev, "%s - out of memory.\n",
164 __FUNCTION__);
165 result = -ENOMEM;
166 goto errout;
167 }
168 urb = usb_alloc_urb(0, GFP_KERNEL);
169 if (!urb) {
Eric Sesterhenn0e185b72006-10-10 14:42:50 -0700170 kfree(buffer);
Andy Gay5dda1712006-07-03 18:43:01 -0400171 dev_err(&port->dev, "%s - no more urbs?\n",
172 __FUNCTION__);
173 result = -ENOMEM;
174 goto errout;
175 }
176 usb_fill_bulk_urb(urb, serial->dev,
177 usb_rcvbulkpipe(serial->dev,
178 port->bulk_out_endpointAddress),
179 buffer, buffer_size,
180 airprime_read_bulk_callback, port);
181 result = usb_submit_urb(urb, GFP_KERNEL);
182 if (result) {
Pete Zaitcevaf59cf42007-02-02 23:13:14 -0800183 usb_free_urb(urb);
184 kfree(buffer);
Andy Gay5dda1712006-07-03 18:43:01 -0400185 dev_err(&port->dev,
186 "%s - failed submitting read urb %d for port %d, error %d\n",
187 __FUNCTION__, i, port->number, result);
188 goto errout;
189 }
190 /* remember this urb so we can kill it when the port is closed */
191 priv->read_urbp[i] = urb;
192 }
Martin Schillercf0cb1a2007-03-01 13:49:48 +0100193
194 airprime_send_setup(port);
195
Andy Gay5dda1712006-07-03 18:43:01 -0400196 goto out;
197
198 errout:
199 /* some error happened, cancel any submitted urbs and clean up anything that
200 got allocated successfully */
201
Pete Zaitcevaf59cf42007-02-02 23:13:14 -0800202 while (i-- != 0) {
Andy Gay5dda1712006-07-03 18:43:01 -0400203 urb = priv->read_urbp[i];
Pete Zaitcevaf59cf42007-02-02 23:13:14 -0800204 buffer = urb->transfer_buffer;
205 usb_kill_urb (urb);
206 usb_free_urb (urb);
207 kfree (buffer);
Andy Gay5dda1712006-07-03 18:43:01 -0400208 }
209
210 out:
211 return result;
212}
213
214static void airprime_close(struct usb_serial_port *port, struct file * filp)
215{
216 struct airprime_private *priv = usb_get_serial_port_data(port);
217 int i;
218
219 dbg("%s - port %d", __FUNCTION__, port->number);
220
Martin Schillercf0cb1a2007-03-01 13:49:48 +0100221 priv->rts_state = 0;
222 priv->dtr_state = 0;
223
224 airprime_send_setup(port);
225
Andy Gay5dda1712006-07-03 18:43:01 -0400226 for (i = 0; i < NUM_READ_URBS; ++i) {
227 usb_kill_urb (priv->read_urbp[i]);
Pete Zaitcevaf59cf42007-02-02 23:13:14 -0800228 kfree (priv->read_urbp[i]->transfer_buffer);
Andy Gay5dda1712006-07-03 18:43:01 -0400229 usb_free_urb (priv->read_urbp[i]);
230 }
231
232 /* free up private structure */
233 kfree (priv);
234 usb_set_serial_port_data(port, NULL);
235}
236
237static int airprime_write(struct usb_serial_port *port,
238 const unsigned char *buf, int count)
239{
240 struct airprime_private *priv = usb_get_serial_port_data(port);
241 struct usb_serial *serial = port->serial;
242 struct urb *urb;
243 unsigned char *buffer;
244 unsigned long flags;
245 int status;
246 dbg("%s - port %d", __FUNCTION__, port->number);
247
248 spin_lock_irqsave(&priv->lock, flags);
249 if (priv->outstanding_urbs > NUM_WRITE_URBS) {
250 spin_unlock_irqrestore(&priv->lock, flags);
251 dbg("%s - write limit hit\n", __FUNCTION__);
252 return 0;
253 }
254 spin_unlock_irqrestore(&priv->lock, flags);
255 buffer = kmalloc(count, GFP_ATOMIC);
256 if (!buffer) {
257 dev_err(&port->dev, "out of memory\n");
258 return -ENOMEM;
259 }
260 urb = usb_alloc_urb(0, GFP_ATOMIC);
261 if (!urb) {
262 dev_err(&port->dev, "no more free urbs\n");
263 kfree (buffer);
264 return -ENOMEM;
265 }
266 memcpy (buffer, buf, count);
267
268 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, buffer);
269
270 usb_fill_bulk_urb(urb, serial->dev,
271 usb_sndbulkpipe(serial->dev,
272 port->bulk_out_endpointAddress),
273 buffer, count,
274 airprime_write_bulk_callback, port);
275
276 /* send it down the pipe */
277 status = usb_submit_urb(urb, GFP_ATOMIC);
278 if (status) {
279 dev_err(&port->dev,
280 "%s - usb_submit_urb(write bulk) failed with status = %d\n",
281 __FUNCTION__, status);
282 count = status;
283 kfree (buffer);
284 } else {
285 spin_lock_irqsave(&priv->lock, flags);
286 ++priv->outstanding_urbs;
287 spin_unlock_irqrestore(&priv->lock, flags);
288 }
289 /* we are done with this urb, so let the host driver
290 * really free it when it is finished with it */
291 usb_free_urb (urb);
292 return count;
293}
294
Greg KH3b86b202005-04-25 21:46:29 -0700295static struct usb_driver airprime_driver = {
Greg KH3b86b202005-04-25 21:46:29 -0700296 .name = "airprime",
297 .probe = usb_serial_probe,
298 .disconnect = usb_serial_disconnect,
299 .id_table = id_table,
Andy Gay5dda1712006-07-03 18:43:01 -0400300 .no_dynamic_id = 1,
Greg KH3b86b202005-04-25 21:46:29 -0700301};
302
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700303static struct usb_serial_driver airprime_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700304 .driver = {
305 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700306 .name = "airprime",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700307 },
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100308 .usb_driver = &airprime_driver,
Greg KH3b86b202005-04-25 21:46:29 -0700309 .id_table = id_table,
310 .num_interrupt_in = NUM_DONT_CARE,
311 .num_bulk_in = NUM_DONT_CARE,
312 .num_bulk_out = NUM_DONT_CARE,
Andy Gay5dda1712006-07-03 18:43:01 -0400313 .open = airprime_open,
314 .close = airprime_close,
315 .write = airprime_write,
Greg KH3b86b202005-04-25 21:46:29 -0700316};
317
318static int __init airprime_init(void)
319{
320 int retval;
321
Andy Gay5dda1712006-07-03 18:43:01 -0400322 airprime_device.num_ports =
323 (endpoints > 0 && endpoints <= MAX_BULK_EPS) ? endpoints : NUM_BULK_EPS;
Greg KH3b86b202005-04-25 21:46:29 -0700324 retval = usb_serial_register(&airprime_device);
325 if (retval)
326 return retval;
327 retval = usb_register(&airprime_driver);
328 if (retval)
329 usb_serial_deregister(&airprime_device);
330 return retval;
331}
332
333static void __exit airprime_exit(void)
334{
Andy Gay5dda1712006-07-03 18:43:01 -0400335 dbg("%s", __FUNCTION__);
336
Greg KH3b86b202005-04-25 21:46:29 -0700337 usb_deregister(&airprime_driver);
338 usb_serial_deregister(&airprime_device);
339}
340
341module_init(airprime_init);
342module_exit(airprime_exit);
343MODULE_LICENSE("GPL");
Andy Gay5dda1712006-07-03 18:43:01 -0400344
345module_param(debug, bool, S_IRUGO | S_IWUSR);
346MODULE_PARM_DESC(debug, "Debug enabled");
347module_param(buffer_size, int, 0);
348MODULE_PARM_DESC(buffer_size, "Size of the transfer buffers in bytes (default 4096)");
349module_param(endpoints, int, 0);
350MODULE_PARM_DESC(endpoints, "Number of bulk EPs to configure (default 3)");