blob: cff6fd190a28273869c4f9b1203e75ccee1ccc85 [file] [log] [blame]
Greg KH3b86b202005-04-25 21:46:29 -07001/*
2 * AirPrime CDMA Wireless Serial USB driver
3 *
Andy Gay5dda1712006-07-03 18:43:01 -04004 * Copyright (C) 2005-2006 Greg Kroah-Hartman <gregkh@suse.de>
Greg KH3b86b202005-04-25 21:46:29 -07005 *
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 */
10
11#include <linux/kernel.h>
12#include <linux/init.h>
13#include <linux/tty.h>
Andy Gay5dda1712006-07-03 18:43:01 -040014#include <linux/tty_flip.h>
Greg KH3b86b202005-04-25 21:46:29 -070015#include <linux/module.h>
16#include <linux/usb.h>
Greg Kroah-Hartmana9698882006-07-11 21:22:58 -070017#include <linux/usb/serial.h>
Greg KH3b86b202005-04-25 21:46:29 -070018
19static struct usb_device_id id_table [] = {
Timothy Sipples34ab86e2006-06-16 20:42:59 +090020 { USB_DEVICE(0x0c88, 0x17da) }, /* Kyocera Wireless KPC650/Passport */
Eagle Jones87f28bd2006-11-24 16:40:04 -080021 { USB_DEVICE(0x413c, 0x8115) }, /* Dell Wireless HSDPA 5500 */
Greg KH3b86b202005-04-25 21:46:29 -070022 { },
23};
24MODULE_DEVICE_TABLE(usb, id_table);
25
Andy Gay5dda1712006-07-03 18:43:01 -040026#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
31
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 */
34static int buffer_size = URB_TRANSFER_BUFFER_SIZE;
35static int endpoints = NUM_BULK_EPS;
36static int debug;
37struct airprime_private {
38 spinlock_t lock;
39 int outstanding_urbs;
40 int throttled;
41 struct urb *read_urbp[NUM_READ_URBS];
Martin Schillercf0cb1a2007-03-01 13:49:48 +010042
43 /* Settings for the port */
44 int rts_state; /* Handshaking pins (outputs) */
45 int dtr_state;
46 int cts_state; /* Handshaking pins (inputs) */
47 int dsr_state;
48 int dcd_state;
49 int ri_state;
Andy Gay5dda1712006-07-03 18:43:01 -040050};
51
Martin Schillercf0cb1a2007-03-01 13:49:48 +010052static int airprime_send_setup(struct usb_serial_port *port)
53{
54 struct usb_serial *serial = port->serial;
55 struct airprime_private *priv;
56
57 dbg("%s", __FUNCTION__);
58
59 if (port->number != 0)
60 return 0;
61
62 priv = usb_get_serial_port_data(port);
63
64 if (port->tty) {
65 int val = 0;
66 if (priv->dtr_state)
67 val |= 0x01;
68 if (priv->rts_state)
69 val |= 0x02;
70
71 return usb_control_msg(serial->dev,
72 usb_rcvctrlpipe(serial->dev, 0),
73 0x22,0x21,val,0,NULL,0,USB_CTRL_SET_TIMEOUT);
74 }
75
76 return 0;
77}
78
David Howells7d12e782006-10-05 14:55:46 +010079static void airprime_read_bulk_callback(struct urb *urb)
Andy Gay5dda1712006-07-03 18:43:01 -040080{
81 struct usb_serial_port *port = urb->context;
82 unsigned char *data = urb->transfer_buffer;
83 struct tty_struct *tty;
84 int result;
Greg Kroah-Hartmand3434cf2007-06-15 15:44:13 -070085 int status = urb->status;
Andy Gay5dda1712006-07-03 18:43:01 -040086
87 dbg("%s - port %d", __FUNCTION__, port->number);
88
Greg Kroah-Hartmand3434cf2007-06-15 15:44:13 -070089 if (status) {
Andy Gay5dda1712006-07-03 18:43:01 -040090 dbg("%s - nonzero read bulk status received: %d",
Greg Kroah-Hartmand3434cf2007-06-15 15:44:13 -070091 __FUNCTION__, status);
Andy Gay5dda1712006-07-03 18:43:01 -040092 return;
93 }
94 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
95
96 tty = port->tty;
97 if (tty && urb->actual_length) {
98 tty_insert_flip_string (tty, data, urb->actual_length);
99 tty_flip_buffer_push (tty);
100 }
101
102 result = usb_submit_urb (urb, GFP_ATOMIC);
103 if (result)
104 dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n",
105 __FUNCTION__, result);
106 return;
107}
108
David Howells7d12e782006-10-05 14:55:46 +0100109static void airprime_write_bulk_callback(struct urb *urb)
Andy Gay5dda1712006-07-03 18:43:01 -0400110{
111 struct usb_serial_port *port = urb->context;
112 struct airprime_private *priv = usb_get_serial_port_data(port);
Greg Kroah-Hartmand3434cf2007-06-15 15:44:13 -0700113 int status = urb->status;
Andy Gay5dda1712006-07-03 18:43:01 -0400114 unsigned long flags;
115
116 dbg("%s - port %d", __FUNCTION__, port->number);
117
118 /* free up the transfer buffer, as usb_free_urb() does not do this */
119 kfree (urb->transfer_buffer);
120
Greg Kroah-Hartmand3434cf2007-06-15 15:44:13 -0700121 if (status)
Andy Gay5dda1712006-07-03 18:43:01 -0400122 dbg("%s - nonzero write bulk status received: %d",
Greg Kroah-Hartmand3434cf2007-06-15 15:44:13 -0700123 __FUNCTION__, status);
Andy Gay5dda1712006-07-03 18:43:01 -0400124 spin_lock_irqsave(&priv->lock, flags);
125 --priv->outstanding_urbs;
126 spin_unlock_irqrestore(&priv->lock, flags);
127
128 usb_serial_port_softint(port);
129}
130
131static int airprime_open(struct usb_serial_port *port, struct file *filp)
132{
133 struct airprime_private *priv = usb_get_serial_port_data(port);
134 struct usb_serial *serial = port->serial;
135 struct urb *urb;
136 char *buffer = NULL;
137 int i;
138 int result = 0;
139
140 dbg("%s - port %d", __FUNCTION__, port->number);
141
142 /* initialize our private data structure if it isn't already created */
143 if (!priv) {
144 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
145 if (!priv) {
146 result = -ENOMEM;
147 goto out;
148 }
149 spin_lock_init(&priv->lock);
150 usb_set_serial_port_data(port, priv);
151 }
152
Martin Schillercf0cb1a2007-03-01 13:49:48 +0100153 /* Set some sane defaults */
154 priv->rts_state = 1;
155 priv->dtr_state = 1;
156
Andy Gay5dda1712006-07-03 18:43:01 -0400157 for (i = 0; i < NUM_READ_URBS; ++i) {
158 buffer = kmalloc(buffer_size, GFP_KERNEL);
159 if (!buffer) {
160 dev_err(&port->dev, "%s - out of memory.\n",
161 __FUNCTION__);
162 result = -ENOMEM;
163 goto errout;
164 }
165 urb = usb_alloc_urb(0, GFP_KERNEL);
166 if (!urb) {
Eric Sesterhenn0e185b72006-10-10 14:42:50 -0700167 kfree(buffer);
Andy Gay5dda1712006-07-03 18:43:01 -0400168 dev_err(&port->dev, "%s - no more urbs?\n",
169 __FUNCTION__);
170 result = -ENOMEM;
171 goto errout;
172 }
173 usb_fill_bulk_urb(urb, serial->dev,
174 usb_rcvbulkpipe(serial->dev,
175 port->bulk_out_endpointAddress),
176 buffer, buffer_size,
177 airprime_read_bulk_callback, port);
178 result = usb_submit_urb(urb, GFP_KERNEL);
179 if (result) {
Pete Zaitcevaf59cf42007-02-02 23:13:14 -0800180 usb_free_urb(urb);
181 kfree(buffer);
Andy Gay5dda1712006-07-03 18:43:01 -0400182 dev_err(&port->dev,
183 "%s - failed submitting read urb %d for port %d, error %d\n",
184 __FUNCTION__, i, port->number, result);
185 goto errout;
186 }
187 /* remember this urb so we can kill it when the port is closed */
188 priv->read_urbp[i] = urb;
189 }
Martin Schillercf0cb1a2007-03-01 13:49:48 +0100190
191 airprime_send_setup(port);
192
Andy Gay5dda1712006-07-03 18:43:01 -0400193 goto out;
194
195 errout:
196 /* some error happened, cancel any submitted urbs and clean up anything that
197 got allocated successfully */
198
Pete Zaitcevaf59cf42007-02-02 23:13:14 -0800199 while (i-- != 0) {
Andy Gay5dda1712006-07-03 18:43:01 -0400200 urb = priv->read_urbp[i];
Pete Zaitcevaf59cf42007-02-02 23:13:14 -0800201 buffer = urb->transfer_buffer;
202 usb_kill_urb (urb);
203 usb_free_urb (urb);
204 kfree (buffer);
Andy Gay5dda1712006-07-03 18:43:01 -0400205 }
206
207 out:
208 return result;
209}
210
211static void airprime_close(struct usb_serial_port *port, struct file * filp)
212{
213 struct airprime_private *priv = usb_get_serial_port_data(port);
214 int i;
215
216 dbg("%s - port %d", __FUNCTION__, port->number);
217
Martin Schillercf0cb1a2007-03-01 13:49:48 +0100218 priv->rts_state = 0;
219 priv->dtr_state = 0;
220
221 airprime_send_setup(port);
222
Andy Gay5dda1712006-07-03 18:43:01 -0400223 for (i = 0; i < NUM_READ_URBS; ++i) {
224 usb_kill_urb (priv->read_urbp[i]);
Pete Zaitcevaf59cf42007-02-02 23:13:14 -0800225 kfree (priv->read_urbp[i]->transfer_buffer);
Andy Gay5dda1712006-07-03 18:43:01 -0400226 usb_free_urb (priv->read_urbp[i]);
227 }
228
229 /* free up private structure */
230 kfree (priv);
231 usb_set_serial_port_data(port, NULL);
232}
233
234static int airprime_write(struct usb_serial_port *port,
235 const unsigned char *buf, int count)
236{
237 struct airprime_private *priv = usb_get_serial_port_data(port);
238 struct usb_serial *serial = port->serial;
239 struct urb *urb;
240 unsigned char *buffer;
241 unsigned long flags;
242 int status;
243 dbg("%s - port %d", __FUNCTION__, port->number);
244
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__);
249 return 0;
250 }
251 spin_unlock_irqrestore(&priv->lock, flags);
252 buffer = kmalloc(count, GFP_ATOMIC);
253 if (!buffer) {
254 dev_err(&port->dev, "out of memory\n");
255 return -ENOMEM;
256 }
257 urb = usb_alloc_urb(0, GFP_ATOMIC);
258 if (!urb) {
259 dev_err(&port->dev, "no more free urbs\n");
260 kfree (buffer);
261 return -ENOMEM;
262 }
263 memcpy (buffer, buf, count);
264
265 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, buffer);
266
267 usb_fill_bulk_urb(urb, serial->dev,
268 usb_sndbulkpipe(serial->dev,
269 port->bulk_out_endpointAddress),
270 buffer, count,
271 airprime_write_bulk_callback, port);
272
273 /* send it down the pipe */
274 status = usb_submit_urb(urb, GFP_ATOMIC);
275 if (status) {
276 dev_err(&port->dev,
277 "%s - usb_submit_urb(write bulk) failed with status = %d\n",
278 __FUNCTION__, status);
279 count = status;
280 kfree (buffer);
281 } else {
282 spin_lock_irqsave(&priv->lock, flags);
283 ++priv->outstanding_urbs;
284 spin_unlock_irqrestore(&priv->lock, flags);
285 }
286 /* we are done with this urb, so let the host driver
287 * really free it when it is finished with it */
288 usb_free_urb (urb);
289 return count;
290}
291
Greg KH3b86b202005-04-25 21:46:29 -0700292static struct usb_driver airprime_driver = {
Greg KH3b86b202005-04-25 21:46:29 -0700293 .name = "airprime",
294 .probe = usb_serial_probe,
295 .disconnect = usb_serial_disconnect,
296 .id_table = id_table,
Andy Gay5dda1712006-07-03 18:43:01 -0400297 .no_dynamic_id = 1,
Greg KH3b86b202005-04-25 21:46:29 -0700298};
299
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700300static struct usb_serial_driver airprime_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700301 .driver = {
302 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700303 .name = "airprime",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700304 },
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100305 .usb_driver = &airprime_driver,
Greg KH3b86b202005-04-25 21:46:29 -0700306 .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,
Andy Gay5dda1712006-07-03 18:43:01 -0400310 .open = airprime_open,
311 .close = airprime_close,
312 .write = airprime_write,
Greg KH3b86b202005-04-25 21:46:29 -0700313};
314
315static int __init airprime_init(void)
316{
317 int retval;
318
Andy Gay5dda1712006-07-03 18:43:01 -0400319 airprime_device.num_ports =
320 (endpoints > 0 && endpoints <= MAX_BULK_EPS) ? endpoints : NUM_BULK_EPS;
Greg KH3b86b202005-04-25 21:46:29 -0700321 retval = usb_serial_register(&airprime_device);
322 if (retval)
323 return retval;
324 retval = usb_register(&airprime_driver);
325 if (retval)
326 usb_serial_deregister(&airprime_device);
327 return retval;
328}
329
330static void __exit airprime_exit(void)
331{
Andy Gay5dda1712006-07-03 18:43:01 -0400332 dbg("%s", __FUNCTION__);
333
Greg KH3b86b202005-04-25 21:46:29 -0700334 usb_deregister(&airprime_driver);
335 usb_serial_deregister(&airprime_device);
336}
337
338module_init(airprime_init);
339module_exit(airprime_exit);
340MODULE_LICENSE("GPL");
Andy Gay5dda1712006-07-03 18:43:01 -0400341
342module_param(debug, bool, S_IRUGO | S_IWUSR);
343MODULE_PARM_DESC(debug, "Debug enabled");
344module_param(buffer_size, int, 0);
345MODULE_PARM_DESC(buffer_size, "Size of the transfer buffers in bytes (default 4096)");
346module_param(endpoints, int, 0);
347MODULE_PARM_DESC(endpoints, "Number of bulk EPs to configure (default 3)");