Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/kernel.h> |
| 12 | #include <linux/errno.h> |
| 13 | #include <linux/tty.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 14 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/module.h> |
| 16 | #include <linux/usb.h> |
Greg Kroah-Hartman | a969888 | 2006-07-11 21:22:58 -0700 | [diff] [blame] | 17 | #include <linux/usb/serial.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | |
Alan Cox | c97611c | 2008-07-22 11:09:48 +0100 | [diff] [blame] | 19 | static int usb_serial_device_match(struct device *dev, |
| 20 | struct device_driver *drv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | { |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 22 | struct usb_serial_driver *driver; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | 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 Cox | c97611c | 2008-07-22 11:09:48 +0100 | [diff] [blame] | 41 | static int usb_serial_device_probe(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | { |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 43 | struct usb_serial_driver *driver; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | struct usb_serial_port *port; |
Johan Hovold | ca4383a | 2015-02-18 10:34:51 +0700 | [diff] [blame] | 45 | struct device *tty_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | int retval = 0; |
| 47 | int minor; |
| 48 | |
| 49 | port = to_usb_serial_port(dev); |
Johan Hovold | d6f7f41 | 2015-02-18 11:04:46 +0700 | [diff] [blame] | 50 | if (!port) |
| 51 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
Bjørn Mork | d5fd650 | 2012-07-27 01:11:42 +0200 | [diff] [blame] | 53 | /* make sure suspend/resume doesn't race against port_probe */ |
| 54 | retval = usb_autopm_get_interface(port->serial->interface); |
| 55 | if (retval) |
Johan Hovold | d6f7f41 | 2015-02-18 11:04:46 +0700 | [diff] [blame] | 56 | return retval; |
Bjørn Mork | d5fd650 | 2012-07-27 01:11:42 +0200 | [diff] [blame] | 57 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | driver = port->serial->type; |
| 59 | if (driver->port_probe) { |
Alan Cox | c97611c | 2008-07-22 11:09:48 +0100 | [diff] [blame] | 60 | retval = driver->port_probe(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | if (retval) |
Johan Hovold | d6f7f41 | 2015-02-18 11:04:46 +0700 | [diff] [blame] | 62 | goto err_autopm_put; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Greg Kroah-Hartman | 1143832 | 2013-06-06 10:32:00 -0700 | [diff] [blame] | 65 | minor = port->minor; |
Johan Hovold | ca4383a | 2015-02-18 10:34:51 +0700 | [diff] [blame] | 66 | tty_dev = tty_register_device(usb_serial_tty_driver, minor, dev); |
| 67 | if (IS_ERR(tty_dev)) { |
| 68 | retval = PTR_ERR(tty_dev); |
Johan Hovold | d6f7f41 | 2015-02-18 11:04:46 +0700 | [diff] [blame] | 69 | goto err_port_remove; |
Johan Hovold | ca4383a | 2015-02-18 10:34:51 +0700 | [diff] [blame] | 70 | } |
| 71 | |
Johan Hovold | d6f7f41 | 2015-02-18 11:04:46 +0700 | [diff] [blame] | 72 | usb_autopm_put_interface(port->serial->interface); |
| 73 | |
Alan Cox | c97611c | 2008-07-22 11:09:48 +0100 | [diff] [blame] | 74 | dev_info(&port->serial->dev->dev, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | "%s converter now attached to ttyUSB%d\n", |
Greg Kroah-Hartman | 269bda1 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 76 | driver->description, minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | |
Johan Hovold | d6f7f41 | 2015-02-18 11:04:46 +0700 | [diff] [blame] | 78 | return 0; |
| 79 | |
| 80 | err_port_remove: |
| 81 | if (driver->port_remove) |
| 82 | driver->port_remove(port); |
| 83 | err_autopm_put: |
Bjørn Mork | d5fd650 | 2012-07-27 01:11:42 +0200 | [diff] [blame] | 84 | usb_autopm_put_interface(port->serial->interface); |
Johan Hovold | d6f7f41 | 2015-02-18 11:04:46 +0700 | [diff] [blame] | 85 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | return retval; |
| 87 | } |
| 88 | |
Alan Cox | c97611c | 2008-07-22 11:09:48 +0100 | [diff] [blame] | 89 | static int usb_serial_device_remove(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | { |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 91 | struct usb_serial_driver *driver; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | struct usb_serial_port *port; |
| 93 | int retval = 0; |
| 94 | int minor; |
Johan Hovold | c14829f | 2014-05-26 19:23:33 +0200 | [diff] [blame] | 95 | int autopm_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | |
| 97 | port = to_usb_serial_port(dev); |
Alan Cox | c97611c | 2008-07-22 11:09:48 +0100 | [diff] [blame] | 98 | if (!port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | |
Johan Hovold | c14829f | 2014-05-26 19:23:33 +0200 | [diff] [blame] | 101 | /* |
| 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 Mork | d5fd650 | 2012-07-27 01:11:42 +0200 | [diff] [blame] | 108 | |
Greg Kroah-Hartman | 1143832 | 2013-06-06 10:32:00 -0700 | [diff] [blame] | 109 | minor = port->minor; |
Johan Hovold | 5cb27dd | 2013-03-21 12:36:50 +0100 | [diff] [blame] | 110 | tty_unregister_device(usb_serial_tty_driver, minor); |
| 111 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | driver = port->serial->type; |
Alan Stern | c706ebd | 2009-06-02 11:54:11 -0400 | [diff] [blame] | 113 | if (driver->port_remove) |
Alan Cox | c97611c | 2008-07-22 11:09:48 +0100 | [diff] [blame] | 114 | retval = driver->port_remove(port); |
Alan Stern | c706ebd | 2009-06-02 11:54:11 -0400 | [diff] [blame] | 115 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | dev_info(dev, "%s converter now disconnected from ttyUSB%d\n", |
Greg Kroah-Hartman | 269bda1 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 117 | driver->description, minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | |
Johan Hovold | c14829f | 2014-05-26 19:23:33 +0200 | [diff] [blame] | 119 | if (!autopm_err) |
| 120 | usb_autopm_put_interface(port->serial->interface); |
| 121 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | return retval; |
| 123 | } |
| 124 | |
Greg Kroah-Hartman | d13a280 | 2013-08-23 14:24:36 -0700 | [diff] [blame] | 125 | static ssize_t new_id_store(struct device_driver *driver, |
Greg Kroah-Hartman | 93bacef | 2006-12-17 21:50:23 +0100 | [diff] [blame] | 126 | const char *buf, size_t count) |
| 127 | { |
| 128 | struct usb_serial_driver *usb_drv = to_usb_serial_driver(driver); |
Wolfram Sang | 2fc82c2 | 2014-01-10 19:36:42 +0100 | [diff] [blame] | 129 | ssize_t retval = usb_store_new_id(&usb_drv->dynids, usb_drv->id_table, |
| 130 | driver, buf, count); |
Greg Kroah-Hartman | 93bacef | 2006-12-17 21:50:23 +0100 | [diff] [blame] | 131 | |
| 132 | if (retval >= 0 && usb_drv->usb_driver != NULL) |
| 133 | retval = usb_store_new_id(&usb_drv->usb_driver->dynids, |
Wolfram Sang | 2fc82c2 | 2014-01-10 19:36:42 +0100 | [diff] [blame] | 134 | usb_drv->usb_driver->id_table, |
Greg Kroah-Hartman | 93bacef | 2006-12-17 21:50:23 +0100 | [diff] [blame] | 135 | &usb_drv->usb_driver->drvwrap.driver, |
| 136 | buf, count); |
| 137 | return retval; |
| 138 | } |
| 139 | |
Greg Kroah-Hartman | d13a280 | 2013-08-23 14:24:36 -0700 | [diff] [blame] | 140 | static ssize_t new_id_show(struct device_driver *driver, char *buf) |
Bjørn Mork | ef206f3 | 2012-05-13 12:35:00 +0200 | [diff] [blame] | 141 | { |
| 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-Hartman | d13a280 | 2013-08-23 14:24:36 -0700 | [diff] [blame] | 146 | static DRIVER_ATTR_RW(new_id); |
Bjørn Mork | ef206f3 | 2012-05-13 12:35:00 +0200 | [diff] [blame] | 147 | |
Greg Kroah-Hartman | d13a280 | 2013-08-23 14:24:36 -0700 | [diff] [blame] | 148 | static struct attribute *usb_serial_drv_attrs[] = { |
| 149 | &driver_attr_new_id.attr, |
| 150 | NULL, |
Greg Kroah-Hartman | 93bacef | 2006-12-17 21:50:23 +0100 | [diff] [blame] | 151 | }; |
Greg Kroah-Hartman | d13a280 | 2013-08-23 14:24:36 -0700 | [diff] [blame] | 152 | ATTRIBUTE_GROUPS(usb_serial_drv); |
Greg Kroah-Hartman | 93bacef | 2006-12-17 21:50:23 +0100 | [diff] [blame] | 153 | |
| 154 | static 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 King | ac33bc3 | 2006-01-05 14:43:11 +0000 | [diff] [blame] | 166 | struct 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-Hartman | d13a280 | 2013-08-23 14:24:36 -0700 | [diff] [blame] | 171 | .drv_groups = usb_serial_drv_groups, |
Russell King | ac33bc3 | 2006-01-05 14:43:11 +0000 | [diff] [blame] | 172 | }; |
| 173 | |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 174 | int usb_serial_bus_register(struct usb_serial_driver *driver) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | { |
| 176 | int retval; |
| 177 | |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 178 | driver->driver.bus = &usb_serial_bus_type; |
Greg Kroah-Hartman | 93bacef | 2006-12-17 21:50:23 +0100 | [diff] [blame] | 179 | spin_lock_init(&driver->dynids.lock); |
| 180 | INIT_LIST_HEAD(&driver->dynids.list); |
| 181 | |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 182 | retval = driver_register(&driver->driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | |
| 184 | return retval; |
| 185 | } |
| 186 | |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 187 | void usb_serial_bus_deregister(struct usb_serial_driver *driver) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | { |
Greg Kroah-Hartman | 93bacef | 2006-12-17 21:50:23 +0100 | [diff] [blame] | 189 | free_dynids(driver); |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 190 | driver_unregister(&driver->driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | } |
| 192 | |