de8861829c73451ef6be10ccde035546d453a14d
[platform/kernel/u-boot.git] / drivers / usb / gadget / udc / udc-uclass.c
1 // 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
7 #define LOG_CATEGORY UCLASS_USB_GADGET_GENERIC
8
9 #include <common.h>
10 #include <dm.h>
11 #include <dm/device-internal.h>
12 #include <linux/usb/gadget.h>
13
14 #if CONFIG_IS_ENABLED(DM_USB_GADGET)
15 #define MAX_UDC_DEVICES 4
16 static struct udevice *dev_array[MAX_UDC_DEVICES];
17 int 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;
26         ret = uclass_get_device_by_seq(UCLASS_USB_GADGET_GENERIC, index, &dev);
27         if (!dev || ret) {
28                 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                 }
33         }
34         dev_array[index] = dev;
35         return 0;
36 }
37
38 int 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
50         return -ENOSYS;
51 #endif
52 }
53
54 int 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 }
60 #endif
61
62 UCLASS_DRIVER(usb_gadget_generic) = {
63         .id             = UCLASS_USB_GADGET_GENERIC,
64         .name           = "usb",
65         .flags          = DM_UC_FLAG_SEQ_ALIAS,
66 };