bus.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. #include <linux/kernel.h>
  11. #include <linux/errno.h>
  12. #include <linux/tty.h>
  13. #include <linux/slab.h>
  14. #include <linux/module.h>
  15. #include <linux/usb.h>
  16. #include <linux/usb/serial.h>
  17. static int usb_serial_device_match(struct device *dev,
  18. struct device_driver *drv)
  19. {
  20. struct usb_serial_driver *driver;
  21. const struct usb_serial_port *port;
  22. /*
  23. * drivers are already assigned to ports in serial_probe so it's
  24. * a simple check here.
  25. */
  26. port = to_usb_serial_port(dev);
  27. if (!port)
  28. return 0;
  29. driver = to_usb_serial_driver(drv);
  30. if (driver == port->serial->type)
  31. return 1;
  32. return 0;
  33. }
  34. static int usb_serial_device_probe(struct device *dev)
  35. {
  36. struct usb_serial_driver *driver;
  37. struct usb_serial_port *port;
  38. struct device *tty_dev;
  39. int retval = 0;
  40. int minor;
  41. port = to_usb_serial_port(dev);
  42. if (!port)
  43. return -ENODEV;
  44. /* make sure suspend/resume doesn't race against port_probe */
  45. retval = usb_autopm_get_interface(port->serial->interface);
  46. if (retval)
  47. return retval;
  48. driver = port->serial->type;
  49. if (driver->port_probe) {
  50. retval = driver->port_probe(port);
  51. if (retval)
  52. goto err_autopm_put;
  53. }
  54. minor = port->minor;
  55. tty_dev = tty_register_device(usb_serial_tty_driver, minor, dev);
  56. if (IS_ERR(tty_dev)) {
  57. retval = PTR_ERR(tty_dev);
  58. goto err_port_remove;
  59. }
  60. usb_autopm_put_interface(port->serial->interface);
  61. dev_info(&port->serial->dev->dev,
  62. "%s converter now attached to ttyUSB%d\n",
  63. driver->description, minor);
  64. return 0;
  65. err_port_remove:
  66. if (driver->port_remove)
  67. driver->port_remove(port);
  68. err_autopm_put:
  69. usb_autopm_put_interface(port->serial->interface);
  70. return retval;
  71. }
  72. static int usb_serial_device_remove(struct device *dev)
  73. {
  74. struct usb_serial_driver *driver;
  75. struct usb_serial_port *port;
  76. int retval = 0;
  77. int minor;
  78. int autopm_err;
  79. port = to_usb_serial_port(dev);
  80. if (!port)
  81. return -ENODEV;
  82. /*
  83. * Make sure suspend/resume doesn't race against port_remove.
  84. *
  85. * Note that no further runtime PM callbacks will be made if
  86. * autopm_get fails.
  87. */
  88. autopm_err = usb_autopm_get_interface(port->serial->interface);
  89. minor = port->minor;
  90. tty_unregister_device(usb_serial_tty_driver, minor);
  91. driver = port->serial->type;
  92. if (driver->port_remove)
  93. retval = driver->port_remove(port);
  94. dev_info(dev, "%s converter now disconnected from ttyUSB%d\n",
  95. driver->description, minor);
  96. if (!autopm_err)
  97. usb_autopm_put_interface(port->serial->interface);
  98. return retval;
  99. }
  100. static ssize_t new_id_store(struct device_driver *driver,
  101. const char *buf, size_t count)
  102. {
  103. struct usb_serial_driver *usb_drv = to_usb_serial_driver(driver);
  104. ssize_t retval = usb_store_new_id(&usb_drv->dynids, usb_drv->id_table,
  105. driver, buf, count);
  106. if (retval >= 0 && usb_drv->usb_driver != NULL)
  107. retval = usb_store_new_id(&usb_drv->usb_driver->dynids,
  108. usb_drv->usb_driver->id_table,
  109. &usb_drv->usb_driver->drvwrap.driver,
  110. buf, count);
  111. return retval;
  112. }
  113. static ssize_t new_id_show(struct device_driver *driver, char *buf)
  114. {
  115. struct usb_serial_driver *usb_drv = to_usb_serial_driver(driver);
  116. return usb_show_dynids(&usb_drv->dynids, buf);
  117. }
  118. static DRIVER_ATTR_RW(new_id);
  119. static struct attribute *usb_serial_drv_attrs[] = {
  120. &driver_attr_new_id.attr,
  121. NULL,
  122. };
  123. ATTRIBUTE_GROUPS(usb_serial_drv);
  124. static void free_dynids(struct usb_serial_driver *drv)
  125. {
  126. struct usb_dynid *dynid, *n;
  127. spin_lock(&drv->dynids.lock);
  128. list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
  129. list_del(&dynid->node);
  130. kfree(dynid);
  131. }
  132. spin_unlock(&drv->dynids.lock);
  133. }
  134. struct bus_type usb_serial_bus_type = {
  135. .name = "usb-serial",
  136. .match = usb_serial_device_match,
  137. .probe = usb_serial_device_probe,
  138. .remove = usb_serial_device_remove,
  139. .drv_groups = usb_serial_drv_groups,
  140. };
  141. int usb_serial_bus_register(struct usb_serial_driver *driver)
  142. {
  143. int retval;
  144. driver->driver.bus = &usb_serial_bus_type;
  145. spin_lock_init(&driver->dynids.lock);
  146. INIT_LIST_HEAD(&driver->dynids.list);
  147. retval = driver_register(&driver->driver);
  148. return retval;
  149. }
  150. void usb_serial_bus_deregister(struct usb_serial_driver *driver)
  151. {
  152. free_dynids(driver);
  153. driver_unregister(&driver->driver);
  154. }