Linux 3.12.39
[linux/fpc-iii.git] / drivers / usb / serial / bus.c
bloba5500cf12e0e2afa393a8fde6db9e5ba078883fe
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 port_number_show(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->port_number);
48 static DEVICE_ATTR_RO(port_number);
50 static int usb_serial_device_probe(struct device *dev)
52 struct usb_serial_driver *driver;
53 struct usb_serial_port *port;
54 struct device *tty_dev;
55 int retval = 0;
56 int minor;
58 port = to_usb_serial_port(dev);
59 if (!port) {
60 retval = -ENODEV;
61 goto exit;
64 /* make sure suspend/resume doesn't race against port_probe */
65 retval = usb_autopm_get_interface(port->serial->interface);
66 if (retval)
67 goto exit;
69 driver = port->serial->type;
70 if (driver->port_probe) {
71 retval = driver->port_probe(port);
72 if (retval)
73 goto exit_with_autopm;
76 retval = device_create_file(dev, &dev_attr_port_number);
77 if (retval) {
78 if (driver->port_remove)
79 driver->port_remove(port);
80 goto exit_with_autopm;
83 minor = port->minor;
84 tty_dev = tty_register_device(usb_serial_tty_driver, minor, dev);
85 if (IS_ERR(tty_dev)) {
86 retval = PTR_ERR(tty_dev);
87 device_remove_file(dev, &dev_attr_port_number);
88 if (driver->port_remove)
89 driver->port_remove(port);
90 goto exit_with_autopm;
93 dev_info(&port->serial->dev->dev,
94 "%s converter now attached to ttyUSB%d\n",
95 driver->description, minor);
97 exit_with_autopm:
98 usb_autopm_put_interface(port->serial->interface);
99 exit:
100 return retval;
103 static int usb_serial_device_remove(struct device *dev)
105 struct usb_serial_driver *driver;
106 struct usb_serial_port *port;
107 int retval = 0;
108 int minor;
109 int autopm_err;
111 port = to_usb_serial_port(dev);
112 if (!port)
113 return -ENODEV;
116 * Make sure suspend/resume doesn't race against port_remove.
118 * Note that no further runtime PM callbacks will be made if
119 * autopm_get fails.
121 autopm_err = usb_autopm_get_interface(port->serial->interface);
123 minor = port->minor;
124 tty_unregister_device(usb_serial_tty_driver, minor);
126 device_remove_file(&port->dev, &dev_attr_port_number);
128 driver = port->serial->type;
129 if (driver->port_remove)
130 retval = driver->port_remove(port);
132 dev_info(dev, "%s converter now disconnected from ttyUSB%d\n",
133 driver->description, minor);
135 if (!autopm_err)
136 usb_autopm_put_interface(port->serial->interface);
138 return retval;
141 static ssize_t new_id_store(struct device_driver *driver,
142 const char *buf, size_t count)
144 struct usb_serial_driver *usb_drv = to_usb_serial_driver(driver);
145 ssize_t retval = usb_store_new_id(&usb_drv->dynids, driver, buf, count);
147 if (retval >= 0 && usb_drv->usb_driver != NULL)
148 retval = usb_store_new_id(&usb_drv->usb_driver->dynids,
149 &usb_drv->usb_driver->drvwrap.driver,
150 buf, count);
151 return retval;
154 static ssize_t new_id_show(struct device_driver *driver, char *buf)
156 struct usb_serial_driver *usb_drv = to_usb_serial_driver(driver);
158 return usb_show_dynids(&usb_drv->dynids, buf);
160 static DRIVER_ATTR_RW(new_id);
162 static struct attribute *usb_serial_drv_attrs[] = {
163 &driver_attr_new_id.attr,
164 NULL,
166 ATTRIBUTE_GROUPS(usb_serial_drv);
168 static void free_dynids(struct usb_serial_driver *drv)
170 struct usb_dynid *dynid, *n;
172 spin_lock(&drv->dynids.lock);
173 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
174 list_del(&dynid->node);
175 kfree(dynid);
177 spin_unlock(&drv->dynids.lock);
180 struct bus_type usb_serial_bus_type = {
181 .name = "usb-serial",
182 .match = usb_serial_device_match,
183 .probe = usb_serial_device_probe,
184 .remove = usb_serial_device_remove,
185 .drv_groups = usb_serial_drv_groups,
188 int usb_serial_bus_register(struct usb_serial_driver *driver)
190 int retval;
192 driver->driver.bus = &usb_serial_bus_type;
193 spin_lock_init(&driver->dynids.lock);
194 INIT_LIST_HEAD(&driver->dynids.list);
196 retval = driver_register(&driver->driver);
198 return retval;
201 void usb_serial_bus_deregister(struct usb_serial_driver *driver)
203 free_dynids(driver);
204 driver_unregister(&driver->driver);