1 // SPDX-License-Identifier: GPL-2.0-only
3 * API for creating and destroying USB onboard hub platform devices
5 * Copyright (c) 2022, Google LLC
8 #include <linux/device.h>
9 #include <linux/export.h>
10 #include <linux/kernel.h>
11 #include <linux/list.h>
13 #include <linux/of_platform.h>
14 #include <linux/platform_device.h>
15 #include <linux/usb.h>
16 #include <linux/usb/hcd.h>
17 #include <linux/usb/of.h>
18 #include <linux/usb/onboard_hub.h>
20 #include "onboard_usb_hub.h"
22 struct pdev_list_entry {
23 struct platform_device *pdev;
24 struct list_head node;
27 static bool of_is_onboard_usb_hub(const struct device_node *np)
29 return !!of_match_node(onboard_hub_match, np);
33 * onboard_hub_create_pdevs -- create platform devices for onboard USB hubs
34 * @parent_hub : parent hub to scan for connected onboard hubs
35 * @pdev_list : list of onboard hub platform devices owned by the parent hub
37 * Creates a platform device for each supported onboard hub that is connected to
38 * the given parent hub. The platform device is in charge of initializing the
39 * hub (enable regulators, take the hub out of reset, ...) and can optionally
40 * control whether the hub remains powered during system suspend or not.
42 * To keep track of the platform devices they are added to a list that is owned
45 * Some background about the logic in this function, which can be a bit hard
48 * Root hubs don't have dedicated device tree nodes, but use the node of their
49 * HCD. The primary and secondary HCD are usually represented by a single DT
50 * node. That means the root hubs of the primary and secondary HCD share the
51 * same device tree node (the HCD node). As a result this function can be called
52 * twice with the same DT node for root hubs. We only want to create a single
53 * platform device for each physical onboard hub, hence for root hubs the loop
54 * is only executed for the root hub of the primary HCD. Since the function
55 * scans through all child nodes it still creates pdevs for onboard hubs
56 * connected to the root hub of the secondary HCD if needed.
58 * Further there must be only one platform device for onboard hubs with a peer
59 * hub (the hub is a single physical device). To achieve this two measures are
60 * taken: pdevs for onboard hubs with a peer are only created when the function
61 * is called on behalf of the parent hub that is connected to the primary HCD
62 * (directly or through other hubs). For onboard hubs connected to root hubs
63 * the function processes the nodes of both peers. A platform device is only
64 * created if the peer hub doesn't have one already.
66 void onboard_hub_create_pdevs(struct usb_device *parent_hub, struct list_head *pdev_list)
69 struct usb_hcd *hcd = bus_to_hcd(parent_hub->bus);
70 struct device_node *np, *npc;
71 struct platform_device *pdev;
72 struct pdev_list_entry *pdle;
74 if (!parent_hub->dev.of_node)
77 if (!parent_hub->parent && !usb_hcd_is_primary_hcd(hcd))
80 for (i = 1; i <= parent_hub->maxchild; i++) {
81 np = usb_of_get_device_node(parent_hub, i);
85 if (!of_is_onboard_usb_hub(np))
88 npc = of_parse_phandle(np, "peer-hub", 0);
90 if (!usb_hcd_is_primary_hcd(hcd)) {
95 pdev = of_find_device_by_node(npc);
99 put_device(&pdev->dev);
104 pdev = of_platform_device_create(np, NULL, &parent_hub->dev);
106 dev_err(&parent_hub->dev,
107 "failed to create platform device for onboard hub '%pOF'\n", np);
111 pdle = kzalloc(sizeof(*pdle), GFP_KERNEL);
113 of_platform_device_destroy(&pdev->dev, NULL);
118 list_add(&pdle->node, pdev_list);
124 EXPORT_SYMBOL_GPL(onboard_hub_create_pdevs);
127 * onboard_hub_destroy_pdevs -- free resources of onboard hub platform devices
128 * @pdev_list : list of onboard hub platform devices
130 * Destroys the platform devices in the given list and frees the memory associated
131 * with the list entry.
133 void onboard_hub_destroy_pdevs(struct list_head *pdev_list)
135 struct pdev_list_entry *pdle, *tmp;
137 list_for_each_entry_safe(pdle, tmp, pdev_list, node) {
138 list_del(&pdle->node);
139 of_platform_device_destroy(&pdle->pdev->dev, NULL);
143 EXPORT_SYMBOL_GPL(onboard_hub_destroy_pdevs);