USB: RTS/DTR signal patch for airprime driver
[linux-2.6] / drivers / usb / serial / airprime.c
1 /*
2  * AirPrime CDMA Wireless Serial USB driver
3  *
4  * Copyright (C) 2005-2006 Greg Kroah-Hartman <gregkh@suse.de>
5  *
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>
14 #include <linux/tty_flip.h>
15 #include <linux/module.h>
16 #include <linux/usb.h>
17 #include <linux/usb/serial.h>
18
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, 0x1130) }, /* Novatel Wireless S720 CDMA/EV-DO */
23         { USB_DEVICE(0x1410, 0x2110) }, /* Novatel Wireless U720 CDMA/EV-DO */
24         { USB_DEVICE(0x1410, 0x1430) }, /* Novatel Merlin XU870 HSDPA/3G */
25         { USB_DEVICE(0x1410, 0x1100) }, /* ExpressCard34 Qualcomm 3G CDMA */
26         { USB_DEVICE(0x413c, 0x8115) }, /* Dell Wireless HSDPA 5500 */
27         { },
28 };
29 MODULE_DEVICE_TABLE(usb, id_table);
30
31 #define URB_TRANSFER_BUFFER_SIZE        4096
32 #define NUM_READ_URBS                   4
33 #define NUM_WRITE_URBS                  4
34 #define NUM_BULK_EPS                    3
35 #define MAX_BULK_EPS                    6
36
37 /* if overridden by the user, then use their value for the size of the
38  * read and write urbs, and the number of endpoints */
39 static int buffer_size = URB_TRANSFER_BUFFER_SIZE;
40 static int endpoints = NUM_BULK_EPS;
41 static int debug;
42 struct airprime_private {
43         spinlock_t lock;
44         int outstanding_urbs;
45         int throttled;
46         struct urb *read_urbp[NUM_READ_URBS];
47
48         /* Settings for the port */
49         int rts_state;  /* Handshaking pins (outputs) */
50         int dtr_state;
51         int cts_state;  /* Handshaking pins (inputs) */
52         int dsr_state;
53         int dcd_state;
54         int ri_state;
55 };
56
57 static int airprime_send_setup(struct usb_serial_port *port)
58 {
59         struct usb_serial *serial = port->serial;
60         struct airprime_private *priv;
61
62         dbg("%s", __FUNCTION__);
63
64         if (port->number != 0)
65                 return 0;
66
67         priv = usb_get_serial_port_data(port);
68
69         if (port->tty) {
70                 int val = 0;
71                 if (priv->dtr_state)
72                         val |= 0x01;
73                 if (priv->rts_state)
74                         val |= 0x02;
75
76                 return usb_control_msg(serial->dev,
77                                 usb_rcvctrlpipe(serial->dev, 0),
78                                 0x22,0x21,val,0,NULL,0,USB_CTRL_SET_TIMEOUT);
79         }
80
81         return 0;
82 }
83
84 static void airprime_read_bulk_callback(struct urb *urb)
85 {
86         struct usb_serial_port *port = urb->context;
87         unsigned char *data = urb->transfer_buffer;
88         struct tty_struct *tty;
89         int result;
90
91         dbg("%s - port %d", __FUNCTION__, port->number);
92
93         if (urb->status) {
94                 dbg("%s - nonzero read bulk status received: %d",
95                     __FUNCTION__, urb->status);
96                 return;
97         }
98         usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
99
100         tty = port->tty;
101         if (tty && urb->actual_length) {
102                 tty_insert_flip_string (tty, data, urb->actual_length);
103                 tty_flip_buffer_push (tty);
104         }
105
106         result = usb_submit_urb (urb, GFP_ATOMIC);
107         if (result)
108                 dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n",
109                         __FUNCTION__, result);
110         return;
111 }
112
113 static void airprime_write_bulk_callback(struct urb *urb)
114 {
115         struct usb_serial_port *port = urb->context;
116         struct airprime_private *priv = usb_get_serial_port_data(port);
117         unsigned long flags;
118
119         dbg("%s - port %d", __FUNCTION__, port->number);
120
121         /* free up the transfer buffer, as usb_free_urb() does not do this */
122         kfree (urb->transfer_buffer);
123
124         if (urb->status)
125                 dbg("%s - nonzero write bulk status received: %d",
126                     __FUNCTION__, urb->status);
127         spin_lock_irqsave(&priv->lock, flags);
128         --priv->outstanding_urbs;
129         spin_unlock_irqrestore(&priv->lock, flags);
130
131         usb_serial_port_softint(port);
132 }
133
134 static int airprime_open(struct usb_serial_port *port, struct file *filp)
135 {
136         struct airprime_private *priv = usb_get_serial_port_data(port);
137         struct usb_serial *serial = port->serial;
138         struct urb *urb;
139         char *buffer = NULL;
140         int i;
141         int result = 0;
142
143         dbg("%s - port %d", __FUNCTION__, port->number);
144
145         /* initialize our private data structure if it isn't already created */
146         if (!priv) {
147                 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
148                 if (!priv) {
149                         result = -ENOMEM;
150                         goto out;
151                 }
152                 spin_lock_init(&priv->lock);
153                 usb_set_serial_port_data(port, priv);
154         }
155
156         /* Set some sane defaults */
157         priv->rts_state = 1;
158         priv->dtr_state = 1;
159
160         for (i = 0; i < NUM_READ_URBS; ++i) {
161                 buffer = kmalloc(buffer_size, GFP_KERNEL);
162                 if (!buffer) {
163                         dev_err(&port->dev, "%s - out of memory.\n",
164                                 __FUNCTION__);
165                         result = -ENOMEM;
166                         goto errout;
167                 }
168                 urb = usb_alloc_urb(0, GFP_KERNEL);
169                 if (!urb) {
170                         kfree(buffer);
171                         dev_err(&port->dev, "%s - no more urbs?\n",
172                                 __FUNCTION__);
173                         result = -ENOMEM;
174                         goto errout;
175                 }
176                 usb_fill_bulk_urb(urb, serial->dev,
177                                   usb_rcvbulkpipe(serial->dev,
178                                                   port->bulk_out_endpointAddress),
179                                   buffer, buffer_size,
180                                   airprime_read_bulk_callback, port);
181                 result = usb_submit_urb(urb, GFP_KERNEL);
182                 if (result) {
183                         usb_free_urb(urb);
184                         kfree(buffer);
185                         dev_err(&port->dev,
186                                 "%s - failed submitting read urb %d for port %d, error %d\n",
187                                 __FUNCTION__, i, port->number, result);
188                         goto errout;
189                 }
190                 /* remember this urb so we can kill it when the port is closed */
191                 priv->read_urbp[i] = urb;
192         }
193
194         airprime_send_setup(port);
195
196         goto out;
197
198  errout:
199         /* some error happened, cancel any submitted urbs and clean up anything that
200            got allocated successfully */
201
202         while (i-- != 0) {
203                 urb = priv->read_urbp[i];
204                 buffer = urb->transfer_buffer;
205                 usb_kill_urb (urb);
206                 usb_free_urb (urb);
207                 kfree (buffer);
208         }
209
210  out:
211         return result;
212 }
213
214 static void airprime_close(struct usb_serial_port *port, struct file * filp)
215 {
216         struct airprime_private *priv = usb_get_serial_port_data(port);
217         int i;
218
219         dbg("%s - port %d", __FUNCTION__, port->number);
220
221         priv->rts_state = 0;
222         priv->dtr_state = 0;
223
224         airprime_send_setup(port);
225
226         for (i = 0; i < NUM_READ_URBS; ++i) {
227                 usb_kill_urb (priv->read_urbp[i]);
228                 kfree (priv->read_urbp[i]->transfer_buffer);
229                 usb_free_urb (priv->read_urbp[i]);
230         }
231
232         /* free up private structure */
233         kfree (priv);
234         usb_set_serial_port_data(port, NULL);
235 }
236
237 static int airprime_write(struct usb_serial_port *port,
238                           const unsigned char *buf, int count)
239 {
240         struct airprime_private *priv = usb_get_serial_port_data(port);
241         struct usb_serial *serial = port->serial;
242         struct urb *urb;
243         unsigned char *buffer;
244         unsigned long flags;
245         int status;
246         dbg("%s - port %d", __FUNCTION__, port->number);
247
248         spin_lock_irqsave(&priv->lock, flags);
249         if (priv->outstanding_urbs > NUM_WRITE_URBS) {
250                 spin_unlock_irqrestore(&priv->lock, flags);
251                 dbg("%s - write limit hit\n", __FUNCTION__);
252                 return 0;
253         }
254         spin_unlock_irqrestore(&priv->lock, flags);
255         buffer = kmalloc(count, GFP_ATOMIC);
256         if (!buffer) {
257                 dev_err(&port->dev, "out of memory\n");
258                 return -ENOMEM;
259         }
260         urb = usb_alloc_urb(0, GFP_ATOMIC);
261         if (!urb) {
262                 dev_err(&port->dev, "no more free urbs\n");
263                 kfree (buffer);
264                 return -ENOMEM;
265         }
266         memcpy (buffer, buf, count);
267
268         usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, buffer);
269
270         usb_fill_bulk_urb(urb, serial->dev,
271                           usb_sndbulkpipe(serial->dev,
272                                           port->bulk_out_endpointAddress),
273                           buffer, count,
274                           airprime_write_bulk_callback, port);
275
276         /* send it down the pipe */
277         status = usb_submit_urb(urb, GFP_ATOMIC);
278         if (status) {
279                 dev_err(&port->dev,
280                         "%s - usb_submit_urb(write bulk) failed with status = %d\n",
281                         __FUNCTION__, status);
282                 count = status;
283                 kfree (buffer);
284         } else {
285                 spin_lock_irqsave(&priv->lock, flags);
286                 ++priv->outstanding_urbs;
287                 spin_unlock_irqrestore(&priv->lock, flags);
288         }
289         /* we are done with this urb, so let the host driver
290          * really free it when it is finished with it */
291         usb_free_urb (urb);
292         return count;
293 }
294
295 static struct usb_driver airprime_driver = {
296         .name =         "airprime",
297         .probe =        usb_serial_probe,
298         .disconnect =   usb_serial_disconnect,
299         .id_table =     id_table,
300         .no_dynamic_id =        1,
301 };
302
303 static struct usb_serial_driver airprime_device = {
304         .driver = {
305                 .owner =        THIS_MODULE,
306                 .name =         "airprime",
307         },
308         .usb_driver =           &airprime_driver,
309         .id_table =             id_table,
310         .num_interrupt_in =     NUM_DONT_CARE,
311         .num_bulk_in =          NUM_DONT_CARE,
312         .num_bulk_out =         NUM_DONT_CARE,
313         .open =                 airprime_open,
314         .close =                airprime_close,
315         .write =                airprime_write,
316 };
317
318 static int __init airprime_init(void)
319 {
320         int retval;
321
322         airprime_device.num_ports =
323                 (endpoints > 0 && endpoints <= MAX_BULK_EPS) ? endpoints : NUM_BULK_EPS;
324         retval = usb_serial_register(&airprime_device);
325         if (retval)
326                 return retval;
327         retval = usb_register(&airprime_driver);
328         if (retval)
329                 usb_serial_deregister(&airprime_device);
330         return retval;
331 }
332
333 static void __exit airprime_exit(void)
334 {
335         dbg("%s", __FUNCTION__);
336
337         usb_deregister(&airprime_driver);
338         usb_serial_deregister(&airprime_device);
339 }
340
341 module_init(airprime_init);
342 module_exit(airprime_exit);
343 MODULE_LICENSE("GPL");
344
345 module_param(debug, bool, S_IRUGO | S_IWUSR);
346 MODULE_PARM_DESC(debug, "Debug enabled");
347 module_param(buffer_size, int, 0);
348 MODULE_PARM_DESC(buffer_size, "Size of the transfer buffers in bytes (default 4096)");
349 module_param(endpoints, int, 0);
350 MODULE_PARM_DESC(endpoints, "Number of bulk EPs to configure (default 3)");