cx18-streams.c 22 KB

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