v4l2-subdev.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. ret = v4l2_fh_init(&subdev_fh->vfh, vdev);
  70. if (ret)
  71. goto err;
  72. if (sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS) {
  73. ret = v4l2_event_init(&subdev_fh->vfh);
  74. if (ret)
  75. goto err;
  76. ret = v4l2_event_alloc(&subdev_fh->vfh, sd->nevents);
  77. if (ret)
  78. goto err;
  79. }
  80. v4l2_fh_add(&subdev_fh->vfh);
  81. file->private_data = &subdev_fh->vfh;
  82. #if defined(CONFIG_MEDIA_CONTROLLER)
  83. if (sd->v4l2_dev->mdev) {
  84. entity = media_entity_get(&sd->entity);
  85. if (!entity) {
  86. ret = -EBUSY;
  87. goto err;
  88. }
  89. }
  90. #endif
  91. if (sd->internal_ops && sd->internal_ops->open) {
  92. ret = sd->internal_ops->open(sd, subdev_fh);
  93. if (ret < 0)
  94. goto err;
  95. }
  96. return 0;
  97. err:
  98. #if defined(CONFIG_MEDIA_CONTROLLER)
  99. if (entity)
  100. media_entity_put(entity);
  101. #endif
  102. v4l2_fh_del(&subdev_fh->vfh);
  103. v4l2_fh_exit(&subdev_fh->vfh);
  104. subdev_fh_free(subdev_fh);
  105. kfree(subdev_fh);
  106. return ret;
  107. }
  108. static int subdev_close(struct file *file)
  109. {
  110. struct video_device *vdev = video_devdata(file);
  111. struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
  112. struct v4l2_fh *vfh = file->private_data;
  113. struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh);
  114. if (sd->internal_ops && sd->internal_ops->close)
  115. sd->internal_ops->close(sd, subdev_fh);
  116. #if defined(CONFIG_MEDIA_CONTROLLER)
  117. if (sd->v4l2_dev->mdev)
  118. media_entity_put(&sd->entity);
  119. #endif
  120. v4l2_fh_del(vfh);
  121. v4l2_fh_exit(vfh);
  122. subdev_fh_free(subdev_fh);
  123. kfree(subdev_fh);
  124. file->private_data = NULL;
  125. return 0;
  126. }
  127. static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg)
  128. {
  129. struct video_device *vdev = video_devdata(file);
  130. struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
  131. struct v4l2_fh *vfh = file->private_data;
  132. switch (cmd) {
  133. case VIDIOC_QUERYCTRL:
  134. return v4l2_subdev_queryctrl(sd, arg);
  135. case VIDIOC_QUERYMENU:
  136. return v4l2_subdev_querymenu(sd, arg);
  137. case VIDIOC_G_CTRL:
  138. return v4l2_subdev_g_ctrl(sd, arg);
  139. case VIDIOC_S_CTRL:
  140. return v4l2_subdev_s_ctrl(sd, arg);
  141. case VIDIOC_G_EXT_CTRLS:
  142. return v4l2_subdev_g_ext_ctrls(sd, arg);
  143. case VIDIOC_S_EXT_CTRLS:
  144. return v4l2_subdev_s_ext_ctrls(sd, arg);
  145. case VIDIOC_TRY_EXT_CTRLS:
  146. return v4l2_subdev_try_ext_ctrls(sd, arg);
  147. case VIDIOC_DQEVENT:
  148. if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
  149. return -ENOIOCTLCMD;
  150. return v4l2_event_dequeue(vfh, arg, file->f_flags & O_NONBLOCK);
  151. case VIDIOC_SUBSCRIBE_EVENT:
  152. return v4l2_subdev_call(sd, core, subscribe_event, vfh, arg);
  153. case VIDIOC_UNSUBSCRIBE_EVENT:
  154. return v4l2_subdev_call(sd, core, unsubscribe_event, vfh, arg);
  155. default:
  156. return -ENOIOCTLCMD;
  157. }
  158. return 0;
  159. }
  160. static long subdev_ioctl(struct file *file, unsigned int cmd,
  161. unsigned long arg)
  162. {
  163. return video_usercopy(file, cmd, arg, subdev_do_ioctl);
  164. }
  165. static unsigned int subdev_poll(struct file *file, poll_table *wait)
  166. {
  167. struct video_device *vdev = video_devdata(file);
  168. struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
  169. struct v4l2_fh *fh = file->private_data;
  170. if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
  171. return POLLERR;
  172. poll_wait(file, &fh->events->wait, wait);
  173. if (v4l2_event_pending(fh))
  174. return POLLPRI;
  175. return 0;
  176. }
  177. const struct v4l2_file_operations v4l2_subdev_fops = {
  178. .owner = THIS_MODULE,
  179. .open = subdev_open,
  180. .unlocked_ioctl = subdev_ioctl,
  181. .release = subdev_close,
  182. .poll = subdev_poll,
  183. };
  184. void v4l2_subdev_init(struct v4l2_subdev *sd, const struct v4l2_subdev_ops *ops)
  185. {
  186. INIT_LIST_HEAD(&sd->list);
  187. BUG_ON(!ops);
  188. sd->ops = ops;
  189. sd->v4l2_dev = NULL;
  190. sd->flags = 0;
  191. sd->name[0] = '\0';
  192. sd->grp_id = 0;
  193. sd->dev_priv = NULL;
  194. sd->host_priv = NULL;
  195. #if defined(CONFIG_MEDIA_CONTROLLER)
  196. sd->entity.name = sd->name;
  197. sd->entity.type = MEDIA_ENT_T_V4L2_SUBDEV;
  198. #endif
  199. }
  200. EXPORT_SYMBOL(v4l2_subdev_init);