^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * USB Serial Converter Bus specific functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2002 Greg Kroah-Hartman (greg@kroah.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/tty.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/usb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/usb/serial.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) static int usb_serial_device_match(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) struct device_driver *drv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) struct usb_serial_driver *driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) const struct usb_serial_port *port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * drivers are already assigned to ports in serial_probe so it's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * a simple check here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) port = to_usb_serial_port(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) if (!port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) driver = to_usb_serial_driver(drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) if (driver == port->serial->type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) static int usb_serial_device_probe(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) struct usb_serial_driver *driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) struct usb_serial_port *port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) struct device *tty_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) int retval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) int minor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) port = to_usb_serial_port(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) if (!port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) /* make sure suspend/resume doesn't race against port_probe */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) retval = usb_autopm_get_interface(port->serial->interface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) driver = port->serial->type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) if (driver->port_probe) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) retval = driver->port_probe(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) goto err_autopm_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) minor = port->minor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) tty_dev = tty_port_register_device(&port->port, usb_serial_tty_driver,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) minor, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) if (IS_ERR(tty_dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) retval = PTR_ERR(tty_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) goto err_port_remove;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) usb_autopm_put_interface(port->serial->interface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) dev_info(&port->serial->dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) "%s converter now attached to ttyUSB%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) driver->description, minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) err_port_remove:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) if (driver->port_remove)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) driver->port_remove(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) err_autopm_put:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) usb_autopm_put_interface(port->serial->interface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) static int usb_serial_device_remove(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) struct usb_serial_driver *driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) struct usb_serial_port *port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) int retval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) int minor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) int autopm_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) port = to_usb_serial_port(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) if (!port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) * Make sure suspend/resume doesn't race against port_remove.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) * Note that no further runtime PM callbacks will be made if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) * autopm_get fails.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) autopm_err = usb_autopm_get_interface(port->serial->interface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) minor = port->minor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) tty_unregister_device(usb_serial_tty_driver, minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) driver = port->serial->type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) if (driver->port_remove)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) retval = driver->port_remove(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) dev_info(dev, "%s converter now disconnected from ttyUSB%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) driver->description, minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) if (!autopm_err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) usb_autopm_put_interface(port->serial->interface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) static ssize_t new_id_store(struct device_driver *driver,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) struct usb_serial_driver *usb_drv = to_usb_serial_driver(driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) ssize_t retval = usb_store_new_id(&usb_drv->dynids, usb_drv->id_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) driver, buf, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) if (retval >= 0 && usb_drv->usb_driver != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) retval = usb_store_new_id(&usb_drv->usb_driver->dynids,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) usb_drv->usb_driver->id_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) &usb_drv->usb_driver->drvwrap.driver,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) buf, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) static ssize_t new_id_show(struct device_driver *driver, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) struct usb_serial_driver *usb_drv = to_usb_serial_driver(driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) return usb_show_dynids(&usb_drv->dynids, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) static DRIVER_ATTR_RW(new_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) static struct attribute *usb_serial_drv_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) &driver_attr_new_id.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) ATTRIBUTE_GROUPS(usb_serial_drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) static void free_dynids(struct usb_serial_driver *drv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) struct usb_dynid *dynid, *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) spin_lock(&drv->dynids.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) list_del(&dynid->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) kfree(dynid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) spin_unlock(&drv->dynids.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) struct bus_type usb_serial_bus_type = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) .name = "usb-serial",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) .match = usb_serial_device_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) .probe = usb_serial_device_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) .remove = usb_serial_device_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) .drv_groups = usb_serial_drv_groups,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) int usb_serial_bus_register(struct usb_serial_driver *driver)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) driver->driver.bus = &usb_serial_bus_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) spin_lock_init(&driver->dynids.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) INIT_LIST_HEAD(&driver->dynids.list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) retval = driver_register(&driver->driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) void usb_serial_bus_deregister(struct usb_serial_driver *driver)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) free_dynids(driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) driver_unregister(&driver->driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)