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