xref: /linux/drivers/usb/serial/generic.c (revision d97b46a64674a267bc41c9e16132ee2a98c3347d)
1 /*
2  * USB Serial Converter Generic functions
3  *
4  * Copyright (C) 2010 - 2011 Johan Hovold (jhovold@gmail.com)
5  * Copyright (C) 1999 - 2002 Greg Kroah-Hartman (greg@kroah.com)
6  *
7  *	This program is free software; you can redistribute it and/or
8  *	modify it under the terms of the GNU General Public License version
9  *	2 as published by the Free Software Foundation.
10  *
11  */
12 
13 #include <linux/kernel.h>
14 #include <linux/errno.h>
15 #include <linux/slab.h>
16 #include <linux/sysrq.h>
17 #include <linux/tty.h>
18 #include <linux/tty_flip.h>
19 #include <linux/module.h>
20 #include <linux/moduleparam.h>
21 #include <linux/usb.h>
22 #include <linux/usb/serial.h>
23 #include <linux/uaccess.h>
24 #include <linux/kfifo.h>
25 #include <linux/serial.h>
26 
27 static int debug;
28 
29 #ifdef CONFIG_USB_SERIAL_GENERIC
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 const struct usb_device_id generic_serial_ids[] = {
45 	{.driver_info = 42},
46 	{}
47 };
48 
49 /* All of the device info needed for the Generic Serial Converter */
50 struct usb_serial_driver usb_serial_generic_device = {
51 	.driver = {
52 		.owner =	THIS_MODULE,
53 		.name =		"generic",
54 	},
55 	.id_table =		generic_device_ids,
56 	.num_ports =		1,
57 	.disconnect =		usb_serial_generic_disconnect,
58 	.release =		usb_serial_generic_release,
59 	.throttle =		usb_serial_generic_throttle,
60 	.unthrottle =		usb_serial_generic_unthrottle,
61 	.resume =		usb_serial_generic_resume,
62 };
63 
64 static struct usb_serial_driver * const serial_drivers[] = {
65 	&usb_serial_generic_device, NULL
66 };
67 
68 #endif
69 
70 int usb_serial_generic_register(int _debug)
71 {
72 	int retval = 0;
73 
74 	debug = _debug;
75 #ifdef CONFIG_USB_SERIAL_GENERIC
76 	generic_device_ids[0].idVendor = vendor;
77 	generic_device_ids[0].idProduct = product;
78 	generic_device_ids[0].match_flags =
79 		USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT;
80 
81 	/* register our generic driver with ourselves */
82 	retval = usb_serial_register_drivers(serial_drivers, "usbserial_generic", generic_serial_ids);
83 #endif
84 	return retval;
85 }
86 
87 void usb_serial_generic_deregister(void)
88 {
89 #ifdef CONFIG_USB_SERIAL_GENERIC
90 	/* remove our generic driver */
91 	usb_serial_deregister_drivers(serial_drivers);
92 #endif
93 }
94 
95 int usb_serial_generic_open(struct tty_struct *tty, struct usb_serial_port *port)
96 {
97 	int result = 0;
98 	unsigned long flags;
99 
100 	/* clear the throttle flags */
101 	spin_lock_irqsave(&port->lock, flags);
102 	port->throttled = 0;
103 	port->throttle_req = 0;
104 	spin_unlock_irqrestore(&port->lock, flags);
105 
106 	/* if we have a bulk endpoint, start reading from it */
107 	if (port->bulk_in_size)
108 		result = usb_serial_generic_submit_read_urbs(port, GFP_KERNEL);
109 
110 	return result;
111 }
112 EXPORT_SYMBOL_GPL(usb_serial_generic_open);
113 
114 static void generic_cleanup(struct usb_serial_port *port)
115 {
116 	struct usb_serial *serial = port->serial;
117 	unsigned long flags;
118 	int i;
119 
120 	if (serial->dev) {
121 		/* shutdown any bulk transfers that might be going on */
122 		if (port->bulk_out_size) {
123 			for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i)
124 				usb_kill_urb(port->write_urbs[i]);
125 
126 			spin_lock_irqsave(&port->lock, flags);
127 			kfifo_reset_out(&port->write_fifo);
128 			spin_unlock_irqrestore(&port->lock, flags);
129 		}
130 		if (port->bulk_in_size) {
131 			for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i)
132 				usb_kill_urb(port->read_urbs[i]);
133 		}
134 	}
135 }
136 
137 void usb_serial_generic_close(struct usb_serial_port *port)
138 {
139 	generic_cleanup(port);
140 }
141 EXPORT_SYMBOL_GPL(usb_serial_generic_close);
142 
143 int usb_serial_generic_prepare_write_buffer(struct usb_serial_port *port,
144 						void *dest, size_t size)
145 {
146 	return kfifo_out_locked(&port->write_fifo, dest, size, &port->lock);
147 }
148 
149 /**
150  * usb_serial_generic_write_start - kick off an URB write
151  * @port:	Pointer to the &struct usb_serial_port data
152  *
153  * Returns zero on success, or a negative errno value
154  */
155 static int usb_serial_generic_write_start(struct usb_serial_port *port)
156 {
157 	struct urb *urb;
158 	int count, result;
159 	unsigned long flags;
160 	int i;
161 
162 	if (test_and_set_bit_lock(USB_SERIAL_WRITE_BUSY, &port->flags))
163 		return 0;
164 retry:
165 	spin_lock_irqsave(&port->lock, flags);
166 	if (!port->write_urbs_free || !kfifo_len(&port->write_fifo)) {
167 		clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
168 		spin_unlock_irqrestore(&port->lock, flags);
169 		return 0;
170 	}
171 	i = (int)find_first_bit(&port->write_urbs_free,
172 						ARRAY_SIZE(port->write_urbs));
173 	spin_unlock_irqrestore(&port->lock, flags);
174 
175 	urb = port->write_urbs[i];
176 	count = port->serial->type->prepare_write_buffer(port,
177 						urb->transfer_buffer,
178 						port->bulk_out_size);
179 	urb->transfer_buffer_length = count;
180 	usb_serial_debug_data(debug, &port->dev, __func__, count,
181 						urb->transfer_buffer);
182 	spin_lock_irqsave(&port->lock, flags);
183 	port->tx_bytes += count;
184 	spin_unlock_irqrestore(&port->lock, flags);
185 
186 	clear_bit(i, &port->write_urbs_free);
187 	result = usb_submit_urb(urb, GFP_ATOMIC);
188 	if (result) {
189 		dev_err_console(port, "%s - error submitting urb: %d\n",
190 						__func__, result);
191 		set_bit(i, &port->write_urbs_free);
192 		spin_lock_irqsave(&port->lock, flags);
193 		port->tx_bytes -= count;
194 		spin_unlock_irqrestore(&port->lock, flags);
195 
196 		clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
197 		return result;
198 	}
199 
200 	/* Try sending off another urb, unless in irq context (in which case
201 	 * there will be no free urb). */
202 	if (!in_irq())
203 		goto retry;
204 
205 	clear_bit_unlock(USB_SERIAL_WRITE_BUSY, &port->flags);
206 
207 	return 0;
208 }
209 
210 /**
211  * usb_serial_generic_write - generic write function for serial USB devices
212  * @tty:	Pointer to &struct tty_struct for the device
213  * @port:	Pointer to the &usb_serial_port structure for the device
214  * @buf:	Pointer to the data to write
215  * @count:	Number of bytes to write
216  *
217  * Returns the number of characters actually written, which may be anything
218  * from zero to @count. If an error occurs, it returns the negative errno
219  * value.
220  */
221 int usb_serial_generic_write(struct tty_struct *tty,
222 	struct usb_serial_port *port, const unsigned char *buf, int count)
223 {
224 	int result;
225 
226 	/* only do something if we have a bulk out endpoint */
227 	if (!port->bulk_out_size)
228 		return -ENODEV;
229 
230 	if (!count)
231 		return 0;
232 
233 	count = kfifo_in_locked(&port->write_fifo, buf, count, &port->lock);
234 	result = usb_serial_generic_write_start(port);
235 	if (result)
236 		return result;
237 
238 	return count;
239 }
240 EXPORT_SYMBOL_GPL(usb_serial_generic_write);
241 
242 int usb_serial_generic_write_room(struct tty_struct *tty)
243 {
244 	struct usb_serial_port *port = tty->driver_data;
245 	unsigned long flags;
246 	int room;
247 
248 	if (!port->bulk_out_size)
249 		return 0;
250 
251 	spin_lock_irqsave(&port->lock, flags);
252 	room = kfifo_avail(&port->write_fifo);
253 	spin_unlock_irqrestore(&port->lock, flags);
254 
255 	dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
256 	return room;
257 }
258 
259 int usb_serial_generic_chars_in_buffer(struct tty_struct *tty)
260 {
261 	struct usb_serial_port *port = tty->driver_data;
262 	unsigned long flags;
263 	int chars;
264 
265 	if (!port->bulk_out_size)
266 		return 0;
267 
268 	spin_lock_irqsave(&port->lock, flags);
269 	chars = kfifo_len(&port->write_fifo) + port->tx_bytes;
270 	spin_unlock_irqrestore(&port->lock, flags);
271 
272 	dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
273 	return chars;
274 }
275 
276 static int usb_serial_generic_submit_read_urb(struct usb_serial_port *port,
277 						int index, gfp_t mem_flags)
278 {
279 	int res;
280 
281 	if (!test_and_clear_bit(index, &port->read_urbs_free))
282 		return 0;
283 
284 	dev_dbg(&port->dev, "%s - port %d, urb %d\n", __func__,
285 		port->number, index);
286 
287 	res = usb_submit_urb(port->read_urbs[index], mem_flags);
288 	if (res) {
289 		if (res != -EPERM) {
290 			dev_err(&port->dev,
291 					"%s - usb_submit_urb failed: %d\n",
292 					__func__, res);
293 		}
294 		set_bit(index, &port->read_urbs_free);
295 		return res;
296 	}
297 
298 	return 0;
299 }
300 
301 int usb_serial_generic_submit_read_urbs(struct usb_serial_port *port,
302 					gfp_t mem_flags)
303 {
304 	int res;
305 	int i;
306 
307 	for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i) {
308 		res = usb_serial_generic_submit_read_urb(port, i, mem_flags);
309 		if (res)
310 			goto err;
311 	}
312 
313 	return 0;
314 err:
315 	for (; i >= 0; --i)
316 		usb_kill_urb(port->read_urbs[i]);
317 
318 	return res;
319 }
320 EXPORT_SYMBOL_GPL(usb_serial_generic_submit_read_urbs);
321 
322 void usb_serial_generic_process_read_urb(struct urb *urb)
323 {
324 	struct usb_serial_port *port = urb->context;
325 	struct tty_struct *tty;
326 	char *ch = (char *)urb->transfer_buffer;
327 	int i;
328 
329 	if (!urb->actual_length)
330 		return;
331 
332 	tty = tty_port_tty_get(&port->port);
333 	if (!tty)
334 		return;
335 
336 	/* The per character mucking around with sysrq path it too slow for
337 	   stuff like 3G modems, so shortcircuit it in the 99.9999999% of cases
338 	   where the USB serial is not a console anyway */
339 	if (!port->port.console || !port->sysrq)
340 		tty_insert_flip_string(tty, ch, urb->actual_length);
341 	else {
342 		for (i = 0; i < urb->actual_length; i++, ch++) {
343 			if (!usb_serial_handle_sysrq_char(port, *ch))
344 				tty_insert_flip_char(tty, *ch, TTY_NORMAL);
345 		}
346 	}
347 	tty_flip_buffer_push(tty);
348 	tty_kref_put(tty);
349 }
350 EXPORT_SYMBOL_GPL(usb_serial_generic_process_read_urb);
351 
352 void usb_serial_generic_read_bulk_callback(struct urb *urb)
353 {
354 	struct usb_serial_port *port = urb->context;
355 	unsigned char *data = urb->transfer_buffer;
356 	unsigned long flags;
357 	int i;
358 
359 	for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i) {
360 		if (urb == port->read_urbs[i])
361 			break;
362 	}
363 	set_bit(i, &port->read_urbs_free);
364 
365 	dev_dbg(&port->dev, "%s - port %d, urb %d, len %d\n",
366 		__func__, port->number, i, urb->actual_length);
367 
368 	if (urb->status) {
369 		dev_dbg(&port->dev, "%s - non-zero urb status: %d\n",
370 			__func__, urb->status);
371 		return;
372 	}
373 
374 	usb_serial_debug_data(debug, &port->dev, __func__,
375 						urb->actual_length, data);
376 	port->serial->type->process_read_urb(urb);
377 
378 	/* Throttle the device if requested by tty */
379 	spin_lock_irqsave(&port->lock, flags);
380 	port->throttled = port->throttle_req;
381 	if (!port->throttled) {
382 		spin_unlock_irqrestore(&port->lock, flags);
383 		usb_serial_generic_submit_read_urb(port, i, GFP_ATOMIC);
384 	} else
385 		spin_unlock_irqrestore(&port->lock, flags);
386 }
387 EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback);
388 
389 void usb_serial_generic_write_bulk_callback(struct urb *urb)
390 {
391 	unsigned long flags;
392 	struct usb_serial_port *port = urb->context;
393 	int status = urb->status;
394 	int i;
395 
396 	for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i)
397 		if (port->write_urbs[i] == urb)
398 			break;
399 
400 	spin_lock_irqsave(&port->lock, flags);
401 	port->tx_bytes -= urb->transfer_buffer_length;
402 	set_bit(i, &port->write_urbs_free);
403 	spin_unlock_irqrestore(&port->lock, flags);
404 
405 	if (status) {
406 		dev_dbg(&port->dev, "%s - non-zero urb status: %d\n",
407 			__func__, status);
408 
409 		spin_lock_irqsave(&port->lock, flags);
410 		kfifo_reset_out(&port->write_fifo);
411 		spin_unlock_irqrestore(&port->lock, flags);
412 	} else {
413 		usb_serial_generic_write_start(port);
414 	}
415 
416 	usb_serial_port_softint(port);
417 }
418 EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback);
419 
420 void usb_serial_generic_throttle(struct tty_struct *tty)
421 {
422 	struct usb_serial_port *port = tty->driver_data;
423 	unsigned long flags;
424 
425 	/* Set the throttle request flag. It will be picked up
426 	 * by usb_serial_generic_read_bulk_callback(). */
427 	spin_lock_irqsave(&port->lock, flags);
428 	port->throttle_req = 1;
429 	spin_unlock_irqrestore(&port->lock, flags);
430 }
431 EXPORT_SYMBOL_GPL(usb_serial_generic_throttle);
432 
433 void usb_serial_generic_unthrottle(struct tty_struct *tty)
434 {
435 	struct usb_serial_port *port = tty->driver_data;
436 	int was_throttled;
437 
438 	/* Clear the throttle flags */
439 	spin_lock_irq(&port->lock);
440 	was_throttled = port->throttled;
441 	port->throttled = port->throttle_req = 0;
442 	spin_unlock_irq(&port->lock);
443 
444 	if (was_throttled)
445 		usb_serial_generic_submit_read_urbs(port, GFP_KERNEL);
446 }
447 EXPORT_SYMBOL_GPL(usb_serial_generic_unthrottle);
448 
449 #ifdef CONFIG_MAGIC_SYSRQ
450 int usb_serial_handle_sysrq_char(struct usb_serial_port *port, unsigned int ch)
451 {
452 	if (port->sysrq && port->port.console) {
453 		if (ch && time_before(jiffies, port->sysrq)) {
454 			handle_sysrq(ch);
455 			port->sysrq = 0;
456 			return 1;
457 		}
458 		port->sysrq = 0;
459 	}
460 	return 0;
461 }
462 #else
463 int usb_serial_handle_sysrq_char(struct usb_serial_port *port, unsigned int ch)
464 {
465 	return 0;
466 }
467 #endif
468 EXPORT_SYMBOL_GPL(usb_serial_handle_sysrq_char);
469 
470 int usb_serial_handle_break(struct usb_serial_port *port)
471 {
472 	if (!port->sysrq) {
473 		port->sysrq = jiffies + HZ*5;
474 		return 1;
475 	}
476 	port->sysrq = 0;
477 	return 0;
478 }
479 EXPORT_SYMBOL_GPL(usb_serial_handle_break);
480 
481 /**
482  *	usb_serial_handle_dcd_change - handle a change of carrier detect state
483  *	@port: usb_serial_port structure for the open port
484  *	@tty: tty_struct structure for the port
485  *	@status: new carrier detect status, nonzero if active
486  */
487 void usb_serial_handle_dcd_change(struct usb_serial_port *usb_port,
488 				struct tty_struct *tty, unsigned int status)
489 {
490 	struct tty_port *port = &usb_port->port;
491 
492 	dev_dbg(&usb_port->dev, "%s - port %d, status %d\n", __func__,
493 		usb_port->number, status);
494 
495 	if (status)
496 		wake_up_interruptible(&port->open_wait);
497 	else if (tty && !C_CLOCAL(tty))
498 		tty_hangup(tty);
499 }
500 EXPORT_SYMBOL_GPL(usb_serial_handle_dcd_change);
501 
502 int usb_serial_generic_resume(struct usb_serial *serial)
503 {
504 	struct usb_serial_port *port;
505 	int i, c = 0, r;
506 
507 	for (i = 0; i < serial->num_ports; i++) {
508 		port = serial->port[i];
509 		if (!test_bit(ASYNCB_INITIALIZED, &port->port.flags))
510 			continue;
511 
512 		if (port->bulk_in_size) {
513 			r = usb_serial_generic_submit_read_urbs(port,
514 								GFP_NOIO);
515 			if (r < 0)
516 				c++;
517 		}
518 
519 		if (port->bulk_out_size) {
520 			r = usb_serial_generic_write_start(port);
521 			if (r < 0)
522 				c++;
523 		}
524 	}
525 
526 	return c ? -EIO : 0;
527 }
528 EXPORT_SYMBOL_GPL(usb_serial_generic_resume);
529 
530 void usb_serial_generic_disconnect(struct usb_serial *serial)
531 {
532 	int i;
533 
534 	/* stop reads and writes on all ports */
535 	for (i = 0; i < serial->num_ports; ++i)
536 		generic_cleanup(serial->port[i]);
537 }
538 EXPORT_SYMBOL_GPL(usb_serial_generic_disconnect);
539 
540 void usb_serial_generic_release(struct usb_serial *serial)
541 {
542 }
543