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, 0x1130) }, /* Novatel Wireless S720 CDMA/EV-DO */
23 { USB_DEVICE(0x1410, 0x2110) }, /* Novatel Wireless U720 CDMA/EV-DO */
24 { USB_DEVICE(0x1410, 0x1430) }, /* Novatel Merlin XU870 HSDPA/3G */
25 { USB_DEVICE(0x1410, 0x1100) }, /* ExpressCard34 Qualcomm 3G CDMA */
26 { USB_DEVICE(0x413c, 0x8115) }, /* Dell Wireless HSDPA 5500 */
29 MODULE_DEVICE_TABLE(usb, id_table);
31 #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
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 */
39 static int buffer_size = URB_TRANSFER_BUFFER_SIZE;
40 static int endpoints = NUM_BULK_EPS;
42 struct airprime_private {
46 struct urb *read_urbp[NUM_READ_URBS];
48 /* Settings for the port */
49 int rts_state; /* Handshaking pins (outputs) */
51 int cts_state; /* Handshaking pins (inputs) */
57 static int airprime_send_setup(struct usb_serial_port *port)
59 struct usb_serial *serial = port->serial;
60 struct airprime_private *priv;
62 dbg("%s", __FUNCTION__);
64 if (port->number != 0)
67 priv = usb_get_serial_port_data(port);
76 return usb_control_msg(serial->dev,
77 usb_rcvctrlpipe(serial->dev, 0),
78 0x22,0x21,val,0,NULL,0,USB_CTRL_SET_TIMEOUT);
84 static void airprime_read_bulk_callback(struct urb *urb)
86 struct usb_serial_port *port = urb->context;
87 unsigned char *data = urb->transfer_buffer;
88 struct tty_struct *tty;
91 dbg("%s - port %d", __FUNCTION__, port->number);
94 dbg("%s - nonzero read bulk status received: %d",
95 __FUNCTION__, urb->status);
98 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
101 if (tty && urb->actual_length) {
102 tty_insert_flip_string (tty, data, urb->actual_length);
103 tty_flip_buffer_push (tty);
106 result = usb_submit_urb (urb, GFP_ATOMIC);
108 dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n",
109 __FUNCTION__, result);
113 static void airprime_write_bulk_callback(struct urb *urb)
115 struct usb_serial_port *port = urb->context;
116 struct airprime_private *priv = usb_get_serial_port_data(port);
119 dbg("%s - port %d", __FUNCTION__, port->number);
121 /* free up the transfer buffer, as usb_free_urb() does not do this */
122 kfree (urb->transfer_buffer);
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);
131 usb_serial_port_softint(port);
134 static int airprime_open(struct usb_serial_port *port, struct file *filp)
136 struct airprime_private *priv = usb_get_serial_port_data(port);
137 struct usb_serial *serial = port->serial;
143 dbg("%s - port %d", __FUNCTION__, port->number);
145 /* initialize our private data structure if it isn't already created */
147 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
152 spin_lock_init(&priv->lock);
153 usb_set_serial_port_data(port, priv);
156 /* Set some sane defaults */
160 for (i = 0; i < NUM_READ_URBS; ++i) {
161 buffer = kmalloc(buffer_size, GFP_KERNEL);
163 dev_err(&port->dev, "%s - out of memory.\n",
168 urb = usb_alloc_urb(0, GFP_KERNEL);
171 dev_err(&port->dev, "%s - no more urbs?\n",
176 usb_fill_bulk_urb(urb, serial->dev,
177 usb_rcvbulkpipe(serial->dev,
178 port->bulk_out_endpointAddress),
180 airprime_read_bulk_callback, port);
181 result = usb_submit_urb(urb, GFP_KERNEL);
186 "%s - failed submitting read urb %d for port %d, error %d\n",
187 __FUNCTION__, i, port->number, result);
190 /* remember this urb so we can kill it when the port is closed */
191 priv->read_urbp[i] = urb;
194 airprime_send_setup(port);
199 /* some error happened, cancel any submitted urbs and clean up anything that
200 got allocated successfully */
203 urb = priv->read_urbp[i];
204 buffer = urb->transfer_buffer;
214 static void airprime_close(struct usb_serial_port *port, struct file * filp)
216 struct airprime_private *priv = usb_get_serial_port_data(port);
219 dbg("%s - port %d", __FUNCTION__, port->number);
224 airprime_send_setup(port);
226 for (i = 0; i < NUM_READ_URBS; ++i) {
227 usb_kill_urb (priv->read_urbp[i]);
228 kfree (priv->read_urbp[i]->transfer_buffer);
229 usb_free_urb (priv->read_urbp[i]);
232 /* free up private structure */
234 usb_set_serial_port_data(port, NULL);
237 static int airprime_write(struct usb_serial_port *port,
238 const unsigned char *buf, int count)
240 struct airprime_private *priv = usb_get_serial_port_data(port);
241 struct usb_serial *serial = port->serial;
243 unsigned char *buffer;
246 dbg("%s - port %d", __FUNCTION__, port->number);
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__);
254 spin_unlock_irqrestore(&priv->lock, flags);
255 buffer = kmalloc(count, GFP_ATOMIC);
257 dev_err(&port->dev, "out of memory\n");
260 urb = usb_alloc_urb(0, GFP_ATOMIC);
262 dev_err(&port->dev, "no more free urbs\n");
266 memcpy (buffer, buf, count);
268 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, buffer);
270 usb_fill_bulk_urb(urb, serial->dev,
271 usb_sndbulkpipe(serial->dev,
272 port->bulk_out_endpointAddress),
274 airprime_write_bulk_callback, port);
276 /* send it down the pipe */
277 status = usb_submit_urb(urb, GFP_ATOMIC);
280 "%s - usb_submit_urb(write bulk) failed with status = %d\n",
281 __FUNCTION__, status);
285 spin_lock_irqsave(&priv->lock, flags);
286 ++priv->outstanding_urbs;
287 spin_unlock_irqrestore(&priv->lock, flags);
289 /* we are done with this urb, so let the host driver
290 * really free it when it is finished with it */
295 static struct usb_driver airprime_driver = {
297 .probe = usb_serial_probe,
298 .disconnect = usb_serial_disconnect,
299 .id_table = id_table,
303 static struct usb_serial_driver airprime_device = {
305 .owner = THIS_MODULE,
308 .usb_driver = &airprime_driver,
309 .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,
313 .open = airprime_open,
314 .close = airprime_close,
315 .write = airprime_write,
318 static int __init airprime_init(void)
322 airprime_device.num_ports =
323 (endpoints > 0 && endpoints <= MAX_BULK_EPS) ? endpoints : NUM_BULK_EPS;
324 retval = usb_serial_register(&airprime_device);
327 retval = usb_register(&airprime_driver);
329 usb_serial_deregister(&airprime_device);
333 static void __exit airprime_exit(void)
335 dbg("%s", __FUNCTION__);
337 usb_deregister(&airprime_driver);
338 usb_serial_deregister(&airprime_device);
341 module_init(airprime_init);
342 module_exit(airprime_exit);
343 MODULE_LICENSE("GPL");
345 module_param(debug, bool, S_IRUGO | S_IWUSR);
346 MODULE_PARM_DESC(debug, "Debug enabled");
347 module_param(buffer_size, int, 0);
348 MODULE_PARM_DESC(buffer_size, "Size of the transfer buffers in bytes (default 4096)");
349 module_param(endpoints, int, 0);
350 MODULE_PARM_DESC(endpoints, "Number of bulk EPs to configure (default 3)");