Jean-Jacques Hiblot | 9dc0d5c | 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 | |
| 7 | #include <common.h> |
| 8 | #include <dm.h> |
| 9 | #include <dm/device-internal.h> |
| 10 | #include <linux/usb/gadget.h> |
| 11 | |
Jean-Jacques Hiblot | db994e0 | 2018-12-21 09:50:21 +0100 | [diff] [blame] | 12 | #if CONFIG_IS_ENABLED(DM_USB_GADGET) |
Jean-Jacques Hiblot | 9dc0d5c | 2018-11-29 10:52:46 +0100 | [diff] [blame] | 13 | #define MAX_UDC_DEVICES 4 |
| 14 | static struct udevice *dev_array[MAX_UDC_DEVICES]; |
| 15 | int 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 Hiblot | 2934dc1 | 2018-12-15 17:43:27 +0100 | [diff] [blame] | 24 | ret = uclass_get_device_by_seq(UCLASS_USB_GADGET_GENERIC, index, &dev); |
Jean-Jacques Hiblot | 9dc0d5c | 2018-11-29 10:52:46 +0100 | [diff] [blame] | 25 | if (!dev || ret) { |
Jean-Jacques Hiblot | fb57691 | 2019-01-24 15:44:53 +0100 | [diff] [blame^] | 26 | ret = uclass_get_device(UCLASS_USB_GADGET_GENERIC, index, &dev); |
| 27 | if (!dev || ret) { |
| 28 | pr_err("No USB device found\n"); |
| 29 | return -ENODEV; |
| 30 | } |
Jean-Jacques Hiblot | 9dc0d5c | 2018-11-29 10:52:46 +0100 | [diff] [blame] | 31 | } |
| 32 | dev_array[index] = dev; |
| 33 | return 0; |
| 34 | } |
| 35 | |
| 36 | int usb_gadget_release(int index) |
| 37 | { |
| 38 | #if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE) |
| 39 | int ret; |
| 40 | if (index < 0 || index >= ARRAY_SIZE(dev_array)) |
| 41 | return -EINVAL; |
| 42 | |
| 43 | ret = device_remove(dev_array[index], DM_REMOVE_NORMAL); |
| 44 | if (!ret) |
| 45 | dev_array[index] = NULL; |
| 46 | return ret; |
| 47 | #else |
| 48 | return -ENOTSUPP; |
| 49 | #endif |
| 50 | } |
| 51 | |
| 52 | int usb_gadget_handle_interrupts(int index) |
| 53 | { |
| 54 | if (index < 0 || index >= ARRAY_SIZE(dev_array)) |
| 55 | return -EINVAL; |
| 56 | return dm_usb_gadget_handle_interrupts(dev_array[index]); |
| 57 | } |
Jean-Jacques Hiblot | db994e0 | 2018-12-21 09:50:21 +0100 | [diff] [blame] | 58 | #endif |
Jean-Jacques Hiblot | 9dc0d5c | 2018-11-29 10:52:46 +0100 | [diff] [blame] | 59 | |
| 60 | UCLASS_DRIVER(usb_gadget_generic) = { |
| 61 | .id = UCLASS_USB_GADGET_GENERIC, |
Jean-Jacques Hiblot | 2934dc1 | 2018-12-15 17:43:27 +0100 | [diff] [blame] | 62 | .name = "usb", |
| 63 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
Jean-Jacques Hiblot | 9dc0d5c | 2018-11-29 10:52:46 +0100 | [diff] [blame] | 64 | }; |