3 * SPDX-License-Identifier: BSD-2-Clause-NetBSD
5 * Copyright (c) 1998 The NetBSD Foundation, Inc. All rights reserved.
6 * Copyright (c) 1998 Lennart Augustsson. All rights reserved.
7 * Copyright (c) 2008-2010 Hans Petter Selasky. All rights reserved.
8 * Copyright (c) 2019 Takanori Watanabe.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * USB spec: http://www.usb.org/developers/docs/usbspec.zip
36 #ifdef USB_GLOBAL_INCLUDE_FILE
37 #include USB_GLOBAL_INCLUDE_FILE
39 #include <sys/stdint.h>
40 #include <sys/stddef.h>
41 #include <sys/param.h>
42 #include <sys/queue.h>
43 #include <sys/types.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
47 #include <sys/module.h>
49 #include <sys/mutex.h>
50 #include <sys/condvar.h>
51 #include <sys/sysctl.h>
53 #include <sys/unistd.h>
54 #include <sys/callout.h>
55 #include <sys/malloc.h>
58 #include <dev/usb/usb.h>
59 #include <dev/usb/usbdi.h>
60 #include <dev/usb/usbdi_util.h>
62 #define USB_DEBUG_VAR uhub_debug
64 #include <dev/usb/usb_core.h>
65 #include <dev/usb/usb_process.h>
66 #include <dev/usb/usb_device.h>
67 #include <dev/usb/usb_request.h>
68 #include <dev/usb/usb_debug.h>
69 #include <dev/usb/usb_hub.h>
70 #include <dev/usb/usb_util.h>
71 #include <dev/usb/usb_busdma.h>
72 #include <dev/usb/usb_transfer.h>
73 #include <dev/usb/usb_dynamic.h>
75 #include <dev/usb/usb_controller.h>
76 #include <dev/usb/usb_bus.h>
77 #endif /* USB_GLOBAL_INCLUDE_FILE */
78 #include <dev/usb/usb_hub_private.h>
79 #include <contrib/dev/acpica/include/acpi.h>
80 #include <contrib/dev/acpica/include/accommon.h>
81 #include <dev/acpica/acpivar.h>
83 struct acpi_uhub_softc {
84 struct uhub_softc usc;
86 ACPI_HANDLE *porthandle;
90 acpi_uhub_find_rh_cb(ACPI_HANDLE ah, UINT32 nl, void *ctx, void **status)
92 ACPI_DEVICE_INFO *devinfo;
98 ret = AcpiGetObjectInfo(ah, &devinfo);
99 if (ACPI_SUCCESS(ret)) {
100 if ((devinfo->Valid & ACPI_VALID_ADR) &&
101 (devinfo->Address == 0)) {
102 ret = AE_CTRL_TERMINATE;
111 acpi_uhub_parse_upc(device_t dev, unsigned int port, ACPI_HANDLE ah)
116 buf.Length = ACPI_ALLOCATE_BUFFER;
118 if (AcpiEvaluateObject(ah, "_UPC", NULL, &buf) == AE_OK) {
119 UINT64 porttypenum, conn;
120 const char *connectable;
121 const char *typelist[] = {
122 "TypeA", "MiniAB", "Express",
123 "USB3-A", "USB3-B", "USB-MicroB",
124 "USB3-MicroAB", "USB3-PowerB",
125 "TypeC-USB2", "TypeC-Switch",
128 const char *porttype;
129 const int last = sizeof(typelist) / sizeof(typelist[0]);
130 ACPI_OBJECT *obj = buf.Pointer;
132 acpi_PkgInt(obj, 0, &conn);
133 acpi_PkgInt(obj, 1, &porttypenum);
134 connectable = conn ? "" : "non";
135 if (porttypenum == 0xff)
136 porttype = "Proprietary";
137 else if (porttypenum < last) {
138 porttype = typelist[porttypenum];
140 porttype = "Unknown";
143 device_printf(dev, "Port %u %sconnectable %s\n",
144 port, connectable, porttype);
146 AcpiOsFree(buf.Pointer);
152 acpi_uhub_parse_pld(device_t dev, unsigned int port, ACPI_HANDLE ah)
157 buf.Length = ACPI_ALLOCATE_BUFFER;
159 if (AcpiEvaluateObject(ah, "_PLD", NULL, &buf) == AE_OK) {
161 unsigned char *resbuf;
166 if (obj->Type == ACPI_TYPE_PACKAGE
167 && obj->Package.Elements[0].Type == ACPI_TYPE_BUFFER) {
170 obj1 = &obj->Package.Elements[0];
171 len = obj1->Buffer.Length;
172 resbuf = obj1->Buffer.Pointer;
173 } else if (obj->Type == ACPI_TYPE_BUFFER) {
174 len = obj->Buffer.Length;
175 resbuf = obj->Buffer.Pointer;
180 device_printf(dev, "Revision:%d\n",
182 if ((resbuf[0] & 0x80) == 0) {
184 "Color:#%02x%02x%02x\n",
185 resbuf[1], resbuf[2],
188 device_printf(dev, "Width %d mm Height %d mm\n",
189 resbuf[4] | (resbuf[5] << 8),
190 resbuf[6] | (resbuf[7] << 8));
192 device_printf(dev, "Visible\n");
195 device_printf(dev, "Dock\n");
198 device_printf(dev, "Lid\n");
200 device_printf(dev, "PanelPosition: %d\n",
201 (resbuf[8] >> 3) & 7);
202 device_printf(dev, "VertPosition: %d\n",
203 (resbuf[8] >> 6) & 3);
204 device_printf(dev, "HorizPosition: %d\n",
206 device_printf(dev, "Shape: %d\n",
207 (resbuf[9] >> 2) & 0xf);
208 device_printf(dev, "80: %02x, %02x, %02x\n",
209 resbuf[9], resbuf[10], resbuf[11]);
210 device_printf(dev, "96: %02x, %02x, %02x, %02x\n",
211 resbuf[12], resbuf[13],
212 resbuf[14], resbuf[15]);
214 if ((resbuf[0] & 0x7f) >= 2) {
215 device_printf(dev, "VOFF%d mm HOFF %dmm",
216 resbuf[16] | (resbuf[17] << 8),
217 resbuf[18] | (resbuf[19] << 8));
221 AcpiOsFree(buf.Pointer);
227 acpi_uhub_find_rh(device_t dev, ACPI_HANDLE *ah)
233 grand = device_get_parent(device_get_parent(dev));
235 if ((gah = acpi_get_handle(grand)) == NULL)
238 return (AcpiWalkNamespace(ACPI_TYPE_DEVICE, gah, 1,
239 acpi_uhub_find_rh_cb, NULL, dev, ah));
243 acpi_usb_hub_port_probe_cb(ACPI_HANDLE ah, UINT32 lv, void *ctx, void **rv)
245 ACPI_DEVICE_INFO *devinfo;
247 struct acpi_uhub_softc *sc = device_get_softc(dev);
250 ret = AcpiGetObjectInfo(ah, &devinfo);
251 if (ACPI_SUCCESS(ret)) {
252 if ((devinfo->Valid & ACPI_VALID_ADR) &&
253 (devinfo->Address > 0) &&
254 (devinfo->Address <= (uint64_t)sc->nports)) {
255 sc->porthandle[devinfo->Address - 1] = ah;
256 acpi_uhub_parse_upc(dev, devinfo->Address, ah);
257 acpi_uhub_parse_pld(dev, devinfo->Address, ah);
265 acpi_usb_hub_port_probe(device_t dev, ACPI_HANDLE ah)
267 return (AcpiWalkNamespace(ACPI_TYPE_DEVICE,
269 acpi_usb_hub_port_probe_cb,
274 acpi_uhub_root_probe(device_t dev)
279 if (acpi_disabled("usb"))
282 status = acpi_uhub_find_rh(dev, &ah);
283 if (ACPI_SUCCESS(status) && ah != NULL &&
284 uhub_probe(dev) <= 0) {
285 /* success prior than non-ACPI USB HUB */
286 return (BUS_PROBE_DEFAULT + 1);
292 acpi_uhub_probe(device_t dev)
296 if (acpi_disabled("usb"))
299 ah = acpi_get_handle(dev);
303 if (uhub_probe(dev) <= 0) {
304 /* success prior than non-ACPI USB HUB */
305 return (BUS_PROBE_DEFAULT + 1);
311 acpi_uhub_root_attach(device_t dev)
313 struct acpi_uhub_softc *sc = device_get_softc(dev);
318 ret = uhub_attach(dev);
322 status = acpi_uhub_find_rh(dev, &ah);
323 if (ACPI_SUCCESS(status) && ah != NULL) {
324 struct usb_hub *uh = sc->usc.sc_udev->hub;
326 sc->nports = uh->nports;
327 sc->porthandle = malloc(sizeof(ACPI_HANDLE) * uh->nports,
328 M_USBDEV, M_WAITOK | M_ZERO);
329 acpi_usb_hub_port_probe(dev, ah);
336 acpi_uhub_attach(device_t dev)
338 struct acpi_uhub_softc *sc = device_get_softc(dev);
342 ret = uhub_attach(dev);
346 ah = acpi_get_handle(dev);
348 struct usb_hub *uh = sc->usc.sc_udev->hub;
350 sc->nports = uh->nports;
351 sc->porthandle = malloc(sizeof(ACPI_HANDLE) * uh->nports,
352 M_USBDEV, M_WAITOK | M_ZERO);
353 acpi_usb_hub_port_probe(dev, ah);
360 acpi_uhub_read_ivar(device_t dev, device_t child, int idx, uintptr_t *res)
362 struct hub_result hres;
363 struct acpi_uhub_softc *sc = device_get_softc(dev);
367 uhub_find_iface_index(sc->usc.sc_udev->hub, child, &hres);
370 if ((idx == ACPI_IVAR_HANDLE) &&
372 (hres.portno <= sc->nports) &&
373 (ah = sc->porthandle[hres.portno - 1])) {
374 *res = (uintptr_t)ah;
381 acpi_uhub_child_location_string(device_t parent, device_t child,
382 char *buf, size_t buflen)
386 uhub_child_location_string(parent, child, buf, buflen);
388 ah = acpi_get_handle(child);
390 strlcat(buf, " handle=", buflen);
391 strlcat(buf, acpi_name(ah), buflen);
397 acpi_uhub_detach(device_t dev)
399 struct acpi_uhub_softc *sc = device_get_softc(dev);
401 free(sc->porthandle, M_USBDEV);
403 return (uhub_detach(dev));
406 static device_method_t acpi_uhub_methods[] = {
407 DEVMETHOD(device_probe, acpi_uhub_probe),
408 DEVMETHOD(device_attach, acpi_uhub_attach),
409 DEVMETHOD(device_detach, acpi_uhub_detach),
410 DEVMETHOD(bus_child_location_str, acpi_uhub_child_location_string),
411 DEVMETHOD(bus_read_ivar, acpi_uhub_read_ivar),
416 static device_method_t acpi_uhub_root_methods[] = {
417 DEVMETHOD(device_probe, acpi_uhub_root_probe),
418 DEVMETHOD(device_attach, acpi_uhub_root_attach),
419 DEVMETHOD(device_detach, acpi_uhub_detach),
420 DEVMETHOD(bus_read_ivar, acpi_uhub_read_ivar),
421 DEVMETHOD(bus_child_location_str, acpi_uhub_child_location_string),
425 static devclass_t uhub_devclass;
426 extern driver_t uhub_driver;
427 static kobj_class_t uhub_baseclasses[] = {&uhub_driver, NULL};
429 static driver_t acpi_uhub_driver = {
431 .methods = acpi_uhub_methods,
432 .size = sizeof(struct acpi_uhub_softc),
433 .baseclasses = uhub_baseclasses,
436 static driver_t acpi_uhub_root_driver = {
438 .methods = acpi_uhub_root_methods,
439 .size = sizeof(struct acpi_uhub_softc),
440 .baseclasses = uhub_baseclasses,
443 DRIVER_MODULE(acpi_uhub, uhub, acpi_uhub_driver, uhub_devclass, 0, 0);
444 MODULE_DEPEND(acpi_uhub, acpi, 1, 1, 1);
445 DRIVER_MODULE(acpi_uhub, usbus, acpi_uhub_root_driver, uhub_devclass, 0, 0);