cx18-streams.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. /*
  2. * cx18 init/start/stop/exit stream functions
  3. *
  4. * Derived from ivtv-streams.c
  5. *
  6. * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
  7. * Copyright (C) 2008 Andy Walls <awalls@radix.net>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  22. * 02111-1307 USA
  23. */
  24. #include "cx18-driver.h"
  25. #include "cx18-io.h"
  26. #include "cx18-fileops.h"
  27. #include "cx18-mailbox.h"
  28. #include "cx18-i2c.h"
  29. #include "cx18-queue.h"
  30. #include "cx18-ioctl.h"
  31. #include "cx18-streams.h"
  32. #include "cx18-cards.h"
  33. #include "cx18-scb.h"
  34. #include "cx18-dvb.h"
  35. #define CX18_DSP0_INTERRUPT_MASK 0xd0004C
  36. static struct v4l2_file_operations cx18_v4l2_enc_fops = {
  37. .owner = THIS_MODULE,
  38. .read = cx18_v4l2_read,
  39. .open = cx18_v4l2_open,
  40. /* FIXME change to video_ioctl2 if serialization lock can be removed */
  41. .ioctl = cx18_v4l2_ioctl,
  42. .release = cx18_v4l2_close,
  43. .poll = cx18_v4l2_enc_poll,
  44. };
  45. /* offset from 0 to register ts v4l2 minors on */
  46. #define CX18_V4L2_ENC_TS_OFFSET 16
  47. /* offset from 0 to register pcm v4l2 minors on */
  48. #define CX18_V4L2_ENC_PCM_OFFSET 24
  49. /* offset from 0 to register yuv v4l2 minors on */
  50. #define CX18_V4L2_ENC_YUV_OFFSET 32
  51. static struct {
  52. const char *name;
  53. int vfl_type;
  54. int num_offset;
  55. int dma;
  56. enum v4l2_buf_type buf_type;
  57. } cx18_stream_info[] = {
  58. { /* CX18_ENC_STREAM_TYPE_MPG */
  59. "encoder MPEG",
  60. VFL_TYPE_GRABBER, 0,
  61. PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_VIDEO_CAPTURE,
  62. },
  63. { /* CX18_ENC_STREAM_TYPE_TS */
  64. "TS",
  65. VFL_TYPE_GRABBER, -1,
  66. PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_VIDEO_CAPTURE,
  67. },
  68. { /* CX18_ENC_STREAM_TYPE_YUV */
  69. "encoder YUV",
  70. VFL_TYPE_GRABBER, CX18_V4L2_ENC_YUV_OFFSET,
  71. PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_VIDEO_CAPTURE,
  72. },
  73. { /* CX18_ENC_STREAM_TYPE_VBI */
  74. "encoder VBI",
  75. VFL_TYPE_VBI, 0,
  76. PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_VBI_CAPTURE,
  77. },
  78. { /* CX18_ENC_STREAM_TYPE_PCM */
  79. "encoder PCM audio",
  80. VFL_TYPE_GRABBER, CX18_V4L2_ENC_PCM_OFFSET,
  81. PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_PRIVATE,
  82. },
  83. { /* CX18_ENC_STREAM_TYPE_IDX */
  84. "encoder IDX",
  85. VFL_TYPE_GRABBER, -1,
  86. PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_VIDEO_CAPTURE,
  87. },
  88. { /* CX18_ENC_STREAM_TYPE_RAD */
  89. "encoder radio",
  90. VFL_TYPE_RADIO, 0,
  91. PCI_DMA_NONE, V4L2_BUF_TYPE_PRIVATE,
  92. },
  93. };
  94. static void cx18_stream_init(struct cx18 *cx, int type)
  95. {
  96. struct cx18_stream *s = &cx->streams[type];
  97. struct video_device *video_dev = s->video_dev;
  98. /* we need to keep video_dev, so restore it afterwards */
  99. memset(s, 0, sizeof(*s));
  100. s->video_dev = video_dev;
  101. /* initialize cx18_stream fields */
  102. s->cx = cx;
  103. s->type = type;
  104. s->name = cx18_stream_info[type].name;
  105. s->handle = CX18_INVALID_TASK_HANDLE;
  106. s->dma = cx18_stream_info[type].dma;
  107. s->buffers = cx->stream_buffers[type];
  108. s->buf_size = cx->stream_buf_size[type];
  109. mutex_init(&s->qlock);
  110. init_waitqueue_head(&s->waitq);
  111. s->id = -1;
  112. cx18_queue_init(&s->q_free);
  113. cx18_queue_init(&s->q_busy);
  114. cx18_queue_init(&s->q_full);
  115. }
  116. static int cx18_prep_dev(struct cx18 *cx, int type)
  117. {
  118. struct cx18_stream *s = &cx->streams[type];
  119. u32 cap = cx->v4l2_cap;
  120. int num_offset = cx18_stream_info[type].num_offset;
  121. int num = cx->instance + cx18_first_minor + num_offset;
  122. /* These four fields are always initialized. If video_dev == NULL, then
  123. this stream is not in use. In that case no other fields but these
  124. four can be used. */
  125. s->video_dev = NULL;
  126. s->cx = cx;
  127. s->type = type;
  128. s->name = cx18_stream_info[type].name;
  129. /* Check whether the radio is supported */
  130. if (type == CX18_ENC_STREAM_TYPE_RAD && !(cap & V4L2_CAP_RADIO))
  131. return 0;
  132. /* Check whether VBI is supported */
  133. if (type == CX18_ENC_STREAM_TYPE_VBI &&
  134. !(cap & (V4L2_CAP_VBI_CAPTURE | V4L2_CAP_SLICED_VBI_CAPTURE)))
  135. return 0;
  136. /* User explicitly selected 0 buffers for these streams, so don't
  137. create them. */
  138. if (cx18_stream_info[type].dma != PCI_DMA_NONE &&
  139. cx->stream_buffers[type] == 0) {
  140. CX18_INFO("Disabled %s device\n", cx18_stream_info[type].name);
  141. return 0;
  142. }
  143. cx18_stream_init(cx, type);
  144. if (num_offset == -1)
  145. return 0;
  146. /* allocate and initialize the v4l2 video device structure */
  147. s->video_dev = video_device_alloc();
  148. if (s->video_dev == NULL) {
  149. CX18_ERR("Couldn't allocate v4l2 video_device for %s\n",
  150. s->name);
  151. return -ENOMEM;
  152. }
  153. snprintf(s->video_dev->name, sizeof(s->video_dev->name), "%s %s",
  154. cx->v4l2_dev.name, s->name);
  155. s->video_dev->num = num;
  156. s->video_dev->v4l2_dev = &cx->v4l2_dev;
  157. s->video_dev->fops = &cx18_v4l2_enc_fops;
  158. s->video_dev->release = video_device_release;
  159. s->video_dev->tvnorms = V4L2_STD_ALL;
  160. cx18_set_funcs(s->video_dev);
  161. return 0;
  162. }
  163. /* Initialize v4l2 variables and register v4l2 devices */
  164. int cx18_streams_setup(struct cx18 *cx)
  165. {
  166. int type, ret;
  167. /* Setup V4L2 Devices */
  168. for (type = 0; type < CX18_MAX_STREAMS; type++) {
  169. /* Prepare device */
  170. ret = cx18_prep_dev(cx, type);
  171. if (ret < 0)
  172. break;
  173. /* Allocate Stream */
  174. ret = cx18_stream_alloc(&cx->streams[type]);
  175. if (ret < 0)
  176. break;
  177. }
  178. if (type == CX18_MAX_STREAMS)
  179. return 0;
  180. /* One or more streams could not be initialized. Clean 'em all up. */
  181. cx18_streams_cleanup(cx, 0);
  182. return ret;
  183. }
  184. static int cx18_reg_dev(struct cx18 *cx, int type)
  185. {
  186. struct cx18_stream *s = &cx->streams[type];
  187. int vfl_type = cx18_stream_info[type].vfl_type;
  188. int num, ret;
  189. /* TODO: Shouldn't this be a VFL_TYPE_TRANSPORT or something?
  190. * We need a VFL_TYPE_TS defined.
  191. */
  192. if (strcmp("TS", s->name) == 0) {
  193. /* just return if no DVB is supported */
  194. if ((cx->card->hw_all & CX18_HW_DVB) == 0)
  195. return 0;
  196. ret = cx18_dvb_register(s);
  197. if (ret < 0) {
  198. CX18_ERR("DVB failed to register\n");
  199. return ret;
  200. }
  201. }
  202. if (s->video_dev == NULL)
  203. return 0;
  204. num = s->video_dev->num;
  205. /* card number + user defined offset + device offset */
  206. if (type != CX18_ENC_STREAM_TYPE_MPG) {
  207. struct cx18_stream *s_mpg = &cx->streams[CX18_ENC_STREAM_TYPE_MPG];
  208. if (s_mpg->video_dev)
  209. num = s_mpg->video_dev->num
  210. + cx18_stream_info[type].num_offset;
  211. }
  212. video_set_drvdata(s->video_dev, s);
  213. /* Register device. First try the desired minor, then any free one. */
  214. ret = video_register_device(s->video_dev, vfl_type, num);
  215. if (ret < 0) {
  216. CX18_ERR("Couldn't register v4l2 device for %s kernel number %d\n",
  217. s->name, num);
  218. video_device_release(s->video_dev);
  219. s->video_dev = NULL;
  220. return ret;
  221. }
  222. num = s->video_dev->num;
  223. switch (vfl_type) {
  224. case VFL_TYPE_GRABBER:
  225. CX18_INFO("Registered device video%d for %s (%d x %d kB)\n",
  226. num, s->name, cx->stream_buffers[type],
  227. cx->stream_buf_size[type]/1024);
  228. break;
  229. case VFL_TYPE_RADIO:
  230. CX18_INFO("Registered device radio%d for %s\n",
  231. num, s->name);
  232. break;
  233. case VFL_TYPE_VBI:
  234. if (cx->stream_buffers[type])
  235. CX18_INFO("Registered device vbi%d for %s "
  236. "(%d x %d bytes)\n",
  237. num, s->name, cx->stream_buffers[type],
  238. cx->stream_buf_size[type]);
  239. else
  240. CX18_INFO("Registered device vbi%d for %s\n",
  241. num, s->name);
  242. break;
  243. }
  244. return 0;
  245. }
  246. /* Register v4l2 devices */
  247. int cx18_streams_register(struct cx18 *cx)
  248. {
  249. int type;
  250. int err;
  251. int ret = 0;
  252. /* Register V4L2 devices */
  253. for (type = 0; type < CX18_MAX_STREAMS; type++) {
  254. err = cx18_reg_dev(cx, type);
  255. if (err && ret == 0)
  256. ret = err;
  257. }
  258. if (ret == 0)
  259. return 0;
  260. /* One or more streams could not be initialized. Clean 'em all up. */
  261. cx18_streams_cleanup(cx, 1);
  262. return ret;
  263. }
  264. /* Unregister v4l2 devices */
  265. void cx18_streams_cleanup(struct cx18 *cx, int unregister)
  266. {
  267. struct video_device *vdev;
  268. int type;
  269. /* Teardown all streams */
  270. for (type = 0; type < CX18_MAX_STREAMS; type++) {
  271. if (cx->streams[type].dvb.enabled) {
  272. cx18_dvb_unregister(&cx->streams[type]);
  273. cx->streams[type].dvb.enabled = false;
  274. }
  275. vdev = cx->streams[type].video_dev;
  276. cx->streams[type].video_dev = NULL;
  277. if (vdev == NULL)
  278. continue;
  279. cx18_stream_free(&cx->streams[type]);
  280. /* Unregister or release device */
  281. if (unregister)
  282. video_unregister_device(vdev);
  283. else
  284. video_device_release(vdev);
  285. }
  286. }
  287. static void cx18_vbi_setup(struct cx18_stream *s)
  288. {
  289. struct cx18 *cx = s->cx;
  290. int raw = cx18_raw_vbi(cx);
  291. u32 data[CX2341X_MBOX_MAX_DATA];
  292. int lines;
  293. if (cx->is_60hz) {
  294. cx->vbi.count = 12;
  295. cx->vbi.start[0] = 10;
  296. cx->vbi.start[1] = 273;
  297. } else { /* PAL/SECAM */
  298. cx->vbi.count = 18;
  299. cx->vbi.start[0] = 6;
  300. cx->vbi.start[1] = 318;
  301. }
  302. /* setup VBI registers */
  303. v4l2_subdev_call(cx->sd_av, video, s_fmt, &cx->vbi.in);
  304. /*
  305. * Send the CX18_CPU_SET_RAW_VBI_PARAM API command to setup Encoder Raw
  306. * VBI when the first analog capture channel starts, as once it starts
  307. * (e.g. MPEG), we can't effect any change in the Encoder Raw VBI setup
  308. * (i.e. for the VBI capture channels). We also send it for each
  309. * analog capture channel anyway just to make sure we get the proper
  310. * behavior
  311. */
  312. if (raw) {
  313. lines = cx->vbi.count * 2;
  314. } else {
  315. /*
  316. * For 525/60 systems, according to the VIP 2 & BT.656 std:
  317. * The EAV RP code's Field bit toggles on line 4, a few lines
  318. * after the Vertcal Blank bit has already toggled.
  319. * Tell the encoder to capture 21-4+1=18 lines per field,
  320. * since we want lines 10 through 21.
  321. *
  322. * FIXME - revisit for 625/50 systems
  323. */
  324. lines = cx->is_60hz ? (21 - 4 + 1) * 2 : 38;
  325. }
  326. data[0] = s->handle;
  327. /* Lines per field */
  328. data[1] = (lines / 2) | ((lines / 2) << 16);
  329. /* bytes per line */
  330. data[2] = (raw ? vbi_active_samples
  331. : (cx->is_60hz ? vbi_hblank_samples_60Hz
  332. : vbi_hblank_samples_50Hz));
  333. /* Every X number of frames a VBI interrupt arrives
  334. (frames as in 25 or 30 fps) */
  335. data[3] = 1;
  336. /*
  337. * Set the SAV/EAV RP codes to look for as start/stop points
  338. * when in VIP-1.1 mode
  339. */
  340. if (raw) {
  341. /*
  342. * Start codes for beginning of "active" line in vertical blank
  343. * 0x20 ( VerticalBlank )
  344. * 0x60 ( EvenField VerticalBlank )
  345. */
  346. data[4] = 0x20602060;
  347. /*
  348. * End codes for end of "active" raw lines and regular lines
  349. * 0x30 ( VerticalBlank HorizontalBlank)
  350. * 0x70 ( EvenField VerticalBlank HorizontalBlank)
  351. * 0x90 (Task HorizontalBlank)
  352. * 0xd0 (Task EvenField HorizontalBlank)
  353. */
  354. data[5] = 0x307090d0;
  355. } else {
  356. /*
  357. * End codes for active video, we want data in the hblank region
  358. * 0xb0 (Task 0 VerticalBlank HorizontalBlank)
  359. * 0xf0 (Task EvenField VerticalBlank HorizontalBlank)
  360. *
  361. * Since the V bit is only allowed to toggle in the EAV RP code,
  362. * just before the first active region line, these two
  363. * are problematic:
  364. * 0x90 (Task HorizontalBlank)
  365. * 0xd0 (Task EvenField HorizontalBlank)
  366. *
  367. * We have set the digitzer to consider the first active line
  368. * as part of VerticalBlank as well so we don't have to look for
  369. * these problem codes nor lose the last line of sliced data.
  370. */
  371. data[4] = 0xB0F0B0F0;
  372. /*
  373. * Start codes for beginning of active line in vertical blank
  374. * 0xa0 (Task VerticalBlank )
  375. * 0xe0 (Task EvenField VerticalBlank )
  376. */
  377. data[5] = 0xA0E0A0E0;
  378. }
  379. CX18_DEBUG_INFO("Setup VBI h: %d lines %x bpl %d fr %d %x %x\n",
  380. data[0], data[1], data[2], data[3], data[4], data[5]);
  381. cx18_api(cx, CX18_CPU_SET_RAW_VBI_PARAM, 6, data);
  382. }
  383. struct cx18_queue *cx18_stream_put_buf_fw(struct cx18_stream *s,
  384. struct cx18_buffer *buf)
  385. {
  386. struct cx18 *cx = s->cx;
  387. struct cx18_queue *q;
  388. /* Don't give it to the firmware, if we're not running a capture */
  389. if (s->handle == CX18_INVALID_TASK_HANDLE ||
  390. !test_bit(CX18_F_S_STREAMING, &s->s_flags))
  391. return cx18_enqueue(s, buf, &s->q_free);
  392. q = cx18_enqueue(s, buf, &s->q_busy);
  393. if (q != &s->q_busy)
  394. return q; /* The firmware has the max buffers it can handle */
  395. cx18_buf_sync_for_device(s, buf);
  396. cx18_vapi(cx, CX18_CPU_DE_SET_MDL, 5, s->handle,
  397. (void __iomem *) &cx->scb->cpu_mdl[buf->id] - cx->enc_mem,
  398. 1, buf->id, s->buf_size);
  399. return q;
  400. }
  401. void cx18_stream_load_fw_queue(struct cx18_stream *s)
  402. {
  403. struct cx18_queue *q;
  404. struct cx18_buffer *buf;
  405. if (atomic_read(&s->q_free.buffers) == 0 ||
  406. atomic_read(&s->q_busy.buffers) >= CX18_MAX_FW_MDLS_PER_STREAM)
  407. return;
  408. /* Move from q_free to q_busy notifying the firmware, until the limit */
  409. do {
  410. buf = cx18_dequeue(s, &s->q_free);
  411. if (buf == NULL)
  412. break;
  413. q = cx18_stream_put_buf_fw(s, buf);
  414. } while (atomic_read(&s->q_busy.buffers) < CX18_MAX_FW_MDLS_PER_STREAM
  415. && q == &s->q_busy);
  416. }
  417. int cx18_start_v4l2_encode_stream(struct cx18_stream *s)
  418. {
  419. u32 data[MAX_MB_ARGUMENTS];
  420. struct cx18 *cx = s->cx;
  421. struct cx18_buffer *buf;
  422. int captype = 0;
  423. struct cx18_api_func_private priv;
  424. if (s->video_dev == NULL && s->dvb.enabled == 0)
  425. return -EINVAL;
  426. CX18_DEBUG_INFO("Start encoder stream %s\n", s->name);
  427. switch (s->type) {
  428. case CX18_ENC_STREAM_TYPE_MPG:
  429. captype = CAPTURE_CHANNEL_TYPE_MPEG;
  430. cx->mpg_data_received = cx->vbi_data_inserted = 0;
  431. cx->dualwatch_jiffies = jiffies;
  432. cx->dualwatch_stereo_mode = cx->params.audio_properties & 0x300;
  433. cx->search_pack_header = 0;
  434. break;
  435. case CX18_ENC_STREAM_TYPE_TS:
  436. captype = CAPTURE_CHANNEL_TYPE_TS;
  437. break;
  438. case CX18_ENC_STREAM_TYPE_YUV:
  439. captype = CAPTURE_CHANNEL_TYPE_YUV;
  440. break;
  441. case CX18_ENC_STREAM_TYPE_PCM:
  442. captype = CAPTURE_CHANNEL_TYPE_PCM;
  443. break;
  444. case CX18_ENC_STREAM_TYPE_VBI:
  445. #ifdef CX18_ENCODER_PARSES_SLICED
  446. captype = cx18_raw_vbi(cx) ?
  447. CAPTURE_CHANNEL_TYPE_VBI : CAPTURE_CHANNEL_TYPE_SLICED_VBI;
  448. #else
  449. /*
  450. * Currently we set things up so that Sliced VBI from the
  451. * digitizer is handled as Raw VBI by the encoder
  452. */
  453. captype = CAPTURE_CHANNEL_TYPE_VBI;
  454. #endif
  455. cx->vbi.frame = 0;
  456. cx->vbi.inserted_frame = 0;
  457. memset(cx->vbi.sliced_mpeg_size,
  458. 0, sizeof(cx->vbi.sliced_mpeg_size));
  459. break;
  460. default:
  461. return -EINVAL;
  462. }
  463. /* Clear Streamoff flags in case left from last capture */
  464. clear_bit(CX18_F_S_STREAMOFF, &s->s_flags);
  465. cx18_vapi_result(cx, data, CX18_CREATE_TASK, 1, CPU_CMD_MASK_CAPTURE);
  466. s->handle = data[0];
  467. cx18_vapi(cx, CX18_CPU_SET_CHANNEL_TYPE, 2, s->handle, captype);
  468. /*
  469. * For everything but CAPTURE_CHANNEL_TYPE_TS, play it safe and
  470. * set up all the parameters, as it is not obvious which parameters the
  471. * firmware shares across capture channel types and which it does not.
  472. *
  473. * Some of the cx18_vapi() calls below apply to only certain capture
  474. * channel types. We're hoping there's no harm in calling most of them
  475. * anyway, as long as the values are all consistent. Setting some
  476. * shared parameters will have no effect once an analog capture channel
  477. * has started streaming.
  478. */
  479. if (captype != CAPTURE_CHANNEL_TYPE_TS) {
  480. cx18_vapi(cx, CX18_CPU_SET_VER_CROP_LINE, 2, s->handle, 0);
  481. cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 3, s->handle, 3, 1);
  482. cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 3, s->handle, 8, 0);
  483. cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 3, s->handle, 4, 1);
  484. /*
  485. * Audio related reset according to
  486. * Documentation/video4linux/cx2341x/fw-encoder-api.txt
  487. */
  488. if (atomic_read(&cx->ana_capturing) == 0)
  489. cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 2,
  490. s->handle, 12);
  491. /*
  492. * Number of lines for Field 1 & Field 2 according to
  493. * Documentation/video4linux/cx2341x/fw-encoder-api.txt
  494. * Field 1 is 312 for 625 line systems in BT.656
  495. * Field 2 is 313 for 625 line systems in BT.656
  496. */
  497. cx18_vapi(cx, CX18_CPU_SET_CAPTURE_LINE_NO, 3,
  498. s->handle, 312, 313);
  499. if (cx->v4l2_cap & V4L2_CAP_VBI_CAPTURE)
  500. cx18_vbi_setup(s);
  501. /*
  502. * assign program index info.
  503. * Mask 7: select I/P/B, Num_req: 400 max
  504. * FIXME - currently we have this hardcoded as disabled
  505. */
  506. cx18_vapi_result(cx, data, CX18_CPU_SET_INDEXTABLE, 1, 0);
  507. /* Call out to the common CX2341x API setup for user controls */
  508. priv.cx = cx;
  509. priv.s = s;
  510. cx2341x_update(&priv, cx18_api_func, NULL, &cx->params);
  511. /*
  512. * When starting a capture and we're set for radio,
  513. * ensure the video is muted, despite the user control.
  514. */
  515. if (!cx->params.video_mute &&
  516. test_bit(CX18_F_I_RADIO_USER, &cx->i_flags))
  517. cx18_vapi(cx, CX18_CPU_SET_VIDEO_MUTE, 2, s->handle,
  518. (cx->params.video_mute_yuv << 8) | 1);
  519. }
  520. if (atomic_read(&cx->tot_capturing) == 0) {
  521. clear_bit(CX18_F_I_EOS, &cx->i_flags);
  522. cx18_write_reg(cx, 7, CX18_DSP0_INTERRUPT_MASK);
  523. }
  524. cx18_vapi(cx, CX18_CPU_DE_SET_MDL_ACK, 3, s->handle,
  525. (void __iomem *)&cx->scb->cpu_mdl_ack[s->type][0] - cx->enc_mem,
  526. (void __iomem *)&cx->scb->cpu_mdl_ack[s->type][1] - cx->enc_mem);
  527. /* Init all the cpu_mdls for this stream */
  528. cx18_flush_queues(s);
  529. mutex_lock(&s->qlock);
  530. list_for_each_entry(buf, &s->q_free.list, list) {
  531. cx18_writel(cx, buf->dma_handle,
  532. &cx->scb->cpu_mdl[buf->id].paddr);
  533. cx18_writel(cx, s->buf_size, &cx->scb->cpu_mdl[buf->id].length);
  534. }
  535. mutex_unlock(&s->qlock);
  536. cx18_stream_load_fw_queue(s);
  537. /* begin_capture */
  538. if (cx18_vapi(cx, CX18_CPU_CAPTURE_START, 1, s->handle)) {
  539. CX18_DEBUG_WARN("Error starting capture!\n");
  540. /* Ensure we're really not capturing before releasing MDLs */
  541. if (s->type == CX18_ENC_STREAM_TYPE_MPG)
  542. cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 2, s->handle, 1);
  543. else
  544. cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 1, s->handle);
  545. clear_bit(CX18_F_S_STREAMING, &s->s_flags);
  546. /* FIXME - CX18_F_S_STREAMOFF as well? */
  547. cx18_vapi(cx, CX18_CPU_DE_RELEASE_MDL, 1, s->handle);
  548. cx18_vapi(cx, CX18_DESTROY_TASK, 1, s->handle);
  549. s->handle = CX18_INVALID_TASK_HANDLE;
  550. if (atomic_read(&cx->tot_capturing) == 0) {
  551. set_bit(CX18_F_I_EOS, &cx->i_flags);
  552. cx18_write_reg(cx, 5, CX18_DSP0_INTERRUPT_MASK);
  553. }
  554. return -EINVAL;
  555. }
  556. /* you're live! sit back and await interrupts :) */
  557. if (captype != CAPTURE_CHANNEL_TYPE_TS)
  558. atomic_inc(&cx->ana_capturing);
  559. atomic_inc(&cx->tot_capturing);
  560. return 0;
  561. }
  562. void cx18_stop_all_captures(struct cx18 *cx)
  563. {
  564. int i;
  565. for (i = CX18_MAX_STREAMS - 1; i >= 0; i--) {
  566. struct cx18_stream *s = &cx->streams[i];
  567. if (s->video_dev == NULL && s->dvb.enabled == 0)
  568. continue;
  569. if (test_bit(CX18_F_S_STREAMING, &s->s_flags))
  570. cx18_stop_v4l2_encode_stream(s, 0);
  571. }
  572. }
  573. int cx18_stop_v4l2_encode_stream(struct cx18_stream *s, int gop_end)
  574. {
  575. struct cx18 *cx = s->cx;
  576. unsigned long then;
  577. if (s->video_dev == NULL && s->dvb.enabled == 0)
  578. return -EINVAL;
  579. /* This function assumes that you are allowed to stop the capture
  580. and that we are actually capturing */
  581. CX18_DEBUG_INFO("Stop Capture\n");
  582. if (atomic_read(&cx->tot_capturing) == 0)
  583. return 0;
  584. if (s->type == CX18_ENC_STREAM_TYPE_MPG)
  585. cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 2, s->handle, !gop_end);
  586. else
  587. cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 1, s->handle);
  588. then = jiffies;
  589. if (s->type == CX18_ENC_STREAM_TYPE_MPG && gop_end) {
  590. CX18_INFO("ignoring gop_end: not (yet?) supported by the firmware\n");
  591. }
  592. if (s->type != CX18_ENC_STREAM_TYPE_TS)
  593. atomic_dec(&cx->ana_capturing);
  594. atomic_dec(&cx->tot_capturing);
  595. /* Clear capture and no-read bits */
  596. clear_bit(CX18_F_S_STREAMING, &s->s_flags);
  597. /* Tell the CX23418 it can't use our buffers anymore */
  598. cx18_vapi(cx, CX18_CPU_DE_RELEASE_MDL, 1, s->handle);
  599. cx18_vapi(cx, CX18_DESTROY_TASK, 1, s->handle);
  600. s->handle = CX18_INVALID_TASK_HANDLE;
  601. if (atomic_read(&cx->tot_capturing) > 0)
  602. return 0;
  603. cx18_write_reg(cx, 5, CX18_DSP0_INTERRUPT_MASK);
  604. wake_up(&s->waitq);
  605. return 0;
  606. }
  607. u32 cx18_find_handle(struct cx18 *cx)
  608. {
  609. int i;
  610. /* find first available handle to be used for global settings */
  611. for (i = 0; i < CX18_MAX_STREAMS; i++) {
  612. struct cx18_stream *s = &cx->streams[i];
  613. if (s->video_dev && (s->handle != CX18_INVALID_TASK_HANDLE))
  614. return s->handle;
  615. }
  616. return CX18_INVALID_TASK_HANDLE;
  617. }
  618. struct cx18_stream *cx18_handle_to_stream(struct cx18 *cx, u32 handle)
  619. {
  620. int i;
  621. struct cx18_stream *s;
  622. if (handle == CX18_INVALID_TASK_HANDLE)
  623. return NULL;
  624. for (i = 0; i < CX18_MAX_STREAMS; i++) {
  625. s = &cx->streams[i];
  626. if (s->handle != handle)
  627. continue;
  628. if (s->video_dev || s->dvb.enabled)
  629. return s;
  630. }
  631. return NULL;
  632. }