2 * Driver for the Auvitek AU0828 USB bridge
4 * Copyright (c) 2008 Steven Toth <stoth@linuxtv.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20 #include <linux/bitops.h>
21 #include <linux/usb.h>
22 #include <linux/i2c.h>
23 #include <linux/i2c-algo-bit.h>
24 #include <media/tveeprom.h>
27 #include <linux/videodev2.h>
28 #include <media/videobuf2-v4l2.h>
29 #include <media/videobuf2-vmalloc.h>
30 #include <media/v4l2-device.h>
31 #include <media/v4l2-ctrls.h>
32 #include <media/v4l2-fh.h>
33 #include <media/media-device.h>
34 #include <media/media-dev-allocator.h>
37 #include <media/demux.h>
38 #include <media/dmxdev.h>
39 #include <media/dvb_demux.h>
40 #include <media/dvb_frontend.h>
41 #include <media/dvb_net.h>
42 #include <media/dvbdev.h>
44 #include "au0828-reg.h"
45 #include "au0828-cards.h"
48 #define URB_BUFSIZE (0xe522)
50 /* Analog constants */
51 #define NTSC_STD_W 720
52 #define NTSC_STD_H 480
54 #define AU0828_INTERLACED_DEFAULT 1
56 /* Definition for AU0828 USB transfer */
57 #define AU0828_MAX_ISO_BUFS 12 /* maybe resize this value in the future */
58 #define AU0828_ISO_PACKETS_PER_URB 128
60 #define AU0828_MIN_BUF 4
61 #define AU0828_DEF_BUF 8
63 #define AU0828_MAX_INPUT 4
65 /* au0828 resource types (used for res_get/res_lock etc */
66 #define AU0828_RESOURCE_VIDEO 0x01
67 #define AU0828_RESOURCE_VBI 0x02
70 AU0828_VMUX_UNDEFINED = 0,
71 AU0828_VMUX_COMPOSITE,
74 AU0828_VMUX_TELEVISION,
79 enum au0828_itype type;
82 void (*audio_setup) (void *priv, int enable);
87 unsigned int tuner_type;
88 unsigned char tuner_addr;
89 unsigned char i2c_clk_divider;
90 unsigned char has_ir_i2c:1;
91 unsigned char has_analog:1;
92 struct au0828_input input[AU0828_MAX_INPUT];
97 struct dvb_adapter adapter;
98 struct dvb_frontend *frontend;
99 struct dvb_demux demux;
100 struct dmxdev dmxdev;
101 struct dmx_frontend fe_hw;
102 struct dmx_frontend fe_mem;
108 int (*set_frontend)(struct dvb_frontend *fe);
111 enum au0828_stream_state {
117 #define AUVI_INPUT(nr) (dev->board.input[nr])
120 enum au0828_dev_state {
122 DEV_DISCONNECTED = 1,
123 DEV_MISCONFIGURED = 2
128 struct au0828_usb_isoc_ctl {
129 /* max packet size of isoc transaction */
132 /* number of allocated urbs */
135 /* urb for isoc transfers */
138 /* transfer buffers for isoc transfer */
139 char **transfer_buffer;
141 /* Last buffer command and region */
143 int pos, size, pktsize;
145 /* Last field: ODD or EVEN? */
148 /* Stores incomplete commands */
152 /* Stores already requested buffers */
153 struct au0828_buffer *buf;
154 struct au0828_buffer *vbi_buf;
156 /* Stores the number of received fields */
159 /* isoc urb callback */
160 int (*isoc_copy) (struct au0828_dev *dev, struct urb *urb);
164 /* buffer for one video frame */
165 struct au0828_buffer {
166 /* common v4l buffer stuff -- must be first */
167 struct vb2_v4l2_buffer vb;
168 struct list_head list;
171 unsigned long length;
173 /* pointer to vmalloc memory address in vb */
177 struct au0828_dmaqueue {
178 struct list_head active;
179 /* Counters to control buffer fill */
185 struct usb_device *usbdev;
187 struct au0828_board board;
191 struct i2c_adapter i2c_adap;
192 struct i2c_algorithm i2c_algo;
193 struct i2c_client i2c_client;
197 struct au0828_dvb dvb;
198 struct work_struct restart_streaming;
199 struct timer_list bulk_timeout;
200 int bulk_timeout_running;
202 #ifdef CONFIG_VIDEO_AU0828_V4L2
204 struct v4l2_device v4l2_dev;
205 struct v4l2_ctrl_handler v4l2_ctrl_hdl;
207 #ifdef CONFIG_VIDEO_AU0828_RC
208 struct au0828_rc *ir;
211 struct video_device vdev;
212 struct video_device vbi_dev;
215 struct vb2_queue vb_vidq;
216 struct vb2_queue vb_vbiq;
217 struct mutex vb_queue_lock;
218 struct mutex vb_vbi_queue_lock;
220 unsigned int frame_count;
221 unsigned int vbi_frame_count;
223 struct timer_list vid_timeout;
224 int vid_timeout_running;
225 struct timer_list vbi_timeout;
226 int vbi_timeout_running;
242 __u8 isoc_in_endpointaddr;
244 int greenscreen_detected;
247 int std_set_in_tuner_core;
248 unsigned int ctrl_input;
249 long unsigned int dev_state; /* defined at enum au0828_dev_state */;
250 enum au0828_stream_state stream_state;
251 wait_queue_head_t open;
255 /* Isoc control struct */
256 struct au0828_dmaqueue vidq;
257 struct au0828_dmaqueue vbiq;
258 struct au0828_usb_isoc_ctl isoc_ctl;
262 int alt; /* alternate */
263 int max_pkt_size; /* max packet size of isoc transaction */
264 int num_alt; /* Number of alternative settings */
265 unsigned int *alt_max_pkt_size; /* array of wMaxPacketSize */
266 struct urb *urb[AU0828_MAX_ISO_BUFS]; /* urb for isoc transfers */
267 char *transfer_buffer[AU0828_MAX_ISO_BUFS];/* transfer buffers for isoc
270 /* DVB USB / URB Related */
271 bool urb_streaming, need_urb_start;
272 struct urb *urbs[URB_COUNT];
274 /* Preallocated transfer digital transfer buffers */
276 char *dig_transfer_buffer[URB_COUNT];
278 #ifdef CONFIG_MEDIA_CONTROLLER
279 struct media_device *media_dev;
280 struct media_pad video_pad, vbi_pad;
281 struct media_entity *decoder;
282 struct media_entity input_ent[AU0828_MAX_INPUT];
283 struct media_pad input_pad[AU0828_MAX_INPUT];
284 struct media_entity_notify entity_notify;
285 struct media_entity *tuner;
286 struct media_link *active_link;
287 struct media_entity *active_source;
288 struct media_entity *active_sink;
289 struct media_entity *active_link_owner;
290 struct media_entity *active_link_user;
291 struct media_pipeline *active_link_user_pipe;
292 bool active_link_shared;
297 /* ----------------------------------------------------------- */
298 #define au0828_read(dev, reg) au0828_readreg(dev, reg)
299 #define au0828_write(dev, reg, value) au0828_writereg(dev, reg, value)
300 #define au0828_andor(dev, reg, mask, value) \
301 au0828_writereg(dev, reg, \
302 (au0828_readreg(dev, reg) & ~(mask)) | ((value) & (mask)))
304 #define au0828_set(dev, reg, bit) au0828_andor(dev, (reg), (bit), (bit))
305 #define au0828_clear(dev, reg, bit) au0828_andor(dev, (reg), (bit), 0)
307 /* ----------------------------------------------------------- */
309 extern u32 au0828_read(struct au0828_dev *dev, u16 reg);
310 extern u32 au0828_write(struct au0828_dev *dev, u16 reg, u32 val);
311 extern void au0828_usb_release(struct au0828_dev *dev);
312 extern int au0828_debug;
314 /* ----------------------------------------------------------- */
316 extern struct au0828_board au0828_boards[];
317 extern struct usb_device_id au0828_usb_id_table[];
318 extern void au0828_gpio_setup(struct au0828_dev *dev);
319 extern int au0828_tuner_callback(void *priv, int component,
320 int command, int arg);
321 extern void au0828_card_setup(struct au0828_dev *dev);
323 /* ----------------------------------------------------------- */
325 extern int au0828_i2c_register(struct au0828_dev *dev);
326 extern int au0828_i2c_unregister(struct au0828_dev *dev);
328 /* ----------------------------------------------------------- */
330 extern int au0828_start_analog_streaming(struct vb2_queue *vq,
332 extern void au0828_stop_vbi_streaming(struct vb2_queue *vq);
333 #ifdef CONFIG_VIDEO_AU0828_V4L2
334 extern int au0828_v4l2_device_register(struct usb_interface *interface,
335 struct au0828_dev *dev);
337 extern int au0828_analog_register(struct au0828_dev *dev,
338 struct usb_interface *interface);
339 extern int au0828_analog_unregister(struct au0828_dev *dev);
340 extern void au0828_usb_v4l2_media_release(struct au0828_dev *dev);
341 extern void au0828_v4l2_suspend(struct au0828_dev *dev);
342 extern void au0828_v4l2_resume(struct au0828_dev *dev);
344 static inline int au0828_v4l2_device_register(struct usb_interface *interface,
345 struct au0828_dev *dev)
347 static inline int au0828_analog_register(struct au0828_dev *dev,
348 struct usb_interface *interface)
350 static inline int au0828_analog_unregister(struct au0828_dev *dev)
352 static inline void au0828_usb_v4l2_media_release(struct au0828_dev *dev) { };
353 static inline void au0828_v4l2_suspend(struct au0828_dev *dev) { };
354 static inline void au0828_v4l2_resume(struct au0828_dev *dev) { };
357 /* ----------------------------------------------------------- */
359 extern int au0828_dvb_register(struct au0828_dev *dev);
360 extern void au0828_dvb_unregister(struct au0828_dev *dev);
361 void au0828_dvb_suspend(struct au0828_dev *dev);
362 void au0828_dvb_resume(struct au0828_dev *dev);
365 extern const struct vb2_ops au0828_vbi_qops;
367 #define dprintk(level, fmt, arg...)\
368 do { if (au0828_debug & level)\
369 printk(KERN_DEBUG pr_fmt(fmt), ## arg);\
373 #ifdef CONFIG_VIDEO_AU0828_RC
374 extern int au0828_rc_register(struct au0828_dev *dev);
375 extern void au0828_rc_unregister(struct au0828_dev *dev);
376 extern int au0828_rc_suspend(struct au0828_dev *dev);
377 extern int au0828_rc_resume(struct au0828_dev *dev);
379 static inline int au0828_rc_register(struct au0828_dev *dev) { return 0; }
380 static inline void au0828_rc_unregister(struct au0828_dev *dev) { }
381 static inline int au0828_rc_suspend(struct au0828_dev *dev) { return 0; }
382 static inline int au0828_rc_resume(struct au0828_dev *dev) { return 0; }