v4l2-subdev.c 15 KB

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