bus.c 2.9 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * USB Serial Converter Bus specific functions
 *
 * Copyright (C) 2002 Greg Kroah-Hartman (greg@kroah.com)
 *
 *	This program is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU General Public License version
 *	2 as published by the Free Software Foundation.
 */

#include <linux/config.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/tty.h>
#include <linux/module.h>
#include <linux/usb.h>
#include "usb-serial.h"

static int usb_serial_device_match (struct device *dev, struct device_driver *drv)
{
21
	struct usb_serial_driver *driver;
L
Linus Torvalds 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
	const struct usb_serial_port *port;

	/*
	 * drivers are already assigned to ports in serial_probe so it's
	 * a simple check here.
	 */
	port = to_usb_serial_port(dev);
	if (!port)
		return 0;

	driver = to_usb_serial_driver(drv);

	if (driver == port->serial->type)
		return 1;

	return 0;
}

struct bus_type usb_serial_bus_type = {
	.name =		"usb-serial",
	.match =	usb_serial_device_match,
};

static int usb_serial_device_probe (struct device *dev)
{
47
	struct usb_serial_driver *driver;
L
Linus Torvalds 已提交
48 49 50 51 52 53 54 55 56 57 58 59
	struct usb_serial_port *port;
	int retval = 0;
	int minor;

	port = to_usb_serial_port(dev);
	if (!port) {
		retval = -ENODEV;
		goto exit;
	}

	driver = port->serial->type;
	if (driver->port_probe) {
60
		if (!try_module_get(driver->driver.owner)) {
L
Linus Torvalds 已提交
61 62 63 64 65
			dev_err(dev, "module get failed, exiting\n");
			retval = -EIO;
			goto exit;
		}
		retval = driver->port_probe (port);
66
		module_put(driver->driver.owner);
L
Linus Torvalds 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
		if (retval)
			goto exit;
	}

	minor = port->number;
	tty_register_device (usb_serial_tty_driver, minor, dev);
	dev_info(&port->serial->dev->dev, 
		 "%s converter now attached to ttyUSB%d\n",
		 driver->name, minor);

exit:
	return retval;
}

static int usb_serial_device_remove (struct device *dev)
{
83
	struct usb_serial_driver *driver;
L
Linus Torvalds 已提交
84 85 86 87 88 89 90 91 92 93 94
	struct usb_serial_port *port;
	int retval = 0;
	int minor;

	port = to_usb_serial_port(dev);
	if (!port) {
		return -ENODEV;
	}

	driver = port->serial->type;
	if (driver->port_remove) {
95
		if (!try_module_get(driver->driver.owner)) {
L
Linus Torvalds 已提交
96 97 98 99 100
			dev_err(dev, "module get failed, exiting\n");
			retval = -EIO;
			goto exit;
		}
		retval = driver->port_remove (port);
101
		module_put(driver->driver.owner);
L
Linus Torvalds 已提交
102 103 104 105 106 107 108 109 110 111
	}
exit:
	minor = port->number;
	tty_unregister_device (usb_serial_tty_driver, minor);
	dev_info(dev, "%s converter now disconnected from ttyUSB%d\n",
		 driver->name, minor);

	return retval;
}

112
int usb_serial_bus_register(struct usb_serial_driver *driver)
L
Linus Torvalds 已提交
113 114 115
{
	int retval;

116 117
	if (driver->short_name)
		driver->driver.name = (char *)driver->short_name;
L
Linus Torvalds 已提交
118
	else
119 120 121 122
		driver->driver.name = (char *)driver->name;
	driver->driver.bus = &usb_serial_bus_type;
	driver->driver.probe = usb_serial_device_probe;
	driver->driver.remove = usb_serial_device_remove;
L
Linus Torvalds 已提交
123

124
	retval = driver_register(&driver->driver);
L
Linus Torvalds 已提交
125 126 127 128

	return retval;
}

129
void usb_serial_bus_deregister(struct usb_serial_driver *driver)
L
Linus Torvalds 已提交
130
{
131
	driver_unregister(&driver->driver);
L
Linus Torvalds 已提交
132 133
}