blob: 8d7864797a71dacf2dd137e1e8e11284c3f16cb6 [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
7#include <common.h>
8#include <dm.h>
9#include <dm/device-internal.h>
10#include <linux/usb/gadget.h>
11
Jean-Jacques Hiblotdb994e02018-12-21 09:50:21 +010012#if CONFIG_IS_ENABLED(DM_USB_GADGET)
Jean-Jacques Hiblot9dc0d5c2018-11-29 10:52:46 +010013#define MAX_UDC_DEVICES 4
14static struct udevice *dev_array[MAX_UDC_DEVICES];
15int usb_gadget_initialize(int index)
16{
17 int ret;
18 struct udevice *dev = NULL;
19
20 if (index < 0 || index >= ARRAY_SIZE(dev_array))
21 return -EINVAL;
22 if (dev_array[index])
23 return 0;
Jean-Jacques Hiblot2934dc12018-12-15 17:43:27 +010024 ret = uclass_get_device_by_seq(UCLASS_USB_GADGET_GENERIC, index, &dev);
Jean-Jacques Hiblot9dc0d5c2018-11-29 10:52:46 +010025 if (!dev || ret) {
26 pr_err("No USB device found\n");
27 return -ENODEV;
28 }
29 dev_array[index] = dev;
30 return 0;
31}
32
33int usb_gadget_release(int index)
34{
35#if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
36 int ret;
37 if (index < 0 || index >= ARRAY_SIZE(dev_array))
38 return -EINVAL;
39
40 ret = device_remove(dev_array[index], DM_REMOVE_NORMAL);
41 if (!ret)
42 dev_array[index] = NULL;
43 return ret;
44#else
45 return -ENOTSUPP;
46#endif
47}
48
49int usb_gadget_handle_interrupts(int index)
50{
51 if (index < 0 || index >= ARRAY_SIZE(dev_array))
52 return -EINVAL;
53 return dm_usb_gadget_handle_interrupts(dev_array[index]);
54}
Jean-Jacques Hiblotdb994e02018-12-21 09:50:21 +010055#endif
Jean-Jacques Hiblot9dc0d5c2018-11-29 10:52:46 +010056
57UCLASS_DRIVER(usb_gadget_generic) = {
58 .id = UCLASS_USB_GADGET_GENERIC,
Jean-Jacques Hiblot2934dc12018-12-15 17:43:27 +010059 .name = "usb",
60 .flags = DM_UC_FLAG_SEQ_ALIAS,
Jean-Jacques Hiblot9dc0d5c2018-11-29 10:52:46 +010061};