MOXA linux-2.6.x / linux-2.6.19-uc1 from UC-7110-LX-BOOTLOADER-1.9_VERSION-4.2.tgz
[linux-2.6.19-moxart.git] / drivers / usb / serial / airprime.c
blob7f5d546da39af963b57123b9b35e58e714a88cd0
1 /*
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.
9 */
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, 0x1100) }, /* ExpressCard34 Qualcomm 3G CDMA */
23 { },
25 MODULE_DEVICE_TABLE(usb, id_table);
27 #define URB_TRANSFER_BUFFER_SIZE 4096
28 #define NUM_READ_URBS 4
29 #define NUM_WRITE_URBS 4
30 #define NUM_BULK_EPS 3
31 #define MAX_BULK_EPS 6
33 /* if overridden by the user, then use their value for the size of the
34 * read and write urbs, and the number of endpoints */
35 static int buffer_size = URB_TRANSFER_BUFFER_SIZE;
36 static int endpoints = NUM_BULK_EPS;
37 static int debug;
38 struct airprime_private {
39 spinlock_t lock;
40 int outstanding_urbs;
41 int throttled;
42 struct urb *read_urbp[NUM_READ_URBS];
45 static void airprime_read_bulk_callback(struct urb *urb)
47 struct usb_serial_port *port = urb->context;
48 unsigned char *data = urb->transfer_buffer;
49 struct tty_struct *tty;
50 int result;
52 dbg("%s - port %d", __FUNCTION__, port->number);
54 if (urb->status) {
55 dbg("%s - nonzero read bulk status received: %d",
56 __FUNCTION__, urb->status);
57 /* something happened, so free up the memory for this urb */
58 if (urb->transfer_buffer) {
59 kfree (urb->transfer_buffer);
60 urb->transfer_buffer = NULL;
62 return;
64 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
66 tty = port->tty;
67 if (tty && urb->actual_length) {
68 tty_insert_flip_string (tty, data, urb->actual_length);
69 tty_flip_buffer_push (tty);
72 result = usb_submit_urb (urb, GFP_ATOMIC);
73 if (result)
74 dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n",
75 __FUNCTION__, result);
76 return;
79 static void airprime_write_bulk_callback(struct urb *urb)
81 struct usb_serial_port *port = urb->context;
82 struct airprime_private *priv = usb_get_serial_port_data(port);
83 unsigned long flags;
85 dbg("%s - port %d", __FUNCTION__, port->number);
87 /* free up the transfer buffer, as usb_free_urb() does not do this */
88 kfree (urb->transfer_buffer);
90 if (urb->status)
91 dbg("%s - nonzero write bulk status received: %d",
92 __FUNCTION__, urb->status);
93 spin_lock_irqsave(&priv->lock, flags);
94 --priv->outstanding_urbs;
95 spin_unlock_irqrestore(&priv->lock, flags);
97 usb_serial_port_softint(port);
100 static int airprime_open(struct usb_serial_port *port, struct file *filp)
102 struct airprime_private *priv = usb_get_serial_port_data(port);
103 struct usb_serial *serial = port->serial;
104 struct urb *urb;
105 char *buffer = NULL;
106 int i;
107 int result = 0;
109 dbg("%s - port %d", __FUNCTION__, port->number);
111 /* initialize our private data structure if it isn't already created */
112 if (!priv) {
113 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
114 if (!priv) {
115 result = -ENOMEM;
116 goto out;
118 spin_lock_init(&priv->lock);
119 usb_set_serial_port_data(port, priv);
122 for (i = 0; i < NUM_READ_URBS; ++i) {
123 buffer = kmalloc(buffer_size, GFP_KERNEL);
124 if (!buffer) {
125 dev_err(&port->dev, "%s - out of memory.\n",
126 __FUNCTION__);
127 result = -ENOMEM;
128 goto errout;
130 urb = usb_alloc_urb(0, GFP_KERNEL);
131 if (!urb) {
132 kfree(buffer);
133 dev_err(&port->dev, "%s - no more urbs?\n",
134 __FUNCTION__);
135 result = -ENOMEM;
136 goto errout;
138 usb_fill_bulk_urb(urb, serial->dev,
139 usb_rcvbulkpipe(serial->dev,
140 port->bulk_out_endpointAddress),
141 buffer, buffer_size,
142 airprime_read_bulk_callback, port);
143 result = usb_submit_urb(urb, GFP_KERNEL);
144 if (result) {
145 dev_err(&port->dev,
146 "%s - failed submitting read urb %d for port %d, error %d\n",
147 __FUNCTION__, i, port->number, result);
148 goto errout;
150 /* remember this urb so we can kill it when the port is closed */
151 priv->read_urbp[i] = urb;
153 goto out;
155 errout:
156 /* some error happened, cancel any submitted urbs and clean up anything that
157 got allocated successfully */
159 for ( ; i >= 0; --i) {
160 urb = priv->read_urbp[i];
161 if (urb) {
162 /* This urb was submitted successfully. So we have to
163 cancel it.
164 Unlinking the urb will invoke read_bulk_callback()
165 with an error status, so its transfer buffer will
166 be freed there */
167 if (usb_unlink_urb (urb) != -EINPROGRESS) {
168 /* comments in drivers/usb/core/urb.c say this
169 can only happen if the urb was never submitted,
170 or has completed already.
171 Either way we may have to free the transfer
172 buffer here. */
173 if (urb->transfer_buffer) {
174 kfree (urb->transfer_buffer);
175 urb->transfer_buffer = NULL;
178 usb_free_urb (urb);
182 out:
183 return result;
186 static void airprime_close(struct usb_serial_port *port, struct file * filp)
188 struct airprime_private *priv = usb_get_serial_port_data(port);
189 int i;
191 dbg("%s - port %d", __FUNCTION__, port->number);
193 /* killing the urb will invoke read_bulk_callback() with an error status,
194 so the transfer buffer will be freed there */
195 for (i = 0; i < NUM_READ_URBS; ++i) {
196 usb_kill_urb (priv->read_urbp[i]);
197 usb_free_urb (priv->read_urbp[i]);
200 /* free up private structure */
201 kfree (priv);
202 usb_set_serial_port_data(port, NULL);
205 static int airprime_write(struct usb_serial_port *port,
206 const unsigned char *buf, int count)
208 struct airprime_private *priv = usb_get_serial_port_data(port);
209 struct usb_serial *serial = port->serial;
210 struct urb *urb;
211 unsigned char *buffer;
212 unsigned long flags;
213 int status;
214 dbg("%s - port %d", __FUNCTION__, port->number);
216 spin_lock_irqsave(&priv->lock, flags);
217 if (priv->outstanding_urbs > NUM_WRITE_URBS) {
218 spin_unlock_irqrestore(&priv->lock, flags);
219 dbg("%s - write limit hit\n", __FUNCTION__);
220 return 0;
222 spin_unlock_irqrestore(&priv->lock, flags);
223 buffer = kmalloc(count, GFP_ATOMIC);
224 if (!buffer) {
225 dev_err(&port->dev, "out of memory\n");
226 return -ENOMEM;
228 urb = usb_alloc_urb(0, GFP_ATOMIC);
229 if (!urb) {
230 dev_err(&port->dev, "no more free urbs\n");
231 kfree (buffer);
232 return -ENOMEM;
234 memcpy (buffer, buf, count);
236 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, buffer);
238 usb_fill_bulk_urb(urb, serial->dev,
239 usb_sndbulkpipe(serial->dev,
240 port->bulk_out_endpointAddress),
241 buffer, count,
242 airprime_write_bulk_callback, port);
244 /* send it down the pipe */
245 status = usb_submit_urb(urb, GFP_ATOMIC);
246 if (status) {
247 dev_err(&port->dev,
248 "%s - usb_submit_urb(write bulk) failed with status = %d\n",
249 __FUNCTION__, status);
250 count = status;
251 kfree (buffer);
252 } else {
253 spin_lock_irqsave(&priv->lock, flags);
254 ++priv->outstanding_urbs;
255 spin_unlock_irqrestore(&priv->lock, flags);
257 /* we are done with this urb, so let the host driver
258 * really free it when it is finished with it */
259 usb_free_urb (urb);
260 return count;
263 static struct usb_driver airprime_driver = {
264 .name = "airprime",
265 .probe = usb_serial_probe,
266 .disconnect = usb_serial_disconnect,
267 .id_table = id_table,
268 .no_dynamic_id = 1,
271 static struct usb_serial_driver airprime_device = {
272 .driver = {
273 .owner = THIS_MODULE,
274 .name = "airprime",
276 .id_table = id_table,
277 .num_interrupt_in = NUM_DONT_CARE,
278 .num_bulk_in = NUM_DONT_CARE,
279 .num_bulk_out = NUM_DONT_CARE,
280 .open = airprime_open,
281 .close = airprime_close,
282 .write = airprime_write,
285 static int __init airprime_init(void)
287 int retval;
289 airprime_device.num_ports =
290 (endpoints > 0 && endpoints <= MAX_BULK_EPS) ? endpoints : NUM_BULK_EPS;
291 retval = usb_serial_register(&airprime_device);
292 if (retval)
293 return retval;
294 retval = usb_register(&airprime_driver);
295 if (retval)
296 usb_serial_deregister(&airprime_device);
297 return retval;
300 static void __exit airprime_exit(void)
302 dbg("%s", __FUNCTION__);
304 usb_deregister(&airprime_driver);
305 usb_serial_deregister(&airprime_device);
308 module_init(airprime_init);
309 module_exit(airprime_exit);
310 MODULE_LICENSE("GPL");
312 module_param(debug, bool, S_IRUGO | S_IWUSR);
313 MODULE_PARM_DESC(debug, "Debug enabled");
314 module_param(buffer_size, int, 0);
315 MODULE_PARM_DESC(buffer_size, "Size of the transfer buffers in bytes (default 4096)");
316 module_param(endpoints, int, 0);
317 MODULE_PARM_DESC(endpoints, "Number of bulk EPs to configure (default 3)");