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> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/usb.h> |
Greg Kroah-Hartman | a969888 | 2006-07-11 21:22:58 -0700 | [diff] [blame] | 16 | #include <linux/usb/serial.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
Alan Cox | c97611c | 2008-07-22 11:09:48 +0100 | [diff] [blame] | 18 | static int usb_serial_device_match(struct device *dev, |
| 19 | struct device_driver *drv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | { |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 21 | struct usb_serial_driver *driver; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | const struct usb_serial_port *port; |
| 23 | |
| 24 | /* |
| 25 | * drivers are already assigned to ports in serial_probe so it's |
| 26 | * a simple check here. |
| 27 | */ |
| 28 | port = to_usb_serial_port(dev); |
| 29 | if (!port) |
| 30 | return 0; |
| 31 | |
| 32 | driver = to_usb_serial_driver(drv); |
| 33 | |
| 34 | if (driver == port->serial->type) |
| 35 | return 1; |
| 36 | |
| 37 | return 0; |
| 38 | } |
| 39 | |
Roman Kagan | ce0d932 | 2007-08-24 20:22:52 +0400 | [diff] [blame] | 40 | static ssize_t show_port_number(struct device *dev, |
| 41 | struct device_attribute *attr, char *buf) |
| 42 | { |
| 43 | struct usb_serial_port *port = to_usb_serial_port(dev); |
| 44 | |
| 45 | return sprintf(buf, "%d\n", port->number - port->serial->minor); |
| 46 | } |
| 47 | |
| 48 | static DEVICE_ATTR(port_number, S_IRUGO, show_port_number, NULL); |
| 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 | |
| 63 | driver = port->serial->type; |
| 64 | if (driver->port_probe) { |
Greg Kroah-Hartman | 18fcac3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 65 | if (!try_module_get(driver->driver.owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | dev_err(dev, "module get failed, exiting\n"); |
| 67 | retval = -EIO; |
| 68 | goto exit; |
| 69 | } |
Alan Cox | c97611c | 2008-07-22 11:09:48 +0100 | [diff] [blame] | 70 | retval = driver->port_probe(port); |
Greg Kroah-Hartman | 18fcac3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 71 | module_put(driver->driver.owner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | if (retval) |
| 73 | goto exit; |
| 74 | } |
| 75 | |
Roman Kagan | ce0d932 | 2007-08-24 20:22:52 +0400 | [diff] [blame] | 76 | retval = device_create_file(dev, &dev_attr_port_number); |
| 77 | if (retval) |
| 78 | goto exit; |
| 79 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | minor = port->number; |
Alan Cox | c97611c | 2008-07-22 11:09:48 +0100 | [diff] [blame] | 81 | tty_register_device(usb_serial_tty_driver, minor, dev); |
| 82 | dev_info(&port->serial->dev->dev, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | "%s converter now attached to ttyUSB%d\n", |
Greg Kroah-Hartman | 269bda1 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 84 | driver->description, minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
| 86 | exit: |
| 87 | return retval; |
| 88 | } |
| 89 | |
Alan Cox | c97611c | 2008-07-22 11:09:48 +0100 | [diff] [blame] | 90 | static int usb_serial_device_remove(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | { |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 92 | struct usb_serial_driver *driver; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | struct usb_serial_port *port; |
| 94 | int retval = 0; |
| 95 | int minor; |
| 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 | |
Roman Kagan | ce0d932 | 2007-08-24 20:22:52 +0400 | [diff] [blame] | 101 | device_remove_file(&port->dev, &dev_attr_port_number); |
| 102 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | driver = port->serial->type; |
| 104 | if (driver->port_remove) { |
Greg Kroah-Hartman | 18fcac3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 105 | if (!try_module_get(driver->driver.owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | dev_err(dev, "module get failed, exiting\n"); |
| 107 | retval = -EIO; |
| 108 | goto exit; |
| 109 | } |
Alan Cox | c97611c | 2008-07-22 11:09:48 +0100 | [diff] [blame] | 110 | retval = driver->port_remove(port); |
Greg Kroah-Hartman | 18fcac3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 111 | module_put(driver->driver.owner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | } |
| 113 | exit: |
| 114 | minor = port->number; |
Alan Cox | c97611c | 2008-07-22 11:09:48 +0100 | [diff] [blame] | 115 | tty_unregister_device(usb_serial_tty_driver, minor); |
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 | |
| 119 | return retval; |
| 120 | } |
| 121 | |
Greg Kroah-Hartman | 93bacef | 2006-12-17 21:50:23 +0100 | [diff] [blame] | 122 | #ifdef CONFIG_HOTPLUG |
| 123 | static ssize_t store_new_id(struct device_driver *driver, |
| 124 | const char *buf, size_t count) |
| 125 | { |
| 126 | struct usb_serial_driver *usb_drv = to_usb_serial_driver(driver); |
| 127 | ssize_t retval = usb_store_new_id(&usb_drv->dynids, driver, buf, count); |
| 128 | |
| 129 | if (retval >= 0 && usb_drv->usb_driver != NULL) |
| 130 | retval = usb_store_new_id(&usb_drv->usb_driver->dynids, |
| 131 | &usb_drv->usb_driver->drvwrap.driver, |
| 132 | buf, count); |
| 133 | return retval; |
| 134 | } |
| 135 | |
| 136 | static struct driver_attribute drv_attrs[] = { |
| 137 | __ATTR(new_id, S_IWUSR, NULL, store_new_id), |
| 138 | __ATTR_NULL, |
| 139 | }; |
| 140 | |
| 141 | static void free_dynids(struct usb_serial_driver *drv) |
| 142 | { |
| 143 | struct usb_dynid *dynid, *n; |
| 144 | |
| 145 | spin_lock(&drv->dynids.lock); |
| 146 | list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) { |
| 147 | list_del(&dynid->node); |
| 148 | kfree(dynid); |
| 149 | } |
| 150 | spin_unlock(&drv->dynids.lock); |
| 151 | } |
| 152 | |
| 153 | #else |
| 154 | static struct driver_attribute drv_attrs[] = { |
| 155 | __ATTR_NULL, |
| 156 | }; |
Satyam Sharma | 03fc370 | 2007-09-03 01:42:43 +0530 | [diff] [blame] | 157 | static inline void free_dynids(struct usb_serial_driver *drv) |
Greg Kroah-Hartman | 93bacef | 2006-12-17 21:50:23 +0100 | [diff] [blame] | 158 | { |
| 159 | } |
| 160 | #endif |
| 161 | |
Russell King | ac33bc3 | 2006-01-05 14:43:11 +0000 | [diff] [blame] | 162 | struct bus_type usb_serial_bus_type = { |
| 163 | .name = "usb-serial", |
| 164 | .match = usb_serial_device_match, |
| 165 | .probe = usb_serial_device_probe, |
| 166 | .remove = usb_serial_device_remove, |
Greg Kroah-Hartman | 93bacef | 2006-12-17 21:50:23 +0100 | [diff] [blame] | 167 | .drv_attrs = drv_attrs, |
Russell King | ac33bc3 | 2006-01-05 14:43:11 +0000 | [diff] [blame] | 168 | }; |
| 169 | |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 170 | int usb_serial_bus_register(struct usb_serial_driver *driver) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | { |
| 172 | int retval; |
| 173 | |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 174 | driver->driver.bus = &usb_serial_bus_type; |
Greg Kroah-Hartman | 93bacef | 2006-12-17 21:50:23 +0100 | [diff] [blame] | 175 | spin_lock_init(&driver->dynids.lock); |
| 176 | INIT_LIST_HEAD(&driver->dynids.list); |
| 177 | |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 178 | retval = driver_register(&driver->driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | |
| 180 | return retval; |
| 181 | } |
| 182 | |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 183 | void usb_serial_bus_deregister(struct usb_serial_driver *driver) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | { |
Greg Kroah-Hartman | 93bacef | 2006-12-17 21:50:23 +0100 | [diff] [blame] | 185 | free_dynids(driver); |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 186 | driver_unregister(&driver->driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | } |
| 188 | |