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