18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * USB Serial Converter Bus specific functions
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2002 Greg Kroah-Hartman (greg@kroah.com)
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/kernel.h>
98c2ecf20Sopenharmony_ci#include <linux/errno.h>
108c2ecf20Sopenharmony_ci#include <linux/tty.h>
118c2ecf20Sopenharmony_ci#include <linux/slab.h>
128c2ecf20Sopenharmony_ci#include <linux/module.h>
138c2ecf20Sopenharmony_ci#include <linux/usb.h>
148c2ecf20Sopenharmony_ci#include <linux/usb/serial.h>
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_cistatic int usb_serial_device_match(struct device *dev,
178c2ecf20Sopenharmony_ci						struct device_driver *drv)
188c2ecf20Sopenharmony_ci{
198c2ecf20Sopenharmony_ci	struct usb_serial_driver *driver;
208c2ecf20Sopenharmony_ci	const struct usb_serial_port *port;
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci	/*
238c2ecf20Sopenharmony_ci	 * drivers are already assigned to ports in serial_probe so it's
248c2ecf20Sopenharmony_ci	 * a simple check here.
258c2ecf20Sopenharmony_ci	 */
268c2ecf20Sopenharmony_ci	port = to_usb_serial_port(dev);
278c2ecf20Sopenharmony_ci	if (!port)
288c2ecf20Sopenharmony_ci		return 0;
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci	driver = to_usb_serial_driver(drv);
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci	if (driver == port->serial->type)
338c2ecf20Sopenharmony_ci		return 1;
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci	return 0;
368c2ecf20Sopenharmony_ci}
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_cistatic int usb_serial_device_probe(struct device *dev)
398c2ecf20Sopenharmony_ci{
408c2ecf20Sopenharmony_ci	struct usb_serial_driver *driver;
418c2ecf20Sopenharmony_ci	struct usb_serial_port *port;
428c2ecf20Sopenharmony_ci	struct device *tty_dev;
438c2ecf20Sopenharmony_ci	int retval = 0;
448c2ecf20Sopenharmony_ci	int minor;
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci	port = to_usb_serial_port(dev);
478c2ecf20Sopenharmony_ci	if (!port)
488c2ecf20Sopenharmony_ci		return -ENODEV;
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci	/* make sure suspend/resume doesn't race against port_probe */
518c2ecf20Sopenharmony_ci	retval = usb_autopm_get_interface(port->serial->interface);
528c2ecf20Sopenharmony_ci	if (retval)
538c2ecf20Sopenharmony_ci		return retval;
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	driver = port->serial->type;
568c2ecf20Sopenharmony_ci	if (driver->port_probe) {
578c2ecf20Sopenharmony_ci		retval = driver->port_probe(port);
588c2ecf20Sopenharmony_ci		if (retval)
598c2ecf20Sopenharmony_ci			goto err_autopm_put;
608c2ecf20Sopenharmony_ci	}
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci	minor = port->minor;
638c2ecf20Sopenharmony_ci	tty_dev = tty_port_register_device(&port->port, usb_serial_tty_driver,
648c2ecf20Sopenharmony_ci					   minor, dev);
658c2ecf20Sopenharmony_ci	if (IS_ERR(tty_dev)) {
668c2ecf20Sopenharmony_ci		retval = PTR_ERR(tty_dev);
678c2ecf20Sopenharmony_ci		goto err_port_remove;
688c2ecf20Sopenharmony_ci	}
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci	usb_autopm_put_interface(port->serial->interface);
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	dev_info(&port->serial->dev->dev,
738c2ecf20Sopenharmony_ci		 "%s converter now attached to ttyUSB%d\n",
748c2ecf20Sopenharmony_ci		 driver->description, minor);
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	return 0;
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_cierr_port_remove:
798c2ecf20Sopenharmony_ci	if (driver->port_remove)
808c2ecf20Sopenharmony_ci		driver->port_remove(port);
818c2ecf20Sopenharmony_cierr_autopm_put:
828c2ecf20Sopenharmony_ci	usb_autopm_put_interface(port->serial->interface);
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	return retval;
858c2ecf20Sopenharmony_ci}
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_cistatic int usb_serial_device_remove(struct device *dev)
888c2ecf20Sopenharmony_ci{
898c2ecf20Sopenharmony_ci	struct usb_serial_driver *driver;
908c2ecf20Sopenharmony_ci	struct usb_serial_port *port;
918c2ecf20Sopenharmony_ci	int retval = 0;
928c2ecf20Sopenharmony_ci	int minor;
938c2ecf20Sopenharmony_ci	int autopm_err;
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	port = to_usb_serial_port(dev);
968c2ecf20Sopenharmony_ci	if (!port)
978c2ecf20Sopenharmony_ci		return -ENODEV;
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci	/*
1008c2ecf20Sopenharmony_ci	 * Make sure suspend/resume doesn't race against port_remove.
1018c2ecf20Sopenharmony_ci	 *
1028c2ecf20Sopenharmony_ci	 * Note that no further runtime PM callbacks will be made if
1038c2ecf20Sopenharmony_ci	 * autopm_get fails.
1048c2ecf20Sopenharmony_ci	 */
1058c2ecf20Sopenharmony_ci	autopm_err = usb_autopm_get_interface(port->serial->interface);
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	minor = port->minor;
1088c2ecf20Sopenharmony_ci	tty_unregister_device(usb_serial_tty_driver, minor);
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	driver = port->serial->type;
1118c2ecf20Sopenharmony_ci	if (driver->port_remove)
1128c2ecf20Sopenharmony_ci		retval = driver->port_remove(port);
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	dev_info(dev, "%s converter now disconnected from ttyUSB%d\n",
1158c2ecf20Sopenharmony_ci		 driver->description, minor);
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	if (!autopm_err)
1188c2ecf20Sopenharmony_ci		usb_autopm_put_interface(port->serial->interface);
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	return retval;
1218c2ecf20Sopenharmony_ci}
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_cistatic ssize_t new_id_store(struct device_driver *driver,
1248c2ecf20Sopenharmony_ci			    const char *buf, size_t count)
1258c2ecf20Sopenharmony_ci{
1268c2ecf20Sopenharmony_ci	struct usb_serial_driver *usb_drv = to_usb_serial_driver(driver);
1278c2ecf20Sopenharmony_ci	ssize_t retval = usb_store_new_id(&usb_drv->dynids, usb_drv->id_table,
1288c2ecf20Sopenharmony_ci					 driver, buf, count);
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	if (retval >= 0 && usb_drv->usb_driver != NULL)
1318c2ecf20Sopenharmony_ci		retval = usb_store_new_id(&usb_drv->usb_driver->dynids,
1328c2ecf20Sopenharmony_ci					  usb_drv->usb_driver->id_table,
1338c2ecf20Sopenharmony_ci					  &usb_drv->usb_driver->drvwrap.driver,
1348c2ecf20Sopenharmony_ci					  buf, count);
1358c2ecf20Sopenharmony_ci	return retval;
1368c2ecf20Sopenharmony_ci}
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_cistatic ssize_t new_id_show(struct device_driver *driver, char *buf)
1398c2ecf20Sopenharmony_ci{
1408c2ecf20Sopenharmony_ci	struct usb_serial_driver *usb_drv = to_usb_serial_driver(driver);
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	return usb_show_dynids(&usb_drv->dynids, buf);
1438c2ecf20Sopenharmony_ci}
1448c2ecf20Sopenharmony_cistatic DRIVER_ATTR_RW(new_id);
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_cistatic struct attribute *usb_serial_drv_attrs[] = {
1478c2ecf20Sopenharmony_ci	&driver_attr_new_id.attr,
1488c2ecf20Sopenharmony_ci	NULL,
1498c2ecf20Sopenharmony_ci};
1508c2ecf20Sopenharmony_ciATTRIBUTE_GROUPS(usb_serial_drv);
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_cistatic void free_dynids(struct usb_serial_driver *drv)
1538c2ecf20Sopenharmony_ci{
1548c2ecf20Sopenharmony_ci	struct usb_dynid *dynid, *n;
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	spin_lock(&drv->dynids.lock);
1578c2ecf20Sopenharmony_ci	list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
1588c2ecf20Sopenharmony_ci		list_del(&dynid->node);
1598c2ecf20Sopenharmony_ci		kfree(dynid);
1608c2ecf20Sopenharmony_ci	}
1618c2ecf20Sopenharmony_ci	spin_unlock(&drv->dynids.lock);
1628c2ecf20Sopenharmony_ci}
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_cistruct bus_type usb_serial_bus_type = {
1658c2ecf20Sopenharmony_ci	.name =		"usb-serial",
1668c2ecf20Sopenharmony_ci	.match =	usb_serial_device_match,
1678c2ecf20Sopenharmony_ci	.probe =	usb_serial_device_probe,
1688c2ecf20Sopenharmony_ci	.remove =	usb_serial_device_remove,
1698c2ecf20Sopenharmony_ci	.drv_groups = 	usb_serial_drv_groups,
1708c2ecf20Sopenharmony_ci};
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ciint usb_serial_bus_register(struct usb_serial_driver *driver)
1738c2ecf20Sopenharmony_ci{
1748c2ecf20Sopenharmony_ci	int retval;
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	driver->driver.bus = &usb_serial_bus_type;
1778c2ecf20Sopenharmony_ci	spin_lock_init(&driver->dynids.lock);
1788c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&driver->dynids.list);
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci	retval = driver_register(&driver->driver);
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	return retval;
1838c2ecf20Sopenharmony_ci}
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_civoid usb_serial_bus_deregister(struct usb_serial_driver *driver)
1868c2ecf20Sopenharmony_ci{
1878c2ecf20Sopenharmony_ci	free_dynids(driver);
1888c2ecf20Sopenharmony_ci	driver_unregister(&driver->driver);
1898c2ecf20Sopenharmony_ci}
1908c2ecf20Sopenharmony_ci
191