blob: c08a38402b93a1287562ae254d367c51da12d3e5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * USB Serial Converter Bus specific functions
3 *
4 * Copyright (C) 2002 Greg Kroah-Hartman (greg@kroah.com)
5 *
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 */
10
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/kernel.h>
12#include <linux/errno.h>
13#include <linux/tty.h>
14#include <linux/module.h>
15#include <linux/usb.h>
Greg Kroah-Hartmana9698882006-07-11 21:22:58 -070016#include <linux/usb/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18static int usb_serial_device_match (struct device *dev, struct device_driver *drv)
19{
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -070020 struct usb_serial_driver *driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 const struct usb_serial_port *port;
22
23 /*
24 * drivers are already assigned to ports in serial_probe so it's
25 * a simple check here.
26 */
27 port = to_usb_serial_port(dev);
28 if (!port)
29 return 0;
30
31 driver = to_usb_serial_driver(drv);
32
33 if (driver == port->serial->type)
34 return 1;
35
36 return 0;
37}
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039static int usb_serial_device_probe (struct device *dev)
40{
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -070041 struct usb_serial_driver *driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 struct usb_serial_port *port;
43 int retval = 0;
44 int minor;
45
46 port = to_usb_serial_port(dev);
47 if (!port) {
48 retval = -ENODEV;
49 goto exit;
50 }
51
52 driver = port->serial->type;
53 if (driver->port_probe) {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -070054 if (!try_module_get(driver->driver.owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 dev_err(dev, "module get failed, exiting\n");
56 retval = -EIO;
57 goto exit;
58 }
59 retval = driver->port_probe (port);
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -070060 module_put(driver->driver.owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 if (retval)
62 goto exit;
63 }
64
65 minor = port->number;
66 tty_register_device (usb_serial_tty_driver, minor, dev);
67 dev_info(&port->serial->dev->dev,
68 "%s converter now attached to ttyUSB%d\n",
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -070069 driver->description, minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71exit:
72 return retval;
73}
74
75static int usb_serial_device_remove (struct device *dev)
76{
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -070077 struct usb_serial_driver *driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 struct usb_serial_port *port;
79 int retval = 0;
80 int minor;
81
82 port = to_usb_serial_port(dev);
83 if (!port) {
84 return -ENODEV;
85 }
86
87 driver = port->serial->type;
88 if (driver->port_remove) {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -070089 if (!try_module_get(driver->driver.owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 dev_err(dev, "module get failed, exiting\n");
91 retval = -EIO;
92 goto exit;
93 }
94 retval = driver->port_remove (port);
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -070095 module_put(driver->driver.owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 }
97exit:
98 minor = port->number;
99 tty_unregister_device (usb_serial_tty_driver, minor);
100 dev_info(dev, "%s converter now disconnected from ttyUSB%d\n",
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700101 driver->description, minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103 return retval;
104}
105
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100106#ifdef CONFIG_HOTPLUG
107static ssize_t store_new_id(struct device_driver *driver,
108 const char *buf, size_t count)
109{
110 struct usb_serial_driver *usb_drv = to_usb_serial_driver(driver);
111 ssize_t retval = usb_store_new_id(&usb_drv->dynids, driver, buf, count);
112
113 if (retval >= 0 && usb_drv->usb_driver != NULL)
114 retval = usb_store_new_id(&usb_drv->usb_driver->dynids,
115 &usb_drv->usb_driver->drvwrap.driver,
116 buf, count);
117 return retval;
118}
119
120static struct driver_attribute drv_attrs[] = {
121 __ATTR(new_id, S_IWUSR, NULL, store_new_id),
122 __ATTR_NULL,
123};
124
125static void free_dynids(struct usb_serial_driver *drv)
126{
127 struct usb_dynid *dynid, *n;
128
129 spin_lock(&drv->dynids.lock);
130 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
131 list_del(&dynid->node);
132 kfree(dynid);
133 }
134 spin_unlock(&drv->dynids.lock);
135}
136
137#else
138static struct driver_attribute drv_attrs[] = {
139 __ATTR_NULL,
140};
141static inline void free_dynids(struct usb_driver *drv)
142{
143}
144#endif
145
Russell Kingac33bc32006-01-05 14:43:11 +0000146struct bus_type usb_serial_bus_type = {
147 .name = "usb-serial",
148 .match = usb_serial_device_match,
149 .probe = usb_serial_device_probe,
150 .remove = usb_serial_device_remove,
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100151 .drv_attrs = drv_attrs,
Russell Kingac33bc32006-01-05 14:43:11 +0000152};
153
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700154int usb_serial_bus_register(struct usb_serial_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
156 int retval;
157
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700158 driver->driver.bus = &usb_serial_bus_type;
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100159 spin_lock_init(&driver->dynids.lock);
160 INIT_LIST_HEAD(&driver->dynids.list);
161
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700162 retval = driver_register(&driver->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 return retval;
165}
166
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700167void usb_serial_bus_deregister(struct usb_serial_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100169 free_dynids(driver);
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700170 driver_unregister(&driver->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172