Jean-Jacques Hiblot | 0131162 | 2018-11-29 10:52:46 +0100 | [diff] [blame] | 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 | |
Patrick Delaunay | b953ec2 | 2021-04-27 11:02:19 +0200 | [diff] [blame] | 7 | #define LOG_CATEGORY UCLASS_USB_GADGET_GENERIC |
| 8 | |
Jean-Jacques Hiblot | 0131162 | 2018-11-29 10:52:46 +0100 | [diff] [blame] | 9 | #include <common.h> |
| 10 | #include <dm.h> |
| 11 | #include <dm/device-internal.h> |
| 12 | #include <linux/usb/gadget.h> |
| 13 | |
Jean-Jacques Hiblot | b3c518a | 2018-12-21 09:50:21 +0100 | [diff] [blame] | 14 | #if CONFIG_IS_ENABLED(DM_USB_GADGET) |
Jean-Jacques Hiblot | 0131162 | 2018-11-29 10:52:46 +0100 | [diff] [blame] | 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; |
Jean-Jacques Hiblot | 801f1fa | 2018-12-15 17:43:27 +0100 | [diff] [blame] | 26 | ret = uclass_get_device_by_seq(UCLASS_USB_GADGET_GENERIC, index, &dev); |
Jean-Jacques Hiblot | 0131162 | 2018-11-29 10:52:46 +0100 | [diff] [blame] | 27 | if (!dev || ret) { |
Jean-Jacques Hiblot | e81d9de | 2019-01-24 15:44:53 +0100 | [diff] [blame] | 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 | } |
Jean-Jacques Hiblot | 0131162 | 2018-11-29 10:52:46 +0100 | [diff] [blame] | 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 |
Simon Glass | 6379a94 | 2021-03-25 10:26:05 +1300 | [diff] [blame] | 50 | return -ENOSYS; |
Jean-Jacques Hiblot | 0131162 | 2018-11-29 10:52:46 +0100 | [diff] [blame] | 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 | } |
Jean-Jacques Hiblot | b3c518a | 2018-12-21 09:50:21 +0100 | [diff] [blame] | 60 | #endif |
Jean-Jacques Hiblot | 0131162 | 2018-11-29 10:52:46 +0100 | [diff] [blame] | 61 | |
| 62 | UCLASS_DRIVER(usb_gadget_generic) = { |
| 63 | .id = UCLASS_USB_GADGET_GENERIC, |
Jean-Jacques Hiblot | 801f1fa | 2018-12-15 17:43:27 +0100 | [diff] [blame] | 64 | .name = "usb", |
| 65 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
Jean-Jacques Hiblot | 0131162 | 2018-11-29 10:52:46 +0100 | [diff] [blame] | 66 | }; |