blob: 39a49836259405605eb041f847e248d63a1728b8 [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;
85
86 dbg("%s - port %d", __FUNCTION__, port->number);
87
88 if (urb->status) {
89 dbg("%s - nonzero read bulk status received: %d",
90 __FUNCTION__, urb->status);
Andy Gay5dda1712006-07-03 18:43:01 -040091 return;
92 }
93 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
94
95 tty = port->tty;
96 if (tty && urb->actual_length) {
97 tty_insert_flip_string (tty, data, urb->actual_length);
98 tty_flip_buffer_push (tty);
99 }
100
101 result = usb_submit_urb (urb, GFP_ATOMIC);
102 if (result)
103 dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n",
104 __FUNCTION__, result);
105 return;
106}
107
David Howells7d12e782006-10-05 14:55:46 +0100108static void airprime_write_bulk_callback(struct urb *urb)
Andy Gay5dda1712006-07-03 18:43:01 -0400109{
110 struct usb_serial_port *port = urb->context;
111 struct airprime_private *priv = usb_get_serial_port_data(port);
112 unsigned long flags;
113
114 dbg("%s - port %d", __FUNCTION__, port->number);
115
116 /* free up the transfer buffer, as usb_free_urb() does not do this */
117 kfree (urb->transfer_buffer);
118
119 if (urb->status)
120 dbg("%s - nonzero write bulk status received: %d",
121 __FUNCTION__, urb->status);
122 spin_lock_irqsave(&priv->lock, flags);
123 --priv->outstanding_urbs;
124 spin_unlock_irqrestore(&priv->lock, flags);
125
126 usb_serial_port_softint(port);
127}
128
129static int airprime_open(struct usb_serial_port *port, struct file *filp)
130{
131 struct airprime_private *priv = usb_get_serial_port_data(port);
132 struct usb_serial *serial = port->serial;
133 struct urb *urb;
134 char *buffer = NULL;
135 int i;
136 int result = 0;
137
138 dbg("%s - port %d", __FUNCTION__, port->number);
139
140 /* initialize our private data structure if it isn't already created */
141 if (!priv) {
142 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
143 if (!priv) {
144 result = -ENOMEM;
145 goto out;
146 }
147 spin_lock_init(&priv->lock);
148 usb_set_serial_port_data(port, priv);
149 }
150
Martin Schillercf0cb1a2007-03-01 13:49:48 +0100151 /* Set some sane defaults */
152 priv->rts_state = 1;
153 priv->dtr_state = 1;
154
Andy Gay5dda1712006-07-03 18:43:01 -0400155 for (i = 0; i < NUM_READ_URBS; ++i) {
156 buffer = kmalloc(buffer_size, GFP_KERNEL);
157 if (!buffer) {
158 dev_err(&port->dev, "%s - out of memory.\n",
159 __FUNCTION__);
160 result = -ENOMEM;
161 goto errout;
162 }
163 urb = usb_alloc_urb(0, GFP_KERNEL);
164 if (!urb) {
Eric Sesterhenn0e185b72006-10-10 14:42:50 -0700165 kfree(buffer);
Andy Gay5dda1712006-07-03 18:43:01 -0400166 dev_err(&port->dev, "%s - no more urbs?\n",
167 __FUNCTION__);
168 result = -ENOMEM;
169 goto errout;
170 }
171 usb_fill_bulk_urb(urb, serial->dev,
172 usb_rcvbulkpipe(serial->dev,
173 port->bulk_out_endpointAddress),
174 buffer, buffer_size,
175 airprime_read_bulk_callback, port);
176 result = usb_submit_urb(urb, GFP_KERNEL);
177 if (result) {
Pete Zaitcevaf59cf42007-02-02 23:13:14 -0800178 usb_free_urb(urb);
179 kfree(buffer);
Andy Gay5dda1712006-07-03 18:43:01 -0400180 dev_err(&port->dev,
181 "%s - failed submitting read urb %d for port %d, error %d\n",
182 __FUNCTION__, i, port->number, result);
183 goto errout;
184 }
185 /* remember this urb so we can kill it when the port is closed */
186 priv->read_urbp[i] = urb;
187 }
Martin Schillercf0cb1a2007-03-01 13:49:48 +0100188
189 airprime_send_setup(port);
190
Andy Gay5dda1712006-07-03 18:43:01 -0400191 goto out;
192
193 errout:
194 /* some error happened, cancel any submitted urbs and clean up anything that
195 got allocated successfully */
196
Pete Zaitcevaf59cf42007-02-02 23:13:14 -0800197 while (i-- != 0) {
Andy Gay5dda1712006-07-03 18:43:01 -0400198 urb = priv->read_urbp[i];
Pete Zaitcevaf59cf42007-02-02 23:13:14 -0800199 buffer = urb->transfer_buffer;
200 usb_kill_urb (urb);
201 usb_free_urb (urb);
202 kfree (buffer);
Andy Gay5dda1712006-07-03 18:43:01 -0400203 }
204
205 out:
206 return result;
207}
208
209static void airprime_close(struct usb_serial_port *port, struct file * filp)
210{
211 struct airprime_private *priv = usb_get_serial_port_data(port);
212 int i;
213
214 dbg("%s - port %d", __FUNCTION__, port->number);
215
Martin Schillercf0cb1a2007-03-01 13:49:48 +0100216 priv->rts_state = 0;
217 priv->dtr_state = 0;
218
219 airprime_send_setup(port);
220
Andy Gay5dda1712006-07-03 18:43:01 -0400221 for (i = 0; i < NUM_READ_URBS; ++i) {
222 usb_kill_urb (priv->read_urbp[i]);
Pete Zaitcevaf59cf42007-02-02 23:13:14 -0800223 kfree (priv->read_urbp[i]->transfer_buffer);
Andy Gay5dda1712006-07-03 18:43:01 -0400224 usb_free_urb (priv->read_urbp[i]);
225 }
226
227 /* free up private structure */
228 kfree (priv);
229 usb_set_serial_port_data(port, NULL);
230}
231
232static int airprime_write(struct usb_serial_port *port,
233 const unsigned char *buf, int count)
234{
235 struct airprime_private *priv = usb_get_serial_port_data(port);
236 struct usb_serial *serial = port->serial;
237 struct urb *urb;
238 unsigned char *buffer;
239 unsigned long flags;
240 int status;
241 dbg("%s - port %d", __FUNCTION__, port->number);
242
243 spin_lock_irqsave(&priv->lock, flags);
244 if (priv->outstanding_urbs > NUM_WRITE_URBS) {
245 spin_unlock_irqrestore(&priv->lock, flags);
246 dbg("%s - write limit hit\n", __FUNCTION__);
247 return 0;
248 }
249 spin_unlock_irqrestore(&priv->lock, flags);
250 buffer = kmalloc(count, GFP_ATOMIC);
251 if (!buffer) {
252 dev_err(&port->dev, "out of memory\n");
253 return -ENOMEM;
254 }
255 urb = usb_alloc_urb(0, GFP_ATOMIC);
256 if (!urb) {
257 dev_err(&port->dev, "no more free urbs\n");
258 kfree (buffer);
259 return -ENOMEM;
260 }
261 memcpy (buffer, buf, count);
262
263 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, buffer);
264
265 usb_fill_bulk_urb(urb, serial->dev,
266 usb_sndbulkpipe(serial->dev,
267 port->bulk_out_endpointAddress),
268 buffer, count,
269 airprime_write_bulk_callback, port);
270
271 /* send it down the pipe */
272 status = usb_submit_urb(urb, GFP_ATOMIC);
273 if (status) {
274 dev_err(&port->dev,
275 "%s - usb_submit_urb(write bulk) failed with status = %d\n",
276 __FUNCTION__, status);
277 count = status;
278 kfree (buffer);
279 } else {
280 spin_lock_irqsave(&priv->lock, flags);
281 ++priv->outstanding_urbs;
282 spin_unlock_irqrestore(&priv->lock, flags);
283 }
284 /* we are done with this urb, so let the host driver
285 * really free it when it is finished with it */
286 usb_free_urb (urb);
287 return count;
288}
289
Greg KH3b86b202005-04-25 21:46:29 -0700290static struct usb_driver airprime_driver = {
Greg KH3b86b202005-04-25 21:46:29 -0700291 .name = "airprime",
292 .probe = usb_serial_probe,
293 .disconnect = usb_serial_disconnect,
294 .id_table = id_table,
Andy Gay5dda1712006-07-03 18:43:01 -0400295 .no_dynamic_id = 1,
Greg KH3b86b202005-04-25 21:46:29 -0700296};
297
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700298static struct usb_serial_driver airprime_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700299 .driver = {
300 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700301 .name = "airprime",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700302 },
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100303 .usb_driver = &airprime_driver,
Greg KH3b86b202005-04-25 21:46:29 -0700304 .id_table = id_table,
305 .num_interrupt_in = NUM_DONT_CARE,
306 .num_bulk_in = NUM_DONT_CARE,
307 .num_bulk_out = NUM_DONT_CARE,
Andy Gay5dda1712006-07-03 18:43:01 -0400308 .open = airprime_open,
309 .close = airprime_close,
310 .write = airprime_write,
Greg KH3b86b202005-04-25 21:46:29 -0700311};
312
313static int __init airprime_init(void)
314{
315 int retval;
316
Andy Gay5dda1712006-07-03 18:43:01 -0400317 airprime_device.num_ports =
318 (endpoints > 0 && endpoints <= MAX_BULK_EPS) ? endpoints : NUM_BULK_EPS;
Greg KH3b86b202005-04-25 21:46:29 -0700319 retval = usb_serial_register(&airprime_device);
320 if (retval)
321 return retval;
322 retval = usb_register(&airprime_driver);
323 if (retval)
324 usb_serial_deregister(&airprime_device);
325 return retval;
326}
327
328static void __exit airprime_exit(void)
329{
Andy Gay5dda1712006-07-03 18:43:01 -0400330 dbg("%s", __FUNCTION__);
331
Greg KH3b86b202005-04-25 21:46:29 -0700332 usb_deregister(&airprime_driver);
333 usb_serial_deregister(&airprime_device);
334}
335
336module_init(airprime_init);
337module_exit(airprime_exit);
338MODULE_LICENSE("GPL");
Andy Gay5dda1712006-07-03 18:43:01 -0400339
340module_param(debug, bool, S_IRUGO | S_IWUSR);
341MODULE_PARM_DESC(debug, "Debug enabled");
342module_param(buffer_size, int, 0);
343MODULE_PARM_DESC(buffer_size, "Size of the transfer buffers in bytes (default 4096)");
344module_param(endpoints, int, 0);
345MODULE_PARM_DESC(endpoints, "Number of bulk EPs to configure (default 3)");