v4l2-subdev.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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. struct v4l2_subdev_frame_interval *fi = arg;
  247. if (fi->pad >= sd->entity.num_pads)
  248. return -EINVAL;
  249. return v4l2_subdev_call(sd, video, g_frame_interval, arg);
  250. }
  251. case VIDIOC_SUBDEV_S_FRAME_INTERVAL: {
  252. struct v4l2_subdev_frame_interval *fi = arg;
  253. if (fi->pad >= sd->entity.num_pads)
  254. return -EINVAL;
  255. return v4l2_subdev_call(sd, video, s_frame_interval, arg);
  256. }
  257. case VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL: {
  258. struct v4l2_subdev_frame_interval_enum *fie = arg;
  259. if (fie->pad >= sd->entity.num_pads)
  260. return -EINVAL;
  261. return v4l2_subdev_call(sd, pad, enum_frame_interval, subdev_fh,
  262. fie);
  263. }
  264. case VIDIOC_SUBDEV_G_SELECTION: {
  265. struct v4l2_subdev_selection *sel = arg;
  266. if (sel->which != V4L2_SUBDEV_FORMAT_TRY &&
  267. sel->which != V4L2_SUBDEV_FORMAT_ACTIVE)
  268. return -EINVAL;
  269. if (sel->pad >= sd->entity.num_pads)
  270. return -EINVAL;
  271. return v4l2_subdev_call(
  272. sd, pad, get_selection, subdev_fh, sel);
  273. }
  274. case VIDIOC_SUBDEV_S_SELECTION: {
  275. struct v4l2_subdev_selection *sel = arg;
  276. if (sel->which != V4L2_SUBDEV_FORMAT_TRY &&
  277. sel->which != V4L2_SUBDEV_FORMAT_ACTIVE)
  278. return -EINVAL;
  279. if (sel->pad >= sd->entity.num_pads)
  280. return -EINVAL;
  281. return v4l2_subdev_call(
  282. sd, pad, set_selection, subdev_fh, sel);
  283. }
  284. case VIDIOC_G_EDID: {
  285. struct v4l2_subdev_edid *edid = arg;
  286. if (edid->pad >= sd->entity.num_pads)
  287. return -EINVAL;
  288. if (edid->blocks && edid->edid == NULL)
  289. return -EINVAL;
  290. return v4l2_subdev_call(sd, pad, get_edid, edid);
  291. }
  292. case VIDIOC_S_EDID: {
  293. struct v4l2_subdev_edid *edid = arg;
  294. if (edid->pad >= sd->entity.num_pads)
  295. return -EINVAL;
  296. if (edid->blocks && edid->edid == NULL)
  297. return -EINVAL;
  298. return v4l2_subdev_call(sd, pad, set_edid, edid);
  299. }
  300. case VIDIOC_SUBDEV_DV_TIMINGS_CAP: {
  301. struct v4l2_dv_timings_cap *cap = arg;
  302. if (cap->pad >= sd->entity.num_pads)
  303. return -EINVAL;
  304. return v4l2_subdev_call(sd, pad, dv_timings_cap, cap);
  305. }
  306. case VIDIOC_SUBDEV_ENUM_DV_TIMINGS: {
  307. struct v4l2_enum_dv_timings *dvt = arg;
  308. if (dvt->pad >= sd->entity.num_pads)
  309. return -EINVAL;
  310. return v4l2_subdev_call(sd, pad, enum_dv_timings, dvt);
  311. }
  312. case VIDIOC_SUBDEV_QUERY_DV_TIMINGS:
  313. return v4l2_subdev_call(sd, video, query_dv_timings, arg);
  314. case VIDIOC_SUBDEV_G_DV_TIMINGS:
  315. return v4l2_subdev_call(sd, video, g_dv_timings, arg);
  316. case VIDIOC_SUBDEV_S_DV_TIMINGS:
  317. return v4l2_subdev_call(sd, video, s_dv_timings, arg);
  318. #endif
  319. default:
  320. return v4l2_subdev_call(sd, core, ioctl, cmd, arg);
  321. }
  322. return 0;
  323. }
  324. static long subdev_ioctl(struct file *file, unsigned int cmd,
  325. unsigned long arg)
  326. {
  327. return video_usercopy(file, cmd, arg, subdev_do_ioctl);
  328. }
  329. #ifdef CONFIG_COMPAT
  330. static long subdev_compat_ioctl32(struct file *file, unsigned int cmd,
  331. unsigned long arg)
  332. {
  333. struct video_device *vdev = video_devdata(file);
  334. struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
  335. return v4l2_subdev_call(sd, core, compat_ioctl32, cmd, arg);
  336. }
  337. #endif
  338. static unsigned int subdev_poll(struct file *file, poll_table *wait)
  339. {
  340. struct video_device *vdev = video_devdata(file);
  341. struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
  342. struct v4l2_fh *fh = file->private_data;
  343. if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
  344. return POLLERR;
  345. poll_wait(file, &fh->wait, wait);
  346. if (v4l2_event_pending(fh))
  347. return POLLPRI;
  348. return 0;
  349. }
  350. const struct v4l2_file_operations v4l2_subdev_fops = {
  351. .owner = THIS_MODULE,
  352. .open = subdev_open,
  353. .unlocked_ioctl = subdev_ioctl,
  354. #ifdef CONFIG_COMPAT
  355. .compat_ioctl32 = subdev_compat_ioctl32,
  356. #endif
  357. .release = subdev_close,
  358. .poll = subdev_poll,
  359. };
  360. #ifdef CONFIG_MEDIA_CONTROLLER
  361. int v4l2_subdev_link_validate_default(struct v4l2_subdev *sd,
  362. struct media_link *link,
  363. struct v4l2_subdev_format *source_fmt,
  364. struct v4l2_subdev_format *sink_fmt)
  365. {
  366. if (source_fmt->format.width != sink_fmt->format.width
  367. || source_fmt->format.height != sink_fmt->format.height
  368. || source_fmt->format.code != sink_fmt->format.code)
  369. return -EINVAL;
  370. return 0;
  371. }
  372. EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate_default);
  373. static int
  374. v4l2_subdev_link_validate_get_format(struct media_pad *pad,
  375. struct v4l2_subdev_format *fmt)
  376. {
  377. if (media_entity_type(pad->entity) == MEDIA_ENT_T_V4L2_SUBDEV) {
  378. struct v4l2_subdev *sd =
  379. media_entity_to_v4l2_subdev(pad->entity);
  380. fmt->which = V4L2_SUBDEV_FORMAT_ACTIVE;
  381. fmt->pad = pad->index;
  382. return v4l2_subdev_call(sd, pad, get_fmt, NULL, fmt);
  383. }
  384. WARN(pad->entity->type != MEDIA_ENT_T_DEVNODE_V4L,
  385. "Driver bug! Wrong media entity type 0x%08x, entity %s\n",
  386. pad->entity->type, pad->entity->name);
  387. return -EINVAL;
  388. }
  389. int v4l2_subdev_link_validate(struct media_link *link)
  390. {
  391. struct v4l2_subdev *sink;
  392. struct v4l2_subdev_format sink_fmt, source_fmt;
  393. int rval;
  394. rval = v4l2_subdev_link_validate_get_format(
  395. link->source, &source_fmt);
  396. if (rval < 0)
  397. return 0;
  398. rval = v4l2_subdev_link_validate_get_format(
  399. link->sink, &sink_fmt);
  400. if (rval < 0)
  401. return 0;
  402. sink = media_entity_to_v4l2_subdev(link->sink->entity);
  403. rval = v4l2_subdev_call(sink, pad, link_validate, link,
  404. &source_fmt, &sink_fmt);
  405. if (rval != -ENOIOCTLCMD)
  406. return rval;
  407. return v4l2_subdev_link_validate_default(
  408. sink, link, &source_fmt, &sink_fmt);
  409. }
  410. EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate);
  411. #endif /* CONFIG_MEDIA_CONTROLLER */
  412. void v4l2_subdev_init(struct v4l2_subdev *sd, const struct v4l2_subdev_ops *ops)
  413. {
  414. INIT_LIST_HEAD(&sd->list);
  415. BUG_ON(!ops);
  416. sd->ops = ops;
  417. sd->v4l2_dev = NULL;
  418. sd->flags = 0;
  419. sd->name[0] = '\0';
  420. sd->grp_id = 0;
  421. sd->dev_priv = NULL;
  422. sd->host_priv = NULL;
  423. #if defined(CONFIG_MEDIA_CONTROLLER)
  424. sd->entity.name = sd->name;
  425. sd->entity.type = MEDIA_ENT_T_V4L2_SUBDEV;
  426. #endif
  427. }
  428. EXPORT_SYMBOL(v4l2_subdev_init);