stk1160-v4l.c 21 KB

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