blob: 0b14aea8ebd52891015e32963784a048e7472ca1 [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
Roman Kagance0d9322007-08-24 20:22:52 +040039static ssize_t show_port_number(struct device *dev,
40 struct device_attribute *attr, char *buf)
41{
42 struct usb_serial_port *port = to_usb_serial_port(dev);
43
44 return sprintf(buf, "%d\n", port->number - port->serial->minor);
45}
46
47static DEVICE_ATTR(port_number, S_IRUGO, show_port_number, NULL);
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049static int usb_serial_device_probe (struct device *dev)
50{
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -070051 struct usb_serial_driver *driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 struct usb_serial_port *port;
53 int retval = 0;
54 int minor;
55
56 port = to_usb_serial_port(dev);
57 if (!port) {
58 retval = -ENODEV;
59 goto exit;
60 }
61
62 driver = port->serial->type;
63 if (driver->port_probe) {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -070064 if (!try_module_get(driver->driver.owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 dev_err(dev, "module get failed, exiting\n");
66 retval = -EIO;
67 goto exit;
68 }
69 retval = driver->port_probe (port);
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -070070 module_put(driver->driver.owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 if (retval)
72 goto exit;
73 }
74
Roman Kagance0d9322007-08-24 20:22:52 +040075 retval = device_create_file(dev, &dev_attr_port_number);
76 if (retval)
77 goto exit;
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 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",
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -070083 driver->description, minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85exit:
86 return retval;
87}
88
89static int usb_serial_device_remove (struct device *dev)
90{
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -070091 struct usb_serial_driver *driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 struct usb_serial_port *port;
93 int retval = 0;
94 int minor;
95
96 port = to_usb_serial_port(dev);
97 if (!port) {
98 return -ENODEV;
99 }
100
Roman Kagance0d9322007-08-24 20:22:52 +0400101 device_remove_file(&port->dev, &dev_attr_port_number);
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 driver = port->serial->type;
104 if (driver->port_remove) {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700105 if (!try_module_get(driver->driver.owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 dev_err(dev, "module get failed, exiting\n");
107 retval = -EIO;
108 goto exit;
109 }
110 retval = driver->port_remove (port);
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700111 module_put(driver->driver.owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 }
113exit:
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",
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700117 driver->description, minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119 return retval;
120}
121
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100122#ifdef CONFIG_HOTPLUG
123static ssize_t store_new_id(struct device_driver *driver,
124 const char *buf, size_t count)
125{
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);
128
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;
134}
135
136static struct driver_attribute drv_attrs[] = {
137 __ATTR(new_id, S_IWUSR, NULL, store_new_id),
138 __ATTR_NULL,
139};
140
141static void free_dynids(struct usb_serial_driver *drv)
142{
143 struct usb_dynid *dynid, *n;
144
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);
149 }
150 spin_unlock(&drv->dynids.lock);
151}
152
153#else
154static struct driver_attribute drv_attrs[] = {
155 __ATTR_NULL,
156};
Satyam Sharma03fc3702007-09-03 01:42:43 +0530157static inline void free_dynids(struct usb_serial_driver *drv)
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100158{
159}
160#endif
161
Russell Kingac33bc32006-01-05 14:43:11 +0000162struct 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,
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100167 .drv_attrs = drv_attrs,
Russell Kingac33bc32006-01-05 14:43:11 +0000168};
169
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700170int usb_serial_bus_register(struct usb_serial_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
172 int retval;
173
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700174 driver->driver.bus = &usb_serial_bus_type;
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100175 spin_lock_init(&driver->dynids.lock);
176 INIT_LIST_HEAD(&driver->dynids.list);
177
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700178 retval = driver_register(&driver->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 return retval;
181}
182
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700183void usb_serial_bus_deregister(struct usb_serial_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100185 free_dynids(driver);
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700186 driver_unregister(&driver->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187}
188