cx18-streams.c 23 KB

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