1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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 <linux/videodev2.h>
34#include <media/tuner.h>
35#include <media/v4l2-common.h>
36
37#include "go7007-priv.h"
38
39
40
41
42
43
44
45int go7007_read_interrupt(struct go7007 *go, u16 *value, u16 *data)
46{
47 go->interrupt_available = 0;
48 go->hpi_ops->read_interrupt(go);
49 if (wait_event_timeout(go->interrupt_waitq,
50 go->interrupt_available, 5*HZ) < 0) {
51 v4l2_err(&go->v4l2_dev, "timeout waiting for read interrupt\n");
52 return -1;
53 }
54 if (!go->interrupt_available)
55 return -1;
56 go->interrupt_available = 0;
57 *value = go->interrupt_value & 0xfffe;
58 *data = go->interrupt_data;
59 return 0;
60}
61EXPORT_SYMBOL(go7007_read_interrupt);
62
63
64
65
66
67
68int go7007_read_addr(struct go7007 *go, u16 addr, u16 *data)
69{
70 int count = 100;
71 u16 value;
72
73 if (go7007_write_interrupt(go, 0x0010, addr) < 0)
74 return -EIO;
75 while (count-- > 0) {
76 if (go7007_read_interrupt(go, &value, data) == 0 &&
77 value == 0xa000)
78 return 0;
79 }
80 return -EIO;
81}
82EXPORT_SYMBOL(go7007_read_addr);
83
84
85
86
87
88
89
90static int go7007_load_encoder(struct go7007 *go)
91{
92 const struct firmware *fw_entry;
93 char fw_name[] = "go7007/go7007fw.bin";
94 void *bounce;
95 int fw_len, rv = 0;
96 u16 intr_val, intr_data;
97
98 if (go->boot_fw == NULL) {
99 if (request_firmware(&fw_entry, fw_name, go->dev)) {
100 v4l2_err(go, "unable to load firmware from file \"%s\"\n", fw_name);
101 return -1;
102 }
103 if (fw_entry->size < 16 || memcmp(fw_entry->data, "WISGO7007FW", 11)) {
104 v4l2_err(go, "file \"%s\" does not appear to be go7007 firmware\n", fw_name);
105 release_firmware(fw_entry);
106 return -1;
107 }
108 fw_len = fw_entry->size - 16;
109 bounce = kmemdup(fw_entry->data + 16, fw_len, GFP_KERNEL);
110 if (bounce == NULL) {
111 v4l2_err(go, "unable to allocate %d bytes for firmware transfer\n", fw_len);
112 release_firmware(fw_entry);
113 return -1;
114 }
115 release_firmware(fw_entry);
116 go->boot_fw_len = fw_len;
117 go->boot_fw = bounce;
118 }
119 if (go7007_interface_reset(go) < 0 ||
120 go7007_send_firmware(go, go->boot_fw, go->boot_fw_len) < 0 ||
121 go7007_read_interrupt(go, &intr_val, &intr_data) < 0 ||
122 (intr_val & ~0x1) != 0x5a5a) {
123 v4l2_err(go, "error transferring firmware\n");
124 rv = -1;
125 }
126 return rv;
127}
128
129MODULE_FIRMWARE("go7007/go7007fw.bin");
130
131
132
133
134
135
136
137
138int go7007_boot_encoder(struct go7007 *go, int init_i2c)
139{
140 int ret;
141
142 mutex_lock(&go->hw_lock);
143 ret = go7007_load_encoder(go);
144 mutex_unlock(&go->hw_lock);
145 if (ret < 0)
146 return -1;
147 if (!init_i2c)
148 return 0;
149 if (go7007_i2c_init(go) < 0)
150 return -1;
151 go->i2c_adapter_online = 1;
152 return 0;
153}
154EXPORT_SYMBOL(go7007_boot_encoder);
155
156
157
158
159
160
161
162
163static int go7007_init_encoder(struct go7007 *go)
164{
165 if (go->board_info->audio_flags & GO7007_AUDIO_I2S_MASTER) {
166 go7007_write_addr(go, 0x1000, 0x0811);
167 go7007_write_addr(go, 0x1000, 0x0c11);
168 }
169 switch (go->board_id) {
170 case GO7007_BOARDID_MATRIX_REV:
171
172 go7007_write_addr(go, 0x3c82, 0x0001);
173 go7007_write_addr(go, 0x3c80, 0x00fe);
174 break;
175 case GO7007_BOARDID_ADLINK_MPG24:
176
177 go7007_write_addr(go, 0x3c82, 0x0000);
178 go7007_write_addr(go, 0x3c80, 0x00df);
179 break;
180 case GO7007_BOARDID_ADS_USBAV_709:
181
182
183
184 go7007_write_addr(go, 0x3c82, 0x000d);
185 go7007_write_addr(go, 0x3c80, 0x00f2);
186 break;
187 }
188 return 0;
189}
190
191
192
193
194
195
196
197int go7007_reset_encoder(struct go7007 *go)
198{
199 if (go7007_load_encoder(go) < 0)
200 return -1;
201 return go7007_init_encoder(go);
202}
203
204
205
206
207static int init_i2c_module(struct i2c_adapter *adapter, const struct go_i2c *const i2c)
208{
209 struct go7007 *go = i2c_get_adapdata(adapter);
210 struct v4l2_device *v4l2_dev = &go->v4l2_dev;
211 struct v4l2_subdev *sd;
212 struct i2c_board_info info;
213
214 memset(&info, 0, sizeof(info));
215 strlcpy(info.type, i2c->type, sizeof(info.type));
216 info.addr = i2c->addr;
217 info.flags = i2c->flags;
218
219 sd = v4l2_i2c_new_subdev_board(v4l2_dev, adapter, &info, NULL);
220 if (sd) {
221 if (i2c->is_video)
222 go->sd_video = sd;
223 if (i2c->is_audio)
224 go->sd_audio = sd;
225 return 0;
226 }
227
228 printk(KERN_INFO "go7007: probing for module i2c:%s failed\n", i2c->type);
229 return -EINVAL;
230}
231
232
233
234
235
236
237static void go7007_remove(struct v4l2_device *v4l2_dev)
238{
239 struct go7007 *go = container_of(v4l2_dev, struct go7007, v4l2_dev);
240
241 v4l2_device_unregister(v4l2_dev);
242 if (go->hpi_ops->release)
243 go->hpi_ops->release(go);
244 if (go->i2c_adapter_online) {
245 i2c_del_adapter(&go->i2c_adapter);
246 go->i2c_adapter_online = 0;
247 }
248
249 kfree(go->boot_fw);
250 go7007_v4l2_remove(go);
251 kfree(go);
252}
253
254
255
256
257
258
259
260
261
262int go7007_register_encoder(struct go7007 *go, unsigned num_i2c_devs)
263{
264 int i, ret;
265
266 dev_info(go->dev, "go7007: registering new %s\n", go->name);
267
268 go->v4l2_dev.release = go7007_remove;
269 ret = v4l2_device_register(go->dev, &go->v4l2_dev);
270 if (ret < 0)
271 return ret;
272
273 mutex_lock(&go->hw_lock);
274 ret = go7007_init_encoder(go);
275 mutex_unlock(&go->hw_lock);
276 if (ret < 0)
277 return ret;
278
279 ret = go7007_v4l2_ctrl_init(go);
280 if (ret < 0)
281 return ret;
282
283 if (!go->i2c_adapter_online &&
284 go->board_info->flags & GO7007_BOARD_USE_ONBOARD_I2C) {
285 ret = go7007_i2c_init(go);
286 if (ret < 0)
287 return ret;
288 go->i2c_adapter_online = 1;
289 }
290 if (go->i2c_adapter_online) {
291 if (go->board_id == GO7007_BOARDID_ADS_USBAV_709) {
292
293 go7007_write_addr(go, 0x3c82, 0x0009);
294 msleep(50);
295 go7007_write_addr(go, 0x3c82, 0x000d);
296 }
297 for (i = 0; i < num_i2c_devs; ++i)
298 init_i2c_module(&go->i2c_adapter, &go->board_info->i2c_devs[i]);
299
300 if (go->tuner_type >= 0) {
301 struct tuner_setup setup = {
302 .addr = ADDR_UNSET,
303 .type = go->tuner_type,
304 .mode_mask = T_ANALOG_TV,
305 };
306
307 v4l2_device_call_all(&go->v4l2_dev, 0, tuner,
308 s_type_addr, &setup);
309 }
310 if (go->board_id == GO7007_BOARDID_ADLINK_MPG24)
311 v4l2_subdev_call(go->sd_video, video, s_routing,
312 0, 0, go->channel_number + 1);
313 }
314
315 ret = go7007_v4l2_init(go);
316 if (ret < 0)
317 return ret;
318
319 if (go->board_info->flags & GO7007_BOARD_HAS_AUDIO) {
320 go->audio_enabled = 1;
321 go7007_snd_init(go);
322 }
323 return 0;
324}
325EXPORT_SYMBOL(go7007_register_encoder);
326
327
328
329
330
331
332
333int go7007_start_encoder(struct go7007 *go)
334{
335 u8 *fw;
336 int fw_len, rv = 0, i;
337 u16 intr_val, intr_data;
338
339 go->modet_enable = 0;
340 if (!go->dvd_mode)
341 for (i = 0; i < 4; ++i) {
342 if (go->modet[i].enable) {
343 go->modet_enable = 1;
344 continue;
345 }
346 go->modet[i].pixel_threshold = 32767;
347 go->modet[i].motion_threshold = 32767;
348 go->modet[i].mb_threshold = 32767;
349 }
350
351 if (go7007_construct_fw_image(go, &fw, &fw_len) < 0)
352 return -1;
353
354 if (go7007_send_firmware(go, fw, fw_len) < 0 ||
355 go7007_read_interrupt(go, &intr_val, &intr_data) < 0) {
356 v4l2_err(&go->v4l2_dev, "error transferring firmware\n");
357 rv = -1;
358 goto start_error;
359 }
360
361 go->state = STATE_DATA;
362 go->parse_length = 0;
363 go->seen_frame = 0;
364 if (go7007_stream_start(go) < 0) {
365 v4l2_err(&go->v4l2_dev, "error starting stream transfer\n");
366 rv = -1;
367 goto start_error;
368 }
369
370start_error:
371 kfree(fw);
372 return rv;
373}
374
375
376
377
378static inline void store_byte(struct go7007_buffer *vb, u8 byte)
379{
380 if (vb && vb->vb.v4l2_planes[0].bytesused < GO7007_BUF_SIZE) {
381 u8 *ptr = vb2_plane_vaddr(&vb->vb, 0);
382
383 ptr[vb->vb.v4l2_planes[0].bytesused++] = byte;
384 }
385}
386
387
388
389
390static struct go7007_buffer *frame_boundary(struct go7007 *go, struct go7007_buffer *vb)
391{
392 struct go7007_buffer *vb_tmp = NULL;
393 u32 *bytesused = &vb->vb.v4l2_planes[0].bytesused;
394 int i;
395
396 if (vb) {
397 if (vb->modet_active) {
398 if (*bytesused + 216 < GO7007_BUF_SIZE) {
399 for (i = 0; i < 216; ++i)
400 store_byte(vb, go->active_map[i]);
401 *bytesused -= 216;
402 } else
403 vb->modet_active = 0;
404 }
405 vb->vb.v4l2_buf.sequence = go->next_seq++;
406 v4l2_get_timestamp(&vb->vb.v4l2_buf.timestamp);
407 vb_tmp = vb;
408 spin_lock(&go->spinlock);
409 list_del(&vb->list);
410 if (list_empty(&go->vidq_active))
411 vb = NULL;
412 else
413 vb = list_first_entry(&go->vidq_active, struct go7007_buffer, list);
414 go->active_buf = vb;
415 spin_unlock(&go->spinlock);
416 vb2_buffer_done(&vb_tmp->vb, VB2_BUF_STATE_DONE);
417 return vb;
418 }
419 spin_lock(&go->spinlock);
420 if (!list_empty(&go->vidq_active))
421 vb = go->active_buf =
422 list_first_entry(&go->vidq_active, struct go7007_buffer, list);
423 spin_unlock(&go->spinlock);
424 go->next_seq++;
425 return vb;
426}
427
428static void write_bitmap_word(struct go7007 *go)
429{
430 int x, y, i, stride = ((go->width >> 4) + 7) >> 3;
431
432 for (i = 0; i < 16; ++i) {
433 y = (((go->parse_length - 1) << 3) + i) / (go->width >> 4);
434 x = (((go->parse_length - 1) << 3) + i) % (go->width >> 4);
435 if (stride * y + (x >> 3) < sizeof(go->active_map))
436 go->active_map[stride * y + (x >> 3)] |=
437 (go->modet_word & 1) << (x & 0x7);
438 go->modet_word >>= 1;
439 }
440}
441
442
443
444
445
446
447void go7007_parse_video_stream(struct go7007 *go, u8 *buf, int length)
448{
449 struct go7007_buffer *vb = go->active_buf;
450 int i, seq_start_code = -1, gop_start_code = -1, frame_start_code = -1;
451
452 switch (go->format) {
453 case V4L2_PIX_FMT_MPEG4:
454 seq_start_code = 0xB0;
455 gop_start_code = 0xB3;
456 frame_start_code = 0xB6;
457 break;
458 case V4L2_PIX_FMT_MPEG1:
459 case V4L2_PIX_FMT_MPEG2:
460 seq_start_code = 0xB3;
461 gop_start_code = 0xB8;
462 frame_start_code = 0x00;
463 break;
464 }
465
466 for (i = 0; i < length; ++i) {
467 if (vb && vb->vb.v4l2_planes[0].bytesused >= GO7007_BUF_SIZE - 3) {
468 v4l2_info(&go->v4l2_dev, "dropping oversized frame\n");
469 vb->vb.v4l2_planes[0].bytesused = 0;
470 vb->frame_offset = 0;
471 vb->modet_active = 0;
472 vb = go->active_buf = NULL;
473 }
474
475 switch (go->state) {
476 case STATE_DATA:
477 switch (buf[i]) {
478 case 0x00:
479 go->state = STATE_00;
480 break;
481 case 0xFF:
482 go->state = STATE_FF;
483 break;
484 default:
485 store_byte(vb, buf[i]);
486 break;
487 }
488 break;
489 case STATE_00:
490 switch (buf[i]) {
491 case 0x00:
492 go->state = STATE_00_00;
493 break;
494 case 0xFF:
495 store_byte(vb, 0x00);
496 go->state = STATE_FF;
497 break;
498 default:
499 store_byte(vb, 0x00);
500 store_byte(vb, buf[i]);
501 go->state = STATE_DATA;
502 break;
503 }
504 break;
505 case STATE_00_00:
506 switch (buf[i]) {
507 case 0x00:
508 store_byte(vb, 0x00);
509
510 break;
511 case 0x01:
512 go->state = STATE_00_00_01;
513 break;
514 case 0xFF:
515 store_byte(vb, 0x00);
516 store_byte(vb, 0x00);
517 go->state = STATE_FF;
518 break;
519 default:
520 store_byte(vb, 0x00);
521 store_byte(vb, 0x00);
522 store_byte(vb, buf[i]);
523 go->state = STATE_DATA;
524 break;
525 }
526 break;
527 case STATE_00_00_01:
528 if (buf[i] == 0xF8 && go->modet_enable == 0) {
529
530 store_byte(vb, 0x00);
531 store_byte(vb, 0x00);
532 store_byte(vb, 0x01);
533 store_byte(vb, 0xF8);
534 go->state = STATE_DATA;
535 break;
536 }
537
538
539 if ((go->format == V4L2_PIX_FMT_MPEG1 ||
540 go->format == V4L2_PIX_FMT_MPEG2 ||
541 go->format == V4L2_PIX_FMT_MPEG4) &&
542 (buf[i] == seq_start_code ||
543 buf[i] == gop_start_code ||
544 buf[i] == frame_start_code)) {
545 if (vb == NULL || go->seen_frame)
546 vb = frame_boundary(go, vb);
547 go->seen_frame = buf[i] == frame_start_code;
548 if (vb && go->seen_frame)
549 vb->frame_offset = vb->vb.v4l2_planes[0].bytesused;
550 }
551
552
553 switch (buf[i]) {
554 case 0xF5:
555 go->parse_length = 12;
556 go->state = STATE_UNPARSED;
557 break;
558 case 0xF6:
559 go->state = STATE_VBI_LEN_A;
560 break;
561 case 0xF8:
562 go->parse_length = 0;
563 memset(go->active_map, 0,
564 sizeof(go->active_map));
565 go->state = STATE_MODET_MAP;
566 break;
567 case 0xFF:
568 store_byte(vb, 0x00);
569 store_byte(vb, 0x00);
570 store_byte(vb, 0x01);
571 go->state = STATE_FF;
572 break;
573 default:
574 store_byte(vb, 0x00);
575 store_byte(vb, 0x00);
576 store_byte(vb, 0x01);
577 store_byte(vb, buf[i]);
578 go->state = STATE_DATA;
579 break;
580 }
581 break;
582 case STATE_FF:
583 switch (buf[i]) {
584 case 0x00:
585 store_byte(vb, 0xFF);
586 go->state = STATE_00;
587 break;
588 case 0xFF:
589 store_byte(vb, 0xFF);
590
591 break;
592 case 0xD8:
593 if (go->format == V4L2_PIX_FMT_MJPEG)
594 vb = frame_boundary(go, vb);
595
596 default:
597 store_byte(vb, 0xFF);
598 store_byte(vb, buf[i]);
599 go->state = STATE_DATA;
600 break;
601 }
602 break;
603 case STATE_VBI_LEN_A:
604 go->parse_length = buf[i] << 8;
605 go->state = STATE_VBI_LEN_B;
606 break;
607 case STATE_VBI_LEN_B:
608 go->parse_length |= buf[i];
609 if (go->parse_length > 0)
610 go->state = STATE_UNPARSED;
611 else
612 go->state = STATE_DATA;
613 break;
614 case STATE_MODET_MAP:
615 if (go->parse_length < 204) {
616 if (go->parse_length & 1) {
617 go->modet_word |= buf[i];
618 write_bitmap_word(go);
619 } else
620 go->modet_word = buf[i] << 8;
621 } else if (go->parse_length == 207 && vb) {
622 vb->modet_active = buf[i];
623 }
624 if (++go->parse_length == 208)
625 go->state = STATE_DATA;
626 break;
627 case STATE_UNPARSED:
628 if (--go->parse_length == 0)
629 go->state = STATE_DATA;
630 break;
631 }
632 }
633}
634EXPORT_SYMBOL(go7007_parse_video_stream);
635
636
637
638
639struct go7007 *go7007_alloc(const struct go7007_board_info *board,
640 struct device *dev)
641{
642 struct go7007 *go;
643 int i;
644
645 go = kzalloc(sizeof(struct go7007), GFP_KERNEL);
646 if (go == NULL)
647 return NULL;
648 go->dev = dev;
649 go->board_info = board;
650 go->board_id = 0;
651 go->tuner_type = -1;
652 go->channel_number = 0;
653 go->name[0] = 0;
654 mutex_init(&go->hw_lock);
655 init_waitqueue_head(&go->frame_waitq);
656 spin_lock_init(&go->spinlock);
657 go->status = STATUS_INIT;
658 memset(&go->i2c_adapter, 0, sizeof(go->i2c_adapter));
659 go->i2c_adapter_online = 0;
660 go->interrupt_available = 0;
661 init_waitqueue_head(&go->interrupt_waitq);
662 go->input = 0;
663 go7007_update_board(go);
664 go->encoder_h_halve = 0;
665 go->encoder_v_halve = 0;
666 go->encoder_subsample = 0;
667 go->format = V4L2_PIX_FMT_MJPEG;
668 go->bitrate = 1500000;
669 go->fps_scale = 1;
670 go->pali = 0;
671 go->aspect_ratio = GO7007_RATIO_1_1;
672 go->gop_size = 0;
673 go->ipb = 0;
674 go->closed_gop = 0;
675 go->repeat_seqhead = 0;
676 go->seq_header_enable = 0;
677 go->gop_header_enable = 0;
678 go->dvd_mode = 0;
679 go->interlace_coding = 0;
680 for (i = 0; i < 4; ++i)
681 go->modet[i].enable = 0;
682 for (i = 0; i < 1624; ++i)
683 go->modet_map[i] = 0;
684 go->audio_deliver = NULL;
685 go->audio_enabled = 0;
686
687 return go;
688}
689EXPORT_SYMBOL(go7007_alloc);
690
691void go7007_update_board(struct go7007 *go)
692{
693 const struct go7007_board_info *board = go->board_info;
694
695 if (board->sensor_flags & GO7007_SENSOR_TV) {
696 go->standard = GO7007_STD_NTSC;
697 go->std = V4L2_STD_NTSC_M;
698 go->width = 720;
699 go->height = 480;
700 go->sensor_framerate = 30000;
701 } else {
702 go->standard = GO7007_STD_OTHER;
703 go->width = board->sensor_width;
704 go->height = board->sensor_height;
705 go->sensor_framerate = board->sensor_framerate;
706 }
707 go->encoder_v_offset = board->sensor_v_offset;
708 go->encoder_h_offset = board->sensor_h_offset;
709}
710EXPORT_SYMBOL(go7007_update_board);
711
712MODULE_LICENSE("GPL v2");
713