stk1160-v4l.c 21 KB

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