Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (C) 2005-2006 Micronas USA Inc.
0004  */
0005 
0006 #include <linux/module.h>
0007 #include <linux/delay.h>
0008 #include <linux/sched.h>
0009 #include <linux/spinlock.h>
0010 #include <linux/unistd.h>
0011 #include <linux/time.h>
0012 #include <linux/mm.h>
0013 #include <linux/vmalloc.h>
0014 #include <linux/device.h>
0015 #include <linux/i2c.h>
0016 #include <linux/firmware.h>
0017 #include <linux/mutex.h>
0018 #include <linux/uaccess.h>
0019 #include <linux/slab.h>
0020 #include <linux/videodev2.h>
0021 #include <media/tuner.h>
0022 #include <media/v4l2-common.h>
0023 #include <media/v4l2-event.h>
0024 
0025 #include "go7007-priv.h"
0026 
0027 /*
0028  * Wait for an interrupt to be delivered from the GO7007SB and return
0029  * the associated value and data.
0030  *
0031  * Must be called with the hw_lock held.
0032  */
0033 int go7007_read_interrupt(struct go7007 *go, u16 *value, u16 *data)
0034 {
0035     go->interrupt_available = 0;
0036     go->hpi_ops->read_interrupt(go);
0037     if (wait_event_timeout(go->interrupt_waitq,
0038                 go->interrupt_available, 5*HZ) < 0) {
0039         v4l2_err(&go->v4l2_dev, "timeout waiting for read interrupt\n");
0040         return -1;
0041     }
0042     if (!go->interrupt_available)
0043         return -1;
0044     go->interrupt_available = 0;
0045     *value = go->interrupt_value & 0xfffe;
0046     *data = go->interrupt_data;
0047     return 0;
0048 }
0049 EXPORT_SYMBOL(go7007_read_interrupt);
0050 
0051 /*
0052  * Read a register/address on the GO7007SB.
0053  *
0054  * Must be called with the hw_lock held.
0055  */
0056 int go7007_read_addr(struct go7007 *go, u16 addr, u16 *data)
0057 {
0058     int count = 100;
0059     u16 value;
0060 
0061     if (go7007_write_interrupt(go, 0x0010, addr) < 0)
0062         return -EIO;
0063     while (count-- > 0) {
0064         if (go7007_read_interrupt(go, &value, data) == 0 &&
0065                 value == 0xa000)
0066             return 0;
0067     }
0068     return -EIO;
0069 }
0070 EXPORT_SYMBOL(go7007_read_addr);
0071 
0072 /*
0073  * Send the boot firmware to the encoder, which just wakes it up and lets
0074  * us talk to the GPIO pins and on-board I2C adapter.
0075  *
0076  * Must be called with the hw_lock held.
0077  */
0078 static int go7007_load_encoder(struct go7007 *go)
0079 {
0080     const struct firmware *fw_entry;
0081     char fw_name[] = "go7007/go7007fw.bin";
0082     void *bounce;
0083     int fw_len, rv = 0;
0084     u16 intr_val, intr_data;
0085 
0086     if (go->boot_fw == NULL) {
0087         if (request_firmware(&fw_entry, fw_name, go->dev)) {
0088             v4l2_err(go, "unable to load firmware from file \"%s\"\n", fw_name);
0089             return -1;
0090         }
0091         if (fw_entry->size < 16 || memcmp(fw_entry->data, "WISGO7007FW", 11)) {
0092             v4l2_err(go, "file \"%s\" does not appear to be go7007 firmware\n", fw_name);
0093             release_firmware(fw_entry);
0094             return -1;
0095         }
0096         fw_len = fw_entry->size - 16;
0097         bounce = kmemdup(fw_entry->data + 16, fw_len, GFP_KERNEL);
0098         if (bounce == NULL) {
0099             v4l2_err(go, "unable to allocate %d bytes for firmware transfer\n", fw_len);
0100             release_firmware(fw_entry);
0101             return -1;
0102         }
0103         release_firmware(fw_entry);
0104         go->boot_fw_len = fw_len;
0105         go->boot_fw = bounce;
0106     }
0107     if (go7007_interface_reset(go) < 0 ||
0108         go7007_send_firmware(go, go->boot_fw, go->boot_fw_len) < 0 ||
0109         go7007_read_interrupt(go, &intr_val, &intr_data) < 0 ||
0110             (intr_val & ~0x1) != 0x5a5a) {
0111         v4l2_err(go, "error transferring firmware\n");
0112         rv = -1;
0113     }
0114     return rv;
0115 }
0116 
0117 MODULE_FIRMWARE("go7007/go7007fw.bin");
0118 
0119 /*
0120  * Boot the encoder and register the I2C adapter if requested.  Do the
0121  * minimum initialization necessary, since the board-specific code may
0122  * still need to probe the board ID.
0123  *
0124  * Must NOT be called with the hw_lock held.
0125  */
0126 int go7007_boot_encoder(struct go7007 *go, int init_i2c)
0127 {
0128     int ret;
0129 
0130     mutex_lock(&go->hw_lock);
0131     ret = go7007_load_encoder(go);
0132     mutex_unlock(&go->hw_lock);
0133     if (ret < 0)
0134         return -1;
0135     if (!init_i2c)
0136         return 0;
0137     if (go7007_i2c_init(go) < 0)
0138         return -1;
0139     go->i2c_adapter_online = 1;
0140     return 0;
0141 }
0142 EXPORT_SYMBOL(go7007_boot_encoder);
0143 
0144 /*
0145  * Configure any hardware-related registers in the GO7007, such as GPIO
0146  * pins and bus parameters, which are board-specific.  This assumes
0147  * the boot firmware has already been downloaded.
0148  *
0149  * Must be called with the hw_lock held.
0150  */
0151 static int go7007_init_encoder(struct go7007 *go)
0152 {
0153     if (go->board_info->audio_flags & GO7007_AUDIO_I2S_MASTER) {
0154         go7007_write_addr(go, 0x1000, 0x0811);
0155         go7007_write_addr(go, 0x1000, 0x0c11);
0156     }
0157     switch (go->board_id) {
0158     case GO7007_BOARDID_MATRIX_REV:
0159         /* Set GPIO pin 0 to be an output (audio clock control) */
0160         go7007_write_addr(go, 0x3c82, 0x0001);
0161         go7007_write_addr(go, 0x3c80, 0x00fe);
0162         break;
0163     case GO7007_BOARDID_ADLINK_MPG24:
0164         /* set GPIO5 to be an output, currently low */
0165         go7007_write_addr(go, 0x3c82, 0x0000);
0166         go7007_write_addr(go, 0x3c80, 0x00df);
0167         break;
0168     case GO7007_BOARDID_ADS_USBAV_709:
0169         /* GPIO pin 0: audio clock control */
0170         /*      pin 2: TW9906 reset */
0171         /*      pin 3: capture LED */
0172         go7007_write_addr(go, 0x3c82, 0x000d);
0173         go7007_write_addr(go, 0x3c80, 0x00f2);
0174         break;
0175     }
0176     return 0;
0177 }
0178 
0179 /*
0180  * Send the boot firmware to the GO7007 and configure the registers.  This
0181  * is the only way to stop the encoder once it has started streaming video.
0182  *
0183  * Must be called with the hw_lock held.
0184  */
0185 int go7007_reset_encoder(struct go7007 *go)
0186 {
0187     if (go7007_load_encoder(go) < 0)
0188         return -1;
0189     return go7007_init_encoder(go);
0190 }
0191 
0192 /*
0193  * Attempt to instantiate an I2C client by ID, probably loading a module.
0194  */
0195 static int init_i2c_module(struct i2c_adapter *adapter, const struct go_i2c *const i2c)
0196 {
0197     struct go7007 *go = i2c_get_adapdata(adapter);
0198     struct v4l2_device *v4l2_dev = &go->v4l2_dev;
0199     struct v4l2_subdev *sd;
0200     struct i2c_board_info info;
0201 
0202     memset(&info, 0, sizeof(info));
0203     strscpy(info.type, i2c->type, sizeof(info.type));
0204     info.addr = i2c->addr;
0205     info.flags = i2c->flags;
0206 
0207     sd = v4l2_i2c_new_subdev_board(v4l2_dev, adapter, &info, NULL);
0208     if (sd) {
0209         if (i2c->is_video)
0210             go->sd_video = sd;
0211         if (i2c->is_audio)
0212             go->sd_audio = sd;
0213         return 0;
0214     }
0215 
0216     pr_info("go7007: probing for module i2c:%s failed\n", i2c->type);
0217     return -EINVAL;
0218 }
0219 
0220 /*
0221  * Detach and unregister the encoder.  The go7007 struct won't be freed
0222  * until v4l2 finishes releasing its resources and all associated fds are
0223  * closed by applications.
0224  */
0225 static void go7007_remove(struct v4l2_device *v4l2_dev)
0226 {
0227     struct go7007 *go = container_of(v4l2_dev, struct go7007, v4l2_dev);
0228 
0229     v4l2_device_unregister(v4l2_dev);
0230     if (go->hpi_ops->release)
0231         go->hpi_ops->release(go);
0232     if (go->i2c_adapter_online) {
0233         i2c_del_adapter(&go->i2c_adapter);
0234         go->i2c_adapter_online = 0;
0235     }
0236 
0237     kfree(go->boot_fw);
0238     go7007_v4l2_remove(go);
0239     kfree(go);
0240 }
0241 
0242 /*
0243  * Finalize the GO7007 hardware setup, register the on-board I2C adapter
0244  * (if used on this board), load the I2C client driver for the sensor
0245  * (SAA7115 or whatever) and other devices, and register the ALSA and V4L2
0246  * interfaces.
0247  *
0248  * Must NOT be called with the hw_lock held.
0249  */
0250 int go7007_register_encoder(struct go7007 *go, unsigned num_i2c_devs)
0251 {
0252     int i, ret;
0253 
0254     dev_info(go->dev, "go7007: registering new %s\n", go->name);
0255 
0256     go->v4l2_dev.release = go7007_remove;
0257     ret = v4l2_device_register(go->dev, &go->v4l2_dev);
0258     if (ret < 0)
0259         return ret;
0260 
0261     mutex_lock(&go->hw_lock);
0262     ret = go7007_init_encoder(go);
0263     mutex_unlock(&go->hw_lock);
0264     if (ret < 0)
0265         return ret;
0266 
0267     ret = go7007_v4l2_ctrl_init(go);
0268     if (ret < 0)
0269         return ret;
0270 
0271     if (!go->i2c_adapter_online &&
0272             go->board_info->flags & GO7007_BOARD_USE_ONBOARD_I2C) {
0273         ret = go7007_i2c_init(go);
0274         if (ret < 0)
0275             return ret;
0276         go->i2c_adapter_online = 1;
0277     }
0278     if (go->i2c_adapter_online) {
0279         if (go->board_id == GO7007_BOARDID_ADS_USBAV_709) {
0280             /* Reset the TW9906 */
0281             go7007_write_addr(go, 0x3c82, 0x0009);
0282             msleep(50);
0283             go7007_write_addr(go, 0x3c82, 0x000d);
0284         }
0285         for (i = 0; i < num_i2c_devs; ++i)
0286             init_i2c_module(&go->i2c_adapter, &go->board_info->i2c_devs[i]);
0287 
0288         if (go->tuner_type >= 0) {
0289             struct tuner_setup setup = {
0290                 .addr = ADDR_UNSET,
0291                 .type = go->tuner_type,
0292                 .mode_mask = T_ANALOG_TV,
0293             };
0294 
0295             v4l2_device_call_all(&go->v4l2_dev, 0, tuner,
0296                 s_type_addr, &setup);
0297         }
0298         if (go->board_id == GO7007_BOARDID_ADLINK_MPG24)
0299             v4l2_subdev_call(go->sd_video, video, s_routing,
0300                     0, 0, go->channel_number + 1);
0301     }
0302 
0303     ret = go7007_v4l2_init(go);
0304     if (ret < 0)
0305         return ret;
0306 
0307     if (go->board_info->flags & GO7007_BOARD_HAS_AUDIO) {
0308         go->audio_enabled = 1;
0309         go7007_snd_init(go);
0310     }
0311     return 0;
0312 }
0313 EXPORT_SYMBOL(go7007_register_encoder);
0314 
0315 /*
0316  * Send the encode firmware to the encoder, which will cause it
0317  * to immediately start delivering the video and audio streams.
0318  *
0319  * Must be called with the hw_lock held.
0320  */
0321 int go7007_start_encoder(struct go7007 *go)
0322 {
0323     u8 *fw;
0324     int fw_len, rv = 0, i, x, y;
0325     u16 intr_val, intr_data;
0326 
0327     go->modet_enable = 0;
0328     for (i = 0; i < 4; i++)
0329         go->modet[i].enable = 0;
0330 
0331     switch (v4l2_ctrl_g_ctrl(go->modet_mode)) {
0332     case V4L2_DETECT_MD_MODE_GLOBAL:
0333         memset(go->modet_map, 0, sizeof(go->modet_map));
0334         go->modet[0].enable = 1;
0335         go->modet_enable = 1;
0336         break;
0337     case V4L2_DETECT_MD_MODE_REGION_GRID:
0338         for (y = 0; y < go->height / 16; y++) {
0339             for (x = 0; x < go->width / 16; x++) {
0340                 int idx = y * go->width / 16 + x;
0341 
0342                 go->modet[go->modet_map[idx]].enable = 1;
0343             }
0344         }
0345         go->modet_enable = 1;
0346         break;
0347     }
0348 
0349     if (go->dvd_mode)
0350         go->modet_enable = 0;
0351 
0352     if (go7007_construct_fw_image(go, &fw, &fw_len) < 0)
0353         return -1;
0354 
0355     if (go7007_send_firmware(go, fw, fw_len) < 0 ||
0356             go7007_read_interrupt(go, &intr_val, &intr_data) < 0) {
0357         v4l2_err(&go->v4l2_dev, "error transferring firmware\n");
0358         rv = -1;
0359         goto start_error;
0360     }
0361 
0362     go->state = STATE_DATA;
0363     go->parse_length = 0;
0364     go->seen_frame = 0;
0365     if (go7007_stream_start(go) < 0) {
0366         v4l2_err(&go->v4l2_dev, "error starting stream transfer\n");
0367         rv = -1;
0368         goto start_error;
0369     }
0370 
0371 start_error:
0372     kfree(fw);
0373     return rv;
0374 }
0375 
0376 /*
0377  * Store a byte in the current video buffer, if there is one.
0378  */
0379 static inline void store_byte(struct go7007_buffer *vb, u8 byte)
0380 {
0381     if (vb && vb->vb.vb2_buf.planes[0].bytesused < GO7007_BUF_SIZE) {
0382         u8 *ptr = vb2_plane_vaddr(&vb->vb.vb2_buf, 0);
0383 
0384         ptr[vb->vb.vb2_buf.planes[0].bytesused++] = byte;
0385     }
0386 }
0387 
0388 static void go7007_set_motion_regions(struct go7007 *go, struct go7007_buffer *vb,
0389         u32 motion_regions)
0390 {
0391     if (motion_regions != go->modet_event_status) {
0392         struct v4l2_event ev = {
0393             .type = V4L2_EVENT_MOTION_DET,
0394             .u.motion_det = {
0395                 .flags = V4L2_EVENT_MD_FL_HAVE_FRAME_SEQ,
0396                 .frame_sequence = vb->vb.sequence,
0397                 .region_mask = motion_regions,
0398             },
0399         };
0400 
0401         v4l2_event_queue(&go->vdev, &ev);
0402         go->modet_event_status = motion_regions;
0403     }
0404 }
0405 
0406 /*
0407  * Determine regions with motion and send a motion detection event
0408  * in case of changes.
0409  */
0410 static void go7007_motion_regions(struct go7007 *go, struct go7007_buffer *vb)
0411 {
0412     u32 *bytesused = &vb->vb.vb2_buf.planes[0].bytesused;
0413     unsigned motion[4] = { 0, 0, 0, 0 };
0414     u32 motion_regions = 0;
0415     unsigned stride = (go->width + 7) >> 3;
0416     unsigned x, y;
0417     int i;
0418 
0419     for (i = 0; i < 216; ++i)
0420         store_byte(vb, go->active_map[i]);
0421     for (y = 0; y < go->height / 16; y++) {
0422         for (x = 0; x < go->width / 16; x++) {
0423             if (!(go->active_map[y * stride + (x >> 3)] & (1 << (x & 7))))
0424                 continue;
0425             motion[go->modet_map[y * (go->width / 16) + x]]++;
0426         }
0427     }
0428     motion_regions = ((motion[0] > 0) << 0) |
0429              ((motion[1] > 0) << 1) |
0430              ((motion[2] > 0) << 2) |
0431              ((motion[3] > 0) << 3);
0432     *bytesused -= 216;
0433     go7007_set_motion_regions(go, vb, motion_regions);
0434 }
0435 
0436 /*
0437  * Deliver the last video buffer and get a new one to start writing to.
0438  */
0439 static struct go7007_buffer *frame_boundary(struct go7007 *go, struct go7007_buffer *vb)
0440 {
0441     u32 *bytesused;
0442     struct go7007_buffer *vb_tmp = NULL;
0443     unsigned long flags;
0444 
0445     if (vb == NULL) {
0446         spin_lock_irqsave(&go->spinlock, flags);
0447         if (!list_empty(&go->vidq_active))
0448             vb = go->active_buf =
0449                 list_first_entry(&go->vidq_active, struct go7007_buffer, list);
0450         spin_unlock_irqrestore(&go->spinlock, flags);
0451         go->next_seq++;
0452         return vb;
0453     }
0454     bytesused = &vb->vb.vb2_buf.planes[0].bytesused;
0455 
0456     vb->vb.sequence = go->next_seq++;
0457     if (vb->modet_active && *bytesused + 216 < GO7007_BUF_SIZE)
0458         go7007_motion_regions(go, vb);
0459     else
0460         go7007_set_motion_regions(go, vb, 0);
0461 
0462     vb->vb.vb2_buf.timestamp = ktime_get_ns();
0463     vb_tmp = vb;
0464     spin_lock_irqsave(&go->spinlock, flags);
0465     list_del(&vb->list);
0466     if (list_empty(&go->vidq_active))
0467         vb = NULL;
0468     else
0469         vb = list_first_entry(&go->vidq_active,
0470                 struct go7007_buffer, list);
0471     go->active_buf = vb;
0472     spin_unlock_irqrestore(&go->spinlock, flags);
0473     vb2_buffer_done(&vb_tmp->vb.vb2_buf, VB2_BUF_STATE_DONE);
0474     return vb;
0475 }
0476 
0477 static void write_bitmap_word(struct go7007 *go)
0478 {
0479     int x, y, i, stride = ((go->width >> 4) + 7) >> 3;
0480 
0481     for (i = 0; i < 16; ++i) {
0482         y = (((go->parse_length - 1) << 3) + i) / (go->width >> 4);
0483         x = (((go->parse_length - 1) << 3) + i) % (go->width >> 4);
0484         if (stride * y + (x >> 3) < sizeof(go->active_map))
0485             go->active_map[stride * y + (x >> 3)] |=
0486                     (go->modet_word & 1) << (x & 0x7);
0487         go->modet_word >>= 1;
0488     }
0489 }
0490 
0491 /*
0492  * Parse a chunk of the video stream into frames.  The frames are not
0493  * delimited by the hardware, so we have to parse the frame boundaries
0494  * based on the type of video stream we're receiving.
0495  */
0496 void go7007_parse_video_stream(struct go7007 *go, u8 *buf, int length)
0497 {
0498     struct go7007_buffer *vb = go->active_buf;
0499     int i, seq_start_code = -1, gop_start_code = -1, frame_start_code = -1;
0500 
0501     switch (go->format) {
0502     case V4L2_PIX_FMT_MPEG4:
0503         seq_start_code = 0xB0;
0504         gop_start_code = 0xB3;
0505         frame_start_code = 0xB6;
0506         break;
0507     case V4L2_PIX_FMT_MPEG1:
0508     case V4L2_PIX_FMT_MPEG2:
0509         seq_start_code = 0xB3;
0510         gop_start_code = 0xB8;
0511         frame_start_code = 0x00;
0512         break;
0513     }
0514 
0515     for (i = 0; i < length; ++i) {
0516         if (vb && vb->vb.vb2_buf.planes[0].bytesused >=
0517                 GO7007_BUF_SIZE - 3) {
0518             v4l2_info(&go->v4l2_dev, "dropping oversized frame\n");
0519             vb2_set_plane_payload(&vb->vb.vb2_buf, 0, 0);
0520             vb->frame_offset = 0;
0521             vb->modet_active = 0;
0522             vb = go->active_buf = NULL;
0523         }
0524 
0525         switch (go->state) {
0526         case STATE_DATA:
0527             switch (buf[i]) {
0528             case 0x00:
0529                 go->state = STATE_00;
0530                 break;
0531             case 0xFF:
0532                 go->state = STATE_FF;
0533                 break;
0534             default:
0535                 store_byte(vb, buf[i]);
0536                 break;
0537             }
0538             break;
0539         case STATE_00:
0540             switch (buf[i]) {
0541             case 0x00:
0542                 go->state = STATE_00_00;
0543                 break;
0544             case 0xFF:
0545                 store_byte(vb, 0x00);
0546                 go->state = STATE_FF;
0547                 break;
0548             default:
0549                 store_byte(vb, 0x00);
0550                 store_byte(vb, buf[i]);
0551                 go->state = STATE_DATA;
0552                 break;
0553             }
0554             break;
0555         case STATE_00_00:
0556             switch (buf[i]) {
0557             case 0x00:
0558                 store_byte(vb, 0x00);
0559                 /* go->state remains STATE_00_00 */
0560                 break;
0561             case 0x01:
0562                 go->state = STATE_00_00_01;
0563                 break;
0564             case 0xFF:
0565                 store_byte(vb, 0x00);
0566                 store_byte(vb, 0x00);
0567                 go->state = STATE_FF;
0568                 break;
0569             default:
0570                 store_byte(vb, 0x00);
0571                 store_byte(vb, 0x00);
0572                 store_byte(vb, buf[i]);
0573                 go->state = STATE_DATA;
0574                 break;
0575             }
0576             break;
0577         case STATE_00_00_01:
0578             if (buf[i] == 0xF8 && go->modet_enable == 0) {
0579                 /* MODET start code, but MODET not enabled */
0580                 store_byte(vb, 0x00);
0581                 store_byte(vb, 0x00);
0582                 store_byte(vb, 0x01);
0583                 store_byte(vb, 0xF8);
0584                 go->state = STATE_DATA;
0585                 break;
0586             }
0587             /* If this is the start of a new MPEG frame,
0588              * get a new buffer */
0589             if ((go->format == V4L2_PIX_FMT_MPEG1 ||
0590                  go->format == V4L2_PIX_FMT_MPEG2 ||
0591                  go->format == V4L2_PIX_FMT_MPEG4) &&
0592                 (buf[i] == seq_start_code ||
0593                  buf[i] == gop_start_code ||
0594                  buf[i] == frame_start_code)) {
0595                 if (vb == NULL || go->seen_frame)
0596                     vb = frame_boundary(go, vb);
0597                 go->seen_frame = buf[i] == frame_start_code;
0598                 if (vb && go->seen_frame)
0599                     vb->frame_offset =
0600                     vb->vb.vb2_buf.planes[0].bytesused;
0601             }
0602             /* Handle any special chunk types, or just write the
0603              * start code to the (potentially new) buffer */
0604             switch (buf[i]) {
0605             case 0xF5: /* timestamp */
0606                 go->parse_length = 12;
0607                 go->state = STATE_UNPARSED;
0608                 break;
0609             case 0xF6: /* vbi */
0610                 go->state = STATE_VBI_LEN_A;
0611                 break;
0612             case 0xF8: /* MD map */
0613                 go->parse_length = 0;
0614                 memset(go->active_map, 0,
0615                         sizeof(go->active_map));
0616                 go->state = STATE_MODET_MAP;
0617                 break;
0618             case 0xFF: /* Potential JPEG start code */
0619                 store_byte(vb, 0x00);
0620                 store_byte(vb, 0x00);
0621                 store_byte(vb, 0x01);
0622                 go->state = STATE_FF;
0623                 break;
0624             default:
0625                 store_byte(vb, 0x00);
0626                 store_byte(vb, 0x00);
0627                 store_byte(vb, 0x01);
0628                 store_byte(vb, buf[i]);
0629                 go->state = STATE_DATA;
0630                 break;
0631             }
0632             break;
0633         case STATE_FF:
0634             switch (buf[i]) {
0635             case 0x00:
0636                 store_byte(vb, 0xFF);
0637                 go->state = STATE_00;
0638                 break;
0639             case 0xFF:
0640                 store_byte(vb, 0xFF);
0641                 /* go->state remains STATE_FF */
0642                 break;
0643             case 0xD8:
0644                 if (go->format == V4L2_PIX_FMT_MJPEG)
0645                     vb = frame_boundary(go, vb);
0646                 fallthrough;
0647             default:
0648                 store_byte(vb, 0xFF);
0649                 store_byte(vb, buf[i]);
0650                 go->state = STATE_DATA;
0651                 break;
0652             }
0653             break;
0654         case STATE_VBI_LEN_A:
0655             go->parse_length = buf[i] << 8;
0656             go->state = STATE_VBI_LEN_B;
0657             break;
0658         case STATE_VBI_LEN_B:
0659             go->parse_length |= buf[i];
0660             if (go->parse_length > 0)
0661                 go->state = STATE_UNPARSED;
0662             else
0663                 go->state = STATE_DATA;
0664             break;
0665         case STATE_MODET_MAP:
0666             if (go->parse_length < 204) {
0667                 if (go->parse_length & 1) {
0668                     go->modet_word |= buf[i];
0669                     write_bitmap_word(go);
0670                 } else
0671                     go->modet_word = buf[i] << 8;
0672             } else if (go->parse_length == 207 && vb) {
0673                 vb->modet_active = buf[i];
0674             }
0675             if (++go->parse_length == 208)
0676                 go->state = STATE_DATA;
0677             break;
0678         case STATE_UNPARSED:
0679             if (--go->parse_length == 0)
0680                 go->state = STATE_DATA;
0681             break;
0682         }
0683     }
0684 }
0685 EXPORT_SYMBOL(go7007_parse_video_stream);
0686 
0687 /*
0688  * Allocate a new go7007 struct.  Used by the hardware-specific probe.
0689  */
0690 struct go7007 *go7007_alloc(const struct go7007_board_info *board,
0691                         struct device *dev)
0692 {
0693     struct go7007 *go;
0694 
0695     go = kzalloc(sizeof(struct go7007), GFP_KERNEL);
0696     if (go == NULL)
0697         return NULL;
0698     go->dev = dev;
0699     go->board_info = board;
0700     go->tuner_type = -1;
0701     mutex_init(&go->hw_lock);
0702     init_waitqueue_head(&go->frame_waitq);
0703     spin_lock_init(&go->spinlock);
0704     go->status = STATUS_INIT;
0705     init_waitqueue_head(&go->interrupt_waitq);
0706     go7007_update_board(go);
0707     go->format = V4L2_PIX_FMT_MJPEG;
0708     go->bitrate = 1500000;
0709     go->fps_scale = 1;
0710     go->aspect_ratio = GO7007_RATIO_1_1;
0711 
0712     return go;
0713 }
0714 EXPORT_SYMBOL(go7007_alloc);
0715 
0716 void go7007_update_board(struct go7007 *go)
0717 {
0718     const struct go7007_board_info *board = go->board_info;
0719 
0720     if (board->sensor_flags & GO7007_SENSOR_TV) {
0721         go->standard = GO7007_STD_NTSC;
0722         go->std = V4L2_STD_NTSC_M;
0723         go->width = 720;
0724         go->height = 480;
0725         go->sensor_framerate = 30000;
0726     } else {
0727         go->standard = GO7007_STD_OTHER;
0728         go->width = board->sensor_width;
0729         go->height = board->sensor_height;
0730         go->sensor_framerate = board->sensor_framerate;
0731     }
0732     go->encoder_v_offset = board->sensor_v_offset;
0733     go->encoder_h_offset = board->sensor_h_offset;
0734 }
0735 EXPORT_SYMBOL(go7007_update_board);
0736 
0737 MODULE_LICENSE("GPL v2");