cx18-streams.c 29 KB

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