pcmcia: CompactFlash driver for PA Semi Electra boards
[pv_ops_mirror.git] / drivers / usb / serial / bus.c
blob0b14aea8ebd52891015e32963784a048e7472ca1
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/module.h>
15 #include <linux/usb.h>
16 #include <linux/usb/serial.h>
18 static int usb_serial_device_match (struct device *dev, struct device_driver *drv)
20 struct usb_serial_driver *driver;
21 const struct usb_serial_port *port;
24 * drivers are already assigned to ports in serial_probe so it's
25 * a simple check here.
27 port = to_usb_serial_port(dev);
28 if (!port)
29 return 0;
31 driver = to_usb_serial_driver(drv);
33 if (driver == port->serial->type)
34 return 1;
36 return 0;
39 static ssize_t show_port_number(struct device *dev,
40 struct device_attribute *attr, char *buf)
42 struct usb_serial_port *port = to_usb_serial_port(dev);
44 return sprintf(buf, "%d\n", port->number - port->serial->minor);
47 static DEVICE_ATTR(port_number, S_IRUGO, show_port_number, NULL);
49 static int usb_serial_device_probe (struct device *dev)
51 struct usb_serial_driver *driver;
52 struct usb_serial_port *port;
53 int retval = 0;
54 int minor;
56 port = to_usb_serial_port(dev);
57 if (!port) {
58 retval = -ENODEV;
59 goto exit;
62 driver = port->serial->type;
63 if (driver->port_probe) {
64 if (!try_module_get(driver->driver.owner)) {
65 dev_err(dev, "module get failed, exiting\n");
66 retval = -EIO;
67 goto exit;
69 retval = driver->port_probe (port);
70 module_put(driver->driver.owner);
71 if (retval)
72 goto exit;
75 retval = device_create_file(dev, &dev_attr_port_number);
76 if (retval)
77 goto exit;
79 minor = port->number;
80 tty_register_device (usb_serial_tty_driver, minor, dev);
81 dev_info(&port->serial->dev->dev,
82 "%s converter now attached to ttyUSB%d\n",
83 driver->description, minor);
85 exit:
86 return retval;
89 static int usb_serial_device_remove (struct device *dev)
91 struct usb_serial_driver *driver;
92 struct usb_serial_port *port;
93 int retval = 0;
94 int minor;
96 port = to_usb_serial_port(dev);
97 if (!port) {
98 return -ENODEV;
101 device_remove_file(&port->dev, &dev_attr_port_number);
103 driver = port->serial->type;
104 if (driver->port_remove) {
105 if (!try_module_get(driver->driver.owner)) {
106 dev_err(dev, "module get failed, exiting\n");
107 retval = -EIO;
108 goto exit;
110 retval = driver->port_remove (port);
111 module_put(driver->driver.owner);
113 exit:
114 minor = port->number;
115 tty_unregister_device (usb_serial_tty_driver, minor);
116 dev_info(dev, "%s converter now disconnected from ttyUSB%d\n",
117 driver->description, minor);
119 return retval;
122 #ifdef CONFIG_HOTPLUG
123 static ssize_t store_new_id(struct device_driver *driver,
124 const char *buf, size_t count)
126 struct usb_serial_driver *usb_drv = to_usb_serial_driver(driver);
127 ssize_t retval = usb_store_new_id(&usb_drv->dynids, driver, buf, count);
129 if (retval >= 0 && usb_drv->usb_driver != NULL)
130 retval = usb_store_new_id(&usb_drv->usb_driver->dynids,
131 &usb_drv->usb_driver->drvwrap.driver,
132 buf, count);
133 return retval;
136 static struct driver_attribute drv_attrs[] = {
137 __ATTR(new_id, S_IWUSR, NULL, store_new_id),
138 __ATTR_NULL,
141 static void free_dynids(struct usb_serial_driver *drv)
143 struct usb_dynid *dynid, *n;
145 spin_lock(&drv->dynids.lock);
146 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
147 list_del(&dynid->node);
148 kfree(dynid);
150 spin_unlock(&drv->dynids.lock);
153 #else
154 static struct driver_attribute drv_attrs[] = {
155 __ATTR_NULL,
157 static inline void free_dynids(struct usb_serial_driver *drv)
160 #endif
162 struct bus_type usb_serial_bus_type = {
163 .name = "usb-serial",
164 .match = usb_serial_device_match,
165 .probe = usb_serial_device_probe,
166 .remove = usb_serial_device_remove,
167 .drv_attrs = drv_attrs,
170 int usb_serial_bus_register(struct usb_serial_driver *driver)
172 int retval;
174 driver->driver.bus = &usb_serial_bus_type;
175 spin_lock_init(&driver->dynids.lock);
176 INIT_LIST_HEAD(&driver->dynids.list);
178 retval = driver_register(&driver->driver);
180 return retval;
183 void usb_serial_bus_deregister(struct usb_serial_driver *driver)
185 free_dynids(driver);
186 driver_unregister(&driver->driver);