stk1160-v4l.c 18 KB

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