• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /netgear-R7000-V1.0.7.12_1.2.5/components/opensource/linux/linux-2.6.36/drivers/staging/go7007/
1/*
2 * Copyright (C) 2005-2006 Micronas USA Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License (Version 2) as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software Foundation,
15 * Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
16 */
17
18#include <linux/module.h>
19#include <linux/init.h>
20#include <linux/delay.h>
21#include <linux/sched.h>
22#include <linux/spinlock.h>
23#include <linux/unistd.h>
24#include <linux/time.h>
25#include <linux/mm.h>
26#include <linux/vmalloc.h>
27#include <linux/device.h>
28#include <linux/i2c.h>
29#include <linux/firmware.h>
30#include <linux/mutex.h>
31#include <linux/uaccess.h>
32#include <linux/slab.h>
33#include <asm/system.h>
34#include <linux/videodev2.h>
35#include <media/tuner.h>
36#include <media/v4l2-common.h>
37
38#include "go7007-priv.h"
39#include "wis-i2c.h"
40
41/*
42 * Wait for an interrupt to be delivered from the GO7007SB and return
43 * the associated value and data.
44 *
45 * Must be called with the hw_lock held.
46 */
47int go7007_read_interrupt(struct go7007 *go, u16 *value, u16 *data)
48{
49	go->interrupt_available = 0;
50	go->hpi_ops->read_interrupt(go);
51	if (wait_event_timeout(go->interrupt_waitq,
52				go->interrupt_available, 5*HZ) < 0) {
53		v4l2_err(&go->v4l2_dev, "timeout waiting for read interrupt\n");
54		return -1;
55	}
56	if (!go->interrupt_available)
57		return -1;
58	go->interrupt_available = 0;
59	*value = go->interrupt_value & 0xfffe;
60	*data = go->interrupt_data;
61	return 0;
62}
63EXPORT_SYMBOL(go7007_read_interrupt);
64
65/*
66 * Read a register/address on the GO7007SB.
67 *
68 * Must be called with the hw_lock held.
69 */
70int go7007_read_addr(struct go7007 *go, u16 addr, u16 *data)
71{
72	int count = 100;
73	u16 value;
74
75	if (go7007_write_interrupt(go, 0x0010, addr) < 0)
76		return -EIO;
77	while (count-- > 0) {
78		if (go7007_read_interrupt(go, &value, data) == 0 &&
79				value == 0xa000)
80			return 0;
81	}
82	return -EIO;
83}
84EXPORT_SYMBOL(go7007_read_addr);
85
86/*
87 * Send the boot firmware to the encoder, which just wakes it up and lets
88 * us talk to the GPIO pins and on-board I2C adapter.
89 *
90 * Must be called with the hw_lock held.
91 */
92static int go7007_load_encoder(struct go7007 *go)
93{
94	const struct firmware *fw_entry;
95	char fw_name[] = "go7007fw.bin";
96	void *bounce;
97	int fw_len, rv = 0;
98	u16 intr_val, intr_data;
99
100	if (request_firmware(&fw_entry, fw_name, go->dev)) {
101		v4l2_err(go, "unable to load firmware from file "
102			"\"%s\"\n", fw_name);
103		return -1;
104	}
105	if (fw_entry->size < 16 || memcmp(fw_entry->data, "WISGO7007FW", 11)) {
106		v4l2_err(go, "file \"%s\" does not appear to be "
107				"go7007 firmware\n", fw_name);
108		release_firmware(fw_entry);
109		return -1;
110	}
111	fw_len = fw_entry->size - 16;
112	bounce = kmalloc(fw_len, GFP_KERNEL);
113	if (bounce == NULL) {
114		v4l2_err(go, "unable to allocate %d bytes for "
115				"firmware transfer\n", fw_len);
116		release_firmware(fw_entry);
117		return -1;
118	}
119	memcpy(bounce, fw_entry->data + 16, fw_len);
120	release_firmware(fw_entry);
121	if (go7007_interface_reset(go) < 0 ||
122			go7007_send_firmware(go, bounce, fw_len) < 0 ||
123			go7007_read_interrupt(go, &intr_val, &intr_data) < 0 ||
124			(intr_val & ~0x1) != 0x5a5a) {
125		v4l2_err(go, "error transferring firmware\n");
126		rv = -1;
127	}
128	kfree(bounce);
129	return rv;
130}
131
132MODULE_FIRMWARE("go7007fw.bin");
133
134/*
135 * Boot the encoder and register the I2C adapter if requested.  Do the
136 * minimum initialization necessary, since the board-specific code may
137 * still need to probe the board ID.
138 *
139 * Must NOT be called with the hw_lock held.
140 */
141int go7007_boot_encoder(struct go7007 *go, int init_i2c)
142{
143	int ret;
144
145	mutex_lock(&go->hw_lock);
146	ret = go7007_load_encoder(go);
147	mutex_unlock(&go->hw_lock);
148	if (ret < 0)
149		return -1;
150	if (!init_i2c)
151		return 0;
152	if (go7007_i2c_init(go) < 0)
153		return -1;
154	go->i2c_adapter_online = 1;
155	return 0;
156}
157EXPORT_SYMBOL(go7007_boot_encoder);
158
159/*
160 * Configure any hardware-related registers in the GO7007, such as GPIO
161 * pins and bus parameters, which are board-specific.  This assumes
162 * the boot firmware has already been downloaded.
163 *
164 * Must be called with the hw_lock held.
165 */
166static int go7007_init_encoder(struct go7007 *go)
167{
168	if (go->board_info->audio_flags & GO7007_AUDIO_I2S_MASTER) {
169		go7007_write_addr(go, 0x1000, 0x0811);
170		go7007_write_addr(go, 0x1000, 0x0c11);
171	}
172	if (go->board_id == GO7007_BOARDID_MATRIX_REV) {
173		/* Set GPIO pin 0 to be an output (audio clock control) */
174		go7007_write_addr(go, 0x3c82, 0x0001);
175		go7007_write_addr(go, 0x3c80, 0x00fe);
176	}
177	return 0;
178}
179
180/*
181 * Send the boot firmware to the GO7007 and configure the registers.  This
182 * is the only way to stop the encoder once it has started streaming video.
183 *
184 * Must be called with the hw_lock held.
185 */
186int go7007_reset_encoder(struct go7007 *go)
187{
188	if (go7007_load_encoder(go) < 0)
189		return -1;
190	return go7007_init_encoder(go);
191}
192
193/*
194 * Attempt to instantiate an I2C client by ID, probably loading a module.
195 */
196static int init_i2c_module(struct i2c_adapter *adapter, const char *type,
197			   int id, int addr)
198{
199	struct go7007 *go = i2c_get_adapdata(adapter);
200	struct v4l2_device *v4l2_dev = &go->v4l2_dev;
201	char *modname;
202
203	switch (id) {
204	case I2C_DRIVERID_WIS_SAA7115:
205		modname = "wis-saa7115";
206		break;
207	case I2C_DRIVERID_WIS_SAA7113:
208		modname = "wis-saa7113";
209		break;
210	case I2C_DRIVERID_WIS_UDA1342:
211		modname = "wis-uda1342";
212		break;
213	case I2C_DRIVERID_WIS_SONY_TUNER:
214		modname = "wis-sony-tuner";
215		break;
216	case I2C_DRIVERID_WIS_TW9903:
217		modname = "wis-tw9903";
218		break;
219	case I2C_DRIVERID_WIS_TW2804:
220		modname = "wis-tw2804";
221		break;
222	case I2C_DRIVERID_WIS_OV7640:
223		modname = "wis-ov7640";
224		break;
225	case I2C_DRIVERID_S2250:
226		modname = "s2250";
227		break;
228	default:
229		modname = NULL;
230		break;
231	}
232
233	if (v4l2_i2c_new_subdev(v4l2_dev, adapter, modname, type, addr, NULL))
234		return 0;
235
236	if (modname != NULL)
237		printk(KERN_INFO
238			"go7007: probing for module %s failed\n", modname);
239	else
240		printk(KERN_INFO
241			"go7007: sensor %u seems to be unsupported!\n", id);
242	return -1;
243}
244
245/*
246 * Finalize the GO7007 hardware setup, register the on-board I2C adapter
247 * (if used on this board), load the I2C client driver for the sensor
248 * (SAA7115 or whatever) and other devices, and register the ALSA and V4L2
249 * interfaces.
250 *
251 * Must NOT be called with the hw_lock held.
252 */
253int go7007_register_encoder(struct go7007 *go)
254{
255	int i, ret;
256
257	printk(KERN_INFO "go7007: registering new %s\n", go->name);
258
259	mutex_lock(&go->hw_lock);
260	ret = go7007_init_encoder(go);
261	mutex_unlock(&go->hw_lock);
262	if (ret < 0)
263		return -1;
264
265	/* v4l2 init must happen before i2c subdevs */
266	ret = go7007_v4l2_init(go);
267	if (ret < 0)
268		return ret;
269
270	if (!go->i2c_adapter_online &&
271			go->board_info->flags & GO7007_BOARD_USE_ONBOARD_I2C) {
272		if (go7007_i2c_init(go) < 0)
273			return -1;
274		go->i2c_adapter_online = 1;
275	}
276	if (go->i2c_adapter_online) {
277		for (i = 0; i < go->board_info->num_i2c_devs; ++i)
278			init_i2c_module(&go->i2c_adapter,
279					go->board_info->i2c_devs[i].type,
280					go->board_info->i2c_devs[i].id,
281					go->board_info->i2c_devs[i].addr);
282		if (go->board_id == GO7007_BOARDID_ADLINK_MPG24)
283			i2c_clients_command(&go->i2c_adapter,
284				DECODER_SET_CHANNEL, &go->channel_number);
285	}
286	if (go->board_info->flags & GO7007_BOARD_HAS_AUDIO) {
287		go->audio_enabled = 1;
288		go7007_snd_init(go);
289	}
290	return 0;
291}
292EXPORT_SYMBOL(go7007_register_encoder);
293
294/*
295 * Send the encode firmware to the encoder, which will cause it
296 * to immediately start delivering the video and audio streams.
297 *
298 * Must be called with the hw_lock held.
299 */
300int go7007_start_encoder(struct go7007 *go)
301{
302	u8 *fw;
303	int fw_len, rv = 0, i;
304	u16 intr_val, intr_data;
305
306	go->modet_enable = 0;
307	if (!go->dvd_mode)
308		for (i = 0; i < 4; ++i) {
309			if (go->modet[i].enable) {
310				go->modet_enable = 1;
311				continue;
312			}
313			go->modet[i].pixel_threshold = 32767;
314			go->modet[i].motion_threshold = 32767;
315			go->modet[i].mb_threshold = 32767;
316		}
317
318	if (go7007_construct_fw_image(go, &fw, &fw_len) < 0)
319		return -1;
320
321	if (go7007_send_firmware(go, fw, fw_len) < 0 ||
322			go7007_read_interrupt(go, &intr_val, &intr_data) < 0) {
323		v4l2_err(&go->v4l2_dev, "error transferring firmware\n");
324		rv = -1;
325		goto start_error;
326	}
327
328	go->state = STATE_DATA;
329	go->parse_length = 0;
330	go->seen_frame = 0;
331	if (go7007_stream_start(go) < 0) {
332		v4l2_err(&go->v4l2_dev, "error starting stream transfer\n");
333		rv = -1;
334		goto start_error;
335	}
336
337start_error:
338	kfree(fw);
339	return rv;
340}
341
342/*
343 * Store a byte in the current video buffer, if there is one.
344 */
345static inline void store_byte(struct go7007_buffer *gobuf, u8 byte)
346{
347	if (gobuf != NULL && gobuf->bytesused < GO7007_BUF_SIZE) {
348		unsigned int pgidx = gobuf->offset >> PAGE_SHIFT;
349		unsigned int pgoff = gobuf->offset & ~PAGE_MASK;
350
351		*((u8 *)page_address(gobuf->pages[pgidx]) + pgoff) = byte;
352		++gobuf->offset;
353		++gobuf->bytesused;
354	}
355}
356
357/*
358 * Deliver the last video buffer and get a new one to start writing to.
359 */
360static void frame_boundary(struct go7007 *go)
361{
362	struct go7007_buffer *gobuf;
363	int i;
364
365	if (go->active_buf) {
366		if (go->active_buf->modet_active) {
367			if (go->active_buf->bytesused + 216 < GO7007_BUF_SIZE) {
368				for (i = 0; i < 216; ++i)
369					store_byte(go->active_buf,
370							go->active_map[i]);
371				go->active_buf->bytesused -= 216;
372			} else
373				go->active_buf->modet_active = 0;
374		}
375		go->active_buf->state = BUF_STATE_DONE;
376		wake_up_interruptible(&go->frame_waitq);
377		go->active_buf = NULL;
378	}
379	list_for_each_entry(gobuf, &go->stream, stream)
380		if (gobuf->state == BUF_STATE_QUEUED) {
381			gobuf->seq = go->next_seq;
382			do_gettimeofday(&gobuf->timestamp);
383			go->active_buf = gobuf;
384			break;
385		}
386	++go->next_seq;
387}
388
389static void write_bitmap_word(struct go7007 *go)
390{
391	int x, y, i, stride = ((go->width >> 4) + 7) >> 3;
392
393	for (i = 0; i < 16; ++i) {
394		y = (((go->parse_length - 1) << 3) + i) / (go->width >> 4);
395		x = (((go->parse_length - 1) << 3) + i) % (go->width >> 4);
396		go->active_map[stride * y + (x >> 3)] |=
397					(go->modet_word & 1) << (x & 0x7);
398		go->modet_word >>= 1;
399	}
400}
401
402/*
403 * Parse a chunk of the video stream into frames.  The frames are not
404 * delimited by the hardware, so we have to parse the frame boundaries
405 * based on the type of video stream we're receiving.
406 */
407void go7007_parse_video_stream(struct go7007 *go, u8 *buf, int length)
408{
409	int i, seq_start_code = -1, frame_start_code = -1;
410
411	spin_lock(&go->spinlock);
412
413	switch (go->format) {
414	case GO7007_FORMAT_MPEG4:
415		seq_start_code = 0xB0;
416		frame_start_code = 0xB6;
417		break;
418	case GO7007_FORMAT_MPEG1:
419	case GO7007_FORMAT_MPEG2:
420		seq_start_code = 0xB3;
421		frame_start_code = 0x00;
422		break;
423	}
424
425	for (i = 0; i < length; ++i) {
426		if (go->active_buf != NULL &&
427			    go->active_buf->bytesused >= GO7007_BUF_SIZE - 3) {
428			v4l2_info(&go->v4l2_dev, "dropping oversized frame\n");
429			go->active_buf->offset -= go->active_buf->bytesused;
430			go->active_buf->bytesused = 0;
431			go->active_buf->modet_active = 0;
432			go->active_buf = NULL;
433		}
434
435		switch (go->state) {
436		case STATE_DATA:
437			switch (buf[i]) {
438			case 0x00:
439				go->state = STATE_00;
440				break;
441			case 0xFF:
442				go->state = STATE_FF;
443				break;
444			default:
445				store_byte(go->active_buf, buf[i]);
446				break;
447			}
448			break;
449		case STATE_00:
450			switch (buf[i]) {
451			case 0x00:
452				go->state = STATE_00_00;
453				break;
454			case 0xFF:
455				store_byte(go->active_buf, 0x00);
456				go->state = STATE_FF;
457				break;
458			default:
459				store_byte(go->active_buf, 0x00);
460				store_byte(go->active_buf, buf[i]);
461				go->state = STATE_DATA;
462				break;
463			}
464			break;
465		case STATE_00_00:
466			switch (buf[i]) {
467			case 0x00:
468				store_byte(go->active_buf, 0x00);
469				/* go->state remains STATE_00_00 */
470				break;
471			case 0x01:
472				go->state = STATE_00_00_01;
473				break;
474			case 0xFF:
475				store_byte(go->active_buf, 0x00);
476				store_byte(go->active_buf, 0x00);
477				go->state = STATE_FF;
478				break;
479			default:
480				store_byte(go->active_buf, 0x00);
481				store_byte(go->active_buf, 0x00);
482				store_byte(go->active_buf, buf[i]);
483				go->state = STATE_DATA;
484				break;
485			}
486			break;
487		case STATE_00_00_01:
488			/* If this is the start of a new MPEG frame,
489			 * get a new buffer */
490			if ((go->format == GO7007_FORMAT_MPEG1 ||
491					go->format == GO7007_FORMAT_MPEG2 ||
492					go->format == GO7007_FORMAT_MPEG4) &&
493					(buf[i] == seq_start_code ||
494						buf[i] == 0xB8 || /* GOP code */
495						buf[i] == frame_start_code)) {
496				if (go->active_buf == NULL || go->seen_frame)
497					frame_boundary(go);
498				if (buf[i] == frame_start_code) {
499					if (go->active_buf != NULL)
500						go->active_buf->frame_offset =
501							go->active_buf->offset;
502					go->seen_frame = 1;
503				} else {
504					go->seen_frame = 0;
505				}
506			}
507			/* Handle any special chunk types, or just write the
508			 * start code to the (potentially new) buffer */
509			switch (buf[i]) {
510			case 0xF5: /* timestamp */
511				go->parse_length = 12;
512				go->state = STATE_UNPARSED;
513				break;
514			case 0xF6: /* vbi */
515				go->state = STATE_VBI_LEN_A;
516				break;
517			case 0xF8: /* MD map */
518				go->parse_length = 0;
519				memset(go->active_map, 0,
520						sizeof(go->active_map));
521				go->state = STATE_MODET_MAP;
522				break;
523			case 0xFF: /* Potential JPEG start code */
524				store_byte(go->active_buf, 0x00);
525				store_byte(go->active_buf, 0x00);
526				store_byte(go->active_buf, 0x01);
527				go->state = STATE_FF;
528				break;
529			default:
530				store_byte(go->active_buf, 0x00);
531				store_byte(go->active_buf, 0x00);
532				store_byte(go->active_buf, 0x01);
533				store_byte(go->active_buf, buf[i]);
534				go->state = STATE_DATA;
535				break;
536			}
537			break;
538		case STATE_FF:
539			switch (buf[i]) {
540			case 0x00:
541				store_byte(go->active_buf, 0xFF);
542				go->state = STATE_00;
543				break;
544			case 0xFF:
545				store_byte(go->active_buf, 0xFF);
546				/* go->state remains STATE_FF */
547				break;
548			case 0xD8:
549				if (go->format == GO7007_FORMAT_MJPEG)
550					frame_boundary(go);
551				/* fall through */
552			default:
553				store_byte(go->active_buf, 0xFF);
554				store_byte(go->active_buf, buf[i]);
555				go->state = STATE_DATA;
556				break;
557			}
558			break;
559		case STATE_VBI_LEN_A:
560			go->parse_length = buf[i] << 8;
561			go->state = STATE_VBI_LEN_B;
562			break;
563		case STATE_VBI_LEN_B:
564			go->parse_length |= buf[i];
565			if (go->parse_length > 0)
566				go->state = STATE_UNPARSED;
567			else
568				go->state = STATE_DATA;
569			break;
570		case STATE_MODET_MAP:
571			if (go->parse_length < 204) {
572				if (go->parse_length & 1) {
573					go->modet_word |= buf[i];
574					write_bitmap_word(go);
575				} else
576					go->modet_word = buf[i] << 8;
577			} else if (go->parse_length == 207 && go->active_buf) {
578				go->active_buf->modet_active = buf[i];
579			}
580			if (++go->parse_length == 208)
581				go->state = STATE_DATA;
582			break;
583		case STATE_UNPARSED:
584			if (--go->parse_length == 0)
585				go->state = STATE_DATA;
586			break;
587		}
588	}
589
590	spin_unlock(&go->spinlock);
591}
592EXPORT_SYMBOL(go7007_parse_video_stream);
593
594/*
595 * Allocate a new go7007 struct.  Used by the hardware-specific probe.
596 */
597struct go7007 *go7007_alloc(struct go7007_board_info *board, struct device *dev)
598{
599	struct go7007 *go;
600	int i;
601
602	go = kmalloc(sizeof(struct go7007), GFP_KERNEL);
603	if (go == NULL)
604		return NULL;
605	go->dev = dev;
606	go->board_info = board;
607	go->board_id = 0;
608	go->tuner_type = -1;
609	go->channel_number = 0;
610	go->name[0] = 0;
611	mutex_init(&go->hw_lock);
612	init_waitqueue_head(&go->frame_waitq);
613	spin_lock_init(&go->spinlock);
614	go->video_dev = NULL;
615	go->ref_count = 0;
616	go->status = STATUS_INIT;
617	memset(&go->i2c_adapter, 0, sizeof(go->i2c_adapter));
618	go->i2c_adapter_online = 0;
619	go->interrupt_available = 0;
620	init_waitqueue_head(&go->interrupt_waitq);
621	go->in_use = 0;
622	go->input = 0;
623	if (board->sensor_flags & GO7007_SENSOR_TV) {
624		go->standard = GO7007_STD_NTSC;
625		go->width = 720;
626		go->height = 480;
627		go->sensor_framerate = 30000;
628	} else {
629		go->standard = GO7007_STD_OTHER;
630		go->width = board->sensor_width;
631		go->height = board->sensor_height;
632		go->sensor_framerate = board->sensor_framerate;
633	}
634	go->encoder_v_offset = board->sensor_v_offset;
635	go->encoder_h_offset = board->sensor_h_offset;
636	go->encoder_h_halve = 0;
637	go->encoder_v_halve = 0;
638	go->encoder_subsample = 0;
639	go->streaming = 0;
640	go->format = GO7007_FORMAT_MJPEG;
641	go->bitrate = 1500000;
642	go->fps_scale = 1;
643	go->pali = 0;
644	go->aspect_ratio = GO7007_RATIO_1_1;
645	go->gop_size = 0;
646	go->ipb = 0;
647	go->closed_gop = 0;
648	go->repeat_seqhead = 0;
649	go->seq_header_enable = 0;
650	go->gop_header_enable = 0;
651	go->dvd_mode = 0;
652	go->interlace_coding = 0;
653	for (i = 0; i < 4; ++i)
654		go->modet[i].enable = 0;;
655	for (i = 0; i < 1624; ++i)
656		go->modet_map[i] = 0;
657	go->audio_deliver = NULL;
658	go->audio_enabled = 0;
659	INIT_LIST_HEAD(&go->stream);
660
661	return go;
662}
663EXPORT_SYMBOL(go7007_alloc);
664
665/*
666 * Detach and unregister the encoder.  The go7007 struct won't be freed
667 * until v4l2 finishes releasing its resources and all associated fds are
668 * closed by applications.
669 */
670void go7007_remove(struct go7007 *go)
671{
672	if (go->i2c_adapter_online) {
673		if (i2c_del_adapter(&go->i2c_adapter) == 0)
674			go->i2c_adapter_online = 0;
675		else
676			v4l2_err(&go->v4l2_dev,
677				"error removing I2C adapter!\n");
678	}
679
680	if (go->audio_enabled)
681		go7007_snd_remove(go);
682	go7007_v4l2_remove(go);
683}
684EXPORT_SYMBOL(go7007_remove);
685
686MODULE_LICENSE("GPL v2");
687