v4l2-subdev.c 13 KB

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