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