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 | |
Greg Kroah-Hartman | 154547c | 2013-08-23 16:18:34 -0700 | [diff] [blame] | 41 | static ssize_t port_number_show(struct device *dev, |
Roman Kagan | ce0d932 | 2007-08-24 20:22:52 +0400 | [diff] [blame] | 42 | struct device_attribute *attr, char *buf) |
| 43 | { |
| 44 | struct usb_serial_port *port = to_usb_serial_port(dev); |
| 45 | |
Greg Kroah-Hartman | 1143832 | 2013-06-06 10:32:00 -0700 | [diff] [blame] | 46 | return sprintf(buf, "%d\n", port->port_number); |
Roman Kagan | ce0d932 | 2007-08-24 20:22:52 +0400 | [diff] [blame] | 47 | } |
Greg Kroah-Hartman | 154547c | 2013-08-23 16:18:34 -0700 | [diff] [blame] | 48 | static DEVICE_ATTR_RO(port_number); |
Roman Kagan | ce0d932 | 2007-08-24 20:22:52 +0400 | [diff] [blame] | 49 | |
Alan Cox | c97611c | 2008-07-22 11:09:48 +0100 | [diff] [blame] | 50 | static int usb_serial_device_probe(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | { |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 52 | struct usb_serial_driver *driver; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | 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 Mork | d5fd650 | 2012-07-27 01:11:42 +0200 | [diff] [blame] | 63 | /* 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | driver = port->serial->type; |
| 69 | if (driver->port_probe) { |
Alan Cox | c97611c | 2008-07-22 11:09:48 +0100 | [diff] [blame] | 70 | retval = driver->port_probe(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | if (retval) |
Bjørn Mork | d5fd650 | 2012-07-27 01:11:42 +0200 | [diff] [blame] | 72 | goto exit_with_autopm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Roman Kagan | ce0d932 | 2007-08-24 20:22:52 +0400 | [diff] [blame] | 75 | retval = device_create_file(dev, &dev_attr_port_number); |
Alan Stern | c706ebd | 2009-06-02 11:54:11 -0400 | [diff] [blame] | 76 | if (retval) { |
| 77 | if (driver->port_remove) |
Johan Hovold | 07fdfc5 | 2015-02-18 10:34:50 +0700 | [diff] [blame^] | 78 | driver->port_remove(port); |
Bjørn Mork | d5fd650 | 2012-07-27 01:11:42 +0200 | [diff] [blame] | 79 | goto exit_with_autopm; |
Alan Stern | c706ebd | 2009-06-02 11:54:11 -0400 | [diff] [blame] | 80 | } |
Roman Kagan | ce0d932 | 2007-08-24 20:22:52 +0400 | [diff] [blame] | 81 | |
Greg Kroah-Hartman | 1143832 | 2013-06-06 10:32:00 -0700 | [diff] [blame] | 82 | minor = port->minor; |
Alan Cox | c97611c | 2008-07-22 11:09:48 +0100 | [diff] [blame] | 83 | tty_register_device(usb_serial_tty_driver, minor, dev); |
| 84 | dev_info(&port->serial->dev->dev, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | "%s converter now attached to ttyUSB%d\n", |
Greg Kroah-Hartman | 269bda1 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 86 | driver->description, minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | |
Bjørn Mork | d5fd650 | 2012-07-27 01:11:42 +0200 | [diff] [blame] | 88 | exit_with_autopm: |
| 89 | usb_autopm_put_interface(port->serial->interface); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | exit: |
| 91 | return retval; |
| 92 | } |
| 93 | |
Alan Cox | c97611c | 2008-07-22 11:09:48 +0100 | [diff] [blame] | 94 | static int usb_serial_device_remove(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | { |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 96 | struct usb_serial_driver *driver; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | struct usb_serial_port *port; |
| 98 | int retval = 0; |
| 99 | int minor; |
Johan Hovold | c14829f | 2014-05-26 19:23:33 +0200 | [diff] [blame] | 100 | int autopm_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | |
| 102 | port = to_usb_serial_port(dev); |
Alan Cox | c97611c | 2008-07-22 11:09:48 +0100 | [diff] [blame] | 103 | if (!port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | |
Johan Hovold | c14829f | 2014-05-26 19:23:33 +0200 | [diff] [blame] | 106 | /* |
| 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 Mork | d5fd650 | 2012-07-27 01:11:42 +0200 | [diff] [blame] | 113 | |
Greg Kroah-Hartman | 1143832 | 2013-06-06 10:32:00 -0700 | [diff] [blame] | 114 | minor = port->minor; |
Johan Hovold | 5cb27dd | 2013-03-21 12:36:50 +0100 | [diff] [blame] | 115 | tty_unregister_device(usb_serial_tty_driver, minor); |
| 116 | |
Roman Kagan | ce0d932 | 2007-08-24 20:22:52 +0400 | [diff] [blame] | 117 | device_remove_file(&port->dev, &dev_attr_port_number); |
| 118 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | driver = port->serial->type; |
Alan Stern | c706ebd | 2009-06-02 11:54:11 -0400 | [diff] [blame] | 120 | if (driver->port_remove) |
Alan Cox | c97611c | 2008-07-22 11:09:48 +0100 | [diff] [blame] | 121 | retval = driver->port_remove(port); |
Alan Stern | c706ebd | 2009-06-02 11:54:11 -0400 | [diff] [blame] | 122 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | dev_info(dev, "%s converter now disconnected from ttyUSB%d\n", |
Greg Kroah-Hartman | 269bda1 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 124 | driver->description, minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | |
Johan Hovold | c14829f | 2014-05-26 19:23:33 +0200 | [diff] [blame] | 126 | if (!autopm_err) |
| 127 | usb_autopm_put_interface(port->serial->interface); |
| 128 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | return retval; |
| 130 | } |
| 131 | |
Greg Kroah-Hartman | d13a280 | 2013-08-23 14:24:36 -0700 | [diff] [blame] | 132 | static ssize_t new_id_store(struct device_driver *driver, |
Greg Kroah-Hartman | 93bacef | 2006-12-17 21:50:23 +0100 | [diff] [blame] | 133 | const char *buf, size_t count) |
| 134 | { |
| 135 | struct usb_serial_driver *usb_drv = to_usb_serial_driver(driver); |
Wolfram Sang | 2fc82c2 | 2014-01-10 19:36:42 +0100 | [diff] [blame] | 136 | ssize_t retval = usb_store_new_id(&usb_drv->dynids, usb_drv->id_table, |
| 137 | driver, buf, count); |
Greg Kroah-Hartman | 93bacef | 2006-12-17 21:50:23 +0100 | [diff] [blame] | 138 | |
| 139 | if (retval >= 0 && usb_drv->usb_driver != NULL) |
| 140 | retval = usb_store_new_id(&usb_drv->usb_driver->dynids, |
Wolfram Sang | 2fc82c2 | 2014-01-10 19:36:42 +0100 | [diff] [blame] | 141 | usb_drv->usb_driver->id_table, |
Greg Kroah-Hartman | 93bacef | 2006-12-17 21:50:23 +0100 | [diff] [blame] | 142 | &usb_drv->usb_driver->drvwrap.driver, |
| 143 | buf, count); |
| 144 | return retval; |
| 145 | } |
| 146 | |
Greg Kroah-Hartman | d13a280 | 2013-08-23 14:24:36 -0700 | [diff] [blame] | 147 | 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] | 148 | { |
| 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-Hartman | d13a280 | 2013-08-23 14:24:36 -0700 | [diff] [blame] | 153 | static DRIVER_ATTR_RW(new_id); |
Bjørn Mork | ef206f3 | 2012-05-13 12:35:00 +0200 | [diff] [blame] | 154 | |
Greg Kroah-Hartman | d13a280 | 2013-08-23 14:24:36 -0700 | [diff] [blame] | 155 | static struct attribute *usb_serial_drv_attrs[] = { |
| 156 | &driver_attr_new_id.attr, |
| 157 | NULL, |
Greg Kroah-Hartman | 93bacef | 2006-12-17 21:50:23 +0100 | [diff] [blame] | 158 | }; |
Greg Kroah-Hartman | d13a280 | 2013-08-23 14:24:36 -0700 | [diff] [blame] | 159 | ATTRIBUTE_GROUPS(usb_serial_drv); |
Greg Kroah-Hartman | 93bacef | 2006-12-17 21:50:23 +0100 | [diff] [blame] | 160 | |
| 161 | static 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 King | ac33bc3 | 2006-01-05 14:43:11 +0000 | [diff] [blame] | 173 | struct 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-Hartman | d13a280 | 2013-08-23 14:24:36 -0700 | [diff] [blame] | 178 | .drv_groups = usb_serial_drv_groups, |
Russell King | ac33bc3 | 2006-01-05 14:43:11 +0000 | [diff] [blame] | 179 | }; |
| 180 | |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 181 | int usb_serial_bus_register(struct usb_serial_driver *driver) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | { |
| 183 | int retval; |
| 184 | |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 185 | driver->driver.bus = &usb_serial_bus_type; |
Greg Kroah-Hartman | 93bacef | 2006-12-17 21:50:23 +0100 | [diff] [blame] | 186 | spin_lock_init(&driver->dynids.lock); |
| 187 | INIT_LIST_HEAD(&driver->dynids.list); |
| 188 | |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 189 | retval = driver_register(&driver->driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | |
| 191 | return retval; |
| 192 | } |
| 193 | |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 194 | void usb_serial_bus_deregister(struct usb_serial_driver *driver) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | { |
Greg Kroah-Hartman | 93bacef | 2006-12-17 21:50:23 +0100 | [diff] [blame] | 196 | free_dynids(driver); |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 197 | driver_unregister(&driver->driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | } |
| 199 | |