Merge branch 'for-3.11' of git://linux-nfs.org/~bfields/linux
[linux-2.6.git] / drivers / usb / core / usb-acpi.c
blob255c14464bf2ea7a30dffab6fa1770c7b030c0aa
1 /*
2 * USB-ACPI glue code
4 * Copyright 2012 Red Hat <mjg@redhat.com>
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.
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>
18 #include <linux/usb/hcd.h>
19 #include <acpi/acpi_bus.h>
21 #include "usb.h"
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
29 * Return true if the port has acpi power resource and false if no.
31 bool usb_acpi_power_manageable(struct usb_device *hdev, int index)
33 acpi_handle port_handle;
34 int port1 = index + 1;
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;
43 EXPORT_SYMBOL_GPL(usb_acpi_power_manageable);
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
52 * Notice to use usb_acpi_power_manageable() to check whether the usb port
53 * has acpi power resource before invoking this function.
55 * Returns 0 on success, else negative errno.
57 int usb_acpi_set_power_state(struct usb_device *hdev, int index, bool enable)
59 acpi_handle port_handle;
60 unsigned char state;
61 int port1 = index + 1;
62 int error = -EINVAL;
64 port_handle = (acpi_handle)usb_get_hub_port_acpi_handle(hdev,
65 port1);
66 if (!port_handle)
67 return error;
69 if (enable)
70 state = ACPI_STATE_D0;
71 else
72 state = ACPI_STATE_D3_COLD;
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");
81 return error;
83 EXPORT_SYMBOL_GPL(usb_acpi_set_power_state);
85 static int usb_acpi_check_port_connect_type(struct usb_device *hdev,
86 acpi_handle handle, int port1)
88 acpi_status status;
89 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
90 union acpi_object *upc;
91 struct acpi_pld_info *pld;
92 int ret = 0;
95 * Accoding to ACPI Spec 9.13. PLD indicates whether usb port is
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.
102 status = acpi_get_physical_device_location(handle, &pld);
103 if (ACPI_FAILURE(status))
104 return -ENODEV;
106 status = acpi_evaluate_object(handle, "_UPC", NULL, &buffer);
107 upc = buffer.pointer;
108 if (!upc || (upc->type != ACPI_TYPE_PACKAGE)
109 || upc->package.count != 4) {
110 ret = -EINVAL;
111 goto out;
114 if (upc->package.elements[0].integer.value)
115 if (pld->user_visible)
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);
121 else if (!pld->user_visible)
122 usb_set_hub_port_connect_type(hdev, port1, USB_PORT_NOT_USED);
124 out:
125 ACPI_FREE(pld);
126 kfree(upc);
127 return ret;
130 static int usb_acpi_find_device(struct device *dev, acpi_handle *handle)
132 struct usb_device *udev;
133 acpi_handle *parent_handle;
134 int port_num;
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.
149 if (is_usb_device(dev)) {
150 udev = to_usb_device(dev);
151 if (udev->parent) {
152 enum usb_port_connect_type type;
155 * According usb port's connect type to set usb device's
156 * removability.
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;
172 return -ENODEV;
175 /* root hub's parent is the usb hcd. */
176 parent_handle = DEVICE_ACPI_HANDLE(dev->parent);
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);
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.
191 if (!udev->parent) {
192 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
193 int raw_port_num;
195 raw_port_num = usb_hcd_find_raw_port_number(hcd,
196 port_num);
197 *handle = acpi_get_child(DEVICE_ACPI_HANDLE(&udev->dev),
198 raw_port_num);
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;
208 *handle = acpi_get_child(parent_handle, port_num);
209 if (!*handle)
210 return -ENODEV;
212 usb_acpi_check_port_connect_type(udev, *handle, port_num);
213 } else
214 return -ENODEV;
216 return 0;
219 static bool usb_acpi_bus_match(struct device *dev)
221 return is_usb_device(dev) || is_usb_port(dev);
224 static struct acpi_bus_type usb_acpi_bus = {
225 .name = "USB",
226 .match = usb_acpi_bus_match,
227 .find_device = usb_acpi_find_device,
230 int usb_acpi_register(void)
232 return register_acpi_bus_type(&usb_acpi_bus);
235 void usb_acpi_unregister(void)
237 unregister_acpi_bus_type(&usb_acpi_bus);