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>
18#include <linux/usb/hcd.h>
19
20#include "hub.h"
21
22/**
23 * usb_acpi_power_manageable - check whether usb port has
24 * acpi power resource.
25 * @hdev: USB device belonging to the usb hub
26 * @index: port index based zero
27 *
28 * Return true if the port has acpi power resource and false if no.
29 */
30bool usb_acpi_power_manageable(struct usb_device *hdev, int index)
31{
32	acpi_handle port_handle;
33	int port1 = index + 1;
34
35	port_handle = usb_get_hub_port_acpi_handle(hdev,
36		port1);
37	if (port_handle)
38		return acpi_bus_power_manageable(port_handle);
39	else
40		return false;
41}
42EXPORT_SYMBOL_GPL(usb_acpi_power_manageable);
43
44/**
45 * usb_acpi_set_power_state - control usb port's power via acpi power
46 * resource
47 * @hdev: USB device belonging to the usb hub
48 * @index: port index based zero
49 * @enable: power state expected to be set
50 *
51 * Notice to use usb_acpi_power_manageable() to check whether the usb port
52 * has acpi power resource before invoking this function.
53 *
54 * Returns 0 on success, else negative errno.
55 */
56int usb_acpi_set_power_state(struct usb_device *hdev, int index, bool enable)
57{
58	struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
59	struct usb_port *port_dev;
60	acpi_handle port_handle;
61	unsigned char state;
62	int port1 = index + 1;
63	int error = -EINVAL;
64
65	if (!hub)
66		return -ENODEV;
67	port_dev = hub->ports[port1 - 1];
68
69	port_handle = (acpi_handle) usb_get_hub_port_acpi_handle(hdev, port1);
70	if (!port_handle)
71		return error;
72
73	if (enable)
74		state = ACPI_STATE_D0;
75	else
76		state = ACPI_STATE_D3_COLD;
77
78	error = acpi_bus_set_power(port_handle, state);
79	if (!error)
80		dev_dbg(&port_dev->dev, "acpi: power was set to %d\n", enable);
81	else
82		dev_dbg(&port_dev->dev, "acpi: power failed to be set\n");
83
84	return error;
85}
86EXPORT_SYMBOL_GPL(usb_acpi_set_power_state);
87
88static enum usb_port_connect_type usb_acpi_get_connect_type(acpi_handle handle,
89		struct acpi_pld_info *pld)
90{
91	enum usb_port_connect_type connect_type = USB_PORT_CONNECT_TYPE_UNKNOWN;
92	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
93	union acpi_object *upc;
94	acpi_status status;
95
96	/*
97	 * According to ACPI Spec 9.13. PLD indicates whether usb port is
98	 * user visible and _UPC indicates whether it is connectable. If
99	 * the port was visible and connectable, it could be freely connected
100	 * and disconnected with USB devices. If no visible and connectable,
101	 * a usb device is directly hard-wired to the port. If no visible and
102	 * no connectable, the port would be not used.
103	 */
104	status = acpi_evaluate_object(handle, "_UPC", NULL, &buffer);
105	upc = buffer.pointer;
106	if (!upc || (upc->type != ACPI_TYPE_PACKAGE)
107		|| upc->package.count != 4) {
108		goto out;
109	}
110
111	if (upc->package.elements[0].integer.value)
112		if (pld->user_visible)
113			connect_type = USB_PORT_CONNECT_TYPE_HOT_PLUG;
114		else
115			connect_type = USB_PORT_CONNECT_TYPE_HARD_WIRED;
116	else if (!pld->user_visible)
117		connect_type = USB_PORT_NOT_USED;
118out:
119	kfree(upc);
120	return connect_type;
121}
122
123
124/*
125 * Private to usb-acpi, all the core needs to know is that
126 * port_dev->location is non-zero when it has been set by the firmware.
127 */
128#define USB_ACPI_LOCATION_VALID (1 << 31)
129
130static struct acpi_device *usb_acpi_find_companion(struct device *dev)
131{
132	struct usb_device *udev;
133	struct acpi_device *adev;
134	acpi_handle *parent_handle;
135
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);
151		if (udev->parent)
152			return NULL;
153
154		/* root hub is only child (_ADR=0) under its parent, the HC */
155		adev = ACPI_COMPANION(dev->parent);
156		return acpi_find_child_device(adev, 0, false);
157	} else if (is_usb_port(dev)) {
158		struct usb_port *port_dev = to_usb_port(dev);
159		int port1 = port_dev->portnum;
160		struct acpi_pld_info *pld;
161		acpi_handle *handle;
162		acpi_status status;
163
164		/* Get the struct usb_device point of port's hub */
165		udev = to_usb_device(dev->parent->parent);
166
167		/*
168		 * The root hub ports' parent is the root hub. The non-root-hub
169		 * ports' parent is the parent hub port which the hub is
170		 * connected to.
171		 */
172		if (!udev->parent) {
173			struct usb_hcd *hcd = bus_to_hcd(udev->bus);
174			int raw;
175
176			raw = usb_hcd_find_raw_port_number(hcd, port1);
177			adev = acpi_find_child_device(ACPI_COMPANION(&udev->dev),
178					raw, false);
179			if (!adev)
180				return NULL;
181		} else {
182			parent_handle =
183				usb_get_hub_port_acpi_handle(udev->parent,
184				udev->portnum);
185			if (!parent_handle)
186				return NULL;
187
188			acpi_bus_get_device(parent_handle, &adev);
189			adev = acpi_find_child_device(adev, port1, false);
190			if (!adev)
191				return NULL;
192		}
193		handle = adev->handle;
194		status = acpi_get_physical_device_location(handle, &pld);
195		if (ACPI_FAILURE(status) || !pld)
196			return adev;
197
198		port_dev->location = USB_ACPI_LOCATION_VALID
199			| pld->group_token << 8 | pld->group_position;
200		port_dev->connect_type = usb_acpi_get_connect_type(handle, pld);
201		ACPI_FREE(pld);
202
203		return adev;
204	}
205
206	return NULL;
207}
208
209static bool usb_acpi_bus_match(struct device *dev)
210{
211	return is_usb_device(dev) || is_usb_port(dev);
212}
213
214static struct acpi_bus_type usb_acpi_bus = {
215	.name = "USB",
216	.match = usb_acpi_bus_match,
217	.find_companion = usb_acpi_find_companion,
218};
219
220int usb_acpi_register(void)
221{
222	return register_acpi_bus_type(&usb_acpi_bus);
223}
224
225void usb_acpi_unregister(void)
226{
227	unregister_acpi_bus_type(&usb_acpi_bus);
228}
229