Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * API for creating and destroying USB onboard hub platform devices
0004  *
0005  * Copyright (c) 2022, Google LLC
0006  */
0007 
0008 #include <linux/device.h>
0009 #include <linux/export.h>
0010 #include <linux/kernel.h>
0011 #include <linux/list.h>
0012 #include <linux/of.h>
0013 #include <linux/of_platform.h>
0014 #include <linux/platform_device.h>
0015 #include <linux/usb.h>
0016 #include <linux/usb/hcd.h>
0017 #include <linux/usb/of.h>
0018 #include <linux/usb/onboard_hub.h>
0019 
0020 #include "onboard_usb_hub.h"
0021 
0022 struct pdev_list_entry {
0023     struct platform_device *pdev;
0024     struct list_head node;
0025 };
0026 
0027 static bool of_is_onboard_usb_hub(const struct device_node *np)
0028 {
0029     return !!of_match_node(onboard_hub_match, np);
0030 }
0031 
0032 /**
0033  * onboard_hub_create_pdevs -- create platform devices for onboard USB hubs
0034  * @parent_hub  : parent hub to scan for connected onboard hubs
0035  * @pdev_list   : list of onboard hub platform devices owned by the parent hub
0036  *
0037  * Creates a platform device for each supported onboard hub that is connected to
0038  * the given parent hub. The platform device is in charge of initializing the
0039  * hub (enable regulators, take the hub out of reset, ...) and can optionally
0040  * control whether the hub remains powered during system suspend or not.
0041  *
0042  * To keep track of the platform devices they are added to a list that is owned
0043  * by the parent hub.
0044  *
0045  * Some background about the logic in this function, which can be a bit hard
0046  * to follow:
0047  *
0048  * Root hubs don't have dedicated device tree nodes, but use the node of their
0049  * HCD. The primary and secondary HCD are usually represented by a single DT
0050  * node. That means the root hubs of the primary and secondary HCD share the
0051  * same device tree node (the HCD node). As a result this function can be called
0052  * twice with the same DT node for root hubs. We only want to create a single
0053  * platform device for each physical onboard hub, hence for root hubs the loop
0054  * is only executed for the root hub of the primary HCD. Since the function
0055  * scans through all child nodes it still creates pdevs for onboard hubs
0056  * connected to the root hub of the secondary HCD if needed.
0057  *
0058  * Further there must be only one platform device for onboard hubs with a peer
0059  * hub (the hub is a single physical device). To achieve this two measures are
0060  * taken: pdevs for onboard hubs with a peer are only created when the function
0061  * is called on behalf of the parent hub that is connected to the primary HCD
0062  * (directly or through other hubs). For onboard hubs connected to root hubs
0063  * the function processes the nodes of both peers. A platform device is only
0064  * created if the peer hub doesn't have one already.
0065  */
0066 void onboard_hub_create_pdevs(struct usb_device *parent_hub, struct list_head *pdev_list)
0067 {
0068     int i;
0069     struct usb_hcd *hcd = bus_to_hcd(parent_hub->bus);
0070     struct device_node *np, *npc;
0071     struct platform_device *pdev;
0072     struct pdev_list_entry *pdle;
0073 
0074     if (!parent_hub->dev.of_node)
0075         return;
0076 
0077     if (!parent_hub->parent && !usb_hcd_is_primary_hcd(hcd))
0078         return;
0079 
0080     for (i = 1; i <= parent_hub->maxchild; i++) {
0081         np = usb_of_get_device_node(parent_hub, i);
0082         if (!np)
0083             continue;
0084 
0085         if (!of_is_onboard_usb_hub(np))
0086             goto node_put;
0087 
0088         npc = of_parse_phandle(np, "peer-hub", 0);
0089         if (npc) {
0090             if (!usb_hcd_is_primary_hcd(hcd)) {
0091                 of_node_put(npc);
0092                 goto node_put;
0093             }
0094 
0095             pdev = of_find_device_by_node(npc);
0096             of_node_put(npc);
0097 
0098             if (pdev) {
0099                 put_device(&pdev->dev);
0100                 goto node_put;
0101             }
0102         }
0103 
0104         pdev = of_platform_device_create(np, NULL, &parent_hub->dev);
0105         if (!pdev) {
0106             dev_err(&parent_hub->dev,
0107                 "failed to create platform device for onboard hub '%pOF'\n", np);
0108             goto node_put;
0109         }
0110 
0111         pdle = kzalloc(sizeof(*pdle), GFP_KERNEL);
0112         if (!pdle) {
0113             of_platform_device_destroy(&pdev->dev, NULL);
0114             goto node_put;
0115         }
0116 
0117         pdle->pdev = pdev;
0118         list_add(&pdle->node, pdev_list);
0119 
0120 node_put:
0121         of_node_put(np);
0122     }
0123 }
0124 EXPORT_SYMBOL_GPL(onboard_hub_create_pdevs);
0125 
0126 /**
0127  * onboard_hub_destroy_pdevs -- free resources of onboard hub platform devices
0128  * @pdev_list   : list of onboard hub platform devices
0129  *
0130  * Destroys the platform devices in the given list and frees the memory associated
0131  * with the list entry.
0132  */
0133 void onboard_hub_destroy_pdevs(struct list_head *pdev_list)
0134 {
0135     struct pdev_list_entry *pdle, *tmp;
0136 
0137     list_for_each_entry_safe(pdle, tmp, pdev_list, node) {
0138         list_del(&pdle->node);
0139         of_platform_device_destroy(&pdle->pdev->dev, NULL);
0140         kfree(pdle);
0141     }
0142 }
0143 EXPORT_SYMBOL_GPL(onboard_hub_destroy_pdevs);