blob: d7cb822d6eab5fd7a04b1557bb754b6c084a6037 [file] [log] [blame]
Matthew Garrettda0af6e2012-05-11 16:08:27 +08001/*
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 Tianyubafcaf62013-03-19 16:48:13 +080018#include <linux/usb/hcd.h>
Matthew Garrettda0af6e2012-05-11 16:08:27 +080019#include <acpi/acpi_bus.h>
20
21#include "usb.h"
22
Lan Tianyuf7ac7782012-09-05 13:44:36 +080023/**
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 */
31bool 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}
43EXPORT_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 */
57int 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}
83EXPORT_SYMBOL_GPL(usb_acpi_set_power_state);
84
Lan Tianyu05f91682012-09-05 13:44:34 +080085static int usb_acpi_check_port_connect_type(struct usb_device *hdev,
86 acpi_handle handle, int port1)
Matthew Garrett54d3f8c2012-05-11 16:08:28 +080087{
88 acpi_status status;
89 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
90 union acpi_object *upc;
Linus Torvaldsd8dc91b2012-10-08 07:14:06 +090091 struct acpi_pld_info *pld;
Matthew Garrett54d3f8c2012-05-11 16:08:28 +080092 int ret = 0;
93
Lan Tianyu05f91682012-09-05 13:44:34 +080094 /*
Rahul Bedarkar025d4432014-01-04 11:24:41 +053095 * According to ACPI Spec 9.13. PLD indicates whether usb port is
Lan Tianyu05f91682012-09-05 13:44:34 +080096 * 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 Garrett54d3f8c2012-05-11 16:08:28 +0800103 if (ACPI_FAILURE(status))
104 return -ENODEV;
105
Lan Tianyu05f91682012-09-05 13:44:34 +0800106 status = acpi_evaluate_object(handle, "_UPC", NULL, &buffer);
Matthew Garrett54d3f8c2012-05-11 16:08:28 +0800107 upc = buffer.pointer;
Matthew Garrett54d3f8c2012-05-11 16:08:28 +0800108 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 Torvaldsd8dc91b2012-10-08 07:14:06 +0900115 if (pld->user_visible)
Lan Tianyu05f91682012-09-05 13:44:34 +0800116 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 Torvaldsd8dc91b2012-10-08 07:14:06 +0900121 else if (!pld->user_visible)
Lan Tianyu05f91682012-09-05 13:44:34 +0800122 usb_set_hub_port_connect_type(hdev, port1, USB_PORT_NOT_USED);
Matthew Garrett54d3f8c2012-05-11 16:08:28 +0800123
124out:
Linus Torvaldsd8dc91b2012-10-08 07:14:06 +0900125 ACPI_FREE(pld);
Matthew Garrett54d3f8c2012-05-11 16:08:28 +0800126 kfree(upc);
127 return ret;
128}
129
Matthew Garrettda0af6e2012-05-11 16:08:27 +0800130static int usb_acpi_find_device(struct device *dev, acpi_handle *handle)
131{
132 struct usb_device *udev;
Matthew Garrettda0af6e2012-05-11 16:08:27 +0800133 acpi_handle *parent_handle;
Lan Tianyud5575422012-09-05 13:44:33 +0800134 int port_num;
Matthew Garrettda0af6e2012-05-11 16:08:27 +0800135
Lan Tianyud5575422012-09-05 13:44:33 +0800136 /*
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 Tianyu05f91682012-09-05 13:44:34 +0800151 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 Tianyud5575422012-09-05 13:44:33 +0800172 return -ENODEV;
Lan Tianyu05f91682012-09-05 13:44:34 +0800173 }
174
Lan Tianyud5575422012-09-05 13:44:33 +0800175 /* root hub's parent is the usb hcd. */
Rafael J. Wysocki3a83f992013-11-14 23:17:21 +0100176 parent_handle = ACPI_HANDLE(dev->parent);
Lan Tianyud5575422012-09-05 13:44:33 +0800177 *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 Garrettda0af6e2012-05-11 16:08:27 +0800185
Lan Tianyud5575422012-09-05 13:44:33 +0800186 /*
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 Tianyubafcaf62013-03-19 16:48:13 +0800192 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 Tianyud5575422012-09-05 13:44:33 +0800196 port_num);
Rafael J. Wysocki3a83f992013-11-14 23:17:21 +0100197 *handle = acpi_get_child(ACPI_HANDLE(&udev->dev),
Lan Tianyubafcaf62013-03-19 16:48:13 +0800198 raw_port_num);
Lan Tianyud5575422012-09-05 13:44:33 +0800199 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 Garrettda0af6e2012-05-11 16:08:27 +0800207
Lan Tianyud5575422012-09-05 13:44:33 +0800208 *handle = acpi_get_child(parent_handle, port_num);
209 if (!*handle)
210 return -ENODEV;
211 }
Lan Tianyu05f91682012-09-05 13:44:34 +0800212 usb_acpi_check_port_connect_type(udev, *handle, port_num);
Lan Tianyud5575422012-09-05 13:44:33 +0800213 } else
Matthew Garrettda0af6e2012-05-11 16:08:27 +0800214 return -ENODEV;
215
Matthew Garrettda0af6e2012-05-11 16:08:27 +0800216 return 0;
217}
218
Rafael J. Wysocki53540092013-03-03 22:35:20 +0100219static bool usb_acpi_bus_match(struct device *dev)
220{
221 return is_usb_device(dev) || is_usb_port(dev);
222}
223
Matthew Garrettda0af6e2012-05-11 16:08:27 +0800224static struct acpi_bus_type usb_acpi_bus = {
Rafael J. Wysocki53540092013-03-03 22:35:20 +0100225 .name = "USB",
226 .match = usb_acpi_bus_match,
Matthew Garrettda0af6e2012-05-11 16:08:27 +0800227 .find_device = usb_acpi_find_device,
228};
229
230int usb_acpi_register(void)
231{
232 return register_acpi_bus_type(&usb_acpi_bus);
233}
234
235void usb_acpi_unregister(void)
236{
237 unregister_acpi_bus_type(&usb_acpi_bus);
238}