xref: /freebsd/sys/dev/firewire/fwcam.h (revision d59c7ea2701fe7b73b32eef49a7c712ef38de5a0)
1 /*
2  * Copyright (c) 2026 Abdelkader Boudih <freebsd@seuros.com>
3  *
4  * SPDX-License-Identifier: BSD-2-Clause
5  */
6 
7 #ifndef _DEV_FIREWIRE_FWCAM_H_
8 #define _DEV_FIREWIRE_FWCAM_H_
9 
10 /*
11  * IIDC 1394-based Digital Camera Specification v1.30
12  * Register offsets relative to command_regs_base
13  */
14 
15 /* Section 1.1 - Camera initialize register */
16 #define IIDC_INITIALIZE		0x000
17 
18 /* Section 1.2 - Inquiry registers for video format/mode/frame rate */
19 #define IIDC_V_FORMAT_INQ	0x100
20 #define IIDC_V_MODE_INQ(f)	(0x180 + (f) * 4)
21 #define IIDC_V_RATE_INQ(f, m)	(0x200 + (f) * 0x20 + (m) * 4)
22 
23 /* Section 1.3 - Inquiry register for basic function */
24 #define IIDC_BASIC_FUNC_INQ	0x400
25 
26 /* Section 1.4 - Inquiry registers for feature presence */
27 #define IIDC_FEATURE_HI_INQ	0x404
28 #define IIDC_FEATURE_LO_INQ	0x408
29 
30 /* Section 1.5 - Inquiry registers for feature elements (per feature) */
31 #define IIDC_BRIGHTNESS_INQ	0x500
32 #define IIDC_AUTO_EXPOSURE_INQ	0x504
33 #define IIDC_SHARPNESS_INQ	0x508
34 #define IIDC_WHITE_BAL_INQ	0x50C
35 #define IIDC_HUE_INQ		0x510
36 #define IIDC_SATURATION_INQ	0x514
37 #define IIDC_GAMMA_INQ		0x518
38 #define IIDC_SHUTTER_INQ	0x51C
39 #define IIDC_GAIN_INQ		0x520
40 #define IIDC_IRIS_INQ		0x524
41 #define IIDC_FOCUS_INQ		0x528
42 #define IIDC_TEMPERATURE_INQ	0x52C
43 #define IIDC_TRIGGER_INQ	0x530
44 
45 /* Section 1.6 - Status and control registers for camera */
46 #define IIDC_CUR_V_FRM_RATE	0x600
47 #define IIDC_CUR_V_MODE		0x604
48 #define IIDC_CUR_V_FORMAT	0x608
49 #define IIDC_ISO_CHANNEL	0x60C
50 #define IIDC_CAMERA_POWER	0x610
51 #define IIDC_ISO_EN		0x614
52 #define IIDC_MEMORY_SAVE	0x618
53 #define IIDC_ONE_SHOT		0x61C
54 #define IIDC_MEM_SAVE_CH	0x620
55 #define IIDC_CUR_MEM_CH		0x624
56 #define IIDC_VMODE_ERR_STATUS	0x628
57 
58 /* Cur_V_Format / Cur_V_Mode / Cur_V_Frm_Rate register fields */
59 #define IIDC_CUR_V_SHIFT	29		/* bits [0..2] */
60 
61 /* Vmode_Error_Status register */
62 #define IIDC_VMODE_ERROR	(1 << 31)	/* bit [0]: mode error */
63 
64 /* Section 1.7 - Status and control register for features */
65 #define IIDC_BRIGHTNESS		0x800
66 #define IIDC_AUTO_EXPOSURE	0x804
67 #define IIDC_SHARPNESS		0x808
68 #define IIDC_WHITE_BALANCE	0x80C
69 #define IIDC_HUE		0x810
70 #define IIDC_SATURATION		0x814
71 #define IIDC_GAMMA		0x818
72 #define IIDC_SHUTTER		0x81C
73 #define IIDC_GAIN		0x820
74 #define IIDC_IRIS		0x824
75 #define IIDC_FOCUS		0x828
76 #define IIDC_TEMPERATURE	0x82C
77 #define IIDC_TRIGGER_MODE	0x830
78 #define IIDC_ZOOM		0x880
79 #define IIDC_PAN		0x884
80 #define IIDC_TILT		0x888
81 
82 /* Video format indices (CUR_V_FORMAT register) */
83 #define IIDC_FMT_VGA		0	/* Format_0: VGA non-compressed */
84 #define IIDC_FMT_SVGA1		1	/* Format_1: Super VGA (1) */
85 #define IIDC_FMT_SVGA2		2	/* Format_2: Super VGA (2) */
86 #define IIDC_FMT_STILL		6	/* Format_6: Still image */
87 #define IIDC_FMT_PARTIAL	7	/* Format_7: Partial/scalable */
88 
89 /* V_FORMAT_INQ bits */
90 #define IIDC_FORMAT_VGA		(1 << 31)	/* Format_0: VGA */
91 #define IIDC_FORMAT_SVGA1	(1 << 30)	/* Format_1: Super VGA (1) */
92 #define IIDC_FORMAT_SVGA2	(1 << 29)	/* Format_2: Super VGA (2) */
93 #define IIDC_FORMAT_STILL	(1 << 25)	/* Format_6: Still image */
94 #define IIDC_FORMAT_PARTIAL	(1 << 24)	/* Format_7: Partial image */
95 
96 /* ISO_CHANNEL register fields */
97 #define IIDC_ISO_CH_MASK	0xf0000000	/* bits [0..3] */
98 #define IIDC_ISO_CH_SHIFT	28
99 #define IIDC_ISO_SPEED_MASK	0x03000000	/* bits [6..7] */
100 #define IIDC_ISO_SPEED_SHIFT	24
101 
102 /* CAMERA_POWER register */
103 #define IIDC_POWER_ON		(1 << 31)	/* bit [0] */
104 
105 /* ISO_EN register */
106 #define IIDC_ISO_EN_ON		(1 << 31)	/* bit [0] */
107 
108 /* BASIC_FUNC_INQ bits */
109 #define IIDC_ADV_FEATURE_INQ	(1 << 31)	/* bit [0] */
110 #define IIDC_CAM_POWER_CTRL	(1 << 15)	/* bit [16] */
111 #define IIDC_ONE_SHOT_INQ	(1 << 12)	/* bit [19] */
112 #define IIDC_MULTI_SHOT_INQ	(1 << 11)	/* bit [20] */
113 
114 /* FEATURE_HI_INQ bits (feature presence, section 1.4) */
115 #define IIDC_HAS_BRIGHTNESS	(1 << 31)
116 #define IIDC_HAS_AUTO_EXPOSURE	(1 << 30)
117 #define IIDC_HAS_SHARPNESS	(1 << 29)
118 #define IIDC_HAS_WHITE_BALANCE	(1 << 28)
119 #define IIDC_HAS_HUE		(1 << 27)
120 #define IIDC_HAS_SATURATION	(1 << 26)
121 #define IIDC_HAS_GAMMA		(1 << 25)
122 #define IIDC_HAS_SHUTTER	(1 << 24)
123 #define IIDC_HAS_GAIN		(1 << 23)
124 #define IIDC_HAS_IRIS		(1 << 22)
125 #define IIDC_HAS_FOCUS		(1 << 21)
126 #define IIDC_HAS_TEMPERATURE	(1 << 20)
127 #define IIDC_HAS_TRIGGER	(1 << 19)
128 
129 /* Config ROM: IIDC unit-dependent directory command_regs_base key */
130 #define IIDC_CROM_CMD_BASE	(CSRTYPE_C | 0x00)	/* 0x40 */
131 
132 /*
133  * Frame size limits for Format_0 (VGA non-compressed):
134  * Mode_1: 320x240 YUV422 = 153,600 bytes
135  * Mode_2: 640x480 YUV411 = 460,800 bytes
136  * Mode_3: 640x480 YUV422 = 614,400 bytes
137  */
138 struct fwcam_feature {
139 	uint32_t	id;		/* FWCAM_FEAT_* */
140 	uint32_t	flags;		/* FWCAM_FEATF_* (from INQ, read-only) */
141 	uint32_t	min;		/* minimum value (from INQ, read-only) */
142 	uint32_t	max;		/* maximum value (from INQ, read-only) */
143 	uint32_t	value;		/* current value / value to set */
144 	uint32_t	value2;		/* second value (white balance V) */
145 };
146 
147 /* Feature IDs (index into IIDC feature register space) */
148 #define FWCAM_FEAT_BRIGHTNESS	0
149 #define FWCAM_FEAT_AUTO_EXPOSURE 1
150 #define FWCAM_FEAT_SHARPNESS	2
151 #define FWCAM_FEAT_WHITE_BALANCE 3
152 #define FWCAM_FEAT_HUE		4
153 #define FWCAM_FEAT_SATURATION	5
154 #define FWCAM_FEAT_GAMMA	6
155 #define FWCAM_FEAT_SHUTTER	7
156 #define FWCAM_FEAT_GAIN		8
157 #define FWCAM_FEAT_IRIS		9
158 #define FWCAM_FEAT_FOCUS	10
159 #define FWCAM_FEAT_TEMPERATURE	11
160 #define FWCAM_FEAT_TRIGGER	12
161 #define FWCAM_FEAT_ZOOM		13
162 #define FWCAM_FEAT_PAN		14
163 #define FWCAM_FEAT_TILT		15
164 #define FWCAM_FEAT_MAX		16
165 
166 /* Feature flags (from INQ register bits) */
167 #define FWCAM_FEATF_PRESENT	(1 << 0)  /* feature is present */
168 #define FWCAM_FEATF_ONOFF	(1 << 1)  /* supports on/off */
169 #define FWCAM_FEATF_AUTO	(1 << 2)  /* supports auto mode */
170 #define FWCAM_FEATF_MANUAL	(1 << 3)  /* supports manual mode */
171 
172 /* fwcam state values */
173 #define FWCAM_STATE_IDLE	0
174 #define FWCAM_STATE_PROBING	1
175 #define FWCAM_STATE_PROBED	2
176 #define FWCAM_STATE_STREAMING	3
177 #define FWCAM_STATE_DETACHING	4
178 
179 static const char * const fwcam_feat_names[FWCAM_FEAT_MAX] = {
180 	[FWCAM_FEAT_BRIGHTNESS]    = "brightness",
181 	[FWCAM_FEAT_AUTO_EXPOSURE] = "auto_exposure",
182 	[FWCAM_FEAT_SHARPNESS]     = "sharpness",
183 	[FWCAM_FEAT_WHITE_BALANCE] = "white_balance",
184 	[FWCAM_FEAT_HUE]           = "hue",
185 	[FWCAM_FEAT_SATURATION]    = "saturation",
186 	[FWCAM_FEAT_GAMMA]         = "gamma",
187 	[FWCAM_FEAT_SHUTTER]       = "shutter",
188 	[FWCAM_FEAT_GAIN]          = "gain",
189 	[FWCAM_FEAT_IRIS]          = "iris",
190 	[FWCAM_FEAT_FOCUS]         = "focus",
191 	[FWCAM_FEAT_TEMPERATURE]   = "temperature",
192 	[FWCAM_FEAT_TRIGGER]       = "trigger",
193 	[FWCAM_FEAT_ZOOM]          = "zoom",
194 	[FWCAM_FEAT_PAN]           = "pan",
195 	[FWCAM_FEAT_TILT]          = "tilt",
196 };
197 
198 /*
199  * Internal constants
200  */
201 #define FWCAM_MAX_FRAME_SIZE	(640 * 480 * 3)	/* 921,600 (RGB24) */
202 #define FWCAM_ISO_NCHUNK	256	/* receive DMA chunks */
203 #define FWCAM_ISO_PKTSIZE	2048	/* max iso packet size (MCLBYTES) */
204 
205 #ifdef _KERNEL
206 
207 struct video_device;
208 
209 struct fwcam_softc {
210 	struct firewire_dev_comm fd;	/* must be first */
211 	struct mtx		mtx;
212 
213 	/* video(4) framework handle */
214 	struct video_device	*sc_vd;
215 	uint32_t		sc_sequence;
216 
217 	/* Remote camera node */
218 	struct fw_device	*fwdev;
219 
220 	/* IIDC command register addressing */
221 	uint16_t		cmd_hi;		/* always 0xffff */
222 	uint32_t		cmd_lo;		/* 0xf0000000 | (base << 2) */
223 
224 	/* Capabilities from INQ registers */
225 	uint32_t		formats;	/* V_FORMAT_INQ */
226 	uint32_t		basic_func;	/* BASIC_FUNC_INQ */
227 	uint32_t		features_hi;	/* FEATURE_HI_INQ */
228 	uint32_t		features_lo;	/* FEATURE_LO_INQ */
229 	uint32_t		modes[8];	/* V_MODE_INQ per format */
230 	uint32_t		rates[8][8];	/* V_RATE_INQ per format/mode */
231 
232 	/* Current settings */
233 	uint8_t			cur_format;
234 	uint8_t			cur_mode;
235 	uint8_t			cur_framerate;
236 	uint8_t			iso_channel;
237 	uint8_t			iso_speed;
238 
239 	/* Deferred probe task */
240 	struct task		probe_task;
241 
242 	/* Isochronous receive */
243 	int			dma_ch;		/* IR DMA channel, -1 if none */
244 	int			iso_active;	/* iso_input running */
245 
246 	/* Frame assembly */
247 	uint8_t			*frame_buf;	/* frame being assembled */
248 	uint32_t		frame_size;	/* expected frame size (bytes) */
249 	uint32_t		frame_offset;	/* write position in frame_buf */
250 	int			frame_dropped;	/* dropped frame count */
251 
252 	/* State: one of FWCAM_STATE_* */
253 	int			state;
254 };
255 
256 #define FWCAM_LOCK(sc)		mtx_lock(&(sc)->mtx)
257 #define FWCAM_UNLOCK(sc)	mtx_unlock(&(sc)->mtx)
258 
259 #endif /* _KERNEL */
260 
261 #endif /* _DEV_FIREWIRE_FWCAM_H_ */
262