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