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, 0x1100) }, /* ExpressCard34 Qualcomm 3G CDMA */
22 { USB_DEVICE(0x1410, 0x1110) }, /* Novatel Wireless Merlin CDMA */
23 { USB_DEVICE(0x1410, 0x1130) }, /* Novatel Wireless S720 CDMA/EV-DO */
24 { USB_DEVICE(0x1410, 0x1430) }, /* Novatel Merlin XU870 HSDPA/3G */
25 { USB_DEVICE(0x1410, 0x2100) }, /* Novatel Wireless EV620 CDMA/EV-DO */
26 { USB_DEVICE(0x1410, 0x2110) }, /* Novatel Wireless U720 CDMA/EV-DO */
27 { USB_DEVICE(0x413c, 0x8115) }, /* Dell Wireless HSDPA 5500 */
30 MODULE_DEVICE_TABLE(usb, id_table);
32 #define URB_TRANSFER_BUFFER_SIZE 4096
33 #define NUM_READ_URBS 4
34 #define NUM_WRITE_URBS 4
35 #define NUM_BULK_EPS 3
36 #define MAX_BULK_EPS 6
38 /* if overridden by the user, then use their value for the size of the
39 * read and write urbs, and the number of endpoints */
40 static int buffer_size = URB_TRANSFER_BUFFER_SIZE;
41 static int endpoints = NUM_BULK_EPS;
43 struct airprime_private {
47 struct urb *read_urbp[NUM_READ_URBS];
49 /* Settings for the port */
50 int rts_state; /* Handshaking pins (outputs) */
52 int cts_state; /* Handshaking pins (inputs) */
58 static int airprime_send_setup(struct usb_serial_port *port)
60 struct usb_serial *serial = port->serial;
61 struct airprime_private *priv;
63 dbg("%s", __FUNCTION__);
65 if (port->number != 0)
68 priv = usb_get_serial_port_data(port);
77 return usb_control_msg(serial->dev,
78 usb_rcvctrlpipe(serial->dev, 0),
79 0x22,0x21,val,0,NULL,0,USB_CTRL_SET_TIMEOUT);
85 static void airprime_read_bulk_callback(struct urb *urb)
87 struct usb_serial_port *port = urb->context;
88 unsigned char *data = urb->transfer_buffer;
89 struct tty_struct *tty;
92 dbg("%s - port %d", __FUNCTION__, port->number);
95 dbg("%s - nonzero read bulk status received: %d",
96 __FUNCTION__, urb->status);
99 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
102 if (tty && urb->actual_length) {
103 tty_insert_flip_string (tty, data, urb->actual_length);
104 tty_flip_buffer_push (tty);
107 result = usb_submit_urb (urb, GFP_ATOMIC);
109 dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n",
110 __FUNCTION__, result);
114 static void airprime_write_bulk_callback(struct urb *urb)
116 struct usb_serial_port *port = urb->context;
117 struct airprime_private *priv = usb_get_serial_port_data(port);
120 dbg("%s - port %d", __FUNCTION__, port->number);
122 /* free up the transfer buffer, as usb_free_urb() does not do this */
123 kfree (urb->transfer_buffer);
126 dbg("%s - nonzero write bulk status received: %d",
127 __FUNCTION__, urb->status);
128 spin_lock_irqsave(&priv->lock, flags);
129 --priv->outstanding_urbs;
130 spin_unlock_irqrestore(&priv->lock, flags);
132 usb_serial_port_softint(port);
135 static int airprime_open(struct usb_serial_port *port, struct file *filp)
137 struct airprime_private *priv = usb_get_serial_port_data(port);
138 struct usb_serial *serial = port->serial;
144 dbg("%s - port %d", __FUNCTION__, port->number);
146 /* initialize our private data structure if it isn't already created */
148 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
153 spin_lock_init(&priv->lock);
154 usb_set_serial_port_data(port, priv);
157 /* Set some sane defaults */
161 for (i = 0; i < NUM_READ_URBS; ++i) {
162 buffer = kmalloc(buffer_size, GFP_KERNEL);
164 dev_err(&port->dev, "%s - out of memory.\n",
169 urb = usb_alloc_urb(0, GFP_KERNEL);
172 dev_err(&port->dev, "%s - no more urbs?\n",
177 usb_fill_bulk_urb(urb, serial->dev,
178 usb_rcvbulkpipe(serial->dev,
179 port->bulk_out_endpointAddress),
181 airprime_read_bulk_callback, port);
182 result = usb_submit_urb(urb, GFP_KERNEL);
187 "%s - failed submitting read urb %d for port %d, error %d\n",
188 __FUNCTION__, i, port->number, result);
191 /* remember this urb so we can kill it when the port is closed */
192 priv->read_urbp[i] = urb;
195 airprime_send_setup(port);
200 /* some error happened, cancel any submitted urbs and clean up anything that
201 got allocated successfully */
204 urb = priv->read_urbp[i];
205 buffer = urb->transfer_buffer;
215 static void airprime_close(struct usb_serial_port *port, struct file * filp)
217 struct airprime_private *priv = usb_get_serial_port_data(port);
220 dbg("%s - port %d", __FUNCTION__, port->number);
225 airprime_send_setup(port);
227 for (i = 0; i < NUM_READ_URBS; ++i) {
228 usb_kill_urb (priv->read_urbp[i]);
229 kfree (priv->read_urbp[i]->transfer_buffer);
230 usb_free_urb (priv->read_urbp[i]);
233 /* free up private structure */
235 usb_set_serial_port_data(port, NULL);
238 static int airprime_write(struct usb_serial_port *port,
239 const unsigned char *buf, int count)
241 struct airprime_private *priv = usb_get_serial_port_data(port);
242 struct usb_serial *serial = port->serial;
244 unsigned char *buffer;
247 dbg("%s - port %d", __FUNCTION__, port->number);
249 spin_lock_irqsave(&priv->lock, flags);
250 if (priv->outstanding_urbs > NUM_WRITE_URBS) {
251 spin_unlock_irqrestore(&priv->lock, flags);
252 dbg("%s - write limit hit\n", __FUNCTION__);
255 spin_unlock_irqrestore(&priv->lock, flags);
256 buffer = kmalloc(count, GFP_ATOMIC);
258 dev_err(&port->dev, "out of memory\n");
261 urb = usb_alloc_urb(0, GFP_ATOMIC);
263 dev_err(&port->dev, "no more free urbs\n");
267 memcpy (buffer, buf, count);
269 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, buffer);
271 usb_fill_bulk_urb(urb, serial->dev,
272 usb_sndbulkpipe(serial->dev,
273 port->bulk_out_endpointAddress),
275 airprime_write_bulk_callback, port);
277 /* send it down the pipe */
278 status = usb_submit_urb(urb, GFP_ATOMIC);
281 "%s - usb_submit_urb(write bulk) failed with status = %d\n",
282 __FUNCTION__, status);
286 spin_lock_irqsave(&priv->lock, flags);
287 ++priv->outstanding_urbs;
288 spin_unlock_irqrestore(&priv->lock, flags);
290 /* we are done with this urb, so let the host driver
291 * really free it when it is finished with it */
296 static struct usb_driver airprime_driver = {
298 .probe = usb_serial_probe,
299 .disconnect = usb_serial_disconnect,
300 .id_table = id_table,
304 static struct usb_serial_driver airprime_device = {
306 .owner = THIS_MODULE,
309 .usb_driver = &airprime_driver,
310 .id_table = id_table,
311 .num_interrupt_in = NUM_DONT_CARE,
312 .num_bulk_in = NUM_DONT_CARE,
313 .num_bulk_out = NUM_DONT_CARE,
314 .open = airprime_open,
315 .close = airprime_close,
316 .write = airprime_write,
319 static int __init airprime_init(void)
323 airprime_device.num_ports =
324 (endpoints > 0 && endpoints <= MAX_BULK_EPS) ? endpoints : NUM_BULK_EPS;
325 retval = usb_serial_register(&airprime_device);
328 retval = usb_register(&airprime_driver);
330 usb_serial_deregister(&airprime_device);
334 static void __exit airprime_exit(void)
336 dbg("%s", __FUNCTION__);
338 usb_deregister(&airprime_driver);
339 usb_serial_deregister(&airprime_device);
342 module_init(airprime_init);
343 module_exit(airprime_exit);
344 MODULE_LICENSE("GPL");
346 module_param(debug, bool, S_IRUGO | S_IWUSR);
347 MODULE_PARM_DESC(debug, "Debug enabled");
348 module_param(buffer_size, int, 0);
349 MODULE_PARM_DESC(buffer_size, "Size of the transfer buffers in bytes (default 4096)");
350 module_param(endpoints, int, 0);
351 MODULE_PARM_DESC(endpoints, "Number of bulk EPs to configure (default 3)");