blob: ba555c528cc6cc8c5b88356140cd0a4e288d1ed1 [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
Alan Coxc97611c2008-07-22 11:09:48 +010018static int usb_serial_device_match(struct device *dev,
19 struct device_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -070020{
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -070021 struct usb_serial_driver *driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 const struct usb_serial_port *port;
23
24 /*
25 * drivers are already assigned to ports in serial_probe so it's
26 * a simple check here.
27 */
28 port = to_usb_serial_port(dev);
29 if (!port)
30 return 0;
31
32 driver = to_usb_serial_driver(drv);
33
34 if (driver == port->serial->type)
35 return 1;
36
37 return 0;
38}
39
Roman Kagance0d9322007-08-24 20:22:52 +040040static ssize_t show_port_number(struct device *dev,
41 struct device_attribute *attr, char *buf)
42{
43 struct usb_serial_port *port = to_usb_serial_port(dev);
44
45 return sprintf(buf, "%d\n", port->number - port->serial->minor);
46}
47
48static DEVICE_ATTR(port_number, S_IRUGO, show_port_number, NULL);
49
Alan Coxc97611c2008-07-22 11:09:48 +010050static int usb_serial_device_probe(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -070052 struct usb_serial_driver *driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 struct usb_serial_port *port;
54 int retval = 0;
55 int minor;
56
57 port = to_usb_serial_port(dev);
58 if (!port) {
59 retval = -ENODEV;
60 goto exit;
61 }
Alan Sternc706ebd2009-06-02 11:54:11 -040062 if (port->dev_state != PORT_REGISTERING)
63 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65 driver = port->serial->type;
66 if (driver->port_probe) {
Alan Coxc97611c2008-07-22 11:09:48 +010067 retval = driver->port_probe(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 if (retval)
69 goto exit;
70 }
71
Roman Kagance0d9322007-08-24 20:22:52 +040072 retval = device_create_file(dev, &dev_attr_port_number);
Alan Sternc706ebd2009-06-02 11:54:11 -040073 if (retval) {
74 if (driver->port_remove)
75 retval = driver->port_remove(port);
Roman Kagance0d9322007-08-24 20:22:52 +040076 goto exit;
Alan Sternc706ebd2009-06-02 11:54:11 -040077 }
Roman Kagance0d9322007-08-24 20:22:52 +040078
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 minor = port->number;
Alan Coxc97611c2008-07-22 11:09:48 +010080 tty_register_device(usb_serial_tty_driver, minor, dev);
81 dev_info(&port->serial->dev->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 "%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
Alan Coxc97611c2008-07-22 11:09:48 +010089static int usb_serial_device_remove(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
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);
Alan Coxc97611c2008-07-22 11:09:48 +010097 if (!port)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Alan Sternc706ebd2009-06-02 11:54:11 -0400100 if (port->dev_state != PORT_UNREGISTERING)
101 return retval;
102
Roman Kagance0d9322007-08-24 20:22:52 +0400103 device_remove_file(&port->dev, &dev_attr_port_number);
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 driver = port->serial->type;
Alan Sternc706ebd2009-06-02 11:54:11 -0400106 if (driver->port_remove)
Alan Coxc97611c2008-07-22 11:09:48 +0100107 retval = driver->port_remove(port);
Alan Sternc706ebd2009-06-02 11:54:11 -0400108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 minor = port->number;
Alan Coxc97611c2008-07-22 11:09:48 +0100110 tty_unregister_device(usb_serial_tty_driver, minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 dev_info(dev, "%s converter now disconnected from ttyUSB%d\n",
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700112 driver->description, minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114 return retval;
115}
116
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100117#ifdef CONFIG_HOTPLUG
118static ssize_t store_new_id(struct device_driver *driver,
119 const char *buf, size_t count)
120{
121 struct usb_serial_driver *usb_drv = to_usb_serial_driver(driver);
122 ssize_t retval = usb_store_new_id(&usb_drv->dynids, driver, buf, count);
123
124 if (retval >= 0 && usb_drv->usb_driver != NULL)
125 retval = usb_store_new_id(&usb_drv->usb_driver->dynids,
126 &usb_drv->usb_driver->drvwrap.driver,
127 buf, count);
128 return retval;
129}
130
131static struct driver_attribute drv_attrs[] = {
132 __ATTR(new_id, S_IWUSR, NULL, store_new_id),
133 __ATTR_NULL,
134};
135
136static void free_dynids(struct usb_serial_driver *drv)
137{
138 struct usb_dynid *dynid, *n;
139
140 spin_lock(&drv->dynids.lock);
141 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
142 list_del(&dynid->node);
143 kfree(dynid);
144 }
145 spin_unlock(&drv->dynids.lock);
146}
147
148#else
149static struct driver_attribute drv_attrs[] = {
150 __ATTR_NULL,
151};
Satyam Sharma03fc3702007-09-03 01:42:43 +0530152static inline void free_dynids(struct usb_serial_driver *drv)
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100153{
154}
155#endif
156
Russell Kingac33bc32006-01-05 14:43:11 +0000157struct bus_type usb_serial_bus_type = {
158 .name = "usb-serial",
159 .match = usb_serial_device_match,
160 .probe = usb_serial_device_probe,
161 .remove = usb_serial_device_remove,
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100162 .drv_attrs = drv_attrs,
Russell Kingac33bc32006-01-05 14:43:11 +0000163};
164
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700165int usb_serial_bus_register(struct usb_serial_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
167 int retval;
168
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700169 driver->driver.bus = &usb_serial_bus_type;
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100170 spin_lock_init(&driver->dynids.lock);
171 INIT_LIST_HEAD(&driver->dynids.list);
172
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700173 retval = driver_register(&driver->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 return retval;
176}
177
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700178void usb_serial_bus_deregister(struct usb_serial_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100180 free_dynids(driver);
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700181 driver_unregister(&driver->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182}
183