v4l2-subdev.c 17 KB

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