blob: 37decb13d7eb82a2f4486f3d17ca56fec73dc6f3 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/module.h>
16#include <linux/usb.h>
Greg Kroah-Hartmana9698882006-07-11 21:22:58 -070017#include <linux/usb/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Alan Coxc97611c2008-07-22 11:09:48 +010019static int usb_serial_device_match(struct device *dev,
20 struct device_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -070021{
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -070022 struct usb_serial_driver *driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 const struct usb_serial_port *port;
24
25 /*
26 * drivers are already assigned to ports in serial_probe so it's
27 * a simple check here.
28 */
29 port = to_usb_serial_port(dev);
30 if (!port)
31 return 0;
32
33 driver = to_usb_serial_driver(drv);
34
35 if (driver == port->serial->type)
36 return 1;
37
38 return 0;
39}
40
Roman Kagance0d9322007-08-24 20:22:52 +040041static ssize_t show_port_number(struct device *dev,
42 struct device_attribute *attr, char *buf)
43{
44 struct usb_serial_port *port = to_usb_serial_port(dev);
45
46 return sprintf(buf, "%d\n", port->number - port->serial->minor);
47}
48
49static DEVICE_ATTR(port_number, S_IRUGO, show_port_number, NULL);
50
Alan Coxc97611c2008-07-22 11:09:48 +010051static int usb_serial_device_probe(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -070053 struct usb_serial_driver *driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 struct usb_serial_port *port;
55 int retval = 0;
56 int minor;
57
58 port = to_usb_serial_port(dev);
59 if (!port) {
60 retval = -ENODEV;
61 goto exit;
62 }
63
Bjørn Morkd5fd6502012-07-27 01:11:42 +020064 /* 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;
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 driver = port->serial->type;
70 if (driver->port_probe) {
Alan Coxc97611c2008-07-22 11:09:48 +010071 retval = driver->port_probe(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 if (retval)
Bjørn Morkd5fd6502012-07-27 01:11:42 +020073 goto exit_with_autopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 }
75
Roman Kagance0d9322007-08-24 20:22:52 +040076 retval = device_create_file(dev, &dev_attr_port_number);
Alan Sternc706ebd2009-06-02 11:54:11 -040077 if (retval) {
78 if (driver->port_remove)
79 retval = driver->port_remove(port);
Bjørn Morkd5fd6502012-07-27 01:11:42 +020080 goto exit_with_autopm;
Alan Sternc706ebd2009-06-02 11:54:11 -040081 }
Roman Kagance0d9322007-08-24 20:22:52 +040082
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 minor = port->number;
Alan Coxc97611c2008-07-22 11:09:48 +010084 tty_register_device(usb_serial_tty_driver, minor, dev);
85 dev_info(&port->serial->dev->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 "%s converter now attached to ttyUSB%d\n",
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -070087 driver->description, minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Bjørn Morkd5fd6502012-07-27 01:11:42 +020089exit_with_autopm:
90 usb_autopm_put_interface(port->serial->interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091exit:
92 return retval;
93}
94
Alan Coxc97611c2008-07-22 11:09:48 +010095static int usb_serial_device_remove(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -070097 struct usb_serial_driver *driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 struct usb_serial_port *port;
99 int retval = 0;
100 int minor;
101
102 port = to_usb_serial_port(dev);
Alan Coxc97611c2008-07-22 11:09:48 +0100103 if (!port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Bjørn Morkd5fd6502012-07-27 01:11:42 +0200106 /* make sure suspend/resume doesn't race against port_remove */
107 usb_autopm_get_interface(port->serial->interface);
108
Roman Kagance0d9322007-08-24 20:22:52 +0400109 device_remove_file(&port->dev, &dev_attr_port_number);
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 driver = port->serial->type;
Alan Sternc706ebd2009-06-02 11:54:11 -0400112 if (driver->port_remove)
Alan Coxc97611c2008-07-22 11:09:48 +0100113 retval = driver->port_remove(port);
Alan Sternc706ebd2009-06-02 11:54:11 -0400114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 minor = port->number;
Alan Coxc97611c2008-07-22 11:09:48 +0100116 tty_unregister_device(usb_serial_tty_driver, minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 dev_info(dev, "%s converter now disconnected from ttyUSB%d\n",
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700118 driver->description, minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Bjørn Morkd5fd6502012-07-27 01:11:42 +0200120 usb_autopm_put_interface(port->serial->interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 return retval;
122}
123
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100124static ssize_t store_new_id(struct device_driver *driver,
125 const char *buf, size_t count)
126{
127 struct usb_serial_driver *usb_drv = to_usb_serial_driver(driver);
128 ssize_t retval = usb_store_new_id(&usb_drv->dynids, driver, buf, count);
129
130 if (retval >= 0 && usb_drv->usb_driver != NULL)
131 retval = usb_store_new_id(&usb_drv->usb_driver->dynids,
132 &usb_drv->usb_driver->drvwrap.driver,
133 buf, count);
134 return retval;
135}
136
Bjørn Morkef206f32012-05-13 12:35:00 +0200137static ssize_t show_dynids(struct device_driver *driver, char *buf)
138{
139 struct usb_serial_driver *usb_drv = to_usb_serial_driver(driver);
140
141 return usb_show_dynids(&usb_drv->dynids, buf);
142}
143
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100144static struct driver_attribute drv_attrs[] = {
Bjørn Morkef206f32012-05-13 12:35:00 +0200145 __ATTR(new_id, S_IRUGO | S_IWUSR, show_dynids, store_new_id),
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100146 __ATTR_NULL,
147};
148
149static void free_dynids(struct usb_serial_driver *drv)
150{
151 struct usb_dynid *dynid, *n;
152
153 spin_lock(&drv->dynids.lock);
154 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
155 list_del(&dynid->node);
156 kfree(dynid);
157 }
158 spin_unlock(&drv->dynids.lock);
159}
160
Russell Kingac33bc32006-01-05 14:43:11 +0000161struct bus_type usb_serial_bus_type = {
162 .name = "usb-serial",
163 .match = usb_serial_device_match,
164 .probe = usb_serial_device_probe,
165 .remove = usb_serial_device_remove,
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100166 .drv_attrs = drv_attrs,
Russell Kingac33bc32006-01-05 14:43:11 +0000167};
168
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700169int usb_serial_bus_register(struct usb_serial_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
171 int retval;
172
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700173 driver->driver.bus = &usb_serial_bus_type;
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100174 spin_lock_init(&driver->dynids.lock);
175 INIT_LIST_HEAD(&driver->dynids.list);
176
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700177 retval = driver_register(&driver->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179 return retval;
180}
181
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700182void usb_serial_bus_deregister(struct usb_serial_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100184 free_dynids(driver);
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700185 driver_unregister(&driver->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
187