USB: sierra: remove incorrect usage of the urb status field
[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(0x413c, 0x8115) }, /* Dell Wireless HSDPA 5500 */
22         { },
23 };
24 MODULE_DEVICE_TABLE(usb, id_table);
25
26 #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 */
34 static int buffer_size = URB_TRANSFER_BUFFER_SIZE;
35 static int endpoints = NUM_BULK_EPS;
36 static int debug;
37 struct airprime_private {
38         spinlock_t lock;
39         int outstanding_urbs;
40         int throttled;
41         struct urb *read_urbp[NUM_READ_URBS];
42
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;
50 };
51
52 static 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
79 static void airprime_read_bulk_callback(struct urb *urb)
80 {
81         struct usb_serial_port *port = urb->context;
82         unsigned char *data = urb->transfer_buffer;
83         struct tty_struct *tty;
84         int result;
85         int status = urb->status;
86
87         dbg("%s - port %d", __FUNCTION__, port->number);
88
89         if (status) {
90                 dbg("%s - nonzero read bulk status received: %d",
91                     __FUNCTION__, status);
92                 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
109 static void airprime_write_bulk_callback(struct urb *urb)
110 {
111         struct usb_serial_port *port = urb->context;
112         struct airprime_private *priv = usb_get_serial_port_data(port);
113         int status = urb->status;
114         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
121         if (status)
122                 dbg("%s - nonzero write bulk status received: %d",
123                     __FUNCTION__, status);
124         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
131 static 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
153         /* Set some sane defaults */
154         priv->rts_state = 1;
155         priv->dtr_state = 1;
156
157         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) {
167                         kfree(buffer);
168                         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) {
180                         usb_free_urb(urb);
181                         kfree(buffer);
182                         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         }
190
191         airprime_send_setup(port);
192
193         goto out;
194
195  errout:
196         /* some error happened, cancel any submitted urbs and clean up anything that
197            got allocated successfully */
198
199         while (i-- != 0) {
200                 urb = priv->read_urbp[i];
201                 buffer = urb->transfer_buffer;
202                 usb_kill_urb (urb);
203                 usb_free_urb (urb);
204                 kfree (buffer);
205         }
206
207  out:
208         return result;
209 }
210
211 static 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
218         priv->rts_state = 0;
219         priv->dtr_state = 0;
220
221         airprime_send_setup(port);
222
223         for (i = 0; i < NUM_READ_URBS; ++i) {
224                 usb_kill_urb (priv->read_urbp[i]);
225                 kfree (priv->read_urbp[i]->transfer_buffer);
226                 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
234 static 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
292 static struct usb_driver airprime_driver = {
293         .name =         "airprime",
294         .probe =        usb_serial_probe,
295         .disconnect =   usb_serial_disconnect,
296         .id_table =     id_table,
297         .no_dynamic_id =        1,
298 };
299
300 static struct usb_serial_driver airprime_device = {
301         .driver = {
302                 .owner =        THIS_MODULE,
303                 .name =         "airprime",
304         },
305         .usb_driver =           &airprime_driver,
306         .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,
310         .open =                 airprime_open,
311         .close =                airprime_close,
312         .write =                airprime_write,
313 };
314
315 static int __init airprime_init(void)
316 {
317         int retval;
318
319         airprime_device.num_ports =
320                 (endpoints > 0 && endpoints <= MAX_BULK_EPS) ? endpoints : NUM_BULK_EPS;
321         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
330 static void __exit airprime_exit(void)
331 {
332         dbg("%s", __FUNCTION__);
333
334         usb_deregister(&airprime_driver);
335         usb_serial_deregister(&airprime_device);
336 }
337
338 module_init(airprime_init);
339 module_exit(airprime_exit);
340 MODULE_LICENSE("GPL");
341
342 module_param(debug, bool, S_IRUGO | S_IWUSR);
343 MODULE_PARM_DESC(debug, "Debug enabled");
344 module_param(buffer_size, int, 0);
345 MODULE_PARM_DESC(buffer_size, "Size of the transfer buffers in bytes (default 4096)");
346 module_param(endpoints, int, 0);
347 MODULE_PARM_DESC(endpoints, "Number of bulk EPs to configure (default 3)");