cx18-streams.c 29 KB

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