stk1160-v4l.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. /*
  2. * STK1160 driver
  3. *
  4. * Copyright (C) 2012 Ezequiel Garcia
  5. * <elezegarcia--a.t--gmail.com>
  6. *
  7. * Based on Easycap driver by R.M. Thomas
  8. * Copyright (C) 2010 R.M. Thomas
  9. * <rmthomas--a.t--sciolus.org>
  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. * GNU General Public License for more details.
  20. *
  21. */
  22. #include <linux/module.h>
  23. #include <linux/usb.h>
  24. #include <linux/mm.h>
  25. #include <linux/slab.h>
  26. #include <linux/videodev2.h>
  27. #include <media/v4l2-device.h>
  28. #include <media/v4l2-common.h>
  29. #include <media/v4l2-ioctl.h>
  30. #include <media/v4l2-fh.h>
  31. #include <media/v4l2-event.h>
  32. #include <media/videobuf2-vmalloc.h>
  33. #include <media/saa7115.h>
  34. #include "stk1160.h"
  35. #include "stk1160-reg.h"
  36. static bool keep_buffers;
  37. module_param(keep_buffers, bool, 0644);
  38. MODULE_PARM_DESC(keep_buffers, "don't release buffers upon stop streaming");
  39. /* supported video standards */
  40. static struct stk1160_fmt format[] = {
  41. {
  42. .name = "16 bpp YUY2, 4:2:2, packed",
  43. .fourcc = V4L2_PIX_FMT_UYVY,
  44. .depth = 16,
  45. }
  46. };
  47. static void stk1160_set_std(struct stk1160 *dev)
  48. {
  49. int i;
  50. static struct regval std525[] = {
  51. /* 720x480 */
  52. /* Frame start */
  53. {STK116_CFSPO_STX_L, 0x0000},
  54. {STK116_CFSPO_STX_H, 0x0000},
  55. {STK116_CFSPO_STY_L, 0x0003},
  56. {STK116_CFSPO_STY_H, 0x0000},
  57. /* Frame end */
  58. {STK116_CFEPO_ENX_L, 0x05a0},
  59. {STK116_CFEPO_ENX_H, 0x0005},
  60. {STK116_CFEPO_ENY_L, 0x00f3},
  61. {STK116_CFEPO_ENY_H, 0x0000},
  62. {0xffff, 0xffff}
  63. };
  64. static struct regval std625[] = {
  65. /* 720x576 */
  66. /* TODO: Each line of frame has some junk at the end */
  67. /* Frame start */
  68. {STK116_CFSPO, 0x0000},
  69. {STK116_CFSPO+1, 0x0000},
  70. {STK116_CFSPO+2, 0x0001},
  71. {STK116_CFSPO+3, 0x0000},
  72. /* Frame end */
  73. {STK116_CFEPO, 0x05a0},
  74. {STK116_CFEPO+1, 0x0005},
  75. {STK116_CFEPO+2, 0x0121},
  76. {STK116_CFEPO+3, 0x0001},
  77. {0xffff, 0xffff}
  78. };
  79. if (dev->norm & V4L2_STD_525_60) {
  80. stk1160_dbg("registers to NTSC like standard\n");
  81. for (i = 0; std525[i].reg != 0xffff; i++)
  82. stk1160_write_reg(dev, std525[i].reg, std525[i].val);
  83. } else {
  84. stk1160_dbg("registers to PAL like standard\n");
  85. for (i = 0; std625[i].reg != 0xffff; i++)
  86. stk1160_write_reg(dev, std625[i].reg, std625[i].val);
  87. }
  88. }
  89. /*
  90. * Set a new alternate setting.
  91. * Returns true is dev->max_pkt_size has changed, false otherwise.
  92. */
  93. static bool stk1160_set_alternate(struct stk1160 *dev)
  94. {
  95. int i, prev_alt = dev->alt;
  96. unsigned int min_pkt_size;
  97. bool new_pkt_size;
  98. /*
  99. * If we don't set right alternate,
  100. * then we will get a green screen with junk.
  101. */
  102. min_pkt_size = STK1160_MIN_PKT_SIZE;
  103. for (i = 0; i < dev->num_alt; i++) {
  104. /* stop when the selected alt setting offers enough bandwidth */
  105. if (dev->alt_max_pkt_size[i] >= min_pkt_size) {
  106. dev->alt = i;
  107. break;
  108. /*
  109. * otherwise make sure that we end up with the maximum bandwidth
  110. * because the min_pkt_size equation might be wrong...
  111. */
  112. } else if (dev->alt_max_pkt_size[i] >
  113. dev->alt_max_pkt_size[dev->alt])
  114. dev->alt = i;
  115. }
  116. stk1160_info("setting alternate %d\n", dev->alt);
  117. if (dev->alt != prev_alt) {
  118. stk1160_dbg("minimum isoc packet size: %u (alt=%d)\n",
  119. min_pkt_size, dev->alt);
  120. stk1160_dbg("setting alt %d with wMaxPacketSize=%u\n",
  121. dev->alt, dev->alt_max_pkt_size[dev->alt]);
  122. usb_set_interface(dev->udev, 0, dev->alt);
  123. }
  124. new_pkt_size = dev->max_pkt_size != dev->alt_max_pkt_size[dev->alt];
  125. dev->max_pkt_size = dev->alt_max_pkt_size[dev->alt];
  126. return new_pkt_size;
  127. }
  128. static int stk1160_start_streaming(struct stk1160 *dev)
  129. {
  130. bool new_pkt_size;
  131. int rc = 0;
  132. int i;
  133. /* Check device presence */
  134. if (!dev->udev)
  135. return -ENODEV;
  136. if (mutex_lock_interruptible(&dev->v4l_lock))
  137. return -ERESTARTSYS;
  138. /*
  139. * For some reason it is mandatory to set alternate *first*
  140. * and only *then* initialize isoc urbs.
  141. * Someone please explain me why ;)
  142. */
  143. new_pkt_size = stk1160_set_alternate(dev);
  144. /*
  145. * We (re)allocate isoc urbs if:
  146. * there is no allocated isoc urbs, OR
  147. * a new dev->max_pkt_size is detected
  148. */
  149. if (!dev->isoc_ctl.num_bufs || new_pkt_size) {
  150. rc = stk1160_alloc_isoc(dev);
  151. if (rc < 0)
  152. goto out_stop_hw;
  153. }
  154. /* submit urbs and enables IRQ */
  155. for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
  156. rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_KERNEL);
  157. if (rc) {
  158. stk1160_err("cannot submit urb[%d] (%d)\n", i, rc);
  159. goto out_uninit;
  160. }
  161. }
  162. /* Start saa711x */
  163. v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 1);
  164. /* Start stk1160 */
  165. stk1160_write_reg(dev, STK1160_DCTRL, 0xb3);
  166. stk1160_write_reg(dev, STK1160_DCTRL+3, 0x00);
  167. stk1160_dbg("streaming started\n");
  168. mutex_unlock(&dev->v4l_lock);
  169. return 0;
  170. out_uninit:
  171. stk1160_uninit_isoc(dev);
  172. out_stop_hw:
  173. usb_set_interface(dev->udev, 0, 0);
  174. stk1160_clear_queue(dev);
  175. mutex_unlock(&dev->v4l_lock);
  176. return rc;
  177. }
  178. /* Must be called with v4l_lock hold */
  179. static void stk1160_stop_hw(struct stk1160 *dev)
  180. {
  181. /* If the device is not physically present, there is nothing to do */
  182. if (!dev->udev)
  183. return;
  184. /* set alternate 0 */
  185. dev->alt = 0;
  186. stk1160_info("setting alternate %d\n", dev->alt);
  187. usb_set_interface(dev->udev, 0, 0);
  188. /* Stop stk1160 */
  189. stk1160_write_reg(dev, STK1160_DCTRL, 0x00);
  190. stk1160_write_reg(dev, STK1160_DCTRL+3, 0x00);
  191. /* Stop saa711x */
  192. v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 0);
  193. }
  194. static int stk1160_stop_streaming(struct stk1160 *dev)
  195. {
  196. if (mutex_lock_interruptible(&dev->v4l_lock))
  197. return -ERESTARTSYS;
  198. /*
  199. * Once URBs are cancelled, the URB complete handler
  200. * won't be running. This is required to safely release the
  201. * current buffer (dev->isoc_ctl.buf).
  202. */
  203. stk1160_cancel_isoc(dev);
  204. /*
  205. * It is possible to keep buffers around using a module parameter.
  206. * This is intended to avoid memory fragmentation.
  207. */
  208. if (!keep_buffers)
  209. stk1160_free_isoc(dev);
  210. stk1160_stop_hw(dev);
  211. stk1160_clear_queue(dev);
  212. stk1160_dbg("streaming stopped\n");
  213. mutex_unlock(&dev->v4l_lock);
  214. return 0;
  215. }
  216. static struct v4l2_file_operations stk1160_fops = {
  217. .owner = THIS_MODULE,
  218. .open = v4l2_fh_open,
  219. .release = vb2_fop_release,
  220. .read = vb2_fop_read,
  221. .poll = vb2_fop_poll,
  222. .mmap = vb2_fop_mmap,
  223. .unlocked_ioctl = video_ioctl2,
  224. };
  225. /*
  226. * vidioc ioctls
  227. */
  228. static int vidioc_querycap(struct file *file,
  229. void *priv, struct v4l2_capability *cap)
  230. {
  231. struct stk1160 *dev = video_drvdata(file);
  232. strcpy(cap->driver, "stk1160");
  233. strcpy(cap->card, "stk1160");
  234. usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
  235. cap->device_caps =
  236. V4L2_CAP_VIDEO_CAPTURE |
  237. V4L2_CAP_STREAMING |
  238. V4L2_CAP_READWRITE;
  239. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  240. return 0;
  241. }
  242. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  243. struct v4l2_fmtdesc *f)
  244. {
  245. if (f->index != 0)
  246. return -EINVAL;
  247. strlcpy(f->description, format[f->index].name, sizeof(f->description));
  248. f->pixelformat = format[f->index].fourcc;
  249. return 0;
  250. }
  251. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  252. struct v4l2_format *f)
  253. {
  254. struct stk1160 *dev = video_drvdata(file);
  255. f->fmt.pix.width = dev->width;
  256. f->fmt.pix.height = dev->height;
  257. f->fmt.pix.field = V4L2_FIELD_INTERLACED;
  258. f->fmt.pix.pixelformat = dev->fmt->fourcc;
  259. f->fmt.pix.bytesperline = dev->width * 2;
  260. f->fmt.pix.sizeimage = dev->height * f->fmt.pix.bytesperline;
  261. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  262. return 0;
  263. }
  264. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  265. struct v4l2_format *f)
  266. {
  267. struct stk1160 *dev = video_drvdata(file);
  268. /*
  269. * User can't choose size at his own will,
  270. * so we just return him the current size chosen
  271. * at standard selection.
  272. * TODO: Implement frame scaling?
  273. */
  274. f->fmt.pix.pixelformat = dev->fmt->fourcc;
  275. f->fmt.pix.width = dev->width;
  276. f->fmt.pix.height = dev->height;
  277. f->fmt.pix.field = V4L2_FIELD_INTERLACED;
  278. f->fmt.pix.bytesperline = dev->width * 2;
  279. f->fmt.pix.sizeimage = dev->height * f->fmt.pix.bytesperline;
  280. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  281. return 0;
  282. }
  283. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  284. struct v4l2_format *f)
  285. {
  286. struct stk1160 *dev = video_drvdata(file);
  287. struct vb2_queue *q = &dev->vb_vidq;
  288. if (vb2_is_busy(q))
  289. return -EBUSY;
  290. vidioc_try_fmt_vid_cap(file, priv, f);
  291. /* We don't support any format changes */
  292. return 0;
  293. }
  294. static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *norm)
  295. {
  296. struct stk1160 *dev = video_drvdata(file);
  297. v4l2_device_call_all(&dev->v4l2_dev, 0, video, querystd, norm);
  298. return 0;
  299. }
  300. static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
  301. {
  302. struct stk1160 *dev = video_drvdata(file);
  303. *norm = dev->norm;
  304. return 0;
  305. }
  306. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm)
  307. {
  308. struct stk1160 *dev = video_drvdata(file);
  309. struct vb2_queue *q = &dev->vb_vidq;
  310. if (dev->norm == norm)
  311. return 0;
  312. if (vb2_is_busy(q))
  313. return -EBUSY;
  314. /* Check device presence */
  315. if (!dev->udev)
  316. return -ENODEV;
  317. /* We need to set this now, before we call stk1160_set_std */
  318. dev->norm = norm;
  319. /* This is taken from saa7115 video decoder */
  320. if (dev->norm & V4L2_STD_525_60) {
  321. dev->width = 720;
  322. dev->height = 480;
  323. } else if (dev->norm & V4L2_STD_625_50) {
  324. dev->width = 720;
  325. dev->height = 576;
  326. } else {
  327. stk1160_err("invalid standard\n");
  328. return -EINVAL;
  329. }
  330. stk1160_set_std(dev);
  331. v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std,
  332. dev->norm);
  333. return 0;
  334. }
  335. static int vidioc_enum_input(struct file *file, void *priv,
  336. struct v4l2_input *i)
  337. {
  338. struct stk1160 *dev = video_drvdata(file);
  339. if (i->index > STK1160_MAX_INPUT)
  340. return -EINVAL;
  341. /* S-Video special handling */
  342. if (i->index == STK1160_SVIDEO_INPUT)
  343. sprintf(i->name, "S-Video");
  344. else
  345. sprintf(i->name, "Composite%d", i->index);
  346. i->type = V4L2_INPUT_TYPE_CAMERA;
  347. i->std = dev->vdev.tvnorms;
  348. return 0;
  349. }
  350. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  351. {
  352. struct stk1160 *dev = video_drvdata(file);
  353. *i = dev->ctl_input;
  354. return 0;
  355. }
  356. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  357. {
  358. struct stk1160 *dev = video_drvdata(file);
  359. if (i > STK1160_MAX_INPUT)
  360. return -EINVAL;
  361. dev->ctl_input = i;
  362. stk1160_select_input(dev);
  363. return 0;
  364. }
  365. #ifdef CONFIG_VIDEO_ADV_DEBUG
  366. static int vidioc_g_register(struct file *file, void *priv,
  367. struct v4l2_dbg_register *reg)
  368. {
  369. struct stk1160 *dev = video_drvdata(file);
  370. int rc;
  371. u8 val;
  372. /* Match host */
  373. rc = stk1160_read_reg(dev, reg->reg, &val);
  374. reg->val = val;
  375. reg->size = 1;
  376. return rc;
  377. }
  378. static int vidioc_s_register(struct file *file, void *priv,
  379. const struct v4l2_dbg_register *reg)
  380. {
  381. struct stk1160 *dev = video_drvdata(file);
  382. /* Match host */
  383. return stk1160_write_reg(dev, reg->reg, reg->val);
  384. }
  385. #endif
  386. static const struct v4l2_ioctl_ops stk1160_ioctl_ops = {
  387. .vidioc_querycap = vidioc_querycap,
  388. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  389. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  390. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  391. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  392. .vidioc_querystd = vidioc_querystd,
  393. .vidioc_g_std = vidioc_g_std,
  394. .vidioc_s_std = vidioc_s_std,
  395. .vidioc_enum_input = vidioc_enum_input,
  396. .vidioc_g_input = vidioc_g_input,
  397. .vidioc_s_input = vidioc_s_input,
  398. /* vb2 takes care of these */
  399. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  400. .vidioc_querybuf = vb2_ioctl_querybuf,
  401. .vidioc_qbuf = vb2_ioctl_qbuf,
  402. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  403. .vidioc_streamon = vb2_ioctl_streamon,
  404. .vidioc_streamoff = vb2_ioctl_streamoff,
  405. .vidioc_log_status = v4l2_ctrl_log_status,
  406. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  407. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  408. #ifdef CONFIG_VIDEO_ADV_DEBUG
  409. .vidioc_g_register = vidioc_g_register,
  410. .vidioc_s_register = vidioc_s_register,
  411. #endif
  412. };
  413. /********************************************************************/
  414. /*
  415. * Videobuf2 operations
  416. */
  417. static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *v4l_fmt,
  418. unsigned int *nbuffers, unsigned int *nplanes,
  419. unsigned int sizes[], void *alloc_ctxs[])
  420. {
  421. struct stk1160 *dev = vb2_get_drv_priv(vq);
  422. unsigned long size;
  423. size = dev->width * dev->height * 2;
  424. /*
  425. * Here we can change the number of buffers being requested.
  426. * So, we set a minimum and a maximum like this:
  427. */
  428. *nbuffers = clamp_t(unsigned int, *nbuffers,
  429. STK1160_MIN_VIDEO_BUFFERS, STK1160_MAX_VIDEO_BUFFERS);
  430. /* This means a packed colorformat */
  431. *nplanes = 1;
  432. sizes[0] = size;
  433. stk1160_info("%s: buffer count %d, each %ld bytes\n",
  434. __func__, *nbuffers, size);
  435. return 0;
  436. }
  437. static void buffer_queue(struct vb2_buffer *vb)
  438. {
  439. unsigned long flags;
  440. struct stk1160 *dev = vb2_get_drv_priv(vb->vb2_queue);
  441. struct stk1160_buffer *buf =
  442. container_of(vb, struct stk1160_buffer, vb);
  443. spin_lock_irqsave(&dev->buf_lock, flags);
  444. if (!dev->udev) {
  445. /*
  446. * If the device is disconnected return the buffer to userspace
  447. * directly. The next QBUF call will fail with -ENODEV.
  448. */
  449. vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
  450. } else {
  451. buf->mem = vb2_plane_vaddr(vb, 0);
  452. buf->length = vb2_plane_size(vb, 0);
  453. buf->bytesused = 0;
  454. buf->pos = 0;
  455. /*
  456. * If buffer length is less from expected then we return
  457. * the buffer to userspace directly.
  458. */
  459. if (buf->length < dev->width * dev->height * 2)
  460. vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
  461. else
  462. list_add_tail(&buf->list, &dev->avail_bufs);
  463. }
  464. spin_unlock_irqrestore(&dev->buf_lock, flags);
  465. }
  466. static int start_streaming(struct vb2_queue *vq, unsigned int count)
  467. {
  468. struct stk1160 *dev = vb2_get_drv_priv(vq);
  469. return stk1160_start_streaming(dev);
  470. }
  471. /* abort streaming and wait for last buffer */
  472. static void stop_streaming(struct vb2_queue *vq)
  473. {
  474. struct stk1160 *dev = vb2_get_drv_priv(vq);
  475. stk1160_stop_streaming(dev);
  476. }
  477. static struct vb2_ops stk1160_video_qops = {
  478. .queue_setup = queue_setup,
  479. .buf_queue = buffer_queue,
  480. .start_streaming = start_streaming,
  481. .stop_streaming = stop_streaming,
  482. .wait_prepare = vb2_ops_wait_prepare,
  483. .wait_finish = vb2_ops_wait_finish,
  484. };
  485. static struct video_device v4l_template = {
  486. .name = "stk1160",
  487. .tvnorms = V4L2_STD_525_60 | V4L2_STD_625_50,
  488. .fops = &stk1160_fops,
  489. .ioctl_ops = &stk1160_ioctl_ops,
  490. .release = video_device_release_empty,
  491. };
  492. /********************************************************************/
  493. /* Must be called with both v4l_lock and vb_queue_lock hold */
  494. void stk1160_clear_queue(struct stk1160 *dev)
  495. {
  496. struct stk1160_buffer *buf;
  497. unsigned long flags;
  498. /* Release all active buffers */
  499. spin_lock_irqsave(&dev->buf_lock, flags);
  500. while (!list_empty(&dev->avail_bufs)) {
  501. buf = list_first_entry(&dev->avail_bufs,
  502. struct stk1160_buffer, list);
  503. list_del(&buf->list);
  504. vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
  505. stk1160_info("buffer [%p/%d] aborted\n",
  506. buf, buf->vb.v4l2_buf.index);
  507. }
  508. /* It's important to release the current buffer */
  509. if (dev->isoc_ctl.buf) {
  510. buf = dev->isoc_ctl.buf;
  511. dev->isoc_ctl.buf = NULL;
  512. vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
  513. stk1160_info("buffer [%p/%d] aborted\n",
  514. buf, buf->vb.v4l2_buf.index);
  515. }
  516. spin_unlock_irqrestore(&dev->buf_lock, flags);
  517. }
  518. int stk1160_vb2_setup(struct stk1160 *dev)
  519. {
  520. int rc;
  521. struct vb2_queue *q;
  522. q = &dev->vb_vidq;
  523. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  524. q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR;
  525. q->drv_priv = dev;
  526. q->buf_struct_size = sizeof(struct stk1160_buffer);
  527. q->ops = &stk1160_video_qops;
  528. q->mem_ops = &vb2_vmalloc_memops;
  529. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  530. rc = vb2_queue_init(q);
  531. if (rc < 0)
  532. return rc;
  533. /* initialize video dma queue */
  534. INIT_LIST_HEAD(&dev->avail_bufs);
  535. return 0;
  536. }
  537. int stk1160_video_register(struct stk1160 *dev)
  538. {
  539. int rc;
  540. /* Initialize video_device with a template structure */
  541. dev->vdev = v4l_template;
  542. dev->vdev.queue = &dev->vb_vidq;
  543. /*
  544. * Provide mutexes for v4l2 core and for videobuf2 queue.
  545. * It will be used to protect *only* v4l2 ioctls.
  546. */
  547. dev->vdev.lock = &dev->v4l_lock;
  548. dev->vdev.queue->lock = &dev->vb_queue_lock;
  549. /* This will be used to set video_device parent */
  550. dev->vdev.v4l2_dev = &dev->v4l2_dev;
  551. /* NTSC is default */
  552. dev->norm = V4L2_STD_NTSC_M;
  553. dev->width = 720;
  554. dev->height = 480;
  555. /* set default format */
  556. dev->fmt = &format[0];
  557. stk1160_set_std(dev);
  558. v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std,
  559. dev->norm);
  560. video_set_drvdata(&dev->vdev, dev);
  561. rc = video_register_device(&dev->vdev, VFL_TYPE_GRABBER, -1);
  562. if (rc < 0) {
  563. stk1160_err("video_register_device failed (%d)\n", rc);
  564. return rc;
  565. }
  566. v4l2_info(&dev->v4l2_dev, "V4L2 device registered as %s\n",
  567. video_device_node_name(&dev->vdev));
  568. return 0;
  569. }