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