[PATCH] USB: remove .owner field from struct usb_driver
[linux-2.6/verdex.git] / drivers / usb / serial / usb-serial.c
blob12aaf18ff9ea8031f8d065dc2eeb82bdfcfebe95
1 /*
2 * USB Serial Converter driver
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)
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.
12 * This driver was originally based on the ACM driver by Armin Fuerst (which was
13 * based on a driver by Brad Keryan)
15 * See Documentation/usb/usb-serial.txt for more information on using this driver
19 #include <linux/config.h>
20 #include <linux/kernel.h>
21 #include <linux/errno.h>
22 #include <linux/init.h>
23 #include <linux/slab.h>
24 #include <linux/tty.h>
25 #include <linux/tty_driver.h>
26 #include <linux/tty_flip.h>
27 #include <linux/module.h>
28 #include <linux/moduleparam.h>
29 #include <linux/spinlock.h>
30 #include <linux/list.h>
31 #include <linux/smp_lock.h>
32 #include <asm/uaccess.h>
33 #include <linux/usb.h>
34 #include "usb-serial.h"
35 #include "pl2303.h"
38 * Version Information
40 #define DRIVER_AUTHOR "Greg Kroah-Hartman, greg@kroah.com, http://www.kroah.com/linux/"
41 #define DRIVER_DESC "USB Serial Driver core"
43 /* Driver structure we register with the USB core */
44 static struct usb_driver usb_serial_driver = {
45 .name = "usbserial",
46 .probe = usb_serial_probe,
47 .disconnect = usb_serial_disconnect,
48 .no_dynamic_id = 1,
51 /* There is no MODULE_DEVICE_TABLE for usbserial.c. Instead
52 the MODULE_DEVICE_TABLE declarations in each serial driver
53 cause the "hotplug" program to pull in whatever module is necessary
54 via modprobe, and modprobe will load usbserial because the serial
55 drivers depend on it.
58 static int debug;
59 static struct usb_serial *serial_table[SERIAL_TTY_MINORS]; /* initially all NULL */
60 static LIST_HEAD(usb_serial_driver_list);
62 struct usb_serial *usb_serial_get_by_index(unsigned index)
64 struct usb_serial *serial = serial_table[index];
66 if (serial)
67 kref_get(&serial->kref);
68 return serial;
71 static struct usb_serial *get_free_serial (struct usb_serial *serial, int num_ports, unsigned int *minor)
73 unsigned int i, j;
74 int good_spot;
76 dbg("%s %d", __FUNCTION__, num_ports);
78 *minor = 0;
79 for (i = 0; i < SERIAL_TTY_MINORS; ++i) {
80 if (serial_table[i])
81 continue;
83 good_spot = 1;
84 for (j = 1; j <= num_ports-1; ++j)
85 if ((i+j >= SERIAL_TTY_MINORS) || (serial_table[i+j])) {
86 good_spot = 0;
87 i += j;
88 break;
90 if (good_spot == 0)
91 continue;
93 *minor = i;
94 dbg("%s - minor base = %d", __FUNCTION__, *minor);
95 for (i = *minor; (i < (*minor + num_ports)) && (i < SERIAL_TTY_MINORS); ++i)
96 serial_table[i] = serial;
97 return serial;
99 return NULL;
102 static void return_serial(struct usb_serial *serial)
104 int i;
106 dbg("%s", __FUNCTION__);
108 if (serial == NULL)
109 return;
111 for (i = 0; i < serial->num_ports; ++i) {
112 serial_table[serial->minor + i] = NULL;
116 static void destroy_serial(struct kref *kref)
118 struct usb_serial *serial;
119 struct usb_serial_port *port;
120 int i;
122 serial = to_usb_serial(kref);
124 dbg("%s - %s", __FUNCTION__, serial->type->description);
126 serial->type->shutdown(serial);
128 /* return the minor range that this device had */
129 return_serial(serial);
131 for (i = 0; i < serial->num_ports; ++i)
132 serial->port[i]->open_count = 0;
134 /* the ports are cleaned up and released in port_release() */
135 for (i = 0; i < serial->num_ports; ++i)
136 if (serial->port[i]->dev.parent != NULL) {
137 device_unregister(&serial->port[i]->dev);
138 serial->port[i] = NULL;
141 /* If this is a "fake" port, we have to clean it up here, as it will
142 * not get cleaned up in port_release() as it was never registered with
143 * the driver core */
144 if (serial->num_ports < serial->num_port_pointers) {
145 for (i = serial->num_ports; i < serial->num_port_pointers; ++i) {
146 port = serial->port[i];
147 if (!port)
148 continue;
149 usb_kill_urb(port->read_urb);
150 usb_free_urb(port->read_urb);
151 usb_kill_urb(port->write_urb);
152 usb_free_urb(port->write_urb);
153 usb_kill_urb(port->interrupt_in_urb);
154 usb_free_urb(port->interrupt_in_urb);
155 usb_kill_urb(port->interrupt_out_urb);
156 usb_free_urb(port->interrupt_out_urb);
157 kfree(port->bulk_in_buffer);
158 kfree(port->bulk_out_buffer);
159 kfree(port->interrupt_in_buffer);
160 kfree(port->interrupt_out_buffer);
164 usb_put_dev(serial->dev);
166 /* free up any memory that we allocated */
167 kfree (serial);
170 /*****************************************************************************
171 * Driver tty interface functions
172 *****************************************************************************/
173 static int serial_open (struct tty_struct *tty, struct file * filp)
175 struct usb_serial *serial;
176 struct usb_serial_port *port;
177 unsigned int portNumber;
178 int retval;
180 dbg("%s", __FUNCTION__);
182 /* get the serial object associated with this tty pointer */
183 serial = usb_serial_get_by_index(tty->index);
184 if (!serial) {
185 tty->driver_data = NULL;
186 return -ENODEV;
189 portNumber = tty->index - serial->minor;
190 port = serial->port[portNumber];
192 ++port->open_count;
194 if (port->open_count == 1) {
196 /* set up our port structure making the tty driver
197 * remember our port object, and us it */
198 tty->driver_data = port;
199 port->tty = tty;
201 /* lock this module before we call it
202 * this may fail, which means we must bail out,
203 * safe because we are called with BKL held */
204 if (!try_module_get(serial->type->driver.owner)) {
205 retval = -ENODEV;
206 goto bailout_kref_put;
209 /* only call the device specific open if this
210 * is the first time the port is opened */
211 retval = serial->type->open(port, filp);
212 if (retval)
213 goto bailout_module_put;
216 return 0;
218 bailout_module_put:
219 module_put(serial->type->driver.owner);
220 bailout_kref_put:
221 kref_put(&serial->kref, destroy_serial);
222 port->open_count = 0;
223 return retval;
226 static void serial_close(struct tty_struct *tty, struct file * filp)
228 struct usb_serial_port *port = tty->driver_data;
230 if (!port)
231 return;
233 dbg("%s - port %d", __FUNCTION__, port->number);
235 if (port->open_count == 0)
236 return;
238 --port->open_count;
239 if (port->open_count == 0) {
240 /* only call the device specific close if this
241 * port is being closed by the last owner */
242 port->serial->type->close(port, filp);
244 if (port->tty) {
245 if (port->tty->driver_data)
246 port->tty->driver_data = NULL;
247 port->tty = NULL;
250 module_put(port->serial->type->driver.owner);
253 kref_put(&port->serial->kref, destroy_serial);
256 static int serial_write (struct tty_struct * tty, const unsigned char *buf, int count)
258 struct usb_serial_port *port = tty->driver_data;
259 int retval = -EINVAL;
261 dbg("%s - port %d, %d byte(s)", __FUNCTION__, port->number, count);
263 if (!port->open_count) {
264 dbg("%s - port not opened", __FUNCTION__);
265 goto exit;
268 /* pass on to the driver specific version of this function */
269 retval = port->serial->type->write(port, buf, count);
271 exit:
272 return retval;
275 static int serial_write_room (struct tty_struct *tty)
277 struct usb_serial_port *port = tty->driver_data;
278 int retval = -EINVAL;
280 dbg("%s - port %d", __FUNCTION__, port->number);
282 if (!port->open_count) {
283 dbg("%s - port not open", __FUNCTION__);
284 goto exit;
287 /* pass on to the driver specific version of this function */
288 retval = port->serial->type->write_room(port);
290 exit:
291 return retval;
294 static int serial_chars_in_buffer (struct tty_struct *tty)
296 struct usb_serial_port *port = tty->driver_data;
297 int retval = -EINVAL;
299 dbg("%s = port %d", __FUNCTION__, port->number);
301 if (!port->open_count) {
302 dbg("%s - port not open", __FUNCTION__);
303 goto exit;
306 /* pass on to the driver specific version of this function */
307 retval = port->serial->type->chars_in_buffer(port);
309 exit:
310 return retval;
313 static void serial_throttle (struct tty_struct * tty)
315 struct usb_serial_port *port = tty->driver_data;
317 dbg("%s - port %d", __FUNCTION__, port->number);
319 if (!port->open_count) {
320 dbg ("%s - port not open", __FUNCTION__);
321 return;
324 /* pass on to the driver specific version of this function */
325 if (port->serial->type->throttle)
326 port->serial->type->throttle(port);
329 static void serial_unthrottle (struct tty_struct * tty)
331 struct usb_serial_port *port = tty->driver_data;
333 dbg("%s - port %d", __FUNCTION__, port->number);
335 if (!port->open_count) {
336 dbg("%s - port not open", __FUNCTION__);
337 return;
340 /* pass on to the driver specific version of this function */
341 if (port->serial->type->unthrottle)
342 port->serial->type->unthrottle(port);
345 static int serial_ioctl (struct tty_struct *tty, struct file * file, unsigned int cmd, unsigned long arg)
347 struct usb_serial_port *port = tty->driver_data;
348 int retval = -ENODEV;
350 dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__, port->number, cmd);
352 if (!port->open_count) {
353 dbg ("%s - port not open", __FUNCTION__);
354 goto exit;
357 /* pass on to the driver specific version of this function if it is available */
358 if (port->serial->type->ioctl)
359 retval = port->serial->type->ioctl(port, file, cmd, arg);
360 else
361 retval = -ENOIOCTLCMD;
363 exit:
364 return retval;
367 static void serial_set_termios (struct tty_struct *tty, struct termios * old)
369 struct usb_serial_port *port = tty->driver_data;
371 dbg("%s - port %d", __FUNCTION__, port->number);
373 if (!port->open_count) {
374 dbg("%s - port not open", __FUNCTION__);
375 return;
378 /* pass on to the driver specific version of this function if it is available */
379 if (port->serial->type->set_termios)
380 port->serial->type->set_termios(port, old);
383 static void serial_break (struct tty_struct *tty, int break_state)
385 struct usb_serial_port *port = tty->driver_data;
387 dbg("%s - port %d", __FUNCTION__, port->number);
389 if (!port->open_count) {
390 dbg("%s - port not open", __FUNCTION__);
391 return;
394 /* pass on to the driver specific version of this function if it is available */
395 if (port->serial->type->break_ctl)
396 port->serial->type->break_ctl(port, break_state);
399 static int serial_read_proc (char *page, char **start, off_t off, int count, int *eof, void *data)
401 struct usb_serial *serial;
402 int length = 0;
403 int i;
404 off_t begin = 0;
405 char tmp[40];
407 dbg("%s", __FUNCTION__);
408 length += sprintf (page, "usbserinfo:1.0 driver:2.0\n");
409 for (i = 0; i < SERIAL_TTY_MINORS && length < PAGE_SIZE; ++i) {
410 serial = usb_serial_get_by_index(i);
411 if (serial == NULL)
412 continue;
414 length += sprintf (page+length, "%d:", i);
415 if (serial->type->driver.owner)
416 length += sprintf (page+length, " module:%s", module_name(serial->type->driver.owner));
417 length += sprintf (page+length, " name:\"%s\"", serial->type->description);
418 length += sprintf (page+length, " vendor:%04x product:%04x",
419 le16_to_cpu(serial->dev->descriptor.idVendor),
420 le16_to_cpu(serial->dev->descriptor.idProduct));
421 length += sprintf (page+length, " num_ports:%d", serial->num_ports);
422 length += sprintf (page+length, " port:%d", i - serial->minor + 1);
424 usb_make_path(serial->dev, tmp, sizeof(tmp));
425 length += sprintf (page+length, " path:%s", tmp);
427 length += sprintf (page+length, "\n");
428 if ((length + begin) > (off + count))
429 goto done;
430 if ((length + begin) < off) {
431 begin += length;
432 length = 0;
434 kref_put(&serial->kref, destroy_serial);
436 *eof = 1;
437 done:
438 if (off >= (length + begin))
439 return 0;
440 *start = page + (off-begin);
441 return ((count < begin+length-off) ? count : begin+length-off);
444 static int serial_tiocmget (struct tty_struct *tty, struct file *file)
446 struct usb_serial_port *port = tty->driver_data;
448 dbg("%s - port %d", __FUNCTION__, port->number);
450 if (!port->open_count) {
451 dbg("%s - port not open", __FUNCTION__);
452 goto exit;
455 if (port->serial->type->tiocmget)
456 return port->serial->type->tiocmget(port, file);
458 exit:
459 return -EINVAL;
462 static int serial_tiocmset (struct tty_struct *tty, struct file *file,
463 unsigned int set, unsigned int clear)
465 struct usb_serial_port *port = tty->driver_data;
467 dbg("%s - port %d", __FUNCTION__, port->number);
469 if (!port->open_count) {
470 dbg("%s - port not open", __FUNCTION__);
471 goto exit;
474 if (port->serial->type->tiocmset)
475 return port->serial->type->tiocmset(port, file, set, clear);
477 exit:
478 return -EINVAL;
481 void usb_serial_port_softint(void *private)
483 struct usb_serial_port *port = private;
484 struct tty_struct *tty;
486 dbg("%s - port %d", __FUNCTION__, port->number);
488 if (!port)
489 return;
491 tty = port->tty;
492 if (!tty)
493 return;
495 tty_wakeup(tty);
498 static void port_release(struct device *dev)
500 struct usb_serial_port *port = to_usb_serial_port(dev);
502 dbg ("%s - %s", __FUNCTION__, dev->bus_id);
503 usb_kill_urb(port->read_urb);
504 usb_free_urb(port->read_urb);
505 usb_kill_urb(port->write_urb);
506 usb_free_urb(port->write_urb);
507 usb_kill_urb(port->interrupt_in_urb);
508 usb_free_urb(port->interrupt_in_urb);
509 usb_kill_urb(port->interrupt_out_urb);
510 usb_free_urb(port->interrupt_out_urb);
511 kfree(port->bulk_in_buffer);
512 kfree(port->bulk_out_buffer);
513 kfree(port->interrupt_in_buffer);
514 kfree(port->interrupt_out_buffer);
515 kfree(port);
518 static struct usb_serial * create_serial (struct usb_device *dev,
519 struct usb_interface *interface,
520 struct usb_serial_driver *driver)
522 struct usb_serial *serial;
524 serial = kmalloc (sizeof (*serial), GFP_KERNEL);
525 if (!serial) {
526 dev_err(&dev->dev, "%s - out of memory\n", __FUNCTION__);
527 return NULL;
529 memset (serial, 0, sizeof(*serial));
530 serial->dev = usb_get_dev(dev);
531 serial->type = driver;
532 serial->interface = interface;
533 kref_init(&serial->kref);
535 return serial;
538 static struct usb_serial_driver *search_serial_device(struct usb_interface *iface)
540 struct list_head *p;
541 const struct usb_device_id *id;
542 struct usb_serial_driver *t;
544 /* List trough know devices and see if the usb id matches */
545 list_for_each(p, &usb_serial_driver_list) {
546 t = list_entry(p, struct usb_serial_driver, driver_list);
547 id = usb_match_id(iface, t->id_table);
548 if (id != NULL) {
549 dbg("descriptor matches");
550 return t;
554 return NULL;
557 int usb_serial_probe(struct usb_interface *interface,
558 const struct usb_device_id *id)
560 struct usb_device *dev = interface_to_usbdev (interface);
561 struct usb_serial *serial = NULL;
562 struct usb_serial_port *port;
563 struct usb_host_interface *iface_desc;
564 struct usb_endpoint_descriptor *endpoint;
565 struct usb_endpoint_descriptor *interrupt_in_endpoint[MAX_NUM_PORTS];
566 struct usb_endpoint_descriptor *interrupt_out_endpoint[MAX_NUM_PORTS];
567 struct usb_endpoint_descriptor *bulk_in_endpoint[MAX_NUM_PORTS];
568 struct usb_endpoint_descriptor *bulk_out_endpoint[MAX_NUM_PORTS];
569 struct usb_serial_driver *type = NULL;
570 int retval;
571 int minor;
572 int buffer_size;
573 int i;
574 int num_interrupt_in = 0;
575 int num_interrupt_out = 0;
576 int num_bulk_in = 0;
577 int num_bulk_out = 0;
578 int num_ports = 0;
579 int max_endpoints;
581 type = search_serial_device(interface);
582 if (!type) {
583 dbg("none matched");
584 return -ENODEV;
587 serial = create_serial (dev, interface, type);
588 if (!serial) {
589 dev_err(&interface->dev, "%s - out of memory\n", __FUNCTION__);
590 return -ENOMEM;
593 /* if this device type has a probe function, call it */
594 if (type->probe) {
595 const struct usb_device_id *id;
597 if (!try_module_get(type->driver.owner)) {
598 dev_err(&interface->dev, "module get failed, exiting\n");
599 kfree (serial);
600 return -EIO;
603 id = usb_match_id(interface, type->id_table);
604 retval = type->probe(serial, id);
605 module_put(type->driver.owner);
607 if (retval) {
608 dbg ("sub driver rejected device");
609 kfree (serial);
610 return retval;
614 /* descriptor matches, let's find the endpoints needed */
615 /* check out the endpoints */
616 iface_desc = interface->cur_altsetting;
617 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
618 endpoint = &iface_desc->endpoint[i].desc;
620 if ((endpoint->bEndpointAddress & 0x80) &&
621 ((endpoint->bmAttributes & 3) == 0x02)) {
622 /* we found a bulk in endpoint */
623 dbg("found bulk in on endpoint %d", i);
624 bulk_in_endpoint[num_bulk_in] = endpoint;
625 ++num_bulk_in;
628 if (((endpoint->bEndpointAddress & 0x80) == 0x00) &&
629 ((endpoint->bmAttributes & 3) == 0x02)) {
630 /* we found a bulk out endpoint */
631 dbg("found bulk out on endpoint %d", i);
632 bulk_out_endpoint[num_bulk_out] = endpoint;
633 ++num_bulk_out;
636 if ((endpoint->bEndpointAddress & 0x80) &&
637 ((endpoint->bmAttributes & 3) == 0x03)) {
638 /* we found a interrupt in endpoint */
639 dbg("found interrupt in on endpoint %d", i);
640 interrupt_in_endpoint[num_interrupt_in] = endpoint;
641 ++num_interrupt_in;
644 if (((endpoint->bEndpointAddress & 0x80) == 0x00) &&
645 ((endpoint->bmAttributes & 3) == 0x03)) {
646 /* we found an interrupt out endpoint */
647 dbg("found interrupt out on endpoint %d", i);
648 interrupt_out_endpoint[num_interrupt_out] = endpoint;
649 ++num_interrupt_out;
653 #if defined(CONFIG_USB_SERIAL_PL2303) || defined(CONFIG_USB_SERIAL_PL2303_MODULE)
654 /* BEGIN HORRIBLE HACK FOR PL2303 */
655 /* this is needed due to the looney way its endpoints are set up */
656 if (((le16_to_cpu(dev->descriptor.idVendor) == PL2303_VENDOR_ID) &&
657 (le16_to_cpu(dev->descriptor.idProduct) == PL2303_PRODUCT_ID)) ||
658 ((le16_to_cpu(dev->descriptor.idVendor) == ATEN_VENDOR_ID) &&
659 (le16_to_cpu(dev->descriptor.idProduct) == ATEN_PRODUCT_ID))) {
660 if (interface != dev->actconfig->interface[0]) {
661 /* check out the endpoints of the other interface*/
662 iface_desc = dev->actconfig->interface[0]->cur_altsetting;
663 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
664 endpoint = &iface_desc->endpoint[i].desc;
665 if ((endpoint->bEndpointAddress & 0x80) &&
666 ((endpoint->bmAttributes & 3) == 0x03)) {
667 /* we found a interrupt in endpoint */
668 dbg("found interrupt in for Prolific device on separate interface");
669 interrupt_in_endpoint[num_interrupt_in] = endpoint;
670 ++num_interrupt_in;
675 /* Now make sure the PL-2303 is configured correctly.
676 * If not, give up now and hope this hack will work
677 * properly during a later invocation of usb_serial_probe
679 if (num_bulk_in == 0 || num_bulk_out == 0) {
680 dev_info(&interface->dev, "PL-2303 hack: descriptors matched but endpoints did not\n");
681 kfree (serial);
682 return -ENODEV;
685 /* END HORRIBLE HACK FOR PL2303 */
686 #endif
688 /* found all that we need */
689 dev_info(&interface->dev, "%s converter detected\n", type->description);
691 #ifdef CONFIG_USB_SERIAL_GENERIC
692 if (type == &usb_serial_generic_device) {
693 num_ports = num_bulk_out;
694 if (num_ports == 0) {
695 dev_err(&interface->dev, "Generic device with no bulk out, not allowed.\n");
696 kfree (serial);
697 return -EIO;
700 #endif
701 if (!num_ports) {
702 /* if this device type has a calc_num_ports function, call it */
703 if (type->calc_num_ports) {
704 if (!try_module_get(type->driver.owner)) {
705 dev_err(&interface->dev, "module get failed, exiting\n");
706 kfree (serial);
707 return -EIO;
709 num_ports = type->calc_num_ports (serial);
710 module_put(type->driver.owner);
712 if (!num_ports)
713 num_ports = type->num_ports;
716 if (get_free_serial (serial, num_ports, &minor) == NULL) {
717 dev_err(&interface->dev, "No more free serial devices\n");
718 kfree (serial);
719 return -ENOMEM;
722 serial->minor = minor;
723 serial->num_ports = num_ports;
724 serial->num_bulk_in = num_bulk_in;
725 serial->num_bulk_out = num_bulk_out;
726 serial->num_interrupt_in = num_interrupt_in;
727 serial->num_interrupt_out = num_interrupt_out;
729 /* create our ports, we need as many as the max endpoints */
730 /* we don't use num_ports here cauz some devices have more endpoint pairs than ports */
731 max_endpoints = max(num_bulk_in, num_bulk_out);
732 max_endpoints = max(max_endpoints, num_interrupt_in);
733 max_endpoints = max(max_endpoints, num_interrupt_out);
734 max_endpoints = max(max_endpoints, (int)serial->num_ports);
735 serial->num_port_pointers = max_endpoints;
736 dbg("%s - setting up %d port structures for this device", __FUNCTION__, max_endpoints);
737 for (i = 0; i < max_endpoints; ++i) {
738 port = kmalloc(sizeof(struct usb_serial_port), GFP_KERNEL);
739 if (!port)
740 goto probe_error;
741 memset(port, 0x00, sizeof(struct usb_serial_port));
742 port->number = i + serial->minor;
743 port->serial = serial;
744 spin_lock_init(&port->lock);
745 INIT_WORK(&port->work, usb_serial_port_softint, port);
746 serial->port[i] = port;
749 /* set up the endpoint information */
750 for (i = 0; i < num_bulk_in; ++i) {
751 endpoint = bulk_in_endpoint[i];
752 port = serial->port[i];
753 port->read_urb = usb_alloc_urb (0, GFP_KERNEL);
754 if (!port->read_urb) {
755 dev_err(&interface->dev, "No free urbs available\n");
756 goto probe_error;
758 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
759 port->bulk_in_size = buffer_size;
760 port->bulk_in_endpointAddress = endpoint->bEndpointAddress;
761 port->bulk_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
762 if (!port->bulk_in_buffer) {
763 dev_err(&interface->dev, "Couldn't allocate bulk_in_buffer\n");
764 goto probe_error;
766 usb_fill_bulk_urb (port->read_urb, dev,
767 usb_rcvbulkpipe (dev,
768 endpoint->bEndpointAddress),
769 port->bulk_in_buffer, buffer_size,
770 serial->type->read_bulk_callback,
771 port);
774 for (i = 0; i < num_bulk_out; ++i) {
775 endpoint = bulk_out_endpoint[i];
776 port = serial->port[i];
777 port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
778 if (!port->write_urb) {
779 dev_err(&interface->dev, "No free urbs available\n");
780 goto probe_error;
782 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
783 port->bulk_out_size = buffer_size;
784 port->bulk_out_endpointAddress = endpoint->bEndpointAddress;
785 port->bulk_out_buffer = kmalloc (buffer_size, GFP_KERNEL);
786 if (!port->bulk_out_buffer) {
787 dev_err(&interface->dev, "Couldn't allocate bulk_out_buffer\n");
788 goto probe_error;
790 usb_fill_bulk_urb (port->write_urb, dev,
791 usb_sndbulkpipe (dev,
792 endpoint->bEndpointAddress),
793 port->bulk_out_buffer, buffer_size,
794 serial->type->write_bulk_callback,
795 port);
798 if (serial->type->read_int_callback) {
799 for (i = 0; i < num_interrupt_in; ++i) {
800 endpoint = interrupt_in_endpoint[i];
801 port = serial->port[i];
802 port->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
803 if (!port->interrupt_in_urb) {
804 dev_err(&interface->dev, "No free urbs available\n");
805 goto probe_error;
807 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
808 port->interrupt_in_endpointAddress = endpoint->bEndpointAddress;
809 port->interrupt_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
810 if (!port->interrupt_in_buffer) {
811 dev_err(&interface->dev, "Couldn't allocate interrupt_in_buffer\n");
812 goto probe_error;
814 usb_fill_int_urb (port->interrupt_in_urb, dev,
815 usb_rcvintpipe (dev,
816 endpoint->bEndpointAddress),
817 port->interrupt_in_buffer, buffer_size,
818 serial->type->read_int_callback, port,
819 endpoint->bInterval);
821 } else if (num_interrupt_in) {
822 dbg("the device claims to support interrupt in transfers, but read_int_callback is not defined");
825 if (serial->type->write_int_callback) {
826 for (i = 0; i < num_interrupt_out; ++i) {
827 endpoint = interrupt_out_endpoint[i];
828 port = serial->port[i];
829 port->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
830 if (!port->interrupt_out_urb) {
831 dev_err(&interface->dev, "No free urbs available\n");
832 goto probe_error;
834 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
835 port->interrupt_out_size = buffer_size;
836 port->interrupt_out_endpointAddress = endpoint->bEndpointAddress;
837 port->interrupt_out_buffer = kmalloc (buffer_size, GFP_KERNEL);
838 if (!port->interrupt_out_buffer) {
839 dev_err(&interface->dev, "Couldn't allocate interrupt_out_buffer\n");
840 goto probe_error;
842 usb_fill_int_urb (port->interrupt_out_urb, dev,
843 usb_sndintpipe (dev,
844 endpoint->bEndpointAddress),
845 port->interrupt_out_buffer, buffer_size,
846 serial->type->write_int_callback, port,
847 endpoint->bInterval);
849 } else if (num_interrupt_out) {
850 dbg("the device claims to support interrupt out transfers, but write_int_callback is not defined");
853 /* if this device type has an attach function, call it */
854 if (type->attach) {
855 if (!try_module_get(type->driver.owner)) {
856 dev_err(&interface->dev, "module get failed, exiting\n");
857 goto probe_error;
859 retval = type->attach (serial);
860 module_put(type->driver.owner);
861 if (retval < 0)
862 goto probe_error;
863 if (retval > 0) {
864 /* quietly accept this device, but don't bind to a serial port
865 * as it's about to disappear */
866 goto exit;
870 /* register all of the individual ports with the driver core */
871 for (i = 0; i < num_ports; ++i) {
872 port = serial->port[i];
873 port->dev.parent = &interface->dev;
874 port->dev.driver = NULL;
875 port->dev.bus = &usb_serial_bus_type;
876 port->dev.release = &port_release;
878 snprintf (&port->dev.bus_id[0], sizeof(port->dev.bus_id), "ttyUSB%d", port->number);
879 dbg ("%s - registering %s", __FUNCTION__, port->dev.bus_id);
880 device_register (&port->dev);
883 usb_serial_console_init (debug, minor);
885 exit:
886 /* success */
887 usb_set_intfdata (interface, serial);
888 return 0;
890 probe_error:
891 for (i = 0; i < num_bulk_in; ++i) {
892 port = serial->port[i];
893 if (!port)
894 continue;
895 if (port->read_urb)
896 usb_free_urb (port->read_urb);
897 kfree(port->bulk_in_buffer);
899 for (i = 0; i < num_bulk_out; ++i) {
900 port = serial->port[i];
901 if (!port)
902 continue;
903 if (port->write_urb)
904 usb_free_urb (port->write_urb);
905 kfree(port->bulk_out_buffer);
907 for (i = 0; i < num_interrupt_in; ++i) {
908 port = serial->port[i];
909 if (!port)
910 continue;
911 if (port->interrupt_in_urb)
912 usb_free_urb (port->interrupt_in_urb);
913 kfree(port->interrupt_in_buffer);
915 for (i = 0; i < num_interrupt_out; ++i) {
916 port = serial->port[i];
917 if (!port)
918 continue;
919 if (port->interrupt_out_urb)
920 usb_free_urb (port->interrupt_out_urb);
921 kfree(port->interrupt_out_buffer);
924 /* return the minor range that this device had */
925 return_serial (serial);
927 /* free up any memory that we allocated */
928 for (i = 0; i < serial->num_port_pointers; ++i)
929 kfree(serial->port[i]);
930 kfree (serial);
931 return -EIO;
934 void usb_serial_disconnect(struct usb_interface *interface)
936 int i;
937 struct usb_serial *serial = usb_get_intfdata (interface);
938 struct device *dev = &interface->dev;
939 struct usb_serial_port *port;
941 dbg ("%s", __FUNCTION__);
943 usb_set_intfdata (interface, NULL);
944 if (serial) {
945 for (i = 0; i < serial->num_ports; ++i) {
946 port = serial->port[i];
947 if (port && port->tty)
948 tty_hangup(port->tty);
950 /* let the last holder of this object
951 * cause it to be cleaned up */
952 kref_put(&serial->kref, destroy_serial);
954 dev_info(dev, "device disconnected\n");
957 static struct tty_operations serial_ops = {
958 .open = serial_open,
959 .close = serial_close,
960 .write = serial_write,
961 .write_room = serial_write_room,
962 .ioctl = serial_ioctl,
963 .set_termios = serial_set_termios,
964 .throttle = serial_throttle,
965 .unthrottle = serial_unthrottle,
966 .break_ctl = serial_break,
967 .chars_in_buffer = serial_chars_in_buffer,
968 .read_proc = serial_read_proc,
969 .tiocmget = serial_tiocmget,
970 .tiocmset = serial_tiocmset,
973 struct tty_driver *usb_serial_tty_driver;
975 static int __init usb_serial_init(void)
977 int i;
978 int result;
980 usb_serial_tty_driver = alloc_tty_driver(SERIAL_TTY_MINORS);
981 if (!usb_serial_tty_driver)
982 return -ENOMEM;
984 /* Initialize our global data */
985 for (i = 0; i < SERIAL_TTY_MINORS; ++i) {
986 serial_table[i] = NULL;
989 result = bus_register(&usb_serial_bus_type);
990 if (result) {
991 err("%s - registering bus driver failed", __FUNCTION__);
992 goto exit_bus;
995 usb_serial_tty_driver->owner = THIS_MODULE;
996 usb_serial_tty_driver->driver_name = "usbserial";
997 usb_serial_tty_driver->devfs_name = "usb/tts/";
998 usb_serial_tty_driver->name = "ttyUSB";
999 usb_serial_tty_driver->major = SERIAL_TTY_MAJOR;
1000 usb_serial_tty_driver->minor_start = 0;
1001 usb_serial_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1002 usb_serial_tty_driver->subtype = SERIAL_TYPE_NORMAL;
1003 usb_serial_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
1004 usb_serial_tty_driver->init_termios = tty_std_termios;
1005 usb_serial_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1006 tty_set_operations(usb_serial_tty_driver, &serial_ops);
1007 result = tty_register_driver(usb_serial_tty_driver);
1008 if (result) {
1009 err("%s - tty_register_driver failed", __FUNCTION__);
1010 goto exit_reg_driver;
1013 /* register the USB driver */
1014 result = usb_register(&usb_serial_driver);
1015 if (result < 0) {
1016 err("%s - usb_register failed", __FUNCTION__);
1017 goto exit_tty;
1020 /* register the generic driver, if we should */
1021 result = usb_serial_generic_register(debug);
1022 if (result < 0) {
1023 err("%s - registering generic driver failed", __FUNCTION__);
1024 goto exit_generic;
1027 info(DRIVER_DESC);
1029 return result;
1031 exit_generic:
1032 usb_deregister(&usb_serial_driver);
1034 exit_tty:
1035 tty_unregister_driver(usb_serial_tty_driver);
1037 exit_reg_driver:
1038 bus_unregister(&usb_serial_bus_type);
1040 exit_bus:
1041 err ("%s - returning with error %d", __FUNCTION__, result);
1042 put_tty_driver(usb_serial_tty_driver);
1043 return result;
1047 static void __exit usb_serial_exit(void)
1049 usb_serial_console_exit();
1051 usb_serial_generic_deregister();
1053 usb_deregister(&usb_serial_driver);
1054 tty_unregister_driver(usb_serial_tty_driver);
1055 put_tty_driver(usb_serial_tty_driver);
1056 bus_unregister(&usb_serial_bus_type);
1060 module_init(usb_serial_init);
1061 module_exit(usb_serial_exit);
1063 #define set_to_generic_if_null(type, function) \
1064 do { \
1065 if (!type->function) { \
1066 type->function = usb_serial_generic_##function; \
1067 dbg("Had to override the " #function \
1068 " usb serial operation with the generic one.");\
1070 } while (0)
1072 static void fixup_generic(struct usb_serial_driver *device)
1074 set_to_generic_if_null(device, open);
1075 set_to_generic_if_null(device, write);
1076 set_to_generic_if_null(device, close);
1077 set_to_generic_if_null(device, write_room);
1078 set_to_generic_if_null(device, chars_in_buffer);
1079 set_to_generic_if_null(device, read_bulk_callback);
1080 set_to_generic_if_null(device, write_bulk_callback);
1081 set_to_generic_if_null(device, shutdown);
1084 int usb_serial_register(struct usb_serial_driver *driver)
1086 int retval;
1088 fixup_generic(driver);
1090 if (!driver->description)
1091 driver->description = driver->driver.name;
1093 /* Add this device to our list of devices */
1094 list_add(&driver->driver_list, &usb_serial_driver_list);
1096 retval = usb_serial_bus_register(driver);
1097 if (retval) {
1098 err("problem %d when registering driver %s", retval, driver->description);
1099 list_del(&driver->driver_list);
1101 else
1102 info("USB Serial support registered for %s", driver->description);
1104 return retval;
1108 void usb_serial_deregister(struct usb_serial_driver *device)
1110 info("USB Serial deregistering driver %s", device->description);
1111 list_del(&device->driver_list);
1112 usb_serial_bus_deregister(device);
1117 /* If the usb-serial core is built into the core, the usb-serial drivers
1118 need these symbols to load properly as modules. */
1119 EXPORT_SYMBOL_GPL(usb_serial_register);
1120 EXPORT_SYMBOL_GPL(usb_serial_deregister);
1121 EXPORT_SYMBOL_GPL(usb_serial_probe);
1122 EXPORT_SYMBOL_GPL(usb_serial_disconnect);
1123 EXPORT_SYMBOL_GPL(usb_serial_port_softint);
1126 /* Module information */
1127 MODULE_AUTHOR( DRIVER_AUTHOR );
1128 MODULE_DESCRIPTION( DRIVER_DESC );
1129 MODULE_LICENSE("GPL");
1131 module_param(debug, bool, S_IRUGO | S_IWUSR);
1132 MODULE_PARM_DESC(debug, "Debug enabled or not");