ispvideo.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255
  1. /*
  2. * ispvideo.c
  3. *
  4. * TI OMAP3 ISP - Generic video node
  5. *
  6. * Copyright (C) 2009-2010 Nokia Corporation
  7. *
  8. * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
  9. * Sakari Ailus <sakari.ailus@iki.fi>
  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 version 2 as
  13. * published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  23. * 02110-1301 USA
  24. */
  25. #include <asm/cacheflush.h>
  26. #include <linux/clk.h>
  27. #include <linux/mm.h>
  28. #include <linux/pagemap.h>
  29. #include <linux/scatterlist.h>
  30. #include <linux/sched.h>
  31. #include <linux/slab.h>
  32. #include <linux/vmalloc.h>
  33. #include <media/v4l2-dev.h>
  34. #include <media/v4l2-ioctl.h>
  35. #include <plat/iommu.h>
  36. #include <plat/iovmm.h>
  37. #include <plat/omap-pm.h>
  38. #include "ispvideo.h"
  39. #include "isp.h"
  40. /* -----------------------------------------------------------------------------
  41. * Helper functions
  42. */
  43. static struct isp_format_info formats[] = {
  44. { V4L2_MBUS_FMT_Y8_1X8, V4L2_MBUS_FMT_Y8_1X8,
  45. V4L2_MBUS_FMT_Y8_1X8, V4L2_PIX_FMT_GREY, 8, },
  46. { V4L2_MBUS_FMT_SGRBG10_DPCM8_1X8, V4L2_MBUS_FMT_SGRBG10_DPCM8_1X8,
  47. V4L2_MBUS_FMT_SGRBG10_1X10, V4L2_PIX_FMT_SGRBG10DPCM8, 8, },
  48. { V4L2_MBUS_FMT_SBGGR10_1X10, V4L2_MBUS_FMT_SBGGR10_1X10,
  49. V4L2_MBUS_FMT_SBGGR10_1X10, V4L2_PIX_FMT_SBGGR10, 10, },
  50. { V4L2_MBUS_FMT_SGBRG10_1X10, V4L2_MBUS_FMT_SGBRG10_1X10,
  51. V4L2_MBUS_FMT_SGBRG10_1X10, V4L2_PIX_FMT_SGBRG10, 10, },
  52. { V4L2_MBUS_FMT_SGRBG10_1X10, V4L2_MBUS_FMT_SGRBG10_1X10,
  53. V4L2_MBUS_FMT_SGRBG10_1X10, V4L2_PIX_FMT_SGRBG10, 10, },
  54. { V4L2_MBUS_FMT_SRGGB10_1X10, V4L2_MBUS_FMT_SRGGB10_1X10,
  55. V4L2_MBUS_FMT_SRGGB10_1X10, V4L2_PIX_FMT_SRGGB10, 10, },
  56. { V4L2_MBUS_FMT_SBGGR12_1X12, V4L2_MBUS_FMT_SBGGR10_1X10,
  57. V4L2_MBUS_FMT_SBGGR12_1X12, V4L2_PIX_FMT_SBGGR12, 12, },
  58. { V4L2_MBUS_FMT_SGBRG12_1X12, V4L2_MBUS_FMT_SGBRG10_1X10,
  59. V4L2_MBUS_FMT_SGBRG12_1X12, V4L2_PIX_FMT_SGBRG12, 12, },
  60. { V4L2_MBUS_FMT_SGRBG12_1X12, V4L2_MBUS_FMT_SGRBG10_1X10,
  61. V4L2_MBUS_FMT_SGRBG12_1X12, V4L2_PIX_FMT_SGRBG12, 12, },
  62. { V4L2_MBUS_FMT_SRGGB12_1X12, V4L2_MBUS_FMT_SRGGB10_1X10,
  63. V4L2_MBUS_FMT_SRGGB12_1X12, V4L2_PIX_FMT_SRGGB12, 12, },
  64. { V4L2_MBUS_FMT_UYVY8_1X16, V4L2_MBUS_FMT_UYVY8_1X16,
  65. V4L2_MBUS_FMT_UYVY8_1X16, V4L2_PIX_FMT_UYVY, 16, },
  66. { V4L2_MBUS_FMT_YUYV8_1X16, V4L2_MBUS_FMT_YUYV8_1X16,
  67. V4L2_MBUS_FMT_YUYV8_1X16, V4L2_PIX_FMT_YUYV, 16, },
  68. };
  69. const struct isp_format_info *
  70. omap3isp_video_format_info(enum v4l2_mbus_pixelcode code)
  71. {
  72. unsigned int i;
  73. for (i = 0; i < ARRAY_SIZE(formats); ++i) {
  74. if (formats[i].code == code)
  75. return &formats[i];
  76. }
  77. return NULL;
  78. }
  79. /*
  80. * isp_video_mbus_to_pix - Convert v4l2_mbus_framefmt to v4l2_pix_format
  81. * @video: ISP video instance
  82. * @mbus: v4l2_mbus_framefmt format (input)
  83. * @pix: v4l2_pix_format format (output)
  84. *
  85. * Fill the output pix structure with information from the input mbus format.
  86. * The bytesperline and sizeimage fields are computed from the requested bytes
  87. * per line value in the pix format and information from the video instance.
  88. *
  89. * Return the number of padding bytes at end of line.
  90. */
  91. static unsigned int isp_video_mbus_to_pix(const struct isp_video *video,
  92. const struct v4l2_mbus_framefmt *mbus,
  93. struct v4l2_pix_format *pix)
  94. {
  95. unsigned int bpl = pix->bytesperline;
  96. unsigned int min_bpl;
  97. unsigned int i;
  98. memset(pix, 0, sizeof(*pix));
  99. pix->width = mbus->width;
  100. pix->height = mbus->height;
  101. for (i = 0; i < ARRAY_SIZE(formats); ++i) {
  102. if (formats[i].code == mbus->code)
  103. break;
  104. }
  105. if (WARN_ON(i == ARRAY_SIZE(formats)))
  106. return 0;
  107. min_bpl = pix->width * ALIGN(formats[i].bpp, 8) / 8;
  108. /* Clamp the requested bytes per line value. If the maximum bytes per
  109. * line value is zero, the module doesn't support user configurable line
  110. * sizes. Override the requested value with the minimum in that case.
  111. */
  112. if (video->bpl_max)
  113. bpl = clamp(bpl, min_bpl, video->bpl_max);
  114. else
  115. bpl = min_bpl;
  116. if (!video->bpl_zero_padding || bpl != min_bpl)
  117. bpl = ALIGN(bpl, video->bpl_alignment);
  118. pix->pixelformat = formats[i].pixelformat;
  119. pix->bytesperline = bpl;
  120. pix->sizeimage = pix->bytesperline * pix->height;
  121. pix->colorspace = mbus->colorspace;
  122. pix->field = mbus->field;
  123. return bpl - min_bpl;
  124. }
  125. static void isp_video_pix_to_mbus(const struct v4l2_pix_format *pix,
  126. struct v4l2_mbus_framefmt *mbus)
  127. {
  128. unsigned int i;
  129. memset(mbus, 0, sizeof(*mbus));
  130. mbus->width = pix->width;
  131. mbus->height = pix->height;
  132. for (i = 0; i < ARRAY_SIZE(formats); ++i) {
  133. if (formats[i].pixelformat == pix->pixelformat)
  134. break;
  135. }
  136. if (WARN_ON(i == ARRAY_SIZE(formats)))
  137. return;
  138. mbus->code = formats[i].code;
  139. mbus->colorspace = pix->colorspace;
  140. mbus->field = pix->field;
  141. }
  142. static struct v4l2_subdev *
  143. isp_video_remote_subdev(struct isp_video *video, u32 *pad)
  144. {
  145. struct media_pad *remote;
  146. remote = media_entity_remote_source(&video->pad);
  147. if (remote == NULL ||
  148. media_entity_type(remote->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
  149. return NULL;
  150. if (pad)
  151. *pad = remote->index;
  152. return media_entity_to_v4l2_subdev(remote->entity);
  153. }
  154. /* Return a pointer to the ISP video instance at the far end of the pipeline. */
  155. static struct isp_video *
  156. isp_video_far_end(struct isp_video *video)
  157. {
  158. struct media_entity_graph graph;
  159. struct media_entity *entity = &video->video.entity;
  160. struct media_device *mdev = entity->parent;
  161. struct isp_video *far_end = NULL;
  162. mutex_lock(&mdev->graph_mutex);
  163. media_entity_graph_walk_start(&graph, entity);
  164. while ((entity = media_entity_graph_walk_next(&graph))) {
  165. if (entity == &video->video.entity)
  166. continue;
  167. if (media_entity_type(entity) != MEDIA_ENT_T_DEVNODE)
  168. continue;
  169. far_end = to_isp_video(media_entity_to_video_device(entity));
  170. if (far_end->type != video->type)
  171. break;
  172. far_end = NULL;
  173. }
  174. mutex_unlock(&mdev->graph_mutex);
  175. return far_end;
  176. }
  177. /*
  178. * Validate a pipeline by checking both ends of all links for format
  179. * discrepancies.
  180. *
  181. * Compute the minimum time per frame value as the maximum of time per frame
  182. * limits reported by every block in the pipeline.
  183. *
  184. * Return 0 if all formats match, or -EPIPE if at least one link is found with
  185. * different formats on its two ends.
  186. */
  187. static int isp_video_validate_pipeline(struct isp_pipeline *pipe)
  188. {
  189. struct isp_device *isp = pipe->output->isp;
  190. struct v4l2_subdev_format fmt_source;
  191. struct v4l2_subdev_format fmt_sink;
  192. struct media_pad *pad;
  193. struct v4l2_subdev *subdev;
  194. int ret;
  195. pipe->max_rate = pipe->l3_ick;
  196. subdev = isp_video_remote_subdev(pipe->output, NULL);
  197. if (subdev == NULL)
  198. return -EPIPE;
  199. while (1) {
  200. /* Retrieve the sink format */
  201. pad = &subdev->entity.pads[0];
  202. if (!(pad->flags & MEDIA_PAD_FL_SINK))
  203. break;
  204. fmt_sink.pad = pad->index;
  205. fmt_sink.which = V4L2_SUBDEV_FORMAT_ACTIVE;
  206. ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt_sink);
  207. if (ret < 0 && ret != -ENOIOCTLCMD)
  208. return -EPIPE;
  209. /* Update the maximum frame rate */
  210. if (subdev == &isp->isp_res.subdev)
  211. omap3isp_resizer_max_rate(&isp->isp_res,
  212. &pipe->max_rate);
  213. /* Check ccdc maximum data rate when data comes from sensor
  214. * TODO: Include ccdc rate in pipe->max_rate and compare the
  215. * total pipe rate with the input data rate from sensor.
  216. */
  217. if (subdev == &isp->isp_ccdc.subdev && pipe->input == NULL) {
  218. unsigned int rate = UINT_MAX;
  219. omap3isp_ccdc_max_rate(&isp->isp_ccdc, &rate);
  220. if (isp->isp_ccdc.vpcfg.pixelclk > rate)
  221. return -ENOSPC;
  222. }
  223. /* Retrieve the source format */
  224. pad = media_entity_remote_source(pad);
  225. if (pad == NULL ||
  226. media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
  227. break;
  228. subdev = media_entity_to_v4l2_subdev(pad->entity);
  229. fmt_source.pad = pad->index;
  230. fmt_source.which = V4L2_SUBDEV_FORMAT_ACTIVE;
  231. ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt_source);
  232. if (ret < 0 && ret != -ENOIOCTLCMD)
  233. return -EPIPE;
  234. /* Check if the two ends match */
  235. if (fmt_source.format.code != fmt_sink.format.code ||
  236. fmt_source.format.width != fmt_sink.format.width ||
  237. fmt_source.format.height != fmt_sink.format.height)
  238. return -EPIPE;
  239. }
  240. return 0;
  241. }
  242. static int
  243. __isp_video_get_format(struct isp_video *video, struct v4l2_format *format)
  244. {
  245. struct v4l2_subdev_format fmt;
  246. struct v4l2_subdev *subdev;
  247. u32 pad;
  248. int ret;
  249. subdev = isp_video_remote_subdev(video, &pad);
  250. if (subdev == NULL)
  251. return -EINVAL;
  252. mutex_lock(&video->mutex);
  253. fmt.pad = pad;
  254. fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
  255. ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
  256. if (ret == -ENOIOCTLCMD)
  257. ret = -EINVAL;
  258. mutex_unlock(&video->mutex);
  259. if (ret)
  260. return ret;
  261. format->type = video->type;
  262. return isp_video_mbus_to_pix(video, &fmt.format, &format->fmt.pix);
  263. }
  264. static int
  265. isp_video_check_format(struct isp_video *video, struct isp_video_fh *vfh)
  266. {
  267. struct v4l2_format format;
  268. int ret;
  269. memcpy(&format, &vfh->format, sizeof(format));
  270. ret = __isp_video_get_format(video, &format);
  271. if (ret < 0)
  272. return ret;
  273. if (vfh->format.fmt.pix.pixelformat != format.fmt.pix.pixelformat ||
  274. vfh->format.fmt.pix.height != format.fmt.pix.height ||
  275. vfh->format.fmt.pix.width != format.fmt.pix.width ||
  276. vfh->format.fmt.pix.bytesperline != format.fmt.pix.bytesperline ||
  277. vfh->format.fmt.pix.sizeimage != format.fmt.pix.sizeimage)
  278. return -EINVAL;
  279. return ret;
  280. }
  281. /* -----------------------------------------------------------------------------
  282. * IOMMU management
  283. */
  284. #define IOMMU_FLAG (IOVMF_ENDIAN_LITTLE | IOVMF_ELSZ_8)
  285. /*
  286. * ispmmu_vmap - Wrapper for Virtual memory mapping of a scatter gather list
  287. * @dev: Device pointer specific to the OMAP3 ISP.
  288. * @sglist: Pointer to source Scatter gather list to allocate.
  289. * @sglen: Number of elements of the scatter-gatter list.
  290. *
  291. * Returns a resulting mapped device address by the ISP MMU, or -ENOMEM if
  292. * we ran out of memory.
  293. */
  294. static dma_addr_t
  295. ispmmu_vmap(struct isp_device *isp, const struct scatterlist *sglist, int sglen)
  296. {
  297. struct sg_table *sgt;
  298. u32 da;
  299. sgt = kmalloc(sizeof(*sgt), GFP_KERNEL);
  300. if (sgt == NULL)
  301. return -ENOMEM;
  302. sgt->sgl = (struct scatterlist *)sglist;
  303. sgt->nents = sglen;
  304. sgt->orig_nents = sglen;
  305. da = iommu_vmap(isp->iommu, 0, sgt, IOMMU_FLAG);
  306. if (IS_ERR_VALUE(da))
  307. kfree(sgt);
  308. return da;
  309. }
  310. /*
  311. * ispmmu_vunmap - Unmap a device address from the ISP MMU
  312. * @dev: Device pointer specific to the OMAP3 ISP.
  313. * @da: Device address generated from a ispmmu_vmap call.
  314. */
  315. static void ispmmu_vunmap(struct isp_device *isp, dma_addr_t da)
  316. {
  317. struct sg_table *sgt;
  318. sgt = iommu_vunmap(isp->iommu, (u32)da);
  319. kfree(sgt);
  320. }
  321. /* -----------------------------------------------------------------------------
  322. * Video queue operations
  323. */
  324. static void isp_video_queue_prepare(struct isp_video_queue *queue,
  325. unsigned int *nbuffers, unsigned int *size)
  326. {
  327. struct isp_video_fh *vfh =
  328. container_of(queue, struct isp_video_fh, queue);
  329. struct isp_video *video = vfh->video;
  330. *size = vfh->format.fmt.pix.sizeimage;
  331. if (*size == 0)
  332. return;
  333. *nbuffers = min(*nbuffers, video->capture_mem / PAGE_ALIGN(*size));
  334. }
  335. static void isp_video_buffer_cleanup(struct isp_video_buffer *buf)
  336. {
  337. struct isp_video_fh *vfh = isp_video_queue_to_isp_video_fh(buf->queue);
  338. struct isp_buffer *buffer = to_isp_buffer(buf);
  339. struct isp_video *video = vfh->video;
  340. if (buffer->isp_addr) {
  341. ispmmu_vunmap(video->isp, buffer->isp_addr);
  342. buffer->isp_addr = 0;
  343. }
  344. }
  345. static int isp_video_buffer_prepare(struct isp_video_buffer *buf)
  346. {
  347. struct isp_video_fh *vfh = isp_video_queue_to_isp_video_fh(buf->queue);
  348. struct isp_buffer *buffer = to_isp_buffer(buf);
  349. struct isp_video *video = vfh->video;
  350. unsigned long addr;
  351. addr = ispmmu_vmap(video->isp, buf->sglist, buf->sglen);
  352. if (IS_ERR_VALUE(addr))
  353. return -EIO;
  354. if (!IS_ALIGNED(addr, 32)) {
  355. dev_dbg(video->isp->dev, "Buffer address must be "
  356. "aligned to 32 bytes boundary.\n");
  357. ispmmu_vunmap(video->isp, buffer->isp_addr);
  358. return -EINVAL;
  359. }
  360. buf->vbuf.bytesused = vfh->format.fmt.pix.sizeimage;
  361. buffer->isp_addr = addr;
  362. return 0;
  363. }
  364. /*
  365. * isp_video_buffer_queue - Add buffer to streaming queue
  366. * @buf: Video buffer
  367. *
  368. * In memory-to-memory mode, start streaming on the pipeline if buffers are
  369. * queued on both the input and the output, if the pipeline isn't already busy.
  370. * If the pipeline is busy, it will be restarted in the output module interrupt
  371. * handler.
  372. */
  373. static void isp_video_buffer_queue(struct isp_video_buffer *buf)
  374. {
  375. struct isp_video_fh *vfh = isp_video_queue_to_isp_video_fh(buf->queue);
  376. struct isp_buffer *buffer = to_isp_buffer(buf);
  377. struct isp_video *video = vfh->video;
  378. struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity);
  379. enum isp_pipeline_state state;
  380. unsigned long flags;
  381. unsigned int empty;
  382. unsigned int start;
  383. empty = list_empty(&video->dmaqueue);
  384. list_add_tail(&buffer->buffer.irqlist, &video->dmaqueue);
  385. if (empty) {
  386. if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  387. state = ISP_PIPELINE_QUEUE_OUTPUT;
  388. else
  389. state = ISP_PIPELINE_QUEUE_INPUT;
  390. spin_lock_irqsave(&pipe->lock, flags);
  391. pipe->state |= state;
  392. video->ops->queue(video, buffer);
  393. video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_QUEUED;
  394. start = isp_pipeline_ready(pipe);
  395. if (start)
  396. pipe->state |= ISP_PIPELINE_STREAM;
  397. spin_unlock_irqrestore(&pipe->lock, flags);
  398. if (start)
  399. omap3isp_pipeline_set_stream(pipe,
  400. ISP_PIPELINE_STREAM_SINGLESHOT);
  401. }
  402. }
  403. static const struct isp_video_queue_operations isp_video_queue_ops = {
  404. .queue_prepare = &isp_video_queue_prepare,
  405. .buffer_prepare = &isp_video_buffer_prepare,
  406. .buffer_queue = &isp_video_buffer_queue,
  407. .buffer_cleanup = &isp_video_buffer_cleanup,
  408. };
  409. /*
  410. * omap3isp_video_buffer_next - Complete the current buffer and return the next
  411. * @video: ISP video object
  412. * @error: Whether an error occured during capture
  413. *
  414. * Remove the current video buffer from the DMA queue and fill its timestamp,
  415. * field count and state fields before waking up its completion handler.
  416. *
  417. * The buffer state is set to VIDEOBUF_DONE if no error occured (@error is 0)
  418. * or VIDEOBUF_ERROR otherwise (@error is non-zero).
  419. *
  420. * The DMA queue is expected to contain at least one buffer.
  421. *
  422. * Return a pointer to the next buffer in the DMA queue, or NULL if the queue is
  423. * empty.
  424. */
  425. struct isp_buffer *omap3isp_video_buffer_next(struct isp_video *video,
  426. unsigned int error)
  427. {
  428. struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity);
  429. struct isp_video_queue *queue = video->queue;
  430. enum isp_pipeline_state state;
  431. struct isp_video_buffer *buf;
  432. unsigned long flags;
  433. struct timespec ts;
  434. spin_lock_irqsave(&queue->irqlock, flags);
  435. if (WARN_ON(list_empty(&video->dmaqueue))) {
  436. spin_unlock_irqrestore(&queue->irqlock, flags);
  437. return NULL;
  438. }
  439. buf = list_first_entry(&video->dmaqueue, struct isp_video_buffer,
  440. irqlist);
  441. list_del(&buf->irqlist);
  442. spin_unlock_irqrestore(&queue->irqlock, flags);
  443. ktime_get_ts(&ts);
  444. buf->vbuf.timestamp.tv_sec = ts.tv_sec;
  445. buf->vbuf.timestamp.tv_usec = ts.tv_nsec / NSEC_PER_USEC;
  446. /* Do frame number propagation only if this is the output video node.
  447. * Frame number either comes from the CSI receivers or it gets
  448. * incremented here if H3A is not active.
  449. * Note: There is no guarantee that the output buffer will finish
  450. * first, so the input number might lag behind by 1 in some cases.
  451. */
  452. if (video == pipe->output && !pipe->do_propagation)
  453. buf->vbuf.sequence = atomic_inc_return(&pipe->frame_number);
  454. else
  455. buf->vbuf.sequence = atomic_read(&pipe->frame_number);
  456. buf->state = error ? ISP_BUF_STATE_ERROR : ISP_BUF_STATE_DONE;
  457. wake_up(&buf->wait);
  458. if (list_empty(&video->dmaqueue)) {
  459. if (queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  460. state = ISP_PIPELINE_QUEUE_OUTPUT
  461. | ISP_PIPELINE_STREAM;
  462. else
  463. state = ISP_PIPELINE_QUEUE_INPUT
  464. | ISP_PIPELINE_STREAM;
  465. spin_lock_irqsave(&pipe->lock, flags);
  466. pipe->state &= ~state;
  467. if (video->pipe.stream_state == ISP_PIPELINE_STREAM_CONTINUOUS)
  468. video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_UNDERRUN;
  469. spin_unlock_irqrestore(&pipe->lock, flags);
  470. return NULL;
  471. }
  472. if (queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && pipe->input != NULL) {
  473. spin_lock_irqsave(&pipe->lock, flags);
  474. pipe->state &= ~ISP_PIPELINE_STREAM;
  475. spin_unlock_irqrestore(&pipe->lock, flags);
  476. }
  477. buf = list_first_entry(&video->dmaqueue, struct isp_video_buffer,
  478. irqlist);
  479. buf->state = ISP_BUF_STATE_ACTIVE;
  480. return to_isp_buffer(buf);
  481. }
  482. /*
  483. * omap3isp_video_resume - Perform resume operation on the buffers
  484. * @video: ISP video object
  485. * @continuous: Pipeline is in single shot mode if 0 or continous mode otherwise
  486. *
  487. * This function is intended to be used on suspend/resume scenario. It
  488. * requests video queue layer to discard buffers marked as DONE if it's in
  489. * continuous mode and requests ISP modules to queue again the ACTIVE buffer
  490. * if there's any.
  491. */
  492. void omap3isp_video_resume(struct isp_video *video, int continuous)
  493. {
  494. struct isp_buffer *buf = NULL;
  495. if (continuous && video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  496. omap3isp_video_queue_discard_done(video->queue);
  497. if (!list_empty(&video->dmaqueue)) {
  498. buf = list_first_entry(&video->dmaqueue,
  499. struct isp_buffer, buffer.irqlist);
  500. video->ops->queue(video, buf);
  501. video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_QUEUED;
  502. } else {
  503. if (continuous)
  504. video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_UNDERRUN;
  505. }
  506. }
  507. /* -----------------------------------------------------------------------------
  508. * V4L2 ioctls
  509. */
  510. static int
  511. isp_video_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
  512. {
  513. struct isp_video *video = video_drvdata(file);
  514. strlcpy(cap->driver, ISP_VIDEO_DRIVER_NAME, sizeof(cap->driver));
  515. strlcpy(cap->card, video->video.name, sizeof(cap->card));
  516. strlcpy(cap->bus_info, "media", sizeof(cap->bus_info));
  517. cap->version = ISP_VIDEO_DRIVER_VERSION;
  518. if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  519. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
  520. else
  521. cap->capabilities = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
  522. return 0;
  523. }
  524. static int
  525. isp_video_get_format(struct file *file, void *fh, struct v4l2_format *format)
  526. {
  527. struct isp_video_fh *vfh = to_isp_video_fh(fh);
  528. struct isp_video *video = video_drvdata(file);
  529. if (format->type != video->type)
  530. return -EINVAL;
  531. mutex_lock(&video->mutex);
  532. *format = vfh->format;
  533. mutex_unlock(&video->mutex);
  534. return 0;
  535. }
  536. static int
  537. isp_video_set_format(struct file *file, void *fh, struct v4l2_format *format)
  538. {
  539. struct isp_video_fh *vfh = to_isp_video_fh(fh);
  540. struct isp_video *video = video_drvdata(file);
  541. struct v4l2_mbus_framefmt fmt;
  542. if (format->type != video->type)
  543. return -EINVAL;
  544. mutex_lock(&video->mutex);
  545. /* Fill the bytesperline and sizeimage fields by converting to media bus
  546. * format and back to pixel format.
  547. */
  548. isp_video_pix_to_mbus(&format->fmt.pix, &fmt);
  549. isp_video_mbus_to_pix(video, &fmt, &format->fmt.pix);
  550. vfh->format = *format;
  551. mutex_unlock(&video->mutex);
  552. return 0;
  553. }
  554. static int
  555. isp_video_try_format(struct file *file, void *fh, struct v4l2_format *format)
  556. {
  557. struct isp_video *video = video_drvdata(file);
  558. struct v4l2_subdev_format fmt;
  559. struct v4l2_subdev *subdev;
  560. u32 pad;
  561. int ret;
  562. if (format->type != video->type)
  563. return -EINVAL;
  564. subdev = isp_video_remote_subdev(video, &pad);
  565. if (subdev == NULL)
  566. return -EINVAL;
  567. isp_video_pix_to_mbus(&format->fmt.pix, &fmt.format);
  568. fmt.pad = pad;
  569. fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
  570. ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
  571. if (ret)
  572. return ret == -ENOIOCTLCMD ? -EINVAL : ret;
  573. isp_video_mbus_to_pix(video, &fmt.format, &format->fmt.pix);
  574. return 0;
  575. }
  576. static int
  577. isp_video_cropcap(struct file *file, void *fh, struct v4l2_cropcap *cropcap)
  578. {
  579. struct isp_video *video = video_drvdata(file);
  580. struct v4l2_subdev *subdev;
  581. int ret;
  582. subdev = isp_video_remote_subdev(video, NULL);
  583. if (subdev == NULL)
  584. return -EINVAL;
  585. mutex_lock(&video->mutex);
  586. ret = v4l2_subdev_call(subdev, video, cropcap, cropcap);
  587. mutex_unlock(&video->mutex);
  588. return ret == -ENOIOCTLCMD ? -EINVAL : ret;
  589. }
  590. static int
  591. isp_video_get_crop(struct file *file, void *fh, struct v4l2_crop *crop)
  592. {
  593. struct isp_video *video = video_drvdata(file);
  594. struct v4l2_subdev_format format;
  595. struct v4l2_subdev *subdev;
  596. u32 pad;
  597. int ret;
  598. subdev = isp_video_remote_subdev(video, &pad);
  599. if (subdev == NULL)
  600. return -EINVAL;
  601. /* Try the get crop operation first and fallback to get format if not
  602. * implemented.
  603. */
  604. ret = v4l2_subdev_call(subdev, video, g_crop, crop);
  605. if (ret != -ENOIOCTLCMD)
  606. return ret;
  607. format.pad = pad;
  608. format.which = V4L2_SUBDEV_FORMAT_ACTIVE;
  609. ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &format);
  610. if (ret < 0)
  611. return ret == -ENOIOCTLCMD ? -EINVAL : ret;
  612. crop->c.left = 0;
  613. crop->c.top = 0;
  614. crop->c.width = format.format.width;
  615. crop->c.height = format.format.height;
  616. return 0;
  617. }
  618. static int
  619. isp_video_set_crop(struct file *file, void *fh, struct v4l2_crop *crop)
  620. {
  621. struct isp_video *video = video_drvdata(file);
  622. struct v4l2_subdev *subdev;
  623. int ret;
  624. subdev = isp_video_remote_subdev(video, NULL);
  625. if (subdev == NULL)
  626. return -EINVAL;
  627. mutex_lock(&video->mutex);
  628. ret = v4l2_subdev_call(subdev, video, s_crop, crop);
  629. mutex_unlock(&video->mutex);
  630. return ret == -ENOIOCTLCMD ? -EINVAL : ret;
  631. }
  632. static int
  633. isp_video_get_param(struct file *file, void *fh, struct v4l2_streamparm *a)
  634. {
  635. struct isp_video_fh *vfh = to_isp_video_fh(fh);
  636. struct isp_video *video = video_drvdata(file);
  637. if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
  638. video->type != a->type)
  639. return -EINVAL;
  640. memset(a, 0, sizeof(*a));
  641. a->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  642. a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
  643. a->parm.output.timeperframe = vfh->timeperframe;
  644. return 0;
  645. }
  646. static int
  647. isp_video_set_param(struct file *file, void *fh, struct v4l2_streamparm *a)
  648. {
  649. struct isp_video_fh *vfh = to_isp_video_fh(fh);
  650. struct isp_video *video = video_drvdata(file);
  651. if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
  652. video->type != a->type)
  653. return -EINVAL;
  654. if (a->parm.output.timeperframe.denominator == 0)
  655. a->parm.output.timeperframe.denominator = 1;
  656. vfh->timeperframe = a->parm.output.timeperframe;
  657. return 0;
  658. }
  659. static int
  660. isp_video_reqbufs(struct file *file, void *fh, struct v4l2_requestbuffers *rb)
  661. {
  662. struct isp_video_fh *vfh = to_isp_video_fh(fh);
  663. return omap3isp_video_queue_reqbufs(&vfh->queue, rb);
  664. }
  665. static int
  666. isp_video_querybuf(struct file *file, void *fh, struct v4l2_buffer *b)
  667. {
  668. struct isp_video_fh *vfh = to_isp_video_fh(fh);
  669. return omap3isp_video_queue_querybuf(&vfh->queue, b);
  670. }
  671. static int
  672. isp_video_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
  673. {
  674. struct isp_video_fh *vfh = to_isp_video_fh(fh);
  675. return omap3isp_video_queue_qbuf(&vfh->queue, b);
  676. }
  677. static int
  678. isp_video_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
  679. {
  680. struct isp_video_fh *vfh = to_isp_video_fh(fh);
  681. return omap3isp_video_queue_dqbuf(&vfh->queue, b,
  682. file->f_flags & O_NONBLOCK);
  683. }
  684. /*
  685. * Stream management
  686. *
  687. * Every ISP pipeline has a single input and a single output. The input can be
  688. * either a sensor or a video node. The output is always a video node.
  689. *
  690. * As every pipeline has an output video node, the ISP video objects at the
  691. * pipeline output stores the pipeline state. It tracks the streaming state of
  692. * both the input and output, as well as the availability of buffers.
  693. *
  694. * In sensor-to-memory mode, frames are always available at the pipeline input.
  695. * Starting the sensor usually requires I2C transfers and must be done in
  696. * interruptible context. The pipeline is started and stopped synchronously
  697. * to the stream on/off commands. All modules in the pipeline will get their
  698. * subdev set stream handler called. The module at the end of the pipeline must
  699. * delay starting the hardware until buffers are available at its output.
  700. *
  701. * In memory-to-memory mode, starting/stopping the stream requires
  702. * synchronization between the input and output. ISP modules can't be stopped
  703. * in the middle of a frame, and at least some of the modules seem to become
  704. * busy as soon as they're started, even if they don't receive a frame start
  705. * event. For that reason frames need to be processed in single-shot mode. The
  706. * driver needs to wait until a frame is completely processed and written to
  707. * memory before restarting the pipeline for the next frame. Pipelined
  708. * processing might be possible but requires more testing.
  709. *
  710. * Stream start must be delayed until buffers are available at both the input
  711. * and output. The pipeline must be started in the videobuf queue callback with
  712. * the buffers queue spinlock held. The modules subdev set stream operation must
  713. * not sleep.
  714. */
  715. static int
  716. isp_video_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
  717. {
  718. struct isp_video_fh *vfh = to_isp_video_fh(fh);
  719. struct isp_video *video = video_drvdata(file);
  720. enum isp_pipeline_state state;
  721. struct isp_pipeline *pipe;
  722. struct isp_video *far_end;
  723. unsigned long flags;
  724. int ret;
  725. if (type != video->type)
  726. return -EINVAL;
  727. mutex_lock(&video->stream_lock);
  728. if (video->streaming) {
  729. mutex_unlock(&video->stream_lock);
  730. return -EBUSY;
  731. }
  732. /* Start streaming on the pipeline. No link touching an entity in the
  733. * pipeline can be activated or deactivated once streaming is started.
  734. */
  735. pipe = video->video.entity.pipe
  736. ? to_isp_pipeline(&video->video.entity) : &video->pipe;
  737. media_entity_pipeline_start(&video->video.entity, &pipe->pipe);
  738. /* Verify that the currently configured format matches the output of
  739. * the connected subdev.
  740. */
  741. ret = isp_video_check_format(video, vfh);
  742. if (ret < 0)
  743. goto error;
  744. video->bpl_padding = ret;
  745. video->bpl_value = vfh->format.fmt.pix.bytesperline;
  746. /* Find the ISP video node connected at the far end of the pipeline and
  747. * update the pipeline.
  748. */
  749. far_end = isp_video_far_end(video);
  750. if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  751. state = ISP_PIPELINE_STREAM_OUTPUT | ISP_PIPELINE_IDLE_OUTPUT;
  752. pipe->input = far_end;
  753. pipe->output = video;
  754. } else {
  755. if (far_end == NULL) {
  756. ret = -EPIPE;
  757. goto error;
  758. }
  759. state = ISP_PIPELINE_STREAM_INPUT | ISP_PIPELINE_IDLE_INPUT;
  760. pipe->input = video;
  761. pipe->output = far_end;
  762. }
  763. if (video->isp->pdata->set_constraints)
  764. video->isp->pdata->set_constraints(video->isp, true);
  765. pipe->l3_ick = clk_get_rate(video->isp->clock[ISP_CLK_L3_ICK]);
  766. /* Validate the pipeline and update its state. */
  767. ret = isp_video_validate_pipeline(pipe);
  768. if (ret < 0)
  769. goto error;
  770. spin_lock_irqsave(&pipe->lock, flags);
  771. pipe->state &= ~ISP_PIPELINE_STREAM;
  772. pipe->state |= state;
  773. spin_unlock_irqrestore(&pipe->lock, flags);
  774. /* Set the maximum time per frame as the value requested by userspace.
  775. * This is a soft limit that can be overridden if the hardware doesn't
  776. * support the request limit.
  777. */
  778. if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
  779. pipe->max_timeperframe = vfh->timeperframe;
  780. video->queue = &vfh->queue;
  781. INIT_LIST_HEAD(&video->dmaqueue);
  782. atomic_set(&pipe->frame_number, -1);
  783. ret = omap3isp_video_queue_streamon(&vfh->queue);
  784. if (ret < 0)
  785. goto error;
  786. /* In sensor-to-memory mode, the stream can be started synchronously
  787. * to the stream on command. In memory-to-memory mode, it will be
  788. * started when buffers are queued on both the input and output.
  789. */
  790. if (pipe->input == NULL) {
  791. ret = omap3isp_pipeline_set_stream(pipe,
  792. ISP_PIPELINE_STREAM_CONTINUOUS);
  793. if (ret < 0)
  794. goto error;
  795. spin_lock_irqsave(&video->queue->irqlock, flags);
  796. if (list_empty(&video->dmaqueue))
  797. video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_UNDERRUN;
  798. spin_unlock_irqrestore(&video->queue->irqlock, flags);
  799. }
  800. error:
  801. if (ret < 0) {
  802. omap3isp_video_queue_streamoff(&vfh->queue);
  803. if (video->isp->pdata->set_constraints)
  804. video->isp->pdata->set_constraints(video->isp, false);
  805. media_entity_pipeline_stop(&video->video.entity);
  806. video->queue = NULL;
  807. }
  808. if (!ret)
  809. video->streaming = 1;
  810. mutex_unlock(&video->stream_lock);
  811. return ret;
  812. }
  813. static int
  814. isp_video_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
  815. {
  816. struct isp_video_fh *vfh = to_isp_video_fh(fh);
  817. struct isp_video *video = video_drvdata(file);
  818. struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity);
  819. enum isp_pipeline_state state;
  820. unsigned int streaming;
  821. unsigned long flags;
  822. if (type != video->type)
  823. return -EINVAL;
  824. mutex_lock(&video->stream_lock);
  825. /* Make sure we're not streaming yet. */
  826. mutex_lock(&vfh->queue.lock);
  827. streaming = vfh->queue.streaming;
  828. mutex_unlock(&vfh->queue.lock);
  829. if (!streaming)
  830. goto done;
  831. /* Update the pipeline state. */
  832. if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  833. state = ISP_PIPELINE_STREAM_OUTPUT
  834. | ISP_PIPELINE_QUEUE_OUTPUT;
  835. else
  836. state = ISP_PIPELINE_STREAM_INPUT
  837. | ISP_PIPELINE_QUEUE_INPUT;
  838. spin_lock_irqsave(&pipe->lock, flags);
  839. pipe->state &= ~state;
  840. spin_unlock_irqrestore(&pipe->lock, flags);
  841. /* Stop the stream. */
  842. omap3isp_pipeline_set_stream(pipe, ISP_PIPELINE_STREAM_STOPPED);
  843. omap3isp_video_queue_streamoff(&vfh->queue);
  844. video->queue = NULL;
  845. video->streaming = 0;
  846. if (video->isp->pdata->set_constraints)
  847. video->isp->pdata->set_constraints(video->isp, false);
  848. media_entity_pipeline_stop(&video->video.entity);
  849. done:
  850. mutex_unlock(&video->stream_lock);
  851. return 0;
  852. }
  853. static int
  854. isp_video_enum_input(struct file *file, void *fh, struct v4l2_input *input)
  855. {
  856. if (input->index > 0)
  857. return -EINVAL;
  858. strlcpy(input->name, "camera", sizeof(input->name));
  859. input->type = V4L2_INPUT_TYPE_CAMERA;
  860. return 0;
  861. }
  862. static int
  863. isp_video_g_input(struct file *file, void *fh, unsigned int *input)
  864. {
  865. *input = 0;
  866. return 0;
  867. }
  868. static int
  869. isp_video_s_input(struct file *file, void *fh, unsigned int input)
  870. {
  871. return input == 0 ? 0 : -EINVAL;
  872. }
  873. static const struct v4l2_ioctl_ops isp_video_ioctl_ops = {
  874. .vidioc_querycap = isp_video_querycap,
  875. .vidioc_g_fmt_vid_cap = isp_video_get_format,
  876. .vidioc_s_fmt_vid_cap = isp_video_set_format,
  877. .vidioc_try_fmt_vid_cap = isp_video_try_format,
  878. .vidioc_g_fmt_vid_out = isp_video_get_format,
  879. .vidioc_s_fmt_vid_out = isp_video_set_format,
  880. .vidioc_try_fmt_vid_out = isp_video_try_format,
  881. .vidioc_cropcap = isp_video_cropcap,
  882. .vidioc_g_crop = isp_video_get_crop,
  883. .vidioc_s_crop = isp_video_set_crop,
  884. .vidioc_g_parm = isp_video_get_param,
  885. .vidioc_s_parm = isp_video_set_param,
  886. .vidioc_reqbufs = isp_video_reqbufs,
  887. .vidioc_querybuf = isp_video_querybuf,
  888. .vidioc_qbuf = isp_video_qbuf,
  889. .vidioc_dqbuf = isp_video_dqbuf,
  890. .vidioc_streamon = isp_video_streamon,
  891. .vidioc_streamoff = isp_video_streamoff,
  892. .vidioc_enum_input = isp_video_enum_input,
  893. .vidioc_g_input = isp_video_g_input,
  894. .vidioc_s_input = isp_video_s_input,
  895. };
  896. /* -----------------------------------------------------------------------------
  897. * V4L2 file operations
  898. */
  899. static int isp_video_open(struct file *file)
  900. {
  901. struct isp_video *video = video_drvdata(file);
  902. struct isp_video_fh *handle;
  903. int ret = 0;
  904. handle = kzalloc(sizeof(*handle), GFP_KERNEL);
  905. if (handle == NULL)
  906. return -ENOMEM;
  907. v4l2_fh_init(&handle->vfh, &video->video);
  908. v4l2_fh_add(&handle->vfh);
  909. /* If this is the first user, initialise the pipeline. */
  910. if (omap3isp_get(video->isp) == NULL) {
  911. ret = -EBUSY;
  912. goto done;
  913. }
  914. ret = omap3isp_pipeline_pm_use(&video->video.entity, 1);
  915. if (ret < 0) {
  916. omap3isp_put(video->isp);
  917. goto done;
  918. }
  919. omap3isp_video_queue_init(&handle->queue, video->type,
  920. &isp_video_queue_ops, video->isp->dev,
  921. sizeof(struct isp_buffer));
  922. memset(&handle->format, 0, sizeof(handle->format));
  923. handle->format.type = video->type;
  924. handle->timeperframe.denominator = 1;
  925. handle->video = video;
  926. file->private_data = &handle->vfh;
  927. done:
  928. if (ret < 0) {
  929. v4l2_fh_del(&handle->vfh);
  930. kfree(handle);
  931. }
  932. return ret;
  933. }
  934. static int isp_video_release(struct file *file)
  935. {
  936. struct isp_video *video = video_drvdata(file);
  937. struct v4l2_fh *vfh = file->private_data;
  938. struct isp_video_fh *handle = to_isp_video_fh(vfh);
  939. /* Disable streaming and free the buffers queue resources. */
  940. isp_video_streamoff(file, vfh, video->type);
  941. mutex_lock(&handle->queue.lock);
  942. omap3isp_video_queue_cleanup(&handle->queue);
  943. mutex_unlock(&handle->queue.lock);
  944. omap3isp_pipeline_pm_use(&video->video.entity, 0);
  945. /* Release the file handle. */
  946. v4l2_fh_del(vfh);
  947. kfree(handle);
  948. file->private_data = NULL;
  949. omap3isp_put(video->isp);
  950. return 0;
  951. }
  952. static unsigned int isp_video_poll(struct file *file, poll_table *wait)
  953. {
  954. struct isp_video_fh *vfh = to_isp_video_fh(file->private_data);
  955. struct isp_video_queue *queue = &vfh->queue;
  956. return omap3isp_video_queue_poll(queue, file, wait);
  957. }
  958. static int isp_video_mmap(struct file *file, struct vm_area_struct *vma)
  959. {
  960. struct isp_video_fh *vfh = to_isp_video_fh(file->private_data);
  961. return omap3isp_video_queue_mmap(&vfh->queue, vma);
  962. }
  963. static struct v4l2_file_operations isp_video_fops = {
  964. .owner = THIS_MODULE,
  965. .unlocked_ioctl = video_ioctl2,
  966. .open = isp_video_open,
  967. .release = isp_video_release,
  968. .poll = isp_video_poll,
  969. .mmap = isp_video_mmap,
  970. };
  971. /* -----------------------------------------------------------------------------
  972. * ISP video core
  973. */
  974. static const struct isp_video_operations isp_video_dummy_ops = {
  975. };
  976. int omap3isp_video_init(struct isp_video *video, const char *name)
  977. {
  978. const char *direction;
  979. int ret;
  980. switch (video->type) {
  981. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  982. direction = "output";
  983. video->pad.flags = MEDIA_PAD_FL_SINK;
  984. break;
  985. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  986. direction = "input";
  987. video->pad.flags = MEDIA_PAD_FL_SOURCE;
  988. break;
  989. default:
  990. return -EINVAL;
  991. }
  992. ret = media_entity_init(&video->video.entity, 1, &video->pad, 0);
  993. if (ret < 0)
  994. return ret;
  995. mutex_init(&video->mutex);
  996. atomic_set(&video->active, 0);
  997. spin_lock_init(&video->pipe.lock);
  998. mutex_init(&video->stream_lock);
  999. /* Initialize the video device. */
  1000. if (video->ops == NULL)
  1001. video->ops = &isp_video_dummy_ops;
  1002. video->video.fops = &isp_video_fops;
  1003. snprintf(video->video.name, sizeof(video->video.name),
  1004. "OMAP3 ISP %s %s", name, direction);
  1005. video->video.vfl_type = VFL_TYPE_GRABBER;
  1006. video->video.release = video_device_release_empty;
  1007. video->video.ioctl_ops = &isp_video_ioctl_ops;
  1008. video->pipe.stream_state = ISP_PIPELINE_STREAM_STOPPED;
  1009. video_set_drvdata(&video->video, video);
  1010. return 0;
  1011. }
  1012. int omap3isp_video_register(struct isp_video *video, struct v4l2_device *vdev)
  1013. {
  1014. int ret;
  1015. video->video.v4l2_dev = vdev;
  1016. ret = video_register_device(&video->video, VFL_TYPE_GRABBER, -1);
  1017. if (ret < 0)
  1018. printk(KERN_ERR "%s: could not register video device (%d)\n",
  1019. __func__, ret);
  1020. return ret;
  1021. }
  1022. void omap3isp_video_unregister(struct isp_video *video)
  1023. {
  1024. if (video_is_registered(&video->video)) {
  1025. media_entity_cleanup(&video->video.entity);
  1026. video_unregister_device(&video->video);
  1027. }
  1028. }