Matthew Garrett | da0af6e | 2012-05-11 16:08:27 +0800 | [diff] [blame] | 1 | /* |
| 2 | * USB-ACPI glue code |
| 3 | * |
| 4 | * Copyright 2012 Red Hat <mjg@redhat.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License as published by the Free |
| 8 | * Software Foundation, version 2. |
| 9 | * |
| 10 | */ |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/usb.h> |
| 13 | #include <linux/device.h> |
| 14 | #include <linux/errno.h> |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/acpi.h> |
| 17 | #include <linux/pci.h> |
Lan Tianyu | bafcaf6 | 2013-03-19 16:48:13 +0800 | [diff] [blame] | 18 | #include <linux/usb/hcd.h> |
Matthew Garrett | da0af6e | 2012-05-11 16:08:27 +0800 | [diff] [blame] | 19 | #include <acpi/acpi_bus.h> |
| 20 | |
| 21 | #include "usb.h" |
| 22 | |
Lan Tianyu | f7ac778 | 2012-09-05 13:44:36 +0800 | [diff] [blame] | 23 | /** |
| 24 | * usb_acpi_power_manageable - check whether usb port has |
| 25 | * acpi power resource. |
| 26 | * @hdev: USB device belonging to the usb hub |
| 27 | * @index: port index based zero |
| 28 | * |
| 29 | * Return true if the port has acpi power resource and false if no. |
| 30 | */ |
| 31 | bool usb_acpi_power_manageable(struct usb_device *hdev, int index) |
| 32 | { |
| 33 | acpi_handle port_handle; |
| 34 | int port1 = index + 1; |
| 35 | |
| 36 | port_handle = usb_get_hub_port_acpi_handle(hdev, |
| 37 | port1); |
| 38 | if (port_handle) |
| 39 | return acpi_bus_power_manageable(port_handle); |
| 40 | else |
| 41 | return false; |
| 42 | } |
| 43 | EXPORT_SYMBOL_GPL(usb_acpi_power_manageable); |
| 44 | |
| 45 | /** |
| 46 | * usb_acpi_set_power_state - control usb port's power via acpi power |
| 47 | * resource |
| 48 | * @hdev: USB device belonging to the usb hub |
| 49 | * @index: port index based zero |
| 50 | * @enable: power state expected to be set |
| 51 | * |
| 52 | * Notice to use usb_acpi_power_manageable() to check whether the usb port |
| 53 | * has acpi power resource before invoking this function. |
| 54 | * |
| 55 | * Returns 0 on success, else negative errno. |
| 56 | */ |
| 57 | int usb_acpi_set_power_state(struct usb_device *hdev, int index, bool enable) |
| 58 | { |
| 59 | acpi_handle port_handle; |
| 60 | unsigned char state; |
| 61 | int port1 = index + 1; |
| 62 | int error = -EINVAL; |
| 63 | |
| 64 | port_handle = (acpi_handle)usb_get_hub_port_acpi_handle(hdev, |
| 65 | port1); |
| 66 | if (!port_handle) |
| 67 | return error; |
| 68 | |
| 69 | if (enable) |
| 70 | state = ACPI_STATE_D0; |
| 71 | else |
| 72 | state = ACPI_STATE_D3_COLD; |
| 73 | |
| 74 | error = acpi_bus_set_power(port_handle, state); |
| 75 | if (!error) |
| 76 | dev_dbg(&hdev->dev, "The power of hub port %d was set to %d\n", |
| 77 | port1, enable); |
| 78 | else |
| 79 | dev_dbg(&hdev->dev, "The power of hub port failed to be set\n"); |
| 80 | |
| 81 | return error; |
| 82 | } |
| 83 | EXPORT_SYMBOL_GPL(usb_acpi_set_power_state); |
| 84 | |
Lan Tianyu | 05f9168 | 2012-09-05 13:44:34 +0800 | [diff] [blame] | 85 | static int usb_acpi_check_port_connect_type(struct usb_device *hdev, |
| 86 | acpi_handle handle, int port1) |
Matthew Garrett | 54d3f8c | 2012-05-11 16:08:28 +0800 | [diff] [blame] | 87 | { |
| 88 | acpi_status status; |
| 89 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; |
| 90 | union acpi_object *upc; |
Linus Torvalds | d8dc91b | 2012-10-08 07:14:06 +0900 | [diff] [blame] | 91 | struct acpi_pld_info *pld; |
Matthew Garrett | 54d3f8c | 2012-05-11 16:08:28 +0800 | [diff] [blame] | 92 | int ret = 0; |
| 93 | |
Lan Tianyu | 05f9168 | 2012-09-05 13:44:34 +0800 | [diff] [blame] | 94 | /* |
Rahul Bedarkar | 025d443 | 2014-01-04 11:24:41 +0530 | [diff] [blame^] | 95 | * According to ACPI Spec 9.13. PLD indicates whether usb port is |
Lan Tianyu | 05f9168 | 2012-09-05 13:44:34 +0800 | [diff] [blame] | 96 | * user visible and _UPC indicates whether it is connectable. If |
| 97 | * the port was visible and connectable, it could be freely connected |
| 98 | * and disconnected with USB devices. If no visible and connectable, |
| 99 | * a usb device is directly hard-wired to the port. If no visible and |
| 100 | * no connectable, the port would be not used. |
| 101 | */ |
| 102 | status = acpi_get_physical_device_location(handle, &pld); |
Matthew Garrett | 54d3f8c | 2012-05-11 16:08:28 +0800 | [diff] [blame] | 103 | if (ACPI_FAILURE(status)) |
| 104 | return -ENODEV; |
| 105 | |
Lan Tianyu | 05f9168 | 2012-09-05 13:44:34 +0800 | [diff] [blame] | 106 | status = acpi_evaluate_object(handle, "_UPC", NULL, &buffer); |
Matthew Garrett | 54d3f8c | 2012-05-11 16:08:28 +0800 | [diff] [blame] | 107 | upc = buffer.pointer; |
Matthew Garrett | 54d3f8c | 2012-05-11 16:08:28 +0800 | [diff] [blame] | 108 | if (!upc || (upc->type != ACPI_TYPE_PACKAGE) |
| 109 | || upc->package.count != 4) { |
| 110 | ret = -EINVAL; |
| 111 | goto out; |
| 112 | } |
| 113 | |
| 114 | if (upc->package.elements[0].integer.value) |
Linus Torvalds | d8dc91b | 2012-10-08 07:14:06 +0900 | [diff] [blame] | 115 | if (pld->user_visible) |
Lan Tianyu | 05f9168 | 2012-09-05 13:44:34 +0800 | [diff] [blame] | 116 | usb_set_hub_port_connect_type(hdev, port1, |
| 117 | USB_PORT_CONNECT_TYPE_HOT_PLUG); |
| 118 | else |
| 119 | usb_set_hub_port_connect_type(hdev, port1, |
| 120 | USB_PORT_CONNECT_TYPE_HARD_WIRED); |
Linus Torvalds | d8dc91b | 2012-10-08 07:14:06 +0900 | [diff] [blame] | 121 | else if (!pld->user_visible) |
Lan Tianyu | 05f9168 | 2012-09-05 13:44:34 +0800 | [diff] [blame] | 122 | usb_set_hub_port_connect_type(hdev, port1, USB_PORT_NOT_USED); |
Matthew Garrett | 54d3f8c | 2012-05-11 16:08:28 +0800 | [diff] [blame] | 123 | |
| 124 | out: |
Linus Torvalds | d8dc91b | 2012-10-08 07:14:06 +0900 | [diff] [blame] | 125 | ACPI_FREE(pld); |
Matthew Garrett | 54d3f8c | 2012-05-11 16:08:28 +0800 | [diff] [blame] | 126 | kfree(upc); |
| 127 | return ret; |
| 128 | } |
| 129 | |
Matthew Garrett | da0af6e | 2012-05-11 16:08:27 +0800 | [diff] [blame] | 130 | static int usb_acpi_find_device(struct device *dev, acpi_handle *handle) |
| 131 | { |
| 132 | struct usb_device *udev; |
Matthew Garrett | da0af6e | 2012-05-11 16:08:27 +0800 | [diff] [blame] | 133 | acpi_handle *parent_handle; |
Lan Tianyu | d557542 | 2012-09-05 13:44:33 +0800 | [diff] [blame] | 134 | int port_num; |
Matthew Garrett | da0af6e | 2012-05-11 16:08:27 +0800 | [diff] [blame] | 135 | |
Lan Tianyu | d557542 | 2012-09-05 13:44:33 +0800 | [diff] [blame] | 136 | /* |
| 137 | * In the ACPI DSDT table, only usb root hub and usb ports are |
| 138 | * acpi device nodes. The hierarchy like following. |
| 139 | * Device (EHC1) |
| 140 | * Device (HUBN) |
| 141 | * Device (PR01) |
| 142 | * Device (PR11) |
| 143 | * Device (PR12) |
| 144 | * Device (PR13) |
| 145 | * ... |
| 146 | * So all binding process is divided into two parts. binding |
| 147 | * root hub and usb ports. |
| 148 | */ |
| 149 | if (is_usb_device(dev)) { |
| 150 | udev = to_usb_device(dev); |
Lan Tianyu | 05f9168 | 2012-09-05 13:44:34 +0800 | [diff] [blame] | 151 | if (udev->parent) { |
| 152 | enum usb_port_connect_type type; |
| 153 | |
| 154 | /* |
| 155 | * According usb port's connect type to set usb device's |
| 156 | * removability. |
| 157 | */ |
| 158 | type = usb_get_hub_port_connect_type(udev->parent, |
| 159 | udev->portnum); |
| 160 | switch (type) { |
| 161 | case USB_PORT_CONNECT_TYPE_HOT_PLUG: |
| 162 | udev->removable = USB_DEVICE_REMOVABLE; |
| 163 | break; |
| 164 | case USB_PORT_CONNECT_TYPE_HARD_WIRED: |
| 165 | udev->removable = USB_DEVICE_FIXED; |
| 166 | break; |
| 167 | default: |
| 168 | udev->removable = USB_DEVICE_REMOVABLE_UNKNOWN; |
| 169 | break; |
| 170 | } |
| 171 | |
Lan Tianyu | d557542 | 2012-09-05 13:44:33 +0800 | [diff] [blame] | 172 | return -ENODEV; |
Lan Tianyu | 05f9168 | 2012-09-05 13:44:34 +0800 | [diff] [blame] | 173 | } |
| 174 | |
Lan Tianyu | d557542 | 2012-09-05 13:44:33 +0800 | [diff] [blame] | 175 | /* root hub's parent is the usb hcd. */ |
Rafael J. Wysocki | 3a83f99 | 2013-11-14 23:17:21 +0100 | [diff] [blame] | 176 | parent_handle = ACPI_HANDLE(dev->parent); |
Lan Tianyu | d557542 | 2012-09-05 13:44:33 +0800 | [diff] [blame] | 177 | *handle = acpi_get_child(parent_handle, udev->portnum); |
| 178 | if (!*handle) |
| 179 | return -ENODEV; |
| 180 | return 0; |
| 181 | } else if (is_usb_port(dev)) { |
| 182 | sscanf(dev_name(dev), "port%d", &port_num); |
| 183 | /* Get the struct usb_device point of port's hub */ |
| 184 | udev = to_usb_device(dev->parent->parent); |
Matthew Garrett | da0af6e | 2012-05-11 16:08:27 +0800 | [diff] [blame] | 185 | |
Lan Tianyu | d557542 | 2012-09-05 13:44:33 +0800 | [diff] [blame] | 186 | /* |
| 187 | * The root hub ports' parent is the root hub. The non-root-hub |
| 188 | * ports' parent is the parent hub port which the hub is |
| 189 | * connected to. |
| 190 | */ |
| 191 | if (!udev->parent) { |
Lan Tianyu | bafcaf6 | 2013-03-19 16:48:13 +0800 | [diff] [blame] | 192 | struct usb_hcd *hcd = bus_to_hcd(udev->bus); |
| 193 | int raw_port_num; |
| 194 | |
| 195 | raw_port_num = usb_hcd_find_raw_port_number(hcd, |
Lan Tianyu | d557542 | 2012-09-05 13:44:33 +0800 | [diff] [blame] | 196 | port_num); |
Rafael J. Wysocki | 3a83f99 | 2013-11-14 23:17:21 +0100 | [diff] [blame] | 197 | *handle = acpi_get_child(ACPI_HANDLE(&udev->dev), |
Lan Tianyu | bafcaf6 | 2013-03-19 16:48:13 +0800 | [diff] [blame] | 198 | raw_port_num); |
Lan Tianyu | d557542 | 2012-09-05 13:44:33 +0800 | [diff] [blame] | 199 | if (!*handle) |
| 200 | return -ENODEV; |
| 201 | } else { |
| 202 | parent_handle = |
| 203 | usb_get_hub_port_acpi_handle(udev->parent, |
| 204 | udev->portnum); |
| 205 | if (!parent_handle) |
| 206 | return -ENODEV; |
Matthew Garrett | da0af6e | 2012-05-11 16:08:27 +0800 | [diff] [blame] | 207 | |
Lan Tianyu | d557542 | 2012-09-05 13:44:33 +0800 | [diff] [blame] | 208 | *handle = acpi_get_child(parent_handle, port_num); |
| 209 | if (!*handle) |
| 210 | return -ENODEV; |
| 211 | } |
Lan Tianyu | 05f9168 | 2012-09-05 13:44:34 +0800 | [diff] [blame] | 212 | usb_acpi_check_port_connect_type(udev, *handle, port_num); |
Lan Tianyu | d557542 | 2012-09-05 13:44:33 +0800 | [diff] [blame] | 213 | } else |
Matthew Garrett | da0af6e | 2012-05-11 16:08:27 +0800 | [diff] [blame] | 214 | return -ENODEV; |
| 215 | |
Matthew Garrett | da0af6e | 2012-05-11 16:08:27 +0800 | [diff] [blame] | 216 | return 0; |
| 217 | } |
| 218 | |
Rafael J. Wysocki | 5354009 | 2013-03-03 22:35:20 +0100 | [diff] [blame] | 219 | static bool usb_acpi_bus_match(struct device *dev) |
| 220 | { |
| 221 | return is_usb_device(dev) || is_usb_port(dev); |
| 222 | } |
| 223 | |
Matthew Garrett | da0af6e | 2012-05-11 16:08:27 +0800 | [diff] [blame] | 224 | static struct acpi_bus_type usb_acpi_bus = { |
Rafael J. Wysocki | 5354009 | 2013-03-03 22:35:20 +0100 | [diff] [blame] | 225 | .name = "USB", |
| 226 | .match = usb_acpi_bus_match, |
Matthew Garrett | da0af6e | 2012-05-11 16:08:27 +0800 | [diff] [blame] | 227 | .find_device = usb_acpi_find_device, |
| 228 | }; |
| 229 | |
| 230 | int usb_acpi_register(void) |
| 231 | { |
| 232 | return register_acpi_bus_type(&usb_acpi_bus); |
| 233 | } |
| 234 | |
| 235 | void usb_acpi_unregister(void) |
| 236 | { |
| 237 | unregister_acpi_bus_type(&usb_acpi_bus); |
| 238 | } |