aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/media/video/au0828
diff options
context:
space:
mode:
authorJonathan Herman <hermanjl@cs.unc.edu>2013-01-22 10:38:37 -0500
committerJonathan Herman <hermanjl@cs.unc.edu>2013-01-22 10:38:37 -0500
commitfcc9d2e5a6c89d22b8b773a64fb4ad21ac318446 (patch)
treea57612d1888735a2ec7972891b68c1ac5ec8faea /drivers/media/video/au0828
parent8dea78da5cee153b8af9c07a2745f6c55057fe12 (diff)
Added missing tegra files.HEADmaster
Diffstat (limited to 'drivers/media/video/au0828')
-rw-r--r--drivers/media/video/au0828/Kconfig16
-rw-r--r--drivers/media/video/au0828/Makefile9
-rw-r--r--drivers/media/video/au0828/au0828-cards.c330
-rw-r--r--drivers/media/video/au0828/au0828-cards.h27
-rw-r--r--drivers/media/video/au0828/au0828-core.c295
-rw-r--r--drivers/media/video/au0828/au0828-dvb.c439
-rw-r--r--drivers/media/video/au0828/au0828-i2c.c390
-rw-r--r--drivers/media/video/au0828/au0828-reg.h65
-rw-r--r--drivers/media/video/au0828/au0828-vbi.c138
-rw-r--r--drivers/media/video/au0828/au0828-video.c1993
-rw-r--r--drivers/media/video/au0828/au0828.h302
11 files changed, 4004 insertions, 0 deletions
diff --git a/drivers/media/video/au0828/Kconfig b/drivers/media/video/au0828/Kconfig
new file mode 100644
index 00000000000..0c3a5ba0e85
--- /dev/null
+++ b/drivers/media/video/au0828/Kconfig
@@ -0,0 +1,16 @@
1
2config VIDEO_AU0828
3 tristate "Auvitek AU0828 support"
4 depends on I2C && INPUT && DVB_CORE && USB && VIDEO_V4L2
5 select I2C_ALGOBIT
6 select VIDEO_TVEEPROM
7 select VIDEOBUF_VMALLOC
8 select DVB_AU8522 if !DVB_FE_CUSTOMISE
9 select MEDIA_TUNER_XC5000 if !MEDIA_TUNER_CUSTOMISE
10 select MEDIA_TUNER_MXL5007T if !MEDIA_TUNER_CUSTOMISE
11 select MEDIA_TUNER_TDA18271 if !MEDIA_TUNER_CUSTOMISE
12 ---help---
13 This is a video4linux driver for Auvitek's USB device.
14
15 To compile this driver as a module, choose M here: the
16 module will be called au0828
diff --git a/drivers/media/video/au0828/Makefile b/drivers/media/video/au0828/Makefile
new file mode 100644
index 00000000000..5c7f2f7d980
--- /dev/null
+++ b/drivers/media/video/au0828/Makefile
@@ -0,0 +1,9 @@
1au0828-objs := au0828-core.o au0828-i2c.o au0828-cards.o au0828-dvb.o au0828-video.o au0828-vbi.o
2
3obj-$(CONFIG_VIDEO_AU0828) += au0828.o
4
5EXTRA_CFLAGS += -Idrivers/media/common/tuners
6EXTRA_CFLAGS += -Idrivers/media/dvb/dvb-core
7EXTRA_CFLAGS += -Idrivers/media/dvb/frontends
8
9EXTRA_CFLAGS += $(extra-cflags-y) $(extra-cflags-m)
diff --git a/drivers/media/video/au0828/au0828-cards.c b/drivers/media/video/au0828/au0828-cards.c
new file mode 100644
index 00000000000..39fc923fc46
--- /dev/null
+++ b/drivers/media/video/au0828/au0828-cards.c
@@ -0,0 +1,330 @@
1/*
2 * Driver for the Auvitek 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 "au0828.h"
23#include "au0828-cards.h"
24#include "au8522.h"
25#include "media/tuner.h"
26#include "media/v4l2-common.h"
27
28void hvr950q_cs5340_audio(void *priv, int enable)
29{
30 /* Because the HVR-950q shares an i2s bus between the cs5340 and the
31 au8522, we need to hold cs5340 in reset when using the au8522 */
32 struct au0828_dev *dev = priv;
33 if (enable == 1)
34 au0828_set(dev, REG_000, 0x10);
35 else
36 au0828_clear(dev, REG_000, 0x10);
37}
38
39struct au0828_board au0828_boards[] = {
40 [AU0828_BOARD_UNKNOWN] = {
41 .name = "Unknown board",
42 .tuner_type = UNSET,
43 .tuner_addr = ADDR_UNSET,
44 },
45 [AU0828_BOARD_HAUPPAUGE_HVR850] = {
46 .name = "Hauppauge HVR850",
47 .tuner_type = TUNER_XC5000,
48 .tuner_addr = 0x61,
49 .i2c_clk_divider = AU0828_I2C_CLK_30KHZ,
50 .input = {
51 {
52 .type = AU0828_VMUX_TELEVISION,
53 .vmux = AU8522_COMPOSITE_CH4_SIF,
54 .amux = AU8522_AUDIO_SIF,
55 },
56 {
57 .type = AU0828_VMUX_COMPOSITE,
58 .vmux = AU8522_COMPOSITE_CH1,
59 .amux = AU8522_AUDIO_NONE,
60 .audio_setup = hvr950q_cs5340_audio,
61 },
62 {
63 .type = AU0828_VMUX_SVIDEO,
64 .vmux = AU8522_SVIDEO_CH13,
65 .amux = AU8522_AUDIO_NONE,
66 .audio_setup = hvr950q_cs5340_audio,
67 },
68 },
69 },
70 [AU0828_BOARD_HAUPPAUGE_HVR950Q] = {
71 .name = "Hauppauge HVR950Q",
72 .tuner_type = TUNER_XC5000,
73 .tuner_addr = 0x61,
74 /* The au0828 hardware i2c implementation does not properly
75 support the xc5000's i2c clock stretching. So we need to
76 lower the clock frequency enough where the 15us clock
77 stretch fits inside of a normal clock cycle, or else the
78 au0828 fails to set the STOP bit. A 30 KHz clock puts the
79 clock pulse width at 18us */
80 .i2c_clk_divider = AU0828_I2C_CLK_30KHZ,
81 .input = {
82 {
83 .type = AU0828_VMUX_TELEVISION,
84 .vmux = AU8522_COMPOSITE_CH4_SIF,
85 .amux = AU8522_AUDIO_SIF,
86 },
87 {
88 .type = AU0828_VMUX_COMPOSITE,
89 .vmux = AU8522_COMPOSITE_CH1,
90 .amux = AU8522_AUDIO_NONE,
91 .audio_setup = hvr950q_cs5340_audio,
92 },
93 {
94 .type = AU0828_VMUX_SVIDEO,
95 .vmux = AU8522_SVIDEO_CH13,
96 .amux = AU8522_AUDIO_NONE,
97 .audio_setup = hvr950q_cs5340_audio,
98 },
99 },
100 },
101 [AU0828_BOARD_HAUPPAUGE_HVR950Q_MXL] = {
102 .name = "Hauppauge HVR950Q rev xxF8",
103 .tuner_type = UNSET,
104 .tuner_addr = ADDR_UNSET,
105 .i2c_clk_divider = AU0828_I2C_CLK_250KHZ,
106 },
107 [AU0828_BOARD_DVICO_FUSIONHDTV7] = {
108 .name = "DViCO FusionHDTV USB",
109 .tuner_type = UNSET,
110 .tuner_addr = ADDR_UNSET,
111 .i2c_clk_divider = AU0828_I2C_CLK_250KHZ,
112 },
113 [AU0828_BOARD_HAUPPAUGE_WOODBURY] = {
114 .name = "Hauppauge Woodbury",
115 .tuner_type = UNSET,
116 .tuner_addr = ADDR_UNSET,
117 .i2c_clk_divider = AU0828_I2C_CLK_250KHZ,
118 },
119};
120
121/* Tuner callback function for au0828 boards. Currently only needed
122 * for HVR1500Q, which has an xc5000 tuner.
123 */
124int au0828_tuner_callback(void *priv, int component, int command, int arg)
125{
126 struct au0828_dev *dev = priv;
127
128 dprintk(1, "%s()\n", __func__);
129
130 switch (dev->boardnr) {
131 case AU0828_BOARD_HAUPPAUGE_HVR850:
132 case AU0828_BOARD_HAUPPAUGE_HVR950Q:
133 case AU0828_BOARD_HAUPPAUGE_HVR950Q_MXL:
134 case AU0828_BOARD_DVICO_FUSIONHDTV7:
135 if (command == 0) {
136 /* Tuner Reset Command from xc5000 */
137 /* Drive the tuner into reset and out */
138 au0828_clear(dev, REG_001, 2);
139 mdelay(10);
140 au0828_set(dev, REG_001, 2);
141 mdelay(10);
142 return 0;
143 } else {
144 printk(KERN_ERR
145 "%s(): Unknown command.\n", __func__);
146 return -EINVAL;
147 }
148 break;
149 }
150
151 return 0; /* Should never be here */
152}
153
154static void hauppauge_eeprom(struct au0828_dev *dev, u8 *eeprom_data)
155{
156 struct tveeprom tv;
157
158 tveeprom_hauppauge_analog(&dev->i2c_client, &tv, eeprom_data);
159 dev->board.tuner_type = tv.tuner_type;
160
161 /* Make sure we support the board model */
162 switch (tv.model) {
163 case 72000: /* WinTV-HVR950q (Retail, IR, ATSC/QAM */
164 case 72001: /* WinTV-HVR950q (Retail, IR, ATSC/QAM and analog video */
165 case 72211: /* WinTV-HVR950q (OEM, IR, ATSC/QAM and analog video */
166 case 72221: /* WinTV-HVR950q (OEM, IR, ATSC/QAM and analog video */
167 case 72231: /* WinTV-HVR950q (OEM, IR, ATSC/QAM and analog video */
168 case 72241: /* WinTV-HVR950q (OEM, No IR, ATSC/QAM and analog video */
169 case 72251: /* WinTV-HVR950q (Retail, IR, ATSC/QAM and analog video */
170 case 72301: /* WinTV-HVR850 (Retail, IR, ATSC and analog video */
171 case 72500: /* WinTV-HVR950q (OEM, No IR, ATSC/QAM */
172 break;
173 default:
174 printk(KERN_WARNING "%s: warning: "
175 "unknown hauppauge model #%d\n", __func__, tv.model);
176 break;
177 }
178
179 printk(KERN_INFO "%s: hauppauge eeprom: model=%d\n",
180 __func__, tv.model);
181}
182
183void au0828_card_setup(struct au0828_dev *dev)
184{
185 static u8 eeprom[256];
186 struct tuner_setup tun_setup;
187 struct v4l2_subdev *sd;
188 unsigned int mode_mask = T_ANALOG_TV;
189
190 dprintk(1, "%s()\n", __func__);
191
192 memcpy(&dev->board, &au0828_boards[dev->boardnr], sizeof(dev->board));
193
194 if (dev->i2c_rc == 0) {
195 dev->i2c_client.addr = 0xa0 >> 1;
196 tveeprom_read(&dev->i2c_client, eeprom, sizeof(eeprom));
197 }
198
199 switch (dev->boardnr) {
200 case AU0828_BOARD_HAUPPAUGE_HVR850:
201 case AU0828_BOARD_HAUPPAUGE_HVR950Q:
202 case AU0828_BOARD_HAUPPAUGE_HVR950Q_MXL:
203 case AU0828_BOARD_HAUPPAUGE_WOODBURY:
204 if (dev->i2c_rc == 0)
205 hauppauge_eeprom(dev, eeprom+0xa0);
206 break;
207 }
208
209 if (AUVI_INPUT(0).type != AU0828_VMUX_UNDEFINED) {
210 /* Load the analog demodulator driver (note this would need to
211 be abstracted out if we ever need to support a different
212 demod) */
213 sd = v4l2_i2c_new_subdev(&dev->v4l2_dev, &dev->i2c_adap,
214 "au8522", 0x8e >> 1, NULL);
215 if (sd == NULL)
216 printk(KERN_ERR "analog subdev registration failed\n");
217 }
218
219 /* Setup tuners */
220 if (dev->board.tuner_type != TUNER_ABSENT) {
221 /* Load the tuner module, which does the attach */
222 sd = v4l2_i2c_new_subdev(&dev->v4l2_dev, &dev->i2c_adap,
223 "tuner", dev->board.tuner_addr, NULL);
224 if (sd == NULL)
225 printk(KERN_ERR "tuner subdev registration fail\n");
226
227 tun_setup.mode_mask = mode_mask;
228 tun_setup.type = dev->board.tuner_type;
229 tun_setup.addr = dev->board.tuner_addr;
230 tun_setup.tuner_callback = au0828_tuner_callback;
231 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_type_addr,
232 &tun_setup);
233 }
234}
235
236/*
237 * The bridge has between 8 and 12 gpios.
238 * Regs 1 and 0 deal with output enables.
239 * Regs 3 and 2 deal with direction.
240 */
241void au0828_gpio_setup(struct au0828_dev *dev)
242{
243 dprintk(1, "%s()\n", __func__);
244
245 switch (dev->boardnr) {
246 case AU0828_BOARD_HAUPPAUGE_HVR850:
247 case AU0828_BOARD_HAUPPAUGE_HVR950Q:
248 case AU0828_BOARD_HAUPPAUGE_HVR950Q_MXL:
249 case AU0828_BOARD_HAUPPAUGE_WOODBURY:
250 /* GPIO's
251 * 4 - CS5340
252 * 5 - AU8522 Demodulator
253 * 6 - eeprom W/P
254 * 7 - power supply
255 * 9 - XC5000 Tuner
256 */
257
258 /* Into reset */
259 au0828_write(dev, REG_003, 0x02);
260 au0828_write(dev, REG_002, 0x80 | 0x20 | 0x10);
261 au0828_write(dev, REG_001, 0x0);
262 au0828_write(dev, REG_000, 0x0);
263 msleep(100);
264
265 /* Out of reset (leave the cs5340 in reset until needed) */
266 au0828_write(dev, REG_003, 0x02);
267 au0828_write(dev, REG_001, 0x02);
268 au0828_write(dev, REG_002, 0x80 | 0x20 | 0x10);
269 au0828_write(dev, REG_000, 0x80 | 0x40 | 0x20);
270
271 msleep(250);
272 break;
273 case AU0828_BOARD_DVICO_FUSIONHDTV7:
274 /* GPIO's
275 * 6 - ?
276 * 8 - AU8522 Demodulator
277 * 9 - XC5000 Tuner
278 */
279
280 /* Into reset */
281 au0828_write(dev, REG_003, 0x02);
282 au0828_write(dev, REG_002, 0xa0);
283 au0828_write(dev, REG_001, 0x0);
284 au0828_write(dev, REG_000, 0x0);
285 msleep(100);
286
287 /* Out of reset */
288 au0828_write(dev, REG_003, 0x02);
289 au0828_write(dev, REG_002, 0xa0);
290 au0828_write(dev, REG_001, 0x02);
291 au0828_write(dev, REG_000, 0xa0);
292 msleep(250);
293 break;
294 }
295}
296
297/* table of devices that work with this driver */
298struct usb_device_id au0828_usb_id_table[] = {
299 { USB_DEVICE(0x2040, 0x7200),
300 .driver_info = AU0828_BOARD_HAUPPAUGE_HVR950Q },
301 { USB_DEVICE(0x2040, 0x7240),
302 .driver_info = AU0828_BOARD_HAUPPAUGE_HVR850 },
303 { USB_DEVICE(0x0fe9, 0xd620),
304 .driver_info = AU0828_BOARD_DVICO_FUSIONHDTV7 },
305 { USB_DEVICE(0x2040, 0x7210),
306 .driver_info = AU0828_BOARD_HAUPPAUGE_HVR950Q },
307 { USB_DEVICE(0x2040, 0x7217),
308 .driver_info = AU0828_BOARD_HAUPPAUGE_HVR950Q },
309 { USB_DEVICE(0x2040, 0x721b),
310 .driver_info = AU0828_BOARD_HAUPPAUGE_HVR950Q },
311 { USB_DEVICE(0x2040, 0x721e),
312 .driver_info = AU0828_BOARD_HAUPPAUGE_HVR950Q },
313 { USB_DEVICE(0x2040, 0x721f),
314 .driver_info = AU0828_BOARD_HAUPPAUGE_HVR950Q },
315 { USB_DEVICE(0x2040, 0x7280),
316 .driver_info = AU0828_BOARD_HAUPPAUGE_HVR950Q },
317 { USB_DEVICE(0x0fd9, 0x0008),
318 .driver_info = AU0828_BOARD_HAUPPAUGE_HVR950Q },
319 { USB_DEVICE(0x2040, 0x7201),
320 .driver_info = AU0828_BOARD_HAUPPAUGE_HVR950Q_MXL },
321 { USB_DEVICE(0x2040, 0x7211),
322 .driver_info = AU0828_BOARD_HAUPPAUGE_HVR950Q_MXL },
323 { USB_DEVICE(0x2040, 0x7281),
324 .driver_info = AU0828_BOARD_HAUPPAUGE_HVR950Q_MXL },
325 { USB_DEVICE(0x2040, 0x8200),
326 .driver_info = AU0828_BOARD_HAUPPAUGE_WOODBURY },
327 { },
328};
329
330MODULE_DEVICE_TABLE(usb, au0828_usb_id_table);
diff --git a/drivers/media/video/au0828/au0828-cards.h b/drivers/media/video/au0828/au0828-cards.h
new file mode 100644
index 00000000000..48a1882c2b6
--- /dev/null
+++ b/drivers/media/video/au0828/au0828-cards.h
@@ -0,0 +1,27 @@
1/*
2 * Driver for the Auvitek 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 AU0828_BOARD_UNKNOWN 0
23#define AU0828_BOARD_HAUPPAUGE_HVR950Q 1
24#define AU0828_BOARD_HAUPPAUGE_HVR850 2
25#define AU0828_BOARD_DVICO_FUSIONHDTV7 3
26#define AU0828_BOARD_HAUPPAUGE_HVR950Q_MXL 4
27#define AU0828_BOARD_HAUPPAUGE_WOODBURY 5
diff --git a/drivers/media/video/au0828/au0828-core.c b/drivers/media/video/au0828/au0828-core.c
new file mode 100644
index 00000000000..1e4ce5068ec
--- /dev/null
+++ b/drivers/media/video/au0828/au0828-core.c
@@ -0,0 +1,295 @@
1/*
2 * Driver for the Auvitek 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/module.h>
23#include <linux/slab.h>
24#include <linux/videodev2.h>
25#include <media/v4l2-common.h>
26#include <linux/mutex.h>
27
28#include "au0828.h"
29
30/*
31 * 1 = General debug messages
32 * 2 = USB handling
33 * 4 = I2C related
34 * 8 = Bridge related
35 */
36int au0828_debug;
37module_param_named(debug, au0828_debug, int, 0644);
38MODULE_PARM_DESC(debug, "enable debug messages");
39
40static unsigned int disable_usb_speed_check;
41module_param(disable_usb_speed_check, int, 0444);
42MODULE_PARM_DESC(disable_usb_speed_check,
43 "override min bandwidth requirement of 480M bps");
44
45#define _AU0828_BULKPIPE 0x03
46#define _BULKPIPESIZE 0xffff
47
48static int send_control_msg(struct au0828_dev *dev, u16 request, u32 value,
49 u16 index, unsigned char *cp, u16 size);
50static int recv_control_msg(struct au0828_dev *dev, u16 request, u32 value,
51 u16 index, unsigned char *cp, u16 size);
52
53/* USB Direction */
54#define CMD_REQUEST_IN 0x00
55#define CMD_REQUEST_OUT 0x01
56
57u32 au0828_readreg(struct au0828_dev *dev, u16 reg)
58{
59 recv_control_msg(dev, CMD_REQUEST_IN, 0, reg, dev->ctrlmsg, 1);
60 dprintk(8, "%s(0x%04x) = 0x%02x\n", __func__, reg, dev->ctrlmsg[0]);
61 return dev->ctrlmsg[0];
62}
63
64u32 au0828_writereg(struct au0828_dev *dev, u16 reg, u32 val)
65{
66 dprintk(8, "%s(0x%04x, 0x%02x)\n", __func__, reg, val);
67 return send_control_msg(dev, CMD_REQUEST_OUT, val, reg,
68 dev->ctrlmsg, 0);
69}
70
71static void cmd_msg_dump(struct au0828_dev *dev)
72{
73 int i;
74
75 for (i = 0; i < sizeof(dev->ctrlmsg); i += 16)
76 dprintk(2, "%s() %02x %02x %02x %02x %02x %02x %02x %02x "
77 "%02x %02x %02x %02x %02x %02x %02x %02x\n",
78 __func__,
79 dev->ctrlmsg[i+0], dev->ctrlmsg[i+1],
80 dev->ctrlmsg[i+2], dev->ctrlmsg[i+3],
81 dev->ctrlmsg[i+4], dev->ctrlmsg[i+5],
82 dev->ctrlmsg[i+6], dev->ctrlmsg[i+7],
83 dev->ctrlmsg[i+8], dev->ctrlmsg[i+9],
84 dev->ctrlmsg[i+10], dev->ctrlmsg[i+11],
85 dev->ctrlmsg[i+12], dev->ctrlmsg[i+13],
86 dev->ctrlmsg[i+14], dev->ctrlmsg[i+15]);
87}
88
89static int send_control_msg(struct au0828_dev *dev, u16 request, u32 value,
90 u16 index, unsigned char *cp, u16 size)
91{
92 int status = -ENODEV;
93 mutex_lock(&dev->mutex);
94 if (dev->usbdev) {
95
96 /* cp must be memory that has been allocated by kmalloc */
97 status = usb_control_msg(dev->usbdev,
98 usb_sndctrlpipe(dev->usbdev, 0),
99 request,
100 USB_DIR_OUT | USB_TYPE_VENDOR |
101 USB_RECIP_DEVICE,
102 value, index,
103 cp, size, 1000);
104
105 status = min(status, 0);
106
107 if (status < 0) {
108 printk(KERN_ERR "%s() Failed sending control message, error %d.\n",
109 __func__, status);
110 }
111
112 }
113 mutex_unlock(&dev->mutex);
114 return status;
115}
116
117static int recv_control_msg(struct au0828_dev *dev, u16 request, u32 value,
118 u16 index, unsigned char *cp, u16 size)
119{
120 int status = -ENODEV;
121 mutex_lock(&dev->mutex);
122 if (dev->usbdev) {
123
124 memset(dev->ctrlmsg, 0, sizeof(dev->ctrlmsg));
125
126 /* cp must be memory that has been allocated by kmalloc */
127 status = usb_control_msg(dev->usbdev,
128 usb_rcvctrlpipe(dev->usbdev, 0),
129 request,
130 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
131 value, index,
132 cp, size, 1000);
133
134 status = min(status, 0);
135
136 if (status < 0) {
137 printk(KERN_ERR "%s() Failed receiving control message, error %d.\n",
138 __func__, status);
139 } else
140 cmd_msg_dump(dev);
141 }
142 mutex_unlock(&dev->mutex);
143 return status;
144}
145
146static void au0828_usb_disconnect(struct usb_interface *interface)
147{
148 struct au0828_dev *dev = usb_get_intfdata(interface);
149
150 dprintk(1, "%s()\n", __func__);
151
152 /* Digital TV */
153 au0828_dvb_unregister(dev);
154
155 if (AUVI_INPUT(0).type != AU0828_VMUX_UNDEFINED)
156 au0828_analog_unregister(dev);
157
158 /* I2C */
159 au0828_i2c_unregister(dev);
160
161 v4l2_device_unregister(&dev->v4l2_dev);
162
163 usb_set_intfdata(interface, NULL);
164
165 mutex_lock(&dev->mutex);
166 dev->usbdev = NULL;
167 mutex_unlock(&dev->mutex);
168
169 kfree(dev);
170
171}
172
173static int au0828_usb_probe(struct usb_interface *interface,
174 const struct usb_device_id *id)
175{
176 int ifnum, retval;
177 struct au0828_dev *dev;
178 struct usb_device *usbdev = interface_to_usbdev(interface);
179
180 ifnum = interface->altsetting->desc.bInterfaceNumber;
181
182 if (ifnum != 0)
183 return -ENODEV;
184
185 dprintk(1, "%s() vendor id 0x%x device id 0x%x ifnum:%d\n", __func__,
186 le16_to_cpu(usbdev->descriptor.idVendor),
187 le16_to_cpu(usbdev->descriptor.idProduct),
188 ifnum);
189
190 /*
191 * Make sure we have 480 Mbps of bandwidth, otherwise things like
192 * video stream wouldn't likely work, since 12 Mbps is generally
193 * not enough even for most Digital TV streams.
194 */
195 if (usbdev->speed != USB_SPEED_HIGH && disable_usb_speed_check == 0) {
196 printk(KERN_ERR "au0828: Device initialization failed.\n");
197 printk(KERN_ERR "au0828: Device must be connected to a "
198 "high-speed USB 2.0 port.\n");
199 return -ENODEV;
200 }
201
202 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
203 if (dev == NULL) {
204 printk(KERN_ERR "%s() Unable to allocate memory\n", __func__);
205 return -ENOMEM;
206 }
207
208 mutex_init(&dev->mutex);
209 mutex_init(&dev->dvb.lock);
210 dev->usbdev = usbdev;
211 dev->boardnr = id->driver_info;
212
213 /* Create the v4l2_device */
214 retval = v4l2_device_register(&interface->dev, &dev->v4l2_dev);
215 if (retval) {
216 printk(KERN_ERR "%s() v4l2_device_register failed\n",
217 __func__);
218 kfree(dev);
219 return -EIO;
220 }
221
222 /* Power Up the bridge */
223 au0828_write(dev, REG_600, 1 << 4);
224
225 /* Bring up the GPIO's and supporting devices */
226 au0828_gpio_setup(dev);
227
228 /* I2C */
229 au0828_i2c_register(dev);
230
231 /* Setup */
232 au0828_card_setup(dev);
233
234 /* Analog TV */
235 if (AUVI_INPUT(0).type != AU0828_VMUX_UNDEFINED)
236 au0828_analog_register(dev, interface);
237
238 /* Digital TV */
239 au0828_dvb_register(dev);
240
241 /* Store the pointer to the au0828_dev so it can be accessed in
242 au0828_usb_disconnect */
243 usb_set_intfdata(interface, dev);
244
245 printk(KERN_INFO "Registered device AU0828 [%s]\n",
246 dev->board.name == NULL ? "Unset" : dev->board.name);
247
248 return 0;
249}
250
251static struct usb_driver au0828_usb_driver = {
252 .name = DRIVER_NAME,
253 .probe = au0828_usb_probe,
254 .disconnect = au0828_usb_disconnect,
255 .id_table = au0828_usb_id_table,
256};
257
258static int __init au0828_init(void)
259{
260 int ret;
261
262 if (au0828_debug & 1)
263 printk(KERN_INFO "%s() Debugging is enabled\n", __func__);
264
265 if (au0828_debug & 2)
266 printk(KERN_INFO "%s() USB Debugging is enabled\n", __func__);
267
268 if (au0828_debug & 4)
269 printk(KERN_INFO "%s() I2C Debugging is enabled\n", __func__);
270
271 if (au0828_debug & 8)
272 printk(KERN_INFO "%s() Bridge Debugging is enabled\n",
273 __func__);
274
275 printk(KERN_INFO "au0828 driver loaded\n");
276
277 ret = usb_register(&au0828_usb_driver);
278 if (ret)
279 printk(KERN_ERR "usb_register failed, error = %d\n", ret);
280
281 return ret;
282}
283
284static void __exit au0828_exit(void)
285{
286 usb_deregister(&au0828_usb_driver);
287}
288
289module_init(au0828_init);
290module_exit(au0828_exit);
291
292MODULE_DESCRIPTION("Driver for Auvitek AU0828 based products");
293MODULE_AUTHOR("Steven Toth <stoth@linuxtv.org>");
294MODULE_LICENSE("GPL");
295MODULE_VERSION("0.0.2");
diff --git a/drivers/media/video/au0828/au0828-dvb.c b/drivers/media/video/au0828/au0828-dvb.c
new file mode 100644
index 00000000000..518216743c9
--- /dev/null
+++ b/drivers/media/video/au0828/au0828-dvb.c
@@ -0,0 +1,439 @@
1/*
2 * Driver for the Auvitek 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/module.h>
23#include <linux/slab.h>
24#include <linux/init.h>
25#include <linux/device.h>
26#include <linux/suspend.h>
27#include <media/v4l2-common.h>
28
29#include "au0828.h"
30#include "au8522.h"
31#include "xc5000.h"
32#include "mxl5007t.h"
33#include "tda18271.h"
34
35DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
36
37#define _AU0828_BULKPIPE 0x83
38#define _BULKPIPESIZE 0xe522
39
40static u8 hauppauge_hvr950q_led_states[] = {
41 0x00, /* off */
42 0x02, /* yellow */
43 0x04, /* green */
44};
45
46static struct au8522_led_config hauppauge_hvr950q_led_cfg = {
47 .gpio_output = 0x00e0,
48 .gpio_output_enable = 0x6006,
49 .gpio_output_disable = 0x0660,
50
51 .gpio_leds = 0x00e2,
52 .led_states = hauppauge_hvr950q_led_states,
53 .num_led_states = sizeof(hauppauge_hvr950q_led_states),
54
55 .vsb8_strong = 20 /* dB */ * 10,
56 .qam64_strong = 25 /* dB */ * 10,
57 .qam256_strong = 32 /* dB */ * 10,
58};
59
60static struct au8522_config hauppauge_hvr950q_config = {
61 .demod_address = 0x8e >> 1,
62 .status_mode = AU8522_DEMODLOCKING,
63 .qam_if = AU8522_IF_6MHZ,
64 .vsb_if = AU8522_IF_6MHZ,
65 .led_cfg = &hauppauge_hvr950q_led_cfg,
66};
67
68static struct au8522_config fusionhdtv7usb_config = {
69 .demod_address = 0x8e >> 1,
70 .status_mode = AU8522_DEMODLOCKING,
71 .qam_if = AU8522_IF_6MHZ,
72 .vsb_if = AU8522_IF_6MHZ,
73};
74
75static struct au8522_config hauppauge_woodbury_config = {
76 .demod_address = 0x8e >> 1,
77 .status_mode = AU8522_DEMODLOCKING,
78 .qam_if = AU8522_IF_4MHZ,
79 .vsb_if = AU8522_IF_3_25MHZ,
80};
81
82static struct xc5000_config hauppauge_hvr950q_tunerconfig = {
83 .i2c_address = 0x61,
84 .if_khz = 6000,
85};
86
87static struct mxl5007t_config mxl5007t_hvr950q_config = {
88 .xtal_freq_hz = MxL_XTAL_24_MHZ,
89 .if_freq_hz = MxL_IF_6_MHZ,
90};
91
92static struct tda18271_config hauppauge_woodbury_tunerconfig = {
93 .gate = TDA18271_GATE_DIGITAL,
94};
95
96/*-------------------------------------------------------------------*/
97static void urb_completion(struct urb *purb)
98{
99 struct au0828_dev *dev = purb->context;
100 int ptype = usb_pipetype(purb->pipe);
101
102 dprintk(2, "%s()\n", __func__);
103
104 if (!dev)
105 return;
106
107 if (dev->urb_streaming == 0)
108 return;
109
110 if (ptype != PIPE_BULK) {
111 printk(KERN_ERR "%s() Unsupported URB type %d\n",
112 __func__, ptype);
113 return;
114 }
115
116 /* Feed the transport payload into the kernel demux */
117 dvb_dmx_swfilter_packets(&dev->dvb.demux,
118 purb->transfer_buffer, purb->actual_length / 188);
119
120 /* Clean the buffer before we requeue */
121 memset(purb->transfer_buffer, 0, URB_BUFSIZE);
122
123 /* Requeue URB */
124 usb_submit_urb(purb, GFP_ATOMIC);
125}
126
127static int stop_urb_transfer(struct au0828_dev *dev)
128{
129 int i;
130
131 dprintk(2, "%s()\n", __func__);
132
133 for (i = 0; i < URB_COUNT; i++) {
134 usb_kill_urb(dev->urbs[i]);
135 kfree(dev->urbs[i]->transfer_buffer);
136 usb_free_urb(dev->urbs[i]);
137 }
138
139 dev->urb_streaming = 0;
140
141 return 0;
142}
143
144static int start_urb_transfer(struct au0828_dev *dev)
145{
146 struct urb *purb;
147 int i, ret = -ENOMEM;
148
149 dprintk(2, "%s()\n", __func__);
150
151 if (dev->urb_streaming) {
152 dprintk(2, "%s: bulk xfer already running!\n", __func__);
153 return 0;
154 }
155
156 for (i = 0; i < URB_COUNT; i++) {
157
158 dev->urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
159 if (!dev->urbs[i])
160 goto err;
161
162 purb = dev->urbs[i];
163
164 purb->transfer_buffer = kzalloc(URB_BUFSIZE, GFP_KERNEL);
165 if (!purb->transfer_buffer) {
166 usb_free_urb(purb);
167 dev->urbs[i] = NULL;
168 goto err;
169 }
170
171 purb->status = -EINPROGRESS;
172 usb_fill_bulk_urb(purb,
173 dev->usbdev,
174 usb_rcvbulkpipe(dev->usbdev,
175 _AU0828_BULKPIPE),
176 purb->transfer_buffer,
177 URB_BUFSIZE,
178 urb_completion,
179 dev);
180
181 }
182
183 for (i = 0; i < URB_COUNT; i++) {
184 ret = usb_submit_urb(dev->urbs[i], GFP_ATOMIC);
185 if (ret != 0) {
186 stop_urb_transfer(dev);
187 printk(KERN_ERR "%s: failed urb submission, "
188 "err = %d\n", __func__, ret);
189 return ret;
190 }
191 }
192
193 dev->urb_streaming = 1;
194 ret = 0;
195
196err:
197 return ret;
198}
199
200static int au0828_dvb_start_feed(struct dvb_demux_feed *feed)
201{
202 struct dvb_demux *demux = feed->demux;
203 struct au0828_dev *dev = (struct au0828_dev *) demux->priv;
204 struct au0828_dvb *dvb = &dev->dvb;
205 int ret = 0;
206
207 dprintk(1, "%s()\n", __func__);
208
209 if (!demux->dmx.frontend)
210 return -EINVAL;
211
212 if (dvb) {
213 mutex_lock(&dvb->lock);
214 if (dvb->feeding++ == 0) {
215 /* Start transport */
216 au0828_write(dev, 0x608, 0x90);
217 au0828_write(dev, 0x609, 0x72);
218 au0828_write(dev, 0x60a, 0x71);
219 au0828_write(dev, 0x60b, 0x01);
220 ret = start_urb_transfer(dev);
221 }
222 mutex_unlock(&dvb->lock);
223 }
224
225 return ret;
226}
227
228static int au0828_dvb_stop_feed(struct dvb_demux_feed *feed)
229{
230 struct dvb_demux *demux = feed->demux;
231 struct au0828_dev *dev = (struct au0828_dev *) demux->priv;
232 struct au0828_dvb *dvb = &dev->dvb;
233 int ret = 0;
234
235 dprintk(1, "%s()\n", __func__);
236
237 if (dvb) {
238 mutex_lock(&dvb->lock);
239 if (--dvb->feeding == 0) {
240 /* Stop transport */
241 au0828_write(dev, 0x608, 0x00);
242 au0828_write(dev, 0x609, 0x00);
243 au0828_write(dev, 0x60a, 0x00);
244 au0828_write(dev, 0x60b, 0x00);
245 ret = stop_urb_transfer(dev);
246 }
247 mutex_unlock(&dvb->lock);
248 }
249
250 return ret;
251}
252
253static int dvb_register(struct au0828_dev *dev)
254{
255 struct au0828_dvb *dvb = &dev->dvb;
256 int result;
257
258 dprintk(1, "%s()\n", __func__);
259
260 /* register adapter */
261 result = dvb_register_adapter(&dvb->adapter, DRIVER_NAME, THIS_MODULE,
262 &dev->usbdev->dev, adapter_nr);
263 if (result < 0) {
264 printk(KERN_ERR "%s: dvb_register_adapter failed "
265 "(errno = %d)\n", DRIVER_NAME, result);
266 goto fail_adapter;
267 }
268 dvb->adapter.priv = dev;
269
270 /* register frontend */
271 result = dvb_register_frontend(&dvb->adapter, dvb->frontend);
272 if (result < 0) {
273 printk(KERN_ERR "%s: dvb_register_frontend failed "
274 "(errno = %d)\n", DRIVER_NAME, result);
275 goto fail_frontend;
276 }
277
278 /* register demux stuff */
279 dvb->demux.dmx.capabilities =
280 DMX_TS_FILTERING | DMX_SECTION_FILTERING |
281 DMX_MEMORY_BASED_FILTERING;
282 dvb->demux.priv = dev;
283 dvb->demux.filternum = 256;
284 dvb->demux.feednum = 256;
285 dvb->demux.start_feed = au0828_dvb_start_feed;
286 dvb->demux.stop_feed = au0828_dvb_stop_feed;
287 result = dvb_dmx_init(&dvb->demux);
288 if (result < 0) {
289 printk(KERN_ERR "%s: dvb_dmx_init failed (errno = %d)\n",
290 DRIVER_NAME, result);
291 goto fail_dmx;
292 }
293
294 dvb->dmxdev.filternum = 256;
295 dvb->dmxdev.demux = &dvb->demux.dmx;
296 dvb->dmxdev.capabilities = 0;
297 result = dvb_dmxdev_init(&dvb->dmxdev, &dvb->adapter);
298 if (result < 0) {
299 printk(KERN_ERR "%s: dvb_dmxdev_init failed (errno = %d)\n",
300 DRIVER_NAME, result);
301 goto fail_dmxdev;
302 }
303
304 dvb->fe_hw.source = DMX_FRONTEND_0;
305 result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_hw);
306 if (result < 0) {
307 printk(KERN_ERR "%s: add_frontend failed "
308 "(DMX_FRONTEND_0, errno = %d)\n", DRIVER_NAME, result);
309 goto fail_fe_hw;
310 }
311
312 dvb->fe_mem.source = DMX_MEMORY_FE;
313 result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_mem);
314 if (result < 0) {
315 printk(KERN_ERR "%s: add_frontend failed "
316 "(DMX_MEMORY_FE, errno = %d)\n", DRIVER_NAME, result);
317 goto fail_fe_mem;
318 }
319
320 result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw);
321 if (result < 0) {
322 printk(KERN_ERR "%s: connect_frontend failed (errno = %d)\n",
323 DRIVER_NAME, result);
324 goto fail_fe_conn;
325 }
326
327 /* register network adapter */
328 dvb_net_init(&dvb->adapter, &dvb->net, &dvb->demux.dmx);
329 return 0;
330
331fail_fe_conn:
332 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
333fail_fe_mem:
334 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
335fail_fe_hw:
336 dvb_dmxdev_release(&dvb->dmxdev);
337fail_dmxdev:
338 dvb_dmx_release(&dvb->demux);
339fail_dmx:
340 dvb_unregister_frontend(dvb->frontend);
341fail_frontend:
342 dvb_frontend_detach(dvb->frontend);
343 dvb_unregister_adapter(&dvb->adapter);
344fail_adapter:
345 return result;
346}
347
348void au0828_dvb_unregister(struct au0828_dev *dev)
349{
350 struct au0828_dvb *dvb = &dev->dvb;
351
352 dprintk(1, "%s()\n", __func__);
353
354 if (dvb->frontend == NULL)
355 return;
356
357 dvb_net_release(&dvb->net);
358 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
359 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
360 dvb_dmxdev_release(&dvb->dmxdev);
361 dvb_dmx_release(&dvb->demux);
362 dvb_unregister_frontend(dvb->frontend);
363 dvb_frontend_detach(dvb->frontend);
364 dvb_unregister_adapter(&dvb->adapter);
365}
366
367/* All the DVB attach calls go here, this function get's modified
368 * for each new card. No other function in this file needs
369 * to change.
370 */
371int au0828_dvb_register(struct au0828_dev *dev)
372{
373 struct au0828_dvb *dvb = &dev->dvb;
374 int ret;
375
376 dprintk(1, "%s()\n", __func__);
377
378 /* init frontend */
379 switch (dev->boardnr) {
380 case AU0828_BOARD_HAUPPAUGE_HVR850:
381 case AU0828_BOARD_HAUPPAUGE_HVR950Q:
382 dvb->frontend = dvb_attach(au8522_attach,
383 &hauppauge_hvr950q_config,
384 &dev->i2c_adap);
385 if (dvb->frontend != NULL)
386 dvb_attach(xc5000_attach, dvb->frontend, &dev->i2c_adap,
387 &hauppauge_hvr950q_tunerconfig);
388 break;
389 case AU0828_BOARD_HAUPPAUGE_HVR950Q_MXL:
390 dvb->frontend = dvb_attach(au8522_attach,
391 &hauppauge_hvr950q_config,
392 &dev->i2c_adap);
393 if (dvb->frontend != NULL)
394 dvb_attach(mxl5007t_attach, dvb->frontend,
395 &dev->i2c_adap, 0x60,
396 &mxl5007t_hvr950q_config);
397 break;
398 case AU0828_BOARD_HAUPPAUGE_WOODBURY:
399 dvb->frontend = dvb_attach(au8522_attach,
400 &hauppauge_woodbury_config,
401 &dev->i2c_adap);
402 if (dvb->frontend != NULL)
403 dvb_attach(tda18271_attach, dvb->frontend,
404 0x60, &dev->i2c_adap,
405 &hauppauge_woodbury_tunerconfig);
406 break;
407 case AU0828_BOARD_DVICO_FUSIONHDTV7:
408 dvb->frontend = dvb_attach(au8522_attach,
409 &fusionhdtv7usb_config,
410 &dev->i2c_adap);
411 if (dvb->frontend != NULL) {
412 dvb_attach(xc5000_attach, dvb->frontend,
413 &dev->i2c_adap,
414 &hauppauge_hvr950q_tunerconfig);
415 }
416 break;
417 default:
418 printk(KERN_WARNING "The frontend of your DVB/ATSC card "
419 "isn't supported yet\n");
420 break;
421 }
422 if (NULL == dvb->frontend) {
423 printk(KERN_ERR "%s() Frontend initialization failed\n",
424 __func__);
425 return -1;
426 }
427 /* define general-purpose callback pointer */
428 dvb->frontend->callback = au0828_tuner_callback;
429
430 /* register everything */
431 ret = dvb_register(dev);
432 if (ret < 0) {
433 if (dvb->frontend->ops.release)
434 dvb->frontend->ops.release(dvb->frontend);
435 return ret;
436 }
437
438 return 0;
439}
diff --git a/drivers/media/video/au0828/au0828-i2c.c b/drivers/media/video/au0828/au0828-i2c.c
new file mode 100644
index 00000000000..cbdb65c34f2
--- /dev/null
+++ b/drivers/media/video/au0828/au0828-i2c.c
@@ -0,0 +1,390 @@
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/module.h>
23#include <linux/moduleparam.h>
24#include <linux/init.h>
25#include <linux/delay.h>
26#include <linux/io.h>
27
28#include "au0828.h"
29
30#include <media/v4l2-common.h>
31
32static int i2c_scan;
33module_param(i2c_scan, int, 0444);
34MODULE_PARM_DESC(i2c_scan, "scan i2c bus at insmod time");
35
36#define I2C_WAIT_DELAY 512
37#define I2C_WAIT_RETRY 64
38
39static inline int i2c_slave_did_write_ack(struct i2c_adapter *i2c_adap)
40{
41 struct au0828_dev *dev = i2c_adap->algo_data;
42 return au0828_read(dev, AU0828_I2C_STATUS_201) &
43 AU0828_I2C_STATUS_NO_WRITE_ACK ? 0 : 1;
44}
45
46static inline int i2c_slave_did_read_ack(struct i2c_adapter *i2c_adap)
47{
48 struct au0828_dev *dev = i2c_adap->algo_data;
49 return au0828_read(dev, AU0828_I2C_STATUS_201) &
50 AU0828_I2C_STATUS_NO_READ_ACK ? 0 : 1;
51}
52
53static int i2c_wait_read_ack(struct i2c_adapter *i2c_adap)
54{
55 int count;
56
57 for (count = 0; count < I2C_WAIT_RETRY; count++) {
58 if (!i2c_slave_did_read_ack(i2c_adap))
59 break;
60 udelay(I2C_WAIT_DELAY);
61 }
62
63 if (I2C_WAIT_RETRY == count)
64 return 0;
65
66 return 1;
67}
68
69static inline int i2c_is_read_busy(struct i2c_adapter *i2c_adap)
70{
71 struct au0828_dev *dev = i2c_adap->algo_data;
72 return au0828_read(dev, AU0828_I2C_STATUS_201) &
73 AU0828_I2C_STATUS_READ_DONE ? 0 : 1;
74}
75
76static int i2c_wait_read_done(struct i2c_adapter *i2c_adap)
77{
78 int count;
79
80 for (count = 0; count < I2C_WAIT_RETRY; count++) {
81 if (!i2c_is_read_busy(i2c_adap))
82 break;
83 udelay(I2C_WAIT_DELAY);
84 }
85
86 if (I2C_WAIT_RETRY == count)
87 return 0;
88
89 return 1;
90}
91
92static inline int i2c_is_write_done(struct i2c_adapter *i2c_adap)
93{
94 struct au0828_dev *dev = i2c_adap->algo_data;
95 return au0828_read(dev, AU0828_I2C_STATUS_201) &
96 AU0828_I2C_STATUS_WRITE_DONE ? 1 : 0;
97}
98
99static int i2c_wait_write_done(struct i2c_adapter *i2c_adap)
100{
101 int count;
102
103 for (count = 0; count < I2C_WAIT_RETRY; count++) {
104 if (i2c_is_write_done(i2c_adap))
105 break;
106 udelay(I2C_WAIT_DELAY);
107 }
108
109 if (I2C_WAIT_RETRY == count)
110 return 0;
111
112 return 1;
113}
114
115static inline int i2c_is_busy(struct i2c_adapter *i2c_adap)
116{
117 struct au0828_dev *dev = i2c_adap->algo_data;
118 return au0828_read(dev, AU0828_I2C_STATUS_201) &
119 AU0828_I2C_STATUS_BUSY ? 1 : 0;
120}
121
122static int i2c_wait_done(struct i2c_adapter *i2c_adap)
123{
124 int count;
125
126 for (count = 0; count < I2C_WAIT_RETRY; count++) {
127 if (!i2c_is_busy(i2c_adap))
128 break;
129 udelay(I2C_WAIT_DELAY);
130 }
131
132 if (I2C_WAIT_RETRY == count)
133 return 0;
134
135 return 1;
136}
137
138/* FIXME: Implement join handling correctly */
139static int i2c_sendbytes(struct i2c_adapter *i2c_adap,
140 const struct i2c_msg *msg, int joined_rlen)
141{
142 int i, strobe = 0;
143 struct au0828_dev *dev = i2c_adap->algo_data;
144
145 dprintk(4, "%s()\n", __func__);
146
147 au0828_write(dev, AU0828_I2C_MULTIBYTE_MODE_2FF, 0x01);
148
149 /* Set the I2C clock */
150 au0828_write(dev, AU0828_I2C_CLK_DIVIDER_202,
151 dev->board.i2c_clk_divider);
152
153 /* Hardware needs 8 bit addresses */
154 au0828_write(dev, AU0828_I2C_DEST_ADDR_203, msg->addr << 1);
155
156 dprintk(4, "SEND: %02x\n", msg->addr);
157
158 /* Deal with i2c_scan */
159 if (msg->len == 0) {
160 /* The analog tuner detection code makes use of the SMBUS_QUICK
161 message (which involves a zero length i2c write). To avoid
162 checking the status register when we didn't strobe out any
163 actual bytes to the bus, just do a read check. This is
164 consistent with how I saw i2c device checking done in the
165 USB trace of the Windows driver */
166 au0828_write(dev, AU0828_I2C_TRIGGER_200,
167 AU0828_I2C_TRIGGER_READ);
168
169 if (!i2c_wait_done(i2c_adap))
170 return -EIO;
171
172 if (i2c_wait_read_ack(i2c_adap))
173 return -EIO;
174
175 return 0;
176 }
177
178 for (i = 0; i < msg->len;) {
179
180 dprintk(4, " %02x\n", msg->buf[i]);
181
182 au0828_write(dev, AU0828_I2C_WRITE_FIFO_205, msg->buf[i]);
183
184 strobe++;
185 i++;
186
187 if ((strobe >= 4) || (i >= msg->len)) {
188
189 /* Strobe the byte into the bus */
190 if (i < msg->len)
191 au0828_write(dev, AU0828_I2C_TRIGGER_200,
192 AU0828_I2C_TRIGGER_WRITE |
193 AU0828_I2C_TRIGGER_HOLD);
194 else
195 au0828_write(dev, AU0828_I2C_TRIGGER_200,
196 AU0828_I2C_TRIGGER_WRITE);
197
198 /* Reset strobe trigger */
199 strobe = 0;
200
201 if (!i2c_wait_write_done(i2c_adap))
202 return -EIO;
203
204 }
205
206 }
207 if (!i2c_wait_done(i2c_adap))
208 return -EIO;
209
210 dprintk(4, "\n");
211
212 return msg->len;
213}
214
215/* FIXME: Implement join handling correctly */
216static int i2c_readbytes(struct i2c_adapter *i2c_adap,
217 const struct i2c_msg *msg, int joined)
218{
219 struct au0828_dev *dev = i2c_adap->algo_data;
220 int i;
221
222 dprintk(4, "%s()\n", __func__);
223
224 au0828_write(dev, AU0828_I2C_MULTIBYTE_MODE_2FF, 0x01);
225
226 /* Set the I2C clock */
227 au0828_write(dev, AU0828_I2C_CLK_DIVIDER_202,
228 dev->board.i2c_clk_divider);
229
230 /* Hardware needs 8 bit addresses */
231 au0828_write(dev, AU0828_I2C_DEST_ADDR_203, msg->addr << 1);
232
233 dprintk(4, " RECV:\n");
234
235 /* Deal with i2c_scan */
236 if (msg->len == 0) {
237 au0828_write(dev, AU0828_I2C_TRIGGER_200,
238 AU0828_I2C_TRIGGER_READ);
239
240 if (i2c_wait_read_ack(i2c_adap))
241 return -EIO;
242 return 0;
243 }
244
245 for (i = 0; i < msg->len;) {
246
247 i++;
248
249 if (i < msg->len)
250 au0828_write(dev, AU0828_I2C_TRIGGER_200,
251 AU0828_I2C_TRIGGER_READ |
252 AU0828_I2C_TRIGGER_HOLD);
253 else
254 au0828_write(dev, AU0828_I2C_TRIGGER_200,
255 AU0828_I2C_TRIGGER_READ);
256
257 if (!i2c_wait_read_done(i2c_adap))
258 return -EIO;
259
260 msg->buf[i-1] = au0828_read(dev, AU0828_I2C_READ_FIFO_209) &
261 0xff;
262
263 dprintk(4, " %02x\n", msg->buf[i-1]);
264 }
265 if (!i2c_wait_done(i2c_adap))
266 return -EIO;
267
268 dprintk(4, "\n");
269
270 return msg->len;
271}
272
273static int i2c_xfer(struct i2c_adapter *i2c_adap,
274 struct i2c_msg *msgs, int num)
275{
276 int i, retval = 0;
277
278 dprintk(4, "%s(num = %d)\n", __func__, num);
279
280 for (i = 0; i < num; i++) {
281 dprintk(4, "%s(num = %d) addr = 0x%02x len = 0x%x\n",
282 __func__, num, msgs[i].addr, msgs[i].len);
283 if (msgs[i].flags & I2C_M_RD) {
284 /* read */
285 retval = i2c_readbytes(i2c_adap, &msgs[i], 0);
286 } else if (i + 1 < num && (msgs[i + 1].flags & I2C_M_RD) &&
287 msgs[i].addr == msgs[i + 1].addr) {
288 /* write then read from same address */
289 retval = i2c_sendbytes(i2c_adap, &msgs[i],
290 msgs[i + 1].len);
291 if (retval < 0)
292 goto err;
293 i++;
294 retval = i2c_readbytes(i2c_adap, &msgs[i], 1);
295 } else {
296 /* write */
297 retval = i2c_sendbytes(i2c_adap, &msgs[i], 0);
298 }
299 if (retval < 0)
300 goto err;
301 }
302 return num;
303
304err:
305 return retval;
306}
307
308static u32 au0828_functionality(struct i2c_adapter *adap)
309{
310 return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_I2C;
311}
312
313static struct i2c_algorithm au0828_i2c_algo_template = {
314 .master_xfer = i2c_xfer,
315 .functionality = au0828_functionality,
316};
317
318/* ----------------------------------------------------------------------- */
319
320static struct i2c_adapter au0828_i2c_adap_template = {
321 .name = DRIVER_NAME,
322 .owner = THIS_MODULE,
323 .algo = &au0828_i2c_algo_template,
324};
325
326static struct i2c_client au0828_i2c_client_template = {
327 .name = "au0828 internal",
328};
329
330static char *i2c_devs[128] = {
331 [0x8e >> 1] = "au8522",
332 [0xa0 >> 1] = "eeprom",
333 [0xc2 >> 1] = "tuner/xc5000",
334};
335
336static void do_i2c_scan(char *name, struct i2c_client *c)
337{
338 unsigned char buf;
339 int i, rc;
340
341 for (i = 0; i < 128; i++) {
342 c->addr = i;
343 rc = i2c_master_recv(c, &buf, 0);
344 if (rc < 0)
345 continue;
346 printk(KERN_INFO "%s: i2c scan: found device @ 0x%x [%s]\n",
347 name, i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
348 }
349}
350
351/* init + register i2c algo-bit adapter */
352int au0828_i2c_register(struct au0828_dev *dev)
353{
354 dprintk(1, "%s()\n", __func__);
355
356 memcpy(&dev->i2c_adap, &au0828_i2c_adap_template,
357 sizeof(dev->i2c_adap));
358 memcpy(&dev->i2c_algo, &au0828_i2c_algo_template,
359 sizeof(dev->i2c_algo));
360 memcpy(&dev->i2c_client, &au0828_i2c_client_template,
361 sizeof(dev->i2c_client));
362
363 dev->i2c_adap.dev.parent = &dev->usbdev->dev;
364
365 strlcpy(dev->i2c_adap.name, DRIVER_NAME,
366 sizeof(dev->i2c_adap.name));
367
368 dev->i2c_adap.algo = &dev->i2c_algo;
369 dev->i2c_adap.algo_data = dev;
370 i2c_set_adapdata(&dev->i2c_adap, &dev->v4l2_dev);
371 i2c_add_adapter(&dev->i2c_adap);
372
373 dev->i2c_client.adapter = &dev->i2c_adap;
374
375 if (0 == dev->i2c_rc) {
376 printk(KERN_INFO "%s: i2c bus registered\n", DRIVER_NAME);
377 if (i2c_scan)
378 do_i2c_scan(DRIVER_NAME, &dev->i2c_client);
379 } else
380 printk(KERN_INFO "%s: i2c bus register FAILED\n", DRIVER_NAME);
381
382 return dev->i2c_rc;
383}
384
385int au0828_i2c_unregister(struct au0828_dev *dev)
386{
387 i2c_del_adapter(&dev->i2c_adap);
388 return 0;
389}
390
diff --git a/drivers/media/video/au0828/au0828-reg.h b/drivers/media/video/au0828/au0828-reg.h
new file mode 100644
index 00000000000..c39f3d2b721
--- /dev/null
+++ b/drivers/media/video/au0828/au0828-reg.h
@@ -0,0 +1,65 @@
1/*
2 * Driver for the Auvitek 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/* We'll start to rename these registers once we have a better
23 * understanding of their meaning.
24 */
25#define REG_000 0x000
26#define REG_001 0x001
27#define REG_002 0x002
28#define REG_003 0x003
29
30#define AU0828_SENSORCTRL_100 0x100
31#define AU0828_SENSORCTRL_VBI_103 0x103
32
33/* I2C registers */
34#define AU0828_I2C_TRIGGER_200 0x200
35#define AU0828_I2C_STATUS_201 0x201
36#define AU0828_I2C_CLK_DIVIDER_202 0x202
37#define AU0828_I2C_DEST_ADDR_203 0x203
38#define AU0828_I2C_WRITE_FIFO_205 0x205
39#define AU0828_I2C_READ_FIFO_209 0x209
40#define AU0828_I2C_MULTIBYTE_MODE_2FF 0x2ff
41
42/* Audio registers */
43#define AU0828_AUDIOCTRL_50C 0x50C
44
45#define REG_600 0x600
46
47/*********************************************************************/
48/* Here are constants for values associated with the above registers */
49
50/* I2C Trigger (Reg 0x200) */
51#define AU0828_I2C_TRIGGER_WRITE 0x01
52#define AU0828_I2C_TRIGGER_READ 0x20
53#define AU0828_I2C_TRIGGER_HOLD 0x40
54
55/* I2C Status (Reg 0x201) */
56#define AU0828_I2C_STATUS_READ_DONE 0x01
57#define AU0828_I2C_STATUS_NO_READ_ACK 0x02
58#define AU0828_I2C_STATUS_WRITE_DONE 0x04
59#define AU0828_I2C_STATUS_NO_WRITE_ACK 0x08
60#define AU0828_I2C_STATUS_BUSY 0x10
61
62/* I2C Clock Divider (Reg 0x202) */
63#define AU0828_I2C_CLK_250KHZ 0x07
64#define AU0828_I2C_CLK_100KHZ 0x14
65#define AU0828_I2C_CLK_30KHZ 0x40
diff --git a/drivers/media/video/au0828/au0828-vbi.c b/drivers/media/video/au0828/au0828-vbi.c
new file mode 100644
index 00000000000..63f593070ee
--- /dev/null
+++ b/drivers/media/video/au0828/au0828-vbi.c
@@ -0,0 +1,138 @@
1/*
2 au0828-vbi.c - VBI driver for au0828
3
4 Copyright (C) 2010 Devin Heitmueller <dheitmueller@kernellabs.com>
5
6 This work was sponsored by GetWellNetwork Inc.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA.
22 */
23
24#include <linux/kernel.h>
25#include <linux/module.h>
26#include <linux/init.h>
27#include <linux/slab.h>
28
29#include "au0828.h"
30
31static unsigned int vbibufs = 5;
32module_param(vbibufs, int, 0644);
33MODULE_PARM_DESC(vbibufs, "number of vbi buffers, range 2-32");
34
35/* ------------------------------------------------------------------ */
36
37static void
38free_buffer(struct videobuf_queue *vq, struct au0828_buffer *buf)
39{
40 struct au0828_fh *fh = vq->priv_data;
41 struct au0828_dev *dev = fh->dev;
42 unsigned long flags = 0;
43 if (in_interrupt())
44 BUG();
45
46 /* We used to wait for the buffer to finish here, but this didn't work
47 because, as we were keeping the state as VIDEOBUF_QUEUED,
48 videobuf_queue_cancel marked it as finished for us.
49 (Also, it could wedge forever if the hardware was misconfigured.)
50
51 This should be safe; by the time we get here, the buffer isn't
52 queued anymore. If we ever start marking the buffers as
53 VIDEOBUF_ACTIVE, it won't be, though.
54 */
55 spin_lock_irqsave(&dev->slock, flags);
56 if (dev->isoc_ctl.vbi_buf == buf)
57 dev->isoc_ctl.vbi_buf = NULL;
58 spin_unlock_irqrestore(&dev->slock, flags);
59
60 videobuf_vmalloc_free(&buf->vb);
61 buf->vb.state = VIDEOBUF_NEEDS_INIT;
62}
63
64static int
65vbi_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
66{
67 struct au0828_fh *fh = q->priv_data;
68 struct au0828_dev *dev = fh->dev;
69
70 *size = dev->vbi_width * dev->vbi_height * 2;
71
72 if (0 == *count)
73 *count = vbibufs;
74 if (*count < 2)
75 *count = 2;
76 if (*count > 32)
77 *count = 32;
78 return 0;
79}
80
81static int
82vbi_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
83 enum v4l2_field field)
84{
85 struct au0828_fh *fh = q->priv_data;
86 struct au0828_dev *dev = fh->dev;
87 struct au0828_buffer *buf = container_of(vb, struct au0828_buffer, vb);
88 int rc = 0;
89
90 buf->vb.size = dev->vbi_width * dev->vbi_height * 2;
91
92 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
93 return -EINVAL;
94
95 buf->vb.width = dev->vbi_width;
96 buf->vb.height = dev->vbi_height;
97 buf->vb.field = field;
98
99 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
100 rc = videobuf_iolock(q, &buf->vb, NULL);
101 if (rc < 0)
102 goto fail;
103 }
104
105 buf->vb.state = VIDEOBUF_PREPARED;
106 return 0;
107
108fail:
109 free_buffer(q, buf);
110 return rc;
111}
112
113static void
114vbi_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
115{
116 struct au0828_buffer *buf = container_of(vb,
117 struct au0828_buffer,
118 vb);
119 struct au0828_fh *fh = vq->priv_data;
120 struct au0828_dev *dev = fh->dev;
121 struct au0828_dmaqueue *vbiq = &dev->vbiq;
122
123 buf->vb.state = VIDEOBUF_QUEUED;
124 list_add_tail(&buf->vb.queue, &vbiq->active);
125}
126
127static void vbi_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
128{
129 struct au0828_buffer *buf = container_of(vb, struct au0828_buffer, vb);
130 free_buffer(q, buf);
131}
132
133struct videobuf_queue_ops au0828_vbi_qops = {
134 .buf_setup = vbi_setup,
135 .buf_prepare = vbi_prepare,
136 .buf_queue = vbi_queue,
137 .buf_release = vbi_release,
138};
diff --git a/drivers/media/video/au0828/au0828-video.c b/drivers/media/video/au0828/au0828-video.c
new file mode 100644
index 00000000000..0b3e481ffe8
--- /dev/null
+++ b/drivers/media/video/au0828/au0828-video.c
@@ -0,0 +1,1993 @@
1/*
2 * Auvitek AU0828 USB Bridge (Analog video support)
3 *
4 * Copyright (C) 2009 Devin Heitmueller <dheitmueller@linuxtv.org>
5 * Copyright (C) 2005-2008 Auvitek International, Ltd.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * As published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
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., 51 Franklin Street, Fifth Floor, Boston, MA
20 * 02110-1301, USA.
21 */
22
23/* Developer Notes:
24 *
25 * VBI support is not yet working
26 * The hardware scaler supported is unimplemented
27 * AC97 audio support is unimplemented (only i2s audio mode)
28 *
29 */
30
31#include <linux/module.h>
32#include <linux/slab.h>
33#include <linux/init.h>
34#include <linux/device.h>
35#include <linux/suspend.h>
36#include <media/v4l2-common.h>
37#include <media/v4l2-ioctl.h>
38#include <media/v4l2-chip-ident.h>
39#include <media/tuner.h>
40#include "au0828.h"
41#include "au0828-reg.h"
42
43static DEFINE_MUTEX(au0828_sysfs_lock);
44
45/* ------------------------------------------------------------------
46 Videobuf operations
47 ------------------------------------------------------------------*/
48
49static unsigned int isoc_debug;
50module_param(isoc_debug, int, 0644);
51MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
52
53#define au0828_isocdbg(fmt, arg...) \
54do {\
55 if (isoc_debug) { \
56 printk(KERN_INFO "au0828 %s :"fmt, \
57 __func__ , ##arg); \
58 } \
59 } while (0)
60
61static inline void print_err_status(struct au0828_dev *dev,
62 int packet, int status)
63{
64 char *errmsg = "Unknown";
65
66 switch (status) {
67 case -ENOENT:
68 errmsg = "unlinked synchronuously";
69 break;
70 case -ECONNRESET:
71 errmsg = "unlinked asynchronuously";
72 break;
73 case -ENOSR:
74 errmsg = "Buffer error (overrun)";
75 break;
76 case -EPIPE:
77 errmsg = "Stalled (device not responding)";
78 break;
79 case -EOVERFLOW:
80 errmsg = "Babble (bad cable?)";
81 break;
82 case -EPROTO:
83 errmsg = "Bit-stuff error (bad cable?)";
84 break;
85 case -EILSEQ:
86 errmsg = "CRC/Timeout (could be anything)";
87 break;
88 case -ETIME:
89 errmsg = "Device does not respond";
90 break;
91 }
92 if (packet < 0) {
93 au0828_isocdbg("URB status %d [%s].\n", status, errmsg);
94 } else {
95 au0828_isocdbg("URB packet %d, status %d [%s].\n",
96 packet, status, errmsg);
97 }
98}
99
100static int check_dev(struct au0828_dev *dev)
101{
102 if (dev->dev_state & DEV_DISCONNECTED) {
103 printk(KERN_INFO "v4l2 ioctl: device not present\n");
104 return -ENODEV;
105 }
106
107 if (dev->dev_state & DEV_MISCONFIGURED) {
108 printk(KERN_INFO "v4l2 ioctl: device is misconfigured; "
109 "close and open it again\n");
110 return -EIO;
111 }
112 return 0;
113}
114
115/*
116 * IRQ callback, called by URB callback
117 */
118static void au0828_irq_callback(struct urb *urb)
119{
120 struct au0828_dmaqueue *dma_q = urb->context;
121 struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vidq);
122 unsigned long flags = 0;
123 int rc, i;
124
125 switch (urb->status) {
126 case 0: /* success */
127 case -ETIMEDOUT: /* NAK */
128 break;
129 case -ECONNRESET: /* kill */
130 case -ENOENT:
131 case -ESHUTDOWN:
132 au0828_isocdbg("au0828_irq_callback called: status kill\n");
133 return;
134 default: /* unknown error */
135 au0828_isocdbg("urb completition error %d.\n", urb->status);
136 break;
137 }
138
139 /* Copy data from URB */
140 spin_lock_irqsave(&dev->slock, flags);
141 rc = dev->isoc_ctl.isoc_copy(dev, urb);
142 spin_unlock_irqrestore(&dev->slock, flags);
143
144 /* Reset urb buffers */
145 for (i = 0; i < urb->number_of_packets; i++) {
146 urb->iso_frame_desc[i].status = 0;
147 urb->iso_frame_desc[i].actual_length = 0;
148 }
149 urb->status = 0;
150
151 urb->status = usb_submit_urb(urb, GFP_ATOMIC);
152 if (urb->status) {
153 au0828_isocdbg("urb resubmit failed (error=%i)\n",
154 urb->status);
155 }
156}
157
158/*
159 * Stop and Deallocate URBs
160 */
161void au0828_uninit_isoc(struct au0828_dev *dev)
162{
163 struct urb *urb;
164 int i;
165
166 au0828_isocdbg("au0828: called au0828_uninit_isoc\n");
167
168 dev->isoc_ctl.nfields = -1;
169 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
170 urb = dev->isoc_ctl.urb[i];
171 if (urb) {
172 if (!irqs_disabled())
173 usb_kill_urb(urb);
174 else
175 usb_unlink_urb(urb);
176
177 if (dev->isoc_ctl.transfer_buffer[i]) {
178 usb_free_coherent(dev->usbdev,
179 urb->transfer_buffer_length,
180 dev->isoc_ctl.transfer_buffer[i],
181 urb->transfer_dma);
182 }
183 usb_free_urb(urb);
184 dev->isoc_ctl.urb[i] = NULL;
185 }
186 dev->isoc_ctl.transfer_buffer[i] = NULL;
187 }
188
189 kfree(dev->isoc_ctl.urb);
190 kfree(dev->isoc_ctl.transfer_buffer);
191
192 dev->isoc_ctl.urb = NULL;
193 dev->isoc_ctl.transfer_buffer = NULL;
194 dev->isoc_ctl.num_bufs = 0;
195}
196
197/*
198 * Allocate URBs and start IRQ
199 */
200int au0828_init_isoc(struct au0828_dev *dev, int max_packets,
201 int num_bufs, int max_pkt_size,
202 int (*isoc_copy) (struct au0828_dev *dev, struct urb *urb))
203{
204 struct au0828_dmaqueue *dma_q = &dev->vidq;
205 int i;
206 int sb_size, pipe;
207 struct urb *urb;
208 int j, k;
209 int rc;
210
211 au0828_isocdbg("au0828: called au0828_prepare_isoc\n");
212
213 /* De-allocates all pending stuff */
214 au0828_uninit_isoc(dev);
215
216 dev->isoc_ctl.isoc_copy = isoc_copy;
217 dev->isoc_ctl.num_bufs = num_bufs;
218
219 dev->isoc_ctl.urb = kzalloc(sizeof(void *)*num_bufs, GFP_KERNEL);
220 if (!dev->isoc_ctl.urb) {
221 au0828_isocdbg("cannot alloc memory for usb buffers\n");
222 return -ENOMEM;
223 }
224
225 dev->isoc_ctl.transfer_buffer = kzalloc(sizeof(void *)*num_bufs,
226 GFP_KERNEL);
227 if (!dev->isoc_ctl.transfer_buffer) {
228 au0828_isocdbg("cannot allocate memory for usb transfer\n");
229 kfree(dev->isoc_ctl.urb);
230 return -ENOMEM;
231 }
232
233 dev->isoc_ctl.max_pkt_size = max_pkt_size;
234 dev->isoc_ctl.buf = NULL;
235
236 sb_size = max_packets * dev->isoc_ctl.max_pkt_size;
237
238 /* allocate urbs and transfer buffers */
239 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
240 urb = usb_alloc_urb(max_packets, GFP_KERNEL);
241 if (!urb) {
242 au0828_isocdbg("cannot alloc isoc_ctl.urb %i\n", i);
243 au0828_uninit_isoc(dev);
244 return -ENOMEM;
245 }
246 dev->isoc_ctl.urb[i] = urb;
247
248 dev->isoc_ctl.transfer_buffer[i] = usb_alloc_coherent(dev->usbdev,
249 sb_size, GFP_KERNEL, &urb->transfer_dma);
250 if (!dev->isoc_ctl.transfer_buffer[i]) {
251 printk("unable to allocate %i bytes for transfer"
252 " buffer %i%s\n",
253 sb_size, i,
254 in_interrupt() ? " while in int" : "");
255 au0828_uninit_isoc(dev);
256 return -ENOMEM;
257 }
258 memset(dev->isoc_ctl.transfer_buffer[i], 0, sb_size);
259
260 pipe = usb_rcvisocpipe(dev->usbdev,
261 dev->isoc_in_endpointaddr),
262
263 usb_fill_int_urb(urb, dev->usbdev, pipe,
264 dev->isoc_ctl.transfer_buffer[i], sb_size,
265 au0828_irq_callback, dma_q, 1);
266
267 urb->number_of_packets = max_packets;
268 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
269
270 k = 0;
271 for (j = 0; j < max_packets; j++) {
272 urb->iso_frame_desc[j].offset = k;
273 urb->iso_frame_desc[j].length =
274 dev->isoc_ctl.max_pkt_size;
275 k += dev->isoc_ctl.max_pkt_size;
276 }
277 }
278
279 init_waitqueue_head(&dma_q->wq);
280
281 /* submit urbs and enables IRQ */
282 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
283 rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
284 if (rc) {
285 au0828_isocdbg("submit of urb %i failed (error=%i)\n",
286 i, rc);
287 au0828_uninit_isoc(dev);
288 return rc;
289 }
290 }
291
292 return 0;
293}
294
295/*
296 * Announces that a buffer were filled and request the next
297 */
298static inline void buffer_filled(struct au0828_dev *dev,
299 struct au0828_dmaqueue *dma_q,
300 struct au0828_buffer *buf)
301{
302 /* Advice that buffer was filled */
303 au0828_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
304
305 buf->vb.state = VIDEOBUF_DONE;
306 buf->vb.field_count++;
307 do_gettimeofday(&buf->vb.ts);
308
309 dev->isoc_ctl.buf = NULL;
310
311 list_del(&buf->vb.queue);
312 wake_up(&buf->vb.done);
313}
314
315static inline void vbi_buffer_filled(struct au0828_dev *dev,
316 struct au0828_dmaqueue *dma_q,
317 struct au0828_buffer *buf)
318{
319 /* Advice that buffer was filled */
320 au0828_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
321
322 buf->vb.state = VIDEOBUF_DONE;
323 buf->vb.field_count++;
324 do_gettimeofday(&buf->vb.ts);
325
326 dev->isoc_ctl.vbi_buf = NULL;
327
328 list_del(&buf->vb.queue);
329 wake_up(&buf->vb.done);
330}
331
332/*
333 * Identify the buffer header type and properly handles
334 */
335static void au0828_copy_video(struct au0828_dev *dev,
336 struct au0828_dmaqueue *dma_q,
337 struct au0828_buffer *buf,
338 unsigned char *p,
339 unsigned char *outp, unsigned long len)
340{
341 void *fieldstart, *startwrite, *startread;
342 int linesdone, currlinedone, offset, lencopy, remain;
343 int bytesperline = dev->width << 1; /* Assumes 16-bit depth @@@@ */
344
345 if (len == 0)
346 return;
347
348 if (dma_q->pos + len > buf->vb.size)
349 len = buf->vb.size - dma_q->pos;
350
351 startread = p;
352 remain = len;
353
354 /* Interlaces frame */
355 if (buf->top_field)
356 fieldstart = outp;
357 else
358 fieldstart = outp + bytesperline;
359
360 linesdone = dma_q->pos / bytesperline;
361 currlinedone = dma_q->pos % bytesperline;
362 offset = linesdone * bytesperline * 2 + currlinedone;
363 startwrite = fieldstart + offset;
364 lencopy = bytesperline - currlinedone;
365 lencopy = lencopy > remain ? remain : lencopy;
366
367 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
368 au0828_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
369 ((char *)startwrite + lencopy) -
370 ((char *)outp + buf->vb.size));
371 remain = (char *)outp + buf->vb.size - (char *)startwrite;
372 lencopy = remain;
373 }
374 if (lencopy <= 0)
375 return;
376 memcpy(startwrite, startread, lencopy);
377
378 remain -= lencopy;
379
380 while (remain > 0) {
381 startwrite += lencopy + bytesperline;
382 startread += lencopy;
383 if (bytesperline > remain)
384 lencopy = remain;
385 else
386 lencopy = bytesperline;
387
388 if ((char *)startwrite + lencopy > (char *)outp +
389 buf->vb.size) {
390 au0828_isocdbg("Overflow %zi bytes past buf end (2)\n",
391 ((char *)startwrite + lencopy) -
392 ((char *)outp + buf->vb.size));
393 lencopy = remain = (char *)outp + buf->vb.size -
394 (char *)startwrite;
395 }
396 if (lencopy <= 0)
397 break;
398
399 memcpy(startwrite, startread, lencopy);
400
401 remain -= lencopy;
402 }
403
404 if (offset > 1440) {
405 /* We have enough data to check for greenscreen */
406 if (outp[0] < 0x60 && outp[1440] < 0x60)
407 dev->greenscreen_detected = 1;
408 }
409
410 dma_q->pos += len;
411}
412
413/*
414 * video-buf generic routine to get the next available buffer
415 */
416static inline void get_next_buf(struct au0828_dmaqueue *dma_q,
417 struct au0828_buffer **buf)
418{
419 struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vidq);
420
421 if (list_empty(&dma_q->active)) {
422 au0828_isocdbg("No active queue to serve\n");
423 dev->isoc_ctl.buf = NULL;
424 *buf = NULL;
425 return;
426 }
427
428 /* Get the next buffer */
429 *buf = list_entry(dma_q->active.next, struct au0828_buffer, vb.queue);
430 dev->isoc_ctl.buf = *buf;
431
432 return;
433}
434
435static void au0828_copy_vbi(struct au0828_dev *dev,
436 struct au0828_dmaqueue *dma_q,
437 struct au0828_buffer *buf,
438 unsigned char *p,
439 unsigned char *outp, unsigned long len)
440{
441 unsigned char *startwrite, *startread;
442 int bytesperline;
443 int i, j = 0;
444
445 if (dev == NULL) {
446 au0828_isocdbg("dev is null\n");
447 return;
448 }
449
450 if (dma_q == NULL) {
451 au0828_isocdbg("dma_q is null\n");
452 return;
453 }
454 if (buf == NULL)
455 return;
456 if (p == NULL) {
457 au0828_isocdbg("p is null\n");
458 return;
459 }
460 if (outp == NULL) {
461 au0828_isocdbg("outp is null\n");
462 return;
463 }
464
465 bytesperline = dev->vbi_width;
466
467 if (dma_q->pos + len > buf->vb.size)
468 len = buf->vb.size - dma_q->pos;
469
470 startread = p;
471 startwrite = outp + (dma_q->pos / 2);
472
473 /* Make sure the bottom field populates the second half of the frame */
474 if (buf->top_field == 0)
475 startwrite += bytesperline * dev->vbi_height;
476
477 for (i = 0; i < len; i += 2)
478 startwrite[j++] = startread[i+1];
479
480 dma_q->pos += len;
481}
482
483
484/*
485 * video-buf generic routine to get the next available VBI buffer
486 */
487static inline void vbi_get_next_buf(struct au0828_dmaqueue *dma_q,
488 struct au0828_buffer **buf)
489{
490 struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vbiq);
491 char *outp;
492
493 if (list_empty(&dma_q->active)) {
494 au0828_isocdbg("No active queue to serve\n");
495 dev->isoc_ctl.vbi_buf = NULL;
496 *buf = NULL;
497 return;
498 }
499
500 /* Get the next buffer */
501 *buf = list_entry(dma_q->active.next, struct au0828_buffer, vb.queue);
502 /* Cleans up buffer - Useful for testing for frame/URB loss */
503 outp = videobuf_to_vmalloc(&(*buf)->vb);
504 memset(outp, 0x00, (*buf)->vb.size);
505
506 dev->isoc_ctl.vbi_buf = *buf;
507
508 return;
509}
510
511/*
512 * Controls the isoc copy of each urb packet
513 */
514static inline int au0828_isoc_copy(struct au0828_dev *dev, struct urb *urb)
515{
516 struct au0828_buffer *buf;
517 struct au0828_buffer *vbi_buf;
518 struct au0828_dmaqueue *dma_q = urb->context;
519 struct au0828_dmaqueue *vbi_dma_q = &dev->vbiq;
520 unsigned char *outp = NULL;
521 unsigned char *vbioutp = NULL;
522 int i, len = 0, rc = 1;
523 unsigned char *p;
524 unsigned char fbyte;
525 unsigned int vbi_field_size;
526 unsigned int remain, lencopy;
527
528 if (!dev)
529 return 0;
530
531 if ((dev->dev_state & DEV_DISCONNECTED) ||
532 (dev->dev_state & DEV_MISCONFIGURED))
533 return 0;
534
535 if (urb->status < 0) {
536 print_err_status(dev, -1, urb->status);
537 if (urb->status == -ENOENT)
538 return 0;
539 }
540
541 buf = dev->isoc_ctl.buf;
542 if (buf != NULL)
543 outp = videobuf_to_vmalloc(&buf->vb);
544
545 vbi_buf = dev->isoc_ctl.vbi_buf;
546 if (vbi_buf != NULL)
547 vbioutp = videobuf_to_vmalloc(&vbi_buf->vb);
548
549 for (i = 0; i < urb->number_of_packets; i++) {
550 int status = urb->iso_frame_desc[i].status;
551
552 if (status < 0) {
553 print_err_status(dev, i, status);
554 if (urb->iso_frame_desc[i].status != -EPROTO)
555 continue;
556 }
557
558 if (urb->iso_frame_desc[i].actual_length <= 0)
559 continue;
560
561 if (urb->iso_frame_desc[i].actual_length >
562 dev->max_pkt_size) {
563 au0828_isocdbg("packet bigger than packet size");
564 continue;
565 }
566
567 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
568 fbyte = p[0];
569 len = urb->iso_frame_desc[i].actual_length - 4;
570 p += 4;
571
572 if (fbyte & 0x80) {
573 len -= 4;
574 p += 4;
575 au0828_isocdbg("Video frame %s\n",
576 (fbyte & 0x40) ? "odd" : "even");
577 if (fbyte & 0x40) {
578 /* VBI */
579 if (vbi_buf != NULL)
580 vbi_buffer_filled(dev,
581 vbi_dma_q,
582 vbi_buf);
583 vbi_get_next_buf(vbi_dma_q, &vbi_buf);
584 if (vbi_buf == NULL)
585 vbioutp = NULL;
586 else
587 vbioutp = videobuf_to_vmalloc(
588 &vbi_buf->vb);
589
590 /* Video */
591 if (buf != NULL)
592 buffer_filled(dev, dma_q, buf);
593 get_next_buf(dma_q, &buf);
594 if (buf == NULL)
595 outp = NULL;
596 else
597 outp = videobuf_to_vmalloc(&buf->vb);
598
599 /* As long as isoc traffic is arriving, keep
600 resetting the timer */
601 if (dev->vid_timeout_running)
602 mod_timer(&dev->vid_timeout,
603 jiffies + (HZ / 10));
604 if (dev->vbi_timeout_running)
605 mod_timer(&dev->vbi_timeout,
606 jiffies + (HZ / 10));
607 }
608
609 if (buf != NULL) {
610 if (fbyte & 0x40)
611 buf->top_field = 1;
612 else
613 buf->top_field = 0;
614 }
615
616 if (vbi_buf != NULL) {
617 if (fbyte & 0x40)
618 vbi_buf->top_field = 1;
619 else
620 vbi_buf->top_field = 0;
621 }
622
623 dev->vbi_read = 0;
624 vbi_dma_q->pos = 0;
625 dma_q->pos = 0;
626 }
627
628 vbi_field_size = dev->vbi_width * dev->vbi_height * 2;
629 if (dev->vbi_read < vbi_field_size) {
630 remain = vbi_field_size - dev->vbi_read;
631 if (len < remain)
632 lencopy = len;
633 else
634 lencopy = remain;
635
636 if (vbi_buf != NULL)
637 au0828_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
638 vbioutp, len);
639
640 len -= lencopy;
641 p += lencopy;
642 dev->vbi_read += lencopy;
643 }
644
645 if (dev->vbi_read >= vbi_field_size && buf != NULL)
646 au0828_copy_video(dev, dma_q, buf, p, outp, len);
647 }
648 return rc;
649}
650
651static int
652buffer_setup(struct videobuf_queue *vq, unsigned int *count,
653 unsigned int *size)
654{
655 struct au0828_fh *fh = vq->priv_data;
656 *size = (fh->dev->width * fh->dev->height * 16 + 7) >> 3;
657
658 if (0 == *count)
659 *count = AU0828_DEF_BUF;
660
661 if (*count < AU0828_MIN_BUF)
662 *count = AU0828_MIN_BUF;
663 return 0;
664}
665
666/* This is called *without* dev->slock held; please keep it that way */
667static void free_buffer(struct videobuf_queue *vq, struct au0828_buffer *buf)
668{
669 struct au0828_fh *fh = vq->priv_data;
670 struct au0828_dev *dev = fh->dev;
671 unsigned long flags = 0;
672 if (in_interrupt())
673 BUG();
674
675 /* We used to wait for the buffer to finish here, but this didn't work
676 because, as we were keeping the state as VIDEOBUF_QUEUED,
677 videobuf_queue_cancel marked it as finished for us.
678 (Also, it could wedge forever if the hardware was misconfigured.)
679
680 This should be safe; by the time we get here, the buffer isn't
681 queued anymore. If we ever start marking the buffers as
682 VIDEOBUF_ACTIVE, it won't be, though.
683 */
684 spin_lock_irqsave(&dev->slock, flags);
685 if (dev->isoc_ctl.buf == buf)
686 dev->isoc_ctl.buf = NULL;
687 spin_unlock_irqrestore(&dev->slock, flags);
688
689 videobuf_vmalloc_free(&buf->vb);
690 buf->vb.state = VIDEOBUF_NEEDS_INIT;
691}
692
693static int
694buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
695 enum v4l2_field field)
696{
697 struct au0828_fh *fh = vq->priv_data;
698 struct au0828_buffer *buf = container_of(vb, struct au0828_buffer, vb);
699 struct au0828_dev *dev = fh->dev;
700 int rc = 0, urb_init = 0;
701
702 buf->vb.size = (fh->dev->width * fh->dev->height * 16 + 7) >> 3;
703
704 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
705 return -EINVAL;
706
707 buf->vb.width = dev->width;
708 buf->vb.height = dev->height;
709 buf->vb.field = field;
710
711 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
712 rc = videobuf_iolock(vq, &buf->vb, NULL);
713 if (rc < 0) {
714 printk(KERN_INFO "videobuf_iolock failed\n");
715 goto fail;
716 }
717 }
718
719 if (!dev->isoc_ctl.num_bufs)
720 urb_init = 1;
721
722 if (urb_init) {
723 rc = au0828_init_isoc(dev, AU0828_ISO_PACKETS_PER_URB,
724 AU0828_MAX_ISO_BUFS, dev->max_pkt_size,
725 au0828_isoc_copy);
726 if (rc < 0) {
727 printk(KERN_INFO "au0828_init_isoc failed\n");
728 goto fail;
729 }
730 }
731
732 buf->vb.state = VIDEOBUF_PREPARED;
733 return 0;
734
735fail:
736 free_buffer(vq, buf);
737 return rc;
738}
739
740static void
741buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
742{
743 struct au0828_buffer *buf = container_of(vb,
744 struct au0828_buffer,
745 vb);
746 struct au0828_fh *fh = vq->priv_data;
747 struct au0828_dev *dev = fh->dev;
748 struct au0828_dmaqueue *vidq = &dev->vidq;
749
750 buf->vb.state = VIDEOBUF_QUEUED;
751 list_add_tail(&buf->vb.queue, &vidq->active);
752}
753
754static void buffer_release(struct videobuf_queue *vq,
755 struct videobuf_buffer *vb)
756{
757 struct au0828_buffer *buf = container_of(vb,
758 struct au0828_buffer,
759 vb);
760
761 free_buffer(vq, buf);
762}
763
764static struct videobuf_queue_ops au0828_video_qops = {
765 .buf_setup = buffer_setup,
766 .buf_prepare = buffer_prepare,
767 .buf_queue = buffer_queue,
768 .buf_release = buffer_release,
769};
770
771/* ------------------------------------------------------------------
772 V4L2 interface
773 ------------------------------------------------------------------*/
774
775static int au0828_i2s_init(struct au0828_dev *dev)
776{
777 /* Enable i2s mode */
778 au0828_writereg(dev, AU0828_AUDIOCTRL_50C, 0x01);
779 return 0;
780}
781
782/*
783 * Auvitek au0828 analog stream enable
784 * Please set interface0 to AS5 before enable the stream
785 */
786int au0828_analog_stream_enable(struct au0828_dev *d)
787{
788 dprintk(1, "au0828_analog_stream_enable called\n");
789 au0828_writereg(d, AU0828_SENSORCTRL_VBI_103, 0x00);
790 au0828_writereg(d, 0x106, 0x00);
791 /* set x position */
792 au0828_writereg(d, 0x110, 0x00);
793 au0828_writereg(d, 0x111, 0x00);
794 au0828_writereg(d, 0x114, 0xa0);
795 au0828_writereg(d, 0x115, 0x05);
796 /* set y position */
797 au0828_writereg(d, 0x112, 0x00);
798 au0828_writereg(d, 0x113, 0x00);
799 au0828_writereg(d, 0x116, 0xf2);
800 au0828_writereg(d, 0x117, 0x00);
801 au0828_writereg(d, AU0828_SENSORCTRL_100, 0xb3);
802
803 return 0;
804}
805
806int au0828_analog_stream_disable(struct au0828_dev *d)
807{
808 dprintk(1, "au0828_analog_stream_disable called\n");
809 au0828_writereg(d, AU0828_SENSORCTRL_100, 0x0);
810 return 0;
811}
812
813void au0828_analog_stream_reset(struct au0828_dev *dev)
814{
815 dprintk(1, "au0828_analog_stream_reset called\n");
816 au0828_writereg(dev, AU0828_SENSORCTRL_100, 0x0);
817 mdelay(30);
818 au0828_writereg(dev, AU0828_SENSORCTRL_100, 0xb3);
819}
820
821/*
822 * Some operations needs to stop current streaming
823 */
824static int au0828_stream_interrupt(struct au0828_dev *dev)
825{
826 int ret = 0;
827
828 dev->stream_state = STREAM_INTERRUPT;
829 if (dev->dev_state == DEV_DISCONNECTED)
830 return -ENODEV;
831 else if (ret) {
832 dev->dev_state = DEV_MISCONFIGURED;
833 dprintk(1, "%s device is misconfigured!\n", __func__);
834 return ret;
835 }
836 return 0;
837}
838
839/*
840 * au0828_release_resources
841 * unregister v4l2 devices
842 */
843void au0828_analog_unregister(struct au0828_dev *dev)
844{
845 dprintk(1, "au0828_release_resources called\n");
846 mutex_lock(&au0828_sysfs_lock);
847
848 if (dev->vdev)
849 video_unregister_device(dev->vdev);
850 if (dev->vbi_dev)
851 video_unregister_device(dev->vbi_dev);
852
853 mutex_unlock(&au0828_sysfs_lock);
854}
855
856
857/* Usage lock check functions */
858static int res_get(struct au0828_fh *fh, unsigned int bit)
859{
860 struct au0828_dev *dev = fh->dev;
861
862 if (fh->resources & bit)
863 /* have it already allocated */
864 return 1;
865
866 /* is it free? */
867 mutex_lock(&dev->lock);
868 if (dev->resources & bit) {
869 /* no, someone else uses it */
870 mutex_unlock(&dev->lock);
871 return 0;
872 }
873 /* it's free, grab it */
874 fh->resources |= bit;
875 dev->resources |= bit;
876 dprintk(1, "res: get %d\n", bit);
877 mutex_unlock(&dev->lock);
878 return 1;
879}
880
881static int res_check(struct au0828_fh *fh, unsigned int bit)
882{
883 return fh->resources & bit;
884}
885
886static int res_locked(struct au0828_dev *dev, unsigned int bit)
887{
888 return dev->resources & bit;
889}
890
891static void res_free(struct au0828_fh *fh, unsigned int bits)
892{
893 struct au0828_dev *dev = fh->dev;
894
895 BUG_ON((fh->resources & bits) != bits);
896
897 mutex_lock(&dev->lock);
898 fh->resources &= ~bits;
899 dev->resources &= ~bits;
900 dprintk(1, "res: put %d\n", bits);
901 mutex_unlock(&dev->lock);
902}
903
904static int get_ressource(struct au0828_fh *fh)
905{
906 switch (fh->type) {
907 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
908 return AU0828_RESOURCE_VIDEO;
909 case V4L2_BUF_TYPE_VBI_CAPTURE:
910 return AU0828_RESOURCE_VBI;
911 default:
912 BUG();
913 return 0;
914 }
915}
916
917/* This function ensures that video frames continue to be delivered even if
918 the ITU-656 input isn't receiving any data (thereby preventing applications
919 such as tvtime from hanging) */
920void au0828_vid_buffer_timeout(unsigned long data)
921{
922 struct au0828_dev *dev = (struct au0828_dev *) data;
923 struct au0828_dmaqueue *dma_q = &dev->vidq;
924 struct au0828_buffer *buf;
925 unsigned char *vid_data;
926 unsigned long flags = 0;
927
928 spin_lock_irqsave(&dev->slock, flags);
929
930 buf = dev->isoc_ctl.buf;
931 if (buf != NULL) {
932 vid_data = videobuf_to_vmalloc(&buf->vb);
933 memset(vid_data, 0x00, buf->vb.size); /* Blank green frame */
934 buffer_filled(dev, dma_q, buf);
935 }
936 get_next_buf(dma_q, &buf);
937
938 if (dev->vid_timeout_running == 1)
939 mod_timer(&dev->vid_timeout, jiffies + (HZ / 10));
940
941 spin_unlock_irqrestore(&dev->slock, flags);
942}
943
944void au0828_vbi_buffer_timeout(unsigned long data)
945{
946 struct au0828_dev *dev = (struct au0828_dev *) data;
947 struct au0828_dmaqueue *dma_q = &dev->vbiq;
948 struct au0828_buffer *buf;
949 unsigned char *vbi_data;
950 unsigned long flags = 0;
951
952 spin_lock_irqsave(&dev->slock, flags);
953
954 buf = dev->isoc_ctl.vbi_buf;
955 if (buf != NULL) {
956 vbi_data = videobuf_to_vmalloc(&buf->vb);
957 memset(vbi_data, 0x00, buf->vb.size);
958 vbi_buffer_filled(dev, dma_q, buf);
959 }
960 vbi_get_next_buf(dma_q, &buf);
961
962 if (dev->vbi_timeout_running == 1)
963 mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
964 spin_unlock_irqrestore(&dev->slock, flags);
965}
966
967
968static int au0828_v4l2_open(struct file *filp)
969{
970 int ret = 0;
971 struct video_device *vdev = video_devdata(filp);
972 struct au0828_dev *dev = video_drvdata(filp);
973 struct au0828_fh *fh;
974 int type;
975
976 switch (vdev->vfl_type) {
977 case VFL_TYPE_GRABBER:
978 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
979 break;
980 case VFL_TYPE_VBI:
981 type = V4L2_BUF_TYPE_VBI_CAPTURE;
982 break;
983 default:
984 return -EINVAL;
985 }
986
987 fh = kzalloc(sizeof(struct au0828_fh), GFP_KERNEL);
988 if (NULL == fh) {
989 dprintk(1, "Failed allocate au0828_fh struct!\n");
990 return -ENOMEM;
991 }
992
993 fh->type = type;
994 fh->dev = dev;
995 filp->private_data = fh;
996
997 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
998 /* set au0828 interface0 to AS5 here again */
999 ret = usb_set_interface(dev->usbdev, 0, 5);
1000 if (ret < 0) {
1001 printk(KERN_INFO "Au0828 can't set alternate to 5!\n");
1002 return -EBUSY;
1003 }
1004 dev->width = NTSC_STD_W;
1005 dev->height = NTSC_STD_H;
1006 dev->frame_size = dev->width * dev->height * 2;
1007 dev->field_size = dev->width * dev->height;
1008 dev->bytesperline = dev->width * 2;
1009
1010 au0828_analog_stream_enable(dev);
1011 au0828_analog_stream_reset(dev);
1012
1013 /* If we were doing ac97 instead of i2s, it would go here...*/
1014 au0828_i2s_init(dev);
1015
1016 dev->stream_state = STREAM_OFF;
1017 dev->dev_state |= DEV_INITIALIZED;
1018 }
1019
1020 dev->users++;
1021
1022 videobuf_queue_vmalloc_init(&fh->vb_vidq, &au0828_video_qops,
1023 NULL, &dev->slock,
1024 V4L2_BUF_TYPE_VIDEO_CAPTURE,
1025 V4L2_FIELD_INTERLACED,
1026 sizeof(struct au0828_buffer), fh, NULL);
1027
1028 /* VBI Setup */
1029 dev->vbi_width = 720;
1030 dev->vbi_height = 1;
1031 videobuf_queue_vmalloc_init(&fh->vb_vbiq, &au0828_vbi_qops,
1032 NULL, &dev->slock,
1033 V4L2_BUF_TYPE_VBI_CAPTURE,
1034 V4L2_FIELD_SEQ_TB,
1035 sizeof(struct au0828_buffer), fh, NULL);
1036
1037 return ret;
1038}
1039
1040static int au0828_v4l2_close(struct file *filp)
1041{
1042 int ret;
1043 struct au0828_fh *fh = filp->private_data;
1044 struct au0828_dev *dev = fh->dev;
1045
1046 if (res_check(fh, AU0828_RESOURCE_VIDEO)) {
1047 /* Cancel timeout thread in case they didn't call streamoff */
1048 dev->vid_timeout_running = 0;
1049 del_timer_sync(&dev->vid_timeout);
1050
1051 videobuf_stop(&fh->vb_vidq);
1052 res_free(fh, AU0828_RESOURCE_VIDEO);
1053 }
1054
1055 if (res_check(fh, AU0828_RESOURCE_VBI)) {
1056 /* Cancel timeout thread in case they didn't call streamoff */
1057 dev->vbi_timeout_running = 0;
1058 del_timer_sync(&dev->vbi_timeout);
1059
1060 videobuf_stop(&fh->vb_vbiq);
1061 res_free(fh, AU0828_RESOURCE_VBI);
1062 }
1063
1064 if (dev->users == 1) {
1065 if (dev->dev_state & DEV_DISCONNECTED) {
1066 au0828_analog_unregister(dev);
1067 kfree(dev);
1068 return 0;
1069 }
1070
1071 au0828_analog_stream_disable(dev);
1072
1073 au0828_uninit_isoc(dev);
1074
1075 /* Save some power by putting tuner to sleep */
1076 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0);
1077
1078 /* When close the device, set the usb intf0 into alt0 to free
1079 USB bandwidth */
1080 ret = usb_set_interface(dev->usbdev, 0, 0);
1081 if (ret < 0)
1082 printk(KERN_INFO "Au0828 can't set alternate to 0!\n");
1083 }
1084
1085 videobuf_mmap_free(&fh->vb_vidq);
1086 videobuf_mmap_free(&fh->vb_vbiq);
1087 kfree(fh);
1088 dev->users--;
1089 wake_up_interruptible_nr(&dev->open, 1);
1090 return 0;
1091}
1092
1093static ssize_t au0828_v4l2_read(struct file *filp, char __user *buf,
1094 size_t count, loff_t *pos)
1095{
1096 struct au0828_fh *fh = filp->private_data;
1097 struct au0828_dev *dev = fh->dev;
1098 int rc;
1099
1100 rc = check_dev(dev);
1101 if (rc < 0)
1102 return rc;
1103
1104 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1105 if (res_locked(dev, AU0828_RESOURCE_VIDEO))
1106 return -EBUSY;
1107
1108 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
1109 filp->f_flags & O_NONBLOCK);
1110 }
1111
1112 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
1113 if (!res_get(fh, AU0828_RESOURCE_VBI))
1114 return -EBUSY;
1115
1116 if (dev->vbi_timeout_running == 0) {
1117 /* Handle case where caller tries to read without
1118 calling streamon first */
1119 dev->vbi_timeout_running = 1;
1120 mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
1121 }
1122
1123 return videobuf_read_stream(&fh->vb_vbiq, buf, count, pos, 0,
1124 filp->f_flags & O_NONBLOCK);
1125 }
1126
1127 return 0;
1128}
1129
1130static unsigned int au0828_v4l2_poll(struct file *filp, poll_table *wait)
1131{
1132 struct au0828_fh *fh = filp->private_data;
1133 struct au0828_dev *dev = fh->dev;
1134 int rc;
1135
1136 rc = check_dev(dev);
1137 if (rc < 0)
1138 return rc;
1139
1140 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1141 if (!res_get(fh, AU0828_RESOURCE_VIDEO))
1142 return POLLERR;
1143 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
1144 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
1145 if (!res_get(fh, AU0828_RESOURCE_VBI))
1146 return POLLERR;
1147 return videobuf_poll_stream(filp, &fh->vb_vbiq, wait);
1148 } else {
1149 return POLLERR;
1150 }
1151}
1152
1153static int au0828_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
1154{
1155 struct au0828_fh *fh = filp->private_data;
1156 struct au0828_dev *dev = fh->dev;
1157 int rc;
1158
1159 rc = check_dev(dev);
1160 if (rc < 0)
1161 return rc;
1162
1163 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1164 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1165 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1166 rc = videobuf_mmap_mapper(&fh->vb_vbiq, vma);
1167
1168 return rc;
1169}
1170
1171static int au0828_set_format(struct au0828_dev *dev, unsigned int cmd,
1172 struct v4l2_format *format)
1173{
1174 int ret;
1175 int width = format->fmt.pix.width;
1176 int height = format->fmt.pix.height;
1177
1178 if (format->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1179 return -EINVAL;
1180
1181 /* If they are demanding a format other than the one we support,
1182 bail out (tvtime asks for UYVY and then retries with YUYV) */
1183 if (format->fmt.pix.pixelformat != V4L2_PIX_FMT_UYVY)
1184 return -EINVAL;
1185
1186 /* format->fmt.pix.width only support 720 and height 480 */
1187 if (width != 720)
1188 width = 720;
1189 if (height != 480)
1190 height = 480;
1191
1192 format->fmt.pix.width = width;
1193 format->fmt.pix.height = height;
1194 format->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
1195 format->fmt.pix.bytesperline = width * 2;
1196 format->fmt.pix.sizeimage = width * height * 2;
1197 format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1198 format->fmt.pix.field = V4L2_FIELD_INTERLACED;
1199
1200 if (cmd == VIDIOC_TRY_FMT)
1201 return 0;
1202
1203 /* maybe set new image format, driver current only support 720*480 */
1204 dev->width = width;
1205 dev->height = height;
1206 dev->frame_size = width * height * 2;
1207 dev->field_size = width * height;
1208 dev->bytesperline = width * 2;
1209
1210 if (dev->stream_state == STREAM_ON) {
1211 dprintk(1, "VIDIOC_SET_FMT: interrupting stream!\n");
1212 ret = au0828_stream_interrupt(dev);
1213 if (ret != 0) {
1214 dprintk(1, "error interrupting video stream!\n");
1215 return ret;
1216 }
1217 }
1218
1219 /* set au0828 interface0 to AS5 here again */
1220 ret = usb_set_interface(dev->usbdev, 0, 5);
1221 if (ret < 0) {
1222 printk(KERN_INFO "Au0828 can't set alt setting to 5!\n");
1223 return -EBUSY;
1224 }
1225
1226 au0828_analog_stream_enable(dev);
1227
1228 return 0;
1229}
1230
1231
1232static int vidioc_queryctrl(struct file *file, void *priv,
1233 struct v4l2_queryctrl *qc)
1234{
1235 struct au0828_fh *fh = priv;
1236 struct au0828_dev *dev = fh->dev;
1237 v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, qc);
1238 if (qc->type)
1239 return 0;
1240 else
1241 return -EINVAL;
1242}
1243
1244static int vidioc_querycap(struct file *file, void *priv,
1245 struct v4l2_capability *cap)
1246{
1247 struct au0828_fh *fh = priv;
1248 struct au0828_dev *dev = fh->dev;
1249
1250 strlcpy(cap->driver, "au0828", sizeof(cap->driver));
1251 strlcpy(cap->card, dev->board.name, sizeof(cap->card));
1252 strlcpy(cap->bus_info, dev->v4l2_dev.name, sizeof(cap->bus_info));
1253
1254 /*set the device capabilities */
1255 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
1256 V4L2_CAP_VBI_CAPTURE |
1257 V4L2_CAP_AUDIO |
1258 V4L2_CAP_READWRITE |
1259 V4L2_CAP_STREAMING |
1260 V4L2_CAP_TUNER;
1261 return 0;
1262}
1263
1264static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1265 struct v4l2_fmtdesc *f)
1266{
1267 if (f->index)
1268 return -EINVAL;
1269
1270 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1271 strcpy(f->description, "Packed YUV2");
1272
1273 f->flags = 0;
1274 f->pixelformat = V4L2_PIX_FMT_UYVY;
1275
1276 return 0;
1277}
1278
1279static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
1280 struct v4l2_format *f)
1281{
1282 struct au0828_fh *fh = priv;
1283 struct au0828_dev *dev = fh->dev;
1284
1285 f->fmt.pix.width = dev->width;
1286 f->fmt.pix.height = dev->height;
1287 f->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
1288 f->fmt.pix.bytesperline = dev->bytesperline;
1289 f->fmt.pix.sizeimage = dev->frame_size;
1290 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; /* NTSC/PAL */
1291 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
1292 return 0;
1293}
1294
1295static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
1296 struct v4l2_format *f)
1297{
1298 struct au0828_fh *fh = priv;
1299 struct au0828_dev *dev = fh->dev;
1300
1301 return au0828_set_format(dev, VIDIOC_TRY_FMT, f);
1302}
1303
1304static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1305 struct v4l2_format *f)
1306{
1307 struct au0828_fh *fh = priv;
1308 struct au0828_dev *dev = fh->dev;
1309 int rc;
1310
1311 rc = check_dev(dev);
1312 if (rc < 0)
1313 return rc;
1314
1315 mutex_lock(&dev->lock);
1316
1317 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
1318 printk(KERN_INFO "%s queue busy\n", __func__);
1319 rc = -EBUSY;
1320 goto out;
1321 }
1322
1323 rc = au0828_set_format(dev, VIDIOC_S_FMT, f);
1324out:
1325 mutex_unlock(&dev->lock);
1326 return rc;
1327}
1328
1329static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id * norm)
1330{
1331 struct au0828_fh *fh = priv;
1332 struct au0828_dev *dev = fh->dev;
1333
1334 /* FIXME: when we support something other than NTSC, we are going to
1335 have to make the au0828 bridge adjust the size of its capture
1336 buffer, which is currently hardcoded at 720x480 */
1337
1338 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, *norm);
1339 return 0;
1340}
1341
1342static int vidioc_enum_input(struct file *file, void *priv,
1343 struct v4l2_input *input)
1344{
1345 struct au0828_fh *fh = priv;
1346 struct au0828_dev *dev = fh->dev;
1347 unsigned int tmp;
1348
1349 static const char *inames[] = {
1350 [AU0828_VMUX_UNDEFINED] = "Undefined",
1351 [AU0828_VMUX_COMPOSITE] = "Composite",
1352 [AU0828_VMUX_SVIDEO] = "S-Video",
1353 [AU0828_VMUX_CABLE] = "Cable TV",
1354 [AU0828_VMUX_TELEVISION] = "Television",
1355 [AU0828_VMUX_DVB] = "DVB",
1356 [AU0828_VMUX_DEBUG] = "tv debug"
1357 };
1358
1359 tmp = input->index;
1360
1361 if (tmp >= AU0828_MAX_INPUT)
1362 return -EINVAL;
1363 if (AUVI_INPUT(tmp).type == 0)
1364 return -EINVAL;
1365
1366 input->index = tmp;
1367 strcpy(input->name, inames[AUVI_INPUT(tmp).type]);
1368 if ((AUVI_INPUT(tmp).type == AU0828_VMUX_TELEVISION) ||
1369 (AUVI_INPUT(tmp).type == AU0828_VMUX_CABLE))
1370 input->type |= V4L2_INPUT_TYPE_TUNER;
1371 else
1372 input->type |= V4L2_INPUT_TYPE_CAMERA;
1373
1374 input->std = dev->vdev->tvnorms;
1375
1376 return 0;
1377}
1378
1379static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1380{
1381 struct au0828_fh *fh = priv;
1382 struct au0828_dev *dev = fh->dev;
1383 *i = dev->ctrl_input;
1384 return 0;
1385}
1386
1387static int vidioc_s_input(struct file *file, void *priv, unsigned int index)
1388{
1389 struct au0828_fh *fh = priv;
1390 struct au0828_dev *dev = fh->dev;
1391 int i;
1392
1393 dprintk(1, "VIDIOC_S_INPUT in function %s, input=%d\n", __func__,
1394 index);
1395 if (index >= AU0828_MAX_INPUT)
1396 return -EINVAL;
1397 if (AUVI_INPUT(index).type == 0)
1398 return -EINVAL;
1399 dev->ctrl_input = index;
1400
1401 switch (AUVI_INPUT(index).type) {
1402 case AU0828_VMUX_SVIDEO:
1403 dev->input_type = AU0828_VMUX_SVIDEO;
1404 break;
1405 case AU0828_VMUX_COMPOSITE:
1406 dev->input_type = AU0828_VMUX_COMPOSITE;
1407 break;
1408 case AU0828_VMUX_TELEVISION:
1409 dev->input_type = AU0828_VMUX_TELEVISION;
1410 break;
1411 default:
1412 dprintk(1, "VIDIOC_S_INPUT unknown input type set [%d]\n",
1413 AUVI_INPUT(index).type);
1414 break;
1415 }
1416
1417 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
1418 AUVI_INPUT(index).vmux, 0, 0);
1419
1420 for (i = 0; i < AU0828_MAX_INPUT; i++) {
1421 int enable = 0;
1422 if (AUVI_INPUT(i).audio_setup == NULL)
1423 continue;
1424
1425 if (i == index)
1426 enable = 1;
1427 else
1428 enable = 0;
1429 if (enable) {
1430 (AUVI_INPUT(i).audio_setup)(dev, enable);
1431 } else {
1432 /* Make sure we leave it turned on if some
1433 other input is routed to this callback */
1434 if ((AUVI_INPUT(i).audio_setup) !=
1435 ((AUVI_INPUT(index).audio_setup))) {
1436 (AUVI_INPUT(i).audio_setup)(dev, enable);
1437 }
1438 }
1439 }
1440
1441 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
1442 AUVI_INPUT(index).amux, 0, 0);
1443 return 0;
1444}
1445
1446static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1447{
1448 struct au0828_fh *fh = priv;
1449 struct au0828_dev *dev = fh->dev;
1450 unsigned int index = a->index;
1451
1452 if (a->index > 1)
1453 return -EINVAL;
1454
1455 index = dev->ctrl_ainput;
1456 if (index == 0)
1457 strcpy(a->name, "Television");
1458 else
1459 strcpy(a->name, "Line in");
1460
1461 a->capability = V4L2_AUDCAP_STEREO;
1462 a->index = index;
1463 return 0;
1464}
1465
1466static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
1467{
1468 struct au0828_fh *fh = priv;
1469 struct au0828_dev *dev = fh->dev;
1470 if (a->index != dev->ctrl_ainput)
1471 return -EINVAL;
1472 return 0;
1473}
1474
1475static int vidioc_g_ctrl(struct file *file, void *priv,
1476 struct v4l2_control *ctrl)
1477{
1478 struct au0828_fh *fh = priv;
1479 struct au0828_dev *dev = fh->dev;
1480
1481 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_ctrl, ctrl);
1482 return 0;
1483
1484}
1485
1486static int vidioc_s_ctrl(struct file *file, void *priv,
1487 struct v4l2_control *ctrl)
1488{
1489 struct au0828_fh *fh = priv;
1490 struct au0828_dev *dev = fh->dev;
1491 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_ctrl, ctrl);
1492 return 0;
1493}
1494
1495static int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1496{
1497 struct au0828_fh *fh = priv;
1498 struct au0828_dev *dev = fh->dev;
1499
1500 if (t->index != 0)
1501 return -EINVAL;
1502
1503 strcpy(t->name, "Auvitek tuner");
1504 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
1505 return 0;
1506}
1507
1508static int vidioc_s_tuner(struct file *file, void *priv,
1509 struct v4l2_tuner *t)
1510{
1511 struct au0828_fh *fh = priv;
1512 struct au0828_dev *dev = fh->dev;
1513
1514 if (t->index != 0)
1515 return -EINVAL;
1516
1517 t->type = V4L2_TUNER_ANALOG_TV;
1518 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
1519 dprintk(1, "VIDIOC_S_TUNER: signal = %x, afc = %x\n", t->signal,
1520 t->afc);
1521 return 0;
1522
1523}
1524
1525static int vidioc_g_frequency(struct file *file, void *priv,
1526 struct v4l2_frequency *freq)
1527{
1528 struct au0828_fh *fh = priv;
1529 struct au0828_dev *dev = fh->dev;
1530
1531 freq->type = V4L2_TUNER_ANALOG_TV;
1532 freq->frequency = dev->ctrl_freq;
1533 return 0;
1534}
1535
1536static int vidioc_s_frequency(struct file *file, void *priv,
1537 struct v4l2_frequency *freq)
1538{
1539 struct au0828_fh *fh = priv;
1540 struct au0828_dev *dev = fh->dev;
1541
1542 if (freq->tuner != 0)
1543 return -EINVAL;
1544 if (freq->type != V4L2_TUNER_ANALOG_TV)
1545 return -EINVAL;
1546
1547 dev->ctrl_freq = freq->frequency;
1548
1549 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, freq);
1550
1551 au0828_analog_stream_reset(dev);
1552
1553 return 0;
1554}
1555
1556
1557/* RAW VBI ioctls */
1558
1559static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1560 struct v4l2_format *format)
1561{
1562 struct au0828_fh *fh = priv;
1563 struct au0828_dev *dev = fh->dev;
1564
1565 format->fmt.vbi.samples_per_line = dev->vbi_width;
1566 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1567 format->fmt.vbi.offset = 0;
1568 format->fmt.vbi.flags = 0;
1569 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1570
1571 format->fmt.vbi.count[0] = dev->vbi_height;
1572 format->fmt.vbi.count[1] = dev->vbi_height;
1573 format->fmt.vbi.start[0] = 21;
1574 format->fmt.vbi.start[1] = 284;
1575
1576 return 0;
1577}
1578
1579static int vidioc_g_chip_ident(struct file *file, void *priv,
1580 struct v4l2_dbg_chip_ident *chip)
1581{
1582 struct au0828_fh *fh = priv;
1583 struct au0828_dev *dev = fh->dev;
1584 chip->ident = V4L2_IDENT_NONE;
1585 chip->revision = 0;
1586
1587 if (v4l2_chip_match_host(&chip->match)) {
1588 chip->ident = V4L2_IDENT_AU0828;
1589 return 0;
1590 }
1591
1592 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip);
1593 if (chip->ident == V4L2_IDENT_NONE)
1594 return -EINVAL;
1595
1596 return 0;
1597}
1598
1599static int vidioc_cropcap(struct file *file, void *priv,
1600 struct v4l2_cropcap *cc)
1601{
1602 struct au0828_fh *fh = priv;
1603 struct au0828_dev *dev = fh->dev;
1604
1605 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1606 return -EINVAL;
1607
1608 cc->bounds.left = 0;
1609 cc->bounds.top = 0;
1610 cc->bounds.width = dev->width;
1611 cc->bounds.height = dev->height;
1612
1613 cc->defrect = cc->bounds;
1614
1615 cc->pixelaspect.numerator = 54;
1616 cc->pixelaspect.denominator = 59;
1617
1618 return 0;
1619}
1620
1621static int vidioc_streamon(struct file *file, void *priv,
1622 enum v4l2_buf_type type)
1623{
1624 struct au0828_fh *fh = priv;
1625 struct au0828_dev *dev = fh->dev;
1626 int rc = -EINVAL;
1627
1628 rc = check_dev(dev);
1629 if (rc < 0)
1630 return rc;
1631
1632 if (unlikely(type != fh->type))
1633 return -EINVAL;
1634
1635 dprintk(1, "vidioc_streamon fh=%p t=%d fh->res=%d dev->res=%d\n",
1636 fh, type, fh->resources, dev->resources);
1637
1638 if (unlikely(!res_get(fh, get_ressource(fh))))
1639 return -EBUSY;
1640
1641 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1642 au0828_analog_stream_enable(dev);
1643 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 1);
1644 }
1645
1646 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1647 rc = videobuf_streamon(&fh->vb_vidq);
1648 dev->vid_timeout_running = 1;
1649 mod_timer(&dev->vid_timeout, jiffies + (HZ / 10));
1650 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
1651 rc = videobuf_streamon(&fh->vb_vbiq);
1652 dev->vbi_timeout_running = 1;
1653 mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
1654 }
1655
1656 return rc;
1657}
1658
1659static int vidioc_streamoff(struct file *file, void *priv,
1660 enum v4l2_buf_type type)
1661{
1662 struct au0828_fh *fh = priv;
1663 struct au0828_dev *dev = fh->dev;
1664 int rc;
1665 int i;
1666
1667 rc = check_dev(dev);
1668 if (rc < 0)
1669 return rc;
1670
1671 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1672 fh->type != V4L2_BUF_TYPE_VBI_CAPTURE)
1673 return -EINVAL;
1674 if (type != fh->type)
1675 return -EINVAL;
1676
1677 dprintk(1, "vidioc_streamoff fh=%p t=%d fh->res=%d dev->res=%d\n",
1678 fh, type, fh->resources, dev->resources);
1679
1680 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1681 dev->vid_timeout_running = 0;
1682 del_timer_sync(&dev->vid_timeout);
1683
1684 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 0);
1685 rc = au0828_stream_interrupt(dev);
1686 if (rc != 0)
1687 return rc;
1688
1689 for (i = 0; i < AU0828_MAX_INPUT; i++) {
1690 if (AUVI_INPUT(i).audio_setup == NULL)
1691 continue;
1692 (AUVI_INPUT(i).audio_setup)(dev, 0);
1693 }
1694
1695 videobuf_streamoff(&fh->vb_vidq);
1696 res_free(fh, AU0828_RESOURCE_VIDEO);
1697 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
1698 dev->vbi_timeout_running = 0;
1699 del_timer_sync(&dev->vbi_timeout);
1700
1701 videobuf_streamoff(&fh->vb_vbiq);
1702 res_free(fh, AU0828_RESOURCE_VBI);
1703 }
1704
1705 return 0;
1706}
1707
1708#ifdef CONFIG_VIDEO_ADV_DEBUG
1709static int vidioc_g_register(struct file *file, void *priv,
1710 struct v4l2_dbg_register *reg)
1711{
1712 struct au0828_fh *fh = priv;
1713 struct au0828_dev *dev = fh->dev;
1714
1715 switch (reg->match.type) {
1716 case V4L2_CHIP_MATCH_I2C_DRIVER:
1717 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
1718 return 0;
1719 default:
1720 return -EINVAL;
1721 }
1722}
1723
1724static int vidioc_s_register(struct file *file, void *priv,
1725 struct v4l2_dbg_register *reg)
1726{
1727 struct au0828_fh *fh = priv;
1728 struct au0828_dev *dev = fh->dev;
1729
1730 switch (reg->match.type) {
1731 case V4L2_CHIP_MATCH_I2C_DRIVER:
1732 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
1733 return 0;
1734 default:
1735 return -EINVAL;
1736 }
1737 return 0;
1738}
1739#endif
1740
1741static int vidioc_reqbufs(struct file *file, void *priv,
1742 struct v4l2_requestbuffers *rb)
1743{
1744 struct au0828_fh *fh = priv;
1745 struct au0828_dev *dev = fh->dev;
1746 int rc;
1747
1748 rc = check_dev(dev);
1749 if (rc < 0)
1750 return rc;
1751
1752 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1753 rc = videobuf_reqbufs(&fh->vb_vidq, rb);
1754 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1755 rc = videobuf_reqbufs(&fh->vb_vbiq, rb);
1756
1757 return rc;
1758}
1759
1760static int vidioc_querybuf(struct file *file, void *priv,
1761 struct v4l2_buffer *b)
1762{
1763 struct au0828_fh *fh = priv;
1764 struct au0828_dev *dev = fh->dev;
1765 int rc;
1766
1767 rc = check_dev(dev);
1768 if (rc < 0)
1769 return rc;
1770
1771 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1772 rc = videobuf_querybuf(&fh->vb_vidq, b);
1773 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1774 rc = videobuf_querybuf(&fh->vb_vbiq, b);
1775
1776 return rc;
1777}
1778
1779static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1780{
1781 struct au0828_fh *fh = priv;
1782 struct au0828_dev *dev = fh->dev;
1783 int rc;
1784
1785 rc = check_dev(dev);
1786 if (rc < 0)
1787 return rc;
1788
1789 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1790 rc = videobuf_qbuf(&fh->vb_vidq, b);
1791 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1792 rc = videobuf_qbuf(&fh->vb_vbiq, b);
1793
1794 return rc;
1795}
1796
1797static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1798{
1799 struct au0828_fh *fh = priv;
1800 struct au0828_dev *dev = fh->dev;
1801 int rc;
1802
1803 rc = check_dev(dev);
1804 if (rc < 0)
1805 return rc;
1806
1807 /* Workaround for a bug in the au0828 hardware design that sometimes
1808 results in the colorspace being inverted */
1809 if (dev->greenscreen_detected == 1) {
1810 dprintk(1, "Detected green frame. Resetting stream...\n");
1811 au0828_analog_stream_reset(dev);
1812 dev->greenscreen_detected = 0;
1813 }
1814
1815 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1816 rc = videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags & O_NONBLOCK);
1817 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1818 rc = videobuf_dqbuf(&fh->vb_vbiq, b, file->f_flags & O_NONBLOCK);
1819
1820 return rc;
1821}
1822
1823static struct v4l2_file_operations au0828_v4l_fops = {
1824 .owner = THIS_MODULE,
1825 .open = au0828_v4l2_open,
1826 .release = au0828_v4l2_close,
1827 .read = au0828_v4l2_read,
1828 .poll = au0828_v4l2_poll,
1829 .mmap = au0828_v4l2_mmap,
1830 .ioctl = video_ioctl2,
1831};
1832
1833static const struct v4l2_ioctl_ops video_ioctl_ops = {
1834 .vidioc_querycap = vidioc_querycap,
1835 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1836 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1837 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1838 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1839 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
1840 .vidioc_s_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
1841 .vidioc_g_audio = vidioc_g_audio,
1842 .vidioc_s_audio = vidioc_s_audio,
1843 .vidioc_cropcap = vidioc_cropcap,
1844 .vidioc_reqbufs = vidioc_reqbufs,
1845 .vidioc_querybuf = vidioc_querybuf,
1846 .vidioc_qbuf = vidioc_qbuf,
1847 .vidioc_dqbuf = vidioc_dqbuf,
1848 .vidioc_s_std = vidioc_s_std,
1849 .vidioc_enum_input = vidioc_enum_input,
1850 .vidioc_g_input = vidioc_g_input,
1851 .vidioc_s_input = vidioc_s_input,
1852 .vidioc_queryctrl = vidioc_queryctrl,
1853 .vidioc_g_ctrl = vidioc_g_ctrl,
1854 .vidioc_s_ctrl = vidioc_s_ctrl,
1855 .vidioc_streamon = vidioc_streamon,
1856 .vidioc_streamoff = vidioc_streamoff,
1857 .vidioc_g_tuner = vidioc_g_tuner,
1858 .vidioc_s_tuner = vidioc_s_tuner,
1859 .vidioc_g_frequency = vidioc_g_frequency,
1860 .vidioc_s_frequency = vidioc_s_frequency,
1861#ifdef CONFIG_VIDEO_ADV_DEBUG
1862 .vidioc_g_register = vidioc_g_register,
1863 .vidioc_s_register = vidioc_s_register,
1864#endif
1865 .vidioc_g_chip_ident = vidioc_g_chip_ident,
1866};
1867
1868static const struct video_device au0828_video_template = {
1869 .fops = &au0828_v4l_fops,
1870 .release = video_device_release,
1871 .ioctl_ops = &video_ioctl_ops,
1872 .tvnorms = V4L2_STD_NTSC_M,
1873 .current_norm = V4L2_STD_NTSC_M,
1874};
1875
1876/**************************************************************************/
1877
1878int au0828_analog_register(struct au0828_dev *dev,
1879 struct usb_interface *interface)
1880{
1881 int retval = -ENOMEM;
1882 struct usb_host_interface *iface_desc;
1883 struct usb_endpoint_descriptor *endpoint;
1884 int i;
1885
1886 dprintk(1, "au0828_analog_register called!\n");
1887
1888 /* set au0828 usb interface0 to as5 */
1889 retval = usb_set_interface(dev->usbdev,
1890 interface->cur_altsetting->desc.bInterfaceNumber, 5);
1891 if (retval != 0) {
1892 printk(KERN_INFO "Failure setting usb interface0 to as5\n");
1893 return retval;
1894 }
1895
1896 /* Figure out which endpoint has the isoc interface */
1897 iface_desc = interface->cur_altsetting;
1898 for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
1899 endpoint = &iface_desc->endpoint[i].desc;
1900 if (((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
1901 == USB_DIR_IN) &&
1902 ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1903 == USB_ENDPOINT_XFER_ISOC)) {
1904
1905 /* we find our isoc in endpoint */
1906 u16 tmp = le16_to_cpu(endpoint->wMaxPacketSize);
1907 dev->max_pkt_size = (tmp & 0x07ff) *
1908 (((tmp & 0x1800) >> 11) + 1);
1909 dev->isoc_in_endpointaddr = endpoint->bEndpointAddress;
1910 }
1911 }
1912 if (!(dev->isoc_in_endpointaddr)) {
1913 printk(KERN_INFO "Could not locate isoc endpoint\n");
1914 kfree(dev);
1915 return -ENODEV;
1916 }
1917
1918 init_waitqueue_head(&dev->open);
1919 spin_lock_init(&dev->slock);
1920 mutex_init(&dev->lock);
1921
1922 /* init video dma queues */
1923 INIT_LIST_HEAD(&dev->vidq.active);
1924 INIT_LIST_HEAD(&dev->vidq.queued);
1925 INIT_LIST_HEAD(&dev->vbiq.active);
1926 INIT_LIST_HEAD(&dev->vbiq.queued);
1927
1928 dev->vid_timeout.function = au0828_vid_buffer_timeout;
1929 dev->vid_timeout.data = (unsigned long) dev;
1930 init_timer(&dev->vid_timeout);
1931
1932 dev->vbi_timeout.function = au0828_vbi_buffer_timeout;
1933 dev->vbi_timeout.data = (unsigned long) dev;
1934 init_timer(&dev->vbi_timeout);
1935
1936 dev->width = NTSC_STD_W;
1937 dev->height = NTSC_STD_H;
1938 dev->field_size = dev->width * dev->height;
1939 dev->frame_size = dev->field_size << 1;
1940 dev->bytesperline = dev->width << 1;
1941 dev->ctrl_ainput = 0;
1942
1943 /* allocate and fill v4l2 video struct */
1944 dev->vdev = video_device_alloc();
1945 if (NULL == dev->vdev) {
1946 dprintk(1, "Can't allocate video_device.\n");
1947 return -ENOMEM;
1948 }
1949
1950 /* allocate the VBI struct */
1951 dev->vbi_dev = video_device_alloc();
1952 if (NULL == dev->vbi_dev) {
1953 dprintk(1, "Can't allocate vbi_device.\n");
1954 kfree(dev->vdev);
1955 return -ENOMEM;
1956 }
1957
1958 /* Fill the video capture device struct */
1959 *dev->vdev = au0828_video_template;
1960 dev->vdev->parent = &dev->usbdev->dev;
1961 strcpy(dev->vdev->name, "au0828a video");
1962
1963 /* Setup the VBI device */
1964 *dev->vbi_dev = au0828_video_template;
1965 dev->vbi_dev->parent = &dev->usbdev->dev;
1966 strcpy(dev->vbi_dev->name, "au0828a vbi");
1967
1968 /* Register the v4l2 device */
1969 video_set_drvdata(dev->vdev, dev);
1970 retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER, -1);
1971 if (retval != 0) {
1972 dprintk(1, "unable to register video device (error = %d).\n",
1973 retval);
1974 video_device_release(dev->vdev);
1975 return -ENODEV;
1976 }
1977
1978 /* Register the vbi device */
1979 video_set_drvdata(dev->vbi_dev, dev);
1980 retval = video_register_device(dev->vbi_dev, VFL_TYPE_VBI, -1);
1981 if (retval != 0) {
1982 dprintk(1, "unable to register vbi device (error = %d).\n",
1983 retval);
1984 video_device_release(dev->vbi_dev);
1985 video_device_release(dev->vdev);
1986 return -ENODEV;
1987 }
1988
1989 dprintk(1, "%s completed!\n", __func__);
1990
1991 return 0;
1992}
1993
diff --git a/drivers/media/video/au0828/au0828.h b/drivers/media/video/au0828/au0828.h
new file mode 100644
index 00000000000..9cde3532182
--- /dev/null
+++ b/drivers/media/video/au0828/au0828.h
@@ -0,0 +1,302 @@
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 128
57
58#define AU0828_MIN_BUF 4
59#define AU0828_DEF_BUF 8
60
61#define AU0828_MAX_INPUT 4
62
63/* au0828 resource types (used for res_get/res_lock etc */
64#define AU0828_RESOURCE_VIDEO 0x01
65#define AU0828_RESOURCE_VBI 0x02
66
67enum au0828_itype {
68 AU0828_VMUX_UNDEFINED = 0,
69 AU0828_VMUX_COMPOSITE,
70 AU0828_VMUX_SVIDEO,
71 AU0828_VMUX_CABLE,
72 AU0828_VMUX_TELEVISION,
73 AU0828_VMUX_DVB,
74 AU0828_VMUX_DEBUG
75};
76
77struct au0828_input {
78 enum au0828_itype type;
79 unsigned int vmux;
80 unsigned int amux;
81 void (*audio_setup) (void *priv, int enable);
82};
83
84struct au0828_board {
85 char *name;
86 unsigned int tuner_type;
87 unsigned char tuner_addr;
88 unsigned char i2c_clk_divider;
89 struct au0828_input input[AU0828_MAX_INPUT];
90
91};
92
93struct 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
105enum 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 */
114enum au0828_dev_state {
115 DEV_INITIALIZED = 0x01,
116 DEV_DISCONNECTED = 0x02,
117 DEV_MISCONFIGURED = 0x04
118};
119
120struct au0828_fh {
121 struct au0828_dev *dev;
122 unsigned int resources;
123
124 struct videobuf_queue vb_vidq;
125 struct videobuf_queue vb_vbiq;
126 enum v4l2_buf_type type;
127};
128
129struct au0828_usb_isoc_ctl {
130 /* max packet size of isoc transaction */
131 int max_pkt_size;
132
133 /* number of allocated urbs */
134 int num_bufs;
135
136 /* urb for isoc transfers */
137 struct urb **urb;
138
139 /* transfer buffers for isoc transfer */
140 char **transfer_buffer;
141
142 /* Last buffer command and region */
143 u8 cmd;
144 int pos, size, pktsize;
145
146 /* Last field: ODD or EVEN? */
147 int field;
148
149 /* Stores incomplete commands */
150 u32 tmp_buf;
151 int tmp_buf_len;
152
153 /* Stores already requested buffers */
154 struct au0828_buffer *buf;
155 struct au0828_buffer *vbi_buf;
156
157 /* Stores the number of received fields */
158 int nfields;
159
160 /* isoc urb callback */
161 int (*isoc_copy) (struct au0828_dev *dev, struct urb *urb);
162
163};
164
165/* buffer for one video frame */
166struct au0828_buffer {
167 /* common v4l buffer stuff -- must be first */
168 struct videobuf_buffer vb;
169
170 struct list_head frame;
171 int top_field;
172 int receiving;
173};
174
175struct au0828_dmaqueue {
176 struct list_head active;
177 struct list_head queued;
178
179 wait_queue_head_t wq;
180
181 /* Counters to control buffer fill */
182 int pos;
183};
184
185struct au0828_dev {
186 struct mutex mutex;
187 struct usb_device *usbdev;
188 int boardnr;
189 struct au0828_board board;
190 u8 ctrlmsg[64];
191
192 /* I2C */
193 struct i2c_adapter i2c_adap;
194 struct i2c_algorithm i2c_algo;
195 struct i2c_client i2c_client;
196 u32 i2c_rc;
197
198 /* Digital */
199 struct au0828_dvb dvb;
200
201 /* Analog */
202 struct v4l2_device v4l2_dev;
203 int users;
204 unsigned int resources; /* resources in use */
205 struct video_device *vdev;
206 struct video_device *vbi_dev;
207 struct timer_list vid_timeout;
208 int vid_timeout_running;
209 struct timer_list vbi_timeout;
210 int vbi_timeout_running;
211 int width;
212 int height;
213 int vbi_width;
214 int vbi_height;
215 u32 vbi_read;
216 u32 field_size;
217 u32 frame_size;
218 u32 bytesperline;
219 int type;
220 u8 ctrl_ainput;
221 __u8 isoc_in_endpointaddr;
222 u8 isoc_init_ok;
223 int greenscreen_detected;
224 unsigned int frame_count;
225 int ctrl_freq;
226 int input_type;
227 unsigned int ctrl_input;
228 enum au0828_dev_state dev_state;
229 enum au0828_stream_state stream_state;
230 wait_queue_head_t open;
231
232 struct mutex lock;
233
234 /* Isoc control struct */
235 struct au0828_dmaqueue vidq;
236 struct au0828_dmaqueue vbiq;
237 struct au0828_usb_isoc_ctl isoc_ctl;
238 spinlock_t slock;
239
240 /* usb transfer */
241 int alt; /* alternate */
242 int max_pkt_size; /* max packet size of isoc transaction */
243 int num_alt; /* Number of alternative settings */
244 unsigned int *alt_max_pkt_size; /* array of wMaxPacketSize */
245 struct urb *urb[AU0828_MAX_ISO_BUFS]; /* urb for isoc transfers */
246 char *transfer_buffer[AU0828_MAX_ISO_BUFS];/* transfer buffers for isoc
247 transfer */
248
249 /* USB / URB Related */
250 int urb_streaming;
251 struct urb *urbs[URB_COUNT];
252};
253
254/* ----------------------------------------------------------- */
255#define au0828_read(dev, reg) au0828_readreg(dev, reg)
256#define au0828_write(dev, reg, value) au0828_writereg(dev, reg, value)
257#define au0828_andor(dev, reg, mask, value) \
258 au0828_writereg(dev, reg, \
259 (au0828_readreg(dev, reg) & ~(mask)) | ((value) & (mask)))
260
261#define au0828_set(dev, reg, bit) au0828_andor(dev, (reg), (bit), (bit))
262#define au0828_clear(dev, reg, bit) au0828_andor(dev, (reg), (bit), 0)
263
264/* ----------------------------------------------------------- */
265/* au0828-core.c */
266extern u32 au0828_read(struct au0828_dev *dev, u16 reg);
267extern u32 au0828_write(struct au0828_dev *dev, u16 reg, u32 val);
268extern int au0828_debug;
269
270/* ----------------------------------------------------------- */
271/* au0828-cards.c */
272extern struct au0828_board au0828_boards[];
273extern struct usb_device_id au0828_usb_id_table[];
274extern void au0828_gpio_setup(struct au0828_dev *dev);
275extern int au0828_tuner_callback(void *priv, int component,
276 int command, int arg);
277extern void au0828_card_setup(struct au0828_dev *dev);
278
279/* ----------------------------------------------------------- */
280/* au0828-i2c.c */
281extern int au0828_i2c_register(struct au0828_dev *dev);
282extern int au0828_i2c_unregister(struct au0828_dev *dev);
283
284/* ----------------------------------------------------------- */
285/* au0828-video.c */
286int au0828_analog_register(struct au0828_dev *dev,
287 struct usb_interface *interface);
288int au0828_analog_stream_disable(struct au0828_dev *d);
289void au0828_analog_unregister(struct au0828_dev *dev);
290
291/* ----------------------------------------------------------- */
292/* au0828-dvb.c */
293extern int au0828_dvb_register(struct au0828_dev *dev);
294extern void au0828_dvb_unregister(struct au0828_dev *dev);
295
296/* au0828-vbi.c */
297extern struct videobuf_queue_ops au0828_vbi_qops;
298
299#define dprintk(level, fmt, arg...)\
300 do { if (au0828_debug & level)\
301 printk(KERN_DEBUG DRIVER_NAME "/0: " fmt, ## arg);\
302 } while (0)