Add linux-next specific files for 20110421
[linux-2.6/next.git] / drivers / usb / serial / bus.c
blob7f547dc3a5903fd25e0837441115007320ae122d
1 /*
2 * USB Serial Converter Bus specific functions
4 * Copyright (C) 2002 Greg Kroah-Hartman (greg@kroah.com)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version
8 * 2 as published by the Free Software Foundation.
9 */
11 #include <linux/kernel.h>
12 #include <linux/errno.h>
13 #include <linux/tty.h>
14 #include <linux/slab.h>
15 #include <linux/module.h>
16 #include <linux/usb.h>
17 #include <linux/usb/serial.h>
19 static int usb_serial_device_match(struct device *dev,
20 struct device_driver *drv)
22 struct usb_serial_driver *driver;
23 const struct usb_serial_port *port;
26 * drivers are already assigned to ports in serial_probe so it's
27 * a simple check here.
29 port = to_usb_serial_port(dev);
30 if (!port)
31 return 0;
33 driver = to_usb_serial_driver(drv);
35 if (driver == port->serial->type)
36 return 1;
38 return 0;
41 static ssize_t show_port_number(struct device *dev,
42 struct device_attribute *attr, char *buf)
44 struct usb_serial_port *port = to_usb_serial_port(dev);
46 return sprintf(buf, "%d\n", port->number - port->serial->minor);
49 static DEVICE_ATTR(port_number, S_IRUGO, show_port_number, NULL);
51 static int usb_serial_device_probe(struct device *dev)
53 struct usb_serial_driver *driver;
54 struct usb_serial_port *port;
55 int retval = 0;
56 int minor;
58 port = to_usb_serial_port(dev);
59 if (!port) {
60 retval = -ENODEV;
61 goto exit;
63 if (port->dev_state != PORT_REGISTERING)
64 goto exit;
66 driver = port->serial->type;
67 if (driver->port_probe) {
68 retval = driver->port_probe(port);
69 if (retval)
70 goto exit;
73 retval = device_create_file(dev, &dev_attr_port_number);
74 if (retval) {
75 if (driver->port_remove)
76 retval = driver->port_remove(port);
77 goto exit;
80 minor = port->number;
81 tty_register_device(usb_serial_tty_driver, minor, dev);
82 dev_info(&port->serial->dev->dev,
83 "%s converter now attached to ttyUSB%d\n",
84 driver->description, minor);
86 exit:
87 return retval;
90 static int usb_serial_device_remove(struct device *dev)
92 struct usb_serial_driver *driver;
93 struct usb_serial_port *port;
94 int retval = 0;
95 int minor;
97 port = to_usb_serial_port(dev);
98 if (!port)
99 return -ENODEV;
101 if (port->dev_state != PORT_UNREGISTERING)
102 return retval;
104 device_remove_file(&port->dev, &dev_attr_port_number);
106 driver = port->serial->type;
107 if (driver->port_remove)
108 retval = driver->port_remove(port);
110 minor = port->number;
111 tty_unregister_device(usb_serial_tty_driver, minor);
112 dev_info(dev, "%s converter now disconnected from ttyUSB%d\n",
113 driver->description, minor);
115 return retval;
118 #ifdef CONFIG_HOTPLUG
119 static ssize_t store_new_id(struct device_driver *driver,
120 const char *buf, size_t count)
122 struct usb_serial_driver *usb_drv = to_usb_serial_driver(driver);
123 ssize_t retval = usb_store_new_id(&usb_drv->dynids, driver, buf, count);
125 if (retval >= 0 && usb_drv->usb_driver != NULL)
126 retval = usb_store_new_id(&usb_drv->usb_driver->dynids,
127 &usb_drv->usb_driver->drvwrap.driver,
128 buf, count);
129 return retval;
132 static struct driver_attribute drv_attrs[] = {
133 __ATTR(new_id, S_IWUSR, NULL, store_new_id),
134 __ATTR_NULL,
137 static void free_dynids(struct usb_serial_driver *drv)
139 struct usb_dynid *dynid, *n;
141 spin_lock(&drv->dynids.lock);
142 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
143 list_del(&dynid->node);
144 kfree(dynid);
146 spin_unlock(&drv->dynids.lock);
149 #else
150 static struct driver_attribute drv_attrs[] = {
151 __ATTR_NULL,
153 static inline void free_dynids(struct usb_serial_driver *drv)
156 #endif
158 struct bus_type usb_serial_bus_type = {
159 .name = "usb-serial",
160 .match = usb_serial_device_match,
161 .probe = usb_serial_device_probe,
162 .remove = usb_serial_device_remove,
163 .drv_attrs = drv_attrs,
166 int usb_serial_bus_register(struct usb_serial_driver *driver)
168 int retval;
170 driver->driver.bus = &usb_serial_bus_type;
171 spin_lock_init(&driver->dynids.lock);
172 INIT_LIST_HEAD(&driver->dynids.list);
174 retval = driver_register(&driver->driver);
176 return retval;
179 void usb_serial_bus_deregister(struct usb_serial_driver *driver)
181 free_dynids(driver);
182 driver_unregister(&driver->driver);