stk1160-v4l.c 18 KB

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