blob: 35a2373cde67bbbe01ce7c7afc8fc23a27b4eea5 [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
Greg Kroah-Hartman154547c2013-08-23 16:18:34 -070041static ssize_t port_number_show(struct device *dev,
Roman Kagance0d9322007-08-24 20:22:52 +040042 struct device_attribute *attr, char *buf)
43{
44 struct usb_serial_port *port = to_usb_serial_port(dev);
45
Greg Kroah-Hartman11438322013-06-06 10:32:00 -070046 return sprintf(buf, "%d\n", port->port_number);
Roman Kagance0d9322007-08-24 20:22:52 +040047}
Greg Kroah-Hartman154547c2013-08-23 16:18:34 -070048static DEVICE_ATTR_RO(port_number);
Roman Kagance0d9322007-08-24 20:22:52 +040049
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 }
62
Bjørn Morkd5fd6502012-07-27 01:11:42 +020063 /* make sure suspend/resume doesn't race against port_probe */
64 retval = usb_autopm_get_interface(port->serial->interface);
65 if (retval)
66 goto exit;
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 driver = port->serial->type;
69 if (driver->port_probe) {
Alan Coxc97611c2008-07-22 11:09:48 +010070 retval = driver->port_probe(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 if (retval)
Bjørn Morkd5fd6502012-07-27 01:11:42 +020072 goto exit_with_autopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 }
74
Roman Kagance0d9322007-08-24 20:22:52 +040075 retval = device_create_file(dev, &dev_attr_port_number);
Alan Sternc706ebd2009-06-02 11:54:11 -040076 if (retval) {
77 if (driver->port_remove)
78 retval = driver->port_remove(port);
Bjørn Morkd5fd6502012-07-27 01:11:42 +020079 goto exit_with_autopm;
Alan Sternc706ebd2009-06-02 11:54:11 -040080 }
Roman Kagance0d9322007-08-24 20:22:52 +040081
Greg Kroah-Hartman11438322013-06-06 10:32:00 -070082 minor = port->minor;
Alan Coxc97611c2008-07-22 11:09:48 +010083 tty_register_device(usb_serial_tty_driver, minor, dev);
84 dev_info(&port->serial->dev->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 "%s converter now attached to ttyUSB%d\n",
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -070086 driver->description, minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Bjørn Morkd5fd6502012-07-27 01:11:42 +020088exit_with_autopm:
89 usb_autopm_put_interface(port->serial->interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090exit:
91 return retval;
92}
93
Alan Coxc97611c2008-07-22 11:09:48 +010094static int usb_serial_device_remove(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -070096 struct usb_serial_driver *driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 struct usb_serial_port *port;
98 int retval = 0;
99 int minor;
100
101 port = to_usb_serial_port(dev);
Alan Coxc97611c2008-07-22 11:09:48 +0100102 if (!port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Bjørn Morkd5fd6502012-07-27 01:11:42 +0200105 /* make sure suspend/resume doesn't race against port_remove */
106 usb_autopm_get_interface(port->serial->interface);
107
Greg Kroah-Hartman11438322013-06-06 10:32:00 -0700108 minor = port->minor;
Johan Hovold5cb27dd2013-03-21 12:36:50 +0100109 tty_unregister_device(usb_serial_tty_driver, minor);
110
Roman Kagance0d9322007-08-24 20:22:52 +0400111 device_remove_file(&port->dev, &dev_attr_port_number);
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 driver = port->serial->type;
Alan Sternc706ebd2009-06-02 11:54:11 -0400114 if (driver->port_remove)
Alan Coxc97611c2008-07-22 11:09:48 +0100115 retval = driver->port_remove(port);
Alan Sternc706ebd2009-06-02 11:54:11 -0400116
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-Hartmand13a2802013-08-23 14:24:36 -0700124static ssize_t new_id_store(struct device_driver *driver,
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100125 const char *buf, size_t count)
126{
127 struct usb_serial_driver *usb_drv = to_usb_serial_driver(driver);
Wolfram Sang2fc82c22014-01-10 19:36:42 +0100128 ssize_t retval = usb_store_new_id(&usb_drv->dynids, usb_drv->id_table,
129 driver, buf, count);
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100130
131 if (retval >= 0 && usb_drv->usb_driver != NULL)
132 retval = usb_store_new_id(&usb_drv->usb_driver->dynids,
Wolfram Sang2fc82c22014-01-10 19:36:42 +0100133 usb_drv->usb_driver->id_table,
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100134 &usb_drv->usb_driver->drvwrap.driver,
135 buf, count);
136 return retval;
137}
138
Greg Kroah-Hartmand13a2802013-08-23 14:24:36 -0700139static ssize_t new_id_show(struct device_driver *driver, char *buf)
Bjørn Morkef206f32012-05-13 12:35:00 +0200140{
141 struct usb_serial_driver *usb_drv = to_usb_serial_driver(driver);
142
143 return usb_show_dynids(&usb_drv->dynids, buf);
144}
Greg Kroah-Hartmand13a2802013-08-23 14:24:36 -0700145static DRIVER_ATTR_RW(new_id);
Bjørn Morkef206f32012-05-13 12:35:00 +0200146
Greg Kroah-Hartmand13a2802013-08-23 14:24:36 -0700147static struct attribute *usb_serial_drv_attrs[] = {
148 &driver_attr_new_id.attr,
149 NULL,
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100150};
Greg Kroah-Hartmand13a2802013-08-23 14:24:36 -0700151ATTRIBUTE_GROUPS(usb_serial_drv);
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100152
153static void free_dynids(struct usb_serial_driver *drv)
154{
155 struct usb_dynid *dynid, *n;
156
157 spin_lock(&drv->dynids.lock);
158 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
159 list_del(&dynid->node);
160 kfree(dynid);
161 }
162 spin_unlock(&drv->dynids.lock);
163}
164
Russell Kingac33bc32006-01-05 14:43:11 +0000165struct bus_type usb_serial_bus_type = {
166 .name = "usb-serial",
167 .match = usb_serial_device_match,
168 .probe = usb_serial_device_probe,
169 .remove = usb_serial_device_remove,
Greg Kroah-Hartmand13a2802013-08-23 14:24:36 -0700170 .drv_groups = usb_serial_drv_groups,
Russell Kingac33bc32006-01-05 14:43:11 +0000171};
172
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700173int usb_serial_bus_register(struct usb_serial_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
175 int retval;
176
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700177 driver->driver.bus = &usb_serial_bus_type;
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100178 spin_lock_init(&driver->dynids.lock);
179 INIT_LIST_HEAD(&driver->dynids.list);
180
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700181 retval = driver_register(&driver->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183 return retval;
184}
185
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700186void usb_serial_bus_deregister(struct usb_serial_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100188 free_dynids(driver);
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700189 driver_unregister(&driver->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
191