1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2008-2023 Hans Petter Selasky 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #ifndef _USB_DEV_H_ 29 #define _USB_DEV_H_ 30 31 struct usb_fifo; 32 struct usb_mbuf; 33 34 #define UID_ROOT 0 35 #define GID_OPERATOR 5 36 37 struct usb_symlink { 38 TAILQ_ENTRY(usb_symlink) sym_entry; 39 char src_path[32]; /* Source path - including terminating 40 * zero */ 41 char dst_path[32]; /* Destination path - including 42 * terminating zero */ 43 uint8_t src_len; /* String length */ 44 uint8_t dst_len; /* String length */ 45 }; 46 47 /* 48 * Private per-device information. 49 */ 50 struct usb_cdev_privdata { 51 struct usb_bus *bus; 52 struct usb_device *udev; 53 struct usb_interface *iface; 54 int bus_index; /* bus index */ 55 int dev_index; /* device index */ 56 int ep_addr; /* endpoint address */ 57 int fflags; 58 uint8_t fifo_index; /* FIFO index */ 59 }; 60 61 /* 62 * The following structure defines a minimum re-implementation of the 63 * ifqueue structure in the kernel. 64 */ 65 struct usb_ifqueue { 66 struct usb_mbuf *ifq_head; 67 struct usb_mbuf *ifq_tail; 68 69 usb_size_t ifq_len; 70 usb_size_t ifq_maxlen; 71 }; 72 73 /* 74 * Private per-device and per-thread reference information 75 */ 76 struct usb_cdev_refdata { 77 struct usb_fifo *rxfifo; 78 struct usb_fifo *txfifo; 79 uint8_t is_read; /* location has read access */ 80 uint8_t is_write; /* location has write access */ 81 uint8_t is_uref; /* USB refcount decr. needed */ 82 uint8_t is_usbfs; /* USB-FS is active */ 83 uint8_t do_unlock; /* USB enum unlock needed */ 84 }; 85 86 struct usb_fs_privdata { 87 int bus_index; 88 int dev_index; 89 int ep_addr; 90 int mode; 91 int fifo_index; 92 char cdev_name[32]; 93 94 LIST_ENTRY(usb_fs_privdata) pd_next; 95 }; 96 97 /* 98 * Most of the fields in the "usb_fifo" structure are used by the 99 * generic USB access layer. 100 */ 101 struct usb_fifo { 102 struct usb_ifqueue free_q; 103 struct usb_ifqueue used_q; 104 struct cv cv_io; 105 struct cv cv_drain; 106 struct usb_fifo_methods *methods; 107 struct usb_symlink *symlink[2];/* our symlinks */ 108 struct proc *async_p; /* process that wants SIGIO */ 109 struct usb_fs_endpoint *fs_ep_ptr; 110 struct usb_device *udev; 111 struct usb_xfer *xfer[2]; 112 struct usb_xfer **fs_xfer; 113 struct mtx *priv_mtx; /* client data */ 114 /* set if FIFO is opened by a FILE: */ 115 struct usb_cdev_privdata *curr_cpd; 116 void *priv_sc0; /* client data */ 117 void *priv_sc1; /* client data */ 118 void *queue_data; 119 usb_timeout_t timeout; /* timeout in milliseconds */ 120 usb_frlength_t bufsize; /* BULK and INTERRUPT buffer size */ 121 usb_frcount_t nframes; /* for isochronous mode */ 122 uint16_t dev_ep_index; /* our device endpoint index */ 123 uint8_t flag_sleeping; /* set if FIFO is sleeping */ 124 uint8_t flag_iscomplete; /* set if a USB transfer is complete */ 125 uint8_t flag_iserror; /* set if FIFO error happened */ 126 uint8_t flag_isselect; /* set if FIFO is selected */ 127 uint8_t flag_flushing; /* set if FIFO is flushing data */ 128 uint8_t flag_short; /* set if short_ok or force_short 129 * transfer flags should be set */ 130 uint8_t flag_stall; /* set if clear stall should be run */ 131 uint8_t flag_write_defrag; /* set to defrag written data */ 132 uint8_t flag_have_fragment; /* set if defragging */ 133 uint8_t iface_index; /* set to the interface we belong to */ 134 uint8_t fifo_index; /* set to the FIFO index in "struct 135 * usb_device" */ 136 uint8_t fs_ep_max; 137 uint8_t fifo_zlp; /* zero length packet count */ 138 uint8_t refcount; 139 #define USB_FIFO_REF_MAX 0xFF 140 }; 141 142 extern struct file_operations_vfs usb_devsw; 143 144 int usb_fifo_wait(struct usb_fifo *fifo); 145 void usb_fifo_signal(struct usb_fifo *fifo); 146 uint8_t usb_fifo_opened(struct usb_fifo *fifo); 147 struct usb_symlink *usb_alloc_symlink(const char *target); 148 void usb_free_symlink(struct usb_symlink *ps); 149 int usb_read_symlink(uint8_t *user_ptr, uint32_t startentry, 150 uint32_t user_len); 151 void usb_dev_init(void *arg); 152 void usb_dev_uninit(void *arg); 153 154 #endif /* _USB_DEV_H_ */ 155