mixer_video.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137
  1. /*
  2. * Samsung TV Mixer driver
  3. *
  4. * Copyright (c) 2010-2011 Samsung Electronics Co., Ltd.
  5. *
  6. * Tomasz Stanislawski, <t.stanislaws@samsung.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published
  10. * by the Free Software Foundation. either version 2 of the License,
  11. * or (at your option) any later version
  12. */
  13. #define pr_fmt(fmt) "s5p-tv (mixer): " fmt
  14. #include "mixer.h"
  15. #include <media/v4l2-ioctl.h>
  16. #include <linux/videodev2.h>
  17. #include <linux/mm.h>
  18. #include <linux/module.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/timer.h>
  21. #include <media/videobuf2-dma-contig.h>
  22. static int find_reg_callback(struct device *dev, void *p)
  23. {
  24. struct v4l2_subdev **sd = p;
  25. *sd = dev_get_drvdata(dev);
  26. /* non-zero value stops iteration */
  27. return 1;
  28. }
  29. static struct v4l2_subdev *find_and_register_subdev(
  30. struct mxr_device *mdev, char *module_name)
  31. {
  32. struct device_driver *drv;
  33. struct v4l2_subdev *sd = NULL;
  34. int ret;
  35. /* TODO: add waiting until probe is finished */
  36. drv = driver_find(module_name, &platform_bus_type);
  37. if (!drv) {
  38. mxr_warn(mdev, "module %s is missing\n", module_name);
  39. return NULL;
  40. }
  41. /* driver refcnt is increased, it is safe to iterate over devices */
  42. ret = driver_for_each_device(drv, NULL, &sd, find_reg_callback);
  43. /* ret == 0 means that find_reg_callback was never executed */
  44. if (sd == NULL) {
  45. mxr_warn(mdev, "module %s provides no subdev!\n", module_name);
  46. goto done;
  47. }
  48. /* v4l2_device_register_subdev detects if sd is NULL */
  49. ret = v4l2_device_register_subdev(&mdev->v4l2_dev, sd);
  50. if (ret) {
  51. mxr_warn(mdev, "failed to register subdev %s\n", sd->name);
  52. sd = NULL;
  53. }
  54. done:
  55. return sd;
  56. }
  57. int mxr_acquire_video(struct mxr_device *mdev,
  58. struct mxr_output_conf *output_conf, int output_count)
  59. {
  60. struct device *dev = mdev->dev;
  61. struct v4l2_device *v4l2_dev = &mdev->v4l2_dev;
  62. int i;
  63. int ret = 0;
  64. struct v4l2_subdev *sd;
  65. strlcpy(v4l2_dev->name, dev_name(mdev->dev), sizeof(v4l2_dev->name));
  66. /* prepare context for V4L2 device */
  67. ret = v4l2_device_register(dev, v4l2_dev);
  68. if (ret) {
  69. mxr_err(mdev, "could not register v4l2 device.\n");
  70. goto fail;
  71. }
  72. mdev->alloc_ctx = vb2_dma_contig_init_ctx(mdev->dev);
  73. if (IS_ERR(mdev->alloc_ctx)) {
  74. mxr_err(mdev, "could not acquire vb2 allocator\n");
  75. ret = PTR_ERR(mdev->alloc_ctx);
  76. goto fail_v4l2_dev;
  77. }
  78. /* registering outputs */
  79. mdev->output_cnt = 0;
  80. for (i = 0; i < output_count; ++i) {
  81. struct mxr_output_conf *conf = &output_conf[i];
  82. struct mxr_output *out;
  83. sd = find_and_register_subdev(mdev, conf->module_name);
  84. /* trying to register next output */
  85. if (sd == NULL)
  86. continue;
  87. out = kzalloc(sizeof(*out), GFP_KERNEL);
  88. if (out == NULL) {
  89. mxr_err(mdev, "no memory for '%s'\n",
  90. conf->output_name);
  91. ret = -ENOMEM;
  92. /* registered subdevs are removed in fail_v4l2_dev */
  93. goto fail_output;
  94. }
  95. strlcpy(out->name, conf->output_name, sizeof(out->name));
  96. out->sd = sd;
  97. out->cookie = conf->cookie;
  98. mdev->output[mdev->output_cnt++] = out;
  99. mxr_info(mdev, "added output '%s' from module '%s'\n",
  100. conf->output_name, conf->module_name);
  101. /* checking if maximal number of outputs is reached */
  102. if (mdev->output_cnt >= MXR_MAX_OUTPUTS)
  103. break;
  104. }
  105. if (mdev->output_cnt == 0) {
  106. mxr_err(mdev, "failed to register any output\n");
  107. ret = -ENODEV;
  108. /* skipping fail_output because there is nothing to free */
  109. goto fail_vb2_allocator;
  110. }
  111. return 0;
  112. fail_output:
  113. /* kfree is NULL-safe */
  114. for (i = 0; i < mdev->output_cnt; ++i)
  115. kfree(mdev->output[i]);
  116. memset(mdev->output, 0, sizeof(mdev->output));
  117. fail_vb2_allocator:
  118. /* freeing allocator context */
  119. vb2_dma_contig_cleanup_ctx(mdev->alloc_ctx);
  120. fail_v4l2_dev:
  121. /* NOTE: automatically unregister all subdevs */
  122. v4l2_device_unregister(v4l2_dev);
  123. fail:
  124. return ret;
  125. }
  126. void mxr_release_video(struct mxr_device *mdev)
  127. {
  128. int i;
  129. /* kfree is NULL-safe */
  130. for (i = 0; i < mdev->output_cnt; ++i)
  131. kfree(mdev->output[i]);
  132. vb2_dma_contig_cleanup_ctx(mdev->alloc_ctx);
  133. v4l2_device_unregister(&mdev->v4l2_dev);
  134. }
  135. static int mxr_querycap(struct file *file, void *priv,
  136. struct v4l2_capability *cap)
  137. {
  138. struct mxr_layer *layer = video_drvdata(file);
  139. mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
  140. strlcpy(cap->driver, MXR_DRIVER_NAME, sizeof(cap->driver));
  141. strlcpy(cap->card, layer->vfd.name, sizeof(cap->card));
  142. sprintf(cap->bus_info, "%d", layer->idx);
  143. cap->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_OUTPUT_MPLANE;
  144. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  145. return 0;
  146. }
  147. static void mxr_geometry_dump(struct mxr_device *mdev, struct mxr_geometry *geo)
  148. {
  149. mxr_dbg(mdev, "src.full_size = (%u, %u)\n",
  150. geo->src.full_width, geo->src.full_height);
  151. mxr_dbg(mdev, "src.size = (%u, %u)\n",
  152. geo->src.width, geo->src.height);
  153. mxr_dbg(mdev, "src.offset = (%u, %u)\n",
  154. geo->src.x_offset, geo->src.y_offset);
  155. mxr_dbg(mdev, "dst.full_size = (%u, %u)\n",
  156. geo->dst.full_width, geo->dst.full_height);
  157. mxr_dbg(mdev, "dst.size = (%u, %u)\n",
  158. geo->dst.width, geo->dst.height);
  159. mxr_dbg(mdev, "dst.offset = (%u, %u)\n",
  160. geo->dst.x_offset, geo->dst.y_offset);
  161. mxr_dbg(mdev, "ratio = (%u, %u)\n",
  162. geo->x_ratio, geo->y_ratio);
  163. }
  164. static void mxr_layer_default_geo(struct mxr_layer *layer)
  165. {
  166. struct mxr_device *mdev = layer->mdev;
  167. struct v4l2_mbus_framefmt mbus_fmt;
  168. memset(&layer->geo, 0, sizeof(layer->geo));
  169. mxr_get_mbus_fmt(mdev, &mbus_fmt);
  170. layer->geo.dst.full_width = mbus_fmt.width;
  171. layer->geo.dst.full_height = mbus_fmt.height;
  172. layer->geo.dst.width = layer->geo.dst.full_width;
  173. layer->geo.dst.height = layer->geo.dst.full_height;
  174. layer->geo.dst.field = mbus_fmt.field;
  175. layer->geo.src.full_width = mbus_fmt.width;
  176. layer->geo.src.full_height = mbus_fmt.height;
  177. layer->geo.src.width = layer->geo.src.full_width;
  178. layer->geo.src.height = layer->geo.src.full_height;
  179. mxr_geometry_dump(mdev, &layer->geo);
  180. layer->ops.fix_geometry(layer, MXR_GEOMETRY_SINK, 0);
  181. mxr_geometry_dump(mdev, &layer->geo);
  182. }
  183. static void mxr_layer_update_output(struct mxr_layer *layer)
  184. {
  185. struct mxr_device *mdev = layer->mdev;
  186. struct v4l2_mbus_framefmt mbus_fmt;
  187. mxr_get_mbus_fmt(mdev, &mbus_fmt);
  188. /* checking if update is needed */
  189. if (layer->geo.dst.full_width == mbus_fmt.width &&
  190. layer->geo.dst.full_height == mbus_fmt.width)
  191. return;
  192. layer->geo.dst.full_width = mbus_fmt.width;
  193. layer->geo.dst.full_height = mbus_fmt.height;
  194. layer->geo.dst.field = mbus_fmt.field;
  195. layer->ops.fix_geometry(layer, MXR_GEOMETRY_SINK, 0);
  196. mxr_geometry_dump(mdev, &layer->geo);
  197. }
  198. static const struct mxr_format *find_format_by_fourcc(
  199. struct mxr_layer *layer, unsigned long fourcc);
  200. static const struct mxr_format *find_format_by_index(
  201. struct mxr_layer *layer, unsigned long index);
  202. static int mxr_enum_fmt(struct file *file, void *priv,
  203. struct v4l2_fmtdesc *f)
  204. {
  205. struct mxr_layer *layer = video_drvdata(file);
  206. struct mxr_device *mdev = layer->mdev;
  207. const struct mxr_format *fmt;
  208. mxr_dbg(mdev, "%s\n", __func__);
  209. fmt = find_format_by_index(layer, f->index);
  210. if (fmt == NULL)
  211. return -EINVAL;
  212. strlcpy(f->description, fmt->name, sizeof(f->description));
  213. f->pixelformat = fmt->fourcc;
  214. return 0;
  215. }
  216. static unsigned int divup(unsigned int divident, unsigned int divisor)
  217. {
  218. return (divident + divisor - 1) / divisor;
  219. }
  220. unsigned long mxr_get_plane_size(const struct mxr_block *blk,
  221. unsigned int width, unsigned int height)
  222. {
  223. unsigned int bl_width = divup(width, blk->width);
  224. unsigned int bl_height = divup(height, blk->height);
  225. return bl_width * bl_height * blk->size;
  226. }
  227. static void mxr_mplane_fill(struct v4l2_plane_pix_format *planes,
  228. const struct mxr_format *fmt, u32 width, u32 height)
  229. {
  230. int i;
  231. /* checking if nothing to fill */
  232. if (!planes)
  233. return;
  234. memset(planes, 0, sizeof(*planes) * fmt->num_subframes);
  235. for (i = 0; i < fmt->num_planes; ++i) {
  236. struct v4l2_plane_pix_format *plane = planes
  237. + fmt->plane2subframe[i];
  238. const struct mxr_block *blk = &fmt->plane[i];
  239. u32 bl_width = divup(width, blk->width);
  240. u32 bl_height = divup(height, blk->height);
  241. u32 sizeimage = bl_width * bl_height * blk->size;
  242. u32 bytesperline = bl_width * blk->size / blk->height;
  243. plane->sizeimage += sizeimage;
  244. plane->bytesperline = max(plane->bytesperline, bytesperline);
  245. }
  246. }
  247. static int mxr_g_fmt(struct file *file, void *priv,
  248. struct v4l2_format *f)
  249. {
  250. struct mxr_layer *layer = video_drvdata(file);
  251. struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
  252. mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
  253. pix->width = layer->geo.src.full_width;
  254. pix->height = layer->geo.src.full_height;
  255. pix->field = V4L2_FIELD_NONE;
  256. pix->pixelformat = layer->fmt->fourcc;
  257. pix->colorspace = layer->fmt->colorspace;
  258. mxr_mplane_fill(pix->plane_fmt, layer->fmt, pix->width, pix->height);
  259. return 0;
  260. }
  261. static int mxr_s_fmt(struct file *file, void *priv,
  262. struct v4l2_format *f)
  263. {
  264. struct mxr_layer *layer = video_drvdata(file);
  265. const struct mxr_format *fmt;
  266. struct v4l2_pix_format_mplane *pix;
  267. struct mxr_device *mdev = layer->mdev;
  268. struct mxr_geometry *geo = &layer->geo;
  269. mxr_dbg(mdev, "%s:%d\n", __func__, __LINE__);
  270. pix = &f->fmt.pix_mp;
  271. fmt = find_format_by_fourcc(layer, pix->pixelformat);
  272. if (fmt == NULL) {
  273. mxr_warn(mdev, "not recognized fourcc: %08x\n",
  274. pix->pixelformat);
  275. return -EINVAL;
  276. }
  277. layer->fmt = fmt;
  278. /* set source size to highest accepted value */
  279. geo->src.full_width = max(geo->dst.full_width, pix->width);
  280. geo->src.full_height = max(geo->dst.full_height, pix->height);
  281. layer->ops.fix_geometry(layer, MXR_GEOMETRY_SOURCE, 0);
  282. mxr_geometry_dump(mdev, &layer->geo);
  283. /* set cropping to total visible screen */
  284. geo->src.width = pix->width;
  285. geo->src.height = pix->height;
  286. geo->src.x_offset = 0;
  287. geo->src.y_offset = 0;
  288. /* assure consistency of geometry */
  289. layer->ops.fix_geometry(layer, MXR_GEOMETRY_CROP, MXR_NO_OFFSET);
  290. mxr_geometry_dump(mdev, &layer->geo);
  291. /* set full size to lowest possible value */
  292. geo->src.full_width = 0;
  293. geo->src.full_height = 0;
  294. layer->ops.fix_geometry(layer, MXR_GEOMETRY_SOURCE, 0);
  295. mxr_geometry_dump(mdev, &layer->geo);
  296. /* returning results */
  297. mxr_g_fmt(file, priv, f);
  298. return 0;
  299. }
  300. static int mxr_g_selection(struct file *file, void *fh,
  301. struct v4l2_selection *s)
  302. {
  303. struct mxr_layer *layer = video_drvdata(file);
  304. struct mxr_geometry *geo = &layer->geo;
  305. mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
  306. if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
  307. s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  308. return -EINVAL;
  309. switch (s->target) {
  310. case V4L2_SEL_TGT_CROP:
  311. s->r.left = geo->src.x_offset;
  312. s->r.top = geo->src.y_offset;
  313. s->r.width = geo->src.width;
  314. s->r.height = geo->src.height;
  315. break;
  316. case V4L2_SEL_TGT_CROP_DEFAULT:
  317. case V4L2_SEL_TGT_CROP_BOUNDS:
  318. s->r.left = 0;
  319. s->r.top = 0;
  320. s->r.width = geo->src.full_width;
  321. s->r.height = geo->src.full_height;
  322. break;
  323. case V4L2_SEL_TGT_COMPOSE:
  324. case V4L2_SEL_TGT_COMPOSE_PADDED:
  325. s->r.left = geo->dst.x_offset;
  326. s->r.top = geo->dst.y_offset;
  327. s->r.width = geo->dst.width;
  328. s->r.height = geo->dst.height;
  329. break;
  330. case V4L2_SEL_TGT_COMPOSE_DEFAULT:
  331. case V4L2_SEL_TGT_COMPOSE_BOUNDS:
  332. s->r.left = 0;
  333. s->r.top = 0;
  334. s->r.width = geo->dst.full_width;
  335. s->r.height = geo->dst.full_height;
  336. break;
  337. default:
  338. return -EINVAL;
  339. }
  340. return 0;
  341. }
  342. /* returns 1 if rectangle 'a' is inside 'b' */
  343. static int mxr_is_rect_inside(struct v4l2_rect *a, struct v4l2_rect *b)
  344. {
  345. if (a->left < b->left)
  346. return 0;
  347. if (a->top < b->top)
  348. return 0;
  349. if (a->left + a->width > b->left + b->width)
  350. return 0;
  351. if (a->top + a->height > b->top + b->height)
  352. return 0;
  353. return 1;
  354. }
  355. static int mxr_s_selection(struct file *file, void *fh,
  356. struct v4l2_selection *s)
  357. {
  358. struct mxr_layer *layer = video_drvdata(file);
  359. struct mxr_geometry *geo = &layer->geo;
  360. struct mxr_crop *target = NULL;
  361. enum mxr_geometry_stage stage;
  362. struct mxr_geometry tmp;
  363. struct v4l2_rect res;
  364. memset(&res, 0, sizeof(res));
  365. mxr_dbg(layer->mdev, "%s: rect: %dx%d@%d,%d\n", __func__,
  366. s->r.width, s->r.height, s->r.left, s->r.top);
  367. if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
  368. s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  369. return -EINVAL;
  370. switch (s->target) {
  371. /* ignore read-only targets */
  372. case V4L2_SEL_TGT_CROP_DEFAULT:
  373. case V4L2_SEL_TGT_CROP_BOUNDS:
  374. res.width = geo->src.full_width;
  375. res.height = geo->src.full_height;
  376. break;
  377. /* ignore read-only targets */
  378. case V4L2_SEL_TGT_COMPOSE_DEFAULT:
  379. case V4L2_SEL_TGT_COMPOSE_BOUNDS:
  380. res.width = geo->dst.full_width;
  381. res.height = geo->dst.full_height;
  382. break;
  383. case V4L2_SEL_TGT_CROP:
  384. target = &geo->src;
  385. stage = MXR_GEOMETRY_CROP;
  386. break;
  387. case V4L2_SEL_TGT_COMPOSE:
  388. case V4L2_SEL_TGT_COMPOSE_PADDED:
  389. target = &geo->dst;
  390. stage = MXR_GEOMETRY_COMPOSE;
  391. break;
  392. default:
  393. return -EINVAL;
  394. }
  395. /* apply change and update geometry if needed */
  396. if (target) {
  397. /* backup current geometry if setup fails */
  398. memcpy(&tmp, geo, sizeof(tmp));
  399. /* apply requested selection */
  400. target->x_offset = s->r.left;
  401. target->y_offset = s->r.top;
  402. target->width = s->r.width;
  403. target->height = s->r.height;
  404. layer->ops.fix_geometry(layer, stage, s->flags);
  405. /* retrieve update selection rectangle */
  406. res.left = target->x_offset;
  407. res.top = target->y_offset;
  408. res.width = target->width;
  409. res.height = target->height;
  410. mxr_geometry_dump(layer->mdev, &layer->geo);
  411. }
  412. /* checking if the rectangle satisfies constraints */
  413. if ((s->flags & V4L2_SEL_FLAG_LE) && !mxr_is_rect_inside(&res, &s->r))
  414. goto fail;
  415. if ((s->flags & V4L2_SEL_FLAG_GE) && !mxr_is_rect_inside(&s->r, &res))
  416. goto fail;
  417. /* return result rectangle */
  418. s->r = res;
  419. return 0;
  420. fail:
  421. /* restore old geometry, which is not touched if target is NULL */
  422. if (target)
  423. memcpy(geo, &tmp, sizeof(tmp));
  424. return -ERANGE;
  425. }
  426. static int mxr_enum_dv_timings(struct file *file, void *fh,
  427. struct v4l2_enum_dv_timings *timings)
  428. {
  429. struct mxr_layer *layer = video_drvdata(file);
  430. struct mxr_device *mdev = layer->mdev;
  431. int ret;
  432. timings->pad = 0;
  433. /* lock protects from changing sd_out */
  434. mutex_lock(&mdev->mutex);
  435. ret = v4l2_subdev_call(to_outsd(mdev), pad, enum_dv_timings, timings);
  436. mutex_unlock(&mdev->mutex);
  437. return ret ? -EINVAL : 0;
  438. }
  439. static int mxr_s_dv_timings(struct file *file, void *fh,
  440. struct v4l2_dv_timings *timings)
  441. {
  442. struct mxr_layer *layer = video_drvdata(file);
  443. struct mxr_device *mdev = layer->mdev;
  444. int ret;
  445. /* lock protects from changing sd_out */
  446. mutex_lock(&mdev->mutex);
  447. /* timings change cannot be done while there is an entity
  448. * dependent on output configuration
  449. */
  450. if (mdev->n_output > 0) {
  451. mutex_unlock(&mdev->mutex);
  452. return -EBUSY;
  453. }
  454. ret = v4l2_subdev_call(to_outsd(mdev), video, s_dv_timings, timings);
  455. mutex_unlock(&mdev->mutex);
  456. mxr_layer_update_output(layer);
  457. /* any failure should return EINVAL according to V4L2 doc */
  458. return ret ? -EINVAL : 0;
  459. }
  460. static int mxr_g_dv_timings(struct file *file, void *fh,
  461. struct v4l2_dv_timings *timings)
  462. {
  463. struct mxr_layer *layer = video_drvdata(file);
  464. struct mxr_device *mdev = layer->mdev;
  465. int ret;
  466. /* lock protects from changing sd_out */
  467. mutex_lock(&mdev->mutex);
  468. ret = v4l2_subdev_call(to_outsd(mdev), video, g_dv_timings, timings);
  469. mutex_unlock(&mdev->mutex);
  470. return ret ? -EINVAL : 0;
  471. }
  472. static int mxr_dv_timings_cap(struct file *file, void *fh,
  473. struct v4l2_dv_timings_cap *cap)
  474. {
  475. struct mxr_layer *layer = video_drvdata(file);
  476. struct mxr_device *mdev = layer->mdev;
  477. int ret;
  478. cap->pad = 0;
  479. /* lock protects from changing sd_out */
  480. mutex_lock(&mdev->mutex);
  481. ret = v4l2_subdev_call(to_outsd(mdev), pad, dv_timings_cap, cap);
  482. mutex_unlock(&mdev->mutex);
  483. return ret ? -EINVAL : 0;
  484. }
  485. static int mxr_s_std(struct file *file, void *fh, v4l2_std_id norm)
  486. {
  487. struct mxr_layer *layer = video_drvdata(file);
  488. struct mxr_device *mdev = layer->mdev;
  489. int ret;
  490. /* lock protects from changing sd_out */
  491. mutex_lock(&mdev->mutex);
  492. /* standard change cannot be done while there is an entity
  493. * dependent on output configuration
  494. */
  495. if (mdev->n_output > 0) {
  496. mutex_unlock(&mdev->mutex);
  497. return -EBUSY;
  498. }
  499. ret = v4l2_subdev_call(to_outsd(mdev), video, s_std_output, norm);
  500. mutex_unlock(&mdev->mutex);
  501. mxr_layer_update_output(layer);
  502. return ret ? -EINVAL : 0;
  503. }
  504. static int mxr_g_std(struct file *file, void *fh, v4l2_std_id *norm)
  505. {
  506. struct mxr_layer *layer = video_drvdata(file);
  507. struct mxr_device *mdev = layer->mdev;
  508. int ret;
  509. /* lock protects from changing sd_out */
  510. mutex_lock(&mdev->mutex);
  511. ret = v4l2_subdev_call(to_outsd(mdev), video, g_std_output, norm);
  512. mutex_unlock(&mdev->mutex);
  513. return ret ? -EINVAL : 0;
  514. }
  515. static int mxr_enum_output(struct file *file, void *fh, struct v4l2_output *a)
  516. {
  517. struct mxr_layer *layer = video_drvdata(file);
  518. struct mxr_device *mdev = layer->mdev;
  519. struct mxr_output *out;
  520. struct v4l2_subdev *sd;
  521. if (a->index >= mdev->output_cnt)
  522. return -EINVAL;
  523. out = mdev->output[a->index];
  524. BUG_ON(out == NULL);
  525. sd = out->sd;
  526. strlcpy(a->name, out->name, sizeof(a->name));
  527. /* try to obtain supported tv norms */
  528. v4l2_subdev_call(sd, video, g_tvnorms_output, &a->std);
  529. a->capabilities = 0;
  530. if (sd->ops->video && sd->ops->video->s_dv_timings)
  531. a->capabilities |= V4L2_OUT_CAP_DV_TIMINGS;
  532. if (sd->ops->video && sd->ops->video->s_std_output)
  533. a->capabilities |= V4L2_OUT_CAP_STD;
  534. a->type = V4L2_OUTPUT_TYPE_ANALOG;
  535. return 0;
  536. }
  537. static int mxr_s_output(struct file *file, void *fh, unsigned int i)
  538. {
  539. struct video_device *vfd = video_devdata(file);
  540. struct mxr_layer *layer = video_drvdata(file);
  541. struct mxr_device *mdev = layer->mdev;
  542. if (i >= mdev->output_cnt || mdev->output[i] == NULL)
  543. return -EINVAL;
  544. mutex_lock(&mdev->mutex);
  545. if (mdev->n_output > 0) {
  546. mutex_unlock(&mdev->mutex);
  547. return -EBUSY;
  548. }
  549. mdev->current_output = i;
  550. vfd->tvnorms = 0;
  551. v4l2_subdev_call(to_outsd(mdev), video, g_tvnorms_output,
  552. &vfd->tvnorms);
  553. mutex_unlock(&mdev->mutex);
  554. /* update layers geometry */
  555. mxr_layer_update_output(layer);
  556. mxr_dbg(mdev, "tvnorms = %08llx\n", vfd->tvnorms);
  557. return 0;
  558. }
  559. static int mxr_g_output(struct file *file, void *fh, unsigned int *p)
  560. {
  561. struct mxr_layer *layer = video_drvdata(file);
  562. struct mxr_device *mdev = layer->mdev;
  563. mutex_lock(&mdev->mutex);
  564. *p = mdev->current_output;
  565. mutex_unlock(&mdev->mutex);
  566. return 0;
  567. }
  568. static int mxr_reqbufs(struct file *file, void *priv,
  569. struct v4l2_requestbuffers *p)
  570. {
  571. struct mxr_layer *layer = video_drvdata(file);
  572. mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
  573. return vb2_reqbufs(&layer->vb_queue, p);
  574. }
  575. static int mxr_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
  576. {
  577. struct mxr_layer *layer = video_drvdata(file);
  578. mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
  579. return vb2_querybuf(&layer->vb_queue, p);
  580. }
  581. static int mxr_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
  582. {
  583. struct mxr_layer *layer = video_drvdata(file);
  584. mxr_dbg(layer->mdev, "%s:%d(%d)\n", __func__, __LINE__, p->index);
  585. return vb2_qbuf(&layer->vb_queue, p);
  586. }
  587. static int mxr_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
  588. {
  589. struct mxr_layer *layer = video_drvdata(file);
  590. mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
  591. return vb2_dqbuf(&layer->vb_queue, p, file->f_flags & O_NONBLOCK);
  592. }
  593. static int mxr_expbuf(struct file *file, void *priv,
  594. struct v4l2_exportbuffer *eb)
  595. {
  596. struct mxr_layer *layer = video_drvdata(file);
  597. mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
  598. return vb2_expbuf(&layer->vb_queue, eb);
  599. }
  600. static int mxr_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
  601. {
  602. struct mxr_layer *layer = video_drvdata(file);
  603. mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
  604. return vb2_streamon(&layer->vb_queue, i);
  605. }
  606. static int mxr_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
  607. {
  608. struct mxr_layer *layer = video_drvdata(file);
  609. mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
  610. return vb2_streamoff(&layer->vb_queue, i);
  611. }
  612. static const struct v4l2_ioctl_ops mxr_ioctl_ops = {
  613. .vidioc_querycap = mxr_querycap,
  614. /* format handling */
  615. .vidioc_enum_fmt_vid_out_mplane = mxr_enum_fmt,
  616. .vidioc_s_fmt_vid_out_mplane = mxr_s_fmt,
  617. .vidioc_g_fmt_vid_out_mplane = mxr_g_fmt,
  618. /* buffer control */
  619. .vidioc_reqbufs = mxr_reqbufs,
  620. .vidioc_querybuf = mxr_querybuf,
  621. .vidioc_qbuf = mxr_qbuf,
  622. .vidioc_dqbuf = mxr_dqbuf,
  623. .vidioc_expbuf = mxr_expbuf,
  624. /* Streaming control */
  625. .vidioc_streamon = mxr_streamon,
  626. .vidioc_streamoff = mxr_streamoff,
  627. /* DV Timings functions */
  628. .vidioc_enum_dv_timings = mxr_enum_dv_timings,
  629. .vidioc_s_dv_timings = mxr_s_dv_timings,
  630. .vidioc_g_dv_timings = mxr_g_dv_timings,
  631. .vidioc_dv_timings_cap = mxr_dv_timings_cap,
  632. /* analog TV standard functions */
  633. .vidioc_s_std = mxr_s_std,
  634. .vidioc_g_std = mxr_g_std,
  635. /* Output handling */
  636. .vidioc_enum_output = mxr_enum_output,
  637. .vidioc_s_output = mxr_s_output,
  638. .vidioc_g_output = mxr_g_output,
  639. /* selection ioctls */
  640. .vidioc_g_selection = mxr_g_selection,
  641. .vidioc_s_selection = mxr_s_selection,
  642. };
  643. static int mxr_video_open(struct file *file)
  644. {
  645. struct mxr_layer *layer = video_drvdata(file);
  646. struct mxr_device *mdev = layer->mdev;
  647. int ret = 0;
  648. mxr_dbg(mdev, "%s:%d\n", __func__, __LINE__);
  649. if (mutex_lock_interruptible(&layer->mutex))
  650. return -ERESTARTSYS;
  651. /* assure device probe is finished */
  652. wait_for_device_probe();
  653. /* creating context for file descriptor */
  654. ret = v4l2_fh_open(file);
  655. if (ret) {
  656. mxr_err(mdev, "v4l2_fh_open failed\n");
  657. goto unlock;
  658. }
  659. /* leaving if layer is already initialized */
  660. if (!v4l2_fh_is_singular_file(file))
  661. goto unlock;
  662. /* FIXME: should power be enabled on open? */
  663. ret = mxr_power_get(mdev);
  664. if (ret) {
  665. mxr_err(mdev, "power on failed\n");
  666. goto fail_fh_open;
  667. }
  668. ret = vb2_queue_init(&layer->vb_queue);
  669. if (ret != 0) {
  670. mxr_err(mdev, "failed to initialize vb2 queue\n");
  671. goto fail_power;
  672. }
  673. /* set default format, first on the list */
  674. layer->fmt = layer->fmt_array[0];
  675. /* setup default geometry */
  676. mxr_layer_default_geo(layer);
  677. mutex_unlock(&layer->mutex);
  678. return 0;
  679. fail_power:
  680. mxr_power_put(mdev);
  681. fail_fh_open:
  682. v4l2_fh_release(file);
  683. unlock:
  684. mutex_unlock(&layer->mutex);
  685. return ret;
  686. }
  687. static unsigned int
  688. mxr_video_poll(struct file *file, struct poll_table_struct *wait)
  689. {
  690. struct mxr_layer *layer = video_drvdata(file);
  691. unsigned int res;
  692. mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
  693. mutex_lock(&layer->mutex);
  694. res = vb2_poll(&layer->vb_queue, file, wait);
  695. mutex_unlock(&layer->mutex);
  696. return res;
  697. }
  698. static int mxr_video_mmap(struct file *file, struct vm_area_struct *vma)
  699. {
  700. struct mxr_layer *layer = video_drvdata(file);
  701. int ret;
  702. mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
  703. if (mutex_lock_interruptible(&layer->mutex))
  704. return -ERESTARTSYS;
  705. ret = vb2_mmap(&layer->vb_queue, vma);
  706. mutex_unlock(&layer->mutex);
  707. return ret;
  708. }
  709. static int mxr_video_release(struct file *file)
  710. {
  711. struct mxr_layer *layer = video_drvdata(file);
  712. mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
  713. mutex_lock(&layer->mutex);
  714. if (v4l2_fh_is_singular_file(file)) {
  715. vb2_queue_release(&layer->vb_queue);
  716. mxr_power_put(layer->mdev);
  717. }
  718. v4l2_fh_release(file);
  719. mutex_unlock(&layer->mutex);
  720. return 0;
  721. }
  722. static const struct v4l2_file_operations mxr_fops = {
  723. .owner = THIS_MODULE,
  724. .open = mxr_video_open,
  725. .poll = mxr_video_poll,
  726. .mmap = mxr_video_mmap,
  727. .release = mxr_video_release,
  728. .unlocked_ioctl = video_ioctl2,
  729. };
  730. static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *pfmt,
  731. unsigned int *nbuffers, unsigned int *nplanes, unsigned int sizes[],
  732. void *alloc_ctxs[])
  733. {
  734. struct mxr_layer *layer = vb2_get_drv_priv(vq);
  735. const struct mxr_format *fmt = layer->fmt;
  736. int i;
  737. struct mxr_device *mdev = layer->mdev;
  738. struct v4l2_plane_pix_format planes[3];
  739. mxr_dbg(mdev, "%s\n", __func__);
  740. /* checking if format was configured */
  741. if (fmt == NULL)
  742. return -EINVAL;
  743. mxr_dbg(mdev, "fmt = %s\n", fmt->name);
  744. mxr_mplane_fill(planes, fmt, layer->geo.src.full_width,
  745. layer->geo.src.full_height);
  746. *nplanes = fmt->num_subframes;
  747. for (i = 0; i < fmt->num_subframes; ++i) {
  748. alloc_ctxs[i] = layer->mdev->alloc_ctx;
  749. sizes[i] = planes[i].sizeimage;
  750. mxr_dbg(mdev, "size[%d] = %08x\n", i, sizes[i]);
  751. }
  752. if (*nbuffers == 0)
  753. *nbuffers = 1;
  754. return 0;
  755. }
  756. static void buf_queue(struct vb2_buffer *vb)
  757. {
  758. struct mxr_buffer *buffer = container_of(vb, struct mxr_buffer, vb);
  759. struct mxr_layer *layer = vb2_get_drv_priv(vb->vb2_queue);
  760. struct mxr_device *mdev = layer->mdev;
  761. unsigned long flags;
  762. spin_lock_irqsave(&layer->enq_slock, flags);
  763. list_add_tail(&buffer->list, &layer->enq_list);
  764. spin_unlock_irqrestore(&layer->enq_slock, flags);
  765. mxr_dbg(mdev, "queuing buffer\n");
  766. }
  767. static int start_streaming(struct vb2_queue *vq, unsigned int count)
  768. {
  769. struct mxr_layer *layer = vb2_get_drv_priv(vq);
  770. struct mxr_device *mdev = layer->mdev;
  771. unsigned long flags;
  772. mxr_dbg(mdev, "%s\n", __func__);
  773. /* block any changes in output configuration */
  774. mxr_output_get(mdev);
  775. mxr_layer_update_output(layer);
  776. layer->ops.format_set(layer);
  777. /* enabling layer in hardware */
  778. spin_lock_irqsave(&layer->enq_slock, flags);
  779. layer->state = MXR_LAYER_STREAMING;
  780. spin_unlock_irqrestore(&layer->enq_slock, flags);
  781. layer->ops.stream_set(layer, MXR_ENABLE);
  782. mxr_streamer_get(mdev);
  783. return 0;
  784. }
  785. static void mxr_watchdog(unsigned long arg)
  786. {
  787. struct mxr_layer *layer = (struct mxr_layer *) arg;
  788. struct mxr_device *mdev = layer->mdev;
  789. unsigned long flags;
  790. mxr_err(mdev, "watchdog fired for layer %s\n", layer->vfd.name);
  791. spin_lock_irqsave(&layer->enq_slock, flags);
  792. if (layer->update_buf == layer->shadow_buf)
  793. layer->update_buf = NULL;
  794. if (layer->update_buf) {
  795. vb2_buffer_done(&layer->update_buf->vb, VB2_BUF_STATE_ERROR);
  796. layer->update_buf = NULL;
  797. }
  798. if (layer->shadow_buf) {
  799. vb2_buffer_done(&layer->shadow_buf->vb, VB2_BUF_STATE_ERROR);
  800. layer->shadow_buf = NULL;
  801. }
  802. spin_unlock_irqrestore(&layer->enq_slock, flags);
  803. }
  804. static void stop_streaming(struct vb2_queue *vq)
  805. {
  806. struct mxr_layer *layer = vb2_get_drv_priv(vq);
  807. struct mxr_device *mdev = layer->mdev;
  808. unsigned long flags;
  809. struct timer_list watchdog;
  810. struct mxr_buffer *buf, *buf_tmp;
  811. mxr_dbg(mdev, "%s\n", __func__);
  812. spin_lock_irqsave(&layer->enq_slock, flags);
  813. /* reset list */
  814. layer->state = MXR_LAYER_STREAMING_FINISH;
  815. /* set all buffer to be done */
  816. list_for_each_entry_safe(buf, buf_tmp, &layer->enq_list, list) {
  817. list_del(&buf->list);
  818. vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
  819. }
  820. spin_unlock_irqrestore(&layer->enq_slock, flags);
  821. /* give 1 seconds to complete to complete last buffers */
  822. setup_timer_on_stack(&watchdog, mxr_watchdog,
  823. (unsigned long)layer);
  824. mod_timer(&watchdog, jiffies + msecs_to_jiffies(1000));
  825. /* wait until all buffers are goes to done state */
  826. vb2_wait_for_all_buffers(vq);
  827. /* stop timer if all synchronization is done */
  828. del_timer_sync(&watchdog);
  829. destroy_timer_on_stack(&watchdog);
  830. /* stopping hardware */
  831. spin_lock_irqsave(&layer->enq_slock, flags);
  832. layer->state = MXR_LAYER_IDLE;
  833. spin_unlock_irqrestore(&layer->enq_slock, flags);
  834. /* disabling layer in hardware */
  835. layer->ops.stream_set(layer, MXR_DISABLE);
  836. /* remove one streamer */
  837. mxr_streamer_put(mdev);
  838. /* allow changes in output configuration */
  839. mxr_output_put(mdev);
  840. }
  841. static struct vb2_ops mxr_video_qops = {
  842. .queue_setup = queue_setup,
  843. .buf_queue = buf_queue,
  844. .wait_prepare = vb2_ops_wait_prepare,
  845. .wait_finish = vb2_ops_wait_finish,
  846. .start_streaming = start_streaming,
  847. .stop_streaming = stop_streaming,
  848. };
  849. /* FIXME: try to put this functions to mxr_base_layer_create */
  850. int mxr_base_layer_register(struct mxr_layer *layer)
  851. {
  852. struct mxr_device *mdev = layer->mdev;
  853. int ret;
  854. ret = video_register_device(&layer->vfd, VFL_TYPE_GRABBER, -1);
  855. if (ret)
  856. mxr_err(mdev, "failed to register video device\n");
  857. else
  858. mxr_info(mdev, "registered layer %s as /dev/video%d\n",
  859. layer->vfd.name, layer->vfd.num);
  860. return ret;
  861. }
  862. void mxr_base_layer_unregister(struct mxr_layer *layer)
  863. {
  864. video_unregister_device(&layer->vfd);
  865. }
  866. void mxr_layer_release(struct mxr_layer *layer)
  867. {
  868. if (layer->ops.release)
  869. layer->ops.release(layer);
  870. }
  871. void mxr_base_layer_release(struct mxr_layer *layer)
  872. {
  873. kfree(layer);
  874. }
  875. static void mxr_vfd_release(struct video_device *vdev)
  876. {
  877. pr_info("video device release\n");
  878. }
  879. struct mxr_layer *mxr_base_layer_create(struct mxr_device *mdev,
  880. int idx, char *name, struct mxr_layer_ops *ops)
  881. {
  882. struct mxr_layer *layer;
  883. layer = kzalloc(sizeof(*layer), GFP_KERNEL);
  884. if (layer == NULL) {
  885. mxr_err(mdev, "not enough memory for layer.\n");
  886. goto fail;
  887. }
  888. layer->mdev = mdev;
  889. layer->idx = idx;
  890. layer->ops = *ops;
  891. spin_lock_init(&layer->enq_slock);
  892. INIT_LIST_HEAD(&layer->enq_list);
  893. mutex_init(&layer->mutex);
  894. layer->vfd = (struct video_device) {
  895. .minor = -1,
  896. .release = mxr_vfd_release,
  897. .fops = &mxr_fops,
  898. .vfl_dir = VFL_DIR_TX,
  899. .ioctl_ops = &mxr_ioctl_ops,
  900. };
  901. strlcpy(layer->vfd.name, name, sizeof(layer->vfd.name));
  902. video_set_drvdata(&layer->vfd, layer);
  903. layer->vfd.lock = &layer->mutex;
  904. layer->vfd.v4l2_dev = &mdev->v4l2_dev;
  905. layer->vb_queue = (struct vb2_queue) {
  906. .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
  907. .io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF,
  908. .drv_priv = layer,
  909. .buf_struct_size = sizeof(struct mxr_buffer),
  910. .ops = &mxr_video_qops,
  911. .min_buffers_needed = 1,
  912. .mem_ops = &vb2_dma_contig_memops,
  913. .lock = &layer->mutex,
  914. };
  915. return layer;
  916. fail:
  917. return NULL;
  918. }
  919. static const struct mxr_format *find_format_by_fourcc(
  920. struct mxr_layer *layer, unsigned long fourcc)
  921. {
  922. int i;
  923. for (i = 0; i < layer->fmt_array_size; ++i)
  924. if (layer->fmt_array[i]->fourcc == fourcc)
  925. return layer->fmt_array[i];
  926. return NULL;
  927. }
  928. static const struct mxr_format *find_format_by_index(
  929. struct mxr_layer *layer, unsigned long index)
  930. {
  931. if (index >= layer->fmt_array_size)
  932. return NULL;
  933. return layer->fmt_array[index];
  934. }