v4l2-subdev.c 7.8 KB

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