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(0x413c, 0x8115) }, /* Dell Wireless HSDPA 5500 */
24 MODULE_DEVICE_TABLE(usb, id_table);
26 #define URB_TRANSFER_BUFFER_SIZE 4096
27 #define NUM_READ_URBS 4
28 #define NUM_WRITE_URBS 4
29 #define NUM_BULK_EPS 3
30 #define MAX_BULK_EPS 6
32 /* if overridden by the user, then use their value for the size of the
33 * read and write urbs, and the number of endpoints */
34 static int buffer_size = URB_TRANSFER_BUFFER_SIZE;
35 static int endpoints = NUM_BULK_EPS;
37 struct airprime_private {
41 struct urb *read_urbp[NUM_READ_URBS];
43 /* Settings for the port */
44 int rts_state; /* Handshaking pins (outputs) */
46 int cts_state; /* Handshaking pins (inputs) */
52 static int airprime_send_setup(struct usb_serial_port *port)
54 struct usb_serial *serial = port->serial;
55 struct airprime_private *priv;
57 dbg("%s", __FUNCTION__);
59 if (port->number != 0)
62 priv = usb_get_serial_port_data(port);
71 return usb_control_msg(serial->dev,
72 usb_rcvctrlpipe(serial->dev, 0),
73 0x22,0x21,val,0,NULL,0,USB_CTRL_SET_TIMEOUT);
79 static void airprime_read_bulk_callback(struct urb *urb)
81 struct usb_serial_port *port = urb->context;
82 unsigned char *data = urb->transfer_buffer;
83 struct tty_struct *tty;
85 int status = urb->status;
87 dbg("%s - port %d", __FUNCTION__, port->number);
90 dbg("%s - nonzero read bulk status received: %d",
91 __FUNCTION__, status);
94 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
97 if (tty && urb->actual_length) {
98 tty_insert_flip_string (tty, data, urb->actual_length);
99 tty_flip_buffer_push (tty);
102 result = usb_submit_urb (urb, GFP_ATOMIC);
104 dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n",
105 __FUNCTION__, result);
109 static void airprime_write_bulk_callback(struct urb *urb)
111 struct usb_serial_port *port = urb->context;
112 struct airprime_private *priv = usb_get_serial_port_data(port);
113 int status = urb->status;
116 dbg("%s - port %d", __FUNCTION__, port->number);
118 /* free up the transfer buffer, as usb_free_urb() does not do this */
119 kfree (urb->transfer_buffer);
122 dbg("%s - nonzero write bulk status received: %d",
123 __FUNCTION__, status);
124 spin_lock_irqsave(&priv->lock, flags);
125 --priv->outstanding_urbs;
126 spin_unlock_irqrestore(&priv->lock, flags);
128 usb_serial_port_softint(port);
131 static int airprime_open(struct usb_serial_port *port, struct file *filp)
133 struct airprime_private *priv = usb_get_serial_port_data(port);
134 struct usb_serial *serial = port->serial;
140 dbg("%s - port %d", __FUNCTION__, port->number);
142 /* initialize our private data structure if it isn't already created */
144 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
149 spin_lock_init(&priv->lock);
150 usb_set_serial_port_data(port, priv);
153 /* Set some sane defaults */
157 for (i = 0; i < NUM_READ_URBS; ++i) {
158 buffer = kmalloc(buffer_size, GFP_KERNEL);
160 dev_err(&port->dev, "%s - out of memory.\n",
165 urb = usb_alloc_urb(0, GFP_KERNEL);
168 dev_err(&port->dev, "%s - no more urbs?\n",
173 usb_fill_bulk_urb(urb, serial->dev,
174 usb_rcvbulkpipe(serial->dev,
175 port->bulk_out_endpointAddress),
177 airprime_read_bulk_callback, port);
178 result = usb_submit_urb(urb, GFP_KERNEL);
183 "%s - failed submitting read urb %d for port %d, error %d\n",
184 __FUNCTION__, i, port->number, result);
187 /* remember this urb so we can kill it when the port is closed */
188 priv->read_urbp[i] = urb;
191 airprime_send_setup(port);
196 /* some error happened, cancel any submitted urbs and clean up anything that
197 got allocated successfully */
200 urb = priv->read_urbp[i];
201 buffer = urb->transfer_buffer;
211 static void airprime_close(struct usb_serial_port *port, struct file * filp)
213 struct airprime_private *priv = usb_get_serial_port_data(port);
216 dbg("%s - port %d", __FUNCTION__, port->number);
221 airprime_send_setup(port);
223 for (i = 0; i < NUM_READ_URBS; ++i) {
224 usb_kill_urb (priv->read_urbp[i]);
225 kfree (priv->read_urbp[i]->transfer_buffer);
226 usb_free_urb (priv->read_urbp[i]);
229 /* free up private structure */
231 usb_set_serial_port_data(port, NULL);
234 static int airprime_write(struct usb_serial_port *port,
235 const unsigned char *buf, int count)
237 struct airprime_private *priv = usb_get_serial_port_data(port);
238 struct usb_serial *serial = port->serial;
240 unsigned char *buffer;
243 dbg("%s - port %d", __FUNCTION__, port->number);
245 spin_lock_irqsave(&priv->lock, flags);
246 if (priv->outstanding_urbs > NUM_WRITE_URBS) {
247 spin_unlock_irqrestore(&priv->lock, flags);
248 dbg("%s - write limit hit\n", __FUNCTION__);
251 spin_unlock_irqrestore(&priv->lock, flags);
252 buffer = kmalloc(count, GFP_ATOMIC);
254 dev_err(&port->dev, "out of memory\n");
257 urb = usb_alloc_urb(0, GFP_ATOMIC);
259 dev_err(&port->dev, "no more free urbs\n");
263 memcpy (buffer, buf, count);
265 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, buffer);
267 usb_fill_bulk_urb(urb, serial->dev,
268 usb_sndbulkpipe(serial->dev,
269 port->bulk_out_endpointAddress),
271 airprime_write_bulk_callback, port);
273 /* send it down the pipe */
274 status = usb_submit_urb(urb, GFP_ATOMIC);
277 "%s - usb_submit_urb(write bulk) failed with status = %d\n",
278 __FUNCTION__, status);
282 spin_lock_irqsave(&priv->lock, flags);
283 ++priv->outstanding_urbs;
284 spin_unlock_irqrestore(&priv->lock, flags);
286 /* we are done with this urb, so let the host driver
287 * really free it when it is finished with it */
292 static struct usb_driver airprime_driver = {
294 .probe = usb_serial_probe,
295 .disconnect = usb_serial_disconnect,
296 .id_table = id_table,
300 static struct usb_serial_driver airprime_device = {
302 .owner = THIS_MODULE,
305 .usb_driver = &airprime_driver,
306 .id_table = id_table,
307 .num_interrupt_in = NUM_DONT_CARE,
308 .num_bulk_in = NUM_DONT_CARE,
309 .num_bulk_out = NUM_DONT_CARE,
310 .open = airprime_open,
311 .close = airprime_close,
312 .write = airprime_write,
315 static int __init airprime_init(void)
319 airprime_device.num_ports =
320 (endpoints > 0 && endpoints <= MAX_BULK_EPS) ? endpoints : NUM_BULK_EPS;
321 retval = usb_serial_register(&airprime_device);
324 retval = usb_register(&airprime_driver);
326 usb_serial_deregister(&airprime_device);
330 static void __exit airprime_exit(void)
332 dbg("%s", __FUNCTION__);
334 usb_deregister(&airprime_driver);
335 usb_serial_deregister(&airprime_device);
338 module_init(airprime_init);
339 module_exit(airprime_exit);
340 MODULE_LICENSE("GPL");
342 module_param(debug, bool, S_IRUGO | S_IWUSR);
343 MODULE_PARM_DESC(debug, "Debug enabled");
344 module_param(buffer_size, int, 0);
345 MODULE_PARM_DESC(buffer_size, "Size of the transfer buffers in bytes (default 4096)");
346 module_param(endpoints, int, 0);
347 MODULE_PARM_DESC(endpoints, "Number of bulk EPs to configure (default 3)");