v4l2-subdev.c 65 KB

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