v4l2-subdev.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. /* Allocate try format and crop in the same memory block */
  36. fh->try_fmt = kzalloc((sizeof(*fh->try_fmt) + sizeof(*fh->try_crop))
  37. * sd->entity.num_pads, GFP_KERNEL);
  38. if (fh->try_fmt == NULL)
  39. return -ENOMEM;
  40. fh->try_crop = (struct v4l2_rect *)
  41. (fh->try_fmt + sd->entity.num_pads);
  42. #endif
  43. return 0;
  44. }
  45. static void subdev_fh_free(struct v4l2_subdev_fh *fh)
  46. {
  47. #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
  48. kfree(fh->try_fmt);
  49. fh->try_fmt = NULL;
  50. fh->try_crop = NULL;
  51. #endif
  52. }
  53. static int subdev_open(struct file *file)
  54. {
  55. struct video_device *vdev = video_devdata(file);
  56. struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
  57. struct v4l2_subdev_fh *subdev_fh;
  58. #if defined(CONFIG_MEDIA_CONTROLLER)
  59. struct media_entity *entity = NULL;
  60. #endif
  61. int ret;
  62. subdev_fh = kzalloc(sizeof(*subdev_fh), GFP_KERNEL);
  63. if (subdev_fh == NULL)
  64. return -ENOMEM;
  65. ret = subdev_fh_init(subdev_fh, sd);
  66. if (ret) {
  67. kfree(subdev_fh);
  68. return ret;
  69. }
  70. v4l2_fh_init(&subdev_fh->vfh, vdev);
  71. v4l2_fh_add(&subdev_fh->vfh);
  72. file->private_data = &subdev_fh->vfh;
  73. #if defined(CONFIG_MEDIA_CONTROLLER)
  74. if (sd->v4l2_dev->mdev) {
  75. entity = media_entity_get(&sd->entity);
  76. if (!entity) {
  77. ret = -EBUSY;
  78. goto err;
  79. }
  80. }
  81. #endif
  82. if (sd->internal_ops && sd->internal_ops->open) {
  83. ret = sd->internal_ops->open(sd, subdev_fh);
  84. if (ret < 0)
  85. goto err;
  86. }
  87. return 0;
  88. err:
  89. #if defined(CONFIG_MEDIA_CONTROLLER)
  90. if (entity)
  91. media_entity_put(entity);
  92. #endif
  93. v4l2_fh_del(&subdev_fh->vfh);
  94. v4l2_fh_exit(&subdev_fh->vfh);
  95. subdev_fh_free(subdev_fh);
  96. kfree(subdev_fh);
  97. return ret;
  98. }
  99. static int subdev_close(struct file *file)
  100. {
  101. struct video_device *vdev = video_devdata(file);
  102. struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
  103. struct v4l2_fh *vfh = file->private_data;
  104. struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh);
  105. if (sd->internal_ops && sd->internal_ops->close)
  106. sd->internal_ops->close(sd, subdev_fh);
  107. #if defined(CONFIG_MEDIA_CONTROLLER)
  108. if (sd->v4l2_dev->mdev)
  109. media_entity_put(&sd->entity);
  110. #endif
  111. v4l2_fh_del(vfh);
  112. v4l2_fh_exit(vfh);
  113. subdev_fh_free(subdev_fh);
  114. kfree(subdev_fh);
  115. file->private_data = NULL;
  116. return 0;
  117. }
  118. static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg)
  119. {
  120. struct video_device *vdev = video_devdata(file);
  121. struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
  122. struct v4l2_fh *vfh = file->private_data;
  123. #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
  124. struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh);
  125. #endif
  126. switch (cmd) {
  127. case VIDIOC_QUERYCTRL:
  128. return v4l2_queryctrl(vfh->ctrl_handler, arg);
  129. case VIDIOC_QUERYMENU:
  130. return v4l2_querymenu(vfh->ctrl_handler, arg);
  131. case VIDIOC_G_CTRL:
  132. return v4l2_g_ctrl(vfh->ctrl_handler, arg);
  133. case VIDIOC_S_CTRL:
  134. return v4l2_s_ctrl(vfh, vfh->ctrl_handler, arg);
  135. case VIDIOC_G_EXT_CTRLS:
  136. return v4l2_g_ext_ctrls(vfh->ctrl_handler, arg);
  137. case VIDIOC_S_EXT_CTRLS:
  138. return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler, arg);
  139. case VIDIOC_TRY_EXT_CTRLS:
  140. return v4l2_try_ext_ctrls(vfh->ctrl_handler, arg);
  141. case VIDIOC_DQEVENT:
  142. if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
  143. return -ENOIOCTLCMD;
  144. return v4l2_event_dequeue(vfh, arg, file->f_flags & O_NONBLOCK);
  145. case VIDIOC_SUBSCRIBE_EVENT:
  146. return v4l2_subdev_call(sd, core, subscribe_event, vfh, arg);
  147. case VIDIOC_UNSUBSCRIBE_EVENT:
  148. return v4l2_subdev_call(sd, core, unsubscribe_event, vfh, arg);
  149. #ifdef CONFIG_VIDEO_ADV_DEBUG
  150. case VIDIOC_DBG_G_REGISTER:
  151. {
  152. struct v4l2_dbg_register *p = arg;
  153. if (!capable(CAP_SYS_ADMIN))
  154. return -EPERM;
  155. return v4l2_subdev_call(sd, core, g_register, p);
  156. }
  157. case VIDIOC_DBG_S_REGISTER:
  158. {
  159. struct v4l2_dbg_register *p = arg;
  160. if (!capable(CAP_SYS_ADMIN))
  161. return -EPERM;
  162. return v4l2_subdev_call(sd, core, s_register, p);
  163. }
  164. #endif
  165. case VIDIOC_LOG_STATUS: {
  166. int ret;
  167. pr_info("%s: ================= START STATUS =================\n",
  168. sd->name);
  169. ret = v4l2_subdev_call(sd, core, log_status);
  170. pr_info("%s: ================== END STATUS ==================\n",
  171. sd->name);
  172. return ret;
  173. }
  174. #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
  175. case VIDIOC_SUBDEV_G_FMT: {
  176. struct v4l2_subdev_format *format = arg;
  177. if (format->which != V4L2_SUBDEV_FORMAT_TRY &&
  178. format->which != V4L2_SUBDEV_FORMAT_ACTIVE)
  179. return -EINVAL;
  180. if (format->pad >= sd->entity.num_pads)
  181. return -EINVAL;
  182. return v4l2_subdev_call(sd, pad, get_fmt, subdev_fh, format);
  183. }
  184. case VIDIOC_SUBDEV_S_FMT: {
  185. struct v4l2_subdev_format *format = arg;
  186. if (format->which != V4L2_SUBDEV_FORMAT_TRY &&
  187. format->which != V4L2_SUBDEV_FORMAT_ACTIVE)
  188. return -EINVAL;
  189. if (format->pad >= sd->entity.num_pads)
  190. return -EINVAL;
  191. return v4l2_subdev_call(sd, pad, set_fmt, subdev_fh, format);
  192. }
  193. case VIDIOC_SUBDEV_G_CROP: {
  194. struct v4l2_subdev_crop *crop = arg;
  195. if (crop->which != V4L2_SUBDEV_FORMAT_TRY &&
  196. crop->which != V4L2_SUBDEV_FORMAT_ACTIVE)
  197. return -EINVAL;
  198. if (crop->pad >= sd->entity.num_pads)
  199. return -EINVAL;
  200. return v4l2_subdev_call(sd, pad, get_crop, subdev_fh, crop);
  201. }
  202. case VIDIOC_SUBDEV_S_CROP: {
  203. struct v4l2_subdev_crop *crop = arg;
  204. if (crop->which != V4L2_SUBDEV_FORMAT_TRY &&
  205. crop->which != V4L2_SUBDEV_FORMAT_ACTIVE)
  206. return -EINVAL;
  207. if (crop->pad >= sd->entity.num_pads)
  208. return -EINVAL;
  209. return v4l2_subdev_call(sd, pad, set_crop, subdev_fh, crop);
  210. }
  211. case VIDIOC_SUBDEV_ENUM_MBUS_CODE: {
  212. struct v4l2_subdev_mbus_code_enum *code = arg;
  213. if (code->pad >= sd->entity.num_pads)
  214. return -EINVAL;
  215. return v4l2_subdev_call(sd, pad, enum_mbus_code, subdev_fh,
  216. code);
  217. }
  218. case VIDIOC_SUBDEV_ENUM_FRAME_SIZE: {
  219. struct v4l2_subdev_frame_size_enum *fse = arg;
  220. if (fse->pad >= sd->entity.num_pads)
  221. return -EINVAL;
  222. return v4l2_subdev_call(sd, pad, enum_frame_size, subdev_fh,
  223. fse);
  224. }
  225. case VIDIOC_SUBDEV_G_FRAME_INTERVAL:
  226. return v4l2_subdev_call(sd, video, g_frame_interval, arg);
  227. case VIDIOC_SUBDEV_S_FRAME_INTERVAL:
  228. return v4l2_subdev_call(sd, video, s_frame_interval, arg);
  229. case VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL: {
  230. struct v4l2_subdev_frame_interval_enum *fie = arg;
  231. if (fie->pad >= sd->entity.num_pads)
  232. return -EINVAL;
  233. return v4l2_subdev_call(sd, pad, enum_frame_interval, subdev_fh,
  234. fie);
  235. }
  236. #endif
  237. default:
  238. return v4l2_subdev_call(sd, core, ioctl, cmd, arg);
  239. }
  240. return 0;
  241. }
  242. static long subdev_ioctl(struct file *file, unsigned int cmd,
  243. unsigned long arg)
  244. {
  245. return video_usercopy(file, cmd, arg, subdev_do_ioctl);
  246. }
  247. static unsigned int subdev_poll(struct file *file, poll_table *wait)
  248. {
  249. struct video_device *vdev = video_devdata(file);
  250. struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
  251. struct v4l2_fh *fh = file->private_data;
  252. if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
  253. return POLLERR;
  254. poll_wait(file, &fh->wait, wait);
  255. if (v4l2_event_pending(fh))
  256. return POLLPRI;
  257. return 0;
  258. }
  259. const struct v4l2_file_operations v4l2_subdev_fops = {
  260. .owner = THIS_MODULE,
  261. .open = subdev_open,
  262. .unlocked_ioctl = subdev_ioctl,
  263. .release = subdev_close,
  264. .poll = subdev_poll,
  265. };
  266. void v4l2_subdev_init(struct v4l2_subdev *sd, const struct v4l2_subdev_ops *ops)
  267. {
  268. INIT_LIST_HEAD(&sd->list);
  269. BUG_ON(!ops);
  270. sd->ops = ops;
  271. sd->v4l2_dev = NULL;
  272. sd->flags = 0;
  273. sd->name[0] = '\0';
  274. sd->grp_id = 0;
  275. sd->dev_priv = NULL;
  276. sd->host_priv = NULL;
  277. #if defined(CONFIG_MEDIA_CONTROLLER)
  278. sd->entity.name = sd->name;
  279. sd->entity.type = MEDIA_ENT_T_V4L2_SUBDEV;
  280. #endif
  281. }
  282. EXPORT_SYMBOL(v4l2_subdev_init);