v4l2-subdev.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. /*
  2. * V4L2 sub-device
  3. *
  4. * Copyright (C) 2010 Nokia Corporation
  5. *
  6. * Contact: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
  7. * Sakari Ailus <sakari.ailus@iki.fi>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. #include <linux/ioctl.h>
  19. #include <linux/mm.h>
  20. #include <linux/slab.h>
  21. #include <linux/types.h>
  22. #include <linux/videodev2.h>
  23. #include <linux/export.h>
  24. #include <media/v4l2-ctrls.h>
  25. #include <media/v4l2-device.h>
  26. #include <media/v4l2-ioctl.h>
  27. #include <media/v4l2-fh.h>
  28. #include <media/v4l2-event.h>
  29. static int subdev_fh_init(struct v4l2_subdev_fh *fh, struct v4l2_subdev *sd)
  30. {
  31. #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
  32. if (sd->entity.num_pads) {
  33. fh->pad = v4l2_subdev_alloc_pad_config(sd);
  34. if (fh->pad == NULL)
  35. return -ENOMEM;
  36. }
  37. #endif
  38. return 0;
  39. }
  40. static void subdev_fh_free(struct v4l2_subdev_fh *fh)
  41. {
  42. #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
  43. v4l2_subdev_free_pad_config(fh->pad);
  44. fh->pad = NULL;
  45. #endif
  46. }
  47. static int subdev_open(struct file *file)
  48. {
  49. struct video_device *vdev = video_devdata(file);
  50. struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
  51. struct v4l2_subdev_fh *subdev_fh;
  52. #if defined(CONFIG_MEDIA_CONTROLLER)
  53. struct media_entity *entity = NULL;
  54. #endif
  55. int ret;
  56. subdev_fh = kzalloc(sizeof(*subdev_fh), GFP_KERNEL);
  57. if (subdev_fh == NULL)
  58. return -ENOMEM;
  59. ret = subdev_fh_init(subdev_fh, sd);
  60. if (ret) {
  61. kfree(subdev_fh);
  62. return ret;
  63. }
  64. v4l2_fh_init(&subdev_fh->vfh, vdev);
  65. v4l2_fh_add(&subdev_fh->vfh);
  66. file->private_data = &subdev_fh->vfh;
  67. #if defined(CONFIG_MEDIA_CONTROLLER)
  68. if (sd->v4l2_dev->mdev) {
  69. entity = media_entity_get(&sd->entity);
  70. if (!entity) {
  71. ret = -EBUSY;
  72. goto err;
  73. }
  74. }
  75. #endif
  76. if (sd->internal_ops && sd->internal_ops->open) {
  77. ret = sd->internal_ops->open(sd, subdev_fh);
  78. if (ret < 0)
  79. goto err;
  80. }
  81. return 0;
  82. err:
  83. #if defined(CONFIG_MEDIA_CONTROLLER)
  84. media_entity_put(entity);
  85. #endif
  86. v4l2_fh_del(&subdev_fh->vfh);
  87. v4l2_fh_exit(&subdev_fh->vfh);
  88. subdev_fh_free(subdev_fh);
  89. kfree(subdev_fh);
  90. return ret;
  91. }
  92. static int subdev_close(struct file *file)
  93. {
  94. struct video_device *vdev = video_devdata(file);
  95. struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
  96. struct v4l2_fh *vfh = file->private_data;
  97. struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh);
  98. if (sd->internal_ops && sd->internal_ops->close)
  99. sd->internal_ops->close(sd, subdev_fh);
  100. #if defined(CONFIG_MEDIA_CONTROLLER)
  101. if (sd->v4l2_dev->mdev)
  102. media_entity_put(&sd->entity);
  103. #endif
  104. v4l2_fh_del(vfh);
  105. v4l2_fh_exit(vfh);
  106. subdev_fh_free(subdev_fh);
  107. kfree(subdev_fh);
  108. file->private_data = NULL;
  109. return 0;
  110. }
  111. #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
  112. static int check_format(struct v4l2_subdev *sd,
  113. struct v4l2_subdev_format *format)
  114. {
  115. if (format->which != V4L2_SUBDEV_FORMAT_TRY &&
  116. format->which != V4L2_SUBDEV_FORMAT_ACTIVE)
  117. return -EINVAL;
  118. if (format->pad >= sd->entity.num_pads)
  119. return -EINVAL;
  120. return 0;
  121. }
  122. static int check_crop(struct v4l2_subdev *sd, struct v4l2_subdev_crop *crop)
  123. {
  124. if (crop->which != V4L2_SUBDEV_FORMAT_TRY &&
  125. crop->which != V4L2_SUBDEV_FORMAT_ACTIVE)
  126. return -EINVAL;
  127. if (crop->pad >= sd->entity.num_pads)
  128. return -EINVAL;
  129. return 0;
  130. }
  131. static int check_selection(struct v4l2_subdev *sd,
  132. struct v4l2_subdev_selection *sel)
  133. {
  134. if (sel->which != V4L2_SUBDEV_FORMAT_TRY &&
  135. sel->which != V4L2_SUBDEV_FORMAT_ACTIVE)
  136. return -EINVAL;
  137. if (sel->pad >= sd->entity.num_pads)
  138. return -EINVAL;
  139. return 0;
  140. }
  141. static int check_edid(struct v4l2_subdev *sd, struct v4l2_subdev_edid *edid)
  142. {
  143. if (edid->pad >= sd->entity.num_pads)
  144. return -EINVAL;
  145. if (edid->blocks && edid->edid == NULL)
  146. return -EINVAL;
  147. return 0;
  148. }
  149. #endif
  150. static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg)
  151. {
  152. struct video_device *vdev = video_devdata(file);
  153. struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
  154. struct v4l2_fh *vfh = file->private_data;
  155. #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
  156. struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh);
  157. int rval;
  158. #endif
  159. switch (cmd) {
  160. case VIDIOC_QUERYCTRL:
  161. return v4l2_queryctrl(vfh->ctrl_handler, arg);
  162. case VIDIOC_QUERY_EXT_CTRL:
  163. return v4l2_query_ext_ctrl(vfh->ctrl_handler, arg);
  164. case VIDIOC_QUERYMENU:
  165. return v4l2_querymenu(vfh->ctrl_handler, arg);
  166. case VIDIOC_G_CTRL:
  167. return v4l2_g_ctrl(vfh->ctrl_handler, arg);
  168. case VIDIOC_S_CTRL:
  169. return v4l2_s_ctrl(vfh, vfh->ctrl_handler, arg);
  170. case VIDIOC_G_EXT_CTRLS:
  171. return v4l2_g_ext_ctrls(vfh->ctrl_handler, arg);
  172. case VIDIOC_S_EXT_CTRLS:
  173. return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler, arg);
  174. case VIDIOC_TRY_EXT_CTRLS:
  175. return v4l2_try_ext_ctrls(vfh->ctrl_handler, arg);
  176. case VIDIOC_DQEVENT:
  177. if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
  178. return -ENOIOCTLCMD;
  179. return v4l2_event_dequeue(vfh, arg, file->f_flags & O_NONBLOCK);
  180. case VIDIOC_SUBSCRIBE_EVENT:
  181. return v4l2_subdev_call(sd, core, subscribe_event, vfh, arg);
  182. case VIDIOC_UNSUBSCRIBE_EVENT:
  183. return v4l2_subdev_call(sd, core, unsubscribe_event, vfh, arg);
  184. #ifdef CONFIG_VIDEO_ADV_DEBUG
  185. case VIDIOC_DBG_G_REGISTER:
  186. {
  187. struct v4l2_dbg_register *p = arg;
  188. if (!capable(CAP_SYS_ADMIN))
  189. return -EPERM;
  190. return v4l2_subdev_call(sd, core, g_register, p);
  191. }
  192. case VIDIOC_DBG_S_REGISTER:
  193. {
  194. struct v4l2_dbg_register *p = arg;
  195. if (!capable(CAP_SYS_ADMIN))
  196. return -EPERM;
  197. return v4l2_subdev_call(sd, core, s_register, p);
  198. }
  199. #endif
  200. case VIDIOC_LOG_STATUS: {
  201. int ret;
  202. pr_info("%s: ================= START STATUS =================\n",
  203. sd->name);
  204. ret = v4l2_subdev_call(sd, core, log_status);
  205. pr_info("%s: ================== END STATUS ==================\n",
  206. sd->name);
  207. return ret;
  208. }
  209. #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
  210. case VIDIOC_SUBDEV_G_FMT: {
  211. struct v4l2_subdev_format *format = arg;
  212. rval = check_format(sd, format);
  213. if (rval)
  214. return rval;
  215. return v4l2_subdev_call(sd, pad, get_fmt, subdev_fh->pad, format);
  216. }
  217. case VIDIOC_SUBDEV_S_FMT: {
  218. struct v4l2_subdev_format *format = arg;
  219. rval = check_format(sd, format);
  220. if (rval)
  221. return rval;
  222. return v4l2_subdev_call(sd, pad, set_fmt, subdev_fh->pad, format);
  223. }
  224. case VIDIOC_SUBDEV_G_CROP: {
  225. struct v4l2_subdev_crop *crop = arg;
  226. struct v4l2_subdev_selection sel;
  227. rval = check_crop(sd, crop);
  228. if (rval)
  229. return rval;
  230. memset(&sel, 0, sizeof(sel));
  231. sel.which = crop->which;
  232. sel.pad = crop->pad;
  233. sel.target = V4L2_SEL_TGT_CROP;
  234. rval = v4l2_subdev_call(
  235. sd, pad, get_selection, subdev_fh->pad, &sel);
  236. crop->rect = sel.r;
  237. return rval;
  238. }
  239. case VIDIOC_SUBDEV_S_CROP: {
  240. struct v4l2_subdev_crop *crop = arg;
  241. struct v4l2_subdev_selection sel;
  242. rval = check_crop(sd, crop);
  243. if (rval)
  244. return rval;
  245. memset(&sel, 0, sizeof(sel));
  246. sel.which = crop->which;
  247. sel.pad = crop->pad;
  248. sel.target = V4L2_SEL_TGT_CROP;
  249. sel.r = crop->rect;
  250. rval = v4l2_subdev_call(
  251. sd, pad, set_selection, subdev_fh->pad, &sel);
  252. crop->rect = sel.r;
  253. return rval;
  254. }
  255. case VIDIOC_SUBDEV_ENUM_MBUS_CODE: {
  256. struct v4l2_subdev_mbus_code_enum *code = arg;
  257. if (code->which != V4L2_SUBDEV_FORMAT_TRY &&
  258. code->which != V4L2_SUBDEV_FORMAT_ACTIVE)
  259. return -EINVAL;
  260. if (code->pad >= sd->entity.num_pads)
  261. return -EINVAL;
  262. return v4l2_subdev_call(sd, pad, enum_mbus_code, subdev_fh->pad,
  263. code);
  264. }
  265. case VIDIOC_SUBDEV_ENUM_FRAME_SIZE: {
  266. struct v4l2_subdev_frame_size_enum *fse = arg;
  267. if (fse->which != V4L2_SUBDEV_FORMAT_TRY &&
  268. fse->which != V4L2_SUBDEV_FORMAT_ACTIVE)
  269. return -EINVAL;
  270. if (fse->pad >= sd->entity.num_pads)
  271. return -EINVAL;
  272. return v4l2_subdev_call(sd, pad, enum_frame_size, subdev_fh->pad,
  273. fse);
  274. }
  275. case VIDIOC_SUBDEV_G_FRAME_INTERVAL: {
  276. struct v4l2_subdev_frame_interval *fi = arg;
  277. if (fi->pad >= sd->entity.num_pads)
  278. return -EINVAL;
  279. return v4l2_subdev_call(sd, video, g_frame_interval, arg);
  280. }
  281. case VIDIOC_SUBDEV_S_FRAME_INTERVAL: {
  282. struct v4l2_subdev_frame_interval *fi = arg;
  283. if (fi->pad >= sd->entity.num_pads)
  284. return -EINVAL;
  285. return v4l2_subdev_call(sd, video, s_frame_interval, arg);
  286. }
  287. case VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL: {
  288. struct v4l2_subdev_frame_interval_enum *fie = arg;
  289. if (fie->which != V4L2_SUBDEV_FORMAT_TRY &&
  290. fie->which != V4L2_SUBDEV_FORMAT_ACTIVE)
  291. return -EINVAL;
  292. if (fie->pad >= sd->entity.num_pads)
  293. return -EINVAL;
  294. return v4l2_subdev_call(sd, pad, enum_frame_interval, subdev_fh->pad,
  295. fie);
  296. }
  297. case VIDIOC_SUBDEV_G_SELECTION: {
  298. struct v4l2_subdev_selection *sel = arg;
  299. rval = check_selection(sd, sel);
  300. if (rval)
  301. return rval;
  302. return v4l2_subdev_call(
  303. sd, pad, get_selection, subdev_fh->pad, sel);
  304. }
  305. case VIDIOC_SUBDEV_S_SELECTION: {
  306. struct v4l2_subdev_selection *sel = arg;
  307. rval = check_selection(sd, sel);
  308. if (rval)
  309. return rval;
  310. return v4l2_subdev_call(
  311. sd, pad, set_selection, subdev_fh->pad, sel);
  312. }
  313. case VIDIOC_G_EDID: {
  314. struct v4l2_subdev_edid *edid = arg;
  315. rval = check_edid(sd, edid);
  316. if (rval)
  317. return rval;
  318. return v4l2_subdev_call(sd, pad, get_edid, edid);
  319. }
  320. case VIDIOC_S_EDID: {
  321. struct v4l2_subdev_edid *edid = arg;
  322. rval = check_edid(sd, edid);
  323. if (rval)
  324. return rval;
  325. return v4l2_subdev_call(sd, pad, set_edid, edid);
  326. }
  327. case VIDIOC_SUBDEV_DV_TIMINGS_CAP: {
  328. struct v4l2_dv_timings_cap *cap = arg;
  329. if (cap->pad >= sd->entity.num_pads)
  330. return -EINVAL;
  331. return v4l2_subdev_call(sd, pad, dv_timings_cap, cap);
  332. }
  333. case VIDIOC_SUBDEV_ENUM_DV_TIMINGS: {
  334. struct v4l2_enum_dv_timings *dvt = arg;
  335. if (dvt->pad >= sd->entity.num_pads)
  336. return -EINVAL;
  337. return v4l2_subdev_call(sd, pad, enum_dv_timings, dvt);
  338. }
  339. case VIDIOC_SUBDEV_QUERY_DV_TIMINGS:
  340. return v4l2_subdev_call(sd, video, query_dv_timings, arg);
  341. case VIDIOC_SUBDEV_G_DV_TIMINGS:
  342. return v4l2_subdev_call(sd, video, g_dv_timings, arg);
  343. case VIDIOC_SUBDEV_S_DV_TIMINGS:
  344. return v4l2_subdev_call(sd, video, s_dv_timings, arg);
  345. #endif
  346. default:
  347. return v4l2_subdev_call(sd, core, ioctl, cmd, arg);
  348. }
  349. return 0;
  350. }
  351. static long subdev_ioctl(struct file *file, unsigned int cmd,
  352. unsigned long arg)
  353. {
  354. return video_usercopy(file, cmd, arg, subdev_do_ioctl);
  355. }
  356. #ifdef CONFIG_COMPAT
  357. static long subdev_compat_ioctl32(struct file *file, unsigned int cmd,
  358. unsigned long arg)
  359. {
  360. struct video_device *vdev = video_devdata(file);
  361. struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
  362. return v4l2_subdev_call(sd, core, compat_ioctl32, cmd, arg);
  363. }
  364. #endif
  365. static __poll_t subdev_poll(struct file *file, poll_table *wait)
  366. {
  367. struct video_device *vdev = video_devdata(file);
  368. struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
  369. struct v4l2_fh *fh = file->private_data;
  370. if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
  371. return POLLERR;
  372. poll_wait(file, &fh->wait, wait);
  373. if (v4l2_event_pending(fh))
  374. return POLLPRI;
  375. return 0;
  376. }
  377. const struct v4l2_file_operations v4l2_subdev_fops = {
  378. .owner = THIS_MODULE,
  379. .open = subdev_open,
  380. .unlocked_ioctl = subdev_ioctl,
  381. #ifdef CONFIG_COMPAT
  382. .compat_ioctl32 = subdev_compat_ioctl32,
  383. #endif
  384. .release = subdev_close,
  385. .poll = subdev_poll,
  386. };
  387. #ifdef CONFIG_MEDIA_CONTROLLER
  388. int v4l2_subdev_link_validate_default(struct v4l2_subdev *sd,
  389. struct media_link *link,
  390. struct v4l2_subdev_format *source_fmt,
  391. struct v4l2_subdev_format *sink_fmt)
  392. {
  393. /* The width, height and code must match. */
  394. if (source_fmt->format.width != sink_fmt->format.width
  395. || source_fmt->format.height != sink_fmt->format.height
  396. || source_fmt->format.code != sink_fmt->format.code)
  397. return -EPIPE;
  398. /* The field order must match, or the sink field order must be NONE
  399. * to support interlaced hardware connected to bridges that support
  400. * progressive formats only.
  401. */
  402. if (source_fmt->format.field != sink_fmt->format.field &&
  403. sink_fmt->format.field != V4L2_FIELD_NONE)
  404. return -EPIPE;
  405. return 0;
  406. }
  407. EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate_default);
  408. static int
  409. v4l2_subdev_link_validate_get_format(struct media_pad *pad,
  410. struct v4l2_subdev_format *fmt)
  411. {
  412. if (is_media_entity_v4l2_subdev(pad->entity)) {
  413. struct v4l2_subdev *sd =
  414. media_entity_to_v4l2_subdev(pad->entity);
  415. fmt->which = V4L2_SUBDEV_FORMAT_ACTIVE;
  416. fmt->pad = pad->index;
  417. return v4l2_subdev_call(sd, pad, get_fmt, NULL, fmt);
  418. }
  419. WARN(pad->entity->function != MEDIA_ENT_F_IO_V4L,
  420. "Driver bug! Wrong media entity type 0x%08x, entity %s\n",
  421. pad->entity->function, pad->entity->name);
  422. return -EINVAL;
  423. }
  424. int v4l2_subdev_link_validate(struct media_link *link)
  425. {
  426. struct v4l2_subdev *sink;
  427. struct v4l2_subdev_format sink_fmt, source_fmt;
  428. int rval;
  429. rval = v4l2_subdev_link_validate_get_format(
  430. link->source, &source_fmt);
  431. if (rval < 0)
  432. return 0;
  433. rval = v4l2_subdev_link_validate_get_format(
  434. link->sink, &sink_fmt);
  435. if (rval < 0)
  436. return 0;
  437. sink = media_entity_to_v4l2_subdev(link->sink->entity);
  438. rval = v4l2_subdev_call(sink, pad, link_validate, link,
  439. &source_fmt, &sink_fmt);
  440. if (rval != -ENOIOCTLCMD)
  441. return rval;
  442. return v4l2_subdev_link_validate_default(
  443. sink, link, &source_fmt, &sink_fmt);
  444. }
  445. EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate);
  446. struct v4l2_subdev_pad_config *
  447. v4l2_subdev_alloc_pad_config(struct v4l2_subdev *sd)
  448. {
  449. struct v4l2_subdev_pad_config *cfg;
  450. int ret;
  451. if (!sd->entity.num_pads)
  452. return NULL;
  453. cfg = kvmalloc_array(sd->entity.num_pads, sizeof(*cfg),
  454. GFP_KERNEL | __GFP_ZERO);
  455. if (!cfg)
  456. return NULL;
  457. ret = v4l2_subdev_call(sd, pad, init_cfg, cfg);
  458. if (ret < 0 && ret != -ENOIOCTLCMD) {
  459. kvfree(cfg);
  460. return NULL;
  461. }
  462. return cfg;
  463. }
  464. EXPORT_SYMBOL_GPL(v4l2_subdev_alloc_pad_config);
  465. void v4l2_subdev_free_pad_config(struct v4l2_subdev_pad_config *cfg)
  466. {
  467. kvfree(cfg);
  468. }
  469. EXPORT_SYMBOL_GPL(v4l2_subdev_free_pad_config);
  470. #endif /* CONFIG_MEDIA_CONTROLLER */
  471. void v4l2_subdev_init(struct v4l2_subdev *sd, const struct v4l2_subdev_ops *ops)
  472. {
  473. INIT_LIST_HEAD(&sd->list);
  474. BUG_ON(!ops);
  475. sd->ops = ops;
  476. sd->v4l2_dev = NULL;
  477. sd->flags = 0;
  478. sd->name[0] = '\0';
  479. sd->grp_id = 0;
  480. sd->dev_priv = NULL;
  481. sd->host_priv = NULL;
  482. #if defined(CONFIG_MEDIA_CONTROLLER)
  483. sd->entity.name = sd->name;
  484. sd->entity.obj_type = MEDIA_ENTITY_TYPE_V4L2_SUBDEV;
  485. sd->entity.function = MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN;
  486. #endif
  487. }
  488. EXPORT_SYMBOL(v4l2_subdev_init);
  489. void v4l2_subdev_notify_event(struct v4l2_subdev *sd,
  490. const struct v4l2_event *ev)
  491. {
  492. v4l2_event_queue(sd->devnode, ev);
  493. v4l2_subdev_notify(sd, V4L2_DEVICE_NOTIFY_EVENT, (void *)ev);
  494. }
  495. EXPORT_SYMBOL_GPL(v4l2_subdev_notify_event);