blob: 30ee1cab0668de0a7469525263ae5deaed535f6d [file] [log] [blame]
Jean-Jacques Hiblot9dc0d5c2018-11-29 10:52:46 +01001// SPDX-License-Identifier: GPL-2.0+
2/*
Nishanth Menoneaa39c62023-11-01 15:56:03 -05003 * Copyright (C) 2018 Texas Instruments Incorporated - https://www.ti.com
Jean-Jacques Hiblot9dc0d5c2018-11-29 10:52:46 +01004 * 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
Tom Riniabb9a042024-05-18 20:20:43 -06009#include <common.h>
Jean-Jacques Hiblot9dc0d5c2018-11-29 10:52:46 +010010#include <dm.h>
11#include <dm/device-internal.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060012#include <linux/printk.h>
Jean-Jacques Hiblot9dc0d5c2018-11-29 10:52:46 +010013#include <linux/usb/gadget.h>
14
Jean-Jacques Hiblotdb994e02018-12-21 09:50:21 +010015#if CONFIG_IS_ENABLED(DM_USB_GADGET)
Marek Vasutb3972032023-09-01 11:49:47 +020016int udc_device_get_by_index(int index, struct udevice **udev)
17{
18 struct udevice *dev = NULL;
19 int ret;
20
21 ret = uclass_get_device_by_seq(UCLASS_USB_GADGET_GENERIC, index, &dev);
22 if (!ret && dev) {
23 *udev = dev;
24 return 0;
25 }
26
27 ret = uclass_get_device(UCLASS_USB_GADGET_GENERIC, index, &dev);
28 if (!ret && dev) {
29 *udev = dev;
30 return 0;
31 }
32
33 pr_err("No USB device found\n");
34 return -ENODEV;
35}
36
37int udc_device_put(struct udevice *udev)
38{
39#if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
40 return device_remove(udev, DM_REMOVE_NORMAL);
41#else
42 return -ENOSYS;
43#endif
44}
Marek Vasutb3972032023-09-01 11:49:47 +020045#else
46/* Backwards hardware compatibility -- switch to DM_USB_GADGET */
47static int legacy_index;
48int udc_device_get_by_index(int index, struct udevice **udev)
49{
50 legacy_index = index;
51 return board_usb_init(index, USB_INIT_DEVICE);
52}
53
54int udc_device_put(struct udevice *udev)
55{
56 return board_usb_cleanup(legacy_index, USB_INIT_DEVICE);
57}
Jean-Jacques Hiblotdb994e02018-12-21 09:50:21 +010058#endif
Jean-Jacques Hiblot9dc0d5c2018-11-29 10:52:46 +010059
Marek Vasutb3972032023-09-01 11:49:47 +020060#if CONFIG_IS_ENABLED(DM)
Jean-Jacques Hiblot9dc0d5c2018-11-29 10:52:46 +010061UCLASS_DRIVER(usb_gadget_generic) = {
62 .id = UCLASS_USB_GADGET_GENERIC,
Jean-Jacques Hiblot2934dc12018-12-15 17:43:27 +010063 .name = "usb",
64 .flags = DM_UC_FLAG_SEQ_ALIAS,
Jean-Jacques Hiblot9dc0d5c2018-11-29 10:52:46 +010065};
Marek Vasutb3972032023-09-01 11:49:47 +020066#endif