Jean-Jacques Hiblot | 9dc0d5c | 2018-11-29 10:52:46 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
Nishanth Menon | eaa39c6 | 2023-11-01 15:56:03 -0500 | [diff] [blame] | 3 | * Copyright (C) 2018 Texas Instruments Incorporated - https://www.ti.com |
Jean-Jacques Hiblot | 9dc0d5c | 2018-11-29 10:52:46 +0100 | [diff] [blame] | 4 | * Written by Jean-Jacques Hiblot <jjhiblot@ti.com> |
| 5 | */ |
| 6 | |
Patrick Delaunay | 8131335 | 2021-04-27 11:02:19 +0200 | [diff] [blame] | 7 | #define LOG_CATEGORY UCLASS_USB_GADGET_GENERIC |
| 8 | |
Jean-Jacques Hiblot | 9dc0d5c | 2018-11-29 10:52:46 +0100 | [diff] [blame] | 9 | #include <dm.h> |
| 10 | #include <dm/device-internal.h> |
Simon Glass | bdd5f81 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 11 | #include <linux/printk.h> |
Jean-Jacques Hiblot | 9dc0d5c | 2018-11-29 10:52:46 +0100 | [diff] [blame] | 12 | #include <linux/usb/gadget.h> |
| 13 | |
Jean-Jacques Hiblot | db994e0 | 2018-12-21 09:50:21 +0100 | [diff] [blame] | 14 | #if CONFIG_IS_ENABLED(DM_USB_GADGET) |
Marek Vasut | cf25f66 | 2024-06-14 02:51:16 +0200 | [diff] [blame] | 15 | static inline const struct usb_gadget_generic_ops * |
| 16 | usb_gadget_generic_dev_ops(struct udevice *dev) |
| 17 | { |
| 18 | return (const struct usb_gadget_generic_ops *)dev->driver->ops; |
| 19 | } |
| 20 | |
Marek Vasut | 5d3df20 | 2024-06-14 02:51:26 +0200 | [diff] [blame] | 21 | int dm_usb_gadget_handle_interrupts(struct udevice *dev) |
Marek Vasut | cf25f66 | 2024-06-14 02:51:16 +0200 | [diff] [blame] | 22 | { |
| 23 | const struct usb_gadget_generic_ops *ops; |
| 24 | |
| 25 | ops = usb_gadget_generic_dev_ops(dev); |
| 26 | if (!ops) |
| 27 | return -EFAULT; |
| 28 | if (!ops->handle_interrupts) |
| 29 | return -ENOSYS; |
| 30 | |
| 31 | return ops->handle_interrupts(dev); |
| 32 | } |
| 33 | |
Marek Vasut | b397203 | 2023-09-01 11:49:47 +0200 | [diff] [blame] | 34 | int udc_device_get_by_index(int index, struct udevice **udev) |
| 35 | { |
| 36 | struct udevice *dev = NULL; |
| 37 | int ret; |
| 38 | |
| 39 | ret = uclass_get_device_by_seq(UCLASS_USB_GADGET_GENERIC, index, &dev); |
| 40 | if (!ret && dev) { |
| 41 | *udev = dev; |
| 42 | return 0; |
| 43 | } |
| 44 | |
| 45 | ret = uclass_get_device(UCLASS_USB_GADGET_GENERIC, index, &dev); |
| 46 | if (!ret && dev) { |
| 47 | *udev = dev; |
| 48 | return 0; |
| 49 | } |
| 50 | |
| 51 | pr_err("No USB device found\n"); |
| 52 | return -ENODEV; |
| 53 | } |
| 54 | |
| 55 | int udc_device_put(struct udevice *udev) |
| 56 | { |
| 57 | #if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE) |
| 58 | return device_remove(udev, DM_REMOVE_NORMAL); |
| 59 | #else |
| 60 | return -ENOSYS; |
| 61 | #endif |
| 62 | } |
Marek Vasut | b397203 | 2023-09-01 11:49:47 +0200 | [diff] [blame] | 63 | #else |
| 64 | /* Backwards hardware compatibility -- switch to DM_USB_GADGET */ |
| 65 | static int legacy_index; |
| 66 | int udc_device_get_by_index(int index, struct udevice **udev) |
| 67 | { |
| 68 | legacy_index = index; |
| 69 | return board_usb_init(index, USB_INIT_DEVICE); |
| 70 | } |
| 71 | |
| 72 | int udc_device_put(struct udevice *udev) |
| 73 | { |
| 74 | return board_usb_cleanup(legacy_index, USB_INIT_DEVICE); |
| 75 | } |
Marek Vasut | cf25f66 | 2024-06-14 02:51:16 +0200 | [diff] [blame] | 76 | |
| 77 | __weak int dm_usb_gadget_handle_interrupts(struct udevice *dev) |
| 78 | { |
| 79 | return 0; |
| 80 | } |
Jean-Jacques Hiblot | db994e0 | 2018-12-21 09:50:21 +0100 | [diff] [blame] | 81 | #endif |
Jean-Jacques Hiblot | 9dc0d5c | 2018-11-29 10:52:46 +0100 | [diff] [blame] | 82 | |
Marek Vasut | b397203 | 2023-09-01 11:49:47 +0200 | [diff] [blame] | 83 | #if CONFIG_IS_ENABLED(DM) |
Jean-Jacques Hiblot | 9dc0d5c | 2018-11-29 10:52:46 +0100 | [diff] [blame] | 84 | UCLASS_DRIVER(usb_gadget_generic) = { |
| 85 | .id = UCLASS_USB_GADGET_GENERIC, |
Jean-Jacques Hiblot | 2934dc1 | 2018-12-15 17:43:27 +0100 | [diff] [blame] | 86 | .name = "usb", |
| 87 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
Jean-Jacques Hiblot | 9dc0d5c | 2018-11-29 10:52:46 +0100 | [diff] [blame] | 88 | }; |
Marek Vasut | b397203 | 2023-09-01 11:49:47 +0200 | [diff] [blame] | 89 | #endif |