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 | |
| 18 | static int usb_serial_device_match (struct device *dev, struct device_driver *drv) |
| 19 | { |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 20 | struct usb_serial_driver *driver; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | const struct usb_serial_port *port; |
| 22 | |
| 23 | /* |
| 24 | * drivers are already assigned to ports in serial_probe so it's |
| 25 | * a simple check here. |
| 26 | */ |
| 27 | port = to_usb_serial_port(dev); |
| 28 | if (!port) |
| 29 | return 0; |
| 30 | |
| 31 | driver = to_usb_serial_driver(drv); |
| 32 | |
| 33 | if (driver == port->serial->type) |
| 34 | return 1; |
| 35 | |
| 36 | return 0; |
| 37 | } |
| 38 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | static int usb_serial_device_probe (struct device *dev) |
| 40 | { |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 41 | struct usb_serial_driver *driver; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | struct usb_serial_port *port; |
| 43 | int retval = 0; |
| 44 | int minor; |
| 45 | |
| 46 | port = to_usb_serial_port(dev); |
| 47 | if (!port) { |
| 48 | retval = -ENODEV; |
| 49 | goto exit; |
| 50 | } |
| 51 | |
| 52 | driver = port->serial->type; |
| 53 | if (driver->port_probe) { |
Greg Kroah-Hartman | 18fcac3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 54 | if (!try_module_get(driver->driver.owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | dev_err(dev, "module get failed, exiting\n"); |
| 56 | retval = -EIO; |
| 57 | goto exit; |
| 58 | } |
| 59 | retval = driver->port_probe (port); |
Greg Kroah-Hartman | 18fcac3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 60 | module_put(driver->driver.owner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | if (retval) |
| 62 | goto exit; |
| 63 | } |
| 64 | |
| 65 | minor = port->number; |
| 66 | tty_register_device (usb_serial_tty_driver, minor, dev); |
| 67 | dev_info(&port->serial->dev->dev, |
| 68 | "%s converter now attached to ttyUSB%d\n", |
Greg Kroah-Hartman | 269bda1 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 69 | driver->description, minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
| 71 | exit: |
| 72 | return retval; |
| 73 | } |
| 74 | |
| 75 | static int usb_serial_device_remove (struct device *dev) |
| 76 | { |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 77 | struct usb_serial_driver *driver; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | struct usb_serial_port *port; |
| 79 | int retval = 0; |
| 80 | int minor; |
| 81 | |
| 82 | port = to_usb_serial_port(dev); |
| 83 | if (!port) { |
| 84 | return -ENODEV; |
| 85 | } |
| 86 | |
| 87 | driver = port->serial->type; |
| 88 | if (driver->port_remove) { |
Greg Kroah-Hartman | 18fcac3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 89 | if (!try_module_get(driver->driver.owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | dev_err(dev, "module get failed, exiting\n"); |
| 91 | retval = -EIO; |
| 92 | goto exit; |
| 93 | } |
| 94 | retval = driver->port_remove (port); |
Greg Kroah-Hartman | 18fcac3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 95 | module_put(driver->driver.owner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | } |
| 97 | exit: |
| 98 | minor = port->number; |
| 99 | tty_unregister_device (usb_serial_tty_driver, minor); |
| 100 | dev_info(dev, "%s converter now disconnected from ttyUSB%d\n", |
Greg Kroah-Hartman | 269bda1 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 101 | driver->description, minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | |
| 103 | return retval; |
| 104 | } |
| 105 | |
Russell King | ac33bc3 | 2006-01-05 14:43:11 +0000 | [diff] [blame] | 106 | struct bus_type usb_serial_bus_type = { |
| 107 | .name = "usb-serial", |
| 108 | .match = usb_serial_device_match, |
| 109 | .probe = usb_serial_device_probe, |
| 110 | .remove = usb_serial_device_remove, |
| 111 | }; |
| 112 | |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 113 | int usb_serial_bus_register(struct usb_serial_driver *driver) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | { |
| 115 | int retval; |
| 116 | |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 117 | driver->driver.bus = &usb_serial_bus_type; |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 118 | retval = driver_register(&driver->driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | |
| 120 | return retval; |
| 121 | } |
| 122 | |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 123 | void usb_serial_bus_deregister(struct usb_serial_driver *driver) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | { |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 125 | driver_unregister(&driver->driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | } |
| 127 | |