cx18-streams.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  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 (%d x %d kB)\n",
  234. num, s->name, cx->stream_buffers[type],
  235. cx->stream_buf_size[type]/1024);
  236. break;
  237. case VFL_TYPE_RADIO:
  238. CX18_INFO("Registered device radio%d for %s\n",
  239. num, s->name);
  240. break;
  241. case VFL_TYPE_VBI:
  242. if (cx->stream_buffers[type])
  243. CX18_INFO("Registered device vbi%d for %s "
  244. "(%d x %d bytes)\n",
  245. num, s->name, cx->stream_buffers[type],
  246. cx->stream_buf_size[type]);
  247. else
  248. CX18_INFO("Registered device vbi%d for %s\n",
  249. num, s->name);
  250. break;
  251. }
  252. return 0;
  253. }
  254. /* Register v4l2 devices */
  255. int cx18_streams_register(struct cx18 *cx)
  256. {
  257. int type;
  258. int err;
  259. int ret = 0;
  260. /* Register V4L2 devices */
  261. for (type = 0; type < CX18_MAX_STREAMS; type++) {
  262. err = cx18_reg_dev(cx, type);
  263. if (err && ret == 0)
  264. ret = err;
  265. }
  266. if (ret == 0)
  267. return 0;
  268. /* One or more streams could not be initialized. Clean 'em all up. */
  269. cx18_streams_cleanup(cx, 1);
  270. return ret;
  271. }
  272. /* Unregister v4l2 devices */
  273. void cx18_streams_cleanup(struct cx18 *cx, int unregister)
  274. {
  275. struct video_device *vdev;
  276. int type;
  277. /* Teardown all streams */
  278. for (type = 0; type < CX18_MAX_STREAMS; type++) {
  279. if (cx->streams[type].dvb.enabled) {
  280. cx18_dvb_unregister(&cx->streams[type]);
  281. cx->streams[type].dvb.enabled = false;
  282. }
  283. vdev = cx->streams[type].video_dev;
  284. cx->streams[type].video_dev = NULL;
  285. if (vdev == NULL)
  286. continue;
  287. cx18_stream_free(&cx->streams[type]);
  288. /* Unregister or release device */
  289. if (unregister)
  290. video_unregister_device(vdev);
  291. else
  292. video_device_release(vdev);
  293. }
  294. }
  295. static void cx18_vbi_setup(struct cx18_stream *s)
  296. {
  297. struct cx18 *cx = s->cx;
  298. int raw = cx18_raw_vbi(cx);
  299. u32 data[CX2341X_MBOX_MAX_DATA];
  300. int lines;
  301. if (cx->is_60hz) {
  302. cx->vbi.count = 12;
  303. cx->vbi.start[0] = 10;
  304. cx->vbi.start[1] = 273;
  305. } else { /* PAL/SECAM */
  306. cx->vbi.count = 18;
  307. cx->vbi.start[0] = 6;
  308. cx->vbi.start[1] = 318;
  309. }
  310. /* setup VBI registers */
  311. v4l2_subdev_call(cx->sd_av, video, s_fmt, &cx->vbi.in);
  312. /*
  313. * Send the CX18_CPU_SET_RAW_VBI_PARAM API command to setup Encoder Raw
  314. * VBI when the first analog capture channel starts, as once it starts
  315. * (e.g. MPEG), we can't effect any change in the Encoder Raw VBI setup
  316. * (i.e. for the VBI capture channels). We also send it for each
  317. * analog capture channel anyway just to make sure we get the proper
  318. * behavior
  319. */
  320. if (raw) {
  321. lines = cx->vbi.count * 2;
  322. } else {
  323. /*
  324. * For 525/60 systems, according to the VIP 2 & BT.656 std:
  325. * The EAV RP code's Field bit toggles on line 4, a few lines
  326. * after the Vertcal Blank bit has already toggled.
  327. * Tell the encoder to capture 21-4+1=18 lines per field,
  328. * since we want lines 10 through 21.
  329. *
  330. * For 625/50 systems, according to the VIP 2 & BT.656 std:
  331. * The EAV RP code's Field bit toggles on line 1, a few lines
  332. * after the Vertcal Blank bit has already toggled.
  333. * (We've actually set the digitizer so that the Field bit
  334. * toggles on line 2.) Tell the encoder to capture 23-2+1=22
  335. * lines per field, since we want lines 6 through 23.
  336. */
  337. lines = cx->is_60hz ? (21 - 4 + 1) * 2 : (23 - 2 + 1) * 2;
  338. }
  339. data[0] = s->handle;
  340. /* Lines per field */
  341. data[1] = (lines / 2) | ((lines / 2) << 16);
  342. /* bytes per line */
  343. data[2] = (raw ? vbi_active_samples
  344. : (cx->is_60hz ? vbi_hblank_samples_60Hz
  345. : vbi_hblank_samples_50Hz));
  346. /* Every X number of frames a VBI interrupt arrives
  347. (frames as in 25 or 30 fps) */
  348. data[3] = 1;
  349. /*
  350. * Set the SAV/EAV RP codes to look for as start/stop points
  351. * when in VIP-1.1 mode
  352. */
  353. if (raw) {
  354. /*
  355. * Start codes for beginning of "active" line in vertical blank
  356. * 0x20 ( VerticalBlank )
  357. * 0x60 ( EvenField VerticalBlank )
  358. */
  359. data[4] = 0x20602060;
  360. /*
  361. * End codes for end of "active" raw lines and regular lines
  362. * 0x30 ( VerticalBlank HorizontalBlank)
  363. * 0x70 ( EvenField VerticalBlank HorizontalBlank)
  364. * 0x90 (Task HorizontalBlank)
  365. * 0xd0 (Task EvenField HorizontalBlank)
  366. */
  367. data[5] = 0x307090d0;
  368. } else {
  369. /*
  370. * End codes for active video, we want data in the hblank region
  371. * 0xb0 (Task 0 VerticalBlank HorizontalBlank)
  372. * 0xf0 (Task EvenField VerticalBlank HorizontalBlank)
  373. *
  374. * Since the V bit is only allowed to toggle in the EAV RP code,
  375. * just before the first active region line, these two
  376. * are problematic:
  377. * 0x90 (Task HorizontalBlank)
  378. * 0xd0 (Task EvenField HorizontalBlank)
  379. *
  380. * We have set the digitzer such that we don't have to worry
  381. * about these problem codes.
  382. */
  383. data[4] = 0xB0F0B0F0;
  384. /*
  385. * Start codes for beginning of active line in vertical blank
  386. * 0xa0 (Task VerticalBlank )
  387. * 0xe0 (Task EvenField VerticalBlank )
  388. */
  389. data[5] = 0xA0E0A0E0;
  390. }
  391. CX18_DEBUG_INFO("Setup VBI h: %d lines %x bpl %d fr %d %x %x\n",
  392. data[0], data[1], data[2], data[3], data[4], data[5]);
  393. cx18_api(cx, CX18_CPU_SET_RAW_VBI_PARAM, 6, data);
  394. }
  395. static
  396. struct cx18_queue *_cx18_stream_put_mdl_fw(struct cx18_stream *s,
  397. struct cx18_mdl *mdl)
  398. {
  399. struct cx18 *cx = s->cx;
  400. struct cx18_queue *q;
  401. /* Don't give it to the firmware, if we're not running a capture */
  402. if (s->handle == CX18_INVALID_TASK_HANDLE ||
  403. test_bit(CX18_F_S_STOPPING, &s->s_flags) ||
  404. !test_bit(CX18_F_S_STREAMING, &s->s_flags))
  405. return cx18_enqueue(s, mdl, &s->q_free);
  406. q = cx18_enqueue(s, mdl, &s->q_busy);
  407. if (q != &s->q_busy)
  408. return q; /* The firmware has the max MDLs it can handle */
  409. cx18_mdl_sync_for_device(s, mdl);
  410. cx18_vapi(cx, CX18_CPU_DE_SET_MDL, 5, s->handle,
  411. (void __iomem *) &cx->scb->cpu_mdl[mdl->id] - cx->enc_mem,
  412. s->bufs_per_mdl, mdl->id, s->mdl_size);
  413. return q;
  414. }
  415. static
  416. void _cx18_stream_load_fw_queue(struct cx18_stream *s)
  417. {
  418. struct cx18_queue *q;
  419. struct cx18_mdl *mdl;
  420. if (atomic_read(&s->q_free.depth) == 0 ||
  421. atomic_read(&s->q_busy.depth) >= CX18_MAX_FW_MDLS_PER_STREAM)
  422. return;
  423. /* Move from q_free to q_busy notifying the firmware, until the limit */
  424. do {
  425. mdl = cx18_dequeue(s, &s->q_free);
  426. if (mdl == NULL)
  427. break;
  428. q = _cx18_stream_put_mdl_fw(s, mdl);
  429. } while (atomic_read(&s->q_busy.depth) < CX18_MAX_FW_MDLS_PER_STREAM
  430. && q == &s->q_busy);
  431. }
  432. void cx18_out_work_handler(struct work_struct *work)
  433. {
  434. struct cx18_stream *s =
  435. container_of(work, struct cx18_stream, out_work_order);
  436. _cx18_stream_load_fw_queue(s);
  437. }
  438. static void cx18_stream_configure_mdls(struct cx18_stream *s)
  439. {
  440. cx18_unload_queues(s);
  441. /* For now */
  442. s->bufs_per_mdl = 1;
  443. s->mdl_size = s->buf_size * s->bufs_per_mdl;
  444. cx18_load_queues(s);
  445. }
  446. int cx18_start_v4l2_encode_stream(struct cx18_stream *s)
  447. {
  448. u32 data[MAX_MB_ARGUMENTS];
  449. struct cx18 *cx = s->cx;
  450. int captype = 0;
  451. struct cx18_api_func_private priv;
  452. if (s->video_dev == NULL && s->dvb.enabled == 0)
  453. return -EINVAL;
  454. CX18_DEBUG_INFO("Start encoder stream %s\n", s->name);
  455. switch (s->type) {
  456. case CX18_ENC_STREAM_TYPE_MPG:
  457. captype = CAPTURE_CHANNEL_TYPE_MPEG;
  458. cx->mpg_data_received = cx->vbi_data_inserted = 0;
  459. cx->dualwatch_jiffies = jiffies;
  460. cx->dualwatch_stereo_mode = cx->params.audio_properties & 0x300;
  461. cx->search_pack_header = 0;
  462. break;
  463. case CX18_ENC_STREAM_TYPE_TS:
  464. captype = CAPTURE_CHANNEL_TYPE_TS;
  465. break;
  466. case CX18_ENC_STREAM_TYPE_YUV:
  467. captype = CAPTURE_CHANNEL_TYPE_YUV;
  468. break;
  469. case CX18_ENC_STREAM_TYPE_PCM:
  470. captype = CAPTURE_CHANNEL_TYPE_PCM;
  471. break;
  472. case CX18_ENC_STREAM_TYPE_VBI:
  473. #ifdef CX18_ENCODER_PARSES_SLICED
  474. captype = cx18_raw_vbi(cx) ?
  475. CAPTURE_CHANNEL_TYPE_VBI : CAPTURE_CHANNEL_TYPE_SLICED_VBI;
  476. #else
  477. /*
  478. * Currently we set things up so that Sliced VBI from the
  479. * digitizer is handled as Raw VBI by the encoder
  480. */
  481. captype = CAPTURE_CHANNEL_TYPE_VBI;
  482. #endif
  483. cx->vbi.frame = 0;
  484. cx->vbi.inserted_frame = 0;
  485. memset(cx->vbi.sliced_mpeg_size,
  486. 0, sizeof(cx->vbi.sliced_mpeg_size));
  487. break;
  488. default:
  489. return -EINVAL;
  490. }
  491. /* Clear Streamoff flags in case left from last capture */
  492. clear_bit(CX18_F_S_STREAMOFF, &s->s_flags);
  493. cx18_vapi_result(cx, data, CX18_CREATE_TASK, 1, CPU_CMD_MASK_CAPTURE);
  494. s->handle = data[0];
  495. cx18_vapi(cx, CX18_CPU_SET_CHANNEL_TYPE, 2, s->handle, captype);
  496. /*
  497. * For everything but CAPTURE_CHANNEL_TYPE_TS, play it safe and
  498. * set up all the parameters, as it is not obvious which parameters the
  499. * firmware shares across capture channel types and which it does not.
  500. *
  501. * Some of the cx18_vapi() calls below apply to only certain capture
  502. * channel types. We're hoping there's no harm in calling most of them
  503. * anyway, as long as the values are all consistent. Setting some
  504. * shared parameters will have no effect once an analog capture channel
  505. * has started streaming.
  506. */
  507. if (captype != CAPTURE_CHANNEL_TYPE_TS) {
  508. cx18_vapi(cx, CX18_CPU_SET_VER_CROP_LINE, 2, s->handle, 0);
  509. cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 3, s->handle, 3, 1);
  510. cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 3, s->handle, 8, 0);
  511. cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 3, s->handle, 4, 1);
  512. /*
  513. * Audio related reset according to
  514. * Documentation/video4linux/cx2341x/fw-encoder-api.txt
  515. */
  516. if (atomic_read(&cx->ana_capturing) == 0)
  517. cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 2,
  518. s->handle, 12);
  519. /*
  520. * Number of lines for Field 1 & Field 2 according to
  521. * Documentation/video4linux/cx2341x/fw-encoder-api.txt
  522. * Field 1 is 312 for 625 line systems in BT.656
  523. * Field 2 is 313 for 625 line systems in BT.656
  524. */
  525. cx18_vapi(cx, CX18_CPU_SET_CAPTURE_LINE_NO, 3,
  526. s->handle, 312, 313);
  527. if (cx->v4l2_cap & V4L2_CAP_VBI_CAPTURE)
  528. cx18_vbi_setup(s);
  529. /*
  530. * assign program index info.
  531. * Mask 7: select I/P/B, Num_req: 400 max
  532. * FIXME - currently we have this hardcoded as disabled
  533. */
  534. cx18_vapi_result(cx, data, CX18_CPU_SET_INDEXTABLE, 1, 0);
  535. /* Call out to the common CX2341x API setup for user controls */
  536. priv.cx = cx;
  537. priv.s = s;
  538. cx2341x_update(&priv, cx18_api_func, NULL, &cx->params);
  539. /*
  540. * When starting a capture and we're set for radio,
  541. * ensure the video is muted, despite the user control.
  542. */
  543. if (!cx->params.video_mute &&
  544. test_bit(CX18_F_I_RADIO_USER, &cx->i_flags))
  545. cx18_vapi(cx, CX18_CPU_SET_VIDEO_MUTE, 2, s->handle,
  546. (cx->params.video_mute_yuv << 8) | 1);
  547. }
  548. if (atomic_read(&cx->tot_capturing) == 0) {
  549. clear_bit(CX18_F_I_EOS, &cx->i_flags);
  550. cx18_write_reg(cx, 7, CX18_DSP0_INTERRUPT_MASK);
  551. }
  552. cx18_vapi(cx, CX18_CPU_DE_SET_MDL_ACK, 3, s->handle,
  553. (void __iomem *)&cx->scb->cpu_mdl_ack[s->type][0] - cx->enc_mem,
  554. (void __iomem *)&cx->scb->cpu_mdl_ack[s->type][1] - cx->enc_mem);
  555. /* Init all the cpu_mdls for this stream */
  556. cx18_stream_configure_mdls(s);
  557. _cx18_stream_load_fw_queue(s);
  558. /* begin_capture */
  559. if (cx18_vapi(cx, CX18_CPU_CAPTURE_START, 1, s->handle)) {
  560. CX18_DEBUG_WARN("Error starting capture!\n");
  561. /* Ensure we're really not capturing before releasing MDLs */
  562. set_bit(CX18_F_S_STOPPING, &s->s_flags);
  563. if (s->type == CX18_ENC_STREAM_TYPE_MPG)
  564. cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 2, s->handle, 1);
  565. else
  566. cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 1, s->handle);
  567. clear_bit(CX18_F_S_STREAMING, &s->s_flags);
  568. /* FIXME - CX18_F_S_STREAMOFF as well? */
  569. cx18_vapi(cx, CX18_CPU_DE_RELEASE_MDL, 1, s->handle);
  570. cx18_vapi(cx, CX18_DESTROY_TASK, 1, s->handle);
  571. s->handle = CX18_INVALID_TASK_HANDLE;
  572. clear_bit(CX18_F_S_STOPPING, &s->s_flags);
  573. if (atomic_read(&cx->tot_capturing) == 0) {
  574. set_bit(CX18_F_I_EOS, &cx->i_flags);
  575. cx18_write_reg(cx, 5, CX18_DSP0_INTERRUPT_MASK);
  576. }
  577. return -EINVAL;
  578. }
  579. /* you're live! sit back and await interrupts :) */
  580. if (captype != CAPTURE_CHANNEL_TYPE_TS)
  581. atomic_inc(&cx->ana_capturing);
  582. atomic_inc(&cx->tot_capturing);
  583. return 0;
  584. }
  585. void cx18_stop_all_captures(struct cx18 *cx)
  586. {
  587. int i;
  588. for (i = CX18_MAX_STREAMS - 1; i >= 0; i--) {
  589. struct cx18_stream *s = &cx->streams[i];
  590. if (s->video_dev == NULL && s->dvb.enabled == 0)
  591. continue;
  592. if (test_bit(CX18_F_S_STREAMING, &s->s_flags))
  593. cx18_stop_v4l2_encode_stream(s, 0);
  594. }
  595. }
  596. int cx18_stop_v4l2_encode_stream(struct cx18_stream *s, int gop_end)
  597. {
  598. struct cx18 *cx = s->cx;
  599. unsigned long then;
  600. if (s->video_dev == NULL && s->dvb.enabled == 0)
  601. return -EINVAL;
  602. /* This function assumes that you are allowed to stop the capture
  603. and that we are actually capturing */
  604. CX18_DEBUG_INFO("Stop Capture\n");
  605. if (atomic_read(&cx->tot_capturing) == 0)
  606. return 0;
  607. set_bit(CX18_F_S_STOPPING, &s->s_flags);
  608. if (s->type == CX18_ENC_STREAM_TYPE_MPG)
  609. cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 2, s->handle, !gop_end);
  610. else
  611. cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 1, s->handle);
  612. then = jiffies;
  613. if (s->type == CX18_ENC_STREAM_TYPE_MPG && gop_end) {
  614. CX18_INFO("ignoring gop_end: not (yet?) supported by the firmware\n");
  615. }
  616. if (s->type != CX18_ENC_STREAM_TYPE_TS)
  617. atomic_dec(&cx->ana_capturing);
  618. atomic_dec(&cx->tot_capturing);
  619. /* Clear capture and no-read bits */
  620. clear_bit(CX18_F_S_STREAMING, &s->s_flags);
  621. /* Tell the CX23418 it can't use our buffers anymore */
  622. cx18_vapi(cx, CX18_CPU_DE_RELEASE_MDL, 1, s->handle);
  623. cx18_vapi(cx, CX18_DESTROY_TASK, 1, s->handle);
  624. s->handle = CX18_INVALID_TASK_HANDLE;
  625. clear_bit(CX18_F_S_STOPPING, &s->s_flags);
  626. if (atomic_read(&cx->tot_capturing) > 0)
  627. return 0;
  628. cx18_write_reg(cx, 5, CX18_DSP0_INTERRUPT_MASK);
  629. wake_up(&s->waitq);
  630. return 0;
  631. }
  632. u32 cx18_find_handle(struct cx18 *cx)
  633. {
  634. int i;
  635. /* find first available handle to be used for global settings */
  636. for (i = 0; i < CX18_MAX_STREAMS; i++) {
  637. struct cx18_stream *s = &cx->streams[i];
  638. if (s->video_dev && (s->handle != CX18_INVALID_TASK_HANDLE))
  639. return s->handle;
  640. }
  641. return CX18_INVALID_TASK_HANDLE;
  642. }
  643. struct cx18_stream *cx18_handle_to_stream(struct cx18 *cx, u32 handle)
  644. {
  645. int i;
  646. struct cx18_stream *s;
  647. if (handle == CX18_INVALID_TASK_HANDLE)
  648. return NULL;
  649. for (i = 0; i < CX18_MAX_STREAMS; i++) {
  650. s = &cx->streams[i];
  651. if (s->handle != handle)
  652. continue;
  653. if (s->video_dev || s->dvb.enabled)
  654. return s;
  655. }
  656. return NULL;
  657. }