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 11 #include <linux/kernel.h> 12 #include <linux/errno.h> 13 #include <linux/tty.h> 14 #include <linux/slab.h> 15 #include <linux/module.h> 16 #include <linux/usb.h> 17 #include <linux/usb/serial.h> 18 19 static int usb_serial_device_match(struct device *dev, 20 struct device_driver *drv) 21 { 22 struct usb_serial_driver *driver; 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 41 static ssize_t port_number_show(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->port_number); 47 } 48 static DEVICE_ATTR_RO(port_number); 49 50 static int usb_serial_device_probe(struct device *dev) 51 { 52 struct usb_serial_driver *driver; 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 /* 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 68 driver = port->serial->type; 69 if (driver->port_probe) { 70 retval = driver->port_probe(port); 71 if (retval) 72 goto exit_with_autopm; 73 } 74 75 retval = device_create_file(dev, &dev_attr_port_number); 76 if (retval) { 77 if (driver->port_remove) 78 retval = driver->port_remove(port); 79 goto exit_with_autopm; 80 } 81 82 minor = port->minor; 83 tty_register_device(usb_serial_tty_driver, minor, dev); 84 dev_info(&port->serial->dev->dev, 85 "%s converter now attached to ttyUSB%d\n", 86 driver->description, minor); 87 88 exit_with_autopm: 89 usb_autopm_put_interface(port->serial->interface); 90 exit: 91 return retval; 92 } 93 94 static int usb_serial_device_remove(struct device *dev) 95 { 96 struct usb_serial_driver *driver; 97 struct usb_serial_port *port; 98 int retval = 0; 99 int minor; 100 int autopm_err; 101 102 port = to_usb_serial_port(dev); 103 if (!port) 104 return -ENODEV; 105 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); 113 114 minor = port->minor; 115 tty_unregister_device(usb_serial_tty_driver, minor); 116 117 device_remove_file(&port->dev, &dev_attr_port_number); 118 119 driver = port->serial->type; 120 if (driver->port_remove) 121 retval = driver->port_remove(port); 122 123 dev_info(dev, "%s converter now disconnected from ttyUSB%d\n", 124 driver->description, minor); 125 126 if (!autopm_err) 127 usb_autopm_put_interface(port->serial->interface); 128 129 return retval; 130 } 131 132 static ssize_t new_id_store(struct device_driver *driver, 133 const char *buf, size_t count) 134 { 135 struct usb_serial_driver *usb_drv = to_usb_serial_driver(driver); 136 ssize_t retval = usb_store_new_id(&usb_drv->dynids, usb_drv->id_table, 137 driver, buf, count); 138 139 if (retval >= 0 && usb_drv->usb_driver != NULL) 140 retval = usb_store_new_id(&usb_drv->usb_driver->dynids, 141 usb_drv->usb_driver->id_table, 142 &usb_drv->usb_driver->drvwrap.driver, 143 buf, count); 144 return retval; 145 } 146 147 static ssize_t new_id_show(struct device_driver *driver, char *buf) 148 { 149 struct usb_serial_driver *usb_drv = to_usb_serial_driver(driver); 150 151 return usb_show_dynids(&usb_drv->dynids, buf); 152 } 153 static DRIVER_ATTR_RW(new_id); 154 155 static struct attribute *usb_serial_drv_attrs[] = { 156 &driver_attr_new_id.attr, 157 NULL, 158 }; 159 ATTRIBUTE_GROUPS(usb_serial_drv); 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 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, 178 .drv_groups = usb_serial_drv_groups, 179 }; 180 181 int usb_serial_bus_register(struct usb_serial_driver *driver) 182 { 183 int retval; 184 185 driver->driver.bus = &usb_serial_bus_type; 186 spin_lock_init(&driver->dynids.lock); 187 INIT_LIST_HEAD(&driver->dynids.list); 188 189 retval = driver_register(&driver->driver); 190 191 return retval; 192 } 193 194 void usb_serial_bus_deregister(struct usb_serial_driver *driver) 195 { 196 free_dynids(driver); 197 driver_unregister(&driver->driver); 198 } 199 200