stk1160-v4l.c 17 KB

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