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