blob: de8861829c73451ef6be10ccde035546d453a14d [file] [log] [blame]
Jean-Jacques Hiblot9dc0d5c2018-11-29 10:52:46 +01001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2018 Texas Instruments Incorporated - http://www.ti.com
4 * Written by Jean-Jacques Hiblot <jjhiblot@ti.com>
5 */
6
Patrick Delaunay81313352021-04-27 11:02:19 +02007#define LOG_CATEGORY UCLASS_USB_GADGET_GENERIC
8
Jean-Jacques Hiblot9dc0d5c2018-11-29 10:52:46 +01009#include <common.h>
10#include <dm.h>
11#include <dm/device-internal.h>
12#include <linux/usb/gadget.h>
13
Jean-Jacques Hiblotdb994e02018-12-21 09:50:21 +010014#if CONFIG_IS_ENABLED(DM_USB_GADGET)
Jean-Jacques Hiblot9dc0d5c2018-11-29 10:52:46 +010015#define MAX_UDC_DEVICES 4
16static struct udevice *dev_array[MAX_UDC_DEVICES];
17int usb_gadget_initialize(int index)
18{
19 int ret;
20 struct udevice *dev = NULL;
21
22 if (index < 0 || index >= ARRAY_SIZE(dev_array))
23 return -EINVAL;
24 if (dev_array[index])
25 return 0;
Jean-Jacques Hiblot2934dc12018-12-15 17:43:27 +010026 ret = uclass_get_device_by_seq(UCLASS_USB_GADGET_GENERIC, index, &dev);
Jean-Jacques Hiblot9dc0d5c2018-11-29 10:52:46 +010027 if (!dev || ret) {
Jean-Jacques Hiblotfb576912019-01-24 15:44:53 +010028 ret = uclass_get_device(UCLASS_USB_GADGET_GENERIC, index, &dev);
29 if (!dev || ret) {
30 pr_err("No USB device found\n");
31 return -ENODEV;
32 }
Jean-Jacques Hiblot9dc0d5c2018-11-29 10:52:46 +010033 }
34 dev_array[index] = dev;
35 return 0;
36}
37
38int usb_gadget_release(int index)
39{
40#if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
41 int ret;
42 if (index < 0 || index >= ARRAY_SIZE(dev_array))
43 return -EINVAL;
44
45 ret = device_remove(dev_array[index], DM_REMOVE_NORMAL);
46 if (!ret)
47 dev_array[index] = NULL;
48 return ret;
49#else
Simon Glass12fb7492021-03-25 10:26:05 +130050 return -ENOSYS;
Jean-Jacques Hiblot9dc0d5c2018-11-29 10:52:46 +010051#endif
52}
53
54int usb_gadget_handle_interrupts(int index)
55{
56 if (index < 0 || index >= ARRAY_SIZE(dev_array))
57 return -EINVAL;
58 return dm_usb_gadget_handle_interrupts(dev_array[index]);
59}
Jean-Jacques Hiblotdb994e02018-12-21 09:50:21 +010060#endif
Jean-Jacques Hiblot9dc0d5c2018-11-29 10:52:46 +010061
62UCLASS_DRIVER(usb_gadget_generic) = {
63 .id = UCLASS_USB_GADGET_GENERIC,
Jean-Jacques Hiblot2934dc12018-12-15 17:43:27 +010064 .name = "usb",
65 .flags = DM_UC_FLAG_SEQ_ALIAS,
Jean-Jacques Hiblot9dc0d5c2018-11-29 10:52:46 +010066};