blob: 8936a83c96cd60c8a226e0c96a9908c06d356773 [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
Alan Coxc97611c2008-07-22 11:09:48 +010041static int usb_serial_device_probe(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -070043 struct usb_serial_driver *driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 struct usb_serial_port *port;
Johan Hovoldca4383a2015-02-18 10:34:51 +070045 struct device *tty_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 int retval = 0;
47 int minor;
48
49 port = to_usb_serial_port(dev);
Johan Hovoldd6f7f412015-02-18 11:04:46 +070050 if (!port)
51 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Bjørn Morkd5fd6502012-07-27 01:11:42 +020053 /* make sure suspend/resume doesn't race against port_probe */
54 retval = usb_autopm_get_interface(port->serial->interface);
55 if (retval)
Johan Hovoldd6f7f412015-02-18 11:04:46 +070056 return retval;
Bjørn Morkd5fd6502012-07-27 01:11:42 +020057
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 driver = port->serial->type;
59 if (driver->port_probe) {
Alan Coxc97611c2008-07-22 11:09:48 +010060 retval = driver->port_probe(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 if (retval)
Johan Hovoldd6f7f412015-02-18 11:04:46 +070062 goto err_autopm_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 }
64
Greg Kroah-Hartman11438322013-06-06 10:32:00 -070065 minor = port->minor;
Johan Hovoldca4383a2015-02-18 10:34:51 +070066 tty_dev = tty_register_device(usb_serial_tty_driver, minor, dev);
67 if (IS_ERR(tty_dev)) {
68 retval = PTR_ERR(tty_dev);
Johan Hovoldd6f7f412015-02-18 11:04:46 +070069 goto err_port_remove;
Johan Hovoldca4383a2015-02-18 10:34:51 +070070 }
71
Johan Hovoldd6f7f412015-02-18 11:04:46 +070072 usb_autopm_put_interface(port->serial->interface);
73
Alan Coxc97611c2008-07-22 11:09:48 +010074 dev_info(&port->serial->dev->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 "%s converter now attached to ttyUSB%d\n",
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -070076 driver->description, minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Johan Hovoldd6f7f412015-02-18 11:04:46 +070078 return 0;
79
80err_port_remove:
81 if (driver->port_remove)
82 driver->port_remove(port);
83err_autopm_put:
Bjørn Morkd5fd6502012-07-27 01:11:42 +020084 usb_autopm_put_interface(port->serial->interface);
Johan Hovoldd6f7f412015-02-18 11:04:46 +070085
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 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;
Johan Hovoldc14829f2014-05-26 19:23:33 +020095 int autopm_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97 port = to_usb_serial_port(dev);
Alan Coxc97611c2008-07-22 11:09:48 +010098 if (!port)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Johan Hovoldc14829f2014-05-26 19:23:33 +0200101 /*
102 * Make sure suspend/resume doesn't race against port_remove.
103 *
104 * Note that no further runtime PM callbacks will be made if
105 * autopm_get fails.
106 */
107 autopm_err = usb_autopm_get_interface(port->serial->interface);
Bjørn Morkd5fd6502012-07-27 01:11:42 +0200108
Greg Kroah-Hartman11438322013-06-06 10:32:00 -0700109 minor = port->minor;
Johan Hovold5cb27dd2013-03-21 12:36:50 +0100110 tty_unregister_device(usb_serial_tty_driver, minor);
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 driver = port->serial->type;
Alan Sternc706ebd2009-06-02 11:54:11 -0400113 if (driver->port_remove)
Alan Coxc97611c2008-07-22 11:09:48 +0100114 retval = driver->port_remove(port);
Alan Sternc706ebd2009-06-02 11:54:11 -0400115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 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
Johan Hovoldc14829f2014-05-26 19:23:33 +0200119 if (!autopm_err)
120 usb_autopm_put_interface(port->serial->interface);
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 return retval;
123}
124
Greg Kroah-Hartmand13a2802013-08-23 14:24:36 -0700125static ssize_t new_id_store(struct device_driver *driver,
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100126 const char *buf, size_t count)
127{
128 struct usb_serial_driver *usb_drv = to_usb_serial_driver(driver);
Wolfram Sang2fc82c22014-01-10 19:36:42 +0100129 ssize_t retval = usb_store_new_id(&usb_drv->dynids, usb_drv->id_table,
130 driver, buf, count);
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100131
132 if (retval >= 0 && usb_drv->usb_driver != NULL)
133 retval = usb_store_new_id(&usb_drv->usb_driver->dynids,
Wolfram Sang2fc82c22014-01-10 19:36:42 +0100134 usb_drv->usb_driver->id_table,
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100135 &usb_drv->usb_driver->drvwrap.driver,
136 buf, count);
137 return retval;
138}
139
Greg Kroah-Hartmand13a2802013-08-23 14:24:36 -0700140static ssize_t new_id_show(struct device_driver *driver, char *buf)
Bjørn Morkef206f32012-05-13 12:35:00 +0200141{
142 struct usb_serial_driver *usb_drv = to_usb_serial_driver(driver);
143
144 return usb_show_dynids(&usb_drv->dynids, buf);
145}
Greg Kroah-Hartmand13a2802013-08-23 14:24:36 -0700146static DRIVER_ATTR_RW(new_id);
Bjørn Morkef206f32012-05-13 12:35:00 +0200147
Greg Kroah-Hartmand13a2802013-08-23 14:24:36 -0700148static struct attribute *usb_serial_drv_attrs[] = {
149 &driver_attr_new_id.attr,
150 NULL,
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100151};
Greg Kroah-Hartmand13a2802013-08-23 14:24:36 -0700152ATTRIBUTE_GROUPS(usb_serial_drv);
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100153
154static void free_dynids(struct usb_serial_driver *drv)
155{
156 struct usb_dynid *dynid, *n;
157
158 spin_lock(&drv->dynids.lock);
159 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
160 list_del(&dynid->node);
161 kfree(dynid);
162 }
163 spin_unlock(&drv->dynids.lock);
164}
165
Russell Kingac33bc32006-01-05 14:43:11 +0000166struct bus_type usb_serial_bus_type = {
167 .name = "usb-serial",
168 .match = usb_serial_device_match,
169 .probe = usb_serial_device_probe,
170 .remove = usb_serial_device_remove,
Greg Kroah-Hartmand13a2802013-08-23 14:24:36 -0700171 .drv_groups = usb_serial_drv_groups,
Russell Kingac33bc32006-01-05 14:43:11 +0000172};
173
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700174int usb_serial_bus_register(struct usb_serial_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
176 int retval;
177
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700178 driver->driver.bus = &usb_serial_bus_type;
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100179 spin_lock_init(&driver->dynids.lock);
180 INIT_LIST_HEAD(&driver->dynids.list);
181
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700182 retval = driver_register(&driver->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184 return retval;
185}
186
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700187void usb_serial_bus_deregister(struct usb_serial_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100189 free_dynids(driver);
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700190 driver_unregister(&driver->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
192