1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * uvc_gadget.h -- USB Video Class Gadget driver 4 * 5 * Copyright (C) 2009-2010 6 * Laurent Pinchart (laurent.pinchart@ideasonboard.com) 7 */ 8 9 #ifndef _UVC_GADGET_H_ 10 #define _UVC_GADGET_H_ 11 12 #include <linux/ioctl.h> 13 #include <linux/types.h> 14 #include <linux/usb/ch9.h> 15 16 #define UVC_EVENT_FIRST (V4L2_EVENT_PRIVATE_START + 0) 17 #define UVC_EVENT_CONNECT (V4L2_EVENT_PRIVATE_START + 0) 18 #define UVC_EVENT_DISCONNECT (V4L2_EVENT_PRIVATE_START + 1) 19 #define UVC_EVENT_STREAMON (V4L2_EVENT_PRIVATE_START + 2) 20 #define UVC_EVENT_STREAMOFF (V4L2_EVENT_PRIVATE_START + 3) 21 #define UVC_EVENT_SETUP (V4L2_EVENT_PRIVATE_START + 4) 22 #define UVC_EVENT_DATA (V4L2_EVENT_PRIVATE_START + 5) 23 #define UVC_EVENT_LAST (V4L2_EVENT_PRIVATE_START + 5) 24 25 struct uvc_request_data { 26 __s32 length; 27 __u8 data[60]; 28 }; 29 30 struct uvc_event { 31 union { 32 enum usb_device_speed speed; 33 struct usb_ctrlrequest req; 34 struct uvc_request_data data; 35 }; 36 }; 37 38 #define UVCIOC_SEND_RESPONSE _IOW('U', 1, struct uvc_request_data) 39 40 #define UVC_INTF_CONTROL 0 41 #define UVC_INTF_STREAMING 1 42 43 /* ------------------------------------------------------------------------ 44 * Debugging, printing and logging 45 */ 46 47 #ifdef __KERNEL__ 48 49 #include <linux/usb.h> /* For usb_endpoint_* */ 50 #include <linux/usb/composite.h> 51 #include <linux/usb/gadget.h> 52 #include <linux/videodev2.h> 53 #include <media/v4l2-fh.h> 54 #include <media/v4l2-device.h> 55 56 #include "uvc_queue.h" 57 58 #define UVC_TRACE_PROBE (1 << 0) 59 #define UVC_TRACE_DESCR (1 << 1) 60 #define UVC_TRACE_CONTROL (1 << 2) 61 #define UVC_TRACE_FORMAT (1 << 3) 62 #define UVC_TRACE_CAPTURE (1 << 4) 63 #define UVC_TRACE_CALLS (1 << 5) 64 #define UVC_TRACE_IOCTL (1 << 6) 65 #define UVC_TRACE_FRAME (1 << 7) 66 #define UVC_TRACE_SUSPEND (1 << 8) 67 #define UVC_TRACE_STATUS (1 << 9) 68 69 #define UVC_WARN_MINMAX 0 70 #define UVC_WARN_PROBE_DEF 1 71 72 extern unsigned int uvc_gadget_trace_param; 73 74 #define uvc_trace(flag, msg...) \ 75 do { \ 76 if (uvc_gadget_trace_param & flag) \ 77 printk(KERN_DEBUG "uvcvideo: " msg); \ 78 } while (0) 79 80 #define uvc_warn_once(dev, warn, msg...) \ 81 do { \ 82 if (!test_and_set_bit(warn, &dev->warnings)) \ 83 printk(KERN_INFO "uvcvideo: " msg); \ 84 } while (0) 85 86 #define uvc_printk(level, msg...) \ 87 printk(level "uvcvideo: " msg) 88 89 /* ------------------------------------------------------------------------ 90 * Driver specific constants 91 */ 92 93 #define UVC_NUM_REQUESTS 4 94 #define UVC_MAX_REQUEST_SIZE 64 95 #define UVC_MAX_EVENTS 4 96 97 /* ------------------------------------------------------------------------ 98 * Structures 99 */ 100 101 struct uvc_video { 102 struct usb_ep *ep; 103 104 /* Frame parameters */ 105 u8 bpp; 106 u32 fcc; 107 unsigned int width; 108 unsigned int height; 109 unsigned int imagesize; 110 struct mutex mutex; /* protects frame parameters */ 111 112 /* Requests */ 113 unsigned int req_size; 114 struct usb_request *req[UVC_NUM_REQUESTS]; 115 __u8 *req_buffer[UVC_NUM_REQUESTS]; 116 struct list_head req_free; 117 spinlock_t req_lock; 118 119 void (*encode) (struct usb_request *req, struct uvc_video *video, 120 struct uvc_buffer *buf); 121 122 /* Context data used by the completion handler */ 123 __u32 payload_size; 124 __u32 max_payload_size; 125 126 struct uvc_video_queue queue; 127 unsigned int fid; 128 }; 129 130 enum uvc_state { 131 UVC_STATE_DISCONNECTED, 132 UVC_STATE_CONNECTED, 133 UVC_STATE_STREAMING, 134 }; 135 136 struct uvc_device { 137 struct video_device vdev; 138 struct v4l2_device v4l2_dev; 139 enum uvc_state state; 140 struct usb_function func; 141 struct uvc_video video; 142 143 /* Descriptors */ 144 struct { 145 const struct uvc_descriptor_header * const *fs_control; 146 const struct uvc_descriptor_header * const *ss_control; 147 const struct uvc_descriptor_header * const *fs_streaming; 148 const struct uvc_descriptor_header * const *hs_streaming; 149 const struct uvc_descriptor_header * const *ss_streaming; 150 } desc; 151 152 unsigned int control_intf; 153 struct usb_ep *control_ep; 154 struct usb_request *control_req; 155 void *control_buf; 156 157 unsigned int streaming_intf; 158 159 /* Events */ 160 unsigned int event_length; 161 unsigned int event_setup_out : 1; 162 }; 163 164 static inline struct uvc_device *to_uvc(struct usb_function *f) 165 { 166 return container_of(f, struct uvc_device, func); 167 } 168 169 struct uvc_file_handle { 170 struct v4l2_fh vfh; 171 struct uvc_video *device; 172 }; 173 174 #define to_uvc_file_handle(handle) \ 175 container_of(handle, struct uvc_file_handle, vfh) 176 177 /* ------------------------------------------------------------------------ 178 * Functions 179 */ 180 181 extern void uvc_function_setup_continue(struct uvc_device *uvc); 182 extern void uvc_endpoint_stream(struct uvc_device *dev); 183 184 extern void uvc_function_connect(struct uvc_device *uvc); 185 extern void uvc_function_disconnect(struct uvc_device *uvc); 186 187 #endif /* __KERNEL__ */ 188 189 #endif /* _UVC_GADGET_H_ */ 190 191