go7007-driver.c 18 KB

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