v4l2-subdev.c 14 KB

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