| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569 |
- // SPDX-License-Identifier: GPL-2.0-only
- /*
- * V4L2 sub-device
- *
- * Copyright (C) 2010 Nokia Corporation
- *
- * Contact: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
- * Sakari Ailus <sakari.ailus@iki.fi>
- */
- #include <linux/export.h>
- #include <linux/ioctl.h>
- #include <linux/leds.h>
- #include <linux/mm.h>
- #include <linux/module.h>
- #include <linux/overflow.h>
- #include <linux/slab.h>
- #include <linux/string.h>
- #include <linux/types.h>
- #include <linux/version.h>
- #include <linux/videodev2.h>
- #include <media/v4l2-ctrls.h>
- #include <media/v4l2-device.h>
- #include <media/v4l2-event.h>
- #include <media/v4l2-fh.h>
- #include <media/v4l2-ioctl.h>
- #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
- /*
- * The Streams API is an experimental feature. To use the Streams API, set
- * 'v4l2_subdev_enable_streams_api' to 1 below.
- */
- static bool v4l2_subdev_enable_streams_api;
- #endif
- /*
- * Maximum stream ID is 63 for now, as we use u64 bitmask to represent a set
- * of streams.
- *
- * Note that V4L2_FRAME_DESC_ENTRY_MAX is related: V4L2_FRAME_DESC_ENTRY_MAX
- * restricts the total number of streams in a pad, although the stream ID is
- * not restricted.
- */
- #define V4L2_SUBDEV_MAX_STREAM_ID 63
- #include "v4l2-subdev-priv.h"
- #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
- static int subdev_fh_init(struct v4l2_subdev_fh *fh, struct v4l2_subdev *sd)
- {
- struct v4l2_subdev_state *state;
- static struct lock_class_key key;
- state = __v4l2_subdev_state_alloc(sd, "fh->state->lock", &key);
- if (IS_ERR(state))
- return PTR_ERR(state);
- fh->state = state;
- return 0;
- }
- static void subdev_fh_free(struct v4l2_subdev_fh *fh)
- {
- __v4l2_subdev_state_free(fh->state);
- fh->state = NULL;
- }
- static int subdev_open(struct file *file)
- {
- struct video_device *vdev = video_devdata(file);
- struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
- struct v4l2_subdev_fh *subdev_fh;
- int ret;
- subdev_fh = kzalloc(sizeof(*subdev_fh), GFP_KERNEL);
- if (subdev_fh == NULL)
- return -ENOMEM;
- ret = subdev_fh_init(subdev_fh, sd);
- if (ret) {
- kfree(subdev_fh);
- return ret;
- }
- v4l2_fh_init(&subdev_fh->vfh, vdev);
- v4l2_fh_add(&subdev_fh->vfh);
- file->private_data = &subdev_fh->vfh;
- if (sd->v4l2_dev->mdev && sd->entity.graph_obj.mdev->dev) {
- struct module *owner;
- owner = sd->entity.graph_obj.mdev->dev->driver->owner;
- if (!try_module_get(owner)) {
- ret = -EBUSY;
- goto err;
- }
- subdev_fh->owner = owner;
- }
- if (sd->internal_ops && sd->internal_ops->open) {
- ret = sd->internal_ops->open(sd, subdev_fh);
- if (ret < 0)
- goto err;
- }
- return 0;
- err:
- module_put(subdev_fh->owner);
- v4l2_fh_del(&subdev_fh->vfh);
- v4l2_fh_exit(&subdev_fh->vfh);
- subdev_fh_free(subdev_fh);
- kfree(subdev_fh);
- return ret;
- }
- static int subdev_close(struct file *file)
- {
- struct video_device *vdev = video_devdata(file);
- struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
- struct v4l2_fh *vfh = file->private_data;
- struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh);
- if (sd->internal_ops && sd->internal_ops->close)
- sd->internal_ops->close(sd, subdev_fh);
- module_put(subdev_fh->owner);
- v4l2_fh_del(vfh);
- v4l2_fh_exit(vfh);
- subdev_fh_free(subdev_fh);
- kfree(subdev_fh);
- file->private_data = NULL;
- return 0;
- }
- #else /* CONFIG_VIDEO_V4L2_SUBDEV_API */
- static int subdev_open(struct file *file)
- {
- return -ENODEV;
- }
- static int subdev_close(struct file *file)
- {
- return -ENODEV;
- }
- #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
- static void v4l2_subdev_enable_privacy_led(struct v4l2_subdev *sd)
- {
- #if IS_REACHABLE(CONFIG_LEDS_CLASS)
- if (!IS_ERR_OR_NULL(sd->privacy_led))
- led_set_brightness(sd->privacy_led,
- sd->privacy_led->max_brightness);
- #endif
- }
- static void v4l2_subdev_disable_privacy_led(struct v4l2_subdev *sd)
- {
- #if IS_REACHABLE(CONFIG_LEDS_CLASS)
- if (!IS_ERR_OR_NULL(sd->privacy_led))
- led_set_brightness(sd->privacy_led, 0);
- #endif
- }
- static inline int check_which(u32 which)
- {
- if (which != V4L2_SUBDEV_FORMAT_TRY &&
- which != V4L2_SUBDEV_FORMAT_ACTIVE)
- return -EINVAL;
- return 0;
- }
- static inline int check_pad(struct v4l2_subdev *sd, u32 pad)
- {
- #if defined(CONFIG_MEDIA_CONTROLLER)
- if (sd->entity.num_pads) {
- if (pad >= sd->entity.num_pads)
- return -EINVAL;
- return 0;
- }
- #endif
- /* allow pad 0 on subdevices not registered as media entities */
- if (pad > 0)
- return -EINVAL;
- return 0;
- }
- static int check_state(struct v4l2_subdev *sd, struct v4l2_subdev_state *state,
- u32 which, u32 pad, u32 stream)
- {
- if (sd->flags & V4L2_SUBDEV_FL_STREAMS) {
- #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
- if (!v4l2_subdev_state_get_format(state, pad, stream))
- return -EINVAL;
- return 0;
- #else
- return -EINVAL;
- #endif
- }
- if (stream != 0)
- return -EINVAL;
- if (which == V4L2_SUBDEV_FORMAT_TRY && (!state || !state->pads))
- return -EINVAL;
- return 0;
- }
- static inline int check_format(struct v4l2_subdev *sd,
- struct v4l2_subdev_state *state,
- struct v4l2_subdev_format *format)
- {
- if (!format)
- return -EINVAL;
- return check_which(format->which) ? : check_pad(sd, format->pad) ? :
- check_state(sd, state, format->which, format->pad, format->stream);
- }
- static int call_get_fmt(struct v4l2_subdev *sd,
- struct v4l2_subdev_state *state,
- struct v4l2_subdev_format *format)
- {
- return check_format(sd, state, format) ? :
- sd->ops->pad->get_fmt(sd, state, format);
- }
- static int call_set_fmt(struct v4l2_subdev *sd,
- struct v4l2_subdev_state *state,
- struct v4l2_subdev_format *format)
- {
- return check_format(sd, state, format) ? :
- sd->ops->pad->set_fmt(sd, state, format);
- }
- static int call_enum_mbus_code(struct v4l2_subdev *sd,
- struct v4l2_subdev_state *state,
- struct v4l2_subdev_mbus_code_enum *code)
- {
- if (!code)
- return -EINVAL;
- return check_which(code->which) ? : check_pad(sd, code->pad) ? :
- check_state(sd, state, code->which, code->pad, code->stream) ? :
- sd->ops->pad->enum_mbus_code(sd, state, code);
- }
- static int call_enum_frame_size(struct v4l2_subdev *sd,
- struct v4l2_subdev_state *state,
- struct v4l2_subdev_frame_size_enum *fse)
- {
- if (!fse)
- return -EINVAL;
- return check_which(fse->which) ? : check_pad(sd, fse->pad) ? :
- check_state(sd, state, fse->which, fse->pad, fse->stream) ? :
- sd->ops->pad->enum_frame_size(sd, state, fse);
- }
- static int call_enum_frame_interval(struct v4l2_subdev *sd,
- struct v4l2_subdev_state *state,
- struct v4l2_subdev_frame_interval_enum *fie)
- {
- if (!fie)
- return -EINVAL;
- return check_which(fie->which) ? : check_pad(sd, fie->pad) ? :
- check_state(sd, state, fie->which, fie->pad, fie->stream) ? :
- sd->ops->pad->enum_frame_interval(sd, state, fie);
- }
- static inline int check_selection(struct v4l2_subdev *sd,
- struct v4l2_subdev_state *state,
- struct v4l2_subdev_selection *sel)
- {
- if (!sel)
- return -EINVAL;
- return check_which(sel->which) ? : check_pad(sd, sel->pad) ? :
- check_state(sd, state, sel->which, sel->pad, sel->stream);
- }
- static int call_get_selection(struct v4l2_subdev *sd,
- struct v4l2_subdev_state *state,
- struct v4l2_subdev_selection *sel)
- {
- return check_selection(sd, state, sel) ? :
- sd->ops->pad->get_selection(sd, state, sel);
- }
- static int call_set_selection(struct v4l2_subdev *sd,
- struct v4l2_subdev_state *state,
- struct v4l2_subdev_selection *sel)
- {
- return check_selection(sd, state, sel) ? :
- sd->ops->pad->set_selection(sd, state, sel);
- }
- static inline int check_frame_interval(struct v4l2_subdev *sd,
- struct v4l2_subdev_state *state,
- struct v4l2_subdev_frame_interval *fi)
- {
- if (!fi)
- return -EINVAL;
- return check_which(fi->which) ? : check_pad(sd, fi->pad) ? :
- check_state(sd, state, fi->which, fi->pad, fi->stream);
- }
- static int call_get_frame_interval(struct v4l2_subdev *sd,
- struct v4l2_subdev_state *state,
- struct v4l2_subdev_frame_interval *fi)
- {
- return check_frame_interval(sd, state, fi) ? :
- sd->ops->pad->get_frame_interval(sd, state, fi);
- }
- static int call_set_frame_interval(struct v4l2_subdev *sd,
- struct v4l2_subdev_state *state,
- struct v4l2_subdev_frame_interval *fi)
- {
- return check_frame_interval(sd, state, fi) ? :
- sd->ops->pad->set_frame_interval(sd, state, fi);
- }
- static int call_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
- struct v4l2_mbus_frame_desc *fd)
- {
- unsigned int i;
- int ret;
- memset(fd, 0, sizeof(*fd));
- ret = sd->ops->pad->get_frame_desc(sd, pad, fd);
- if (ret)
- return ret;
- dev_dbg(sd->dev, "Frame descriptor on pad %u, type %s\n", pad,
- fd->type == V4L2_MBUS_FRAME_DESC_TYPE_PARALLEL ? "parallel" :
- fd->type == V4L2_MBUS_FRAME_DESC_TYPE_CSI2 ? "CSI-2" :
- "unknown");
- for (i = 0; i < fd->num_entries; i++) {
- struct v4l2_mbus_frame_desc_entry *entry = &fd->entry[i];
- char buf[20] = "";
- if (fd->type == V4L2_MBUS_FRAME_DESC_TYPE_CSI2)
- WARN_ON(snprintf(buf, sizeof(buf),
- ", vc %u, dt 0x%02x",
- entry->bus.csi2.vc,
- entry->bus.csi2.dt) >= sizeof(buf));
- dev_dbg(sd->dev,
- "\tstream %u, code 0x%04x, length %u, flags 0x%04x%s\n",
- entry->stream, entry->pixelcode, entry->length,
- entry->flags, buf);
- }
- return 0;
- }
- static inline int check_edid(struct v4l2_subdev *sd,
- struct v4l2_subdev_edid *edid)
- {
- if (!edid)
- return -EINVAL;
- if (edid->blocks && edid->edid == NULL)
- return -EINVAL;
- return check_pad(sd, edid->pad);
- }
- static int call_get_edid(struct v4l2_subdev *sd, struct v4l2_subdev_edid *edid)
- {
- return check_edid(sd, edid) ? : sd->ops->pad->get_edid(sd, edid);
- }
- static int call_set_edid(struct v4l2_subdev *sd, struct v4l2_subdev_edid *edid)
- {
- return check_edid(sd, edid) ? : sd->ops->pad->set_edid(sd, edid);
- }
- static int call_s_dv_timings(struct v4l2_subdev *sd, unsigned int pad,
- struct v4l2_dv_timings *timings)
- {
- if (!timings)
- return -EINVAL;
- return check_pad(sd, pad) ? :
- sd->ops->pad->s_dv_timings(sd, pad, timings);
- }
- static int call_g_dv_timings(struct v4l2_subdev *sd, unsigned int pad,
- struct v4l2_dv_timings *timings)
- {
- if (!timings)
- return -EINVAL;
- return check_pad(sd, pad) ? :
- sd->ops->pad->g_dv_timings(sd, pad, timings);
- }
- static int call_query_dv_timings(struct v4l2_subdev *sd, unsigned int pad,
- struct v4l2_dv_timings *timings)
- {
- if (!timings)
- return -EINVAL;
- return check_pad(sd, pad) ? :
- sd->ops->pad->query_dv_timings(sd, pad, timings);
- }
- static int call_dv_timings_cap(struct v4l2_subdev *sd,
- struct v4l2_dv_timings_cap *cap)
- {
- if (!cap)
- return -EINVAL;
- return check_pad(sd, cap->pad) ? :
- sd->ops->pad->dv_timings_cap(sd, cap);
- }
- static int call_enum_dv_timings(struct v4l2_subdev *sd,
- struct v4l2_enum_dv_timings *dvt)
- {
- if (!dvt)
- return -EINVAL;
- return check_pad(sd, dvt->pad) ? :
- sd->ops->pad->enum_dv_timings(sd, dvt);
- }
- static int call_get_mbus_config(struct v4l2_subdev *sd, unsigned int pad,
- struct v4l2_mbus_config *config)
- {
- return check_pad(sd, pad) ? :
- sd->ops->pad->get_mbus_config(sd, pad, config);
- }
- static int call_s_stream(struct v4l2_subdev *sd, int enable)
- {
- int ret;
- /*
- * The .s_stream() operation must never be called to start or stop an
- * already started or stopped subdev. Catch offenders but don't return
- * an error yet to avoid regressions.
- */
- if (WARN_ON(sd->s_stream_enabled == !!enable))
- return 0;
- ret = sd->ops->video->s_stream(sd, enable);
- if (!enable && ret < 0) {
- dev_warn(sd->dev, "disabling streaming failed (%d)\n", ret);
- ret = 0;
- }
- if (!ret) {
- sd->s_stream_enabled = enable;
- if (enable)
- v4l2_subdev_enable_privacy_led(sd);
- else
- v4l2_subdev_disable_privacy_led(sd);
- }
- return ret;
- }
- #ifdef CONFIG_MEDIA_CONTROLLER
- /*
- * Create state-management wrapper for pad ops dealing with subdev state. The
- * wrapper handles the case where the caller does not provide the called
- * subdev's state. This should be removed when all the callers are fixed.
- */
- #define DEFINE_STATE_WRAPPER(f, arg_type) \
- static int call_##f##_state(struct v4l2_subdev *sd, \
- struct v4l2_subdev_state *_state, \
- arg_type *arg) \
- { \
- struct v4l2_subdev_state *state = _state; \
- int ret; \
- if (!_state) \
- state = v4l2_subdev_lock_and_get_active_state(sd); \
- ret = call_##f(sd, state, arg); \
- if (!_state && state) \
- v4l2_subdev_unlock_state(state); \
- return ret; \
- }
- #else /* CONFIG_MEDIA_CONTROLLER */
- #define DEFINE_STATE_WRAPPER(f, arg_type) \
- static int call_##f##_state(struct v4l2_subdev *sd, \
- struct v4l2_subdev_state *state, \
- arg_type *arg) \
- { \
- return call_##f(sd, state, arg); \
- }
- #endif /* CONFIG_MEDIA_CONTROLLER */
- DEFINE_STATE_WRAPPER(get_fmt, struct v4l2_subdev_format);
- DEFINE_STATE_WRAPPER(set_fmt, struct v4l2_subdev_format);
- DEFINE_STATE_WRAPPER(enum_mbus_code, struct v4l2_subdev_mbus_code_enum);
- DEFINE_STATE_WRAPPER(enum_frame_size, struct v4l2_subdev_frame_size_enum);
- DEFINE_STATE_WRAPPER(enum_frame_interval, struct v4l2_subdev_frame_interval_enum);
- DEFINE_STATE_WRAPPER(get_selection, struct v4l2_subdev_selection);
- DEFINE_STATE_WRAPPER(set_selection, struct v4l2_subdev_selection);
- static const struct v4l2_subdev_pad_ops v4l2_subdev_call_pad_wrappers = {
- .get_fmt = call_get_fmt_state,
- .set_fmt = call_set_fmt_state,
- .enum_mbus_code = call_enum_mbus_code_state,
- .enum_frame_size = call_enum_frame_size_state,
- .enum_frame_interval = call_enum_frame_interval_state,
- .get_selection = call_get_selection_state,
- .set_selection = call_set_selection_state,
- .get_frame_interval = call_get_frame_interval,
- .set_frame_interval = call_set_frame_interval,
- .get_edid = call_get_edid,
- .set_edid = call_set_edid,
- .s_dv_timings = call_s_dv_timings,
- .g_dv_timings = call_g_dv_timings,
- .query_dv_timings = call_query_dv_timings,
- .dv_timings_cap = call_dv_timings_cap,
- .enum_dv_timings = call_enum_dv_timings,
- .get_frame_desc = call_get_frame_desc,
- .get_mbus_config = call_get_mbus_config,
- };
- static const struct v4l2_subdev_video_ops v4l2_subdev_call_video_wrappers = {
- .s_stream = call_s_stream,
- };
- const struct v4l2_subdev_ops v4l2_subdev_call_wrappers = {
- .pad = &v4l2_subdev_call_pad_wrappers,
- .video = &v4l2_subdev_call_video_wrappers,
- };
- EXPORT_SYMBOL(v4l2_subdev_call_wrappers);
- #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
- static struct v4l2_subdev_state *
- subdev_ioctl_get_state(struct v4l2_subdev *sd, struct v4l2_subdev_fh *subdev_fh,
- unsigned int cmd, void *arg)
- {
- u32 which;
- switch (cmd) {
- default:
- return NULL;
- case VIDIOC_SUBDEV_G_FMT:
- case VIDIOC_SUBDEV_S_FMT:
- which = ((struct v4l2_subdev_format *)arg)->which;
- break;
- case VIDIOC_SUBDEV_G_CROP:
- case VIDIOC_SUBDEV_S_CROP:
- which = ((struct v4l2_subdev_crop *)arg)->which;
- break;
- case VIDIOC_SUBDEV_ENUM_MBUS_CODE:
- which = ((struct v4l2_subdev_mbus_code_enum *)arg)->which;
- break;
- case VIDIOC_SUBDEV_ENUM_FRAME_SIZE:
- which = ((struct v4l2_subdev_frame_size_enum *)arg)->which;
- break;
- case VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL:
- which = ((struct v4l2_subdev_frame_interval_enum *)arg)->which;
- break;
- case VIDIOC_SUBDEV_G_SELECTION:
- case VIDIOC_SUBDEV_S_SELECTION:
- which = ((struct v4l2_subdev_selection *)arg)->which;
- break;
- case VIDIOC_SUBDEV_G_FRAME_INTERVAL:
- case VIDIOC_SUBDEV_S_FRAME_INTERVAL: {
- struct v4l2_subdev_frame_interval *fi = arg;
- if (!(subdev_fh->client_caps &
- V4L2_SUBDEV_CLIENT_CAP_INTERVAL_USES_WHICH))
- fi->which = V4L2_SUBDEV_FORMAT_ACTIVE;
- which = fi->which;
- break;
- }
- case VIDIOC_SUBDEV_G_ROUTING:
- case VIDIOC_SUBDEV_S_ROUTING:
- which = ((struct v4l2_subdev_routing *)arg)->which;
- break;
- }
- return which == V4L2_SUBDEV_FORMAT_TRY ?
- subdev_fh->state :
- v4l2_subdev_get_unlocked_active_state(sd);
- }
- static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg,
- struct v4l2_subdev_state *state)
- {
- struct video_device *vdev = video_devdata(file);
- struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
- struct v4l2_fh *vfh = file->private_data;
- struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh);
- bool ro_subdev = test_bit(V4L2_FL_SUBDEV_RO_DEVNODE, &vdev->flags);
- bool streams_subdev = sd->flags & V4L2_SUBDEV_FL_STREAMS;
- bool client_supports_streams = subdev_fh->client_caps &
- V4L2_SUBDEV_CLIENT_CAP_STREAMS;
- int rval;
- /*
- * If the streams API is not enabled, remove V4L2_SUBDEV_CAP_STREAMS.
- * Remove this when the API is no longer experimental.
- */
- if (!v4l2_subdev_enable_streams_api)
- streams_subdev = false;
- switch (cmd) {
- case VIDIOC_SUBDEV_QUERYCAP: {
- struct v4l2_subdev_capability *cap = arg;
- memset(cap->reserved, 0, sizeof(cap->reserved));
- cap->version = LINUX_VERSION_CODE;
- cap->capabilities =
- (ro_subdev ? V4L2_SUBDEV_CAP_RO_SUBDEV : 0) |
- (streams_subdev ? V4L2_SUBDEV_CAP_STREAMS : 0);
- return 0;
- }
- case VIDIOC_QUERYCTRL:
- /*
- * TODO: this really should be folded into v4l2_queryctrl (this
- * currently returns -EINVAL for NULL control handlers).
- * However, v4l2_queryctrl() is still called directly by
- * drivers as well and until that has been addressed I believe
- * it is safer to do the check here. The same is true for the
- * other control ioctls below.
- */
- if (!vfh->ctrl_handler)
- return -ENOTTY;
- return v4l2_queryctrl(vfh->ctrl_handler, arg);
- case VIDIOC_QUERY_EXT_CTRL:
- if (!vfh->ctrl_handler)
- return -ENOTTY;
- return v4l2_query_ext_ctrl(vfh->ctrl_handler, arg);
- case VIDIOC_QUERYMENU:
- if (!vfh->ctrl_handler)
- return -ENOTTY;
- return v4l2_querymenu(vfh->ctrl_handler, arg);
- case VIDIOC_G_CTRL:
- if (!vfh->ctrl_handler)
- return -ENOTTY;
- return v4l2_g_ctrl(vfh->ctrl_handler, arg);
- case VIDIOC_S_CTRL:
- if (!vfh->ctrl_handler)
- return -ENOTTY;
- return v4l2_s_ctrl(vfh, vfh->ctrl_handler, arg);
- case VIDIOC_G_EXT_CTRLS:
- if (!vfh->ctrl_handler)
- return -ENOTTY;
- return v4l2_g_ext_ctrls(vfh->ctrl_handler,
- vdev, sd->v4l2_dev->mdev, arg);
- case VIDIOC_S_EXT_CTRLS:
- if (!vfh->ctrl_handler)
- return -ENOTTY;
- return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler,
- vdev, sd->v4l2_dev->mdev, arg);
- case VIDIOC_TRY_EXT_CTRLS:
- if (!vfh->ctrl_handler)
- return -ENOTTY;
- return v4l2_try_ext_ctrls(vfh->ctrl_handler,
- vdev, sd->v4l2_dev->mdev, arg);
- case VIDIOC_DQEVENT:
- if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
- return -ENOIOCTLCMD;
- return v4l2_event_dequeue(vfh, arg, file->f_flags & O_NONBLOCK);
- case VIDIOC_SUBSCRIBE_EVENT:
- return v4l2_subdev_call(sd, core, subscribe_event, vfh, arg);
- case VIDIOC_UNSUBSCRIBE_EVENT:
- return v4l2_subdev_call(sd, core, unsubscribe_event, vfh, arg);
- #ifdef CONFIG_VIDEO_ADV_DEBUG
- case VIDIOC_DBG_G_REGISTER:
- {
- struct v4l2_dbg_register *p = arg;
- if (!capable(CAP_SYS_ADMIN))
- return -EPERM;
- return v4l2_subdev_call(sd, core, g_register, p);
- }
- case VIDIOC_DBG_S_REGISTER:
- {
- struct v4l2_dbg_register *p = arg;
- if (!capable(CAP_SYS_ADMIN))
- return -EPERM;
- return v4l2_subdev_call(sd, core, s_register, p);
- }
- case VIDIOC_DBG_G_CHIP_INFO:
- {
- struct v4l2_dbg_chip_info *p = arg;
- if (p->match.type != V4L2_CHIP_MATCH_SUBDEV || p->match.addr)
- return -EINVAL;
- if (sd->ops->core && sd->ops->core->s_register)
- p->flags |= V4L2_CHIP_FL_WRITABLE;
- if (sd->ops->core && sd->ops->core->g_register)
- p->flags |= V4L2_CHIP_FL_READABLE;
- strscpy(p->name, sd->name, sizeof(p->name));
- return 0;
- }
- #endif
- case VIDIOC_LOG_STATUS: {
- int ret;
- pr_info("%s: ================= START STATUS =================\n",
- sd->name);
- ret = v4l2_subdev_call(sd, core, log_status);
- pr_info("%s: ================== END STATUS ==================\n",
- sd->name);
- return ret;
- }
- case VIDIOC_SUBDEV_G_FMT: {
- struct v4l2_subdev_format *format = arg;
- if (!client_supports_streams)
- format->stream = 0;
- memset(format->reserved, 0, sizeof(format->reserved));
- memset(format->format.reserved, 0, sizeof(format->format.reserved));
- return v4l2_subdev_call(sd, pad, get_fmt, state, format);
- }
- case VIDIOC_SUBDEV_S_FMT: {
- struct v4l2_subdev_format *format = arg;
- if (format->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev)
- return -EPERM;
- if (!client_supports_streams)
- format->stream = 0;
- memset(format->reserved, 0, sizeof(format->reserved));
- memset(format->format.reserved, 0, sizeof(format->format.reserved));
- return v4l2_subdev_call(sd, pad, set_fmt, state, format);
- }
- case VIDIOC_SUBDEV_G_CROP: {
- struct v4l2_subdev_crop *crop = arg;
- struct v4l2_subdev_selection sel;
- if (!client_supports_streams)
- crop->stream = 0;
- memset(crop->reserved, 0, sizeof(crop->reserved));
- memset(&sel, 0, sizeof(sel));
- sel.which = crop->which;
- sel.pad = crop->pad;
- sel.stream = crop->stream;
- sel.target = V4L2_SEL_TGT_CROP;
- rval = v4l2_subdev_call(
- sd, pad, get_selection, state, &sel);
- crop->rect = sel.r;
- return rval;
- }
- case VIDIOC_SUBDEV_S_CROP: {
- struct v4l2_subdev_crop *crop = arg;
- struct v4l2_subdev_selection sel;
- if (crop->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev)
- return -EPERM;
- if (!client_supports_streams)
- crop->stream = 0;
- memset(crop->reserved, 0, sizeof(crop->reserved));
- memset(&sel, 0, sizeof(sel));
- sel.which = crop->which;
- sel.pad = crop->pad;
- sel.stream = crop->stream;
- sel.target = V4L2_SEL_TGT_CROP;
- sel.r = crop->rect;
- rval = v4l2_subdev_call(
- sd, pad, set_selection, state, &sel);
- crop->rect = sel.r;
- return rval;
- }
- case VIDIOC_SUBDEV_ENUM_MBUS_CODE: {
- struct v4l2_subdev_mbus_code_enum *code = arg;
- if (!client_supports_streams)
- code->stream = 0;
- memset(code->reserved, 0, sizeof(code->reserved));
- return v4l2_subdev_call(sd, pad, enum_mbus_code, state,
- code);
- }
- case VIDIOC_SUBDEV_ENUM_FRAME_SIZE: {
- struct v4l2_subdev_frame_size_enum *fse = arg;
- if (!client_supports_streams)
- fse->stream = 0;
- memset(fse->reserved, 0, sizeof(fse->reserved));
- return v4l2_subdev_call(sd, pad, enum_frame_size, state,
- fse);
- }
- case VIDIOC_SUBDEV_G_FRAME_INTERVAL: {
- struct v4l2_subdev_frame_interval *fi = arg;
- if (!client_supports_streams)
- fi->stream = 0;
- memset(fi->reserved, 0, sizeof(fi->reserved));
- return v4l2_subdev_call(sd, pad, get_frame_interval, state, fi);
- }
- case VIDIOC_SUBDEV_S_FRAME_INTERVAL: {
- struct v4l2_subdev_frame_interval *fi = arg;
- if (!client_supports_streams)
- fi->stream = 0;
- if (fi->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev)
- return -EPERM;
- memset(fi->reserved, 0, sizeof(fi->reserved));
- return v4l2_subdev_call(sd, pad, set_frame_interval, state, fi);
- }
- case VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL: {
- struct v4l2_subdev_frame_interval_enum *fie = arg;
- if (!client_supports_streams)
- fie->stream = 0;
- memset(fie->reserved, 0, sizeof(fie->reserved));
- return v4l2_subdev_call(sd, pad, enum_frame_interval, state,
- fie);
- }
- case VIDIOC_SUBDEV_G_SELECTION: {
- struct v4l2_subdev_selection *sel = arg;
- if (!client_supports_streams)
- sel->stream = 0;
- memset(sel->reserved, 0, sizeof(sel->reserved));
- return v4l2_subdev_call(
- sd, pad, get_selection, state, sel);
- }
- case VIDIOC_SUBDEV_S_SELECTION: {
- struct v4l2_subdev_selection *sel = arg;
- if (sel->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev)
- return -EPERM;
- if (!client_supports_streams)
- sel->stream = 0;
- memset(sel->reserved, 0, sizeof(sel->reserved));
- return v4l2_subdev_call(
- sd, pad, set_selection, state, sel);
- }
- case VIDIOC_G_EDID: {
- struct v4l2_subdev_edid *edid = arg;
- return v4l2_subdev_call(sd, pad, get_edid, edid);
- }
- case VIDIOC_S_EDID: {
- struct v4l2_subdev_edid *edid = arg;
- return v4l2_subdev_call(sd, pad, set_edid, edid);
- }
- case VIDIOC_SUBDEV_DV_TIMINGS_CAP: {
- struct v4l2_dv_timings_cap *cap = arg;
- return v4l2_subdev_call(sd, pad, dv_timings_cap, cap);
- }
- case VIDIOC_SUBDEV_ENUM_DV_TIMINGS: {
- struct v4l2_enum_dv_timings *dvt = arg;
- return v4l2_subdev_call(sd, pad, enum_dv_timings, dvt);
- }
- case VIDIOC_SUBDEV_QUERY_DV_TIMINGS:
- return v4l2_subdev_call(sd, pad, query_dv_timings, 0, arg);
- case VIDIOC_SUBDEV_G_DV_TIMINGS:
- return v4l2_subdev_call(sd, pad, g_dv_timings, 0, arg);
- case VIDIOC_SUBDEV_S_DV_TIMINGS:
- if (ro_subdev)
- return -EPERM;
- return v4l2_subdev_call(sd, pad, s_dv_timings, 0, arg);
- case VIDIOC_SUBDEV_G_STD:
- return v4l2_subdev_call(sd, video, g_std, arg);
- case VIDIOC_SUBDEV_S_STD: {
- v4l2_std_id *std = arg;
- if (ro_subdev)
- return -EPERM;
- return v4l2_subdev_call(sd, video, s_std, *std);
- }
- case VIDIOC_SUBDEV_ENUMSTD: {
- struct v4l2_standard *p = arg;
- v4l2_std_id id;
- if (v4l2_subdev_call(sd, video, g_tvnorms, &id))
- return -EINVAL;
- return v4l_video_std_enumstd(p, id);
- }
- case VIDIOC_SUBDEV_QUERYSTD:
- return v4l2_subdev_call(sd, video, querystd, arg);
- case VIDIOC_SUBDEV_G_ROUTING: {
- struct v4l2_subdev_routing *routing = arg;
- struct v4l2_subdev_krouting *krouting;
- if (!v4l2_subdev_enable_streams_api)
- return -ENOIOCTLCMD;
- if (!(sd->flags & V4L2_SUBDEV_FL_STREAMS))
- return -ENOIOCTLCMD;
- memset(routing->reserved, 0, sizeof(routing->reserved));
- krouting = &state->routing;
- memcpy((struct v4l2_subdev_route *)(uintptr_t)routing->routes,
- krouting->routes,
- min(krouting->num_routes, routing->len_routes) *
- sizeof(*krouting->routes));
- routing->num_routes = krouting->num_routes;
- return 0;
- }
- case VIDIOC_SUBDEV_S_ROUTING: {
- struct v4l2_subdev_routing *routing = arg;
- struct v4l2_subdev_route *routes =
- (struct v4l2_subdev_route *)(uintptr_t)routing->routes;
- struct v4l2_subdev_krouting krouting = {};
- unsigned int i;
- if (!v4l2_subdev_enable_streams_api)
- return -ENOIOCTLCMD;
- if (!(sd->flags & V4L2_SUBDEV_FL_STREAMS))
- return -ENOIOCTLCMD;
- if (routing->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev)
- return -EPERM;
- if (routing->num_routes > routing->len_routes)
- return -EINVAL;
- memset(routing->reserved, 0, sizeof(routing->reserved));
- for (i = 0; i < routing->num_routes; ++i) {
- const struct v4l2_subdev_route *route = &routes[i];
- const struct media_pad *pads = sd->entity.pads;
- if (route->sink_stream > V4L2_SUBDEV_MAX_STREAM_ID ||
- route->source_stream > V4L2_SUBDEV_MAX_STREAM_ID)
- return -EINVAL;
- if (route->sink_pad >= sd->entity.num_pads)
- return -EINVAL;
- if (!(pads[route->sink_pad].flags &
- MEDIA_PAD_FL_SINK))
- return -EINVAL;
- if (route->source_pad >= sd->entity.num_pads)
- return -EINVAL;
- if (!(pads[route->source_pad].flags &
- MEDIA_PAD_FL_SOURCE))
- return -EINVAL;
- }
- /*
- * If the driver doesn't support setting routing, just return
- * the routing table.
- */
- if (!v4l2_subdev_has_op(sd, pad, set_routing)) {
- memcpy((struct v4l2_subdev_route *)(uintptr_t)routing->routes,
- state->routing.routes,
- min(state->routing.num_routes, routing->len_routes) *
- sizeof(*state->routing.routes));
- routing->num_routes = state->routing.num_routes;
- return 0;
- }
- krouting.num_routes = routing->num_routes;
- krouting.len_routes = routing->len_routes;
- krouting.routes = routes;
- rval = v4l2_subdev_call(sd, pad, set_routing, state,
- routing->which, &krouting);
- if (rval < 0)
- return rval;
- memcpy((struct v4l2_subdev_route *)(uintptr_t)routing->routes,
- state->routing.routes,
- min(state->routing.num_routes, routing->len_routes) *
- sizeof(*state->routing.routes));
- routing->num_routes = state->routing.num_routes;
- return 0;
- }
- case VIDIOC_SUBDEV_G_CLIENT_CAP: {
- struct v4l2_subdev_client_capability *client_cap = arg;
- client_cap->capabilities = subdev_fh->client_caps;
- return 0;
- }
- case VIDIOC_SUBDEV_S_CLIENT_CAP: {
- struct v4l2_subdev_client_capability *client_cap = arg;
- /*
- * Clear V4L2_SUBDEV_CLIENT_CAP_STREAMS if streams API is not
- * enabled. Remove this when streams API is no longer
- * experimental.
- */
- if (!v4l2_subdev_enable_streams_api)
- client_cap->capabilities &= ~V4L2_SUBDEV_CLIENT_CAP_STREAMS;
- /* Filter out unsupported capabilities */
- client_cap->capabilities &= (V4L2_SUBDEV_CLIENT_CAP_STREAMS |
- V4L2_SUBDEV_CLIENT_CAP_INTERVAL_USES_WHICH);
- subdev_fh->client_caps = client_cap->capabilities;
- return 0;
- }
- default:
- return v4l2_subdev_call(sd, core, ioctl, cmd, arg);
- }
- return 0;
- }
- static long subdev_do_ioctl_lock(struct file *file, unsigned int cmd, void *arg)
- {
- struct video_device *vdev = video_devdata(file);
- struct mutex *lock = vdev->lock;
- long ret = -ENODEV;
- if (lock && mutex_lock_interruptible(lock))
- return -ERESTARTSYS;
- if (video_is_registered(vdev)) {
- struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
- struct v4l2_fh *vfh = file->private_data;
- struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh);
- struct v4l2_subdev_state *state;
- state = subdev_ioctl_get_state(sd, subdev_fh, cmd, arg);
- if (state)
- v4l2_subdev_lock_state(state);
- ret = subdev_do_ioctl(file, cmd, arg, state);
- if (state)
- v4l2_subdev_unlock_state(state);
- }
- if (lock)
- mutex_unlock(lock);
- return ret;
- }
- static long subdev_ioctl(struct file *file, unsigned int cmd,
- unsigned long arg)
- {
- return video_usercopy(file, cmd, arg, subdev_do_ioctl_lock);
- }
- #ifdef CONFIG_COMPAT
- static long subdev_compat_ioctl32(struct file *file, unsigned int cmd,
- unsigned long arg)
- {
- struct video_device *vdev = video_devdata(file);
- struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
- return v4l2_subdev_call(sd, core, compat_ioctl32, cmd, arg);
- }
- #endif
- #else /* CONFIG_VIDEO_V4L2_SUBDEV_API */
- static long subdev_ioctl(struct file *file, unsigned int cmd,
- unsigned long arg)
- {
- return -ENODEV;
- }
- #ifdef CONFIG_COMPAT
- static long subdev_compat_ioctl32(struct file *file, unsigned int cmd,
- unsigned long arg)
- {
- return -ENODEV;
- }
- #endif
- #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
- static __poll_t subdev_poll(struct file *file, poll_table *wait)
- {
- struct video_device *vdev = video_devdata(file);
- struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
- struct v4l2_fh *fh = file->private_data;
- if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
- return EPOLLERR;
- poll_wait(file, &fh->wait, wait);
- if (v4l2_event_pending(fh))
- return EPOLLPRI;
- return 0;
- }
- const struct v4l2_file_operations v4l2_subdev_fops = {
- .owner = THIS_MODULE,
- .open = subdev_open,
- .unlocked_ioctl = subdev_ioctl,
- #ifdef CONFIG_COMPAT
- .compat_ioctl32 = subdev_compat_ioctl32,
- #endif
- .release = subdev_close,
- .poll = subdev_poll,
- };
- #ifdef CONFIG_MEDIA_CONTROLLER
- int v4l2_subdev_get_fwnode_pad_1_to_1(struct media_entity *entity,
- struct fwnode_endpoint *endpoint)
- {
- struct fwnode_handle *fwnode;
- struct v4l2_subdev *sd;
- if (!is_media_entity_v4l2_subdev(entity))
- return -EINVAL;
- sd = media_entity_to_v4l2_subdev(entity);
- fwnode = fwnode_graph_get_port_parent(endpoint->local_fwnode);
- fwnode_handle_put(fwnode);
- if (device_match_fwnode(sd->dev, fwnode))
- return endpoint->port;
- return -ENXIO;
- }
- EXPORT_SYMBOL_GPL(v4l2_subdev_get_fwnode_pad_1_to_1);
- int v4l2_subdev_link_validate_default(struct v4l2_subdev *sd,
- struct media_link *link,
- struct v4l2_subdev_format *source_fmt,
- struct v4l2_subdev_format *sink_fmt)
- {
- bool pass = true;
- /* The width, height and code must match. */
- if (source_fmt->format.width != sink_fmt->format.width) {
- dev_dbg(sd->entity.graph_obj.mdev->dev,
- "%s: width does not match (source %u, sink %u)\n",
- __func__,
- source_fmt->format.width, sink_fmt->format.width);
- pass = false;
- }
- if (source_fmt->format.height != sink_fmt->format.height) {
- dev_dbg(sd->entity.graph_obj.mdev->dev,
- "%s: height does not match (source %u, sink %u)\n",
- __func__,
- source_fmt->format.height, sink_fmt->format.height);
- pass = false;
- }
- if (source_fmt->format.code != sink_fmt->format.code) {
- dev_dbg(sd->entity.graph_obj.mdev->dev,
- "%s: media bus code does not match (source 0x%8.8x, sink 0x%8.8x)\n",
- __func__,
- source_fmt->format.code, sink_fmt->format.code);
- pass = false;
- }
- /* The field order must match, or the sink field order must be NONE
- * to support interlaced hardware connected to bridges that support
- * progressive formats only.
- */
- if (source_fmt->format.field != sink_fmt->format.field &&
- sink_fmt->format.field != V4L2_FIELD_NONE) {
- dev_dbg(sd->entity.graph_obj.mdev->dev,
- "%s: field does not match (source %u, sink %u)\n",
- __func__,
- source_fmt->format.field, sink_fmt->format.field);
- pass = false;
- }
- if (pass)
- return 0;
- dev_dbg(sd->entity.graph_obj.mdev->dev,
- "%s: link was \"%s\":%u -> \"%s\":%u\n", __func__,
- link->source->entity->name, link->source->index,
- link->sink->entity->name, link->sink->index);
- return -EPIPE;
- }
- EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate_default);
- static int
- v4l2_subdev_link_validate_get_format(struct media_pad *pad, u32 stream,
- struct v4l2_subdev_format *fmt,
- bool states_locked)
- {
- struct v4l2_subdev_state *state;
- struct v4l2_subdev *sd;
- int ret;
- sd = media_entity_to_v4l2_subdev(pad->entity);
- fmt->which = V4L2_SUBDEV_FORMAT_ACTIVE;
- fmt->pad = pad->index;
- fmt->stream = stream;
- if (states_locked)
- state = v4l2_subdev_get_locked_active_state(sd);
- else
- state = v4l2_subdev_lock_and_get_active_state(sd);
- ret = v4l2_subdev_call(sd, pad, get_fmt, state, fmt);
- if (!states_locked && state)
- v4l2_subdev_unlock_state(state);
- return ret;
- }
- #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
- static void __v4l2_link_validate_get_streams(struct media_pad *pad,
- u64 *streams_mask,
- bool states_locked)
- {
- struct v4l2_subdev_route *route;
- struct v4l2_subdev_state *state;
- struct v4l2_subdev *subdev;
- subdev = media_entity_to_v4l2_subdev(pad->entity);
- *streams_mask = 0;
- if (states_locked)
- state = v4l2_subdev_get_locked_active_state(subdev);
- else
- state = v4l2_subdev_lock_and_get_active_state(subdev);
- if (WARN_ON(!state))
- return;
- for_each_active_route(&state->routing, route) {
- u32 route_pad;
- u32 route_stream;
- if (pad->flags & MEDIA_PAD_FL_SOURCE) {
- route_pad = route->source_pad;
- route_stream = route->source_stream;
- } else {
- route_pad = route->sink_pad;
- route_stream = route->sink_stream;
- }
- if (route_pad != pad->index)
- continue;
- *streams_mask |= BIT_ULL(route_stream);
- }
- if (!states_locked)
- v4l2_subdev_unlock_state(state);
- }
- #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
- static void v4l2_link_validate_get_streams(struct media_pad *pad,
- u64 *streams_mask,
- bool states_locked)
- {
- struct v4l2_subdev *subdev = media_entity_to_v4l2_subdev(pad->entity);
- if (!(subdev->flags & V4L2_SUBDEV_FL_STREAMS)) {
- /* Non-streams subdevs have an implicit stream 0 */
- *streams_mask = BIT_ULL(0);
- return;
- }
- #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
- __v4l2_link_validate_get_streams(pad, streams_mask, states_locked);
- #else
- /* This shouldn't happen */
- *streams_mask = 0;
- #endif
- }
- static int v4l2_subdev_link_validate_locked(struct media_link *link, bool states_locked)
- {
- struct v4l2_subdev *sink_subdev =
- media_entity_to_v4l2_subdev(link->sink->entity);
- struct device *dev = sink_subdev->entity.graph_obj.mdev->dev;
- u64 source_streams_mask;
- u64 sink_streams_mask;
- u64 dangling_sink_streams;
- u32 stream;
- int ret;
- dev_dbg(dev, "validating link \"%s\":%u -> \"%s\":%u\n",
- link->source->entity->name, link->source->index,
- link->sink->entity->name, link->sink->index);
- v4l2_link_validate_get_streams(link->source, &source_streams_mask, states_locked);
- v4l2_link_validate_get_streams(link->sink, &sink_streams_mask, states_locked);
- /*
- * It is ok to have more source streams than sink streams as extra
- * source streams can just be ignored by the receiver, but having extra
- * sink streams is an error as streams must have a source.
- */
- dangling_sink_streams = (source_streams_mask ^ sink_streams_mask) &
- sink_streams_mask;
- if (dangling_sink_streams) {
- dev_err(dev, "Dangling sink streams: mask %#llx\n",
- dangling_sink_streams);
- return -EINVAL;
- }
- /* Validate source and sink stream formats */
- for (stream = 0; stream < sizeof(sink_streams_mask) * 8; ++stream) {
- struct v4l2_subdev_format sink_fmt, source_fmt;
- if (!(sink_streams_mask & BIT_ULL(stream)))
- continue;
- dev_dbg(dev, "validating stream \"%s\":%u:%u -> \"%s\":%u:%u\n",
- link->source->entity->name, link->source->index, stream,
- link->sink->entity->name, link->sink->index, stream);
- ret = v4l2_subdev_link_validate_get_format(link->source, stream,
- &source_fmt, states_locked);
- if (ret < 0) {
- dev_dbg(dev,
- "Failed to get format for \"%s\":%u:%u (but that's ok)\n",
- link->source->entity->name, link->source->index,
- stream);
- continue;
- }
- ret = v4l2_subdev_link_validate_get_format(link->sink, stream,
- &sink_fmt, states_locked);
- if (ret < 0) {
- dev_dbg(dev,
- "Failed to get format for \"%s\":%u:%u (but that's ok)\n",
- link->sink->entity->name, link->sink->index,
- stream);
- continue;
- }
- /* TODO: add stream number to link_validate() */
- ret = v4l2_subdev_call(sink_subdev, pad, link_validate, link,
- &source_fmt, &sink_fmt);
- if (!ret)
- continue;
- if (ret != -ENOIOCTLCMD)
- return ret;
- ret = v4l2_subdev_link_validate_default(sink_subdev, link,
- &source_fmt, &sink_fmt);
- if (ret)
- return ret;
- }
- return 0;
- }
- int v4l2_subdev_link_validate(struct media_link *link)
- {
- struct v4l2_subdev *source_sd, *sink_sd;
- struct v4l2_subdev_state *source_state, *sink_state;
- bool states_locked;
- int ret;
- /*
- * Links are validated in the context of the sink entity. Usage of this
- * helper on a sink that is not a subdev is a clear driver bug.
- */
- if (WARN_ON_ONCE(!is_media_entity_v4l2_subdev(link->sink->entity)))
- return -EINVAL;
- /*
- * If the source is a video device, delegate link validation to it. This
- * allows usage of this helper for subdev connected to a video output
- * device, provided that the driver implement the video output device's
- * .link_validate() operation.
- */
- if (is_media_entity_v4l2_video_device(link->source->entity)) {
- struct media_entity *source = link->source->entity;
- if (!source->ops || !source->ops->link_validate) {
- /*
- * Many existing drivers do not implement the required
- * .link_validate() operation for their video devices.
- * Print a warning to get the drivers fixed, and return
- * 0 to avoid breaking userspace. This should
- * eventually be turned into a WARN_ON() when all
- * drivers will have been fixed.
- */
- pr_warn_once("video device '%s' does not implement .link_validate(), driver bug!\n",
- source->name);
- return 0;
- }
- /*
- * Avoid infinite loops in case a video device incorrectly uses
- * this helper function as its .link_validate() handler.
- */
- if (WARN_ON(source->ops->link_validate == v4l2_subdev_link_validate))
- return -EINVAL;
- return source->ops->link_validate(link);
- }
- /*
- * If the source is still not a subdev, usage of this helper is a clear
- * driver bug.
- */
- if (WARN_ON(!is_media_entity_v4l2_subdev(link->source->entity)))
- return -EINVAL;
- sink_sd = media_entity_to_v4l2_subdev(link->sink->entity);
- source_sd = media_entity_to_v4l2_subdev(link->source->entity);
- sink_state = v4l2_subdev_get_unlocked_active_state(sink_sd);
- source_state = v4l2_subdev_get_unlocked_active_state(source_sd);
- states_locked = sink_state && source_state;
- if (states_locked)
- v4l2_subdev_lock_states(sink_state, source_state);
- ret = v4l2_subdev_link_validate_locked(link, states_locked);
- if (states_locked)
- v4l2_subdev_unlock_states(sink_state, source_state);
- return ret;
- }
- EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate);
- bool v4l2_subdev_has_pad_interdep(struct media_entity *entity,
- unsigned int pad0, unsigned int pad1)
- {
- struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
- struct v4l2_subdev_krouting *routing;
- struct v4l2_subdev_state *state;
- unsigned int i;
- state = v4l2_subdev_lock_and_get_active_state(sd);
- routing = &state->routing;
- for (i = 0; i < routing->num_routes; ++i) {
- struct v4l2_subdev_route *route = &routing->routes[i];
- if (!(route->flags & V4L2_SUBDEV_ROUTE_FL_ACTIVE))
- continue;
- if ((route->sink_pad == pad0 && route->source_pad == pad1) ||
- (route->source_pad == pad0 && route->sink_pad == pad1)) {
- v4l2_subdev_unlock_state(state);
- return true;
- }
- }
- v4l2_subdev_unlock_state(state);
- return false;
- }
- EXPORT_SYMBOL_GPL(v4l2_subdev_has_pad_interdep);
- struct v4l2_subdev_state *
- __v4l2_subdev_state_alloc(struct v4l2_subdev *sd, const char *lock_name,
- struct lock_class_key *lock_key)
- {
- struct v4l2_subdev_state *state;
- int ret;
- state = kzalloc(sizeof(*state), GFP_KERNEL);
- if (!state)
- return ERR_PTR(-ENOMEM);
- __mutex_init(&state->_lock, lock_name, lock_key);
- if (sd->state_lock)
- state->lock = sd->state_lock;
- else
- state->lock = &state->_lock;
- state->sd = sd;
- /* Drivers that support streams do not need the legacy pad config */
- if (!(sd->flags & V4L2_SUBDEV_FL_STREAMS) && sd->entity.num_pads) {
- state->pads = kvcalloc(sd->entity.num_pads,
- sizeof(*state->pads), GFP_KERNEL);
- if (!state->pads) {
- ret = -ENOMEM;
- goto err;
- }
- }
- if (sd->internal_ops && sd->internal_ops->init_state) {
- /*
- * There can be no race at this point, but we lock the state
- * anyway to satisfy lockdep checks.
- */
- v4l2_subdev_lock_state(state);
- ret = sd->internal_ops->init_state(sd, state);
- v4l2_subdev_unlock_state(state);
- if (ret)
- goto err;
- }
- return state;
- err:
- if (state && state->pads)
- kvfree(state->pads);
- kfree(state);
- return ERR_PTR(ret);
- }
- EXPORT_SYMBOL_GPL(__v4l2_subdev_state_alloc);
- void __v4l2_subdev_state_free(struct v4l2_subdev_state *state)
- {
- if (!state)
- return;
- mutex_destroy(&state->_lock);
- kfree(state->routing.routes);
- kvfree(state->stream_configs.configs);
- kvfree(state->pads);
- kfree(state);
- }
- EXPORT_SYMBOL_GPL(__v4l2_subdev_state_free);
- int __v4l2_subdev_init_finalize(struct v4l2_subdev *sd, const char *name,
- struct lock_class_key *key)
- {
- struct v4l2_subdev_state *state;
- struct device *dev = sd->dev;
- bool has_disable_streams;
- bool has_enable_streams;
- bool has_s_stream;
- /* Check that the subdevice implements the required features */
- has_s_stream = v4l2_subdev_has_op(sd, video, s_stream);
- has_enable_streams = v4l2_subdev_has_op(sd, pad, enable_streams);
- has_disable_streams = v4l2_subdev_has_op(sd, pad, disable_streams);
- if (has_enable_streams != has_disable_streams) {
- dev_err(dev,
- "subdev '%s' must implement both or neither of .enable_streams() and .disable_streams()\n",
- sd->name);
- return -EINVAL;
- }
- if (sd->flags & V4L2_SUBDEV_FL_STREAMS) {
- if (has_s_stream && !has_enable_streams) {
- dev_err(dev,
- "subdev '%s' must implement .enable/disable_streams()\n",
- sd->name);
- return -EINVAL;
- }
- }
- state = __v4l2_subdev_state_alloc(sd, name, key);
- if (IS_ERR(state))
- return PTR_ERR(state);
- sd->active_state = state;
- return 0;
- }
- EXPORT_SYMBOL_GPL(__v4l2_subdev_init_finalize);
- void v4l2_subdev_cleanup(struct v4l2_subdev *sd)
- {
- struct v4l2_async_subdev_endpoint *ase, *ase_tmp;
- __v4l2_subdev_state_free(sd->active_state);
- sd->active_state = NULL;
- /* Uninitialised sub-device, bail out here. */
- if (!sd->async_subdev_endpoint_list.next)
- return;
- list_for_each_entry_safe(ase, ase_tmp, &sd->async_subdev_endpoint_list,
- async_subdev_endpoint_entry) {
- list_del(&ase->async_subdev_endpoint_entry);
- kfree(ase);
- }
- }
- EXPORT_SYMBOL_GPL(v4l2_subdev_cleanup);
- struct v4l2_mbus_framefmt *
- __v4l2_subdev_state_get_format(struct v4l2_subdev_state *state,
- unsigned int pad, u32 stream)
- {
- struct v4l2_subdev_stream_configs *stream_configs;
- unsigned int i;
- if (WARN_ON_ONCE(!state))
- return NULL;
- if (state->pads) {
- if (stream)
- return NULL;
- if (pad >= state->sd->entity.num_pads)
- return NULL;
- return &state->pads[pad].format;
- }
- lockdep_assert_held(state->lock);
- stream_configs = &state->stream_configs;
- for (i = 0; i < stream_configs->num_configs; ++i) {
- if (stream_configs->configs[i].pad == pad &&
- stream_configs->configs[i].stream == stream)
- return &stream_configs->configs[i].fmt;
- }
- return NULL;
- }
- EXPORT_SYMBOL_GPL(__v4l2_subdev_state_get_format);
- struct v4l2_rect *
- __v4l2_subdev_state_get_crop(struct v4l2_subdev_state *state, unsigned int pad,
- u32 stream)
- {
- struct v4l2_subdev_stream_configs *stream_configs;
- unsigned int i;
- if (WARN_ON_ONCE(!state))
- return NULL;
- if (state->pads) {
- if (stream)
- return NULL;
- if (pad >= state->sd->entity.num_pads)
- return NULL;
- return &state->pads[pad].crop;
- }
- lockdep_assert_held(state->lock);
- stream_configs = &state->stream_configs;
- for (i = 0; i < stream_configs->num_configs; ++i) {
- if (stream_configs->configs[i].pad == pad &&
- stream_configs->configs[i].stream == stream)
- return &stream_configs->configs[i].crop;
- }
- return NULL;
- }
- EXPORT_SYMBOL_GPL(__v4l2_subdev_state_get_crop);
- struct v4l2_rect *
- __v4l2_subdev_state_get_compose(struct v4l2_subdev_state *state,
- unsigned int pad, u32 stream)
- {
- struct v4l2_subdev_stream_configs *stream_configs;
- unsigned int i;
- if (WARN_ON_ONCE(!state))
- return NULL;
- if (state->pads) {
- if (stream)
- return NULL;
- if (pad >= state->sd->entity.num_pads)
- return NULL;
- return &state->pads[pad].compose;
- }
- lockdep_assert_held(state->lock);
- stream_configs = &state->stream_configs;
- for (i = 0; i < stream_configs->num_configs; ++i) {
- if (stream_configs->configs[i].pad == pad &&
- stream_configs->configs[i].stream == stream)
- return &stream_configs->configs[i].compose;
- }
- return NULL;
- }
- EXPORT_SYMBOL_GPL(__v4l2_subdev_state_get_compose);
- struct v4l2_fract *
- __v4l2_subdev_state_get_interval(struct v4l2_subdev_state *state,
- unsigned int pad, u32 stream)
- {
- struct v4l2_subdev_stream_configs *stream_configs;
- unsigned int i;
- if (WARN_ON(!state))
- return NULL;
- lockdep_assert_held(state->lock);
- if (state->pads) {
- if (stream)
- return NULL;
- if (pad >= state->sd->entity.num_pads)
- return NULL;
- return &state->pads[pad].interval;
- }
- lockdep_assert_held(state->lock);
- stream_configs = &state->stream_configs;
- for (i = 0; i < stream_configs->num_configs; ++i) {
- if (stream_configs->configs[i].pad == pad &&
- stream_configs->configs[i].stream == stream)
- return &stream_configs->configs[i].interval;
- }
- return NULL;
- }
- EXPORT_SYMBOL_GPL(__v4l2_subdev_state_get_interval);
- #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
- static int
- v4l2_subdev_init_stream_configs(struct v4l2_subdev_stream_configs *stream_configs,
- const struct v4l2_subdev_krouting *routing)
- {
- struct v4l2_subdev_stream_configs new_configs = { 0 };
- struct v4l2_subdev_route *route;
- u32 idx;
- /* Count number of formats needed */
- for_each_active_route(routing, route) {
- /*
- * Each route needs a format on both ends of the route.
- */
- new_configs.num_configs += 2;
- }
- if (new_configs.num_configs) {
- new_configs.configs = kvcalloc(new_configs.num_configs,
- sizeof(*new_configs.configs),
- GFP_KERNEL);
- if (!new_configs.configs)
- return -ENOMEM;
- }
- /*
- * Fill in the 'pad' and stream' value for each item in the array from
- * the routing table
- */
- idx = 0;
- for_each_active_route(routing, route) {
- new_configs.configs[idx].pad = route->sink_pad;
- new_configs.configs[idx].stream = route->sink_stream;
- idx++;
- new_configs.configs[idx].pad = route->source_pad;
- new_configs.configs[idx].stream = route->source_stream;
- idx++;
- }
- kvfree(stream_configs->configs);
- *stream_configs = new_configs;
- return 0;
- }
- int v4l2_subdev_get_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_state *state,
- struct v4l2_subdev_format *format)
- {
- struct v4l2_mbus_framefmt *fmt;
- fmt = v4l2_subdev_state_get_format(state, format->pad, format->stream);
- if (!fmt)
- return -EINVAL;
- format->format = *fmt;
- return 0;
- }
- EXPORT_SYMBOL_GPL(v4l2_subdev_get_fmt);
- int v4l2_subdev_get_frame_interval(struct v4l2_subdev *sd,
- struct v4l2_subdev_state *state,
- struct v4l2_subdev_frame_interval *fi)
- {
- struct v4l2_fract *interval;
- interval = v4l2_subdev_state_get_interval(state, fi->pad, fi->stream);
- if (!interval)
- return -EINVAL;
- fi->interval = *interval;
- return 0;
- }
- EXPORT_SYMBOL_GPL(v4l2_subdev_get_frame_interval);
- int v4l2_subdev_set_routing(struct v4l2_subdev *sd,
- struct v4l2_subdev_state *state,
- const struct v4l2_subdev_krouting *routing)
- {
- struct v4l2_subdev_krouting *dst = &state->routing;
- const struct v4l2_subdev_krouting *src = routing;
- struct v4l2_subdev_krouting new_routing = { 0 };
- size_t bytes;
- int r;
- if (unlikely(check_mul_overflow((size_t)src->num_routes,
- sizeof(*src->routes), &bytes)))
- return -EOVERFLOW;
- lockdep_assert_held(state->lock);
- if (src->num_routes > 0) {
- new_routing.routes = kmemdup(src->routes, bytes, GFP_KERNEL);
- if (!new_routing.routes)
- return -ENOMEM;
- }
- new_routing.num_routes = src->num_routes;
- r = v4l2_subdev_init_stream_configs(&state->stream_configs,
- &new_routing);
- if (r) {
- kfree(new_routing.routes);
- return r;
- }
- kfree(dst->routes);
- *dst = new_routing;
- return 0;
- }
- EXPORT_SYMBOL_GPL(v4l2_subdev_set_routing);
- struct v4l2_subdev_route *
- __v4l2_subdev_next_active_route(const struct v4l2_subdev_krouting *routing,
- struct v4l2_subdev_route *route)
- {
- if (route)
- ++route;
- else
- route = &routing->routes[0];
- for (; route < routing->routes + routing->num_routes; ++route) {
- if (!(route->flags & V4L2_SUBDEV_ROUTE_FL_ACTIVE))
- continue;
- return route;
- }
- return NULL;
- }
- EXPORT_SYMBOL_GPL(__v4l2_subdev_next_active_route);
- int v4l2_subdev_set_routing_with_fmt(struct v4l2_subdev *sd,
- struct v4l2_subdev_state *state,
- const struct v4l2_subdev_krouting *routing,
- const struct v4l2_mbus_framefmt *fmt)
- {
- struct v4l2_subdev_stream_configs *stream_configs;
- unsigned int i;
- int ret;
- ret = v4l2_subdev_set_routing(sd, state, routing);
- if (ret)
- return ret;
- stream_configs = &state->stream_configs;
- for (i = 0; i < stream_configs->num_configs; ++i)
- stream_configs->configs[i].fmt = *fmt;
- return 0;
- }
- EXPORT_SYMBOL_GPL(v4l2_subdev_set_routing_with_fmt);
- int v4l2_subdev_routing_find_opposite_end(const struct v4l2_subdev_krouting *routing,
- u32 pad, u32 stream, u32 *other_pad,
- u32 *other_stream)
- {
- unsigned int i;
- for (i = 0; i < routing->num_routes; ++i) {
- struct v4l2_subdev_route *route = &routing->routes[i];
- if (route->source_pad == pad &&
- route->source_stream == stream) {
- if (other_pad)
- *other_pad = route->sink_pad;
- if (other_stream)
- *other_stream = route->sink_stream;
- return 0;
- }
- if (route->sink_pad == pad && route->sink_stream == stream) {
- if (other_pad)
- *other_pad = route->source_pad;
- if (other_stream)
- *other_stream = route->source_stream;
- return 0;
- }
- }
- return -EINVAL;
- }
- EXPORT_SYMBOL_GPL(v4l2_subdev_routing_find_opposite_end);
- struct v4l2_mbus_framefmt *
- v4l2_subdev_state_get_opposite_stream_format(struct v4l2_subdev_state *state,
- u32 pad, u32 stream)
- {
- u32 other_pad, other_stream;
- int ret;
- ret = v4l2_subdev_routing_find_opposite_end(&state->routing,
- pad, stream,
- &other_pad, &other_stream);
- if (ret)
- return NULL;
- return v4l2_subdev_state_get_format(state, other_pad, other_stream);
- }
- EXPORT_SYMBOL_GPL(v4l2_subdev_state_get_opposite_stream_format);
- u64 v4l2_subdev_state_xlate_streams(const struct v4l2_subdev_state *state,
- u32 pad0, u32 pad1, u64 *streams)
- {
- const struct v4l2_subdev_krouting *routing = &state->routing;
- struct v4l2_subdev_route *route;
- u64 streams0 = 0;
- u64 streams1 = 0;
- for_each_active_route(routing, route) {
- if (route->sink_pad == pad0 && route->source_pad == pad1 &&
- (*streams & BIT_ULL(route->sink_stream))) {
- streams0 |= BIT_ULL(route->sink_stream);
- streams1 |= BIT_ULL(route->source_stream);
- }
- if (route->source_pad == pad0 && route->sink_pad == pad1 &&
- (*streams & BIT_ULL(route->source_stream))) {
- streams0 |= BIT_ULL(route->source_stream);
- streams1 |= BIT_ULL(route->sink_stream);
- }
- }
- *streams = streams0;
- return streams1;
- }
- EXPORT_SYMBOL_GPL(v4l2_subdev_state_xlate_streams);
- int v4l2_subdev_routing_validate(struct v4l2_subdev *sd,
- const struct v4l2_subdev_krouting *routing,
- enum v4l2_subdev_routing_restriction disallow)
- {
- u32 *remote_pads = NULL;
- unsigned int i, j;
- int ret = -EINVAL;
- if (disallow & (V4L2_SUBDEV_ROUTING_NO_STREAM_MIX |
- V4L2_SUBDEV_ROUTING_NO_MULTIPLEXING)) {
- remote_pads = kcalloc(sd->entity.num_pads, sizeof(*remote_pads),
- GFP_KERNEL);
- if (!remote_pads)
- return -ENOMEM;
- for (i = 0; i < sd->entity.num_pads; ++i)
- remote_pads[i] = U32_MAX;
- }
- for (i = 0; i < routing->num_routes; ++i) {
- const struct v4l2_subdev_route *route = &routing->routes[i];
- /* Validate the sink and source pad numbers. */
- if (route->sink_pad >= sd->entity.num_pads ||
- !(sd->entity.pads[route->sink_pad].flags & MEDIA_PAD_FL_SINK)) {
- dev_dbg(sd->dev, "route %u sink (%u) is not a sink pad\n",
- i, route->sink_pad);
- goto out;
- }
- if (route->source_pad >= sd->entity.num_pads ||
- !(sd->entity.pads[route->source_pad].flags & MEDIA_PAD_FL_SOURCE)) {
- dev_dbg(sd->dev, "route %u source (%u) is not a source pad\n",
- i, route->source_pad);
- goto out;
- }
- /*
- * V4L2_SUBDEV_ROUTING_NO_SINK_STREAM_MIX: all streams from a
- * sink pad must be routed to a single source pad.
- */
- if (disallow & V4L2_SUBDEV_ROUTING_NO_SINK_STREAM_MIX) {
- if (remote_pads[route->sink_pad] != U32_MAX &&
- remote_pads[route->sink_pad] != route->source_pad) {
- dev_dbg(sd->dev,
- "route %u attempts to mix %s streams\n",
- i, "sink");
- goto out;
- }
- }
- /*
- * V4L2_SUBDEV_ROUTING_NO_SOURCE_STREAM_MIX: all streams on a
- * source pad must originate from a single sink pad.
- */
- if (disallow & V4L2_SUBDEV_ROUTING_NO_SOURCE_STREAM_MIX) {
- if (remote_pads[route->source_pad] != U32_MAX &&
- remote_pads[route->source_pad] != route->sink_pad) {
- dev_dbg(sd->dev,
- "route %u attempts to mix %s streams\n",
- i, "source");
- goto out;
- }
- }
- /*
- * V4L2_SUBDEV_ROUTING_NO_SINK_MULTIPLEXING: Pads on the sink
- * side can not do stream multiplexing, i.e. there can be only
- * a single stream in a sink pad.
- */
- if (disallow & V4L2_SUBDEV_ROUTING_NO_SINK_MULTIPLEXING) {
- if (remote_pads[route->sink_pad] != U32_MAX) {
- dev_dbg(sd->dev,
- "route %u attempts to multiplex on %s pad %u\n",
- i, "sink", route->sink_pad);
- goto out;
- }
- }
- /*
- * V4L2_SUBDEV_ROUTING_NO_SOURCE_MULTIPLEXING: Pads on the
- * source side can not do stream multiplexing, i.e. there can
- * be only a single stream in a source pad.
- */
- if (disallow & V4L2_SUBDEV_ROUTING_NO_SOURCE_MULTIPLEXING) {
- if (remote_pads[route->source_pad] != U32_MAX) {
- dev_dbg(sd->dev,
- "route %u attempts to multiplex on %s pad %u\n",
- i, "source", route->source_pad);
- goto out;
- }
- }
- if (remote_pads) {
- remote_pads[route->sink_pad] = route->source_pad;
- remote_pads[route->source_pad] = route->sink_pad;
- }
- for (j = i + 1; j < routing->num_routes; ++j) {
- const struct v4l2_subdev_route *r = &routing->routes[j];
- /*
- * V4L2_SUBDEV_ROUTING_NO_1_TO_N: No two routes can
- * originate from the same (sink) stream.
- */
- if ((disallow & V4L2_SUBDEV_ROUTING_NO_1_TO_N) &&
- route->sink_pad == r->sink_pad &&
- route->sink_stream == r->sink_stream) {
- dev_dbg(sd->dev,
- "routes %u and %u originate from same sink (%u/%u)\n",
- i, j, route->sink_pad,
- route->sink_stream);
- goto out;
- }
- /*
- * V4L2_SUBDEV_ROUTING_NO_N_TO_1: No two routes can end
- * at the same (source) stream.
- */
- if ((disallow & V4L2_SUBDEV_ROUTING_NO_N_TO_1) &&
- route->source_pad == r->source_pad &&
- route->source_stream == r->source_stream) {
- dev_dbg(sd->dev,
- "routes %u and %u end at same source (%u/%u)\n",
- i, j, route->source_pad,
- route->source_stream);
- goto out;
- }
- }
- }
- ret = 0;
- out:
- kfree(remote_pads);
- return ret;
- }
- EXPORT_SYMBOL_GPL(v4l2_subdev_routing_validate);
- static void v4l2_subdev_collect_streams(struct v4l2_subdev *sd,
- struct v4l2_subdev_state *state,
- u32 pad, u64 streams_mask,
- u64 *found_streams,
- u64 *enabled_streams)
- {
- if (!(sd->flags & V4L2_SUBDEV_FL_STREAMS)) {
- *found_streams = BIT_ULL(0);
- *enabled_streams =
- (sd->enabled_pads & BIT_ULL(pad)) ? BIT_ULL(0) : 0;
- return;
- }
- *found_streams = 0;
- *enabled_streams = 0;
- for (unsigned int i = 0; i < state->stream_configs.num_configs; ++i) {
- const struct v4l2_subdev_stream_config *cfg =
- &state->stream_configs.configs[i];
- if (cfg->pad != pad || !(streams_mask & BIT_ULL(cfg->stream)))
- continue;
- *found_streams |= BIT_ULL(cfg->stream);
- if (cfg->enabled)
- *enabled_streams |= BIT_ULL(cfg->stream);
- }
- }
- static void v4l2_subdev_set_streams_enabled(struct v4l2_subdev *sd,
- struct v4l2_subdev_state *state,
- u32 pad, u64 streams_mask,
- bool enabled)
- {
- if (!(sd->flags & V4L2_SUBDEV_FL_STREAMS)) {
- if (enabled)
- sd->enabled_pads |= BIT_ULL(pad);
- else
- sd->enabled_pads &= ~BIT_ULL(pad);
- return;
- }
- for (unsigned int i = 0; i < state->stream_configs.num_configs; ++i) {
- struct v4l2_subdev_stream_config *cfg =
- &state->stream_configs.configs[i];
- if (cfg->pad == pad && (streams_mask & BIT_ULL(cfg->stream)))
- cfg->enabled = enabled;
- }
- }
- int v4l2_subdev_enable_streams(struct v4l2_subdev *sd, u32 pad,
- u64 streams_mask)
- {
- struct device *dev = sd->entity.graph_obj.mdev->dev;
- struct v4l2_subdev_state *state;
- bool already_streaming;
- u64 enabled_streams;
- u64 found_streams;
- bool use_s_stream;
- int ret;
- /* A few basic sanity checks first. */
- if (pad >= sd->entity.num_pads)
- return -EINVAL;
- if (!(sd->entity.pads[pad].flags & MEDIA_PAD_FL_SOURCE))
- return -EOPNOTSUPP;
- /*
- * We use a 64-bit bitmask for tracking enabled pads, so only subdevices
- * with 64 pads or less can be supported.
- */
- if (pad >= sizeof(sd->enabled_pads) * BITS_PER_BYTE)
- return -EOPNOTSUPP;
- if (!streams_mask)
- return 0;
- /* Fallback on .s_stream() if .enable_streams() isn't available. */
- use_s_stream = !v4l2_subdev_has_op(sd, pad, enable_streams);
- if (!use_s_stream)
- state = v4l2_subdev_lock_and_get_active_state(sd);
- else
- state = NULL;
- /*
- * Verify that the requested streams exist and that they are not
- * already enabled.
- */
- v4l2_subdev_collect_streams(sd, state, pad, streams_mask,
- &found_streams, &enabled_streams);
- if (found_streams != streams_mask) {
- dev_dbg(dev, "streams 0x%llx not found on %s:%u\n",
- streams_mask & ~found_streams, sd->entity.name, pad);
- ret = -EINVAL;
- goto done;
- }
- if (enabled_streams) {
- dev_dbg(dev, "streams 0x%llx already enabled on %s:%u\n",
- enabled_streams, sd->entity.name, pad);
- ret = -EALREADY;
- goto done;
- }
- dev_dbg(dev, "enable streams %u:%#llx\n", pad, streams_mask);
- already_streaming = v4l2_subdev_is_streaming(sd);
- if (!use_s_stream) {
- /* Call the .enable_streams() operation. */
- ret = v4l2_subdev_call(sd, pad, enable_streams, state, pad,
- streams_mask);
- } else {
- /* Start streaming when the first pad is enabled. */
- if (!already_streaming)
- ret = v4l2_subdev_call(sd, video, s_stream, 1);
- else
- ret = 0;
- }
- if (ret) {
- dev_dbg(dev, "enable streams %u:%#llx failed: %d\n", pad,
- streams_mask, ret);
- goto done;
- }
- /* Mark the streams as enabled. */
- v4l2_subdev_set_streams_enabled(sd, state, pad, streams_mask, true);
- /*
- * TODO: When all the drivers have been changed to use
- * v4l2_subdev_enable_streams() and v4l2_subdev_disable_streams(),
- * instead of calling .s_stream() operation directly, we can remove
- * the privacy LED handling from call_s_stream() and do it here
- * for all cases.
- */
- if (!use_s_stream && !already_streaming)
- v4l2_subdev_enable_privacy_led(sd);
- done:
- if (!use_s_stream)
- v4l2_subdev_unlock_state(state);
- return ret;
- }
- EXPORT_SYMBOL_GPL(v4l2_subdev_enable_streams);
- int v4l2_subdev_disable_streams(struct v4l2_subdev *sd, u32 pad,
- u64 streams_mask)
- {
- struct device *dev = sd->entity.graph_obj.mdev->dev;
- struct v4l2_subdev_state *state;
- u64 enabled_streams;
- u64 found_streams;
- bool use_s_stream;
- int ret;
- /* A few basic sanity checks first. */
- if (pad >= sd->entity.num_pads)
- return -EINVAL;
- if (!(sd->entity.pads[pad].flags & MEDIA_PAD_FL_SOURCE))
- return -EOPNOTSUPP;
- /*
- * We use a 64-bit bitmask for tracking enabled pads, so only subdevices
- * with 64 pads or less can be supported.
- */
- if (pad >= sizeof(sd->enabled_pads) * BITS_PER_BYTE)
- return -EOPNOTSUPP;
- if (!streams_mask)
- return 0;
- /* Fallback on .s_stream() if .disable_streams() isn't available. */
- use_s_stream = !v4l2_subdev_has_op(sd, pad, disable_streams);
- if (!use_s_stream)
- state = v4l2_subdev_lock_and_get_active_state(sd);
- else
- state = NULL;
- /*
- * Verify that the requested streams exist and that they are not
- * already disabled.
- */
- v4l2_subdev_collect_streams(sd, state, pad, streams_mask,
- &found_streams, &enabled_streams);
- if (found_streams != streams_mask) {
- dev_dbg(dev, "streams 0x%llx not found on %s:%u\n",
- streams_mask & ~found_streams, sd->entity.name, pad);
- ret = -EINVAL;
- goto done;
- }
- if (enabled_streams != streams_mask) {
- dev_dbg(dev, "streams 0x%llx already disabled on %s:%u\n",
- streams_mask & ~enabled_streams, sd->entity.name, pad);
- ret = -EALREADY;
- goto done;
- }
- dev_dbg(dev, "disable streams %u:%#llx\n", pad, streams_mask);
- if (!use_s_stream) {
- /* Call the .disable_streams() operation. */
- ret = v4l2_subdev_call(sd, pad, disable_streams, state, pad,
- streams_mask);
- } else {
- /* Stop streaming when the last streams are disabled. */
- if (!(sd->enabled_pads & ~BIT_ULL(pad)))
- ret = v4l2_subdev_call(sd, video, s_stream, 0);
- else
- ret = 0;
- }
- if (ret) {
- dev_dbg(dev, "disable streams %u:%#llx failed: %d\n", pad,
- streams_mask, ret);
- goto done;
- }
- v4l2_subdev_set_streams_enabled(sd, state, pad, streams_mask, false);
- done:
- if (!use_s_stream) {
- if (!v4l2_subdev_is_streaming(sd))
- v4l2_subdev_disable_privacy_led(sd);
- v4l2_subdev_unlock_state(state);
- }
- return ret;
- }
- EXPORT_SYMBOL_GPL(v4l2_subdev_disable_streams);
- int v4l2_subdev_s_stream_helper(struct v4l2_subdev *sd, int enable)
- {
- struct v4l2_subdev_state *state;
- struct v4l2_subdev_route *route;
- struct media_pad *pad;
- u64 source_mask = 0;
- int pad_index = -1;
- /*
- * Find the source pad. This helper is meant for subdevs that have a
- * single source pad, so failures shouldn't happen, but catch them
- * loudly nonetheless as they indicate a driver bug.
- */
- media_entity_for_each_pad(&sd->entity, pad) {
- if (pad->flags & MEDIA_PAD_FL_SOURCE) {
- pad_index = pad->index;
- break;
- }
- }
- if (WARN_ON(pad_index == -1))
- return -EINVAL;
- if (sd->flags & V4L2_SUBDEV_FL_STREAMS) {
- /*
- * As there's a single source pad, just collect all the source
- * streams.
- */
- state = v4l2_subdev_lock_and_get_active_state(sd);
- for_each_active_route(&state->routing, route)
- source_mask |= BIT_ULL(route->source_stream);
- v4l2_subdev_unlock_state(state);
- } else {
- /*
- * For non-streams subdevices, there's a single implicit stream
- * per pad.
- */
- source_mask = BIT_ULL(0);
- }
- if (enable)
- return v4l2_subdev_enable_streams(sd, pad_index, source_mask);
- else
- return v4l2_subdev_disable_streams(sd, pad_index, source_mask);
- }
- EXPORT_SYMBOL_GPL(v4l2_subdev_s_stream_helper);
- #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
- #endif /* CONFIG_MEDIA_CONTROLLER */
- void v4l2_subdev_init(struct v4l2_subdev *sd, const struct v4l2_subdev_ops *ops)
- {
- INIT_LIST_HEAD(&sd->list);
- BUG_ON(!ops);
- sd->ops = ops;
- sd->v4l2_dev = NULL;
- sd->flags = 0;
- sd->name[0] = '\0';
- sd->grp_id = 0;
- sd->dev_priv = NULL;
- sd->host_priv = NULL;
- sd->privacy_led = NULL;
- INIT_LIST_HEAD(&sd->async_subdev_endpoint_list);
- #if defined(CONFIG_MEDIA_CONTROLLER)
- sd->entity.name = sd->name;
- sd->entity.obj_type = MEDIA_ENTITY_TYPE_V4L2_SUBDEV;
- sd->entity.function = MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN;
- #endif
- }
- EXPORT_SYMBOL(v4l2_subdev_init);
- void v4l2_subdev_notify_event(struct v4l2_subdev *sd,
- const struct v4l2_event *ev)
- {
- v4l2_event_queue(sd->devnode, ev);
- v4l2_subdev_notify(sd, V4L2_DEVICE_NOTIFY_EVENT, (void *)ev);
- }
- EXPORT_SYMBOL_GPL(v4l2_subdev_notify_event);
- bool v4l2_subdev_is_streaming(struct v4l2_subdev *sd)
- {
- struct v4l2_subdev_state *state;
- if (!v4l2_subdev_has_op(sd, pad, enable_streams))
- return sd->s_stream_enabled;
- if (!(sd->flags & V4L2_SUBDEV_FL_STREAMS))
- return !!sd->enabled_pads;
- state = v4l2_subdev_get_locked_active_state(sd);
- for (unsigned int i = 0; i < state->stream_configs.num_configs; ++i) {
- const struct v4l2_subdev_stream_config *cfg;
- cfg = &state->stream_configs.configs[i];
- if (cfg->enabled)
- return true;
- }
- return false;
- }
- EXPORT_SYMBOL_GPL(v4l2_subdev_is_streaming);
- int v4l2_subdev_get_privacy_led(struct v4l2_subdev *sd)
- {
- #if IS_REACHABLE(CONFIG_LEDS_CLASS)
- sd->privacy_led = led_get(sd->dev, "privacy-led");
- if (IS_ERR(sd->privacy_led) && PTR_ERR(sd->privacy_led) != -ENOENT)
- return dev_err_probe(sd->dev, PTR_ERR(sd->privacy_led),
- "getting privacy LED\n");
- if (!IS_ERR_OR_NULL(sd->privacy_led)) {
- mutex_lock(&sd->privacy_led->led_access);
- led_sysfs_disable(sd->privacy_led);
- led_trigger_remove(sd->privacy_led);
- led_set_brightness(sd->privacy_led, 0);
- mutex_unlock(&sd->privacy_led->led_access);
- }
- #endif
- return 0;
- }
- EXPORT_SYMBOL_GPL(v4l2_subdev_get_privacy_led);
- void v4l2_subdev_put_privacy_led(struct v4l2_subdev *sd)
- {
- #if IS_REACHABLE(CONFIG_LEDS_CLASS)
- if (!IS_ERR_OR_NULL(sd->privacy_led)) {
- mutex_lock(&sd->privacy_led->led_access);
- led_sysfs_enable(sd->privacy_led);
- mutex_unlock(&sd->privacy_led->led_access);
- led_put(sd->privacy_led);
- }
- #endif
- }
- EXPORT_SYMBOL_GPL(v4l2_subdev_put_privacy_led);
|