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