v4l2-subdev.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
  133. struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh);
  134. #endif
  135. switch (cmd) {
  136. case VIDIOC_QUERYCTRL:
  137. return v4l2_subdev_queryctrl(sd, arg);
  138. case VIDIOC_QUERYMENU:
  139. return v4l2_subdev_querymenu(sd, arg);
  140. case VIDIOC_G_CTRL:
  141. return v4l2_subdev_g_ctrl(sd, arg);
  142. case VIDIOC_S_CTRL:
  143. return v4l2_subdev_s_ctrl(sd, arg);
  144. case VIDIOC_G_EXT_CTRLS:
  145. return v4l2_subdev_g_ext_ctrls(sd, arg);
  146. case VIDIOC_S_EXT_CTRLS:
  147. return v4l2_subdev_s_ext_ctrls(sd, arg);
  148. case VIDIOC_TRY_EXT_CTRLS:
  149. return v4l2_subdev_try_ext_ctrls(sd, arg);
  150. case VIDIOC_DQEVENT:
  151. if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
  152. return -ENOIOCTLCMD;
  153. return v4l2_event_dequeue(vfh, arg, file->f_flags & O_NONBLOCK);
  154. case VIDIOC_SUBSCRIBE_EVENT:
  155. return v4l2_subdev_call(sd, core, subscribe_event, vfh, arg);
  156. case VIDIOC_UNSUBSCRIBE_EVENT:
  157. return v4l2_subdev_call(sd, core, unsubscribe_event, vfh, arg);
  158. #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
  159. case VIDIOC_SUBDEV_G_FMT: {
  160. struct v4l2_subdev_format *format = arg;
  161. if (format->which != V4L2_SUBDEV_FORMAT_TRY &&
  162. format->which != V4L2_SUBDEV_FORMAT_ACTIVE)
  163. return -EINVAL;
  164. if (format->pad >= sd->entity.num_pads)
  165. return -EINVAL;
  166. return v4l2_subdev_call(sd, pad, get_fmt, subdev_fh, format);
  167. }
  168. case VIDIOC_SUBDEV_S_FMT: {
  169. struct v4l2_subdev_format *format = arg;
  170. if (format->which != V4L2_SUBDEV_FORMAT_TRY &&
  171. format->which != V4L2_SUBDEV_FORMAT_ACTIVE)
  172. return -EINVAL;
  173. if (format->pad >= sd->entity.num_pads)
  174. return -EINVAL;
  175. return v4l2_subdev_call(sd, pad, set_fmt, subdev_fh, format);
  176. }
  177. case VIDIOC_SUBDEV_G_CROP: {
  178. struct v4l2_subdev_crop *crop = arg;
  179. if (crop->which != V4L2_SUBDEV_FORMAT_TRY &&
  180. crop->which != V4L2_SUBDEV_FORMAT_ACTIVE)
  181. return -EINVAL;
  182. if (crop->pad >= sd->entity.num_pads)
  183. return -EINVAL;
  184. return v4l2_subdev_call(sd, pad, get_crop, subdev_fh, crop);
  185. }
  186. case VIDIOC_SUBDEV_S_CROP: {
  187. struct v4l2_subdev_crop *crop = arg;
  188. if (crop->which != V4L2_SUBDEV_FORMAT_TRY &&
  189. crop->which != V4L2_SUBDEV_FORMAT_ACTIVE)
  190. return -EINVAL;
  191. if (crop->pad >= sd->entity.num_pads)
  192. return -EINVAL;
  193. return v4l2_subdev_call(sd, pad, set_crop, subdev_fh, crop);
  194. }
  195. case VIDIOC_SUBDEV_ENUM_MBUS_CODE: {
  196. struct v4l2_subdev_mbus_code_enum *code = arg;
  197. if (code->pad >= sd->entity.num_pads)
  198. return -EINVAL;
  199. return v4l2_subdev_call(sd, pad, enum_mbus_code, subdev_fh,
  200. code);
  201. }
  202. case VIDIOC_SUBDEV_ENUM_FRAME_SIZE: {
  203. struct v4l2_subdev_frame_size_enum *fse = arg;
  204. if (fse->pad >= sd->entity.num_pads)
  205. return -EINVAL;
  206. return v4l2_subdev_call(sd, pad, enum_frame_size, subdev_fh,
  207. fse);
  208. }
  209. case VIDIOC_SUBDEV_G_FRAME_INTERVAL:
  210. return v4l2_subdev_call(sd, video, g_frame_interval, arg);
  211. case VIDIOC_SUBDEV_S_FRAME_INTERVAL:
  212. return v4l2_subdev_call(sd, video, s_frame_interval, arg);
  213. case VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL: {
  214. struct v4l2_subdev_frame_interval_enum *fie = arg;
  215. if (fie->pad >= sd->entity.num_pads)
  216. return -EINVAL;
  217. return v4l2_subdev_call(sd, pad, enum_frame_interval, subdev_fh,
  218. fie);
  219. }
  220. #endif
  221. default:
  222. return v4l2_subdev_call(sd, core, ioctl, cmd, arg);
  223. }
  224. return 0;
  225. }
  226. static long subdev_ioctl(struct file *file, unsigned int cmd,
  227. unsigned long arg)
  228. {
  229. return video_usercopy(file, cmd, arg, subdev_do_ioctl);
  230. }
  231. static unsigned int subdev_poll(struct file *file, poll_table *wait)
  232. {
  233. struct video_device *vdev = video_devdata(file);
  234. struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
  235. struct v4l2_fh *fh = file->private_data;
  236. if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
  237. return POLLERR;
  238. poll_wait(file, &fh->events->wait, wait);
  239. if (v4l2_event_pending(fh))
  240. return POLLPRI;
  241. return 0;
  242. }
  243. const struct v4l2_file_operations v4l2_subdev_fops = {
  244. .owner = THIS_MODULE,
  245. .open = subdev_open,
  246. .unlocked_ioctl = subdev_ioctl,
  247. .release = subdev_close,
  248. .poll = subdev_poll,
  249. };
  250. void v4l2_subdev_init(struct v4l2_subdev *sd, const struct v4l2_subdev_ops *ops)
  251. {
  252. INIT_LIST_HEAD(&sd->list);
  253. BUG_ON(!ops);
  254. sd->ops = ops;
  255. sd->v4l2_dev = NULL;
  256. sd->flags = 0;
  257. sd->name[0] = '\0';
  258. sd->grp_id = 0;
  259. sd->dev_priv = NULL;
  260. sd->host_priv = NULL;
  261. #if defined(CONFIG_MEDIA_CONTROLLER)
  262. sd->entity.name = sd->name;
  263. sd->entity.type = MEDIA_ENT_T_V4L2_SUBDEV;
  264. #endif
  265. }
  266. EXPORT_SYMBOL(v4l2_subdev_init);