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. buf->vb.vb2_buf.timestamp = ktime_get_ns();
  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,
  118. unsigned int *num_buffers, unsigned int *num_planes,
  119. unsigned int sizes[], struct device *alloc_devs[])
  120. {
  121. struct cx25821_channel *chan = q->drv_priv;
  122. unsigned size = (chan->fmt->depth * chan->width * chan->height) >> 3;
  123. if (*num_planes)
  124. return sizes[0] < size ? -EINVAL : 0;
  125. *num_planes = 1;
  126. sizes[0] = size;
  127. return 0;
  128. }
  129. static int cx25821_buffer_prepare(struct vb2_buffer *vb)
  130. {
  131. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  132. struct cx25821_channel *chan = vb->vb2_queue->drv_priv;
  133. struct cx25821_dev *dev = chan->dev;
  134. struct cx25821_buffer *buf =
  135. container_of(vbuf, struct cx25821_buffer, vb);
  136. struct sg_table *sgt = vb2_dma_sg_plane_desc(vb, 0);
  137. u32 line0_offset;
  138. int bpl_local = LINE_SIZE_D1;
  139. int ret;
  140. if (chan->pixel_formats == PIXEL_FRMT_411)
  141. buf->bpl = (chan->fmt->depth * chan->width) >> 3;
  142. else
  143. buf->bpl = (chan->fmt->depth >> 3) * chan->width;
  144. if (vb2_plane_size(vb, 0) < chan->height * buf->bpl)
  145. return -EINVAL;
  146. vb2_set_plane_payload(vb, 0, chan->height * buf->bpl);
  147. buf->vb.field = chan->field;
  148. if (chan->pixel_formats == PIXEL_FRMT_411) {
  149. bpl_local = buf->bpl;
  150. } else {
  151. bpl_local = buf->bpl; /* Default */
  152. if (chan->use_cif_resolution) {
  153. if (dev->tvnorm & V4L2_STD_625_50)
  154. bpl_local = 352 << 1;
  155. else
  156. bpl_local = chan->cif_width << 1;
  157. }
  158. }
  159. switch (chan->field) {
  160. case V4L2_FIELD_TOP:
  161. ret = cx25821_risc_buffer(dev->pci, &buf->risc,
  162. sgt->sgl, 0, UNSET,
  163. buf->bpl, 0, chan->height);
  164. break;
  165. case V4L2_FIELD_BOTTOM:
  166. ret = cx25821_risc_buffer(dev->pci, &buf->risc,
  167. sgt->sgl, UNSET, 0,
  168. buf->bpl, 0, chan->height);
  169. break;
  170. case V4L2_FIELD_INTERLACED:
  171. /* All other formats are top field first */
  172. line0_offset = 0;
  173. dprintk(1, "top field first\n");
  174. ret = cx25821_risc_buffer(dev->pci, &buf->risc,
  175. sgt->sgl, line0_offset,
  176. bpl_local, bpl_local, bpl_local,
  177. chan->height >> 1);
  178. break;
  179. case V4L2_FIELD_SEQ_TB:
  180. ret = cx25821_risc_buffer(dev->pci, &buf->risc,
  181. sgt->sgl,
  182. 0, buf->bpl * (chan->height >> 1),
  183. buf->bpl, 0, chan->height >> 1);
  184. break;
  185. case V4L2_FIELD_SEQ_BT:
  186. ret = cx25821_risc_buffer(dev->pci, &buf->risc,
  187. sgt->sgl,
  188. buf->bpl * (chan->height >> 1), 0,
  189. buf->bpl, 0, chan->height >> 1);
  190. break;
  191. default:
  192. WARN_ON(1);
  193. ret = -EINVAL;
  194. break;
  195. }
  196. dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
  197. buf, buf->vb.vb2_buf.index, chan->width, chan->height,
  198. chan->fmt->depth, chan->fmt->name,
  199. (unsigned long)buf->risc.dma);
  200. return ret;
  201. }
  202. static void cx25821_buffer_finish(struct vb2_buffer *vb)
  203. {
  204. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  205. struct cx25821_buffer *buf =
  206. container_of(vbuf, struct cx25821_buffer, vb);
  207. struct cx25821_channel *chan = vb->vb2_queue->drv_priv;
  208. struct cx25821_dev *dev = chan->dev;
  209. cx25821_free_buffer(dev, buf);
  210. }
  211. static void cx25821_buffer_queue(struct vb2_buffer *vb)
  212. {
  213. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  214. struct cx25821_buffer *buf =
  215. container_of(vbuf, struct cx25821_buffer, vb);
  216. struct cx25821_channel *chan = vb->vb2_queue->drv_priv;
  217. struct cx25821_dev *dev = chan->dev;
  218. struct cx25821_buffer *prev;
  219. struct cx25821_dmaqueue *q = &dev->channels[chan->id].dma_vidq;
  220. buf->risc.cpu[1] = cpu_to_le32(buf->risc.dma + 12);
  221. buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_CNT_INC);
  222. buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma + 12);
  223. buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
  224. if (list_empty(&q->active)) {
  225. list_add_tail(&buf->queue, &q->active);
  226. } else {
  227. buf->risc.cpu[0] |= cpu_to_le32(RISC_IRQ1);
  228. prev = list_entry(q->active.prev, struct cx25821_buffer,
  229. queue);
  230. list_add_tail(&buf->queue, &q->active);
  231. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  232. }
  233. }
  234. static int cx25821_start_streaming(struct vb2_queue *q, unsigned int count)
  235. {
  236. struct cx25821_channel *chan = q->drv_priv;
  237. struct cx25821_dev *dev = chan->dev;
  238. struct cx25821_dmaqueue *dmaq = &dev->channels[chan->id].dma_vidq;
  239. struct cx25821_buffer *buf = list_entry(dmaq->active.next,
  240. struct cx25821_buffer, queue);
  241. dmaq->count = 0;
  242. cx25821_start_video_dma(dev, dmaq, buf, chan->sram_channels);
  243. return 0;
  244. }
  245. static void cx25821_stop_streaming(struct vb2_queue *q)
  246. {
  247. struct cx25821_channel *chan = q->drv_priv;
  248. struct cx25821_dev *dev = chan->dev;
  249. struct cx25821_dmaqueue *dmaq = &dev->channels[chan->id].dma_vidq;
  250. unsigned long flags;
  251. cx_write(chan->sram_channels->dma_ctl, 0); /* FIFO and RISC disable */
  252. spin_lock_irqsave(&dev->slock, flags);
  253. while (!list_empty(&dmaq->active)) {
  254. struct cx25821_buffer *buf = list_entry(dmaq->active.next,
  255. struct cx25821_buffer, queue);
  256. list_del(&buf->queue);
  257. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
  258. }
  259. spin_unlock_irqrestore(&dev->slock, flags);
  260. }
  261. static const struct vb2_ops cx25821_video_qops = {
  262. .queue_setup = cx25821_queue_setup,
  263. .buf_prepare = cx25821_buffer_prepare,
  264. .buf_finish = cx25821_buffer_finish,
  265. .buf_queue = cx25821_buffer_queue,
  266. .wait_prepare = vb2_ops_wait_prepare,
  267. .wait_finish = vb2_ops_wait_finish,
  268. .start_streaming = cx25821_start_streaming,
  269. .stop_streaming = cx25821_stop_streaming,
  270. };
  271. /* VIDEO IOCTLS */
  272. static int cx25821_vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  273. struct v4l2_fmtdesc *f)
  274. {
  275. if (unlikely(f->index >= ARRAY_SIZE(formats)))
  276. return -EINVAL;
  277. strlcpy(f->description, formats[f->index].name, sizeof(f->description));
  278. f->pixelformat = formats[f->index].fourcc;
  279. return 0;
  280. }
  281. static int cx25821_vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  282. struct v4l2_format *f)
  283. {
  284. struct cx25821_channel *chan = video_drvdata(file);
  285. f->fmt.pix.width = chan->width;
  286. f->fmt.pix.height = chan->height;
  287. f->fmt.pix.field = chan->field;
  288. f->fmt.pix.pixelformat = chan->fmt->fourcc;
  289. f->fmt.pix.bytesperline = (chan->width * chan->fmt->depth) >> 3;
  290. f->fmt.pix.sizeimage = chan->height * f->fmt.pix.bytesperline;
  291. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  292. return 0;
  293. }
  294. static int cx25821_vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  295. struct v4l2_format *f)
  296. {
  297. struct cx25821_channel *chan = video_drvdata(file);
  298. struct cx25821_dev *dev = chan->dev;
  299. const struct cx25821_fmt *fmt;
  300. enum v4l2_field field = f->fmt.pix.field;
  301. unsigned int maxh;
  302. unsigned w;
  303. fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
  304. if (NULL == fmt)
  305. return -EINVAL;
  306. maxh = (dev->tvnorm & V4L2_STD_625_50) ? 576 : 480;
  307. w = f->fmt.pix.width;
  308. if (field != V4L2_FIELD_BOTTOM)
  309. field = V4L2_FIELD_TOP;
  310. if (w < 352) {
  311. w = 176;
  312. f->fmt.pix.height = maxh / 4;
  313. } else if (w < 720) {
  314. w = 352;
  315. f->fmt.pix.height = maxh / 2;
  316. } else {
  317. w = 720;
  318. f->fmt.pix.height = maxh;
  319. field = V4L2_FIELD_INTERLACED;
  320. }
  321. f->fmt.pix.field = field;
  322. f->fmt.pix.width = w;
  323. f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
  324. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  325. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  326. return 0;
  327. }
  328. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  329. struct v4l2_format *f)
  330. {
  331. struct cx25821_channel *chan = video_drvdata(file);
  332. struct cx25821_dev *dev = chan->dev;
  333. int pix_format = PIXEL_FRMT_422;
  334. int err;
  335. err = cx25821_vidioc_try_fmt_vid_cap(file, priv, f);
  336. if (0 != err)
  337. return err;
  338. chan->fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
  339. chan->field = f->fmt.pix.field;
  340. chan->width = f->fmt.pix.width;
  341. chan->height = f->fmt.pix.height;
  342. if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_Y41P)
  343. pix_format = PIXEL_FRMT_411;
  344. else
  345. pix_format = PIXEL_FRMT_422;
  346. cx25821_set_pixel_format(dev, SRAM_CH00, pix_format);
  347. /* check if cif resolution */
  348. if (chan->width == 320 || chan->width == 352)
  349. chan->use_cif_resolution = 1;
  350. else
  351. chan->use_cif_resolution = 0;
  352. chan->cif_width = chan->width;
  353. medusa_set_resolution(dev, chan->width, SRAM_CH00);
  354. return 0;
  355. }
  356. static int vidioc_log_status(struct file *file, void *priv)
  357. {
  358. struct cx25821_channel *chan = video_drvdata(file);
  359. struct cx25821_dev *dev = chan->dev;
  360. const struct sram_channel *sram_ch = chan->sram_channels;
  361. u32 tmp = 0;
  362. tmp = cx_read(sram_ch->dma_ctl);
  363. pr_info("Video input 0 is %s\n",
  364. (tmp & 0x11) ? "streaming" : "stopped");
  365. return 0;
  366. }
  367. static int cx25821_vidioc_querycap(struct file *file, void *priv,
  368. struct v4l2_capability *cap)
  369. {
  370. struct cx25821_channel *chan = video_drvdata(file);
  371. struct cx25821_dev *dev = chan->dev;
  372. const u32 cap_input = V4L2_CAP_VIDEO_CAPTURE |
  373. V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  374. const u32 cap_output = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_READWRITE;
  375. strcpy(cap->driver, "cx25821");
  376. strlcpy(cap->card, cx25821_boards[dev->board].name, sizeof(cap->card));
  377. sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
  378. if (chan->id >= VID_CHANNEL_NUM)
  379. cap->device_caps = cap_output;
  380. else
  381. cap->device_caps = cap_input;
  382. cap->capabilities = cap_input | cap_output | V4L2_CAP_DEVICE_CAPS;
  383. return 0;
  384. }
  385. static int cx25821_vidioc_g_std(struct file *file, void *priv, v4l2_std_id *tvnorms)
  386. {
  387. struct cx25821_channel *chan = video_drvdata(file);
  388. *tvnorms = chan->dev->tvnorm;
  389. return 0;
  390. }
  391. static int cx25821_vidioc_s_std(struct file *file, void *priv,
  392. v4l2_std_id tvnorms)
  393. {
  394. struct cx25821_channel *chan = video_drvdata(file);
  395. struct cx25821_dev *dev = chan->dev;
  396. if (dev->tvnorm == tvnorms)
  397. return 0;
  398. dev->tvnorm = tvnorms;
  399. chan->width = 720;
  400. chan->height = (dev->tvnorm & V4L2_STD_625_50) ? 576 : 480;
  401. medusa_set_videostandard(dev);
  402. return 0;
  403. }
  404. static int cx25821_vidioc_enum_input(struct file *file, void *priv,
  405. struct v4l2_input *i)
  406. {
  407. if (i->index)
  408. return -EINVAL;
  409. i->type = V4L2_INPUT_TYPE_CAMERA;
  410. i->std = CX25821_NORMS;
  411. strcpy(i->name, "Composite");
  412. return 0;
  413. }
  414. static int cx25821_vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  415. {
  416. *i = 0;
  417. return 0;
  418. }
  419. static int cx25821_vidioc_s_input(struct file *file, void *priv, unsigned int i)
  420. {
  421. return i ? -EINVAL : 0;
  422. }
  423. static int cx25821_s_ctrl(struct v4l2_ctrl *ctrl)
  424. {
  425. struct cx25821_channel *chan =
  426. container_of(ctrl->handler, struct cx25821_channel, hdl);
  427. struct cx25821_dev *dev = chan->dev;
  428. switch (ctrl->id) {
  429. case V4L2_CID_BRIGHTNESS:
  430. medusa_set_brightness(dev, ctrl->val, chan->id);
  431. break;
  432. case V4L2_CID_HUE:
  433. medusa_set_hue(dev, ctrl->val, chan->id);
  434. break;
  435. case V4L2_CID_CONTRAST:
  436. medusa_set_contrast(dev, ctrl->val, chan->id);
  437. break;
  438. case V4L2_CID_SATURATION:
  439. medusa_set_saturation(dev, ctrl->val, chan->id);
  440. break;
  441. default:
  442. return -EINVAL;
  443. }
  444. return 0;
  445. }
  446. static int cx25821_vidioc_enum_output(struct file *file, void *priv,
  447. struct v4l2_output *o)
  448. {
  449. if (o->index)
  450. return -EINVAL;
  451. o->type = V4L2_INPUT_TYPE_CAMERA;
  452. o->std = CX25821_NORMS;
  453. strcpy(o->name, "Composite");
  454. return 0;
  455. }
  456. static int cx25821_vidioc_g_output(struct file *file, void *priv, unsigned int *o)
  457. {
  458. *o = 0;
  459. return 0;
  460. }
  461. static int cx25821_vidioc_s_output(struct file *file, void *priv, unsigned int o)
  462. {
  463. return o ? -EINVAL : 0;
  464. }
  465. static int cx25821_vidioc_try_fmt_vid_out(struct file *file, void *priv,
  466. struct v4l2_format *f)
  467. {
  468. struct cx25821_channel *chan = video_drvdata(file);
  469. struct cx25821_dev *dev = chan->dev;
  470. const struct cx25821_fmt *fmt;
  471. fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
  472. if (NULL == fmt)
  473. return -EINVAL;
  474. f->fmt.pix.width = 720;
  475. f->fmt.pix.height = (dev->tvnorm & V4L2_STD_625_50) ? 576 : 480;
  476. f->fmt.pix.field = V4L2_FIELD_INTERLACED;
  477. f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
  478. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  479. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  480. return 0;
  481. }
  482. static int vidioc_s_fmt_vid_out(struct file *file, void *priv,
  483. struct v4l2_format *f)
  484. {
  485. struct cx25821_channel *chan = video_drvdata(file);
  486. int err;
  487. err = cx25821_vidioc_try_fmt_vid_out(file, priv, f);
  488. if (0 != err)
  489. return err;
  490. chan->fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
  491. chan->field = f->fmt.pix.field;
  492. chan->width = f->fmt.pix.width;
  493. chan->height = f->fmt.pix.height;
  494. if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_Y41P)
  495. chan->pixel_formats = PIXEL_FRMT_411;
  496. else
  497. chan->pixel_formats = PIXEL_FRMT_422;
  498. return 0;
  499. }
  500. static const struct v4l2_ctrl_ops cx25821_ctrl_ops = {
  501. .s_ctrl = cx25821_s_ctrl,
  502. };
  503. static const struct v4l2_file_operations video_fops = {
  504. .owner = THIS_MODULE,
  505. .open = v4l2_fh_open,
  506. .release = vb2_fop_release,
  507. .read = vb2_fop_read,
  508. .poll = vb2_fop_poll,
  509. .unlocked_ioctl = video_ioctl2,
  510. .mmap = vb2_fop_mmap,
  511. };
  512. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  513. .vidioc_querycap = cx25821_vidioc_querycap,
  514. .vidioc_enum_fmt_vid_cap = cx25821_vidioc_enum_fmt_vid_cap,
  515. .vidioc_g_fmt_vid_cap = cx25821_vidioc_g_fmt_vid_cap,
  516. .vidioc_try_fmt_vid_cap = cx25821_vidioc_try_fmt_vid_cap,
  517. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  518. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  519. .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
  520. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  521. .vidioc_querybuf = vb2_ioctl_querybuf,
  522. .vidioc_qbuf = vb2_ioctl_qbuf,
  523. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  524. .vidioc_streamon = vb2_ioctl_streamon,
  525. .vidioc_streamoff = vb2_ioctl_streamoff,
  526. .vidioc_g_std = cx25821_vidioc_g_std,
  527. .vidioc_s_std = cx25821_vidioc_s_std,
  528. .vidioc_enum_input = cx25821_vidioc_enum_input,
  529. .vidioc_g_input = cx25821_vidioc_g_input,
  530. .vidioc_s_input = cx25821_vidioc_s_input,
  531. .vidioc_log_status = vidioc_log_status,
  532. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  533. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  534. };
  535. static const struct video_device cx25821_video_device = {
  536. .name = "cx25821-video",
  537. .fops = &video_fops,
  538. .release = video_device_release_empty,
  539. .minor = -1,
  540. .ioctl_ops = &video_ioctl_ops,
  541. .tvnorms = CX25821_NORMS,
  542. };
  543. static const struct v4l2_file_operations video_out_fops = {
  544. .owner = THIS_MODULE,
  545. .open = v4l2_fh_open,
  546. .release = vb2_fop_release,
  547. .write = vb2_fop_write,
  548. .poll = vb2_fop_poll,
  549. .unlocked_ioctl = video_ioctl2,
  550. .mmap = vb2_fop_mmap,
  551. };
  552. static const struct v4l2_ioctl_ops video_out_ioctl_ops = {
  553. .vidioc_querycap = cx25821_vidioc_querycap,
  554. .vidioc_enum_fmt_vid_out = cx25821_vidioc_enum_fmt_vid_cap,
  555. .vidioc_g_fmt_vid_out = cx25821_vidioc_g_fmt_vid_cap,
  556. .vidioc_try_fmt_vid_out = cx25821_vidioc_try_fmt_vid_out,
  557. .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
  558. .vidioc_g_std = cx25821_vidioc_g_std,
  559. .vidioc_s_std = cx25821_vidioc_s_std,
  560. .vidioc_enum_output = cx25821_vidioc_enum_output,
  561. .vidioc_g_output = cx25821_vidioc_g_output,
  562. .vidioc_s_output = cx25821_vidioc_s_output,
  563. .vidioc_log_status = vidioc_log_status,
  564. };
  565. static const struct video_device cx25821_video_out_device = {
  566. .name = "cx25821-video",
  567. .fops = &video_out_fops,
  568. .release = video_device_release_empty,
  569. .minor = -1,
  570. .ioctl_ops = &video_out_ioctl_ops,
  571. .tvnorms = CX25821_NORMS,
  572. };
  573. void cx25821_video_unregister(struct cx25821_dev *dev, int chan_num)
  574. {
  575. cx_clear(PCI_INT_MSK, 1);
  576. if (video_is_registered(&dev->channels[chan_num].vdev)) {
  577. video_unregister_device(&dev->channels[chan_num].vdev);
  578. v4l2_ctrl_handler_free(&dev->channels[chan_num].hdl);
  579. }
  580. }
  581. int cx25821_video_register(struct cx25821_dev *dev)
  582. {
  583. int err;
  584. int i;
  585. /* initial device configuration */
  586. dev->tvnorm = V4L2_STD_NTSC_M;
  587. spin_lock_init(&dev->slock);
  588. for (i = 0; i < MAX_VID_CAP_CHANNEL_NUM - 1; ++i) {
  589. struct cx25821_channel *chan = &dev->channels[i];
  590. struct video_device *vdev = &chan->vdev;
  591. struct v4l2_ctrl_handler *hdl = &chan->hdl;
  592. struct vb2_queue *q;
  593. bool is_output = i > SRAM_CH08;
  594. if (i == SRAM_CH08) /* audio channel */
  595. continue;
  596. if (!is_output) {
  597. v4l2_ctrl_handler_init(hdl, 4);
  598. v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
  599. V4L2_CID_BRIGHTNESS, 0, 10000, 1, 6200);
  600. v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
  601. V4L2_CID_CONTRAST, 0, 10000, 1, 5000);
  602. v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
  603. V4L2_CID_SATURATION, 0, 10000, 1, 5000);
  604. v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
  605. V4L2_CID_HUE, 0, 10000, 1, 5000);
  606. if (hdl->error) {
  607. err = hdl->error;
  608. goto fail_unreg;
  609. }
  610. err = v4l2_ctrl_handler_setup(hdl);
  611. if (err)
  612. goto fail_unreg;
  613. } else {
  614. chan->out = &dev->vid_out_data[i - SRAM_CH09];
  615. chan->out->chan = chan;
  616. }
  617. chan->sram_channels = &cx25821_sram_channels[i];
  618. chan->width = 720;
  619. chan->field = V4L2_FIELD_INTERLACED;
  620. if (dev->tvnorm & V4L2_STD_625_50)
  621. chan->height = 576;
  622. else
  623. chan->height = 480;
  624. if (chan->pixel_formats == PIXEL_FRMT_411)
  625. chan->fmt = cx25821_format_by_fourcc(V4L2_PIX_FMT_Y41P);
  626. else
  627. chan->fmt = cx25821_format_by_fourcc(V4L2_PIX_FMT_YUYV);
  628. cx_write(chan->sram_channels->int_stat, 0xffffffff);
  629. INIT_LIST_HEAD(&chan->dma_vidq.active);
  630. q = &chan->vidq;
  631. q->type = is_output ? V4L2_BUF_TYPE_VIDEO_OUTPUT :
  632. V4L2_BUF_TYPE_VIDEO_CAPTURE;
  633. q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
  634. q->io_modes |= is_output ? VB2_WRITE : VB2_READ;
  635. q->gfp_flags = GFP_DMA32;
  636. q->min_buffers_needed = 2;
  637. q->drv_priv = chan;
  638. q->buf_struct_size = sizeof(struct cx25821_buffer);
  639. q->ops = &cx25821_video_qops;
  640. q->mem_ops = &vb2_dma_sg_memops;
  641. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  642. q->lock = &dev->lock;
  643. q->dev = &dev->pci->dev;
  644. if (!is_output) {
  645. err = vb2_queue_init(q);
  646. if (err < 0)
  647. goto fail_unreg;
  648. }
  649. /* register v4l devices */
  650. *vdev = is_output ? cx25821_video_out_device : cx25821_video_device;
  651. vdev->v4l2_dev = &dev->v4l2_dev;
  652. if (!is_output)
  653. vdev->ctrl_handler = hdl;
  654. else
  655. vdev->vfl_dir = VFL_DIR_TX;
  656. vdev->lock = &dev->lock;
  657. vdev->queue = q;
  658. snprintf(vdev->name, sizeof(vdev->name), "%s #%d", dev->name, i);
  659. video_set_drvdata(vdev, chan);
  660. err = video_register_device(vdev, VFL_TYPE_GRABBER,
  661. video_nr[dev->nr]);
  662. if (err < 0)
  663. goto fail_unreg;
  664. }
  665. /* set PCI interrupt */
  666. cx_set(PCI_INT_MSK, 0xff);
  667. return 0;
  668. fail_unreg:
  669. while (i >= 0)
  670. cx25821_video_unregister(dev, i--);
  671. return err;
  672. }