cx25821-video.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. /*
  2. * Driver for the Conexant CX25821 PCIe bridge
  3. *
  4. * Copyright (C) 2009 Conexant Systems Inc.
  5. * Authors <shu.lin@conexant.com>, <hiep.huynh@conexant.com>
  6. * Based on Steven Toth <stoth@linuxtv.org> cx25821 driver
  7. * Parts adapted/taken from Eduardo Moscoso Rubino
  8. * Copyright (C) 2009 Eduardo Moscoso Rubino <moscoso@TopoLogica.com>
  9. *
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. *
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25. */
  26. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  27. #include "cx25821-video.h"
  28. MODULE_DESCRIPTION("v4l2 driver module for cx25821 based TV cards");
  29. MODULE_AUTHOR("Hiep Huynh <hiep.huynh@conexant.com>");
  30. MODULE_LICENSE("GPL");
  31. static unsigned int video_nr[] = {[0 ... (CX25821_MAXBOARDS - 1)] = UNSET };
  32. module_param_array(video_nr, int, NULL, 0444);
  33. MODULE_PARM_DESC(video_nr, "video device numbers");
  34. static unsigned int video_debug = VIDEO_DEBUG;
  35. module_param(video_debug, int, 0644);
  36. MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
  37. static unsigned int irq_debug;
  38. module_param(irq_debug, int, 0644);
  39. MODULE_PARM_DESC(irq_debug, "enable debug messages [IRQ handler]");
  40. #define FORMAT_FLAGS_PACKED 0x01
  41. static const struct cx25821_fmt formats[] = {
  42. {
  43. .name = "4:1:1, packed, Y41P",
  44. .fourcc = V4L2_PIX_FMT_Y41P,
  45. .depth = 12,
  46. .flags = FORMAT_FLAGS_PACKED,
  47. }, {
  48. .name = "4:2:2, packed, YUYV",
  49. .fourcc = V4L2_PIX_FMT_YUYV,
  50. .depth = 16,
  51. .flags = FORMAT_FLAGS_PACKED,
  52. },
  53. };
  54. static const struct cx25821_fmt *cx25821_format_by_fourcc(unsigned int fourcc)
  55. {
  56. unsigned int i;
  57. for (i = 0; i < ARRAY_SIZE(formats); i++)
  58. if (formats[i].fourcc == fourcc)
  59. return formats + i;
  60. return NULL;
  61. }
  62. int cx25821_start_video_dma(struct cx25821_dev *dev,
  63. struct cx25821_dmaqueue *q,
  64. struct cx25821_buffer *buf,
  65. const struct sram_channel *channel)
  66. {
  67. int tmp = 0;
  68. /* setup fifo + format */
  69. cx25821_sram_channel_setup(dev, channel, buf->bpl, buf->risc.dma);
  70. /* reset counter */
  71. cx_write(channel->gpcnt_ctl, 3);
  72. /* enable irq */
  73. cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | (1 << channel->i));
  74. cx_set(channel->int_msk, 0x11);
  75. /* start dma */
  76. cx_write(channel->dma_ctl, 0x11); /* FIFO and RISC enable */
  77. /* make sure upstream setting if any is reversed */
  78. tmp = cx_read(VID_CH_MODE_SEL);
  79. cx_write(VID_CH_MODE_SEL, tmp & 0xFFFFFE00);
  80. return 0;
  81. }
  82. int cx25821_video_irq(struct cx25821_dev *dev, int chan_num, u32 status)
  83. {
  84. int handled = 0;
  85. u32 mask;
  86. const struct sram_channel *channel = dev->channels[chan_num].sram_channels;
  87. mask = cx_read(channel->int_msk);
  88. if (0 == (status & mask))
  89. return handled;
  90. cx_write(channel->int_stat, status);
  91. /* risc op code error */
  92. if (status & (1 << 16)) {
  93. pr_warn("%s, %s: video risc op code error\n",
  94. dev->name, channel->name);
  95. cx_clear(channel->dma_ctl, 0x11);
  96. cx25821_sram_channel_dump(dev, channel);
  97. }
  98. /* risc1 y */
  99. if (status & FLD_VID_DST_RISC1) {
  100. struct cx25821_dmaqueue *dmaq =
  101. &dev->channels[channel->i].dma_vidq;
  102. struct cx25821_buffer *buf;
  103. spin_lock(&dev->slock);
  104. if (!list_empty(&dmaq->active)) {
  105. buf = list_entry(dmaq->active.next,
  106. struct cx25821_buffer, queue);
  107. v4l2_get_timestamp(&buf->vb.timestamp);
  108. buf->vb.sequence = dmaq->count++;
  109. list_del(&buf->queue);
  110. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
  111. }
  112. spin_unlock(&dev->slock);
  113. handled++;
  114. }
  115. return handled;
  116. }
  117. static int cx25821_queue_setup(struct vb2_queue *q, const void *parg,
  118. unsigned int *num_buffers, unsigned int *num_planes,
  119. unsigned int sizes[], void *alloc_ctxs[])
  120. {
  121. const struct v4l2_format *fmt = parg;
  122. struct cx25821_channel *chan = q->drv_priv;
  123. unsigned size = (chan->fmt->depth * chan->width * chan->height) >> 3;
  124. if (fmt && fmt->fmt.pix.sizeimage < size)
  125. return -EINVAL;
  126. *num_planes = 1;
  127. sizes[0] = fmt ? fmt->fmt.pix.sizeimage : size;
  128. alloc_ctxs[0] = chan->dev->alloc_ctx;
  129. return 0;
  130. }
  131. static int cx25821_buffer_prepare(struct vb2_buffer *vb)
  132. {
  133. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  134. struct cx25821_channel *chan = vb->vb2_queue->drv_priv;
  135. struct cx25821_dev *dev = chan->dev;
  136. struct cx25821_buffer *buf =
  137. container_of(vbuf, struct cx25821_buffer, vb);
  138. struct sg_table *sgt = vb2_dma_sg_plane_desc(vb, 0);
  139. u32 line0_offset;
  140. int bpl_local = LINE_SIZE_D1;
  141. int ret;
  142. if (chan->pixel_formats == PIXEL_FRMT_411)
  143. buf->bpl = (chan->fmt->depth * chan->width) >> 3;
  144. else
  145. buf->bpl = (chan->fmt->depth >> 3) * chan->width;
  146. if (vb2_plane_size(vb, 0) < chan->height * buf->bpl)
  147. return -EINVAL;
  148. vb2_set_plane_payload(vb, 0, chan->height * buf->bpl);
  149. buf->vb.field = chan->field;
  150. if (chan->pixel_formats == PIXEL_FRMT_411) {
  151. bpl_local = buf->bpl;
  152. } else {
  153. bpl_local = buf->bpl; /* Default */
  154. if (chan->use_cif_resolution) {
  155. if (dev->tvnorm & V4L2_STD_625_50)
  156. bpl_local = 352 << 1;
  157. else
  158. bpl_local = chan->cif_width << 1;
  159. }
  160. }
  161. switch (chan->field) {
  162. case V4L2_FIELD_TOP:
  163. ret = cx25821_risc_buffer(dev->pci, &buf->risc,
  164. sgt->sgl, 0, UNSET,
  165. buf->bpl, 0, chan->height);
  166. break;
  167. case V4L2_FIELD_BOTTOM:
  168. ret = cx25821_risc_buffer(dev->pci, &buf->risc,
  169. sgt->sgl, UNSET, 0,
  170. buf->bpl, 0, chan->height);
  171. break;
  172. case V4L2_FIELD_INTERLACED:
  173. /* All other formats are top field first */
  174. line0_offset = 0;
  175. dprintk(1, "top field first\n");
  176. ret = cx25821_risc_buffer(dev->pci, &buf->risc,
  177. sgt->sgl, line0_offset,
  178. bpl_local, bpl_local, bpl_local,
  179. chan->height >> 1);
  180. break;
  181. case V4L2_FIELD_SEQ_TB:
  182. ret = cx25821_risc_buffer(dev->pci, &buf->risc,
  183. sgt->sgl,
  184. 0, buf->bpl * (chan->height >> 1),
  185. buf->bpl, 0, chan->height >> 1);
  186. break;
  187. case V4L2_FIELD_SEQ_BT:
  188. ret = cx25821_risc_buffer(dev->pci, &buf->risc,
  189. sgt->sgl,
  190. buf->bpl * (chan->height >> 1), 0,
  191. buf->bpl, 0, chan->height >> 1);
  192. break;
  193. default:
  194. WARN_ON(1);
  195. ret = -EINVAL;
  196. break;
  197. }
  198. dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
  199. buf, buf->vb.vb2_buf.index, chan->width, chan->height,
  200. chan->fmt->depth, chan->fmt->name,
  201. (unsigned long)buf->risc.dma);
  202. return ret;
  203. }
  204. static void cx25821_buffer_finish(struct vb2_buffer *vb)
  205. {
  206. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  207. struct cx25821_buffer *buf =
  208. container_of(vbuf, struct cx25821_buffer, vb);
  209. struct cx25821_channel *chan = vb->vb2_queue->drv_priv;
  210. struct cx25821_dev *dev = chan->dev;
  211. cx25821_free_buffer(dev, buf);
  212. }
  213. static void cx25821_buffer_queue(struct vb2_buffer *vb)
  214. {
  215. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  216. struct cx25821_buffer *buf =
  217. container_of(vbuf, struct cx25821_buffer, vb);
  218. struct cx25821_channel *chan = vb->vb2_queue->drv_priv;
  219. struct cx25821_dev *dev = chan->dev;
  220. struct cx25821_buffer *prev;
  221. struct cx25821_dmaqueue *q = &dev->channels[chan->id].dma_vidq;
  222. buf->risc.cpu[1] = cpu_to_le32(buf->risc.dma + 12);
  223. buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_CNT_INC);
  224. buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma + 12);
  225. buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
  226. if (list_empty(&q->active)) {
  227. list_add_tail(&buf->queue, &q->active);
  228. } else {
  229. buf->risc.cpu[0] |= cpu_to_le32(RISC_IRQ1);
  230. prev = list_entry(q->active.prev, struct cx25821_buffer,
  231. queue);
  232. list_add_tail(&buf->queue, &q->active);
  233. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  234. }
  235. }
  236. static int cx25821_start_streaming(struct vb2_queue *q, unsigned int count)
  237. {
  238. struct cx25821_channel *chan = q->drv_priv;
  239. struct cx25821_dev *dev = chan->dev;
  240. struct cx25821_dmaqueue *dmaq = &dev->channels[chan->id].dma_vidq;
  241. struct cx25821_buffer *buf = list_entry(dmaq->active.next,
  242. struct cx25821_buffer, queue);
  243. dmaq->count = 0;
  244. cx25821_start_video_dma(dev, dmaq, buf, chan->sram_channels);
  245. return 0;
  246. }
  247. static void cx25821_stop_streaming(struct vb2_queue *q)
  248. {
  249. struct cx25821_channel *chan = q->drv_priv;
  250. struct cx25821_dev *dev = chan->dev;
  251. struct cx25821_dmaqueue *dmaq = &dev->channels[chan->id].dma_vidq;
  252. unsigned long flags;
  253. cx_write(chan->sram_channels->dma_ctl, 0); /* FIFO and RISC disable */
  254. spin_lock_irqsave(&dev->slock, flags);
  255. while (!list_empty(&dmaq->active)) {
  256. struct cx25821_buffer *buf = list_entry(dmaq->active.next,
  257. struct cx25821_buffer, queue);
  258. list_del(&buf->queue);
  259. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
  260. }
  261. spin_unlock_irqrestore(&dev->slock, flags);
  262. }
  263. static struct vb2_ops cx25821_video_qops = {
  264. .queue_setup = cx25821_queue_setup,
  265. .buf_prepare = cx25821_buffer_prepare,
  266. .buf_finish = cx25821_buffer_finish,
  267. .buf_queue = cx25821_buffer_queue,
  268. .wait_prepare = vb2_ops_wait_prepare,
  269. .wait_finish = vb2_ops_wait_finish,
  270. .start_streaming = cx25821_start_streaming,
  271. .stop_streaming = cx25821_stop_streaming,
  272. };
  273. /* VIDEO IOCTLS */
  274. static int cx25821_vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  275. struct v4l2_fmtdesc *f)
  276. {
  277. if (unlikely(f->index >= ARRAY_SIZE(formats)))
  278. return -EINVAL;
  279. strlcpy(f->description, formats[f->index].name, sizeof(f->description));
  280. f->pixelformat = formats[f->index].fourcc;
  281. return 0;
  282. }
  283. static int cx25821_vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  284. struct v4l2_format *f)
  285. {
  286. struct cx25821_channel *chan = video_drvdata(file);
  287. f->fmt.pix.width = chan->width;
  288. f->fmt.pix.height = chan->height;
  289. f->fmt.pix.field = chan->field;
  290. f->fmt.pix.pixelformat = chan->fmt->fourcc;
  291. f->fmt.pix.bytesperline = (chan->width * chan->fmt->depth) >> 3;
  292. f->fmt.pix.sizeimage = chan->height * f->fmt.pix.bytesperline;
  293. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  294. return 0;
  295. }
  296. static int cx25821_vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  297. struct v4l2_format *f)
  298. {
  299. struct cx25821_channel *chan = video_drvdata(file);
  300. struct cx25821_dev *dev = chan->dev;
  301. const struct cx25821_fmt *fmt;
  302. enum v4l2_field field = f->fmt.pix.field;
  303. unsigned int maxh;
  304. unsigned w;
  305. fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
  306. if (NULL == fmt)
  307. return -EINVAL;
  308. maxh = (dev->tvnorm & V4L2_STD_625_50) ? 576 : 480;
  309. w = f->fmt.pix.width;
  310. if (field != V4L2_FIELD_BOTTOM)
  311. field = V4L2_FIELD_TOP;
  312. if (w < 352) {
  313. w = 176;
  314. f->fmt.pix.height = maxh / 4;
  315. } else if (w < 720) {
  316. w = 352;
  317. f->fmt.pix.height = maxh / 2;
  318. } else {
  319. w = 720;
  320. f->fmt.pix.height = maxh;
  321. field = V4L2_FIELD_INTERLACED;
  322. }
  323. f->fmt.pix.field = field;
  324. f->fmt.pix.width = w;
  325. f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
  326. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  327. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  328. return 0;
  329. }
  330. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  331. struct v4l2_format *f)
  332. {
  333. struct cx25821_channel *chan = video_drvdata(file);
  334. struct cx25821_dev *dev = chan->dev;
  335. int pix_format = PIXEL_FRMT_422;
  336. int err;
  337. err = cx25821_vidioc_try_fmt_vid_cap(file, priv, f);
  338. if (0 != err)
  339. return err;
  340. chan->fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
  341. chan->field = f->fmt.pix.field;
  342. chan->width = f->fmt.pix.width;
  343. chan->height = f->fmt.pix.height;
  344. if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_Y41P)
  345. pix_format = PIXEL_FRMT_411;
  346. else
  347. pix_format = PIXEL_FRMT_422;
  348. cx25821_set_pixel_format(dev, SRAM_CH00, pix_format);
  349. /* check if cif resolution */
  350. if (chan->width == 320 || chan->width == 352)
  351. chan->use_cif_resolution = 1;
  352. else
  353. chan->use_cif_resolution = 0;
  354. chan->cif_width = chan->width;
  355. medusa_set_resolution(dev, chan->width, SRAM_CH00);
  356. return 0;
  357. }
  358. static int vidioc_log_status(struct file *file, void *priv)
  359. {
  360. struct cx25821_channel *chan = video_drvdata(file);
  361. struct cx25821_dev *dev = chan->dev;
  362. const struct sram_channel *sram_ch = chan->sram_channels;
  363. u32 tmp = 0;
  364. tmp = cx_read(sram_ch->dma_ctl);
  365. pr_info("Video input 0 is %s\n",
  366. (tmp & 0x11) ? "streaming" : "stopped");
  367. return 0;
  368. }
  369. static int cx25821_vidioc_querycap(struct file *file, void *priv,
  370. struct v4l2_capability *cap)
  371. {
  372. struct cx25821_channel *chan = video_drvdata(file);
  373. struct cx25821_dev *dev = chan->dev;
  374. const u32 cap_input = V4L2_CAP_VIDEO_CAPTURE |
  375. V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  376. const u32 cap_output = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_READWRITE;
  377. strcpy(cap->driver, "cx25821");
  378. strlcpy(cap->card, cx25821_boards[dev->board].name, sizeof(cap->card));
  379. sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
  380. if (chan->id >= VID_CHANNEL_NUM)
  381. cap->device_caps = cap_output;
  382. else
  383. cap->device_caps = cap_input;
  384. cap->capabilities = cap_input | cap_output | V4L2_CAP_DEVICE_CAPS;
  385. return 0;
  386. }
  387. static int cx25821_vidioc_g_std(struct file *file, void *priv, v4l2_std_id *tvnorms)
  388. {
  389. struct cx25821_channel *chan = video_drvdata(file);
  390. *tvnorms = chan->dev->tvnorm;
  391. return 0;
  392. }
  393. static int cx25821_vidioc_s_std(struct file *file, void *priv,
  394. v4l2_std_id tvnorms)
  395. {
  396. struct cx25821_channel *chan = video_drvdata(file);
  397. struct cx25821_dev *dev = chan->dev;
  398. if (dev->tvnorm == tvnorms)
  399. return 0;
  400. dev->tvnorm = tvnorms;
  401. chan->width = 720;
  402. chan->height = (dev->tvnorm & V4L2_STD_625_50) ? 576 : 480;
  403. medusa_set_videostandard(dev);
  404. return 0;
  405. }
  406. static int cx25821_vidioc_enum_input(struct file *file, void *priv,
  407. struct v4l2_input *i)
  408. {
  409. if (i->index)
  410. return -EINVAL;
  411. i->type = V4L2_INPUT_TYPE_CAMERA;
  412. i->std = CX25821_NORMS;
  413. strcpy(i->name, "Composite");
  414. return 0;
  415. }
  416. static int cx25821_vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  417. {
  418. *i = 0;
  419. return 0;
  420. }
  421. static int cx25821_vidioc_s_input(struct file *file, void *priv, unsigned int i)
  422. {
  423. return i ? -EINVAL : 0;
  424. }
  425. static int cx25821_s_ctrl(struct v4l2_ctrl *ctrl)
  426. {
  427. struct cx25821_channel *chan =
  428. container_of(ctrl->handler, struct cx25821_channel, hdl);
  429. struct cx25821_dev *dev = chan->dev;
  430. switch (ctrl->id) {
  431. case V4L2_CID_BRIGHTNESS:
  432. medusa_set_brightness(dev, ctrl->val, chan->id);
  433. break;
  434. case V4L2_CID_HUE:
  435. medusa_set_hue(dev, ctrl->val, chan->id);
  436. break;
  437. case V4L2_CID_CONTRAST:
  438. medusa_set_contrast(dev, ctrl->val, chan->id);
  439. break;
  440. case V4L2_CID_SATURATION:
  441. medusa_set_saturation(dev, ctrl->val, chan->id);
  442. break;
  443. default:
  444. return -EINVAL;
  445. }
  446. return 0;
  447. }
  448. static int cx25821_vidioc_enum_output(struct file *file, void *priv,
  449. struct v4l2_output *o)
  450. {
  451. if (o->index)
  452. return -EINVAL;
  453. o->type = V4L2_INPUT_TYPE_CAMERA;
  454. o->std = CX25821_NORMS;
  455. strcpy(o->name, "Composite");
  456. return 0;
  457. }
  458. static int cx25821_vidioc_g_output(struct file *file, void *priv, unsigned int *o)
  459. {
  460. *o = 0;
  461. return 0;
  462. }
  463. static int cx25821_vidioc_s_output(struct file *file, void *priv, unsigned int o)
  464. {
  465. return o ? -EINVAL : 0;
  466. }
  467. static int cx25821_vidioc_try_fmt_vid_out(struct file *file, void *priv,
  468. struct v4l2_format *f)
  469. {
  470. struct cx25821_channel *chan = video_drvdata(file);
  471. struct cx25821_dev *dev = chan->dev;
  472. const struct cx25821_fmt *fmt;
  473. fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
  474. if (NULL == fmt)
  475. return -EINVAL;
  476. f->fmt.pix.width = 720;
  477. f->fmt.pix.height = (dev->tvnorm & V4L2_STD_625_50) ? 576 : 480;
  478. f->fmt.pix.field = V4L2_FIELD_INTERLACED;
  479. f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
  480. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  481. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  482. return 0;
  483. }
  484. static int vidioc_s_fmt_vid_out(struct file *file, void *priv,
  485. struct v4l2_format *f)
  486. {
  487. struct cx25821_channel *chan = video_drvdata(file);
  488. int err;
  489. err = cx25821_vidioc_try_fmt_vid_out(file, priv, f);
  490. if (0 != err)
  491. return err;
  492. chan->fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
  493. chan->field = f->fmt.pix.field;
  494. chan->width = f->fmt.pix.width;
  495. chan->height = f->fmt.pix.height;
  496. if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_Y41P)
  497. chan->pixel_formats = PIXEL_FRMT_411;
  498. else
  499. chan->pixel_formats = PIXEL_FRMT_422;
  500. return 0;
  501. }
  502. static const struct v4l2_ctrl_ops cx25821_ctrl_ops = {
  503. .s_ctrl = cx25821_s_ctrl,
  504. };
  505. static const struct v4l2_file_operations video_fops = {
  506. .owner = THIS_MODULE,
  507. .open = v4l2_fh_open,
  508. .release = vb2_fop_release,
  509. .read = vb2_fop_read,
  510. .poll = vb2_fop_poll,
  511. .unlocked_ioctl = video_ioctl2,
  512. .mmap = vb2_fop_mmap,
  513. };
  514. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  515. .vidioc_querycap = cx25821_vidioc_querycap,
  516. .vidioc_enum_fmt_vid_cap = cx25821_vidioc_enum_fmt_vid_cap,
  517. .vidioc_g_fmt_vid_cap = cx25821_vidioc_g_fmt_vid_cap,
  518. .vidioc_try_fmt_vid_cap = cx25821_vidioc_try_fmt_vid_cap,
  519. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  520. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  521. .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
  522. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  523. .vidioc_querybuf = vb2_ioctl_querybuf,
  524. .vidioc_qbuf = vb2_ioctl_qbuf,
  525. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  526. .vidioc_streamon = vb2_ioctl_streamon,
  527. .vidioc_streamoff = vb2_ioctl_streamoff,
  528. .vidioc_g_std = cx25821_vidioc_g_std,
  529. .vidioc_s_std = cx25821_vidioc_s_std,
  530. .vidioc_enum_input = cx25821_vidioc_enum_input,
  531. .vidioc_g_input = cx25821_vidioc_g_input,
  532. .vidioc_s_input = cx25821_vidioc_s_input,
  533. .vidioc_log_status = vidioc_log_status,
  534. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  535. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  536. };
  537. static const struct video_device cx25821_video_device = {
  538. .name = "cx25821-video",
  539. .fops = &video_fops,
  540. .release = video_device_release_empty,
  541. .minor = -1,
  542. .ioctl_ops = &video_ioctl_ops,
  543. .tvnorms = CX25821_NORMS,
  544. };
  545. static const struct v4l2_file_operations video_out_fops = {
  546. .owner = THIS_MODULE,
  547. .open = v4l2_fh_open,
  548. .release = vb2_fop_release,
  549. .write = vb2_fop_write,
  550. .poll = vb2_fop_poll,
  551. .unlocked_ioctl = video_ioctl2,
  552. .mmap = vb2_fop_mmap,
  553. };
  554. static const struct v4l2_ioctl_ops video_out_ioctl_ops = {
  555. .vidioc_querycap = cx25821_vidioc_querycap,
  556. .vidioc_enum_fmt_vid_out = cx25821_vidioc_enum_fmt_vid_cap,
  557. .vidioc_g_fmt_vid_out = cx25821_vidioc_g_fmt_vid_cap,
  558. .vidioc_try_fmt_vid_out = cx25821_vidioc_try_fmt_vid_out,
  559. .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
  560. .vidioc_g_std = cx25821_vidioc_g_std,
  561. .vidioc_s_std = cx25821_vidioc_s_std,
  562. .vidioc_enum_output = cx25821_vidioc_enum_output,
  563. .vidioc_g_output = cx25821_vidioc_g_output,
  564. .vidioc_s_output = cx25821_vidioc_s_output,
  565. .vidioc_log_status = vidioc_log_status,
  566. };
  567. static const struct video_device cx25821_video_out_device = {
  568. .name = "cx25821-video",
  569. .fops = &video_out_fops,
  570. .release = video_device_release_empty,
  571. .minor = -1,
  572. .ioctl_ops = &video_out_ioctl_ops,
  573. .tvnorms = CX25821_NORMS,
  574. };
  575. void cx25821_video_unregister(struct cx25821_dev *dev, int chan_num)
  576. {
  577. cx_clear(PCI_INT_MSK, 1);
  578. if (video_is_registered(&dev->channels[chan_num].vdev)) {
  579. video_unregister_device(&dev->channels[chan_num].vdev);
  580. v4l2_ctrl_handler_free(&dev->channels[chan_num].hdl);
  581. }
  582. }
  583. int cx25821_video_register(struct cx25821_dev *dev)
  584. {
  585. int err;
  586. int i;
  587. /* initial device configuration */
  588. dev->tvnorm = V4L2_STD_NTSC_M;
  589. spin_lock_init(&dev->slock);
  590. for (i = 0; i < MAX_VID_CAP_CHANNEL_NUM - 1; ++i) {
  591. struct cx25821_channel *chan = &dev->channels[i];
  592. struct video_device *vdev = &chan->vdev;
  593. struct v4l2_ctrl_handler *hdl = &chan->hdl;
  594. struct vb2_queue *q;
  595. bool is_output = i > SRAM_CH08;
  596. if (i == SRAM_CH08) /* audio channel */
  597. continue;
  598. if (!is_output) {
  599. v4l2_ctrl_handler_init(hdl, 4);
  600. v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
  601. V4L2_CID_BRIGHTNESS, 0, 10000, 1, 6200);
  602. v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
  603. V4L2_CID_CONTRAST, 0, 10000, 1, 5000);
  604. v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
  605. V4L2_CID_SATURATION, 0, 10000, 1, 5000);
  606. v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
  607. V4L2_CID_HUE, 0, 10000, 1, 5000);
  608. if (hdl->error) {
  609. err = hdl->error;
  610. goto fail_unreg;
  611. }
  612. err = v4l2_ctrl_handler_setup(hdl);
  613. if (err)
  614. goto fail_unreg;
  615. } else {
  616. chan->out = &dev->vid_out_data[i - SRAM_CH09];
  617. chan->out->chan = chan;
  618. }
  619. chan->sram_channels = &cx25821_sram_channels[i];
  620. chan->width = 720;
  621. chan->field = V4L2_FIELD_INTERLACED;
  622. if (dev->tvnorm & V4L2_STD_625_50)
  623. chan->height = 576;
  624. else
  625. chan->height = 480;
  626. if (chan->pixel_formats == PIXEL_FRMT_411)
  627. chan->fmt = cx25821_format_by_fourcc(V4L2_PIX_FMT_Y41P);
  628. else
  629. chan->fmt = cx25821_format_by_fourcc(V4L2_PIX_FMT_YUYV);
  630. cx_write(chan->sram_channels->int_stat, 0xffffffff);
  631. INIT_LIST_HEAD(&chan->dma_vidq.active);
  632. q = &chan->vidq;
  633. q->type = is_output ? V4L2_BUF_TYPE_VIDEO_OUTPUT :
  634. V4L2_BUF_TYPE_VIDEO_CAPTURE;
  635. q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
  636. q->io_modes |= is_output ? VB2_WRITE : VB2_READ;
  637. q->gfp_flags = GFP_DMA32;
  638. q->min_buffers_needed = 2;
  639. q->drv_priv = chan;
  640. q->buf_struct_size = sizeof(struct cx25821_buffer);
  641. q->ops = &cx25821_video_qops;
  642. q->mem_ops = &vb2_dma_sg_memops;
  643. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  644. q->lock = &dev->lock;
  645. if (!is_output) {
  646. err = vb2_queue_init(q);
  647. if (err < 0)
  648. goto fail_unreg;
  649. }
  650. /* register v4l devices */
  651. *vdev = is_output ? cx25821_video_out_device : cx25821_video_device;
  652. vdev->v4l2_dev = &dev->v4l2_dev;
  653. if (!is_output)
  654. vdev->ctrl_handler = hdl;
  655. else
  656. vdev->vfl_dir = VFL_DIR_TX;
  657. vdev->lock = &dev->lock;
  658. vdev->queue = q;
  659. snprintf(vdev->name, sizeof(vdev->name), "%s #%d", dev->name, i);
  660. video_set_drvdata(vdev, chan);
  661. err = video_register_device(vdev, VFL_TYPE_GRABBER,
  662. video_nr[dev->nr]);
  663. if (err < 0)
  664. goto fail_unreg;
  665. }
  666. /* set PCI interrupt */
  667. cx_set(PCI_INT_MSK, 0xff);
  668. return 0;
  669. fail_unreg:
  670. while (i >= 0)
  671. cx25821_video_unregister(dev, i--);
  672. return err;
  673. }