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