xref: /openbmc/linux/drivers/media/usb/au0828/au0828.h (revision 234489ac)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  *  Driver for the Auvitek AU0828 USB bridge
4  *
5  *  Copyright (c) 2008 Steven Toth <stoth@linuxtv.org>
6  */
7 
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9 
10 #include <linux/bitops.h>
11 #include <linux/usb.h>
12 #include <linux/i2c.h>
13 #include <linux/i2c-algo-bit.h>
14 #include <media/tveeprom.h>
15 
16 /* Analog */
17 #include <linux/videodev2.h>
18 #include <media/videobuf2-v4l2.h>
19 #include <media/videobuf2-vmalloc.h>
20 #include <media/v4l2-device.h>
21 #include <media/v4l2-ctrls.h>
22 #include <media/v4l2-fh.h>
23 #include <media/media-device.h>
24 #include <media/media-dev-allocator.h>
25 
26 /* DVB */
27 #include <media/demux.h>
28 #include <media/dmxdev.h>
29 #include <media/dvb_demux.h>
30 #include <media/dvb_frontend.h>
31 #include <media/dvb_net.h>
32 #include <media/dvbdev.h>
33 
34 #include "au0828-reg.h"
35 #include "au0828-cards.h"
36 
37 #define URB_COUNT   16
38 #define URB_BUFSIZE (0xe522)
39 
40 /* Analog constants */
41 #define NTSC_STD_W      720
42 #define NTSC_STD_H      480
43 
44 #define AU0828_INTERLACED_DEFAULT       1
45 
46 /* Definition for AU0828 USB transfer */
47 #define AU0828_MAX_ISO_BUFS    12  /* maybe resize this value in the future */
48 #define AU0828_ISO_PACKETS_PER_URB      128
49 
50 #define AU0828_MIN_BUF 4
51 #define AU0828_DEF_BUF 8
52 
53 #define AU0828_MAX_INPUT        4
54 
55 /* au0828 resource types (used for res_get/res_lock etc */
56 #define AU0828_RESOURCE_VIDEO 0x01
57 #define AU0828_RESOURCE_VBI   0x02
58 
59 enum au0828_itype {
60 	AU0828_VMUX_UNDEFINED = 0,
61 	AU0828_VMUX_COMPOSITE,
62 	AU0828_VMUX_SVIDEO,
63 	AU0828_VMUX_CABLE,
64 	AU0828_VMUX_TELEVISION,
65 	AU0828_VMUX_DVB,
66 };
67 
68 struct au0828_input {
69 	enum au0828_itype type;
70 	unsigned int vmux;
71 	unsigned int amux;
72 	void (*audio_setup) (void *priv, int enable);
73 };
74 
75 struct au0828_board {
76 	char *name;
77 	unsigned int tuner_type;
78 	unsigned char tuner_addr;
79 	unsigned char i2c_clk_divider;
80 	unsigned char has_ir_i2c:1;
81 	unsigned char has_analog:1;
82 	struct au0828_input input[AU0828_MAX_INPUT];
83 };
84 
85 struct au0828_dvb {
86 	struct mutex lock;
87 	struct dvb_adapter adapter;
88 	struct dvb_frontend *frontend;
89 	struct dvb_demux demux;
90 	struct dmxdev dmxdev;
91 	struct dmx_frontend fe_hw;
92 	struct dmx_frontend fe_mem;
93 	struct dvb_net net;
94 	int feeding;
95 	int start_count;
96 	int stop_count;
97 
98 	int (*set_frontend)(struct dvb_frontend *fe);
99 };
100 
101 enum au0828_stream_state {
102 	STREAM_OFF,
103 	STREAM_INTERRUPT,
104 	STREAM_ON
105 };
106 
107 #define AUVI_INPUT(nr) (dev->board.input[nr])
108 
109 /* device state */
110 enum au0828_dev_state {
111 	DEV_INITIALIZED = 0,
112 	DEV_DISCONNECTED = 1,
113 	DEV_MISCONFIGURED = 2
114 };
115 
116 struct au0828_dev;
117 
118 struct au0828_usb_isoc_ctl {
119 		/* max packet size of isoc transaction */
120 	int				max_pkt_size;
121 
122 		/* number of allocated urbs */
123 	int				num_bufs;
124 
125 		/* urb for isoc transfers */
126 	struct urb			**urb;
127 
128 		/* transfer buffers for isoc transfer */
129 	char				**transfer_buffer;
130 
131 		/* Last buffer command and region */
132 	u8				cmd;
133 	int				pos, size, pktsize;
134 
135 		/* Last field: ODD or EVEN? */
136 	int				field;
137 
138 		/* Stores incomplete commands */
139 	u32				tmp_buf;
140 	int				tmp_buf_len;
141 
142 		/* Stores already requested buffers */
143 	struct au0828_buffer		*buf;
144 	struct au0828_buffer		*vbi_buf;
145 
146 		/* Stores the number of received fields */
147 	int				nfields;
148 
149 		/* isoc urb callback */
150 	int (*isoc_copy) (struct au0828_dev *dev, struct urb *urb);
151 
152 };
153 
154 /* buffer for one video frame */
155 struct au0828_buffer {
156 	/* common v4l buffer stuff -- must be first */
157 	struct vb2_v4l2_buffer vb;
158 	struct list_head list;
159 
160 	void *mem;
161 	unsigned long length;
162 	int top_field;
163 	/* pointer to vmalloc memory address in vb */
164 	char *vb_buf;
165 };
166 
167 struct au0828_dmaqueue {
168 	struct list_head       active;
169 	/* Counters to control buffer fill */
170 	int                    pos;
171 };
172 
173 struct au0828_dev {
174 	struct mutex mutex;
175 	struct usb_device	*usbdev;
176 	int			boardnr;
177 	struct au0828_board	board;
178 	u8			ctrlmsg[64];
179 
180 	/* I2C */
181 	struct i2c_adapter		i2c_adap;
182 	struct i2c_algorithm		i2c_algo;
183 	struct i2c_client		i2c_client;
184 	u32				i2c_rc;
185 
186 	/* Digital */
187 	struct au0828_dvb		dvb;
188 	struct work_struct              restart_streaming;
189 	struct timer_list               bulk_timeout;
190 	int                             bulk_timeout_running;
191 
192 #ifdef CONFIG_VIDEO_AU0828_V4L2
193 	/* Analog */
194 	struct v4l2_device v4l2_dev;
195 	struct v4l2_ctrl_handler v4l2_ctrl_hdl;
196 #endif
197 #ifdef CONFIG_VIDEO_AU0828_RC
198 	struct au0828_rc *ir;
199 #endif
200 
201 	struct video_device vdev;
202 	struct video_device vbi_dev;
203 
204 	/* Videobuf2 */
205 	struct vb2_queue vb_vidq;
206 	struct vb2_queue vb_vbiq;
207 	struct mutex vb_queue_lock;
208 	struct mutex vb_vbi_queue_lock;
209 
210 	unsigned int frame_count;
211 	unsigned int vbi_frame_count;
212 
213 	struct timer_list vid_timeout;
214 	int vid_timeout_running;
215 	struct timer_list vbi_timeout;
216 	int vbi_timeout_running;
217 
218 	int users;
219 	int streaming_users;
220 
221 	int width;
222 	int height;
223 	int vbi_width;
224 	int vbi_height;
225 	u32 vbi_read;
226 	v4l2_std_id std;
227 	u32 field_size;
228 	u32 frame_size;
229 	u32 bytesperline;
230 	int type;
231 	u8 ctrl_ainput;
232 	__u8 isoc_in_endpointaddr;
233 	u8 isoc_init_ok;
234 	int greenscreen_detected;
235 	int ctrl_freq;
236 	int input_type;
237 	int std_set_in_tuner_core;
238 	unsigned int ctrl_input;
239 	long unsigned int dev_state; /* defined at enum au0828_dev_state */;
240 	enum au0828_stream_state stream_state;
241 	wait_queue_head_t open;
242 
243 	struct mutex lock;
244 
245 	/* Isoc control struct */
246 	struct au0828_dmaqueue vidq;
247 	struct au0828_dmaqueue vbiq;
248 	struct au0828_usb_isoc_ctl isoc_ctl;
249 	spinlock_t slock;
250 
251 	/* usb transfer */
252 	int alt;		/* alternate */
253 	int max_pkt_size;	/* max packet size of isoc transaction */
254 	int num_alt;		/* Number of alternative settings */
255 	unsigned int *alt_max_pkt_size;	/* array of wMaxPacketSize */
256 	struct urb *urb[AU0828_MAX_ISO_BUFS];	/* urb for isoc transfers */
257 	char *transfer_buffer[AU0828_MAX_ISO_BUFS];/* transfer buffers for isoc
258 						   transfer */
259 
260 	/* DVB USB / URB Related */
261 	bool		urb_streaming, need_urb_start;
262 	struct urb	*urbs[URB_COUNT];
263 
264 	/* Preallocated transfer digital transfer buffers */
265 
266 	char *dig_transfer_buffer[URB_COUNT];
267 
268 #ifdef CONFIG_MEDIA_CONTROLLER
269 	struct media_device *media_dev;
270 	struct media_pad video_pad, vbi_pad;
271 	struct media_entity *decoder;
272 	struct media_entity input_ent[AU0828_MAX_INPUT];
273 	struct media_pad input_pad[AU0828_MAX_INPUT];
274 	struct media_entity_notify entity_notify;
275 	struct media_entity *tuner;
276 	struct media_link *active_link;
277 	struct media_entity *active_source;
278 	struct media_entity *active_sink;
279 	struct media_entity *active_link_owner;
280 	struct media_entity *active_link_user;
281 	struct media_pipeline *active_link_user_pipe;
282 	bool active_link_shared;
283 #endif
284 };
285 
286 
287 /* ----------------------------------------------------------- */
288 #define au0828_read(dev, reg) au0828_readreg(dev, reg)
289 #define au0828_write(dev, reg, value) au0828_writereg(dev, reg, value)
290 #define au0828_andor(dev, reg, mask, value)				\
291 	 au0828_writereg(dev, reg,					\
292 	(au0828_readreg(dev, reg) & ~(mask)) | ((value) & (mask)))
293 
294 #define au0828_set(dev, reg, bit) au0828_andor(dev, (reg), (bit), (bit))
295 #define au0828_clear(dev, reg, bit) au0828_andor(dev, (reg), (bit), 0)
296 
297 /* ----------------------------------------------------------- */
298 /* au0828-core.c */
299 extern u32 au0828_read(struct au0828_dev *dev, u16 reg);
300 extern u32 au0828_write(struct au0828_dev *dev, u16 reg, u32 val);
301 extern void au0828_usb_release(struct au0828_dev *dev);
302 extern int au0828_debug;
303 
304 /* ----------------------------------------------------------- */
305 /* au0828-cards.c */
306 extern struct au0828_board au0828_boards[];
307 extern struct usb_device_id au0828_usb_id_table[];
308 extern void au0828_gpio_setup(struct au0828_dev *dev);
309 extern int au0828_tuner_callback(void *priv, int component,
310 				 int command, int arg);
311 extern void au0828_card_setup(struct au0828_dev *dev);
312 
313 /* ----------------------------------------------------------- */
314 /* au0828-i2c.c */
315 extern int au0828_i2c_register(struct au0828_dev *dev);
316 extern int au0828_i2c_unregister(struct au0828_dev *dev);
317 
318 /* ----------------------------------------------------------- */
319 /* au0828-video.c */
320 extern int au0828_start_analog_streaming(struct vb2_queue *vq,
321 						unsigned int count);
322 extern void au0828_stop_vbi_streaming(struct vb2_queue *vq);
323 #ifdef CONFIG_VIDEO_AU0828_V4L2
324 extern int au0828_v4l2_device_register(struct usb_interface *interface,
325 				      struct au0828_dev *dev);
326 
327 extern int au0828_analog_register(struct au0828_dev *dev,
328 			   struct usb_interface *interface);
329 extern int au0828_analog_unregister(struct au0828_dev *dev);
330 extern void au0828_usb_v4l2_media_release(struct au0828_dev *dev);
331 extern void au0828_v4l2_suspend(struct au0828_dev *dev);
332 extern void au0828_v4l2_resume(struct au0828_dev *dev);
333 #else
334 static inline int au0828_v4l2_device_register(struct usb_interface *interface,
335 					      struct au0828_dev *dev)
336 { return 0; };
337 static inline int au0828_analog_register(struct au0828_dev *dev,
338 				     struct usb_interface *interface)
339 { return 0; };
340 static inline int au0828_analog_unregister(struct au0828_dev *dev)
341 { return 0; };
342 static inline void au0828_usb_v4l2_media_release(struct au0828_dev *dev) { };
343 static inline void au0828_v4l2_suspend(struct au0828_dev *dev) { };
344 static inline void au0828_v4l2_resume(struct au0828_dev *dev) { };
345 #endif
346 
347 /* ----------------------------------------------------------- */
348 /* au0828-dvb.c */
349 extern int au0828_dvb_register(struct au0828_dev *dev);
350 extern void au0828_dvb_unregister(struct au0828_dev *dev);
351 void au0828_dvb_suspend(struct au0828_dev *dev);
352 void au0828_dvb_resume(struct au0828_dev *dev);
353 
354 /* au0828-vbi.c */
355 extern const struct vb2_ops au0828_vbi_qops;
356 
357 #define dprintk(level, fmt, arg...)\
358 	do { if (au0828_debug & level)\
359 		printk(KERN_DEBUG pr_fmt(fmt), ## arg);\
360 	} while (0)
361 
362 /* au0828-input.c */
363 #ifdef CONFIG_VIDEO_AU0828_RC
364 extern int au0828_rc_register(struct au0828_dev *dev);
365 extern void au0828_rc_unregister(struct au0828_dev *dev);
366 extern int au0828_rc_suspend(struct au0828_dev *dev);
367 extern int au0828_rc_resume(struct au0828_dev *dev);
368 #else
369 static inline int au0828_rc_register(struct au0828_dev *dev) { return 0; }
370 static inline void au0828_rc_unregister(struct au0828_dev *dev) { }
371 static inline int au0828_rc_suspend(struct au0828_dev *dev) { return 0; }
372 static inline int au0828_rc_resume(struct au0828_dev *dev) { return 0; }
373 #endif
374