]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blob - drivers/usb/serial/usb-serial.c
headers: smp_lock.h redux
[mirror_ubuntu-hirsute-kernel.git] / drivers / usb / serial / usb-serial.c
1 /*
2 * USB Serial Converter driver
3 *
4 * Copyright (C) 1999 - 2005 Greg Kroah-Hartman (greg@kroah.com)
5 * Copyright (C) 2000 Peter Berger (pberger@brimson.com)
6 * Copyright (C) 2000 Al Borchers (borchers@steinerpoint.com)
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License version
10 * 2 as published by the Free Software Foundation.
11 *
12 * This driver was originally based on the ACM driver by Armin Fuerst (which was
13 * based on a driver by Brad Keryan)
14 *
15 * See Documentation/usb/usb-serial.txt for more information on using this
16 * driver
17 *
18 */
19
20 #include <linux/kernel.h>
21 #include <linux/errno.h>
22 #include <linux/init.h>
23 #include <linux/slab.h>
24 #include <linux/smp_lock.h>
25 #include <linux/tty.h>
26 #include <linux/tty_driver.h>
27 #include <linux/tty_flip.h>
28 #include <linux/module.h>
29 #include <linux/moduleparam.h>
30 #include <linux/seq_file.h>
31 #include <linux/spinlock.h>
32 #include <linux/mutex.h>
33 #include <linux/list.h>
34 #include <linux/uaccess.h>
35 #include <linux/usb.h>
36 #include <linux/usb/serial.h>
37 #include "pl2303.h"
38
39 /*
40 * Version Information
41 */
42 #define DRIVER_AUTHOR "Greg Kroah-Hartman, greg@kroah.com, http://www.kroah.com/linux/"
43 #define DRIVER_DESC "USB Serial Driver core"
44
45 static void port_free(struct usb_serial_port *port);
46
47 /* Driver structure we register with the USB core */
48 static struct usb_driver usb_serial_driver = {
49 .name = "usbserial",
50 .probe = usb_serial_probe,
51 .disconnect = usb_serial_disconnect,
52 .suspend = usb_serial_suspend,
53 .resume = usb_serial_resume,
54 .no_dynamic_id = 1,
55 };
56
57 /* There is no MODULE_DEVICE_TABLE for usbserial.c. Instead
58 the MODULE_DEVICE_TABLE declarations in each serial driver
59 cause the "hotplug" program to pull in whatever module is necessary
60 via modprobe, and modprobe will load usbserial because the serial
61 drivers depend on it.
62 */
63
64 static int debug;
65 /* initially all NULL */
66 static struct usb_serial *serial_table[SERIAL_TTY_MINORS];
67 static DEFINE_MUTEX(table_lock);
68 static LIST_HEAD(usb_serial_driver_list);
69
70 struct usb_serial *usb_serial_get_by_index(unsigned index)
71 {
72 struct usb_serial *serial;
73
74 mutex_lock(&table_lock);
75 serial = serial_table[index];
76
77 if (serial)
78 kref_get(&serial->kref);
79 mutex_unlock(&table_lock);
80 return serial;
81 }
82
83 static struct usb_serial *get_free_serial(struct usb_serial *serial,
84 int num_ports, unsigned int *minor)
85 {
86 unsigned int i, j;
87 int good_spot;
88
89 dbg("%s %d", __func__, num_ports);
90
91 *minor = 0;
92 mutex_lock(&table_lock);
93 for (i = 0; i < SERIAL_TTY_MINORS; ++i) {
94 if (serial_table[i])
95 continue;
96
97 good_spot = 1;
98 for (j = 1; j <= num_ports-1; ++j)
99 if ((i+j >= SERIAL_TTY_MINORS) || (serial_table[i+j])) {
100 good_spot = 0;
101 i += j;
102 break;
103 }
104 if (good_spot == 0)
105 continue;
106
107 *minor = i;
108 j = 0;
109 dbg("%s - minor base = %d", __func__, *minor);
110 for (i = *minor; (i < (*minor + num_ports)) && (i < SERIAL_TTY_MINORS); ++i) {
111 serial_table[i] = serial;
112 serial->port[j++]->number = i;
113 }
114 mutex_unlock(&table_lock);
115 return serial;
116 }
117 mutex_unlock(&table_lock);
118 return NULL;
119 }
120
121 static void return_serial(struct usb_serial *serial)
122 {
123 int i;
124
125 dbg("%s", __func__);
126
127 for (i = 0; i < serial->num_ports; ++i)
128 serial_table[serial->minor + i] = NULL;
129 }
130
131 static void destroy_serial(struct kref *kref)
132 {
133 struct usb_serial *serial;
134 struct usb_serial_port *port;
135 int i;
136
137 serial = to_usb_serial(kref);
138
139 dbg("%s - %s", __func__, serial->type->description);
140
141 /* return the minor range that this device had */
142 if (serial->minor != SERIAL_TTY_NO_MINOR)
143 return_serial(serial);
144
145 serial->type->release(serial);
146
147 for (i = 0; i < serial->num_ports; ++i) {
148 port = serial->port[i];
149 if (port)
150 put_device(&port->dev);
151 }
152
153 /* If this is a "fake" port, we have to clean it up here, as it will
154 * not get cleaned up in port_release() as it was never registered with
155 * the driver core */
156 if (serial->num_ports < serial->num_port_pointers) {
157 for (i = serial->num_ports;
158 i < serial->num_port_pointers; ++i) {
159 port = serial->port[i];
160 if (port)
161 port_free(port);
162 }
163 }
164
165 usb_put_dev(serial->dev);
166
167 /* free up any memory that we allocated */
168 kfree(serial);
169 }
170
171 void usb_serial_put(struct usb_serial *serial)
172 {
173 mutex_lock(&table_lock);
174 kref_put(&serial->kref, destroy_serial);
175 mutex_unlock(&table_lock);
176 }
177
178 /*****************************************************************************
179 * Driver tty interface functions
180 *****************************************************************************/
181 static int serial_open (struct tty_struct *tty, struct file *filp)
182 {
183 struct usb_serial *serial;
184 struct usb_serial_port *port;
185 unsigned int portNumber;
186 int retval = 0;
187
188 dbg("%s", __func__);
189
190 /* get the serial object associated with this tty pointer */
191 serial = usb_serial_get_by_index(tty->index);
192 if (!serial) {
193 tty->driver_data = NULL;
194 return -ENODEV;
195 }
196
197 mutex_lock(&serial->disc_mutex);
198 portNumber = tty->index - serial->minor;
199 port = serial->port[portNumber];
200 if (!port || serial->disconnected)
201 retval = -ENODEV;
202 else
203 get_device(&port->dev);
204 /*
205 * Note: Our locking order requirement does not allow port->mutex
206 * to be acquired while serial->disc_mutex is held.
207 */
208 mutex_unlock(&serial->disc_mutex);
209 if (retval)
210 goto bailout_serial_put;
211
212 if (mutex_lock_interruptible(&port->mutex)) {
213 retval = -ERESTARTSYS;
214 goto bailout_port_put;
215 }
216
217 ++port->port.count;
218
219 /* set up our port structure making the tty driver
220 * remember our port object, and us it */
221 tty->driver_data = port;
222 tty_port_tty_set(&port->port, tty);
223
224 if (port->port.count == 1) {
225
226 /* lock this module before we call it
227 * this may fail, which means we must bail out,
228 * safe because we are called with BKL held */
229 if (!try_module_get(serial->type->driver.owner)) {
230 retval = -ENODEV;
231 goto bailout_mutex_unlock;
232 }
233
234 mutex_lock(&serial->disc_mutex);
235 if (serial->disconnected)
236 retval = -ENODEV;
237 else
238 retval = usb_autopm_get_interface(serial->interface);
239 if (retval)
240 goto bailout_module_put;
241
242 /* only call the device specific open if this
243 * is the first time the port is opened */
244 retval = serial->type->open(tty, port, filp);
245 if (retval)
246 goto bailout_interface_put;
247 mutex_unlock(&serial->disc_mutex);
248 }
249 mutex_unlock(&port->mutex);
250 /* Now do the correct tty layer semantics */
251 retval = tty_port_block_til_ready(&port->port, tty, filp);
252 if (retval == 0)
253 return 0;
254
255 bailout_interface_put:
256 usb_autopm_put_interface(serial->interface);
257 bailout_module_put:
258 mutex_unlock(&serial->disc_mutex);
259 module_put(serial->type->driver.owner);
260 bailout_mutex_unlock:
261 port->port.count = 0;
262 tty->driver_data = NULL;
263 tty_port_tty_set(&port->port, NULL);
264 mutex_unlock(&port->mutex);
265 bailout_port_put:
266 put_device(&port->dev);
267 bailout_serial_put:
268 usb_serial_put(serial);
269 return retval;
270 }
271
272 /**
273 * serial_do_down - shut down hardware
274 * @port: port to shut down
275 *
276 * Shut down a USB port unless it is the console. We never shut down the
277 * console hardware as it will always be in use.
278 *
279 * Don't free any resources at this point
280 */
281 static void serial_do_down(struct usb_serial_port *port)
282 {
283 struct usb_serial_driver *drv = port->serial->type;
284 struct usb_serial *serial;
285 struct module *owner;
286
287 /* The console is magical, do not hang up the console hardware
288 or there will be tears */
289 if (port->console)
290 return;
291
292 mutex_lock(&port->mutex);
293 serial = port->serial;
294 owner = serial->type->driver.owner;
295
296 if (drv->close)
297 drv->close(port);
298
299 mutex_unlock(&port->mutex);
300 }
301
302 /**
303 * serial_do_free - free resources post close/hangup
304 * @port: port to free up
305 *
306 * Do the resource freeing and refcount dropping for the port. We must
307 * be careful about ordering and we must avoid freeing up the console.
308 */
309
310 static void serial_do_free(struct usb_serial_port *port)
311 {
312 struct usb_serial *serial;
313 struct module *owner;
314
315 /* The console is magical, do not hang up the console hardware
316 or there will be tears */
317 if (port->console)
318 return;
319
320 serial = port->serial;
321 owner = serial->type->driver.owner;
322 put_device(&port->dev);
323 /* Mustn't dereference port any more */
324 mutex_lock(&serial->disc_mutex);
325 if (!serial->disconnected)
326 usb_autopm_put_interface(serial->interface);
327 mutex_unlock(&serial->disc_mutex);
328 usb_serial_put(serial);
329 /* Mustn't dereference serial any more */
330 module_put(owner);
331 }
332
333 static void serial_close(struct tty_struct *tty, struct file *filp)
334 {
335 struct usb_serial_port *port = tty->driver_data;
336
337 if (!port)
338 return;
339
340 dbg("%s - port %d", __func__, port->number);
341
342
343 if (tty_port_close_start(&port->port, tty, filp) == 0)
344 return;
345
346 serial_do_down(port);
347 tty_port_close_end(&port->port, tty);
348 tty_port_tty_set(&port->port, NULL);
349 serial_do_free(port);
350 }
351
352 static void serial_hangup(struct tty_struct *tty)
353 {
354 struct usb_serial_port *port = tty->driver_data;
355 serial_do_down(port);
356 tty_port_hangup(&port->port);
357 serial_do_free(port);
358 }
359
360 static int serial_write(struct tty_struct *tty, const unsigned char *buf,
361 int count)
362 {
363 struct usb_serial_port *port = tty->driver_data;
364 int retval = -ENODEV;
365
366 if (port->serial->dev->state == USB_STATE_NOTATTACHED)
367 goto exit;
368
369 dbg("%s - port %d, %d byte(s)", __func__, port->number, count);
370
371 /* count is managed under the mutex lock for the tty so cannot
372 drop to zero until after the last close completes */
373 WARN_ON(!port->port.count);
374
375 /* pass on to the driver specific version of this function */
376 retval = port->serial->type->write(tty, port, buf, count);
377
378 exit:
379 return retval;
380 }
381
382 static int serial_write_room(struct tty_struct *tty)
383 {
384 struct usb_serial_port *port = tty->driver_data;
385 dbg("%s - port %d", __func__, port->number);
386 WARN_ON(!port->port.count);
387 /* pass on to the driver specific version of this function */
388 return port->serial->type->write_room(tty);
389 }
390
391 static int serial_chars_in_buffer(struct tty_struct *tty)
392 {
393 struct usb_serial_port *port = tty->driver_data;
394 dbg("%s = port %d", __func__, port->number);
395
396 WARN_ON(!port->port.count);
397 /* if the device was unplugged then any remaining characters
398 fell out of the connector ;) */
399 if (port->serial->disconnected)
400 return 0;
401 /* pass on to the driver specific version of this function */
402 return port->serial->type->chars_in_buffer(tty);
403 }
404
405 static void serial_throttle(struct tty_struct *tty)
406 {
407 struct usb_serial_port *port = tty->driver_data;
408 dbg("%s - port %d", __func__, port->number);
409
410 WARN_ON(!port->port.count);
411 /* pass on to the driver specific version of this function */
412 if (port->serial->type->throttle)
413 port->serial->type->throttle(tty);
414 }
415
416 static void serial_unthrottle(struct tty_struct *tty)
417 {
418 struct usb_serial_port *port = tty->driver_data;
419 dbg("%s - port %d", __func__, port->number);
420
421 WARN_ON(!port->port.count);
422 /* pass on to the driver specific version of this function */
423 if (port->serial->type->unthrottle)
424 port->serial->type->unthrottle(tty);
425 }
426
427 static int serial_ioctl(struct tty_struct *tty, struct file *file,
428 unsigned int cmd, unsigned long arg)
429 {
430 struct usb_serial_port *port = tty->driver_data;
431 int retval = -ENODEV;
432
433 dbg("%s - port %d, cmd 0x%.4x", __func__, port->number, cmd);
434
435 WARN_ON(!port->port.count);
436
437 /* pass on to the driver specific version of this function
438 if it is available */
439 if (port->serial->type->ioctl) {
440 retval = port->serial->type->ioctl(tty, file, cmd, arg);
441 } else
442 retval = -ENOIOCTLCMD;
443 return retval;
444 }
445
446 static void serial_set_termios(struct tty_struct *tty, struct ktermios *old)
447 {
448 struct usb_serial_port *port = tty->driver_data;
449 dbg("%s - port %d", __func__, port->number);
450
451 WARN_ON(!port->port.count);
452 /* pass on to the driver specific version of this function
453 if it is available */
454 if (port->serial->type->set_termios)
455 port->serial->type->set_termios(tty, port, old);
456 else
457 tty_termios_copy_hw(tty->termios, old);
458 }
459
460 static int serial_break(struct tty_struct *tty, int break_state)
461 {
462 struct usb_serial_port *port = tty->driver_data;
463
464 dbg("%s - port %d", __func__, port->number);
465
466 WARN_ON(!port->port.count);
467 /* pass on to the driver specific version of this function
468 if it is available */
469 if (port->serial->type->break_ctl)
470 port->serial->type->break_ctl(tty, break_state);
471 return 0;
472 }
473
474 static int serial_proc_show(struct seq_file *m, void *v)
475 {
476 struct usb_serial *serial;
477 int i;
478 char tmp[40];
479
480 dbg("%s", __func__);
481 seq_puts(m, "usbserinfo:1.0 driver:2.0\n");
482 for (i = 0; i < SERIAL_TTY_MINORS; ++i) {
483 serial = usb_serial_get_by_index(i);
484 if (serial == NULL)
485 continue;
486
487 seq_printf(m, "%d:", i);
488 if (serial->type->driver.owner)
489 seq_printf(m, " module:%s",
490 module_name(serial->type->driver.owner));
491 seq_printf(m, " name:\"%s\"",
492 serial->type->description);
493 seq_printf(m, " vendor:%04x product:%04x",
494 le16_to_cpu(serial->dev->descriptor.idVendor),
495 le16_to_cpu(serial->dev->descriptor.idProduct));
496 seq_printf(m, " num_ports:%d", serial->num_ports);
497 seq_printf(m, " port:%d", i - serial->minor + 1);
498 usb_make_path(serial->dev, tmp, sizeof(tmp));
499 seq_printf(m, " path:%s", tmp);
500
501 seq_putc(m, '\n');
502 usb_serial_put(serial);
503 }
504 return 0;
505 }
506
507 static int serial_proc_open(struct inode *inode, struct file *file)
508 {
509 return single_open(file, serial_proc_show, NULL);
510 }
511
512 static const struct file_operations serial_proc_fops = {
513 .owner = THIS_MODULE,
514 .open = serial_proc_open,
515 .read = seq_read,
516 .llseek = seq_lseek,
517 .release = single_release,
518 };
519
520 static int serial_tiocmget(struct tty_struct *tty, struct file *file)
521 {
522 struct usb_serial_port *port = tty->driver_data;
523
524 dbg("%s - port %d", __func__, port->number);
525
526 WARN_ON(!port->port.count);
527 if (port->serial->type->tiocmget)
528 return port->serial->type->tiocmget(tty, file);
529 return -EINVAL;
530 }
531
532 static int serial_tiocmset(struct tty_struct *tty, struct file *file,
533 unsigned int set, unsigned int clear)
534 {
535 struct usb_serial_port *port = tty->driver_data;
536
537 dbg("%s - port %d", __func__, port->number);
538
539 WARN_ON(!port->port.count);
540 if (port->serial->type->tiocmset)
541 return port->serial->type->tiocmset(tty, file, set, clear);
542 return -EINVAL;
543 }
544
545 /*
546 * We would be calling tty_wakeup here, but unfortunately some line
547 * disciplines have an annoying habit of calling tty->write from
548 * the write wakeup callback (e.g. n_hdlc.c).
549 */
550 void usb_serial_port_softint(struct usb_serial_port *port)
551 {
552 schedule_work(&port->work);
553 }
554 EXPORT_SYMBOL_GPL(usb_serial_port_softint);
555
556 static void usb_serial_port_work(struct work_struct *work)
557 {
558 struct usb_serial_port *port =
559 container_of(work, struct usb_serial_port, work);
560 struct tty_struct *tty;
561
562 dbg("%s - port %d", __func__, port->number);
563
564 tty = tty_port_tty_get(&port->port);
565 if (!tty)
566 return;
567
568 tty_wakeup(tty);
569 tty_kref_put(tty);
570 }
571
572 static void port_release(struct device *dev)
573 {
574 struct usb_serial_port *port = to_usb_serial_port(dev);
575
576 dbg ("%s - %s", __func__, dev_name(dev));
577 port_free(port);
578 }
579
580 static void kill_traffic(struct usb_serial_port *port)
581 {
582 usb_kill_urb(port->read_urb);
583 usb_kill_urb(port->write_urb);
584 /*
585 * This is tricky.
586 * Some drivers submit the read_urb in the
587 * handler for the write_urb or vice versa
588 * this order determines the order in which
589 * usb_kill_urb() must be used to reliably
590 * kill the URBs. As it is unknown here,
591 * both orders must be used in turn.
592 * The call below is not redundant.
593 */
594 usb_kill_urb(port->read_urb);
595 usb_kill_urb(port->interrupt_in_urb);
596 usb_kill_urb(port->interrupt_out_urb);
597 }
598
599 static void port_free(struct usb_serial_port *port)
600 {
601 /*
602 * Stop all the traffic before cancelling the work, so that
603 * nobody will restart it by calling usb_serial_port_softint.
604 */
605 kill_traffic(port);
606 cancel_work_sync(&port->work);
607
608 usb_free_urb(port->read_urb);
609 usb_free_urb(port->write_urb);
610 usb_free_urb(port->interrupt_in_urb);
611 usb_free_urb(port->interrupt_out_urb);
612 kfree(port->bulk_in_buffer);
613 kfree(port->bulk_out_buffer);
614 kfree(port->interrupt_in_buffer);
615 kfree(port->interrupt_out_buffer);
616 kfree(port);
617 }
618
619 static struct usb_serial *create_serial(struct usb_device *dev,
620 struct usb_interface *interface,
621 struct usb_serial_driver *driver)
622 {
623 struct usb_serial *serial;
624
625 serial = kzalloc(sizeof(*serial), GFP_KERNEL);
626 if (!serial) {
627 dev_err(&dev->dev, "%s - out of memory\n", __func__);
628 return NULL;
629 }
630 serial->dev = usb_get_dev(dev);
631 serial->type = driver;
632 serial->interface = interface;
633 kref_init(&serial->kref);
634 mutex_init(&serial->disc_mutex);
635 serial->minor = SERIAL_TTY_NO_MINOR;
636
637 return serial;
638 }
639
640 static const struct usb_device_id *match_dynamic_id(struct usb_interface *intf,
641 struct usb_serial_driver *drv)
642 {
643 struct usb_dynid *dynid;
644
645 spin_lock(&drv->dynids.lock);
646 list_for_each_entry(dynid, &drv->dynids.list, node) {
647 if (usb_match_one_id(intf, &dynid->id)) {
648 spin_unlock(&drv->dynids.lock);
649 return &dynid->id;
650 }
651 }
652 spin_unlock(&drv->dynids.lock);
653 return NULL;
654 }
655
656 static const struct usb_device_id *get_iface_id(struct usb_serial_driver *drv,
657 struct usb_interface *intf)
658 {
659 const struct usb_device_id *id;
660
661 id = usb_match_id(intf, drv->id_table);
662 if (id) {
663 dbg("static descriptor matches");
664 goto exit;
665 }
666 id = match_dynamic_id(intf, drv);
667 if (id)
668 dbg("dynamic descriptor matches");
669 exit:
670 return id;
671 }
672
673 static struct usb_serial_driver *search_serial_device(
674 struct usb_interface *iface)
675 {
676 const struct usb_device_id *id;
677 struct usb_serial_driver *drv;
678
679 /* Check if the usb id matches a known device */
680 list_for_each_entry(drv, &usb_serial_driver_list, driver_list) {
681 id = get_iface_id(drv, iface);
682 if (id)
683 return drv;
684 }
685
686 return NULL;
687 }
688
689 static int serial_carrier_raised(struct tty_port *port)
690 {
691 struct usb_serial_port *p = container_of(port, struct usb_serial_port, port);
692 struct usb_serial_driver *drv = p->serial->type;
693 if (drv->carrier_raised)
694 return drv->carrier_raised(p);
695 /* No carrier control - don't block */
696 return 1;
697 }
698
699 static void serial_dtr_rts(struct tty_port *port, int on)
700 {
701 struct usb_serial_port *p = container_of(port, struct usb_serial_port, port);
702 struct usb_serial_driver *drv = p->serial->type;
703 if (drv->dtr_rts)
704 drv->dtr_rts(p, on);
705 }
706
707 static const struct tty_port_operations serial_port_ops = {
708 .carrier_raised = serial_carrier_raised,
709 .dtr_rts = serial_dtr_rts,
710 };
711
712 int usb_serial_probe(struct usb_interface *interface,
713 const struct usb_device_id *id)
714 {
715 struct usb_device *dev = interface_to_usbdev(interface);
716 struct usb_serial *serial = NULL;
717 struct usb_serial_port *port;
718 struct usb_host_interface *iface_desc;
719 struct usb_endpoint_descriptor *endpoint;
720 struct usb_endpoint_descriptor *interrupt_in_endpoint[MAX_NUM_PORTS];
721 struct usb_endpoint_descriptor *interrupt_out_endpoint[MAX_NUM_PORTS];
722 struct usb_endpoint_descriptor *bulk_in_endpoint[MAX_NUM_PORTS];
723 struct usb_endpoint_descriptor *bulk_out_endpoint[MAX_NUM_PORTS];
724 struct usb_serial_driver *type = NULL;
725 int retval;
726 unsigned int minor;
727 int buffer_size;
728 int i;
729 int num_interrupt_in = 0;
730 int num_interrupt_out = 0;
731 int num_bulk_in = 0;
732 int num_bulk_out = 0;
733 int num_ports = 0;
734 int max_endpoints;
735
736 lock_kernel(); /* guard against unloading a serial driver module */
737 type = search_serial_device(interface);
738 if (!type) {
739 unlock_kernel();
740 dbg("none matched");
741 return -ENODEV;
742 }
743
744 serial = create_serial(dev, interface, type);
745 if (!serial) {
746 unlock_kernel();
747 dev_err(&interface->dev, "%s - out of memory\n", __func__);
748 return -ENOMEM;
749 }
750
751 /* if this device type has a probe function, call it */
752 if (type->probe) {
753 const struct usb_device_id *id;
754
755 if (!try_module_get(type->driver.owner)) {
756 unlock_kernel();
757 dev_err(&interface->dev,
758 "module get failed, exiting\n");
759 kfree(serial);
760 return -EIO;
761 }
762
763 id = get_iface_id(type, interface);
764 retval = type->probe(serial, id);
765 module_put(type->driver.owner);
766
767 if (retval) {
768 unlock_kernel();
769 dbg("sub driver rejected device");
770 kfree(serial);
771 return retval;
772 }
773 }
774
775 /* descriptor matches, let's find the endpoints needed */
776 /* check out the endpoints */
777 iface_desc = interface->cur_altsetting;
778 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
779 endpoint = &iface_desc->endpoint[i].desc;
780
781 if (usb_endpoint_is_bulk_in(endpoint)) {
782 /* we found a bulk in endpoint */
783 dbg("found bulk in on endpoint %d", i);
784 bulk_in_endpoint[num_bulk_in] = endpoint;
785 ++num_bulk_in;
786 }
787
788 if (usb_endpoint_is_bulk_out(endpoint)) {
789 /* we found a bulk out endpoint */
790 dbg("found bulk out on endpoint %d", i);
791 bulk_out_endpoint[num_bulk_out] = endpoint;
792 ++num_bulk_out;
793 }
794
795 if (usb_endpoint_is_int_in(endpoint)) {
796 /* we found a interrupt in endpoint */
797 dbg("found interrupt in on endpoint %d", i);
798 interrupt_in_endpoint[num_interrupt_in] = endpoint;
799 ++num_interrupt_in;
800 }
801
802 if (usb_endpoint_is_int_out(endpoint)) {
803 /* we found an interrupt out endpoint */
804 dbg("found interrupt out on endpoint %d", i);
805 interrupt_out_endpoint[num_interrupt_out] = endpoint;
806 ++num_interrupt_out;
807 }
808 }
809
810 #if defined(CONFIG_USB_SERIAL_PL2303) || defined(CONFIG_USB_SERIAL_PL2303_MODULE)
811 /* BEGIN HORRIBLE HACK FOR PL2303 */
812 /* this is needed due to the looney way its endpoints are set up */
813 if (((le16_to_cpu(dev->descriptor.idVendor) == PL2303_VENDOR_ID) &&
814 (le16_to_cpu(dev->descriptor.idProduct) == PL2303_PRODUCT_ID)) ||
815 ((le16_to_cpu(dev->descriptor.idVendor) == ATEN_VENDOR_ID) &&
816 (le16_to_cpu(dev->descriptor.idProduct) == ATEN_PRODUCT_ID)) ||
817 ((le16_to_cpu(dev->descriptor.idVendor) == ALCOR_VENDOR_ID) &&
818 (le16_to_cpu(dev->descriptor.idProduct) == ALCOR_PRODUCT_ID)) ||
819 ((le16_to_cpu(dev->descriptor.idVendor) == SIEMENS_VENDOR_ID) &&
820 (le16_to_cpu(dev->descriptor.idProduct) == SIEMENS_PRODUCT_ID_EF81))) {
821 if (interface != dev->actconfig->interface[0]) {
822 /* check out the endpoints of the other interface*/
823 iface_desc = dev->actconfig->interface[0]->cur_altsetting;
824 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
825 endpoint = &iface_desc->endpoint[i].desc;
826 if (usb_endpoint_is_int_in(endpoint)) {
827 /* we found a interrupt in endpoint */
828 dbg("found interrupt in for Prolific device on separate interface");
829 interrupt_in_endpoint[num_interrupt_in] = endpoint;
830 ++num_interrupt_in;
831 }
832 }
833 }
834
835 /* Now make sure the PL-2303 is configured correctly.
836 * If not, give up now and hope this hack will work
837 * properly during a later invocation of usb_serial_probe
838 */
839 if (num_bulk_in == 0 || num_bulk_out == 0) {
840 unlock_kernel();
841 dev_info(&interface->dev, "PL-2303 hack: descriptors matched but endpoints did not\n");
842 kfree(serial);
843 return -ENODEV;
844 }
845 }
846 /* END HORRIBLE HACK FOR PL2303 */
847 #endif
848
849 #ifdef CONFIG_USB_SERIAL_GENERIC
850 if (type == &usb_serial_generic_device) {
851 num_ports = num_bulk_out;
852 if (num_ports == 0) {
853 unlock_kernel();
854 dev_err(&interface->dev,
855 "Generic device with no bulk out, not allowed.\n");
856 kfree(serial);
857 return -EIO;
858 }
859 }
860 #endif
861 if (!num_ports) {
862 /* if this device type has a calc_num_ports function, call it */
863 if (type->calc_num_ports) {
864 if (!try_module_get(type->driver.owner)) {
865 unlock_kernel();
866 dev_err(&interface->dev,
867 "module get failed, exiting\n");
868 kfree(serial);
869 return -EIO;
870 }
871 num_ports = type->calc_num_ports(serial);
872 module_put(type->driver.owner);
873 }
874 if (!num_ports)
875 num_ports = type->num_ports;
876 }
877
878 serial->num_ports = num_ports;
879 serial->num_bulk_in = num_bulk_in;
880 serial->num_bulk_out = num_bulk_out;
881 serial->num_interrupt_in = num_interrupt_in;
882 serial->num_interrupt_out = num_interrupt_out;
883
884 /* found all that we need */
885 dev_info(&interface->dev, "%s converter detected\n",
886 type->description);
887
888 /* create our ports, we need as many as the max endpoints */
889 /* we don't use num_ports here because some devices have more
890 endpoint pairs than ports */
891 max_endpoints = max(num_bulk_in, num_bulk_out);
892 max_endpoints = max(max_endpoints, num_interrupt_in);
893 max_endpoints = max(max_endpoints, num_interrupt_out);
894 max_endpoints = max(max_endpoints, (int)serial->num_ports);
895 serial->num_port_pointers = max_endpoints;
896 unlock_kernel();
897
898 dbg("%s - setting up %d port structures for this device",
899 __func__, max_endpoints);
900 for (i = 0; i < max_endpoints; ++i) {
901 port = kzalloc(sizeof(struct usb_serial_port), GFP_KERNEL);
902 if (!port)
903 goto probe_error;
904 tty_port_init(&port->port);
905 port->port.ops = &serial_port_ops;
906 port->serial = serial;
907 spin_lock_init(&port->lock);
908 mutex_init(&port->mutex);
909 INIT_WORK(&port->work, usb_serial_port_work);
910 serial->port[i] = port;
911 }
912
913 /* set up the endpoint information */
914 for (i = 0; i < num_bulk_in; ++i) {
915 endpoint = bulk_in_endpoint[i];
916 port = serial->port[i];
917 port->read_urb = usb_alloc_urb(0, GFP_KERNEL);
918 if (!port->read_urb) {
919 dev_err(&interface->dev, "No free urbs available\n");
920 goto probe_error;
921 }
922 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
923 port->bulk_in_size = buffer_size;
924 port->bulk_in_endpointAddress = endpoint->bEndpointAddress;
925 port->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
926 if (!port->bulk_in_buffer) {
927 dev_err(&interface->dev,
928 "Couldn't allocate bulk_in_buffer\n");
929 goto probe_error;
930 }
931 usb_fill_bulk_urb(port->read_urb, dev,
932 usb_rcvbulkpipe(dev,
933 endpoint->bEndpointAddress),
934 port->bulk_in_buffer, buffer_size,
935 serial->type->read_bulk_callback, port);
936 }
937
938 for (i = 0; i < num_bulk_out; ++i) {
939 endpoint = bulk_out_endpoint[i];
940 port = serial->port[i];
941 port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
942 if (!port->write_urb) {
943 dev_err(&interface->dev, "No free urbs available\n");
944 goto probe_error;
945 }
946 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
947 port->bulk_out_size = buffer_size;
948 port->bulk_out_endpointAddress = endpoint->bEndpointAddress;
949 port->bulk_out_buffer = kmalloc(buffer_size, GFP_KERNEL);
950 if (!port->bulk_out_buffer) {
951 dev_err(&interface->dev,
952 "Couldn't allocate bulk_out_buffer\n");
953 goto probe_error;
954 }
955 usb_fill_bulk_urb(port->write_urb, dev,
956 usb_sndbulkpipe(dev,
957 endpoint->bEndpointAddress),
958 port->bulk_out_buffer, buffer_size,
959 serial->type->write_bulk_callback, port);
960 }
961
962 if (serial->type->read_int_callback) {
963 for (i = 0; i < num_interrupt_in; ++i) {
964 endpoint = interrupt_in_endpoint[i];
965 port = serial->port[i];
966 port->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
967 if (!port->interrupt_in_urb) {
968 dev_err(&interface->dev,
969 "No free urbs available\n");
970 goto probe_error;
971 }
972 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
973 port->interrupt_in_endpointAddress =
974 endpoint->bEndpointAddress;
975 port->interrupt_in_buffer = kmalloc(buffer_size,
976 GFP_KERNEL);
977 if (!port->interrupt_in_buffer) {
978 dev_err(&interface->dev,
979 "Couldn't allocate interrupt_in_buffer\n");
980 goto probe_error;
981 }
982 usb_fill_int_urb(port->interrupt_in_urb, dev,
983 usb_rcvintpipe(dev,
984 endpoint->bEndpointAddress),
985 port->interrupt_in_buffer, buffer_size,
986 serial->type->read_int_callback, port,
987 endpoint->bInterval);
988 }
989 } else if (num_interrupt_in) {
990 dbg("the device claims to support interrupt in transfers, but read_int_callback is not defined");
991 }
992
993 if (serial->type->write_int_callback) {
994 for (i = 0; i < num_interrupt_out; ++i) {
995 endpoint = interrupt_out_endpoint[i];
996 port = serial->port[i];
997 port->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
998 if (!port->interrupt_out_urb) {
999 dev_err(&interface->dev,
1000 "No free urbs available\n");
1001 goto probe_error;
1002 }
1003 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
1004 port->interrupt_out_size = buffer_size;
1005 port->interrupt_out_endpointAddress =
1006 endpoint->bEndpointAddress;
1007 port->interrupt_out_buffer = kmalloc(buffer_size,
1008 GFP_KERNEL);
1009 if (!port->interrupt_out_buffer) {
1010 dev_err(&interface->dev,
1011 "Couldn't allocate interrupt_out_buffer\n");
1012 goto probe_error;
1013 }
1014 usb_fill_int_urb(port->interrupt_out_urb, dev,
1015 usb_sndintpipe(dev,
1016 endpoint->bEndpointAddress),
1017 port->interrupt_out_buffer, buffer_size,
1018 serial->type->write_int_callback, port,
1019 endpoint->bInterval);
1020 }
1021 } else if (num_interrupt_out) {
1022 dbg("the device claims to support interrupt out transfers, but write_int_callback is not defined");
1023 }
1024
1025 /* if this device type has an attach function, call it */
1026 if (type->attach) {
1027 if (!try_module_get(type->driver.owner)) {
1028 dev_err(&interface->dev,
1029 "module get failed, exiting\n");
1030 goto probe_error;
1031 }
1032 retval = type->attach(serial);
1033 module_put(type->driver.owner);
1034 if (retval < 0)
1035 goto probe_error;
1036 if (retval > 0) {
1037 /* quietly accept this device, but don't bind to a
1038 serial port as it's about to disappear */
1039 serial->num_ports = 0;
1040 goto exit;
1041 }
1042 }
1043
1044 if (get_free_serial(serial, num_ports, &minor) == NULL) {
1045 dev_err(&interface->dev, "No more free serial devices\n");
1046 goto probe_error;
1047 }
1048 serial->minor = minor;
1049
1050 /* register all of the individual ports with the driver core */
1051 for (i = 0; i < num_ports; ++i) {
1052 port = serial->port[i];
1053 port->dev.parent = &interface->dev;
1054 port->dev.driver = NULL;
1055 port->dev.bus = &usb_serial_bus_type;
1056 port->dev.release = &port_release;
1057
1058 dev_set_name(&port->dev, "ttyUSB%d", port->number);
1059 dbg ("%s - registering %s", __func__, dev_name(&port->dev));
1060 port->dev_state = PORT_REGISTERING;
1061 retval = device_register(&port->dev);
1062 if (retval) {
1063 dev_err(&port->dev, "Error registering port device, "
1064 "continuing\n");
1065 port->dev_state = PORT_UNREGISTERED;
1066 } else {
1067 port->dev_state = PORT_REGISTERED;
1068 }
1069 }
1070
1071 usb_serial_console_init(debug, minor);
1072
1073 exit:
1074 /* success */
1075 usb_set_intfdata(interface, serial);
1076 return 0;
1077
1078 probe_error:
1079 for (i = 0; i < num_bulk_in; ++i) {
1080 port = serial->port[i];
1081 if (!port)
1082 continue;
1083 usb_free_urb(port->read_urb);
1084 kfree(port->bulk_in_buffer);
1085 }
1086 for (i = 0; i < num_bulk_out; ++i) {
1087 port = serial->port[i];
1088 if (!port)
1089 continue;
1090 usb_free_urb(port->write_urb);
1091 kfree(port->bulk_out_buffer);
1092 }
1093 for (i = 0; i < num_interrupt_in; ++i) {
1094 port = serial->port[i];
1095 if (!port)
1096 continue;
1097 usb_free_urb(port->interrupt_in_urb);
1098 kfree(port->interrupt_in_buffer);
1099 }
1100 for (i = 0; i < num_interrupt_out; ++i) {
1101 port = serial->port[i];
1102 if (!port)
1103 continue;
1104 usb_free_urb(port->interrupt_out_urb);
1105 kfree(port->interrupt_out_buffer);
1106 }
1107
1108 /* free up any memory that we allocated */
1109 for (i = 0; i < serial->num_port_pointers; ++i)
1110 kfree(serial->port[i]);
1111 kfree(serial);
1112 return -EIO;
1113 }
1114 EXPORT_SYMBOL_GPL(usb_serial_probe);
1115
1116 void usb_serial_disconnect(struct usb_interface *interface)
1117 {
1118 int i;
1119 struct usb_serial *serial = usb_get_intfdata(interface);
1120 struct device *dev = &interface->dev;
1121 struct usb_serial_port *port;
1122
1123 usb_serial_console_disconnect(serial);
1124 dbg("%s", __func__);
1125
1126 mutex_lock(&serial->disc_mutex);
1127 usb_set_intfdata(interface, NULL);
1128 /* must set a flag, to signal subdrivers */
1129 serial->disconnected = 1;
1130 mutex_unlock(&serial->disc_mutex);
1131
1132 for (i = 0; i < serial->num_ports; ++i) {
1133 port = serial->port[i];
1134 if (port) {
1135 struct tty_struct *tty = tty_port_tty_get(&port->port);
1136 if (tty) {
1137 /* The hangup will occur asynchronously but
1138 the object refcounts will sort out all the
1139 cleanup */
1140 tty_hangup(tty);
1141 tty_kref_put(tty);
1142 }
1143 kill_traffic(port);
1144 cancel_work_sync(&port->work);
1145 if (port->dev_state == PORT_REGISTERED) {
1146
1147 /* Make sure the port is bound so that the
1148 * driver's port_remove method is called.
1149 */
1150 if (!port->dev.driver) {
1151 int rc;
1152
1153 port->dev.driver =
1154 &serial->type->driver;
1155 rc = device_bind_driver(&port->dev);
1156 }
1157 port->dev_state = PORT_UNREGISTERING;
1158 device_del(&port->dev);
1159 port->dev_state = PORT_UNREGISTERED;
1160 }
1161 }
1162 }
1163 serial->type->disconnect(serial);
1164
1165 /* let the last holder of this object
1166 * cause it to be cleaned up */
1167 usb_serial_put(serial);
1168 dev_info(dev, "device disconnected\n");
1169 }
1170 EXPORT_SYMBOL_GPL(usb_serial_disconnect);
1171
1172 int usb_serial_suspend(struct usb_interface *intf, pm_message_t message)
1173 {
1174 struct usb_serial *serial = usb_get_intfdata(intf);
1175 struct usb_serial_port *port;
1176 int i, r = 0;
1177
1178 serial->suspending = 1;
1179
1180 for (i = 0; i < serial->num_ports; ++i) {
1181 port = serial->port[i];
1182 if (port)
1183 kill_traffic(port);
1184 }
1185
1186 if (serial->type->suspend)
1187 r = serial->type->suspend(serial, message);
1188
1189 return r;
1190 }
1191 EXPORT_SYMBOL(usb_serial_suspend);
1192
1193 int usb_serial_resume(struct usb_interface *intf)
1194 {
1195 struct usb_serial *serial = usb_get_intfdata(intf);
1196 int rv;
1197
1198 serial->suspending = 0;
1199 if (serial->type->resume)
1200 rv = serial->type->resume(serial);
1201 else
1202 rv = usb_serial_generic_resume(serial);
1203
1204 return rv;
1205 }
1206 EXPORT_SYMBOL(usb_serial_resume);
1207
1208 static const struct tty_operations serial_ops = {
1209 .open = serial_open,
1210 .close = serial_close,
1211 .write = serial_write,
1212 .hangup = serial_hangup,
1213 .write_room = serial_write_room,
1214 .ioctl = serial_ioctl,
1215 .set_termios = serial_set_termios,
1216 .throttle = serial_throttle,
1217 .unthrottle = serial_unthrottle,
1218 .break_ctl = serial_break,
1219 .chars_in_buffer = serial_chars_in_buffer,
1220 .tiocmget = serial_tiocmget,
1221 .tiocmset = serial_tiocmset,
1222 .proc_fops = &serial_proc_fops,
1223 };
1224
1225
1226 struct tty_driver *usb_serial_tty_driver;
1227
1228 static int __init usb_serial_init(void)
1229 {
1230 int i;
1231 int result;
1232
1233 usb_serial_tty_driver = alloc_tty_driver(SERIAL_TTY_MINORS);
1234 if (!usb_serial_tty_driver)
1235 return -ENOMEM;
1236
1237 /* Initialize our global data */
1238 for (i = 0; i < SERIAL_TTY_MINORS; ++i)
1239 serial_table[i] = NULL;
1240
1241 result = bus_register(&usb_serial_bus_type);
1242 if (result) {
1243 printk(KERN_ERR "usb-serial: %s - registering bus driver "
1244 "failed\n", __func__);
1245 goto exit_bus;
1246 }
1247
1248 usb_serial_tty_driver->owner = THIS_MODULE;
1249 usb_serial_tty_driver->driver_name = "usbserial";
1250 usb_serial_tty_driver->name = "ttyUSB";
1251 usb_serial_tty_driver->major = SERIAL_TTY_MAJOR;
1252 usb_serial_tty_driver->minor_start = 0;
1253 usb_serial_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1254 usb_serial_tty_driver->subtype = SERIAL_TYPE_NORMAL;
1255 usb_serial_tty_driver->flags = TTY_DRIVER_REAL_RAW |
1256 TTY_DRIVER_DYNAMIC_DEV;
1257 usb_serial_tty_driver->init_termios = tty_std_termios;
1258 usb_serial_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD
1259 | HUPCL | CLOCAL;
1260 usb_serial_tty_driver->init_termios.c_ispeed = 9600;
1261 usb_serial_tty_driver->init_termios.c_ospeed = 9600;
1262 tty_set_operations(usb_serial_tty_driver, &serial_ops);
1263 result = tty_register_driver(usb_serial_tty_driver);
1264 if (result) {
1265 printk(KERN_ERR "usb-serial: %s - tty_register_driver failed\n",
1266 __func__);
1267 goto exit_reg_driver;
1268 }
1269
1270 /* register the USB driver */
1271 result = usb_register(&usb_serial_driver);
1272 if (result < 0) {
1273 printk(KERN_ERR "usb-serial: %s - usb_register failed\n",
1274 __func__);
1275 goto exit_tty;
1276 }
1277
1278 /* register the generic driver, if we should */
1279 result = usb_serial_generic_register(debug);
1280 if (result < 0) {
1281 printk(KERN_ERR "usb-serial: %s - registering generic "
1282 "driver failed\n", __func__);
1283 goto exit_generic;
1284 }
1285
1286 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n");
1287
1288 return result;
1289
1290 exit_generic:
1291 usb_deregister(&usb_serial_driver);
1292
1293 exit_tty:
1294 tty_unregister_driver(usb_serial_tty_driver);
1295
1296 exit_reg_driver:
1297 bus_unregister(&usb_serial_bus_type);
1298
1299 exit_bus:
1300 printk(KERN_ERR "usb-serial: %s - returning with error %d\n",
1301 __func__, result);
1302 put_tty_driver(usb_serial_tty_driver);
1303 return result;
1304 }
1305
1306
1307 static void __exit usb_serial_exit(void)
1308 {
1309 usb_serial_console_exit();
1310
1311 usb_serial_generic_deregister();
1312
1313 usb_deregister(&usb_serial_driver);
1314 tty_unregister_driver(usb_serial_tty_driver);
1315 put_tty_driver(usb_serial_tty_driver);
1316 bus_unregister(&usb_serial_bus_type);
1317 }
1318
1319
1320 module_init(usb_serial_init);
1321 module_exit(usb_serial_exit);
1322
1323 #define set_to_generic_if_null(type, function) \
1324 do { \
1325 if (!type->function) { \
1326 type->function = usb_serial_generic_##function; \
1327 dbg("Had to override the " #function \
1328 " usb serial operation with the generic one.");\
1329 } \
1330 } while (0)
1331
1332 static void fixup_generic(struct usb_serial_driver *device)
1333 {
1334 set_to_generic_if_null(device, open);
1335 set_to_generic_if_null(device, write);
1336 set_to_generic_if_null(device, close);
1337 set_to_generic_if_null(device, write_room);
1338 set_to_generic_if_null(device, chars_in_buffer);
1339 set_to_generic_if_null(device, read_bulk_callback);
1340 set_to_generic_if_null(device, write_bulk_callback);
1341 set_to_generic_if_null(device, disconnect);
1342 set_to_generic_if_null(device, release);
1343 }
1344
1345 int usb_serial_register(struct usb_serial_driver *driver)
1346 {
1347 /* must be called with BKL held */
1348 int retval;
1349
1350 if (usb_disabled())
1351 return -ENODEV;
1352
1353 fixup_generic(driver);
1354
1355 if (!driver->description)
1356 driver->description = driver->driver.name;
1357
1358 /* Add this device to our list of devices */
1359 list_add(&driver->driver_list, &usb_serial_driver_list);
1360
1361 retval = usb_serial_bus_register(driver);
1362 if (retval) {
1363 printk(KERN_ERR "usb-serial: problem %d when registering "
1364 "driver %s\n", retval, driver->description);
1365 list_del(&driver->driver_list);
1366 } else
1367 printk(KERN_INFO "USB Serial support registered for %s\n",
1368 driver->description);
1369
1370 return retval;
1371 }
1372 EXPORT_SYMBOL_GPL(usb_serial_register);
1373
1374
1375 void usb_serial_deregister(struct usb_serial_driver *device)
1376 {
1377 /* must be called with BKL held */
1378 printk(KERN_INFO "USB Serial deregistering driver %s\n",
1379 device->description);
1380 list_del(&device->driver_list);
1381 usb_serial_bus_deregister(device);
1382 }
1383 EXPORT_SYMBOL_GPL(usb_serial_deregister);
1384
1385 /* Module information */
1386 MODULE_AUTHOR(DRIVER_AUTHOR);
1387 MODULE_DESCRIPTION(DRIVER_DESC);
1388 MODULE_LICENSE("GPL");
1389
1390 module_param(debug, bool, S_IRUGO | S_IWUSR);
1391 MODULE_PARM_DESC(debug, "Debug enabled or not");