blob: 0798c14ce787767718e216b000cc8e6aef10c5ed [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
Harvey Harrison441b62c2008-03-03 16:08:34 -080056 dbg("%s", __func__);
Martin Schillercf0cb1a2007-03-01 13:49:48 +010057
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,
Alan Coxc4d0f8c2008-04-29 14:35:39 +010071 usb_rcvctrlpipe(serial->dev, 0),
72 0x22, 0x21, val, 0, NULL, 0,
73 USB_CTRL_SET_TIMEOUT);
Martin Schillercf0cb1a2007-03-01 13:49:48 +010074 }
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
Harvey Harrison441b62c2008-03-03 16:08:34 -080087 dbg("%s - port %d", __func__, port->number);
Andy Gay5dda1712006-07-03 18:43:01 -040088
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",
Harvey Harrison441b62c2008-03-03 16:08:34 -080091 __func__, status);
Andy Gay5dda1712006-07-03 18:43:01 -040092 return;
93 }
Alan Coxc4d0f8c2008-04-29 14:35:39 +010094 usb_serial_debug_data(debug, &port->dev, __func__,
95 urb->actual_length, data);
Andy Gay5dda1712006-07-03 18:43:01 -040096
97 tty = port->tty;
98 if (tty && urb->actual_length) {
Alan Coxc4d0f8c2008-04-29 14:35:39 +010099 tty_insert_flip_string(tty, data, urb->actual_length);
100 tty_flip_buffer_push(tty);
Andy Gay5dda1712006-07-03 18:43:01 -0400101 }
102
Alan Coxc4d0f8c2008-04-29 14:35:39 +0100103 result = usb_submit_urb(urb, GFP_ATOMIC);
Andy Gay5dda1712006-07-03 18:43:01 -0400104 if (result)
Alan Coxc4d0f8c2008-04-29 14:35:39 +0100105 dev_err(&port->dev,
106 "%s - failed resubmitting read urb, error %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800107 __func__, result);
Andy Gay5dda1712006-07-03 18:43:01 -0400108 return;
109}
110
David Howells7d12e782006-10-05 14:55:46 +0100111static void airprime_write_bulk_callback(struct urb *urb)
Andy Gay5dda1712006-07-03 18:43:01 -0400112{
113 struct usb_serial_port *port = urb->context;
114 struct airprime_private *priv = usb_get_serial_port_data(port);
Greg Kroah-Hartmand3434cf2007-06-15 15:44:13 -0700115 int status = urb->status;
Andy Gay5dda1712006-07-03 18:43:01 -0400116 unsigned long flags;
117
Harvey Harrison441b62c2008-03-03 16:08:34 -0800118 dbg("%s - port %d", __func__, port->number);
Andy Gay5dda1712006-07-03 18:43:01 -0400119
120 /* free up the transfer buffer, as usb_free_urb() does not do this */
Alan Coxc4d0f8c2008-04-29 14:35:39 +0100121 kfree(urb->transfer_buffer);
Andy Gay5dda1712006-07-03 18:43:01 -0400122
Greg Kroah-Hartmand3434cf2007-06-15 15:44:13 -0700123 if (status)
Andy Gay5dda1712006-07-03 18:43:01 -0400124 dbg("%s - nonzero write bulk status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800125 __func__, status);
Andy Gay5dda1712006-07-03 18:43:01 -0400126 spin_lock_irqsave(&priv->lock, flags);
127 --priv->outstanding_urbs;
128 spin_unlock_irqrestore(&priv->lock, flags);
129
130 usb_serial_port_softint(port);
131}
132
133static int airprime_open(struct usb_serial_port *port, struct file *filp)
134{
135 struct airprime_private *priv = usb_get_serial_port_data(port);
136 struct usb_serial *serial = port->serial;
137 struct urb *urb;
138 char *buffer = NULL;
139 int i;
140 int result = 0;
141
Harvey Harrison441b62c2008-03-03 16:08:34 -0800142 dbg("%s - port %d", __func__, port->number);
Andy Gay5dda1712006-07-03 18:43:01 -0400143
144 /* initialize our private data structure if it isn't already created */
145 if (!priv) {
146 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
147 if (!priv) {
148 result = -ENOMEM;
149 goto out;
150 }
151 spin_lock_init(&priv->lock);
152 usb_set_serial_port_data(port, priv);
153 }
154
Martin Schillercf0cb1a2007-03-01 13:49:48 +0100155 /* Set some sane defaults */
156 priv->rts_state = 1;
157 priv->dtr_state = 1;
158
Andy Gay5dda1712006-07-03 18:43:01 -0400159 for (i = 0; i < NUM_READ_URBS; ++i) {
160 buffer = kmalloc(buffer_size, GFP_KERNEL);
161 if (!buffer) {
162 dev_err(&port->dev, "%s - out of memory.\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800163 __func__);
Andy Gay5dda1712006-07-03 18:43:01 -0400164 result = -ENOMEM;
165 goto errout;
166 }
167 urb = usb_alloc_urb(0, GFP_KERNEL);
168 if (!urb) {
Eric Sesterhenn0e185b72006-10-10 14:42:50 -0700169 kfree(buffer);
Andy Gay5dda1712006-07-03 18:43:01 -0400170 dev_err(&port->dev, "%s - no more urbs?\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800171 __func__);
Andy Gay5dda1712006-07-03 18:43:01 -0400172 result = -ENOMEM;
173 goto errout;
174 }
175 usb_fill_bulk_urb(urb, serial->dev,
176 usb_rcvbulkpipe(serial->dev,
Alan Coxc4d0f8c2008-04-29 14:35:39 +0100177 port->bulk_out_endpointAddress),
Andy Gay5dda1712006-07-03 18:43:01 -0400178 buffer, buffer_size,
179 airprime_read_bulk_callback, port);
180 result = usb_submit_urb(urb, GFP_KERNEL);
181 if (result) {
Pete Zaitcevaf59cf42007-02-02 23:13:14 -0800182 usb_free_urb(urb);
183 kfree(buffer);
Andy Gay5dda1712006-07-03 18:43:01 -0400184 dev_err(&port->dev,
185 "%s - failed submitting read urb %d for port %d, error %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800186 __func__, i, port->number, result);
Andy Gay5dda1712006-07-03 18:43:01 -0400187 goto errout;
188 }
Alan Coxc4d0f8c2008-04-29 14:35:39 +0100189 /* remember this urb so we can kill it when the
190 port is closed */
Andy Gay5dda1712006-07-03 18:43:01 -0400191 priv->read_urbp[i] = urb;
192 }
Martin Schillercf0cb1a2007-03-01 13:49:48 +0100193
194 airprime_send_setup(port);
195
Andy Gay5dda1712006-07-03 18:43:01 -0400196 goto out;
197
198 errout:
Alan Coxc4d0f8c2008-04-29 14:35:39 +0100199 /* some error happened, cancel any submitted urbs and clean up
200 anything that got allocated successfully */
Andy Gay5dda1712006-07-03 18:43:01 -0400201
Pete Zaitcevaf59cf42007-02-02 23:13:14 -0800202 while (i-- != 0) {
Andy Gay5dda1712006-07-03 18:43:01 -0400203 urb = priv->read_urbp[i];
Pete Zaitcevaf59cf42007-02-02 23:13:14 -0800204 buffer = urb->transfer_buffer;
Alan Coxc4d0f8c2008-04-29 14:35:39 +0100205 usb_kill_urb(urb);
206 usb_free_urb(urb);
207 kfree(buffer);
Andy Gay5dda1712006-07-03 18:43:01 -0400208 }
209
210 out:
211 return result;
212}
213
Alan Coxc4d0f8c2008-04-29 14:35:39 +0100214static void airprime_close(struct usb_serial_port *port, struct file *filp)
Andy Gay5dda1712006-07-03 18:43:01 -0400215{
216 struct airprime_private *priv = usb_get_serial_port_data(port);
217 int i;
218
Harvey Harrison441b62c2008-03-03 16:08:34 -0800219 dbg("%s - port %d", __func__, port->number);
Andy Gay5dda1712006-07-03 18:43:01 -0400220
Martin Schillercf0cb1a2007-03-01 13:49:48 +0100221 priv->rts_state = 0;
222 priv->dtr_state = 0;
223
Oliver Neukum95bef012008-01-22 13:56:18 +0100224 mutex_lock(&port->serial->disc_mutex);
225 if (!port->serial->disconnected)
226 airprime_send_setup(port);
Leonardo Chiquitto21ae1dd2008-04-22 16:02:03 -0300227 mutex_unlock(&port->serial->disc_mutex);
Martin Schillercf0cb1a2007-03-01 13:49:48 +0100228
Andy Gay5dda1712006-07-03 18:43:01 -0400229 for (i = 0; i < NUM_READ_URBS; ++i) {
Alan Coxc4d0f8c2008-04-29 14:35:39 +0100230 usb_kill_urb(priv->read_urbp[i]);
231 kfree(priv->read_urbp[i]->transfer_buffer);
232 usb_free_urb(priv->read_urbp[i]);
Andy Gay5dda1712006-07-03 18:43:01 -0400233 }
234
235 /* free up private structure */
Alan Coxc4d0f8c2008-04-29 14:35:39 +0100236 kfree(priv);
Andy Gay5dda1712006-07-03 18:43:01 -0400237 usb_set_serial_port_data(port, NULL);
238}
239
240static int airprime_write(struct usb_serial_port *port,
241 const unsigned char *buf, int count)
242{
243 struct airprime_private *priv = usb_get_serial_port_data(port);
244 struct usb_serial *serial = port->serial;
245 struct urb *urb;
246 unsigned char *buffer;
247 unsigned long flags;
248 int status;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800249 dbg("%s - port %d", __func__, port->number);
Andy Gay5dda1712006-07-03 18:43:01 -0400250
251 spin_lock_irqsave(&priv->lock, flags);
252 if (priv->outstanding_urbs > NUM_WRITE_URBS) {
253 spin_unlock_irqrestore(&priv->lock, flags);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800254 dbg("%s - write limit hit\n", __func__);
Andy Gay5dda1712006-07-03 18:43:01 -0400255 return 0;
256 }
257 spin_unlock_irqrestore(&priv->lock, flags);
258 buffer = kmalloc(count, GFP_ATOMIC);
259 if (!buffer) {
260 dev_err(&port->dev, "out of memory\n");
261 return -ENOMEM;
262 }
263 urb = usb_alloc_urb(0, GFP_ATOMIC);
264 if (!urb) {
265 dev_err(&port->dev, "no more free urbs\n");
Alan Coxc4d0f8c2008-04-29 14:35:39 +0100266 kfree(buffer);
Andy Gay5dda1712006-07-03 18:43:01 -0400267 return -ENOMEM;
268 }
Alan Coxc4d0f8c2008-04-29 14:35:39 +0100269 memcpy(buffer, buf, count);
Andy Gay5dda1712006-07-03 18:43:01 -0400270
Harvey Harrison441b62c2008-03-03 16:08:34 -0800271 usb_serial_debug_data(debug, &port->dev, __func__, count, buffer);
Andy Gay5dda1712006-07-03 18:43:01 -0400272
273 usb_fill_bulk_urb(urb, serial->dev,
274 usb_sndbulkpipe(serial->dev,
275 port->bulk_out_endpointAddress),
276 buffer, count,
277 airprime_write_bulk_callback, port);
278
279 /* send it down the pipe */
280 status = usb_submit_urb(urb, GFP_ATOMIC);
281 if (status) {
282 dev_err(&port->dev,
283 "%s - usb_submit_urb(write bulk) failed with status = %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800284 __func__, status);
Andy Gay5dda1712006-07-03 18:43:01 -0400285 count = status;
Alan Coxc4d0f8c2008-04-29 14:35:39 +0100286 kfree(buffer);
Andy Gay5dda1712006-07-03 18:43:01 -0400287 } else {
288 spin_lock_irqsave(&priv->lock, flags);
289 ++priv->outstanding_urbs;
290 spin_unlock_irqrestore(&priv->lock, flags);
291 }
292 /* we are done with this urb, so let the host driver
293 * really free it when it is finished with it */
Alan Coxc4d0f8c2008-04-29 14:35:39 +0100294 usb_free_urb(urb);
Andy Gay5dda1712006-07-03 18:43:01 -0400295 return count;
296}
297
Greg KH3b86b202005-04-25 21:46:29 -0700298static struct usb_driver airprime_driver = {
Greg KH3b86b202005-04-25 21:46:29 -0700299 .name = "airprime",
300 .probe = usb_serial_probe,
301 .disconnect = usb_serial_disconnect,
302 .id_table = id_table,
Andy Gay5dda1712006-07-03 18:43:01 -0400303 .no_dynamic_id = 1,
Greg KH3b86b202005-04-25 21:46:29 -0700304};
305
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700306static struct usb_serial_driver airprime_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700307 .driver = {
308 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700309 .name = "airprime",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700310 },
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100311 .usb_driver = &airprime_driver,
Greg KH3b86b202005-04-25 21:46:29 -0700312 .id_table = id_table,
Andy Gay5dda1712006-07-03 18:43:01 -0400313 .open = airprime_open,
314 .close = airprime_close,
315 .write = airprime_write,
Greg KH3b86b202005-04-25 21:46:29 -0700316};
317
318static int __init airprime_init(void)
319{
320 int retval;
321
Alan Coxc4d0f8c2008-04-29 14:35:39 +0100322 airprime_device.num_ports = endpoints;
323 if (endpoints < 0 || endpoints >= MAX_BULK_EPS)
324 airprime_device.num_ports = NUM_BULK_EPS;
325
Greg KH3b86b202005-04-25 21:46:29 -0700326 retval = usb_serial_register(&airprime_device);
327 if (retval)
328 return retval;
329 retval = usb_register(&airprime_driver);
330 if (retval)
331 usb_serial_deregister(&airprime_device);
332 return retval;
333}
334
335static void __exit airprime_exit(void)
336{
Harvey Harrison441b62c2008-03-03 16:08:34 -0800337 dbg("%s", __func__);
Andy Gay5dda1712006-07-03 18:43:01 -0400338
Greg KH3b86b202005-04-25 21:46:29 -0700339 usb_deregister(&airprime_driver);
340 usb_serial_deregister(&airprime_device);
341}
342
343module_init(airprime_init);
344module_exit(airprime_exit);
345MODULE_LICENSE("GPL");
Andy Gay5dda1712006-07-03 18:43:01 -0400346
347module_param(debug, bool, S_IRUGO | S_IWUSR);
348MODULE_PARM_DESC(debug, "Debug enabled");
349module_param(buffer_size, int, 0);
Alan Coxc4d0f8c2008-04-29 14:35:39 +0100350MODULE_PARM_DESC(buffer_size,
351 "Size of the transfer buffers in bytes (default 4096)");
Andy Gay5dda1712006-07-03 18:43:01 -0400352module_param(endpoints, int, 0);
353MODULE_PARM_DESC(endpoints, "Number of bulk EPs to configure (default 3)");