1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 #ifndef HWS_PCIE_H 3 #define HWS_PCIE_H 4 5 #include <linux/types.h> 6 #include <linux/compiler.h> 7 #include <linux/dma-mapping.h> 8 #include <linux/kthread.h> 9 #include <linux/pci.h> 10 #include <linux/list.h> 11 #include <linux/spinlock.h> 12 #include <linux/sizes.h> 13 #include <linux/atomic.h> 14 15 #include <media/v4l2-ctrls.h> 16 #include <media/v4l2-device.h> 17 #include <media/v4l2-dv-timings.h> 18 #include <media/videobuf2-dma-sg.h> 19 20 #include "hws_reg.h" 21 22 struct hwsmem_param { 23 u32 index; 24 u32 type; 25 u32 status; 26 }; 27 28 struct hws_pix_state { 29 u32 width; 30 u32 height; 31 u32 fourcc; /* V4L2_PIX_FMT_* (YUYV only here) */ 32 u32 bytesperline; /* stride */ 33 u32 sizeimage; /* full frame */ 34 enum v4l2_field field; /* V4L2_FIELD_NONE or INTERLACED */ 35 enum v4l2_colorspace colorspace; /* e.g., REC709 */ 36 enum v4l2_ycbcr_encoding ycbcr_enc; /* V4L2_YCBCR_ENC_DEFAULT */ 37 enum v4l2_quantization quantization; /* V4L2_QUANTIZATION_LIM_RANGE */ 38 enum v4l2_xfer_func xfer_func; /* V4L2_XFER_FUNC_DEFAULT */ 39 bool interlaced; /* cached hardware state */ 40 u32 half_size; /* hardware half-frame size */ 41 }; 42 43 #define UNSET (-1U) 44 45 struct hws_pcie_dev; 46 struct hws_adapter; 47 struct hws_video; 48 49 struct hwsvideo_buffer { 50 struct vb2_v4l2_buffer vb; 51 struct list_head list; 52 int slot; 53 }; 54 55 struct hws_video { 56 /* Linkage */ 57 struct hws_pcie_dev *parent; 58 struct video_device *video_device; 59 60 struct vb2_queue buffer_queue; 61 struct list_head capture_queue; 62 struct hwsvideo_buffer *active; 63 struct hwsvideo_buffer *next_prepared; 64 65 /* Locking */ 66 struct mutex state_lock; 67 spinlock_t irq_lock; /* Protects capture_queue and active buffers. */ 68 69 /* Indices */ 70 int channel_index; 71 72 /* Color controls */ 73 int current_brightness; 74 int current_contrast; 75 int current_saturation; 76 int current_hue; 77 78 /* V4L2 controls */ 79 struct v4l2_ctrl_handler control_handler; 80 struct v4l2_ctrl *ctrl_brightness; 81 struct v4l2_ctrl *ctrl_contrast; 82 struct v4l2_ctrl *ctrl_saturation; 83 struct v4l2_ctrl *ctrl_hue; 84 85 /* Capture queue status */ 86 struct hws_pix_state pix; 87 struct v4l2_dv_timings cur_dv_timings; /* last configured/notified DV timings */ 88 u32 current_fps; /* Hz, updated by mode changes, not by read-only queries */ 89 90 /* Per-channel capture state */ 91 bool cap_active; 92 bool stop_requested; 93 u8 last_buf_half_toggle; 94 bool half_seen; 95 atomic_t sequence_number; 96 u32 queued_count; 97 98 /* Timeout and error handling */ 99 u32 timeout_count; 100 u32 error_count; 101 102 bool window_valid; 103 u32 last_dma_hi; 104 u32 last_dma_page; 105 u32 last_pci_addr; 106 u32 last_half16; 107 108 /* Misc counters */ 109 int signal_loss_cnt; 110 }; 111 112 static inline void hws_set_current_dv_timings(struct hws_video *vid, 113 u32 width, u32 height, 114 bool interlaced) 115 { 116 if (!vid) 117 return; 118 119 vid->cur_dv_timings = (struct v4l2_dv_timings) { 120 .type = V4L2_DV_BT_656_1120, 121 .bt = { 122 .width = width, 123 .height = height, 124 .interlaced = interlaced, 125 }, 126 }; 127 } 128 129 struct hws_scratch_dma { 130 void *cpu; 131 dma_addr_t dma; 132 size_t size; 133 }; 134 135 struct hws_pcie_dev { 136 /* Core objects */ 137 struct pci_dev *pdev; 138 struct hws_video video[MAX_VID_CHANNELS]; 139 140 /* BAR and workqueues */ 141 void __iomem *bar0_base; 142 143 /* Device identity and capabilities */ 144 u16 vendor_id; 145 u16 device_id; 146 u16 device_ver; 147 u16 hw_ver; 148 u32 sub_ver; 149 u32 port_id; 150 /* Tri-state support flag used by set_video_format_size(). */ 151 u32 support_yv12; 152 u32 max_hw_video_buf_sz; 153 u8 max_channels; 154 u8 cur_max_video_ch; 155 bool start_run; 156 157 bool buf_allocated; 158 159 /* V4L2 framework objects */ 160 struct v4l2_device v4l2_device; 161 162 /* Kernel thread */ 163 struct task_struct *main_task; 164 struct hws_scratch_dma scratch_vid[MAX_VID_CHANNELS]; 165 166 bool suspended; 167 int irq; 168 169 /* Error flags */ 170 int pci_lost; 171 }; 172 173 #endif 174