1// Copyright 2016 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#pragma once
6
7
8#include <ddk/driver.h>
9#include <zircon/compiler.h>
10#include <zircon/types.h>
11#include <zircon/hw/usb.h>
12#include <zircon/hw/usb-hub.h>
13#include <ddk/protocol/usb-hub.h>
14
15__BEGIN_CDECLS;
16
17typedef struct usb_bus_protocol_ops {
18    // Hub support
19    zx_status_t (*configure_hub)(void* ctx, zx_device_t* hub_device, usb_speed_t speed,
20                 usb_hub_descriptor_t* descriptor);
21    zx_status_t (*hub_device_added)(void* ctx, zx_device_t* hub_device, int port,
22                                    usb_speed_t speed);
23    zx_status_t (*hub_device_removed)(void* ctx, zx_device_t* hub_device, int port);
24    zx_status_t (*set_hub_interface)(void* ctx, zx_device_t* usb_device, usb_hub_interface_t* hub);
25} usb_bus_protocol_ops_t;
26
27typedef struct usb_bus_protocol {
28    usb_bus_protocol_ops_t* ops;
29    void* ctx;
30} usb_bus_protocol_t;
31
32static inline zx_status_t usb_bus_configure_hub(usb_bus_protocol_t* bus, zx_device_t* hub_device,
33                                                usb_speed_t speed,
34                                                usb_hub_descriptor_t* descriptor) {
35    return bus->ops->configure_hub(bus->ctx, hub_device, speed, descriptor);
36}
37
38static inline zx_status_t usb_bus_hub_device_added(usb_bus_protocol_t* bus, zx_device_t* hub_device,
39                                                   int port, usb_speed_t speed) {
40    return bus->ops->hub_device_added(bus->ctx, hub_device, port, speed);
41}
42
43static inline zx_status_t usb_bus_hub_device_removed(usb_bus_protocol_t* bus,
44                                                     zx_device_t* hub_device, int port) {
45    return bus->ops->hub_device_removed(bus->ctx, hub_device, port);
46}
47
48static inline zx_status_t usb_bus_set_hub_interface(usb_bus_protocol_t* bus,
49                                                    zx_device_t* usb_device, usb_hub_interface_t* hub) {
50    return bus->ops->set_hub_interface(bus->ctx, usb_device, hub);
51}
52
53// interface for use by the HCI controller to use to notify when devices are added and removed
54typedef struct {
55    zx_status_t (*add_device)(void* ctx, uint32_t device_id, uint32_t hub_id, usb_speed_t speed);
56    void (*remove_device)(void* ctx, uint32_t device_id);
57    void (*reset_hub_port)(void* ctx, uint32_t hub_id, uint32_t port);
58} usb_bus_interface_ops_t;
59
60typedef struct usb_bus_interface {
61    usb_bus_interface_ops_t* ops;
62    void* ctx;
63} usb_bus_interface_t;
64
65static inline zx_status_t usb_bus_add_device(usb_bus_interface_t* bus, uint32_t device_id,
66                                             uint32_t hub_id, usb_speed_t speed) {
67    return bus->ops->add_device(bus->ctx, device_id, hub_id, speed);
68}
69
70static inline void usb_bus_remove_device(usb_bus_interface_t* bus, uint32_t device_id) {
71    bus->ops->remove_device(bus->ctx, device_id);
72}
73
74static inline void usb_bus_reset_hub_port(usb_bus_interface_t* bus, uint32_t hub_id, uint32_t port) {
75    bus->ops->reset_hub_port(bus->ctx, hub_id, port);
76}
77
78__END_CDECLS;
79