/drivers/media/video/cx18/cx18-streams.c

https://bitbucket.org/slukk/jb-tsm-kernel-4.2 · C · 1050 lines · 698 code · 150 blank · 202 comment · 160 complexity · a96eac7a47c1bb68b0cc7d939affc165 MD5 · raw file

  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. spin_lock_init(&s->vbuf_q_lock);
  238. s->vb_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  239. videobuf_queue_vmalloc_init(&s->vbuf_q, &cx18_videobuf_qops,
  240. &cx->pci_dev->dev, &s->vbuf_q_lock,
  241. V4L2_BUF_TYPE_VIDEO_CAPTURE,
  242. V4L2_FIELD_INTERLACED,
  243. sizeof(struct cx18_videobuf_buffer),
  244. s, &cx->serialize_lock);
  245. /* Assume the previous pixel default */
  246. s->pixelformat = V4L2_PIX_FMT_HM12;
  247. }
  248. }
  249. static int cx18_prep_dev(struct cx18 *cx, int type)
  250. {
  251. struct cx18_stream *s = &cx->streams[type];
  252. u32 cap = cx->v4l2_cap;
  253. int num_offset = cx18_stream_info[type].num_offset;
  254. int num = cx->instance + cx18_first_minor + num_offset;
  255. /*
  256. * These five fields are always initialized.
  257. * For analog capture related streams, if video_dev == NULL then the
  258. * stream is not in use.
  259. * For the TS stream, if dvb == NULL then the stream is not in use.
  260. * In those cases no other fields but these four can be used.
  261. */
  262. s->video_dev = NULL;
  263. s->dvb = NULL;
  264. s->cx = cx;
  265. s->type = type;
  266. s->name = cx18_stream_info[type].name;
  267. /* Check whether the radio is supported */
  268. if (type == CX18_ENC_STREAM_TYPE_RAD && !(cap & V4L2_CAP_RADIO))
  269. return 0;
  270. /* Check whether VBI is supported */
  271. if (type == CX18_ENC_STREAM_TYPE_VBI &&
  272. !(cap & (V4L2_CAP_VBI_CAPTURE | V4L2_CAP_SLICED_VBI_CAPTURE)))
  273. return 0;
  274. /* User explicitly selected 0 buffers for these streams, so don't
  275. create them. */
  276. if (cx18_stream_info[type].dma != PCI_DMA_NONE &&
  277. cx->stream_buffers[type] == 0) {
  278. CX18_INFO("Disabled %s device\n", cx18_stream_info[type].name);
  279. return 0;
  280. }
  281. cx18_stream_init(cx, type);
  282. /* Allocate the cx18_dvb struct only for the TS on cards with DTV */
  283. if (type == CX18_ENC_STREAM_TYPE_TS) {
  284. if (cx->card->hw_all & CX18_HW_DVB) {
  285. s->dvb = kzalloc(sizeof(struct cx18_dvb), GFP_KERNEL);
  286. if (s->dvb == NULL) {
  287. CX18_ERR("Couldn't allocate cx18_dvb structure"
  288. " for %s\n", s->name);
  289. return -ENOMEM;
  290. }
  291. } else {
  292. /* Don't need buffers for the TS, if there is no DVB */
  293. s->buffers = 0;
  294. }
  295. }
  296. if (num_offset == -1)
  297. return 0;
  298. /* allocate and initialize the v4l2 video device structure */
  299. s->video_dev = video_device_alloc();
  300. if (s->video_dev == NULL) {
  301. CX18_ERR("Couldn't allocate v4l2 video_device for %s\n",
  302. s->name);
  303. return -ENOMEM;
  304. }
  305. snprintf(s->video_dev->name, sizeof(s->video_dev->name), "%s %s",
  306. cx->v4l2_dev.name, s->name);
  307. s->video_dev->num = num;
  308. s->video_dev->v4l2_dev = &cx->v4l2_dev;
  309. s->video_dev->fops = &cx18_v4l2_enc_fops;
  310. s->video_dev->release = video_device_release;
  311. s->video_dev->tvnorms = V4L2_STD_ALL;
  312. set_bit(V4L2_FL_USE_FH_PRIO, &s->video_dev->flags);
  313. cx18_set_funcs(s->video_dev);
  314. return 0;
  315. }
  316. /* Initialize v4l2 variables and register v4l2 devices */
  317. int cx18_streams_setup(struct cx18 *cx)
  318. {
  319. int type, ret;
  320. /* Setup V4L2 Devices */
  321. for (type = 0; type < CX18_MAX_STREAMS; type++) {
  322. /* Prepare device */
  323. ret = cx18_prep_dev(cx, type);
  324. if (ret < 0)
  325. break;
  326. /* Allocate Stream */
  327. ret = cx18_stream_alloc(&cx->streams[type]);
  328. if (ret < 0)
  329. break;
  330. }
  331. if (type == CX18_MAX_STREAMS)
  332. return 0;
  333. /* One or more streams could not be initialized. Clean 'em all up. */
  334. cx18_streams_cleanup(cx, 0);
  335. return ret;
  336. }
  337. static int cx18_reg_dev(struct cx18 *cx, int type)
  338. {
  339. struct cx18_stream *s = &cx->streams[type];
  340. int vfl_type = cx18_stream_info[type].vfl_type;
  341. const char *name;
  342. int num, ret;
  343. if (type == CX18_ENC_STREAM_TYPE_TS && s->dvb != NULL) {
  344. ret = cx18_dvb_register(s);
  345. if (ret < 0) {
  346. CX18_ERR("DVB failed to register\n");
  347. return ret;
  348. }
  349. }
  350. if (s->video_dev == NULL)
  351. return 0;
  352. num = s->video_dev->num;
  353. /* card number + user defined offset + device offset */
  354. if (type != CX18_ENC_STREAM_TYPE_MPG) {
  355. struct cx18_stream *s_mpg = &cx->streams[CX18_ENC_STREAM_TYPE_MPG];
  356. if (s_mpg->video_dev)
  357. num = s_mpg->video_dev->num
  358. + cx18_stream_info[type].num_offset;
  359. }
  360. video_set_drvdata(s->video_dev, s);
  361. /* Register device. First try the desired minor, then any free one. */
  362. ret = video_register_device_no_warn(s->video_dev, vfl_type, num);
  363. if (ret < 0) {
  364. CX18_ERR("Couldn't register v4l2 device for %s (device node number %d)\n",
  365. s->name, num);
  366. video_device_release(s->video_dev);
  367. s->video_dev = NULL;
  368. return ret;
  369. }
  370. name = video_device_node_name(s->video_dev);
  371. switch (vfl_type) {
  372. case VFL_TYPE_GRABBER:
  373. CX18_INFO("Registered device %s for %s (%d x %d.%02d kB)\n",
  374. name, s->name, cx->stream_buffers[type],
  375. cx->stream_buf_size[type] / 1024,
  376. (cx->stream_buf_size[type] * 100 / 1024) % 100);
  377. break;
  378. case VFL_TYPE_RADIO:
  379. CX18_INFO("Registered device %s for %s\n", name, s->name);
  380. break;
  381. case VFL_TYPE_VBI:
  382. if (cx->stream_buffers[type])
  383. CX18_INFO("Registered device %s for %s "
  384. "(%d x %d bytes)\n",
  385. name, s->name, cx->stream_buffers[type],
  386. cx->stream_buf_size[type]);
  387. else
  388. CX18_INFO("Registered device %s for %s\n",
  389. name, s->name);
  390. break;
  391. }
  392. return 0;
  393. }
  394. /* Register v4l2 devices */
  395. int cx18_streams_register(struct cx18 *cx)
  396. {
  397. int type;
  398. int err;
  399. int ret = 0;
  400. /* Register V4L2 devices */
  401. for (type = 0; type < CX18_MAX_STREAMS; type++) {
  402. err = cx18_reg_dev(cx, type);
  403. if (err && ret == 0)
  404. ret = err;
  405. }
  406. if (ret == 0)
  407. return 0;
  408. /* One or more streams could not be initialized. Clean 'em all up. */
  409. cx18_streams_cleanup(cx, 1);
  410. return ret;
  411. }
  412. /* Unregister v4l2 devices */
  413. void cx18_streams_cleanup(struct cx18 *cx, int unregister)
  414. {
  415. struct video_device *vdev;
  416. int type;
  417. /* Teardown all streams */
  418. for (type = 0; type < CX18_MAX_STREAMS; type++) {
  419. /* The TS has a cx18_dvb structure, not a video_device */
  420. if (type == CX18_ENC_STREAM_TYPE_TS) {
  421. if (cx->streams[type].dvb != NULL) {
  422. if (unregister)
  423. cx18_dvb_unregister(&cx->streams[type]);
  424. kfree(cx->streams[type].dvb);
  425. cx->streams[type].dvb = NULL;
  426. cx18_stream_free(&cx->streams[type]);
  427. }
  428. continue;
  429. }
  430. /* No struct video_device, but can have buffers allocated */
  431. if (type == CX18_ENC_STREAM_TYPE_IDX) {
  432. /* If the module params didn't inhibit IDX ... */
  433. if (cx->stream_buffers[type] != 0) {
  434. cx->stream_buffers[type] = 0;
  435. /*
  436. * Before calling cx18_stream_free(),
  437. * check if the IDX stream was actually set up.
  438. * Needed, since the cx18_probe() error path
  439. * exits through here as well as normal clean up
  440. */
  441. if (cx->streams[type].buffers != 0)
  442. cx18_stream_free(&cx->streams[type]);
  443. }
  444. continue;
  445. }
  446. /* If struct video_device exists, can have buffers allocated */
  447. vdev = cx->streams[type].video_dev;
  448. cx->streams[type].video_dev = NULL;
  449. if (vdev == NULL)
  450. continue;
  451. if (type == CX18_ENC_STREAM_TYPE_YUV)
  452. videobuf_mmap_free(&cx->streams[type].vbuf_q);
  453. cx18_stream_free(&cx->streams[type]);
  454. /* Unregister or release device */
  455. if (unregister)
  456. video_unregister_device(vdev);
  457. else
  458. video_device_release(vdev);
  459. }
  460. }
  461. static void cx18_vbi_setup(struct cx18_stream *s)
  462. {
  463. struct cx18 *cx = s->cx;
  464. int raw = cx18_raw_vbi(cx);
  465. u32 data[CX2341X_MBOX_MAX_DATA];
  466. int lines;
  467. if (cx->is_60hz) {
  468. cx->vbi.count = 12;
  469. cx->vbi.start[0] = 10;
  470. cx->vbi.start[1] = 273;
  471. } else { /* PAL/SECAM */
  472. cx->vbi.count = 18;
  473. cx->vbi.start[0] = 6;
  474. cx->vbi.start[1] = 318;
  475. }
  476. /* setup VBI registers */
  477. if (raw)
  478. v4l2_subdev_call(cx->sd_av, vbi, s_raw_fmt, &cx->vbi.in.fmt.vbi);
  479. else
  480. v4l2_subdev_call(cx->sd_av, vbi, s_sliced_fmt, &cx->vbi.in.fmt.sliced);
  481. /*
  482. * Send the CX18_CPU_SET_RAW_VBI_PARAM API command to setup Encoder Raw
  483. * VBI when the first analog capture channel starts, as once it starts
  484. * (e.g. MPEG), we can't effect any change in the Encoder Raw VBI setup
  485. * (i.e. for the VBI capture channels). We also send it for each
  486. * analog capture channel anyway just to make sure we get the proper
  487. * behavior
  488. */
  489. if (raw) {
  490. lines = cx->vbi.count * 2;
  491. } else {
  492. /*
  493. * For 525/60 systems, according to the VIP 2 & BT.656 std:
  494. * The EAV RP code's Field bit toggles on line 4, a few lines
  495. * after the Vertcal Blank bit has already toggled.
  496. * Tell the encoder to capture 21-4+1=18 lines per field,
  497. * since we want lines 10 through 21.
  498. *
  499. * For 625/50 systems, according to the VIP 2 & BT.656 std:
  500. * The EAV RP code's Field bit toggles on line 1, a few lines
  501. * after the Vertcal Blank bit has already toggled.
  502. * (We've actually set the digitizer so that the Field bit
  503. * toggles on line 2.) Tell the encoder to capture 23-2+1=22
  504. * lines per field, since we want lines 6 through 23.
  505. */
  506. lines = cx->is_60hz ? (21 - 4 + 1) * 2 : (23 - 2 + 1) * 2;
  507. }
  508. data[0] = s->handle;
  509. /* Lines per field */
  510. data[1] = (lines / 2) | ((lines / 2) << 16);
  511. /* bytes per line */
  512. data[2] = (raw ? vbi_active_samples
  513. : (cx->is_60hz ? vbi_hblank_samples_60Hz
  514. : vbi_hblank_samples_50Hz));
  515. /* Every X number of frames a VBI interrupt arrives
  516. (frames as in 25 or 30 fps) */
  517. data[3] = 1;
  518. /*
  519. * Set the SAV/EAV RP codes to look for as start/stop points
  520. * when in VIP-1.1 mode
  521. */
  522. if (raw) {
  523. /*
  524. * Start codes for beginning of "active" line in vertical blank
  525. * 0x20 ( VerticalBlank )
  526. * 0x60 ( EvenField VerticalBlank )
  527. */
  528. data[4] = 0x20602060;
  529. /*
  530. * End codes for end of "active" raw lines and regular lines
  531. * 0x30 ( VerticalBlank HorizontalBlank)
  532. * 0x70 ( EvenField VerticalBlank HorizontalBlank)
  533. * 0x90 (Task HorizontalBlank)
  534. * 0xd0 (Task EvenField HorizontalBlank)
  535. */
  536. data[5] = 0x307090d0;
  537. } else {
  538. /*
  539. * End codes for active video, we want data in the hblank region
  540. * 0xb0 (Task 0 VerticalBlank HorizontalBlank)
  541. * 0xf0 (Task EvenField VerticalBlank HorizontalBlank)
  542. *
  543. * Since the V bit is only allowed to toggle in the EAV RP code,
  544. * just before the first active region line, these two
  545. * are problematic:
  546. * 0x90 (Task HorizontalBlank)
  547. * 0xd0 (Task EvenField HorizontalBlank)
  548. *
  549. * We have set the digitzer such that we don't have to worry
  550. * about these problem codes.
  551. */
  552. data[4] = 0xB0F0B0F0;
  553. /*
  554. * Start codes for beginning of active line in vertical blank
  555. * 0xa0 (Task VerticalBlank )
  556. * 0xe0 (Task EvenField VerticalBlank )
  557. */
  558. data[5] = 0xA0E0A0E0;
  559. }
  560. CX18_DEBUG_INFO("Setup VBI h: %d lines %x bpl %d fr %d %x %x\n",
  561. data[0], data[1], data[2], data[3], data[4], data[5]);
  562. cx18_api(cx, CX18_CPU_SET_RAW_VBI_PARAM, 6, data);
  563. }
  564. void cx18_stream_rotate_idx_mdls(struct cx18 *cx)
  565. {
  566. struct cx18_stream *s = &cx->streams[CX18_ENC_STREAM_TYPE_IDX];
  567. struct cx18_mdl *mdl;
  568. if (!cx18_stream_enabled(s))
  569. return;
  570. /* Return if the firmware is not running low on MDLs */
  571. if ((atomic_read(&s->q_free.depth) + atomic_read(&s->q_busy.depth)) >=
  572. CX18_ENC_STREAM_TYPE_IDX_FW_MDL_MIN)
  573. return;
  574. /* Return if there are no MDLs to rotate back to the firmware */
  575. if (atomic_read(&s->q_full.depth) < 2)
  576. return;
  577. /*
  578. * Take the oldest IDX MDL still holding data, and discard its index
  579. * entries by scheduling the MDL to go back to the firmware
  580. */
  581. mdl = cx18_dequeue(s, &s->q_full);
  582. if (mdl != NULL)
  583. cx18_enqueue(s, mdl, &s->q_free);
  584. }
  585. static
  586. struct cx18_queue *_cx18_stream_put_mdl_fw(struct cx18_stream *s,
  587. struct cx18_mdl *mdl)
  588. {
  589. struct cx18 *cx = s->cx;
  590. struct cx18_queue *q;
  591. /* Don't give it to the firmware, if we're not running a capture */
  592. if (s->handle == CX18_INVALID_TASK_HANDLE ||
  593. test_bit(CX18_F_S_STOPPING, &s->s_flags) ||
  594. !test_bit(CX18_F_S_STREAMING, &s->s_flags))
  595. return cx18_enqueue(s, mdl, &s->q_free);
  596. q = cx18_enqueue(s, mdl, &s->q_busy);
  597. if (q != &s->q_busy)
  598. return q; /* The firmware has the max MDLs it can handle */
  599. cx18_mdl_sync_for_device(s, mdl);
  600. cx18_vapi(cx, CX18_CPU_DE_SET_MDL, 5, s->handle,
  601. (void __iomem *) &cx->scb->cpu_mdl[mdl->id] - cx->enc_mem,
  602. s->bufs_per_mdl, mdl->id, s->mdl_size);
  603. return q;
  604. }
  605. static
  606. void _cx18_stream_load_fw_queue(struct cx18_stream *s)
  607. {
  608. struct cx18_queue *q;
  609. struct cx18_mdl *mdl;
  610. if (atomic_read(&s->q_free.depth) == 0 ||
  611. atomic_read(&s->q_busy.depth) >= CX18_MAX_FW_MDLS_PER_STREAM)
  612. return;
  613. /* Move from q_free to q_busy notifying the firmware, until the limit */
  614. do {
  615. mdl = cx18_dequeue(s, &s->q_free);
  616. if (mdl == NULL)
  617. break;
  618. q = _cx18_stream_put_mdl_fw(s, mdl);
  619. } while (atomic_read(&s->q_busy.depth) < CX18_MAX_FW_MDLS_PER_STREAM
  620. && q == &s->q_busy);
  621. }
  622. void cx18_out_work_handler(struct work_struct *work)
  623. {
  624. struct cx18_stream *s =
  625. container_of(work, struct cx18_stream, out_work_order);
  626. _cx18_stream_load_fw_queue(s);
  627. }
  628. static void cx18_stream_configure_mdls(struct cx18_stream *s)
  629. {
  630. cx18_unload_queues(s);
  631. switch (s->type) {
  632. case CX18_ENC_STREAM_TYPE_YUV:
  633. /*
  634. * Height should be a multiple of 32 lines.
  635. * Set the MDL size to the exact size needed for one frame.
  636. * Use enough buffers per MDL to cover the MDL size
  637. */
  638. if (s->pixelformat == V4L2_PIX_FMT_HM12)
  639. s->mdl_size = 720 * s->cx->cxhdl.height * 3 / 2;
  640. else
  641. s->mdl_size = 720 * s->cx->cxhdl.height * 2;
  642. s->bufs_per_mdl = s->mdl_size / s->buf_size;
  643. if (s->mdl_size % s->buf_size)
  644. s->bufs_per_mdl++;
  645. break;
  646. case CX18_ENC_STREAM_TYPE_VBI:
  647. s->bufs_per_mdl = 1;
  648. if (cx18_raw_vbi(s->cx)) {
  649. s->mdl_size = (s->cx->is_60hz ? 12 : 18)
  650. * 2 * vbi_active_samples;
  651. } else {
  652. /*
  653. * See comment in cx18_vbi_setup() below about the
  654. * extra lines we capture in sliced VBI mode due to
  655. * the lines on which EAV RP codes toggle.
  656. */
  657. s->mdl_size = s->cx->is_60hz
  658. ? (21 - 4 + 1) * 2 * vbi_hblank_samples_60Hz
  659. : (23 - 2 + 1) * 2 * vbi_hblank_samples_50Hz;
  660. }
  661. break;
  662. default:
  663. s->bufs_per_mdl = 1;
  664. s->mdl_size = s->buf_size * s->bufs_per_mdl;
  665. break;
  666. }
  667. cx18_load_queues(s);
  668. }
  669. int cx18_start_v4l2_encode_stream(struct cx18_stream *s)
  670. {
  671. u32 data[MAX_MB_ARGUMENTS];
  672. struct cx18 *cx = s->cx;
  673. int captype = 0;
  674. struct cx18_stream *s_idx;
  675. if (!cx18_stream_enabled(s))
  676. return -EINVAL;
  677. CX18_DEBUG_INFO("Start encoder stream %s\n", s->name);
  678. switch (s->type) {
  679. case CX18_ENC_STREAM_TYPE_MPG:
  680. captype = CAPTURE_CHANNEL_TYPE_MPEG;
  681. cx->mpg_data_received = cx->vbi_data_inserted = 0;
  682. cx->dualwatch_jiffies = jiffies;
  683. cx->dualwatch_stereo_mode = v4l2_ctrl_g_ctrl(cx->cxhdl.audio_mode);
  684. cx->search_pack_header = 0;
  685. break;
  686. case CX18_ENC_STREAM_TYPE_IDX:
  687. captype = CAPTURE_CHANNEL_TYPE_INDEX;
  688. break;
  689. case CX18_ENC_STREAM_TYPE_TS:
  690. captype = CAPTURE_CHANNEL_TYPE_TS;
  691. break;
  692. case CX18_ENC_STREAM_TYPE_YUV:
  693. captype = CAPTURE_CHANNEL_TYPE_YUV;
  694. break;
  695. case CX18_ENC_STREAM_TYPE_PCM:
  696. captype = CAPTURE_CHANNEL_TYPE_PCM;
  697. break;
  698. case CX18_ENC_STREAM_TYPE_VBI:
  699. #ifdef CX18_ENCODER_PARSES_SLICED
  700. captype = cx18_raw_vbi(cx) ?
  701. CAPTURE_CHANNEL_TYPE_VBI : CAPTURE_CHANNEL_TYPE_SLICED_VBI;
  702. #else
  703. /*
  704. * Currently we set things up so that Sliced VBI from the
  705. * digitizer is handled as Raw VBI by the encoder
  706. */
  707. captype = CAPTURE_CHANNEL_TYPE_VBI;
  708. #endif
  709. cx->vbi.frame = 0;
  710. cx->vbi.inserted_frame = 0;
  711. memset(cx->vbi.sliced_mpeg_size,
  712. 0, sizeof(cx->vbi.sliced_mpeg_size));
  713. break;
  714. default:
  715. return -EINVAL;
  716. }
  717. /* Clear Streamoff flags in case left from last capture */
  718. clear_bit(CX18_F_S_STREAMOFF, &s->s_flags);
  719. cx18_vapi_result(cx, data, CX18_CREATE_TASK, 1, CPU_CMD_MASK_CAPTURE);
  720. s->handle = data[0];
  721. cx18_vapi(cx, CX18_CPU_SET_CHANNEL_TYPE, 2, s->handle, captype);
  722. /*
  723. * For everything but CAPTURE_CHANNEL_TYPE_TS, play it safe and
  724. * set up all the parameters, as it is not obvious which parameters the
  725. * firmware shares across capture channel types and which it does not.
  726. *
  727. * Some of the cx18_vapi() calls below apply to only certain capture
  728. * channel types. We're hoping there's no harm in calling most of them
  729. * anyway, as long as the values are all consistent. Setting some
  730. * shared parameters will have no effect once an analog capture channel
  731. * has started streaming.
  732. */
  733. if (captype != CAPTURE_CHANNEL_TYPE_TS) {
  734. cx18_vapi(cx, CX18_CPU_SET_VER_CROP_LINE, 2, s->handle, 0);
  735. cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 3, s->handle, 3, 1);
  736. cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 3, s->handle, 8, 0);
  737. cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 3, s->handle, 4, 1);
  738. /*
  739. * Audio related reset according to
  740. * Documentation/video4linux/cx2341x/fw-encoder-api.txt
  741. */
  742. if (atomic_read(&cx->ana_capturing) == 0)
  743. cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 2,
  744. s->handle, 12);
  745. /*
  746. * Number of lines for Field 1 & Field 2 according to
  747. * Documentation/video4linux/cx2341x/fw-encoder-api.txt
  748. * Field 1 is 312 for 625 line systems in BT.656
  749. * Field 2 is 313 for 625 line systems in BT.656
  750. */
  751. cx18_vapi(cx, CX18_CPU_SET_CAPTURE_LINE_NO, 3,
  752. s->handle, 312, 313);
  753. if (cx->v4l2_cap & V4L2_CAP_VBI_CAPTURE)
  754. cx18_vbi_setup(s);
  755. /*
  756. * Select to receive I, P, and B frame index entries, if the
  757. * index stream is enabled. Otherwise disable index entry
  758. * generation.
  759. */
  760. s_idx = &cx->streams[CX18_ENC_STREAM_TYPE_IDX];
  761. cx18_vapi_result(cx, data, CX18_CPU_SET_INDEXTABLE, 2,
  762. s->handle, cx18_stream_enabled(s_idx) ? 7 : 0);
  763. /* Call out to the common CX2341x API setup for user controls */
  764. cx->cxhdl.priv = s;
  765. cx2341x_handler_setup(&cx->cxhdl);
  766. /*
  767. * When starting a capture and we're set for radio,
  768. * ensure the video is muted, despite the user control.
  769. */
  770. if (!cx->cxhdl.video_mute &&
  771. test_bit(CX18_F_I_RADIO_USER, &cx->i_flags))
  772. cx18_vapi(cx, CX18_CPU_SET_VIDEO_MUTE, 2, s->handle,
  773. (v4l2_ctrl_g_ctrl(cx->cxhdl.video_mute_yuv) << 8) | 1);
  774. /* Enable the Video Format Converter for UYVY 4:2:2 support,
  775. * rather than the default HM12 Macroblovk 4:2:0 support.
  776. */
  777. if (captype == CAPTURE_CHANNEL_TYPE_YUV) {
  778. if (s->pixelformat == V4L2_PIX_FMT_UYVY)
  779. cx18_vapi(cx, CX18_CPU_SET_VFC_PARAM, 2,
  780. s->handle, 1);
  781. else
  782. /* If in doubt, default to HM12 */
  783. cx18_vapi(cx, CX18_CPU_SET_VFC_PARAM, 2,
  784. s->handle, 0);
  785. }
  786. }
  787. if (atomic_read(&cx->tot_capturing) == 0) {
  788. cx2341x_handler_set_busy(&cx->cxhdl, 1);
  789. clear_bit(CX18_F_I_EOS, &cx->i_flags);
  790. cx18_write_reg(cx, 7, CX18_DSP0_INTERRUPT_MASK);
  791. }
  792. cx18_vapi(cx, CX18_CPU_DE_SET_MDL_ACK, 3, s->handle,
  793. (void __iomem *)&cx->scb->cpu_mdl_ack[s->type][0] - cx->enc_mem,
  794. (void __iomem *)&cx->scb->cpu_mdl_ack[s->type][1] - cx->enc_mem);
  795. /* Init all the cpu_mdls for this stream */
  796. cx18_stream_configure_mdls(s);
  797. _cx18_stream_load_fw_queue(s);
  798. /* begin_capture */
  799. if (cx18_vapi(cx, CX18_CPU_CAPTURE_START, 1, s->handle)) {
  800. CX18_DEBUG_WARN("Error starting capture!\n");
  801. /* Ensure we're really not capturing before releasing MDLs */
  802. set_bit(CX18_F_S_STOPPING, &s->s_flags);
  803. if (s->type == CX18_ENC_STREAM_TYPE_MPG)
  804. cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 2, s->handle, 1);
  805. else
  806. cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 1, s->handle);
  807. clear_bit(CX18_F_S_STREAMING, &s->s_flags);
  808. /* FIXME - CX18_F_S_STREAMOFF as well? */
  809. cx18_vapi(cx, CX18_CPU_DE_RELEASE_MDL, 1, s->handle);
  810. cx18_vapi(cx, CX18_DESTROY_TASK, 1, s->handle);
  811. s->handle = CX18_INVALID_TASK_HANDLE;
  812. clear_bit(CX18_F_S_STOPPING, &s->s_flags);
  813. if (atomic_read(&cx->tot_capturing) == 0) {
  814. set_bit(CX18_F_I_EOS, &cx->i_flags);
  815. cx18_write_reg(cx, 5, CX18_DSP0_INTERRUPT_MASK);
  816. }
  817. return -EINVAL;
  818. }
  819. /* you're live! sit back and await interrupts :) */
  820. if (captype != CAPTURE_CHANNEL_TYPE_TS)
  821. atomic_inc(&cx->ana_capturing);
  822. atomic_inc(&cx->tot_capturing);
  823. return 0;
  824. }
  825. EXPORT_SYMBOL(cx18_start_v4l2_encode_stream);
  826. void cx18_stop_all_captures(struct cx18 *cx)
  827. {
  828. int i;
  829. for (i = CX18_MAX_STREAMS - 1; i >= 0; i--) {
  830. struct cx18_stream *s = &cx->streams[i];
  831. if (!cx18_stream_enabled(s))
  832. continue;
  833. if (test_bit(CX18_F_S_STREAMING, &s->s_flags))
  834. cx18_stop_v4l2_encode_stream(s, 0);
  835. }
  836. }
  837. int cx18_stop_v4l2_encode_stream(struct cx18_stream *s, int gop_end)
  838. {
  839. struct cx18 *cx = s->cx;
  840. unsigned long then;
  841. if (!cx18_stream_enabled(s))
  842. return -EINVAL;
  843. /* This function assumes that you are allowed to stop the capture
  844. and that we are actually capturing */
  845. CX18_DEBUG_INFO("Stop Capture\n");
  846. if (atomic_read(&cx->tot_capturing) == 0)
  847. return 0;
  848. set_bit(CX18_F_S_STOPPING, &s->s_flags);
  849. if (s->type == CX18_ENC_STREAM_TYPE_MPG)
  850. cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 2, s->handle, !gop_end);
  851. else
  852. cx18_vapi(cx, CX18_CPU_CAPTURE_STOP, 1, s->handle);
  853. then = jiffies;
  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 && (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. }