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.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24 #include <linux/bitops.h>
25 #include <linux/usb.h>
26 #include <linux/i2c.h>
27 #include <linux/i2c-algo-bit.h>
28 #include <media/tveeprom.h>
31 #include <linux/videodev2.h>
32 #include <media/videobuf2-v4l2.h>
33 #include <media/videobuf2-vmalloc.h>
34 #include <media/v4l2-device.h>
35 #include <media/v4l2-ctrls.h>
36 #include <media/v4l2-fh.h>
41 #include "dvb_demux.h"
42 #include "dvb_frontend.h"
46 #include "au0828-reg.h"
47 #include "au0828-cards.h"
50 #define URB_BUFSIZE (0xe522)
52 /* Analog constants */
53 #define NTSC_STD_W 720
54 #define NTSC_STD_H 480
56 #define AU0828_INTERLACED_DEFAULT 1
57 #define V4L2_CID_PRIVATE_SHARPNESS (V4L2_CID_PRIVATE_BASE + 0)
59 /* Defination for AU0828 USB transfer */
60 #define AU0828_MAX_ISO_BUFS 12 /* maybe resize this value in the future */
61 #define AU0828_ISO_PACKETS_PER_URB 128
63 #define AU0828_MIN_BUF 4
64 #define AU0828_DEF_BUF 8
66 #define AU0828_MAX_INPUT 4
68 /* au0828 resource types (used for res_get/res_lock etc */
69 #define AU0828_RESOURCE_VIDEO 0x01
70 #define AU0828_RESOURCE_VBI 0x02
73 AU0828_VMUX_UNDEFINED = 0,
74 AU0828_VMUX_COMPOSITE,
77 AU0828_VMUX_TELEVISION,
83 enum au0828_itype type;
86 void (*audio_setup) (void *priv, int enable);
91 unsigned int tuner_type;
92 unsigned char tuner_addr;
93 unsigned char i2c_clk_divider;
94 unsigned char has_ir_i2c:1;
95 unsigned char has_analog:1;
96 struct au0828_input input[AU0828_MAX_INPUT];
102 struct dvb_adapter adapter;
103 struct dvb_frontend *frontend;
104 struct dvb_demux demux;
105 struct dmxdev dmxdev;
106 struct dmx_frontend fe_hw;
107 struct dmx_frontend fe_mem;
113 int (*set_frontend)(struct dvb_frontend *fe);
116 enum au0828_stream_state {
122 #define AUVI_INPUT(nr) (dev->board.input[nr])
125 enum au0828_dev_state {
127 DEV_DISCONNECTED = 1,
128 DEV_MISCONFIGURED = 2
133 struct au0828_usb_isoc_ctl {
134 /* max packet size of isoc transaction */
137 /* number of allocated urbs */
140 /* urb for isoc transfers */
143 /* transfer buffers for isoc transfer */
144 char **transfer_buffer;
146 /* Last buffer command and region */
148 int pos, size, pktsize;
150 /* Last field: ODD or EVEN? */
153 /* Stores incomplete commands */
157 /* Stores already requested buffers */
158 struct au0828_buffer *buf;
159 struct au0828_buffer *vbi_buf;
161 /* Stores the number of received fields */
164 /* isoc urb callback */
165 int (*isoc_copy) (struct au0828_dev *dev, struct urb *urb);
169 /* buffer for one video frame */
170 struct au0828_buffer {
171 /* common v4l buffer stuff -- must be first */
172 struct vb2_v4l2_buffer vb;
173 struct list_head list;
176 unsigned long length;
178 /* pointer to vmalloc memory address in vb */
182 struct au0828_dmaqueue {
183 struct list_head active;
184 /* Counters to control buffer fill */
190 struct usb_device *usbdev;
192 struct au0828_board board;
196 struct i2c_adapter i2c_adap;
197 struct i2c_algorithm i2c_algo;
198 struct i2c_client i2c_client;
202 struct au0828_dvb dvb;
203 struct work_struct restart_streaming;
205 #ifdef CONFIG_VIDEO_AU0828_V4L2
207 struct v4l2_device v4l2_dev;
208 struct v4l2_ctrl_handler v4l2_ctrl_hdl;
210 #ifdef CONFIG_VIDEO_AU0828_RC
211 struct au0828_rc *ir;
214 struct video_device vdev;
215 struct video_device vbi_dev;
218 struct vb2_queue vb_vidq;
219 struct vb2_queue vb_vbiq;
220 struct mutex vb_queue_lock;
221 struct mutex vb_vbi_queue_lock;
223 unsigned int frame_count;
224 unsigned int vbi_frame_count;
226 struct timer_list vid_timeout;
227 int vid_timeout_running;
228 struct timer_list vbi_timeout;
229 int vbi_timeout_running;
245 __u8 isoc_in_endpointaddr;
247 int greenscreen_detected;
250 int std_set_in_tuner_core;
251 unsigned int ctrl_input;
252 long unsigned int dev_state; /* defined at enum au0828_dev_state */;
253 enum au0828_stream_state stream_state;
254 wait_queue_head_t open;
258 /* Isoc control struct */
259 struct au0828_dmaqueue vidq;
260 struct au0828_dmaqueue vbiq;
261 struct au0828_usb_isoc_ctl isoc_ctl;
265 int alt; /* alternate */
266 int max_pkt_size; /* max packet size of isoc transaction */
267 int num_alt; /* Number of alternative settings */
268 unsigned int *alt_max_pkt_size; /* array of wMaxPacketSize */
269 struct urb *urb[AU0828_MAX_ISO_BUFS]; /* urb for isoc transfers */
270 char *transfer_buffer[AU0828_MAX_ISO_BUFS];/* transfer buffers for isoc
273 /* DVB USB / URB Related */
274 bool urb_streaming, need_urb_start;
275 struct urb *urbs[URB_COUNT];
277 /* Preallocated transfer digital transfer buffers */
279 char *dig_transfer_buffer[URB_COUNT];
283 /* ----------------------------------------------------------- */
284 #define au0828_read(dev, reg) au0828_readreg(dev, reg)
285 #define au0828_write(dev, reg, value) au0828_writereg(dev, reg, value)
286 #define au0828_andor(dev, reg, mask, value) \
287 au0828_writereg(dev, reg, \
288 (au0828_readreg(dev, reg) & ~(mask)) | ((value) & (mask)))
290 #define au0828_set(dev, reg, bit) au0828_andor(dev, (reg), (bit), (bit))
291 #define au0828_clear(dev, reg, bit) au0828_andor(dev, (reg), (bit), 0)
293 /* ----------------------------------------------------------- */
295 extern u32 au0828_read(struct au0828_dev *dev, u16 reg);
296 extern u32 au0828_write(struct au0828_dev *dev, u16 reg, u32 val);
297 extern int au0828_debug;
299 /* ----------------------------------------------------------- */
301 extern struct au0828_board au0828_boards[];
302 extern struct usb_device_id au0828_usb_id_table[];
303 extern void au0828_gpio_setup(struct au0828_dev *dev);
304 extern int au0828_tuner_callback(void *priv, int component,
305 int command, int arg);
306 extern void au0828_card_setup(struct au0828_dev *dev);
308 /* ----------------------------------------------------------- */
310 extern int au0828_i2c_register(struct au0828_dev *dev);
311 extern int au0828_i2c_unregister(struct au0828_dev *dev);
313 /* ----------------------------------------------------------- */
315 extern int au0828_analog_register(struct au0828_dev *dev,
316 struct usb_interface *interface);
317 extern void au0828_analog_unregister(struct au0828_dev *dev);
318 extern int au0828_start_analog_streaming(struct vb2_queue *vq,
320 extern void au0828_stop_vbi_streaming(struct vb2_queue *vq);
321 #ifdef CONFIG_VIDEO_AU0828_V4L2
322 extern void au0828_v4l2_suspend(struct au0828_dev *dev);
323 extern void au0828_v4l2_resume(struct au0828_dev *dev);
325 static inline void au0828_v4l2_suspend(struct au0828_dev *dev) { };
326 static inline void au0828_v4l2_resume(struct au0828_dev *dev) { };
329 /* ----------------------------------------------------------- */
331 extern int au0828_dvb_register(struct au0828_dev *dev);
332 extern void au0828_dvb_unregister(struct au0828_dev *dev);
333 void au0828_dvb_suspend(struct au0828_dev *dev);
334 void au0828_dvb_resume(struct au0828_dev *dev);
337 extern struct vb2_ops au0828_vbi_qops;
339 #define dprintk(level, fmt, arg...)\
340 do { if (au0828_debug & level)\
341 printk(KERN_DEBUG pr_fmt(fmt), ## arg);\
345 #ifdef CONFIG_VIDEO_AU0828_RC
346 extern int au0828_rc_register(struct au0828_dev *dev);
347 extern void au0828_rc_unregister(struct au0828_dev *dev);
348 extern int au0828_rc_suspend(struct au0828_dev *dev);
349 extern int au0828_rc_resume(struct au0828_dev *dev);
351 static inline int au0828_rc_register(struct au0828_dev *dev) { return 0; }
352 static inline void au0828_rc_unregister(struct au0828_dev *dev) { }
353 static inline int au0828_rc_suspend(struct au0828_dev *dev) { return 0; }
354 static inline int au0828_rc_resume(struct au0828_dev *dev) { return 0; }