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 | |
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 <common.h> |
| 10 | #include <dm.h> |
| 11 | #include <dm/device-internal.h> |
| 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 | b397203 | 2023-09-01 11:49:47 +0200 | [diff] [blame] | 15 | int udc_device_get_by_index(int index, struct udevice **udev) |
| 16 | { |
| 17 | struct udevice *dev = NULL; |
| 18 | int ret; |
| 19 | |
| 20 | ret = uclass_get_device_by_seq(UCLASS_USB_GADGET_GENERIC, index, &dev); |
| 21 | if (!ret && dev) { |
| 22 | *udev = dev; |
| 23 | return 0; |
| 24 | } |
| 25 | |
| 26 | ret = uclass_get_device(UCLASS_USB_GADGET_GENERIC, index, &dev); |
| 27 | if (!ret && dev) { |
| 28 | *udev = dev; |
| 29 | return 0; |
| 30 | } |
| 31 | |
| 32 | pr_err("No USB device found\n"); |
| 33 | return -ENODEV; |
| 34 | } |
| 35 | |
| 36 | int udc_device_put(struct udevice *udev) |
| 37 | { |
| 38 | #if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE) |
| 39 | return device_remove(udev, DM_REMOVE_NORMAL); |
| 40 | #else |
| 41 | return -ENOSYS; |
| 42 | #endif |
| 43 | } |
| 44 | |
Marek Vasut | b7e2ba7 | 2023-09-01 11:50:02 +0200 | [diff] [blame^] | 45 | int usb_gadget_handle_interrupts(int index) |
Jean-Jacques Hiblot | 9dc0d5c | 2018-11-29 10:52:46 +0100 | [diff] [blame] | 46 | { |
Marek Vasut | b7e2ba7 | 2023-09-01 11:50:02 +0200 | [diff] [blame^] | 47 | struct udevice *udc; |
Jean-Jacques Hiblot | 9dc0d5c | 2018-11-29 10:52:46 +0100 | [diff] [blame] | 48 | int ret; |
Jean-Jacques Hiblot | 9dc0d5c | 2018-11-29 10:52:46 +0100 | [diff] [blame] | 49 | |
Marek Vasut | b7e2ba7 | 2023-09-01 11:50:02 +0200 | [diff] [blame^] | 50 | ret = udc_device_get_by_index(index, &udc); |
| 51 | if (ret) |
| 52 | return ret; |
Jean-Jacques Hiblot | 9dc0d5c | 2018-11-29 10:52:46 +0100 | [diff] [blame] | 53 | |
Marek Vasut | b7e2ba7 | 2023-09-01 11:50:02 +0200 | [diff] [blame^] | 54 | return dm_usb_gadget_handle_interrupts(udc); |
Jean-Jacques Hiblot | 9dc0d5c | 2018-11-29 10:52:46 +0100 | [diff] [blame] | 55 | } |
Marek Vasut | b397203 | 2023-09-01 11:49:47 +0200 | [diff] [blame] | 56 | #else |
| 57 | /* Backwards hardware compatibility -- switch to DM_USB_GADGET */ |
| 58 | static int legacy_index; |
| 59 | int udc_device_get_by_index(int index, struct udevice **udev) |
| 60 | { |
| 61 | legacy_index = index; |
| 62 | return board_usb_init(index, USB_INIT_DEVICE); |
| 63 | } |
| 64 | |
| 65 | int udc_device_put(struct udevice *udev) |
| 66 | { |
| 67 | return board_usb_cleanup(legacy_index, USB_INIT_DEVICE); |
| 68 | } |
Jean-Jacques Hiblot | db994e0 | 2018-12-21 09:50:21 +0100 | [diff] [blame] | 69 | #endif |
Jean-Jacques Hiblot | 9dc0d5c | 2018-11-29 10:52:46 +0100 | [diff] [blame] | 70 | |
Marek Vasut | b397203 | 2023-09-01 11:49:47 +0200 | [diff] [blame] | 71 | #if CONFIG_IS_ENABLED(DM) |
Jean-Jacques Hiblot | 9dc0d5c | 2018-11-29 10:52:46 +0100 | [diff] [blame] | 72 | UCLASS_DRIVER(usb_gadget_generic) = { |
| 73 | .id = UCLASS_USB_GADGET_GENERIC, |
Jean-Jacques Hiblot | 2934dc1 | 2018-12-15 17:43:27 +0100 | [diff] [blame] | 74 | .name = "usb", |
| 75 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
Jean-Jacques Hiblot | 9dc0d5c | 2018-11-29 10:52:46 +0100 | [diff] [blame] | 76 | }; |
Marek Vasut | b397203 | 2023-09-01 11:49:47 +0200 | [diff] [blame] | 77 | #endif |