cx18-streams.c 20 KB

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