stk1160-v4l.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  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. /* S-Video special handling */
  339. if (i->index == STK1160_SVIDEO_INPUT)
  340. sprintf(i->name, "S-Video");
  341. else
  342. sprintf(i->name, "Composite%d", i->index);
  343. i->type = V4L2_INPUT_TYPE_CAMERA;
  344. i->std = dev->vdev.tvnorms;
  345. return 0;
  346. }
  347. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  348. {
  349. struct stk1160 *dev = video_drvdata(file);
  350. *i = dev->ctl_input;
  351. return 0;
  352. }
  353. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  354. {
  355. struct stk1160 *dev = video_drvdata(file);
  356. if (vb2_is_busy(&dev->vb_vidq))
  357. return -EBUSY;
  358. if (i > STK1160_MAX_INPUT)
  359. return -EINVAL;
  360. dev->ctl_input = i;
  361. stk1160_select_input(dev);
  362. return 0;
  363. }
  364. static int vidioc_g_chip_ident(struct file *file, void *priv,
  365. struct v4l2_dbg_chip_ident *chip)
  366. {
  367. switch (chip->match.type) {
  368. case V4L2_CHIP_MATCH_HOST:
  369. chip->ident = V4L2_IDENT_NONE;
  370. chip->revision = 0;
  371. return 0;
  372. default:
  373. return -EINVAL;
  374. }
  375. }
  376. #ifdef CONFIG_VIDEO_ADV_DEBUG
  377. static int vidioc_g_register(struct file *file, void *priv,
  378. struct v4l2_dbg_register *reg)
  379. {
  380. struct stk1160 *dev = video_drvdata(file);
  381. int rc;
  382. u8 val;
  383. switch (reg->match.type) {
  384. case V4L2_CHIP_MATCH_AC97:
  385. /* TODO: Support me please :-( */
  386. return -EINVAL;
  387. case V4L2_CHIP_MATCH_I2C_DRIVER:
  388. v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
  389. return 0;
  390. case V4L2_CHIP_MATCH_I2C_ADDR:
  391. /* TODO: is this correct? */
  392. v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
  393. return 0;
  394. default:
  395. if (!v4l2_chip_match_host(&reg->match))
  396. return -EINVAL;
  397. }
  398. /* Match host */
  399. rc = stk1160_read_reg(dev, reg->reg, &val);
  400. reg->val = val;
  401. reg->size = 1;
  402. return rc;
  403. }
  404. static int vidioc_s_register(struct file *file, void *priv,
  405. struct v4l2_dbg_register *reg)
  406. {
  407. struct stk1160 *dev = video_drvdata(file);
  408. switch (reg->match.type) {
  409. case V4L2_CHIP_MATCH_AC97:
  410. return -EINVAL;
  411. case V4L2_CHIP_MATCH_I2C_DRIVER:
  412. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
  413. return 0;
  414. case V4L2_CHIP_MATCH_I2C_ADDR:
  415. /* TODO: is this correct? */
  416. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
  417. return 0;
  418. default:
  419. if (!v4l2_chip_match_host(&reg->match))
  420. return -EINVAL;
  421. }
  422. /* Match host */
  423. return stk1160_write_reg(dev, reg->reg, cpu_to_le16(reg->val));
  424. }
  425. #endif
  426. static const struct v4l2_ioctl_ops stk1160_ioctl_ops = {
  427. .vidioc_querycap = vidioc_querycap,
  428. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  429. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  430. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  431. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  432. .vidioc_querystd = vidioc_querystd,
  433. .vidioc_g_std = vidioc_g_std,
  434. .vidioc_s_std = vidioc_s_std,
  435. .vidioc_enum_input = vidioc_enum_input,
  436. .vidioc_g_input = vidioc_g_input,
  437. .vidioc_s_input = vidioc_s_input,
  438. /* vb2 takes care of these */
  439. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  440. .vidioc_querybuf = vb2_ioctl_querybuf,
  441. .vidioc_qbuf = vb2_ioctl_qbuf,
  442. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  443. .vidioc_streamon = vb2_ioctl_streamon,
  444. .vidioc_streamoff = vb2_ioctl_streamoff,
  445. .vidioc_log_status = v4l2_ctrl_log_status,
  446. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  447. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  448. .vidioc_g_chip_ident = vidioc_g_chip_ident,
  449. #ifdef CONFIG_VIDEO_ADV_DEBUG
  450. .vidioc_g_register = vidioc_g_register,
  451. .vidioc_s_register = vidioc_s_register,
  452. #endif
  453. };
  454. /********************************************************************/
  455. /*
  456. * Videobuf2 operations
  457. */
  458. static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *v4l_fmt,
  459. unsigned int *nbuffers, unsigned int *nplanes,
  460. unsigned int sizes[], void *alloc_ctxs[])
  461. {
  462. struct stk1160 *dev = vb2_get_drv_priv(vq);
  463. unsigned long size;
  464. size = dev->width * dev->height * 2;
  465. /*
  466. * Here we can change the number of buffers being requested.
  467. * So, we set a minimum and a maximum like this:
  468. */
  469. *nbuffers = clamp_t(unsigned int, *nbuffers,
  470. STK1160_MIN_VIDEO_BUFFERS, STK1160_MAX_VIDEO_BUFFERS);
  471. /* This means a packed colorformat */
  472. *nplanes = 1;
  473. sizes[0] = size;
  474. stk1160_info("%s: buffer count %d, each %ld bytes\n",
  475. __func__, *nbuffers, size);
  476. return 0;
  477. }
  478. static void buffer_queue(struct vb2_buffer *vb)
  479. {
  480. unsigned long flags;
  481. struct stk1160 *dev = vb2_get_drv_priv(vb->vb2_queue);
  482. struct stk1160_buffer *buf =
  483. container_of(vb, struct stk1160_buffer, vb);
  484. spin_lock_irqsave(&dev->buf_lock, flags);
  485. if (!dev->udev) {
  486. /*
  487. * If the device is disconnected return the buffer to userspace
  488. * directly. The next QBUF call will fail with -ENODEV.
  489. */
  490. vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
  491. } else {
  492. buf->mem = vb2_plane_vaddr(vb, 0);
  493. buf->length = vb2_plane_size(vb, 0);
  494. buf->bytesused = 0;
  495. buf->pos = 0;
  496. /*
  497. * If buffer length is less from expected then we return
  498. * the buffer to userspace directly.
  499. */
  500. if (buf->length < dev->width * dev->height * 2)
  501. vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
  502. else
  503. list_add_tail(&buf->list, &dev->avail_bufs);
  504. }
  505. spin_unlock_irqrestore(&dev->buf_lock, flags);
  506. }
  507. static int start_streaming(struct vb2_queue *vq, unsigned int count)
  508. {
  509. struct stk1160 *dev = vb2_get_drv_priv(vq);
  510. return stk1160_start_streaming(dev);
  511. }
  512. /* abort streaming and wait for last buffer */
  513. static int stop_streaming(struct vb2_queue *vq)
  514. {
  515. struct stk1160 *dev = vb2_get_drv_priv(vq);
  516. return stk1160_stop_streaming(dev);
  517. }
  518. static struct vb2_ops stk1160_video_qops = {
  519. .queue_setup = queue_setup,
  520. .buf_queue = buffer_queue,
  521. .start_streaming = start_streaming,
  522. .stop_streaming = stop_streaming,
  523. .wait_prepare = vb2_ops_wait_prepare,
  524. .wait_finish = vb2_ops_wait_finish,
  525. };
  526. static struct video_device v4l_template = {
  527. .name = "stk1160",
  528. .tvnorms = V4L2_STD_525_60 | V4L2_STD_625_50,
  529. .fops = &stk1160_fops,
  530. .ioctl_ops = &stk1160_ioctl_ops,
  531. .release = video_device_release_empty,
  532. };
  533. /********************************************************************/
  534. /* Must be called with both v4l_lock and vb_queue_lock hold */
  535. void stk1160_clear_queue(struct stk1160 *dev)
  536. {
  537. struct stk1160_buffer *buf;
  538. unsigned long flags;
  539. /* Release all active buffers */
  540. spin_lock_irqsave(&dev->buf_lock, flags);
  541. while (!list_empty(&dev->avail_bufs)) {
  542. buf = list_first_entry(&dev->avail_bufs,
  543. struct stk1160_buffer, list);
  544. list_del(&buf->list);
  545. vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
  546. stk1160_info("buffer [%p/%d] aborted\n",
  547. buf, buf->vb.v4l2_buf.index);
  548. }
  549. /* It's important to clear current buffer */
  550. dev->isoc_ctl.buf = NULL;
  551. spin_unlock_irqrestore(&dev->buf_lock, flags);
  552. }
  553. int stk1160_vb2_setup(struct stk1160 *dev)
  554. {
  555. int rc;
  556. struct vb2_queue *q;
  557. q = &dev->vb_vidq;
  558. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  559. q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR;
  560. q->drv_priv = dev;
  561. q->buf_struct_size = sizeof(struct stk1160_buffer);
  562. q->ops = &stk1160_video_qops;
  563. q->mem_ops = &vb2_vmalloc_memops;
  564. rc = vb2_queue_init(q);
  565. if (rc < 0)
  566. return rc;
  567. /* initialize video dma queue */
  568. INIT_LIST_HEAD(&dev->avail_bufs);
  569. return 0;
  570. }
  571. int stk1160_video_register(struct stk1160 *dev)
  572. {
  573. int rc;
  574. /* Initialize video_device with a template structure */
  575. dev->vdev = v4l_template;
  576. dev->vdev.debug = vidioc_debug;
  577. dev->vdev.queue = &dev->vb_vidq;
  578. /*
  579. * Provide mutexes for v4l2 core and for videobuf2 queue.
  580. * It will be used to protect *only* v4l2 ioctls.
  581. */
  582. dev->vdev.lock = &dev->v4l_lock;
  583. dev->vdev.queue->lock = &dev->vb_queue_lock;
  584. /* This will be used to set video_device parent */
  585. dev->vdev.v4l2_dev = &dev->v4l2_dev;
  586. set_bit(V4L2_FL_USE_FH_PRIO, &dev->vdev.flags);
  587. /* NTSC is default */
  588. dev->norm = V4L2_STD_NTSC_M;
  589. dev->width = 720;
  590. dev->height = 480;
  591. /* set default format */
  592. dev->fmt = &format[0];
  593. stk1160_set_std(dev);
  594. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std,
  595. dev->norm);
  596. video_set_drvdata(&dev->vdev, dev);
  597. rc = video_register_device(&dev->vdev, VFL_TYPE_GRABBER, -1);
  598. if (rc < 0) {
  599. stk1160_err("video_register_device failed (%d)\n", rc);
  600. return rc;
  601. }
  602. v4l2_info(&dev->v4l2_dev, "V4L2 device registered as %s\n",
  603. video_device_node_name(&dev->vdev));
  604. return 0;
  605. }