blob: f156dba0300f77168f7774199e988074bd249e8a [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 */
Greg KH3b86b202005-04-25 21:46:29 -070021 { },
22};
23MODULE_DEVICE_TABLE(usb, id_table);
24
Andy Gay5dda1712006-07-03 18:43:01 -040025#define URB_TRANSFER_BUFFER_SIZE 4096
26#define NUM_READ_URBS 4
27#define NUM_WRITE_URBS 4
28#define NUM_BULK_EPS 3
29#define MAX_BULK_EPS 6
30
31/* if overridden by the user, then use their value for the size of the
32 * read and write urbs, and the number of endpoints */
33static int buffer_size = URB_TRANSFER_BUFFER_SIZE;
34static int endpoints = NUM_BULK_EPS;
35static int debug;
36struct airprime_private {
37 spinlock_t lock;
38 int outstanding_urbs;
39 int throttled;
40 struct urb *read_urbp[NUM_READ_URBS];
Martin Schillercf0cb1a2007-03-01 13:49:48 +010041
42 /* Settings for the port */
43 int rts_state; /* Handshaking pins (outputs) */
44 int dtr_state;
45 int cts_state; /* Handshaking pins (inputs) */
46 int dsr_state;
47 int dcd_state;
48 int ri_state;
Andy Gay5dda1712006-07-03 18:43:01 -040049};
50
Martin Schillercf0cb1a2007-03-01 13:49:48 +010051static int airprime_send_setup(struct usb_serial_port *port)
52{
53 struct usb_serial *serial = port->serial;
54 struct airprime_private *priv;
55
56 dbg("%s", __FUNCTION__);
57
58 if (port->number != 0)
59 return 0;
60
61 priv = usb_get_serial_port_data(port);
62
63 if (port->tty) {
64 int val = 0;
65 if (priv->dtr_state)
66 val |= 0x01;
67 if (priv->rts_state)
68 val |= 0x02;
69
70 return usb_control_msg(serial->dev,
71 usb_rcvctrlpipe(serial->dev, 0),
72 0x22,0x21,val,0,NULL,0,USB_CTRL_SET_TIMEOUT);
73 }
74
75 return 0;
76}
77
David Howells7d12e782006-10-05 14:55:46 +010078static void airprime_read_bulk_callback(struct urb *urb)
Andy Gay5dda1712006-07-03 18:43:01 -040079{
80 struct usb_serial_port *port = urb->context;
81 unsigned char *data = urb->transfer_buffer;
82 struct tty_struct *tty;
83 int result;
Greg Kroah-Hartmand3434cf2007-06-15 15:44:13 -070084 int status = urb->status;
Andy Gay5dda1712006-07-03 18:43:01 -040085
86 dbg("%s - port %d", __FUNCTION__, port->number);
87
Greg Kroah-Hartmand3434cf2007-06-15 15:44:13 -070088 if (status) {
Andy Gay5dda1712006-07-03 18:43:01 -040089 dbg("%s - nonzero read bulk status received: %d",
Greg Kroah-Hartmand3434cf2007-06-15 15:44:13 -070090 __FUNCTION__, 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);
Greg Kroah-Hartmand3434cf2007-06-15 15:44:13 -0700112 int status = urb->status;
Andy Gay5dda1712006-07-03 18:43:01 -0400113 unsigned long flags;
114
115 dbg("%s - port %d", __FUNCTION__, port->number);
116
117 /* free up the transfer buffer, as usb_free_urb() does not do this */
118 kfree (urb->transfer_buffer);
119
Greg Kroah-Hartmand3434cf2007-06-15 15:44:13 -0700120 if (status)
Andy Gay5dda1712006-07-03 18:43:01 -0400121 dbg("%s - nonzero write bulk status received: %d",
Greg Kroah-Hartmand3434cf2007-06-15 15:44:13 -0700122 __FUNCTION__, status);
Andy Gay5dda1712006-07-03 18:43:01 -0400123 spin_lock_irqsave(&priv->lock, flags);
124 --priv->outstanding_urbs;
125 spin_unlock_irqrestore(&priv->lock, flags);
126
127 usb_serial_port_softint(port);
128}
129
130static int airprime_open(struct usb_serial_port *port, struct file *filp)
131{
132 struct airprime_private *priv = usb_get_serial_port_data(port);
133 struct usb_serial *serial = port->serial;
134 struct urb *urb;
135 char *buffer = NULL;
136 int i;
137 int result = 0;
138
139 dbg("%s - port %d", __FUNCTION__, port->number);
140
141 /* initialize our private data structure if it isn't already created */
142 if (!priv) {
143 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
144 if (!priv) {
145 result = -ENOMEM;
146 goto out;
147 }
148 spin_lock_init(&priv->lock);
149 usb_set_serial_port_data(port, priv);
150 }
151
Martin Schillercf0cb1a2007-03-01 13:49:48 +0100152 /* Set some sane defaults */
153 priv->rts_state = 1;
154 priv->dtr_state = 1;
155
Andy Gay5dda1712006-07-03 18:43:01 -0400156 for (i = 0; i < NUM_READ_URBS; ++i) {
157 buffer = kmalloc(buffer_size, GFP_KERNEL);
158 if (!buffer) {
159 dev_err(&port->dev, "%s - out of memory.\n",
160 __FUNCTION__);
161 result = -ENOMEM;
162 goto errout;
163 }
164 urb = usb_alloc_urb(0, GFP_KERNEL);
165 if (!urb) {
Eric Sesterhenn0e185b72006-10-10 14:42:50 -0700166 kfree(buffer);
Andy Gay5dda1712006-07-03 18:43:01 -0400167 dev_err(&port->dev, "%s - no more urbs?\n",
168 __FUNCTION__);
169 result = -ENOMEM;
170 goto errout;
171 }
172 usb_fill_bulk_urb(urb, serial->dev,
173 usb_rcvbulkpipe(serial->dev,
174 port->bulk_out_endpointAddress),
175 buffer, buffer_size,
176 airprime_read_bulk_callback, port);
177 result = usb_submit_urb(urb, GFP_KERNEL);
178 if (result) {
Pete Zaitcevaf59cf42007-02-02 23:13:14 -0800179 usb_free_urb(urb);
180 kfree(buffer);
Andy Gay5dda1712006-07-03 18:43:01 -0400181 dev_err(&port->dev,
182 "%s - failed submitting read urb %d for port %d, error %d\n",
183 __FUNCTION__, i, port->number, result);
184 goto errout;
185 }
186 /* remember this urb so we can kill it when the port is closed */
187 priv->read_urbp[i] = urb;
188 }
Martin Schillercf0cb1a2007-03-01 13:49:48 +0100189
190 airprime_send_setup(port);
191
Andy Gay5dda1712006-07-03 18:43:01 -0400192 goto out;
193
194 errout:
195 /* some error happened, cancel any submitted urbs and clean up anything that
196 got allocated successfully */
197
Pete Zaitcevaf59cf42007-02-02 23:13:14 -0800198 while (i-- != 0) {
Andy Gay5dda1712006-07-03 18:43:01 -0400199 urb = priv->read_urbp[i];
Pete Zaitcevaf59cf42007-02-02 23:13:14 -0800200 buffer = urb->transfer_buffer;
201 usb_kill_urb (urb);
202 usb_free_urb (urb);
203 kfree (buffer);
Andy Gay5dda1712006-07-03 18:43:01 -0400204 }
205
206 out:
207 return result;
208}
209
210static void airprime_close(struct usb_serial_port *port, struct file * filp)
211{
212 struct airprime_private *priv = usb_get_serial_port_data(port);
213 int i;
214
215 dbg("%s - port %d", __FUNCTION__, port->number);
216
Martin Schillercf0cb1a2007-03-01 13:49:48 +0100217 priv->rts_state = 0;
218 priv->dtr_state = 0;
219
Oliver Neukum95bef012008-01-22 13:56:18 +0100220 mutex_lock(&port->serial->disc_mutex);
221 if (!port->serial->disconnected)
222 airprime_send_setup(port);
223 mutex_lock(&port->serial->disc_mutex);
Martin Schillercf0cb1a2007-03-01 13:49:48 +0100224
Andy Gay5dda1712006-07-03 18:43:01 -0400225 for (i = 0; i < NUM_READ_URBS; ++i) {
226 usb_kill_urb (priv->read_urbp[i]);
Pete Zaitcevaf59cf42007-02-02 23:13:14 -0800227 kfree (priv->read_urbp[i]->transfer_buffer);
Andy Gay5dda1712006-07-03 18:43:01 -0400228 usb_free_urb (priv->read_urbp[i]);
229 }
230
231 /* free up private structure */
232 kfree (priv);
233 usb_set_serial_port_data(port, NULL);
234}
235
236static int airprime_write(struct usb_serial_port *port,
237 const unsigned char *buf, int count)
238{
239 struct airprime_private *priv = usb_get_serial_port_data(port);
240 struct usb_serial *serial = port->serial;
241 struct urb *urb;
242 unsigned char *buffer;
243 unsigned long flags;
244 int status;
245 dbg("%s - port %d", __FUNCTION__, port->number);
246
247 spin_lock_irqsave(&priv->lock, flags);
248 if (priv->outstanding_urbs > NUM_WRITE_URBS) {
249 spin_unlock_irqrestore(&priv->lock, flags);
250 dbg("%s - write limit hit\n", __FUNCTION__);
251 return 0;
252 }
253 spin_unlock_irqrestore(&priv->lock, flags);
254 buffer = kmalloc(count, GFP_ATOMIC);
255 if (!buffer) {
256 dev_err(&port->dev, "out of memory\n");
257 return -ENOMEM;
258 }
259 urb = usb_alloc_urb(0, GFP_ATOMIC);
260 if (!urb) {
261 dev_err(&port->dev, "no more free urbs\n");
262 kfree (buffer);
263 return -ENOMEM;
264 }
265 memcpy (buffer, buf, count);
266
267 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, buffer);
268
269 usb_fill_bulk_urb(urb, serial->dev,
270 usb_sndbulkpipe(serial->dev,
271 port->bulk_out_endpointAddress),
272 buffer, count,
273 airprime_write_bulk_callback, port);
274
275 /* send it down the pipe */
276 status = usb_submit_urb(urb, GFP_ATOMIC);
277 if (status) {
278 dev_err(&port->dev,
279 "%s - usb_submit_urb(write bulk) failed with status = %d\n",
280 __FUNCTION__, status);
281 count = status;
282 kfree (buffer);
283 } else {
284 spin_lock_irqsave(&priv->lock, flags);
285 ++priv->outstanding_urbs;
286 spin_unlock_irqrestore(&priv->lock, flags);
287 }
288 /* we are done with this urb, so let the host driver
289 * really free it when it is finished with it */
290 usb_free_urb (urb);
291 return count;
292}
293
Greg KH3b86b202005-04-25 21:46:29 -0700294static struct usb_driver airprime_driver = {
Greg KH3b86b202005-04-25 21:46:29 -0700295 .name = "airprime",
296 .probe = usb_serial_probe,
297 .disconnect = usb_serial_disconnect,
298 .id_table = id_table,
Andy Gay5dda1712006-07-03 18:43:01 -0400299 .no_dynamic_id = 1,
Greg KH3b86b202005-04-25 21:46:29 -0700300};
301
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700302static struct usb_serial_driver airprime_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700303 .driver = {
304 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700305 .name = "airprime",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700306 },
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100307 .usb_driver = &airprime_driver,
Greg KH3b86b202005-04-25 21:46:29 -0700308 .id_table = id_table,
309 .num_interrupt_in = NUM_DONT_CARE,
310 .num_bulk_in = NUM_DONT_CARE,
311 .num_bulk_out = NUM_DONT_CARE,
Andy Gay5dda1712006-07-03 18:43:01 -0400312 .open = airprime_open,
313 .close = airprime_close,
314 .write = airprime_write,
Greg KH3b86b202005-04-25 21:46:29 -0700315};
316
317static int __init airprime_init(void)
318{
319 int retval;
320
Andy Gay5dda1712006-07-03 18:43:01 -0400321 airprime_device.num_ports =
322 (endpoints > 0 && endpoints <= MAX_BULK_EPS) ? endpoints : NUM_BULK_EPS;
Greg KH3b86b202005-04-25 21:46:29 -0700323 retval = usb_serial_register(&airprime_device);
324 if (retval)
325 return retval;
326 retval = usb_register(&airprime_driver);
327 if (retval)
328 usb_serial_deregister(&airprime_device);
329 return retval;
330}
331
332static void __exit airprime_exit(void)
333{
Andy Gay5dda1712006-07-03 18:43:01 -0400334 dbg("%s", __FUNCTION__);
335
Greg KH3b86b202005-04-25 21:46:29 -0700336 usb_deregister(&airprime_driver);
337 usb_serial_deregister(&airprime_device);
338}
339
340module_init(airprime_init);
341module_exit(airprime_exit);
342MODULE_LICENSE("GPL");
Andy Gay5dda1712006-07-03 18:43:01 -0400343
344module_param(debug, bool, S_IRUGO | S_IWUSR);
345MODULE_PARM_DESC(debug, "Debug enabled");
346module_param(buffer_size, int, 0);
347MODULE_PARM_DESC(buffer_size, "Size of the transfer buffers in bytes (default 4096)");
348module_param(endpoints, int, 0);
349MODULE_PARM_DESC(endpoints, "Number of bulk EPs to configure (default 3)");