cx18-streams.c 16 KB

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