go7007-driver.c 19 KB

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