cx18-streams.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  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. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  21. * 02111-1307 USA
  22. */
  23. #include "cx18-driver.h"
  24. #include "cx18-io.h"
  25. #include "cx18-fileops.h"
  26. #include "cx18-mailbox.h"
  27. #include "cx18-i2c.h"
  28. #include "cx18-queue.h"
  29. #include "cx18-ioctl.h"
  30. #include "cx18-streams.h"
  31. #include "cx18-cards.h"
  32. #include "cx18-scb.h"
  33. #include "cx18-av-core.h"
  34. #include "cx18-dvb.h"
  35. #define CX18_DSP0_INTERRUPT_MASK 0xd0004C
  36. static struct 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. .compat_ioctl = v4l_compat_ioctl32,
  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. struct file_operations *fops;
  59. } cx18_stream_info[] = {
  60. { /* CX18_ENC_STREAM_TYPE_MPG */
  61. "encoder MPEG",
  62. VFL_TYPE_GRABBER, 0,
  63. PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_VIDEO_CAPTURE,
  64. &cx18_v4l2_enc_fops
  65. },
  66. { /* CX18_ENC_STREAM_TYPE_TS */
  67. "TS",
  68. VFL_TYPE_GRABBER, -1,
  69. PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_VIDEO_CAPTURE,
  70. &cx18_v4l2_enc_fops
  71. },
  72. { /* CX18_ENC_STREAM_TYPE_YUV */
  73. "encoder YUV",
  74. VFL_TYPE_GRABBER, CX18_V4L2_ENC_YUV_OFFSET,
  75. PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_VIDEO_CAPTURE,
  76. &cx18_v4l2_enc_fops
  77. },
  78. { /* CX18_ENC_STREAM_TYPE_VBI */
  79. "encoder VBI",
  80. VFL_TYPE_VBI, 0,
  81. PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_VBI_CAPTURE,
  82. &cx18_v4l2_enc_fops
  83. },
  84. { /* CX18_ENC_STREAM_TYPE_PCM */
  85. "encoder PCM audio",
  86. VFL_TYPE_GRABBER, CX18_V4L2_ENC_PCM_OFFSET,
  87. PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_PRIVATE,
  88. &cx18_v4l2_enc_fops
  89. },
  90. { /* CX18_ENC_STREAM_TYPE_IDX */
  91. "encoder IDX",
  92. VFL_TYPE_GRABBER, -1,
  93. PCI_DMA_FROMDEVICE, V4L2_BUF_TYPE_VIDEO_CAPTURE,
  94. &cx18_v4l2_enc_fops
  95. },
  96. { /* CX18_ENC_STREAM_TYPE_RAD */
  97. "encoder radio",
  98. VFL_TYPE_RADIO, 0,
  99. PCI_DMA_NONE, V4L2_BUF_TYPE_PRIVATE,
  100. &cx18_v4l2_enc_fops
  101. },
  102. };
  103. static void cx18_stream_init(struct cx18 *cx, int type)
  104. {
  105. struct cx18_stream *s = &cx->streams[type];
  106. struct video_device *dev = s->v4l2dev;
  107. u32 max_size = cx->options.megabytes[type] * 1024 * 1024;
  108. /* we need to keep v4l2dev, so restore it afterwards */
  109. memset(s, 0, sizeof(*s));
  110. s->v4l2dev = dev;
  111. /* initialize cx18_stream fields */
  112. s->cx = cx;
  113. s->type = type;
  114. s->name = cx18_stream_info[type].name;
  115. s->handle = CX18_INVALID_TASK_HANDLE;
  116. s->dma = cx18_stream_info[type].dma;
  117. s->buf_size = cx->stream_buf_size[type];
  118. if (s->buf_size)
  119. s->buffers = max_size / s->buf_size;
  120. if (s->buffers > 63) {
  121. /* Each stream has a maximum of 63 buffers,
  122. ensure we do not exceed that. */
  123. s->buffers = 63;
  124. s->buf_size = (max_size / s->buffers) & ~0xfff;
  125. }
  126. spin_lock_init(&s->qlock);
  127. init_waitqueue_head(&s->waitq);
  128. s->id = -1;
  129. cx18_queue_init(&s->q_free);
  130. cx18_queue_init(&s->q_full);
  131. cx18_queue_init(&s->q_io);
  132. }
  133. static int cx18_prep_dev(struct cx18 *cx, int type)
  134. {
  135. struct cx18_stream *s = &cx->streams[type];
  136. u32 cap = cx->v4l2_cap;
  137. int num_offset = cx18_stream_info[type].num_offset;
  138. int num = cx->num + cx18_first_minor + num_offset;
  139. /* These four fields are always initialized. If v4l2dev == NULL, then
  140. this stream is not in use. In that case no other fields but these
  141. four can be used. */
  142. s->v4l2dev = NULL;
  143. s->cx = cx;
  144. s->type = type;
  145. s->name = cx18_stream_info[type].name;
  146. /* Check whether the radio is supported */
  147. if (type == CX18_ENC_STREAM_TYPE_RAD && !(cap & V4L2_CAP_RADIO))
  148. return 0;
  149. /* Check whether VBI is supported */
  150. if (type == CX18_ENC_STREAM_TYPE_VBI &&
  151. !(cap & (V4L2_CAP_VBI_CAPTURE | V4L2_CAP_SLICED_VBI_CAPTURE)))
  152. return 0;
  153. /* User explicitly selected 0 buffers for these streams, so don't
  154. create them. */
  155. if (cx18_stream_info[type].dma != PCI_DMA_NONE &&
  156. cx->options.megabytes[type] == 0) {
  157. CX18_INFO("Disabled %s device\n", cx18_stream_info[type].name);
  158. return 0;
  159. }
  160. cx18_stream_init(cx, type);
  161. if (num_offset == -1)
  162. return 0;
  163. /* allocate and initialize the v4l2 video device structure */
  164. s->v4l2dev = video_device_alloc();
  165. if (s->v4l2dev == NULL) {
  166. CX18_ERR("Couldn't allocate v4l2 video_device for %s\n",
  167. s->name);
  168. return -ENOMEM;
  169. }
  170. snprintf(s->v4l2dev->name, sizeof(s->v4l2dev->name), "cx18-%d",
  171. cx->num);
  172. s->v4l2dev->num = num;
  173. s->v4l2dev->parent = &cx->dev->dev;
  174. s->v4l2dev->fops = cx18_stream_info[type].fops;
  175. s->v4l2dev->release = video_device_release;
  176. s->v4l2dev->tvnorms = V4L2_STD_ALL;
  177. cx18_set_funcs(s->v4l2dev);
  178. return 0;
  179. }
  180. /* Initialize v4l2 variables and register v4l2 devices */
  181. int cx18_streams_setup(struct cx18 *cx)
  182. {
  183. int type, ret;
  184. /* Setup V4L2 Devices */
  185. for (type = 0; type < CX18_MAX_STREAMS; type++) {
  186. /* Prepare device */
  187. ret = cx18_prep_dev(cx, type);
  188. if (ret < 0)
  189. break;
  190. /* Allocate Stream */
  191. ret = cx18_stream_alloc(&cx->streams[type]);
  192. if (ret < 0)
  193. break;
  194. }
  195. if (type == CX18_MAX_STREAMS)
  196. return 0;
  197. /* One or more streams could not be initialized. Clean 'em all up. */
  198. cx18_streams_cleanup(cx, 0);
  199. return ret;
  200. }
  201. static int cx18_reg_dev(struct cx18 *cx, int type)
  202. {
  203. struct cx18_stream *s = &cx->streams[type];
  204. int vfl_type = cx18_stream_info[type].vfl_type;
  205. int num, ret;
  206. /* TODO: Shouldn't this be a VFL_TYPE_TRANSPORT or something?
  207. * We need a VFL_TYPE_TS defined.
  208. */
  209. if (strcmp("TS", s->name) == 0) {
  210. /* just return if no DVB is supported */
  211. if ((cx->card->hw_all & CX18_HW_DVB) == 0)
  212. return 0;
  213. ret = cx18_dvb_register(s);
  214. if (ret < 0) {
  215. CX18_ERR("DVB failed to register\n");
  216. return ret;
  217. }
  218. }
  219. if (s->v4l2dev == NULL)
  220. return 0;
  221. num = s->v4l2dev->num;
  222. /* card number + user defined offset + device offset */
  223. if (type != CX18_ENC_STREAM_TYPE_MPG) {
  224. struct cx18_stream *s_mpg = &cx->streams[CX18_ENC_STREAM_TYPE_MPG];
  225. if (s_mpg->v4l2dev)
  226. num = s_mpg->v4l2dev->num + cx18_stream_info[type].num_offset;
  227. }
  228. /* Register device. First try the desired minor, then any free one. */
  229. ret = video_register_device(s->v4l2dev, vfl_type, num);
  230. if (ret < 0) {
  231. CX18_ERR("Couldn't register v4l2 device for %s kernel number %d\n",
  232. s->name, num);
  233. video_device_release(s->v4l2dev);
  234. s->v4l2dev = NULL;
  235. return ret;
  236. }
  237. num = s->v4l2dev->num;
  238. switch (vfl_type) {
  239. case VFL_TYPE_GRABBER:
  240. CX18_INFO("Registered device video%d for %s (%d MB)\n",
  241. num, s->name, cx->options.megabytes[type]);
  242. break;
  243. case VFL_TYPE_RADIO:
  244. CX18_INFO("Registered device radio%d for %s\n",
  245. num, s->name);
  246. break;
  247. case VFL_TYPE_VBI:
  248. if (cx->options.megabytes[type])
  249. CX18_INFO("Registered device vbi%d for %s (%d MB)\n",
  250. num,
  251. s->name, cx->options.megabytes[type]);
  252. else
  253. CX18_INFO("Registered device vbi%d for %s\n",
  254. num, s->name);
  255. break;
  256. }
  257. return 0;
  258. }
  259. /* Register v4l2 devices */
  260. int cx18_streams_register(struct cx18 *cx)
  261. {
  262. int type;
  263. int err;
  264. int ret = 0;
  265. /* Register V4L2 devices */
  266. for (type = 0; type < CX18_MAX_STREAMS; type++) {
  267. err = cx18_reg_dev(cx, type);
  268. if (err && ret == 0)
  269. ret = err;
  270. }
  271. if (ret == 0)
  272. return 0;
  273. /* One or more streams could not be initialized. Clean 'em all up. */
  274. cx18_streams_cleanup(cx, 1);
  275. return ret;
  276. }
  277. /* Unregister v4l2 devices */
  278. void cx18_streams_cleanup(struct cx18 *cx, int unregister)
  279. {
  280. struct video_device *vdev;
  281. int type;
  282. /* Teardown all streams */
  283. for (type = 0; type < CX18_MAX_STREAMS; type++) {
  284. if (cx->streams[type].dvb.enabled) {
  285. cx18_dvb_unregister(&cx->streams[type]);
  286. cx->streams[type].dvb.enabled = false;
  287. }
  288. vdev = cx->streams[type].v4l2dev;
  289. cx->streams[type].v4l2dev = NULL;
  290. if (vdev == NULL)
  291. continue;
  292. cx18_stream_free(&cx->streams[type]);
  293. /* Unregister or release device */
  294. if (unregister)
  295. video_unregister_device(vdev);
  296. else
  297. video_device_release(vdev);
  298. }
  299. }
  300. static void cx18_vbi_setup(struct cx18_stream *s)
  301. {
  302. struct cx18 *cx = s->cx;
  303. int raw = cx->vbi.sliced_in->service_set == 0;
  304. u32 data[CX2341X_MBOX_MAX_DATA];
  305. int lines;
  306. if (cx->is_60hz) {
  307. cx->vbi.count = 12;
  308. cx->vbi.start[0] = 10;
  309. cx->vbi.start[1] = 273;
  310. } else { /* PAL/SECAM */
  311. cx->vbi.count = 18;
  312. cx->vbi.start[0] = 6;
  313. cx->vbi.start[1] = 318;
  314. }
  315. /* setup VBI registers */
  316. cx18_av_cmd(cx, VIDIOC_S_FMT, &cx->vbi.in);
  317. /* determine number of lines and total number of VBI bytes.
  318. A raw line takes 1443 bytes: 2 * 720 + 4 byte frame header - 1
  319. The '- 1' byte is probably an unused U or V byte. Or something...
  320. A sliced line takes 51 bytes: 4 byte frame header, 4 byte internal
  321. header, 42 data bytes + checksum (to be confirmed) */
  322. if (raw) {
  323. lines = cx->vbi.count * 2;
  324. } else {
  325. lines = cx->is_60hz ? 24 : 38;
  326. if (cx->is_60hz)
  327. lines += 2;
  328. }
  329. cx->vbi.enc_size = lines *
  330. (raw ? cx->vbi.raw_size : cx->vbi.sliced_size);
  331. data[0] = s->handle;
  332. /* Lines per field */
  333. data[1] = (lines / 2) | ((lines / 2) << 16);
  334. /* bytes per line */
  335. data[2] = (raw ? cx->vbi.raw_size : cx->vbi.sliced_size);
  336. /* Every X number of frames a VBI interrupt arrives
  337. (frames as in 25 or 30 fps) */
  338. data[3] = 1;
  339. /* Setup VBI for the cx25840 digitizer */
  340. if (raw) {
  341. data[4] = 0x20602060;
  342. data[5] = 0x30703070;
  343. } else {
  344. data[4] = 0xB0F0B0F0;
  345. data[5] = 0xA0E0A0E0;
  346. }
  347. CX18_DEBUG_INFO("Setup VBI h: %d lines %x bpl %d fr %d %x %x\n",
  348. data[0], data[1], data[2], data[3], data[4], data[5]);
  349. if (s->type == CX18_ENC_STREAM_TYPE_VBI)
  350. cx18_api(cx, CX18_CPU_SET_RAW_VBI_PARAM, 6, data);
  351. }
  352. int cx18_start_v4l2_encode_stream(struct cx18_stream *s)
  353. {
  354. u32 data[MAX_MB_ARGUMENTS];
  355. struct cx18 *cx = s->cx;
  356. struct list_head *p;
  357. int ts = 0;
  358. int captype = 0;
  359. if (s->v4l2dev == NULL && s->dvb.enabled == 0)
  360. return -EINVAL;
  361. CX18_DEBUG_INFO("Start encoder stream %s\n", s->name);
  362. switch (s->type) {
  363. case CX18_ENC_STREAM_TYPE_MPG:
  364. captype = CAPTURE_CHANNEL_TYPE_MPEG;
  365. cx->mpg_data_received = cx->vbi_data_inserted = 0;
  366. cx->dualwatch_jiffies = jiffies;
  367. cx->dualwatch_stereo_mode = cx->params.audio_properties & 0x300;
  368. cx->search_pack_header = 0;
  369. break;
  370. case CX18_ENC_STREAM_TYPE_TS:
  371. captype = CAPTURE_CHANNEL_TYPE_TS;
  372. ts = 1;
  373. break;
  374. case CX18_ENC_STREAM_TYPE_YUV:
  375. captype = CAPTURE_CHANNEL_TYPE_YUV;
  376. break;
  377. case CX18_ENC_STREAM_TYPE_PCM:
  378. captype = CAPTURE_CHANNEL_TYPE_PCM;
  379. break;
  380. case CX18_ENC_STREAM_TYPE_VBI:
  381. captype = cx->vbi.sliced_in->service_set ?
  382. CAPTURE_CHANNEL_TYPE_SLICED_VBI : CAPTURE_CHANNEL_TYPE_VBI;
  383. cx->vbi.frame = 0;
  384. cx->vbi.inserted_frame = 0;
  385. memset(cx->vbi.sliced_mpeg_size,
  386. 0, sizeof(cx->vbi.sliced_mpeg_size));
  387. break;
  388. default:
  389. return -EINVAL;
  390. }
  391. /* mute/unmute video */
  392. cx18_vapi(cx, CX18_CPU_SET_VIDEO_MUTE, 2,
  393. s->handle, !!test_bit(CX18_F_I_RADIO_USER, &cx->i_flags));
  394. /* Clear Streamoff flags in case left from last capture */
  395. clear_bit(CX18_F_S_STREAMOFF, &s->s_flags);
  396. cx18_vapi_result(cx, data, CX18_CREATE_TASK, 1, CPU_CMD_MASK_CAPTURE);
  397. s->handle = data[0];
  398. cx18_vapi(cx, CX18_CPU_SET_CHANNEL_TYPE, 2, s->handle, captype);
  399. if (atomic_read(&cx->ana_capturing) == 0 && !ts) {
  400. /* Stuff from Windows, we don't know what it is */
  401. cx18_vapi(cx, CX18_CPU_SET_VER_CROP_LINE, 2, s->handle, 0);
  402. cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 3, s->handle, 3, 1);
  403. cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 3, s->handle, 8, 0);
  404. cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 3, s->handle, 4, 1);
  405. cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 2, s->handle, 12);
  406. cx18_vapi(cx, CX18_CPU_SET_CAPTURE_LINE_NO, 3,
  407. s->handle, cx->digitizer, cx->digitizer);
  408. /* Setup VBI */
  409. if (cx->v4l2_cap & V4L2_CAP_VBI_CAPTURE)
  410. cx18_vbi_setup(s);
  411. /* assign program index info.
  412. Mask 7: select I/P/B, Num_req: 400 max */
  413. cx18_vapi_result(cx, data, CX18_CPU_SET_INDEXTABLE, 1, 0);
  414. /* Setup API for Stream */
  415. cx2341x_update(cx, cx18_api_func, NULL, &cx->params);
  416. }
  417. if (atomic_read(&cx->tot_capturing) == 0) {
  418. clear_bit(CX18_F_I_EOS, &cx->i_flags);
  419. cx18_write_reg(cx, 7, CX18_DSP0_INTERRUPT_MASK);
  420. }
  421. cx18_vapi(cx, CX18_CPU_DE_SET_MDL_ACK, 3, s->handle,
  422. (void __iomem *)&cx->scb->cpu_mdl_ack[s->type][0] - cx->enc_mem,
  423. (void __iomem *)&cx->scb->cpu_mdl_ack[s->type][1] - cx->enc_mem);
  424. list_for_each(p, &s->q_free.list) {
  425. struct cx18_buffer *buf = list_entry(p, struct cx18_buffer, list);
  426. cx18_writel(cx, buf->dma_handle,
  427. &cx->scb->cpu_mdl[buf->id].paddr);
  428. cx18_writel(cx, s->buf_size, &cx->scb->cpu_mdl[buf->id].length);
  429. cx18_vapi(cx, CX18_CPU_DE_SET_MDL, 5, s->handle,
  430. (void __iomem *)&cx->scb->cpu_mdl[buf->id] - cx->enc_mem,
  431. 1, buf->id, s->buf_size);
  432. }
  433. /* begin_capture */
  434. if (cx18_vapi(cx, CX18_CPU_CAPTURE_START, 1, s->handle)) {
  435. CX18_DEBUG_WARN("Error starting capture!\n");
  436. /* Ensure we're really not capturing before releasing MDLs */
  437. if (s->type == CX18_ENC_STREAM_TYPE_MPG)
  438. cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 2, s->handle, 1);
  439. else
  440. cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 1, s->handle);
  441. cx18_vapi(cx, CX18_CPU_DE_RELEASE_MDL, 1, s->handle);
  442. cx18_vapi(cx, CX18_DESTROY_TASK, 1, s->handle);
  443. /* FIXME - clean-up DSP0_INT mask, i_flags, s_flags, etc. */
  444. return -EINVAL;
  445. }
  446. /* you're live! sit back and await interrupts :) */
  447. if (!ts)
  448. atomic_inc(&cx->ana_capturing);
  449. atomic_inc(&cx->tot_capturing);
  450. return 0;
  451. }
  452. void cx18_stop_all_captures(struct cx18 *cx)
  453. {
  454. int i;
  455. for (i = CX18_MAX_STREAMS - 1; i >= 0; i--) {
  456. struct cx18_stream *s = &cx->streams[i];
  457. if (s->v4l2dev == NULL && s->dvb.enabled == 0)
  458. continue;
  459. if (test_bit(CX18_F_S_STREAMING, &s->s_flags))
  460. cx18_stop_v4l2_encode_stream(s, 0);
  461. }
  462. }
  463. int cx18_stop_v4l2_encode_stream(struct cx18_stream *s, int gop_end)
  464. {
  465. struct cx18 *cx = s->cx;
  466. unsigned long then;
  467. if (s->v4l2dev == NULL && s->dvb.enabled == 0)
  468. return -EINVAL;
  469. /* This function assumes that you are allowed to stop the capture
  470. and that we are actually capturing */
  471. CX18_DEBUG_INFO("Stop Capture\n");
  472. if (atomic_read(&cx->tot_capturing) == 0)
  473. return 0;
  474. if (s->type == CX18_ENC_STREAM_TYPE_MPG)
  475. cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 2, s->handle, !gop_end);
  476. else
  477. cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 1, s->handle);
  478. then = jiffies;
  479. if (s->type == CX18_ENC_STREAM_TYPE_MPG && gop_end) {
  480. CX18_INFO("ignoring gop_end: not (yet?) supported by the firmware\n");
  481. }
  482. if (s->type != CX18_ENC_STREAM_TYPE_TS)
  483. atomic_dec(&cx->ana_capturing);
  484. atomic_dec(&cx->tot_capturing);
  485. /* Clear capture and no-read bits */
  486. clear_bit(CX18_F_S_STREAMING, &s->s_flags);
  487. /* Tell the CX23418 it can't use our buffers anymore */
  488. cx18_vapi(cx, CX18_CPU_DE_RELEASE_MDL, 1, s->handle);
  489. cx18_vapi(cx, CX18_DESTROY_TASK, 1, s->handle);
  490. s->handle = CX18_INVALID_TASK_HANDLE;
  491. if (atomic_read(&cx->tot_capturing) > 0)
  492. return 0;
  493. cx18_write_reg(cx, 5, CX18_DSP0_INTERRUPT_MASK);
  494. wake_up(&s->waitq);
  495. return 0;
  496. }
  497. u32 cx18_find_handle(struct cx18 *cx)
  498. {
  499. int i;
  500. /* find first available handle to be used for global settings */
  501. for (i = 0; i < CX18_MAX_STREAMS; i++) {
  502. struct cx18_stream *s = &cx->streams[i];
  503. if (s->v4l2dev && (s->handle != CX18_INVALID_TASK_HANDLE))
  504. return s->handle;
  505. }
  506. return CX18_INVALID_TASK_HANDLE;
  507. }
  508. struct cx18_stream *cx18_handle_to_stream(struct cx18 *cx, u32 handle)
  509. {
  510. int i;
  511. struct cx18_stream *s;
  512. if (handle == CX18_INVALID_TASK_HANDLE)
  513. return NULL;
  514. for (i = 0; i < CX18_MAX_STREAMS; i++) {
  515. s = &cx->streams[i];
  516. if (s->handle != handle)
  517. continue;
  518. if (s->v4l2dev || s->dvb.enabled)
  519. return s;
  520. }
  521. return NULL;
  522. }