go7007-driver.c 19 KB

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