v4l2-subdev.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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. #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
  114. static int check_format(struct v4l2_subdev *sd,
  115. struct v4l2_subdev_format *format)
  116. {
  117. if (format->which != V4L2_SUBDEV_FORMAT_TRY &&
  118. format->which != V4L2_SUBDEV_FORMAT_ACTIVE)
  119. return -EINVAL;
  120. if (format->pad >= sd->entity.num_pads)
  121. return -EINVAL;
  122. return 0;
  123. }
  124. static int check_crop(struct v4l2_subdev *sd, struct v4l2_subdev_crop *crop)
  125. {
  126. if (crop->which != V4L2_SUBDEV_FORMAT_TRY &&
  127. crop->which != V4L2_SUBDEV_FORMAT_ACTIVE)
  128. return -EINVAL;
  129. if (crop->pad >= sd->entity.num_pads)
  130. return -EINVAL;
  131. return 0;
  132. }
  133. static int check_selection(struct v4l2_subdev *sd,
  134. struct v4l2_subdev_selection *sel)
  135. {
  136. if (sel->which != V4L2_SUBDEV_FORMAT_TRY &&
  137. sel->which != V4L2_SUBDEV_FORMAT_ACTIVE)
  138. return -EINVAL;
  139. if (sel->pad >= sd->entity.num_pads)
  140. return -EINVAL;
  141. return 0;
  142. }
  143. static int check_edid(struct v4l2_subdev *sd, struct v4l2_subdev_edid *edid)
  144. {
  145. if (edid->pad >= sd->entity.num_pads)
  146. return -EINVAL;
  147. if (edid->blocks && edid->edid == NULL)
  148. return -EINVAL;
  149. return 0;
  150. }
  151. #endif
  152. static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg)
  153. {
  154. struct video_device *vdev = video_devdata(file);
  155. struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
  156. struct v4l2_fh *vfh = file->private_data;
  157. #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
  158. struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh);
  159. int rval;
  160. #endif
  161. switch (cmd) {
  162. case VIDIOC_QUERYCTRL:
  163. return v4l2_queryctrl(vfh->ctrl_handler, arg);
  164. case VIDIOC_QUERY_EXT_CTRL:
  165. return v4l2_query_ext_ctrl(vfh->ctrl_handler, arg);
  166. case VIDIOC_QUERYMENU:
  167. return v4l2_querymenu(vfh->ctrl_handler, arg);
  168. case VIDIOC_G_CTRL:
  169. return v4l2_g_ctrl(vfh->ctrl_handler, arg);
  170. case VIDIOC_S_CTRL:
  171. return v4l2_s_ctrl(vfh, vfh->ctrl_handler, arg);
  172. case VIDIOC_G_EXT_CTRLS:
  173. return v4l2_g_ext_ctrls(vfh->ctrl_handler, arg);
  174. case VIDIOC_S_EXT_CTRLS:
  175. return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler, arg);
  176. case VIDIOC_TRY_EXT_CTRLS:
  177. return v4l2_try_ext_ctrls(vfh->ctrl_handler, arg);
  178. case VIDIOC_DQEVENT:
  179. if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
  180. return -ENOIOCTLCMD;
  181. return v4l2_event_dequeue(vfh, arg, file->f_flags & O_NONBLOCK);
  182. case VIDIOC_SUBSCRIBE_EVENT:
  183. return v4l2_subdev_call(sd, core, subscribe_event, vfh, arg);
  184. case VIDIOC_UNSUBSCRIBE_EVENT:
  185. return v4l2_subdev_call(sd, core, unsubscribe_event, vfh, arg);
  186. #ifdef CONFIG_VIDEO_ADV_DEBUG
  187. case VIDIOC_DBG_G_REGISTER:
  188. {
  189. struct v4l2_dbg_register *p = arg;
  190. if (!capable(CAP_SYS_ADMIN))
  191. return -EPERM;
  192. return v4l2_subdev_call(sd, core, g_register, p);
  193. }
  194. case VIDIOC_DBG_S_REGISTER:
  195. {
  196. struct v4l2_dbg_register *p = arg;
  197. if (!capable(CAP_SYS_ADMIN))
  198. return -EPERM;
  199. return v4l2_subdev_call(sd, core, s_register, p);
  200. }
  201. #endif
  202. case VIDIOC_LOG_STATUS: {
  203. int ret;
  204. pr_info("%s: ================= START STATUS =================\n",
  205. sd->name);
  206. ret = v4l2_subdev_call(sd, core, log_status);
  207. pr_info("%s: ================== END STATUS ==================\n",
  208. sd->name);
  209. return ret;
  210. }
  211. #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
  212. case VIDIOC_SUBDEV_G_FMT: {
  213. struct v4l2_subdev_format *format = arg;
  214. rval = check_format(sd, format);
  215. if (rval)
  216. return rval;
  217. return v4l2_subdev_call(sd, pad, get_fmt, subdev_fh, format);
  218. }
  219. case VIDIOC_SUBDEV_S_FMT: {
  220. struct v4l2_subdev_format *format = arg;
  221. rval = check_format(sd, format);
  222. if (rval)
  223. return rval;
  224. return v4l2_subdev_call(sd, pad, set_fmt, subdev_fh, format);
  225. }
  226. case VIDIOC_SUBDEV_G_CROP: {
  227. struct v4l2_subdev_crop *crop = arg;
  228. struct v4l2_subdev_selection sel;
  229. rval = check_crop(sd, crop);
  230. if (rval)
  231. return rval;
  232. rval = v4l2_subdev_call(sd, pad, get_crop, subdev_fh, crop);
  233. if (rval != -ENOIOCTLCMD)
  234. return rval;
  235. memset(&sel, 0, sizeof(sel));
  236. sel.which = crop->which;
  237. sel.pad = crop->pad;
  238. sel.target = V4L2_SEL_TGT_CROP;
  239. rval = v4l2_subdev_call(
  240. sd, pad, get_selection, subdev_fh, &sel);
  241. crop->rect = sel.r;
  242. return rval;
  243. }
  244. case VIDIOC_SUBDEV_S_CROP: {
  245. struct v4l2_subdev_crop *crop = arg;
  246. struct v4l2_subdev_selection sel;
  247. rval = check_crop(sd, crop);
  248. if (rval)
  249. return rval;
  250. rval = v4l2_subdev_call(sd, pad, set_crop, subdev_fh, crop);
  251. if (rval != -ENOIOCTLCMD)
  252. return rval;
  253. memset(&sel, 0, sizeof(sel));
  254. sel.which = crop->which;
  255. sel.pad = crop->pad;
  256. sel.target = V4L2_SEL_TGT_CROP;
  257. sel.r = crop->rect;
  258. rval = v4l2_subdev_call(
  259. sd, pad, set_selection, subdev_fh, &sel);
  260. crop->rect = sel.r;
  261. return rval;
  262. }
  263. case VIDIOC_SUBDEV_ENUM_MBUS_CODE: {
  264. struct v4l2_subdev_mbus_code_enum *code = arg;
  265. if (code->pad >= sd->entity.num_pads)
  266. return -EINVAL;
  267. return v4l2_subdev_call(sd, pad, enum_mbus_code, subdev_fh,
  268. code);
  269. }
  270. case VIDIOC_SUBDEV_ENUM_FRAME_SIZE: {
  271. struct v4l2_subdev_frame_size_enum *fse = arg;
  272. if (fse->pad >= sd->entity.num_pads)
  273. return -EINVAL;
  274. return v4l2_subdev_call(sd, pad, enum_frame_size, subdev_fh,
  275. fse);
  276. }
  277. case VIDIOC_SUBDEV_G_FRAME_INTERVAL: {
  278. struct v4l2_subdev_frame_interval *fi = arg;
  279. if (fi->pad >= sd->entity.num_pads)
  280. return -EINVAL;
  281. return v4l2_subdev_call(sd, video, g_frame_interval, arg);
  282. }
  283. case VIDIOC_SUBDEV_S_FRAME_INTERVAL: {
  284. struct v4l2_subdev_frame_interval *fi = arg;
  285. if (fi->pad >= sd->entity.num_pads)
  286. return -EINVAL;
  287. return v4l2_subdev_call(sd, video, s_frame_interval, arg);
  288. }
  289. case VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL: {
  290. struct v4l2_subdev_frame_interval_enum *fie = arg;
  291. if (fie->pad >= sd->entity.num_pads)
  292. return -EINVAL;
  293. return v4l2_subdev_call(sd, pad, enum_frame_interval, subdev_fh,
  294. fie);
  295. }
  296. case VIDIOC_SUBDEV_G_SELECTION: {
  297. struct v4l2_subdev_selection *sel = arg;
  298. rval = check_selection(sd, sel);
  299. if (rval)
  300. return rval;
  301. return v4l2_subdev_call(
  302. sd, pad, get_selection, subdev_fh, sel);
  303. }
  304. case VIDIOC_SUBDEV_S_SELECTION: {
  305. struct v4l2_subdev_selection *sel = arg;
  306. rval = check_selection(sd, sel);
  307. if (rval)
  308. return rval;
  309. return v4l2_subdev_call(
  310. sd, pad, set_selection, subdev_fh, sel);
  311. }
  312. case VIDIOC_G_EDID: {
  313. struct v4l2_subdev_edid *edid = arg;
  314. rval = check_edid(sd, edid);
  315. if (rval)
  316. return rval;
  317. return v4l2_subdev_call(sd, pad, get_edid, edid);
  318. }
  319. case VIDIOC_S_EDID: {
  320. struct v4l2_subdev_edid *edid = arg;
  321. rval = check_edid(sd, edid);
  322. if (rval)
  323. return rval;
  324. return v4l2_subdev_call(sd, pad, set_edid, edid);
  325. }
  326. case VIDIOC_SUBDEV_DV_TIMINGS_CAP: {
  327. struct v4l2_dv_timings_cap *cap = arg;
  328. if (cap->pad >= sd->entity.num_pads)
  329. return -EINVAL;
  330. return v4l2_subdev_call(sd, pad, dv_timings_cap, cap);
  331. }
  332. case VIDIOC_SUBDEV_ENUM_DV_TIMINGS: {
  333. struct v4l2_enum_dv_timings *dvt = arg;
  334. if (dvt->pad >= sd->entity.num_pads)
  335. return -EINVAL;
  336. return v4l2_subdev_call(sd, pad, enum_dv_timings, dvt);
  337. }
  338. case VIDIOC_SUBDEV_QUERY_DV_TIMINGS:
  339. return v4l2_subdev_call(sd, video, query_dv_timings, arg);
  340. case VIDIOC_SUBDEV_G_DV_TIMINGS:
  341. return v4l2_subdev_call(sd, video, g_dv_timings, arg);
  342. case VIDIOC_SUBDEV_S_DV_TIMINGS:
  343. return v4l2_subdev_call(sd, video, s_dv_timings, arg);
  344. #endif
  345. default:
  346. return v4l2_subdev_call(sd, core, ioctl, cmd, arg);
  347. }
  348. return 0;
  349. }
  350. static long subdev_ioctl(struct file *file, unsigned int cmd,
  351. unsigned long arg)
  352. {
  353. return video_usercopy(file, cmd, arg, subdev_do_ioctl);
  354. }
  355. #ifdef CONFIG_COMPAT
  356. static long subdev_compat_ioctl32(struct file *file, unsigned int cmd,
  357. unsigned long arg)
  358. {
  359. struct video_device *vdev = video_devdata(file);
  360. struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
  361. return v4l2_subdev_call(sd, core, compat_ioctl32, cmd, arg);
  362. }
  363. #endif
  364. static unsigned int subdev_poll(struct file *file, poll_table *wait)
  365. {
  366. struct video_device *vdev = video_devdata(file);
  367. struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
  368. struct v4l2_fh *fh = file->private_data;
  369. if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
  370. return POLLERR;
  371. poll_wait(file, &fh->wait, wait);
  372. if (v4l2_event_pending(fh))
  373. return POLLPRI;
  374. return 0;
  375. }
  376. const struct v4l2_file_operations v4l2_subdev_fops = {
  377. .owner = THIS_MODULE,
  378. .open = subdev_open,
  379. .unlocked_ioctl = subdev_ioctl,
  380. #ifdef CONFIG_COMPAT
  381. .compat_ioctl32 = subdev_compat_ioctl32,
  382. #endif
  383. .release = subdev_close,
  384. .poll = subdev_poll,
  385. };
  386. #ifdef CONFIG_MEDIA_CONTROLLER
  387. int v4l2_subdev_link_validate_default(struct v4l2_subdev *sd,
  388. struct media_link *link,
  389. struct v4l2_subdev_format *source_fmt,
  390. struct v4l2_subdev_format *sink_fmt)
  391. {
  392. if (source_fmt->format.width != sink_fmt->format.width
  393. || source_fmt->format.height != sink_fmt->format.height
  394. || source_fmt->format.code != sink_fmt->format.code)
  395. return -EINVAL;
  396. return 0;
  397. }
  398. EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate_default);
  399. static int
  400. v4l2_subdev_link_validate_get_format(struct media_pad *pad,
  401. struct v4l2_subdev_format *fmt)
  402. {
  403. if (media_entity_type(pad->entity) == MEDIA_ENT_T_V4L2_SUBDEV) {
  404. struct v4l2_subdev *sd =
  405. media_entity_to_v4l2_subdev(pad->entity);
  406. fmt->which = V4L2_SUBDEV_FORMAT_ACTIVE;
  407. fmt->pad = pad->index;
  408. return v4l2_subdev_call(sd, pad, get_fmt, NULL, fmt);
  409. }
  410. WARN(pad->entity->type != MEDIA_ENT_T_DEVNODE_V4L,
  411. "Driver bug! Wrong media entity type 0x%08x, entity %s\n",
  412. pad->entity->type, pad->entity->name);
  413. return -EINVAL;
  414. }
  415. int v4l2_subdev_link_validate(struct media_link *link)
  416. {
  417. struct v4l2_subdev *sink;
  418. struct v4l2_subdev_format sink_fmt, source_fmt;
  419. int rval;
  420. rval = v4l2_subdev_link_validate_get_format(
  421. link->source, &source_fmt);
  422. if (rval < 0)
  423. return 0;
  424. rval = v4l2_subdev_link_validate_get_format(
  425. link->sink, &sink_fmt);
  426. if (rval < 0)
  427. return 0;
  428. sink = media_entity_to_v4l2_subdev(link->sink->entity);
  429. rval = v4l2_subdev_call(sink, pad, link_validate, link,
  430. &source_fmt, &sink_fmt);
  431. if (rval != -ENOIOCTLCMD)
  432. return rval;
  433. return v4l2_subdev_link_validate_default(
  434. sink, link, &source_fmt, &sink_fmt);
  435. }
  436. EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate);
  437. #endif /* CONFIG_MEDIA_CONTROLLER */
  438. void v4l2_subdev_init(struct v4l2_subdev *sd, const struct v4l2_subdev_ops *ops)
  439. {
  440. INIT_LIST_HEAD(&sd->list);
  441. BUG_ON(!ops);
  442. sd->ops = ops;
  443. sd->v4l2_dev = NULL;
  444. sd->flags = 0;
  445. sd->name[0] = '\0';
  446. sd->grp_id = 0;
  447. sd->dev_priv = NULL;
  448. sd->host_priv = NULL;
  449. #if defined(CONFIG_MEDIA_CONTROLLER)
  450. sd->entity.name = sd->name;
  451. sd->entity.type = MEDIA_ENT_T_V4L2_SUBDEV;
  452. #endif
  453. }
  454. EXPORT_SYMBOL(v4l2_subdev_init);