Pull hp-machvec into release branch
[linux-2.6] / drivers / usb / serial / generic.c
1 /*
2  * USB Serial Converter Generic functions
3  *
4  * Copyright (C) 1999 - 2002 Greg Kroah-Hartman (greg@kroah.com)
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
12 #include <linux/config.h>
13 #include <linux/kernel.h>
14 #include <linux/errno.h>
15 #include <linux/slab.h>
16 #include <linux/tty.h>
17 #include <linux/tty_flip.h>
18 #include <linux/module.h>
19 #include <linux/moduleparam.h>
20 #include <linux/usb.h>
21 #include <asm/uaccess.h>
22 #include "usb-serial.h"
23
24 static int debug;
25
26 #ifdef CONFIG_USB_SERIAL_GENERIC
27 static __u16 vendor  = 0x05f9;
28 static __u16 product = 0xffff;
29
30 module_param(vendor, ushort, 0);
31 MODULE_PARM_DESC(vendor, "User specified USB idVendor");
32
33 module_param(product, ushort, 0);
34 MODULE_PARM_DESC(product, "User specified USB idProduct");
35
36 static struct usb_device_id generic_device_ids[2]; /* Initially all zeroes. */
37
38 /* All of the device info needed for the Generic Serial Converter */
39 struct usb_serial_device_type usb_serial_generic_device = {
40         .owner =                THIS_MODULE,
41         .name =                 "Generic",
42         .short_name =           "generic",
43         .id_table =             generic_device_ids,
44         .num_interrupt_in =     NUM_DONT_CARE,
45         .num_bulk_in =          NUM_DONT_CARE,
46         .num_bulk_out =         NUM_DONT_CARE,
47         .num_ports =            1,
48         .shutdown =             usb_serial_generic_shutdown,
49 };
50
51 /* we want to look at all devices, as the vendor/product id can change
52  * depending on the command line argument */
53 static struct usb_device_id generic_serial_ids[] = {
54         {.driver_info = 42},
55         {}
56 };
57
58 static int generic_probe(struct usb_interface *interface,
59                                const struct usb_device_id *id)
60 {
61         const struct usb_device_id *id_pattern;
62
63         id_pattern = usb_match_id(interface, generic_device_ids);
64         if (id_pattern != NULL)
65                 return usb_serial_probe(interface, id);
66         return -ENODEV;
67 }
68
69 static struct usb_driver generic_driver = {
70         .owner =        THIS_MODULE,
71         .name =         "usbserial_generic",
72         .probe =        generic_probe,
73         .disconnect =   usb_serial_disconnect,
74         .id_table =     generic_serial_ids,
75 };
76 #endif
77
78 int usb_serial_generic_register (int _debug)
79 {
80         int retval = 0;
81
82         debug = _debug;
83 #ifdef CONFIG_USB_SERIAL_GENERIC
84         generic_device_ids[0].idVendor = vendor;
85         generic_device_ids[0].idProduct = product;
86         generic_device_ids[0].match_flags = USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT;
87
88         /* register our generic driver with ourselves */
89         retval = usb_serial_register (&usb_serial_generic_device);
90         if (retval)
91                 goto exit;
92         retval = usb_register(&generic_driver);
93         if (retval)
94                 usb_serial_deregister(&usb_serial_generic_device);
95 exit:
96 #endif
97         return retval;
98 }
99
100 void usb_serial_generic_deregister (void)
101 {
102 #ifdef CONFIG_USB_SERIAL_GENERIC
103         /* remove our generic driver */
104         usb_deregister(&generic_driver);
105         usb_serial_deregister (&usb_serial_generic_device);
106 #endif
107 }
108
109 int usb_serial_generic_open (struct usb_serial_port *port, struct file *filp)
110 {
111         struct usb_serial *serial = port->serial;
112         int result = 0;
113
114         dbg("%s - port %d", __FUNCTION__, port->number);
115
116         /* force low_latency on so that our tty_push actually forces the data through, 
117            otherwise it is scheduled, and with high data rates (like with OHCI) data
118            can get lost. */
119         if (port->tty)
120                 port->tty->low_latency = 1;
121
122         /* if we have a bulk interrupt, start reading from it */
123         if (serial->num_bulk_in) {
124                 /* Start reading from the device */
125                 usb_fill_bulk_urb (port->read_urb, serial->dev,
126                                    usb_rcvbulkpipe(serial->dev, port->bulk_in_endpointAddress),
127                                    port->read_urb->transfer_buffer,
128                                    port->read_urb->transfer_buffer_length,
129                                    ((serial->type->read_bulk_callback) ?
130                                      serial->type->read_bulk_callback :
131                                      usb_serial_generic_read_bulk_callback),
132                                    port);
133                 result = usb_submit_urb(port->read_urb, GFP_KERNEL);
134                 if (result)
135                         dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
136         }
137
138         return result;
139 }
140
141 static void generic_cleanup (struct usb_serial_port *port)
142 {
143         struct usb_serial *serial = port->serial;
144
145         dbg("%s - port %d", __FUNCTION__, port->number);
146
147         if (serial->dev) {
148                 /* shutdown any bulk reads that might be going on */
149                 if (serial->num_bulk_out)
150                         usb_kill_urb(port->write_urb);
151                 if (serial->num_bulk_in)
152                         usb_kill_urb(port->read_urb);
153         }
154 }
155
156 void usb_serial_generic_close (struct usb_serial_port *port, struct file * filp)
157 {
158         dbg("%s - port %d", __FUNCTION__, port->number);
159         generic_cleanup (port);
160 }
161
162 int usb_serial_generic_write(struct usb_serial_port *port, const unsigned char *buf, int count)
163 {
164         struct usb_serial *serial = port->serial;
165         int result;
166         unsigned char *data;
167
168         dbg("%s - port %d", __FUNCTION__, port->number);
169
170         if (count == 0) {
171                 dbg("%s - write request of 0 bytes", __FUNCTION__);
172                 return (0);
173         }
174
175         /* only do something if we have a bulk out endpoint */
176         if (serial->num_bulk_out) {
177                 spin_lock(&port->lock);
178                 if (port->write_urb_busy) {
179                         spin_unlock(&port->lock);
180                         dbg("%s - already writing", __FUNCTION__);
181                         return 0;
182                 }
183                 port->write_urb_busy = 1;
184                 spin_unlock(&port->lock);
185
186                 count = (count > port->bulk_out_size) ? port->bulk_out_size : count;
187
188                 memcpy (port->write_urb->transfer_buffer, buf, count);
189                 data = port->write_urb->transfer_buffer;
190                 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, data);
191
192                 /* set up our urb */
193                 usb_fill_bulk_urb (port->write_urb, serial->dev,
194                                    usb_sndbulkpipe (serial->dev,
195                                                     port->bulk_out_endpointAddress),
196                                    port->write_urb->transfer_buffer, count,
197                                    ((serial->type->write_bulk_callback) ? 
198                                      serial->type->write_bulk_callback :
199                                      usb_serial_generic_write_bulk_callback), port);
200
201                 /* send the data out the bulk port */
202                 port->write_urb_busy = 1;
203                 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
204                 if (result) {
205                         dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __FUNCTION__, result);
206                         /* don't have to grab the lock here, as we will retry if != 0 */
207                         port->write_urb_busy = 0;
208                 } else
209                         result = count;
210
211                 return result;
212         }
213
214         /* no bulk out, so return 0 bytes written */
215         return 0;
216 }
217
218 int usb_serial_generic_write_room (struct usb_serial_port *port)
219 {
220         struct usb_serial *serial = port->serial;
221         int room = 0;
222
223         dbg("%s - port %d", __FUNCTION__, port->number);
224
225         if (serial->num_bulk_out) {
226                 if (!(port->write_urb_busy))
227                         room = port->bulk_out_size;
228         }
229
230         dbg("%s - returns %d", __FUNCTION__, room);
231         return (room);
232 }
233
234 int usb_serial_generic_chars_in_buffer (struct usb_serial_port *port)
235 {
236         struct usb_serial *serial = port->serial;
237         int chars = 0;
238
239         dbg("%s - port %d", __FUNCTION__, port->number);
240
241         if (serial->num_bulk_out) {
242                 if (port->write_urb_busy)
243                         chars = port->write_urb->transfer_buffer_length;
244         }
245
246         dbg("%s - returns %d", __FUNCTION__, chars);
247         return (chars);
248 }
249
250 void usb_serial_generic_read_bulk_callback (struct urb *urb, struct pt_regs *regs)
251 {
252         struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
253         struct usb_serial *serial = port->serial;
254         struct tty_struct *tty;
255         unsigned char *data = urb->transfer_buffer;
256         int i;
257         int result;
258
259         dbg("%s - port %d", __FUNCTION__, port->number);
260
261         if (urb->status) {
262                 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
263                 return;
264         }
265
266         usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
267
268         tty = port->tty;
269         if (tty && urb->actual_length) {
270                 for (i = 0; i < urb->actual_length ; ++i) {
271                         /* if we insert more than TTY_FLIPBUF_SIZE characters, we drop them. */
272                         if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
273                                 tty_flip_buffer_push(tty);
274                         }
275                         /* this doesn't actually push the data through unless tty->low_latency is set */
276                         tty_insert_flip_char(tty, data[i], 0);
277                 }
278                 tty_flip_buffer_push(tty);
279         }
280
281         /* Continue trying to always read  */
282         usb_fill_bulk_urb (port->read_urb, serial->dev,
283                            usb_rcvbulkpipe (serial->dev,
284                                             port->bulk_in_endpointAddress),
285                            port->read_urb->transfer_buffer,
286                            port->read_urb->transfer_buffer_length,
287                            ((serial->type->read_bulk_callback) ? 
288                              serial->type->read_bulk_callback : 
289                              usb_serial_generic_read_bulk_callback), port);
290         result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
291         if (result)
292                 dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
293 }
294
295 void usb_serial_generic_write_bulk_callback (struct urb *urb, struct pt_regs *regs)
296 {
297         struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
298
299         dbg("%s - port %d", __FUNCTION__, port->number);
300
301         port->write_urb_busy = 0;
302         if (urb->status) {
303                 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
304                 return;
305         }
306
307         usb_serial_port_softint((void *)port);
308
309         schedule_work(&port->work);
310 }
311
312 void usb_serial_generic_shutdown (struct usb_serial *serial)
313 {
314         int i;
315
316         dbg("%s", __FUNCTION__);
317
318         /* stop reads and writes on all ports */
319         for (i=0; i < serial->num_ports; ++i) {
320                 generic_cleanup(serial->port[i]);
321         }
322 }
323