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 | |
Roman Kagan | ce0d932 | 2007-08-24 20:22:52 +0400 | [diff] [blame] | 41 | static ssize_t show_port_number(struct device *dev, |
| 42 | struct device_attribute *attr, char *buf) |
| 43 | { |
| 44 | struct usb_serial_port *port = to_usb_serial_port(dev); |
| 45 | |
| 46 | return sprintf(buf, "%d\n", port->number - port->serial->minor); |
| 47 | } |
| 48 | |
| 49 | static DEVICE_ATTR(port_number, S_IRUGO, show_port_number, NULL); |
| 50 | |
Alan Cox | c97611c | 2008-07-22 11:09:48 +0100 | [diff] [blame] | 51 | static int usb_serial_device_probe(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | { |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 53 | struct usb_serial_driver *driver; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | struct usb_serial_port *port; |
| 55 | int retval = 0; |
| 56 | int minor; |
| 57 | |
| 58 | port = to_usb_serial_port(dev); |
| 59 | if (!port) { |
| 60 | retval = -ENODEV; |
| 61 | goto exit; |
| 62 | } |
| 63 | |
Bjørn Mork | d5fd650 | 2012-07-27 01:11:42 +0200 | [diff] [blame] | 64 | /* make sure suspend/resume doesn't race against port_probe */ |
| 65 | retval = usb_autopm_get_interface(port->serial->interface); |
| 66 | if (retval) |
| 67 | goto exit; |
| 68 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | driver = port->serial->type; |
| 70 | if (driver->port_probe) { |
Alan Cox | c97611c | 2008-07-22 11:09:48 +0100 | [diff] [blame] | 71 | retval = driver->port_probe(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | if (retval) |
Bjørn Mork | d5fd650 | 2012-07-27 01:11:42 +0200 | [diff] [blame] | 73 | goto exit_with_autopm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Roman Kagan | ce0d932 | 2007-08-24 20:22:52 +0400 | [diff] [blame] | 76 | retval = device_create_file(dev, &dev_attr_port_number); |
Alan Stern | c706ebd | 2009-06-02 11:54:11 -0400 | [diff] [blame] | 77 | if (retval) { |
| 78 | if (driver->port_remove) |
| 79 | retval = driver->port_remove(port); |
Bjørn Mork | d5fd650 | 2012-07-27 01:11:42 +0200 | [diff] [blame] | 80 | goto exit_with_autopm; |
Alan Stern | c706ebd | 2009-06-02 11:54:11 -0400 | [diff] [blame] | 81 | } |
Roman Kagan | ce0d932 | 2007-08-24 20:22:52 +0400 | [diff] [blame] | 82 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | minor = port->number; |
Alan Cox | c97611c | 2008-07-22 11:09:48 +0100 | [diff] [blame] | 84 | tty_register_device(usb_serial_tty_driver, minor, dev); |
| 85 | dev_info(&port->serial->dev->dev, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | "%s converter now attached to ttyUSB%d\n", |
Greg Kroah-Hartman | 269bda1 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 87 | driver->description, minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | |
Bjørn Mork | d5fd650 | 2012-07-27 01:11:42 +0200 | [diff] [blame] | 89 | exit_with_autopm: |
| 90 | usb_autopm_put_interface(port->serial->interface); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | exit: |
| 92 | return retval; |
| 93 | } |
| 94 | |
Alan Cox | c97611c | 2008-07-22 11:09:48 +0100 | [diff] [blame] | 95 | static int usb_serial_device_remove(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | { |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 97 | struct usb_serial_driver *driver; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | struct usb_serial_port *port; |
| 99 | int retval = 0; |
| 100 | int minor; |
| 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 | |
Bjørn Mork | d5fd650 | 2012-07-27 01:11:42 +0200 | [diff] [blame] | 106 | /* make sure suspend/resume doesn't race against port_remove */ |
| 107 | usb_autopm_get_interface(port->serial->interface); |
| 108 | |
Roman Kagan | ce0d932 | 2007-08-24 20:22:52 +0400 | [diff] [blame] | 109 | device_remove_file(&port->dev, &dev_attr_port_number); |
| 110 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | driver = port->serial->type; |
Alan Stern | c706ebd | 2009-06-02 11:54:11 -0400 | [diff] [blame] | 112 | if (driver->port_remove) |
Alan Cox | c97611c | 2008-07-22 11:09:48 +0100 | [diff] [blame] | 113 | retval = driver->port_remove(port); |
Alan Stern | c706ebd | 2009-06-02 11:54:11 -0400 | [diff] [blame] | 114 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | minor = port->number; |
Alan Cox | c97611c | 2008-07-22 11:09:48 +0100 | [diff] [blame] | 116 | tty_unregister_device(usb_serial_tty_driver, minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | dev_info(dev, "%s converter now disconnected from ttyUSB%d\n", |
Greg Kroah-Hartman | 269bda1 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 118 | driver->description, minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | |
Bjørn Mork | d5fd650 | 2012-07-27 01:11:42 +0200 | [diff] [blame] | 120 | usb_autopm_put_interface(port->serial->interface); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | return retval; |
| 122 | } |
| 123 | |
Greg Kroah-Hartman | 93bacef | 2006-12-17 21:50:23 +0100 | [diff] [blame] | 124 | #ifdef CONFIG_HOTPLUG |
| 125 | static ssize_t store_new_id(struct device_driver *driver, |
| 126 | const char *buf, size_t count) |
| 127 | { |
| 128 | struct usb_serial_driver *usb_drv = to_usb_serial_driver(driver); |
| 129 | ssize_t retval = usb_store_new_id(&usb_drv->dynids, driver, buf, count); |
| 130 | |
| 131 | if (retval >= 0 && usb_drv->usb_driver != NULL) |
| 132 | retval = usb_store_new_id(&usb_drv->usb_driver->dynids, |
| 133 | &usb_drv->usb_driver->drvwrap.driver, |
| 134 | buf, count); |
| 135 | return retval; |
| 136 | } |
| 137 | |
Bjørn Mork | ef206f3 | 2012-05-13 12:35:00 +0200 | [diff] [blame] | 138 | static ssize_t show_dynids(struct device_driver *driver, char *buf) |
| 139 | { |
| 140 | struct usb_serial_driver *usb_drv = to_usb_serial_driver(driver); |
| 141 | |
| 142 | return usb_show_dynids(&usb_drv->dynids, buf); |
| 143 | } |
| 144 | |
Greg Kroah-Hartman | 93bacef | 2006-12-17 21:50:23 +0100 | [diff] [blame] | 145 | static struct driver_attribute drv_attrs[] = { |
Bjørn Mork | ef206f3 | 2012-05-13 12:35:00 +0200 | [diff] [blame] | 146 | __ATTR(new_id, S_IRUGO | S_IWUSR, show_dynids, store_new_id), |
Greg Kroah-Hartman | 93bacef | 2006-12-17 21:50:23 +0100 | [diff] [blame] | 147 | __ATTR_NULL, |
| 148 | }; |
| 149 | |
| 150 | static void free_dynids(struct usb_serial_driver *drv) |
| 151 | { |
| 152 | struct usb_dynid *dynid, *n; |
| 153 | |
| 154 | spin_lock(&drv->dynids.lock); |
| 155 | list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) { |
| 156 | list_del(&dynid->node); |
| 157 | kfree(dynid); |
| 158 | } |
| 159 | spin_unlock(&drv->dynids.lock); |
| 160 | } |
| 161 | |
| 162 | #else |
| 163 | static struct driver_attribute drv_attrs[] = { |
| 164 | __ATTR_NULL, |
| 165 | }; |
Satyam Sharma | 03fc370 | 2007-09-03 01:42:43 +0530 | [diff] [blame] | 166 | static inline void free_dynids(struct usb_serial_driver *drv) |
Greg Kroah-Hartman | 93bacef | 2006-12-17 21:50:23 +0100 | [diff] [blame] | 167 | { |
| 168 | } |
| 169 | #endif |
| 170 | |
Russell King | ac33bc3 | 2006-01-05 14:43:11 +0000 | [diff] [blame] | 171 | struct bus_type usb_serial_bus_type = { |
| 172 | .name = "usb-serial", |
| 173 | .match = usb_serial_device_match, |
| 174 | .probe = usb_serial_device_probe, |
| 175 | .remove = usb_serial_device_remove, |
Greg Kroah-Hartman | 93bacef | 2006-12-17 21:50:23 +0100 | [diff] [blame] | 176 | .drv_attrs = drv_attrs, |
Russell King | ac33bc3 | 2006-01-05 14:43:11 +0000 | [diff] [blame] | 177 | }; |
| 178 | |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 179 | int usb_serial_bus_register(struct usb_serial_driver *driver) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | { |
| 181 | int retval; |
| 182 | |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 183 | driver->driver.bus = &usb_serial_bus_type; |
Greg Kroah-Hartman | 93bacef | 2006-12-17 21:50:23 +0100 | [diff] [blame] | 184 | spin_lock_init(&driver->dynids.lock); |
| 185 | INIT_LIST_HEAD(&driver->dynids.list); |
| 186 | |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 187 | retval = driver_register(&driver->driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | |
| 189 | return retval; |
| 190 | } |
| 191 | |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 192 | void usb_serial_bus_deregister(struct usb_serial_driver *driver) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | { |
Greg Kroah-Hartman | 93bacef | 2006-12-17 21:50:23 +0100 | [diff] [blame] | 194 | free_dynids(driver); |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 195 | driver_unregister(&driver->driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | } |
| 197 | |