xref: /openbmc/linux/drivers/usb/serial/generic.c (revision 64c70b1c)
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 
24 static int debug;
25 
26 #ifdef CONFIG_USB_SERIAL_GENERIC
27 
28 static int generic_probe(struct usb_interface *interface,
29 			 const struct usb_device_id *id);
30 
31 static __u16 vendor  = 0x05f9;
32 static __u16 product = 0xffff;
33 
34 module_param(vendor, ushort, 0);
35 MODULE_PARM_DESC(vendor, "User specified USB idVendor");
36 
37 module_param(product, ushort, 0);
38 MODULE_PARM_DESC(product, "User specified USB idProduct");
39 
40 static struct usb_device_id generic_device_ids[2]; /* Initially all zeroes. */
41 
42 /* we want to look at all devices, as the vendor/product id can change
43  * depending on the command line argument */
44 static struct usb_device_id generic_serial_ids[] = {
45 	{.driver_info = 42},
46 	{}
47 };
48 
49 static struct usb_driver generic_driver = {
50 	.name =		"usbserial_generic",
51 	.probe =	generic_probe,
52 	.disconnect =	usb_serial_disconnect,
53 	.id_table =	generic_serial_ids,
54 	.no_dynamic_id =	1,
55 };
56 
57 /* All of the device info needed for the Generic Serial Converter */
58 struct usb_serial_driver usb_serial_generic_device = {
59 	.driver = {
60 		.owner =	THIS_MODULE,
61 		.name =		"generic",
62 	},
63 	.id_table =		generic_device_ids,
64 	.usb_driver = 		&generic_driver,
65 	.num_interrupt_in =	NUM_DONT_CARE,
66 	.num_bulk_in =		NUM_DONT_CARE,
67 	.num_bulk_out =		NUM_DONT_CARE,
68 	.num_ports =		1,
69 	.shutdown =		usb_serial_generic_shutdown,
70 	.throttle =		usb_serial_generic_throttle,
71 	.unthrottle =		usb_serial_generic_unthrottle,
72 };
73 
74 static int generic_probe(struct usb_interface *interface,
75 			       const struct usb_device_id *id)
76 {
77 	const struct usb_device_id *id_pattern;
78 
79 	id_pattern = usb_match_id(interface, generic_device_ids);
80 	if (id_pattern != NULL)
81 		return usb_serial_probe(interface, id);
82 	return -ENODEV;
83 }
84 #endif
85 
86 int usb_serial_generic_register (int _debug)
87 {
88 	int retval = 0;
89 
90 	debug = _debug;
91 #ifdef CONFIG_USB_SERIAL_GENERIC
92 	generic_device_ids[0].idVendor = vendor;
93 	generic_device_ids[0].idProduct = product;
94 	generic_device_ids[0].match_flags = USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT;
95 
96 	/* register our generic driver with ourselves */
97 	retval = usb_serial_register (&usb_serial_generic_device);
98 	if (retval)
99 		goto exit;
100 	retval = usb_register(&generic_driver);
101 	if (retval)
102 		usb_serial_deregister(&usb_serial_generic_device);
103 exit:
104 #endif
105 	return retval;
106 }
107 
108 void usb_serial_generic_deregister (void)
109 {
110 #ifdef CONFIG_USB_SERIAL_GENERIC
111 	/* remove our generic driver */
112 	usb_deregister(&generic_driver);
113 	usb_serial_deregister (&usb_serial_generic_device);
114 #endif
115 }
116 
117 int usb_serial_generic_open (struct usb_serial_port *port, struct file *filp)
118 {
119 	struct usb_serial *serial = port->serial;
120 	int result = 0;
121 	unsigned long flags;
122 
123 	dbg("%s - port %d", __FUNCTION__, port->number);
124 
125 	/* force low_latency on so that our tty_push actually forces the data through,
126 	   otherwise it is scheduled, and with high data rates (like with OHCI) data
127 	   can get lost. */
128 	if (port->tty)
129 		port->tty->low_latency = 1;
130 
131 	/* clear the throttle flags */
132 	spin_lock_irqsave(&port->lock, flags);
133 	port->throttled = 0;
134 	port->throttle_req = 0;
135 	spin_unlock_irqrestore(&port->lock, flags);
136 
137 	/* if we have a bulk endpoint, start reading from it */
138 	if (serial->num_bulk_in) {
139 		/* Start reading from the device */
140 		usb_fill_bulk_urb (port->read_urb, serial->dev,
141 				   usb_rcvbulkpipe(serial->dev, port->bulk_in_endpointAddress),
142 				   port->read_urb->transfer_buffer,
143 				   port->read_urb->transfer_buffer_length,
144 				   ((serial->type->read_bulk_callback) ?
145 				     serial->type->read_bulk_callback :
146 				     usb_serial_generic_read_bulk_callback),
147 				   port);
148 		result = usb_submit_urb(port->read_urb, GFP_KERNEL);
149 		if (result)
150 			dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
151 	}
152 
153 	return result;
154 }
155 EXPORT_SYMBOL_GPL(usb_serial_generic_open);
156 
157 static void generic_cleanup (struct usb_serial_port *port)
158 {
159 	struct usb_serial *serial = port->serial;
160 
161 	dbg("%s - port %d", __FUNCTION__, port->number);
162 
163 	if (serial->dev) {
164 		/* shutdown any bulk reads that might be going on */
165 		if (serial->num_bulk_out)
166 			usb_kill_urb(port->write_urb);
167 		if (serial->num_bulk_in)
168 			usb_kill_urb(port->read_urb);
169 	}
170 }
171 
172 void usb_serial_generic_close (struct usb_serial_port *port, struct file * filp)
173 {
174 	dbg("%s - port %d", __FUNCTION__, port->number);
175 	generic_cleanup (port);
176 }
177 
178 int usb_serial_generic_write(struct usb_serial_port *port, const unsigned char *buf, int count)
179 {
180 	struct usb_serial *serial = port->serial;
181 	int result;
182 	unsigned char *data;
183 
184 	dbg("%s - port %d", __FUNCTION__, port->number);
185 
186 	if (count == 0) {
187 		dbg("%s - write request of 0 bytes", __FUNCTION__);
188 		return (0);
189 	}
190 
191 	/* only do something if we have a bulk out endpoint */
192 	if (serial->num_bulk_out) {
193 		spin_lock_bh(&port->lock);
194 		if (port->write_urb_busy) {
195 			spin_unlock_bh(&port->lock);
196 			dbg("%s - already writing", __FUNCTION__);
197 			return 0;
198 		}
199 		port->write_urb_busy = 1;
200 		spin_unlock_bh(&port->lock);
201 
202 		count = (count > port->bulk_out_size) ? port->bulk_out_size : count;
203 
204 		memcpy (port->write_urb->transfer_buffer, buf, count);
205 		data = port->write_urb->transfer_buffer;
206 		usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, data);
207 
208 		/* set up our urb */
209 		usb_fill_bulk_urb (port->write_urb, serial->dev,
210 				   usb_sndbulkpipe (serial->dev,
211 						    port->bulk_out_endpointAddress),
212 				   port->write_urb->transfer_buffer, count,
213 				   ((serial->type->write_bulk_callback) ?
214 				     serial->type->write_bulk_callback :
215 				     usb_serial_generic_write_bulk_callback), port);
216 
217 		/* send the data out the bulk port */
218 		port->write_urb_busy = 1;
219 		result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
220 		if (result) {
221 			dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __FUNCTION__, result);
222 			/* don't have to grab the lock here, as we will retry if != 0 */
223 			port->write_urb_busy = 0;
224 		} else
225 			result = count;
226 
227 		return result;
228 	}
229 
230 	/* no bulk out, so return 0 bytes written */
231 	return 0;
232 }
233 
234 int usb_serial_generic_write_room (struct usb_serial_port *port)
235 {
236 	struct usb_serial *serial = port->serial;
237 	int room = 0;
238 
239 	dbg("%s - port %d", __FUNCTION__, port->number);
240 
241 	if (serial->num_bulk_out) {
242 		if (!(port->write_urb_busy))
243 			room = port->bulk_out_size;
244 	}
245 
246 	dbg("%s - returns %d", __FUNCTION__, room);
247 	return (room);
248 }
249 
250 int usb_serial_generic_chars_in_buffer (struct usb_serial_port *port)
251 {
252 	struct usb_serial *serial = port->serial;
253 	int chars = 0;
254 
255 	dbg("%s - port %d", __FUNCTION__, port->number);
256 
257 	if (serial->num_bulk_out) {
258 		if (port->write_urb_busy)
259 			chars = port->write_urb->transfer_buffer_length;
260 	}
261 
262 	dbg("%s - returns %d", __FUNCTION__, chars);
263 	return (chars);
264 }
265 
266 /* Push data to tty layer and resubmit the bulk read URB */
267 static void flush_and_resubmit_read_urb (struct usb_serial_port *port)
268 {
269 	struct usb_serial *serial = port->serial;
270 	struct urb *urb = port->read_urb;
271 	struct tty_struct *tty = port->tty;
272 	int result;
273 
274 	/* Push data to tty */
275 	if (tty && urb->actual_length) {
276 		tty_buffer_request_room(tty, urb->actual_length);
277 		tty_insert_flip_string(tty, urb->transfer_buffer, urb->actual_length);
278 	  	tty_flip_buffer_push(tty); /* is this allowed from an URB callback ? */
279 	}
280 
281 	/* Continue reading from device */
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_read_bulk_callback (struct urb *urb)
296 {
297 	struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
298 	unsigned char *data = urb->transfer_buffer;
299 	int is_throttled;
300 	unsigned long flags;
301 
302 	dbg("%s - port %d", __FUNCTION__, port->number);
303 
304 	if (urb->status) {
305 		dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
306 		return;
307 	}
308 
309 	usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
310 
311 	/* Throttle the device if requested by tty */
312 	if (urb->actual_length) {
313 		spin_lock_irqsave(&port->lock, flags);
314 		is_throttled = port->throttled = port->throttle_req;
315 		spin_unlock_irqrestore(&port->lock, flags);
316 		if (is_throttled) {
317 			/* Let the received data linger in the read URB;
318 			 * usb_serial_generic_unthrottle() will pick it
319 			 * up later. */
320 			dbg("%s - throttling device", __FUNCTION__);
321 			return;
322 		}
323 	}
324 
325 	/* Handle data and continue reading from device */
326 	flush_and_resubmit_read_urb(port);
327 }
328 EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback);
329 
330 void usb_serial_generic_write_bulk_callback (struct urb *urb)
331 {
332 	struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
333 
334 	dbg("%s - port %d", __FUNCTION__, port->number);
335 
336 	port->write_urb_busy = 0;
337 	if (urb->status) {
338 		dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
339 		return;
340 	}
341 
342 	usb_serial_port_softint(port);
343 }
344 EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
345 
346 void usb_serial_generic_throttle (struct usb_serial_port *port)
347 {
348 	unsigned long flags;
349 
350 	dbg("%s - port %d", __FUNCTION__, port->number);
351 
352 	/* Set the throttle request flag. It will be picked up
353 	 * by usb_serial_generic_read_bulk_callback(). */
354 	spin_lock_irqsave(&port->lock, flags);
355 	port->throttle_req = 1;
356 	spin_unlock_irqrestore(&port->lock, flags);
357 }
358 
359 void usb_serial_generic_unthrottle (struct usb_serial_port *port)
360 {
361 	int was_throttled;
362 	unsigned long flags;
363 
364 	dbg("%s - port %d", __FUNCTION__, port->number);
365 
366 	/* Clear the throttle flags */
367 	spin_lock_irqsave(&port->lock, flags);
368 	was_throttled = port->throttled;
369 	port->throttled = port->throttle_req = 0;
370 	spin_unlock_irqrestore(&port->lock, flags);
371 
372 	if (was_throttled) {
373 		/* Handle pending data and resume reading from device */
374 		flush_and_resubmit_read_urb(port);
375 	}
376 }
377 
378 void usb_serial_generic_shutdown (struct usb_serial *serial)
379 {
380 	int i;
381 
382 	dbg("%s", __FUNCTION__);
383 
384 	/* stop reads and writes on all ports */
385 	for (i=0; i < serial->num_ports; ++i) {
386 		generic_cleanup(serial->port[i]);
387 	}
388 }
389 
390