Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Copyright (C) 2005-2006 Micronas USA Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/vmalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/firmware.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/videodev2.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <media/tuner.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <media/v4l2-common.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <media/v4l2-event.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include "go7007-priv.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  * Wait for an interrupt to be delivered from the GO7007SB and return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  * the associated value and data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  * Must be called with the hw_lock held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) int go7007_read_interrupt(struct go7007 *go, u16 *value, u16 *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	go->interrupt_available = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	go->hpi_ops->read_interrupt(go);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	if (wait_event_timeout(go->interrupt_waitq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 				go->interrupt_available, 5*HZ) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 		v4l2_err(&go->v4l2_dev, "timeout waiting for read interrupt\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	if (!go->interrupt_available)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	go->interrupt_available = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	*value = go->interrupt_value & 0xfffe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	*data = go->interrupt_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) EXPORT_SYMBOL(go7007_read_interrupt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52)  * Read a register/address on the GO7007SB.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54)  * Must be called with the hw_lock held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) int go7007_read_addr(struct go7007 *go, u16 addr, u16 *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	int count = 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	u16 value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	if (go7007_write_interrupt(go, 0x0010, addr) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	while (count-- > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		if (go7007_read_interrupt(go, &value, data) == 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 				value == 0xa000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) EXPORT_SYMBOL(go7007_read_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73)  * Send the boot firmware to the encoder, which just wakes it up and lets
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74)  * us talk to the GPIO pins and on-board I2C adapter.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76)  * Must be called with the hw_lock held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) static int go7007_load_encoder(struct go7007 *go)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	const struct firmware *fw_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	char fw_name[] = "go7007/go7007fw.bin";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	void *bounce;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	int fw_len, rv = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	u16 intr_val, intr_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	if (go->boot_fw == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		if (request_firmware(&fw_entry, fw_name, go->dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 			v4l2_err(go, "unable to load firmware from file \"%s\"\n", fw_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		if (fw_entry->size < 16 || memcmp(fw_entry->data, "WISGO7007FW", 11)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 			v4l2_err(go, "file \"%s\" does not appear to be go7007 firmware\n", fw_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 			release_firmware(fw_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		fw_len = fw_entry->size - 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		bounce = kmemdup(fw_entry->data + 16, fw_len, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		if (bounce == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 			v4l2_err(go, "unable to allocate %d bytes for firmware transfer\n", fw_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 			release_firmware(fw_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		release_firmware(fw_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		go->boot_fw_len = fw_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		go->boot_fw = bounce;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	if (go7007_interface_reset(go) < 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	    go7007_send_firmware(go, go->boot_fw, go->boot_fw_len) < 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	    go7007_read_interrupt(go, &intr_val, &intr_data) < 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 			(intr_val & ~0x1) != 0x5a5a) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		v4l2_err(go, "error transferring firmware\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		rv = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) MODULE_FIRMWARE("go7007/go7007fw.bin");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)  * Boot the encoder and register the I2C adapter if requested.  Do the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)  * minimum initialization necessary, since the board-specific code may
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)  * still need to probe the board ID.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)  * Must NOT be called with the hw_lock held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) int go7007_boot_encoder(struct go7007 *go, int init_i2c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	mutex_lock(&go->hw_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	ret = go7007_load_encoder(go);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	mutex_unlock(&go->hw_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	if (!init_i2c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	if (go7007_i2c_init(go) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	go->i2c_adapter_online = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) EXPORT_SYMBOL(go7007_boot_encoder);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)  * Configure any hardware-related registers in the GO7007, such as GPIO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)  * pins and bus parameters, which are board-specific.  This assumes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)  * the boot firmware has already been downloaded.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)  * Must be called with the hw_lock held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) static int go7007_init_encoder(struct go7007 *go)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	if (go->board_info->audio_flags & GO7007_AUDIO_I2S_MASTER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		go7007_write_addr(go, 0x1000, 0x0811);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		go7007_write_addr(go, 0x1000, 0x0c11);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	switch (go->board_id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	case GO7007_BOARDID_MATRIX_REV:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		/* Set GPIO pin 0 to be an output (audio clock control) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		go7007_write_addr(go, 0x3c82, 0x0001);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 		go7007_write_addr(go, 0x3c80, 0x00fe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	case GO7007_BOARDID_ADLINK_MPG24:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		/* set GPIO5 to be an output, currently low */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		go7007_write_addr(go, 0x3c82, 0x0000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 		go7007_write_addr(go, 0x3c80, 0x00df);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	case GO7007_BOARDID_ADS_USBAV_709:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		/* GPIO pin 0: audio clock control */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		/*      pin 2: TW9906 reset */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		/*      pin 3: capture LED */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		go7007_write_addr(go, 0x3c82, 0x000d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		go7007_write_addr(go, 0x3c80, 0x00f2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)  * Send the boot firmware to the GO7007 and configure the registers.  This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)  * is the only way to stop the encoder once it has started streaming video.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)  * Must be called with the hw_lock held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) int go7007_reset_encoder(struct go7007 *go)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	if (go7007_load_encoder(go) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	return go7007_init_encoder(go);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)  * Attempt to instantiate an I2C client by ID, probably loading a module.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) static int init_i2c_module(struct i2c_adapter *adapter, const struct go_i2c *const i2c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	struct go7007 *go = i2c_get_adapdata(adapter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	struct v4l2_device *v4l2_dev = &go->v4l2_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	struct v4l2_subdev *sd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	struct i2c_board_info info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	memset(&info, 0, sizeof(info));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	strscpy(info.type, i2c->type, sizeof(info.type));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	info.addr = i2c->addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	info.flags = i2c->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	sd = v4l2_i2c_new_subdev_board(v4l2_dev, adapter, &info, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	if (sd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		if (i2c->is_video)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 			go->sd_video = sd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		if (i2c->is_audio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 			go->sd_audio = sd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	pr_info("go7007: probing for module i2c:%s failed\n", i2c->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)  * Detach and unregister the encoder.  The go7007 struct won't be freed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)  * until v4l2 finishes releasing its resources and all associated fds are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)  * closed by applications.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) static void go7007_remove(struct v4l2_device *v4l2_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	struct go7007 *go = container_of(v4l2_dev, struct go7007, v4l2_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	v4l2_device_unregister(v4l2_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	if (go->hpi_ops->release)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 		go->hpi_ops->release(go);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	if (go->i2c_adapter_online) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 		i2c_del_adapter(&go->i2c_adapter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		go->i2c_adapter_online = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	kfree(go->boot_fw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	go7007_v4l2_remove(go);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	kfree(go);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)  * Finalize the GO7007 hardware setup, register the on-board I2C adapter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)  * (if used on this board), load the I2C client driver for the sensor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)  * (SAA7115 or whatever) and other devices, and register the ALSA and V4L2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)  * interfaces.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)  * Must NOT be called with the hw_lock held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) int go7007_register_encoder(struct go7007 *go, unsigned num_i2c_devs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	int i, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	dev_info(go->dev, "go7007: registering new %s\n", go->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	go->v4l2_dev.release = go7007_remove;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	ret = v4l2_device_register(go->dev, &go->v4l2_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	mutex_lock(&go->hw_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	ret = go7007_init_encoder(go);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	mutex_unlock(&go->hw_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	ret = go7007_v4l2_ctrl_init(go);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	if (!go->i2c_adapter_online &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 			go->board_info->flags & GO7007_BOARD_USE_ONBOARD_I2C) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 		ret = go7007_i2c_init(go);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 		go->i2c_adapter_online = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	if (go->i2c_adapter_online) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 		if (go->board_id == GO7007_BOARDID_ADS_USBAV_709) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 			/* Reset the TW9906 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 			go7007_write_addr(go, 0x3c82, 0x0009);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 			msleep(50);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 			go7007_write_addr(go, 0x3c82, 0x000d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 		for (i = 0; i < num_i2c_devs; ++i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 			init_i2c_module(&go->i2c_adapter, &go->board_info->i2c_devs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		if (go->tuner_type >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 			struct tuner_setup setup = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 				.addr = ADDR_UNSET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 				.type = go->tuner_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 				.mode_mask = T_ANALOG_TV,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 			};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 			v4l2_device_call_all(&go->v4l2_dev, 0, tuner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 				s_type_addr, &setup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 		if (go->board_id == GO7007_BOARDID_ADLINK_MPG24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 			v4l2_subdev_call(go->sd_video, video, s_routing,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 					0, 0, go->channel_number + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	ret = go7007_v4l2_init(go);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	if (go->board_info->flags & GO7007_BOARD_HAS_AUDIO) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 		go->audio_enabled = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 		go7007_snd_init(go);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) EXPORT_SYMBOL(go7007_register_encoder);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)  * Send the encode firmware to the encoder, which will cause it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)  * to immediately start delivering the video and audio streams.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319)  * Must be called with the hw_lock held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) int go7007_start_encoder(struct go7007 *go)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	u8 *fw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	int fw_len, rv = 0, i, x, y;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	u16 intr_val, intr_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	go->modet_enable = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	for (i = 0; i < 4; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 		go->modet[i].enable = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	switch (v4l2_ctrl_g_ctrl(go->modet_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	case V4L2_DETECT_MD_MODE_GLOBAL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 		memset(go->modet_map, 0, sizeof(go->modet_map));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 		go->modet[0].enable = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 		go->modet_enable = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	case V4L2_DETECT_MD_MODE_REGION_GRID:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 		for (y = 0; y < go->height / 16; y++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 			for (x = 0; x < go->width / 16; x++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 				int idx = y * go->width / 16 + x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 				go->modet[go->modet_map[idx]].enable = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 		go->modet_enable = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	if (go->dvd_mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 		go->modet_enable = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	if (go7007_construct_fw_image(go, &fw, &fw_len) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	if (go7007_send_firmware(go, fw, fw_len) < 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 			go7007_read_interrupt(go, &intr_val, &intr_data) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 		v4l2_err(&go->v4l2_dev, "error transferring firmware\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 		rv = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 		goto start_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	go->state = STATE_DATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	go->parse_length = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	go->seen_frame = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	if (go7007_stream_start(go) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 		v4l2_err(&go->v4l2_dev, "error starting stream transfer\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 		rv = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 		goto start_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) start_error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	kfree(fw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)  * Store a byte in the current video buffer, if there is one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) static inline void store_byte(struct go7007_buffer *vb, u8 byte)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	if (vb && vb->vb.vb2_buf.planes[0].bytesused < GO7007_BUF_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 		u8 *ptr = vb2_plane_vaddr(&vb->vb.vb2_buf, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 		ptr[vb->vb.vb2_buf.planes[0].bytesused++] = byte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) static void go7007_set_motion_regions(struct go7007 *go, struct go7007_buffer *vb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 		u32 motion_regions)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	if (motion_regions != go->modet_event_status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 		struct v4l2_event ev = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 			.type = V4L2_EVENT_MOTION_DET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 			.u.motion_det = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 				.flags = V4L2_EVENT_MD_FL_HAVE_FRAME_SEQ,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 				.frame_sequence = vb->vb.sequence,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 				.region_mask = motion_regions,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 			},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 		};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 		v4l2_event_queue(&go->vdev, &ev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 		go->modet_event_status = motion_regions;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407)  * Determine regions with motion and send a motion detection event
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408)  * in case of changes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) static void go7007_motion_regions(struct go7007 *go, struct go7007_buffer *vb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	u32 *bytesused = &vb->vb.vb2_buf.planes[0].bytesused;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	unsigned motion[4] = { 0, 0, 0, 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	u32 motion_regions = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	unsigned stride = (go->width + 7) >> 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	unsigned x, y;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	for (i = 0; i < 216; ++i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 		store_byte(vb, go->active_map[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	for (y = 0; y < go->height / 16; y++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 		for (x = 0; x < go->width / 16; x++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 			if (!(go->active_map[y * stride + (x >> 3)] & (1 << (x & 7))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 			motion[go->modet_map[y * (go->width / 16) + x]]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	motion_regions = ((motion[0] > 0) << 0) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 			 ((motion[1] > 0) << 1) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 			 ((motion[2] > 0) << 2) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 			 ((motion[3] > 0) << 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	*bytesused -= 216;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	go7007_set_motion_regions(go, vb, motion_regions);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437)  * Deliver the last video buffer and get a new one to start writing to.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) static struct go7007_buffer *frame_boundary(struct go7007 *go, struct go7007_buffer *vb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	u32 *bytesused;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	struct go7007_buffer *vb_tmp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	if (vb == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 		spin_lock_irqsave(&go->spinlock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 		if (!list_empty(&go->vidq_active))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 			vb = go->active_buf =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 				list_first_entry(&go->vidq_active, struct go7007_buffer, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 		spin_unlock_irqrestore(&go->spinlock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 		go->next_seq++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 		return vb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	bytesused = &vb->vb.vb2_buf.planes[0].bytesused;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	vb->vb.sequence = go->next_seq++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	if (vb->modet_active && *bytesused + 216 < GO7007_BUF_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 		go7007_motion_regions(go, vb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 		go7007_set_motion_regions(go, vb, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	vb->vb.vb2_buf.timestamp = ktime_get_ns();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	vb_tmp = vb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	spin_lock_irqsave(&go->spinlock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	list_del(&vb->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	if (list_empty(&go->vidq_active))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 		vb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 		vb = list_first_entry(&go->vidq_active,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 				struct go7007_buffer, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	go->active_buf = vb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	spin_unlock_irqrestore(&go->spinlock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 	vb2_buffer_done(&vb_tmp->vb.vb2_buf, VB2_BUF_STATE_DONE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	return vb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) static void write_bitmap_word(struct go7007 *go)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	int x, y, i, stride = ((go->width >> 4) + 7) >> 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	for (i = 0; i < 16; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 		y = (((go->parse_length - 1) << 3) + i) / (go->width >> 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 		x = (((go->parse_length - 1) << 3) + i) % (go->width >> 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 		if (stride * y + (x >> 3) < sizeof(go->active_map))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 			go->active_map[stride * y + (x >> 3)] |=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 					(go->modet_word & 1) << (x & 0x7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 		go->modet_word >>= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492)  * Parse a chunk of the video stream into frames.  The frames are not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493)  * delimited by the hardware, so we have to parse the frame boundaries
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494)  * based on the type of video stream we're receiving.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) void go7007_parse_video_stream(struct go7007 *go, u8 *buf, int length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	struct go7007_buffer *vb = go->active_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	int i, seq_start_code = -1, gop_start_code = -1, frame_start_code = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	switch (go->format) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 	case V4L2_PIX_FMT_MPEG4:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 		seq_start_code = 0xB0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 		gop_start_code = 0xB3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 		frame_start_code = 0xB6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 	case V4L2_PIX_FMT_MPEG1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	case V4L2_PIX_FMT_MPEG2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 		seq_start_code = 0xB3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 		gop_start_code = 0xB8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 		frame_start_code = 0x00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 	for (i = 0; i < length; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 		if (vb && vb->vb.vb2_buf.planes[0].bytesused >=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 				GO7007_BUF_SIZE - 3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 			v4l2_info(&go->v4l2_dev, "dropping oversized frame\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 			vb->vb.vb2_buf.planes[0].bytesused = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 			vb->frame_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 			vb->modet_active = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 			vb = go->active_buf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 		switch (go->state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 		case STATE_DATA:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 			switch (buf[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 			case 0x00:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 				go->state = STATE_00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 			case 0xFF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 				go->state = STATE_FF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 			default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 				store_byte(vb, buf[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 		case STATE_00:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 			switch (buf[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 			case 0x00:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 				go->state = STATE_00_00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 			case 0xFF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 				store_byte(vb, 0x00);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 				go->state = STATE_FF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 			default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 				store_byte(vb, 0x00);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 				store_byte(vb, buf[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 				go->state = STATE_DATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 		case STATE_00_00:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 			switch (buf[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 			case 0x00:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 				store_byte(vb, 0x00);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 				/* go->state remains STATE_00_00 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 			case 0x01:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 				go->state = STATE_00_00_01;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 			case 0xFF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 				store_byte(vb, 0x00);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 				store_byte(vb, 0x00);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 				go->state = STATE_FF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 			default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 				store_byte(vb, 0x00);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 				store_byte(vb, 0x00);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 				store_byte(vb, buf[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 				go->state = STATE_DATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 		case STATE_00_00_01:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 			if (buf[i] == 0xF8 && go->modet_enable == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 				/* MODET start code, but MODET not enabled */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 				store_byte(vb, 0x00);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 				store_byte(vb, 0x00);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 				store_byte(vb, 0x01);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 				store_byte(vb, 0xF8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 				go->state = STATE_DATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 			/* If this is the start of a new MPEG frame,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 			 * get a new buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 			if ((go->format == V4L2_PIX_FMT_MPEG1 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 			     go->format == V4L2_PIX_FMT_MPEG2 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 			     go->format == V4L2_PIX_FMT_MPEG4) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 			    (buf[i] == seq_start_code ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 			     buf[i] == gop_start_code ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 			     buf[i] == frame_start_code)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 				if (vb == NULL || go->seen_frame)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 					vb = frame_boundary(go, vb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 				go->seen_frame = buf[i] == frame_start_code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 				if (vb && go->seen_frame)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 					vb->frame_offset =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 					vb->vb.vb2_buf.planes[0].bytesused;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 			/* Handle any special chunk types, or just write the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 			 * start code to the (potentially new) buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 			switch (buf[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 			case 0xF5: /* timestamp */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 				go->parse_length = 12;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 				go->state = STATE_UNPARSED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 			case 0xF6: /* vbi */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 				go->state = STATE_VBI_LEN_A;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 			case 0xF8: /* MD map */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 				go->parse_length = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 				memset(go->active_map, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 						sizeof(go->active_map));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 				go->state = STATE_MODET_MAP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 			case 0xFF: /* Potential JPEG start code */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 				store_byte(vb, 0x00);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 				store_byte(vb, 0x00);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 				store_byte(vb, 0x01);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 				go->state = STATE_FF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 			default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 				store_byte(vb, 0x00);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 				store_byte(vb, 0x00);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 				store_byte(vb, 0x01);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 				store_byte(vb, buf[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 				go->state = STATE_DATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 		case STATE_FF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 			switch (buf[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 			case 0x00:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 				store_byte(vb, 0xFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 				go->state = STATE_00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 			case 0xFF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 				store_byte(vb, 0xFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 				/* go->state remains STATE_FF */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 			case 0xD8:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 				if (go->format == V4L2_PIX_FMT_MJPEG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) 					vb = frame_boundary(go, vb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) 				fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 			default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) 				store_byte(vb, 0xFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) 				store_byte(vb, buf[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 				go->state = STATE_DATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 		case STATE_VBI_LEN_A:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 			go->parse_length = buf[i] << 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 			go->state = STATE_VBI_LEN_B;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 		case STATE_VBI_LEN_B:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 			go->parse_length |= buf[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) 			if (go->parse_length > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) 				go->state = STATE_UNPARSED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 				go->state = STATE_DATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) 		case STATE_MODET_MAP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) 			if (go->parse_length < 204) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) 				if (go->parse_length & 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) 					go->modet_word |= buf[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 					write_bitmap_word(go);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 				} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 					go->modet_word = buf[i] << 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) 			} else if (go->parse_length == 207 && vb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 				vb->modet_active = buf[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 			if (++go->parse_length == 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) 				go->state = STATE_DATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) 		case STATE_UNPARSED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 			if (--go->parse_length == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) 				go->state = STATE_DATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) EXPORT_SYMBOL(go7007_parse_video_stream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688)  * Allocate a new go7007 struct.  Used by the hardware-specific probe.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) struct go7007 *go7007_alloc(const struct go7007_board_info *board,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) 						struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) 	struct go7007 *go;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) 	go = kzalloc(sizeof(struct go7007), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) 	if (go == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) 	go->dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) 	go->board_info = board;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) 	go->tuner_type = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) 	mutex_init(&go->hw_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) 	init_waitqueue_head(&go->frame_waitq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) 	spin_lock_init(&go->spinlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) 	go->status = STATUS_INIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) 	init_waitqueue_head(&go->interrupt_waitq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) 	go7007_update_board(go);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) 	go->format = V4L2_PIX_FMT_MJPEG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) 	go->bitrate = 1500000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) 	go->fps_scale = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) 	go->aspect_ratio = GO7007_RATIO_1_1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) 	return go;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) EXPORT_SYMBOL(go7007_alloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) void go7007_update_board(struct go7007 *go)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) 	const struct go7007_board_info *board = go->board_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) 	if (board->sensor_flags & GO7007_SENSOR_TV) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) 		go->standard = GO7007_STD_NTSC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) 		go->std = V4L2_STD_NTSC_M;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) 		go->width = 720;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) 		go->height = 480;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) 		go->sensor_framerate = 30000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) 		go->standard = GO7007_STD_OTHER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) 		go->width = board->sensor_width;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) 		go->height = board->sensor_height;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) 		go->sensor_framerate = board->sensor_framerate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) 	go->encoder_v_offset = board->sensor_v_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) 	go->encoder_h_offset = board->sensor_h_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) EXPORT_SYMBOL(go7007_update_board);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) MODULE_LICENSE("GPL v2");