stk1160-v4l.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  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. /*
  202. * Once URBs are cancelled, the URB complete handler
  203. * won't be running. This is required to safely release the
  204. * current buffer (dev->isoc_ctl.buf).
  205. */
  206. stk1160_cancel_isoc(dev);
  207. /*
  208. * It is possible to keep buffers around using a module parameter.
  209. * This is intended to avoid memory fragmentation.
  210. */
  211. if (!keep_buffers)
  212. stk1160_free_isoc(dev);
  213. stk1160_stop_hw(dev);
  214. stk1160_clear_queue(dev);
  215. stk1160_dbg("streaming stopped\n");
  216. mutex_unlock(&dev->v4l_lock);
  217. return 0;
  218. }
  219. static struct v4l2_file_operations stk1160_fops = {
  220. .owner = THIS_MODULE,
  221. .open = v4l2_fh_open,
  222. .release = vb2_fop_release,
  223. .read = vb2_fop_read,
  224. .poll = vb2_fop_poll,
  225. .mmap = vb2_fop_mmap,
  226. .unlocked_ioctl = video_ioctl2,
  227. };
  228. /*
  229. * vidioc ioctls
  230. */
  231. static int vidioc_querycap(struct file *file,
  232. void *priv, struct v4l2_capability *cap)
  233. {
  234. struct stk1160 *dev = video_drvdata(file);
  235. strcpy(cap->driver, "stk1160");
  236. strcpy(cap->card, "stk1160");
  237. usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
  238. cap->device_caps =
  239. V4L2_CAP_VIDEO_CAPTURE |
  240. V4L2_CAP_STREAMING |
  241. V4L2_CAP_READWRITE;
  242. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  243. return 0;
  244. }
  245. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  246. struct v4l2_fmtdesc *f)
  247. {
  248. if (f->index != 0)
  249. return -EINVAL;
  250. strlcpy(f->description, format[f->index].name, sizeof(f->description));
  251. f->pixelformat = format[f->index].fourcc;
  252. return 0;
  253. }
  254. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  255. struct v4l2_format *f)
  256. {
  257. struct stk1160 *dev = video_drvdata(file);
  258. f->fmt.pix.width = dev->width;
  259. f->fmt.pix.height = dev->height;
  260. f->fmt.pix.field = V4L2_FIELD_INTERLACED;
  261. f->fmt.pix.pixelformat = dev->fmt->fourcc;
  262. f->fmt.pix.bytesperline = dev->width * 2;
  263. f->fmt.pix.sizeimage = dev->height * f->fmt.pix.bytesperline;
  264. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  265. return 0;
  266. }
  267. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  268. struct v4l2_format *f)
  269. {
  270. struct stk1160 *dev = video_drvdata(file);
  271. /*
  272. * User can't choose size at his own will,
  273. * so we just return him the current size chosen
  274. * at standard selection.
  275. * TODO: Implement frame scaling?
  276. */
  277. f->fmt.pix.pixelformat = dev->fmt->fourcc;
  278. f->fmt.pix.width = dev->width;
  279. f->fmt.pix.height = dev->height;
  280. f->fmt.pix.field = V4L2_FIELD_INTERLACED;
  281. f->fmt.pix.bytesperline = dev->width * 2;
  282. f->fmt.pix.sizeimage = dev->height * f->fmt.pix.bytesperline;
  283. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  284. return 0;
  285. }
  286. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  287. struct v4l2_format *f)
  288. {
  289. struct stk1160 *dev = video_drvdata(file);
  290. struct vb2_queue *q = &dev->vb_vidq;
  291. if (vb2_is_busy(q))
  292. return -EBUSY;
  293. vidioc_try_fmt_vid_cap(file, priv, f);
  294. /* We don't support any format changes */
  295. return 0;
  296. }
  297. static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *norm)
  298. {
  299. struct stk1160 *dev = video_drvdata(file);
  300. v4l2_device_call_all(&dev->v4l2_dev, 0, video, querystd, norm);
  301. return 0;
  302. }
  303. static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
  304. {
  305. struct stk1160 *dev = video_drvdata(file);
  306. *norm = dev->norm;
  307. return 0;
  308. }
  309. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm)
  310. {
  311. struct stk1160 *dev = video_drvdata(file);
  312. struct vb2_queue *q = &dev->vb_vidq;
  313. if (dev->norm == norm)
  314. return 0;
  315. if (vb2_is_busy(q))
  316. return -EBUSY;
  317. /* Check device presence */
  318. if (!dev->udev)
  319. return -ENODEV;
  320. /* We need to set this now, before we call stk1160_set_std */
  321. dev->norm = norm;
  322. /* This is taken from saa7115 video decoder */
  323. if (dev->norm & V4L2_STD_525_60) {
  324. dev->width = 720;
  325. dev->height = 480;
  326. } else if (dev->norm & V4L2_STD_625_50) {
  327. dev->width = 720;
  328. dev->height = 576;
  329. } else {
  330. stk1160_err("invalid standard\n");
  331. return -EINVAL;
  332. }
  333. stk1160_set_std(dev);
  334. v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std,
  335. dev->norm);
  336. return 0;
  337. }
  338. static int vidioc_enum_input(struct file *file, void *priv,
  339. struct v4l2_input *i)
  340. {
  341. struct stk1160 *dev = video_drvdata(file);
  342. if (i->index > STK1160_MAX_INPUT)
  343. return -EINVAL;
  344. /* S-Video special handling */
  345. if (i->index == STK1160_SVIDEO_INPUT)
  346. sprintf(i->name, "S-Video");
  347. else
  348. sprintf(i->name, "Composite%d", i->index);
  349. i->type = V4L2_INPUT_TYPE_CAMERA;
  350. i->std = dev->vdev.tvnorms;
  351. return 0;
  352. }
  353. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  354. {
  355. struct stk1160 *dev = video_drvdata(file);
  356. *i = dev->ctl_input;
  357. return 0;
  358. }
  359. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  360. {
  361. struct stk1160 *dev = video_drvdata(file);
  362. if (i > STK1160_MAX_INPUT)
  363. return -EINVAL;
  364. dev->ctl_input = i;
  365. stk1160_select_input(dev);
  366. return 0;
  367. }
  368. #ifdef CONFIG_VIDEO_ADV_DEBUG
  369. static int vidioc_g_register(struct file *file, void *priv,
  370. struct v4l2_dbg_register *reg)
  371. {
  372. struct stk1160 *dev = video_drvdata(file);
  373. int rc;
  374. u8 val;
  375. /* Match host */
  376. rc = stk1160_read_reg(dev, reg->reg, &val);
  377. reg->val = val;
  378. reg->size = 1;
  379. return rc;
  380. }
  381. static int vidioc_s_register(struct file *file, void *priv,
  382. const struct v4l2_dbg_register *reg)
  383. {
  384. struct stk1160 *dev = video_drvdata(file);
  385. /* Match host */
  386. return stk1160_write_reg(dev, reg->reg, cpu_to_le16(reg->val));
  387. }
  388. #endif
  389. static const struct v4l2_ioctl_ops stk1160_ioctl_ops = {
  390. .vidioc_querycap = vidioc_querycap,
  391. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  392. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  393. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  394. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  395. .vidioc_querystd = vidioc_querystd,
  396. .vidioc_g_std = vidioc_g_std,
  397. .vidioc_s_std = vidioc_s_std,
  398. .vidioc_enum_input = vidioc_enum_input,
  399. .vidioc_g_input = vidioc_g_input,
  400. .vidioc_s_input = vidioc_s_input,
  401. /* vb2 takes care of these */
  402. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  403. .vidioc_querybuf = vb2_ioctl_querybuf,
  404. .vidioc_qbuf = vb2_ioctl_qbuf,
  405. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  406. .vidioc_streamon = vb2_ioctl_streamon,
  407. .vidioc_streamoff = vb2_ioctl_streamoff,
  408. .vidioc_log_status = v4l2_ctrl_log_status,
  409. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  410. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  411. #ifdef CONFIG_VIDEO_ADV_DEBUG
  412. .vidioc_g_register = vidioc_g_register,
  413. .vidioc_s_register = vidioc_s_register,
  414. #endif
  415. };
  416. /********************************************************************/
  417. /*
  418. * Videobuf2 operations
  419. */
  420. static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *v4l_fmt,
  421. unsigned int *nbuffers, unsigned int *nplanes,
  422. unsigned int sizes[], void *alloc_ctxs[])
  423. {
  424. struct stk1160 *dev = vb2_get_drv_priv(vq);
  425. unsigned long size;
  426. size = dev->width * dev->height * 2;
  427. /*
  428. * Here we can change the number of buffers being requested.
  429. * So, we set a minimum and a maximum like this:
  430. */
  431. *nbuffers = clamp_t(unsigned int, *nbuffers,
  432. STK1160_MIN_VIDEO_BUFFERS, STK1160_MAX_VIDEO_BUFFERS);
  433. /* This means a packed colorformat */
  434. *nplanes = 1;
  435. sizes[0] = size;
  436. stk1160_info("%s: buffer count %d, each %ld bytes\n",
  437. __func__, *nbuffers, size);
  438. return 0;
  439. }
  440. static void buffer_queue(struct vb2_buffer *vb)
  441. {
  442. unsigned long flags;
  443. struct stk1160 *dev = vb2_get_drv_priv(vb->vb2_queue);
  444. struct stk1160_buffer *buf =
  445. container_of(vb, struct stk1160_buffer, vb);
  446. spin_lock_irqsave(&dev->buf_lock, flags);
  447. if (!dev->udev) {
  448. /*
  449. * If the device is disconnected return the buffer to userspace
  450. * directly. The next QBUF call will fail with -ENODEV.
  451. */
  452. vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
  453. } else {
  454. buf->mem = vb2_plane_vaddr(vb, 0);
  455. buf->length = vb2_plane_size(vb, 0);
  456. buf->bytesused = 0;
  457. buf->pos = 0;
  458. /*
  459. * If buffer length is less from expected then we return
  460. * the buffer to userspace directly.
  461. */
  462. if (buf->length < dev->width * dev->height * 2)
  463. vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
  464. else
  465. list_add_tail(&buf->list, &dev->avail_bufs);
  466. }
  467. spin_unlock_irqrestore(&dev->buf_lock, flags);
  468. }
  469. static int start_streaming(struct vb2_queue *vq, unsigned int count)
  470. {
  471. struct stk1160 *dev = vb2_get_drv_priv(vq);
  472. return stk1160_start_streaming(dev);
  473. }
  474. /* abort streaming and wait for last buffer */
  475. static void stop_streaming(struct vb2_queue *vq)
  476. {
  477. struct stk1160 *dev = vb2_get_drv_priv(vq);
  478. stk1160_stop_streaming(dev);
  479. }
  480. static struct vb2_ops stk1160_video_qops = {
  481. .queue_setup = queue_setup,
  482. .buf_queue = buffer_queue,
  483. .start_streaming = start_streaming,
  484. .stop_streaming = stop_streaming,
  485. .wait_prepare = vb2_ops_wait_prepare,
  486. .wait_finish = vb2_ops_wait_finish,
  487. };
  488. static struct video_device v4l_template = {
  489. .name = "stk1160",
  490. .tvnorms = V4L2_STD_525_60 | V4L2_STD_625_50,
  491. .fops = &stk1160_fops,
  492. .ioctl_ops = &stk1160_ioctl_ops,
  493. .release = video_device_release_empty,
  494. };
  495. /********************************************************************/
  496. /* Must be called with both v4l_lock and vb_queue_lock hold */
  497. void stk1160_clear_queue(struct stk1160 *dev)
  498. {
  499. struct stk1160_buffer *buf;
  500. unsigned long flags;
  501. /* Release all active buffers */
  502. spin_lock_irqsave(&dev->buf_lock, flags);
  503. while (!list_empty(&dev->avail_bufs)) {
  504. buf = list_first_entry(&dev->avail_bufs,
  505. struct stk1160_buffer, list);
  506. list_del(&buf->list);
  507. vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
  508. stk1160_info("buffer [%p/%d] aborted\n",
  509. buf, buf->vb.v4l2_buf.index);
  510. }
  511. /* It's important to release the current buffer */
  512. if (dev->isoc_ctl.buf) {
  513. buf = dev->isoc_ctl.buf;
  514. dev->isoc_ctl.buf = NULL;
  515. vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
  516. stk1160_info("buffer [%p/%d] aborted\n",
  517. buf, buf->vb.v4l2_buf.index);
  518. }
  519. spin_unlock_irqrestore(&dev->buf_lock, flags);
  520. }
  521. int stk1160_vb2_setup(struct stk1160 *dev)
  522. {
  523. int rc;
  524. struct vb2_queue *q;
  525. q = &dev->vb_vidq;
  526. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  527. q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR;
  528. q->drv_priv = dev;
  529. q->buf_struct_size = sizeof(struct stk1160_buffer);
  530. q->ops = &stk1160_video_qops;
  531. q->mem_ops = &vb2_vmalloc_memops;
  532. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  533. rc = vb2_queue_init(q);
  534. if (rc < 0)
  535. return rc;
  536. /* initialize video dma queue */
  537. INIT_LIST_HEAD(&dev->avail_bufs);
  538. return 0;
  539. }
  540. int stk1160_video_register(struct stk1160 *dev)
  541. {
  542. int rc;
  543. /* Initialize video_device with a template structure */
  544. dev->vdev = v4l_template;
  545. dev->vdev.debug = vidioc_debug;
  546. dev->vdev.queue = &dev->vb_vidq;
  547. /*
  548. * Provide mutexes for v4l2 core and for videobuf2 queue.
  549. * It will be used to protect *only* v4l2 ioctls.
  550. */
  551. dev->vdev.lock = &dev->v4l_lock;
  552. dev->vdev.queue->lock = &dev->vb_queue_lock;
  553. /* This will be used to set video_device parent */
  554. dev->vdev.v4l2_dev = &dev->v4l2_dev;
  555. /* NTSC is default */
  556. dev->norm = V4L2_STD_NTSC_M;
  557. dev->width = 720;
  558. dev->height = 480;
  559. /* set default format */
  560. dev->fmt = &format[0];
  561. stk1160_set_std(dev);
  562. v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std,
  563. dev->norm);
  564. video_set_drvdata(&dev->vdev, dev);
  565. rc = video_register_device(&dev->vdev, VFL_TYPE_GRABBER, -1);
  566. if (rc < 0) {
  567. stk1160_err("video_register_device failed (%d)\n", rc);
  568. return rc;
  569. }
  570. v4l2_info(&dev->v4l2_dev, "V4L2 device registered as %s\n",
  571. video_device_node_name(&dev->vdev));
  572. return 0;
  573. }