1 From 866b8695d67e83f47194731d3a7ba55826a7ec70 Mon Sep 17 00:00:00 2001
2 From: Greg Kroah-Hartman <gregkh@suse.de>
3 Date: Fri, 15 Feb 2008 16:53:09 -0800
4 Subject: [PATCH 10/23] Staging: add the go7007 video driver
8 - checkpatch.pl cleanups
10 - lots of little modules, should be merged together
11 and added to the build.
13 - handle churn in v4l layer.
15 Many thanks to Ross Cohen <rcohen@snurgle.org> for cleanup patches on
18 Cc: Ross Cohen <rcohen@snurgle.org>
19 Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
21 drivers/staging/Kconfig | 2 +
22 drivers/staging/Makefile | 1 +
23 drivers/staging/go7007/Kconfig | 25 +
24 drivers/staging/go7007/Makefile | 18 +
25 drivers/staging/go7007/README | 11 +
26 drivers/staging/go7007/go7007-driver.c | 688 +++++++++++++
27 drivers/staging/go7007/go7007-fw.c | 1639 +++++++++++++++++++++++++++++++
28 drivers/staging/go7007/go7007-i2c.c | 309 ++++++
29 drivers/staging/go7007/go7007-priv.h | 279 ++++++
30 drivers/staging/go7007/go7007-usb.c | 1229 +++++++++++++++++++++++
31 drivers/staging/go7007/go7007-v4l2.c | 1503 ++++++++++++++++++++++++++++
32 drivers/staging/go7007/go7007.h | 114 +++
33 drivers/staging/go7007/saa7134-go7007.c | 484 +++++++++
34 drivers/staging/go7007/snd-go7007.c | 305 ++++++
35 drivers/staging/go7007/wis-i2c.h | 55 +
36 drivers/staging/go7007/wis-ov7640.c | 131 +++
37 drivers/staging/go7007/wis-saa7113.c | 363 +++++++
38 drivers/staging/go7007/wis-saa7115.c | 492 +++++++++
39 drivers/staging/go7007/wis-sony-tuner.c | 741 ++++++++++++++
40 drivers/staging/go7007/wis-tw2804.c | 381 +++++++
41 drivers/staging/go7007/wis-tw9903.c | 363 +++++++
42 drivers/staging/go7007/wis-uda1342.c | 136 +++
43 22 files changed, 9269 insertions(+), 0 deletions(-)
44 create mode 100644 drivers/staging/go7007/Kconfig
45 create mode 100644 drivers/staging/go7007/Makefile
46 create mode 100644 drivers/staging/go7007/README
47 create mode 100644 drivers/staging/go7007/go7007-driver.c
48 create mode 100644 drivers/staging/go7007/go7007-fw.c
49 create mode 100644 drivers/staging/go7007/go7007-i2c.c
50 create mode 100644 drivers/staging/go7007/go7007-priv.h
51 create mode 100644 drivers/staging/go7007/go7007-usb.c
52 create mode 100644 drivers/staging/go7007/go7007-v4l2.c
53 create mode 100644 drivers/staging/go7007/go7007.h
54 create mode 100644 drivers/staging/go7007/saa7134-go7007.c
55 create mode 100644 drivers/staging/go7007/snd-go7007.c
56 create mode 100644 drivers/staging/go7007/wis-i2c.h
57 create mode 100644 drivers/staging/go7007/wis-ov7640.c
58 create mode 100644 drivers/staging/go7007/wis-saa7113.c
59 create mode 100644 drivers/staging/go7007/wis-saa7115.c
60 create mode 100644 drivers/staging/go7007/wis-sony-tuner.c
61 create mode 100644 drivers/staging/go7007/wis-tw2804.c
62 create mode 100644 drivers/staging/go7007/wis-tw9903.c
63 create mode 100644 drivers/staging/go7007/wis-uda1342.c
65 diff --git a/drivers/staging/Kconfig b/drivers/staging/Kconfig
66 index 56c73bc..f16bc9c 100644
67 --- a/drivers/staging/Kconfig
68 +++ b/drivers/staging/Kconfig
69 @@ -31,4 +31,6 @@ source "drivers/staging/sxg/Kconfig"
71 source "drivers/staging/me4000/Kconfig"
73 +source "drivers/staging/go7007/Kconfig"
76 diff --git a/drivers/staging/Makefile b/drivers/staging/Makefile
77 index 97df19b..aa61662 100644
78 --- a/drivers/staging/Makefile
79 +++ b/drivers/staging/Makefile
80 @@ -4,3 +4,4 @@ obj-$(CONFIG_ET131X) += et131x/
81 obj-$(CONFIG_SLICOSS) += slicoss/
82 obj-$(CONFIG_SXG) += sxg/
83 obj-$(CONFIG_ME4000) += me4000/
84 +obj-$(CONFIG_VIDEO_GO7007) += go7007/
85 diff --git a/drivers/staging/go7007/Kconfig b/drivers/staging/go7007/Kconfig
87 index 0000000..57a121c
89 +++ b/drivers/staging/go7007/Kconfig
92 + tristate "Go 7007 support"
93 + depends on VIDEO_DEV && PCI && I2C && INPUT
94 + select VIDEOBUF_DMA_SG
97 + select VIDEO_TVEEPROM
101 + This is a video4linux driver for some wierd device...
103 + To compile this driver as a module, choose M here: the
104 + module will be called go7007
106 +config VIDEO_GO7007_USB
107 + tristate "Go 7007 USB support"
108 + depends on VIDEO_GO7007 && USB
111 + This is a video4linux driver for some wierd device...
113 + To compile this driver as a module, choose M here: the
114 + module will be called go7007-usb
116 diff --git a/drivers/staging/go7007/Makefile b/drivers/staging/go7007/Makefile
118 index 0000000..9b9310c
120 +++ b/drivers/staging/go7007/Makefile
122 +#obj-m += go7007.o go7007-usb.o snd-go7007.o wis-saa7115.o wis-tw9903.o \
123 + wis-uda1342.o wis-sony-tuner.o wis-saa7113.o wis-ov7640.o \
127 +obj-$(CONFIG_VIDEO_GO7007) += go7007.o
128 +obj-$(CONFIG_VIDEO_GO7007_USB) += go7007-usb.o
130 +go7007-objs += go7007-v4l2.o go7007-driver.o go7007-i2c.o go7007-fw.o snd-go7007.o
133 +#ifneq ($(SAA7134_BUILD),)
134 +#obj-m += saa7134-go7007.o
137 +EXTRA_CFLAGS += -Idrivers/staging/saa7134
138 +EXTRA_CFLAGS += -Idrivers/media/dvb/frontends
139 +EXTRA_CFLAGS += -Idrivers/media/dvb/dvb-core
140 diff --git a/drivers/staging/go7007/README b/drivers/staging/go7007/README
142 index 0000000..48f4476
144 +++ b/drivers/staging/go7007/README
147 + - checkpatch.pl cleanups
149 + - lots of little modules, should be merged together
150 + and added to the build.
152 + - handle churn in v4l layer.
154 +Please send patchs to Greg Kroah-Hartman <greg@kroah.com> and Cc: Ross
155 +Cohen <rcohen@snurgle.org> as well.
157 diff --git a/drivers/staging/go7007/go7007-driver.c b/drivers/staging/go7007/go7007-driver.c
159 index 0000000..5a336ff
161 +++ b/drivers/staging/go7007/go7007-driver.c
164 + * Copyright (C) 2005-2006 Micronas USA Inc.
166 + * This program is free software; you can redistribute it and/or modify
167 + * it under the terms of the GNU General Public License (Version 2) as
168 + * published by the Free Software Foundation.
170 + * This program is distributed in the hope that it will be useful,
171 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
172 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
173 + * GNU General Public License for more details.
175 + * You should have received a copy of the GNU General Public License
176 + * along with this program; if not, write to the Free Software Foundation,
177 + * Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
180 +#include <linux/module.h>
181 +#include <linux/version.h>
182 +#include <linux/init.h>
183 +#include <linux/delay.h>
184 +#include <linux/sched.h>
185 +#include <linux/spinlock.h>
186 +#include <linux/unistd.h>
187 +#include <linux/time.h>
188 +#include <linux/mm.h>
189 +#include <linux/vmalloc.h>
190 +#include <linux/device.h>
191 +#include <linux/i2c.h>
192 +#include <linux/firmware.h>
193 +#include <linux/semaphore.h>
194 +#include <linux/uaccess.h>
195 +#include <asm/system.h>
196 +#include <linux/videodev.h>
197 +#include <media/tuner.h>
198 +#include <media/v4l2-common.h>
200 +#include "go7007-priv.h"
201 +#include "wis-i2c.h"
204 + * Wait for an interrupt to be delivered from the GO7007SB and return
205 + * the associated value and data.
207 + * Must be called with the hw_lock held.
209 +int go7007_read_interrupt(struct go7007 *go, u16 *value, u16 *data)
211 + go->interrupt_available = 0;
212 + go->hpi_ops->read_interrupt(go);
213 + if (wait_event_timeout(go->interrupt_waitq,
214 + go->interrupt_available, 5*HZ) < 0) {
215 + printk(KERN_ERR "go7007: timeout waiting for read interrupt\n");
218 + if (!go->interrupt_available)
220 + go->interrupt_available = 0;
221 + *value = go->interrupt_value & 0xfffe;
222 + *data = go->interrupt_data;
225 +EXPORT_SYMBOL(go7007_read_interrupt);
228 + * Read a register/address on the GO7007SB.
230 + * Must be called with the hw_lock held.
232 +int go7007_read_addr(struct go7007 *go, u16 addr, u16 *data)
237 + if (go7007_write_interrupt(go, 0x0010, addr) < 0)
239 + while (count-- > 0) {
240 + if (go7007_read_interrupt(go, &value, data) == 0 &&
246 +EXPORT_SYMBOL(go7007_read_addr);
249 + * Send the boot firmware to the encoder, which just wakes it up and lets
250 + * us talk to the GPIO pins and on-board I2C adapter.
252 + * Must be called with the hw_lock held.
254 +static int go7007_load_encoder(struct go7007 *go)
256 + const struct firmware *fw_entry;
257 + char fw_name[] = "go7007fw.bin";
259 + int fw_len, rv = 0;
260 + u16 intr_val, intr_data;
262 + if (request_firmware(&fw_entry, fw_name, go->dev)) {
264 + "go7007: unable to load firmware from file \"%s\"\n",
268 + if (fw_entry->size < 16 || memcmp(fw_entry->data, "WISGO7007FW", 11)) {
269 + printk(KERN_ERR "go7007: file \"%s\" does not appear to be "
270 + "go7007 firmware\n", fw_name);
271 + release_firmware(fw_entry);
274 + fw_len = fw_entry->size - 16;
275 + bounce = kmalloc(fw_len, GFP_KERNEL);
276 + if (bounce == NULL) {
277 + printk(KERN_ERR "go7007: unable to allocate %d bytes for "
278 + "firmware transfer\n", fw_len);
279 + release_firmware(fw_entry);
282 + memcpy(bounce, fw_entry->data + 16, fw_len);
283 + release_firmware(fw_entry);
284 + if (go7007_interface_reset(go) < 0 ||
285 + go7007_send_firmware(go, bounce, fw_len) < 0 ||
286 + go7007_read_interrupt(go, &intr_val, &intr_data) < 0 ||
287 + (intr_val & ~0x1) != 0x5a5a) {
288 + printk(KERN_ERR "go7007: error transferring firmware\n");
296 + * Boot the encoder and register the I2C adapter if requested. Do the
297 + * minimum initialization necessary, since the board-specific code may
298 + * still need to probe the board ID.
300 + * Must NOT be called with the hw_lock held.
302 +int go7007_boot_encoder(struct go7007 *go, int init_i2c)
306 + down(&go->hw_lock);
307 + ret = go7007_load_encoder(go);
313 + if (go7007_i2c_init(go) < 0)
315 + go->i2c_adapter_online = 1;
318 +EXPORT_SYMBOL(go7007_boot_encoder);
321 + * Configure any hardware-related registers in the GO7007, such as GPIO
322 + * pins and bus parameters, which are board-specific. This assumes
323 + * the boot firmware has already been downloaded.
325 + * Must be called with the hw_lock held.
327 +static int go7007_init_encoder(struct go7007 *go)
329 + if (go->board_info->audio_flags & GO7007_AUDIO_I2S_MASTER) {
330 + go7007_write_addr(go, 0x1000, 0x0811);
331 + go7007_write_addr(go, 0x1000, 0x0c11);
333 + if (go->board_id == GO7007_BOARDID_MATRIX_REV) {
334 + /* Set GPIO pin 0 to be an output (audio clock control) */
335 + go7007_write_addr(go, 0x3c82, 0x0001);
336 + go7007_write_addr(go, 0x3c80, 0x00fe);
342 + * Send the boot firmware to the GO7007 and configure the registers. This
343 + * is the only way to stop the encoder once it has started streaming video.
345 + * Must be called with the hw_lock held.
347 +int go7007_reset_encoder(struct go7007 *go)
349 + if (go7007_load_encoder(go) < 0)
351 + return go7007_init_encoder(go);
355 + * Attempt to instantiate an I2C client by ID, probably loading a module.
357 +static int init_i2c_module(struct i2c_adapter *adapter, int id, int addr)
362 + case I2C_DRIVERID_WIS_SAA7115:
363 + modname = "wis-saa7115";
365 + case I2C_DRIVERID_WIS_SAA7113:
366 + modname = "wis-saa7113";
368 + case I2C_DRIVERID_WIS_UDA1342:
369 + modname = "wis-uda1342";
371 + case I2C_DRIVERID_WIS_SONY_TUNER:
372 + modname = "wis-sony-tuner";
374 + case I2C_DRIVERID_WIS_TW9903:
375 + modname = "wis-tw9903";
377 + case I2C_DRIVERID_WIS_TW2804:
378 + modname = "wis-tw2804";
380 + case I2C_DRIVERID_WIS_OV7640:
381 + modname = "wis-ov7640";
387 + if (modname != NULL)
388 + request_module(modname);
389 + if (wis_i2c_probe_device(adapter, id, addr) == 1)
391 + if (modname != NULL)
393 + "go7007: probing for module %s failed", modname);
396 + "go7007: sensor %u seems to be unsupported!\n", id);
401 + * Finalize the GO7007 hardware setup, register the on-board I2C adapter
402 + * (if used on this board), load the I2C client driver for the sensor
403 + * (SAA7115 or whatever) and other devices, and register the ALSA and V4L2
406 + * Must NOT be called with the hw_lock held.
408 +int go7007_register_encoder(struct go7007 *go)
412 + printk(KERN_INFO "go7007: registering new %s\n", go->name);
414 + down(&go->hw_lock);
415 + ret = go7007_init_encoder(go);
420 + if (!go->i2c_adapter_online &&
421 + go->board_info->flags & GO7007_BOARD_USE_ONBOARD_I2C) {
422 + if (go7007_i2c_init(go) < 0)
424 + go->i2c_adapter_online = 1;
426 + if (go->i2c_adapter_online) {
427 + for (i = 0; i < go->board_info->num_i2c_devs; ++i)
428 + init_i2c_module(&go->i2c_adapter,
429 + go->board_info->i2c_devs[i].id,
430 + go->board_info->i2c_devs[i].addr);
431 +#ifdef TUNER_SET_TYPE_ADDR
432 + if (go->tuner_type >= 0) {
433 + struct tuner_setup tun_setup = {
434 + .mode_mask = T_ANALOG_TV,
435 + .addr = ADDR_UNSET,
436 + .type = go->tuner_type
438 + i2c_clients_command(&go->i2c_adapter,
439 + TUNER_SET_TYPE_ADDR, &tun_setup);
442 + if (go->tuner_type >= 0)
443 + i2c_clients_command(&go->i2c_adapter,
444 + TUNER_SET_TYPE, &go->tuner_type);
446 + if (go->board_id == GO7007_BOARDID_ADLINK_MPG24)
447 + i2c_clients_command(&go->i2c_adapter,
448 + DECODER_SET_CHANNEL, &go->channel_number);
450 + if (go->board_info->flags & GO7007_BOARD_HAS_AUDIO) {
451 + go->audio_enabled = 1;
452 + go7007_snd_init(go);
454 + return go7007_v4l2_init(go);
456 +EXPORT_SYMBOL(go7007_register_encoder);
459 + * Send the encode firmware to the encoder, which will cause it
460 + * to immediately start delivering the video and audio streams.
462 + * Must be called with the hw_lock held.
464 +int go7007_start_encoder(struct go7007 *go)
467 + int fw_len, rv = 0, i;
468 + u16 intr_val, intr_data;
470 + go->modet_enable = 0;
472 + for (i = 0; i < 4; ++i) {
473 + if (go->modet[i].enable) {
474 + go->modet_enable = 1;
477 + go->modet[i].pixel_threshold = 32767;
478 + go->modet[i].motion_threshold = 32767;
479 + go->modet[i].mb_threshold = 32767;
482 + if (go7007_construct_fw_image(go, &fw, &fw_len) < 0)
485 + if (go7007_send_firmware(go, fw, fw_len) < 0 ||
486 + go7007_read_interrupt(go, &intr_val, &intr_data) < 0) {
487 + printk(KERN_ERR "go7007: error transferring firmware\n");
492 + go->state = STATE_DATA;
493 + go->parse_length = 0;
494 + go->seen_frame = 0;
495 + if (go7007_stream_start(go) < 0) {
496 + printk(KERN_ERR "go7007: error starting stream transfer\n");
507 + * Store a byte in the current video buffer, if there is one.
509 +static inline void store_byte(struct go7007_buffer *gobuf, u8 byte)
511 + if (gobuf != NULL && gobuf->bytesused < GO7007_BUF_SIZE) {
512 + unsigned int pgidx = gobuf->offset >> PAGE_SHIFT;
513 + unsigned int pgoff = gobuf->offset & ~PAGE_MASK;
515 + *((u8 *)page_address(gobuf->pages[pgidx]) + pgoff) = byte;
517 + ++gobuf->bytesused;
522 + * Deliver the last video buffer and get a new one to start writing to.
524 +static void frame_boundary(struct go7007 *go)
526 + struct go7007_buffer *gobuf;
529 + if (go->active_buf) {
530 + if (go->active_buf->modet_active) {
531 + if (go->active_buf->bytesused + 216 < GO7007_BUF_SIZE) {
532 + for (i = 0; i < 216; ++i)
533 + store_byte(go->active_buf,
534 + go->active_map[i]);
535 + go->active_buf->bytesused -= 216;
537 + go->active_buf->modet_active = 0;
539 + go->active_buf->state = BUF_STATE_DONE;
540 + wake_up_interruptible(&go->frame_waitq);
541 + go->active_buf = NULL;
543 + list_for_each_entry(gobuf, &go->stream, stream)
544 + if (gobuf->state == BUF_STATE_QUEUED) {
545 + gobuf->seq = go->next_seq;
546 + do_gettimeofday(&gobuf->timestamp);
547 + go->active_buf = gobuf;
553 +static void write_bitmap_word(struct go7007 *go)
555 + int x, y, i, stride = ((go->width >> 4) + 7) >> 3;
557 + for (i = 0; i < 16; ++i) {
558 + y = (((go->parse_length - 1) << 3) + i) / (go->width >> 4);
559 + x = (((go->parse_length - 1) << 3) + i) % (go->width >> 4);
560 + go->active_map[stride * y + (x >> 3)] |=
561 + (go->modet_word & 1) << (x & 0x7);
562 + go->modet_word >>= 1;
567 + * Parse a chunk of the video stream into frames. The frames are not
568 + * delimited by the hardware, so we have to parse the frame boundaries
569 + * based on the type of video stream we're receiving.
571 +void go7007_parse_video_stream(struct go7007 *go, u8 *buf, int length)
573 + int i, seq_start_code = -1, frame_start_code = -1;
575 + spin_lock(&go->spinlock);
577 + switch (go->format) {
578 + case GO7007_FORMAT_MPEG4:
579 + seq_start_code = 0xB0;
580 + frame_start_code = 0xB6;
582 + case GO7007_FORMAT_MPEG1:
583 + case GO7007_FORMAT_MPEG2:
584 + seq_start_code = 0xB3;
585 + frame_start_code = 0x00;
589 + for (i = 0; i < length; ++i) {
590 + if (go->active_buf != NULL &&
591 + go->active_buf->bytesused >= GO7007_BUF_SIZE - 3) {
592 + printk(KERN_DEBUG "go7007: dropping oversized frame\n");
593 + go->active_buf->offset -= go->active_buf->bytesused;
594 + go->active_buf->bytesused = 0;
595 + go->active_buf->modet_active = 0;
596 + go->active_buf = NULL;
599 + switch (go->state) {
603 + go->state = STATE_00;
606 + go->state = STATE_FF;
609 + store_byte(go->active_buf, buf[i]);
616 + go->state = STATE_00_00;
619 + store_byte(go->active_buf, 0x00);
620 + go->state = STATE_FF;
623 + store_byte(go->active_buf, 0x00);
624 + store_byte(go->active_buf, buf[i]);
625 + go->state = STATE_DATA;
632 + store_byte(go->active_buf, 0x00);
633 + /* go->state remains STATE_00_00 */
636 + go->state = STATE_00_00_01;
639 + store_byte(go->active_buf, 0x00);
640 + store_byte(go->active_buf, 0x00);
641 + go->state = STATE_FF;
644 + store_byte(go->active_buf, 0x00);
645 + store_byte(go->active_buf, 0x00);
646 + store_byte(go->active_buf, buf[i]);
647 + go->state = STATE_DATA;
651 + case STATE_00_00_01:
652 + /* If this is the start of a new MPEG frame,
653 + * get a new buffer */
654 + if ((go->format == GO7007_FORMAT_MPEG1 ||
655 + go->format == GO7007_FORMAT_MPEG2 ||
656 + go->format == GO7007_FORMAT_MPEG4) &&
657 + (buf[i] == seq_start_code ||
658 + buf[i] == 0xB8 || /* GOP code */
659 + buf[i] == frame_start_code)) {
660 + if (go->active_buf == NULL || go->seen_frame)
661 + frame_boundary(go);
662 + if (buf[i] == frame_start_code) {
663 + if (go->active_buf != NULL)
664 + go->active_buf->frame_offset =
665 + go->active_buf->offset;
666 + go->seen_frame = 1;
668 + go->seen_frame = 0;
671 + /* Handle any special chunk types, or just write the
672 + * start code to the (potentially new) buffer */
674 + case 0xF5: /* timestamp */
675 + go->parse_length = 12;
676 + go->state = STATE_UNPARSED;
678 + case 0xF6: /* vbi */
679 + go->state = STATE_VBI_LEN_A;
681 + case 0xF8: /* MD map */
682 + go->parse_length = 0;
683 + memset(go->active_map, 0,
684 + sizeof(go->active_map));
685 + go->state = STATE_MODET_MAP;
687 + case 0xFF: /* Potential JPEG start code */
688 + store_byte(go->active_buf, 0x00);
689 + store_byte(go->active_buf, 0x00);
690 + store_byte(go->active_buf, 0x01);
691 + go->state = STATE_FF;
694 + store_byte(go->active_buf, 0x00);
695 + store_byte(go->active_buf, 0x00);
696 + store_byte(go->active_buf, 0x01);
697 + store_byte(go->active_buf, buf[i]);
698 + go->state = STATE_DATA;
705 + store_byte(go->active_buf, 0xFF);
706 + go->state = STATE_00;
709 + store_byte(go->active_buf, 0xFF);
710 + /* go->state remains STATE_FF */
713 + if (go->format == GO7007_FORMAT_MJPEG)
714 + frame_boundary(go);
717 + store_byte(go->active_buf, 0xFF);
718 + store_byte(go->active_buf, buf[i]);
719 + go->state = STATE_DATA;
723 + case STATE_VBI_LEN_A:
724 + go->parse_length = buf[i] << 8;
725 + go->state = STATE_VBI_LEN_B;
727 + case STATE_VBI_LEN_B:
728 + go->parse_length |= buf[i];
729 + if (go->parse_length > 0)
730 + go->state = STATE_UNPARSED;
732 + go->state = STATE_DATA;
734 + case STATE_MODET_MAP:
735 + if (go->parse_length < 204) {
736 + if (go->parse_length & 1) {
737 + go->modet_word |= buf[i];
738 + write_bitmap_word(go);
740 + go->modet_word = buf[i] << 8;
741 + } else if (go->parse_length == 207 && go->active_buf) {
742 + go->active_buf->modet_active = buf[i];
744 + if (++go->parse_length == 208)
745 + go->state = STATE_DATA;
747 + case STATE_UNPARSED:
748 + if (--go->parse_length == 0)
749 + go->state = STATE_DATA;
754 + spin_unlock(&go->spinlock);
756 +EXPORT_SYMBOL(go7007_parse_video_stream);
759 + * Allocate a new go7007 struct. Used by the hardware-specific probe.
761 +struct go7007 *go7007_alloc(struct go7007_board_info *board, struct device *dev)
766 + go = kmalloc(sizeof(struct go7007), GFP_KERNEL);
770 + go->board_info = board;
772 + go->tuner_type = -1;
773 + go->channel_number = 0;
775 + init_MUTEX(&go->hw_lock);
776 + init_waitqueue_head(&go->frame_waitq);
777 + spin_lock_init(&go->spinlock);
778 + go->video_dev = NULL;
780 + go->status = STATUS_INIT;
781 + memset(&go->i2c_adapter, 0, sizeof(go->i2c_adapter));
782 + go->i2c_adapter_online = 0;
783 + go->interrupt_available = 0;
784 + init_waitqueue_head(&go->interrupt_waitq);
787 + if (board->sensor_flags & GO7007_SENSOR_TV) {
788 + go->standard = GO7007_STD_NTSC;
791 + go->sensor_framerate = 30000;
793 + go->standard = GO7007_STD_OTHER;
794 + go->width = board->sensor_width;
795 + go->height = board->sensor_height;
796 + go->sensor_framerate = board->sensor_framerate;
798 + go->encoder_v_offset = board->sensor_v_offset;
799 + go->encoder_h_offset = board->sensor_h_offset;
800 + go->encoder_h_halve = 0;
801 + go->encoder_v_halve = 0;
802 + go->encoder_subsample = 0;
804 + go->format = GO7007_FORMAT_MJPEG;
805 + go->bitrate = 1500000;
808 + go->aspect_ratio = GO7007_RATIO_1_1;
811 + go->closed_gop = 0;
812 + go->repeat_seqhead = 0;
813 + go->seq_header_enable = 0;
814 + go->gop_header_enable = 0;
816 + go->interlace_coding = 0;
817 + for (i = 0; i < 4; ++i)
818 + go->modet[i].enable = 0;;
819 + for (i = 0; i < 1624; ++i)
820 + go->modet_map[i] = 0;
821 + go->audio_deliver = NULL;
822 + go->audio_enabled = 0;
823 + INIT_LIST_HEAD(&go->stream);
827 +EXPORT_SYMBOL(go7007_alloc);
830 + * Detach and unregister the encoder. The go7007 struct won't be freed
831 + * until v4l2 finishes releasing its resources and all associated fds are
832 + * closed by applications.
834 +void go7007_remove(struct go7007 *go)
836 + if (go->i2c_adapter_online) {
837 + if (i2c_del_adapter(&go->i2c_adapter) == 0)
838 + go->i2c_adapter_online = 0;
841 + "go7007: error removing I2C adapter!\n");
844 + if (go->audio_enabled)
845 + go7007_snd_remove(go);
846 + go7007_v4l2_remove(go);
848 +EXPORT_SYMBOL(go7007_remove);
850 +MODULE_LICENSE("GPL v2");
851 diff --git a/drivers/staging/go7007/go7007-fw.c b/drivers/staging/go7007/go7007-fw.c
853 index 0000000..c2aea10
855 +++ b/drivers/staging/go7007/go7007-fw.c
858 + * Copyright (C) 2005-2006 Micronas USA Inc.
860 + * This program is free software; you can redistribute it and/or modify
861 + * it under the terms of the GNU General Public License (Version 2) as
862 + * published by the Free Software Foundation.
864 + * This program is distributed in the hope that it will be useful,
865 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
866 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
867 + * GNU General Public License for more details.
869 + * You should have received a copy of the GNU General Public License
870 + * along with this program; if not, write to the Free Software Foundation,
871 + * Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
875 + * This file contains code to generate a firmware image for the GO7007SB
876 + * encoder. Much of the firmware is read verbatim from a file, but some of
877 + * it concerning bitrate control and other things that can be configured at
878 + * run-time are generated dynamically. Note that the format headers
879 + * generated here do not affect the functioning of the encoder; they are
880 + * merely parroted back to the host at the start of each frame.
883 +#include <linux/module.h>
884 +#include <linux/init.h>
885 +#include <linux/version.h>
886 +#include <linux/time.h>
887 +#include <linux/mm.h>
888 +#include <linux/device.h>
889 +#include <linux/i2c.h>
890 +#include <linux/firmware.h>
891 +#include <asm/byteorder.h>
893 +#include "go7007-priv.h"
895 +/* Constants used in the source firmware image to describe code segments */
897 +#define FLAG_MODE_MJPEG (1)
898 +#define FLAG_MODE_MPEG1 (1<<1)
899 +#define FLAG_MODE_MPEG2 (1<<2)
900 +#define FLAG_MODE_MPEG4 (1<<3)
901 +#define FLAG_MODE_H263 (1<<4)
902 +#define FLAG_MODE_ALL (FLAG_MODE_MJPEG | FLAG_MODE_MPEG1 | \
903 + FLAG_MODE_MPEG2 | FLAG_MODE_MPEG4 | \
905 +#define FLAG_SPECIAL (1<<8)
907 +#define SPECIAL_FRM_HEAD 0
908 +#define SPECIAL_BRC_CTRL 1
909 +#define SPECIAL_CONFIG 2
910 +#define SPECIAL_SEQHEAD 3
911 +#define SPECIAL_AV_SYNC 4
912 +#define SPECIAL_FINAL 5
913 +#define SPECIAL_AUDIO 6
914 +#define SPECIAL_MODET 7
916 +/* Little data class for creating MPEG headers bit-by-bit */
919 + unsigned char *p; /* destination */
920 + u32 a; /* collects bits at the top of the variable */
921 + int b; /* bit position of most recently-written bit */
922 + int len; /* written out so far */
925 +#define CODE_GEN(name, dest) struct code_gen name = { dest, 0, 32, 0 }
927 +#define CODE_ADD(name, val, length) do { \
928 + name.b -= (length); \
929 + name.a |= (val) << name.b; \
930 + while (name.b <= 24) { \
931 + *name.p = name.a >> 24; \
939 +#define CODE_LENGTH(name) (name.len + (32 - name.b))
941 +/* Tables for creating the bitrate control data */
943 +static const s16 converge_speed_ip[101] = {
944 + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
945 + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
946 + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
947 + 1, 1, 1, 1, 1, 1, 1, 1, 1, 2,
948 + 2, 2, 2, 2, 2, 2, 2, 2, 2, 3,
949 + 3, 3, 3, 3, 3, 4, 4, 4, 4, 4,
950 + 5, 5, 5, 6, 6, 6, 7, 7, 8, 8,
951 + 9, 10, 10, 11, 12, 13, 14, 15, 16, 17,
952 + 19, 20, 22, 23, 25, 27, 30, 32, 35, 38,
953 + 41, 45, 49, 53, 58, 63, 69, 76, 83, 91,
957 +static const s16 converge_speed_ipb[101] = {
958 + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
959 + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
960 + 3, 3, 3, 3, 3, 4, 4, 4, 4, 4,
961 + 4, 4, 4, 4, 5, 5, 5, 5, 5, 6,
962 + 6, 6, 6, 7, 7, 7, 7, 8, 8, 9,
963 + 9, 9, 10, 10, 11, 12, 12, 13, 14, 14,
964 + 15, 16, 17, 18, 19, 20, 22, 23, 25, 26,
965 + 28, 30, 32, 34, 37, 40, 42, 46, 49, 53,
966 + 57, 61, 66, 71, 77, 83, 90, 97, 106, 115,
967 + 125, 135, 147, 161, 175, 191, 209, 228, 249, 273,
971 +static const s16 LAMBDA_table[4][101] = {
972 + { 16, 16, 16, 16, 17, 17, 17, 18, 18, 18,
973 + 19, 19, 19, 20, 20, 20, 21, 21, 22, 22,
974 + 22, 23, 23, 24, 24, 25, 25, 25, 26, 26,
975 + 27, 27, 28, 28, 29, 29, 30, 31, 31, 32,
976 + 32, 33, 33, 34, 35, 35, 36, 37, 37, 38,
977 + 39, 39, 40, 41, 42, 42, 43, 44, 45, 46,
978 + 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
979 + 56, 57, 58, 59, 60, 61, 62, 63, 64, 65,
980 + 67, 68, 69, 70, 72, 73, 74, 76, 77, 78,
981 + 80, 81, 83, 84, 86, 87, 89, 90, 92, 94,
985 + 20, 20, 20, 21, 21, 21, 22, 22, 23, 23,
986 + 23, 24, 24, 25, 25, 26, 26, 27, 27, 28,
987 + 28, 29, 29, 30, 30, 31, 31, 32, 33, 33,
988 + 34, 34, 35, 36, 36, 37, 38, 38, 39, 40,
989 + 40, 41, 42, 43, 43, 44, 45, 46, 47, 48,
990 + 48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
991 + 58, 59, 60, 61, 62, 64, 65, 66, 67, 68,
992 + 70, 71, 72, 73, 75, 76, 78, 79, 80, 82,
993 + 83, 85, 86, 88, 90, 91, 93, 95, 96, 98,
994 + 100, 102, 103, 105, 107, 109, 111, 113, 115, 117,
998 + 24, 24, 24, 25, 25, 26, 26, 27, 27, 28,
999 + 28, 29, 29, 30, 30, 31, 31, 32, 33, 33,
1000 + 34, 34, 35, 36, 36, 37, 38, 38, 39, 40,
1001 + 41, 41, 42, 43, 44, 44, 45, 46, 47, 48,
1002 + 49, 50, 50, 51, 52, 53, 54, 55, 56, 57,
1003 + 58, 59, 60, 62, 63, 64, 65, 66, 67, 69,
1004 + 70, 71, 72, 74, 75, 76, 78, 79, 81, 82,
1005 + 84, 85, 87, 88, 90, 92, 93, 95, 97, 98,
1006 + 100, 102, 104, 106, 108, 110, 112, 114, 116, 118,
1007 + 120, 122, 124, 127, 129, 131, 134, 136, 138, 141,
1011 + 32, 32, 33, 33, 34, 34, 35, 36, 36, 37,
1012 + 38, 38, 39, 40, 41, 41, 42, 43, 44, 44,
1013 + 45, 46, 47, 48, 49, 50, 50, 51, 52, 53,
1014 + 54, 55, 56, 57, 58, 59, 60, 62, 63, 64,
1015 + 65, 66, 67, 69, 70, 71, 72, 74, 75, 76,
1016 + 78, 79, 81, 82, 84, 85, 87, 88, 90, 92,
1017 + 93, 95, 97, 98, 100, 102, 104, 106, 108, 110,
1018 + 112, 114, 116, 118, 120, 122, 124, 127, 129, 131,
1019 + 134, 136, 139, 141, 144, 146, 149, 152, 154, 157,
1020 + 160, 163, 166, 169, 172, 175, 178, 181, 185, 188,
1025 +/* MPEG blank frame generation tables */
1027 +enum mpeg_frame_type {
1035 +static const u32 addrinctab[33][2] = {
1036 + { 0x01, 1 }, { 0x03, 3 }, { 0x02, 3 }, { 0x03, 4 },
1037 + { 0x02, 4 }, { 0x03, 5 }, { 0x02, 5 }, { 0x07, 7 },
1038 + { 0x06, 7 }, { 0x0b, 8 }, { 0x0a, 8 }, { 0x09, 8 },
1039 + { 0x08, 8 }, { 0x07, 8 }, { 0x06, 8 }, { 0x17, 10 },
1040 + { 0x16, 10 }, { 0x15, 10 }, { 0x14, 10 }, { 0x13, 10 },
1041 + { 0x12, 10 }, { 0x23, 11 }, { 0x22, 11 }, { 0x21, 11 },
1042 + { 0x20, 11 }, { 0x1f, 11 }, { 0x1e, 11 }, { 0x1d, 11 },
1043 + { 0x1c, 11 }, { 0x1b, 11 }, { 0x1a, 11 }, { 0x19, 11 },
1047 +/* Standard JPEG tables */
1049 +static const u8 default_intra_quant_table[] = {
1050 + 8, 16, 19, 22, 26, 27, 29, 34,
1051 + 16, 16, 22, 24, 27, 29, 34, 37,
1052 + 19, 22, 26, 27, 29, 34, 34, 38,
1053 + 22, 22, 26, 27, 29, 34, 37, 40,
1054 + 22, 26, 27, 29, 32, 35, 40, 48,
1055 + 26, 27, 29, 32, 35, 40, 48, 58,
1056 + 26, 27, 29, 34, 38, 46, 56, 69,
1057 + 27, 29, 35, 38, 46, 56, 69, 83
1060 +static const u8 bits_dc_luminance[] = {
1061 + 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0
1064 +static const u8 val_dc_luminance[] = {
1065 + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
1068 +static const u8 bits_dc_chrominance[] = {
1069 + 0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0
1072 +static const u8 val_dc_chrominance[] = {
1073 + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
1076 +static const u8 bits_ac_luminance[] = {
1077 + 0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d
1080 +static const u8 val_ac_luminance[] = {
1081 + 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
1082 + 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
1083 + 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
1084 + 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
1085 + 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
1086 + 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
1087 + 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
1088 + 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
1089 + 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
1090 + 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
1091 + 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
1092 + 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
1093 + 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
1094 + 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
1095 + 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
1096 + 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
1097 + 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
1098 + 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
1099 + 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
1100 + 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
1104 +static const u8 bits_ac_chrominance[] = {
1105 + 0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77
1108 +static const u8 val_ac_chrominance[] = {
1109 + 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
1110 + 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
1111 + 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
1112 + 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
1113 + 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
1114 + 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
1115 + 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
1116 + 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
1117 + 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
1118 + 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
1119 + 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
1120 + 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
1121 + 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
1122 + 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
1123 + 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
1124 + 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
1125 + 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
1126 + 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
1127 + 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
1128 + 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
1132 +/* Zig-zag mapping for quant table
1134 + * OK, let's do this mapping on the actual table above so it doesn't have
1135 + * to be done on the fly.
1137 +static const int zz[64] = {
1138 + 0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5,
1139 + 12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13, 6, 7, 14, 21, 28,
1140 + 35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, 30, 37, 44, 51,
1141 + 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63
1144 +static int copy_packages(u16 *dest, u16 *src, int pkg_cnt, int space)
1146 + int i, cnt = pkg_cnt * 32;
1151 + for (i = 0; i < cnt; ++i)
1152 + dest[i] = __cpu_to_le16(src[i]);
1157 +static int mjpeg_frame_header(struct go7007 *go, unsigned char *buf, int q)
1166 + buf[p++] = 2 + 65;
1168 + buf[p++] = default_intra_quant_table[0];
1169 + for (i = 1; i < 64; ++i)
1170 + /* buf[p++] = (default_intra_quant_table[i] * q) >> 3; */
1171 + buf[p++] = (default_intra_quant_table[zz[i]] * q) >> 3;
1177 + buf[p++] = go->height >> 8;
1178 + buf[p++] = go->height & 0xff;
1179 + buf[p++] = go->width >> 8;
1180 + buf[p++] = go->width & 0xff;
1193 + buf[p++] = 418 >> 8;
1194 + buf[p++] = 418 & 0xff;
1196 + memcpy(buf + p, bits_dc_luminance + 1, 16);
1198 + memcpy(buf + p, val_dc_luminance, sizeof(val_dc_luminance));
1199 + p += sizeof(val_dc_luminance);
1201 + memcpy(buf + p, bits_dc_chrominance + 1, 16);
1203 + memcpy(buf + p, val_dc_chrominance, sizeof(val_dc_chrominance));
1204 + p += sizeof(val_dc_chrominance);
1206 + memcpy(buf + p, bits_ac_luminance + 1, 16);
1208 + memcpy(buf + p, val_ac_luminance, sizeof(val_ac_luminance));
1209 + p += sizeof(val_ac_luminance);
1211 + memcpy(buf + p, bits_ac_chrominance + 1, 16);
1213 + memcpy(buf + p, val_ac_chrominance, sizeof(val_ac_chrominance));
1214 + p += sizeof(val_ac_chrominance);
1232 +static int gen_mjpeghdr_to_package(struct go7007 *go, u16 *code, int space)
1236 + unsigned int addr = 0x19;
1237 + int size = 0, i, off = 0, chunk;
1239 + buf = kmalloc(4096, GFP_KERNEL);
1240 + if (buf == NULL) {
1241 + printk(KERN_ERR "go7007: unable to allocate 4096 bytes for "
1242 + "firmware construction\n");
1245 + memset(buf, 0, 4096);
1247 + for (i = 1; i < 32; ++i) {
1248 + mjpeg_frame_header(go, buf + size, i);
1251 + chunk = mjpeg_frame_header(go, buf + size, 1);
1252 + memmove(buf + size, buf + size + 80, chunk - 80);
1253 + size += chunk - 80;
1255 + for (i = 0; i < size; i += chunk * 2) {
1256 + if (space - off < 32) {
1261 + code[off + 1] = __cpu_to_le16(0x8000 | mem);
1264 + if (mem + chunk > 0x4000)
1265 + chunk = 0x4000 - mem;
1266 + if (i + 2 * chunk > size)
1267 + chunk = (size - i) / 2;
1270 + code[off] = __cpu_to_le16(0x4000 | chunk);
1271 + code[off + 31] = __cpu_to_le16(addr++);
1274 + code[off] = __cpu_to_le16(0x1000 | 28);
1275 + code[off + 31] = 0;
1279 + memcpy(&code[off + 2], buf + i, chunk * 2);
1287 +static int mpeg1_frame_header(struct go7007 *go, unsigned char *buf,
1288 + int modulo, int pict_struct, enum mpeg_frame_type frame)
1290 + int i, j, mb_code, mb_len;
1291 + int rows = go->interlace_coding ? go->height / 32 : go->height / 16;
1292 + CODE_GEN(c, buf + 6);
1307 + case BFRAME_BIDIR:
1311 + default: /* keep the compiler happy */
1312 + mb_code = mb_len = 0;
1316 + CODE_ADD(c, frame == PFRAME ? 0x2 : 0x3, 13);
1317 + CODE_ADD(c, 0xffff, 16);
1318 + CODE_ADD(c, go->format == GO7007_FORMAT_MPEG2 ? 0x7 : 0x4, 4);
1319 + if (frame != PFRAME)
1320 + CODE_ADD(c, go->format == GO7007_FORMAT_MPEG2 ? 0x7 : 0x4, 4);
1322 + CODE_ADD(c, 0, 4); /* Is this supposed to be here?? */
1323 + CODE_ADD(c, 0, 3); /* What is this?? */
1324 + /* Byte-align with zeros */
1325 + j = 8 - (CODE_LENGTH(c) % 8);
1327 + CODE_ADD(c, 0, j);
1329 + if (go->format == GO7007_FORMAT_MPEG2) {
1330 + CODE_ADD(c, 0x1, 24);
1331 + CODE_ADD(c, 0xb5, 8);
1332 + CODE_ADD(c, 0x844, 12);
1333 + CODE_ADD(c, frame == PFRAME ? 0xff : 0x44, 8);
1334 + if (go->interlace_coding) {
1335 + CODE_ADD(c, pict_struct, 4);
1337 + CODE_ADD(c, 0x000, 11);
1339 + CODE_ADD(c, 0x200, 11);
1341 + CODE_ADD(c, 0x3, 4);
1342 + CODE_ADD(c, 0x20c, 11);
1344 + /* Byte-align with zeros */
1345 + j = 8 - (CODE_LENGTH(c) % 8);
1347 + CODE_ADD(c, 0, j);
1350 + for (i = 0; i < rows; ++i) {
1351 + CODE_ADD(c, 1, 24);
1352 + CODE_ADD(c, i + 1, 8);
1353 + CODE_ADD(c, 0x2, 6);
1354 + CODE_ADD(c, 0x1, 1);
1355 + CODE_ADD(c, mb_code, mb_len);
1356 + if (go->interlace_coding) {
1357 + CODE_ADD(c, 0x1, 2);
1358 + CODE_ADD(c, pict_struct == 1 ? 0x0 : 0x1, 1);
1360 + if (frame == BFRAME_BIDIR) {
1361 + CODE_ADD(c, 0x3, 2);
1362 + if (go->interlace_coding)
1363 + CODE_ADD(c, pict_struct == 1 ? 0x0 : 0x1, 1);
1365 + CODE_ADD(c, 0x3, 2);
1366 + for (j = (go->width >> 4) - 2; j >= 33; j -= 33)
1367 + CODE_ADD(c, 0x8, 11);
1368 + CODE_ADD(c, addrinctab[j][0], addrinctab[j][1]);
1369 + CODE_ADD(c, mb_code, mb_len);
1370 + if (go->interlace_coding) {
1371 + CODE_ADD(c, 0x1, 2);
1372 + CODE_ADD(c, pict_struct == 1 ? 0x0 : 0x1, 1);
1374 + if (frame == BFRAME_BIDIR) {
1375 + CODE_ADD(c, 0x3, 2);
1376 + if (go->interlace_coding)
1377 + CODE_ADD(c, pict_struct == 1 ? 0x0 : 0x1, 1);
1379 + CODE_ADD(c, 0x3, 2);
1381 + /* Byte-align with zeros */
1382 + j = 8 - (CODE_LENGTH(c) % 8);
1384 + CODE_ADD(c, 0, j);
1387 + i = CODE_LENGTH(c) + 4 * 8;
1395 +static int mpeg1_sequence_header(struct go7007 *go, unsigned char *buf, int ext)
1397 + int i, aspect_ratio, picture_rate;
1398 + CODE_GEN(c, buf + 6);
1400 + if (go->format == GO7007_FORMAT_MPEG1) {
1401 + switch (go->aspect_ratio) {
1402 + case GO7007_RATIO_4_3:
1403 + aspect_ratio = go->standard == GO7007_STD_NTSC ? 3 : 2;
1405 + case GO7007_RATIO_16_9:
1406 + aspect_ratio = go->standard == GO7007_STD_NTSC ? 5 : 4;
1413 + switch (go->aspect_ratio) {
1414 + case GO7007_RATIO_4_3:
1417 + case GO7007_RATIO_16_9:
1425 + switch (go->sensor_framerate) {
1433 + picture_rate = go->interlace_coding ? 6 : 3;
1436 + picture_rate = go->interlace_coding ? 7 : 4;
1439 + picture_rate = go->interlace_coding ? 8 : 5;
1442 + picture_rate = 5; /* 30 fps seems like a reasonable default */
1446 + CODE_ADD(c, go->width, 12);
1447 + CODE_ADD(c, go->height, 12);
1448 + CODE_ADD(c, aspect_ratio, 4);
1449 + CODE_ADD(c, picture_rate, 4);
1450 + CODE_ADD(c, go->format == GO7007_FORMAT_MPEG2 ? 20000 : 0x3ffff, 18);
1451 + CODE_ADD(c, 1, 1);
1452 + CODE_ADD(c, go->format == GO7007_FORMAT_MPEG2 ? 112 : 20, 10);
1453 + CODE_ADD(c, 0, 3);
1455 + /* Byte-align with zeros */
1456 + i = 8 - (CODE_LENGTH(c) % 8);
1458 + CODE_ADD(c, 0, i);
1460 + if (go->format == GO7007_FORMAT_MPEG2) {
1461 + CODE_ADD(c, 0x1, 24);
1462 + CODE_ADD(c, 0xb5, 8);
1463 + CODE_ADD(c, 0x148, 12);
1464 + if (go->interlace_coding)
1465 + CODE_ADD(c, 0x20001, 20);
1467 + CODE_ADD(c, 0xa0001, 20);
1468 + CODE_ADD(c, 0, 16);
1470 + /* Byte-align with zeros */
1471 + i = 8 - (CODE_LENGTH(c) % 8);
1473 + CODE_ADD(c, 0, i);
1476 + CODE_ADD(c, 0x1, 24);
1477 + CODE_ADD(c, 0xb52, 12);
1478 + CODE_ADD(c, go->standard == GO7007_STD_NTSC ? 2 : 1, 3);
1479 + CODE_ADD(c, 0x105, 9);
1480 + CODE_ADD(c, 0x505, 16);
1481 + CODE_ADD(c, go->width, 14);
1482 + CODE_ADD(c, 1, 1);
1483 + CODE_ADD(c, go->height, 14);
1485 + /* Byte-align with zeros */
1486 + i = 8 - (CODE_LENGTH(c) % 8);
1488 + CODE_ADD(c, 0, i);
1492 + i = CODE_LENGTH(c) + 4 * 8;
1493 + buf[0] = i & 0xff;
1502 +static int gen_mpeg1hdr_to_package(struct go7007 *go,
1503 + u16 *code, int space, int *framelen)
1507 + unsigned int addr = 0x19;
1508 + int i, off = 0, chunk;
1510 + buf = kmalloc(5120, GFP_KERNEL);
1511 + if (buf == NULL) {
1512 + printk(KERN_ERR "go7007: unable to allocate 5120 bytes for "
1513 + "firmware construction\n");
1516 + memset(buf, 0, 5120);
1517 + framelen[0] = mpeg1_frame_header(go, buf, 0, 1, PFRAME);
1518 + if (go->interlace_coding)
1519 + framelen[0] += mpeg1_frame_header(go, buf + framelen[0] / 8,
1521 + buf[0] = framelen[0] & 0xff;
1522 + buf[1] = framelen[0] >> 8;
1524 + framelen[1] = mpeg1_frame_header(go, buf + i, 0, 1, BFRAME_PRE);
1525 + if (go->interlace_coding)
1526 + framelen[1] += mpeg1_frame_header(go, buf + i + framelen[1] / 8,
1527 + 0, 2, BFRAME_PRE);
1528 + buf[i] = framelen[1] & 0xff;
1529 + buf[i + 1] = framelen[1] >> 8;
1531 + framelen[2] = mpeg1_frame_header(go, buf + i, 0, 1, BFRAME_POST);
1532 + if (go->interlace_coding)
1533 + framelen[2] += mpeg1_frame_header(go, buf + i + framelen[2] / 8,
1534 + 0, 2, BFRAME_POST);
1535 + buf[i] = framelen[2] & 0xff;
1536 + buf[i + 1] = framelen[2] >> 8;
1538 + framelen[3] = mpeg1_frame_header(go, buf + i, 0, 1, BFRAME_BIDIR);
1539 + if (go->interlace_coding)
1540 + framelen[3] += mpeg1_frame_header(go, buf + i + framelen[3] / 8,
1541 + 0, 2, BFRAME_BIDIR);
1542 + buf[i] = framelen[3] & 0xff;
1543 + buf[i + 1] = framelen[3] >> 8;
1545 + mpeg1_sequence_header(go, buf + i, 0);
1547 + for (i = 0; i < 5120; i += chunk * 2) {
1548 + if (space - off < 32) {
1553 + code[off + 1] = __cpu_to_le16(0x8000 | mem);
1556 + if (mem + chunk > 0x4000)
1557 + chunk = 0x4000 - mem;
1558 + if (i + 2 * chunk > 5120)
1559 + chunk = (5120 - i) / 2;
1562 + code[off] = __cpu_to_le16(0x4000 | chunk);
1563 + code[off + 31] = __cpu_to_le16(addr);
1564 + if (mem + chunk == 0x4000) {
1569 + code[off] = __cpu_to_le16(0x1000 | 28);
1570 + code[off + 31] = 0;
1574 + memcpy(&code[off + 2], buf + i, chunk * 2);
1582 +static int vti_bitlen(struct go7007 *go)
1584 + unsigned int i, max_time_incr = go->sensor_framerate / go->fps_scale;
1586 + for (i = 31; (max_time_incr & ((1 << i) - 1)) == max_time_incr; --i);
1590 +static int mpeg4_frame_header(struct go7007 *go, unsigned char *buf,
1591 + int modulo, enum mpeg_frame_type frame)
1594 + CODE_GEN(c, buf + 6);
1595 + int mb_count = (go->width >> 4) * (go->height >> 4);
1597 + CODE_ADD(c, frame == PFRAME ? 0x1 : 0x2, 2);
1599 + CODE_ADD(c, 0x1, 1);
1600 + CODE_ADD(c, 0x1, 2);
1601 + CODE_ADD(c, 0, vti_bitlen(go));
1602 + CODE_ADD(c, 0x3, 2);
1603 + if (frame == PFRAME)
1604 + CODE_ADD(c, 0, 1);
1605 + CODE_ADD(c, 0xc, 11);
1606 + if (frame != PFRAME)
1607 + CODE_ADD(c, 0x4, 3);
1608 + if (frame != BFRAME_EMPTY) {
1609 + for (i = 0; i < mb_count; ++i) {
1612 + CODE_ADD(c, 0x1, 1);
1615 + CODE_ADD(c, 0x47, 8);
1618 + CODE_ADD(c, 0x27, 7);
1620 + case BFRAME_BIDIR:
1621 + CODE_ADD(c, 0x5f, 8);
1623 + case BFRAME_EMPTY: /* keep compiler quiet */
1629 + /* Byte-align with a zero followed by ones */
1630 + i = 8 - (CODE_LENGTH(c) % 8);
1631 + CODE_ADD(c, 0, 1);
1632 + CODE_ADD(c, (1 << (i - 1)) - 1, i - 1);
1634 + i = CODE_LENGTH(c) + 4 * 8;
1635 + buf[0] = i & 0xff;
1644 +static int mpeg4_sequence_header(struct go7007 *go, unsigned char *buf, int ext)
1646 + const unsigned char head[] = { 0x00, 0x00, 0x01, 0xb0, go->pali,
1647 + 0x00, 0x00, 0x01, 0xb5, 0x09,
1648 + 0x00, 0x00, 0x01, 0x00,
1649 + 0x00, 0x00, 0x01, 0x20, };
1650 + int i, aspect_ratio;
1651 + int fps = go->sensor_framerate / go->fps_scale;
1652 + CODE_GEN(c, buf + 2 + sizeof(head));
1654 + switch (go->aspect_ratio) {
1655 + case GO7007_RATIO_4_3:
1656 + aspect_ratio = go->standard == GO7007_STD_NTSC ? 3 : 2;
1658 + case GO7007_RATIO_16_9:
1659 + aspect_ratio = go->standard == GO7007_STD_NTSC ? 5 : 4;
1666 + memcpy(buf + 2, head, sizeof(head));
1667 + CODE_ADD(c, 0x191, 17);
1668 + CODE_ADD(c, aspect_ratio, 4);
1669 + CODE_ADD(c, 0x1, 4);
1670 + CODE_ADD(c, fps, 16);
1671 + CODE_ADD(c, 0x3, 2);
1672 + CODE_ADD(c, 1001, vti_bitlen(go));
1673 + CODE_ADD(c, 1, 1);
1674 + CODE_ADD(c, go->width, 13);
1675 + CODE_ADD(c, 1, 1);
1676 + CODE_ADD(c, go->height, 13);
1677 + CODE_ADD(c, 0x2830, 14);
1680 + i = 8 - (CODE_LENGTH(c) % 8);
1681 + CODE_ADD(c, 0, 1);
1682 + CODE_ADD(c, (1 << (i - 1)) - 1, i - 1);
1684 + i = CODE_LENGTH(c) + sizeof(head) * 8;
1685 + buf[0] = i & 0xff;
1690 +static int gen_mpeg4hdr_to_package(struct go7007 *go,
1691 + u16 *code, int space, int *framelen)
1695 + unsigned int addr = 0x19;
1696 + int i, off = 0, chunk;
1698 + buf = kmalloc(5120, GFP_KERNEL);
1699 + if (buf == NULL) {
1700 + printk(KERN_ERR "go7007: unable to allocate 5120 bytes for "
1701 + "firmware construction\n");
1704 + memset(buf, 0, 5120);
1705 + framelen[0] = mpeg4_frame_header(go, buf, 0, PFRAME);
1707 + framelen[1] = mpeg4_frame_header(go, buf + i, 0, BFRAME_PRE);
1709 + framelen[2] = mpeg4_frame_header(go, buf + i, 0, BFRAME_POST);
1711 + framelen[3] = mpeg4_frame_header(go, buf + i, 0, BFRAME_BIDIR);
1713 + mpeg4_frame_header(go, buf + i, 0, BFRAME_EMPTY);
1715 + mpeg4_sequence_header(go, buf + i, 0);
1717 + for (i = 0; i < 5120; i += chunk * 2) {
1718 + if (space - off < 32) {
1723 + code[off + 1] = __cpu_to_le16(0x8000 | mem);
1726 + if (mem + chunk > 0x4000)
1727 + chunk = 0x4000 - mem;
1728 + if (i + 2 * chunk > 5120)
1729 + chunk = (5120 - i) / 2;
1732 + code[off] = __cpu_to_le16(0x4000 | chunk);
1733 + code[off + 31] = __cpu_to_le16(addr);
1734 + if (mem + chunk == 0x4000) {
1739 + code[off] = __cpu_to_le16(0x1000 | 28);
1740 + code[off + 31] = 0;
1744 + memcpy(&code[off + 2], buf + i, chunk * 2);
1748 + addr = go->ipb ? 0x14f9 : 0x0af9;
1749 + memset(buf, 0, 5120);
1750 + framelen[4] = mpeg4_frame_header(go, buf, 1, PFRAME);
1752 + framelen[5] = mpeg4_frame_header(go, buf + i, 1, BFRAME_PRE);
1754 + framelen[6] = mpeg4_frame_header(go, buf + i, 1, BFRAME_POST);
1756 + framelen[7] = mpeg4_frame_header(go, buf + i, 1, BFRAME_BIDIR);
1758 + mpeg4_frame_header(go, buf + i, 1, BFRAME_EMPTY);
1760 + for (i = 0; i < 5120; i += chunk * 2) {
1761 + if (space - off < 32) {
1766 + code[off + 1] = __cpu_to_le16(0x8000 | mem);
1769 + if (mem + chunk > 0x4000)
1770 + chunk = 0x4000 - mem;
1771 + if (i + 2 * chunk > 5120)
1772 + chunk = (5120 - i) / 2;
1775 + code[off] = __cpu_to_le16(0x4000 | chunk);
1776 + code[off + 31] = __cpu_to_le16(addr);
1777 + if (mem + chunk == 0x4000) {
1782 + code[off] = __cpu_to_le16(0x1000 | 28);
1783 + code[off + 31] = 0;
1787 + memcpy(&code[off + 2], buf + i, chunk * 2);
1795 +static int brctrl_to_package(struct go7007 *go,
1796 + u16 *code, int space, int *framelen)
1798 + int converge_speed = 0;
1799 + int lambda = (go->format == GO7007_FORMAT_MJPEG || go->dvd_mode) ?
1801 + int peak_rate = 6 * go->bitrate / 5;
1802 + int vbv_buffer = go->format == GO7007_FORMAT_MJPEG ?
1804 + (go->dvd_mode ? 900000 : peak_rate);
1805 + int fps = go->sensor_framerate / go->fps_scale;
1807 + /* Bizarre math below depends on rounding errors in division */
1808 + u32 sgop_expt_addr = go->bitrate / 32 * (go->ipb ? 3 : 1) * 1001 / fps;
1809 + u32 sgop_peak_addr = peak_rate / 32 * 1001 / fps;
1810 + u32 total_expt_addr = go->bitrate / 32 * 1000 / fps * (fps / 1000);
1811 + u32 vbv_alert_addr = vbv_buffer * 3 / (4 * 32);
1813 + q > 0 ? sgop_expt_addr * q :
1814 + 2 * go->width * go->height * (go->ipb ? 6 : 4) / 32,
1815 + q > 0 ? sgop_expt_addr * q :
1816 + 2 * go->width * go->height * (go->ipb ? 6 : 4) / 32,
1817 + q > 0 ? sgop_expt_addr * q :
1818 + 2 * go->width * go->height * (go->ipb ? 6 : 4) / 32,
1819 + q > 0 ? sgop_expt_addr * q :
1820 + 2 * go->width * go->height * (go->ipb ? 6 : 4) / 32,
1822 + u32 calc_q = q > 0 ? q : cplx[0] / sgop_expt_addr;
1825 + 0xBF20, go->ipb ? converge_speed_ipb[converge_speed]
1826 + : converge_speed_ip[converge_speed],
1827 + 0xBF21, go->ipb ? 2 : 0,
1828 + 0xBF22, go->ipb ? LAMBDA_table[0][lambda / 2 + 50]
1830 + 0xBF23, go->ipb ? LAMBDA_table[1][lambda] : 32767,
1832 + 0xBF25, lambda > 99 ? 32767 : LAMBDA_table[3][lambda],
1833 + 0xBF26, sgop_expt_addr & 0x0000FFFF,
1834 + 0xBF27, sgop_expt_addr >> 16,
1835 + 0xBF28, sgop_peak_addr & 0x0000FFFF,
1836 + 0xBF29, sgop_peak_addr >> 16,
1837 + 0xBF2A, vbv_alert_addr & 0x0000FFFF,
1838 + 0xBF2B, vbv_alert_addr >> 16,
1844 + 0xBF2E, vbv_alert_addr & 0x0000FFFF,
1845 + 0xBF2F, vbv_alert_addr >> 16,
1846 + 0xBF30, cplx[0] & 0x0000FFFF,
1847 + 0xBF31, cplx[0] >> 16,
1848 + 0xBF32, cplx[1] & 0x0000FFFF,
1849 + 0xBF33, cplx[1] >> 16,
1850 + 0xBF34, cplx[2] & 0x0000FFFF,
1851 + 0xBF35, cplx[2] >> 16,
1852 + 0xBF36, cplx[3] & 0x0000FFFF,
1853 + 0xBF37, cplx[3] >> 16,
1856 + 0xBF3A, total_expt_addr & 0x0000FFFF,
1857 + 0xBF3B, total_expt_addr >> 16,
1861 + 0xBF3C, total_expt_addr & 0x0000FFFF,
1862 + 0xBF3D, total_expt_addr >> 16,
1867 + 0xBF4A, calc_q < 4 ? 4 : (calc_q > 124 ? 124 : calc_q),
1878 + 0xBF40, sgop_expt_addr & 0x0000FFFF,
1879 + 0xBF41, sgop_expt_addr >> 16,
1884 + 0xBF46, (go->width >> 4) * (go->height >> 4),
1888 + 0xBF18, framelen[4],
1889 + 0xBF19, framelen[5],
1890 + 0xBF1A, framelen[6],
1891 + 0xBF1B, framelen[7],
1894 +#if 0 /* Remove once we don't care about matching */
1941 + 0xBF52, framelen[0],
1942 + 0xBF53, framelen[1],
1943 + 0xBF54, framelen[2],
1944 + 0xBF55, framelen[3],
1948 + return copy_packages(code, pack, 6, space);
1951 +static int config_package(struct go7007 *go, u16 *code, int space)
1953 + int fps = go->sensor_framerate / go->fps_scale / 1000;
1954 + int rows = go->interlace_coding ? go->height / 32 : go->height / 16;
1955 + int brc_window_size = fps;
1956 + int q_min = 2, q_max = 31;
1957 + int THACCoeffSet0 = 0;
1964 + 0xc6c3, go->format == GO7007_FORMAT_MPEG4 ? 0 :
1965 + (go->format == GO7007_FORMAT_H263 ? 0 : 1),
1966 + 0xc680, go->format == GO7007_FORMAT_MPEG4 ? 0xf1 :
1967 + (go->format == GO7007_FORMAT_H263 ? 0x61 :
1982 + 0xd472, (go->board_info->sensor_flags &
1983 + GO7007_SENSOR_TV) &&
1984 + (!go->interlace_coding) ?
1986 + 0xd475, (go->board_info->sensor_flags &
1987 + GO7007_SENSOR_TV) &&
1988 + (!go->interlace_coding) ?
1990 + 0xc404, go->interlace_coding ? 0x44 :
1991 + (go->format == GO7007_FORMAT_MPEG4 ? 0x11 :
1992 + (go->format == GO7007_FORMAT_MPEG1 ? 0x02 :
1993 + (go->format == GO7007_FORMAT_MPEG2 ? 0x04 :
1994 + (go->format == GO7007_FORMAT_H263 ? 0x08 :
1996 + 0xbf0a, (go->format == GO7007_FORMAT_MPEG4 ? 8 :
1997 + (go->format == GO7007_FORMAT_MPEG1 ? 1 :
1998 + (go->format == GO7007_FORMAT_MPEG2 ? 2 :
1999 + (go->format == GO7007_FORMAT_H263 ? 4 : 16)))) |
2000 + ((go->repeat_seqhead ? 1 : 0) << 6) |
2001 + ((go->dvd_mode ? 1 : 0) << 9) |
2002 + ((go->gop_header_enable ? 1 : 0) << 10),
2004 + 0xdd5a, go->ipb ? 0x14 : 0x0a,
2007 + 0xc683, THACCoeffSet0,
2008 + 0xc40a, (go->width << 4) | rows,
2009 + 0xe01a, go->board_info->hpi_buffer_cap,
2031 + 0xbf66, brc_window_size,
2038 + 0xbfe3, go->ipb ? 3 : 1,
2039 + 0xc031, go->board_info->sensor_flags &
2040 + GO7007_SENSOR_VBI ? 1 : 0,
2044 + 0xdd88, go->ipb ? 0x1401 : 0x0a01,
2045 + 0xdd90, go->ipb ? 0x1401 : 0x0a01,
2057 + 0xbfec, (go->interlace_coding ? 1 << 15 : 0) |
2058 + (go->modet_enable ? 0xa : 0) |
2059 + (go->board_info->sensor_flags &
2060 + GO7007_SENSOR_VBI ? 1 : 0),
2064 + 0xbff0, go->board_info->sensor_flags &
2065 + GO7007_SENSOR_TV ? 0xf060 : 0xb060,
2070 + return copy_packages(code, pack, 5, space);
2073 +static int seqhead_to_package(struct go7007 *go, u16 *code, int space,
2074 + int (*sequence_header_func)(struct go7007 *go,
2075 + unsigned char *buf, int ext))
2077 + int vop_time_increment_bitlength = vti_bitlen(go);
2078 + int fps = go->sensor_framerate / go->fps_scale *
2079 + (go->interlace_coding ? 2 : 1);
2080 + unsigned char buf[40] = { };
2081 + int len = sequence_header_func(go, buf, 1);
2086 + 0xbff2, vop_time_increment_bitlength,
2087 + 0xbff3, (1 << vop_time_increment_bitlength) - 1,
2089 + 0xbfe7, (fps / 1000) << 8,
2101 + 0xc800, buf[2] << 8 | buf[3],
2102 + 0xc801, buf[4] << 8 | buf[5],
2103 + 0xc802, buf[6] << 8 | buf[7],
2104 + 0xc803, buf[8] << 8 | buf[9],
2118 + 0xc808, buf[10] << 8 | buf[11],
2119 + 0xc809, buf[12] << 8 | buf[13],
2120 + 0xc80a, buf[14] << 8 | buf[15],
2121 + 0xc80b, buf[16] << 8 | buf[17],
2122 + 0xc80c, buf[18] << 8 | buf[19],
2123 + 0xc80d, buf[20] << 8 | buf[21],
2124 + 0xc80e, buf[22] << 8 | buf[23],
2125 + 0xc80f, buf[24] << 8 | buf[25],
2126 + 0xc810, buf[26] << 8 | buf[27],
2127 + 0xc811, buf[28] << 8 | buf[29],
2128 + 0xc812, buf[30] << 8 | buf[31],
2129 + 0xc813, buf[32] << 8 | buf[33],
2130 + 0xc814, buf[34] << 8 | buf[35],
2131 + 0xc815, buf[36] << 8 | buf[37],
2137 + return copy_packages(code, pack, 3, space);
2140 +static int relative_prime(int big, int little)
2144 + while (little != 0) {
2145 + remainder = big % little;
2147 + little = remainder;
2152 +static int avsync_to_package(struct go7007 *go, u16 *code, int space)
2154 + int arate = go->board_info->audio_rate * 1001 * go->fps_scale;
2155 + int ratio = arate / go->sensor_framerate;
2156 + int adjratio = ratio * 215 / 100;
2157 + int rprime = relative_prime(go->sensor_framerate,
2158 + arate % go->sensor_framerate);
2159 + int f1 = (arate % go->sensor_framerate) / rprime;
2160 + int f2 = (go->sensor_framerate - arate % go->sensor_framerate) / rprime;
2163 + 0xbf98, (u16)((-adjratio) & 0xffff),
2164 + 0xbf99, (u16)((-adjratio) >> 16),
2167 + 0xbff4, f1 > f2 ? f1 : f2,
2168 + 0xbff5, f1 < f2 ? f1 : f2,
2169 + 0xbff6, f1 < f2 ? ratio : ratio + 1,
2170 + 0xbff7, f1 > f2 ? ratio : ratio + 1,
2173 + 0xbffa, adjratio & 0xffff,
2174 + 0xbffb, adjratio >> 16,
2180 + return copy_packages(code, pack, 1, space);
2183 +static int final_package(struct go7007 *go, u16 *code, int space)
2185 + int rows = go->interlace_coding ? go->height / 32 : go->height / 16;
2195 + ((go->board_info->sensor_flags & GO7007_SENSOR_TV) &&
2196 + (!go->interlace_coding) ?
2197 + (1 << 14) | (1 << 9) : 0) |
2198 + ((go->encoder_subsample ? 1 : 0) << 8) |
2199 + (go->board_info->sensor_flags &
2200 + GO7007_SENSOR_CONFIG_MASK),
2201 + ((go->encoder_v_halve ? 1 : 0) << 14) |
2202 + (go->encoder_v_halve ? rows << 9 : rows << 8) |
2203 + (go->encoder_h_halve ? 1 << 6 : 0) |
2204 + (go->encoder_h_halve ? go->width >> 3 : go->width >> 4),
2205 + (1 << 15) | (go->encoder_v_offset << 6) |
2206 + (1 << 7) | (go->encoder_h_offset >> 2),
2210 + ((go->fps_scale - 1) << 8) |
2211 + (go->board_info->sensor_flags & GO7007_SENSOR_TV ?
2214 + go->ipb ? 0xd4c : 0x36b,
2215 + (rows << 8) | (go->width >> 4),
2216 + go->format == GO7007_FORMAT_MPEG4 ? 0x0404 : 0,
2217 + (1 << 15) | ((go->interlace_coding ? 1 : 0) << 13) |
2218 + ((go->closed_gop ? 1 : 0) << 12) |
2219 + ((go->format == GO7007_FORMAT_MPEG4 ? 1 : 0) << 11) |
2221 + ((go->ipb ? 3 : 0) << 7) |
2222 + ((go->modet_enable ? 1 : 0) << 2) |
2223 + ((go->dvd_mode ? 1 : 0) << 1) | 1,
2224 + (go->format == GO7007_FORMAT_MPEG1 ? 0x89a0 :
2225 + (go->format == GO7007_FORMAT_MPEG2 ? 0x89a0 :
2226 + (go->format == GO7007_FORMAT_MJPEG ? 0x89a0 :
2227 + (go->format == GO7007_FORMAT_MPEG4 ? 0x8920 :
2228 + (go->format == GO7007_FORMAT_H263 ? 0x8920 : 0))))),
2229 + go->ipb ? 0x1f15 : 0x1f0b,
2230 + go->ipb ? 0x0015 : 0x000b,
2231 + go->ipb ? 0xa800 : 0x5800,
2233 + 0x0020 + 0x034b * 0,
2234 + 0x0020 + 0x034b * 1,
2235 + 0x0020 + 0x034b * 2,
2236 + 0x0020 + 0x034b * 3,
2237 + 0x0020 + 0x034b * 4,
2238 + 0x0020 + 0x034b * 5,
2239 + go->ipb ? (go->gop_size / 3) : go->gop_size,
2240 + (go->height >> 4) * (go->width >> 4) * 110 / 100,
2243 + return copy_packages(code, pack, 1, space);
2246 +static int audio_to_package(struct go7007 *go, u16 *code, int space)
2248 + int clock_config = ((go->board_info->audio_flags &
2249 + GO7007_AUDIO_I2S_MASTER ? 1 : 0) << 11) |
2250 + ((go->board_info->audio_flags &
2251 + GO7007_AUDIO_OKI_MODE ? 1 : 0) << 8) |
2252 + (((go->board_info->audio_bclk_div / 4) - 1) << 4) |
2253 + (go->board_info->audio_main_div - 1);
2266 + 0x9000, clock_config,
2267 + 0x9001, (go->board_info->audio_flags & 0xffff) |
2269 + 0x9000, ((go->board_info->audio_flags &
2270 + GO7007_AUDIO_I2S_MASTER ?
2293 + return copy_packages(code, pack, 2, space);
2296 +static int modet_to_package(struct go7007 *go, u16 *code, int space)
2298 + int ret, mb, i, addr, cnt = 0;
2300 + u16 thresholds[] = {
2302 + 0xbf82, go->modet[0].pixel_threshold,
2303 + 0xbf83, go->modet[1].pixel_threshold,
2304 + 0xbf84, go->modet[2].pixel_threshold,
2305 + 0xbf85, go->modet[3].pixel_threshold,
2306 + 0xbf86, go->modet[0].motion_threshold,
2307 + 0xbf87, go->modet[1].motion_threshold,
2308 + 0xbf88, go->modet[2].motion_threshold,
2309 + 0xbf89, go->modet[3].motion_threshold,
2310 + 0xbf8a, go->modet[0].mb_threshold,
2311 + 0xbf8b, go->modet[1].mb_threshold,
2312 + 0xbf8c, go->modet[2].mb_threshold,
2313 + 0xbf8d, go->modet[3].mb_threshold,
2319 + ret = copy_packages(code, thresholds, 1, space);
2325 + memset(pack, 0, 64);
2327 + for (mb = 0; mb < 1624; ++mb) {
2328 + pack[i * 2 + 3] <<= 2;
2329 + pack[i * 2 + 3] |= go->modet_map[mb];
2332 + pack[i * 2 + 2] = addr++;
2334 + if (i == 10 || mb == 1623) {
2335 + pack[0] = 0x2000 | i;
2336 + ret = copy_packages(code + cnt, pack, 1, space - cnt);
2341 + memset(pack, 0, 64);
2343 + pack[i * 2 + 3] = 0;
2346 + memset(pack, 0, 64);
2348 + for (addr = 0xbb90; addr < 0xbbfa; ++addr) {
2349 + pack[i * 2 + 2] = addr;
2350 + pack[i * 2 + 3] = 0;
2352 + if (i == 10 || addr == 0xbbf9) {
2353 + pack[0] = 0x2000 | i;
2354 + ret = copy_packages(code + cnt, pack, 1, space - cnt);
2359 + memset(pack, 0, 64);
2365 +static int do_special(struct go7007 *go, u16 type, u16 *code, int space,
2369 + case SPECIAL_FRM_HEAD:
2370 + switch (go->format) {
2371 + case GO7007_FORMAT_MJPEG:
2372 + return gen_mjpeghdr_to_package(go, code, space);
2373 + case GO7007_FORMAT_MPEG1:
2374 + case GO7007_FORMAT_MPEG2:
2375 + return gen_mpeg1hdr_to_package(go, code, space,
2377 + case GO7007_FORMAT_MPEG4:
2378 + return gen_mpeg4hdr_to_package(go, code, space,
2381 + case SPECIAL_BRC_CTRL:
2382 + return brctrl_to_package(go, code, space, framelen);
2383 + case SPECIAL_CONFIG:
2384 + return config_package(go, code, space);
2385 + case SPECIAL_SEQHEAD:
2386 + switch (go->format) {
2387 + case GO7007_FORMAT_MPEG1:
2388 + case GO7007_FORMAT_MPEG2:
2389 + return seqhead_to_package(go, code, space,
2390 + mpeg1_sequence_header);
2391 + case GO7007_FORMAT_MPEG4:
2392 + return seqhead_to_package(go, code, space,
2393 + mpeg4_sequence_header);
2397 + case SPECIAL_AV_SYNC:
2398 + return avsync_to_package(go, code, space);
2399 + case SPECIAL_FINAL:
2400 + return final_package(go, code, space);
2401 + case SPECIAL_AUDIO:
2402 + return audio_to_package(go, code, space);
2403 + case SPECIAL_MODET:
2404 + return modet_to_package(go, code, space);
2407 + "go7007: firmware file contains unsupported feature %04x\n",
2412 +int go7007_construct_fw_image(struct go7007 *go, u8 **fw, int *fwlen)
2414 + const struct firmware *fw_entry;
2416 + int framelen[8] = { }; /* holds the lengths of empty frame templates */
2417 + int codespace = 64 * 1024, i = 0, srclen, chunk_len, chunk_flags;
2421 + switch (go->format) {
2422 + case GO7007_FORMAT_MJPEG:
2423 + mode_flag = FLAG_MODE_MJPEG;
2425 + case GO7007_FORMAT_MPEG1:
2426 + mode_flag = FLAG_MODE_MPEG1;
2428 + case GO7007_FORMAT_MPEG2:
2429 + mode_flag = FLAG_MODE_MPEG2;
2431 + case GO7007_FORMAT_MPEG4:
2432 + mode_flag = FLAG_MODE_MPEG4;
2437 + if (request_firmware(&fw_entry, go->board_info->firmware, go->dev)) {
2439 + "go7007: unable to load firmware from file \"%s\"\n",
2440 + go->board_info->firmware);
2443 + code = kmalloc(codespace * 2, GFP_KERNEL);
2444 + if (code == NULL) {
2445 + printk(KERN_ERR "go7007: unable to allocate %d bytes for "
2446 + "firmware construction\n", codespace * 2);
2449 + memset(code, 0, codespace * 2);
2450 + src = (u16 *)fw_entry->data;
2451 + srclen = fw_entry->size / 2;
2452 + while (srclen >= 2) {
2453 + chunk_flags = __le16_to_cpu(src[0]);
2454 + chunk_len = __le16_to_cpu(src[1]);
2455 + if (chunk_len + 2 > srclen) {
2456 + printk(KERN_ERR "go7007: firmware file \"%s\" "
2457 + "appears to be corrupted\n",
2458 + go->board_info->firmware);
2461 + if (chunk_flags & mode_flag) {
2462 + if (chunk_flags & FLAG_SPECIAL) {
2463 + ret = do_special(go, __le16_to_cpu(src[2]),
2464 + &code[i], codespace - i, framelen);
2466 + printk(KERN_ERR "go7007: insufficient "
2467 + "memory for firmware "
2468 + "construction\n");
2473 + if (codespace - i < chunk_len) {
2474 + printk(KERN_ERR "go7007: insufficient "
2475 + "memory for firmware "
2476 + "construction\n");
2479 + memcpy(&code[i], &src[2], chunk_len * 2);
2483 + srclen -= chunk_len + 2;
2484 + src += chunk_len + 2;
2486 + release_firmware(fw_entry);
2493 + release_firmware(fw_entry);
2496 diff --git a/drivers/staging/go7007/go7007-i2c.c b/drivers/staging/go7007/go7007-i2c.c
2497 new file mode 100644
2498 index 0000000..10baae3
2500 +++ b/drivers/staging/go7007/go7007-i2c.c
2503 + * Copyright (C) 2005-2006 Micronas USA Inc.
2505 + * This program is free software; you can redistribute it and/or modify
2506 + * it under the terms of the GNU General Public License (Version 2) as
2507 + * published by the Free Software Foundation.
2509 + * This program is distributed in the hope that it will be useful,
2510 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2511 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2512 + * GNU General Public License for more details.
2514 + * You should have received a copy of the GNU General Public License
2515 + * along with this program; if not, write to the Free Software Foundation,
2516 + * Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
2519 +#include <linux/version.h>
2520 +#include <linux/module.h>
2521 +#include <linux/init.h>
2522 +#include <linux/delay.h>
2523 +#include <linux/sched.h>
2524 +#include <linux/list.h>
2525 +#include <linux/unistd.h>
2526 +#include <linux/time.h>
2527 +#include <linux/device.h>
2528 +#include <linux/i2c.h>
2529 +#include <linux/semaphore.h>
2530 +#include <linux/uaccess.h>
2531 +#include <asm/system.h>
2533 +#include "go7007-priv.h"
2534 +#include "wis-i2c.h"
2536 +/************** Registration interface for I2C client drivers **************/
2538 +/* Since there's no way to auto-probe the I2C devices connected to the I2C
2539 + * bus on the go7007, we have this silly little registration system that
2540 + * client drivers can use to register their I2C driver ID and their
2541 + * detect_client function (the one that's normally passed to i2c_probe).
2543 + * When a new go7007 device is connected, we can look up in a board info
2544 + * table by the USB or PCI vendor/product/revision ID to determine
2545 + * which I2C client module to load. The client driver module will register
2546 + * itself here, and then we can call the registered detect_client function
2547 + * to force-load a new client at the address listed in the board info table.
2549 + * Really the I2C subsystem should have a way to force-load I2C client
2550 + * drivers when we have a priori knowledge of what's on the bus, especially
2551 + * since the existing I2C auto-probe mechanism is so hokey, but we'll use
2552 + * our own mechanism for the time being. */
2554 +struct wis_i2c_client_driver {
2556 + found_proc found_proc;
2557 + struct list_head list;
2560 +static LIST_HEAD(i2c_client_drivers);
2561 +static DECLARE_MUTEX(i2c_client_driver_list_lock);
2563 +/* Client drivers register here by their I2C driver ID */
2564 +int wis_i2c_add_driver(unsigned int id, found_proc found_proc)
2566 + struct wis_i2c_client_driver *driver;
2568 + driver = kmalloc(sizeof(struct wis_i2c_client_driver), GFP_KERNEL);
2569 + if (driver == NULL)
2572 + driver->found_proc = found_proc;
2574 + down(&i2c_client_driver_list_lock);
2575 + list_add_tail(&driver->list, &i2c_client_drivers);
2576 + up(&i2c_client_driver_list_lock);
2580 +EXPORT_SYMBOL(wis_i2c_add_driver);
2582 +void wis_i2c_del_driver(found_proc found_proc)
2584 + struct wis_i2c_client_driver *driver, *next;
2586 + down(&i2c_client_driver_list_lock);
2587 + list_for_each_entry_safe(driver, next, &i2c_client_drivers, list)
2588 + if (driver->found_proc == found_proc) {
2589 + list_del(&driver->list);
2592 + up(&i2c_client_driver_list_lock);
2594 +EXPORT_SYMBOL(wis_i2c_del_driver);
2596 +/* The main go7007 driver calls this to instantiate a client by driver
2597 + * ID and bus address, which are both stored in the board info table */
2598 +int wis_i2c_probe_device(struct i2c_adapter *adapter,
2599 + unsigned int id, int addr)
2601 + struct wis_i2c_client_driver *driver;
2604 + if (addr < 0 || addr > 0x7f)
2606 + down(&i2c_client_driver_list_lock);
2607 + list_for_each_entry(driver, &i2c_client_drivers, list)
2608 + if (driver->id == id) {
2609 + if (driver->found_proc(adapter, addr, 0) == 0)
2613 + up(&i2c_client_driver_list_lock);
2617 +/********************* Driver for on-board I2C adapter *********************/
2619 +/* #define GO7007_I2C_DEBUG */
2621 +#define SPI_I2C_ADDR_BASE 0x1400
2622 +#define STATUS_REG_ADDR (SPI_I2C_ADDR_BASE + 0x2)
2623 +#define I2C_CTRL_REG_ADDR (SPI_I2C_ADDR_BASE + 0x6)
2624 +#define I2C_DEV_UP_ADDR_REG_ADDR (SPI_I2C_ADDR_BASE + 0x7)
2625 +#define I2C_LO_ADDR_REG_ADDR (SPI_I2C_ADDR_BASE + 0x8)
2626 +#define I2C_DATA_REG_ADDR (SPI_I2C_ADDR_BASE + 0x9)
2627 +#define I2C_CLKFREQ_REG_ADDR (SPI_I2C_ADDR_BASE + 0xa)
2629 +#define I2C_STATE_MASK 0x0007
2630 +#define I2C_READ_READY_MASK 0x0008
2632 +/* There is only one I2C port on the TW2804 that feeds all four GO7007 VIPs
2633 + * on the Adlink PCI-MPG24, so access is shared between all of them. */
2634 +static DECLARE_MUTEX(adlink_mpg24_i2c_lock);
2636 +static int go7007_i2c_xfer(struct go7007 *go, u16 addr, int read,
2637 + u16 command, int flags, u8 *data)
2642 + if (go->status == STATUS_SHUTDOWN)
2645 +#ifdef GO7007_I2C_DEBUG
2647 + printk(KERN_DEBUG "go7007-i2c: reading 0x%02x on 0x%02x\n",
2651 + "go7007-i2c: writing 0x%02x to 0x%02x on 0x%02x\n",
2652 + *data, command, addr);
2655 + down(&go->hw_lock);
2657 + if (go->board_id == GO7007_BOARDID_ADLINK_MPG24) {
2658 + /* Bridge the I2C port on this GO7007 to the shared bus */
2659 + down(&adlink_mpg24_i2c_lock);
2660 + go7007_write_addr(go, 0x3c82, 0x0020);
2663 + /* Wait for I2C adapter to be ready */
2664 + for (i = 0; i < 10; ++i) {
2665 + if (go7007_read_addr(go, STATUS_REG_ADDR, &val) < 0)
2667 + if (!(val & I2C_STATE_MASK))
2672 + printk(KERN_ERR "go7007-i2c: I2C adapter is hung\n");
2676 + /* Set target register (command) */
2677 + go7007_write_addr(go, I2C_CTRL_REG_ADDR, flags);
2678 + go7007_write_addr(go, I2C_LO_ADDR_REG_ADDR, command);
2680 + /* If we're writing, send the data and target address and we're done */
2682 + go7007_write_addr(go, I2C_DATA_REG_ADDR, *data);
2683 + go7007_write_addr(go, I2C_DEV_UP_ADDR_REG_ADDR,
2684 + (addr << 9) | (command >> 8));
2689 + /* Otherwise, we're reading. First clear i2c_rx_data_rdy. */
2690 + if (go7007_read_addr(go, I2C_DATA_REG_ADDR, &val) < 0)
2693 + /* Send the target address plus read flag */
2694 + go7007_write_addr(go, I2C_DEV_UP_ADDR_REG_ADDR,
2695 + (addr << 9) | 0x0100 | (command >> 8));
2697 + /* Wait for i2c_rx_data_rdy */
2698 + for (i = 0; i < 10; ++i) {
2699 + if (go7007_read_addr(go, STATUS_REG_ADDR, &val) < 0)
2701 + if (val & I2C_READ_READY_MASK)
2706 + printk(KERN_ERR "go7007-i2c: I2C adapter is hung\n");
2710 + /* Retrieve the read byte */
2711 + if (go7007_read_addr(go, I2C_DATA_REG_ADDR, &val) < 0)
2717 + if (go->board_id == GO7007_BOARDID_ADLINK_MPG24) {
2718 + /* Isolate the I2C port on this GO7007 from the shared bus */
2719 + go7007_write_addr(go, 0x3c82, 0x0000);
2720 + up(&adlink_mpg24_i2c_lock);
2726 +static int go7007_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
2727 + unsigned short flags, char read_write,
2728 + u8 command, int size, union i2c_smbus_data *data)
2730 + struct go7007 *go = i2c_get_adapdata(adapter);
2732 + if (size != I2C_SMBUS_BYTE_DATA)
2734 + return go7007_i2c_xfer(go, addr, read_write == I2C_SMBUS_READ, command,
2735 + flags & I2C_CLIENT_SCCB ? 0x10 : 0x00, &data->byte);
2738 +/* VERY LIMITED I2C master xfer function -- only needed because the
2739 + * SMBus functions only support 8-bit commands and the SAA7135 uses
2740 + * 16-bit commands. The I2C interface on the GO7007, as limited as
2741 + * it is, does support this mode. */
2743 +static int go7007_i2c_master_xfer(struct i2c_adapter *adapter,
2744 + struct i2c_msg msgs[], int num)
2746 + struct go7007 *go = i2c_get_adapdata(adapter);
2749 + for (i = 0; i < num; ++i) {
2750 + /* We can only do two things here -- write three bytes, or
2751 + * write two bytes and read one byte. */
2752 + if (msgs[i].len == 2) {
2753 + if (i + 1 == num || msgs[i].addr != msgs[i + 1].addr ||
2754 + (msgs[i].flags & I2C_M_RD) ||
2755 + !(msgs[i + 1].flags & I2C_M_RD) ||
2756 + msgs[i + 1].len != 1)
2758 + if (go7007_i2c_xfer(go, msgs[i].addr, 1,
2759 + (msgs[i].buf[0] << 8) | msgs[i].buf[1],
2760 + 0x01, &msgs[i + 1].buf[0]) < 0)
2763 + } else if (msgs[i].len == 3) {
2764 + if (msgs[i].flags & I2C_M_RD)
2766 + if (msgs[i].len != 3)
2768 + if (go7007_i2c_xfer(go, msgs[i].addr, 0,
2769 + (msgs[i].buf[0] << 8) | msgs[i].buf[1],
2770 + 0x01, &msgs[i].buf[2]) < 0)
2779 +static u32 go7007_functionality(struct i2c_adapter *adapter)
2781 + return I2C_FUNC_SMBUS_BYTE_DATA;
2784 +static struct i2c_algorithm go7007_algo = {
2785 + .smbus_xfer = go7007_smbus_xfer,
2786 + .master_xfer = go7007_i2c_master_xfer,
2787 + .functionality = go7007_functionality,
2790 +static struct i2c_adapter go7007_adap_templ = {
2791 + .owner = THIS_MODULE,
2792 + .class = I2C_CLASS_TV_ANALOG,
2793 + .name = "WIS GO7007SB",
2794 + .id = I2C_ALGO_GO7007,
2795 + .algo = &go7007_algo,
2798 +int go7007_i2c_init(struct go7007 *go)
2800 + memcpy(&go->i2c_adapter, &go7007_adap_templ,
2801 + sizeof(go7007_adap_templ));
2802 + go->i2c_adapter.dev.parent = go->dev;
2803 + i2c_set_adapdata(&go->i2c_adapter, go);
2804 + if (i2c_add_adapter(&go->i2c_adapter) < 0) {
2806 + "go7007-i2c: error: i2c_add_adapter failed\n");
2811 diff --git a/drivers/staging/go7007/go7007-priv.h b/drivers/staging/go7007/go7007-priv.h
2812 new file mode 100644
2813 index 0000000..005542d
2815 +++ b/drivers/staging/go7007/go7007-priv.h
2818 + * Copyright (C) 2005-2006 Micronas USA Inc.
2820 + * This program is free software; you can redistribute it and/or modify
2821 + * it under the terms of the GNU General Public License (Version 2) as
2822 + * published by the Free Software Foundation.
2824 + * This program is distributed in the hope that it will be useful,
2825 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2826 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2827 + * GNU General Public License for more details.
2829 + * You should have received a copy of the GNU General Public License
2830 + * along with this program; if not, write to the Free Software Foundation,
2831 + * Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
2835 + * This is the private include file for the go7007 driver. It should not
2836 + * be included by anybody but the driver itself, and especially not by
2837 + * user-space applications.
2842 +/* IDs to activate board-specific support code */
2843 +#define GO7007_BOARDID_MATRIX_II 0
2844 +#define GO7007_BOARDID_MATRIX_RELOAD 1
2845 +#define GO7007_BOARDID_STAR_TREK 2
2846 +#define GO7007_BOARDID_PCI_VOYAGER 3
2847 +#define GO7007_BOARDID_XMEN 4
2848 +#define GO7007_BOARDID_XMEN_II 5
2849 +#define GO7007_BOARDID_XMEN_III 6
2850 +#define GO7007_BOARDID_MATRIX_REV 7
2851 +#define GO7007_BOARDID_PX_M402U 16
2852 +#define GO7007_BOARDID_PX_TV402U_ANY 17 /* need to check tuner model */
2853 +#define GO7007_BOARDID_PX_TV402U_NA 18 /* detected NTSC tuner */
2854 +#define GO7007_BOARDID_PX_TV402U_EU 19 /* detected PAL tuner */
2855 +#define GO7007_BOARDID_PX_TV402U_JP 20 /* detected NTSC-J tuner */
2856 +#define GO7007_BOARDID_LIFEVIEW_LR192 21 /* TV Walker Ultra */
2857 +#define GO7007_BOARDID_ENDURA 22
2858 +#define GO7007_BOARDID_ADLINK_MPG24 23
2860 +/* Various characteristics of each board */
2861 +#define GO7007_BOARD_HAS_AUDIO (1<<0)
2862 +#define GO7007_BOARD_USE_ONBOARD_I2C (1<<1)
2863 +#define GO7007_BOARD_HAS_TUNER (1<<2)
2865 +/* Characteristics of sensor devices */
2866 +#define GO7007_SENSOR_VALID_POLAR (1<<0)
2867 +#define GO7007_SENSOR_HREF_POLAR (1<<1)
2868 +#define GO7007_SENSOR_VREF_POLAR (1<<2)
2869 +#define GO7007_SENSOR_FIELD_ID_POLAR (1<<3)
2870 +#define GO7007_SENSOR_BIT_WIDTH (1<<4)
2871 +#define GO7007_SENSOR_VALID_ENABLE (1<<5)
2872 +#define GO7007_SENSOR_656 (1<<6)
2873 +#define GO7007_SENSOR_CONFIG_MASK 0x7f
2874 +#define GO7007_SENSOR_TV (1<<7)
2875 +#define GO7007_SENSOR_VBI (1<<8)
2876 +#define GO7007_SENSOR_SCALING (1<<9)
2878 +/* Characteristics of audio sensor devices */
2879 +#define GO7007_AUDIO_I2S_MODE_1 (1)
2880 +#define GO7007_AUDIO_I2S_MODE_2 (2)
2881 +#define GO7007_AUDIO_I2S_MODE_3 (3)
2882 +#define GO7007_AUDIO_BCLK_POLAR (1<<2)
2883 +#define GO7007_AUDIO_WORD_14 (14<<4)
2884 +#define GO7007_AUDIO_WORD_16 (16<<4)
2885 +#define GO7007_AUDIO_ONE_CHANNEL (1<<11)
2886 +#define GO7007_AUDIO_I2S_MASTER (1<<16)
2887 +#define GO7007_AUDIO_OKI_MODE (1<<17)
2889 +struct go7007_board_info {
2891 + unsigned int flags;
2892 + int hpi_buffer_cap;
2893 + unsigned int sensor_flags;
2895 + int sensor_height;
2896 + int sensor_framerate;
2897 + int sensor_h_offset;
2898 + int sensor_v_offset;
2899 + unsigned int audio_flags;
2901 + int audio_bclk_div;
2902 + int audio_main_div;
2916 +struct go7007_hpi_ops {
2917 + int (*interface_reset)(struct go7007 *go);
2918 + int (*write_interrupt)(struct go7007 *go, int addr, int data);
2919 + int (*read_interrupt)(struct go7007 *go);
2920 + int (*stream_start)(struct go7007 *go);
2921 + int (*stream_stop)(struct go7007 *go);
2922 + int (*send_firmware)(struct go7007 *go, u8 *data, int len);
2925 +/* The video buffer size must be a multiple of PAGE_SIZE */
2926 +#define GO7007_BUF_PAGES (128 * 1024 / PAGE_SIZE)
2927 +#define GO7007_BUF_SIZE (GO7007_BUF_PAGES << PAGE_SHIFT)
2929 +struct go7007_buffer {
2930 + struct go7007 *go; /* Reverse reference for VMA ops */
2931 + int index; /* Reverse reference for DQBUF */
2932 + enum { BUF_STATE_IDLE, BUF_STATE_QUEUED, BUF_STATE_DONE } state;
2934 + struct timeval timestamp;
2935 + struct list_head stream;
2936 + struct page *pages[GO7007_BUF_PAGES + 1]; /* extra for straddling */
2937 + unsigned long user_addr;
2938 + unsigned int page_count;
2939 + unsigned int offset;
2940 + unsigned int bytesused;
2941 + unsigned int frame_offset;
2946 +struct go7007_file {
2947 + struct go7007 *go;
2948 + struct semaphore lock;
2950 + struct go7007_buffer *bufs;
2953 +#define GO7007_FORMAT_MJPEG 0
2954 +#define GO7007_FORMAT_MPEG4 1
2955 +#define GO7007_FORMAT_MPEG1 2
2956 +#define GO7007_FORMAT_MPEG2 3
2957 +#define GO7007_FORMAT_H263 4
2959 +#define GO7007_RATIO_1_1 0
2960 +#define GO7007_RATIO_4_3 1
2961 +#define GO7007_RATIO_16_9 2
2963 +enum go7007_parser_state {
2976 + struct device *dev;
2977 + struct go7007_board_info *board_info;
2978 + unsigned int board_id;
2980 + int channel_number; /* for multi-channel boards like Adlink PCI-MPG24 */
2982 + struct video_device *video_dev;
2984 + enum { STATUS_INIT, STATUS_ONLINE, STATUS_SHUTDOWN } status;
2985 + spinlock_t spinlock;
2986 + struct semaphore hw_lock;
2989 + int audio_enabled;
2993 + enum { GO7007_STD_NTSC, GO7007_STD_PAL, GO7007_STD_OTHER } standard;
2994 + int sensor_framerate;
2997 + int encoder_h_offset;
2998 + int encoder_v_offset;
2999 + unsigned int encoder_h_halve:1;
3000 + unsigned int encoder_v_halve:1;
3001 + unsigned int encoder_subsample:1;
3003 + /* Encoder config */
3010 + unsigned int ipb:1;
3011 + unsigned int closed_gop:1;
3012 + unsigned int repeat_seqhead:1;
3013 + unsigned int seq_header_enable:1;
3014 + unsigned int gop_header_enable:1;
3015 + unsigned int dvd_mode:1;
3016 + unsigned int interlace_coding:1;
3018 + /* Motion detection */
3019 + unsigned int modet_enable:1;
3021 + unsigned int enable:1;
3022 + int pixel_threshold;
3023 + int motion_threshold;
3026 + unsigned char modet_map[1624];
3027 + unsigned char active_map[216];
3029 + /* Video streaming */
3030 + struct go7007_buffer *active_buf;
3031 + enum go7007_parser_state state;
3036 + struct list_head stream;
3037 + wait_queue_head_t frame_waitq;
3039 + /* Audio streaming */
3040 + void (*audio_deliver)(struct go7007 *go, u8 *buf, int length);
3041 + void *snd_context;
3044 + int i2c_adapter_online;
3045 + struct i2c_adapter i2c_adapter;
3048 + struct go7007_hpi_ops *hpi_ops;
3049 + void *hpi_context;
3050 + int interrupt_available;
3051 + wait_queue_head_t interrupt_waitq;
3052 + unsigned short interrupt_value;
3053 + unsigned short interrupt_data;
3056 +/* All of these must be called with the hpi_lock semaphore held! */
3057 +#define go7007_interface_reset(go) \
3058 + ((go)->hpi_ops->interface_reset(go))
3059 +#define go7007_write_interrupt(go, x, y) \
3060 + ((go)->hpi_ops->write_interrupt)((go), (x), (y))
3061 +#define go7007_stream_start(go) \
3062 + ((go)->hpi_ops->stream_start(go))
3063 +#define go7007_stream_stop(go) \
3064 + ((go)->hpi_ops->stream_stop(go))
3065 +#define go7007_send_firmware(go, x, y) \
3066 + ((go)->hpi_ops->send_firmware)((go), (x), (y))
3067 +#define go7007_write_addr(go, x, y) \
3068 + ((go)->hpi_ops->write_interrupt)((go), (x)|0x8000, (y))
3070 +/* go7007-driver.c */
3071 +int go7007_read_addr(struct go7007 *go, u16 addr, u16 *data);
3072 +int go7007_read_interrupt(struct go7007 *go, u16 *value, u16 *data);
3073 +int go7007_boot_encoder(struct go7007 *go, int init_i2c);
3074 +int go7007_reset_encoder(struct go7007 *go);
3075 +int go7007_register_encoder(struct go7007 *go);
3076 +int go7007_start_encoder(struct go7007 *go);
3077 +void go7007_parse_video_stream(struct go7007 *go, u8 *buf, int length);
3078 +struct go7007 *go7007_alloc(struct go7007_board_info *board,
3079 + struct device *dev);
3080 +void go7007_remove(struct go7007 *go);
3083 +int go7007_construct_fw_image(struct go7007 *go, u8 **fw, int *fwlen);
3086 +int go7007_i2c_init(struct go7007 *go);
3087 +int go7007_i2c_remove(struct go7007 *go);
3089 +/* go7007-v4l2.c */
3090 +int go7007_v4l2_init(struct go7007 *go);
3091 +void go7007_v4l2_remove(struct go7007 *go);
3094 +int go7007_snd_init(struct go7007 *go);
3095 +int go7007_snd_remove(struct go7007 *go);
3096 diff --git a/drivers/staging/go7007/go7007-usb.c b/drivers/staging/go7007/go7007-usb.c
3097 new file mode 100644
3098 index 0000000..d4ed6d2
3100 +++ b/drivers/staging/go7007/go7007-usb.c
3103 + * Copyright (C) 2005-2006 Micronas USA Inc.
3105 + * This program is free software; you can redistribute it and/or modify
3106 + * it under the terms of the GNU General Public License (Version 2) as
3107 + * published by the Free Software Foundation.
3109 + * This program is distributed in the hope that it will be useful,
3110 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
3111 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3112 + * GNU General Public License for more details.
3114 + * You should have received a copy of the GNU General Public License
3115 + * along with this program; if not, write to the Free Software Foundation,
3116 + * Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
3119 +#include <linux/module.h>
3120 +#include <linux/version.h>
3121 +#include <linux/kernel.h>
3122 +#include <linux/init.h>
3123 +#include <linux/wait.h>
3124 +#include <linux/list.h>
3125 +#include <linux/slab.h>
3126 +#include <linux/time.h>
3127 +#include <linux/mm.h>
3128 +#include <linux/usb.h>
3129 +#include <linux/i2c.h>
3130 +#include <asm/byteorder.h>
3131 +#include <media/tvaudio.h>
3133 +#include "go7007-priv.h"
3134 +#include "wis-i2c.h"
3136 +static unsigned int assume_endura;
3137 +module_param(assume_endura, int, 0644);
3138 +MODULE_PARM_DESC(assume_endura, "when probing fails, hardware is a Pelco Endura");
3140 +/* #define GO7007_USB_DEBUG */
3141 +/* #define GO7007_I2C_DEBUG */ /* for debugging the EZ-USB I2C adapter */
3143 +#define HPI_STATUS_ADDR 0xFFF4
3144 +#define INT_PARAM_ADDR 0xFFF6
3145 +#define INT_INDEX_ADDR 0xFFF8
3148 + * Pipes on EZ-USB interface:
3151 + * 2 snd - Download firmware (control)
3152 + * 4 rcv - Read Interrupt (interrupt)
3153 + * 6 rcv - Read Video (bulk)
3154 + * 8 rcv - Read Audio (bulk)
3157 +#define GO7007_USB_EZUSB (1<<0)
3158 +#define GO7007_USB_EZUSB_I2C (1<<1)
3160 +struct go7007_usb_board {
3161 + unsigned int flags;
3162 + struct go7007_board_info main_info;
3165 +struct go7007_usb {
3166 + struct go7007_usb_board *board;
3167 + struct semaphore i2c_lock;
3168 + struct usb_device *usbdev;
3169 + struct urb *video_urbs[8];
3170 + struct urb *audio_urbs[8];
3171 + struct urb *intr_urb;
3174 +/*********************** Product specification data ***********************/
3176 +static struct go7007_usb_board board_matrix_ii = {
3177 + .flags = GO7007_USB_EZUSB,
3179 + .firmware = "go7007tv.bin",
3180 + .flags = GO7007_BOARD_HAS_AUDIO |
3181 + GO7007_BOARD_USE_ONBOARD_I2C,
3182 + .audio_flags = GO7007_AUDIO_I2S_MODE_1 |
3183 + GO7007_AUDIO_WORD_16,
3184 + .audio_rate = 48000,
3185 + .audio_bclk_div = 8,
3186 + .audio_main_div = 2,
3187 + .hpi_buffer_cap = 7,
3188 + .sensor_flags = GO7007_SENSOR_656 |
3189 + GO7007_SENSOR_VALID_ENABLE |
3190 + GO7007_SENSOR_TV |
3191 + GO7007_SENSOR_VBI |
3192 + GO7007_SENSOR_SCALING,
3193 + .num_i2c_devs = 1,
3196 + .id = I2C_DRIVERID_WIS_SAA7115,
3204 + .name = "Composite",
3208 + .name = "S-Video",
3214 +static struct go7007_usb_board board_matrix_reload = {
3215 + .flags = GO7007_USB_EZUSB,
3217 + .firmware = "go7007tv.bin",
3218 + .flags = GO7007_BOARD_HAS_AUDIO |
3219 + GO7007_BOARD_USE_ONBOARD_I2C,
3220 + .audio_flags = GO7007_AUDIO_I2S_MODE_1 |
3221 + GO7007_AUDIO_I2S_MASTER |
3222 + GO7007_AUDIO_WORD_16,
3223 + .audio_rate = 48000,
3224 + .audio_bclk_div = 8,
3225 + .audio_main_div = 2,
3226 + .hpi_buffer_cap = 7,
3227 + .sensor_flags = GO7007_SENSOR_656 |
3229 + .num_i2c_devs = 1,
3232 + .id = I2C_DRIVERID_WIS_SAA7113,
3240 + .name = "Composite",
3244 + .name = "S-Video",
3250 +static struct go7007_usb_board board_star_trek = {
3251 + .flags = GO7007_USB_EZUSB | GO7007_USB_EZUSB_I2C,
3253 + .firmware = "go7007tv.bin",
3254 + .flags = GO7007_BOARD_HAS_AUDIO, /* |
3255 + GO7007_BOARD_HAS_TUNER, */
3256 + .sensor_flags = GO7007_SENSOR_656 |
3257 + GO7007_SENSOR_VALID_ENABLE |
3258 + GO7007_SENSOR_TV |
3259 + GO7007_SENSOR_VBI |
3260 + GO7007_SENSOR_SCALING,
3261 + .audio_flags = GO7007_AUDIO_I2S_MODE_1 |
3262 + GO7007_AUDIO_WORD_16,
3263 + .audio_bclk_div = 8,
3264 + .audio_main_div = 2,
3265 + .hpi_buffer_cap = 7,
3266 + .num_i2c_devs = 1,
3269 + .id = I2C_DRIVERID_WIS_SAA7115,
3277 + /* .audio_input = AUDIO_EXTERN, */
3278 + .name = "Composite",
3282 + /* .audio_input = AUDIO_EXTERN, */
3283 + .name = "S-Video",
3286 + * .video_input = 3,
3287 + * .audio_input = AUDIO_TUNER,
3288 + * .name = "Tuner",
3295 +static struct go7007_usb_board board_px_tv402u = {
3296 + .flags = GO7007_USB_EZUSB | GO7007_USB_EZUSB_I2C,
3298 + .firmware = "go7007tv.bin",
3299 + .flags = GO7007_BOARD_HAS_AUDIO |
3300 + GO7007_BOARD_HAS_TUNER,
3301 + .sensor_flags = GO7007_SENSOR_656 |
3302 + GO7007_SENSOR_VALID_ENABLE |
3303 + GO7007_SENSOR_TV |
3304 + GO7007_SENSOR_VBI |
3305 + GO7007_SENSOR_SCALING,
3306 + .audio_flags = GO7007_AUDIO_I2S_MODE_1 |
3307 + GO7007_AUDIO_WORD_16,
3308 + .audio_bclk_div = 8,
3309 + .audio_main_div = 2,
3310 + .hpi_buffer_cap = 7,
3311 + .num_i2c_devs = 3,
3314 + .id = I2C_DRIVERID_WIS_SAA7115,
3318 + .id = I2C_DRIVERID_WIS_UDA1342,
3322 + .id = I2C_DRIVERID_WIS_SONY_TUNER,
3330 + .audio_input = TVAUDIO_INPUT_EXTERN,
3331 + .name = "Composite",
3335 + .audio_input = TVAUDIO_INPUT_EXTERN,
3336 + .name = "S-Video",
3340 + .audio_input = TVAUDIO_INPUT_TUNER,
3347 +static struct go7007_usb_board board_xmen = {
3350 + .firmware = "go7007tv.bin",
3351 + .flags = GO7007_BOARD_USE_ONBOARD_I2C,
3352 + .hpi_buffer_cap = 0,
3353 + .sensor_flags = GO7007_SENSOR_VREF_POLAR,
3354 + .sensor_width = 320,
3355 + .sensor_height = 240,
3356 + .sensor_framerate = 30030,
3357 + .audio_flags = GO7007_AUDIO_ONE_CHANNEL |
3358 + GO7007_AUDIO_I2S_MODE_3 |
3359 + GO7007_AUDIO_WORD_14 |
3360 + GO7007_AUDIO_I2S_MASTER |
3361 + GO7007_AUDIO_BCLK_POLAR |
3362 + GO7007_AUDIO_OKI_MODE,
3363 + .audio_rate = 8000,
3364 + .audio_bclk_div = 48,
3365 + .audio_main_div = 1,
3366 + .num_i2c_devs = 1,
3369 + .id = I2C_DRIVERID_WIS_OV7640,
3382 +static struct go7007_usb_board board_matrix_revolution = {
3383 + .flags = GO7007_USB_EZUSB,
3385 + .firmware = "go7007tv.bin",
3386 + .flags = GO7007_BOARD_HAS_AUDIO |
3387 + GO7007_BOARD_USE_ONBOARD_I2C,
3388 + .audio_flags = GO7007_AUDIO_I2S_MODE_1 |
3389 + GO7007_AUDIO_I2S_MASTER |
3390 + GO7007_AUDIO_WORD_16,
3391 + .audio_rate = 48000,
3392 + .audio_bclk_div = 8,
3393 + .audio_main_div = 2,
3394 + .hpi_buffer_cap = 7,
3395 + .sensor_flags = GO7007_SENSOR_656 |
3396 + GO7007_SENSOR_TV |
3397 + GO7007_SENSOR_VBI,
3398 + .num_i2c_devs = 1,
3401 + .id = I2C_DRIVERID_WIS_TW9903,
3409 + .name = "Composite",
3413 + .name = "S-Video",
3419 +static struct go7007_usb_board board_lifeview_lr192 = {
3420 + .flags = GO7007_USB_EZUSB,
3422 + .firmware = "go7007tv.bin",
3423 + .flags = GO7007_BOARD_HAS_AUDIO |
3424 + GO7007_BOARD_USE_ONBOARD_I2C,
3425 + .audio_flags = GO7007_AUDIO_I2S_MODE_1 |
3426 + GO7007_AUDIO_WORD_16,
3427 + .audio_rate = 48000,
3428 + .audio_bclk_div = 8,
3429 + .audio_main_div = 2,
3430 + .hpi_buffer_cap = 7,
3431 + .sensor_flags = GO7007_SENSOR_656 |
3432 + GO7007_SENSOR_VALID_ENABLE |
3433 + GO7007_SENSOR_TV |
3434 + GO7007_SENSOR_VBI |
3435 + GO7007_SENSOR_SCALING,
3436 + .num_i2c_devs = 0,
3441 + .name = "Composite",
3447 +static struct go7007_usb_board board_endura = {
3450 + .firmware = "go7007tv.bin",
3452 + .audio_flags = GO7007_AUDIO_I2S_MODE_1 |
3453 + GO7007_AUDIO_I2S_MASTER |
3454 + GO7007_AUDIO_WORD_16,
3455 + .audio_rate = 8000,
3456 + .audio_bclk_div = 48,
3457 + .audio_main_div = 8,
3458 + .hpi_buffer_cap = 0,
3459 + .sensor_flags = GO7007_SENSOR_656 |
3461 + .sensor_h_offset = 8,
3462 + .num_i2c_devs = 0,
3472 +static struct go7007_usb_board board_adlink_mpg24 = {
3475 + .firmware = "go7007tv.bin",
3476 + .flags = GO7007_BOARD_USE_ONBOARD_I2C,
3477 + .audio_flags = GO7007_AUDIO_I2S_MODE_1 |
3478 + GO7007_AUDIO_I2S_MASTER |
3479 + GO7007_AUDIO_WORD_16,
3480 + .audio_rate = 48000,
3481 + .audio_bclk_div = 8,
3482 + .audio_main_div = 2,
3483 + .hpi_buffer_cap = 0,
3484 + .sensor_flags = GO7007_SENSOR_656 |
3485 + GO7007_SENSOR_TV |
3486 + GO7007_SENSOR_VBI,
3487 + .num_i2c_devs = 1,
3490 + .id = I2C_DRIVERID_WIS_TW2804,
3491 + .addr = 0x00, /* yes, really */
3497 + .name = "Composite",
3503 +static struct usb_device_id go7007_usb_id_table[] = {
3505 + .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION |
3506 + USB_DEVICE_ID_MATCH_INT_INFO,
3507 + .idVendor = 0x0eb1, /* Vendor ID of WIS Technologies */
3508 + .idProduct = 0x7007, /* Product ID of GO7007SB chip */
3509 + .bcdDevice_lo = 0x200, /* Revision number of XMen */
3510 + .bcdDevice_hi = 0x200,
3511 + .bInterfaceClass = 255,
3512 + .bInterfaceSubClass = 0,
3513 + .bInterfaceProtocol = 255,
3514 + .driver_info = (kernel_ulong_t)GO7007_BOARDID_XMEN,
3517 + .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION,
3518 + .idVendor = 0x0eb1, /* Vendor ID of WIS Technologies */
3519 + .idProduct = 0x7007, /* Product ID of GO7007SB chip */
3520 + .bcdDevice_lo = 0x202, /* Revision number of Matrix II */
3521 + .bcdDevice_hi = 0x202,
3522 + .driver_info = (kernel_ulong_t)GO7007_BOARDID_MATRIX_II,
3525 + .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION,
3526 + .idVendor = 0x0eb1, /* Vendor ID of WIS Technologies */
3527 + .idProduct = 0x7007, /* Product ID of GO7007SB chip */
3528 + .bcdDevice_lo = 0x204, /* Revision number of Matrix */
3529 + .bcdDevice_hi = 0x204, /* Reloaded */
3530 + .driver_info = (kernel_ulong_t)GO7007_BOARDID_MATRIX_RELOAD,
3533 + .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION |
3534 + USB_DEVICE_ID_MATCH_INT_INFO,
3535 + .idVendor = 0x0eb1, /* Vendor ID of WIS Technologies */
3536 + .idProduct = 0x7007, /* Product ID of GO7007SB chip */
3537 + .bcdDevice_lo = 0x205, /* Revision number of XMen-II */
3538 + .bcdDevice_hi = 0x205,
3539 + .bInterfaceClass = 255,
3540 + .bInterfaceSubClass = 0,
3541 + .bInterfaceProtocol = 255,
3542 + .driver_info = (kernel_ulong_t)GO7007_BOARDID_XMEN_II,
3545 + .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION,
3546 + .idVendor = 0x0eb1, /* Vendor ID of WIS Technologies */
3547 + .idProduct = 0x7007, /* Product ID of GO7007SB chip */
3548 + .bcdDevice_lo = 0x208, /* Revision number of Star Trek */
3549 + .bcdDevice_hi = 0x208,
3550 + .driver_info = (kernel_ulong_t)GO7007_BOARDID_STAR_TREK,
3553 + .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION |
3554 + USB_DEVICE_ID_MATCH_INT_INFO,
3555 + .idVendor = 0x0eb1, /* Vendor ID of WIS Technologies */
3556 + .idProduct = 0x7007, /* Product ID of GO7007SB chip */
3557 + .bcdDevice_lo = 0x209, /* Revision number of XMen-III */
3558 + .bcdDevice_hi = 0x209,
3559 + .bInterfaceClass = 255,
3560 + .bInterfaceSubClass = 0,
3561 + .bInterfaceProtocol = 255,
3562 + .driver_info = (kernel_ulong_t)GO7007_BOARDID_XMEN_III,
3565 + .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION,
3566 + .idVendor = 0x0eb1, /* Vendor ID of WIS Technologies */
3567 + .idProduct = 0x7007, /* Product ID of GO7007SB chip */
3568 + .bcdDevice_lo = 0x210, /* Revision number of Matrix */
3569 + .bcdDevice_hi = 0x210, /* Revolution */
3570 + .driver_info = (kernel_ulong_t)GO7007_BOARDID_MATRIX_REV,
3573 + .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION,
3574 + .idVendor = 0x093b, /* Vendor ID of Plextor */
3575 + .idProduct = 0xa102, /* Product ID of M402U */
3576 + .bcdDevice_lo = 0x1, /* revision number of Blueberry */
3577 + .bcdDevice_hi = 0x1,
3578 + .driver_info = (kernel_ulong_t)GO7007_BOARDID_PX_M402U,
3581 + .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION,
3582 + .idVendor = 0x093b, /* Vendor ID of Plextor */
3583 + .idProduct = 0xa104, /* Product ID of TV402U */
3584 + .bcdDevice_lo = 0x1,
3585 + .bcdDevice_hi = 0x1,
3586 + .driver_info = (kernel_ulong_t)GO7007_BOARDID_PX_TV402U_ANY,
3589 + .match_flags = USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION,
3590 + .idVendor = 0x10fd, /* Vendor ID of Anubis Electronics */
3591 + .idProduct = 0xde00, /* Product ID of Lifeview LR192 */
3592 + .bcdDevice_lo = 0x1,
3593 + .bcdDevice_hi = 0x1,
3594 + .driver_info = (kernel_ulong_t)GO7007_BOARDID_LIFEVIEW_LR192,
3596 + { } /* Terminating entry */
3599 +MODULE_DEVICE_TABLE(usb, go7007_usb_id_table);
3601 +/********************* Driver for EZ-USB HPI interface *********************/
3603 +static int go7007_usb_vendor_request(struct go7007 *go, int request,
3604 + int value, int index, void *transfer_buffer, int length, int in)
3606 + struct go7007_usb *usb = go->hpi_context;
3607 + int timeout = 5000;
3610 + return usb_control_msg(usb->usbdev,
3611 + usb_rcvctrlpipe(usb->usbdev, 0), request,
3612 + USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
3613 + value, index, transfer_buffer, length, timeout);
3615 + return usb_control_msg(usb->usbdev,
3616 + usb_sndctrlpipe(usb->usbdev, 0), request,
3617 + USB_TYPE_VENDOR | USB_RECIP_DEVICE,
3618 + value, index, transfer_buffer, length, timeout);
3622 +static int go7007_usb_interface_reset(struct go7007 *go)
3624 + struct go7007_usb *usb = go->hpi_context;
3625 + u16 intr_val, intr_data;
3627 + /* Reset encoder */
3628 + if (go7007_write_interrupt(go, 0x0001, 0x0001) < 0)
3632 + if (usb->board->flags & GO7007_USB_EZUSB) {
3633 + /* Reset buffer in EZ-USB */
3634 +#ifdef GO7007_USB_DEBUG
3635 + printk(KERN_DEBUG "go7007-usb: resetting EZ-USB buffers\n");
3637 + if (go7007_usb_vendor_request(go, 0x10, 0, 0, NULL, 0, 0) < 0 ||
3638 + go7007_usb_vendor_request(go, 0x10, 0, 0, NULL, 0, 0) < 0)
3641 + /* Reset encoder again */
3642 + if (go7007_write_interrupt(go, 0x0001, 0x0001) < 0)
3647 + /* Wait for an interrupt to indicate successful hardware reset */
3648 + if (go7007_read_interrupt(go, &intr_val, &intr_data) < 0 ||
3649 + (intr_val & ~0x1) != 0x55aa) {
3651 + "go7007-usb: unable to reset the USB interface\n");
3657 +static int go7007_usb_ezusb_write_interrupt(struct go7007 *go,
3658 + int addr, int data)
3660 + struct go7007_usb *usb = go->hpi_context;
3663 + int timeout = 500;
3665 +#ifdef GO7007_USB_DEBUG
3667 + "go7007-usb: WriteInterrupt: %04x %04x\n", addr, data);
3670 + for (i = 0; i < 100; ++i) {
3671 + r = usb_control_msg(usb->usbdev,
3672 + usb_rcvctrlpipe(usb->usbdev, 0), 0x14,
3673 + USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
3674 + 0, HPI_STATUS_ADDR, &status_reg,
3675 + sizeof(status_reg), timeout);
3677 + goto write_int_error;
3678 + __le16_to_cpus(&status_reg);
3679 + if (!(status_reg & 0x0010))
3685 + "go7007-usb: device is hung, status reg = 0x%04x\n",
3689 + r = usb_control_msg(usb->usbdev, usb_sndctrlpipe(usb->usbdev, 0), 0x12,
3690 + USB_TYPE_VENDOR | USB_RECIP_DEVICE, data,
3691 + INT_PARAM_ADDR, NULL, 0, timeout);
3693 + goto write_int_error;
3694 + r = usb_control_msg(usb->usbdev, usb_sndctrlpipe(usb->usbdev, 0),
3695 + 0x12, USB_TYPE_VENDOR | USB_RECIP_DEVICE, addr,
3696 + INT_INDEX_ADDR, NULL, 0, timeout);
3698 + goto write_int_error;
3702 + printk(KERN_ERR "go7007-usb: error in WriteInterrupt: %d\n", r);
3706 +static int go7007_usb_onboard_write_interrupt(struct go7007 *go,
3707 + int addr, int data)
3709 + struct go7007_usb *usb = go->hpi_context;
3712 + int timeout = 500;
3714 +#ifdef GO7007_USB_DEBUG
3716 + "go7007-usb: WriteInterrupt: %04x %04x\n", addr, data);
3719 + tbuf = kmalloc(8, GFP_KERNEL);
3722 + memset(tbuf, 0, 8);
3723 + tbuf[0] = data & 0xff;
3724 + tbuf[1] = data >> 8;
3725 + tbuf[2] = addr & 0xff;
3726 + tbuf[3] = addr >> 8;
3727 + r = usb_control_msg(usb->usbdev, usb_sndctrlpipe(usb->usbdev, 2), 0x00,
3728 + USB_TYPE_VENDOR | USB_RECIP_ENDPOINT, 0x55aa,
3729 + 0xf0f0, tbuf, 8, timeout);
3732 + printk(KERN_ERR "go7007-usb: error in WriteInterrupt: %d\n", r);
3738 +static void go7007_usb_readinterrupt_complete(struct urb *urb)
3740 + struct go7007 *go = (struct go7007 *)urb->context;
3741 + u16 *regs = (u16 *)urb->transfer_buffer;
3743 + if (urb->status != 0) {
3744 + if (urb->status != -ESHUTDOWN &&
3745 + go->status != STATUS_SHUTDOWN) {
3747 + "go7007-usb: error in read interrupt: %d\n",
3750 + wake_up(&go->interrupt_waitq);
3753 + } else if (urb->actual_length != urb->transfer_buffer_length) {
3754 + printk(KERN_ERR "go7007-usb: short read in interrupt pipe!\n");
3756 + go->interrupt_available = 1;
3757 + go->interrupt_data = __le16_to_cpu(regs[0]);
3758 + go->interrupt_value = __le16_to_cpu(regs[1]);
3759 +#ifdef GO7007_USB_DEBUG
3760 + printk(KERN_DEBUG "go7007-usb: ReadInterrupt: %04x %04x\n",
3761 + go->interrupt_value, go->interrupt_data);
3765 + wake_up(&go->interrupt_waitq);
3768 +static int go7007_usb_read_interrupt(struct go7007 *go)
3770 + struct go7007_usb *usb = go->hpi_context;
3773 + r = usb_submit_urb(usb->intr_urb, GFP_KERNEL);
3776 + "go7007-usb: unable to submit interrupt urb: %d\n", r);
3782 +static void go7007_usb_read_video_pipe_complete(struct urb *urb)
3784 + struct go7007 *go = (struct go7007 *)urb->context;
3787 + if (!go->streaming) {
3788 + wake_up_interruptible(&go->frame_waitq);
3791 + if (urb->status != 0) {
3792 + printk(KERN_ERR "go7007-usb: error in video pipe: %d\n",
3796 + if (urb->actual_length != urb->transfer_buffer_length) {
3797 + printk(KERN_ERR "go7007-usb: short read in video pipe!\n");
3800 + go7007_parse_video_stream(go, urb->transfer_buffer, urb->actual_length);
3801 + r = usb_submit_urb(urb, GFP_ATOMIC);
3803 + printk(KERN_ERR "go7007-usb: error in video pipe: %d\n", r);
3806 +static void go7007_usb_read_audio_pipe_complete(struct urb *urb)
3808 + struct go7007 *go = (struct go7007 *)urb->context;
3811 + if (!go->streaming)
3813 + if (urb->status != 0) {
3814 + printk(KERN_ERR "go7007-usb: error in audio pipe: %d\n",
3818 + if (urb->actual_length != urb->transfer_buffer_length) {
3819 + printk(KERN_ERR "go7007-usb: short read in audio pipe!\n");
3822 + if (go->audio_deliver != NULL)
3823 + go->audio_deliver(go, urb->transfer_buffer, urb->actual_length);
3824 + r = usb_submit_urb(urb, GFP_ATOMIC);
3826 + printk(KERN_ERR "go7007-usb: error in audio pipe: %d\n", r);
3829 +static int go7007_usb_stream_start(struct go7007 *go)
3831 + struct go7007_usb *usb = go->hpi_context;
3834 + for (i = 0; i < 8; ++i) {
3835 + r = usb_submit_urb(usb->video_urbs[i], GFP_KERNEL);
3837 + printk(KERN_ERR "go7007-usb: error submitting video "
3838 + "urb %d: %d\n", i, r);
3839 + goto video_submit_failed;
3842 + if (!go->audio_enabled)
3845 + for (i = 0; i < 8; ++i) {
3846 + r = usb_submit_urb(usb->audio_urbs[i], GFP_KERNEL);
3848 + printk(KERN_ERR "go7007-usb: error submitting audio "
3849 + "urb %d: %d\n", i, r);
3850 + goto audio_submit_failed;
3855 +audio_submit_failed:
3856 + for (i = 0; i < 8; ++i)
3857 + usb_kill_urb(usb->audio_urbs[i]);
3858 +video_submit_failed:
3859 + for (i = 0; i < 8; ++i)
3860 + usb_kill_urb(usb->video_urbs[i]);
3864 +static int go7007_usb_stream_stop(struct go7007 *go)
3866 + struct go7007_usb *usb = go->hpi_context;
3869 + if (go->status == STATUS_SHUTDOWN)
3871 + for (i = 0; i < 8; ++i)
3872 + usb_kill_urb(usb->video_urbs[i]);
3873 + if (go->audio_enabled)
3874 + for (i = 0; i < 8; ++i)
3875 + usb_kill_urb(usb->audio_urbs[i]);
3879 +static int go7007_usb_send_firmware(struct go7007 *go, u8 *data, int len)
3881 + struct go7007_usb *usb = go->hpi_context;
3882 + int transferred, pipe;
3883 + int timeout = 500;
3885 +#ifdef GO7007_USB_DEBUG
3886 + printk(KERN_DEBUG "go7007-usb: DownloadBuffer sending %d bytes\n", len);
3889 + if (usb->board->flags & GO7007_USB_EZUSB)
3890 + pipe = usb_sndbulkpipe(usb->usbdev, 2);
3892 + pipe = usb_sndbulkpipe(usb->usbdev, 3);
3894 + return usb_bulk_msg(usb->usbdev, pipe, data, len,
3895 + &transferred, timeout);
3898 +static struct go7007_hpi_ops go7007_usb_ezusb_hpi_ops = {
3899 + .interface_reset = go7007_usb_interface_reset,
3900 + .write_interrupt = go7007_usb_ezusb_write_interrupt,
3901 + .read_interrupt = go7007_usb_read_interrupt,
3902 + .stream_start = go7007_usb_stream_start,
3903 + .stream_stop = go7007_usb_stream_stop,
3904 + .send_firmware = go7007_usb_send_firmware,
3907 +static struct go7007_hpi_ops go7007_usb_onboard_hpi_ops = {
3908 + .interface_reset = go7007_usb_interface_reset,
3909 + .write_interrupt = go7007_usb_onboard_write_interrupt,
3910 + .read_interrupt = go7007_usb_read_interrupt,
3911 + .stream_start = go7007_usb_stream_start,
3912 + .stream_stop = go7007_usb_stream_stop,
3913 + .send_firmware = go7007_usb_send_firmware,
3916 +/********************* Driver for EZ-USB I2C adapter *********************/
3918 +static int go7007_usb_i2c_master_xfer(struct i2c_adapter *adapter,
3919 + struct i2c_msg msgs[], int num)
3921 + struct go7007 *go = i2c_get_adapdata(adapter);
3922 + struct go7007_usb *usb = go->hpi_context;
3927 + if (go->status == STATUS_SHUTDOWN)
3930 + down(&usb->i2c_lock);
3932 + for (i = 0; i < num; ++i) {
3933 + /* The hardware command is "write some bytes then read some
3934 + * bytes", so we try to coalesce a write followed by a read
3935 + * into a single USB transaction */
3936 + if (i + 1 < num && msgs[i].addr == msgs[i + 1].addr &&
3937 + !(msgs[i].flags & I2C_M_RD) &&
3938 + (msgs[i + 1].flags & I2C_M_RD)) {
3939 +#ifdef GO7007_I2C_DEBUG
3940 + printk(KERN_DEBUG "go7007-usb: i2c write/read %d/%d "
3941 + "bytes on %02x\n", msgs[i].len,
3942 + msgs[i + 1].len, msgs[i].addr);
3945 + buf[1] = msgs[i].len + 1;
3946 + buf[2] = msgs[i].addr << 1;
3947 + memcpy(&buf[3], msgs[i].buf, msgs[i].len);
3948 + buf_len = msgs[i].len + 3;
3949 + buf[buf_len++] = msgs[++i].len;
3950 + } else if (msgs[i].flags & I2C_M_RD) {
3951 +#ifdef GO7007_I2C_DEBUG
3952 + printk(KERN_DEBUG "go7007-usb: i2c read %d "
3953 + "bytes on %02x\n", msgs[i].len,
3958 + buf[2] = msgs[i].addr << 1;
3959 + buf[3] = msgs[i].len;
3962 +#ifdef GO7007_I2C_DEBUG
3963 + printk(KERN_DEBUG "go7007-usb: i2c write %d "
3964 + "bytes on %02x\n", msgs[i].len,
3968 + buf[1] = msgs[i].len + 1;
3969 + buf[2] = msgs[i].addr << 1;
3970 + memcpy(&buf[3], msgs[i].buf, msgs[i].len);
3971 + buf_len = msgs[i].len + 3;
3972 + buf[buf_len++] = 0;
3974 + if (go7007_usb_vendor_request(go, 0x24, 0, 0,
3975 + buf, buf_len, 0) < 0)
3977 + if (msgs[i].flags & I2C_M_RD) {
3978 + memset(buf, 0, sizeof(buf));
3979 + if (go7007_usb_vendor_request(go, 0x25, 0, 0, buf,
3980 + msgs[i].len + 1, 1) < 0)
3982 + memcpy(msgs[i].buf, buf + 1, msgs[i].len);
3988 + up(&usb->i2c_lock);
3992 +static u32 go7007_usb_functionality(struct i2c_adapter *adapter)
3994 + /* No errors are reported by the hardware, so we don't bother
3995 + * supporting quick writes to avoid confusing probing */
3996 + return (I2C_FUNC_SMBUS_EMUL) & ~I2C_FUNC_SMBUS_QUICK;
3999 +static struct i2c_algorithm go7007_usb_algo = {
4000 + .master_xfer = go7007_usb_i2c_master_xfer,
4001 + .functionality = go7007_usb_functionality,
4004 +static struct i2c_adapter go7007_usb_adap_templ = {
4005 + .owner = THIS_MODULE,
4006 + .class = I2C_CLASS_TV_ANALOG,
4007 + .name = "WIS GO7007SB EZ-USB",
4008 + .id = I2C_ALGO_GO7007_USB,
4009 + .algo = &go7007_usb_algo,
4012 +/********************* USB add/remove functions *********************/
4014 +static int go7007_usb_probe(struct usb_interface *intf,
4015 + const struct usb_device_id *id)
4017 + struct go7007 *go;
4018 + struct go7007_usb *usb;
4019 + struct go7007_usb_board *board;
4020 + struct usb_device *usbdev = interface_to_usbdev(intf);
4022 + int video_pipe, i, v_urb_len;
4024 + printk(KERN_DEBUG "go7007-usb: probing new GO7007 USB board\n");
4026 + switch (id->driver_info) {
4027 + case GO7007_BOARDID_MATRIX_II:
4028 + name = "WIS Matrix II or compatible";
4029 + board = &board_matrix_ii;
4031 + case GO7007_BOARDID_MATRIX_RELOAD:
4032 + name = "WIS Matrix Reloaded or compatible";
4033 + board = &board_matrix_reload;
4035 + case GO7007_BOARDID_MATRIX_REV:
4036 + name = "WIS Matrix Revolution or compatible";
4037 + board = &board_matrix_revolution;
4039 + case GO7007_BOARDID_STAR_TREK:
4040 + name = "WIS Star Trek or compatible";
4041 + board = &board_star_trek;
4043 + case GO7007_BOARDID_XMEN:
4044 + name = "WIS XMen or compatible";
4045 + board = &board_xmen;
4047 + case GO7007_BOARDID_XMEN_II:
4048 + name = "WIS XMen II or compatible";
4049 + board = &board_xmen;
4051 + case GO7007_BOARDID_XMEN_III:
4052 + name = "WIS XMen III or compatible";
4053 + board = &board_xmen;
4055 + case GO7007_BOARDID_PX_M402U:
4056 + name = "Plextor PX-M402U";
4057 + board = &board_matrix_ii;
4059 + case GO7007_BOARDID_PX_TV402U_ANY:
4060 + name = "Plextor PX-TV402U (unknown tuner)";
4061 + board = &board_px_tv402u;
4063 + case GO7007_BOARDID_LIFEVIEW_LR192:
4064 + printk(KERN_ERR "go7007-usb: The Lifeview TV Walker Ultra "
4065 + "is not supported. Sorry!\n");
4067 + name = "Lifeview TV Walker Ultra";
4068 + board = &board_lifeview_lr192;
4071 + printk(KERN_ERR "go7007-usb: unknown board ID %d!\n",
4072 + (unsigned int)id->driver_info);
4076 + usb = kmalloc(sizeof(struct go7007_usb), GFP_KERNEL);
4079 + memset(usb, 0, sizeof(struct go7007_usb));
4081 + /* Allocate the URB and buffer for receiving incoming interrupts */
4082 + usb->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
4083 + if (usb->intr_urb == NULL)
4085 + usb->intr_urb->transfer_buffer = kmalloc(2*sizeof(u16), GFP_KERNEL);
4086 + if (usb->intr_urb->transfer_buffer == NULL)
4089 + go = go7007_alloc(&board->main_info, &intf->dev);
4092 + usb->board = board;
4093 + usb->usbdev = usbdev;
4094 + go->board_id = id->driver_info;
4095 + strncpy(go->name, name, sizeof(go->name));
4096 + if (board->flags & GO7007_USB_EZUSB)
4097 + go->hpi_ops = &go7007_usb_ezusb_hpi_ops;
4099 + go->hpi_ops = &go7007_usb_onboard_hpi_ops;
4100 + go->hpi_context = usb;
4101 + usb_fill_int_urb(usb->intr_urb, usb->usbdev,
4102 + usb_rcvintpipe(usb->usbdev, 4),
4103 + usb->intr_urb->transfer_buffer, 2*sizeof(u16),
4104 + go7007_usb_readinterrupt_complete, go, 8);
4105 + usb_set_intfdata(intf, go);
4107 + /* Boot the GO7007 */
4108 + if (go7007_boot_encoder(go, go->board_info->flags &
4109 + GO7007_BOARD_USE_ONBOARD_I2C) < 0)
4112 + /* Register the EZ-USB I2C adapter, if we're using it */
4113 + if (board->flags & GO7007_USB_EZUSB_I2C) {
4114 + memcpy(&go->i2c_adapter, &go7007_usb_adap_templ,
4115 + sizeof(go7007_usb_adap_templ));
4116 + init_MUTEX(&usb->i2c_lock);
4117 + go->i2c_adapter.dev.parent = go->dev;
4118 + i2c_set_adapdata(&go->i2c_adapter, go);
4119 + if (i2c_add_adapter(&go->i2c_adapter) < 0) {
4121 + "go7007-usb: error: i2c_add_adapter failed\n");
4124 + go->i2c_adapter_online = 1;
4127 + /* Pelco and Adlink reused the XMen and XMen-III vendor and product
4128 + * IDs for their own incompatible designs. We can detect XMen boards
4129 + * by probing the sensor, but there is no way to probe the sensors on
4130 + * the Pelco and Adlink designs so we default to the Adlink. If it
4131 + * is actually a Pelco, the user must set the assume_endura module
4133 + if ((go->board_id == GO7007_BOARDID_XMEN ||
4134 + go->board_id == GO7007_BOARDID_XMEN_III) &&
4135 + go->i2c_adapter_online) {
4136 + union i2c_smbus_data data;
4138 + /* Check to see if register 0x0A is 0x76 */
4139 + i2c_smbus_xfer(&go->i2c_adapter, 0x21, I2C_CLIENT_SCCB,
4140 + I2C_SMBUS_READ, 0x0A, I2C_SMBUS_BYTE_DATA, &data);
4141 + if (data.byte != 0x76) {
4142 + if (assume_endura) {
4143 + go->board_id = GO7007_BOARDID_ENDURA;
4144 + usb->board = board = &board_endura;
4145 + go->board_info = &board->main_info;
4146 + strncpy(go->name, "Pelco Endura",
4147 + sizeof(go->name));
4151 + /* set GPIO5 to be an output, currently low */
4152 + go7007_write_addr(go, 0x3c82, 0x0000);
4153 + go7007_write_addr(go, 0x3c80, 0x00df);
4154 + /* read channel number from GPIO[1:0] */
4155 + go7007_read_addr(go, 0x3c81, &channel);
4157 + go->board_id = GO7007_BOARDID_ADLINK_MPG24;
4158 + usb->board = board = &board_adlink_mpg24;
4159 + go->board_info = &board->main_info;
4160 + go->channel_number = channel;
4161 + snprintf(go->name, sizeof(go->name),
4162 + "Adlink PCI-MPG24, channel #%d",
4168 + /* Probe the tuner model on the TV402U */
4169 + if (go->board_id == GO7007_BOARDID_PX_TV402U_ANY) {
4172 + /* Board strapping indicates tuner model */
4173 + if (go7007_usb_vendor_request(go, 0x41, 0, 0, data, 3, 1) < 0) {
4174 + printk(KERN_ERR "go7007-usb: GPIO read failed!\n");
4177 + switch (data[0] >> 6) {
4179 + go->board_id = GO7007_BOARDID_PX_TV402U_EU;
4180 + go->tuner_type = TUNER_SONY_BTF_PG472Z;
4181 + strncpy(go->name, "Plextor PX-TV402U-EU",
4182 + sizeof(go->name));
4185 + go->board_id = GO7007_BOARDID_PX_TV402U_JP;
4186 + go->tuner_type = TUNER_SONY_BTF_PK467Z;
4187 + strncpy(go->name, "Plextor PX-TV402U-JP",
4188 + sizeof(go->name));
4191 + go->board_id = GO7007_BOARDID_PX_TV402U_NA;
4192 + go->tuner_type = TUNER_SONY_BTF_PB463Z;
4193 + strncpy(go->name, "Plextor PX-TV402U-NA",
4194 + sizeof(go->name));
4197 + printk(KERN_DEBUG "go7007-usb: unable to detect "
4201 + /* Configure tuner mode selection inputs connected
4202 + * to the EZ-USB GPIO output pins */
4203 + if (go7007_usb_vendor_request(go, 0x40, 0x7f02, 0,
4204 + NULL, 0, 0) < 0) {
4206 + "go7007-usb: GPIO write failed!\n");
4211 + /* Print a nasty message if the user attempts to use a USB2.0 device in
4212 + * a USB1.1 port. There will be silent corruption of the stream. */
4213 + if ((board->flags & GO7007_USB_EZUSB) &&
4214 + usbdev->speed != USB_SPEED_HIGH)
4215 + printk(KERN_ERR "go7007-usb: *** WARNING *** This device "
4216 + "must be connected to a USB 2.0 port! "
4217 + "Attempting to capture video through a USB 1.1 "
4218 + "port will result in stream corruption, even "
4219 + "at low bitrates!\n");
4221 + /* Do any final GO7007 initialization, then register the
4222 + * V4L2 and ALSA interfaces */
4223 + if (go7007_register_encoder(go) < 0)
4226 + /* Allocate the URBs and buffers for receiving the video stream */
4227 + if (board->flags & GO7007_USB_EZUSB) {
4229 + video_pipe = usb_rcvbulkpipe(usb->usbdev, 6);
4232 + video_pipe = usb_rcvbulkpipe(usb->usbdev, 1);
4234 + for (i = 0; i < 8; ++i) {
4235 + usb->video_urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
4236 + if (usb->video_urbs[i] == NULL)
4238 + usb->video_urbs[i]->transfer_buffer =
4239 + kmalloc(v_urb_len, GFP_KERNEL);
4240 + if (usb->video_urbs[i]->transfer_buffer == NULL)
4242 + usb_fill_bulk_urb(usb->video_urbs[i], usb->usbdev, video_pipe,
4243 + usb->video_urbs[i]->transfer_buffer, v_urb_len,
4244 + go7007_usb_read_video_pipe_complete, go);
4247 + /* Allocate the URBs and buffers for receiving the audio stream */
4248 + if ((board->flags & GO7007_USB_EZUSB) && go->audio_enabled)
4249 + for (i = 0; i < 8; ++i) {
4250 + usb->audio_urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
4251 + if (usb->audio_urbs[i] == NULL)
4253 + usb->audio_urbs[i]->transfer_buffer = kmalloc(4096,
4255 + if (usb->audio_urbs[i]->transfer_buffer == NULL)
4257 + usb_fill_bulk_urb(usb->audio_urbs[i], usb->usbdev,
4258 + usb_rcvbulkpipe(usb->usbdev, 8),
4259 + usb->audio_urbs[i]->transfer_buffer, 4096,
4260 + go7007_usb_read_audio_pipe_complete, go);
4264 + go->status = STATUS_ONLINE;
4268 + go->status = STATUS_SHUTDOWN;
4272 + if (usb->intr_urb) {
4273 + kfree(usb->intr_urb->transfer_buffer);
4274 + usb_free_urb(usb->intr_urb);
4280 +static void go7007_usb_disconnect(struct usb_interface *intf)
4282 + struct go7007 *go = usb_get_intfdata(intf);
4283 + struct go7007_usb *usb = go->hpi_context;
4286 + go->status = STATUS_SHUTDOWN;
4287 + usb_kill_urb(usb->intr_urb);
4289 + /* Free USB-related structs */
4290 + for (i = 0; i < 8; ++i) {
4291 + if (usb->video_urbs[i] != NULL) {
4292 + if (usb->video_urbs[i]->transfer_buffer != NULL)
4293 + kfree(usb->video_urbs[i]->transfer_buffer);
4294 + usb_free_urb(usb->video_urbs[i]);
4296 + if (usb->audio_urbs[i] != NULL) {
4297 + if (usb->audio_urbs[i]->transfer_buffer != NULL)
4298 + kfree(usb->audio_urbs[i]->transfer_buffer);
4299 + usb_free_urb(usb->audio_urbs[i]);
4302 + kfree(usb->intr_urb->transfer_buffer);
4303 + usb_free_urb(usb->intr_urb);
4305 + kfree(go->hpi_context);
4307 + go7007_remove(go);
4310 +static struct usb_driver go7007_usb_driver = {
4312 + .probe = go7007_usb_probe,
4313 + .disconnect = go7007_usb_disconnect,
4314 + .id_table = go7007_usb_id_table,
4317 +static int __init go7007_usb_init(void)
4319 + return usb_register(&go7007_usb_driver);
4322 +static void __exit go7007_usb_cleanup(void)
4324 + usb_deregister(&go7007_usb_driver);
4327 +module_init(go7007_usb_init);
4328 +module_exit(go7007_usb_cleanup);
4330 +MODULE_LICENSE("GPL v2");
4331 diff --git a/drivers/staging/go7007/go7007-v4l2.c b/drivers/staging/go7007/go7007-v4l2.c
4332 new file mode 100644
4333 index 0000000..d54d019
4335 +++ b/drivers/staging/go7007/go7007-v4l2.c
4338 + * Copyright (C) 2005-2006 Micronas USA Inc.
4340 + * This program is free software; you can redistribute it and/or modify
4341 + * it under the terms of the GNU General Public License (Version 2) as
4342 + * published by the Free Software Foundation.
4344 + * This program is distributed in the hope that it will be useful,
4345 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4346 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4347 + * GNU General Public License for more details.
4349 + * You should have received a copy of the GNU General Public License
4350 + * along with this program; if not, write to the Free Software Foundation,
4351 + * Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
4354 +#include <linux/module.h>
4355 +#include <linux/init.h>
4356 +#include <linux/version.h>
4357 +#include <linux/delay.h>
4358 +#include <linux/sched.h>
4359 +#include <linux/spinlock.h>
4360 +#include <linux/fs.h>
4361 +#include <linux/unistd.h>
4362 +#include <linux/time.h>
4363 +#include <linux/vmalloc.h>
4364 +#include <linux/pagemap.h>
4365 +#include <linux/videodev.h>
4366 +#include <linux/video_decoder.h>
4367 +#include <media/v4l2-common.h>
4368 +#include <media/v4l2-ioctl.h>
4369 +#include <linux/i2c.h>
4370 +#include <linux/semaphore.h>
4371 +#include <linux/uaccess.h>
4372 +#include <asm/system.h>
4374 +#include "go7007.h"
4375 +#include "go7007-priv.h"
4376 +#include "wis-i2c.h"
4378 +static void deactivate_buffer(struct go7007_buffer *gobuf)
4382 + if (gobuf->state != BUF_STATE_IDLE) {
4383 + list_del(&gobuf->stream);
4384 + gobuf->state = BUF_STATE_IDLE;
4386 + if (gobuf->page_count > 0) {
4387 + for (i = 0; i < gobuf->page_count; ++i)
4388 + page_cache_release(gobuf->pages[i]);
4389 + gobuf->page_count = 0;
4393 +static void abort_queued(struct go7007 *go)
4395 + struct go7007_buffer *gobuf, *next;
4397 + list_for_each_entry_safe(gobuf, next, &go->stream, stream) {
4398 + deactivate_buffer(gobuf);
4402 +static int go7007_streamoff(struct go7007 *go)
4404 + int retval = -EINVAL;
4405 + unsigned long flags;
4407 + down(&go->hw_lock);
4408 + if (go->streaming) {
4409 + go->streaming = 0;
4410 + go7007_stream_stop(go);
4411 + spin_lock_irqsave(&go->spinlock, flags);
4413 + spin_unlock_irqrestore(&go->spinlock, flags);
4414 + go7007_reset_encoder(go);
4421 +static int go7007_open(struct inode *inode, struct file *file)
4423 + struct go7007 *go = video_get_drvdata(video_devdata(file));
4424 + struct go7007_file *gofh;
4426 + if (go->status != STATUS_ONLINE)
4428 + gofh = kmalloc(sizeof(struct go7007_file), GFP_KERNEL);
4433 + init_MUTEX(&gofh->lock);
4434 + gofh->buf_count = 0;
4435 + file->private_data = gofh;
4439 +static int go7007_release(struct inode *inode, struct file *file)
4441 + struct go7007_file *gofh = file->private_data;
4442 + struct go7007 *go = gofh->go;
4444 + if (gofh->buf_count > 0) {
4445 + go7007_streamoff(go);
4447 + kfree(gofh->bufs);
4448 + gofh->buf_count = 0;
4451 + if (--go->ref_count == 0)
4453 + file->private_data = NULL;
4457 +static u32 get_frame_type_flag(struct go7007_buffer *gobuf, int format)
4459 + u8 *f = page_address(gobuf->pages[0]);
4462 + case GO7007_FORMAT_MJPEG:
4463 + return V4L2_BUF_FLAG_KEYFRAME;
4464 + case GO7007_FORMAT_MPEG4:
4465 + switch ((f[gobuf->frame_offset + 4] >> 6) & 0x3) {
4467 + return V4L2_BUF_FLAG_KEYFRAME;
4469 + return V4L2_BUF_FLAG_PFRAME;
4471 + return V4L2_BUF_FLAG_BFRAME;
4475 + case GO7007_FORMAT_MPEG1:
4476 + case GO7007_FORMAT_MPEG2:
4477 + switch ((f[gobuf->frame_offset + 5] >> 3) & 0x7) {
4479 + return V4L2_BUF_FLAG_KEYFRAME;
4481 + return V4L2_BUF_FLAG_PFRAME;
4483 + return V4L2_BUF_FLAG_BFRAME;
4492 +static int set_capture_size(struct go7007 *go, struct v4l2_format *fmt, int try)
4494 + int sensor_height = 0, sensor_width = 0;
4495 + int width, height, i;
4497 + if (fmt != NULL && fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG &&
4498 + fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MPEG &&
4499 + fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MPEG4)
4502 + switch (go->standard) {
4503 + case GO7007_STD_NTSC:
4504 + sensor_width = 720;
4505 + sensor_height = 480;
4507 + case GO7007_STD_PAL:
4508 + sensor_width = 720;
4509 + sensor_height = 576;
4511 + case GO7007_STD_OTHER:
4512 + sensor_width = go->board_info->sensor_width;
4513 + sensor_height = go->board_info->sensor_height;
4517 + if (fmt == NULL) {
4518 + width = sensor_width;
4519 + height = sensor_height;
4520 + } else if (go->board_info->sensor_flags & GO7007_SENSOR_SCALING) {
4521 + if (fmt->fmt.pix.width > sensor_width)
4522 + width = sensor_width;
4523 + else if (fmt->fmt.pix.width < 144)
4526 + width = fmt->fmt.pix.width & ~0x0f;
4528 + if (fmt->fmt.pix.height > sensor_height)
4529 + height = sensor_height;
4530 + else if (fmt->fmt.pix.height < 96)
4533 + height = fmt->fmt.pix.height & ~0x0f;
4535 + int requested_size = fmt->fmt.pix.width * fmt->fmt.pix.height;
4536 + int sensor_size = sensor_width * sensor_height;
4538 + if (64 * requested_size < 9 * sensor_size) {
4539 + width = sensor_width / 4;
4540 + height = sensor_height / 4;
4541 + } else if (64 * requested_size < 36 * sensor_size) {
4542 + width = sensor_width / 2;
4543 + height = sensor_height / 2;
4545 + width = sensor_width;
4546 + height = sensor_height;
4552 + if (fmt != NULL) {
4553 + u32 pixelformat = fmt->fmt.pix.pixelformat;
4555 + memset(fmt, 0, sizeof(*fmt));
4556 + fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
4557 + fmt->fmt.pix.width = width;
4558 + fmt->fmt.pix.height = height;
4559 + fmt->fmt.pix.pixelformat = pixelformat;
4560 + fmt->fmt.pix.field = V4L2_FIELD_NONE;
4561 + fmt->fmt.pix.bytesperline = 0;
4562 + fmt->fmt.pix.sizeimage = GO7007_BUF_SIZE;
4563 + fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; /* ?? */
4569 + go->width = width;
4570 + go->height = height;
4571 + go->encoder_h_offset = go->board_info->sensor_h_offset;
4572 + go->encoder_v_offset = go->board_info->sensor_v_offset;
4573 + for (i = 0; i < 4; ++i)
4574 + go->modet[i].enable = 0;
4575 + for (i = 0; i < 1624; ++i)
4576 + go->modet_map[i] = 0;
4578 + if (go->board_info->sensor_flags & GO7007_SENSOR_SCALING) {
4579 + struct video_decoder_resolution res;
4581 + res.width = width;
4582 + if (height > sensor_height / 2) {
4583 + res.height = height / 2;
4584 + go->encoder_v_halve = 0;
4586 + res.height = height;
4587 + go->encoder_v_halve = 1;
4589 + if (go->i2c_adapter_online)
4590 + i2c_clients_command(&go->i2c_adapter,
4591 + DECODER_SET_RESOLUTION, &res);
4593 + if (width <= sensor_width / 4) {
4594 + go->encoder_h_halve = 1;
4595 + go->encoder_v_halve = 1;
4596 + go->encoder_subsample = 1;
4597 + } else if (width <= sensor_width / 2) {
4598 + go->encoder_h_halve = 1;
4599 + go->encoder_v_halve = 1;
4600 + go->encoder_subsample = 0;
4602 + go->encoder_h_halve = 0;
4603 + go->encoder_v_halve = 0;
4604 + go->encoder_subsample = 0;
4611 + switch (fmt->fmt.pix.pixelformat) {
4612 + case V4L2_PIX_FMT_MPEG:
4613 + if (go->format == GO7007_FORMAT_MPEG1 ||
4614 + go->format == GO7007_FORMAT_MPEG2 ||
4615 + go->format == GO7007_FORMAT_MPEG4)
4617 + go->format = GO7007_FORMAT_MPEG1;
4619 + go->aspect_ratio = GO7007_RATIO_1_1;
4620 + go->gop_size = go->sensor_framerate / 1000;
4622 + go->closed_gop = 1;
4623 + go->repeat_seqhead = 1;
4624 + go->seq_header_enable = 1;
4625 + go->gop_header_enable = 1;
4628 + /* Backwards compatibility only! */
4629 + case V4L2_PIX_FMT_MPEG4:
4630 + if (go->format == GO7007_FORMAT_MPEG4)
4632 + go->format = GO7007_FORMAT_MPEG4;
4634 + go->aspect_ratio = GO7007_RATIO_1_1;
4635 + go->gop_size = go->sensor_framerate / 1000;
4637 + go->closed_gop = 1;
4638 + go->repeat_seqhead = 1;
4639 + go->seq_header_enable = 1;
4640 + go->gop_header_enable = 1;
4643 + case V4L2_PIX_FMT_MJPEG:
4644 + go->format = GO7007_FORMAT_MJPEG;
4646 + go->aspect_ratio = GO7007_RATIO_1_1;
4649 + go->closed_gop = 0;
4650 + go->repeat_seqhead = 0;
4651 + go->seq_header_enable = 0;
4652 + go->gop_header_enable = 0;
4659 +static int clip_to_modet_map(struct go7007 *go, int region,
4660 + struct v4l2_clip *clip_list)
4662 + struct v4l2_clip clip, *clip_ptr;
4665 + /* Check if coordinates are OK and if any macroblocks are already
4666 + * used by other regions (besides 0) */
4667 + clip_ptr = clip_list;
4668 + while (clip_ptr) {
4669 + if (copy_from_user(&clip, clip_ptr, sizeof(clip)))
4671 + if (clip.c.left < 0 || (clip.c.left & 0xF) ||
4672 + clip.c.width <= 0 || (clip.c.width & 0xF))
4674 + if (clip.c.left + clip.c.width > go->width)
4676 + if (clip.c.top < 0 || (clip.c.top & 0xF) ||
4677 + clip.c.height <= 0 || (clip.c.height & 0xF))
4679 + if (clip.c.top + clip.c.height > go->height)
4681 + for (y = 0; y < clip.c.height; y += 16)
4682 + for (x = 0; x < clip.c.width; x += 16) {
4683 + mbnum = (go->width >> 4) *
4684 + ((clip.c.top + y) >> 4) +
4685 + ((clip.c.left + x) >> 4);
4686 + if (go->modet_map[mbnum] != 0 &&
4687 + go->modet_map[mbnum] != region)
4690 + clip_ptr = clip.next;
4693 + /* Clear old region macroblocks */
4694 + for (mbnum = 0; mbnum < 1624; ++mbnum)
4695 + if (go->modet_map[mbnum] == region)
4696 + go->modet_map[mbnum] = 0;
4698 + /* Claim macroblocks in this list */
4699 + clip_ptr = clip_list;
4700 + while (clip_ptr) {
4701 + if (copy_from_user(&clip, clip_ptr, sizeof(clip)))
4703 + for (y = 0; y < clip.c.height; y += 16)
4704 + for (x = 0; x < clip.c.width; x += 16) {
4705 + mbnum = (go->width >> 4) *
4706 + ((clip.c.top + y) >> 4) +
4707 + ((clip.c.left + x) >> 4);
4708 + go->modet_map[mbnum] = region;
4710 + clip_ptr = clip.next;
4715 +static int go7007_do_ioctl(struct inode *inode, struct file *file,
4716 + unsigned int cmd, void *arg)
4718 + struct go7007_file *gofh = file->private_data;
4719 + struct go7007 *go = gofh->go;
4720 + unsigned long flags;
4724 + case VIDIOC_QUERYCAP:
4726 + struct v4l2_capability *cap = arg;
4728 + memset(cap, 0, sizeof(*cap));
4729 + strcpy(cap->driver, "go7007");
4730 + strncpy(cap->card, go->name, sizeof(cap->card));
4731 + cap->version = KERNEL_VERSION(0, 9, 8);
4732 + cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
4733 + V4L2_CAP_STREAMING; /* | V4L2_CAP_AUDIO; */
4734 + if (go->board_info->flags & GO7007_BOARD_HAS_TUNER)
4735 + cap->capabilities |= V4L2_CAP_TUNER;
4738 + case VIDIOC_ENUM_FMT:
4740 + struct v4l2_fmtdesc *fmt = arg;
4741 + unsigned int index;
4745 + if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
4747 + switch (fmt->index) {
4749 + pixelformat = V4L2_PIX_FMT_MJPEG;
4750 + desc = "Motion-JPEG";
4753 + pixelformat = V4L2_PIX_FMT_MPEG;
4754 + desc = "MPEG1/MPEG2/MPEG4";
4759 + index = fmt->index;
4760 + memset(fmt, 0, sizeof(*fmt));
4761 + fmt->index = index;
4762 + fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
4763 + fmt->flags = V4L2_FMT_FLAG_COMPRESSED;
4764 + strncpy(fmt->description, desc, sizeof(fmt->description));
4765 + fmt->pixelformat = pixelformat;
4769 + case VIDIOC_TRY_FMT:
4771 + struct v4l2_format *fmt = arg;
4773 + if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
4775 + return set_capture_size(go, fmt, 1);
4777 + case VIDIOC_G_FMT:
4779 + struct v4l2_format *fmt = arg;
4781 + if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
4783 + memset(fmt, 0, sizeof(*fmt));
4784 + fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
4785 + fmt->fmt.pix.width = go->width;
4786 + fmt->fmt.pix.height = go->height;
4787 + fmt->fmt.pix.pixelformat = go->format == GO7007_FORMAT_MJPEG ?
4788 + V4L2_PIX_FMT_MJPEG : V4L2_PIX_FMT_MPEG;
4789 + fmt->fmt.pix.field = V4L2_FIELD_NONE;
4790 + fmt->fmt.pix.bytesperline = 0;
4791 + fmt->fmt.pix.sizeimage = GO7007_BUF_SIZE;
4792 + fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; /* ?? */
4795 + case VIDIOC_S_FMT:
4797 + struct v4l2_format *fmt = arg;
4799 + if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
4801 + if (go->streaming)
4803 + return set_capture_size(go, fmt, 0);
4805 + case VIDIOC_G_FBUF:
4806 + case VIDIOC_S_FBUF:
4808 + case VIDIOC_REQBUFS:
4810 + struct v4l2_requestbuffers *req = arg;
4811 + unsigned int count, i;
4813 + if (go->streaming)
4815 + if (req->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
4816 + req->memory != V4L2_MEMORY_MMAP)
4819 + down(&gofh->lock);
4821 + for (i = 0; i < gofh->buf_count; ++i)
4822 + if (gofh->bufs[i].mapped > 0)
4823 + goto unlock_and_return;
4824 + down(&go->hw_lock);
4825 + if (go->in_use > 0 && gofh->buf_count == 0) {
4827 + goto unlock_and_return;
4829 + if (gofh->buf_count > 0)
4830 + kfree(gofh->bufs);
4832 + count = req->count;
4838 + gofh->bufs = kmalloc(count *
4839 + sizeof(struct go7007_buffer),
4841 + if (gofh->bufs == NULL) {
4843 + goto unlock_and_return;
4845 + memset(gofh->bufs, 0,
4846 + count * sizeof(struct go7007_buffer));
4847 + for (i = 0; i < count; ++i) {
4848 + gofh->bufs[i].go = go;
4849 + gofh->bufs[i].index = i;
4850 + gofh->bufs[i].state = BUF_STATE_IDLE;
4851 + gofh->bufs[i].mapped = 0;
4857 + gofh->buf_count = count;
4860 + memset(req, 0, sizeof(*req));
4861 + req->count = count;
4862 + req->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
4863 + req->memory = V4L2_MEMORY_MMAP;
4866 + case VIDIOC_QUERYBUF:
4868 + struct v4l2_buffer *buf = arg;
4869 + unsigned int index;
4872 + if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
4874 + index = buf->index;
4875 + down(&gofh->lock);
4876 + if (index >= gofh->buf_count)
4877 + goto unlock_and_return;
4878 + memset(buf, 0, sizeof(*buf));
4879 + buf->index = index;
4880 + buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
4881 + switch (gofh->bufs[index].state) {
4882 + case BUF_STATE_QUEUED:
4883 + buf->flags = V4L2_BUF_FLAG_QUEUED;
4885 + case BUF_STATE_DONE:
4886 + buf->flags = V4L2_BUF_FLAG_DONE;
4891 + if (gofh->bufs[index].mapped)
4892 + buf->flags |= V4L2_BUF_FLAG_MAPPED;
4893 + buf->memory = V4L2_MEMORY_MMAP;
4894 + buf->m.offset = index * GO7007_BUF_SIZE;
4895 + buf->length = GO7007_BUF_SIZE;
4902 + struct v4l2_buffer *buf = arg;
4903 + struct go7007_buffer *gobuf;
4907 + if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
4908 + buf->memory != V4L2_MEMORY_MMAP)
4910 + down(&gofh->lock);
4911 + if (buf->index < 0 || buf->index >= gofh->buf_count)
4912 + goto unlock_and_return;
4913 + gobuf = &gofh->bufs[buf->index];
4914 + if (gobuf->mapped == 0)
4915 + goto unlock_and_return;
4917 + if (gobuf->state != BUF_STATE_IDLE)
4918 + goto unlock_and_return;
4919 + /* offset will be 0 until we really support USERPTR streaming */
4920 + gobuf->offset = gobuf->user_addr & ~PAGE_MASK;
4921 + gobuf->bytesused = 0;
4922 + gobuf->frame_offset = 0;
4923 + gobuf->modet_active = 0;
4924 + if (gobuf->offset > 0)
4925 + gobuf->page_count = GO7007_BUF_PAGES + 1;
4927 + gobuf->page_count = GO7007_BUF_PAGES;
4929 + down_read(¤t->mm->mmap_sem);
4930 + ret = get_user_pages(current, current->mm,
4931 + gobuf->user_addr & PAGE_MASK, gobuf->page_count,
4932 + 1, 1, gobuf->pages, NULL);
4933 + up_read(¤t->mm->mmap_sem);
4934 + if (ret != gobuf->page_count) {
4936 + for (i = 0; i < ret; ++i)
4937 + page_cache_release(gobuf->pages[i]);
4938 + gobuf->page_count = 0;
4939 + goto unlock_and_return;
4941 + gobuf->state = BUF_STATE_QUEUED;
4942 + spin_lock_irqsave(&go->spinlock, flags);
4943 + list_add_tail(&gobuf->stream, &go->stream);
4944 + spin_unlock_irqrestore(&go->spinlock, flags);
4948 + case VIDIOC_DQBUF:
4950 + struct v4l2_buffer *buf = arg;
4951 + struct go7007_buffer *gobuf;
4952 + u32 frame_type_flag;
4953 + DEFINE_WAIT(wait);
4955 + if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
4957 + if (buf->memory != V4L2_MEMORY_MMAP)
4959 + down(&gofh->lock);
4961 + if (list_empty(&go->stream))
4962 + goto unlock_and_return;
4963 + gobuf = list_entry(go->stream.next,
4964 + struct go7007_buffer, stream);
4966 + if (gobuf->state != BUF_STATE_DONE &&
4967 + !(file->f_flags & O_NONBLOCK)) {
4969 + prepare_to_wait(&go->frame_waitq, &wait,
4970 + TASK_INTERRUPTIBLE);
4971 + if (gobuf->state == BUF_STATE_DONE)
4973 + if (signal_pending(current)) {
4974 + retval = -ERESTARTSYS;
4979 + finish_wait(&go->frame_waitq, &wait);
4981 + if (gobuf->state != BUF_STATE_DONE)
4982 + goto unlock_and_return;
4983 + spin_lock_irqsave(&go->spinlock, flags);
4984 + deactivate_buffer(gobuf);
4985 + spin_unlock_irqrestore(&go->spinlock, flags);
4986 + frame_type_flag = get_frame_type_flag(gobuf, go->format);
4987 + gobuf->state = BUF_STATE_IDLE;
4988 + memset(buf, 0, sizeof(*buf));
4989 + buf->index = gobuf->index;
4990 + buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
4991 + buf->bytesused = gobuf->bytesused;
4992 + buf->flags = V4L2_BUF_FLAG_MAPPED | frame_type_flag;
4993 + buf->field = V4L2_FIELD_NONE;
4994 + buf->timestamp = gobuf->timestamp;
4995 + buf->sequence = gobuf->seq;
4996 + buf->memory = V4L2_MEMORY_MMAP;
4997 + buf->m.offset = gobuf->index * GO7007_BUF_SIZE;
4998 + buf->length = GO7007_BUF_SIZE;
4999 + buf->reserved = gobuf->modet_active;
5003 + case VIDIOC_STREAMON:
5005 + unsigned int *type = arg;
5007 + if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
5009 + down(&gofh->lock);
5010 + down(&go->hw_lock);
5011 + if (!go->streaming) {
5012 + go->streaming = 1;
5014 + go->active_buf = NULL;
5015 + if (go7007_start_encoder(go) < 0)
5024 + case VIDIOC_STREAMOFF:
5026 + unsigned int *type = arg;
5028 + if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
5030 + down(&gofh->lock);
5031 + go7007_streamoff(go);
5035 + case VIDIOC_QUERYCTRL:
5037 + struct v4l2_queryctrl *ctrl = arg;
5040 + if (!go->i2c_adapter_online)
5043 + memset(ctrl, 0, sizeof(*ctrl));
5045 + i2c_clients_command(&go->i2c_adapter, VIDIOC_QUERYCTRL, arg);
5046 + return ctrl->name[0] == 0 ? -EINVAL : 0;
5048 + case VIDIOC_G_CTRL:
5050 + struct v4l2_control *ctrl = arg;
5051 + struct v4l2_queryctrl query;
5053 + if (!go->i2c_adapter_online)
5055 + memset(&query, 0, sizeof(query));
5056 + query.id = ctrl->id;
5057 + i2c_clients_command(&go->i2c_adapter, VIDIOC_QUERYCTRL, &query);
5058 + if (query.name[0] == 0)
5060 + i2c_clients_command(&go->i2c_adapter, VIDIOC_G_CTRL, arg);
5063 + case VIDIOC_S_CTRL:
5065 + struct v4l2_control *ctrl = arg;
5066 + struct v4l2_queryctrl query;
5068 + if (!go->i2c_adapter_online)
5070 + memset(&query, 0, sizeof(query));
5071 + query.id = ctrl->id;
5072 + i2c_clients_command(&go->i2c_adapter, VIDIOC_QUERYCTRL, &query);
5073 + if (query.name[0] == 0)
5075 + i2c_clients_command(&go->i2c_adapter, VIDIOC_S_CTRL, arg);
5078 + case VIDIOC_G_PARM:
5080 + struct v4l2_streamparm *parm = arg;
5081 + struct v4l2_fract timeperframe = {
5082 + .numerator = 1001 * go->fps_scale,
5083 + .denominator = go->sensor_framerate,
5086 + if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
5088 + memset(parm, 0, sizeof(*parm));
5089 + parm->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
5090 + parm->parm.capture.capability |= V4L2_CAP_TIMEPERFRAME;
5091 + parm->parm.capture.timeperframe = timeperframe;
5094 + case VIDIOC_S_PARM:
5096 + struct v4l2_streamparm *parm = arg;
5097 + unsigned int n, d;
5099 + if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
5101 + if (parm->parm.capture.capturemode != 0)
5103 + n = go->sensor_framerate *
5104 + parm->parm.capture.timeperframe.numerator;
5105 + d = 1001 * parm->parm.capture.timeperframe.denominator;
5106 + if (n != 0 && d != 0 && n > d)
5107 + go->fps_scale = (n + d/2) / d;
5109 + go->fps_scale = 1;
5112 + case VIDIOC_ENUMSTD:
5114 + struct v4l2_standard *std = arg;
5116 + if ((go->board_info->flags & GO7007_BOARD_HAS_TUNER) &&
5117 + go->input == go->board_info->num_inputs - 1) {
5118 + if (!go->i2c_adapter_online)
5120 + i2c_clients_command(&go->i2c_adapter,
5121 + VIDIOC_ENUMSTD, arg);
5122 + if (!std->id) /* hack to indicate EINVAL from tuner */
5124 + } else if (go->board_info->sensor_flags & GO7007_SENSOR_TV) {
5125 + switch (std->index) {
5127 + v4l2_video_std_construct(std,
5128 + V4L2_STD_NTSC, "NTSC");
5131 + v4l2_video_std_construct(std,
5132 + V4L2_STD_PAL | V4L2_STD_SECAM,
5139 + if (std->index != 0)
5141 + memset(std, 0, sizeof(*std));
5142 + snprintf(std->name, sizeof(std->name), "%dx%d, %dfps",
5143 + go->board_info->sensor_width,
5144 + go->board_info->sensor_height,
5145 + go->board_info->sensor_framerate / 1000);
5146 + std->frameperiod.numerator = 1001;
5147 + std->frameperiod.denominator =
5148 + go->board_info->sensor_framerate;
5152 + case VIDIOC_G_STD:
5154 + v4l2_std_id *std = arg;
5156 + if ((go->board_info->flags & GO7007_BOARD_HAS_TUNER) &&
5157 + go->input == go->board_info->num_inputs - 1) {
5158 + if (!go->i2c_adapter_online)
5160 + i2c_clients_command(&go->i2c_adapter,
5161 + VIDIOC_G_STD, arg);
5162 + } else if (go->board_info->sensor_flags & GO7007_SENSOR_TV) {
5163 + if (go->standard == GO7007_STD_NTSC)
5164 + *std = V4L2_STD_NTSC;
5166 + *std = V4L2_STD_PAL | V4L2_STD_SECAM;
5171 + case VIDIOC_S_STD:
5173 + v4l2_std_id *std = arg;
5176 + if (go->streaming)
5178 + if (!(go->board_info->sensor_flags & GO7007_SENSOR_TV) &&
5183 + if ((go->board_info->flags & GO7007_BOARD_HAS_TUNER) &&
5184 + go->input == go->board_info->num_inputs - 1) {
5185 + if (!go->i2c_adapter_online)
5187 + i2c_clients_command(&go->i2c_adapter,
5188 + VIDIOC_S_STD, arg);
5189 + if (!*std) /* hack to indicate EINVAL from tuner */
5192 + if (*std & V4L2_STD_NTSC) {
5193 + go->standard = GO7007_STD_NTSC;
5194 + go->sensor_framerate = 30000;
5195 + norm = VIDEO_MODE_NTSC;
5196 + } else if (*std & V4L2_STD_PAL) {
5197 + go->standard = GO7007_STD_PAL;
5198 + go->sensor_framerate = 25025;
5199 + norm = VIDEO_MODE_PAL;
5200 + } else if (*std & V4L2_STD_SECAM) {
5201 + go->standard = GO7007_STD_PAL;
5202 + go->sensor_framerate = 25025;
5203 + norm = VIDEO_MODE_SECAM;
5206 + if (go->i2c_adapter_online)
5207 + i2c_clients_command(&go->i2c_adapter,
5208 + DECODER_SET_NORM, &norm);
5209 + set_capture_size(go, NULL, 0);
5212 + case VIDIOC_QUERYSTD:
5214 + v4l2_std_id *std = arg;
5216 + if ((go->board_info->flags & GO7007_BOARD_HAS_TUNER) &&
5217 + go->input == go->board_info->num_inputs - 1) {
5218 + if (!go->i2c_adapter_online)
5220 + i2c_clients_command(&go->i2c_adapter,
5221 + VIDIOC_QUERYSTD, arg);
5222 + } else if (go->board_info->sensor_flags & GO7007_SENSOR_TV)
5223 + *std = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM;
5228 + case VIDIOC_ENUMINPUT:
5230 + struct v4l2_input *inp = arg;
5233 + if (inp->index >= go->board_info->num_inputs)
5235 + index = inp->index;
5236 + memset(inp, 0, sizeof(*inp));
5237 + inp->index = index;
5238 + strncpy(inp->name, go->board_info->inputs[index].name,
5239 + sizeof(inp->name));
5240 + /* If this board has a tuner, it will be the last input */
5241 + if ((go->board_info->flags & GO7007_BOARD_HAS_TUNER) &&
5242 + index == go->board_info->num_inputs - 1)
5243 + inp->type = V4L2_INPUT_TYPE_TUNER;
5245 + inp->type = V4L2_INPUT_TYPE_CAMERA;
5246 + inp->audioset = 0;
5248 + if (go->board_info->sensor_flags & GO7007_SENSOR_TV)
5249 + inp->std = V4L2_STD_NTSC | V4L2_STD_PAL |
5255 + case VIDIOC_G_INPUT:
5259 + *input = go->input;
5262 + case VIDIOC_S_INPUT:
5266 + if (*input >= go->board_info->num_inputs)
5268 + if (go->streaming)
5270 + go->input = *input;
5271 + if (go->i2c_adapter_online) {
5272 + i2c_clients_command(&go->i2c_adapter, DECODER_SET_INPUT,
5273 + &go->board_info->inputs[*input].video_input);
5274 + i2c_clients_command(&go->i2c_adapter, VIDIOC_S_AUDIO,
5275 + &go->board_info->inputs[*input].audio_input);
5279 + case VIDIOC_G_TUNER:
5281 + struct v4l2_tuner *t = arg;
5283 + if (!(go->board_info->flags & GO7007_BOARD_HAS_TUNER))
5285 + if (t->index != 0)
5287 + if (!go->i2c_adapter_online)
5289 + i2c_clients_command(&go->i2c_adapter, VIDIOC_G_TUNER, arg);
5293 + case VIDIOC_S_TUNER:
5295 + struct v4l2_tuner *t = arg;
5297 + if (!(go->board_info->flags & GO7007_BOARD_HAS_TUNER))
5299 + if (t->index != 0)
5301 + if (!go->i2c_adapter_online)
5303 + switch (go->board_id) {
5304 + case GO7007_BOARDID_PX_TV402U_NA:
5305 + case GO7007_BOARDID_PX_TV402U_JP:
5306 + /* No selectable options currently */
5307 + if (t->audmode != V4L2_TUNER_MODE_STEREO)
5311 + i2c_clients_command(&go->i2c_adapter, VIDIOC_S_TUNER, arg);
5314 + case VIDIOC_G_FREQUENCY:
5316 + struct v4l2_frequency *f = arg;
5318 + if (!(go->board_info->flags & GO7007_BOARD_HAS_TUNER))
5320 + if (!go->i2c_adapter_online)
5322 + memset(f, 0, sizeof(*f));
5323 + f->type = V4L2_TUNER_ANALOG_TV;
5324 + i2c_clients_command(&go->i2c_adapter, VIDIOC_G_FREQUENCY, arg);
5327 + case VIDIOC_S_FREQUENCY:
5329 + if (!(go->board_info->flags & GO7007_BOARD_HAS_TUNER))
5331 + if (!go->i2c_adapter_online)
5333 + i2c_clients_command(&go->i2c_adapter, VIDIOC_S_FREQUENCY, arg);
5336 + case VIDIOC_CROPCAP:
5338 + struct v4l2_cropcap *cropcap = arg;
5340 + if (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
5342 + memset(cropcap, 0, sizeof(*cropcap));
5343 + cropcap->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
5344 + /* These specify the raw input of the sensor */
5345 + switch (go->standard) {
5346 + case GO7007_STD_NTSC:
5347 + cropcap->bounds.top = 0;
5348 + cropcap->bounds.left = 0;
5349 + cropcap->bounds.width = 720;
5350 + cropcap->bounds.height = 480;
5351 + cropcap->defrect.top = 0;
5352 + cropcap->defrect.left = 0;
5353 + cropcap->defrect.width = 720;
5354 + cropcap->defrect.height = 480;
5356 + case GO7007_STD_PAL:
5357 + cropcap->bounds.top = 0;
5358 + cropcap->bounds.left = 0;
5359 + cropcap->bounds.width = 720;
5360 + cropcap->bounds.height = 576;
5361 + cropcap->defrect.top = 0;
5362 + cropcap->defrect.left = 0;
5363 + cropcap->defrect.width = 720;
5364 + cropcap->defrect.height = 576;
5366 + case GO7007_STD_OTHER:
5367 + cropcap->bounds.top = 0;
5368 + cropcap->bounds.left = 0;
5369 + cropcap->bounds.width = go->board_info->sensor_width;
5370 + cropcap->bounds.height = go->board_info->sensor_height;
5371 + cropcap->defrect.top = 0;
5372 + cropcap->defrect.left = 0;
5373 + cropcap->defrect.width = go->board_info->sensor_width;
5374 + cropcap->defrect.height = go->board_info->sensor_height;
5380 + case VIDIOC_G_CROP:
5382 + struct v4l2_crop *crop = arg;
5384 + if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
5386 + memset(crop, 0, sizeof(*crop));
5387 + crop->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
5388 + /* These specify the raw input of the sensor */
5389 + switch (go->standard) {
5390 + case GO7007_STD_NTSC:
5393 + crop->c.width = 720;
5394 + crop->c.height = 480;
5396 + case GO7007_STD_PAL:
5399 + crop->c.width = 720;
5400 + crop->c.height = 576;
5402 + case GO7007_STD_OTHER:
5405 + crop->c.width = go->board_info->sensor_width;
5406 + crop->c.height = go->board_info->sensor_height;
5412 + case VIDIOC_S_CROP:
5414 + struct v4l2_crop *crop = arg;
5416 + if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
5420 + case VIDIOC_G_JPEGCOMP:
5422 + struct v4l2_jpegcompression *params = arg;
5424 + memset(params, 0, sizeof(*params));
5425 + params->quality = 50; /* ?? */
5426 + params->jpeg_markers = V4L2_JPEG_MARKER_DHT |
5427 + V4L2_JPEG_MARKER_DQT;
5431 + case VIDIOC_S_JPEGCOMP:
5433 + struct v4l2_jpegcompression *params = arg;
5435 + if (params->quality != 50 ||
5436 + params->jpeg_markers != (V4L2_JPEG_MARKER_DHT |
5437 + V4L2_JPEG_MARKER_DQT))
5441 + /* Temporary ioctls for controlling compression characteristics */
5442 + case GO7007IOC_S_BITRATE:
5444 + int *bitrate = arg;
5446 + if (go->streaming)
5448 + /* Upper bound is kind of arbitrary here */
5449 + if (*bitrate < 64000 || *bitrate > 10000000)
5451 + go->bitrate = *bitrate;
5454 + case GO7007IOC_G_BITRATE:
5456 + int *bitrate = arg;
5458 + *bitrate = go->bitrate;
5461 + case GO7007IOC_S_COMP_PARAMS:
5463 + struct go7007_comp_params *comp = arg;
5465 + if (go->format == GO7007_FORMAT_MJPEG)
5467 + if (comp->gop_size > 0)
5468 + go->gop_size = comp->gop_size;
5470 + go->gop_size = go->sensor_framerate / 1000;
5471 + if (go->gop_size != 15)
5473 + /*go->ipb = comp->max_b_frames > 0;*/ /* completely untested */
5474 + if (go->board_info->sensor_flags & GO7007_SENSOR_TV) {
5475 + switch (comp->aspect_ratio) {
5476 + case GO7007_ASPECT_RATIO_4_3_NTSC:
5477 + case GO7007_ASPECT_RATIO_4_3_PAL:
5478 + go->aspect_ratio = GO7007_RATIO_4_3;
5480 + case GO7007_ASPECT_RATIO_16_9_NTSC:
5481 + case GO7007_ASPECT_RATIO_16_9_PAL:
5482 + go->aspect_ratio = GO7007_RATIO_16_9;
5485 + go->aspect_ratio = GO7007_RATIO_1_1;
5489 + if (comp->flags & GO7007_COMP_OMIT_SEQ_HEADER) {
5491 + go->seq_header_enable = 0;
5493 + go->seq_header_enable = 1;
5495 + /* fall-through */
5497 + case GO7007IOC_G_COMP_PARAMS:
5499 + struct go7007_comp_params *comp = arg;
5501 + if (go->format == GO7007_FORMAT_MJPEG)
5503 + memset(comp, 0, sizeof(*comp));
5504 + comp->gop_size = go->gop_size;
5505 + comp->max_b_frames = go->ipb ? 2 : 0;
5506 + switch (go->aspect_ratio) {
5507 + case GO7007_RATIO_4_3:
5508 + if (go->standard == GO7007_STD_NTSC)
5509 + comp->aspect_ratio =
5510 + GO7007_ASPECT_RATIO_4_3_NTSC;
5512 + comp->aspect_ratio =
5513 + GO7007_ASPECT_RATIO_4_3_PAL;
5515 + case GO7007_RATIO_16_9:
5516 + if (go->standard == GO7007_STD_NTSC)
5517 + comp->aspect_ratio =
5518 + GO7007_ASPECT_RATIO_16_9_NTSC;
5520 + comp->aspect_ratio =
5521 + GO7007_ASPECT_RATIO_16_9_PAL;
5524 + comp->aspect_ratio = GO7007_ASPECT_RATIO_1_1;
5527 + if (go->closed_gop)
5528 + comp->flags |= GO7007_COMP_CLOSED_GOP;
5529 + if (!go->seq_header_enable)
5530 + comp->flags |= GO7007_COMP_OMIT_SEQ_HEADER;
5533 + case GO7007IOC_S_MPEG_PARAMS:
5535 + struct go7007_mpeg_params *mpeg = arg;
5537 + if (go->format != GO7007_FORMAT_MPEG1 &&
5538 + go->format != GO7007_FORMAT_MPEG2 &&
5539 + go->format != GO7007_FORMAT_MPEG4)
5542 + if (mpeg->flags & GO7007_MPEG_FORCE_DVD_MODE) {
5543 + go->format = GO7007_FORMAT_MPEG2;
5544 + go->bitrate = 9800000;
5545 + go->gop_size = 15;
5547 + go->closed_gop = 1;
5548 + go->repeat_seqhead = 0;
5549 + go->seq_header_enable = 1;
5550 + go->gop_header_enable = 1;
5553 + switch (mpeg->mpeg_video_standard) {
5554 + case GO7007_MPEG_VIDEO_MPEG1:
5555 + go->format = GO7007_FORMAT_MPEG1;
5558 + case GO7007_MPEG_VIDEO_MPEG2:
5559 + go->format = GO7007_FORMAT_MPEG2;
5560 + if (mpeg->pali >> 24 == 2)
5561 + go->pali = mpeg->pali & 0xff;
5565 + case GO7007_MPEG_VIDEO_MPEG4:
5566 + go->format = GO7007_FORMAT_MPEG4;
5567 + if (mpeg->pali >> 24 == 4)
5568 + go->pali = mpeg->pali & 0xff;
5575 + go->gop_header_enable =
5576 + mpeg->flags & GO7007_MPEG_OMIT_GOP_HEADER
5578 + if (mpeg->flags & GO7007_MPEG_REPEAT_SEQHEADER)
5579 + go->repeat_seqhead = 1;
5581 + go->repeat_seqhead = 0;
5584 + /* fall-through */
5586 + case GO7007IOC_G_MPEG_PARAMS:
5588 + struct go7007_mpeg_params *mpeg = arg;
5590 + memset(mpeg, 0, sizeof(*mpeg));
5591 + switch (go->format) {
5592 + case GO7007_FORMAT_MPEG1:
5593 + mpeg->mpeg_video_standard = GO7007_MPEG_VIDEO_MPEG1;
5596 + case GO7007_FORMAT_MPEG2:
5597 + mpeg->mpeg_video_standard = GO7007_MPEG_VIDEO_MPEG2;
5598 + mpeg->pali = GO7007_MPEG_PROFILE(2, go->pali);
5600 + case GO7007_FORMAT_MPEG4:
5601 + mpeg->mpeg_video_standard = GO7007_MPEG_VIDEO_MPEG4;
5602 + mpeg->pali = GO7007_MPEG_PROFILE(4, go->pali);
5607 + if (!go->gop_header_enable)
5608 + mpeg->flags |= GO7007_MPEG_OMIT_GOP_HEADER;
5609 + if (go->repeat_seqhead)
5610 + mpeg->flags |= GO7007_MPEG_REPEAT_SEQHEADER;
5612 + mpeg->flags |= GO7007_MPEG_FORCE_DVD_MODE;
5615 + case GO7007IOC_S_MD_PARAMS:
5617 + struct go7007_md_params *mdp = arg;
5619 + if (mdp->region > 3)
5621 + if (mdp->trigger > 0) {
5622 + go->modet[mdp->region].pixel_threshold =
5623 + mdp->pixel_threshold >> 1;
5624 + go->modet[mdp->region].motion_threshold =
5625 + mdp->motion_threshold >> 1;
5626 + go->modet[mdp->region].mb_threshold =
5627 + mdp->trigger >> 1;
5628 + go->modet[mdp->region].enable = 1;
5630 + go->modet[mdp->region].enable = 0;
5631 + /* fall-through */
5633 + case GO7007IOC_G_MD_PARAMS:
5635 + struct go7007_md_params *mdp = arg;
5636 + int region = mdp->region;
5638 + if (mdp->region > 3)
5640 + memset(mdp, 0, sizeof(struct go7007_md_params));
5641 + mdp->region = region;
5642 + if (!go->modet[region].enable)
5644 + mdp->pixel_threshold =
5645 + (go->modet[region].pixel_threshold << 1) + 1;
5646 + mdp->motion_threshold =
5647 + (go->modet[region].motion_threshold << 1) + 1;
5649 + (go->modet[region].mb_threshold << 1) + 1;
5652 + case GO7007IOC_S_MD_REGION:
5654 + struct go7007_md_region *region = arg;
5656 + if (region->region < 1 || region->region > 3)
5658 + return clip_to_modet_map(go, region->region, region->clips);
5661 + printk(KERN_DEBUG "go7007: unsupported ioctl %d\n", cmd);
5662 + return -ENOIOCTLCMD;
5671 +static int go7007_ioctl(struct inode *inode, struct file *file,
5672 + unsigned int cmd, unsigned long arg)
5674 + struct go7007_file *gofh = file->private_data;
5676 + if (gofh->go->status != STATUS_ONLINE)
5679 + return video_usercopy(inode, file, cmd, arg, go7007_do_ioctl);
5682 +static ssize_t go7007_read(struct file *file, char __user *data,
5683 + size_t count, loff_t *ppos)
5688 +static void go7007_vm_open(struct vm_area_struct *vma)
5690 + struct go7007_buffer *gobuf = vma->vm_private_data;
5695 +static void go7007_vm_close(struct vm_area_struct *vma)
5697 + struct go7007_buffer *gobuf = vma->vm_private_data;
5698 + unsigned long flags;
5700 + if (--gobuf->mapped == 0) {
5701 + spin_lock_irqsave(&gobuf->go->spinlock, flags);
5702 + deactivate_buffer(gobuf);
5703 + spin_unlock_irqrestore(&gobuf->go->spinlock, flags);
5707 +/* Copied from videobuf-dma-sg.c */
5708 +static int go7007_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
5710 + struct page *page;
5712 + page = alloc_page(GFP_USER | __GFP_DMA32);
5714 + return VM_FAULT_OOM;
5715 + clear_user_page(page_address(page), (unsigned long)vmf->virtual_address,
5721 +static struct vm_operations_struct go7007_vm_ops = {
5722 + .open = go7007_vm_open,
5723 + .close = go7007_vm_close,
5724 + .fault = go7007_vm_fault,
5727 +static int go7007_mmap(struct file *file, struct vm_area_struct *vma)
5729 + struct go7007_file *gofh = file->private_data;
5730 + unsigned int index;
5732 + if (gofh->go->status != STATUS_ONLINE)
5734 + if (!(vma->vm_flags & VM_SHARED))
5735 + return -EINVAL; /* only support VM_SHARED mapping */
5736 + if (vma->vm_end - vma->vm_start != GO7007_BUF_SIZE)
5737 + return -EINVAL; /* must map exactly one full buffer */
5738 + down(&gofh->lock);
5739 + index = vma->vm_pgoff / GO7007_BUF_PAGES;
5740 + if (index >= gofh->buf_count) {
5742 + return -EINVAL; /* trying to map beyond requested buffers */
5744 + if (index * GO7007_BUF_PAGES != vma->vm_pgoff) {
5746 + return -EINVAL; /* offset is not aligned on buffer boundary */
5748 + if (gofh->bufs[index].mapped > 0) {
5752 + gofh->bufs[index].mapped = 1;
5753 + gofh->bufs[index].user_addr = vma->vm_start;
5754 + vma->vm_ops = &go7007_vm_ops;
5755 + vma->vm_flags |= VM_DONTEXPAND;
5756 + vma->vm_flags &= ~VM_IO;
5757 + vma->vm_private_data = &gofh->bufs[index];
5762 +static unsigned int go7007_poll(struct file *file, poll_table *wait)
5764 + struct go7007_file *gofh = file->private_data;
5765 + struct go7007_buffer *gobuf;
5767 + if (list_empty(&gofh->go->stream))
5769 + gobuf = list_entry(gofh->go->stream.next, struct go7007_buffer, stream);
5770 + poll_wait(file, &gofh->go->frame_waitq, wait);
5771 + if (gobuf->state == BUF_STATE_DONE)
5772 + return POLLIN | POLLRDNORM;
5776 +static void go7007_vfl_release(struct video_device *vfd)
5778 + struct go7007 *go = video_get_drvdata(vfd);
5780 + video_device_release(vfd);
5781 + if (--go->ref_count == 0)
5785 +static struct file_operations go7007_fops = {
5786 + .owner = THIS_MODULE,
5787 + .open = go7007_open,
5788 + .release = go7007_release,
5789 + .ioctl = go7007_ioctl,
5790 + .llseek = no_llseek,
5791 + .read = go7007_read,
5792 + .mmap = go7007_mmap,
5793 + .poll = go7007_poll,
5796 +static struct video_device go7007_template = {
5798 + .fops = &go7007_fops,
5800 + .release = go7007_vfl_release,
5803 +int go7007_v4l2_init(struct go7007 *go)
5807 + go->video_dev = video_device_alloc();
5808 + if (go->video_dev == NULL)
5810 + memcpy(go->video_dev, &go7007_template, sizeof(go7007_template));
5811 + go->video_dev->parent = go->dev;
5812 + rv = video_register_device(go->video_dev, VFL_TYPE_GRABBER, -1);
5814 + video_device_release(go->video_dev);
5815 + go->video_dev = NULL;
5818 + video_set_drvdata(go->video_dev, go);
5824 +void go7007_v4l2_remove(struct go7007 *go)
5826 + unsigned long flags;
5828 + down(&go->hw_lock);
5829 + if (go->streaming) {
5830 + go->streaming = 0;
5831 + go7007_stream_stop(go);
5832 + spin_lock_irqsave(&go->spinlock, flags);
5834 + spin_unlock_irqrestore(&go->spinlock, flags);
5837 + if (go->video_dev)
5838 + video_unregister_device(go->video_dev);
5840 diff --git a/drivers/staging/go7007/go7007.h b/drivers/staging/go7007/go7007.h
5841 new file mode 100644
5842 index 0000000..7399c91
5844 +++ b/drivers/staging/go7007/go7007.h
5847 + * Copyright (C) 2005-2006 Micronas USA Inc.
5849 + * Permission is hereby granted, free of charge, to any person obtaining a
5850 + * copy of this software and the associated README documentation file (the
5851 + * "Software"), to deal in the Software without restriction, including
5852 + * without limitation the rights to use, copy, modify, merge, publish,
5853 + * distribute, sublicense, and/or sell copies of the Software, and to
5854 + * permit persons to whom the Software is furnished to do so.
5856 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
5857 + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
5858 + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
5859 + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
5860 + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
5861 + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
5862 + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
5865 +/* DEPRECATED -- use V4L2_PIX_FMT_MPEG and then call GO7007IOC_S_MPEG_PARAMS
5866 + * to select between MPEG1, MPEG2, and MPEG4 */
5867 +#define V4L2_PIX_FMT_MPEG4 v4l2_fourcc('M', 'P', 'G', '4') /* MPEG4 */
5869 +/* These will be replaced with a better interface
5870 + * soon, so don't get too attached to them */
5871 +#define GO7007IOC_S_BITRATE _IOW('V', BASE_VIDIOC_PRIVATE + 0, int)
5872 +#define GO7007IOC_G_BITRATE _IOR('V', BASE_VIDIOC_PRIVATE + 1, int)
5874 +enum go7007_aspect_ratio {
5875 + GO7007_ASPECT_RATIO_1_1 = 0,
5876 + GO7007_ASPECT_RATIO_4_3_NTSC = 1,
5877 + GO7007_ASPECT_RATIO_4_3_PAL = 2,
5878 + GO7007_ASPECT_RATIO_16_9_NTSC = 3,
5879 + GO7007_ASPECT_RATIO_16_9_PAL = 4,
5882 +/* Used to set generic compression parameters */
5883 +struct go7007_comp_params {
5885 + __u32 max_b_frames;
5886 + enum go7007_aspect_ratio aspect_ratio;
5888 + __u32 reserved[8];
5891 +#define GO7007_COMP_CLOSED_GOP 0x00000001
5892 +#define GO7007_COMP_OMIT_SEQ_HEADER 0x00000002
5894 +enum go7007_mpeg_video_standard {
5895 + GO7007_MPEG_VIDEO_MPEG1 = 0,
5896 + GO7007_MPEG_VIDEO_MPEG2 = 1,
5897 + GO7007_MPEG_VIDEO_MPEG4 = 2,
5900 +/* Used to set parameters for V4L2_PIX_FMT_MPEG format */
5901 +struct go7007_mpeg_params {
5902 + enum go7007_mpeg_video_standard mpeg_video_standard;
5905 + __u32 reserved[8];
5908 +#define GO7007_MPEG_FORCE_DVD_MODE 0x00000001
5909 +#define GO7007_MPEG_OMIT_GOP_HEADER 0x00000002
5910 +#define GO7007_MPEG_REPEAT_SEQHEADER 0x00000004
5912 +#define GO7007_MPEG_PROFILE(format, pali) (((format)<<24)|(pali))
5914 +#define GO7007_MPEG2_PROFILE_MAIN_MAIN GO7007_MPEG_PROFILE(2, 0x48)
5916 +#define GO7007_MPEG4_PROFILE_S_L0 GO7007_MPEG_PROFILE(4, 0x08)
5917 +#define GO7007_MPEG4_PROFILE_S_L1 GO7007_MPEG_PROFILE(4, 0x01)
5918 +#define GO7007_MPEG4_PROFILE_S_L2 GO7007_MPEG_PROFILE(4, 0x02)
5919 +#define GO7007_MPEG4_PROFILE_S_L3 GO7007_MPEG_PROFILE(4, 0x03)
5920 +#define GO7007_MPEG4_PROFILE_ARTS_L1 GO7007_MPEG_PROFILE(4, 0x91)
5921 +#define GO7007_MPEG4_PROFILE_ARTS_L2 GO7007_MPEG_PROFILE(4, 0x92)
5922 +#define GO7007_MPEG4_PROFILE_ARTS_L3 GO7007_MPEG_PROFILE(4, 0x93)
5923 +#define GO7007_MPEG4_PROFILE_ARTS_L4 GO7007_MPEG_PROFILE(4, 0x94)
5924 +#define GO7007_MPEG4_PROFILE_AS_L0 GO7007_MPEG_PROFILE(4, 0xf0)
5925 +#define GO7007_MPEG4_PROFILE_AS_L1 GO7007_MPEG_PROFILE(4, 0xf1)
5926 +#define GO7007_MPEG4_PROFILE_AS_L2 GO7007_MPEG_PROFILE(4, 0xf2)
5927 +#define GO7007_MPEG4_PROFILE_AS_L3 GO7007_MPEG_PROFILE(4, 0xf3)
5928 +#define GO7007_MPEG4_PROFILE_AS_L4 GO7007_MPEG_PROFILE(4, 0xf4)
5929 +#define GO7007_MPEG4_PROFILE_AS_L5 GO7007_MPEG_PROFILE(4, 0xf5)
5931 +struct go7007_md_params {
5934 + __u16 pixel_threshold;
5935 + __u16 motion_threshold;
5936 + __u32 reserved[8];
5939 +struct go7007_md_region {
5942 + struct v4l2_clip *clips;
5943 + __u32 reserved[8];
5946 +#define GO7007IOC_S_MPEG_PARAMS _IOWR('V', BASE_VIDIOC_PRIVATE + 2, \
5947 + struct go7007_mpeg_params)
5948 +#define GO7007IOC_G_MPEG_PARAMS _IOR('V', BASE_VIDIOC_PRIVATE + 3, \
5949 + struct go7007_mpeg_params)
5950 +#define GO7007IOC_S_COMP_PARAMS _IOWR('V', BASE_VIDIOC_PRIVATE + 4, \
5951 + struct go7007_comp_params)
5952 +#define GO7007IOC_G_COMP_PARAMS _IOR('V', BASE_VIDIOC_PRIVATE + 5, \
5953 + struct go7007_comp_params)
5954 +#define GO7007IOC_S_MD_PARAMS _IOWR('V', BASE_VIDIOC_PRIVATE + 6, \
5955 + struct go7007_md_params)
5956 +#define GO7007IOC_G_MD_PARAMS _IOR('V', BASE_VIDIOC_PRIVATE + 7, \
5957 + struct go7007_md_params)
5958 +#define GO7007IOC_S_MD_REGION _IOW('V', BASE_VIDIOC_PRIVATE + 8, \
5959 + struct go7007_md_region)
5960 diff --git a/drivers/staging/go7007/saa7134-go7007.c b/drivers/staging/go7007/saa7134-go7007.c
5961 new file mode 100644
5962 index 0000000..c4a6d8e
5964 +++ b/drivers/staging/go7007/saa7134-go7007.c
5967 + * Copyright (C) 2005-2006 Micronas USA Inc.
5969 + * This program is free software; you can redistribute it and/or modify
5970 + * it under the terms of the GNU General Public License (Version 2) as
5971 + * published by the Free Software Foundation.
5973 + * This program is distributed in the hope that it will be useful,
5974 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
5975 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5976 + * GNU General Public License for more details.
5978 + * You should have received a copy of the GNU General Public License
5979 + * along with this program; if not, write to the Free Software Foundation,
5980 + * Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
5983 +#include <linux/module.h>
5984 +#include <linux/kernel.h>
5985 +#include <linux/init.h>
5986 +#include <linux/spinlock.h>
5987 +#include <linux/wait.h>
5988 +#include <linux/list.h>
5989 +#include <linux/slab.h>
5990 +#include <linux/time.h>
5991 +#include <linux/mm.h>
5992 +#include <linux/usb.h>
5993 +#include <linux/i2c.h>
5994 +#include <asm/byteorder.h>
5995 +#include <media/audiochip.h>
5997 +#include "saa7134-reg.h"
5998 +#include "saa7134.h"
5999 +#include "go7007-priv.h"
6001 +#define GO7007_HPI_DEBUG
6004 + HPI_ADDR_VIDEO_BUFFER = 0xe4,
6005 + HPI_ADDR_INIT_BUFFER = 0xea,
6006 + HPI_ADDR_INTR_RET_VALUE = 0xee,
6007 + HPI_ADDR_INTR_RET_DATA = 0xec,
6008 + HPI_ADDR_INTR_STATUS = 0xf4,
6009 + HPI_ADDR_INTR_WR_PARAM = 0xf6,
6010 + HPI_ADDR_INTR_WR_INDEX = 0xf8,
6013 +enum gpio_command {
6014 + GPIO_COMMAND_RESET = 0x00, /* 000b */
6015 + GPIO_COMMAND_REQ1 = 0x04, /* 001b */
6016 + GPIO_COMMAND_WRITE = 0x20, /* 010b */
6017 + GPIO_COMMAND_REQ2 = 0x24, /* 011b */
6018 + GPIO_COMMAND_READ = 0x80, /* 100b */
6019 + GPIO_COMMAND_VIDEO = 0x84, /* 101b */
6020 + GPIO_COMMAND_IDLE = 0xA0, /* 110b */
6021 + GPIO_COMMAND_ADDR = 0xA4, /* 111b */
6024 +struct saa7134_go7007 {
6025 + struct saa7134_dev *dev;
6028 + dma_addr_t top_dma;
6029 + dma_addr_t bottom_dma;
6032 +static struct go7007_board_info board_voyager = {
6033 + .firmware = "go7007tv.bin",
6035 + .sensor_flags = GO7007_SENSOR_656 |
6036 + GO7007_SENSOR_VALID_ENABLE |
6037 + GO7007_SENSOR_TV |
6038 + GO7007_SENSOR_VBI,
6039 + .audio_flags = GO7007_AUDIO_I2S_MODE_1 |
6040 + GO7007_AUDIO_WORD_16,
6041 + .audio_rate = 48000,
6042 + .audio_bclk_div = 8,
6043 + .audio_main_div = 2,
6044 + .hpi_buffer_cap = 7,
6048 + .name = "SAA7134",
6053 +/********************* Driver for GPIO HPI interface *********************/
6055 +static int gpio_write(struct saa7134_dev *dev, u8 addr, u16 data)
6057 + saa_writeb(SAA7134_GPIO_GPMODE0, 0xff);
6059 + /* Write HPI address */
6060 + saa_writeb(SAA7134_GPIO_GPSTATUS0, addr);
6061 + saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_ADDR);
6062 + saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_IDLE);
6064 + /* Write low byte */
6065 + saa_writeb(SAA7134_GPIO_GPSTATUS0, data & 0xff);
6066 + saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_WRITE);
6067 + saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_IDLE);
6069 + /* Write high byte */
6070 + saa_writeb(SAA7134_GPIO_GPSTATUS0, data >> 8);
6071 + saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_WRITE);
6072 + saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_IDLE);
6077 +static int gpio_read(struct saa7134_dev *dev, u8 addr, u16 *data)
6079 + saa_writeb(SAA7134_GPIO_GPMODE0, 0xff);
6081 + /* Write HPI address */
6082 + saa_writeb(SAA7134_GPIO_GPSTATUS0, addr);
6083 + saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_ADDR);
6084 + saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_IDLE);
6086 + saa_writeb(SAA7134_GPIO_GPMODE0, 0x00);
6088 + /* Read low byte */
6089 + saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_READ);
6090 + saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
6091 + saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
6092 + *data = saa_readb(SAA7134_GPIO_GPSTATUS0);
6093 + saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_IDLE);
6095 + /* Read high byte */
6096 + saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_READ);
6097 + saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
6098 + saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
6099 + *data |= saa_readb(SAA7134_GPIO_GPSTATUS0) << 8;
6100 + saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_IDLE);
6105 +static int saa7134_go7007_interface_reset(struct go7007 *go)
6107 + struct saa7134_go7007 *saa = go->hpi_context;
6108 + struct saa7134_dev *dev = saa->dev;
6110 + u16 intr_val, intr_data;
6113 + saa_clearb(SAA7134_TS_PARALLEL, 0x80); /* Disable TS interface */
6114 + saa_writeb(SAA7134_GPIO_GPMODE2, 0xa4);
6115 + saa_writeb(SAA7134_GPIO_GPMODE0, 0xff);
6117 + saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_REQ1);
6118 + saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_RESET);
6120 + saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_REQ1);
6121 + saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_REQ2);
6124 + saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
6125 + saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
6127 + status = saa_readb(SAA7134_GPIO_GPSTATUS2);
6128 + /*printk(KERN_DEBUG "status is %s\n", status & 0x40 ? "OK" : "not OK"); */
6130 + /* enter command mode...(?) */
6131 + saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_REQ1);
6132 + saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_REQ2);
6135 + saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
6136 + saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
6137 + status = saa_readb(SAA7134_GPIO_GPSTATUS2);
6138 + /*printk(KERN_INFO "gpio is %08x\n", saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2)); */
6139 + } while (--count > 0);
6141 + /* Wait for an interrupt to indicate successful hardware reset */
6142 + if (go7007_read_interrupt(go, &intr_val, &intr_data) < 0 ||
6143 + (intr_val & ~0x1) != 0x55aa) {
6145 + "saa7134-go7007: unable to reset the GO7007\n");
6151 +static int saa7134_go7007_write_interrupt(struct go7007 *go, int addr, int data)
6153 + struct saa7134_go7007 *saa = go->hpi_context;
6154 + struct saa7134_dev *dev = saa->dev;
6158 +#ifdef GO7007_HPI_DEBUG
6160 + "saa7134-go7007: WriteInterrupt: %04x %04x\n", addr, data);
6163 + for (i = 0; i < 100; ++i) {
6164 + gpio_read(dev, HPI_ADDR_INTR_STATUS, &status_reg);
6165 + if (!(status_reg & 0x0010))
6171 + "saa7134-go7007: device is hung, status reg = 0x%04x\n",
6175 + gpio_write(dev, HPI_ADDR_INTR_WR_PARAM, data);
6176 + gpio_write(dev, HPI_ADDR_INTR_WR_INDEX, addr);
6181 +static int saa7134_go7007_read_interrupt(struct go7007 *go)
6183 + struct saa7134_go7007 *saa = go->hpi_context;
6184 + struct saa7134_dev *dev = saa->dev;
6186 + /* XXX we need to wait if there is no interrupt available */
6187 + go->interrupt_available = 1;
6188 + gpio_read(dev, HPI_ADDR_INTR_RET_VALUE, &go->interrupt_value);
6189 + gpio_read(dev, HPI_ADDR_INTR_RET_DATA, &go->interrupt_data);
6190 +#ifdef GO7007_HPI_DEBUG
6191 + printk(KERN_DEBUG "saa7134-go7007: ReadInterrupt: %04x %04x\n",
6192 + go->interrupt_value, go->interrupt_data);
6197 +static void saa7134_go7007_irq_ts_done(struct saa7134_dev *dev,
6198 + unsigned long status)
6200 + struct go7007 *go = video_get_drvdata(dev->empress_dev);
6201 + struct saa7134_go7007 *saa = go->hpi_context;
6203 + if (!go->streaming)
6205 + if (0 != (status & 0x000f0000))
6206 + printk(KERN_DEBUG "saa7134-go7007: irq: lost %ld\n",
6207 + (status >> 16) & 0x0f);
6208 + if (status & 0x100000) {
6209 + dma_sync_single(&dev->pci->dev,
6210 + saa->bottom_dma, PAGE_SIZE, DMA_FROM_DEVICE);
6211 + go7007_parse_video_stream(go, saa->bottom, PAGE_SIZE);
6212 + saa_writel(SAA7134_RS_BA2(5), cpu_to_le32(saa->bottom_dma));
6214 + dma_sync_single(&dev->pci->dev,
6215 + saa->top_dma, PAGE_SIZE, DMA_FROM_DEVICE);
6216 + go7007_parse_video_stream(go, saa->top, PAGE_SIZE);
6217 + saa_writel(SAA7134_RS_BA1(5), cpu_to_le32(saa->top_dma));
6221 +static int saa7134_go7007_stream_start(struct go7007 *go)
6223 + struct saa7134_go7007 *saa = go->hpi_context;
6224 + struct saa7134_dev *dev = saa->dev;
6226 + saa->top_dma = dma_map_page(&dev->pci->dev, virt_to_page(saa->top),
6227 + 0, PAGE_SIZE, DMA_FROM_DEVICE);
6228 + if (!saa->top_dma)
6230 + saa->bottom_dma = dma_map_page(&dev->pci->dev,
6231 + virt_to_page(saa->bottom),
6232 + 0, PAGE_SIZE, DMA_FROM_DEVICE);
6233 + if (!saa->bottom_dma) {
6234 + dma_unmap_page(&dev->pci->dev, saa->top_dma, PAGE_SIZE,
6239 + saa_writel(SAA7134_VIDEO_PORT_CTRL0 >> 2, 0xA300B000);
6240 + saa_writel(SAA7134_VIDEO_PORT_CTRL4 >> 2, 0x40000200);
6242 + /* Set HPI interface for video */
6243 + saa_writeb(SAA7134_GPIO_GPMODE0, 0xff);
6244 + saa_writeb(SAA7134_GPIO_GPSTATUS0, HPI_ADDR_VIDEO_BUFFER);
6245 + saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_ADDR);
6246 + saa_writeb(SAA7134_GPIO_GPMODE0, 0x00);
6248 + /* Enable TS interface */
6249 + saa_writeb(SAA7134_TS_PARALLEL, 0xe6);
6251 + /* Reset TS interface */
6252 + saa_setb(SAA7134_TS_SERIAL1, 0x01);
6253 + saa_clearb(SAA7134_TS_SERIAL1, 0x01);
6255 + /* Set up transfer block size */
6256 + saa_writeb(SAA7134_TS_PARALLEL_SERIAL, 128 - 1);
6257 + saa_writeb(SAA7134_TS_DMA0, (PAGE_SIZE >> 7) - 1);
6258 + saa_writeb(SAA7134_TS_DMA1, 0);
6259 + saa_writeb(SAA7134_TS_DMA2, 0);
6261 + /* Enable video streaming mode */
6262 + saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_VIDEO);
6264 + saa_writel(SAA7134_RS_BA1(5), cpu_to_le32(saa->top_dma));
6265 + saa_writel(SAA7134_RS_BA2(5), cpu_to_le32(saa->bottom_dma));
6266 + saa_writel(SAA7134_RS_PITCH(5), 128);
6267 + saa_writel(SAA7134_RS_CONTROL(5), SAA7134_RS_CONTROL_BURST_MAX);
6269 + /* Enable TS FIFO */
6270 + saa_setl(SAA7134_MAIN_CTRL, SAA7134_MAIN_CTRL_TE5);
6272 + /* Enable DMA IRQ */
6273 + saa_setl(SAA7134_IRQ1,
6274 + SAA7134_IRQ1_INTE_RA2_1 | SAA7134_IRQ1_INTE_RA2_0);
6279 +static int saa7134_go7007_stream_stop(struct go7007 *go)
6281 + struct saa7134_go7007 *saa = go->hpi_context;
6282 + struct saa7134_dev *dev = saa->dev;
6284 + /* Shut down TS FIFO */
6285 + saa_clearl(SAA7134_MAIN_CTRL, SAA7134_MAIN_CTRL_TE5);
6287 + /* Disable DMA IRQ */
6288 + saa_clearl(SAA7134_IRQ1,
6289 + SAA7134_IRQ1_INTE_RA2_1 | SAA7134_IRQ1_INTE_RA2_0);
6291 + /* Disable TS interface */
6292 + saa_clearb(SAA7134_TS_PARALLEL, 0x80);
6294 + dma_unmap_page(&dev->pci->dev, saa->top_dma, PAGE_SIZE,
6296 + dma_unmap_page(&dev->pci->dev, saa->bottom_dma, PAGE_SIZE,
6302 +static int saa7134_go7007_send_firmware(struct go7007 *go, u8 *data, int len)
6304 + struct saa7134_go7007 *saa = go->hpi_context;
6305 + struct saa7134_dev *dev = saa->dev;
6309 +#ifdef GO7007_HPI_DEBUG
6310 + printk(KERN_DEBUG "saa7134-go7007: DownloadBuffer "
6311 + "sending %d bytes\n", len);
6315 + i = len > 64 ? 64 : len;
6316 + saa_writeb(SAA7134_GPIO_GPMODE0, 0xff);
6317 + saa_writeb(SAA7134_GPIO_GPSTATUS0, HPI_ADDR_INIT_BUFFER);
6318 + saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_ADDR);
6319 + saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_IDLE);
6321 + saa_writeb(SAA7134_GPIO_GPSTATUS0, *data);
6322 + saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_WRITE);
6323 + saa_writeb(SAA7134_GPIO_GPSTATUS2, GPIO_COMMAND_IDLE);
6327 + for (i = 0; i < 100; ++i) {
6328 + gpio_read(dev, HPI_ADDR_INTR_STATUS, &status_reg);
6329 + if (!(status_reg & 0x0002))
6333 + printk(KERN_ERR "saa7134-go7007: device is hung, "
6334 + "status reg = 0x%04x\n", status_reg);
6341 +static struct go7007_hpi_ops saa7134_go7007_hpi_ops = {
6342 + .interface_reset = saa7134_go7007_interface_reset,
6343 + .write_interrupt = saa7134_go7007_write_interrupt,
6344 + .read_interrupt = saa7134_go7007_read_interrupt,
6345 + .stream_start = saa7134_go7007_stream_start,
6346 + .stream_stop = saa7134_go7007_stream_stop,
6347 + .send_firmware = saa7134_go7007_send_firmware,
6350 +/********************* Add/remove functions *********************/
6352 +static int saa7134_go7007_init(struct saa7134_dev *dev)
6354 + struct go7007 *go;
6355 + struct saa7134_go7007 *saa;
6357 + printk(KERN_DEBUG "saa7134-go7007: probing new SAA713X board\n");
6359 + saa = kmalloc(sizeof(struct saa7134_go7007), GFP_KERNEL);
6362 + memset(saa, 0, sizeof(struct saa7134_go7007));
6364 + /* Allocate a couple pages for receiving the compressed stream */
6365 + saa->top = (u8 *)get_zeroed_page(GFP_KERNEL);
6368 + saa->bottom = (u8 *)get_zeroed_page(GFP_KERNEL);
6372 + go = go7007_alloc(&board_voyager, &dev->pci->dev);
6375 + go->board_id = GO7007_BOARDID_PCI_VOYAGER;
6376 + strncpy(go->name, saa7134_boards[dev->board].name, sizeof(go->name));
6377 + go->hpi_ops = &saa7134_go7007_hpi_ops;
6378 + go->hpi_context = saa;
6381 + /* Boot the GO7007 */
6382 + if (go7007_boot_encoder(go, go->board_info->flags &
6383 + GO7007_BOARD_USE_ONBOARD_I2C) < 0)
6386 + /* Do any final GO7007 initialization, then register the
6387 + * V4L2 and ALSA interfaces */
6388 + if (go7007_register_encoder(go) < 0)
6390 + dev->empress_dev = go->video_dev;
6391 + video_set_drvdata(dev->empress_dev, go);
6393 + go->status = STATUS_ONLINE;
6397 + go->status = STATUS_SHUTDOWN;
6402 + free_page((unsigned long)saa->top);
6404 + free_page((unsigned long)saa->bottom);
6409 +static int saa7134_go7007_fini(struct saa7134_dev *dev)
6411 + struct go7007 *go;
6412 + struct saa7134_go7007 *saa;
6414 + if (NULL == dev->empress_dev)
6417 + go = video_get_drvdata(dev->empress_dev);
6418 + saa = go->hpi_context;
6419 + go->status = STATUS_SHUTDOWN;
6420 + free_page((unsigned long)saa->top);
6421 + free_page((unsigned long)saa->bottom);
6423 + go7007_remove(go);
6424 + dev->empress_dev = NULL;
6429 +static struct saa7134_mpeg_ops saa7134_go7007_ops = {
6430 + .type = SAA7134_MPEG_GO7007,
6431 + .init = saa7134_go7007_init,
6432 + .fini = saa7134_go7007_fini,
6433 + .irq_ts_done = saa7134_go7007_irq_ts_done,
6436 +static int __init saa7134_go7007_mod_init(void)
6438 + return saa7134_ts_register(&saa7134_go7007_ops);
6441 +static void __exit saa7134_go7007_mod_cleanup(void)
6443 + saa7134_ts_unregister(&saa7134_go7007_ops);
6446 +module_init(saa7134_go7007_mod_init);
6447 +module_exit(saa7134_go7007_mod_cleanup);
6449 +MODULE_LICENSE("GPL v2");
6450 diff --git a/drivers/staging/go7007/snd-go7007.c b/drivers/staging/go7007/snd-go7007.c
6451 new file mode 100644
6452 index 0000000..f5cac08
6454 +++ b/drivers/staging/go7007/snd-go7007.c
6457 + * Copyright (C) 2005-2006 Micronas USA Inc.
6459 + * This program is free software; you can redistribute it and/or modify
6460 + * it under the terms of the GNU General Public License (Version 2) as
6461 + * published by the Free Software Foundation.
6463 + * This program is distributed in the hope that it will be useful,
6464 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6465 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6466 + * GNU General Public License for more details.
6468 + * You should have received a copy of the GNU General Public License
6469 + * along with this program; if not, write to the Free Software Foundation,
6470 + * Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
6473 +#include <linux/kernel.h>
6474 +#include <linux/module.h>
6475 +#include <linux/version.h>
6476 +#include <linux/moduleparam.h>
6477 +#include <linux/init.h>
6478 +#include <linux/spinlock.h>
6479 +#include <linux/delay.h>
6480 +#include <linux/sched.h>
6481 +#include <linux/vmalloc.h>
6482 +#include <linux/time.h>
6483 +#include <linux/mm.h>
6484 +#include <linux/i2c.h>
6485 +#include <linux/semaphore.h>
6486 +#include <linux/uaccess.h>
6487 +#include <asm/system.h>
6488 +#include <sound/core.h>
6489 +#include <sound/pcm.h>
6490 +#include <sound/initval.h>
6492 +#include "go7007-priv.h"
6494 +static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
6495 +static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
6496 +static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
6498 +module_param_array(index, int, NULL, 0444);
6499 +module_param_array(id, charp, NULL, 0444);
6500 +module_param_array(enable, bool, NULL, 0444);
6501 +MODULE_PARM_DESC(index, "Index value for the go7007 audio driver");
6502 +MODULE_PARM_DESC(index, "ID string for the go7007 audio driver");
6503 +MODULE_PARM_DESC(index, "Enable for the go7007 audio driver");
6505 +struct go7007_snd {
6506 + struct snd_card *card;
6507 + struct snd_pcm *pcm;
6508 + struct snd_pcm_substream *substream;
6516 +static struct snd_pcm_hardware go7007_snd_capture_hw = {
6517 + .info = (SNDRV_PCM_INFO_MMAP |
6518 + SNDRV_PCM_INFO_INTERLEAVED |
6519 + SNDRV_PCM_INFO_BLOCK_TRANSFER |
6520 + SNDRV_PCM_INFO_MMAP_VALID),
6521 + .formats = SNDRV_PCM_FMTBIT_S16_LE,
6522 + .rates = SNDRV_PCM_RATE_48000,
6523 + .rate_min = 48000,
6524 + .rate_max = 48000,
6525 + .channels_min = 2,
6526 + .channels_max = 2,
6527 + .buffer_bytes_max = (128*1024),
6528 + .period_bytes_min = 4096,
6529 + .period_bytes_max = (128*1024),
6531 + .periods_max = 32,
6534 +static void parse_audio_stream_data(struct go7007 *go, u8 *buf, int length)
6536 + struct go7007_snd *gosnd = go->snd_context;
6537 + struct snd_pcm_runtime *runtime = gosnd->substream->runtime;
6538 + int frames = bytes_to_frames(runtime, length);
6540 + spin_lock(&gosnd->lock);
6541 + gosnd->hw_ptr += frames;
6542 + if (gosnd->hw_ptr >= runtime->buffer_size)
6543 + gosnd->hw_ptr -= runtime->buffer_size;
6544 + gosnd->avail += frames;
6545 + spin_unlock(&gosnd->lock);
6546 + if (gosnd->w_idx + length > runtime->dma_bytes) {
6547 + int cpy = runtime->dma_bytes - gosnd->w_idx;
6549 + memcpy(runtime->dma_area + gosnd->w_idx, buf, cpy);
6554 + memcpy(runtime->dma_area + gosnd->w_idx, buf, length);
6555 + gosnd->w_idx += length;
6556 + spin_lock(&gosnd->lock);
6557 + if (gosnd->avail < runtime->period_size) {
6558 + spin_unlock(&gosnd->lock);
6561 + gosnd->avail -= runtime->period_size;
6562 + spin_unlock(&gosnd->lock);
6563 + if (gosnd->capturing)
6564 + snd_pcm_period_elapsed(gosnd->substream);
6567 +static int go7007_snd_hw_params(struct snd_pcm_substream *substream,
6568 + struct snd_pcm_hw_params *hw_params)
6570 + struct go7007 *go = snd_pcm_substream_chip(substream);
6571 + unsigned int bytes;
6573 + bytes = params_buffer_bytes(hw_params);
6574 + if (substream->runtime->dma_bytes > 0)
6575 + vfree(substream->runtime->dma_area);
6576 + substream->runtime->dma_bytes = 0;
6577 + substream->runtime->dma_area = vmalloc(bytes);
6578 + if (substream->runtime->dma_area == NULL)
6580 + substream->runtime->dma_bytes = bytes;
6581 + go->audio_deliver = parse_audio_stream_data;
6585 +static int go7007_snd_hw_free(struct snd_pcm_substream *substream)
6587 + struct go7007 *go = snd_pcm_substream_chip(substream);
6589 + go->audio_deliver = NULL;
6590 + if (substream->runtime->dma_bytes > 0)
6591 + vfree(substream->runtime->dma_area);
6592 + substream->runtime->dma_bytes = 0;
6596 +static int go7007_snd_capture_open(struct snd_pcm_substream *substream)
6598 + struct go7007 *go = snd_pcm_substream_chip(substream);
6599 + struct go7007_snd *gosnd = go->snd_context;
6600 + unsigned long flags;
6603 + spin_lock_irqsave(&gosnd->lock, flags);
6604 + if (gosnd->substream == NULL) {
6605 + gosnd->substream = substream;
6606 + substream->runtime->hw = go7007_snd_capture_hw;
6610 + spin_unlock_irqrestore(&gosnd->lock, flags);
6614 +static int go7007_snd_capture_close(struct snd_pcm_substream *substream)
6616 + struct go7007 *go = snd_pcm_substream_chip(substream);
6617 + struct go7007_snd *gosnd = go->snd_context;
6619 + gosnd->substream = NULL;
6623 +static int go7007_snd_pcm_prepare(struct snd_pcm_substream *substream)
6628 +static int go7007_snd_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
6630 + struct go7007 *go = snd_pcm_substream_chip(substream);
6631 + struct go7007_snd *gosnd = go->snd_context;
6634 + case SNDRV_PCM_TRIGGER_START:
6635 + /* Just set a flag to indicate we should signal ALSA when
6636 + * sound comes in */
6637 + gosnd->capturing = 1;
6639 + case SNDRV_PCM_TRIGGER_STOP:
6640 + gosnd->hw_ptr = gosnd->w_idx = gosnd->avail = 0;
6641 + gosnd->capturing = 0;
6648 +static snd_pcm_uframes_t go7007_snd_pcm_pointer(struct snd_pcm_substream *substream)
6650 + struct go7007 *go = snd_pcm_substream_chip(substream);
6651 + struct go7007_snd *gosnd = go->snd_context;
6653 + return gosnd->hw_ptr;
6656 +static struct page *go7007_snd_pcm_page(struct snd_pcm_substream *substream,
6657 + unsigned long offset)
6659 + return vmalloc_to_page(substream->runtime->dma_area + offset);
6662 +static struct snd_pcm_ops go7007_snd_capture_ops = {
6663 + .open = go7007_snd_capture_open,
6664 + .close = go7007_snd_capture_close,
6665 + .ioctl = snd_pcm_lib_ioctl,
6666 + .hw_params = go7007_snd_hw_params,
6667 + .hw_free = go7007_snd_hw_free,
6668 + .prepare = go7007_snd_pcm_prepare,
6669 + .trigger = go7007_snd_pcm_trigger,
6670 + .pointer = go7007_snd_pcm_pointer,
6671 + .page = go7007_snd_pcm_page,
6674 +static int go7007_snd_free(struct snd_device *device)
6676 + struct go7007 *go = device->device_data;
6678 + kfree(go->snd_context);
6679 + go->snd_context = NULL;
6680 + if (--go->ref_count == 0)
6685 +static struct snd_device_ops go7007_snd_device_ops = {
6686 + .dev_free = go7007_snd_free,
6689 +int go7007_snd_init(struct go7007 *go)
6692 + struct go7007_snd *gosnd;
6695 + if (dev >= SNDRV_CARDS)
6697 + if (!enable[dev]) {
6701 + gosnd = kmalloc(sizeof(struct go7007_snd), GFP_KERNEL);
6702 + if (gosnd == NULL)
6704 + spin_lock_init(&gosnd->lock);
6705 + gosnd->hw_ptr = gosnd->w_idx = gosnd->avail = 0;
6706 + gosnd->capturing = 0;
6707 + gosnd->card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
6708 + if (gosnd->card == NULL) {
6712 + ret = snd_device_new(gosnd->card, SNDRV_DEV_LOWLEVEL, go,
6713 + &go7007_snd_device_ops);
6718 + snd_card_set_dev(gosnd->card, go->dev);
6719 + ret = snd_pcm_new(gosnd->card, "go7007", 0, 0, 1, &gosnd->pcm);
6721 + snd_card_free(gosnd->card);
6725 + strncpy(gosnd->card->driver, "go7007", sizeof(gosnd->card->driver));
6726 + strncpy(gosnd->card->shortname, go->name, sizeof(gosnd->card->driver));
6727 + strncpy(gosnd->card->longname, gosnd->card->shortname,
6728 + sizeof(gosnd->card->longname));
6730 + gosnd->pcm->private_data = go;
6731 + snd_pcm_set_ops(gosnd->pcm, SNDRV_PCM_STREAM_CAPTURE,
6732 + &go7007_snd_capture_ops);
6734 + ret = snd_card_register(gosnd->card);
6736 + snd_card_free(gosnd->card);
6741 + gosnd->substream = NULL;
6742 + go->snd_context = gosnd;
6748 +EXPORT_SYMBOL(go7007_snd_init);
6750 +int go7007_snd_remove(struct go7007 *go)
6752 + struct go7007_snd *gosnd = go->snd_context;
6754 + snd_card_disconnect(gosnd->card);
6755 + snd_card_free_when_closed(gosnd->card);
6758 +EXPORT_SYMBOL(go7007_snd_remove);
6760 +MODULE_LICENSE("GPL v2");
6761 diff --git a/drivers/staging/go7007/wis-i2c.h b/drivers/staging/go7007/wis-i2c.h
6762 new file mode 100644
6763 index 0000000..993f658
6765 +++ b/drivers/staging/go7007/wis-i2c.h
6768 + * Copyright (C) 2005-2006 Micronas USA Inc.
6770 + * This program is free software; you can redistribute it and/or modify
6771 + * it under the terms of the GNU General Public License (Version 2) as
6772 + * published by the Free Software Foundation.
6774 + * This program is distributed in the hope that it will be useful,
6775 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6776 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6777 + * GNU General Public License for more details.
6779 + * You should have received a copy of the GNU General Public License
6780 + * along with this program; if not, write to the Free Software Foundation,
6781 + * Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
6784 +/* Temporary I2C IDs -- these need to be replaced with real registered IDs */
6785 +#define I2C_DRIVERID_WIS_SAA7115 0xf0f0
6786 +#define I2C_DRIVERID_WIS_UDA1342 0xf0f1
6787 +#define I2C_DRIVERID_WIS_SONY_TUNER 0xf0f2
6788 +#define I2C_DRIVERID_WIS_TW9903 0xf0f3
6789 +#define I2C_DRIVERID_WIS_SAA7113 0xf0f4
6790 +#define I2C_DRIVERID_WIS_OV7640 0xf0f5
6791 +#define I2C_DRIVERID_WIS_TW2804 0xf0f6
6792 +#define I2C_ALGO_GO7007 0xf00000
6793 +#define I2C_ALGO_GO7007_USB 0xf10000
6795 +/* Flag to indicate that the client needs to be accessed with SCCB semantics */
6796 +/* We re-use the I2C_M_TEN value so the flag passes through the masks in the
6797 + * core I2C code. Major kludge, but the I2C layer ain't exactly flexible. */
6798 +#define I2C_CLIENT_SCCB 0x10
6800 +typedef int (*found_proc) (struct i2c_adapter *, int, int);
6801 +int wis_i2c_add_driver(unsigned int id, found_proc found_proc);
6802 +void wis_i2c_del_driver(found_proc found_proc);
6804 +int wis_i2c_probe_device(struct i2c_adapter *adapter,
6805 + unsigned int id, int addr);
6807 +/* Definitions for new video decoder commands */
6809 +struct video_decoder_resolution {
6810 + unsigned int width;
6811 + unsigned int height;
6814 +#define DECODER_SET_RESOLUTION _IOW('d', 200, struct video_decoder_resolution)
6815 +#define DECODER_SET_CHANNEL _IOW('d', 201, int)
6817 +/* Sony tuner types */
6819 +#define TUNER_SONY_BTF_PG472Z 200
6820 +#define TUNER_SONY_BTF_PK467Z 201
6821 +#define TUNER_SONY_BTF_PB463Z 202
6822 diff --git a/drivers/staging/go7007/wis-ov7640.c b/drivers/staging/go7007/wis-ov7640.c
6823 new file mode 100644
6824 index 0000000..815615a
6826 +++ b/drivers/staging/go7007/wis-ov7640.c
6829 + * Copyright (C) 2005-2006 Micronas USA Inc.
6831 + * This program is free software; you can redistribute it and/or modify
6832 + * it under the terms of the GNU General Public License (Version 2) as
6833 + * published by the Free Software Foundation.
6835 + * This program is distributed in the hope that it will be useful,
6836 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6837 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6838 + * GNU General Public License for more details.
6840 + * You should have received a copy of the GNU General Public License
6841 + * along with this program; if not, write to the Free Software Foundation,
6842 + * Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
6845 +#include <linux/module.h>
6846 +#include <linux/init.h>
6847 +#include <linux/version.h>
6848 +#include <linux/i2c.h>
6849 +#include <linux/videodev.h>
6850 +#include <linux/video_decoder.h>
6852 +#include "wis-i2c.h"
6854 +struct wis_ov7640 {
6861 +static u8 initial_registers[] =
6869 + 0xFF, 0xFF, /* Terminator (reg 0xFF is unused) */
6872 +static int write_regs(struct i2c_client *client, u8 *regs)
6876 + for (i = 0; regs[i] != 0xFF; i += 2)
6877 + if (i2c_smbus_write_byte_data(client, regs[i], regs[i + 1]) < 0)
6882 +static struct i2c_driver wis_ov7640_driver;
6884 +static struct i2c_client wis_ov7640_client_templ = {
6885 + .name = "OV7640 (WIS)",
6886 + .driver = &wis_ov7640_driver,
6889 +static int wis_ov7640_detect(struct i2c_adapter *adapter, int addr, int kind)
6891 + struct i2c_client *client;
6893 + if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
6896 + client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
6897 + if (client == NULL)
6899 + memcpy(client, &wis_ov7640_client_templ,
6900 + sizeof(wis_ov7640_client_templ));
6901 + client->adapter = adapter;
6902 + client->addr = addr;
6903 + client->flags = I2C_CLIENT_SCCB;
6906 + "wis-ov7640: initializing OV7640 at address %d on %s\n",
6907 + addr, adapter->name);
6909 + if (write_regs(client, initial_registers) < 0) {
6910 + printk(KERN_ERR "wis-ov7640: error initializing OV7640\n");
6915 + i2c_attach_client(client);
6919 +static int wis_ov7640_detach(struct i2c_client *client)
6923 + r = i2c_detach_client(client);
6931 +static struct i2c_driver wis_ov7640_driver = {
6933 + .name = "WIS OV7640 I2C driver",
6935 + .id = I2C_DRIVERID_WIS_OV7640,
6936 + .detach_client = wis_ov7640_detach,
6939 +static int __init wis_ov7640_init(void)
6943 + r = i2c_add_driver(&wis_ov7640_driver);
6946 + return wis_i2c_add_driver(wis_ov7640_driver.id, wis_ov7640_detect);
6949 +static void __exit wis_ov7640_cleanup(void)
6951 + wis_i2c_del_driver(wis_ov7640_detect);
6952 + i2c_del_driver(&wis_ov7640_driver);
6955 +module_init(wis_ov7640_init);
6956 +module_exit(wis_ov7640_cleanup);
6958 +MODULE_LICENSE("GPL v2");
6959 diff --git a/drivers/staging/go7007/wis-saa7113.c b/drivers/staging/go7007/wis-saa7113.c
6960 new file mode 100644
6961 index 0000000..4b14ca8
6963 +++ b/drivers/staging/go7007/wis-saa7113.c
6966 + * Copyright (C) 2005-2006 Micronas USA Inc.
6968 + * This program is free software; you can redistribute it and/or modify
6969 + * it under the terms of the GNU General Public License (Version 2) as
6970 + * published by the Free Software Foundation.
6972 + * This program is distributed in the hope that it will be useful,
6973 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6974 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6975 + * GNU General Public License for more details.
6977 + * You should have received a copy of the GNU General Public License
6978 + * along with this program; if not, write to the Free Software Foundation,
6979 + * Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
6982 +#include <linux/module.h>
6983 +#include <linux/init.h>
6984 +#include <linux/version.h>
6985 +#include <linux/i2c.h>
6986 +#include <linux/videodev.h>
6987 +#include <linux/video_decoder.h>
6988 +#include <linux/ioctl.h>
6990 +#include "wis-i2c.h"
6992 +struct wis_saa7113 {
7000 +static u8 initial_registers[] =
7067 + 0x00, 0x00, /* Terminator (reg 0x00 is read-only) */
7070 +static int write_reg(struct i2c_client *client, u8 reg, u8 value)
7072 + return i2c_smbus_write_byte_data(client, reg, value);
7075 +static int write_regs(struct i2c_client *client, u8 *regs)
7079 + for (i = 0; regs[i] != 0x00; i += 2)
7080 + if (i2c_smbus_write_byte_data(client, regs[i], regs[i + 1]) < 0)
7085 +static int wis_saa7113_command(struct i2c_client *client,
7086 + unsigned int cmd, void *arg)
7088 + struct wis_saa7113 *dec = i2c_get_clientdata(client);
7091 + case DECODER_SET_INPUT:
7095 + i2c_smbus_write_byte_data(client, 0x02, 0xC0 | *input);
7096 + i2c_smbus_write_byte_data(client, 0x09,
7097 + *input < 6 ? 0x40 : 0x80);
7100 + case DECODER_SET_NORM:
7103 + dec->norm = *input;
7104 + switch (dec->norm) {
7105 + case VIDEO_MODE_PAL:
7106 + write_reg(client, 0x0e, 0x01);
7107 + write_reg(client, 0x10, 0x48);
7109 + case VIDEO_MODE_NTSC:
7110 + write_reg(client, 0x0e, 0x01);
7111 + write_reg(client, 0x10, 0x40);
7113 + case VIDEO_MODE_SECAM:
7114 + write_reg(client, 0x0e, 0x50);
7115 + write_reg(client, 0x10, 0x48);
7120 + case VIDIOC_QUERYCTRL:
7122 + struct v4l2_queryctrl *ctrl = arg;
7124 + switch (ctrl->id) {
7125 + case V4L2_CID_BRIGHTNESS:
7126 + ctrl->type = V4L2_CTRL_TYPE_INTEGER;
7127 + strncpy(ctrl->name, "Brightness", sizeof(ctrl->name));
7128 + ctrl->minimum = 0;
7129 + ctrl->maximum = 255;
7131 + ctrl->default_value = 128;
7134 + case V4L2_CID_CONTRAST:
7135 + ctrl->type = V4L2_CTRL_TYPE_INTEGER;
7136 + strncpy(ctrl->name, "Contrast", sizeof(ctrl->name));
7137 + ctrl->minimum = 0;
7138 + ctrl->maximum = 127;
7140 + ctrl->default_value = 71;
7143 + case V4L2_CID_SATURATION:
7144 + ctrl->type = V4L2_CTRL_TYPE_INTEGER;
7145 + strncpy(ctrl->name, "Saturation", sizeof(ctrl->name));
7146 + ctrl->minimum = 0;
7147 + ctrl->maximum = 127;
7149 + ctrl->default_value = 64;
7152 + case V4L2_CID_HUE:
7153 + ctrl->type = V4L2_CTRL_TYPE_INTEGER;
7154 + strncpy(ctrl->name, "Hue", sizeof(ctrl->name));
7155 + ctrl->minimum = -128;
7156 + ctrl->maximum = 127;
7158 + ctrl->default_value = 0;
7164 + case VIDIOC_S_CTRL:
7166 + struct v4l2_control *ctrl = arg;
7168 + switch (ctrl->id) {
7169 + case V4L2_CID_BRIGHTNESS:
7170 + if (ctrl->value > 255)
7171 + dec->brightness = 255;
7172 + else if (ctrl->value < 0)
7173 + dec->brightness = 0;
7175 + dec->brightness = ctrl->value;
7176 + write_reg(client, 0x0a, dec->brightness);
7178 + case V4L2_CID_CONTRAST:
7179 + if (ctrl->value > 127)
7180 + dec->contrast = 127;
7181 + else if (ctrl->value < 0)
7182 + dec->contrast = 0;
7184 + dec->contrast = ctrl->value;
7185 + write_reg(client, 0x0b, dec->contrast);
7187 + case V4L2_CID_SATURATION:
7188 + if (ctrl->value > 127)
7189 + dec->saturation = 127;
7190 + else if (ctrl->value < 0)
7191 + dec->saturation = 0;
7193 + dec->saturation = ctrl->value;
7194 + write_reg(client, 0x0c, dec->saturation);
7196 + case V4L2_CID_HUE:
7197 + if (ctrl->value > 127)
7199 + else if (ctrl->value < -128)
7202 + dec->hue = ctrl->value;
7203 + write_reg(client, 0x0d, dec->hue);
7208 + case VIDIOC_G_CTRL:
7210 + struct v4l2_control *ctrl = arg;
7212 + switch (ctrl->id) {
7213 + case V4L2_CID_BRIGHTNESS:
7214 + ctrl->value = dec->brightness;
7216 + case V4L2_CID_CONTRAST:
7217 + ctrl->value = dec->contrast;
7219 + case V4L2_CID_SATURATION:
7220 + ctrl->value = dec->saturation;
7222 + case V4L2_CID_HUE:
7223 + ctrl->value = dec->hue;
7234 +static struct i2c_driver wis_saa7113_driver;
7236 +static struct i2c_client wis_saa7113_client_templ = {
7237 + .name = "SAA7113 (WIS)",
7238 + .driver = &wis_saa7113_driver,
7241 +static int wis_saa7113_detect(struct i2c_adapter *adapter, int addr, int kind)
7243 + struct i2c_client *client;
7244 + struct wis_saa7113 *dec;
7246 + if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
7249 + client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
7250 + if (client == NULL)
7252 + memcpy(client, &wis_saa7113_client_templ,
7253 + sizeof(wis_saa7113_client_templ));
7254 + client->adapter = adapter;
7255 + client->addr = addr;
7257 + dec = kmalloc(sizeof(struct wis_saa7113), GFP_KERNEL);
7258 + if (dec == NULL) {
7262 + dec->norm = VIDEO_MODE_NTSC;
7263 + dec->brightness = 128;
7264 + dec->contrast = 71;
7265 + dec->saturation = 64;
7267 + i2c_set_clientdata(client, dec);
7270 + "wis-saa7113: initializing SAA7113 at address %d on %s\n",
7271 + addr, adapter->name);
7273 + if (write_regs(client, initial_registers) < 0) {
7275 + "wis-saa7113: error initializing SAA7113\n");
7281 + i2c_attach_client(client);
7285 +static int wis_saa7113_detach(struct i2c_client *client)
7287 + struct wis_saa7113 *dec = i2c_get_clientdata(client);
7290 + r = i2c_detach_client(client);
7299 +static struct i2c_driver wis_saa7113_driver = {
7301 + .name = "WIS SAA7113 I2C driver",
7303 + .id = I2C_DRIVERID_WIS_SAA7113,
7304 + .detach_client = wis_saa7113_detach,
7305 + .command = wis_saa7113_command,
7308 +static int __init wis_saa7113_init(void)
7312 + r = i2c_add_driver(&wis_saa7113_driver);
7315 + return wis_i2c_add_driver(wis_saa7113_driver.id, wis_saa7113_detect);
7318 +static void __exit wis_saa7113_cleanup(void)
7320 + wis_i2c_del_driver(wis_saa7113_detect);
7321 + i2c_del_driver(&wis_saa7113_driver);
7324 +module_init(wis_saa7113_init);
7325 +module_exit(wis_saa7113_cleanup);
7327 +MODULE_LICENSE("GPL v2");
7328 diff --git a/drivers/staging/go7007/wis-saa7115.c b/drivers/staging/go7007/wis-saa7115.c
7329 new file mode 100644
7330 index 0000000..bd40bf4
7332 +++ b/drivers/staging/go7007/wis-saa7115.c
7335 + * Copyright (C) 2005-2006 Micronas USA Inc.
7337 + * This program is free software; you can redistribute it and/or modify
7338 + * it under the terms of the GNU General Public License (Version 2) as
7339 + * published by the Free Software Foundation.
7341 + * This program is distributed in the hope that it will be useful,
7342 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7343 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7344 + * GNU General Public License for more details.
7346 + * You should have received a copy of the GNU General Public License
7347 + * along with this program; if not, write to the Free Software Foundation,
7348 + * Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
7351 +#include <linux/module.h>
7352 +#include <linux/init.h>
7353 +#include <linux/version.h>
7354 +#include <linux/i2c.h>
7355 +#include <linux/videodev.h>
7356 +#include <linux/video_decoder.h>
7357 +#include <linux/ioctl.h>
7359 +#include "wis-i2c.h"
7361 +struct wis_saa7115 {
7369 +static u8 initial_registers[] =
7378 + 0x08, 0xf0, /* always toggle FID */
7428 + 0x54, 0xf4 /*0xff*/,
7434 + 0x5a, 0x06 /*0x03*/,
7438 + 0x80, 0x30, /* window defined scaler operation, task A and B enabled */
7439 + 0x81, 0x03, /* use scaler datapath generated V */
7446 + 0x90, 0x02, /* task A process top field */
7481 + 0xc0, 0x03, /* task B process bottom field */
7515 + 0x88, 0xf0, /* End of original static list */
7516 + 0x00, 0x00, /* Terminator (reg 0x00 is read-only) */
7519 +static int write_reg(struct i2c_client *client, u8 reg, u8 value)
7521 + return i2c_smbus_write_byte_data(client, reg, value);
7524 +static int write_regs(struct i2c_client *client, u8 *regs)
7528 + for (i = 0; regs[i] != 0x00; i += 2)
7529 + if (i2c_smbus_write_byte_data(client, regs[i], regs[i + 1]) < 0)
7534 +static int wis_saa7115_command(struct i2c_client *client,
7535 + unsigned int cmd, void *arg)
7537 + struct wis_saa7115 *dec = i2c_get_clientdata(client);
7540 + case DECODER_SET_INPUT:
7544 + i2c_smbus_write_byte_data(client, 0x02, 0xC0 | *input);
7545 + i2c_smbus_write_byte_data(client, 0x09,
7546 + *input < 6 ? 0x40 : 0xC0);
7549 + case DECODER_SET_RESOLUTION:
7551 + struct video_decoder_resolution *res = arg;
7552 + /* Course-grained scaler */
7553 + int h_integer_scaler = res->width < 704 ? 704 / res->width : 1;
7554 + /* Fine-grained scaler to take care of remainder */
7555 + int h_scaling_increment = (704 / h_integer_scaler) *
7556 + 1024 / res->width;
7557 + /* Fine-grained scaler only */
7558 + int v_scaling_increment = (dec->norm == VIDEO_MODE_NTSC ?
7559 + 240 : 288) * 1024 / res->height;
7562 + 0x9c, res->width & 0xff,
7563 + 0x9d, res->width >> 8,
7564 + 0x9e, res->height & 0xff,
7565 + 0x9f, res->height >> 8,
7566 + 0xa0, h_integer_scaler,
7569 + 0xa8, h_scaling_increment & 0xff,
7570 + 0xa9, h_scaling_increment >> 8,
7571 + 0xac, (h_scaling_increment / 2) & 0xff,
7572 + 0xad, (h_scaling_increment / 2) >> 8,
7573 + 0xb0, v_scaling_increment & 0xff,
7574 + 0xb1, v_scaling_increment >> 8,
7575 + 0xb2, v_scaling_increment & 0xff,
7576 + 0xb3, v_scaling_increment >> 8,
7577 + 0xcc, res->width & 0xff,
7578 + 0xcd, res->width >> 8,
7579 + 0xce, res->height & 0xff,
7580 + 0xcf, res->height >> 8,
7581 + 0xd0, h_integer_scaler,
7584 + 0xd8, h_scaling_increment & 0xff,
7585 + 0xd9, h_scaling_increment >> 8,
7586 + 0xdc, (h_scaling_increment / 2) & 0xff,
7587 + 0xdd, (h_scaling_increment / 2) >> 8,
7588 + 0xe0, v_scaling_increment & 0xff,
7589 + 0xe1, v_scaling_increment >> 8,
7590 + 0xe2, v_scaling_increment & 0xff,
7591 + 0xe3, v_scaling_increment >> 8,
7595 + write_regs(client, regs);
7598 + case DECODER_SET_NORM:
7603 + 0x98, *input == VIDEO_MODE_NTSC ? 0x12 : 0x16,
7604 + 0x9a, *input == VIDEO_MODE_NTSC ? 0xf2 : 0x20,
7605 + 0x9b, *input == VIDEO_MODE_NTSC ? 0x00 : 0x01,
7606 + 0xc8, *input == VIDEO_MODE_NTSC ? 0x12 : 0x16,
7607 + 0xca, *input == VIDEO_MODE_NTSC ? 0xf2 : 0x20,
7608 + 0xcb, *input == VIDEO_MODE_NTSC ? 0x00 : 0x01,
7610 + 0x30, *input == VIDEO_MODE_NTSC ? 0x66 : 0x00,
7611 + 0x31, *input == VIDEO_MODE_NTSC ? 0x90 : 0xe0,
7614 + write_regs(client, regs);
7615 + dec->norm = *input;
7618 + case VIDIOC_QUERYCTRL:
7620 + struct v4l2_queryctrl *ctrl = arg;
7622 + switch (ctrl->id) {
7623 + case V4L2_CID_BRIGHTNESS:
7624 + ctrl->type = V4L2_CTRL_TYPE_INTEGER;
7625 + strncpy(ctrl->name, "Brightness", sizeof(ctrl->name));
7626 + ctrl->minimum = 0;
7627 + ctrl->maximum = 255;
7629 + ctrl->default_value = 128;
7632 + case V4L2_CID_CONTRAST:
7633 + ctrl->type = V4L2_CTRL_TYPE_INTEGER;
7634 + strncpy(ctrl->name, "Contrast", sizeof(ctrl->name));
7635 + ctrl->minimum = 0;
7636 + ctrl->maximum = 127;
7638 + ctrl->default_value = 64;
7641 + case V4L2_CID_SATURATION:
7642 + ctrl->type = V4L2_CTRL_TYPE_INTEGER;
7643 + strncpy(ctrl->name, "Saturation", sizeof(ctrl->name));
7644 + ctrl->minimum = 0;
7645 + ctrl->maximum = 127;
7647 + ctrl->default_value = 64;
7650 + case V4L2_CID_HUE:
7651 + ctrl->type = V4L2_CTRL_TYPE_INTEGER;
7652 + strncpy(ctrl->name, "Hue", sizeof(ctrl->name));
7653 + ctrl->minimum = -128;
7654 + ctrl->maximum = 127;
7656 + ctrl->default_value = 0;
7662 + case VIDIOC_S_CTRL:
7664 + struct v4l2_control *ctrl = arg;
7666 + switch (ctrl->id) {
7667 + case V4L2_CID_BRIGHTNESS:
7668 + if (ctrl->value > 255)
7669 + dec->brightness = 255;
7670 + else if (ctrl->value < 0)
7671 + dec->brightness = 0;
7673 + dec->brightness = ctrl->value;
7674 + write_reg(client, 0x0a, dec->brightness);
7676 + case V4L2_CID_CONTRAST:
7677 + if (ctrl->value > 127)
7678 + dec->contrast = 127;
7679 + else if (ctrl->value < 0)
7680 + dec->contrast = 0;
7682 + dec->contrast = ctrl->value;
7683 + write_reg(client, 0x0b, dec->contrast);
7685 + case V4L2_CID_SATURATION:
7686 + if (ctrl->value > 127)
7687 + dec->saturation = 127;
7688 + else if (ctrl->value < 0)
7689 + dec->saturation = 0;
7691 + dec->saturation = ctrl->value;
7692 + write_reg(client, 0x0c, dec->saturation);
7694 + case V4L2_CID_HUE:
7695 + if (ctrl->value > 127)
7697 + else if (ctrl->value < -128)
7700 + dec->hue = ctrl->value;
7701 + write_reg(client, 0x0d, dec->hue);
7706 + case VIDIOC_G_CTRL:
7708 + struct v4l2_control *ctrl = arg;
7710 + switch (ctrl->id) {
7711 + case V4L2_CID_BRIGHTNESS:
7712 + ctrl->value = dec->brightness;
7714 + case V4L2_CID_CONTRAST:
7715 + ctrl->value = dec->contrast;
7717 + case V4L2_CID_SATURATION:
7718 + ctrl->value = dec->saturation;
7720 + case V4L2_CID_HUE:
7721 + ctrl->value = dec->hue;
7732 +static struct i2c_driver wis_saa7115_driver;
7734 +static struct i2c_client wis_saa7115_client_templ = {
7735 + .name = "SAA7115 (WIS)",
7736 + .driver = &wis_saa7115_driver,
7739 +static int wis_saa7115_detect(struct i2c_adapter *adapter, int addr, int kind)
7741 + struct i2c_client *client;
7742 + struct wis_saa7115 *dec;
7744 + if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
7747 + client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
7748 + if (client == NULL)
7750 + memcpy(client, &wis_saa7115_client_templ,
7751 + sizeof(wis_saa7115_client_templ));
7752 + client->adapter = adapter;
7753 + client->addr = addr;
7755 + dec = kmalloc(sizeof(struct wis_saa7115), GFP_KERNEL);
7756 + if (dec == NULL) {
7760 + dec->norm = VIDEO_MODE_NTSC;
7761 + dec->brightness = 128;
7762 + dec->contrast = 64;
7763 + dec->saturation = 64;
7765 + i2c_set_clientdata(client, dec);
7768 + "wis-saa7115: initializing SAA7115 at address %d on %s\n",
7769 + addr, adapter->name);
7771 + if (write_regs(client, initial_registers) < 0) {
7773 + "wis-saa7115: error initializing SAA7115\n");
7779 + i2c_attach_client(client);
7783 +static int wis_saa7115_detach(struct i2c_client *client)
7785 + struct wis_saa7115 *dec = i2c_get_clientdata(client);
7788 + r = i2c_detach_client(client);
7797 +static struct i2c_driver wis_saa7115_driver = {
7799 + .name = "WIS SAA7115 I2C driver",
7801 + .id = I2C_DRIVERID_WIS_SAA7115,
7802 + .detach_client = wis_saa7115_detach,
7803 + .command = wis_saa7115_command,
7806 +static int __init wis_saa7115_init(void)
7810 + r = i2c_add_driver(&wis_saa7115_driver);
7813 + return wis_i2c_add_driver(wis_saa7115_driver.id, wis_saa7115_detect);
7816 +static void __exit wis_saa7115_cleanup(void)
7818 + wis_i2c_del_driver(wis_saa7115_detect);
7819 + i2c_del_driver(&wis_saa7115_driver);
7822 +module_init(wis_saa7115_init);
7823 +module_exit(wis_saa7115_cleanup);
7825 +MODULE_LICENSE("GPL v2");
7826 diff --git a/drivers/staging/go7007/wis-sony-tuner.c b/drivers/staging/go7007/wis-sony-tuner.c
7827 new file mode 100644
7828 index 0000000..82e66d6
7830 +++ b/drivers/staging/go7007/wis-sony-tuner.c
7833 + * Copyright (C) 2005-2006 Micronas USA Inc.
7835 + * This program is free software; you can redistribute it and/or modify
7836 + * it under the terms of the GNU General Public License (Version 2) as
7837 + * published by the Free Software Foundation.
7839 + * This program is distributed in the hope that it will be useful,
7840 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7841 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7842 + * GNU General Public License for more details.
7844 + * You should have received a copy of the GNU General Public License
7845 + * along with this program; if not, write to the Free Software Foundation,
7846 + * Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
7849 +#include <linux/module.h>
7850 +#include <linux/init.h>
7851 +#include <linux/version.h>
7852 +#include <linux/i2c.h>
7853 +#include <linux/videodev.h>
7854 +#include <media/tuner.h>
7855 +#include <media/v4l2-common.h>
7857 +#include "wis-i2c.h"
7859 +/* #define MPX_DEBUG */
7861 +/* AS(IF/MPX) pin: LOW HIGH/OPEN
7862 + * IF/MPX address: 0x42/0x40 0x43/0x44
7864 +#define IF_I2C_ADDR 0x43
7865 +#define MPX_I2C_ADDR 0x44
7867 +static v4l2_std_id force_band;
7868 +static char force_band_str[] = "-";
7869 +module_param_string(force_band, force_band_str, sizeof(force_band_str), 0644);
7870 +static int force_mpx_mode = -1;
7871 +module_param(force_mpx_mode, int, 0644);
7873 +/* Store tuner info in the same format as tuner.c, so maybe we can put the
7874 + * Sony tuner support in there. */
7875 +struct sony_tunertype {
7877 + unsigned char Vendor; /* unused here */
7878 + unsigned char Type; /* unused here */
7880 + unsigned short thresh1; /* band switch VHF_LO <=> VHF_HI */
7881 + unsigned short thresh2; /* band switch VHF_HI <=> UHF */
7882 + unsigned char VHF_L;
7883 + unsigned char VHF_H;
7884 + unsigned char UHF;
7885 + unsigned char config;
7886 + unsigned short IFPCoff;
7889 +/* This array is indexed by (tuner_type - 200) */
7890 +static struct sony_tunertype sony_tuners[] = {
7891 + { "Sony PAL+SECAM (BTF-PG472Z)", 0, 0,
7892 + 16*144.25, 16*427.25, 0x01, 0x02, 0x04, 0xc6, 623},
7893 + { "Sony NTSC_JP (BTF-PK467Z)", 0, 0,
7894 + 16*220.25, 16*467.25, 0x01, 0x02, 0x04, 0xc6, 940},
7895 + { "Sony NTSC (BTF-PB463Z)", 0, 0,
7896 + 16*130.25, 16*364.25, 0x01, 0x02, 0x04, 0xc6, 732},
7899 +struct wis_sony_tuner {
7902 + unsigned int freq;
7907 +/* Basically the same as default_set_tv_freq() in tuner.c */
7908 +static int set_freq(struct i2c_client *client, int freq)
7910 + struct wis_sony_tuner *t = i2c_get_clientdata(client);
7914 + struct sony_tunertype *tun;
7917 + tun = &sony_tuners[t->type - 200];
7918 + if (freq < tun->thresh1) {
7919 + band_name = "VHF_L";
7920 + band_select = tun->VHF_L;
7921 + } else if (freq < tun->thresh2) {
7922 + band_name = "VHF_H";
7923 + band_select = tun->VHF_H;
7925 + band_name = "UHF";
7926 + band_select = tun->UHF;
7928 + printk(KERN_DEBUG "wis-sony-tuner: tuning to frequency %d.%04d (%s)\n",
7929 + freq / 16, (freq % 16) * 625, band_name);
7930 + n = freq + tun->IFPCoff;
7932 + buffer[0] = n >> 8;
7933 + buffer[1] = n & 0xff;
7934 + buffer[2] = tun->config;
7935 + buffer[3] = band_select;
7936 + i2c_master_send(client, buffer, 4);
7941 +static int mpx_write(struct i2c_client *client, int dev, int addr, int val)
7944 + struct i2c_msg msg;
7947 + buffer[1] = addr >> 8;
7948 + buffer[2] = addr & 0xff;
7949 + buffer[3] = val >> 8;
7950 + buffer[4] = val & 0xff;
7951 + msg.addr = MPX_I2C_ADDR;
7955 + i2c_transfer(client->adapter, &msg, 1);
7960 + * MPX register values for the BTF-PG472Z:
7962 + * FM_ NICAM_ SCART_
7963 + * MODUS SOURCE ACB PRESCAL PRESCAL PRESCAL SYSTEM VOLUME
7964 + * 10/0030 12/0008 12/0013 12/000E 12/0010 12/0000 10/0020 12/0000
7965 + * ---------------------------------------------------------------
7966 + * Auto 1003 0020 0100 2603 5000 XXXX 0001 7500
7969 + * Mono 1003 0020 0100 2603 5000 XXXX 0003 7500
7970 + * A2 1003 0020 0100 2601 5000 XXXX 0003 7500
7971 + * NICAM 1003 0120 0100 2603 5000 XXXX 0008 7500
7974 + * Mono 1003 0020 0100 2603 7900 XXXX 000A 7500
7975 + * NICAM 1003 0120 0100 2603 7900 XXXX 000A 7500
7978 + * Mono 1003 0020 0100 2603 5000 XXXX 0004 7500
7979 + * A2-1 1003 0020 0100 2601 5000 XXXX 0004 7500
7980 + * A2-2 1003 0020 0100 2601 5000 XXXX 0005 7500
7981 + * A2-3 1003 0020 0100 2601 5000 XXXX 0007 7500
7982 + * NICAM 1003 0120 0100 2603 5000 XXXX 000B 7500
7985 + * Mono 0003 0200 0100 7C03 5000 2200 0009 7500
7986 + * NICAM 0003 0120 0100 7C03 5000 XXXX 0009 7500
7989 + * Mono 1003 0200 0100 2B03 5000 2B00 0002 7500
7991 + * For Asia, replace the 0x26XX in FM_PRESCALE with 0x14XX.
7993 + * Bilingual selection in A2/NICAM:
7995 + * High byte of SOURCE Left chan Right chan
8000 + * Force mono in NICAM by setting the high byte of SOURCE to 0x02 (L/L') or
8001 + * 0x00 (all other bands). Force mono in A2 with FMONO_A2:
8006 + * Forced mono ON 07F0
8007 + * Forced mono OFF 0190
8011 + enum { AUD_MONO, AUD_A2, AUD_NICAM, AUD_NICAM_L } audio_mode;
8016 + u16 nicam_prescale;
8017 + u16 scart_prescale;
8020 +} mpx_audio_modes[] = {
8021 + /* Auto */ { AUD_MONO, 0x1003, 0x0020, 0x0100, 0x2603,
8022 + 0x5000, 0x0000, 0x0001, 0x7500 },
8023 + /* B/G Mono */ { AUD_MONO, 0x1003, 0x0020, 0x0100, 0x2603,
8024 + 0x5000, 0x0000, 0x0003, 0x7500 },
8025 + /* B/G A2 */ { AUD_A2, 0x1003, 0x0020, 0x0100, 0x2601,
8026 + 0x5000, 0x0000, 0x0003, 0x7500 },
8027 + /* B/G NICAM */ { AUD_NICAM, 0x1003, 0x0120, 0x0100, 0x2603,
8028 + 0x5000, 0x0000, 0x0008, 0x7500 },
8029 + /* I Mono */ { AUD_MONO, 0x1003, 0x0020, 0x0100, 0x2603,
8030 + 0x7900, 0x0000, 0x000A, 0x7500 },
8031 + /* I NICAM */ { AUD_NICAM, 0x1003, 0x0120, 0x0100, 0x2603,
8032 + 0x7900, 0x0000, 0x000A, 0x7500 },
8033 + /* D/K Mono */ { AUD_MONO, 0x1003, 0x0020, 0x0100, 0x2603,
8034 + 0x5000, 0x0000, 0x0004, 0x7500 },
8035 + /* D/K A2-1 */ { AUD_A2, 0x1003, 0x0020, 0x0100, 0x2601,
8036 + 0x5000, 0x0000, 0x0004, 0x7500 },
8037 + /* D/K A2-2 */ { AUD_A2, 0x1003, 0x0020, 0x0100, 0x2601,
8038 + 0x5000, 0x0000, 0x0005, 0x7500 },
8039 + /* D/K A2-3 */ { AUD_A2, 0x1003, 0x0020, 0x0100, 0x2601,
8040 + 0x5000, 0x0000, 0x0007, 0x7500 },
8041 + /* D/K NICAM */ { AUD_NICAM, 0x1003, 0x0120, 0x0100, 0x2603,
8042 + 0x5000, 0x0000, 0x000B, 0x7500 },
8043 + /* L/L' Mono */ { AUD_MONO, 0x0003, 0x0200, 0x0100, 0x7C03,
8044 + 0x5000, 0x2200, 0x0009, 0x7500 },
8045 + /* L/L' NICAM */{ AUD_NICAM_L, 0x0003, 0x0120, 0x0100, 0x7C03,
8046 + 0x5000, 0x0000, 0x0009, 0x7500 },
8049 +#define MPX_NUM_MODES ARRAY_SIZE(mpx_audio_modes)
8051 +static int mpx_setup(struct i2c_client *client)
8053 + struct wis_sony_tuner *t = i2c_get_clientdata(client);
8056 + struct i2c_msg msg;
8062 + msg.addr = MPX_I2C_ADDR;
8066 + i2c_transfer(client->adapter, &msg, 1);
8068 + i2c_transfer(client->adapter, &msg, 1);
8070 + if (mpx_audio_modes[t->mpxmode].audio_mode != AUD_MONO) {
8071 + switch (t->audmode) {
8072 + case V4L2_TUNER_MODE_MONO:
8073 + switch (mpx_audio_modes[t->mpxmode].audio_mode) {
8075 + source = mpx_audio_modes[t->mpxmode].source;
8087 + case V4L2_TUNER_MODE_STEREO:
8088 + source = mpx_audio_modes[t->mpxmode].source;
8090 + case V4L2_TUNER_MODE_LANG1:
8093 + case V4L2_TUNER_MODE_LANG2:
8097 + source |= mpx_audio_modes[t->mpxmode].source & 0x00ff;
8099 + source = mpx_audio_modes[t->mpxmode].source;
8101 + mpx_write(client, 0x10, 0x0030, mpx_audio_modes[t->mpxmode].modus);
8102 + mpx_write(client, 0x12, 0x0008, source);
8103 + mpx_write(client, 0x12, 0x0013, mpx_audio_modes[t->mpxmode].acb);
8104 + mpx_write(client, 0x12, 0x000e,
8105 + mpx_audio_modes[t->mpxmode].fm_prescale);
8106 + mpx_write(client, 0x12, 0x0010,
8107 + mpx_audio_modes[t->mpxmode].nicam_prescale);
8108 + mpx_write(client, 0x12, 0x000d,
8109 + mpx_audio_modes[t->mpxmode].scart_prescale);
8110 + mpx_write(client, 0x10, 0x0020, mpx_audio_modes[t->mpxmode].system);
8111 + mpx_write(client, 0x12, 0x0000, mpx_audio_modes[t->mpxmode].volume);
8112 + if (mpx_audio_modes[t->mpxmode].audio_mode == AUD_A2)
8113 + mpx_write(client, 0x10, 0x0022,
8114 + t->audmode == V4L2_TUNER_MODE_MONO ? 0x07f0 : 0x0190);
8118 + u8 buf1[3], buf2[2];
8119 + struct i2c_msg msgs[2];
8121 + printk(KERN_DEBUG "wis-sony-tuner: MPX registers: %04x %04x "
8122 + "%04x %04x %04x %04x %04x %04x\n",
8123 + mpx_audio_modes[t->mpxmode].modus,
8125 + mpx_audio_modes[t->mpxmode].acb,
8126 + mpx_audio_modes[t->mpxmode].fm_prescale,
8127 + mpx_audio_modes[t->mpxmode].nicam_prescale,
8128 + mpx_audio_modes[t->mpxmode].scart_prescale,
8129 + mpx_audio_modes[t->mpxmode].system,
8130 + mpx_audio_modes[t->mpxmode].volume);
8134 + msgs[0].addr = MPX_I2C_ADDR;
8135 + msgs[0].flags = 0;
8137 + msgs[0].buf = buf1;
8138 + msgs[1].addr = MPX_I2C_ADDR;
8139 + msgs[1].flags = I2C_M_RD;
8141 + msgs[1].buf = buf2;
8142 + i2c_transfer(client->adapter, msgs, 2);
8143 + printk(KERN_DEBUG "wis-sony-tuner: MPX system: %02x%02x\n",
8144 + buf2[0], buf2[1]);
8148 + i2c_transfer(client->adapter, msgs, 2);
8149 + printk(KERN_DEBUG "wis-sony-tuner: MPX status: %02x%02x\n",
8150 + buf2[0], buf2[1]);
8157 + * IF configuration values for the BTF-PG472Z:
8159 + * B/G: 0x94 0x70 0x49
8160 + * I: 0x14 0x70 0x4a
8161 + * D/K: 0x14 0x70 0x4b
8162 + * L: 0x04 0x70 0x4b
8163 + * L': 0x44 0x70 0x53
8164 + * M: 0x50 0x30 0x4c
8167 +static int set_if(struct i2c_client *client)
8169 + struct wis_sony_tuner *t = i2c_get_clientdata(client);
8171 + struct i2c_msg msg;
8172 + int default_mpx_mode = 0;
8174 + /* configure IF */
8176 + if (t->std & V4L2_STD_PAL_BG) {
8180 + default_mpx_mode = 1;
8181 + } else if (t->std & V4L2_STD_PAL_I) {
8185 + default_mpx_mode = 4;
8186 + } else if (t->std & V4L2_STD_PAL_DK) {
8190 + default_mpx_mode = 6;
8191 + } else if (t->std & V4L2_STD_SECAM_L) {
8195 + default_mpx_mode = 11;
8197 + msg.addr = IF_I2C_ADDR;
8201 + i2c_transfer(client->adapter, &msg, 1);
8203 + /* Select MPX mode if not forced by the user */
8204 + if (force_mpx_mode >= 0 || force_mpx_mode < MPX_NUM_MODES)
8205 + t->mpxmode = force_mpx_mode;
8207 + t->mpxmode = default_mpx_mode;
8208 + printk(KERN_DEBUG "wis-sony-tuner: setting MPX to mode %d\n",
8210 + mpx_setup(client);
8215 +static int tuner_command(struct i2c_client *client, unsigned int cmd, void *arg)
8217 + struct wis_sony_tuner *t = i2c_get_clientdata(client);
8220 +#ifdef TUNER_SET_TYPE_ADDR
8221 + case TUNER_SET_TYPE_ADDR:
8223 + struct tuner_setup *tun_setup = arg;
8224 + int *type = &tun_setup->type;
8226 + case TUNER_SET_TYPE:
8231 + if (t->type >= 0) {
8232 + if (t->type != *type)
8233 + printk(KERN_ERR "wis-sony-tuner: type already "
8234 + "set to %d, ignoring request for %d\n",
8239 + switch (t->type) {
8240 + case TUNER_SONY_BTF_PG472Z:
8241 + switch (force_band_str[0]) {
8246 + printk(KERN_INFO "wis-sony-tuner: forcing "
8247 + "tuner to PAL-B/G bands\n");
8248 + force_band = V4L2_STD_PAL_BG;
8252 + printk(KERN_INFO "wis-sony-tuner: forcing "
8253 + "tuner to PAL-I band\n");
8254 + force_band = V4L2_STD_PAL_I;
8260 + printk(KERN_INFO "wis-sony-tuner: forcing "
8261 + "tuner to PAL-D/K bands\n");
8262 + force_band = V4L2_STD_PAL_I;
8266 + printk(KERN_INFO "wis-sony-tuner: forcing "
8267 + "tuner to SECAM-L band\n");
8268 + force_band = V4L2_STD_SECAM_L;
8275 + t->std = force_band;
8277 + t->std = V4L2_STD_PAL_BG;
8280 + case TUNER_SONY_BTF_PK467Z:
8281 + t->std = V4L2_STD_NTSC_M_JP;
8283 + case TUNER_SONY_BTF_PB463Z:
8284 + t->std = V4L2_STD_NTSC_M;
8287 + printk(KERN_ERR "wis-sony-tuner: tuner type %d is not "
8288 + "supported by this module\n", *type);
8293 + "wis-sony-tuner: type set to %d (%s)\n",
8294 + t->type, sony_tuners[t->type - 200].name);
8297 + case VIDIOC_G_FREQUENCY:
8299 + struct v4l2_frequency *f = arg;
8301 + f->frequency = t->freq;
8304 + case VIDIOC_S_FREQUENCY:
8306 + struct v4l2_frequency *f = arg;
8308 + t->freq = f->frequency;
8309 + set_freq(client, t->freq);
8312 + case VIDIOC_ENUMSTD:
8314 + struct v4l2_standard *std = arg;
8316 + switch (t->type) {
8317 + case TUNER_SONY_BTF_PG472Z:
8318 + switch (std->index) {
8320 + v4l2_video_std_construct(std,
8321 + V4L2_STD_PAL_BG, "PAL-B/G");
8324 + v4l2_video_std_construct(std,
8325 + V4L2_STD_PAL_I, "PAL-I");
8328 + v4l2_video_std_construct(std,
8329 + V4L2_STD_PAL_DK, "PAL-D/K");
8332 + v4l2_video_std_construct(std,
8333 + V4L2_STD_SECAM_L, "SECAM-L");
8336 + std->id = 0; /* hack to indicate EINVAL */
8340 + case TUNER_SONY_BTF_PK467Z:
8341 + if (std->index != 0) {
8342 + std->id = 0; /* hack to indicate EINVAL */
8345 + v4l2_video_std_construct(std,
8346 + V4L2_STD_NTSC_M_JP, "NTSC-J");
8348 + case TUNER_SONY_BTF_PB463Z:
8349 + if (std->index != 0) {
8350 + std->id = 0; /* hack to indicate EINVAL */
8353 + v4l2_video_std_construct(std, V4L2_STD_NTSC_M, "NTSC");
8358 + case VIDIOC_G_STD:
8360 + v4l2_std_id *std = arg;
8365 + case VIDIOC_S_STD:
8367 + v4l2_std_id *std = arg;
8368 + v4l2_std_id old = t->std;
8370 + switch (t->type) {
8371 + case TUNER_SONY_BTF_PG472Z:
8372 + if (force_band && (*std & force_band) != *std &&
8373 + *std != V4L2_STD_PAL &&
8374 + *std != V4L2_STD_SECAM) {
8375 + printk(KERN_DEBUG "wis-sony-tuner: ignoring "
8376 + "requested TV standard in "
8377 + "favor of force_band value\n");
8378 + t->std = force_band;
8379 + } else if (*std & V4L2_STD_PAL_BG) { /* default */
8380 + t->std = V4L2_STD_PAL_BG;
8381 + } else if (*std & V4L2_STD_PAL_I) {
8382 + t->std = V4L2_STD_PAL_I;
8383 + } else if (*std & V4L2_STD_PAL_DK) {
8384 + t->std = V4L2_STD_PAL_DK;
8385 + } else if (*std & V4L2_STD_SECAM_L) {
8386 + t->std = V4L2_STD_SECAM_L;
8388 + printk(KERN_ERR "wis-sony-tuner: TV standard "
8389 + "not supported\n");
8390 + *std = 0; /* hack to indicate EINVAL */
8393 + if (old != t->std)
8396 + case TUNER_SONY_BTF_PK467Z:
8397 + if (!(*std & V4L2_STD_NTSC_M_JP)) {
8398 + printk(KERN_ERR "wis-sony-tuner: TV standard "
8399 + "not supported\n");
8400 + *std = 0; /* hack to indicate EINVAL */
8403 + case TUNER_SONY_BTF_PB463Z:
8404 + if (!(*std & V4L2_STD_NTSC_M)) {
8405 + printk(KERN_ERR "wis-sony-tuner: TV standard "
8406 + "not supported\n");
8407 + *std = 0; /* hack to indicate EINVAL */
8413 + case VIDIOC_QUERYSTD:
8415 + v4l2_std_id *std = arg;
8417 + switch (t->type) {
8418 + case TUNER_SONY_BTF_PG472Z:
8420 + *std = force_band;
8422 + *std = V4L2_STD_PAL_BG | V4L2_STD_PAL_I |
8423 + V4L2_STD_PAL_DK | V4L2_STD_SECAM_L;
8425 + case TUNER_SONY_BTF_PK467Z:
8426 + *std = V4L2_STD_NTSC_M_JP;
8428 + case TUNER_SONY_BTF_PB463Z:
8429 + *std = V4L2_STD_NTSC_M;
8434 + case VIDIOC_G_TUNER:
8436 + struct v4l2_tuner *tun = arg;
8438 + memset(t, 0, sizeof(*tun));
8439 + strcpy(tun->name, "Television");
8440 + tun->type = V4L2_TUNER_ANALOG_TV;
8441 + tun->rangelow = 0UL; /* does anything use these? */
8442 + tun->rangehigh = 0xffffffffUL;
8443 + switch (t->type) {
8444 + case TUNER_SONY_BTF_PG472Z:
8445 + tun->capability = V4L2_TUNER_CAP_NORM |
8446 + V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LANG1 |
8447 + V4L2_TUNER_CAP_LANG2;
8448 + tun->rxsubchans = V4L2_TUNER_SUB_MONO |
8449 + V4L2_TUNER_SUB_STEREO | V4L2_TUNER_SUB_LANG1 |
8450 + V4L2_TUNER_SUB_LANG2;
8452 + case TUNER_SONY_BTF_PK467Z:
8453 + case TUNER_SONY_BTF_PB463Z:
8454 + tun->capability = V4L2_TUNER_CAP_STEREO;
8455 + tun->rxsubchans = V4L2_TUNER_SUB_MONO |
8456 + V4L2_TUNER_SUB_STEREO;
8459 + tun->audmode = t->audmode;
8462 + case VIDIOC_S_TUNER:
8464 + struct v4l2_tuner *tun = arg;
8466 + switch (t->type) {
8467 + case TUNER_SONY_BTF_PG472Z:
8468 + if (tun->audmode != t->audmode) {
8469 + t->audmode = tun->audmode;
8470 + mpx_setup(client);
8473 + case TUNER_SONY_BTF_PK467Z:
8474 + case TUNER_SONY_BTF_PB463Z:
8485 +static struct i2c_driver wis_sony_tuner_driver;
8487 +static struct i2c_client wis_sony_tuner_client_templ = {
8488 + .name = "Sony TV Tuner (WIS)",
8489 + .driver = &wis_sony_tuner_driver,
8492 +static int wis_sony_tuner_detect(struct i2c_adapter *adapter,
8493 + int addr, int kind)
8495 + struct i2c_client *client;
8496 + struct wis_sony_tuner *t;
8498 + if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_I2C_BLOCK))
8501 + client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
8502 + if (client == NULL)
8504 + memcpy(client, &wis_sony_tuner_client_templ,
8505 + sizeof(wis_sony_tuner_client_templ));
8506 + client->adapter = adapter;
8507 + client->addr = addr;
8509 + t = kmalloc(sizeof(struct wis_sony_tuner), GFP_KERNEL);
8517 + t->audmode = V4L2_TUNER_MODE_STEREO;
8518 + i2c_set_clientdata(client, t);
8521 + "wis-sony-tuner: initializing tuner at address %d on %s\n",
8522 + addr, adapter->name);
8524 + i2c_attach_client(client);
8529 +static int wis_sony_tuner_detach(struct i2c_client *client)
8531 + struct wis_sony_tuner *t = i2c_get_clientdata(client);
8534 + r = i2c_detach_client(client);
8543 +static struct i2c_driver wis_sony_tuner_driver = {
8545 + .name = "WIS Sony TV Tuner I2C driver",
8547 + .id = I2C_DRIVERID_WIS_SONY_TUNER,
8548 + .detach_client = wis_sony_tuner_detach,
8549 + .command = tuner_command,
8552 +static int __init wis_sony_tuner_init(void)
8556 + r = i2c_add_driver(&wis_sony_tuner_driver);
8559 + return wis_i2c_add_driver(wis_sony_tuner_driver.id,
8560 + wis_sony_tuner_detect);
8563 +static void __exit wis_sony_tuner_cleanup(void)
8565 + wis_i2c_del_driver(wis_sony_tuner_detect);
8566 + i2c_del_driver(&wis_sony_tuner_driver);
8569 +module_init(wis_sony_tuner_init);
8570 +module_exit(wis_sony_tuner_cleanup);
8572 +MODULE_LICENSE("GPL v2");
8573 diff --git a/drivers/staging/go7007/wis-tw2804.c b/drivers/staging/go7007/wis-tw2804.c
8574 new file mode 100644
8575 index 0000000..69ed7bf
8577 +++ b/drivers/staging/go7007/wis-tw2804.c
8580 + * Copyright (C) 2005-2006 Micronas USA Inc.
8582 + * This program is free software; you can redistribute it and/or modify
8583 + * it under the terms of the GNU General Public License (Version 2) as
8584 + * published by the Free Software Foundation.
8586 + * This program is distributed in the hope that it will be useful,
8587 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
8588 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8589 + * GNU General Public License for more details.
8591 + * You should have received a copy of the GNU General Public License
8592 + * along with this program; if not, write to the Free Software Foundation,
8593 + * Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
8596 +#include <linux/module.h>
8597 +#include <linux/init.h>
8598 +#include <linux/version.h>
8599 +#include <linux/i2c.h>
8600 +#include <linux/videodev.h>
8601 +#include <linux/video_decoder.h>
8602 +#include <linux/ioctl.h>
8604 +#include "wis-i2c.h"
8606 +struct wis_tw2804 {
8615 +static u8 global_registers[] =
8624 + 0xff, 0xff, /* Terminator (reg 0xff does not exist) */
8627 +static u8 channel_registers[] =
8684 + 0xff, 0xff, /* Terminator (reg 0xff does not exist) */
8687 +static int write_reg(struct i2c_client *client, u8 reg, u8 value, int channel)
8689 + return i2c_smbus_write_byte_data(client, reg | (channel << 6), value);
8692 +static int write_regs(struct i2c_client *client, u8 *regs, int channel)
8696 + for (i = 0; regs[i] != 0xff; i += 2)
8697 + if (i2c_smbus_write_byte_data(client,
8698 + regs[i] | (channel << 6), regs[i + 1]) < 0)
8703 +static int wis_tw2804_command(struct i2c_client *client,
8704 + unsigned int cmd, void *arg)
8706 + struct wis_tw2804 *dec = i2c_get_clientdata(client);
8708 + if (cmd == DECODER_SET_CHANNEL) {
8711 + if (*input < 0 || *input > 3) {
8712 + printk(KERN_ERR "wis-tw2804: channel %d is not "
8713 + "between 0 and 3!\n", *input);
8716 + dec->channel = *input;
8717 + printk(KERN_DEBUG "wis-tw2804: initializing TW2804 "
8718 + "channel %d\n", dec->channel);
8719 + if (dec->channel == 0 &&
8720 + write_regs(client, global_registers, 0) < 0) {
8721 + printk(KERN_ERR "wis-tw2804: error initializing "
8722 + "TW2804 global registers\n");
8725 + if (write_regs(client, channel_registers, dec->channel) < 0) {
8726 + printk(KERN_ERR "wis-tw2804: error initializing "
8727 + "TW2804 channel %d\n", dec->channel);
8733 + if (dec->channel < 0) {
8734 + printk(KERN_DEBUG "wis-tw2804: ignoring command %08x until "
8735 + "channel number is set\n", cmd);
8740 + case DECODER_SET_NORM:
8744 + 0x01, *input == VIDEO_MODE_NTSC ? 0xc4 : 0x84,
8745 + 0x09, *input == VIDEO_MODE_NTSC ? 0x07 : 0x04,
8746 + 0x0a, *input == VIDEO_MODE_NTSC ? 0xf0 : 0x20,
8747 + 0x0b, *input == VIDEO_MODE_NTSC ? 0x07 : 0x04,
8748 + 0x0c, *input == VIDEO_MODE_NTSC ? 0xf0 : 0x20,
8749 + 0x0d, *input == VIDEO_MODE_NTSC ? 0x40 : 0x4a,
8750 + 0x16, *input == VIDEO_MODE_NTSC ? 0x00 : 0x40,
8751 + 0x17, *input == VIDEO_MODE_NTSC ? 0x00 : 0x40,
8752 + 0x20, *input == VIDEO_MODE_NTSC ? 0x07 : 0x0f,
8753 + 0x21, *input == VIDEO_MODE_NTSC ? 0x07 : 0x0f,
8756 + write_regs(client, regs, dec->channel);
8757 + dec->norm = *input;
8760 + case VIDIOC_QUERYCTRL:
8762 + struct v4l2_queryctrl *ctrl = arg;
8764 + switch (ctrl->id) {
8765 + case V4L2_CID_BRIGHTNESS:
8766 + ctrl->type = V4L2_CTRL_TYPE_INTEGER;
8767 + strncpy(ctrl->name, "Brightness", sizeof(ctrl->name));
8768 + ctrl->minimum = 0;
8769 + ctrl->maximum = 255;
8771 + ctrl->default_value = 128;
8774 + case V4L2_CID_CONTRAST:
8775 + ctrl->type = V4L2_CTRL_TYPE_INTEGER;
8776 + strncpy(ctrl->name, "Contrast", sizeof(ctrl->name));
8777 + ctrl->minimum = 0;
8778 + ctrl->maximum = 255;
8780 + ctrl->default_value = 128;
8783 + case V4L2_CID_SATURATION:
8784 + ctrl->type = V4L2_CTRL_TYPE_INTEGER;
8785 + strncpy(ctrl->name, "Saturation", sizeof(ctrl->name));
8786 + ctrl->minimum = 0;
8787 + ctrl->maximum = 255;
8789 + ctrl->default_value = 128;
8792 + case V4L2_CID_HUE:
8793 + ctrl->type = V4L2_CTRL_TYPE_INTEGER;
8794 + strncpy(ctrl->name, "Hue", sizeof(ctrl->name));
8795 + ctrl->minimum = 0;
8796 + ctrl->maximum = 255;
8798 + ctrl->default_value = 128;
8804 + case VIDIOC_S_CTRL:
8806 + struct v4l2_control *ctrl = arg;
8808 + switch (ctrl->id) {
8809 + case V4L2_CID_BRIGHTNESS:
8810 + if (ctrl->value > 255)
8811 + dec->brightness = 255;
8812 + else if (ctrl->value < 0)
8813 + dec->brightness = 0;
8815 + dec->brightness = ctrl->value;
8816 + write_reg(client, 0x12, dec->brightness, dec->channel);
8818 + case V4L2_CID_CONTRAST:
8819 + if (ctrl->value > 255)
8820 + dec->contrast = 255;
8821 + else if (ctrl->value < 0)
8822 + dec->contrast = 0;
8824 + dec->contrast = ctrl->value;
8825 + write_reg(client, 0x11, dec->contrast, dec->channel);
8827 + case V4L2_CID_SATURATION:
8828 + if (ctrl->value > 255)
8829 + dec->saturation = 255;
8830 + else if (ctrl->value < 0)
8831 + dec->saturation = 0;
8833 + dec->saturation = ctrl->value;
8834 + write_reg(client, 0x10, dec->saturation, dec->channel);
8836 + case V4L2_CID_HUE:
8837 + if (ctrl->value > 255)
8839 + else if (ctrl->value < 0)
8842 + dec->hue = ctrl->value;
8843 + write_reg(client, 0x0f, dec->hue, dec->channel);
8848 + case VIDIOC_G_CTRL:
8850 + struct v4l2_control *ctrl = arg;
8852 + switch (ctrl->id) {
8853 + case V4L2_CID_BRIGHTNESS:
8854 + ctrl->value = dec->brightness;
8856 + case V4L2_CID_CONTRAST:
8857 + ctrl->value = dec->contrast;
8859 + case V4L2_CID_SATURATION:
8860 + ctrl->value = dec->saturation;
8862 + case V4L2_CID_HUE:
8863 + ctrl->value = dec->hue;
8874 +static struct i2c_driver wis_tw2804_driver;
8876 +static struct i2c_client wis_tw2804_client_templ = {
8877 + .name = "TW2804 (WIS)",
8878 + .driver = &wis_tw2804_driver,
8881 +static int wis_tw2804_detect(struct i2c_adapter *adapter, int addr, int kind)
8883 + struct i2c_client *client;
8884 + struct wis_tw2804 *dec;
8886 + if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
8889 + client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
8890 + if (client == NULL)
8892 + memcpy(client, &wis_tw2804_client_templ,
8893 + sizeof(wis_tw2804_client_templ));
8894 + client->adapter = adapter;
8895 + client->addr = addr;
8897 + dec = kmalloc(sizeof(struct wis_tw2804), GFP_KERNEL);
8898 + if (dec == NULL) {
8902 + dec->channel = -1;
8903 + dec->norm = VIDEO_MODE_NTSC;
8904 + dec->brightness = 128;
8905 + dec->contrast = 128;
8906 + dec->saturation = 128;
8908 + i2c_set_clientdata(client, dec);
8910 + printk(KERN_DEBUG "wis-tw2804: creating TW2804 at address %d on %s\n",
8911 + addr, adapter->name);
8913 + i2c_attach_client(client);
8917 +static int wis_tw2804_detach(struct i2c_client *client)
8919 + struct wis_tw2804 *dec = i2c_get_clientdata(client);
8922 + r = i2c_detach_client(client);
8931 +static struct i2c_driver wis_tw2804_driver = {
8933 + .name = "WIS TW2804 I2C driver",
8935 + .id = I2C_DRIVERID_WIS_TW2804,
8936 + .detach_client = wis_tw2804_detach,
8937 + .command = wis_tw2804_command,
8940 +static int __init wis_tw2804_init(void)
8944 + r = i2c_add_driver(&wis_tw2804_driver);
8947 + return wis_i2c_add_driver(wis_tw2804_driver.id, wis_tw2804_detect);
8950 +static void __exit wis_tw2804_cleanup(void)
8952 + wis_i2c_del_driver(wis_tw2804_detect);
8953 + i2c_del_driver(&wis_tw2804_driver);
8956 +module_init(wis_tw2804_init);
8957 +module_exit(wis_tw2804_cleanup);
8959 +MODULE_LICENSE("GPL v2");
8960 diff --git a/drivers/staging/go7007/wis-tw9903.c b/drivers/staging/go7007/wis-tw9903.c
8961 new file mode 100644
8962 index 0000000..1cdf01a
8964 +++ b/drivers/staging/go7007/wis-tw9903.c
8967 + * Copyright (C) 2005-2006 Micronas USA Inc.
8969 + * This program is free software; you can redistribute it and/or modify
8970 + * it under the terms of the GNU General Public License (Version 2) as
8971 + * published by the Free Software Foundation.
8973 + * This program is distributed in the hope that it will be useful,
8974 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
8975 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8976 + * GNU General Public License for more details.
8978 + * You should have received a copy of the GNU General Public License
8979 + * along with this program; if not, write to the Free Software Foundation,
8980 + * Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
8983 +#include <linux/module.h>
8984 +#include <linux/init.h>
8985 +#include <linux/version.h>
8986 +#include <linux/i2c.h>
8987 +#include <linux/videodev.h>
8988 +#include <linux/video_decoder.h>
8989 +#include <linux/ioctl.h>
8991 +#include "wis-i2c.h"
8993 +struct wis_tw9903 {
9000 +static u8 initial_registers[] =
9002 + 0x02, 0x44, /* input 1, composite */
9003 + 0x03, 0x92, /* correct digital format */
9005 + 0x05, 0x80, /* or 0x00 for PAL */
9006 + 0x06, 0x40, /* second internal current reference */
9007 + 0x07, 0x02, /* window */
9008 + 0x08, 0x14, /* window */
9009 + 0x09, 0xf0, /* window */
9010 + 0x0a, 0x81, /* window */
9011 + 0x0b, 0xd0, /* window */
9013 + 0x0d, 0x00, /* scaling */
9014 + 0x0e, 0x11, /* scaling */
9015 + 0x0f, 0x00, /* scaling */
9016 + 0x10, 0x00, /* brightness */
9017 + 0x11, 0x60, /* contrast */
9018 + 0x12, 0x01, /* sharpness */
9019 + 0x13, 0x7f, /* U gain */
9020 + 0x14, 0x5a, /* V gain */
9021 + 0x15, 0x00, /* hue */
9022 + 0x16, 0xc3, /* sharpness */
9024 + 0x19, 0x58, /* vbi */
9026 + 0x1c, 0x0f, /* video norm */
9027 + 0x1d, 0x7f, /* video norm */
9028 + 0x20, 0xa0, /* clamping gain (working 0x50) */
9042 + 0x2e, 0xa5, /* burst PLL control (working: a9) */
9043 + 0x2f, 0xe0, /* 0xea is blue test frame -- 0xe0 for normal */
9049 + 0x06, 0xc0, /* reset device */
9050 + 0x00, 0x00, /* Terminator (reg 0x00 is read-only) */
9053 +static int write_reg(struct i2c_client *client, u8 reg, u8 value)
9055 + return i2c_smbus_write_byte_data(client, reg, value);
9058 +static int write_regs(struct i2c_client *client, u8 *regs)
9062 + for (i = 0; regs[i] != 0x00; i += 2)
9063 + if (i2c_smbus_write_byte_data(client, regs[i], regs[i + 1]) < 0)
9068 +static int wis_tw9903_command(struct i2c_client *client,
9069 + unsigned int cmd, void *arg)
9071 + struct wis_tw9903 *dec = i2c_get_clientdata(client);
9074 + case DECODER_SET_INPUT:
9078 + i2c_smbus_write_byte_data(client, 0x02, 0x40 | (*input << 1));
9081 +#if 0 /* The scaler on this thing seems to be horribly broken */
9082 + case DECODER_SET_RESOLUTION:
9084 + struct video_decoder_resolution *res = arg;
9085 + /*int hscale = 256 * 720 / res->width;*/
9086 + int hscale = 256 * 720 / (res->width - (res->width > 704 ? 0 : 8));
9087 + int vscale = 256 * (dec->norm == VIDEO_MODE_NTSC ? 240 : 288)
9090 + 0x0d, vscale & 0xff,
9091 + 0x0f, hscale & 0xff,
9092 + 0x0e, ((vscale & 0xf00) >> 4) | ((hscale & 0xf00) >> 8),
9093 + 0x06, 0xc0, /* reset device */
9096 + printk(KERN_DEBUG "vscale is %04x, hscale is %04x\n",
9098 + /*write_regs(client, regs);*/
9102 + case DECODER_SET_NORM:
9106 + 0x05, *input == VIDEO_MODE_NTSC ? 0x80 : 0x00,
9107 + 0x07, *input == VIDEO_MODE_NTSC ? 0x02 : 0x12,
9108 + 0x08, *input == VIDEO_MODE_NTSC ? 0x14 : 0x18,
9109 + 0x09, *input == VIDEO_MODE_NTSC ? 0xf0 : 0x20,
9112 + write_regs(client, regs);
9113 + dec->norm = *input;
9116 + case VIDIOC_QUERYCTRL:
9118 + struct v4l2_queryctrl *ctrl = arg;
9120 + switch (ctrl->id) {
9121 + case V4L2_CID_BRIGHTNESS:
9122 + ctrl->type = V4L2_CTRL_TYPE_INTEGER;
9123 + strncpy(ctrl->name, "Brightness", sizeof(ctrl->name));
9124 + ctrl->minimum = -128;
9125 + ctrl->maximum = 127;
9127 + ctrl->default_value = 0x00;
9130 + case V4L2_CID_CONTRAST:
9131 + ctrl->type = V4L2_CTRL_TYPE_INTEGER;
9132 + strncpy(ctrl->name, "Contrast", sizeof(ctrl->name));
9133 + ctrl->minimum = 0;
9134 + ctrl->maximum = 255;
9136 + ctrl->default_value = 0x60;
9140 + /* I don't understand how the Chroma Gain registers work... */
9141 + case V4L2_CID_SATURATION:
9142 + ctrl->type = V4L2_CTRL_TYPE_INTEGER;
9143 + strncpy(ctrl->name, "Saturation", sizeof(ctrl->name));
9144 + ctrl->minimum = 0;
9145 + ctrl->maximum = 127;
9147 + ctrl->default_value = 64;
9151 + case V4L2_CID_HUE:
9152 + ctrl->type = V4L2_CTRL_TYPE_INTEGER;
9153 + strncpy(ctrl->name, "Hue", sizeof(ctrl->name));
9154 + ctrl->minimum = -128;
9155 + ctrl->maximum = 127;
9157 + ctrl->default_value = 0;
9163 + case VIDIOC_S_CTRL:
9165 + struct v4l2_control *ctrl = arg;
9167 + switch (ctrl->id) {
9168 + case V4L2_CID_BRIGHTNESS:
9169 + if (ctrl->value > 127)
9170 + dec->brightness = 127;
9171 + else if (ctrl->value < -128)
9172 + dec->brightness = -128;
9174 + dec->brightness = ctrl->value;
9175 + write_reg(client, 0x10, dec->brightness);
9177 + case V4L2_CID_CONTRAST:
9178 + if (ctrl->value > 255)
9179 + dec->contrast = 255;
9180 + else if (ctrl->value < 0)
9181 + dec->contrast = 0;
9183 + dec->contrast = ctrl->value;
9184 + write_reg(client, 0x11, dec->contrast);
9187 + case V4L2_CID_SATURATION:
9188 + if (ctrl->value > 127)
9189 + dec->saturation = 127;
9190 + else if (ctrl->value < 0)
9191 + dec->saturation = 0;
9193 + dec->saturation = ctrl->value;
9194 + /*write_reg(client, 0x0c, dec->saturation);*/
9197 + case V4L2_CID_HUE:
9198 + if (ctrl->value > 127)
9200 + else if (ctrl->value < -128)
9203 + dec->hue = ctrl->value;
9204 + write_reg(client, 0x15, dec->hue);
9209 + case VIDIOC_G_CTRL:
9211 + struct v4l2_control *ctrl = arg;
9213 + switch (ctrl->id) {
9214 + case V4L2_CID_BRIGHTNESS:
9215 + ctrl->value = dec->brightness;
9217 + case V4L2_CID_CONTRAST:
9218 + ctrl->value = dec->contrast;
9221 + case V4L2_CID_SATURATION:
9222 + ctrl->value = dec->saturation;
9225 + case V4L2_CID_HUE:
9226 + ctrl->value = dec->hue;
9237 +static struct i2c_driver wis_tw9903_driver;
9239 +static struct i2c_client wis_tw9903_client_templ = {
9240 + .name = "TW9903 (WIS)",
9241 + .driver = &wis_tw9903_driver,
9244 +static int wis_tw9903_detect(struct i2c_adapter *adapter, int addr, int kind)
9246 + struct i2c_client *client;
9247 + struct wis_tw9903 *dec;
9249 + if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
9252 + client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
9253 + if (client == NULL)
9255 + memcpy(client, &wis_tw9903_client_templ,
9256 + sizeof(wis_tw9903_client_templ));
9257 + client->adapter = adapter;
9258 + client->addr = addr;
9260 + dec = kmalloc(sizeof(struct wis_tw9903), GFP_KERNEL);
9261 + if (dec == NULL) {
9265 + dec->norm = VIDEO_MODE_NTSC;
9266 + dec->brightness = 0;
9267 + dec->contrast = 0x60;
9269 + i2c_set_clientdata(client, dec);
9272 + "wis-tw9903: initializing TW9903 at address %d on %s\n",
9273 + addr, adapter->name);
9275 + if (write_regs(client, initial_registers) < 0) {
9276 + printk(KERN_ERR "wis-tw9903: error initializing TW9903\n");
9282 + i2c_attach_client(client);
9286 +static int wis_tw9903_detach(struct i2c_client *client)
9288 + struct wis_tw9903 *dec = i2c_get_clientdata(client);
9291 + r = i2c_detach_client(client);
9300 +static struct i2c_driver wis_tw9903_driver = {
9302 + .name = "WIS TW9903 I2C driver",
9304 + .id = I2C_DRIVERID_WIS_TW9903,
9305 + .detach_client = wis_tw9903_detach,
9306 + .command = wis_tw9903_command,
9309 +static int __init wis_tw9903_init(void)
9313 + r = i2c_add_driver(&wis_tw9903_driver);
9316 + return wis_i2c_add_driver(wis_tw9903_driver.id, wis_tw9903_detect);
9319 +static void __exit wis_tw9903_cleanup(void)
9321 + wis_i2c_del_driver(wis_tw9903_detect);
9322 + i2c_del_driver(&wis_tw9903_driver);
9325 +module_init(wis_tw9903_init);
9326 +module_exit(wis_tw9903_cleanup);
9328 +MODULE_LICENSE("GPL v2");
9329 diff --git a/drivers/staging/go7007/wis-uda1342.c b/drivers/staging/go7007/wis-uda1342.c
9330 new file mode 100644
9331 index 0000000..28c10bf
9333 +++ b/drivers/staging/go7007/wis-uda1342.c
9336 + * Copyright (C) 2005-2006 Micronas USA Inc.
9338 + * This program is free software; you can redistribute it and/or modify
9339 + * it under the terms of the GNU General Public License (Version 2) as
9340 + * published by the Free Software Foundation.
9342 + * This program is distributed in the hope that it will be useful,
9343 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9344 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9345 + * GNU General Public License for more details.
9347 + * You should have received a copy of the GNU General Public License
9348 + * along with this program; if not, write to the Free Software Foundation,
9349 + * Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
9352 +#include <linux/module.h>
9353 +#include <linux/init.h>
9354 +#include <linux/version.h>
9355 +#include <linux/i2c.h>
9356 +#include <linux/videodev.h>
9357 +#include <media/tvaudio.h>
9358 +#include <media/v4l2-common.h>
9360 +#include "wis-i2c.h"
9362 +static int write_reg(struct i2c_client *client, int reg, int value)
9364 + /* UDA1342 wants MSB first, but SMBus sends LSB first */
9365 + i2c_smbus_write_word_data(client, reg, swab16(value));
9369 +static int wis_uda1342_command(struct i2c_client *client,
9370 + unsigned int cmd, void *arg)
9373 + case VIDIOC_S_AUDIO:
9378 + case TVAUDIO_INPUT_TUNER:
9379 + write_reg(client, 0x00, 0x1441); /* select input 2 */
9381 + case TVAUDIO_INPUT_EXTERN:
9382 + write_reg(client, 0x00, 0x1241); /* select input 1 */
9385 + printk(KERN_ERR "wis-uda1342: input %d not supported\n",
9397 +static struct i2c_driver wis_uda1342_driver;
9399 +static struct i2c_client wis_uda1342_client_templ = {
9400 + .name = "UDA1342 (WIS)",
9401 + .driver = &wis_uda1342_driver,
9404 +static int wis_uda1342_detect(struct i2c_adapter *adapter, int addr, int kind)
9406 + struct i2c_client *client;
9408 + if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA))
9411 + client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
9412 + if (client == NULL)
9414 + memcpy(client, &wis_uda1342_client_templ,
9415 + sizeof(wis_uda1342_client_templ));
9416 + client->adapter = adapter;
9417 + client->addr = addr;
9420 + "wis-uda1342: initializing UDA1342 at address %d on %s\n",
9421 + addr, adapter->name);
9423 + write_reg(client, 0x00, 0x8000); /* reset registers */
9424 + write_reg(client, 0x00, 0x1241); /* select input 1 */
9426 + i2c_attach_client(client);
9430 +static int wis_uda1342_detach(struct i2c_client *client)
9434 + r = i2c_detach_client(client);
9442 +static struct i2c_driver wis_uda1342_driver = {
9444 + .name = "WIS UDA1342 I2C driver",
9446 + .id = I2C_DRIVERID_WIS_UDA1342,
9447 + .detach_client = wis_uda1342_detach,
9448 + .command = wis_uda1342_command,
9451 +static int __init wis_uda1342_init(void)
9455 + r = i2c_add_driver(&wis_uda1342_driver);
9458 + return wis_i2c_add_driver(wis_uda1342_driver.id, wis_uda1342_detect);
9461 +static void __exit wis_uda1342_cleanup(void)
9463 + wis_i2c_del_driver(wis_uda1342_detect);
9464 + i2c_del_driver(&wis_uda1342_driver);
9467 +module_init(wis_uda1342_init);
9468 +module_exit(wis_uda1342_cleanup);
9470 +MODULE_LICENSE("GPL v2");