cx18-streams.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  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@md.metrocast.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. #include "cx18-driver.h"
  20. #include "cx18-io.h"
  21. #include "cx18-fileops.h"
  22. #include "cx18-mailbox.h"
  23. #include "cx18-i2c.h"
  24. #include "cx18-queue.h"
  25. #include "cx18-ioctl.h"
  26. #include "cx18-streams.h"
  27. #include "cx18-cards.h"
  28. #include "cx18-scb.h"
  29. #include "cx18-dvb.h"
  30. #define CX18_DSP0_INTERRUPT_MASK 0xd0004C
  31. static struct v4l2_file_operations cx18_v4l2_enc_fops = {
  32. .owner = THIS_MODULE,
  33. .read = cx18_v4l2_read,
  34. .open = cx18_v4l2_open,
  35. .unlocked_ioctl = video_ioctl2,
  36. .release = cx18_v4l2_close,
  37. .poll = cx18_v4l2_enc_poll,
  38. .mmap = cx18_v4l2_mmap,
  39. };
  40. /* offset from 0 to register ts v4l2 minors on */
  41. #define CX18_V4L2_ENC_TS_OFFSET 16
  42. /* offset from 0 to register pcm v4l2 minors on */
  43. #define CX18_V4L2_ENC_PCM_OFFSET 24
  44. /* offset from 0 to register yuv v4l2 minors on */
  45. #define CX18_V4L2_ENC_YUV_OFFSET 32
  46. static struct {
  47. const char *name;
  48. int vfl_type;
  49. int num_offset;
  50. int dma;
  51. u32 caps;
  52. } cx18_stream_info[] = {
  53. { /* CX18_ENC_STREAM_TYPE_MPG */
  54. "encoder MPEG",
  55. VFL_TYPE_GRABBER, 0,
  56. PCI_DMA_FROMDEVICE,
  57. V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
  58. V4L2_CAP_AUDIO | V4L2_CAP_TUNER
  59. },
  60. { /* CX18_ENC_STREAM_TYPE_TS */
  61. "TS",
  62. VFL_TYPE_GRABBER, -1,
  63. PCI_DMA_FROMDEVICE,
  64. },
  65. { /* CX18_ENC_STREAM_TYPE_YUV */
  66. "encoder YUV",
  67. VFL_TYPE_GRABBER, CX18_V4L2_ENC_YUV_OFFSET,
  68. PCI_DMA_FROMDEVICE,
  69. V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
  70. V4L2_CAP_STREAMING | V4L2_CAP_AUDIO | V4L2_CAP_TUNER
  71. },
  72. { /* CX18_ENC_STREAM_TYPE_VBI */
  73. "encoder VBI",
  74. VFL_TYPE_VBI, 0,
  75. PCI_DMA_FROMDEVICE,
  76. V4L2_CAP_VBI_CAPTURE | V4L2_CAP_SLICED_VBI_CAPTURE |
  77. V4L2_CAP_READWRITE | V4L2_CAP_TUNER
  78. },
  79. { /* CX18_ENC_STREAM_TYPE_PCM */
  80. "encoder PCM audio",
  81. VFL_TYPE_GRABBER, CX18_V4L2_ENC_PCM_OFFSET,
  82. PCI_DMA_FROMDEVICE,
  83. V4L2_CAP_TUNER | V4L2_CAP_AUDIO | V4L2_CAP_READWRITE,
  84. },
  85. { /* CX18_ENC_STREAM_TYPE_IDX */
  86. "encoder IDX",
  87. VFL_TYPE_GRABBER, -1,
  88. PCI_DMA_FROMDEVICE,
  89. },
  90. { /* CX18_ENC_STREAM_TYPE_RAD */
  91. "encoder radio",
  92. VFL_TYPE_RADIO, 0,
  93. PCI_DMA_NONE,
  94. V4L2_CAP_RADIO | V4L2_CAP_TUNER
  95. },
  96. };
  97. static void cx18_dma_free(struct videobuf_queue *q,
  98. struct cx18_stream *s, struct cx18_videobuf_buffer *buf)
  99. {
  100. videobuf_waiton(q, &buf->vb, 0, 0);
  101. videobuf_vmalloc_free(&buf->vb);
  102. buf->vb.state = VIDEOBUF_NEEDS_INIT;
  103. }
  104. static int cx18_prepare_buffer(struct videobuf_queue *q,
  105. struct cx18_stream *s,
  106. struct cx18_videobuf_buffer *buf,
  107. u32 pixelformat,
  108. unsigned int width, unsigned int height,
  109. enum v4l2_field field)
  110. {
  111. struct cx18 *cx = s->cx;
  112. int rc = 0;
  113. /* check settings */
  114. buf->bytes_used = 0;
  115. if ((width < 48) || (height < 32))
  116. return -EINVAL;
  117. buf->vb.size = (width * height * 2);
  118. if ((buf->vb.baddr != 0) && (buf->vb.bsize < buf->vb.size))
  119. return -EINVAL;
  120. /* alloc + fill struct (if changed) */
  121. if (buf->vb.width != width || buf->vb.height != height ||
  122. buf->vb.field != field || s->pixelformat != pixelformat ||
  123. buf->tvnorm != cx->std) {
  124. buf->vb.width = width;
  125. buf->vb.height = height;
  126. buf->vb.field = field;
  127. buf->tvnorm = cx->std;
  128. s->pixelformat = pixelformat;
  129. /* HM12 YUV size is (Y=(h*720) + UV=(h*(720/2)))
  130. UYUV YUV size is (Y=(h*720) + UV=(h*(720))) */
  131. if (s->pixelformat == V4L2_PIX_FMT_HM12)
  132. s->vb_bytes_per_frame = height * 720 * 3 / 2;
  133. else
  134. s->vb_bytes_per_frame = height * 720 * 2;
  135. cx18_dma_free(q, s, buf);
  136. }
  137. if ((buf->vb.baddr != 0) && (buf->vb.bsize < buf->vb.size))
  138. return -EINVAL;
  139. if (buf->vb.field == 0)
  140. buf->vb.field = V4L2_FIELD_INTERLACED;
  141. if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
  142. buf->vb.width = width;
  143. buf->vb.height = height;
  144. buf->vb.field = field;
  145. buf->tvnorm = cx->std;
  146. s->pixelformat = pixelformat;
  147. /* HM12 YUV size is (Y=(h*720) + UV=(h*(720/2)))
  148. UYUV YUV size is (Y=(h*720) + UV=(h*(720))) */
  149. if (s->pixelformat == V4L2_PIX_FMT_HM12)
  150. s->vb_bytes_per_frame = height * 720 * 3 / 2;
  151. else
  152. s->vb_bytes_per_frame = height * 720 * 2;
  153. rc = videobuf_iolock(q, &buf->vb, NULL);
  154. if (rc != 0)
  155. goto fail;
  156. }
  157. buf->vb.state = VIDEOBUF_PREPARED;
  158. return 0;
  159. fail:
  160. cx18_dma_free(q, s, buf);
  161. return rc;
  162. }
  163. /* VB_MIN_BUFSIZE is lcm(1440 * 480, 1440 * 576)
  164. 1440 is a single line of 4:2:2 YUV at 720 luma samples wide
  165. */
  166. #define VB_MIN_BUFFERS 32
  167. #define VB_MIN_BUFSIZE 4147200
  168. static int buffer_setup(struct videobuf_queue *q,
  169. unsigned int *count, unsigned int *size)
  170. {
  171. struct cx18_stream *s = q->priv_data;
  172. struct cx18 *cx = s->cx;
  173. *size = 2 * cx->cxhdl.width * cx->cxhdl.height;
  174. if (*count == 0)
  175. *count = VB_MIN_BUFFERS;
  176. while (*size * *count > VB_MIN_BUFFERS * VB_MIN_BUFSIZE)
  177. (*count)--;
  178. q->field = V4L2_FIELD_INTERLACED;
  179. q->last = V4L2_FIELD_INTERLACED;
  180. return 0;
  181. }
  182. static int buffer_prepare(struct videobuf_queue *q,
  183. struct videobuf_buffer *vb,
  184. enum v4l2_field field)
  185. {
  186. struct cx18_videobuf_buffer *buf =
  187. container_of(vb, struct cx18_videobuf_buffer, vb);
  188. struct cx18_stream *s = q->priv_data;
  189. struct cx18 *cx = s->cx;
  190. return cx18_prepare_buffer(q, s, buf, s->pixelformat,
  191. cx->cxhdl.width, cx->cxhdl.height, field);
  192. }
  193. static void buffer_release(struct videobuf_queue *q,
  194. struct videobuf_buffer *vb)
  195. {
  196. struct cx18_videobuf_buffer *buf =
  197. container_of(vb, struct cx18_videobuf_buffer, vb);
  198. struct cx18_stream *s = q->priv_data;
  199. cx18_dma_free(q, s, buf);
  200. }
  201. static void buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
  202. {
  203. struct cx18_videobuf_buffer *buf =
  204. container_of(vb, struct cx18_videobuf_buffer, vb);
  205. struct cx18_stream *s = q->priv_data;
  206. buf->vb.state = VIDEOBUF_QUEUED;
  207. list_add_tail(&buf->vb.queue, &s->vb_capture);
  208. }
  209. static struct videobuf_queue_ops cx18_videobuf_qops = {
  210. .buf_setup = buffer_setup,
  211. .buf_prepare = buffer_prepare,
  212. .buf_queue = buffer_queue,
  213. .buf_release = buffer_release,
  214. };
  215. static void cx18_stream_init(struct cx18 *cx, int type)
  216. {
  217. struct cx18_stream *s = &cx->streams[type];
  218. memset(s, 0, sizeof(*s));
  219. /* initialize cx18_stream fields */
  220. s->dvb = NULL;
  221. s->cx = cx;
  222. s->type = type;
  223. s->name = cx18_stream_info[type].name;
  224. s->handle = CX18_INVALID_TASK_HANDLE;
  225. s->dma = cx18_stream_info[type].dma;
  226. s->v4l2_dev_caps = cx18_stream_info[type].caps;
  227. s->buffers = cx->stream_buffers[type];
  228. s->buf_size = cx->stream_buf_size[type];
  229. INIT_LIST_HEAD(&s->buf_pool);
  230. s->bufs_per_mdl = 1;
  231. s->mdl_size = s->buf_size * s->bufs_per_mdl;
  232. init_waitqueue_head(&s->waitq);
  233. s->id = -1;
  234. spin_lock_init(&s->q_free.lock);
  235. cx18_queue_init(&s->q_free);
  236. spin_lock_init(&s->q_busy.lock);
  237. cx18_queue_init(&s->q_busy);
  238. spin_lock_init(&s->q_full.lock);
  239. cx18_queue_init(&s->q_full);
  240. spin_lock_init(&s->q_idle.lock);
  241. cx18_queue_init(&s->q_idle);
  242. INIT_WORK(&s->out_work_order, cx18_out_work_handler);
  243. INIT_LIST_HEAD(&s->vb_capture);
  244. setup_timer(&s->vb_timeout, cx18_vb_timeout, (unsigned long)s);
  245. spin_lock_init(&s->vb_lock);
  246. if (type == CX18_ENC_STREAM_TYPE_YUV) {
  247. spin_lock_init(&s->vbuf_q_lock);
  248. s->vb_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  249. videobuf_queue_vmalloc_init(&s->vbuf_q, &cx18_videobuf_qops,
  250. &cx->pci_dev->dev, &s->vbuf_q_lock,
  251. V4L2_BUF_TYPE_VIDEO_CAPTURE,
  252. V4L2_FIELD_INTERLACED,
  253. sizeof(struct cx18_videobuf_buffer),
  254. s, &cx->serialize_lock);
  255. /* Assume the previous pixel default */
  256. s->pixelformat = V4L2_PIX_FMT_HM12;
  257. s->vb_bytes_per_frame = cx->cxhdl.height * 720 * 3 / 2;
  258. s->vb_bytes_per_line = 720;
  259. }
  260. }
  261. static int cx18_prep_dev(struct cx18 *cx, int type)
  262. {
  263. struct cx18_stream *s = &cx->streams[type];
  264. u32 cap = cx->v4l2_cap;
  265. int num_offset = cx18_stream_info[type].num_offset;
  266. int num = cx->instance + cx18_first_minor + num_offset;
  267. /*
  268. * These five fields are always initialized.
  269. * For analog capture related streams, if video_dev.v4l2_dev == NULL then the
  270. * stream is not in use.
  271. * For the TS stream, if dvb == NULL then the stream is not in use.
  272. * In those cases no other fields but these four can be used.
  273. */
  274. s->video_dev.v4l2_dev = NULL;
  275. s->dvb = NULL;
  276. s->cx = cx;
  277. s->type = type;
  278. s->name = cx18_stream_info[type].name;
  279. /* Check whether the radio is supported */
  280. if (type == CX18_ENC_STREAM_TYPE_RAD && !(cap & V4L2_CAP_RADIO))
  281. return 0;
  282. /* Check whether VBI is supported */
  283. if (type == CX18_ENC_STREAM_TYPE_VBI &&
  284. !(cap & (V4L2_CAP_VBI_CAPTURE | V4L2_CAP_SLICED_VBI_CAPTURE)))
  285. return 0;
  286. /* User explicitly selected 0 buffers for these streams, so don't
  287. create them. */
  288. if (cx18_stream_info[type].dma != PCI_DMA_NONE &&
  289. cx->stream_buffers[type] == 0) {
  290. CX18_INFO("Disabled %s device\n", cx18_stream_info[type].name);
  291. return 0;
  292. }
  293. cx18_stream_init(cx, type);
  294. /* Allocate the cx18_dvb struct only for the TS on cards with DTV */
  295. if (type == CX18_ENC_STREAM_TYPE_TS) {
  296. if (cx->card->hw_all & CX18_HW_DVB) {
  297. s->dvb = kzalloc(sizeof(struct cx18_dvb), GFP_KERNEL);
  298. if (s->dvb == NULL) {
  299. CX18_ERR("Couldn't allocate cx18_dvb structure for %s\n",
  300. s->name);
  301. return -ENOMEM;
  302. }
  303. } else {
  304. /* Don't need buffers for the TS, if there is no DVB */
  305. s->buffers = 0;
  306. }
  307. }
  308. if (num_offset == -1)
  309. return 0;
  310. /* initialize the v4l2 video device structure */
  311. snprintf(s->video_dev.name, sizeof(s->video_dev.name), "%s %s",
  312. cx->v4l2_dev.name, s->name);
  313. s->video_dev.num = num;
  314. s->video_dev.v4l2_dev = &cx->v4l2_dev;
  315. s->video_dev.fops = &cx18_v4l2_enc_fops;
  316. s->video_dev.release = video_device_release_empty;
  317. if (cx->card->video_inputs->video_type == CX18_CARD_INPUT_VID_TUNER)
  318. s->video_dev.tvnorms = cx->tuner_std;
  319. else
  320. s->video_dev.tvnorms = V4L2_STD_ALL;
  321. s->video_dev.lock = &cx->serialize_lock;
  322. cx18_set_funcs(&s->video_dev);
  323. return 0;
  324. }
  325. /* Initialize v4l2 variables and register v4l2 devices */
  326. int cx18_streams_setup(struct cx18 *cx)
  327. {
  328. int type, ret;
  329. /* Setup V4L2 Devices */
  330. for (type = 0; type < CX18_MAX_STREAMS; type++) {
  331. /* Prepare device */
  332. ret = cx18_prep_dev(cx, type);
  333. if (ret < 0)
  334. break;
  335. /* Allocate Stream */
  336. ret = cx18_stream_alloc(&cx->streams[type]);
  337. if (ret < 0)
  338. break;
  339. }
  340. if (type == CX18_MAX_STREAMS)
  341. return 0;
  342. /* One or more streams could not be initialized. Clean 'em all up. */
  343. cx18_streams_cleanup(cx, 0);
  344. return ret;
  345. }
  346. static int cx18_reg_dev(struct cx18 *cx, int type)
  347. {
  348. struct cx18_stream *s = &cx->streams[type];
  349. int vfl_type = cx18_stream_info[type].vfl_type;
  350. const char *name;
  351. int num, ret;
  352. if (type == CX18_ENC_STREAM_TYPE_TS && s->dvb != NULL) {
  353. ret = cx18_dvb_register(s);
  354. if (ret < 0) {
  355. CX18_ERR("DVB failed to register\n");
  356. return ret;
  357. }
  358. }
  359. if (s->video_dev.v4l2_dev == NULL)
  360. return 0;
  361. num = s->video_dev.num;
  362. /* card number + user defined offset + device offset */
  363. if (type != CX18_ENC_STREAM_TYPE_MPG) {
  364. struct cx18_stream *s_mpg = &cx->streams[CX18_ENC_STREAM_TYPE_MPG];
  365. if (s_mpg->video_dev.v4l2_dev)
  366. num = s_mpg->video_dev.num
  367. + cx18_stream_info[type].num_offset;
  368. }
  369. video_set_drvdata(&s->video_dev, s);
  370. /* Register device. First try the desired minor, then any free one. */
  371. ret = video_register_device_no_warn(&s->video_dev, vfl_type, num);
  372. if (ret < 0) {
  373. CX18_ERR("Couldn't register v4l2 device for %s (device node number %d)\n",
  374. s->name, num);
  375. s->video_dev.v4l2_dev = NULL;
  376. return ret;
  377. }
  378. name = video_device_node_name(&s->video_dev);
  379. switch (vfl_type) {
  380. case VFL_TYPE_GRABBER:
  381. CX18_INFO("Registered device %s for %s (%d x %d.%02d kB)\n",
  382. name, s->name, cx->stream_buffers[type],
  383. cx->stream_buf_size[type] / 1024,
  384. (cx->stream_buf_size[type] * 100 / 1024) % 100);
  385. break;
  386. case VFL_TYPE_RADIO:
  387. CX18_INFO("Registered device %s for %s\n", name, s->name);
  388. break;
  389. case VFL_TYPE_VBI:
  390. if (cx->stream_buffers[type])
  391. CX18_INFO("Registered device %s for %s (%d x %d bytes)\n",
  392. name, s->name, cx->stream_buffers[type],
  393. cx->stream_buf_size[type]);
  394. else
  395. CX18_INFO("Registered device %s for %s\n",
  396. name, s->name);
  397. break;
  398. }
  399. return 0;
  400. }
  401. /* Register v4l2 devices */
  402. int cx18_streams_register(struct cx18 *cx)
  403. {
  404. int type;
  405. int err;
  406. int ret = 0;
  407. /* Register V4L2 devices */
  408. for (type = 0; type < CX18_MAX_STREAMS; type++) {
  409. err = cx18_reg_dev(cx, type);
  410. if (err && ret == 0)
  411. ret = err;
  412. }
  413. if (ret == 0)
  414. return 0;
  415. /* One or more streams could not be initialized. Clean 'em all up. */
  416. cx18_streams_cleanup(cx, 1);
  417. return ret;
  418. }
  419. /* Unregister v4l2 devices */
  420. void cx18_streams_cleanup(struct cx18 *cx, int unregister)
  421. {
  422. struct video_device *vdev;
  423. int type;
  424. /* Teardown all streams */
  425. for (type = 0; type < CX18_MAX_STREAMS; type++) {
  426. /* The TS has a cx18_dvb structure, not a video_device */
  427. if (type == CX18_ENC_STREAM_TYPE_TS) {
  428. if (cx->streams[type].dvb != NULL) {
  429. if (unregister)
  430. cx18_dvb_unregister(&cx->streams[type]);
  431. kfree(cx->streams[type].dvb);
  432. cx->streams[type].dvb = NULL;
  433. cx18_stream_free(&cx->streams[type]);
  434. }
  435. continue;
  436. }
  437. /* No struct video_device, but can have buffers allocated */
  438. if (type == CX18_ENC_STREAM_TYPE_IDX) {
  439. /* If the module params didn't inhibit IDX ... */
  440. if (cx->stream_buffers[type] != 0) {
  441. cx->stream_buffers[type] = 0;
  442. /*
  443. * Before calling cx18_stream_free(),
  444. * check if the IDX stream was actually set up.
  445. * Needed, since the cx18_probe() error path
  446. * exits through here as well as normal clean up
  447. */
  448. if (cx->streams[type].buffers != 0)
  449. cx18_stream_free(&cx->streams[type]);
  450. }
  451. continue;
  452. }
  453. /* If struct video_device exists, can have buffers allocated */
  454. vdev = &cx->streams[type].video_dev;
  455. if (vdev->v4l2_dev == NULL)
  456. continue;
  457. if (type == CX18_ENC_STREAM_TYPE_YUV)
  458. videobuf_mmap_free(&cx->streams[type].vbuf_q);
  459. cx18_stream_free(&cx->streams[type]);
  460. video_unregister_device(vdev);
  461. }
  462. }
  463. static void cx18_vbi_setup(struct cx18_stream *s)
  464. {
  465. struct cx18 *cx = s->cx;
  466. int raw = cx18_raw_vbi(cx);
  467. u32 data[CX2341X_MBOX_MAX_DATA];
  468. int lines;
  469. if (cx->is_60hz) {
  470. cx->vbi.count = 12;
  471. cx->vbi.start[0] = 10;
  472. cx->vbi.start[1] = 273;
  473. } else { /* PAL/SECAM */
  474. cx->vbi.count = 18;
  475. cx->vbi.start[0] = 6;
  476. cx->vbi.start[1] = 318;
  477. }
  478. /* setup VBI registers */
  479. if (raw)
  480. v4l2_subdev_call(cx->sd_av, vbi, s_raw_fmt, &cx->vbi.in.fmt.vbi);
  481. else
  482. v4l2_subdev_call(cx->sd_av, vbi, s_sliced_fmt, &cx->vbi.in.fmt.sliced);
  483. /*
  484. * Send the CX18_CPU_SET_RAW_VBI_PARAM API command to setup Encoder Raw
  485. * VBI when the first analog capture channel starts, as once it starts
  486. * (e.g. MPEG), we can't effect any change in the Encoder Raw VBI setup
  487. * (i.e. for the VBI capture channels). We also send it for each
  488. * analog capture channel anyway just to make sure we get the proper
  489. * behavior
  490. */
  491. if (raw) {
  492. lines = cx->vbi.count * 2;
  493. } else {
  494. /*
  495. * For 525/60 systems, according to the VIP 2 & BT.656 std:
  496. * The EAV RP code's Field bit toggles on line 4, a few lines
  497. * after the Vertcal Blank bit has already toggled.
  498. * Tell the encoder to capture 21-4+1=18 lines per field,
  499. * since we want lines 10 through 21.
  500. *
  501. * For 625/50 systems, according to the VIP 2 & BT.656 std:
  502. * The EAV RP code's Field bit toggles on line 1, a few lines
  503. * after the Vertcal Blank bit has already toggled.
  504. * (We've actually set the digitizer so that the Field bit
  505. * toggles on line 2.) Tell the encoder to capture 23-2+1=22
  506. * lines per field, since we want lines 6 through 23.
  507. */
  508. lines = cx->is_60hz ? (21 - 4 + 1) * 2 : (23 - 2 + 1) * 2;
  509. }
  510. data[0] = s->handle;
  511. /* Lines per field */
  512. data[1] = (lines / 2) | ((lines / 2) << 16);
  513. /* bytes per line */
  514. data[2] = (raw ? VBI_ACTIVE_SAMPLES
  515. : (cx->is_60hz ? VBI_HBLANK_SAMPLES_60HZ
  516. : VBI_HBLANK_SAMPLES_50HZ));
  517. /* Every X number of frames a VBI interrupt arrives
  518. (frames as in 25 or 30 fps) */
  519. data[3] = 1;
  520. /*
  521. * Set the SAV/EAV RP codes to look for as start/stop points
  522. * when in VIP-1.1 mode
  523. */
  524. if (raw) {
  525. /*
  526. * Start codes for beginning of "active" line in vertical blank
  527. * 0x20 ( VerticalBlank )
  528. * 0x60 ( EvenField VerticalBlank )
  529. */
  530. data[4] = 0x20602060;
  531. /*
  532. * End codes for end of "active" raw lines and regular lines
  533. * 0x30 ( VerticalBlank HorizontalBlank)
  534. * 0x70 ( EvenField VerticalBlank HorizontalBlank)
  535. * 0x90 (Task HorizontalBlank)
  536. * 0xd0 (Task EvenField HorizontalBlank)
  537. */
  538. data[5] = 0x307090d0;
  539. } else {
  540. /*
  541. * End codes for active video, we want data in the hblank region
  542. * 0xb0 (Task 0 VerticalBlank HorizontalBlank)
  543. * 0xf0 (Task EvenField VerticalBlank HorizontalBlank)
  544. *
  545. * Since the V bit is only allowed to toggle in the EAV RP code,
  546. * just before the first active region line, these two
  547. * are problematic:
  548. * 0x90 (Task HorizontalBlank)
  549. * 0xd0 (Task EvenField HorizontalBlank)
  550. *
  551. * We have set the digitzer such that we don't have to worry
  552. * about these problem codes.
  553. */
  554. data[4] = 0xB0F0B0F0;
  555. /*
  556. * Start codes for beginning of active line in vertical blank
  557. * 0xa0 (Task VerticalBlank )
  558. * 0xe0 (Task EvenField VerticalBlank )
  559. */
  560. data[5] = 0xA0E0A0E0;
  561. }
  562. CX18_DEBUG_INFO("Setup VBI h: %d lines %x bpl %d fr %d %x %x\n",
  563. data[0], data[1], data[2], data[3], data[4], data[5]);
  564. cx18_api(cx, CX18_CPU_SET_RAW_VBI_PARAM, 6, data);
  565. }
  566. void cx18_stream_rotate_idx_mdls(struct cx18 *cx)
  567. {
  568. struct cx18_stream *s = &cx->streams[CX18_ENC_STREAM_TYPE_IDX];
  569. struct cx18_mdl *mdl;
  570. if (!cx18_stream_enabled(s))
  571. return;
  572. /* Return if the firmware is not running low on MDLs */
  573. if ((atomic_read(&s->q_free.depth) + atomic_read(&s->q_busy.depth)) >=
  574. CX18_ENC_STREAM_TYPE_IDX_FW_MDL_MIN)
  575. return;
  576. /* Return if there are no MDLs to rotate back to the firmware */
  577. if (atomic_read(&s->q_full.depth) < 2)
  578. return;
  579. /*
  580. * Take the oldest IDX MDL still holding data, and discard its index
  581. * entries by scheduling the MDL to go back to the firmware
  582. */
  583. mdl = cx18_dequeue(s, &s->q_full);
  584. if (mdl != NULL)
  585. cx18_enqueue(s, mdl, &s->q_free);
  586. }
  587. static
  588. struct cx18_queue *_cx18_stream_put_mdl_fw(struct cx18_stream *s,
  589. struct cx18_mdl *mdl)
  590. {
  591. struct cx18 *cx = s->cx;
  592. struct cx18_queue *q;
  593. /* Don't give it to the firmware, if we're not running a capture */
  594. if (s->handle == CX18_INVALID_TASK_HANDLE ||
  595. test_bit(CX18_F_S_STOPPING, &s->s_flags) ||
  596. !test_bit(CX18_F_S_STREAMING, &s->s_flags))
  597. return cx18_enqueue(s, mdl, &s->q_free);
  598. q = cx18_enqueue(s, mdl, &s->q_busy);
  599. if (q != &s->q_busy)
  600. return q; /* The firmware has the max MDLs it can handle */
  601. cx18_mdl_sync_for_device(s, mdl);
  602. cx18_vapi(cx, CX18_CPU_DE_SET_MDL, 5, s->handle,
  603. (void __iomem *) &cx->scb->cpu_mdl[mdl->id] - cx->enc_mem,
  604. s->bufs_per_mdl, mdl->id, s->mdl_size);
  605. return q;
  606. }
  607. static
  608. void _cx18_stream_load_fw_queue(struct cx18_stream *s)
  609. {
  610. struct cx18_queue *q;
  611. struct cx18_mdl *mdl;
  612. if (atomic_read(&s->q_free.depth) == 0 ||
  613. atomic_read(&s->q_busy.depth) >= CX18_MAX_FW_MDLS_PER_STREAM)
  614. return;
  615. /* Move from q_free to q_busy notifying the firmware, until the limit */
  616. do {
  617. mdl = cx18_dequeue(s, &s->q_free);
  618. if (mdl == NULL)
  619. break;
  620. q = _cx18_stream_put_mdl_fw(s, mdl);
  621. } while (atomic_read(&s->q_busy.depth) < CX18_MAX_FW_MDLS_PER_STREAM
  622. && q == &s->q_busy);
  623. }
  624. void cx18_out_work_handler(struct work_struct *work)
  625. {
  626. struct cx18_stream *s =
  627. container_of(work, struct cx18_stream, out_work_order);
  628. _cx18_stream_load_fw_queue(s);
  629. }
  630. static void cx18_stream_configure_mdls(struct cx18_stream *s)
  631. {
  632. cx18_unload_queues(s);
  633. switch (s->type) {
  634. case CX18_ENC_STREAM_TYPE_YUV:
  635. /*
  636. * Height should be a multiple of 32 lines.
  637. * Set the MDL size to the exact size needed for one frame.
  638. * Use enough buffers per MDL to cover the MDL size
  639. */
  640. if (s->pixelformat == V4L2_PIX_FMT_HM12)
  641. s->mdl_size = 720 * s->cx->cxhdl.height * 3 / 2;
  642. else
  643. s->mdl_size = 720 * s->cx->cxhdl.height * 2;
  644. s->bufs_per_mdl = s->mdl_size / s->buf_size;
  645. if (s->mdl_size % s->buf_size)
  646. s->bufs_per_mdl++;
  647. break;
  648. case CX18_ENC_STREAM_TYPE_VBI:
  649. s->bufs_per_mdl = 1;
  650. if (cx18_raw_vbi(s->cx)) {
  651. s->mdl_size = (s->cx->is_60hz ? 12 : 18)
  652. * 2 * VBI_ACTIVE_SAMPLES;
  653. } else {
  654. /*
  655. * See comment in cx18_vbi_setup() below about the
  656. * extra lines we capture in sliced VBI mode due to
  657. * the lines on which EAV RP codes toggle.
  658. */
  659. s->mdl_size = s->cx->is_60hz
  660. ? (21 - 4 + 1) * 2 * VBI_HBLANK_SAMPLES_60HZ
  661. : (23 - 2 + 1) * 2 * VBI_HBLANK_SAMPLES_50HZ;
  662. }
  663. break;
  664. default:
  665. s->bufs_per_mdl = 1;
  666. s->mdl_size = s->buf_size * s->bufs_per_mdl;
  667. break;
  668. }
  669. cx18_load_queues(s);
  670. }
  671. int cx18_start_v4l2_encode_stream(struct cx18_stream *s)
  672. {
  673. u32 data[MAX_MB_ARGUMENTS];
  674. struct cx18 *cx = s->cx;
  675. int captype = 0;
  676. struct cx18_stream *s_idx;
  677. if (!cx18_stream_enabled(s))
  678. return -EINVAL;
  679. CX18_DEBUG_INFO("Start encoder stream %s\n", s->name);
  680. switch (s->type) {
  681. case CX18_ENC_STREAM_TYPE_MPG:
  682. captype = CAPTURE_CHANNEL_TYPE_MPEG;
  683. cx->mpg_data_received = cx->vbi_data_inserted = 0;
  684. cx->dualwatch_jiffies = jiffies;
  685. cx->dualwatch_stereo_mode = v4l2_ctrl_g_ctrl(cx->cxhdl.audio_mode);
  686. cx->search_pack_header = 0;
  687. break;
  688. case CX18_ENC_STREAM_TYPE_IDX:
  689. captype = CAPTURE_CHANNEL_TYPE_INDEX;
  690. break;
  691. case CX18_ENC_STREAM_TYPE_TS:
  692. captype = CAPTURE_CHANNEL_TYPE_TS;
  693. break;
  694. case CX18_ENC_STREAM_TYPE_YUV:
  695. captype = CAPTURE_CHANNEL_TYPE_YUV;
  696. break;
  697. case CX18_ENC_STREAM_TYPE_PCM:
  698. captype = CAPTURE_CHANNEL_TYPE_PCM;
  699. break;
  700. case CX18_ENC_STREAM_TYPE_VBI:
  701. #ifdef CX18_ENCODER_PARSES_SLICED
  702. captype = cx18_raw_vbi(cx) ?
  703. CAPTURE_CHANNEL_TYPE_VBI : CAPTURE_CHANNEL_TYPE_SLICED_VBI;
  704. #else
  705. /*
  706. * Currently we set things up so that Sliced VBI from the
  707. * digitizer is handled as Raw VBI by the encoder
  708. */
  709. captype = CAPTURE_CHANNEL_TYPE_VBI;
  710. #endif
  711. cx->vbi.frame = 0;
  712. cx->vbi.inserted_frame = 0;
  713. memset(cx->vbi.sliced_mpeg_size,
  714. 0, sizeof(cx->vbi.sliced_mpeg_size));
  715. break;
  716. default:
  717. return -EINVAL;
  718. }
  719. /* Clear Streamoff flags in case left from last capture */
  720. clear_bit(CX18_F_S_STREAMOFF, &s->s_flags);
  721. cx18_vapi_result(cx, data, CX18_CREATE_TASK, 1, CPU_CMD_MASK_CAPTURE);
  722. s->handle = data[0];
  723. cx18_vapi(cx, CX18_CPU_SET_CHANNEL_TYPE, 2, s->handle, captype);
  724. /*
  725. * For everything but CAPTURE_CHANNEL_TYPE_TS, play it safe and
  726. * set up all the parameters, as it is not obvious which parameters the
  727. * firmware shares across capture channel types and which it does not.
  728. *
  729. * Some of the cx18_vapi() calls below apply to only certain capture
  730. * channel types. We're hoping there's no harm in calling most of them
  731. * anyway, as long as the values are all consistent. Setting some
  732. * shared parameters will have no effect once an analog capture channel
  733. * has started streaming.
  734. */
  735. if (captype != CAPTURE_CHANNEL_TYPE_TS) {
  736. cx18_vapi(cx, CX18_CPU_SET_VER_CROP_LINE, 2, s->handle, 0);
  737. cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 3, s->handle, 3, 1);
  738. cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 3, s->handle, 8, 0);
  739. cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 3, s->handle, 4, 1);
  740. /*
  741. * Audio related reset according to
  742. * Documentation/video4linux/cx2341x/fw-encoder-api.txt
  743. */
  744. if (atomic_read(&cx->ana_capturing) == 0)
  745. cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 2,
  746. s->handle, 12);
  747. /*
  748. * Number of lines for Field 1 & Field 2 according to
  749. * Documentation/video4linux/cx2341x/fw-encoder-api.txt
  750. * Field 1 is 312 for 625 line systems in BT.656
  751. * Field 2 is 313 for 625 line systems in BT.656
  752. */
  753. cx18_vapi(cx, CX18_CPU_SET_CAPTURE_LINE_NO, 3,
  754. s->handle, 312, 313);
  755. if (cx->v4l2_cap & V4L2_CAP_VBI_CAPTURE)
  756. cx18_vbi_setup(s);
  757. /*
  758. * Select to receive I, P, and B frame index entries, if the
  759. * index stream is enabled. Otherwise disable index entry
  760. * generation.
  761. */
  762. s_idx = &cx->streams[CX18_ENC_STREAM_TYPE_IDX];
  763. cx18_vapi_result(cx, data, CX18_CPU_SET_INDEXTABLE, 2,
  764. s->handle, cx18_stream_enabled(s_idx) ? 7 : 0);
  765. /* Call out to the common CX2341x API setup for user controls */
  766. cx->cxhdl.priv = s;
  767. cx2341x_handler_setup(&cx->cxhdl);
  768. /*
  769. * When starting a capture and we're set for radio,
  770. * ensure the video is muted, despite the user control.
  771. */
  772. if (!cx->cxhdl.video_mute &&
  773. test_bit(CX18_F_I_RADIO_USER, &cx->i_flags))
  774. cx18_vapi(cx, CX18_CPU_SET_VIDEO_MUTE, 2, s->handle,
  775. (v4l2_ctrl_g_ctrl(cx->cxhdl.video_mute_yuv) << 8) | 1);
  776. /* Enable the Video Format Converter for UYVY 4:2:2 support,
  777. * rather than the default HM12 Macroblovk 4:2:0 support.
  778. */
  779. if (captype == CAPTURE_CHANNEL_TYPE_YUV) {
  780. if (s->pixelformat == V4L2_PIX_FMT_UYVY)
  781. cx18_vapi(cx, CX18_CPU_SET_VFC_PARAM, 2,
  782. s->handle, 1);
  783. else
  784. /* If in doubt, default to HM12 */
  785. cx18_vapi(cx, CX18_CPU_SET_VFC_PARAM, 2,
  786. s->handle, 0);
  787. }
  788. }
  789. if (atomic_read(&cx->tot_capturing) == 0) {
  790. cx2341x_handler_set_busy(&cx->cxhdl, 1);
  791. clear_bit(CX18_F_I_EOS, &cx->i_flags);
  792. cx18_write_reg(cx, 7, CX18_DSP0_INTERRUPT_MASK);
  793. }
  794. cx18_vapi(cx, CX18_CPU_DE_SET_MDL_ACK, 3, s->handle,
  795. (void __iomem *)&cx->scb->cpu_mdl_ack[s->type][0] - cx->enc_mem,
  796. (void __iomem *)&cx->scb->cpu_mdl_ack[s->type][1] - cx->enc_mem);
  797. /* Init all the cpu_mdls for this stream */
  798. cx18_stream_configure_mdls(s);
  799. _cx18_stream_load_fw_queue(s);
  800. /* begin_capture */
  801. if (cx18_vapi(cx, CX18_CPU_CAPTURE_START, 1, s->handle)) {
  802. CX18_DEBUG_WARN("Error starting capture!\n");
  803. /* Ensure we're really not capturing before releasing MDLs */
  804. set_bit(CX18_F_S_STOPPING, &s->s_flags);
  805. if (s->type == CX18_ENC_STREAM_TYPE_MPG)
  806. cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 2, s->handle, 1);
  807. else
  808. cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 1, s->handle);
  809. clear_bit(CX18_F_S_STREAMING, &s->s_flags);
  810. /* FIXME - CX18_F_S_STREAMOFF as well? */
  811. cx18_vapi(cx, CX18_CPU_DE_RELEASE_MDL, 1, s->handle);
  812. cx18_vapi(cx, CX18_DESTROY_TASK, 1, s->handle);
  813. s->handle = CX18_INVALID_TASK_HANDLE;
  814. clear_bit(CX18_F_S_STOPPING, &s->s_flags);
  815. if (atomic_read(&cx->tot_capturing) == 0) {
  816. set_bit(CX18_F_I_EOS, &cx->i_flags);
  817. cx18_write_reg(cx, 5, CX18_DSP0_INTERRUPT_MASK);
  818. }
  819. return -EINVAL;
  820. }
  821. /* you're live! sit back and await interrupts :) */
  822. if (captype != CAPTURE_CHANNEL_TYPE_TS)
  823. atomic_inc(&cx->ana_capturing);
  824. atomic_inc(&cx->tot_capturing);
  825. return 0;
  826. }
  827. EXPORT_SYMBOL(cx18_start_v4l2_encode_stream);
  828. void cx18_stop_all_captures(struct cx18 *cx)
  829. {
  830. int i;
  831. for (i = CX18_MAX_STREAMS - 1; i >= 0; i--) {
  832. struct cx18_stream *s = &cx->streams[i];
  833. if (!cx18_stream_enabled(s))
  834. continue;
  835. if (test_bit(CX18_F_S_STREAMING, &s->s_flags))
  836. cx18_stop_v4l2_encode_stream(s, 0);
  837. }
  838. }
  839. int cx18_stop_v4l2_encode_stream(struct cx18_stream *s, int gop_end)
  840. {
  841. struct cx18 *cx = s->cx;
  842. if (!cx18_stream_enabled(s))
  843. return -EINVAL;
  844. /* This function assumes that you are allowed to stop the capture
  845. and that we are actually capturing */
  846. CX18_DEBUG_INFO("Stop Capture\n");
  847. if (atomic_read(&cx->tot_capturing) == 0)
  848. return 0;
  849. set_bit(CX18_F_S_STOPPING, &s->s_flags);
  850. if (s->type == CX18_ENC_STREAM_TYPE_MPG)
  851. cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 2, s->handle, !gop_end);
  852. else
  853. cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 1, s->handle);
  854. if (s->type == CX18_ENC_STREAM_TYPE_MPG && gop_end) {
  855. CX18_INFO("ignoring gop_end: not (yet?) supported by the firmware\n");
  856. }
  857. if (s->type != CX18_ENC_STREAM_TYPE_TS)
  858. atomic_dec(&cx->ana_capturing);
  859. atomic_dec(&cx->tot_capturing);
  860. /* Clear capture and no-read bits */
  861. clear_bit(CX18_F_S_STREAMING, &s->s_flags);
  862. /* Tell the CX23418 it can't use our buffers anymore */
  863. cx18_vapi(cx, CX18_CPU_DE_RELEASE_MDL, 1, s->handle);
  864. cx18_vapi(cx, CX18_DESTROY_TASK, 1, s->handle);
  865. s->handle = CX18_INVALID_TASK_HANDLE;
  866. clear_bit(CX18_F_S_STOPPING, &s->s_flags);
  867. if (atomic_read(&cx->tot_capturing) > 0)
  868. return 0;
  869. cx2341x_handler_set_busy(&cx->cxhdl, 0);
  870. cx18_write_reg(cx, 5, CX18_DSP0_INTERRUPT_MASK);
  871. wake_up(&s->waitq);
  872. return 0;
  873. }
  874. EXPORT_SYMBOL(cx18_stop_v4l2_encode_stream);
  875. u32 cx18_find_handle(struct cx18 *cx)
  876. {
  877. int i;
  878. /* find first available handle to be used for global settings */
  879. for (i = 0; i < CX18_MAX_STREAMS; i++) {
  880. struct cx18_stream *s = &cx->streams[i];
  881. if (s->video_dev.v4l2_dev && (s->handle != CX18_INVALID_TASK_HANDLE))
  882. return s->handle;
  883. }
  884. return CX18_INVALID_TASK_HANDLE;
  885. }
  886. struct cx18_stream *cx18_handle_to_stream(struct cx18 *cx, u32 handle)
  887. {
  888. int i;
  889. struct cx18_stream *s;
  890. if (handle == CX18_INVALID_TASK_HANDLE)
  891. return NULL;
  892. for (i = 0; i < CX18_MAX_STREAMS; i++) {
  893. s = &cx->streams[i];
  894. if (s->handle != handle)
  895. continue;
  896. if (cx18_stream_enabled(s))
  897. return s;
  898. }
  899. return NULL;
  900. }