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