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 #include <common.h>
8 #include <dm.h>
9 #include <dm/device-internal.h>
10 #include <linux/usb/gadget.h>
11 
12 #define MAX_UDC_DEVICES 4
13 static struct udevice *dev_array[MAX_UDC_DEVICES];
14 int usb_gadget_initialize(int index)
15 {
16 	int ret;
17 	struct udevice *dev = NULL;
18 
19 	if (index < 0 || index >= ARRAY_SIZE(dev_array))
20 		return -EINVAL;
21 	if (dev_array[index])
22 		return 0;
23 	ret = uclass_get_device(UCLASS_USB_GADGET_GENERIC, index, &dev);
24 	if (!dev || ret) {
25 		pr_err("No USB device found\n");
26 		return -ENODEV;
27 	}
28 	dev_array[index] = dev;
29 	return 0;
30 }
31 
32 int usb_gadget_release(int index)
33 {
34 #if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
35 	int ret;
36 	if (index < 0 || index >= ARRAY_SIZE(dev_array))
37 		return -EINVAL;
38 
39 	ret = device_remove(dev_array[index], DM_REMOVE_NORMAL);
40 	if (!ret)
41 		dev_array[index] = NULL;
42 	return ret;
43 #else
44 	return -ENOTSUPP;
45 #endif
46 }
47 
48 int usb_gadget_handle_interrupts(int index)
49 {
50 	if (index < 0 || index >= ARRAY_SIZE(dev_array))
51 		return -EINVAL;
52 	return dm_usb_gadget_handle_interrupts(dev_array[index]);
53 }
54 
55 UCLASS_DRIVER(usb_gadget_generic) = {
56 	.id		= UCLASS_USB_GADGET_GENERIC,
57 	.name		= "usb_gadget_generic",
58 };
59