590d15e461dc9fd1d1c2bf150bb0b0c841d36c73
[pandora-kernel.git] / drivers / media / video / au0828 / au0828.h
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
31 /* DVB */
32 #include "demux.h"
33 #include "dmxdev.h"
34 #include "dvb_demux.h"
35 #include "dvb_frontend.h"
36 #include "dvb_net.h"
37 #include "dvbdev.h"
38
39 #include "au0828-reg.h"
40 #include "au0828-cards.h"
41
42 #define DRIVER_NAME "au0828"
43 #define URB_COUNT   16
44 #define URB_BUFSIZE (0xe522)
45
46 /* Analog constants */
47 #define NTSC_STD_W      720
48 #define NTSC_STD_H      480
49
50 #define AU0828_INTERLACED_DEFAULT       1
51 #define V4L2_CID_PRIVATE_SHARPNESS  (V4L2_CID_PRIVATE_BASE + 0)
52
53 /* Defination for AU0828 USB transfer */
54 #define AU0828_MAX_ISO_BUFS    12  /* maybe resize this value in the future */
55 #define AU0828_ISO_PACKETS_PER_URB      10
56 #define AU0828_ISO_MAX_FRAME_SIZE       (3 * 1024)
57 #define AU0828_ISO_BUFFER_SIZE          (AU0828_ISO_PACKETS_PER_URB * AU0828_ISO_MAX_FRAME_SIZE)
58
59 #define AU0828_MIN_BUF 4
60 #define AU0828_DEF_BUF 8
61
62 #define AU0828_MAX_IMAGES       10
63 #define AU0828_FRAME_SIZE       (1028 * 1024 * 4)
64 #define AU0828_URB_TIMEOUT      msecs_to_jiffies(AU0828_MAX_ISO_BUFS * AU0828_ISO_PACKETS_PER_URB)
65
66 #define AU0828_MAX_INPUT        4
67
68 enum au0828_itype {
69         AU0828_VMUX_UNDEFINED = 0,
70         AU0828_VMUX_COMPOSITE,
71         AU0828_VMUX_SVIDEO,
72         AU0828_VMUX_CABLE,
73         AU0828_VMUX_TELEVISION,
74         AU0828_VMUX_DVB,
75         AU0828_VMUX_DEBUG
76 };
77
78 struct au0828_input {
79         enum au0828_itype type;
80         unsigned int vmux;
81         unsigned int amux;
82         void (*audio_setup) (void *priv, int enable);
83 };
84
85 struct au0828_board {
86         char *name;
87         unsigned int tuner_type;
88         unsigned char tuner_addr;
89         struct au0828_input input[AU0828_MAX_INPUT];
90
91 };
92
93 struct au0828_dvb {
94         struct mutex lock;
95         struct dvb_adapter adapter;
96         struct dvb_frontend *frontend;
97         struct dvb_demux demux;
98         struct dmxdev dmxdev;
99         struct dmx_frontend fe_hw;
100         struct dmx_frontend fe_mem;
101         struct dvb_net net;
102         int feeding;
103 };
104
105 enum au0828_stream_state {
106         STREAM_OFF,
107         STREAM_INTERRUPT,
108         STREAM_ON
109 };
110
111 #define AUVI_INPUT(nr) (dev->board.input[nr])
112
113 /* device state */
114 enum au0828_dev_state {
115         DEV_INITIALIZED = 0x01,
116         DEV_DISCONNECTED = 0x02,
117         DEV_MISCONFIGURED = 0x04
118 };
119
120 struct au0828_fh {
121         struct au0828_dev *dev;
122         unsigned int  stream_on:1;      /* Locks streams */
123         struct videobuf_queue        vb_vidq;
124         enum v4l2_buf_type           type;
125 };
126
127 struct au0828_usb_isoc_ctl {
128                 /* max packet size of isoc transaction */
129         int                             max_pkt_size;
130
131                 /* number of allocated urbs */
132         int                             num_bufs;
133
134                 /* urb for isoc transfers */
135         struct urb                      **urb;
136
137                 /* transfer buffers for isoc transfer */
138         char                            **transfer_buffer;
139
140                 /* Last buffer command and region */
141         u8                              cmd;
142         int                             pos, size, pktsize;
143
144                 /* Last field: ODD or EVEN? */
145         int                             field;
146
147                 /* Stores incomplete commands */
148         u32                             tmp_buf;
149         int                             tmp_buf_len;
150
151                 /* Stores already requested buffers */
152         struct au0828_buffer            *buf;
153
154                 /* Stores the number of received fields */
155         int                             nfields;
156
157                 /* isoc urb callback */
158         int (*isoc_copy) (struct au0828_dev *dev, struct urb *urb);
159
160 };
161
162 /* buffer for one video frame */
163 struct au0828_buffer {
164         /* common v4l buffer stuff -- must be first */
165         struct videobuf_buffer vb;
166
167         struct list_head frame;
168         int top_field;
169         int receiving;
170 };
171
172 struct au0828_dmaqueue {
173         struct list_head       active;
174         struct list_head       queued;
175
176         wait_queue_head_t          wq;
177
178         /* Counters to control buffer fill */
179         int                        pos;
180 };
181
182 struct au0828_dev {
183         struct mutex mutex;
184         struct usb_device       *usbdev;
185         int                     boardnr;
186         struct au0828_board     board;
187         u8                      ctrlmsg[64];
188
189         /* I2C */
190         struct i2c_adapter              i2c_adap;
191         struct i2c_algo_bit_data        i2c_algo;
192         struct i2c_client               i2c_client;
193         u32                             i2c_rc;
194
195         /* Digital */
196         struct au0828_dvb               dvb;
197
198         /* Analog */
199         struct list_head au0828list;
200         int users;
201         unsigned int stream_on:1;       /* Locks streams */
202         struct video_device *vdev;
203         struct video_device *vbi_dev;
204         int width;
205         int height;
206         u32 field_size;
207         u32 frame_size;
208         u32 bytesperline;
209         int type;
210         u8 ctrl_ainput;
211         __u8 isoc_in_endpointaddr;
212         u8 isoc_init_ok;
213         int greenscreen_detected;
214         unsigned int frame_count;
215         int ctrl_freq;
216         int input_type;
217         unsigned int ctrl_input;
218         enum au0828_dev_state dev_state;
219         enum au0828_stream_state stream_state;
220         wait_queue_head_t open;
221
222         struct mutex lock;
223
224         /* Isoc control struct */
225         struct au0828_dmaqueue vidq;
226         struct au0828_usb_isoc_ctl isoc_ctl;
227         spinlock_t slock;
228
229         /* usb transfer */
230         int alt;                /* alternate */
231         int max_pkt_size;       /* max packet size of isoc transaction */
232         int num_alt;            /* Number of alternative settings */
233         unsigned int *alt_max_pkt_size; /* array of wMaxPacketSize */
234         struct urb *urb[AU0828_MAX_ISO_BUFS];   /* urb for isoc transfers */
235         char *transfer_buffer[AU0828_MAX_ISO_BUFS];/* transfer buffers for isoc
236                                                    transfer */
237
238         /* USB / URB Related */
239         int             urb_streaming;
240         struct urb      *urbs[URB_COUNT];
241 };
242
243 /* ----------------------------------------------------------- */
244 #define au0828_read(dev, reg) au0828_readreg(dev, reg)
245 #define au0828_write(dev, reg, value) au0828_writereg(dev, reg, value)
246 #define au0828_andor(dev, reg, mask, value)                             \
247          au0828_writereg(dev, reg,                                      \
248         (au0828_readreg(dev, reg) & ~(mask)) | ((value) & (mask)))
249
250 #define au0828_set(dev, reg, bit) au0828_andor(dev, (reg), (bit), (bit))
251 #define au0828_clear(dev, reg, bit) au0828_andor(dev, (reg), (bit), 0)
252
253 /* ----------------------------------------------------------- */
254 /* au0828-core.c */
255 extern u32 au0828_read(struct au0828_dev *dev, u16 reg);
256 extern u32 au0828_write(struct au0828_dev *dev, u16 reg, u32 val);
257 extern int au0828_debug;
258
259 /* ----------------------------------------------------------- */
260 /* au0828-cards.c */
261 extern struct au0828_board au0828_boards[];
262 extern struct usb_device_id au0828_usb_id_table[];
263 extern void au0828_gpio_setup(struct au0828_dev *dev);
264 extern int au0828_tuner_callback(void *priv, int component,
265                                  int command, int arg);
266 extern void au0828_card_setup(struct au0828_dev *dev);
267
268 /* ----------------------------------------------------------- */
269 /* au0828-i2c.c */
270 extern int au0828_i2c_register(struct au0828_dev *dev);
271 extern int au0828_i2c_unregister(struct au0828_dev *dev);
272 extern void au0828_call_i2c_clients(struct au0828_dev *dev,
273         unsigned int cmd, void *arg);
274
275 /* ----------------------------------------------------------- */
276 /* au0828-video.c */
277 int au0828_analog_register(struct au0828_dev *dev,
278                            struct usb_interface *interface);
279 int au0828_analog_stream_disable(struct au0828_dev *d);
280 void au0828_analog_unregister(struct au0828_dev *dev);
281
282 /* ----------------------------------------------------------- */
283 /* au0828-dvb.c */
284 extern int au0828_dvb_register(struct au0828_dev *dev);
285 extern void au0828_dvb_unregister(struct au0828_dev *dev);
286
287 #define dprintk(level, fmt, arg...)\
288         do { if (au0828_debug & level)\
289                 printk(KERN_DEBUG DRIVER_NAME "/0: " fmt, ## arg);\
290         } while (0)