1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Driver for the Auvitek AU0828 USB bridge
5 * Copyright (c) 2008 Steven Toth <stoth@linuxtv.org>
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
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>
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>
34 #include "au0828-reg.h"
35 #include "au0828-cards.h"
38 #define URB_BUFSIZE (0xe522)
40 /* Analog constants */
41 #define NTSC_STD_W 720
42 #define NTSC_STD_H 480
44 #define AU0828_INTERLACED_DEFAULT 1
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
50 #define AU0828_MIN_BUF 4
51 #define AU0828_DEF_BUF 8
53 #define AU0828_MAX_INPUT 4
55 /* au0828 resource types (used for res_get/res_lock etc */
56 #define AU0828_RESOURCE_VIDEO 0x01
57 #define AU0828_RESOURCE_VBI 0x02
60 AU0828_VMUX_UNDEFINED
= 0,
61 AU0828_VMUX_COMPOSITE
,
64 AU0828_VMUX_TELEVISION
,
69 enum au0828_itype type
;
72 void (*audio_setup
) (void *priv
, int enable
);
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
];
87 struct dvb_adapter adapter
;
88 struct dvb_frontend
*frontend
;
89 struct dvb_demux demux
;
91 struct dmx_frontend fe_hw
;
92 struct dmx_frontend fe_mem
;
98 int (*set_frontend
)(struct dvb_frontend
*fe
);
101 enum au0828_stream_state
{
107 #define AUVI_INPUT(nr) (dev->board.input[nr])
110 enum au0828_dev_state
{
112 DEV_DISCONNECTED
= 1,
113 DEV_MISCONFIGURED
= 2
118 struct au0828_usb_isoc_ctl
{
119 /* max packet size of isoc transaction */
122 /* number of allocated urbs */
125 /* urb for isoc transfers */
128 /* transfer buffers for isoc transfer */
129 char **transfer_buffer
;
131 /* Last buffer command and region */
133 int pos
, size
, pktsize
;
135 /* Last field: ODD or EVEN? */
138 /* Stores incomplete commands */
142 /* Stores already requested buffers */
143 struct au0828_buffer
*buf
;
144 struct au0828_buffer
*vbi_buf
;
146 /* Stores the number of received fields */
149 /* isoc urb callback */
150 int (*isoc_copy
) (struct au0828_dev
*dev
, struct urb
*urb
);
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
;
161 unsigned long length
;
163 /* pointer to vmalloc memory address in vb */
167 struct au0828_dmaqueue
{
168 struct list_head active
;
169 /* Counters to control buffer fill */
175 struct usb_device
*usbdev
;
177 struct au0828_board board
;
181 struct i2c_adapter i2c_adap
;
182 struct i2c_algorithm i2c_algo
;
183 struct i2c_client i2c_client
;
187 struct au0828_dvb dvb
;
188 struct work_struct restart_streaming
;
189 struct timer_list bulk_timeout
;
190 int bulk_timeout_running
;
192 #ifdef CONFIG_VIDEO_AU0828_V4L2
194 struct v4l2_device v4l2_dev
;
195 struct v4l2_ctrl_handler v4l2_ctrl_hdl
;
197 #ifdef CONFIG_VIDEO_AU0828_RC
198 struct au0828_rc
*ir
;
201 struct video_device vdev
;
202 struct video_device vbi_dev
;
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
;
210 unsigned int frame_count
;
211 unsigned int vbi_frame_count
;
213 struct timer_list vid_timeout
;
214 int vid_timeout_running
;
215 struct timer_list vbi_timeout
;
216 int vbi_timeout_running
;
232 __u8 isoc_in_endpointaddr
;
234 int greenscreen_detected
;
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
;
245 /* Isoc control struct */
246 struct au0828_dmaqueue vidq
;
247 struct au0828_dmaqueue vbiq
;
248 struct au0828_usb_isoc_ctl isoc_ctl
;
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
260 /* DVB USB / URB Related */
261 bool urb_streaming
, need_urb_start
;
262 struct urb
*urbs
[URB_COUNT
];
264 /* Preallocated transfer digital transfer buffers */
266 char *dig_transfer_buffer
[URB_COUNT
];
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
;
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)))
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)
297 /* ----------------------------------------------------------- */
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
;
304 /* ----------------------------------------------------------- */
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
);
313 /* ----------------------------------------------------------- */
315 extern int au0828_i2c_register(struct au0828_dev
*dev
);
316 extern int au0828_i2c_unregister(struct au0828_dev
*dev
);
318 /* ----------------------------------------------------------- */
320 extern int au0828_start_analog_streaming(struct vb2_queue
*vq
,
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
);
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
);
334 static inline int au0828_v4l2_device_register(struct usb_interface
*interface
,
335 struct au0828_dev
*dev
)
337 static inline int au0828_analog_register(struct au0828_dev
*dev
,
338 struct usb_interface
*interface
)
340 static inline int au0828_analog_unregister(struct au0828_dev
*dev
)
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
) { };
347 /* ----------------------------------------------------------- */
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
);
355 extern const struct vb2_ops au0828_vbi_qops
;
357 #define dprintk(level, fmt, arg...)\
358 do { if (au0828_debug & level)\
359 printk(KERN_DEBUG pr_fmt(fmt), ## arg);\
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
);
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; }