cx18-streams.c 25 KB

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