2 * AirPrime CDMA Wireless Serial USB driver
4 * Copyright (C) 2005-2006 Greg Kroah-Hartman <gregkh@suse.de>
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.
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/tty.h>
14 #include <linux/tty_flip.h>
15 #include <linux/module.h>
16 #include <linux/usb.h>
17 #include <linux/usb/serial.h>
19 static struct usb_device_id id_table [] = {
20 { USB_DEVICE(0x0c88, 0x17da) }, /* Kyocera Wireless KPC650/Passport */
21 { USB_DEVICE(0x1410, 0x1110) }, /* Novatel Wireless Merlin CDMA */
22 { USB_DEVICE(0x1410, 0x1430) }, /* Novatel Merlin XU870 HSDPA/3G */
23 { USB_DEVICE(0x1410, 0x1100) }, /* ExpressCard34 Qualcomm 3G CDMA */
24 { USB_DEVICE(0x413c, 0x8115) }, /* Dell Wireless HSDPA 5500 */
27 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
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 */
37 static int buffer_size = URB_TRANSFER_BUFFER_SIZE;
38 static int endpoints = NUM_BULK_EPS;
40 struct airprime_private {
44 struct urb *read_urbp[NUM_READ_URBS];
47 static void airprime_read_bulk_callback(struct urb *urb)
49 struct usb_serial_port *port = urb->context;
50 unsigned char *data = urb->transfer_buffer;
51 struct tty_struct *tty;
54 dbg("%s - port %d", __FUNCTION__, port->number);
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;
66 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
69 if (tty && urb->actual_length) {
70 tty_insert_flip_string (tty, data, urb->actual_length);
71 tty_flip_buffer_push (tty);
74 result = usb_submit_urb (urb, GFP_ATOMIC);
76 dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n",
77 __FUNCTION__, result);
81 static void airprime_write_bulk_callback(struct urb *urb)
83 struct usb_serial_port *port = urb->context;
84 struct airprime_private *priv = usb_get_serial_port_data(port);
87 dbg("%s - port %d", __FUNCTION__, port->number);
89 /* free up the transfer buffer, as usb_free_urb() does not do this */
90 kfree (urb->transfer_buffer);
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);
99 usb_serial_port_softint(port);
102 static int airprime_open(struct usb_serial_port *port, struct file *filp)
104 struct airprime_private *priv = usb_get_serial_port_data(port);
105 struct usb_serial *serial = port->serial;
111 dbg("%s - port %d", __FUNCTION__, port->number);
113 /* initialize our private data structure if it isn't already created */
115 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
120 spin_lock_init(&priv->lock);
121 usb_set_serial_port_data(port, priv);
124 for (i = 0; i < NUM_READ_URBS; ++i) {
125 buffer = kmalloc(buffer_size, GFP_KERNEL);
127 dev_err(&port->dev, "%s - out of memory.\n",
132 urb = usb_alloc_urb(0, GFP_KERNEL);
135 dev_err(&port->dev, "%s - no more urbs?\n",
140 usb_fill_bulk_urb(urb, serial->dev,
141 usb_rcvbulkpipe(serial->dev,
142 port->bulk_out_endpointAddress),
144 airprime_read_bulk_callback, port);
145 result = usb_submit_urb(urb, GFP_KERNEL);
148 "%s - failed submitting read urb %d for port %d, error %d\n",
149 __FUNCTION__, i, port->number, result);
152 /* remember this urb so we can kill it when the port is closed */
153 priv->read_urbp[i] = urb;
158 /* some error happened, cancel any submitted urbs and clean up anything that
159 got allocated successfully */
161 for ( ; i >= 0; --i) {
162 urb = priv->read_urbp[i];
164 /* This urb was submitted successfully. So we have to
166 Unlinking the urb will invoke read_bulk_callback()
167 with an error status, so its transfer buffer will
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
175 if (urb->transfer_buffer) {
176 kfree (urb->transfer_buffer);
177 urb->transfer_buffer = NULL;
188 static void airprime_close(struct usb_serial_port *port, struct file * filp)
190 struct airprime_private *priv = usb_get_serial_port_data(port);
193 dbg("%s - port %d", __FUNCTION__, port->number);
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]);
202 /* free up private structure */
204 usb_set_serial_port_data(port, NULL);
207 static int airprime_write(struct usb_serial_port *port,
208 const unsigned char *buf, int count)
210 struct airprime_private *priv = usb_get_serial_port_data(port);
211 struct usb_serial *serial = port->serial;
213 unsigned char *buffer;
216 dbg("%s - port %d", __FUNCTION__, port->number);
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__);
224 spin_unlock_irqrestore(&priv->lock, flags);
225 buffer = kmalloc(count, GFP_ATOMIC);
227 dev_err(&port->dev, "out of memory\n");
230 urb = usb_alloc_urb(0, GFP_ATOMIC);
232 dev_err(&port->dev, "no more free urbs\n");
236 memcpy (buffer, buf, count);
238 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, buffer);
240 usb_fill_bulk_urb(urb, serial->dev,
241 usb_sndbulkpipe(serial->dev,
242 port->bulk_out_endpointAddress),
244 airprime_write_bulk_callback, port);
246 /* send it down the pipe */
247 status = usb_submit_urb(urb, GFP_ATOMIC);
250 "%s - usb_submit_urb(write bulk) failed with status = %d\n",
251 __FUNCTION__, status);
255 spin_lock_irqsave(&priv->lock, flags);
256 ++priv->outstanding_urbs;
257 spin_unlock_irqrestore(&priv->lock, flags);
259 /* we are done with this urb, so let the host driver
260 * really free it when it is finished with it */
265 static struct usb_driver airprime_driver = {
267 .probe = usb_serial_probe,
268 .disconnect = usb_serial_disconnect,
269 .id_table = id_table,
273 static struct usb_serial_driver airprime_device = {
275 .owner = THIS_MODULE,
278 .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,
282 .open = airprime_open,
283 .close = airprime_close,
284 .write = airprime_write,
287 static int __init airprime_init(void)
291 airprime_device.num_ports =
292 (endpoints > 0 && endpoints <= MAX_BULK_EPS) ? endpoints : NUM_BULK_EPS;
293 retval = usb_serial_register(&airprime_device);
296 retval = usb_register(&airprime_driver);
298 usb_serial_deregister(&airprime_device);
302 static void __exit airprime_exit(void)
304 dbg("%s", __FUNCTION__);
306 usb_deregister(&airprime_driver);
307 usb_serial_deregister(&airprime_device);
310 module_init(airprime_init);
311 module_exit(airprime_exit);
312 MODULE_LICENSE("GPL");
314 module_param(debug, bool, S_IRUGO | S_IWUSR);
315 MODULE_PARM_DESC(debug, "Debug enabled");
316 module_param(buffer_size, int, 0);
317 MODULE_PARM_DESC(buffer_size, "Size of the transfer buffers in bytes (default 4096)");
318 module_param(endpoints, int, 0);
319 MODULE_PARM_DESC(endpoints, "Number of bulk EPs to configure (default 3)");