v4l2-subdev.c 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * V4L2 sub-device
  4. *
  5. * Copyright (C) 2010 Nokia Corporation
  6. *
  7. * Contact: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
  8. * Sakari Ailus <sakari.ailus@iki.fi>
  9. */
  10. #include <linux/export.h>
  11. #include <linux/ioctl.h>
  12. #include <linux/leds.h>
  13. #include <linux/mm.h>
  14. #include <linux/module.h>
  15. #include <linux/overflow.h>
  16. #include <linux/slab.h>
  17. #include <linux/string.h>
  18. #include <linux/types.h>
  19. #include <linux/version.h>
  20. #include <linux/videodev2.h>
  21. #include <media/v4l2-ctrls.h>
  22. #include <media/v4l2-device.h>
  23. #include <media/v4l2-event.h>
  24. #include <media/v4l2-fh.h>
  25. #include <media/v4l2-ioctl.h>
  26. #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
  27. /*
  28. * The Streams API is an experimental feature. To use the Streams API, set
  29. * 'v4l2_subdev_enable_streams_api' to 1 below.
  30. */
  31. static bool v4l2_subdev_enable_streams_api;
  32. #endif
  33. /*
  34. * Maximum stream ID is 63 for now, as we use u64 bitmask to represent a set
  35. * of streams.
  36. *
  37. * Note that V4L2_FRAME_DESC_ENTRY_MAX is related: V4L2_FRAME_DESC_ENTRY_MAX
  38. * restricts the total number of streams in a pad, although the stream ID is
  39. * not restricted.
  40. */
  41. #define V4L2_SUBDEV_MAX_STREAM_ID 63
  42. #include "v4l2-subdev-priv.h"
  43. #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
  44. static int subdev_fh_init(struct v4l2_subdev_fh *fh, struct v4l2_subdev *sd)
  45. {
  46. struct v4l2_subdev_state *state;
  47. static struct lock_class_key key;
  48. state = __v4l2_subdev_state_alloc(sd, "fh->state->lock", &key);
  49. if (IS_ERR(state))
  50. return PTR_ERR(state);
  51. fh->state = state;
  52. return 0;
  53. }
  54. static void subdev_fh_free(struct v4l2_subdev_fh *fh)
  55. {
  56. __v4l2_subdev_state_free(fh->state);
  57. fh->state = NULL;
  58. }
  59. static int subdev_open(struct file *file)
  60. {
  61. struct video_device *vdev = video_devdata(file);
  62. struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
  63. struct v4l2_subdev_fh *subdev_fh;
  64. int ret;
  65. subdev_fh = kzalloc(sizeof(*subdev_fh), GFP_KERNEL);
  66. if (subdev_fh == NULL)
  67. return -ENOMEM;
  68. ret = subdev_fh_init(subdev_fh, sd);
  69. if (ret) {
  70. kfree(subdev_fh);
  71. return ret;
  72. }
  73. v4l2_fh_init(&subdev_fh->vfh, vdev);
  74. v4l2_fh_add(&subdev_fh->vfh);
  75. file->private_data = &subdev_fh->vfh;
  76. if (sd->v4l2_dev->mdev && sd->entity.graph_obj.mdev->dev) {
  77. struct module *owner;
  78. owner = sd->entity.graph_obj.mdev->dev->driver->owner;
  79. if (!try_module_get(owner)) {
  80. ret = -EBUSY;
  81. goto err;
  82. }
  83. subdev_fh->owner = owner;
  84. }
  85. if (sd->internal_ops && sd->internal_ops->open) {
  86. ret = sd->internal_ops->open(sd, subdev_fh);
  87. if (ret < 0)
  88. goto err;
  89. }
  90. return 0;
  91. err:
  92. module_put(subdev_fh->owner);
  93. v4l2_fh_del(&subdev_fh->vfh);
  94. v4l2_fh_exit(&subdev_fh->vfh);
  95. subdev_fh_free(subdev_fh);
  96. kfree(subdev_fh);
  97. return ret;
  98. }
  99. static int subdev_close(struct file *file)
  100. {
  101. struct video_device *vdev = video_devdata(file);
  102. struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
  103. struct v4l2_fh *vfh = file->private_data;
  104. struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh);
  105. if (sd->internal_ops && sd->internal_ops->close)
  106. sd->internal_ops->close(sd, subdev_fh);
  107. module_put(subdev_fh->owner);
  108. v4l2_fh_del(vfh);
  109. v4l2_fh_exit(vfh);
  110. subdev_fh_free(subdev_fh);
  111. kfree(subdev_fh);
  112. file->private_data = NULL;
  113. return 0;
  114. }
  115. #else /* CONFIG_VIDEO_V4L2_SUBDEV_API */
  116. static int subdev_open(struct file *file)
  117. {
  118. return -ENODEV;
  119. }
  120. static int subdev_close(struct file *file)
  121. {
  122. return -ENODEV;
  123. }
  124. #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
  125. static void v4l2_subdev_enable_privacy_led(struct v4l2_subdev *sd)
  126. {
  127. #if IS_REACHABLE(CONFIG_LEDS_CLASS)
  128. if (!IS_ERR_OR_NULL(sd->privacy_led))
  129. led_set_brightness(sd->privacy_led,
  130. sd->privacy_led->max_brightness);
  131. #endif
  132. }
  133. static void v4l2_subdev_disable_privacy_led(struct v4l2_subdev *sd)
  134. {
  135. #if IS_REACHABLE(CONFIG_LEDS_CLASS)
  136. if (!IS_ERR_OR_NULL(sd->privacy_led))
  137. led_set_brightness(sd->privacy_led, 0);
  138. #endif
  139. }
  140. static inline int check_which(u32 which)
  141. {
  142. if (which != V4L2_SUBDEV_FORMAT_TRY &&
  143. which != V4L2_SUBDEV_FORMAT_ACTIVE)
  144. return -EINVAL;
  145. return 0;
  146. }
  147. static inline int check_pad(struct v4l2_subdev *sd, u32 pad)
  148. {
  149. #if defined(CONFIG_MEDIA_CONTROLLER)
  150. if (sd->entity.num_pads) {
  151. if (pad >= sd->entity.num_pads)
  152. return -EINVAL;
  153. return 0;
  154. }
  155. #endif
  156. /* allow pad 0 on subdevices not registered as media entities */
  157. if (pad > 0)
  158. return -EINVAL;
  159. return 0;
  160. }
  161. static int check_state(struct v4l2_subdev *sd, struct v4l2_subdev_state *state,
  162. u32 which, u32 pad, u32 stream)
  163. {
  164. if (sd->flags & V4L2_SUBDEV_FL_STREAMS) {
  165. #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
  166. if (!v4l2_subdev_state_get_format(state, pad, stream))
  167. return -EINVAL;
  168. return 0;
  169. #else
  170. return -EINVAL;
  171. #endif
  172. }
  173. if (stream != 0)
  174. return -EINVAL;
  175. if (which == V4L2_SUBDEV_FORMAT_TRY && (!state || !state->pads))
  176. return -EINVAL;
  177. return 0;
  178. }
  179. static inline int check_format(struct v4l2_subdev *sd,
  180. struct v4l2_subdev_state *state,
  181. struct v4l2_subdev_format *format)
  182. {
  183. if (!format)
  184. return -EINVAL;
  185. return check_which(format->which) ? : check_pad(sd, format->pad) ? :
  186. check_state(sd, state, format->which, format->pad, format->stream);
  187. }
  188. static int call_get_fmt(struct v4l2_subdev *sd,
  189. struct v4l2_subdev_state *state,
  190. struct v4l2_subdev_format *format)
  191. {
  192. return check_format(sd, state, format) ? :
  193. sd->ops->pad->get_fmt(sd, state, format);
  194. }
  195. static int call_set_fmt(struct v4l2_subdev *sd,
  196. struct v4l2_subdev_state *state,
  197. struct v4l2_subdev_format *format)
  198. {
  199. return check_format(sd, state, format) ? :
  200. sd->ops->pad->set_fmt(sd, state, format);
  201. }
  202. static int call_enum_mbus_code(struct v4l2_subdev *sd,
  203. struct v4l2_subdev_state *state,
  204. struct v4l2_subdev_mbus_code_enum *code)
  205. {
  206. if (!code)
  207. return -EINVAL;
  208. return check_which(code->which) ? : check_pad(sd, code->pad) ? :
  209. check_state(sd, state, code->which, code->pad, code->stream) ? :
  210. sd->ops->pad->enum_mbus_code(sd, state, code);
  211. }
  212. static int call_enum_frame_size(struct v4l2_subdev *sd,
  213. struct v4l2_subdev_state *state,
  214. struct v4l2_subdev_frame_size_enum *fse)
  215. {
  216. if (!fse)
  217. return -EINVAL;
  218. return check_which(fse->which) ? : check_pad(sd, fse->pad) ? :
  219. check_state(sd, state, fse->which, fse->pad, fse->stream) ? :
  220. sd->ops->pad->enum_frame_size(sd, state, fse);
  221. }
  222. static int call_enum_frame_interval(struct v4l2_subdev *sd,
  223. struct v4l2_subdev_state *state,
  224. struct v4l2_subdev_frame_interval_enum *fie)
  225. {
  226. if (!fie)
  227. return -EINVAL;
  228. return check_which(fie->which) ? : check_pad(sd, fie->pad) ? :
  229. check_state(sd, state, fie->which, fie->pad, fie->stream) ? :
  230. sd->ops->pad->enum_frame_interval(sd, state, fie);
  231. }
  232. static inline int check_selection(struct v4l2_subdev *sd,
  233. struct v4l2_subdev_state *state,
  234. struct v4l2_subdev_selection *sel)
  235. {
  236. if (!sel)
  237. return -EINVAL;
  238. return check_which(sel->which) ? : check_pad(sd, sel->pad) ? :
  239. check_state(sd, state, sel->which, sel->pad, sel->stream);
  240. }
  241. static int call_get_selection(struct v4l2_subdev *sd,
  242. struct v4l2_subdev_state *state,
  243. struct v4l2_subdev_selection *sel)
  244. {
  245. return check_selection(sd, state, sel) ? :
  246. sd->ops->pad->get_selection(sd, state, sel);
  247. }
  248. static int call_set_selection(struct v4l2_subdev *sd,
  249. struct v4l2_subdev_state *state,
  250. struct v4l2_subdev_selection *sel)
  251. {
  252. return check_selection(sd, state, sel) ? :
  253. sd->ops->pad->set_selection(sd, state, sel);
  254. }
  255. static inline int check_frame_interval(struct v4l2_subdev *sd,
  256. struct v4l2_subdev_state *state,
  257. struct v4l2_subdev_frame_interval *fi)
  258. {
  259. if (!fi)
  260. return -EINVAL;
  261. return check_which(fi->which) ? : check_pad(sd, fi->pad) ? :
  262. check_state(sd, state, fi->which, fi->pad, fi->stream);
  263. }
  264. static int call_get_frame_interval(struct v4l2_subdev *sd,
  265. struct v4l2_subdev_state *state,
  266. struct v4l2_subdev_frame_interval *fi)
  267. {
  268. return check_frame_interval(sd, state, fi) ? :
  269. sd->ops->pad->get_frame_interval(sd, state, fi);
  270. }
  271. static int call_set_frame_interval(struct v4l2_subdev *sd,
  272. struct v4l2_subdev_state *state,
  273. struct v4l2_subdev_frame_interval *fi)
  274. {
  275. return check_frame_interval(sd, state, fi) ? :
  276. sd->ops->pad->set_frame_interval(sd, state, fi);
  277. }
  278. static int call_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
  279. struct v4l2_mbus_frame_desc *fd)
  280. {
  281. unsigned int i;
  282. int ret;
  283. memset(fd, 0, sizeof(*fd));
  284. ret = sd->ops->pad->get_frame_desc(sd, pad, fd);
  285. if (ret)
  286. return ret;
  287. dev_dbg(sd->dev, "Frame descriptor on pad %u, type %s\n", pad,
  288. fd->type == V4L2_MBUS_FRAME_DESC_TYPE_PARALLEL ? "parallel" :
  289. fd->type == V4L2_MBUS_FRAME_DESC_TYPE_CSI2 ? "CSI-2" :
  290. "unknown");
  291. for (i = 0; i < fd->num_entries; i++) {
  292. struct v4l2_mbus_frame_desc_entry *entry = &fd->entry[i];
  293. char buf[20] = "";
  294. if (fd->type == V4L2_MBUS_FRAME_DESC_TYPE_CSI2)
  295. WARN_ON(snprintf(buf, sizeof(buf),
  296. ", vc %u, dt 0x%02x",
  297. entry->bus.csi2.vc,
  298. entry->bus.csi2.dt) >= sizeof(buf));
  299. dev_dbg(sd->dev,
  300. "\tstream %u, code 0x%04x, length %u, flags 0x%04x%s\n",
  301. entry->stream, entry->pixelcode, entry->length,
  302. entry->flags, buf);
  303. }
  304. return 0;
  305. }
  306. static inline int check_edid(struct v4l2_subdev *sd,
  307. struct v4l2_subdev_edid *edid)
  308. {
  309. if (!edid)
  310. return -EINVAL;
  311. if (edid->blocks && edid->edid == NULL)
  312. return -EINVAL;
  313. return check_pad(sd, edid->pad);
  314. }
  315. static int call_get_edid(struct v4l2_subdev *sd, struct v4l2_subdev_edid *edid)
  316. {
  317. return check_edid(sd, edid) ? : sd->ops->pad->get_edid(sd, edid);
  318. }
  319. static int call_set_edid(struct v4l2_subdev *sd, struct v4l2_subdev_edid *edid)
  320. {
  321. return check_edid(sd, edid) ? : sd->ops->pad->set_edid(sd, edid);
  322. }
  323. static int call_s_dv_timings(struct v4l2_subdev *sd, unsigned int pad,
  324. struct v4l2_dv_timings *timings)
  325. {
  326. if (!timings)
  327. return -EINVAL;
  328. return check_pad(sd, pad) ? :
  329. sd->ops->pad->s_dv_timings(sd, pad, timings);
  330. }
  331. static int call_g_dv_timings(struct v4l2_subdev *sd, unsigned int pad,
  332. struct v4l2_dv_timings *timings)
  333. {
  334. if (!timings)
  335. return -EINVAL;
  336. return check_pad(sd, pad) ? :
  337. sd->ops->pad->g_dv_timings(sd, pad, timings);
  338. }
  339. static int call_query_dv_timings(struct v4l2_subdev *sd, unsigned int pad,
  340. struct v4l2_dv_timings *timings)
  341. {
  342. if (!timings)
  343. return -EINVAL;
  344. return check_pad(sd, pad) ? :
  345. sd->ops->pad->query_dv_timings(sd, pad, timings);
  346. }
  347. static int call_dv_timings_cap(struct v4l2_subdev *sd,
  348. struct v4l2_dv_timings_cap *cap)
  349. {
  350. if (!cap)
  351. return -EINVAL;
  352. return check_pad(sd, cap->pad) ? :
  353. sd->ops->pad->dv_timings_cap(sd, cap);
  354. }
  355. static int call_enum_dv_timings(struct v4l2_subdev *sd,
  356. struct v4l2_enum_dv_timings *dvt)
  357. {
  358. if (!dvt)
  359. return -EINVAL;
  360. return check_pad(sd, dvt->pad) ? :
  361. sd->ops->pad->enum_dv_timings(sd, dvt);
  362. }
  363. static int call_get_mbus_config(struct v4l2_subdev *sd, unsigned int pad,
  364. struct v4l2_mbus_config *config)
  365. {
  366. return check_pad(sd, pad) ? :
  367. sd->ops->pad->get_mbus_config(sd, pad, config);
  368. }
  369. static int call_s_stream(struct v4l2_subdev *sd, int enable)
  370. {
  371. int ret;
  372. /*
  373. * The .s_stream() operation must never be called to start or stop an
  374. * already started or stopped subdev. Catch offenders but don't return
  375. * an error yet to avoid regressions.
  376. */
  377. if (WARN_ON(sd->s_stream_enabled == !!enable))
  378. return 0;
  379. ret = sd->ops->video->s_stream(sd, enable);
  380. if (!enable && ret < 0) {
  381. dev_warn(sd->dev, "disabling streaming failed (%d)\n", ret);
  382. ret = 0;
  383. }
  384. if (!ret) {
  385. sd->s_stream_enabled = enable;
  386. if (enable)
  387. v4l2_subdev_enable_privacy_led(sd);
  388. else
  389. v4l2_subdev_disable_privacy_led(sd);
  390. }
  391. return ret;
  392. }
  393. #ifdef CONFIG_MEDIA_CONTROLLER
  394. /*
  395. * Create state-management wrapper for pad ops dealing with subdev state. The
  396. * wrapper handles the case where the caller does not provide the called
  397. * subdev's state. This should be removed when all the callers are fixed.
  398. */
  399. #define DEFINE_STATE_WRAPPER(f, arg_type) \
  400. static int call_##f##_state(struct v4l2_subdev *sd, \
  401. struct v4l2_subdev_state *_state, \
  402. arg_type *arg) \
  403. { \
  404. struct v4l2_subdev_state *state = _state; \
  405. int ret; \
  406. if (!_state) \
  407. state = v4l2_subdev_lock_and_get_active_state(sd); \
  408. ret = call_##f(sd, state, arg); \
  409. if (!_state && state) \
  410. v4l2_subdev_unlock_state(state); \
  411. return ret; \
  412. }
  413. #else /* CONFIG_MEDIA_CONTROLLER */
  414. #define DEFINE_STATE_WRAPPER(f, arg_type) \
  415. static int call_##f##_state(struct v4l2_subdev *sd, \
  416. struct v4l2_subdev_state *state, \
  417. arg_type *arg) \
  418. { \
  419. return call_##f(sd, state, arg); \
  420. }
  421. #endif /* CONFIG_MEDIA_CONTROLLER */
  422. DEFINE_STATE_WRAPPER(get_fmt, struct v4l2_subdev_format);
  423. DEFINE_STATE_WRAPPER(set_fmt, struct v4l2_subdev_format);
  424. DEFINE_STATE_WRAPPER(enum_mbus_code, struct v4l2_subdev_mbus_code_enum);
  425. DEFINE_STATE_WRAPPER(enum_frame_size, struct v4l2_subdev_frame_size_enum);
  426. DEFINE_STATE_WRAPPER(enum_frame_interval, struct v4l2_subdev_frame_interval_enum);
  427. DEFINE_STATE_WRAPPER(get_selection, struct v4l2_subdev_selection);
  428. DEFINE_STATE_WRAPPER(set_selection, struct v4l2_subdev_selection);
  429. static const struct v4l2_subdev_pad_ops v4l2_subdev_call_pad_wrappers = {
  430. .get_fmt = call_get_fmt_state,
  431. .set_fmt = call_set_fmt_state,
  432. .enum_mbus_code = call_enum_mbus_code_state,
  433. .enum_frame_size = call_enum_frame_size_state,
  434. .enum_frame_interval = call_enum_frame_interval_state,
  435. .get_selection = call_get_selection_state,
  436. .set_selection = call_set_selection_state,
  437. .get_frame_interval = call_get_frame_interval,
  438. .set_frame_interval = call_set_frame_interval,
  439. .get_edid = call_get_edid,
  440. .set_edid = call_set_edid,
  441. .s_dv_timings = call_s_dv_timings,
  442. .g_dv_timings = call_g_dv_timings,
  443. .query_dv_timings = call_query_dv_timings,
  444. .dv_timings_cap = call_dv_timings_cap,
  445. .enum_dv_timings = call_enum_dv_timings,
  446. .get_frame_desc = call_get_frame_desc,
  447. .get_mbus_config = call_get_mbus_config,
  448. };
  449. static const struct v4l2_subdev_video_ops v4l2_subdev_call_video_wrappers = {
  450. .s_stream = call_s_stream,
  451. };
  452. const struct v4l2_subdev_ops v4l2_subdev_call_wrappers = {
  453. .pad = &v4l2_subdev_call_pad_wrappers,
  454. .video = &v4l2_subdev_call_video_wrappers,
  455. };
  456. EXPORT_SYMBOL(v4l2_subdev_call_wrappers);
  457. #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
  458. static struct v4l2_subdev_state *
  459. subdev_ioctl_get_state(struct v4l2_subdev *sd, struct v4l2_subdev_fh *subdev_fh,
  460. unsigned int cmd, void *arg)
  461. {
  462. u32 which;
  463. switch (cmd) {
  464. default:
  465. return NULL;
  466. case VIDIOC_SUBDEV_G_FMT:
  467. case VIDIOC_SUBDEV_S_FMT:
  468. which = ((struct v4l2_subdev_format *)arg)->which;
  469. break;
  470. case VIDIOC_SUBDEV_G_CROP:
  471. case VIDIOC_SUBDEV_S_CROP:
  472. which = ((struct v4l2_subdev_crop *)arg)->which;
  473. break;
  474. case VIDIOC_SUBDEV_ENUM_MBUS_CODE:
  475. which = ((struct v4l2_subdev_mbus_code_enum *)arg)->which;
  476. break;
  477. case VIDIOC_SUBDEV_ENUM_FRAME_SIZE:
  478. which = ((struct v4l2_subdev_frame_size_enum *)arg)->which;
  479. break;
  480. case VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL:
  481. which = ((struct v4l2_subdev_frame_interval_enum *)arg)->which;
  482. break;
  483. case VIDIOC_SUBDEV_G_SELECTION:
  484. case VIDIOC_SUBDEV_S_SELECTION:
  485. which = ((struct v4l2_subdev_selection *)arg)->which;
  486. break;
  487. case VIDIOC_SUBDEV_G_FRAME_INTERVAL:
  488. case VIDIOC_SUBDEV_S_FRAME_INTERVAL: {
  489. struct v4l2_subdev_frame_interval *fi = arg;
  490. if (!(subdev_fh->client_caps &
  491. V4L2_SUBDEV_CLIENT_CAP_INTERVAL_USES_WHICH))
  492. fi->which = V4L2_SUBDEV_FORMAT_ACTIVE;
  493. which = fi->which;
  494. break;
  495. }
  496. case VIDIOC_SUBDEV_G_ROUTING:
  497. case VIDIOC_SUBDEV_S_ROUTING:
  498. which = ((struct v4l2_subdev_routing *)arg)->which;
  499. break;
  500. }
  501. return which == V4L2_SUBDEV_FORMAT_TRY ?
  502. subdev_fh->state :
  503. v4l2_subdev_get_unlocked_active_state(sd);
  504. }
  505. static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg,
  506. struct v4l2_subdev_state *state)
  507. {
  508. struct video_device *vdev = video_devdata(file);
  509. struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
  510. struct v4l2_fh *vfh = file->private_data;
  511. struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh);
  512. bool ro_subdev = test_bit(V4L2_FL_SUBDEV_RO_DEVNODE, &vdev->flags);
  513. bool streams_subdev = sd->flags & V4L2_SUBDEV_FL_STREAMS;
  514. bool client_supports_streams = subdev_fh->client_caps &
  515. V4L2_SUBDEV_CLIENT_CAP_STREAMS;
  516. int rval;
  517. /*
  518. * If the streams API is not enabled, remove V4L2_SUBDEV_CAP_STREAMS.
  519. * Remove this when the API is no longer experimental.
  520. */
  521. if (!v4l2_subdev_enable_streams_api)
  522. streams_subdev = false;
  523. switch (cmd) {
  524. case VIDIOC_SUBDEV_QUERYCAP: {
  525. struct v4l2_subdev_capability *cap = arg;
  526. memset(cap->reserved, 0, sizeof(cap->reserved));
  527. cap->version = LINUX_VERSION_CODE;
  528. cap->capabilities =
  529. (ro_subdev ? V4L2_SUBDEV_CAP_RO_SUBDEV : 0) |
  530. (streams_subdev ? V4L2_SUBDEV_CAP_STREAMS : 0);
  531. return 0;
  532. }
  533. case VIDIOC_QUERYCTRL:
  534. /*
  535. * TODO: this really should be folded into v4l2_queryctrl (this
  536. * currently returns -EINVAL for NULL control handlers).
  537. * However, v4l2_queryctrl() is still called directly by
  538. * drivers as well and until that has been addressed I believe
  539. * it is safer to do the check here. The same is true for the
  540. * other control ioctls below.
  541. */
  542. if (!vfh->ctrl_handler)
  543. return -ENOTTY;
  544. return v4l2_queryctrl(vfh->ctrl_handler, arg);
  545. case VIDIOC_QUERY_EXT_CTRL:
  546. if (!vfh->ctrl_handler)
  547. return -ENOTTY;
  548. return v4l2_query_ext_ctrl(vfh->ctrl_handler, arg);
  549. case VIDIOC_QUERYMENU:
  550. if (!vfh->ctrl_handler)
  551. return -ENOTTY;
  552. return v4l2_querymenu(vfh->ctrl_handler, arg);
  553. case VIDIOC_G_CTRL:
  554. if (!vfh->ctrl_handler)
  555. return -ENOTTY;
  556. return v4l2_g_ctrl(vfh->ctrl_handler, arg);
  557. case VIDIOC_S_CTRL:
  558. if (!vfh->ctrl_handler)
  559. return -ENOTTY;
  560. return v4l2_s_ctrl(vfh, vfh->ctrl_handler, arg);
  561. case VIDIOC_G_EXT_CTRLS:
  562. if (!vfh->ctrl_handler)
  563. return -ENOTTY;
  564. return v4l2_g_ext_ctrls(vfh->ctrl_handler,
  565. vdev, sd->v4l2_dev->mdev, arg);
  566. case VIDIOC_S_EXT_CTRLS:
  567. if (!vfh->ctrl_handler)
  568. return -ENOTTY;
  569. return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler,
  570. vdev, sd->v4l2_dev->mdev, arg);
  571. case VIDIOC_TRY_EXT_CTRLS:
  572. if (!vfh->ctrl_handler)
  573. return -ENOTTY;
  574. return v4l2_try_ext_ctrls(vfh->ctrl_handler,
  575. vdev, sd->v4l2_dev->mdev, arg);
  576. case VIDIOC_DQEVENT:
  577. if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
  578. return -ENOIOCTLCMD;
  579. return v4l2_event_dequeue(vfh, arg, file->f_flags & O_NONBLOCK);
  580. case VIDIOC_SUBSCRIBE_EVENT:
  581. return v4l2_subdev_call(sd, core, subscribe_event, vfh, arg);
  582. case VIDIOC_UNSUBSCRIBE_EVENT:
  583. return v4l2_subdev_call(sd, core, unsubscribe_event, vfh, arg);
  584. #ifdef CONFIG_VIDEO_ADV_DEBUG
  585. case VIDIOC_DBG_G_REGISTER:
  586. {
  587. struct v4l2_dbg_register *p = arg;
  588. if (!capable(CAP_SYS_ADMIN))
  589. return -EPERM;
  590. return v4l2_subdev_call(sd, core, g_register, p);
  591. }
  592. case VIDIOC_DBG_S_REGISTER:
  593. {
  594. struct v4l2_dbg_register *p = arg;
  595. if (!capable(CAP_SYS_ADMIN))
  596. return -EPERM;
  597. return v4l2_subdev_call(sd, core, s_register, p);
  598. }
  599. case VIDIOC_DBG_G_CHIP_INFO:
  600. {
  601. struct v4l2_dbg_chip_info *p = arg;
  602. if (p->match.type != V4L2_CHIP_MATCH_SUBDEV || p->match.addr)
  603. return -EINVAL;
  604. if (sd->ops->core && sd->ops->core->s_register)
  605. p->flags |= V4L2_CHIP_FL_WRITABLE;
  606. if (sd->ops->core && sd->ops->core->g_register)
  607. p->flags |= V4L2_CHIP_FL_READABLE;
  608. strscpy(p->name, sd->name, sizeof(p->name));
  609. return 0;
  610. }
  611. #endif
  612. case VIDIOC_LOG_STATUS: {
  613. int ret;
  614. pr_info("%s: ================= START STATUS =================\n",
  615. sd->name);
  616. ret = v4l2_subdev_call(sd, core, log_status);
  617. pr_info("%s: ================== END STATUS ==================\n",
  618. sd->name);
  619. return ret;
  620. }
  621. case VIDIOC_SUBDEV_G_FMT: {
  622. struct v4l2_subdev_format *format = arg;
  623. if (!client_supports_streams)
  624. format->stream = 0;
  625. memset(format->reserved, 0, sizeof(format->reserved));
  626. memset(format->format.reserved, 0, sizeof(format->format.reserved));
  627. return v4l2_subdev_call(sd, pad, get_fmt, state, format);
  628. }
  629. case VIDIOC_SUBDEV_S_FMT: {
  630. struct v4l2_subdev_format *format = arg;
  631. if (format->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev)
  632. return -EPERM;
  633. if (!client_supports_streams)
  634. format->stream = 0;
  635. memset(format->reserved, 0, sizeof(format->reserved));
  636. memset(format->format.reserved, 0, sizeof(format->format.reserved));
  637. return v4l2_subdev_call(sd, pad, set_fmt, state, format);
  638. }
  639. case VIDIOC_SUBDEV_G_CROP: {
  640. struct v4l2_subdev_crop *crop = arg;
  641. struct v4l2_subdev_selection sel;
  642. if (!client_supports_streams)
  643. crop->stream = 0;
  644. memset(crop->reserved, 0, sizeof(crop->reserved));
  645. memset(&sel, 0, sizeof(sel));
  646. sel.which = crop->which;
  647. sel.pad = crop->pad;
  648. sel.stream = crop->stream;
  649. sel.target = V4L2_SEL_TGT_CROP;
  650. rval = v4l2_subdev_call(
  651. sd, pad, get_selection, state, &sel);
  652. crop->rect = sel.r;
  653. return rval;
  654. }
  655. case VIDIOC_SUBDEV_S_CROP: {
  656. struct v4l2_subdev_crop *crop = arg;
  657. struct v4l2_subdev_selection sel;
  658. if (crop->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev)
  659. return -EPERM;
  660. if (!client_supports_streams)
  661. crop->stream = 0;
  662. memset(crop->reserved, 0, sizeof(crop->reserved));
  663. memset(&sel, 0, sizeof(sel));
  664. sel.which = crop->which;
  665. sel.pad = crop->pad;
  666. sel.stream = crop->stream;
  667. sel.target = V4L2_SEL_TGT_CROP;
  668. sel.r = crop->rect;
  669. rval = v4l2_subdev_call(
  670. sd, pad, set_selection, state, &sel);
  671. crop->rect = sel.r;
  672. return rval;
  673. }
  674. case VIDIOC_SUBDEV_ENUM_MBUS_CODE: {
  675. struct v4l2_subdev_mbus_code_enum *code = arg;
  676. if (!client_supports_streams)
  677. code->stream = 0;
  678. memset(code->reserved, 0, sizeof(code->reserved));
  679. return v4l2_subdev_call(sd, pad, enum_mbus_code, state,
  680. code);
  681. }
  682. case VIDIOC_SUBDEV_ENUM_FRAME_SIZE: {
  683. struct v4l2_subdev_frame_size_enum *fse = arg;
  684. if (!client_supports_streams)
  685. fse->stream = 0;
  686. memset(fse->reserved, 0, sizeof(fse->reserved));
  687. return v4l2_subdev_call(sd, pad, enum_frame_size, state,
  688. fse);
  689. }
  690. case VIDIOC_SUBDEV_G_FRAME_INTERVAL: {
  691. struct v4l2_subdev_frame_interval *fi = arg;
  692. if (!client_supports_streams)
  693. fi->stream = 0;
  694. memset(fi->reserved, 0, sizeof(fi->reserved));
  695. return v4l2_subdev_call(sd, pad, get_frame_interval, state, fi);
  696. }
  697. case VIDIOC_SUBDEV_S_FRAME_INTERVAL: {
  698. struct v4l2_subdev_frame_interval *fi = arg;
  699. if (!client_supports_streams)
  700. fi->stream = 0;
  701. if (fi->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev)
  702. return -EPERM;
  703. memset(fi->reserved, 0, sizeof(fi->reserved));
  704. return v4l2_subdev_call(sd, pad, set_frame_interval, state, fi);
  705. }
  706. case VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL: {
  707. struct v4l2_subdev_frame_interval_enum *fie = arg;
  708. if (!client_supports_streams)
  709. fie->stream = 0;
  710. memset(fie->reserved, 0, sizeof(fie->reserved));
  711. return v4l2_subdev_call(sd, pad, enum_frame_interval, state,
  712. fie);
  713. }
  714. case VIDIOC_SUBDEV_G_SELECTION: {
  715. struct v4l2_subdev_selection *sel = arg;
  716. if (!client_supports_streams)
  717. sel->stream = 0;
  718. memset(sel->reserved, 0, sizeof(sel->reserved));
  719. return v4l2_subdev_call(
  720. sd, pad, get_selection, state, sel);
  721. }
  722. case VIDIOC_SUBDEV_S_SELECTION: {
  723. struct v4l2_subdev_selection *sel = arg;
  724. if (sel->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev)
  725. return -EPERM;
  726. if (!client_supports_streams)
  727. sel->stream = 0;
  728. memset(sel->reserved, 0, sizeof(sel->reserved));
  729. return v4l2_subdev_call(
  730. sd, pad, set_selection, state, sel);
  731. }
  732. case VIDIOC_G_EDID: {
  733. struct v4l2_subdev_edid *edid = arg;
  734. return v4l2_subdev_call(sd, pad, get_edid, edid);
  735. }
  736. case VIDIOC_S_EDID: {
  737. struct v4l2_subdev_edid *edid = arg;
  738. return v4l2_subdev_call(sd, pad, set_edid, edid);
  739. }
  740. case VIDIOC_SUBDEV_DV_TIMINGS_CAP: {
  741. struct v4l2_dv_timings_cap *cap = arg;
  742. return v4l2_subdev_call(sd, pad, dv_timings_cap, cap);
  743. }
  744. case VIDIOC_SUBDEV_ENUM_DV_TIMINGS: {
  745. struct v4l2_enum_dv_timings *dvt = arg;
  746. return v4l2_subdev_call(sd, pad, enum_dv_timings, dvt);
  747. }
  748. case VIDIOC_SUBDEV_QUERY_DV_TIMINGS:
  749. return v4l2_subdev_call(sd, pad, query_dv_timings, 0, arg);
  750. case VIDIOC_SUBDEV_G_DV_TIMINGS:
  751. return v4l2_subdev_call(sd, pad, g_dv_timings, 0, arg);
  752. case VIDIOC_SUBDEV_S_DV_TIMINGS:
  753. if (ro_subdev)
  754. return -EPERM;
  755. return v4l2_subdev_call(sd, pad, s_dv_timings, 0, arg);
  756. case VIDIOC_SUBDEV_G_STD:
  757. return v4l2_subdev_call(sd, video, g_std, arg);
  758. case VIDIOC_SUBDEV_S_STD: {
  759. v4l2_std_id *std = arg;
  760. if (ro_subdev)
  761. return -EPERM;
  762. return v4l2_subdev_call(sd, video, s_std, *std);
  763. }
  764. case VIDIOC_SUBDEV_ENUMSTD: {
  765. struct v4l2_standard *p = arg;
  766. v4l2_std_id id;
  767. if (v4l2_subdev_call(sd, video, g_tvnorms, &id))
  768. return -EINVAL;
  769. return v4l_video_std_enumstd(p, id);
  770. }
  771. case VIDIOC_SUBDEV_QUERYSTD:
  772. return v4l2_subdev_call(sd, video, querystd, arg);
  773. case VIDIOC_SUBDEV_G_ROUTING: {
  774. struct v4l2_subdev_routing *routing = arg;
  775. struct v4l2_subdev_krouting *krouting;
  776. if (!v4l2_subdev_enable_streams_api)
  777. return -ENOIOCTLCMD;
  778. if (!(sd->flags & V4L2_SUBDEV_FL_STREAMS))
  779. return -ENOIOCTLCMD;
  780. memset(routing->reserved, 0, sizeof(routing->reserved));
  781. krouting = &state->routing;
  782. memcpy((struct v4l2_subdev_route *)(uintptr_t)routing->routes,
  783. krouting->routes,
  784. min(krouting->num_routes, routing->len_routes) *
  785. sizeof(*krouting->routes));
  786. routing->num_routes = krouting->num_routes;
  787. return 0;
  788. }
  789. case VIDIOC_SUBDEV_S_ROUTING: {
  790. struct v4l2_subdev_routing *routing = arg;
  791. struct v4l2_subdev_route *routes =
  792. (struct v4l2_subdev_route *)(uintptr_t)routing->routes;
  793. struct v4l2_subdev_krouting krouting = {};
  794. unsigned int i;
  795. if (!v4l2_subdev_enable_streams_api)
  796. return -ENOIOCTLCMD;
  797. if (!(sd->flags & V4L2_SUBDEV_FL_STREAMS))
  798. return -ENOIOCTLCMD;
  799. if (routing->which != V4L2_SUBDEV_FORMAT_TRY && ro_subdev)
  800. return -EPERM;
  801. if (routing->num_routes > routing->len_routes)
  802. return -EINVAL;
  803. memset(routing->reserved, 0, sizeof(routing->reserved));
  804. for (i = 0; i < routing->num_routes; ++i) {
  805. const struct v4l2_subdev_route *route = &routes[i];
  806. const struct media_pad *pads = sd->entity.pads;
  807. if (route->sink_stream > V4L2_SUBDEV_MAX_STREAM_ID ||
  808. route->source_stream > V4L2_SUBDEV_MAX_STREAM_ID)
  809. return -EINVAL;
  810. if (route->sink_pad >= sd->entity.num_pads)
  811. return -EINVAL;
  812. if (!(pads[route->sink_pad].flags &
  813. MEDIA_PAD_FL_SINK))
  814. return -EINVAL;
  815. if (route->source_pad >= sd->entity.num_pads)
  816. return -EINVAL;
  817. if (!(pads[route->source_pad].flags &
  818. MEDIA_PAD_FL_SOURCE))
  819. return -EINVAL;
  820. }
  821. /*
  822. * If the driver doesn't support setting routing, just return
  823. * the routing table.
  824. */
  825. if (!v4l2_subdev_has_op(sd, pad, set_routing)) {
  826. memcpy((struct v4l2_subdev_route *)(uintptr_t)routing->routes,
  827. state->routing.routes,
  828. min(state->routing.num_routes, routing->len_routes) *
  829. sizeof(*state->routing.routes));
  830. routing->num_routes = state->routing.num_routes;
  831. return 0;
  832. }
  833. krouting.num_routes = routing->num_routes;
  834. krouting.len_routes = routing->len_routes;
  835. krouting.routes = routes;
  836. rval = v4l2_subdev_call(sd, pad, set_routing, state,
  837. routing->which, &krouting);
  838. if (rval < 0)
  839. return rval;
  840. memcpy((struct v4l2_subdev_route *)(uintptr_t)routing->routes,
  841. state->routing.routes,
  842. min(state->routing.num_routes, routing->len_routes) *
  843. sizeof(*state->routing.routes));
  844. routing->num_routes = state->routing.num_routes;
  845. return 0;
  846. }
  847. case VIDIOC_SUBDEV_G_CLIENT_CAP: {
  848. struct v4l2_subdev_client_capability *client_cap = arg;
  849. client_cap->capabilities = subdev_fh->client_caps;
  850. return 0;
  851. }
  852. case VIDIOC_SUBDEV_S_CLIENT_CAP: {
  853. struct v4l2_subdev_client_capability *client_cap = arg;
  854. /*
  855. * Clear V4L2_SUBDEV_CLIENT_CAP_STREAMS if streams API is not
  856. * enabled. Remove this when streams API is no longer
  857. * experimental.
  858. */
  859. if (!v4l2_subdev_enable_streams_api)
  860. client_cap->capabilities &= ~V4L2_SUBDEV_CLIENT_CAP_STREAMS;
  861. /* Filter out unsupported capabilities */
  862. client_cap->capabilities &= (V4L2_SUBDEV_CLIENT_CAP_STREAMS |
  863. V4L2_SUBDEV_CLIENT_CAP_INTERVAL_USES_WHICH);
  864. subdev_fh->client_caps = client_cap->capabilities;
  865. return 0;
  866. }
  867. default:
  868. return v4l2_subdev_call(sd, core, ioctl, cmd, arg);
  869. }
  870. return 0;
  871. }
  872. static long subdev_do_ioctl_lock(struct file *file, unsigned int cmd, void *arg)
  873. {
  874. struct video_device *vdev = video_devdata(file);
  875. struct mutex *lock = vdev->lock;
  876. long ret = -ENODEV;
  877. if (lock && mutex_lock_interruptible(lock))
  878. return -ERESTARTSYS;
  879. if (video_is_registered(vdev)) {
  880. struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
  881. struct v4l2_fh *vfh = file->private_data;
  882. struct v4l2_subdev_fh *subdev_fh = to_v4l2_subdev_fh(vfh);
  883. struct v4l2_subdev_state *state;
  884. state = subdev_ioctl_get_state(sd, subdev_fh, cmd, arg);
  885. if (state)
  886. v4l2_subdev_lock_state(state);
  887. ret = subdev_do_ioctl(file, cmd, arg, state);
  888. if (state)
  889. v4l2_subdev_unlock_state(state);
  890. }
  891. if (lock)
  892. mutex_unlock(lock);
  893. return ret;
  894. }
  895. static long subdev_ioctl(struct file *file, unsigned int cmd,
  896. unsigned long arg)
  897. {
  898. return video_usercopy(file, cmd, arg, subdev_do_ioctl_lock);
  899. }
  900. #ifdef CONFIG_COMPAT
  901. static long subdev_compat_ioctl32(struct file *file, unsigned int cmd,
  902. unsigned long arg)
  903. {
  904. struct video_device *vdev = video_devdata(file);
  905. struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
  906. return v4l2_subdev_call(sd, core, compat_ioctl32, cmd, arg);
  907. }
  908. #endif
  909. #else /* CONFIG_VIDEO_V4L2_SUBDEV_API */
  910. static long subdev_ioctl(struct file *file, unsigned int cmd,
  911. unsigned long arg)
  912. {
  913. return -ENODEV;
  914. }
  915. #ifdef CONFIG_COMPAT
  916. static long subdev_compat_ioctl32(struct file *file, unsigned int cmd,
  917. unsigned long arg)
  918. {
  919. return -ENODEV;
  920. }
  921. #endif
  922. #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
  923. static __poll_t subdev_poll(struct file *file, poll_table *wait)
  924. {
  925. struct video_device *vdev = video_devdata(file);
  926. struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
  927. struct v4l2_fh *fh = file->private_data;
  928. if (!(sd->flags & V4L2_SUBDEV_FL_HAS_EVENTS))
  929. return EPOLLERR;
  930. poll_wait(file, &fh->wait, wait);
  931. if (v4l2_event_pending(fh))
  932. return EPOLLPRI;
  933. return 0;
  934. }
  935. const struct v4l2_file_operations v4l2_subdev_fops = {
  936. .owner = THIS_MODULE,
  937. .open = subdev_open,
  938. .unlocked_ioctl = subdev_ioctl,
  939. #ifdef CONFIG_COMPAT
  940. .compat_ioctl32 = subdev_compat_ioctl32,
  941. #endif
  942. .release = subdev_close,
  943. .poll = subdev_poll,
  944. };
  945. #ifdef CONFIG_MEDIA_CONTROLLER
  946. int v4l2_subdev_get_fwnode_pad_1_to_1(struct media_entity *entity,
  947. struct fwnode_endpoint *endpoint)
  948. {
  949. struct fwnode_handle *fwnode;
  950. struct v4l2_subdev *sd;
  951. if (!is_media_entity_v4l2_subdev(entity))
  952. return -EINVAL;
  953. sd = media_entity_to_v4l2_subdev(entity);
  954. fwnode = fwnode_graph_get_port_parent(endpoint->local_fwnode);
  955. fwnode_handle_put(fwnode);
  956. if (device_match_fwnode(sd->dev, fwnode))
  957. return endpoint->port;
  958. return -ENXIO;
  959. }
  960. EXPORT_SYMBOL_GPL(v4l2_subdev_get_fwnode_pad_1_to_1);
  961. int v4l2_subdev_link_validate_default(struct v4l2_subdev *sd,
  962. struct media_link *link,
  963. struct v4l2_subdev_format *source_fmt,
  964. struct v4l2_subdev_format *sink_fmt)
  965. {
  966. bool pass = true;
  967. /* The width, height and code must match. */
  968. if (source_fmt->format.width != sink_fmt->format.width) {
  969. dev_dbg(sd->entity.graph_obj.mdev->dev,
  970. "%s: width does not match (source %u, sink %u)\n",
  971. __func__,
  972. source_fmt->format.width, sink_fmt->format.width);
  973. pass = false;
  974. }
  975. if (source_fmt->format.height != sink_fmt->format.height) {
  976. dev_dbg(sd->entity.graph_obj.mdev->dev,
  977. "%s: height does not match (source %u, sink %u)\n",
  978. __func__,
  979. source_fmt->format.height, sink_fmt->format.height);
  980. pass = false;
  981. }
  982. if (source_fmt->format.code != sink_fmt->format.code) {
  983. dev_dbg(sd->entity.graph_obj.mdev->dev,
  984. "%s: media bus code does not match (source 0x%8.8x, sink 0x%8.8x)\n",
  985. __func__,
  986. source_fmt->format.code, sink_fmt->format.code);
  987. pass = false;
  988. }
  989. /* The field order must match, or the sink field order must be NONE
  990. * to support interlaced hardware connected to bridges that support
  991. * progressive formats only.
  992. */
  993. if (source_fmt->format.field != sink_fmt->format.field &&
  994. sink_fmt->format.field != V4L2_FIELD_NONE) {
  995. dev_dbg(sd->entity.graph_obj.mdev->dev,
  996. "%s: field does not match (source %u, sink %u)\n",
  997. __func__,
  998. source_fmt->format.field, sink_fmt->format.field);
  999. pass = false;
  1000. }
  1001. if (pass)
  1002. return 0;
  1003. dev_dbg(sd->entity.graph_obj.mdev->dev,
  1004. "%s: link was \"%s\":%u -> \"%s\":%u\n", __func__,
  1005. link->source->entity->name, link->source->index,
  1006. link->sink->entity->name, link->sink->index);
  1007. return -EPIPE;
  1008. }
  1009. EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate_default);
  1010. static int
  1011. v4l2_subdev_link_validate_get_format(struct media_pad *pad, u32 stream,
  1012. struct v4l2_subdev_format *fmt,
  1013. bool states_locked)
  1014. {
  1015. struct v4l2_subdev_state *state;
  1016. struct v4l2_subdev *sd;
  1017. int ret;
  1018. sd = media_entity_to_v4l2_subdev(pad->entity);
  1019. fmt->which = V4L2_SUBDEV_FORMAT_ACTIVE;
  1020. fmt->pad = pad->index;
  1021. fmt->stream = stream;
  1022. if (states_locked)
  1023. state = v4l2_subdev_get_locked_active_state(sd);
  1024. else
  1025. state = v4l2_subdev_lock_and_get_active_state(sd);
  1026. ret = v4l2_subdev_call(sd, pad, get_fmt, state, fmt);
  1027. if (!states_locked && state)
  1028. v4l2_subdev_unlock_state(state);
  1029. return ret;
  1030. }
  1031. #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
  1032. static void __v4l2_link_validate_get_streams(struct media_pad *pad,
  1033. u64 *streams_mask,
  1034. bool states_locked)
  1035. {
  1036. struct v4l2_subdev_route *route;
  1037. struct v4l2_subdev_state *state;
  1038. struct v4l2_subdev *subdev;
  1039. subdev = media_entity_to_v4l2_subdev(pad->entity);
  1040. *streams_mask = 0;
  1041. if (states_locked)
  1042. state = v4l2_subdev_get_locked_active_state(subdev);
  1043. else
  1044. state = v4l2_subdev_lock_and_get_active_state(subdev);
  1045. if (WARN_ON(!state))
  1046. return;
  1047. for_each_active_route(&state->routing, route) {
  1048. u32 route_pad;
  1049. u32 route_stream;
  1050. if (pad->flags & MEDIA_PAD_FL_SOURCE) {
  1051. route_pad = route->source_pad;
  1052. route_stream = route->source_stream;
  1053. } else {
  1054. route_pad = route->sink_pad;
  1055. route_stream = route->sink_stream;
  1056. }
  1057. if (route_pad != pad->index)
  1058. continue;
  1059. *streams_mask |= BIT_ULL(route_stream);
  1060. }
  1061. if (!states_locked)
  1062. v4l2_subdev_unlock_state(state);
  1063. }
  1064. #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
  1065. static void v4l2_link_validate_get_streams(struct media_pad *pad,
  1066. u64 *streams_mask,
  1067. bool states_locked)
  1068. {
  1069. struct v4l2_subdev *subdev = media_entity_to_v4l2_subdev(pad->entity);
  1070. if (!(subdev->flags & V4L2_SUBDEV_FL_STREAMS)) {
  1071. /* Non-streams subdevs have an implicit stream 0 */
  1072. *streams_mask = BIT_ULL(0);
  1073. return;
  1074. }
  1075. #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
  1076. __v4l2_link_validate_get_streams(pad, streams_mask, states_locked);
  1077. #else
  1078. /* This shouldn't happen */
  1079. *streams_mask = 0;
  1080. #endif
  1081. }
  1082. static int v4l2_subdev_link_validate_locked(struct media_link *link, bool states_locked)
  1083. {
  1084. struct v4l2_subdev *sink_subdev =
  1085. media_entity_to_v4l2_subdev(link->sink->entity);
  1086. struct device *dev = sink_subdev->entity.graph_obj.mdev->dev;
  1087. u64 source_streams_mask;
  1088. u64 sink_streams_mask;
  1089. u64 dangling_sink_streams;
  1090. u32 stream;
  1091. int ret;
  1092. dev_dbg(dev, "validating link \"%s\":%u -> \"%s\":%u\n",
  1093. link->source->entity->name, link->source->index,
  1094. link->sink->entity->name, link->sink->index);
  1095. v4l2_link_validate_get_streams(link->source, &source_streams_mask, states_locked);
  1096. v4l2_link_validate_get_streams(link->sink, &sink_streams_mask, states_locked);
  1097. /*
  1098. * It is ok to have more source streams than sink streams as extra
  1099. * source streams can just be ignored by the receiver, but having extra
  1100. * sink streams is an error as streams must have a source.
  1101. */
  1102. dangling_sink_streams = (source_streams_mask ^ sink_streams_mask) &
  1103. sink_streams_mask;
  1104. if (dangling_sink_streams) {
  1105. dev_err(dev, "Dangling sink streams: mask %#llx\n",
  1106. dangling_sink_streams);
  1107. return -EINVAL;
  1108. }
  1109. /* Validate source and sink stream formats */
  1110. for (stream = 0; stream < sizeof(sink_streams_mask) * 8; ++stream) {
  1111. struct v4l2_subdev_format sink_fmt, source_fmt;
  1112. if (!(sink_streams_mask & BIT_ULL(stream)))
  1113. continue;
  1114. dev_dbg(dev, "validating stream \"%s\":%u:%u -> \"%s\":%u:%u\n",
  1115. link->source->entity->name, link->source->index, stream,
  1116. link->sink->entity->name, link->sink->index, stream);
  1117. ret = v4l2_subdev_link_validate_get_format(link->source, stream,
  1118. &source_fmt, states_locked);
  1119. if (ret < 0) {
  1120. dev_dbg(dev,
  1121. "Failed to get format for \"%s\":%u:%u (but that's ok)\n",
  1122. link->source->entity->name, link->source->index,
  1123. stream);
  1124. continue;
  1125. }
  1126. ret = v4l2_subdev_link_validate_get_format(link->sink, stream,
  1127. &sink_fmt, states_locked);
  1128. if (ret < 0) {
  1129. dev_dbg(dev,
  1130. "Failed to get format for \"%s\":%u:%u (but that's ok)\n",
  1131. link->sink->entity->name, link->sink->index,
  1132. stream);
  1133. continue;
  1134. }
  1135. /* TODO: add stream number to link_validate() */
  1136. ret = v4l2_subdev_call(sink_subdev, pad, link_validate, link,
  1137. &source_fmt, &sink_fmt);
  1138. if (!ret)
  1139. continue;
  1140. if (ret != -ENOIOCTLCMD)
  1141. return ret;
  1142. ret = v4l2_subdev_link_validate_default(sink_subdev, link,
  1143. &source_fmt, &sink_fmt);
  1144. if (ret)
  1145. return ret;
  1146. }
  1147. return 0;
  1148. }
  1149. int v4l2_subdev_link_validate(struct media_link *link)
  1150. {
  1151. struct v4l2_subdev *source_sd, *sink_sd;
  1152. struct v4l2_subdev_state *source_state, *sink_state;
  1153. bool states_locked;
  1154. int ret;
  1155. /*
  1156. * Links are validated in the context of the sink entity. Usage of this
  1157. * helper on a sink that is not a subdev is a clear driver bug.
  1158. */
  1159. if (WARN_ON_ONCE(!is_media_entity_v4l2_subdev(link->sink->entity)))
  1160. return -EINVAL;
  1161. /*
  1162. * If the source is a video device, delegate link validation to it. This
  1163. * allows usage of this helper for subdev connected to a video output
  1164. * device, provided that the driver implement the video output device's
  1165. * .link_validate() operation.
  1166. */
  1167. if (is_media_entity_v4l2_video_device(link->source->entity)) {
  1168. struct media_entity *source = link->source->entity;
  1169. if (!source->ops || !source->ops->link_validate) {
  1170. /*
  1171. * Many existing drivers do not implement the required
  1172. * .link_validate() operation for their video devices.
  1173. * Print a warning to get the drivers fixed, and return
  1174. * 0 to avoid breaking userspace. This should
  1175. * eventually be turned into a WARN_ON() when all
  1176. * drivers will have been fixed.
  1177. */
  1178. pr_warn_once("video device '%s' does not implement .link_validate(), driver bug!\n",
  1179. source->name);
  1180. return 0;
  1181. }
  1182. /*
  1183. * Avoid infinite loops in case a video device incorrectly uses
  1184. * this helper function as its .link_validate() handler.
  1185. */
  1186. if (WARN_ON(source->ops->link_validate == v4l2_subdev_link_validate))
  1187. return -EINVAL;
  1188. return source->ops->link_validate(link);
  1189. }
  1190. /*
  1191. * If the source is still not a subdev, usage of this helper is a clear
  1192. * driver bug.
  1193. */
  1194. if (WARN_ON(!is_media_entity_v4l2_subdev(link->source->entity)))
  1195. return -EINVAL;
  1196. sink_sd = media_entity_to_v4l2_subdev(link->sink->entity);
  1197. source_sd = media_entity_to_v4l2_subdev(link->source->entity);
  1198. sink_state = v4l2_subdev_get_unlocked_active_state(sink_sd);
  1199. source_state = v4l2_subdev_get_unlocked_active_state(source_sd);
  1200. states_locked = sink_state && source_state;
  1201. if (states_locked)
  1202. v4l2_subdev_lock_states(sink_state, source_state);
  1203. ret = v4l2_subdev_link_validate_locked(link, states_locked);
  1204. if (states_locked)
  1205. v4l2_subdev_unlock_states(sink_state, source_state);
  1206. return ret;
  1207. }
  1208. EXPORT_SYMBOL_GPL(v4l2_subdev_link_validate);
  1209. bool v4l2_subdev_has_pad_interdep(struct media_entity *entity,
  1210. unsigned int pad0, unsigned int pad1)
  1211. {
  1212. struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
  1213. struct v4l2_subdev_krouting *routing;
  1214. struct v4l2_subdev_state *state;
  1215. unsigned int i;
  1216. state = v4l2_subdev_lock_and_get_active_state(sd);
  1217. routing = &state->routing;
  1218. for (i = 0; i < routing->num_routes; ++i) {
  1219. struct v4l2_subdev_route *route = &routing->routes[i];
  1220. if (!(route->flags & V4L2_SUBDEV_ROUTE_FL_ACTIVE))
  1221. continue;
  1222. if ((route->sink_pad == pad0 && route->source_pad == pad1) ||
  1223. (route->source_pad == pad0 && route->sink_pad == pad1)) {
  1224. v4l2_subdev_unlock_state(state);
  1225. return true;
  1226. }
  1227. }
  1228. v4l2_subdev_unlock_state(state);
  1229. return false;
  1230. }
  1231. EXPORT_SYMBOL_GPL(v4l2_subdev_has_pad_interdep);
  1232. struct v4l2_subdev_state *
  1233. __v4l2_subdev_state_alloc(struct v4l2_subdev *sd, const char *lock_name,
  1234. struct lock_class_key *lock_key)
  1235. {
  1236. struct v4l2_subdev_state *state;
  1237. int ret;
  1238. state = kzalloc(sizeof(*state), GFP_KERNEL);
  1239. if (!state)
  1240. return ERR_PTR(-ENOMEM);
  1241. __mutex_init(&state->_lock, lock_name, lock_key);
  1242. if (sd->state_lock)
  1243. state->lock = sd->state_lock;
  1244. else
  1245. state->lock = &state->_lock;
  1246. state->sd = sd;
  1247. /* Drivers that support streams do not need the legacy pad config */
  1248. if (!(sd->flags & V4L2_SUBDEV_FL_STREAMS) && sd->entity.num_pads) {
  1249. state->pads = kvcalloc(sd->entity.num_pads,
  1250. sizeof(*state->pads), GFP_KERNEL);
  1251. if (!state->pads) {
  1252. ret = -ENOMEM;
  1253. goto err;
  1254. }
  1255. }
  1256. if (sd->internal_ops && sd->internal_ops->init_state) {
  1257. /*
  1258. * There can be no race at this point, but we lock the state
  1259. * anyway to satisfy lockdep checks.
  1260. */
  1261. v4l2_subdev_lock_state(state);
  1262. ret = sd->internal_ops->init_state(sd, state);
  1263. v4l2_subdev_unlock_state(state);
  1264. if (ret)
  1265. goto err;
  1266. }
  1267. return state;
  1268. err:
  1269. if (state && state->pads)
  1270. kvfree(state->pads);
  1271. kfree(state);
  1272. return ERR_PTR(ret);
  1273. }
  1274. EXPORT_SYMBOL_GPL(__v4l2_subdev_state_alloc);
  1275. void __v4l2_subdev_state_free(struct v4l2_subdev_state *state)
  1276. {
  1277. if (!state)
  1278. return;
  1279. mutex_destroy(&state->_lock);
  1280. kfree(state->routing.routes);
  1281. kvfree(state->stream_configs.configs);
  1282. kvfree(state->pads);
  1283. kfree(state);
  1284. }
  1285. EXPORT_SYMBOL_GPL(__v4l2_subdev_state_free);
  1286. int __v4l2_subdev_init_finalize(struct v4l2_subdev *sd, const char *name,
  1287. struct lock_class_key *key)
  1288. {
  1289. struct v4l2_subdev_state *state;
  1290. struct device *dev = sd->dev;
  1291. bool has_disable_streams;
  1292. bool has_enable_streams;
  1293. bool has_s_stream;
  1294. /* Check that the subdevice implements the required features */
  1295. has_s_stream = v4l2_subdev_has_op(sd, video, s_stream);
  1296. has_enable_streams = v4l2_subdev_has_op(sd, pad, enable_streams);
  1297. has_disable_streams = v4l2_subdev_has_op(sd, pad, disable_streams);
  1298. if (has_enable_streams != has_disable_streams) {
  1299. dev_err(dev,
  1300. "subdev '%s' must implement both or neither of .enable_streams() and .disable_streams()\n",
  1301. sd->name);
  1302. return -EINVAL;
  1303. }
  1304. if (sd->flags & V4L2_SUBDEV_FL_STREAMS) {
  1305. if (has_s_stream && !has_enable_streams) {
  1306. dev_err(dev,
  1307. "subdev '%s' must implement .enable/disable_streams()\n",
  1308. sd->name);
  1309. return -EINVAL;
  1310. }
  1311. }
  1312. state = __v4l2_subdev_state_alloc(sd, name, key);
  1313. if (IS_ERR(state))
  1314. return PTR_ERR(state);
  1315. sd->active_state = state;
  1316. return 0;
  1317. }
  1318. EXPORT_SYMBOL_GPL(__v4l2_subdev_init_finalize);
  1319. void v4l2_subdev_cleanup(struct v4l2_subdev *sd)
  1320. {
  1321. struct v4l2_async_subdev_endpoint *ase, *ase_tmp;
  1322. __v4l2_subdev_state_free(sd->active_state);
  1323. sd->active_state = NULL;
  1324. /* Uninitialised sub-device, bail out here. */
  1325. if (!sd->async_subdev_endpoint_list.next)
  1326. return;
  1327. list_for_each_entry_safe(ase, ase_tmp, &sd->async_subdev_endpoint_list,
  1328. async_subdev_endpoint_entry) {
  1329. list_del(&ase->async_subdev_endpoint_entry);
  1330. kfree(ase);
  1331. }
  1332. }
  1333. EXPORT_SYMBOL_GPL(v4l2_subdev_cleanup);
  1334. struct v4l2_mbus_framefmt *
  1335. __v4l2_subdev_state_get_format(struct v4l2_subdev_state *state,
  1336. unsigned int pad, u32 stream)
  1337. {
  1338. struct v4l2_subdev_stream_configs *stream_configs;
  1339. unsigned int i;
  1340. if (WARN_ON_ONCE(!state))
  1341. return NULL;
  1342. if (state->pads) {
  1343. if (stream)
  1344. return NULL;
  1345. if (pad >= state->sd->entity.num_pads)
  1346. return NULL;
  1347. return &state->pads[pad].format;
  1348. }
  1349. lockdep_assert_held(state->lock);
  1350. stream_configs = &state->stream_configs;
  1351. for (i = 0; i < stream_configs->num_configs; ++i) {
  1352. if (stream_configs->configs[i].pad == pad &&
  1353. stream_configs->configs[i].stream == stream)
  1354. return &stream_configs->configs[i].fmt;
  1355. }
  1356. return NULL;
  1357. }
  1358. EXPORT_SYMBOL_GPL(__v4l2_subdev_state_get_format);
  1359. struct v4l2_rect *
  1360. __v4l2_subdev_state_get_crop(struct v4l2_subdev_state *state, unsigned int pad,
  1361. u32 stream)
  1362. {
  1363. struct v4l2_subdev_stream_configs *stream_configs;
  1364. unsigned int i;
  1365. if (WARN_ON_ONCE(!state))
  1366. return NULL;
  1367. if (state->pads) {
  1368. if (stream)
  1369. return NULL;
  1370. if (pad >= state->sd->entity.num_pads)
  1371. return NULL;
  1372. return &state->pads[pad].crop;
  1373. }
  1374. lockdep_assert_held(state->lock);
  1375. stream_configs = &state->stream_configs;
  1376. for (i = 0; i < stream_configs->num_configs; ++i) {
  1377. if (stream_configs->configs[i].pad == pad &&
  1378. stream_configs->configs[i].stream == stream)
  1379. return &stream_configs->configs[i].crop;
  1380. }
  1381. return NULL;
  1382. }
  1383. EXPORT_SYMBOL_GPL(__v4l2_subdev_state_get_crop);
  1384. struct v4l2_rect *
  1385. __v4l2_subdev_state_get_compose(struct v4l2_subdev_state *state,
  1386. unsigned int pad, u32 stream)
  1387. {
  1388. struct v4l2_subdev_stream_configs *stream_configs;
  1389. unsigned int i;
  1390. if (WARN_ON_ONCE(!state))
  1391. return NULL;
  1392. if (state->pads) {
  1393. if (stream)
  1394. return NULL;
  1395. if (pad >= state->sd->entity.num_pads)
  1396. return NULL;
  1397. return &state->pads[pad].compose;
  1398. }
  1399. lockdep_assert_held(state->lock);
  1400. stream_configs = &state->stream_configs;
  1401. for (i = 0; i < stream_configs->num_configs; ++i) {
  1402. if (stream_configs->configs[i].pad == pad &&
  1403. stream_configs->configs[i].stream == stream)
  1404. return &stream_configs->configs[i].compose;
  1405. }
  1406. return NULL;
  1407. }
  1408. EXPORT_SYMBOL_GPL(__v4l2_subdev_state_get_compose);
  1409. struct v4l2_fract *
  1410. __v4l2_subdev_state_get_interval(struct v4l2_subdev_state *state,
  1411. unsigned int pad, u32 stream)
  1412. {
  1413. struct v4l2_subdev_stream_configs *stream_configs;
  1414. unsigned int i;
  1415. if (WARN_ON(!state))
  1416. return NULL;
  1417. lockdep_assert_held(state->lock);
  1418. if (state->pads) {
  1419. if (stream)
  1420. return NULL;
  1421. if (pad >= state->sd->entity.num_pads)
  1422. return NULL;
  1423. return &state->pads[pad].interval;
  1424. }
  1425. lockdep_assert_held(state->lock);
  1426. stream_configs = &state->stream_configs;
  1427. for (i = 0; i < stream_configs->num_configs; ++i) {
  1428. if (stream_configs->configs[i].pad == pad &&
  1429. stream_configs->configs[i].stream == stream)
  1430. return &stream_configs->configs[i].interval;
  1431. }
  1432. return NULL;
  1433. }
  1434. EXPORT_SYMBOL_GPL(__v4l2_subdev_state_get_interval);
  1435. #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
  1436. static int
  1437. v4l2_subdev_init_stream_configs(struct v4l2_subdev_stream_configs *stream_configs,
  1438. const struct v4l2_subdev_krouting *routing)
  1439. {
  1440. struct v4l2_subdev_stream_configs new_configs = { 0 };
  1441. struct v4l2_subdev_route *route;
  1442. u32 idx;
  1443. /* Count number of formats needed */
  1444. for_each_active_route(routing, route) {
  1445. /*
  1446. * Each route needs a format on both ends of the route.
  1447. */
  1448. new_configs.num_configs += 2;
  1449. }
  1450. if (new_configs.num_configs) {
  1451. new_configs.configs = kvcalloc(new_configs.num_configs,
  1452. sizeof(*new_configs.configs),
  1453. GFP_KERNEL);
  1454. if (!new_configs.configs)
  1455. return -ENOMEM;
  1456. }
  1457. /*
  1458. * Fill in the 'pad' and stream' value for each item in the array from
  1459. * the routing table
  1460. */
  1461. idx = 0;
  1462. for_each_active_route(routing, route) {
  1463. new_configs.configs[idx].pad = route->sink_pad;
  1464. new_configs.configs[idx].stream = route->sink_stream;
  1465. idx++;
  1466. new_configs.configs[idx].pad = route->source_pad;
  1467. new_configs.configs[idx].stream = route->source_stream;
  1468. idx++;
  1469. }
  1470. kvfree(stream_configs->configs);
  1471. *stream_configs = new_configs;
  1472. return 0;
  1473. }
  1474. int v4l2_subdev_get_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_state *state,
  1475. struct v4l2_subdev_format *format)
  1476. {
  1477. struct v4l2_mbus_framefmt *fmt;
  1478. fmt = v4l2_subdev_state_get_format(state, format->pad, format->stream);
  1479. if (!fmt)
  1480. return -EINVAL;
  1481. format->format = *fmt;
  1482. return 0;
  1483. }
  1484. EXPORT_SYMBOL_GPL(v4l2_subdev_get_fmt);
  1485. int v4l2_subdev_get_frame_interval(struct v4l2_subdev *sd,
  1486. struct v4l2_subdev_state *state,
  1487. struct v4l2_subdev_frame_interval *fi)
  1488. {
  1489. struct v4l2_fract *interval;
  1490. interval = v4l2_subdev_state_get_interval(state, fi->pad, fi->stream);
  1491. if (!interval)
  1492. return -EINVAL;
  1493. fi->interval = *interval;
  1494. return 0;
  1495. }
  1496. EXPORT_SYMBOL_GPL(v4l2_subdev_get_frame_interval);
  1497. int v4l2_subdev_set_routing(struct v4l2_subdev *sd,
  1498. struct v4l2_subdev_state *state,
  1499. const struct v4l2_subdev_krouting *routing)
  1500. {
  1501. struct v4l2_subdev_krouting *dst = &state->routing;
  1502. const struct v4l2_subdev_krouting *src = routing;
  1503. struct v4l2_subdev_krouting new_routing = { 0 };
  1504. size_t bytes;
  1505. int r;
  1506. if (unlikely(check_mul_overflow((size_t)src->num_routes,
  1507. sizeof(*src->routes), &bytes)))
  1508. return -EOVERFLOW;
  1509. lockdep_assert_held(state->lock);
  1510. if (src->num_routes > 0) {
  1511. new_routing.routes = kmemdup(src->routes, bytes, GFP_KERNEL);
  1512. if (!new_routing.routes)
  1513. return -ENOMEM;
  1514. }
  1515. new_routing.num_routes = src->num_routes;
  1516. r = v4l2_subdev_init_stream_configs(&state->stream_configs,
  1517. &new_routing);
  1518. if (r) {
  1519. kfree(new_routing.routes);
  1520. return r;
  1521. }
  1522. kfree(dst->routes);
  1523. *dst = new_routing;
  1524. return 0;
  1525. }
  1526. EXPORT_SYMBOL_GPL(v4l2_subdev_set_routing);
  1527. struct v4l2_subdev_route *
  1528. __v4l2_subdev_next_active_route(const struct v4l2_subdev_krouting *routing,
  1529. struct v4l2_subdev_route *route)
  1530. {
  1531. if (route)
  1532. ++route;
  1533. else
  1534. route = &routing->routes[0];
  1535. for (; route < routing->routes + routing->num_routes; ++route) {
  1536. if (!(route->flags & V4L2_SUBDEV_ROUTE_FL_ACTIVE))
  1537. continue;
  1538. return route;
  1539. }
  1540. return NULL;
  1541. }
  1542. EXPORT_SYMBOL_GPL(__v4l2_subdev_next_active_route);
  1543. int v4l2_subdev_set_routing_with_fmt(struct v4l2_subdev *sd,
  1544. struct v4l2_subdev_state *state,
  1545. const struct v4l2_subdev_krouting *routing,
  1546. const struct v4l2_mbus_framefmt *fmt)
  1547. {
  1548. struct v4l2_subdev_stream_configs *stream_configs;
  1549. unsigned int i;
  1550. int ret;
  1551. ret = v4l2_subdev_set_routing(sd, state, routing);
  1552. if (ret)
  1553. return ret;
  1554. stream_configs = &state->stream_configs;
  1555. for (i = 0; i < stream_configs->num_configs; ++i)
  1556. stream_configs->configs[i].fmt = *fmt;
  1557. return 0;
  1558. }
  1559. EXPORT_SYMBOL_GPL(v4l2_subdev_set_routing_with_fmt);
  1560. int v4l2_subdev_routing_find_opposite_end(const struct v4l2_subdev_krouting *routing,
  1561. u32 pad, u32 stream, u32 *other_pad,
  1562. u32 *other_stream)
  1563. {
  1564. unsigned int i;
  1565. for (i = 0; i < routing->num_routes; ++i) {
  1566. struct v4l2_subdev_route *route = &routing->routes[i];
  1567. if (route->source_pad == pad &&
  1568. route->source_stream == stream) {
  1569. if (other_pad)
  1570. *other_pad = route->sink_pad;
  1571. if (other_stream)
  1572. *other_stream = route->sink_stream;
  1573. return 0;
  1574. }
  1575. if (route->sink_pad == pad && route->sink_stream == stream) {
  1576. if (other_pad)
  1577. *other_pad = route->source_pad;
  1578. if (other_stream)
  1579. *other_stream = route->source_stream;
  1580. return 0;
  1581. }
  1582. }
  1583. return -EINVAL;
  1584. }
  1585. EXPORT_SYMBOL_GPL(v4l2_subdev_routing_find_opposite_end);
  1586. struct v4l2_mbus_framefmt *
  1587. v4l2_subdev_state_get_opposite_stream_format(struct v4l2_subdev_state *state,
  1588. u32 pad, u32 stream)
  1589. {
  1590. u32 other_pad, other_stream;
  1591. int ret;
  1592. ret = v4l2_subdev_routing_find_opposite_end(&state->routing,
  1593. pad, stream,
  1594. &other_pad, &other_stream);
  1595. if (ret)
  1596. return NULL;
  1597. return v4l2_subdev_state_get_format(state, other_pad, other_stream);
  1598. }
  1599. EXPORT_SYMBOL_GPL(v4l2_subdev_state_get_opposite_stream_format);
  1600. u64 v4l2_subdev_state_xlate_streams(const struct v4l2_subdev_state *state,
  1601. u32 pad0, u32 pad1, u64 *streams)
  1602. {
  1603. const struct v4l2_subdev_krouting *routing = &state->routing;
  1604. struct v4l2_subdev_route *route;
  1605. u64 streams0 = 0;
  1606. u64 streams1 = 0;
  1607. for_each_active_route(routing, route) {
  1608. if (route->sink_pad == pad0 && route->source_pad == pad1 &&
  1609. (*streams & BIT_ULL(route->sink_stream))) {
  1610. streams0 |= BIT_ULL(route->sink_stream);
  1611. streams1 |= BIT_ULL(route->source_stream);
  1612. }
  1613. if (route->source_pad == pad0 && route->sink_pad == pad1 &&
  1614. (*streams & BIT_ULL(route->source_stream))) {
  1615. streams0 |= BIT_ULL(route->source_stream);
  1616. streams1 |= BIT_ULL(route->sink_stream);
  1617. }
  1618. }
  1619. *streams = streams0;
  1620. return streams1;
  1621. }
  1622. EXPORT_SYMBOL_GPL(v4l2_subdev_state_xlate_streams);
  1623. int v4l2_subdev_routing_validate(struct v4l2_subdev *sd,
  1624. const struct v4l2_subdev_krouting *routing,
  1625. enum v4l2_subdev_routing_restriction disallow)
  1626. {
  1627. u32 *remote_pads = NULL;
  1628. unsigned int i, j;
  1629. int ret = -EINVAL;
  1630. if (disallow & (V4L2_SUBDEV_ROUTING_NO_STREAM_MIX |
  1631. V4L2_SUBDEV_ROUTING_NO_MULTIPLEXING)) {
  1632. remote_pads = kcalloc(sd->entity.num_pads, sizeof(*remote_pads),
  1633. GFP_KERNEL);
  1634. if (!remote_pads)
  1635. return -ENOMEM;
  1636. for (i = 0; i < sd->entity.num_pads; ++i)
  1637. remote_pads[i] = U32_MAX;
  1638. }
  1639. for (i = 0; i < routing->num_routes; ++i) {
  1640. const struct v4l2_subdev_route *route = &routing->routes[i];
  1641. /* Validate the sink and source pad numbers. */
  1642. if (route->sink_pad >= sd->entity.num_pads ||
  1643. !(sd->entity.pads[route->sink_pad].flags & MEDIA_PAD_FL_SINK)) {
  1644. dev_dbg(sd->dev, "route %u sink (%u) is not a sink pad\n",
  1645. i, route->sink_pad);
  1646. goto out;
  1647. }
  1648. if (route->source_pad >= sd->entity.num_pads ||
  1649. !(sd->entity.pads[route->source_pad].flags & MEDIA_PAD_FL_SOURCE)) {
  1650. dev_dbg(sd->dev, "route %u source (%u) is not a source pad\n",
  1651. i, route->source_pad);
  1652. goto out;
  1653. }
  1654. /*
  1655. * V4L2_SUBDEV_ROUTING_NO_SINK_STREAM_MIX: all streams from a
  1656. * sink pad must be routed to a single source pad.
  1657. */
  1658. if (disallow & V4L2_SUBDEV_ROUTING_NO_SINK_STREAM_MIX) {
  1659. if (remote_pads[route->sink_pad] != U32_MAX &&
  1660. remote_pads[route->sink_pad] != route->source_pad) {
  1661. dev_dbg(sd->dev,
  1662. "route %u attempts to mix %s streams\n",
  1663. i, "sink");
  1664. goto out;
  1665. }
  1666. }
  1667. /*
  1668. * V4L2_SUBDEV_ROUTING_NO_SOURCE_STREAM_MIX: all streams on a
  1669. * source pad must originate from a single sink pad.
  1670. */
  1671. if (disallow & V4L2_SUBDEV_ROUTING_NO_SOURCE_STREAM_MIX) {
  1672. if (remote_pads[route->source_pad] != U32_MAX &&
  1673. remote_pads[route->source_pad] != route->sink_pad) {
  1674. dev_dbg(sd->dev,
  1675. "route %u attempts to mix %s streams\n",
  1676. i, "source");
  1677. goto out;
  1678. }
  1679. }
  1680. /*
  1681. * V4L2_SUBDEV_ROUTING_NO_SINK_MULTIPLEXING: Pads on the sink
  1682. * side can not do stream multiplexing, i.e. there can be only
  1683. * a single stream in a sink pad.
  1684. */
  1685. if (disallow & V4L2_SUBDEV_ROUTING_NO_SINK_MULTIPLEXING) {
  1686. if (remote_pads[route->sink_pad] != U32_MAX) {
  1687. dev_dbg(sd->dev,
  1688. "route %u attempts to multiplex on %s pad %u\n",
  1689. i, "sink", route->sink_pad);
  1690. goto out;
  1691. }
  1692. }
  1693. /*
  1694. * V4L2_SUBDEV_ROUTING_NO_SOURCE_MULTIPLEXING: Pads on the
  1695. * source side can not do stream multiplexing, i.e. there can
  1696. * be only a single stream in a source pad.
  1697. */
  1698. if (disallow & V4L2_SUBDEV_ROUTING_NO_SOURCE_MULTIPLEXING) {
  1699. if (remote_pads[route->source_pad] != U32_MAX) {
  1700. dev_dbg(sd->dev,
  1701. "route %u attempts to multiplex on %s pad %u\n",
  1702. i, "source", route->source_pad);
  1703. goto out;
  1704. }
  1705. }
  1706. if (remote_pads) {
  1707. remote_pads[route->sink_pad] = route->source_pad;
  1708. remote_pads[route->source_pad] = route->sink_pad;
  1709. }
  1710. for (j = i + 1; j < routing->num_routes; ++j) {
  1711. const struct v4l2_subdev_route *r = &routing->routes[j];
  1712. /*
  1713. * V4L2_SUBDEV_ROUTING_NO_1_TO_N: No two routes can
  1714. * originate from the same (sink) stream.
  1715. */
  1716. if ((disallow & V4L2_SUBDEV_ROUTING_NO_1_TO_N) &&
  1717. route->sink_pad == r->sink_pad &&
  1718. route->sink_stream == r->sink_stream) {
  1719. dev_dbg(sd->dev,
  1720. "routes %u and %u originate from same sink (%u/%u)\n",
  1721. i, j, route->sink_pad,
  1722. route->sink_stream);
  1723. goto out;
  1724. }
  1725. /*
  1726. * V4L2_SUBDEV_ROUTING_NO_N_TO_1: No two routes can end
  1727. * at the same (source) stream.
  1728. */
  1729. if ((disallow & V4L2_SUBDEV_ROUTING_NO_N_TO_1) &&
  1730. route->source_pad == r->source_pad &&
  1731. route->source_stream == r->source_stream) {
  1732. dev_dbg(sd->dev,
  1733. "routes %u and %u end at same source (%u/%u)\n",
  1734. i, j, route->source_pad,
  1735. route->source_stream);
  1736. goto out;
  1737. }
  1738. }
  1739. }
  1740. ret = 0;
  1741. out:
  1742. kfree(remote_pads);
  1743. return ret;
  1744. }
  1745. EXPORT_SYMBOL_GPL(v4l2_subdev_routing_validate);
  1746. static void v4l2_subdev_collect_streams(struct v4l2_subdev *sd,
  1747. struct v4l2_subdev_state *state,
  1748. u32 pad, u64 streams_mask,
  1749. u64 *found_streams,
  1750. u64 *enabled_streams)
  1751. {
  1752. if (!(sd->flags & V4L2_SUBDEV_FL_STREAMS)) {
  1753. *found_streams = BIT_ULL(0);
  1754. *enabled_streams =
  1755. (sd->enabled_pads & BIT_ULL(pad)) ? BIT_ULL(0) : 0;
  1756. return;
  1757. }
  1758. *found_streams = 0;
  1759. *enabled_streams = 0;
  1760. for (unsigned int i = 0; i < state->stream_configs.num_configs; ++i) {
  1761. const struct v4l2_subdev_stream_config *cfg =
  1762. &state->stream_configs.configs[i];
  1763. if (cfg->pad != pad || !(streams_mask & BIT_ULL(cfg->stream)))
  1764. continue;
  1765. *found_streams |= BIT_ULL(cfg->stream);
  1766. if (cfg->enabled)
  1767. *enabled_streams |= BIT_ULL(cfg->stream);
  1768. }
  1769. }
  1770. static void v4l2_subdev_set_streams_enabled(struct v4l2_subdev *sd,
  1771. struct v4l2_subdev_state *state,
  1772. u32 pad, u64 streams_mask,
  1773. bool enabled)
  1774. {
  1775. if (!(sd->flags & V4L2_SUBDEV_FL_STREAMS)) {
  1776. if (enabled)
  1777. sd->enabled_pads |= BIT_ULL(pad);
  1778. else
  1779. sd->enabled_pads &= ~BIT_ULL(pad);
  1780. return;
  1781. }
  1782. for (unsigned int i = 0; i < state->stream_configs.num_configs; ++i) {
  1783. struct v4l2_subdev_stream_config *cfg =
  1784. &state->stream_configs.configs[i];
  1785. if (cfg->pad == pad && (streams_mask & BIT_ULL(cfg->stream)))
  1786. cfg->enabled = enabled;
  1787. }
  1788. }
  1789. int v4l2_subdev_enable_streams(struct v4l2_subdev *sd, u32 pad,
  1790. u64 streams_mask)
  1791. {
  1792. struct device *dev = sd->entity.graph_obj.mdev->dev;
  1793. struct v4l2_subdev_state *state;
  1794. bool already_streaming;
  1795. u64 enabled_streams;
  1796. u64 found_streams;
  1797. bool use_s_stream;
  1798. int ret;
  1799. /* A few basic sanity checks first. */
  1800. if (pad >= sd->entity.num_pads)
  1801. return -EINVAL;
  1802. if (!(sd->entity.pads[pad].flags & MEDIA_PAD_FL_SOURCE))
  1803. return -EOPNOTSUPP;
  1804. /*
  1805. * We use a 64-bit bitmask for tracking enabled pads, so only subdevices
  1806. * with 64 pads or less can be supported.
  1807. */
  1808. if (pad >= sizeof(sd->enabled_pads) * BITS_PER_BYTE)
  1809. return -EOPNOTSUPP;
  1810. if (!streams_mask)
  1811. return 0;
  1812. /* Fallback on .s_stream() if .enable_streams() isn't available. */
  1813. use_s_stream = !v4l2_subdev_has_op(sd, pad, enable_streams);
  1814. if (!use_s_stream)
  1815. state = v4l2_subdev_lock_and_get_active_state(sd);
  1816. else
  1817. state = NULL;
  1818. /*
  1819. * Verify that the requested streams exist and that they are not
  1820. * already enabled.
  1821. */
  1822. v4l2_subdev_collect_streams(sd, state, pad, streams_mask,
  1823. &found_streams, &enabled_streams);
  1824. if (found_streams != streams_mask) {
  1825. dev_dbg(dev, "streams 0x%llx not found on %s:%u\n",
  1826. streams_mask & ~found_streams, sd->entity.name, pad);
  1827. ret = -EINVAL;
  1828. goto done;
  1829. }
  1830. if (enabled_streams) {
  1831. dev_dbg(dev, "streams 0x%llx already enabled on %s:%u\n",
  1832. enabled_streams, sd->entity.name, pad);
  1833. ret = -EALREADY;
  1834. goto done;
  1835. }
  1836. dev_dbg(dev, "enable streams %u:%#llx\n", pad, streams_mask);
  1837. already_streaming = v4l2_subdev_is_streaming(sd);
  1838. if (!use_s_stream) {
  1839. /* Call the .enable_streams() operation. */
  1840. ret = v4l2_subdev_call(sd, pad, enable_streams, state, pad,
  1841. streams_mask);
  1842. } else {
  1843. /* Start streaming when the first pad is enabled. */
  1844. if (!already_streaming)
  1845. ret = v4l2_subdev_call(sd, video, s_stream, 1);
  1846. else
  1847. ret = 0;
  1848. }
  1849. if (ret) {
  1850. dev_dbg(dev, "enable streams %u:%#llx failed: %d\n", pad,
  1851. streams_mask, ret);
  1852. goto done;
  1853. }
  1854. /* Mark the streams as enabled. */
  1855. v4l2_subdev_set_streams_enabled(sd, state, pad, streams_mask, true);
  1856. /*
  1857. * TODO: When all the drivers have been changed to use
  1858. * v4l2_subdev_enable_streams() and v4l2_subdev_disable_streams(),
  1859. * instead of calling .s_stream() operation directly, we can remove
  1860. * the privacy LED handling from call_s_stream() and do it here
  1861. * for all cases.
  1862. */
  1863. if (!use_s_stream && !already_streaming)
  1864. v4l2_subdev_enable_privacy_led(sd);
  1865. done:
  1866. if (!use_s_stream)
  1867. v4l2_subdev_unlock_state(state);
  1868. return ret;
  1869. }
  1870. EXPORT_SYMBOL_GPL(v4l2_subdev_enable_streams);
  1871. int v4l2_subdev_disable_streams(struct v4l2_subdev *sd, u32 pad,
  1872. u64 streams_mask)
  1873. {
  1874. struct device *dev = sd->entity.graph_obj.mdev->dev;
  1875. struct v4l2_subdev_state *state;
  1876. u64 enabled_streams;
  1877. u64 found_streams;
  1878. bool use_s_stream;
  1879. int ret;
  1880. /* A few basic sanity checks first. */
  1881. if (pad >= sd->entity.num_pads)
  1882. return -EINVAL;
  1883. if (!(sd->entity.pads[pad].flags & MEDIA_PAD_FL_SOURCE))
  1884. return -EOPNOTSUPP;
  1885. /*
  1886. * We use a 64-bit bitmask for tracking enabled pads, so only subdevices
  1887. * with 64 pads or less can be supported.
  1888. */
  1889. if (pad >= sizeof(sd->enabled_pads) * BITS_PER_BYTE)
  1890. return -EOPNOTSUPP;
  1891. if (!streams_mask)
  1892. return 0;
  1893. /* Fallback on .s_stream() if .disable_streams() isn't available. */
  1894. use_s_stream = !v4l2_subdev_has_op(sd, pad, disable_streams);
  1895. if (!use_s_stream)
  1896. state = v4l2_subdev_lock_and_get_active_state(sd);
  1897. else
  1898. state = NULL;
  1899. /*
  1900. * Verify that the requested streams exist and that they are not
  1901. * already disabled.
  1902. */
  1903. v4l2_subdev_collect_streams(sd, state, pad, streams_mask,
  1904. &found_streams, &enabled_streams);
  1905. if (found_streams != streams_mask) {
  1906. dev_dbg(dev, "streams 0x%llx not found on %s:%u\n",
  1907. streams_mask & ~found_streams, sd->entity.name, pad);
  1908. ret = -EINVAL;
  1909. goto done;
  1910. }
  1911. if (enabled_streams != streams_mask) {
  1912. dev_dbg(dev, "streams 0x%llx already disabled on %s:%u\n",
  1913. streams_mask & ~enabled_streams, sd->entity.name, pad);
  1914. ret = -EALREADY;
  1915. goto done;
  1916. }
  1917. dev_dbg(dev, "disable streams %u:%#llx\n", pad, streams_mask);
  1918. if (!use_s_stream) {
  1919. /* Call the .disable_streams() operation. */
  1920. ret = v4l2_subdev_call(sd, pad, disable_streams, state, pad,
  1921. streams_mask);
  1922. } else {
  1923. /* Stop streaming when the last streams are disabled. */
  1924. if (!(sd->enabled_pads & ~BIT_ULL(pad)))
  1925. ret = v4l2_subdev_call(sd, video, s_stream, 0);
  1926. else
  1927. ret = 0;
  1928. }
  1929. if (ret) {
  1930. dev_dbg(dev, "disable streams %u:%#llx failed: %d\n", pad,
  1931. streams_mask, ret);
  1932. goto done;
  1933. }
  1934. v4l2_subdev_set_streams_enabled(sd, state, pad, streams_mask, false);
  1935. done:
  1936. if (!use_s_stream) {
  1937. if (!v4l2_subdev_is_streaming(sd))
  1938. v4l2_subdev_disable_privacy_led(sd);
  1939. v4l2_subdev_unlock_state(state);
  1940. }
  1941. return ret;
  1942. }
  1943. EXPORT_SYMBOL_GPL(v4l2_subdev_disable_streams);
  1944. int v4l2_subdev_s_stream_helper(struct v4l2_subdev *sd, int enable)
  1945. {
  1946. struct v4l2_subdev_state *state;
  1947. struct v4l2_subdev_route *route;
  1948. struct media_pad *pad;
  1949. u64 source_mask = 0;
  1950. int pad_index = -1;
  1951. /*
  1952. * Find the source pad. This helper is meant for subdevs that have a
  1953. * single source pad, so failures shouldn't happen, but catch them
  1954. * loudly nonetheless as they indicate a driver bug.
  1955. */
  1956. media_entity_for_each_pad(&sd->entity, pad) {
  1957. if (pad->flags & MEDIA_PAD_FL_SOURCE) {
  1958. pad_index = pad->index;
  1959. break;
  1960. }
  1961. }
  1962. if (WARN_ON(pad_index == -1))
  1963. return -EINVAL;
  1964. if (sd->flags & V4L2_SUBDEV_FL_STREAMS) {
  1965. /*
  1966. * As there's a single source pad, just collect all the source
  1967. * streams.
  1968. */
  1969. state = v4l2_subdev_lock_and_get_active_state(sd);
  1970. for_each_active_route(&state->routing, route)
  1971. source_mask |= BIT_ULL(route->source_stream);
  1972. v4l2_subdev_unlock_state(state);
  1973. } else {
  1974. /*
  1975. * For non-streams subdevices, there's a single implicit stream
  1976. * per pad.
  1977. */
  1978. source_mask = BIT_ULL(0);
  1979. }
  1980. if (enable)
  1981. return v4l2_subdev_enable_streams(sd, pad_index, source_mask);
  1982. else
  1983. return v4l2_subdev_disable_streams(sd, pad_index, source_mask);
  1984. }
  1985. EXPORT_SYMBOL_GPL(v4l2_subdev_s_stream_helper);
  1986. #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
  1987. #endif /* CONFIG_MEDIA_CONTROLLER */
  1988. void v4l2_subdev_init(struct v4l2_subdev *sd, const struct v4l2_subdev_ops *ops)
  1989. {
  1990. INIT_LIST_HEAD(&sd->list);
  1991. BUG_ON(!ops);
  1992. sd->ops = ops;
  1993. sd->v4l2_dev = NULL;
  1994. sd->flags = 0;
  1995. sd->name[0] = '\0';
  1996. sd->grp_id = 0;
  1997. sd->dev_priv = NULL;
  1998. sd->host_priv = NULL;
  1999. sd->privacy_led = NULL;
  2000. INIT_LIST_HEAD(&sd->async_subdev_endpoint_list);
  2001. #if defined(CONFIG_MEDIA_CONTROLLER)
  2002. sd->entity.name = sd->name;
  2003. sd->entity.obj_type = MEDIA_ENTITY_TYPE_V4L2_SUBDEV;
  2004. sd->entity.function = MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN;
  2005. #endif
  2006. }
  2007. EXPORT_SYMBOL(v4l2_subdev_init);
  2008. void v4l2_subdev_notify_event(struct v4l2_subdev *sd,
  2009. const struct v4l2_event *ev)
  2010. {
  2011. v4l2_event_queue(sd->devnode, ev);
  2012. v4l2_subdev_notify(sd, V4L2_DEVICE_NOTIFY_EVENT, (void *)ev);
  2013. }
  2014. EXPORT_SYMBOL_GPL(v4l2_subdev_notify_event);
  2015. bool v4l2_subdev_is_streaming(struct v4l2_subdev *sd)
  2016. {
  2017. struct v4l2_subdev_state *state;
  2018. if (!v4l2_subdev_has_op(sd, pad, enable_streams))
  2019. return sd->s_stream_enabled;
  2020. if (!(sd->flags & V4L2_SUBDEV_FL_STREAMS))
  2021. return !!sd->enabled_pads;
  2022. state = v4l2_subdev_get_locked_active_state(sd);
  2023. for (unsigned int i = 0; i < state->stream_configs.num_configs; ++i) {
  2024. const struct v4l2_subdev_stream_config *cfg;
  2025. cfg = &state->stream_configs.configs[i];
  2026. if (cfg->enabled)
  2027. return true;
  2028. }
  2029. return false;
  2030. }
  2031. EXPORT_SYMBOL_GPL(v4l2_subdev_is_streaming);
  2032. int v4l2_subdev_get_privacy_led(struct v4l2_subdev *sd)
  2033. {
  2034. #if IS_REACHABLE(CONFIG_LEDS_CLASS)
  2035. sd->privacy_led = led_get(sd->dev, "privacy-led");
  2036. if (IS_ERR(sd->privacy_led) && PTR_ERR(sd->privacy_led) != -ENOENT)
  2037. return dev_err_probe(sd->dev, PTR_ERR(sd->privacy_led),
  2038. "getting privacy LED\n");
  2039. if (!IS_ERR_OR_NULL(sd->privacy_led)) {
  2040. mutex_lock(&sd->privacy_led->led_access);
  2041. led_sysfs_disable(sd->privacy_led);
  2042. led_trigger_remove(sd->privacy_led);
  2043. led_set_brightness(sd->privacy_led, 0);
  2044. mutex_unlock(&sd->privacy_led->led_access);
  2045. }
  2046. #endif
  2047. return 0;
  2048. }
  2049. EXPORT_SYMBOL_GPL(v4l2_subdev_get_privacy_led);
  2050. void v4l2_subdev_put_privacy_led(struct v4l2_subdev *sd)
  2051. {
  2052. #if IS_REACHABLE(CONFIG_LEDS_CLASS)
  2053. if (!IS_ERR_OR_NULL(sd->privacy_led)) {
  2054. mutex_lock(&sd->privacy_led->led_access);
  2055. led_sysfs_enable(sd->privacy_led);
  2056. mutex_unlock(&sd->privacy_led->led_access);
  2057. led_put(sd->privacy_led);
  2058. }
  2059. #endif
  2060. }
  2061. EXPORT_SYMBOL_GPL(v4l2_subdev_put_privacy_led);