v4l2-subdev.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  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. /*
  162. * TODO: this really should be folded into v4l2_queryctrl (this
  163. * currently returns -EINVAL for NULL control handlers).
  164. * However, v4l2_queryctrl() is still called directly by
  165. * drivers as well and until that has been addressed I believe
  166. * it is safer to do the check here. The same is true for the
  167. * other control ioctls below.
  168. */
  169. if (!vfh->ctrl_handler)
  170. return -ENOTTY;
  171. return v4l2_queryctrl(vfh->ctrl_handler, arg);
  172. case VIDIOC_QUERY_EXT_CTRL:
  173. if (!vfh->ctrl_handler)
  174. return -ENOTTY;
  175. return v4l2_query_ext_ctrl(vfh->ctrl_handler, arg);
  176. case VIDIOC_QUERYMENU:
  177. if (!vfh->ctrl_handler)
  178. return -ENOTTY;
  179. return v4l2_querymenu(vfh->ctrl_handler, arg);
  180. case VIDIOC_G_CTRL:
  181. if (!vfh->ctrl_handler)
  182. return -ENOTTY;
  183. return v4l2_g_ctrl(vfh->ctrl_handler, arg);
  184. case VIDIOC_S_CTRL:
  185. if (!vfh->ctrl_handler)
  186. return -ENOTTY;
  187. return v4l2_s_ctrl(vfh, vfh->ctrl_handler, arg);
  188. case VIDIOC_G_EXT_CTRLS:
  189. if (!vfh->ctrl_handler)
  190. return -ENOTTY;
  191. return v4l2_g_ext_ctrls(vfh->ctrl_handler,
  192. sd->v4l2_dev->mdev, arg);
  193. case VIDIOC_S_EXT_CTRLS:
  194. if (!vfh->ctrl_handler)
  195. return -ENOTTY;
  196. return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler,
  197. sd->v4l2_dev->mdev, arg);
  198. case VIDIOC_TRY_EXT_CTRLS:
  199. if (!vfh->ctrl_handler)
  200. return -ENOTTY;
  201. return v4l2_try_ext_ctrls(vfh->ctrl_handler,
  202. sd->v4l2_dev->mdev, arg);
  203. case VIDIOC_DQEVENT:
  204. if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
  205. return -ENOIOCTLCMD;
  206. return v4l2_event_dequeue(vfh, arg, file->f_flags & O_NONBLOCK);
  207. case VIDIOC_SUBSCRIBE_EVENT:
  208. return v4l2_subdev_call(sd, core, subscribe_event, vfh, arg);
  209. case VIDIOC_UNSUBSCRIBE_EVENT:
  210. return v4l2_subdev_call(sd, core, unsubscribe_event, vfh, arg);
  211. #ifdef CONFIG_VIDEO_ADV_DEBUG
  212. case VIDIOC_DBG_G_REGISTER:
  213. {
  214. struct v4l2_dbg_register *p = arg;
  215. if (!capable(CAP_SYS_ADMIN))
  216. return -EPERM;
  217. return v4l2_subdev_call(sd, core, g_register, p);
  218. }
  219. case VIDIOC_DBG_S_REGISTER:
  220. {
  221. struct v4l2_dbg_register *p = arg;
  222. if (!capable(CAP_SYS_ADMIN))
  223. return -EPERM;
  224. return v4l2_subdev_call(sd, core, s_register, p);
  225. }
  226. case VIDIOC_DBG_G_CHIP_INFO:
  227. {
  228. struct v4l2_dbg_chip_info *p = arg;
  229. if (p->match.type != V4L2_CHIP_MATCH_SUBDEV || p->match.addr)
  230. return -EINVAL;
  231. if (sd->ops->core && sd->ops->core->s_register)
  232. p->flags |= V4L2_CHIP_FL_WRITABLE;
  233. if (sd->ops->core && sd->ops->core->g_register)
  234. p->flags |= V4L2_CHIP_FL_READABLE;
  235. strscpy(p->name, sd->name, sizeof(p->name));
  236. return 0;
  237. }
  238. #endif
  239. case VIDIOC_LOG_STATUS: {
  240. int ret;
  241. pr_info("%s: ================= START STATUS =================\n",
  242. sd->name);
  243. ret = v4l2_subdev_call(sd, core, log_status);
  244. pr_info("%s: ================== END STATUS ==================\n",
  245. sd->name);
  246. return ret;
  247. }
  248. #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
  249. case VIDIOC_SUBDEV_G_FMT: {
  250. struct v4l2_subdev_format *format = arg;
  251. rval = check_format(sd, format);
  252. if (rval)
  253. return rval;
  254. memset(format->reserved, 0, sizeof(format->reserved));
  255. memset(format->format.reserved, 0, sizeof(format->format.reserved));
  256. return v4l2_subdev_call(sd, pad, get_fmt, subdev_fh->pad, format);
  257. }
  258. case VIDIOC_SUBDEV_S_FMT: {
  259. struct v4l2_subdev_format *format = arg;
  260. rval = check_format(sd, format);
  261. if (rval)
  262. return rval;
  263. memset(format->reserved, 0, sizeof(format->reserved));
  264. memset(format->format.reserved, 0, sizeof(format->format.reserved));
  265. return v4l2_subdev_call(sd, pad, set_fmt, subdev_fh->pad, format);
  266. }
  267. case VIDIOC_SUBDEV_G_CROP: {
  268. struct v4l2_subdev_crop *crop = arg;
  269. struct v4l2_subdev_selection sel;
  270. rval = check_crop(sd, crop);
  271. if (rval)
  272. return rval;
  273. memset(crop->reserved, 0, sizeof(crop->reserved));
  274. memset(&sel, 0, sizeof(sel));
  275. sel.which = crop->which;
  276. sel.pad = crop->pad;
  277. sel.target = V4L2_SEL_TGT_CROP;
  278. rval = v4l2_subdev_call(
  279. sd, pad, get_selection, subdev_fh->pad, &sel);
  280. crop->rect = sel.r;
  281. return rval;
  282. }
  283. case VIDIOC_SUBDEV_S_CROP: {
  284. struct v4l2_subdev_crop *crop = arg;
  285. struct v4l2_subdev_selection sel;
  286. memset(crop->reserved, 0, sizeof(crop->reserved));
  287. rval = check_crop(sd, crop);
  288. if (rval)
  289. return rval;
  290. memset(&sel, 0, sizeof(sel));
  291. sel.which = crop->which;
  292. sel.pad = crop->pad;
  293. sel.target = V4L2_SEL_TGT_CROP;
  294. sel.r = crop->rect;
  295. rval = v4l2_subdev_call(
  296. sd, pad, set_selection, subdev_fh->pad, &sel);
  297. crop->rect = sel.r;
  298. return rval;
  299. }
  300. case VIDIOC_SUBDEV_ENUM_MBUS_CODE: {
  301. struct v4l2_subdev_mbus_code_enum *code = arg;
  302. if (code->which != V4L2_SUBDEV_FORMAT_TRY &&
  303. code->which != V4L2_SUBDEV_FORMAT_ACTIVE)
  304. return -EINVAL;
  305. if (code->pad >= sd->entity.num_pads)
  306. return -EINVAL;
  307. memset(code->reserved, 0, sizeof(code->reserved));
  308. return v4l2_subdev_call(sd, pad, enum_mbus_code, subdev_fh->pad,
  309. code);
  310. }
  311. case VIDIOC_SUBDEV_ENUM_FRAME_SIZE: {
  312. struct v4l2_subdev_frame_size_enum *fse = arg;
  313. if (fse->which != V4L2_SUBDEV_FORMAT_TRY &&
  314. fse->which != V4L2_SUBDEV_FORMAT_ACTIVE)
  315. return -EINVAL;
  316. if (fse->pad >= sd->entity.num_pads)
  317. return -EINVAL;
  318. memset(fse->reserved, 0, sizeof(fse->reserved));
  319. return v4l2_subdev_call(sd, pad, enum_frame_size, subdev_fh->pad,
  320. fse);
  321. }
  322. case VIDIOC_SUBDEV_G_FRAME_INTERVAL: {
  323. struct v4l2_subdev_frame_interval *fi = arg;
  324. if (fi->pad >= sd->entity.num_pads)
  325. return -EINVAL;
  326. memset(fi->reserved, 0, sizeof(fi->reserved));
  327. return v4l2_subdev_call(sd, video, g_frame_interval, arg);
  328. }
  329. case VIDIOC_SUBDEV_S_FRAME_INTERVAL: {
  330. struct v4l2_subdev_frame_interval *fi = arg;
  331. if (fi->pad >= sd->entity.num_pads)
  332. return -EINVAL;
  333. memset(fi->reserved, 0, sizeof(fi->reserved));
  334. return v4l2_subdev_call(sd, video, s_frame_interval, arg);
  335. }
  336. case VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL: {
  337. struct v4l2_subdev_frame_interval_enum *fie = arg;
  338. if (fie->which != V4L2_SUBDEV_FORMAT_TRY &&
  339. fie->which != V4L2_SUBDEV_FORMAT_ACTIVE)
  340. return -EINVAL;
  341. if (fie->pad >= sd->entity.num_pads)
  342. return -EINVAL;
  343. memset(fie->reserved, 0, sizeof(fie->reserved));
  344. return v4l2_subdev_call(sd, pad, enum_frame_interval, subdev_fh->pad,
  345. fie);
  346. }
  347. case VIDIOC_SUBDEV_G_SELECTION: {
  348. struct v4l2_subdev_selection *sel = arg;
  349. rval = check_selection(sd, sel);
  350. if (rval)
  351. return rval;
  352. memset(sel->reserved, 0, sizeof(sel->reserved));
  353. return v4l2_subdev_call(
  354. sd, pad, get_selection, subdev_fh->pad, sel);
  355. }
  356. case VIDIOC_SUBDEV_S_SELECTION: {
  357. struct v4l2_subdev_selection *sel = arg;
  358. rval = check_selection(sd, sel);
  359. if (rval)
  360. return rval;
  361. memset(sel->reserved, 0, sizeof(sel->reserved));
  362. return v4l2_subdev_call(
  363. sd, pad, set_selection, subdev_fh->pad, sel);
  364. }
  365. case VIDIOC_G_EDID: {
  366. struct v4l2_subdev_edid *edid = arg;
  367. rval = check_edid(sd, edid);
  368. if (rval)
  369. return rval;
  370. return v4l2_subdev_call(sd, pad, get_edid, edid);
  371. }
  372. case VIDIOC_S_EDID: {
  373. struct v4l2_subdev_edid *edid = arg;
  374. rval = check_edid(sd, edid);
  375. if (rval)
  376. return rval;
  377. return v4l2_subdev_call(sd, pad, set_edid, edid);
  378. }
  379. case VIDIOC_SUBDEV_DV_TIMINGS_CAP: {
  380. struct v4l2_dv_timings_cap *cap = arg;
  381. if (cap->pad >= sd->entity.num_pads)
  382. return -EINVAL;
  383. return v4l2_subdev_call(sd, pad, dv_timings_cap, cap);
  384. }
  385. case VIDIOC_SUBDEV_ENUM_DV_TIMINGS: {
  386. struct v4l2_enum_dv_timings *dvt = arg;
  387. if (dvt->pad >= sd->entity.num_pads)
  388. return -EINVAL;
  389. return v4l2_subdev_call(sd, pad, enum_dv_timings, dvt);
  390. }
  391. case VIDIOC_SUBDEV_QUERY_DV_TIMINGS:
  392. return v4l2_subdev_call(sd, video, query_dv_timings, arg);
  393. case VIDIOC_SUBDEV_G_DV_TIMINGS:
  394. return v4l2_subdev_call(sd, video, g_dv_timings, arg);
  395. case VIDIOC_SUBDEV_S_DV_TIMINGS:
  396. return v4l2_subdev_call(sd, video, s_dv_timings, arg);
  397. case VIDIOC_SUBDEV_G_STD:
  398. return v4l2_subdev_call(sd, video, g_std, arg);
  399. case VIDIOC_SUBDEV_S_STD: {
  400. v4l2_std_id *std = arg;
  401. return v4l2_subdev_call(sd, video, s_std, *std);
  402. }
  403. case VIDIOC_SUBDEV_ENUMSTD: {
  404. struct v4l2_standard *p = arg;
  405. v4l2_std_id id;
  406. if (v4l2_subdev_call(sd, video, g_tvnorms, &id))
  407. return -EINVAL;
  408. return v4l_video_std_enumstd(p, id);
  409. }
  410. case VIDIOC_SUBDEV_QUERYSTD:
  411. return v4l2_subdev_call(sd, video, querystd, arg);
  412. #endif
  413. default:
  414. return v4l2_subdev_call(sd, core, ioctl, cmd, arg);
  415. }
  416. return 0;
  417. }
  418. static long subdev_do_ioctl_lock(struct file *file, unsigned int cmd, void *arg)
  419. {
  420. struct video_device *vdev = video_devdata(file);
  421. struct mutex *lock = vdev->lock;
  422. long ret = -ENODEV;
  423. if (lock && mutex_lock_interruptible(lock))
  424. return -ERESTARTSYS;
  425. if (video_is_registered(vdev))
  426. ret = subdev_do_ioctl(file, cmd, arg);
  427. if (lock)
  428. mutex_unlock(lock);
  429. return ret;
  430. }
  431. static long subdev_ioctl(struct file *file, unsigned int cmd,
  432. unsigned long arg)
  433. {
  434. return video_usercopy(file, cmd, arg, subdev_do_ioctl_lock);
  435. }
  436. #ifdef CONFIG_COMPAT
  437. static long subdev_compat_ioctl32(struct file *file, unsigned int cmd,
  438. unsigned long arg)
  439. {
  440. struct video_device *vdev = video_devdata(file);
  441. struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
  442. return v4l2_subdev_call(sd, core, compat_ioctl32, cmd, arg);
  443. }
  444. #endif
  445. static __poll_t subdev_poll(struct file *file, poll_table *wait)
  446. {
  447. struct video_device *vdev = video_devdata(file);
  448. struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
  449. struct v4l2_fh *fh = file->private_data;
  450. if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
  451. return EPOLLERR;
  452. poll_wait(file, &fh->wait, wait);
  453. if (v4l2_event_pending(fh))
  454. return EPOLLPRI;
  455. return 0;
  456. }
  457. const struct v4l2_file_operations v4l2_subdev_fops = {
  458. .owner = THIS_MODULE,
  459. .open = subdev_open,
  460. .unlocked_ioctl = subdev_ioctl,
  461. #ifdef CONFIG_COMPAT
  462. .compat_ioctl32 = subdev_compat_ioctl32,
  463. #endif
  464. .release = subdev_close,
  465. .poll = subdev_poll,
  466. };
  467. #ifdef CONFIG_MEDIA_CONTROLLER
  468. int v4l2_subdev_link_validate_default(struct v4l2_subdev *sd,
  469. struct media_link *link,
  470. struct v4l2_subdev_format *source_fmt,
  471. struct v4l2_subdev_format *sink_fmt)
  472. {
  473. /* The width, height and code must match. */
  474. if (source_fmt->format.width != sink_fmt->format.width
  475. || source_fmt->format.height != sink_fmt->format.height
  476. || source_fmt->format.code != sink_fmt->format.code)
  477. return -EPIPE;
  478. /* The field order must match, or the sink field order must be NONE
  479. * to support interlaced hardware connected to bridges that support
  480. * progressive formats only.
  481. */
  482. if (source_fmt->format.field != sink_fmt->format.field &&
  483. sink_fmt->format.field != V4L2_FIELD_NONE)
  484. return -EPIPE;
  485. return 0;
  486. }
  487. EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate_default);
  488. static int
  489. v4l2_subdev_link_validate_get_format(struct media_pad *pad,
  490. struct v4l2_subdev_format *fmt)
  491. {
  492. if (is_media_entity_v4l2_subdev(pad->entity)) {
  493. struct v4l2_subdev *sd =
  494. media_entity_to_v4l2_subdev(pad->entity);
  495. fmt->which = V4L2_SUBDEV_FORMAT_ACTIVE;
  496. fmt->pad = pad->index;
  497. return v4l2_subdev_call(sd, pad, get_fmt, NULL, fmt);
  498. }
  499. WARN(pad->entity->function != MEDIA_ENT_F_IO_V4L,
  500. "Driver bug! Wrong media entity type 0x%08x, entity %s\n",
  501. pad->entity->function, pad->entity->name);
  502. return -EINVAL;
  503. }
  504. int v4l2_subdev_link_validate(struct media_link *link)
  505. {
  506. struct v4l2_subdev *sink;
  507. struct v4l2_subdev_format sink_fmt, source_fmt;
  508. int rval;
  509. rval = v4l2_subdev_link_validate_get_format(
  510. link->source, &source_fmt);
  511. if (rval < 0)
  512. return 0;
  513. rval = v4l2_subdev_link_validate_get_format(
  514. link->sink, &sink_fmt);
  515. if (rval < 0)
  516. return 0;
  517. sink = media_entity_to_v4l2_subdev(link->sink->entity);
  518. rval = v4l2_subdev_call(sink, pad, link_validate, link,
  519. &source_fmt, &sink_fmt);
  520. if (rval != -ENOIOCTLCMD)
  521. return rval;
  522. return v4l2_subdev_link_validate_default(
  523. sink, link, &source_fmt, &sink_fmt);
  524. }
  525. EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate);
  526. struct v4l2_subdev_pad_config *
  527. v4l2_subdev_alloc_pad_config(struct v4l2_subdev *sd)
  528. {
  529. struct v4l2_subdev_pad_config *cfg;
  530. int ret;
  531. if (!sd->entity.num_pads)
  532. return NULL;
  533. cfg = kvmalloc_array(sd->entity.num_pads, sizeof(*cfg),
  534. GFP_KERNEL | __GFP_ZERO);
  535. if (!cfg)
  536. return NULL;
  537. ret = v4l2_subdev_call(sd, pad, init_cfg, cfg);
  538. if (ret < 0 && ret != -ENOIOCTLCMD) {
  539. kvfree(cfg);
  540. return NULL;
  541. }
  542. return cfg;
  543. }
  544. EXPORT_SYMBOL_GPL(v4l2_subdev_alloc_pad_config);
  545. void v4l2_subdev_free_pad_config(struct v4l2_subdev_pad_config *cfg)
  546. {
  547. kvfree(cfg);
  548. }
  549. EXPORT_SYMBOL_GPL(v4l2_subdev_free_pad_config);
  550. #endif /* CONFIG_MEDIA_CONTROLLER */
  551. void v4l2_subdev_init(struct v4l2_subdev *sd, const struct v4l2_subdev_ops *ops)
  552. {
  553. INIT_LIST_HEAD(&sd->list);
  554. BUG_ON(!ops);
  555. sd->ops = ops;
  556. sd->v4l2_dev = NULL;
  557. sd->flags = 0;
  558. sd->name[0] = '\0';
  559. sd->grp_id = 0;
  560. sd->dev_priv = NULL;
  561. sd->host_priv = NULL;
  562. #if defined(CONFIG_MEDIA_CONTROLLER)
  563. sd->entity.name = sd->name;
  564. sd->entity.obj_type = MEDIA_ENTITY_TYPE_V4L2_SUBDEV;
  565. sd->entity.function = MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN;
  566. #endif
  567. }
  568. EXPORT_SYMBOL(v4l2_subdev_init);
  569. void v4l2_subdev_notify_event(struct v4l2_subdev *sd,
  570. const struct v4l2_event *ev)
  571. {
  572. v4l2_event_queue(sd->devnode, ev);
  573. v4l2_subdev_notify(sd, V4L2_DEVICE_NOTIFY_EVENT, (void *)ev);
  574. }
  575. EXPORT_SYMBOL_GPL(v4l2_subdev_notify_event);