Greg Kroah-Hartman | 866b869 | 2008-02-15 16:53:09 -0800 | [diff] [blame] | 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> |
Greg Kroah-Hartman | 866b869 | 2008-02-15 16:53:09 -0800 | [diff] [blame] | 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> |
Mauro Carvalho Chehab | fd9a40d | 2009-09-15 11:07:59 -0300 | [diff] [blame] | 30 | #include <linux/mutex.h> |
Greg Kroah-Hartman | 866b869 | 2008-02-15 16:53:09 -0800 | [diff] [blame] | 31 | #include <linux/uaccess.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 32 | #include <linux/slab.h> |
Ross Cohen | df20d69 | 2008-09-29 22:36:24 -0400 | [diff] [blame] | 33 | #include <linux/videodev2.h> |
Greg Kroah-Hartman | 866b869 | 2008-02-15 16:53:09 -0800 | [diff] [blame] | 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) { |
Pete Eberlein | 0b398f4 | 2009-11-16 15:07:42 -0300 | [diff] [blame] | 52 | v4l2_err(&go->v4l2_dev, "timeout waiting for read interrupt\n"); |
Greg Kroah-Hartman | 866b869 | 2008-02-15 16:53:09 -0800 | [diff] [blame] | 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)) { |
Pete Eberlein | 6247407 | 2009-09-18 23:06:15 -0300 | [diff] [blame] | 100 | v4l2_err(go, "unable to load firmware from file " |
| 101 | "\"%s\"\n", fw_name); |
Greg Kroah-Hartman | 866b869 | 2008-02-15 16:53:09 -0800 | [diff] [blame] | 102 | return -1; |
| 103 | } |
| 104 | if (fw_entry->size < 16 || memcmp(fw_entry->data, "WISGO7007FW", 11)) { |
Pete Eberlein | 6247407 | 2009-09-18 23:06:15 -0300 | [diff] [blame] | 105 | v4l2_err(go, "file \"%s\" does not appear to be " |
Greg Kroah-Hartman | 866b869 | 2008-02-15 16:53:09 -0800 | [diff] [blame] | 106 | "go7007 firmware\n", fw_name); |
| 107 | release_firmware(fw_entry); |
| 108 | return -1; |
| 109 | } |
| 110 | fw_len = fw_entry->size - 16; |
Peter Huewe | 42f9de6 | 2013-01-25 19:23:30 -0300 | [diff] [blame] | 111 | bounce = kmemdup(fw_entry->data + 16, fw_len, GFP_KERNEL); |
Greg Kroah-Hartman | 866b869 | 2008-02-15 16:53:09 -0800 | [diff] [blame] | 112 | if (bounce == NULL) { |
Pete Eberlein | 6247407 | 2009-09-18 23:06:15 -0300 | [diff] [blame] | 113 | v4l2_err(go, "unable to allocate %d bytes for " |
Greg Kroah-Hartman | 866b869 | 2008-02-15 16:53:09 -0800 | [diff] [blame] | 114 | "firmware transfer\n", fw_len); |
| 115 | release_firmware(fw_entry); |
| 116 | return -1; |
| 117 | } |
Greg Kroah-Hartman | 866b869 | 2008-02-15 16:53:09 -0800 | [diff] [blame] | 118 | release_firmware(fw_entry); |
| 119 | if (go7007_interface_reset(go) < 0 || |
| 120 | go7007_send_firmware(go, bounce, fw_len) < 0 || |
| 121 | go7007_read_interrupt(go, &intr_val, &intr_data) < 0 || |
| 122 | (intr_val & ~0x1) != 0x5a5a) { |
Pete Eberlein | 6247407 | 2009-09-18 23:06:15 -0300 | [diff] [blame] | 123 | v4l2_err(go, "error transferring firmware\n"); |
Greg Kroah-Hartman | 866b869 | 2008-02-15 16:53:09 -0800 | [diff] [blame] | 124 | rv = -1; |
| 125 | } |
| 126 | kfree(bounce); |
| 127 | return rv; |
| 128 | } |
| 129 | |
Ben Hutchings | 5d929a7 | 2010-01-13 23:36:09 +0000 | [diff] [blame] | 130 | MODULE_FIRMWARE("go7007fw.bin"); |
| 131 | |
Greg Kroah-Hartman | 866b869 | 2008-02-15 16:53:09 -0800 | [diff] [blame] | 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 | |
Mauro Carvalho Chehab | fd9a40d | 2009-09-15 11:07:59 -0300 | [diff] [blame] | 143 | mutex_lock(&go->hw_lock); |
Greg Kroah-Hartman | 866b869 | 2008-02-15 16:53:09 -0800 | [diff] [blame] | 144 | ret = go7007_load_encoder(go); |
Mauro Carvalho Chehab | fd9a40d | 2009-09-15 11:07:59 -0300 | [diff] [blame] | 145 | mutex_unlock(&go->hw_lock); |
Greg Kroah-Hartman | 866b869 | 2008-02-15 16:53:09 -0800 | [diff] [blame] | 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 | } |
Volokh Konstantin | d18b6ac | 2013-01-16 09:00:50 -0300 | [diff] [blame] | 175 | if (go->board_id == GO7007_BOARDID_ADLINK_MPG24) { |
| 176 | /* set GPIO5 to be an output, currently low */ |
| 177 | go7007_write_addr(go, 0x3c82, 0x0000); |
| 178 | go7007_write_addr(go, 0x3c80, 0x00df); |
| 179 | } |
Greg Kroah-Hartman | 866b869 | 2008-02-15 16:53:09 -0800 | [diff] [blame] | 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | /* |
| 184 | * Send the boot firmware to the GO7007 and configure the registers. This |
| 185 | * is the only way to stop the encoder once it has started streaming video. |
| 186 | * |
| 187 | * Must be called with the hw_lock held. |
| 188 | */ |
| 189 | int go7007_reset_encoder(struct go7007 *go) |
| 190 | { |
| 191 | if (go7007_load_encoder(go) < 0) |
| 192 | return -1; |
| 193 | return go7007_init_encoder(go); |
| 194 | } |
| 195 | |
| 196 | /* |
| 197 | * Attempt to instantiate an I2C client by ID, probably loading a module. |
| 198 | */ |
Volokh Konstantin | dcafb6d | 2013-03-09 08:48:25 -0300 | [diff] [blame^] | 199 | static int init_i2c_module(struct i2c_adapter *adapter, const struct go_i2c *const i2c) |
Greg Kroah-Hartman | 866b869 | 2008-02-15 16:53:09 -0800 | [diff] [blame] | 200 | { |
Pete Eberlein | fa3c39b | 2009-11-16 15:16:00 -0300 | [diff] [blame] | 201 | struct go7007 *go = i2c_get_adapdata(adapter); |
| 202 | struct v4l2_device *v4l2_dev = &go->v4l2_dev; |
Volokh Konstantin | dcafb6d | 2013-03-09 08:48:25 -0300 | [diff] [blame^] | 203 | struct i2c_board_info info; |
Greg Kroah-Hartman | 866b869 | 2008-02-15 16:53:09 -0800 | [diff] [blame] | 204 | |
Volokh Konstantin | dcafb6d | 2013-03-09 08:48:25 -0300 | [diff] [blame^] | 205 | memset(&info, 0, sizeof(info)); |
| 206 | strlcpy(info.type, i2c->type, sizeof(info.type)); |
| 207 | info.addr = i2c->addr; |
| 208 | info.flags = i2c->flags; |
| 209 | |
| 210 | if (v4l2_i2c_new_subdev_board(v4l2_dev, adapter, &info, NULL)) |
Greg Kroah-Hartman | 866b869 | 2008-02-15 16:53:09 -0800 | [diff] [blame] | 211 | return 0; |
Pete Eberlein | fa3c39b | 2009-11-16 15:16:00 -0300 | [diff] [blame] | 212 | |
Volokh Konstantin | dcafb6d | 2013-03-09 08:48:25 -0300 | [diff] [blame^] | 213 | printk(KERN_INFO "go7007: probing for module i2c:%s failed\n", i2c->type); |
| 214 | return -EINVAL; |
Greg Kroah-Hartman | 866b869 | 2008-02-15 16:53:09 -0800 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | /* |
| 218 | * Finalize the GO7007 hardware setup, register the on-board I2C adapter |
| 219 | * (if used on this board), load the I2C client driver for the sensor |
| 220 | * (SAA7115 or whatever) and other devices, and register the ALSA and V4L2 |
| 221 | * interfaces. |
| 222 | * |
| 223 | * Must NOT be called with the hw_lock held. |
| 224 | */ |
| 225 | int go7007_register_encoder(struct go7007 *go) |
| 226 | { |
| 227 | int i, ret; |
| 228 | |
YAMANE Toshiaki | 6d56950 | 2012-11-04 16:40:22 -0300 | [diff] [blame] | 229 | dev_info(go->dev, "go7007: registering new %s\n", go->name); |
Greg Kroah-Hartman | 866b869 | 2008-02-15 16:53:09 -0800 | [diff] [blame] | 230 | |
Mauro Carvalho Chehab | fd9a40d | 2009-09-15 11:07:59 -0300 | [diff] [blame] | 231 | mutex_lock(&go->hw_lock); |
Greg Kroah-Hartman | 866b869 | 2008-02-15 16:53:09 -0800 | [diff] [blame] | 232 | ret = go7007_init_encoder(go); |
Mauro Carvalho Chehab | fd9a40d | 2009-09-15 11:07:59 -0300 | [diff] [blame] | 233 | mutex_unlock(&go->hw_lock); |
Greg Kroah-Hartman | 866b869 | 2008-02-15 16:53:09 -0800 | [diff] [blame] | 234 | if (ret < 0) |
| 235 | return -1; |
| 236 | |
Pete Eberlein | fa3c39b | 2009-11-16 15:16:00 -0300 | [diff] [blame] | 237 | /* v4l2 init must happen before i2c subdevs */ |
| 238 | ret = go7007_v4l2_init(go); |
| 239 | if (ret < 0) |
| 240 | return ret; |
| 241 | |
Greg Kroah-Hartman | 866b869 | 2008-02-15 16:53:09 -0800 | [diff] [blame] | 242 | if (!go->i2c_adapter_online && |
| 243 | go->board_info->flags & GO7007_BOARD_USE_ONBOARD_I2C) { |
| 244 | if (go7007_i2c_init(go) < 0) |
| 245 | return -1; |
| 246 | go->i2c_adapter_online = 1; |
| 247 | } |
| 248 | if (go->i2c_adapter_online) { |
| 249 | for (i = 0; i < go->board_info->num_i2c_devs; ++i) |
Volokh Konstantin | dcafb6d | 2013-03-09 08:48:25 -0300 | [diff] [blame^] | 250 | init_i2c_module(&go->i2c_adapter, &go->board_info->i2c_devs[i]); |
Greg Kroah-Hartman | 866b869 | 2008-02-15 16:53:09 -0800 | [diff] [blame] | 251 | if (go->board_id == GO7007_BOARDID_ADLINK_MPG24) |
| 252 | i2c_clients_command(&go->i2c_adapter, |
| 253 | DECODER_SET_CHANNEL, &go->channel_number); |
| 254 | } |
| 255 | if (go->board_info->flags & GO7007_BOARD_HAS_AUDIO) { |
| 256 | go->audio_enabled = 1; |
| 257 | go7007_snd_init(go); |
| 258 | } |
Pete Eberlein | fa3c39b | 2009-11-16 15:16:00 -0300 | [diff] [blame] | 259 | return 0; |
Greg Kroah-Hartman | 866b869 | 2008-02-15 16:53:09 -0800 | [diff] [blame] | 260 | } |
| 261 | EXPORT_SYMBOL(go7007_register_encoder); |
| 262 | |
| 263 | /* |
| 264 | * Send the encode firmware to the encoder, which will cause it |
| 265 | * to immediately start delivering the video and audio streams. |
| 266 | * |
| 267 | * Must be called with the hw_lock held. |
| 268 | */ |
| 269 | int go7007_start_encoder(struct go7007 *go) |
| 270 | { |
| 271 | u8 *fw; |
| 272 | int fw_len, rv = 0, i; |
| 273 | u16 intr_val, intr_data; |
| 274 | |
| 275 | go->modet_enable = 0; |
| 276 | if (!go->dvd_mode) |
| 277 | for (i = 0; i < 4; ++i) { |
| 278 | if (go->modet[i].enable) { |
| 279 | go->modet_enable = 1; |
| 280 | continue; |
| 281 | } |
| 282 | go->modet[i].pixel_threshold = 32767; |
| 283 | go->modet[i].motion_threshold = 32767; |
| 284 | go->modet[i].mb_threshold = 32767; |
| 285 | } |
| 286 | |
| 287 | if (go7007_construct_fw_image(go, &fw, &fw_len) < 0) |
| 288 | return -1; |
| 289 | |
| 290 | if (go7007_send_firmware(go, fw, fw_len) < 0 || |
| 291 | go7007_read_interrupt(go, &intr_val, &intr_data) < 0) { |
Pete Eberlein | 0b398f4 | 2009-11-16 15:07:42 -0300 | [diff] [blame] | 292 | v4l2_err(&go->v4l2_dev, "error transferring firmware\n"); |
Greg Kroah-Hartman | 866b869 | 2008-02-15 16:53:09 -0800 | [diff] [blame] | 293 | rv = -1; |
| 294 | goto start_error; |
| 295 | } |
| 296 | |
| 297 | go->state = STATE_DATA; |
| 298 | go->parse_length = 0; |
| 299 | go->seen_frame = 0; |
| 300 | if (go7007_stream_start(go) < 0) { |
Pete Eberlein | 0b398f4 | 2009-11-16 15:07:42 -0300 | [diff] [blame] | 301 | v4l2_err(&go->v4l2_dev, "error starting stream transfer\n"); |
Greg Kroah-Hartman | 866b869 | 2008-02-15 16:53:09 -0800 | [diff] [blame] | 302 | rv = -1; |
| 303 | goto start_error; |
| 304 | } |
| 305 | |
| 306 | start_error: |
| 307 | kfree(fw); |
| 308 | return rv; |
| 309 | } |
| 310 | |
| 311 | /* |
| 312 | * Store a byte in the current video buffer, if there is one. |
| 313 | */ |
| 314 | static inline void store_byte(struct go7007_buffer *gobuf, u8 byte) |
| 315 | { |
| 316 | if (gobuf != NULL && gobuf->bytesused < GO7007_BUF_SIZE) { |
| 317 | unsigned int pgidx = gobuf->offset >> PAGE_SHIFT; |
| 318 | unsigned int pgoff = gobuf->offset & ~PAGE_MASK; |
| 319 | |
| 320 | *((u8 *)page_address(gobuf->pages[pgidx]) + pgoff) = byte; |
| 321 | ++gobuf->offset; |
| 322 | ++gobuf->bytesused; |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | /* |
| 327 | * Deliver the last video buffer and get a new one to start writing to. |
| 328 | */ |
| 329 | static void frame_boundary(struct go7007 *go) |
| 330 | { |
| 331 | struct go7007_buffer *gobuf; |
| 332 | int i; |
| 333 | |
| 334 | if (go->active_buf) { |
| 335 | if (go->active_buf->modet_active) { |
| 336 | if (go->active_buf->bytesused + 216 < GO7007_BUF_SIZE) { |
| 337 | for (i = 0; i < 216; ++i) |
| 338 | store_byte(go->active_buf, |
| 339 | go->active_map[i]); |
| 340 | go->active_buf->bytesused -= 216; |
| 341 | } else |
| 342 | go->active_buf->modet_active = 0; |
| 343 | } |
| 344 | go->active_buf->state = BUF_STATE_DONE; |
| 345 | wake_up_interruptible(&go->frame_waitq); |
| 346 | go->active_buf = NULL; |
| 347 | } |
| 348 | list_for_each_entry(gobuf, &go->stream, stream) |
| 349 | if (gobuf->state == BUF_STATE_QUEUED) { |
| 350 | gobuf->seq = go->next_seq; |
| 351 | do_gettimeofday(&gobuf->timestamp); |
| 352 | go->active_buf = gobuf; |
| 353 | break; |
| 354 | } |
| 355 | ++go->next_seq; |
| 356 | } |
| 357 | |
| 358 | static void write_bitmap_word(struct go7007 *go) |
| 359 | { |
| 360 | int x, y, i, stride = ((go->width >> 4) + 7) >> 3; |
| 361 | |
| 362 | for (i = 0; i < 16; ++i) { |
| 363 | y = (((go->parse_length - 1) << 3) + i) / (go->width >> 4); |
| 364 | x = (((go->parse_length - 1) << 3) + i) % (go->width >> 4); |
Pete Eberlein | a716e9d | 2010-09-23 14:43:41 -0300 | [diff] [blame] | 365 | if (stride * y + (x >> 3) < sizeof(go->active_map)) |
| 366 | go->active_map[stride * y + (x >> 3)] |= |
Greg Kroah-Hartman | 866b869 | 2008-02-15 16:53:09 -0800 | [diff] [blame] | 367 | (go->modet_word & 1) << (x & 0x7); |
| 368 | go->modet_word >>= 1; |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | /* |
| 373 | * Parse a chunk of the video stream into frames. The frames are not |
| 374 | * delimited by the hardware, so we have to parse the frame boundaries |
| 375 | * based on the type of video stream we're receiving. |
| 376 | */ |
| 377 | void go7007_parse_video_stream(struct go7007 *go, u8 *buf, int length) |
| 378 | { |
| 379 | int i, seq_start_code = -1, frame_start_code = -1; |
| 380 | |
| 381 | spin_lock(&go->spinlock); |
| 382 | |
| 383 | switch (go->format) { |
| 384 | case GO7007_FORMAT_MPEG4: |
| 385 | seq_start_code = 0xB0; |
| 386 | frame_start_code = 0xB6; |
| 387 | break; |
| 388 | case GO7007_FORMAT_MPEG1: |
| 389 | case GO7007_FORMAT_MPEG2: |
| 390 | seq_start_code = 0xB3; |
| 391 | frame_start_code = 0x00; |
| 392 | break; |
| 393 | } |
| 394 | |
| 395 | for (i = 0; i < length; ++i) { |
| 396 | if (go->active_buf != NULL && |
| 397 | go->active_buf->bytesused >= GO7007_BUF_SIZE - 3) { |
Pete Eberlein | 0b398f4 | 2009-11-16 15:07:42 -0300 | [diff] [blame] | 398 | v4l2_info(&go->v4l2_dev, "dropping oversized frame\n"); |
Greg Kroah-Hartman | 866b869 | 2008-02-15 16:53:09 -0800 | [diff] [blame] | 399 | go->active_buf->offset -= go->active_buf->bytesused; |
| 400 | go->active_buf->bytesused = 0; |
| 401 | go->active_buf->modet_active = 0; |
| 402 | go->active_buf = NULL; |
| 403 | } |
| 404 | |
| 405 | switch (go->state) { |
| 406 | case STATE_DATA: |
| 407 | switch (buf[i]) { |
| 408 | case 0x00: |
| 409 | go->state = STATE_00; |
| 410 | break; |
| 411 | case 0xFF: |
| 412 | go->state = STATE_FF; |
| 413 | break; |
| 414 | default: |
| 415 | store_byte(go->active_buf, buf[i]); |
| 416 | break; |
| 417 | } |
| 418 | break; |
| 419 | case STATE_00: |
| 420 | switch (buf[i]) { |
| 421 | case 0x00: |
| 422 | go->state = STATE_00_00; |
| 423 | break; |
| 424 | case 0xFF: |
| 425 | store_byte(go->active_buf, 0x00); |
| 426 | go->state = STATE_FF; |
| 427 | break; |
| 428 | default: |
| 429 | store_byte(go->active_buf, 0x00); |
| 430 | store_byte(go->active_buf, buf[i]); |
| 431 | go->state = STATE_DATA; |
| 432 | break; |
| 433 | } |
| 434 | break; |
| 435 | case STATE_00_00: |
| 436 | switch (buf[i]) { |
| 437 | case 0x00: |
| 438 | store_byte(go->active_buf, 0x00); |
| 439 | /* go->state remains STATE_00_00 */ |
| 440 | break; |
| 441 | case 0x01: |
| 442 | go->state = STATE_00_00_01; |
| 443 | break; |
| 444 | case 0xFF: |
| 445 | store_byte(go->active_buf, 0x00); |
| 446 | store_byte(go->active_buf, 0x00); |
| 447 | go->state = STATE_FF; |
| 448 | break; |
| 449 | default: |
| 450 | store_byte(go->active_buf, 0x00); |
| 451 | store_byte(go->active_buf, 0x00); |
| 452 | store_byte(go->active_buf, buf[i]); |
| 453 | go->state = STATE_DATA; |
| 454 | break; |
| 455 | } |
| 456 | break; |
| 457 | case STATE_00_00_01: |
Pete Eberlein | a716e9d | 2010-09-23 14:43:41 -0300 | [diff] [blame] | 458 | if (buf[i] == 0xF8 && go->modet_enable == 0) { |
| 459 | /* MODET start code, but MODET not enabled */ |
| 460 | store_byte(go->active_buf, 0x00); |
| 461 | store_byte(go->active_buf, 0x00); |
| 462 | store_byte(go->active_buf, 0x01); |
| 463 | store_byte(go->active_buf, 0xF8); |
| 464 | go->state = STATE_DATA; |
| 465 | break; |
| 466 | } |
Greg Kroah-Hartman | 866b869 | 2008-02-15 16:53:09 -0800 | [diff] [blame] | 467 | /* If this is the start of a new MPEG frame, |
| 468 | * get a new buffer */ |
| 469 | if ((go->format == GO7007_FORMAT_MPEG1 || |
| 470 | go->format == GO7007_FORMAT_MPEG2 || |
| 471 | go->format == GO7007_FORMAT_MPEG4) && |
| 472 | (buf[i] == seq_start_code || |
| 473 | buf[i] == 0xB8 || /* GOP code */ |
| 474 | buf[i] == frame_start_code)) { |
| 475 | if (go->active_buf == NULL || go->seen_frame) |
| 476 | frame_boundary(go); |
| 477 | if (buf[i] == frame_start_code) { |
| 478 | if (go->active_buf != NULL) |
| 479 | go->active_buf->frame_offset = |
| 480 | go->active_buf->offset; |
| 481 | go->seen_frame = 1; |
| 482 | } else { |
| 483 | go->seen_frame = 0; |
| 484 | } |
| 485 | } |
| 486 | /* Handle any special chunk types, or just write the |
| 487 | * start code to the (potentially new) buffer */ |
| 488 | switch (buf[i]) { |
| 489 | case 0xF5: /* timestamp */ |
| 490 | go->parse_length = 12; |
| 491 | go->state = STATE_UNPARSED; |
| 492 | break; |
| 493 | case 0xF6: /* vbi */ |
| 494 | go->state = STATE_VBI_LEN_A; |
| 495 | break; |
| 496 | case 0xF8: /* MD map */ |
| 497 | go->parse_length = 0; |
| 498 | memset(go->active_map, 0, |
| 499 | sizeof(go->active_map)); |
| 500 | go->state = STATE_MODET_MAP; |
| 501 | break; |
| 502 | case 0xFF: /* Potential JPEG start code */ |
| 503 | store_byte(go->active_buf, 0x00); |
| 504 | store_byte(go->active_buf, 0x00); |
| 505 | store_byte(go->active_buf, 0x01); |
| 506 | go->state = STATE_FF; |
| 507 | break; |
| 508 | default: |
| 509 | store_byte(go->active_buf, 0x00); |
| 510 | store_byte(go->active_buf, 0x00); |
| 511 | store_byte(go->active_buf, 0x01); |
| 512 | store_byte(go->active_buf, buf[i]); |
| 513 | go->state = STATE_DATA; |
| 514 | break; |
| 515 | } |
| 516 | break; |
| 517 | case STATE_FF: |
| 518 | switch (buf[i]) { |
| 519 | case 0x00: |
| 520 | store_byte(go->active_buf, 0xFF); |
| 521 | go->state = STATE_00; |
| 522 | break; |
| 523 | case 0xFF: |
| 524 | store_byte(go->active_buf, 0xFF); |
| 525 | /* go->state remains STATE_FF */ |
| 526 | break; |
| 527 | case 0xD8: |
| 528 | if (go->format == GO7007_FORMAT_MJPEG) |
| 529 | frame_boundary(go); |
| 530 | /* fall through */ |
| 531 | default: |
| 532 | store_byte(go->active_buf, 0xFF); |
| 533 | store_byte(go->active_buf, buf[i]); |
| 534 | go->state = STATE_DATA; |
| 535 | break; |
| 536 | } |
| 537 | break; |
| 538 | case STATE_VBI_LEN_A: |
| 539 | go->parse_length = buf[i] << 8; |
| 540 | go->state = STATE_VBI_LEN_B; |
| 541 | break; |
| 542 | case STATE_VBI_LEN_B: |
| 543 | go->parse_length |= buf[i]; |
| 544 | if (go->parse_length > 0) |
| 545 | go->state = STATE_UNPARSED; |
| 546 | else |
| 547 | go->state = STATE_DATA; |
| 548 | break; |
| 549 | case STATE_MODET_MAP: |
| 550 | if (go->parse_length < 204) { |
| 551 | if (go->parse_length & 1) { |
| 552 | go->modet_word |= buf[i]; |
| 553 | write_bitmap_word(go); |
| 554 | } else |
| 555 | go->modet_word = buf[i] << 8; |
| 556 | } else if (go->parse_length == 207 && go->active_buf) { |
| 557 | go->active_buf->modet_active = buf[i]; |
| 558 | } |
| 559 | if (++go->parse_length == 208) |
| 560 | go->state = STATE_DATA; |
| 561 | break; |
| 562 | case STATE_UNPARSED: |
| 563 | if (--go->parse_length == 0) |
| 564 | go->state = STATE_DATA; |
| 565 | break; |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | spin_unlock(&go->spinlock); |
| 570 | } |
| 571 | EXPORT_SYMBOL(go7007_parse_video_stream); |
| 572 | |
| 573 | /* |
| 574 | * Allocate a new go7007 struct. Used by the hardware-specific probe. |
| 575 | */ |
| 576 | struct go7007 *go7007_alloc(struct go7007_board_info *board, struct device *dev) |
| 577 | { |
| 578 | struct go7007 *go; |
| 579 | int i; |
| 580 | |
Volokh Konstantin | 61a2353 | 2013-01-16 09:00:48 -0300 | [diff] [blame] | 581 | go = kzalloc(sizeof(struct go7007), GFP_KERNEL); |
Greg Kroah-Hartman | 866b869 | 2008-02-15 16:53:09 -0800 | [diff] [blame] | 582 | if (go == NULL) |
| 583 | return NULL; |
| 584 | go->dev = dev; |
| 585 | go->board_info = board; |
| 586 | go->board_id = 0; |
| 587 | go->tuner_type = -1; |
| 588 | go->channel_number = 0; |
| 589 | go->name[0] = 0; |
Mauro Carvalho Chehab | fd9a40d | 2009-09-15 11:07:59 -0300 | [diff] [blame] | 590 | mutex_init(&go->hw_lock); |
Greg Kroah-Hartman | 866b869 | 2008-02-15 16:53:09 -0800 | [diff] [blame] | 591 | init_waitqueue_head(&go->frame_waitq); |
| 592 | spin_lock_init(&go->spinlock); |
| 593 | go->video_dev = NULL; |
| 594 | go->ref_count = 0; |
| 595 | go->status = STATUS_INIT; |
| 596 | memset(&go->i2c_adapter, 0, sizeof(go->i2c_adapter)); |
| 597 | go->i2c_adapter_online = 0; |
| 598 | go->interrupt_available = 0; |
| 599 | init_waitqueue_head(&go->interrupt_waitq); |
| 600 | go->in_use = 0; |
| 601 | go->input = 0; |
| 602 | if (board->sensor_flags & GO7007_SENSOR_TV) { |
| 603 | go->standard = GO7007_STD_NTSC; |
| 604 | go->width = 720; |
| 605 | go->height = 480; |
| 606 | go->sensor_framerate = 30000; |
| 607 | } else { |
| 608 | go->standard = GO7007_STD_OTHER; |
| 609 | go->width = board->sensor_width; |
| 610 | go->height = board->sensor_height; |
| 611 | go->sensor_framerate = board->sensor_framerate; |
| 612 | } |
| 613 | go->encoder_v_offset = board->sensor_v_offset; |
| 614 | go->encoder_h_offset = board->sensor_h_offset; |
| 615 | go->encoder_h_halve = 0; |
| 616 | go->encoder_v_halve = 0; |
| 617 | go->encoder_subsample = 0; |
| 618 | go->streaming = 0; |
| 619 | go->format = GO7007_FORMAT_MJPEG; |
| 620 | go->bitrate = 1500000; |
| 621 | go->fps_scale = 1; |
| 622 | go->pali = 0; |
| 623 | go->aspect_ratio = GO7007_RATIO_1_1; |
| 624 | go->gop_size = 0; |
| 625 | go->ipb = 0; |
| 626 | go->closed_gop = 0; |
| 627 | go->repeat_seqhead = 0; |
| 628 | go->seq_header_enable = 0; |
| 629 | go->gop_header_enable = 0; |
| 630 | go->dvd_mode = 0; |
| 631 | go->interlace_coding = 0; |
| 632 | for (i = 0; i < 4; ++i) |
Joe Perches | 859171c | 2010-11-14 19:04:48 -0800 | [diff] [blame] | 633 | go->modet[i].enable = 0; |
Greg Kroah-Hartman | 866b869 | 2008-02-15 16:53:09 -0800 | [diff] [blame] | 634 | for (i = 0; i < 1624; ++i) |
| 635 | go->modet_map[i] = 0; |
| 636 | go->audio_deliver = NULL; |
| 637 | go->audio_enabled = 0; |
| 638 | INIT_LIST_HEAD(&go->stream); |
| 639 | |
| 640 | return go; |
| 641 | } |
| 642 | EXPORT_SYMBOL(go7007_alloc); |
| 643 | |
| 644 | /* |
| 645 | * Detach and unregister the encoder. The go7007 struct won't be freed |
| 646 | * until v4l2 finishes releasing its resources and all associated fds are |
| 647 | * closed by applications. |
| 648 | */ |
| 649 | void go7007_remove(struct go7007 *go) |
| 650 | { |
| 651 | if (go->i2c_adapter_online) { |
| 652 | if (i2c_del_adapter(&go->i2c_adapter) == 0) |
| 653 | go->i2c_adapter_online = 0; |
| 654 | else |
Pete Eberlein | 0b398f4 | 2009-11-16 15:07:42 -0300 | [diff] [blame] | 655 | v4l2_err(&go->v4l2_dev, |
Pete Eberlein | 6247407 | 2009-09-18 23:06:15 -0300 | [diff] [blame] | 656 | "error removing I2C adapter!\n"); |
Greg Kroah-Hartman | 866b869 | 2008-02-15 16:53:09 -0800 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | if (go->audio_enabled) |
| 660 | go7007_snd_remove(go); |
| 661 | go7007_v4l2_remove(go); |
| 662 | } |
| 663 | EXPORT_SYMBOL(go7007_remove); |
| 664 | |
| 665 | MODULE_LICENSE("GPL v2"); |