blob: 9374bd2aba20759f4bb4b03d2a9d51058aead9fd [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;
Johan Hovoldc14829f2014-05-26 19:23:33 +0200100 int autopm_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
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
Johan Hovoldc14829f2014-05-26 19:23:33 +0200106 /*
107 * Make sure suspend/resume doesn't race against port_remove.
108 *
109 * Note that no further runtime PM callbacks will be made if
110 * autopm_get fails.
111 */
112 autopm_err = usb_autopm_get_interface(port->serial->interface);
Bjørn Morkd5fd6502012-07-27 01:11:42 +0200113
Greg Kroah-Hartman11438322013-06-06 10:32:00 -0700114 minor = port->minor;
Johan Hovold5cb27dd2013-03-21 12:36:50 +0100115 tty_unregister_device(usb_serial_tty_driver, minor);
116
Roman Kagance0d9322007-08-24 20:22:52 +0400117 device_remove_file(&port->dev, &dev_attr_port_number);
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 driver = port->serial->type;
Alan Sternc706ebd2009-06-02 11:54:11 -0400120 if (driver->port_remove)
Alan Coxc97611c2008-07-22 11:09:48 +0100121 retval = driver->port_remove(port);
Alan Sternc706ebd2009-06-02 11:54:11 -0400122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 dev_info(dev, "%s converter now disconnected from ttyUSB%d\n",
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700124 driver->description, minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Johan Hovoldc14829f2014-05-26 19:23:33 +0200126 if (!autopm_err)
127 usb_autopm_put_interface(port->serial->interface);
128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 return retval;
130}
131
Greg Kroah-Hartmand13a2802013-08-23 14:24:36 -0700132static ssize_t new_id_store(struct device_driver *driver,
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100133 const char *buf, size_t count)
134{
135 struct usb_serial_driver *usb_drv = to_usb_serial_driver(driver);
Wolfram Sang2fc82c22014-01-10 19:36:42 +0100136 ssize_t retval = usb_store_new_id(&usb_drv->dynids, usb_drv->id_table,
137 driver, buf, count);
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100138
139 if (retval >= 0 && usb_drv->usb_driver != NULL)
140 retval = usb_store_new_id(&usb_drv->usb_driver->dynids,
Wolfram Sang2fc82c22014-01-10 19:36:42 +0100141 usb_drv->usb_driver->id_table,
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100142 &usb_drv->usb_driver->drvwrap.driver,
143 buf, count);
144 return retval;
145}
146
Greg Kroah-Hartmand13a2802013-08-23 14:24:36 -0700147static ssize_t new_id_show(struct device_driver *driver, char *buf)
Bjørn Morkef206f32012-05-13 12:35:00 +0200148{
149 struct usb_serial_driver *usb_drv = to_usb_serial_driver(driver);
150
151 return usb_show_dynids(&usb_drv->dynids, buf);
152}
Greg Kroah-Hartmand13a2802013-08-23 14:24:36 -0700153static DRIVER_ATTR_RW(new_id);
Bjørn Morkef206f32012-05-13 12:35:00 +0200154
Greg Kroah-Hartmand13a2802013-08-23 14:24:36 -0700155static struct attribute *usb_serial_drv_attrs[] = {
156 &driver_attr_new_id.attr,
157 NULL,
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100158};
Greg Kroah-Hartmand13a2802013-08-23 14:24:36 -0700159ATTRIBUTE_GROUPS(usb_serial_drv);
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100160
161static void free_dynids(struct usb_serial_driver *drv)
162{
163 struct usb_dynid *dynid, *n;
164
165 spin_lock(&drv->dynids.lock);
166 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
167 list_del(&dynid->node);
168 kfree(dynid);
169 }
170 spin_unlock(&drv->dynids.lock);
171}
172
Russell Kingac33bc32006-01-05 14:43:11 +0000173struct bus_type usb_serial_bus_type = {
174 .name = "usb-serial",
175 .match = usb_serial_device_match,
176 .probe = usb_serial_device_probe,
177 .remove = usb_serial_device_remove,
Greg Kroah-Hartmand13a2802013-08-23 14:24:36 -0700178 .drv_groups = usb_serial_drv_groups,
Russell Kingac33bc32006-01-05 14:43:11 +0000179};
180
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700181int usb_serial_bus_register(struct usb_serial_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
183 int retval;
184
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700185 driver->driver.bus = &usb_serial_bus_type;
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100186 spin_lock_init(&driver->dynids.lock);
187 INIT_LIST_HEAD(&driver->dynids.list);
188
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700189 retval = driver_register(&driver->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191 return retval;
192}
193
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700194void usb_serial_bus_deregister(struct usb_serial_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100196 free_dynids(driver);
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700197 driver_unregister(&driver->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}
199