v4l2-subdev.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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_QUERYMENU:
  125. return v4l2_querymenu(vfh->ctrl_handler, arg);
  126. case VIDIOC_G_CTRL:
  127. return v4l2_g_ctrl(vfh->ctrl_handler, arg);
  128. case VIDIOC_S_CTRL:
  129. return v4l2_s_ctrl(vfh, vfh->ctrl_handler, arg);
  130. case VIDIOC_G_EXT_CTRLS:
  131. return v4l2_g_ext_ctrls(vfh->ctrl_handler, arg);
  132. case VIDIOC_S_EXT_CTRLS:
  133. return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler, arg);
  134. case VIDIOC_TRY_EXT_CTRLS:
  135. return v4l2_try_ext_ctrls(vfh->ctrl_handler, arg);
  136. case VIDIOC_DQEVENT:
  137. if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
  138. return -ENOIOCTLCMD;
  139. return v4l2_event_dequeue(vfh, arg, file->f_flags & O_NONBLOCK);
  140. case VIDIOC_SUBSCRIBE_EVENT:
  141. return v4l2_subdev_call(sd, core, subscribe_event, vfh, arg);
  142. case VIDIOC_UNSUBSCRIBE_EVENT:
  143. return v4l2_subdev_call(sd, core, unsubscribe_event, vfh, arg);
  144. #ifdef CONFIG_VIDEO_ADV_DEBUG
  145. case VIDIOC_DBG_G_REGISTER:
  146. {
  147. struct v4l2_dbg_register *p = arg;
  148. if (!capable(CAP_SYS_ADMIN))
  149. return -EPERM;
  150. return v4l2_subdev_call(sd, core, g_register, p);
  151. }
  152. case VIDIOC_DBG_S_REGISTER:
  153. {
  154. struct v4l2_dbg_register *p = arg;
  155. if (!capable(CAP_SYS_ADMIN))
  156. return -EPERM;
  157. return v4l2_subdev_call(sd, core, s_register, p);
  158. }
  159. #endif
  160. case VIDIOC_LOG_STATUS: {
  161. int ret;
  162. pr_info("%s: ================= START STATUS =================\n",
  163. sd->name);
  164. ret = v4l2_subdev_call(sd, core, log_status);
  165. pr_info("%s: ================== END STATUS ==================\n",
  166. sd->name);
  167. return ret;
  168. }
  169. #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
  170. case VIDIOC_SUBDEV_G_FMT: {
  171. struct v4l2_subdev_format *format = arg;
  172. if (format->which != V4L2_SUBDEV_FORMAT_TRY &&
  173. format->which != V4L2_SUBDEV_FORMAT_ACTIVE)
  174. return -EINVAL;
  175. if (format->pad >= sd->entity.num_pads)
  176. return -EINVAL;
  177. return v4l2_subdev_call(sd, pad, get_fmt, subdev_fh, format);
  178. }
  179. case VIDIOC_SUBDEV_S_FMT: {
  180. struct v4l2_subdev_format *format = arg;
  181. if (format->which != V4L2_SUBDEV_FORMAT_TRY &&
  182. format->which != V4L2_SUBDEV_FORMAT_ACTIVE)
  183. return -EINVAL;
  184. if (format->pad >= sd->entity.num_pads)
  185. return -EINVAL;
  186. return v4l2_subdev_call(sd, pad, set_fmt, subdev_fh, format);
  187. }
  188. case VIDIOC_SUBDEV_G_CROP: {
  189. struct v4l2_subdev_crop *crop = arg;
  190. struct v4l2_subdev_selection sel;
  191. int rval;
  192. if (crop->which != V4L2_SUBDEV_FORMAT_TRY &&
  193. crop->which != V4L2_SUBDEV_FORMAT_ACTIVE)
  194. return -EINVAL;
  195. if (crop->pad >= sd->entity.num_pads)
  196. return -EINVAL;
  197. rval = v4l2_subdev_call(sd, pad, get_crop, subdev_fh, crop);
  198. if (rval != -ENOIOCTLCMD)
  199. return rval;
  200. memset(&sel, 0, sizeof(sel));
  201. sel.which = crop->which;
  202. sel.pad = crop->pad;
  203. sel.target = V4L2_SEL_TGT_CROP;
  204. rval = v4l2_subdev_call(
  205. sd, pad, get_selection, subdev_fh, &sel);
  206. crop->rect = sel.r;
  207. return rval;
  208. }
  209. case VIDIOC_SUBDEV_S_CROP: {
  210. struct v4l2_subdev_crop *crop = arg;
  211. struct v4l2_subdev_selection sel;
  212. int rval;
  213. if (crop->which != V4L2_SUBDEV_FORMAT_TRY &&
  214. crop->which != V4L2_SUBDEV_FORMAT_ACTIVE)
  215. return -EINVAL;
  216. if (crop->pad >= sd->entity.num_pads)
  217. return -EINVAL;
  218. rval = v4l2_subdev_call(sd, pad, set_crop, subdev_fh, crop);
  219. if (rval != -ENOIOCTLCMD)
  220. return rval;
  221. memset(&sel, 0, sizeof(sel));
  222. sel.which = crop->which;
  223. sel.pad = crop->pad;
  224. sel.target = V4L2_SEL_TGT_CROP;
  225. sel.r = crop->rect;
  226. rval = v4l2_subdev_call(
  227. sd, pad, set_selection, subdev_fh, &sel);
  228. crop->rect = sel.r;
  229. return rval;
  230. }
  231. case VIDIOC_SUBDEV_ENUM_MBUS_CODE: {
  232. struct v4l2_subdev_mbus_code_enum *code = arg;
  233. if (code->pad >= sd->entity.num_pads)
  234. return -EINVAL;
  235. return v4l2_subdev_call(sd, pad, enum_mbus_code, subdev_fh,
  236. code);
  237. }
  238. case VIDIOC_SUBDEV_ENUM_FRAME_SIZE: {
  239. struct v4l2_subdev_frame_size_enum *fse = arg;
  240. if (fse->pad >= sd->entity.num_pads)
  241. return -EINVAL;
  242. return v4l2_subdev_call(sd, pad, enum_frame_size, subdev_fh,
  243. fse);
  244. }
  245. case VIDIOC_SUBDEV_G_FRAME_INTERVAL:
  246. return v4l2_subdev_call(sd, video, g_frame_interval, arg);
  247. case VIDIOC_SUBDEV_S_FRAME_INTERVAL:
  248. return v4l2_subdev_call(sd, video, s_frame_interval, arg);
  249. case VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL: {
  250. struct v4l2_subdev_frame_interval_enum *fie = arg;
  251. if (fie->pad >= sd->entity.num_pads)
  252. return -EINVAL;
  253. return v4l2_subdev_call(sd, pad, enum_frame_interval, subdev_fh,
  254. fie);
  255. }
  256. case VIDIOC_SUBDEV_G_SELECTION: {
  257. struct v4l2_subdev_selection *sel = arg;
  258. if (sel->which != V4L2_SUBDEV_FORMAT_TRY &&
  259. sel->which != V4L2_SUBDEV_FORMAT_ACTIVE)
  260. return -EINVAL;
  261. if (sel->pad >= sd->entity.num_pads)
  262. return -EINVAL;
  263. return v4l2_subdev_call(
  264. sd, pad, get_selection, subdev_fh, sel);
  265. }
  266. case VIDIOC_SUBDEV_S_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, set_selection, subdev_fh, sel);
  275. }
  276. #endif
  277. default:
  278. return v4l2_subdev_call(sd, core, ioctl, cmd, arg);
  279. }
  280. return 0;
  281. }
  282. static long subdev_ioctl(struct file *file, unsigned int cmd,
  283. unsigned long arg)
  284. {
  285. return video_usercopy(file, cmd, arg, subdev_do_ioctl);
  286. }
  287. static unsigned int subdev_poll(struct file *file, poll_table *wait)
  288. {
  289. struct video_device *vdev = video_devdata(file);
  290. struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
  291. struct v4l2_fh *fh = file->private_data;
  292. if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
  293. return POLLERR;
  294. poll_wait(file, &fh->wait, wait);
  295. if (v4l2_event_pending(fh))
  296. return POLLPRI;
  297. return 0;
  298. }
  299. const struct v4l2_file_operations v4l2_subdev_fops = {
  300. .owner = THIS_MODULE,
  301. .open = subdev_open,
  302. .unlocked_ioctl = subdev_ioctl,
  303. .release = subdev_close,
  304. .poll = subdev_poll,
  305. };
  306. #ifdef CONFIG_MEDIA_CONTROLLER
  307. int v4l2_subdev_link_validate_default(struct v4l2_subdev *sd,
  308. struct media_link *link,
  309. struct v4l2_subdev_format *source_fmt,
  310. struct v4l2_subdev_format *sink_fmt)
  311. {
  312. if (source_fmt->format.width != sink_fmt->format.width
  313. || source_fmt->format.height != sink_fmt->format.height
  314. || source_fmt->format.code != sink_fmt->format.code)
  315. return -EINVAL;
  316. return 0;
  317. }
  318. EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate_default);
  319. static int
  320. v4l2_subdev_link_validate_get_format(struct media_pad *pad,
  321. struct v4l2_subdev_format *fmt)
  322. {
  323. switch (media_entity_type(pad->entity)) {
  324. case MEDIA_ENT_T_V4L2_SUBDEV:
  325. fmt->which = V4L2_SUBDEV_FORMAT_ACTIVE;
  326. fmt->pad = pad->index;
  327. return v4l2_subdev_call(media_entity_to_v4l2_subdev(
  328. pad->entity),
  329. pad, get_fmt, NULL, fmt);
  330. default:
  331. WARN(1, "Driver bug! Wrong media entity type %d, entity %s\n",
  332. media_entity_type(pad->entity), pad->entity->name);
  333. /* Fall through */
  334. case MEDIA_ENT_T_DEVNODE_V4L:
  335. return -EINVAL;
  336. }
  337. }
  338. int v4l2_subdev_link_validate(struct media_link *link)
  339. {
  340. struct v4l2_subdev *sink;
  341. struct v4l2_subdev_format sink_fmt, source_fmt;
  342. int rval;
  343. rval = v4l2_subdev_link_validate_get_format(
  344. link->source, &source_fmt);
  345. if (rval < 0)
  346. return 0;
  347. rval = v4l2_subdev_link_validate_get_format(
  348. link->sink, &sink_fmt);
  349. if (rval < 0)
  350. return 0;
  351. sink = media_entity_to_v4l2_subdev(link->sink->entity);
  352. rval = v4l2_subdev_call(sink, pad, link_validate, link,
  353. &source_fmt, &sink_fmt);
  354. if (rval != -ENOIOCTLCMD)
  355. return rval;
  356. return v4l2_subdev_link_validate_default(
  357. sink, link, &source_fmt, &sink_fmt);
  358. }
  359. EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate);
  360. #endif /* CONFIG_MEDIA_CONTROLLER */
  361. void v4l2_subdev_init(struct v4l2_subdev *sd, const struct v4l2_subdev_ops *ops)
  362. {
  363. INIT_LIST_HEAD(&sd->list);
  364. BUG_ON(!ops);
  365. sd->ops = ops;
  366. sd->v4l2_dev = NULL;
  367. sd->flags = 0;
  368. sd->name[0] = '\0';
  369. sd->grp_id = 0;
  370. sd->dev_priv = NULL;
  371. sd->host_priv = NULL;
  372. #if defined(CONFIG_MEDIA_CONTROLLER)
  373. sd->entity.name = sd->name;
  374. sd->entity.type = MEDIA_ENT_T_V4L2_SUBDEV;
  375. #endif
  376. }
  377. EXPORT_SYMBOL(v4l2_subdev_init);