drm_crtc.c 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408
  1. /*
  2. * Copyright (c) 2006-2008 Intel Corporation
  3. * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
  4. * Copyright (c) 2008 Red Hat Inc.
  5. *
  6. * DRM core CRTC related functions
  7. *
  8. * Permission to use, copy, modify, distribute, and sell this software and its
  9. * documentation for any purpose is hereby granted without fee, provided that
  10. * the above copyright notice appear in all copies and that both that copyright
  11. * notice and this permission notice appear in supporting documentation, and
  12. * that the name of the copyright holders not be used in advertising or
  13. * publicity pertaining to distribution of the software without specific,
  14. * written prior permission. The copyright holders make no representations
  15. * about the suitability of this software for any purpose. It is provided "as
  16. * is" without express or implied warranty.
  17. *
  18. * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  19. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  20. * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  21. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  22. * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  23. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  24. * OF THIS SOFTWARE.
  25. *
  26. * Authors:
  27. * Keith Packard
  28. * Eric Anholt <eric@anholt.net>
  29. * Dave Airlie <airlied@linux.ie>
  30. * Jesse Barnes <jesse.barnes@intel.com>
  31. */
  32. #include <linux/list.h>
  33. #include <linux/slab.h>
  34. #include <linux/export.h>
  35. #include "drm.h"
  36. #include "drmP.h"
  37. #include "drm_crtc.h"
  38. #include "drm_edid.h"
  39. #include "drm_fourcc.h"
  40. /* Avoid boilerplate. I'm tired of typing. */
  41. #define DRM_ENUM_NAME_FN(fnname, list) \
  42. char *fnname(int val) \
  43. { \
  44. int i; \
  45. for (i = 0; i < ARRAY_SIZE(list); i++) { \
  46. if (list[i].type == val) \
  47. return list[i].name; \
  48. } \
  49. return "(unknown)"; \
  50. }
  51. /*
  52. * Global properties
  53. */
  54. static struct drm_prop_enum_list drm_dpms_enum_list[] =
  55. { { DRM_MODE_DPMS_ON, "On" },
  56. { DRM_MODE_DPMS_STANDBY, "Standby" },
  57. { DRM_MODE_DPMS_SUSPEND, "Suspend" },
  58. { DRM_MODE_DPMS_OFF, "Off" }
  59. };
  60. DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
  61. /*
  62. * Optional properties
  63. */
  64. static struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
  65. {
  66. { DRM_MODE_SCALE_NONE, "None" },
  67. { DRM_MODE_SCALE_FULLSCREEN, "Full" },
  68. { DRM_MODE_SCALE_CENTER, "Center" },
  69. { DRM_MODE_SCALE_ASPECT, "Full aspect" },
  70. };
  71. static struct drm_prop_enum_list drm_dithering_mode_enum_list[] =
  72. {
  73. { DRM_MODE_DITHERING_OFF, "Off" },
  74. { DRM_MODE_DITHERING_ON, "On" },
  75. { DRM_MODE_DITHERING_AUTO, "Automatic" },
  76. };
  77. /*
  78. * Non-global properties, but "required" for certain connectors.
  79. */
  80. static struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
  81. {
  82. { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
  83. { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
  84. { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
  85. };
  86. DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
  87. static struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
  88. {
  89. { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
  90. { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
  91. { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
  92. };
  93. DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
  94. drm_dvi_i_subconnector_enum_list)
  95. static struct drm_prop_enum_list drm_tv_select_enum_list[] =
  96. {
  97. { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
  98. { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
  99. { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
  100. { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
  101. { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
  102. };
  103. DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
  104. static struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
  105. {
  106. { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
  107. { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
  108. { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
  109. { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
  110. { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
  111. };
  112. DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
  113. drm_tv_subconnector_enum_list)
  114. static struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
  115. { DRM_MODE_DIRTY_OFF, "Off" },
  116. { DRM_MODE_DIRTY_ON, "On" },
  117. { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
  118. };
  119. DRM_ENUM_NAME_FN(drm_get_dirty_info_name,
  120. drm_dirty_info_enum_list)
  121. struct drm_conn_prop_enum_list {
  122. int type;
  123. char *name;
  124. int count;
  125. };
  126. /*
  127. * Connector and encoder types.
  128. */
  129. static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
  130. { { DRM_MODE_CONNECTOR_Unknown, "Unknown", 0 },
  131. { DRM_MODE_CONNECTOR_VGA, "VGA", 0 },
  132. { DRM_MODE_CONNECTOR_DVII, "DVI-I", 0 },
  133. { DRM_MODE_CONNECTOR_DVID, "DVI-D", 0 },
  134. { DRM_MODE_CONNECTOR_DVIA, "DVI-A", 0 },
  135. { DRM_MODE_CONNECTOR_Composite, "Composite", 0 },
  136. { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO", 0 },
  137. { DRM_MODE_CONNECTOR_LVDS, "LVDS", 0 },
  138. { DRM_MODE_CONNECTOR_Component, "Component", 0 },
  139. { DRM_MODE_CONNECTOR_9PinDIN, "DIN", 0 },
  140. { DRM_MODE_CONNECTOR_DisplayPort, "DP", 0 },
  141. { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A", 0 },
  142. { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B", 0 },
  143. { DRM_MODE_CONNECTOR_TV, "TV", 0 },
  144. { DRM_MODE_CONNECTOR_eDP, "eDP", 0 },
  145. { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual", 0},
  146. };
  147. static struct drm_prop_enum_list drm_encoder_enum_list[] =
  148. { { DRM_MODE_ENCODER_NONE, "None" },
  149. { DRM_MODE_ENCODER_DAC, "DAC" },
  150. { DRM_MODE_ENCODER_TMDS, "TMDS" },
  151. { DRM_MODE_ENCODER_LVDS, "LVDS" },
  152. { DRM_MODE_ENCODER_TVDAC, "TV" },
  153. { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
  154. };
  155. char *drm_get_encoder_name(struct drm_encoder *encoder)
  156. {
  157. static char buf[32];
  158. snprintf(buf, 32, "%s-%d",
  159. drm_encoder_enum_list[encoder->encoder_type].name,
  160. encoder->base.id);
  161. return buf;
  162. }
  163. EXPORT_SYMBOL(drm_get_encoder_name);
  164. char *drm_get_connector_name(struct drm_connector *connector)
  165. {
  166. static char buf[32];
  167. snprintf(buf, 32, "%s-%d",
  168. drm_connector_enum_list[connector->connector_type].name,
  169. connector->connector_type_id);
  170. return buf;
  171. }
  172. EXPORT_SYMBOL(drm_get_connector_name);
  173. char *drm_get_connector_status_name(enum drm_connector_status status)
  174. {
  175. if (status == connector_status_connected)
  176. return "connected";
  177. else if (status == connector_status_disconnected)
  178. return "disconnected";
  179. else
  180. return "unknown";
  181. }
  182. /**
  183. * drm_mode_object_get - allocate a new identifier
  184. * @dev: DRM device
  185. * @ptr: object pointer, used to generate unique ID
  186. * @type: object type
  187. *
  188. * LOCKING:
  189. *
  190. * Create a unique identifier based on @ptr in @dev's identifier space. Used
  191. * for tracking modes, CRTCs and connectors.
  192. *
  193. * RETURNS:
  194. * New unique (relative to other objects in @dev) integer identifier for the
  195. * object.
  196. */
  197. static int drm_mode_object_get(struct drm_device *dev,
  198. struct drm_mode_object *obj, uint32_t obj_type)
  199. {
  200. int new_id = 0;
  201. int ret;
  202. again:
  203. if (idr_pre_get(&dev->mode_config.crtc_idr, GFP_KERNEL) == 0) {
  204. DRM_ERROR("Ran out memory getting a mode number\n");
  205. return -EINVAL;
  206. }
  207. mutex_lock(&dev->mode_config.idr_mutex);
  208. ret = idr_get_new_above(&dev->mode_config.crtc_idr, obj, 1, &new_id);
  209. mutex_unlock(&dev->mode_config.idr_mutex);
  210. if (ret == -EAGAIN)
  211. goto again;
  212. obj->id = new_id;
  213. obj->type = obj_type;
  214. return 0;
  215. }
  216. /**
  217. * drm_mode_object_put - free an identifer
  218. * @dev: DRM device
  219. * @id: ID to free
  220. *
  221. * LOCKING:
  222. * Caller must hold DRM mode_config lock.
  223. *
  224. * Free @id from @dev's unique identifier pool.
  225. */
  226. static void drm_mode_object_put(struct drm_device *dev,
  227. struct drm_mode_object *object)
  228. {
  229. mutex_lock(&dev->mode_config.idr_mutex);
  230. idr_remove(&dev->mode_config.crtc_idr, object->id);
  231. mutex_unlock(&dev->mode_config.idr_mutex);
  232. }
  233. struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
  234. uint32_t id, uint32_t type)
  235. {
  236. struct drm_mode_object *obj = NULL;
  237. mutex_lock(&dev->mode_config.idr_mutex);
  238. obj = idr_find(&dev->mode_config.crtc_idr, id);
  239. if (!obj || (obj->type != type) || (obj->id != id))
  240. obj = NULL;
  241. mutex_unlock(&dev->mode_config.idr_mutex);
  242. return obj;
  243. }
  244. EXPORT_SYMBOL(drm_mode_object_find);
  245. /**
  246. * drm_framebuffer_init - initialize a framebuffer
  247. * @dev: DRM device
  248. *
  249. * LOCKING:
  250. * Caller must hold mode config lock.
  251. *
  252. * Allocates an ID for the framebuffer's parent mode object, sets its mode
  253. * functions & device file and adds it to the master fd list.
  254. *
  255. * RETURNS:
  256. * Zero on success, error code on failure.
  257. */
  258. int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
  259. const struct drm_framebuffer_funcs *funcs)
  260. {
  261. int ret;
  262. ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
  263. if (ret) {
  264. return ret;
  265. }
  266. fb->dev = dev;
  267. fb->funcs = funcs;
  268. dev->mode_config.num_fb++;
  269. list_add(&fb->head, &dev->mode_config.fb_list);
  270. return 0;
  271. }
  272. EXPORT_SYMBOL(drm_framebuffer_init);
  273. /**
  274. * drm_framebuffer_cleanup - remove a framebuffer object
  275. * @fb: framebuffer to remove
  276. *
  277. * LOCKING:
  278. * Caller must hold mode config lock.
  279. *
  280. * Scans all the CRTCs in @dev's mode_config. If they're using @fb, removes
  281. * it, setting it to NULL.
  282. */
  283. void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
  284. {
  285. struct drm_device *dev = fb->dev;
  286. struct drm_crtc *crtc;
  287. struct drm_plane *plane;
  288. struct drm_mode_set set;
  289. int ret;
  290. /* remove from any CRTC */
  291. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  292. if (crtc->fb == fb) {
  293. /* should turn off the crtc */
  294. memset(&set, 0, sizeof(struct drm_mode_set));
  295. set.crtc = crtc;
  296. set.fb = NULL;
  297. ret = crtc->funcs->set_config(&set);
  298. if (ret)
  299. DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
  300. }
  301. }
  302. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  303. if (plane->fb == fb) {
  304. /* should turn off the crtc */
  305. ret = plane->funcs->disable_plane(plane);
  306. if (ret)
  307. DRM_ERROR("failed to disable plane with busy fb\n");
  308. /* disconnect the plane from the fb and crtc: */
  309. plane->fb = NULL;
  310. plane->crtc = NULL;
  311. }
  312. }
  313. drm_mode_object_put(dev, &fb->base);
  314. list_del(&fb->head);
  315. dev->mode_config.num_fb--;
  316. }
  317. EXPORT_SYMBOL(drm_framebuffer_cleanup);
  318. /**
  319. * drm_crtc_init - Initialise a new CRTC object
  320. * @dev: DRM device
  321. * @crtc: CRTC object to init
  322. * @funcs: callbacks for the new CRTC
  323. *
  324. * LOCKING:
  325. * Caller must hold mode config lock.
  326. *
  327. * Inits a new object created as base part of an driver crtc object.
  328. */
  329. void drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
  330. const struct drm_crtc_funcs *funcs)
  331. {
  332. crtc->dev = dev;
  333. crtc->funcs = funcs;
  334. mutex_lock(&dev->mode_config.mutex);
  335. drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
  336. list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
  337. dev->mode_config.num_crtc++;
  338. mutex_unlock(&dev->mode_config.mutex);
  339. }
  340. EXPORT_SYMBOL(drm_crtc_init);
  341. /**
  342. * drm_crtc_cleanup - Cleans up the core crtc usage.
  343. * @crtc: CRTC to cleanup
  344. *
  345. * LOCKING:
  346. * Caller must hold mode config lock.
  347. *
  348. * Cleanup @crtc. Removes from drm modesetting space
  349. * does NOT free object, caller does that.
  350. */
  351. void drm_crtc_cleanup(struct drm_crtc *crtc)
  352. {
  353. struct drm_device *dev = crtc->dev;
  354. if (crtc->gamma_store) {
  355. kfree(crtc->gamma_store);
  356. crtc->gamma_store = NULL;
  357. }
  358. drm_mode_object_put(dev, &crtc->base);
  359. list_del(&crtc->head);
  360. dev->mode_config.num_crtc--;
  361. }
  362. EXPORT_SYMBOL(drm_crtc_cleanup);
  363. /**
  364. * drm_mode_probed_add - add a mode to a connector's probed mode list
  365. * @connector: connector the new mode
  366. * @mode: mode data
  367. *
  368. * LOCKING:
  369. * Caller must hold mode config lock.
  370. *
  371. * Add @mode to @connector's mode list for later use.
  372. */
  373. void drm_mode_probed_add(struct drm_connector *connector,
  374. struct drm_display_mode *mode)
  375. {
  376. list_add(&mode->head, &connector->probed_modes);
  377. }
  378. EXPORT_SYMBOL(drm_mode_probed_add);
  379. /**
  380. * drm_mode_remove - remove and free a mode
  381. * @connector: connector list to modify
  382. * @mode: mode to remove
  383. *
  384. * LOCKING:
  385. * Caller must hold mode config lock.
  386. *
  387. * Remove @mode from @connector's mode list, then free it.
  388. */
  389. void drm_mode_remove(struct drm_connector *connector,
  390. struct drm_display_mode *mode)
  391. {
  392. list_del(&mode->head);
  393. drm_mode_destroy(connector->dev, mode);
  394. }
  395. EXPORT_SYMBOL(drm_mode_remove);
  396. /**
  397. * drm_connector_init - Init a preallocated connector
  398. * @dev: DRM device
  399. * @connector: the connector to init
  400. * @funcs: callbacks for this connector
  401. * @name: user visible name of the connector
  402. *
  403. * LOCKING:
  404. * Takes mode config lock.
  405. *
  406. * Initialises a preallocated connector. Connectors should be
  407. * subclassed as part of driver connector objects.
  408. */
  409. void drm_connector_init(struct drm_device *dev,
  410. struct drm_connector *connector,
  411. const struct drm_connector_funcs *funcs,
  412. int connector_type)
  413. {
  414. mutex_lock(&dev->mode_config.mutex);
  415. connector->dev = dev;
  416. connector->funcs = funcs;
  417. drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
  418. connector->connector_type = connector_type;
  419. connector->connector_type_id =
  420. ++drm_connector_enum_list[connector_type].count; /* TODO */
  421. INIT_LIST_HEAD(&connector->user_modes);
  422. INIT_LIST_HEAD(&connector->probed_modes);
  423. INIT_LIST_HEAD(&connector->modes);
  424. connector->edid_blob_ptr = NULL;
  425. list_add_tail(&connector->head, &dev->mode_config.connector_list);
  426. dev->mode_config.num_connector++;
  427. if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
  428. drm_connector_attach_property(connector,
  429. dev->mode_config.edid_property,
  430. 0);
  431. drm_connector_attach_property(connector,
  432. dev->mode_config.dpms_property, 0);
  433. mutex_unlock(&dev->mode_config.mutex);
  434. }
  435. EXPORT_SYMBOL(drm_connector_init);
  436. /**
  437. * drm_connector_cleanup - cleans up an initialised connector
  438. * @connector: connector to cleanup
  439. *
  440. * LOCKING:
  441. * Takes mode config lock.
  442. *
  443. * Cleans up the connector but doesn't free the object.
  444. */
  445. void drm_connector_cleanup(struct drm_connector *connector)
  446. {
  447. struct drm_device *dev = connector->dev;
  448. struct drm_display_mode *mode, *t;
  449. list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
  450. drm_mode_remove(connector, mode);
  451. list_for_each_entry_safe(mode, t, &connector->modes, head)
  452. drm_mode_remove(connector, mode);
  453. list_for_each_entry_safe(mode, t, &connector->user_modes, head)
  454. drm_mode_remove(connector, mode);
  455. mutex_lock(&dev->mode_config.mutex);
  456. drm_mode_object_put(dev, &connector->base);
  457. list_del(&connector->head);
  458. dev->mode_config.num_connector--;
  459. mutex_unlock(&dev->mode_config.mutex);
  460. }
  461. EXPORT_SYMBOL(drm_connector_cleanup);
  462. void drm_encoder_init(struct drm_device *dev,
  463. struct drm_encoder *encoder,
  464. const struct drm_encoder_funcs *funcs,
  465. int encoder_type)
  466. {
  467. mutex_lock(&dev->mode_config.mutex);
  468. encoder->dev = dev;
  469. drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
  470. encoder->encoder_type = encoder_type;
  471. encoder->funcs = funcs;
  472. list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
  473. dev->mode_config.num_encoder++;
  474. mutex_unlock(&dev->mode_config.mutex);
  475. }
  476. EXPORT_SYMBOL(drm_encoder_init);
  477. void drm_encoder_cleanup(struct drm_encoder *encoder)
  478. {
  479. struct drm_device *dev = encoder->dev;
  480. mutex_lock(&dev->mode_config.mutex);
  481. drm_mode_object_put(dev, &encoder->base);
  482. list_del(&encoder->head);
  483. dev->mode_config.num_encoder--;
  484. mutex_unlock(&dev->mode_config.mutex);
  485. }
  486. EXPORT_SYMBOL(drm_encoder_cleanup);
  487. int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
  488. unsigned long possible_crtcs,
  489. const struct drm_plane_funcs *funcs,
  490. const uint32_t *formats, uint32_t format_count,
  491. bool priv)
  492. {
  493. mutex_lock(&dev->mode_config.mutex);
  494. plane->dev = dev;
  495. drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
  496. plane->funcs = funcs;
  497. plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
  498. GFP_KERNEL);
  499. if (!plane->format_types) {
  500. DRM_DEBUG_KMS("out of memory when allocating plane\n");
  501. drm_mode_object_put(dev, &plane->base);
  502. mutex_unlock(&dev->mode_config.mutex);
  503. return -ENOMEM;
  504. }
  505. memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
  506. plane->format_count = format_count;
  507. plane->possible_crtcs = possible_crtcs;
  508. /* private planes are not exposed to userspace, but depending on
  509. * display hardware, might be convenient to allow sharing programming
  510. * for the scanout engine with the crtc implementation.
  511. */
  512. if (!priv) {
  513. list_add_tail(&plane->head, &dev->mode_config.plane_list);
  514. dev->mode_config.num_plane++;
  515. } else {
  516. INIT_LIST_HEAD(&plane->head);
  517. }
  518. mutex_unlock(&dev->mode_config.mutex);
  519. return 0;
  520. }
  521. EXPORT_SYMBOL(drm_plane_init);
  522. void drm_plane_cleanup(struct drm_plane *plane)
  523. {
  524. struct drm_device *dev = plane->dev;
  525. mutex_lock(&dev->mode_config.mutex);
  526. kfree(plane->format_types);
  527. drm_mode_object_put(dev, &plane->base);
  528. /* if not added to a list, it must be a private plane */
  529. if (!list_empty(&plane->head)) {
  530. list_del(&plane->head);
  531. dev->mode_config.num_plane--;
  532. }
  533. mutex_unlock(&dev->mode_config.mutex);
  534. }
  535. EXPORT_SYMBOL(drm_plane_cleanup);
  536. /**
  537. * drm_mode_create - create a new display mode
  538. * @dev: DRM device
  539. *
  540. * LOCKING:
  541. * Caller must hold DRM mode_config lock.
  542. *
  543. * Create a new drm_display_mode, give it an ID, and return it.
  544. *
  545. * RETURNS:
  546. * Pointer to new mode on success, NULL on error.
  547. */
  548. struct drm_display_mode *drm_mode_create(struct drm_device *dev)
  549. {
  550. struct drm_display_mode *nmode;
  551. nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
  552. if (!nmode)
  553. return NULL;
  554. drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE);
  555. return nmode;
  556. }
  557. EXPORT_SYMBOL(drm_mode_create);
  558. /**
  559. * drm_mode_destroy - remove a mode
  560. * @dev: DRM device
  561. * @mode: mode to remove
  562. *
  563. * LOCKING:
  564. * Caller must hold mode config lock.
  565. *
  566. * Free @mode's unique identifier, then free it.
  567. */
  568. void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
  569. {
  570. if (!mode)
  571. return;
  572. drm_mode_object_put(dev, &mode->base);
  573. kfree(mode);
  574. }
  575. EXPORT_SYMBOL(drm_mode_destroy);
  576. static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
  577. {
  578. struct drm_property *edid;
  579. struct drm_property *dpms;
  580. /*
  581. * Standard properties (apply to all connectors)
  582. */
  583. edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
  584. DRM_MODE_PROP_IMMUTABLE,
  585. "EDID", 0);
  586. dev->mode_config.edid_property = edid;
  587. dpms = drm_property_create_enum(dev, 0,
  588. "DPMS", drm_dpms_enum_list,
  589. ARRAY_SIZE(drm_dpms_enum_list));
  590. dev->mode_config.dpms_property = dpms;
  591. return 0;
  592. }
  593. /**
  594. * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
  595. * @dev: DRM device
  596. *
  597. * Called by a driver the first time a DVI-I connector is made.
  598. */
  599. int drm_mode_create_dvi_i_properties(struct drm_device *dev)
  600. {
  601. struct drm_property *dvi_i_selector;
  602. struct drm_property *dvi_i_subconnector;
  603. if (dev->mode_config.dvi_i_select_subconnector_property)
  604. return 0;
  605. dvi_i_selector =
  606. drm_property_create_enum(dev, 0,
  607. "select subconnector",
  608. drm_dvi_i_select_enum_list,
  609. ARRAY_SIZE(drm_dvi_i_select_enum_list));
  610. dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
  611. dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  612. "subconnector",
  613. drm_dvi_i_subconnector_enum_list,
  614. ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
  615. dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
  616. return 0;
  617. }
  618. EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
  619. /**
  620. * drm_create_tv_properties - create TV specific connector properties
  621. * @dev: DRM device
  622. * @num_modes: number of different TV formats (modes) supported
  623. * @modes: array of pointers to strings containing name of each format
  624. *
  625. * Called by a driver's TV initialization routine, this function creates
  626. * the TV specific connector properties for a given device. Caller is
  627. * responsible for allocating a list of format names and passing them to
  628. * this routine.
  629. */
  630. int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
  631. char *modes[])
  632. {
  633. struct drm_property *tv_selector;
  634. struct drm_property *tv_subconnector;
  635. int i;
  636. if (dev->mode_config.tv_select_subconnector_property)
  637. return 0;
  638. /*
  639. * Basic connector properties
  640. */
  641. tv_selector = drm_property_create_enum(dev, 0,
  642. "select subconnector",
  643. drm_tv_select_enum_list,
  644. ARRAY_SIZE(drm_tv_select_enum_list));
  645. dev->mode_config.tv_select_subconnector_property = tv_selector;
  646. tv_subconnector =
  647. drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  648. "subconnector",
  649. drm_tv_subconnector_enum_list,
  650. ARRAY_SIZE(drm_tv_subconnector_enum_list));
  651. dev->mode_config.tv_subconnector_property = tv_subconnector;
  652. /*
  653. * Other, TV specific properties: margins & TV modes.
  654. */
  655. dev->mode_config.tv_left_margin_property =
  656. drm_property_create_range(dev, 0, "left margin", 0, 100);
  657. dev->mode_config.tv_right_margin_property =
  658. drm_property_create_range(dev, 0, "right margin", 0, 100);
  659. dev->mode_config.tv_top_margin_property =
  660. drm_property_create_range(dev, 0, "top margin", 0, 100);
  661. dev->mode_config.tv_bottom_margin_property =
  662. drm_property_create_range(dev, 0, "bottom margin", 0, 100);
  663. dev->mode_config.tv_mode_property =
  664. drm_property_create(dev, DRM_MODE_PROP_ENUM,
  665. "mode", num_modes);
  666. for (i = 0; i < num_modes; i++)
  667. drm_property_add_enum(dev->mode_config.tv_mode_property, i,
  668. i, modes[i]);
  669. dev->mode_config.tv_brightness_property =
  670. drm_property_create_range(dev, 0, "brightness", 0, 100);
  671. dev->mode_config.tv_contrast_property =
  672. drm_property_create_range(dev, 0, "contrast", 0, 100);
  673. dev->mode_config.tv_flicker_reduction_property =
  674. drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
  675. dev->mode_config.tv_overscan_property =
  676. drm_property_create_range(dev, 0, "overscan", 0, 100);
  677. dev->mode_config.tv_saturation_property =
  678. drm_property_create_range(dev, 0, "saturation", 0, 100);
  679. dev->mode_config.tv_hue_property =
  680. drm_property_create_range(dev, 0, "hue", 0, 100);
  681. return 0;
  682. }
  683. EXPORT_SYMBOL(drm_mode_create_tv_properties);
  684. /**
  685. * drm_mode_create_scaling_mode_property - create scaling mode property
  686. * @dev: DRM device
  687. *
  688. * Called by a driver the first time it's needed, must be attached to desired
  689. * connectors.
  690. */
  691. int drm_mode_create_scaling_mode_property(struct drm_device *dev)
  692. {
  693. struct drm_property *scaling_mode;
  694. if (dev->mode_config.scaling_mode_property)
  695. return 0;
  696. scaling_mode =
  697. drm_property_create_enum(dev, 0, "scaling mode",
  698. drm_scaling_mode_enum_list,
  699. ARRAY_SIZE(drm_scaling_mode_enum_list));
  700. dev->mode_config.scaling_mode_property = scaling_mode;
  701. return 0;
  702. }
  703. EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
  704. /**
  705. * drm_mode_create_dithering_property - create dithering property
  706. * @dev: DRM device
  707. *
  708. * Called by a driver the first time it's needed, must be attached to desired
  709. * connectors.
  710. */
  711. int drm_mode_create_dithering_property(struct drm_device *dev)
  712. {
  713. struct drm_property *dithering_mode;
  714. if (dev->mode_config.dithering_mode_property)
  715. return 0;
  716. dithering_mode =
  717. drm_property_create_enum(dev, 0, "dithering",
  718. drm_dithering_mode_enum_list,
  719. ARRAY_SIZE(drm_dithering_mode_enum_list));
  720. dev->mode_config.dithering_mode_property = dithering_mode;
  721. return 0;
  722. }
  723. EXPORT_SYMBOL(drm_mode_create_dithering_property);
  724. /**
  725. * drm_mode_create_dirty_property - create dirty property
  726. * @dev: DRM device
  727. *
  728. * Called by a driver the first time it's needed, must be attached to desired
  729. * connectors.
  730. */
  731. int drm_mode_create_dirty_info_property(struct drm_device *dev)
  732. {
  733. struct drm_property *dirty_info;
  734. if (dev->mode_config.dirty_info_property)
  735. return 0;
  736. dirty_info =
  737. drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  738. "dirty",
  739. drm_dirty_info_enum_list,
  740. ARRAY_SIZE(drm_dirty_info_enum_list));
  741. dev->mode_config.dirty_info_property = dirty_info;
  742. return 0;
  743. }
  744. EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
  745. /**
  746. * drm_mode_config_init - initialize DRM mode_configuration structure
  747. * @dev: DRM device
  748. *
  749. * LOCKING:
  750. * None, should happen single threaded at init time.
  751. *
  752. * Initialize @dev's mode_config structure, used for tracking the graphics
  753. * configuration of @dev.
  754. */
  755. void drm_mode_config_init(struct drm_device *dev)
  756. {
  757. mutex_init(&dev->mode_config.mutex);
  758. mutex_init(&dev->mode_config.idr_mutex);
  759. INIT_LIST_HEAD(&dev->mode_config.fb_list);
  760. INIT_LIST_HEAD(&dev->mode_config.crtc_list);
  761. INIT_LIST_HEAD(&dev->mode_config.connector_list);
  762. INIT_LIST_HEAD(&dev->mode_config.encoder_list);
  763. INIT_LIST_HEAD(&dev->mode_config.property_list);
  764. INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
  765. INIT_LIST_HEAD(&dev->mode_config.plane_list);
  766. idr_init(&dev->mode_config.crtc_idr);
  767. mutex_lock(&dev->mode_config.mutex);
  768. drm_mode_create_standard_connector_properties(dev);
  769. mutex_unlock(&dev->mode_config.mutex);
  770. /* Just to be sure */
  771. dev->mode_config.num_fb = 0;
  772. dev->mode_config.num_connector = 0;
  773. dev->mode_config.num_crtc = 0;
  774. dev->mode_config.num_encoder = 0;
  775. }
  776. EXPORT_SYMBOL(drm_mode_config_init);
  777. int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
  778. {
  779. uint32_t total_objects = 0;
  780. total_objects += dev->mode_config.num_crtc;
  781. total_objects += dev->mode_config.num_connector;
  782. total_objects += dev->mode_config.num_encoder;
  783. group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
  784. if (!group->id_list)
  785. return -ENOMEM;
  786. group->num_crtcs = 0;
  787. group->num_connectors = 0;
  788. group->num_encoders = 0;
  789. return 0;
  790. }
  791. int drm_mode_group_init_legacy_group(struct drm_device *dev,
  792. struct drm_mode_group *group)
  793. {
  794. struct drm_crtc *crtc;
  795. struct drm_encoder *encoder;
  796. struct drm_connector *connector;
  797. int ret;
  798. if ((ret = drm_mode_group_init(dev, group)))
  799. return ret;
  800. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
  801. group->id_list[group->num_crtcs++] = crtc->base.id;
  802. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  803. group->id_list[group->num_crtcs + group->num_encoders++] =
  804. encoder->base.id;
  805. list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  806. group->id_list[group->num_crtcs + group->num_encoders +
  807. group->num_connectors++] = connector->base.id;
  808. return 0;
  809. }
  810. /**
  811. * drm_mode_config_cleanup - free up DRM mode_config info
  812. * @dev: DRM device
  813. *
  814. * LOCKING:
  815. * Caller must hold mode config lock.
  816. *
  817. * Free up all the connectors and CRTCs associated with this DRM device, then
  818. * free up the framebuffers and associated buffer objects.
  819. *
  820. * FIXME: cleanup any dangling user buffer objects too
  821. */
  822. void drm_mode_config_cleanup(struct drm_device *dev)
  823. {
  824. struct drm_connector *connector, *ot;
  825. struct drm_crtc *crtc, *ct;
  826. struct drm_encoder *encoder, *enct;
  827. struct drm_framebuffer *fb, *fbt;
  828. struct drm_property *property, *pt;
  829. struct drm_plane *plane, *plt;
  830. list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
  831. head) {
  832. encoder->funcs->destroy(encoder);
  833. }
  834. list_for_each_entry_safe(connector, ot,
  835. &dev->mode_config.connector_list, head) {
  836. connector->funcs->destroy(connector);
  837. }
  838. list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
  839. head) {
  840. drm_property_destroy(dev, property);
  841. }
  842. list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
  843. fb->funcs->destroy(fb);
  844. }
  845. list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
  846. crtc->funcs->destroy(crtc);
  847. }
  848. list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
  849. head) {
  850. plane->funcs->destroy(plane);
  851. }
  852. idr_remove_all(&dev->mode_config.crtc_idr);
  853. idr_destroy(&dev->mode_config.crtc_idr);
  854. }
  855. EXPORT_SYMBOL(drm_mode_config_cleanup);
  856. /**
  857. * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
  858. * @out: drm_mode_modeinfo struct to return to the user
  859. * @in: drm_display_mode to use
  860. *
  861. * LOCKING:
  862. * None.
  863. *
  864. * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
  865. * the user.
  866. */
  867. void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
  868. struct drm_display_mode *in)
  869. {
  870. WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
  871. in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
  872. in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
  873. in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
  874. in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
  875. "timing values too large for mode info\n");
  876. out->clock = in->clock;
  877. out->hdisplay = in->hdisplay;
  878. out->hsync_start = in->hsync_start;
  879. out->hsync_end = in->hsync_end;
  880. out->htotal = in->htotal;
  881. out->hskew = in->hskew;
  882. out->vdisplay = in->vdisplay;
  883. out->vsync_start = in->vsync_start;
  884. out->vsync_end = in->vsync_end;
  885. out->vtotal = in->vtotal;
  886. out->vscan = in->vscan;
  887. out->vrefresh = in->vrefresh;
  888. out->flags = in->flags;
  889. out->type = in->type;
  890. strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
  891. out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
  892. }
  893. /**
  894. * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
  895. * @out: drm_display_mode to return to the user
  896. * @in: drm_mode_modeinfo to use
  897. *
  898. * LOCKING:
  899. * None.
  900. *
  901. * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
  902. * the caller.
  903. *
  904. * RETURNS:
  905. * Zero on success, errno on failure.
  906. */
  907. int drm_crtc_convert_umode(struct drm_display_mode *out,
  908. struct drm_mode_modeinfo *in)
  909. {
  910. if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
  911. return -ERANGE;
  912. out->clock = in->clock;
  913. out->hdisplay = in->hdisplay;
  914. out->hsync_start = in->hsync_start;
  915. out->hsync_end = in->hsync_end;
  916. out->htotal = in->htotal;
  917. out->hskew = in->hskew;
  918. out->vdisplay = in->vdisplay;
  919. out->vsync_start = in->vsync_start;
  920. out->vsync_end = in->vsync_end;
  921. out->vtotal = in->vtotal;
  922. out->vscan = in->vscan;
  923. out->vrefresh = in->vrefresh;
  924. out->flags = in->flags;
  925. out->type = in->type;
  926. strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
  927. out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
  928. return 0;
  929. }
  930. /**
  931. * drm_mode_getresources - get graphics configuration
  932. * @inode: inode from the ioctl
  933. * @filp: file * from the ioctl
  934. * @cmd: cmd from ioctl
  935. * @arg: arg from ioctl
  936. *
  937. * LOCKING:
  938. * Takes mode config lock.
  939. *
  940. * Construct a set of configuration description structures and return
  941. * them to the user, including CRTC, connector and framebuffer configuration.
  942. *
  943. * Called by the user via ioctl.
  944. *
  945. * RETURNS:
  946. * Zero on success, errno on failure.
  947. */
  948. int drm_mode_getresources(struct drm_device *dev, void *data,
  949. struct drm_file *file_priv)
  950. {
  951. struct drm_mode_card_res *card_res = data;
  952. struct list_head *lh;
  953. struct drm_framebuffer *fb;
  954. struct drm_connector *connector;
  955. struct drm_crtc *crtc;
  956. struct drm_encoder *encoder;
  957. int ret = 0;
  958. int connector_count = 0;
  959. int crtc_count = 0;
  960. int fb_count = 0;
  961. int encoder_count = 0;
  962. int copied = 0, i;
  963. uint32_t __user *fb_id;
  964. uint32_t __user *crtc_id;
  965. uint32_t __user *connector_id;
  966. uint32_t __user *encoder_id;
  967. struct drm_mode_group *mode_group;
  968. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  969. return -EINVAL;
  970. mutex_lock(&dev->mode_config.mutex);
  971. /*
  972. * For the non-control nodes we need to limit the list of resources
  973. * by IDs in the group list for this node
  974. */
  975. list_for_each(lh, &file_priv->fbs)
  976. fb_count++;
  977. mode_group = &file_priv->master->minor->mode_group;
  978. if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
  979. list_for_each(lh, &dev->mode_config.crtc_list)
  980. crtc_count++;
  981. list_for_each(lh, &dev->mode_config.connector_list)
  982. connector_count++;
  983. list_for_each(lh, &dev->mode_config.encoder_list)
  984. encoder_count++;
  985. } else {
  986. crtc_count = mode_group->num_crtcs;
  987. connector_count = mode_group->num_connectors;
  988. encoder_count = mode_group->num_encoders;
  989. }
  990. card_res->max_height = dev->mode_config.max_height;
  991. card_res->min_height = dev->mode_config.min_height;
  992. card_res->max_width = dev->mode_config.max_width;
  993. card_res->min_width = dev->mode_config.min_width;
  994. /* handle this in 4 parts */
  995. /* FBs */
  996. if (card_res->count_fbs >= fb_count) {
  997. copied = 0;
  998. fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
  999. list_for_each_entry(fb, &file_priv->fbs, filp_head) {
  1000. if (put_user(fb->base.id, fb_id + copied)) {
  1001. ret = -EFAULT;
  1002. goto out;
  1003. }
  1004. copied++;
  1005. }
  1006. }
  1007. card_res->count_fbs = fb_count;
  1008. /* CRTCs */
  1009. if (card_res->count_crtcs >= crtc_count) {
  1010. copied = 0;
  1011. crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
  1012. if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
  1013. list_for_each_entry(crtc, &dev->mode_config.crtc_list,
  1014. head) {
  1015. DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
  1016. if (put_user(crtc->base.id, crtc_id + copied)) {
  1017. ret = -EFAULT;
  1018. goto out;
  1019. }
  1020. copied++;
  1021. }
  1022. } else {
  1023. for (i = 0; i < mode_group->num_crtcs; i++) {
  1024. if (put_user(mode_group->id_list[i],
  1025. crtc_id + copied)) {
  1026. ret = -EFAULT;
  1027. goto out;
  1028. }
  1029. copied++;
  1030. }
  1031. }
  1032. }
  1033. card_res->count_crtcs = crtc_count;
  1034. /* Encoders */
  1035. if (card_res->count_encoders >= encoder_count) {
  1036. copied = 0;
  1037. encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
  1038. if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
  1039. list_for_each_entry(encoder,
  1040. &dev->mode_config.encoder_list,
  1041. head) {
  1042. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
  1043. drm_get_encoder_name(encoder));
  1044. if (put_user(encoder->base.id, encoder_id +
  1045. copied)) {
  1046. ret = -EFAULT;
  1047. goto out;
  1048. }
  1049. copied++;
  1050. }
  1051. } else {
  1052. for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
  1053. if (put_user(mode_group->id_list[i],
  1054. encoder_id + copied)) {
  1055. ret = -EFAULT;
  1056. goto out;
  1057. }
  1058. copied++;
  1059. }
  1060. }
  1061. }
  1062. card_res->count_encoders = encoder_count;
  1063. /* Connectors */
  1064. if (card_res->count_connectors >= connector_count) {
  1065. copied = 0;
  1066. connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
  1067. if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
  1068. list_for_each_entry(connector,
  1069. &dev->mode_config.connector_list,
  1070. head) {
  1071. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  1072. connector->base.id,
  1073. drm_get_connector_name(connector));
  1074. if (put_user(connector->base.id,
  1075. connector_id + copied)) {
  1076. ret = -EFAULT;
  1077. goto out;
  1078. }
  1079. copied++;
  1080. }
  1081. } else {
  1082. int start = mode_group->num_crtcs +
  1083. mode_group->num_encoders;
  1084. for (i = start; i < start + mode_group->num_connectors; i++) {
  1085. if (put_user(mode_group->id_list[i],
  1086. connector_id + copied)) {
  1087. ret = -EFAULT;
  1088. goto out;
  1089. }
  1090. copied++;
  1091. }
  1092. }
  1093. }
  1094. card_res->count_connectors = connector_count;
  1095. DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
  1096. card_res->count_connectors, card_res->count_encoders);
  1097. out:
  1098. mutex_unlock(&dev->mode_config.mutex);
  1099. return ret;
  1100. }
  1101. /**
  1102. * drm_mode_getcrtc - get CRTC configuration
  1103. * @inode: inode from the ioctl
  1104. * @filp: file * from the ioctl
  1105. * @cmd: cmd from ioctl
  1106. * @arg: arg from ioctl
  1107. *
  1108. * LOCKING:
  1109. * Takes mode config lock.
  1110. *
  1111. * Construct a CRTC configuration structure to return to the user.
  1112. *
  1113. * Called by the user via ioctl.
  1114. *
  1115. * RETURNS:
  1116. * Zero on success, errno on failure.
  1117. */
  1118. int drm_mode_getcrtc(struct drm_device *dev,
  1119. void *data, struct drm_file *file_priv)
  1120. {
  1121. struct drm_mode_crtc *crtc_resp = data;
  1122. struct drm_crtc *crtc;
  1123. struct drm_mode_object *obj;
  1124. int ret = 0;
  1125. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1126. return -EINVAL;
  1127. mutex_lock(&dev->mode_config.mutex);
  1128. obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
  1129. DRM_MODE_OBJECT_CRTC);
  1130. if (!obj) {
  1131. ret = -EINVAL;
  1132. goto out;
  1133. }
  1134. crtc = obj_to_crtc(obj);
  1135. crtc_resp->x = crtc->x;
  1136. crtc_resp->y = crtc->y;
  1137. crtc_resp->gamma_size = crtc->gamma_size;
  1138. if (crtc->fb)
  1139. crtc_resp->fb_id = crtc->fb->base.id;
  1140. else
  1141. crtc_resp->fb_id = 0;
  1142. if (crtc->enabled) {
  1143. drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
  1144. crtc_resp->mode_valid = 1;
  1145. } else {
  1146. crtc_resp->mode_valid = 0;
  1147. }
  1148. out:
  1149. mutex_unlock(&dev->mode_config.mutex);
  1150. return ret;
  1151. }
  1152. /**
  1153. * drm_mode_getconnector - get connector configuration
  1154. * @inode: inode from the ioctl
  1155. * @filp: file * from the ioctl
  1156. * @cmd: cmd from ioctl
  1157. * @arg: arg from ioctl
  1158. *
  1159. * LOCKING:
  1160. * Takes mode config lock.
  1161. *
  1162. * Construct a connector configuration structure to return to the user.
  1163. *
  1164. * Called by the user via ioctl.
  1165. *
  1166. * RETURNS:
  1167. * Zero on success, errno on failure.
  1168. */
  1169. int drm_mode_getconnector(struct drm_device *dev, void *data,
  1170. struct drm_file *file_priv)
  1171. {
  1172. struct drm_mode_get_connector *out_resp = data;
  1173. struct drm_mode_object *obj;
  1174. struct drm_connector *connector;
  1175. struct drm_display_mode *mode;
  1176. int mode_count = 0;
  1177. int props_count = 0;
  1178. int encoders_count = 0;
  1179. int ret = 0;
  1180. int copied = 0;
  1181. int i;
  1182. struct drm_mode_modeinfo u_mode;
  1183. struct drm_mode_modeinfo __user *mode_ptr;
  1184. uint32_t __user *prop_ptr;
  1185. uint64_t __user *prop_values;
  1186. uint32_t __user *encoder_ptr;
  1187. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1188. return -EINVAL;
  1189. memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
  1190. DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
  1191. mutex_lock(&dev->mode_config.mutex);
  1192. obj = drm_mode_object_find(dev, out_resp->connector_id,
  1193. DRM_MODE_OBJECT_CONNECTOR);
  1194. if (!obj) {
  1195. ret = -EINVAL;
  1196. goto out;
  1197. }
  1198. connector = obj_to_connector(obj);
  1199. for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
  1200. if (connector->property_ids[i] != 0) {
  1201. props_count++;
  1202. }
  1203. }
  1204. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  1205. if (connector->encoder_ids[i] != 0) {
  1206. encoders_count++;
  1207. }
  1208. }
  1209. if (out_resp->count_modes == 0) {
  1210. connector->funcs->fill_modes(connector,
  1211. dev->mode_config.max_width,
  1212. dev->mode_config.max_height);
  1213. }
  1214. /* delayed so we get modes regardless of pre-fill_modes state */
  1215. list_for_each_entry(mode, &connector->modes, head)
  1216. mode_count++;
  1217. out_resp->connector_id = connector->base.id;
  1218. out_resp->connector_type = connector->connector_type;
  1219. out_resp->connector_type_id = connector->connector_type_id;
  1220. out_resp->mm_width = connector->display_info.width_mm;
  1221. out_resp->mm_height = connector->display_info.height_mm;
  1222. out_resp->subpixel = connector->display_info.subpixel_order;
  1223. out_resp->connection = connector->status;
  1224. if (connector->encoder)
  1225. out_resp->encoder_id = connector->encoder->base.id;
  1226. else
  1227. out_resp->encoder_id = 0;
  1228. /*
  1229. * This ioctl is called twice, once to determine how much space is
  1230. * needed, and the 2nd time to fill it.
  1231. */
  1232. if ((out_resp->count_modes >= mode_count) && mode_count) {
  1233. copied = 0;
  1234. mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
  1235. list_for_each_entry(mode, &connector->modes, head) {
  1236. drm_crtc_convert_to_umode(&u_mode, mode);
  1237. if (copy_to_user(mode_ptr + copied,
  1238. &u_mode, sizeof(u_mode))) {
  1239. ret = -EFAULT;
  1240. goto out;
  1241. }
  1242. copied++;
  1243. }
  1244. }
  1245. out_resp->count_modes = mode_count;
  1246. if ((out_resp->count_props >= props_count) && props_count) {
  1247. copied = 0;
  1248. prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
  1249. prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
  1250. for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
  1251. if (connector->property_ids[i] != 0) {
  1252. if (put_user(connector->property_ids[i],
  1253. prop_ptr + copied)) {
  1254. ret = -EFAULT;
  1255. goto out;
  1256. }
  1257. if (put_user(connector->property_values[i],
  1258. prop_values + copied)) {
  1259. ret = -EFAULT;
  1260. goto out;
  1261. }
  1262. copied++;
  1263. }
  1264. }
  1265. }
  1266. out_resp->count_props = props_count;
  1267. if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
  1268. copied = 0;
  1269. encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
  1270. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  1271. if (connector->encoder_ids[i] != 0) {
  1272. if (put_user(connector->encoder_ids[i],
  1273. encoder_ptr + copied)) {
  1274. ret = -EFAULT;
  1275. goto out;
  1276. }
  1277. copied++;
  1278. }
  1279. }
  1280. }
  1281. out_resp->count_encoders = encoders_count;
  1282. out:
  1283. mutex_unlock(&dev->mode_config.mutex);
  1284. return ret;
  1285. }
  1286. int drm_mode_getencoder(struct drm_device *dev, void *data,
  1287. struct drm_file *file_priv)
  1288. {
  1289. struct drm_mode_get_encoder *enc_resp = data;
  1290. struct drm_mode_object *obj;
  1291. struct drm_encoder *encoder;
  1292. int ret = 0;
  1293. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1294. return -EINVAL;
  1295. mutex_lock(&dev->mode_config.mutex);
  1296. obj = drm_mode_object_find(dev, enc_resp->encoder_id,
  1297. DRM_MODE_OBJECT_ENCODER);
  1298. if (!obj) {
  1299. ret = -EINVAL;
  1300. goto out;
  1301. }
  1302. encoder = obj_to_encoder(obj);
  1303. if (encoder->crtc)
  1304. enc_resp->crtc_id = encoder->crtc->base.id;
  1305. else
  1306. enc_resp->crtc_id = 0;
  1307. enc_resp->encoder_type = encoder->encoder_type;
  1308. enc_resp->encoder_id = encoder->base.id;
  1309. enc_resp->possible_crtcs = encoder->possible_crtcs;
  1310. enc_resp->possible_clones = encoder->possible_clones;
  1311. out:
  1312. mutex_unlock(&dev->mode_config.mutex);
  1313. return ret;
  1314. }
  1315. /**
  1316. * drm_mode_getplane_res - get plane info
  1317. * @dev: DRM device
  1318. * @data: ioctl data
  1319. * @file_priv: DRM file info
  1320. *
  1321. * LOCKING:
  1322. * Takes mode config lock.
  1323. *
  1324. * Return an plane count and set of IDs.
  1325. */
  1326. int drm_mode_getplane_res(struct drm_device *dev, void *data,
  1327. struct drm_file *file_priv)
  1328. {
  1329. struct drm_mode_get_plane_res *plane_resp = data;
  1330. struct drm_mode_config *config;
  1331. struct drm_plane *plane;
  1332. uint32_t __user *plane_ptr;
  1333. int copied = 0, ret = 0;
  1334. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1335. return -EINVAL;
  1336. mutex_lock(&dev->mode_config.mutex);
  1337. config = &dev->mode_config;
  1338. /*
  1339. * This ioctl is called twice, once to determine how much space is
  1340. * needed, and the 2nd time to fill it.
  1341. */
  1342. if (config->num_plane &&
  1343. (plane_resp->count_planes >= config->num_plane)) {
  1344. plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
  1345. list_for_each_entry(plane, &config->plane_list, head) {
  1346. if (put_user(plane->base.id, plane_ptr + copied)) {
  1347. ret = -EFAULT;
  1348. goto out;
  1349. }
  1350. copied++;
  1351. }
  1352. }
  1353. plane_resp->count_planes = config->num_plane;
  1354. out:
  1355. mutex_unlock(&dev->mode_config.mutex);
  1356. return ret;
  1357. }
  1358. /**
  1359. * drm_mode_getplane - get plane info
  1360. * @dev: DRM device
  1361. * @data: ioctl data
  1362. * @file_priv: DRM file info
  1363. *
  1364. * LOCKING:
  1365. * Takes mode config lock.
  1366. *
  1367. * Return plane info, including formats supported, gamma size, any
  1368. * current fb, etc.
  1369. */
  1370. int drm_mode_getplane(struct drm_device *dev, void *data,
  1371. struct drm_file *file_priv)
  1372. {
  1373. struct drm_mode_get_plane *plane_resp = data;
  1374. struct drm_mode_object *obj;
  1375. struct drm_plane *plane;
  1376. uint32_t __user *format_ptr;
  1377. int ret = 0;
  1378. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1379. return -EINVAL;
  1380. mutex_lock(&dev->mode_config.mutex);
  1381. obj = drm_mode_object_find(dev, plane_resp->plane_id,
  1382. DRM_MODE_OBJECT_PLANE);
  1383. if (!obj) {
  1384. ret = -ENOENT;
  1385. goto out;
  1386. }
  1387. plane = obj_to_plane(obj);
  1388. if (plane->crtc)
  1389. plane_resp->crtc_id = plane->crtc->base.id;
  1390. else
  1391. plane_resp->crtc_id = 0;
  1392. if (plane->fb)
  1393. plane_resp->fb_id = plane->fb->base.id;
  1394. else
  1395. plane_resp->fb_id = 0;
  1396. plane_resp->plane_id = plane->base.id;
  1397. plane_resp->possible_crtcs = plane->possible_crtcs;
  1398. plane_resp->gamma_size = plane->gamma_size;
  1399. /*
  1400. * This ioctl is called twice, once to determine how much space is
  1401. * needed, and the 2nd time to fill it.
  1402. */
  1403. if (plane->format_count &&
  1404. (plane_resp->count_format_types >= plane->format_count)) {
  1405. format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
  1406. if (copy_to_user(format_ptr,
  1407. plane->format_types,
  1408. sizeof(uint32_t) * plane->format_count)) {
  1409. ret = -EFAULT;
  1410. goto out;
  1411. }
  1412. }
  1413. plane_resp->count_format_types = plane->format_count;
  1414. out:
  1415. mutex_unlock(&dev->mode_config.mutex);
  1416. return ret;
  1417. }
  1418. /**
  1419. * drm_mode_setplane - set up or tear down an plane
  1420. * @dev: DRM device
  1421. * @data: ioctl data*
  1422. * @file_prive: DRM file info
  1423. *
  1424. * LOCKING:
  1425. * Takes mode config lock.
  1426. *
  1427. * Set plane info, including placement, fb, scaling, and other factors.
  1428. * Or pass a NULL fb to disable.
  1429. */
  1430. int drm_mode_setplane(struct drm_device *dev, void *data,
  1431. struct drm_file *file_priv)
  1432. {
  1433. struct drm_mode_set_plane *plane_req = data;
  1434. struct drm_mode_object *obj;
  1435. struct drm_plane *plane;
  1436. struct drm_crtc *crtc;
  1437. struct drm_framebuffer *fb;
  1438. int ret = 0;
  1439. unsigned int fb_width, fb_height;
  1440. int i;
  1441. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1442. return -EINVAL;
  1443. mutex_lock(&dev->mode_config.mutex);
  1444. /*
  1445. * First, find the plane, crtc, and fb objects. If not available,
  1446. * we don't bother to call the driver.
  1447. */
  1448. obj = drm_mode_object_find(dev, plane_req->plane_id,
  1449. DRM_MODE_OBJECT_PLANE);
  1450. if (!obj) {
  1451. DRM_DEBUG_KMS("Unknown plane ID %d\n",
  1452. plane_req->plane_id);
  1453. ret = -ENOENT;
  1454. goto out;
  1455. }
  1456. plane = obj_to_plane(obj);
  1457. /* No fb means shut it down */
  1458. if (!plane_req->fb_id) {
  1459. plane->funcs->disable_plane(plane);
  1460. plane->crtc = NULL;
  1461. plane->fb = NULL;
  1462. goto out;
  1463. }
  1464. obj = drm_mode_object_find(dev, plane_req->crtc_id,
  1465. DRM_MODE_OBJECT_CRTC);
  1466. if (!obj) {
  1467. DRM_DEBUG_KMS("Unknown crtc ID %d\n",
  1468. plane_req->crtc_id);
  1469. ret = -ENOENT;
  1470. goto out;
  1471. }
  1472. crtc = obj_to_crtc(obj);
  1473. obj = drm_mode_object_find(dev, plane_req->fb_id,
  1474. DRM_MODE_OBJECT_FB);
  1475. if (!obj) {
  1476. DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
  1477. plane_req->fb_id);
  1478. ret = -ENOENT;
  1479. goto out;
  1480. }
  1481. fb = obj_to_fb(obj);
  1482. /* Check whether this plane supports the fb pixel format. */
  1483. for (i = 0; i < plane->format_count; i++)
  1484. if (fb->pixel_format == plane->format_types[i])
  1485. break;
  1486. if (i == plane->format_count) {
  1487. DRM_DEBUG_KMS("Invalid pixel format 0x%08x\n", fb->pixel_format);
  1488. ret = -EINVAL;
  1489. goto out;
  1490. }
  1491. fb_width = fb->width << 16;
  1492. fb_height = fb->height << 16;
  1493. /* Make sure source coordinates are inside the fb. */
  1494. if (plane_req->src_w > fb_width ||
  1495. plane_req->src_x > fb_width - plane_req->src_w ||
  1496. plane_req->src_h > fb_height ||
  1497. plane_req->src_y > fb_height - plane_req->src_h) {
  1498. DRM_DEBUG_KMS("Invalid source coordinates "
  1499. "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
  1500. plane_req->src_w >> 16,
  1501. ((plane_req->src_w & 0xffff) * 15625) >> 10,
  1502. plane_req->src_h >> 16,
  1503. ((plane_req->src_h & 0xffff) * 15625) >> 10,
  1504. plane_req->src_x >> 16,
  1505. ((plane_req->src_x & 0xffff) * 15625) >> 10,
  1506. plane_req->src_y >> 16,
  1507. ((plane_req->src_y & 0xffff) * 15625) >> 10);
  1508. ret = -ENOSPC;
  1509. goto out;
  1510. }
  1511. /* Give drivers some help against integer overflows */
  1512. if (plane_req->crtc_w > INT_MAX ||
  1513. plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
  1514. plane_req->crtc_h > INT_MAX ||
  1515. plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
  1516. DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
  1517. plane_req->crtc_w, plane_req->crtc_h,
  1518. plane_req->crtc_x, plane_req->crtc_y);
  1519. ret = -ERANGE;
  1520. goto out;
  1521. }
  1522. ret = plane->funcs->update_plane(plane, crtc, fb,
  1523. plane_req->crtc_x, plane_req->crtc_y,
  1524. plane_req->crtc_w, plane_req->crtc_h,
  1525. plane_req->src_x, plane_req->src_y,
  1526. plane_req->src_w, plane_req->src_h);
  1527. if (!ret) {
  1528. plane->crtc = crtc;
  1529. plane->fb = fb;
  1530. }
  1531. out:
  1532. mutex_unlock(&dev->mode_config.mutex);
  1533. return ret;
  1534. }
  1535. /**
  1536. * drm_mode_setcrtc - set CRTC configuration
  1537. * @inode: inode from the ioctl
  1538. * @filp: file * from the ioctl
  1539. * @cmd: cmd from ioctl
  1540. * @arg: arg from ioctl
  1541. *
  1542. * LOCKING:
  1543. * Takes mode config lock.
  1544. *
  1545. * Build a new CRTC configuration based on user request.
  1546. *
  1547. * Called by the user via ioctl.
  1548. *
  1549. * RETURNS:
  1550. * Zero on success, errno on failure.
  1551. */
  1552. int drm_mode_setcrtc(struct drm_device *dev, void *data,
  1553. struct drm_file *file_priv)
  1554. {
  1555. struct drm_mode_config *config = &dev->mode_config;
  1556. struct drm_mode_crtc *crtc_req = data;
  1557. struct drm_mode_object *obj;
  1558. struct drm_crtc *crtc;
  1559. struct drm_connector **connector_set = NULL, *connector;
  1560. struct drm_framebuffer *fb = NULL;
  1561. struct drm_display_mode *mode = NULL;
  1562. struct drm_mode_set set;
  1563. uint32_t __user *set_connectors_ptr;
  1564. int ret = 0;
  1565. int i;
  1566. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1567. return -EINVAL;
  1568. /* For some reason crtc x/y offsets are signed internally. */
  1569. if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
  1570. return -ERANGE;
  1571. mutex_lock(&dev->mode_config.mutex);
  1572. obj = drm_mode_object_find(dev, crtc_req->crtc_id,
  1573. DRM_MODE_OBJECT_CRTC);
  1574. if (!obj) {
  1575. DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
  1576. ret = -EINVAL;
  1577. goto out;
  1578. }
  1579. crtc = obj_to_crtc(obj);
  1580. DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
  1581. if (crtc_req->mode_valid) {
  1582. /* If we have a mode we need a framebuffer. */
  1583. /* If we pass -1, set the mode with the currently bound fb */
  1584. if (crtc_req->fb_id == -1) {
  1585. if (!crtc->fb) {
  1586. DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
  1587. ret = -EINVAL;
  1588. goto out;
  1589. }
  1590. fb = crtc->fb;
  1591. } else {
  1592. obj = drm_mode_object_find(dev, crtc_req->fb_id,
  1593. DRM_MODE_OBJECT_FB);
  1594. if (!obj) {
  1595. DRM_DEBUG_KMS("Unknown FB ID%d\n",
  1596. crtc_req->fb_id);
  1597. ret = -EINVAL;
  1598. goto out;
  1599. }
  1600. fb = obj_to_fb(obj);
  1601. }
  1602. mode = drm_mode_create(dev);
  1603. if (!mode) {
  1604. ret = -ENOMEM;
  1605. goto out;
  1606. }
  1607. ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
  1608. if (ret) {
  1609. DRM_DEBUG_KMS("Invalid mode\n");
  1610. goto out;
  1611. }
  1612. drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
  1613. if (mode->hdisplay > fb->width ||
  1614. mode->vdisplay > fb->height ||
  1615. crtc_req->x > fb->width - mode->hdisplay ||
  1616. crtc_req->y > fb->height - mode->vdisplay) {
  1617. DRM_DEBUG_KMS("Invalid CRTC viewport %ux%u+%u+%u for fb size %ux%u.\n",
  1618. mode->hdisplay, mode->vdisplay,
  1619. crtc_req->x, crtc_req->y,
  1620. fb->width, fb->height);
  1621. ret = -ENOSPC;
  1622. goto out;
  1623. }
  1624. }
  1625. if (crtc_req->count_connectors == 0 && mode) {
  1626. DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
  1627. ret = -EINVAL;
  1628. goto out;
  1629. }
  1630. if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
  1631. DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
  1632. crtc_req->count_connectors);
  1633. ret = -EINVAL;
  1634. goto out;
  1635. }
  1636. if (crtc_req->count_connectors > 0) {
  1637. u32 out_id;
  1638. /* Avoid unbounded kernel memory allocation */
  1639. if (crtc_req->count_connectors > config->num_connector) {
  1640. ret = -EINVAL;
  1641. goto out;
  1642. }
  1643. connector_set = kmalloc(crtc_req->count_connectors *
  1644. sizeof(struct drm_connector *),
  1645. GFP_KERNEL);
  1646. if (!connector_set) {
  1647. ret = -ENOMEM;
  1648. goto out;
  1649. }
  1650. for (i = 0; i < crtc_req->count_connectors; i++) {
  1651. set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
  1652. if (get_user(out_id, &set_connectors_ptr[i])) {
  1653. ret = -EFAULT;
  1654. goto out;
  1655. }
  1656. obj = drm_mode_object_find(dev, out_id,
  1657. DRM_MODE_OBJECT_CONNECTOR);
  1658. if (!obj) {
  1659. DRM_DEBUG_KMS("Connector id %d unknown\n",
  1660. out_id);
  1661. ret = -EINVAL;
  1662. goto out;
  1663. }
  1664. connector = obj_to_connector(obj);
  1665. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  1666. connector->base.id,
  1667. drm_get_connector_name(connector));
  1668. connector_set[i] = connector;
  1669. }
  1670. }
  1671. set.crtc = crtc;
  1672. set.x = crtc_req->x;
  1673. set.y = crtc_req->y;
  1674. set.mode = mode;
  1675. set.connectors = connector_set;
  1676. set.num_connectors = crtc_req->count_connectors;
  1677. set.fb = fb;
  1678. ret = crtc->funcs->set_config(&set);
  1679. out:
  1680. kfree(connector_set);
  1681. drm_mode_destroy(dev, mode);
  1682. mutex_unlock(&dev->mode_config.mutex);
  1683. return ret;
  1684. }
  1685. int drm_mode_cursor_ioctl(struct drm_device *dev,
  1686. void *data, struct drm_file *file_priv)
  1687. {
  1688. struct drm_mode_cursor *req = data;
  1689. struct drm_mode_object *obj;
  1690. struct drm_crtc *crtc;
  1691. int ret = 0;
  1692. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1693. return -EINVAL;
  1694. if (!req->flags)
  1695. return -EINVAL;
  1696. mutex_lock(&dev->mode_config.mutex);
  1697. obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
  1698. if (!obj) {
  1699. DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
  1700. ret = -EINVAL;
  1701. goto out;
  1702. }
  1703. crtc = obj_to_crtc(obj);
  1704. if (req->flags & DRM_MODE_CURSOR_BO) {
  1705. if (!crtc->funcs->cursor_set) {
  1706. ret = -ENXIO;
  1707. goto out;
  1708. }
  1709. /* Turns off the cursor if handle is 0 */
  1710. ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
  1711. req->width, req->height);
  1712. }
  1713. if (req->flags & DRM_MODE_CURSOR_MOVE) {
  1714. if (crtc->funcs->cursor_move) {
  1715. ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
  1716. } else {
  1717. ret = -EFAULT;
  1718. goto out;
  1719. }
  1720. }
  1721. out:
  1722. mutex_unlock(&dev->mode_config.mutex);
  1723. return ret;
  1724. }
  1725. /* Original addfb only supported RGB formats, so figure out which one */
  1726. uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
  1727. {
  1728. uint32_t fmt;
  1729. switch (bpp) {
  1730. case 8:
  1731. fmt = DRM_FORMAT_RGB332;
  1732. break;
  1733. case 16:
  1734. if (depth == 15)
  1735. fmt = DRM_FORMAT_XRGB1555;
  1736. else
  1737. fmt = DRM_FORMAT_RGB565;
  1738. break;
  1739. case 24:
  1740. fmt = DRM_FORMAT_RGB888;
  1741. break;
  1742. case 32:
  1743. if (depth == 24)
  1744. fmt = DRM_FORMAT_XRGB8888;
  1745. else if (depth == 30)
  1746. fmt = DRM_FORMAT_XRGB2101010;
  1747. else
  1748. fmt = DRM_FORMAT_ARGB8888;
  1749. break;
  1750. default:
  1751. DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
  1752. fmt = DRM_FORMAT_XRGB8888;
  1753. break;
  1754. }
  1755. return fmt;
  1756. }
  1757. EXPORT_SYMBOL(drm_mode_legacy_fb_format);
  1758. /**
  1759. * drm_mode_addfb - add an FB to the graphics configuration
  1760. * @inode: inode from the ioctl
  1761. * @filp: file * from the ioctl
  1762. * @cmd: cmd from ioctl
  1763. * @arg: arg from ioctl
  1764. *
  1765. * LOCKING:
  1766. * Takes mode config lock.
  1767. *
  1768. * Add a new FB to the specified CRTC, given a user request.
  1769. *
  1770. * Called by the user via ioctl.
  1771. *
  1772. * RETURNS:
  1773. * Zero on success, errno on failure.
  1774. */
  1775. int drm_mode_addfb(struct drm_device *dev,
  1776. void *data, struct drm_file *file_priv)
  1777. {
  1778. struct drm_mode_fb_cmd *or = data;
  1779. struct drm_mode_fb_cmd2 r = {};
  1780. struct drm_mode_config *config = &dev->mode_config;
  1781. struct drm_framebuffer *fb;
  1782. int ret = 0;
  1783. /* Use new struct with format internally */
  1784. r.fb_id = or->fb_id;
  1785. r.width = or->width;
  1786. r.height = or->height;
  1787. r.pitches[0] = or->pitch;
  1788. r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
  1789. r.handles[0] = or->handle;
  1790. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1791. return -EINVAL;
  1792. if ((config->min_width > r.width) || (r.width > config->max_width))
  1793. return -EINVAL;
  1794. if ((config->min_height > r.height) || (r.height > config->max_height))
  1795. return -EINVAL;
  1796. mutex_lock(&dev->mode_config.mutex);
  1797. /* TODO check buffer is sufficiently large */
  1798. /* TODO setup destructor callback */
  1799. fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
  1800. if (IS_ERR(fb)) {
  1801. DRM_ERROR("could not create framebuffer\n");
  1802. ret = PTR_ERR(fb);
  1803. goto out;
  1804. }
  1805. or->fb_id = fb->base.id;
  1806. list_add(&fb->filp_head, &file_priv->fbs);
  1807. DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
  1808. out:
  1809. mutex_unlock(&dev->mode_config.mutex);
  1810. return ret;
  1811. }
  1812. static int format_check(struct drm_mode_fb_cmd2 *r)
  1813. {
  1814. uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
  1815. switch (format) {
  1816. case DRM_FORMAT_C8:
  1817. case DRM_FORMAT_RGB332:
  1818. case DRM_FORMAT_BGR233:
  1819. case DRM_FORMAT_XRGB4444:
  1820. case DRM_FORMAT_XBGR4444:
  1821. case DRM_FORMAT_RGBX4444:
  1822. case DRM_FORMAT_BGRX4444:
  1823. case DRM_FORMAT_ARGB4444:
  1824. case DRM_FORMAT_ABGR4444:
  1825. case DRM_FORMAT_RGBA4444:
  1826. case DRM_FORMAT_BGRA4444:
  1827. case DRM_FORMAT_XRGB1555:
  1828. case DRM_FORMAT_XBGR1555:
  1829. case DRM_FORMAT_RGBX5551:
  1830. case DRM_FORMAT_BGRX5551:
  1831. case DRM_FORMAT_ARGB1555:
  1832. case DRM_FORMAT_ABGR1555:
  1833. case DRM_FORMAT_RGBA5551:
  1834. case DRM_FORMAT_BGRA5551:
  1835. case DRM_FORMAT_RGB565:
  1836. case DRM_FORMAT_BGR565:
  1837. case DRM_FORMAT_RGB888:
  1838. case DRM_FORMAT_BGR888:
  1839. case DRM_FORMAT_XRGB8888:
  1840. case DRM_FORMAT_XBGR8888:
  1841. case DRM_FORMAT_RGBX8888:
  1842. case DRM_FORMAT_BGRX8888:
  1843. case DRM_FORMAT_ARGB8888:
  1844. case DRM_FORMAT_ABGR8888:
  1845. case DRM_FORMAT_RGBA8888:
  1846. case DRM_FORMAT_BGRA8888:
  1847. case DRM_FORMAT_XRGB2101010:
  1848. case DRM_FORMAT_XBGR2101010:
  1849. case DRM_FORMAT_RGBX1010102:
  1850. case DRM_FORMAT_BGRX1010102:
  1851. case DRM_FORMAT_ARGB2101010:
  1852. case DRM_FORMAT_ABGR2101010:
  1853. case DRM_FORMAT_RGBA1010102:
  1854. case DRM_FORMAT_BGRA1010102:
  1855. case DRM_FORMAT_YUYV:
  1856. case DRM_FORMAT_YVYU:
  1857. case DRM_FORMAT_UYVY:
  1858. case DRM_FORMAT_VYUY:
  1859. case DRM_FORMAT_AYUV:
  1860. case DRM_FORMAT_NV12:
  1861. case DRM_FORMAT_NV21:
  1862. case DRM_FORMAT_NV16:
  1863. case DRM_FORMAT_NV61:
  1864. case DRM_FORMAT_YUV410:
  1865. case DRM_FORMAT_YVU410:
  1866. case DRM_FORMAT_YUV411:
  1867. case DRM_FORMAT_YVU411:
  1868. case DRM_FORMAT_YUV420:
  1869. case DRM_FORMAT_YVU420:
  1870. case DRM_FORMAT_YUV422:
  1871. case DRM_FORMAT_YVU422:
  1872. case DRM_FORMAT_YUV444:
  1873. case DRM_FORMAT_YVU444:
  1874. return 0;
  1875. default:
  1876. return -EINVAL;
  1877. }
  1878. }
  1879. /**
  1880. * drm_mode_addfb2 - add an FB to the graphics configuration
  1881. * @inode: inode from the ioctl
  1882. * @filp: file * from the ioctl
  1883. * @cmd: cmd from ioctl
  1884. * @arg: arg from ioctl
  1885. *
  1886. * LOCKING:
  1887. * Takes mode config lock.
  1888. *
  1889. * Add a new FB to the specified CRTC, given a user request with format.
  1890. *
  1891. * Called by the user via ioctl.
  1892. *
  1893. * RETURNS:
  1894. * Zero on success, errno on failure.
  1895. */
  1896. int drm_mode_addfb2(struct drm_device *dev,
  1897. void *data, struct drm_file *file_priv)
  1898. {
  1899. struct drm_mode_fb_cmd2 *r = data;
  1900. struct drm_mode_config *config = &dev->mode_config;
  1901. struct drm_framebuffer *fb;
  1902. int ret = 0;
  1903. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1904. return -EINVAL;
  1905. if ((config->min_width > r->width) || (r->width > config->max_width)) {
  1906. DRM_ERROR("bad framebuffer width %d, should be >= %d && <= %d\n",
  1907. r->width, config->min_width, config->max_width);
  1908. return -EINVAL;
  1909. }
  1910. if ((config->min_height > r->height) || (r->height > config->max_height)) {
  1911. DRM_ERROR("bad framebuffer height %d, should be >= %d && <= %d\n",
  1912. r->height, config->min_height, config->max_height);
  1913. return -EINVAL;
  1914. }
  1915. ret = format_check(r);
  1916. if (ret) {
  1917. DRM_ERROR("bad framebuffer format 0x%08x\n", r->pixel_format);
  1918. return ret;
  1919. }
  1920. mutex_lock(&dev->mode_config.mutex);
  1921. fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
  1922. if (IS_ERR(fb)) {
  1923. DRM_ERROR("could not create framebuffer\n");
  1924. ret = PTR_ERR(fb);
  1925. goto out;
  1926. }
  1927. r->fb_id = fb->base.id;
  1928. list_add(&fb->filp_head, &file_priv->fbs);
  1929. DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
  1930. out:
  1931. mutex_unlock(&dev->mode_config.mutex);
  1932. return ret;
  1933. }
  1934. /**
  1935. * drm_mode_rmfb - remove an FB from the configuration
  1936. * @inode: inode from the ioctl
  1937. * @filp: file * from the ioctl
  1938. * @cmd: cmd from ioctl
  1939. * @arg: arg from ioctl
  1940. *
  1941. * LOCKING:
  1942. * Takes mode config lock.
  1943. *
  1944. * Remove the FB specified by the user.
  1945. *
  1946. * Called by the user via ioctl.
  1947. *
  1948. * RETURNS:
  1949. * Zero on success, errno on failure.
  1950. */
  1951. int drm_mode_rmfb(struct drm_device *dev,
  1952. void *data, struct drm_file *file_priv)
  1953. {
  1954. struct drm_mode_object *obj;
  1955. struct drm_framebuffer *fb = NULL;
  1956. struct drm_framebuffer *fbl = NULL;
  1957. uint32_t *id = data;
  1958. int ret = 0;
  1959. int found = 0;
  1960. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1961. return -EINVAL;
  1962. mutex_lock(&dev->mode_config.mutex);
  1963. obj = drm_mode_object_find(dev, *id, DRM_MODE_OBJECT_FB);
  1964. /* TODO check that we really get a framebuffer back. */
  1965. if (!obj) {
  1966. ret = -EINVAL;
  1967. goto out;
  1968. }
  1969. fb = obj_to_fb(obj);
  1970. list_for_each_entry(fbl, &file_priv->fbs, filp_head)
  1971. if (fb == fbl)
  1972. found = 1;
  1973. if (!found) {
  1974. ret = -EINVAL;
  1975. goto out;
  1976. }
  1977. /* TODO release all crtc connected to the framebuffer */
  1978. /* TODO unhock the destructor from the buffer object */
  1979. list_del(&fb->filp_head);
  1980. fb->funcs->destroy(fb);
  1981. out:
  1982. mutex_unlock(&dev->mode_config.mutex);
  1983. return ret;
  1984. }
  1985. /**
  1986. * drm_mode_getfb - get FB info
  1987. * @inode: inode from the ioctl
  1988. * @filp: file * from the ioctl
  1989. * @cmd: cmd from ioctl
  1990. * @arg: arg from ioctl
  1991. *
  1992. * LOCKING:
  1993. * Takes mode config lock.
  1994. *
  1995. * Lookup the FB given its ID and return info about it.
  1996. *
  1997. * Called by the user via ioctl.
  1998. *
  1999. * RETURNS:
  2000. * Zero on success, errno on failure.
  2001. */
  2002. int drm_mode_getfb(struct drm_device *dev,
  2003. void *data, struct drm_file *file_priv)
  2004. {
  2005. struct drm_mode_fb_cmd *r = data;
  2006. struct drm_mode_object *obj;
  2007. struct drm_framebuffer *fb;
  2008. int ret = 0;
  2009. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2010. return -EINVAL;
  2011. mutex_lock(&dev->mode_config.mutex);
  2012. obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
  2013. if (!obj) {
  2014. ret = -EINVAL;
  2015. goto out;
  2016. }
  2017. fb = obj_to_fb(obj);
  2018. r->height = fb->height;
  2019. r->width = fb->width;
  2020. r->depth = fb->depth;
  2021. r->bpp = fb->bits_per_pixel;
  2022. r->pitch = fb->pitches[0];
  2023. fb->funcs->create_handle(fb, file_priv, &r->handle);
  2024. out:
  2025. mutex_unlock(&dev->mode_config.mutex);
  2026. return ret;
  2027. }
  2028. int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
  2029. void *data, struct drm_file *file_priv)
  2030. {
  2031. struct drm_clip_rect __user *clips_ptr;
  2032. struct drm_clip_rect *clips = NULL;
  2033. struct drm_mode_fb_dirty_cmd *r = data;
  2034. struct drm_mode_object *obj;
  2035. struct drm_framebuffer *fb;
  2036. unsigned flags;
  2037. int num_clips;
  2038. int ret = 0;
  2039. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2040. return -EINVAL;
  2041. mutex_lock(&dev->mode_config.mutex);
  2042. obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
  2043. if (!obj) {
  2044. ret = -EINVAL;
  2045. goto out_err1;
  2046. }
  2047. fb = obj_to_fb(obj);
  2048. num_clips = r->num_clips;
  2049. clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
  2050. if (!num_clips != !clips_ptr) {
  2051. ret = -EINVAL;
  2052. goto out_err1;
  2053. }
  2054. flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
  2055. /* If userspace annotates copy, clips must come in pairs */
  2056. if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
  2057. ret = -EINVAL;
  2058. goto out_err1;
  2059. }
  2060. if (num_clips && clips_ptr) {
  2061. if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
  2062. ret = -EINVAL;
  2063. goto out_err1;
  2064. }
  2065. clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
  2066. if (!clips) {
  2067. ret = -ENOMEM;
  2068. goto out_err1;
  2069. }
  2070. ret = copy_from_user(clips, clips_ptr,
  2071. num_clips * sizeof(*clips));
  2072. if (ret) {
  2073. ret = -EFAULT;
  2074. goto out_err2;
  2075. }
  2076. }
  2077. if (fb->funcs->dirty) {
  2078. ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
  2079. clips, num_clips);
  2080. } else {
  2081. ret = -ENOSYS;
  2082. goto out_err2;
  2083. }
  2084. out_err2:
  2085. kfree(clips);
  2086. out_err1:
  2087. mutex_unlock(&dev->mode_config.mutex);
  2088. return ret;
  2089. }
  2090. /**
  2091. * drm_fb_release - remove and free the FBs on this file
  2092. * @filp: file * from the ioctl
  2093. *
  2094. * LOCKING:
  2095. * Takes mode config lock.
  2096. *
  2097. * Destroy all the FBs associated with @filp.
  2098. *
  2099. * Called by the user via ioctl.
  2100. *
  2101. * RETURNS:
  2102. * Zero on success, errno on failure.
  2103. */
  2104. void drm_fb_release(struct drm_file *priv)
  2105. {
  2106. struct drm_device *dev = priv->minor->dev;
  2107. struct drm_framebuffer *fb, *tfb;
  2108. mutex_lock(&dev->mode_config.mutex);
  2109. list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
  2110. list_del(&fb->filp_head);
  2111. fb->funcs->destroy(fb);
  2112. }
  2113. mutex_unlock(&dev->mode_config.mutex);
  2114. }
  2115. /**
  2116. * drm_mode_attachmode - add a mode to the user mode list
  2117. * @dev: DRM device
  2118. * @connector: connector to add the mode to
  2119. * @mode: mode to add
  2120. *
  2121. * Add @mode to @connector's user mode list.
  2122. */
  2123. static void drm_mode_attachmode(struct drm_device *dev,
  2124. struct drm_connector *connector,
  2125. struct drm_display_mode *mode)
  2126. {
  2127. list_add_tail(&mode->head, &connector->user_modes);
  2128. }
  2129. int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
  2130. const struct drm_display_mode *mode)
  2131. {
  2132. struct drm_connector *connector;
  2133. int ret = 0;
  2134. struct drm_display_mode *dup_mode, *next;
  2135. LIST_HEAD(list);
  2136. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  2137. if (!connector->encoder)
  2138. continue;
  2139. if (connector->encoder->crtc == crtc) {
  2140. dup_mode = drm_mode_duplicate(dev, mode);
  2141. if (!dup_mode) {
  2142. ret = -ENOMEM;
  2143. goto out;
  2144. }
  2145. list_add_tail(&dup_mode->head, &list);
  2146. }
  2147. }
  2148. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  2149. if (!connector->encoder)
  2150. continue;
  2151. if (connector->encoder->crtc == crtc)
  2152. list_move_tail(list.next, &connector->user_modes);
  2153. }
  2154. WARN_ON(!list_empty(&list));
  2155. out:
  2156. list_for_each_entry_safe(dup_mode, next, &list, head)
  2157. drm_mode_destroy(dev, dup_mode);
  2158. return ret;
  2159. }
  2160. EXPORT_SYMBOL(drm_mode_attachmode_crtc);
  2161. static int drm_mode_detachmode(struct drm_device *dev,
  2162. struct drm_connector *connector,
  2163. struct drm_display_mode *mode)
  2164. {
  2165. int found = 0;
  2166. int ret = 0;
  2167. struct drm_display_mode *match_mode, *t;
  2168. list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
  2169. if (drm_mode_equal(match_mode, mode)) {
  2170. list_del(&match_mode->head);
  2171. drm_mode_destroy(dev, match_mode);
  2172. found = 1;
  2173. break;
  2174. }
  2175. }
  2176. if (!found)
  2177. ret = -EINVAL;
  2178. return ret;
  2179. }
  2180. int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
  2181. {
  2182. struct drm_connector *connector;
  2183. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  2184. drm_mode_detachmode(dev, connector, mode);
  2185. }
  2186. return 0;
  2187. }
  2188. EXPORT_SYMBOL(drm_mode_detachmode_crtc);
  2189. /**
  2190. * drm_fb_attachmode - Attach a user mode to an connector
  2191. * @inode: inode from the ioctl
  2192. * @filp: file * from the ioctl
  2193. * @cmd: cmd from ioctl
  2194. * @arg: arg from ioctl
  2195. *
  2196. * This attaches a user specified mode to an connector.
  2197. * Called by the user via ioctl.
  2198. *
  2199. * RETURNS:
  2200. * Zero on success, errno on failure.
  2201. */
  2202. int drm_mode_attachmode_ioctl(struct drm_device *dev,
  2203. void *data, struct drm_file *file_priv)
  2204. {
  2205. struct drm_mode_mode_cmd *mode_cmd = data;
  2206. struct drm_connector *connector;
  2207. struct drm_display_mode *mode;
  2208. struct drm_mode_object *obj;
  2209. struct drm_mode_modeinfo *umode = &mode_cmd->mode;
  2210. int ret = 0;
  2211. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2212. return -EINVAL;
  2213. mutex_lock(&dev->mode_config.mutex);
  2214. obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
  2215. if (!obj) {
  2216. ret = -EINVAL;
  2217. goto out;
  2218. }
  2219. connector = obj_to_connector(obj);
  2220. mode = drm_mode_create(dev);
  2221. if (!mode) {
  2222. ret = -ENOMEM;
  2223. goto out;
  2224. }
  2225. ret = drm_crtc_convert_umode(mode, umode);
  2226. if (ret) {
  2227. DRM_DEBUG_KMS("Invalid mode\n");
  2228. drm_mode_destroy(dev, mode);
  2229. goto out;
  2230. }
  2231. drm_mode_attachmode(dev, connector, mode);
  2232. out:
  2233. mutex_unlock(&dev->mode_config.mutex);
  2234. return ret;
  2235. }
  2236. /**
  2237. * drm_fb_detachmode - Detach a user specified mode from an connector
  2238. * @inode: inode from the ioctl
  2239. * @filp: file * from the ioctl
  2240. * @cmd: cmd from ioctl
  2241. * @arg: arg from ioctl
  2242. *
  2243. * Called by the user via ioctl.
  2244. *
  2245. * RETURNS:
  2246. * Zero on success, errno on failure.
  2247. */
  2248. int drm_mode_detachmode_ioctl(struct drm_device *dev,
  2249. void *data, struct drm_file *file_priv)
  2250. {
  2251. struct drm_mode_object *obj;
  2252. struct drm_mode_mode_cmd *mode_cmd = data;
  2253. struct drm_connector *connector;
  2254. struct drm_display_mode mode;
  2255. struct drm_mode_modeinfo *umode = &mode_cmd->mode;
  2256. int ret = 0;
  2257. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2258. return -EINVAL;
  2259. mutex_lock(&dev->mode_config.mutex);
  2260. obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
  2261. if (!obj) {
  2262. ret = -EINVAL;
  2263. goto out;
  2264. }
  2265. connector = obj_to_connector(obj);
  2266. ret = drm_crtc_convert_umode(&mode, umode);
  2267. if (ret) {
  2268. DRM_DEBUG_KMS("Invalid mode\n");
  2269. goto out;
  2270. }
  2271. ret = drm_mode_detachmode(dev, connector, &mode);
  2272. out:
  2273. mutex_unlock(&dev->mode_config.mutex);
  2274. return ret;
  2275. }
  2276. struct drm_property *drm_property_create(struct drm_device *dev, int flags,
  2277. const char *name, int num_values)
  2278. {
  2279. struct drm_property *property = NULL;
  2280. property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
  2281. if (!property)
  2282. return NULL;
  2283. if (num_values) {
  2284. property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
  2285. if (!property->values)
  2286. goto fail;
  2287. }
  2288. drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
  2289. property->flags = flags;
  2290. property->num_values = num_values;
  2291. INIT_LIST_HEAD(&property->enum_blob_list);
  2292. if (name) {
  2293. strncpy(property->name, name, DRM_PROP_NAME_LEN);
  2294. property->name[DRM_PROP_NAME_LEN-1] = '\0';
  2295. }
  2296. list_add_tail(&property->head, &dev->mode_config.property_list);
  2297. return property;
  2298. fail:
  2299. kfree(property);
  2300. return NULL;
  2301. }
  2302. EXPORT_SYMBOL(drm_property_create);
  2303. struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
  2304. const char *name,
  2305. const struct drm_prop_enum_list *props,
  2306. int num_values)
  2307. {
  2308. struct drm_property *property;
  2309. int i, ret;
  2310. flags |= DRM_MODE_PROP_ENUM;
  2311. property = drm_property_create(dev, flags, name, num_values);
  2312. if (!property)
  2313. return NULL;
  2314. for (i = 0; i < num_values; i++) {
  2315. ret = drm_property_add_enum(property, i,
  2316. props[i].type,
  2317. props[i].name);
  2318. if (ret) {
  2319. drm_property_destroy(dev, property);
  2320. return NULL;
  2321. }
  2322. }
  2323. return property;
  2324. }
  2325. EXPORT_SYMBOL(drm_property_create_enum);
  2326. struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
  2327. const char *name,
  2328. uint64_t min, uint64_t max)
  2329. {
  2330. struct drm_property *property;
  2331. flags |= DRM_MODE_PROP_RANGE;
  2332. property = drm_property_create(dev, flags, name, 2);
  2333. if (!property)
  2334. return NULL;
  2335. property->values[0] = min;
  2336. property->values[1] = max;
  2337. return property;
  2338. }
  2339. EXPORT_SYMBOL(drm_property_create_range);
  2340. int drm_property_add_enum(struct drm_property *property, int index,
  2341. uint64_t value, const char *name)
  2342. {
  2343. struct drm_property_enum *prop_enum;
  2344. if (!(property->flags & DRM_MODE_PROP_ENUM))
  2345. return -EINVAL;
  2346. if (!list_empty(&property->enum_blob_list)) {
  2347. list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
  2348. if (prop_enum->value == value) {
  2349. strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
  2350. prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
  2351. return 0;
  2352. }
  2353. }
  2354. }
  2355. prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
  2356. if (!prop_enum)
  2357. return -ENOMEM;
  2358. strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
  2359. prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
  2360. prop_enum->value = value;
  2361. property->values[index] = value;
  2362. list_add_tail(&prop_enum->head, &property->enum_blob_list);
  2363. return 0;
  2364. }
  2365. EXPORT_SYMBOL(drm_property_add_enum);
  2366. void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
  2367. {
  2368. struct drm_property_enum *prop_enum, *pt;
  2369. list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
  2370. list_del(&prop_enum->head);
  2371. kfree(prop_enum);
  2372. }
  2373. if (property->num_values)
  2374. kfree(property->values);
  2375. drm_mode_object_put(dev, &property->base);
  2376. list_del(&property->head);
  2377. kfree(property);
  2378. }
  2379. EXPORT_SYMBOL(drm_property_destroy);
  2380. int drm_connector_attach_property(struct drm_connector *connector,
  2381. struct drm_property *property, uint64_t init_val)
  2382. {
  2383. int i;
  2384. for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
  2385. if (connector->property_ids[i] == 0) {
  2386. connector->property_ids[i] = property->base.id;
  2387. connector->property_values[i] = init_val;
  2388. break;
  2389. }
  2390. }
  2391. if (i == DRM_CONNECTOR_MAX_PROPERTY)
  2392. return -EINVAL;
  2393. return 0;
  2394. }
  2395. EXPORT_SYMBOL(drm_connector_attach_property);
  2396. int drm_connector_property_set_value(struct drm_connector *connector,
  2397. struct drm_property *property, uint64_t value)
  2398. {
  2399. int i;
  2400. for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
  2401. if (connector->property_ids[i] == property->base.id) {
  2402. connector->property_values[i] = value;
  2403. break;
  2404. }
  2405. }
  2406. if (i == DRM_CONNECTOR_MAX_PROPERTY)
  2407. return -EINVAL;
  2408. return 0;
  2409. }
  2410. EXPORT_SYMBOL(drm_connector_property_set_value);
  2411. int drm_connector_property_get_value(struct drm_connector *connector,
  2412. struct drm_property *property, uint64_t *val)
  2413. {
  2414. int i;
  2415. for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
  2416. if (connector->property_ids[i] == property->base.id) {
  2417. *val = connector->property_values[i];
  2418. break;
  2419. }
  2420. }
  2421. if (i == DRM_CONNECTOR_MAX_PROPERTY)
  2422. return -EINVAL;
  2423. return 0;
  2424. }
  2425. EXPORT_SYMBOL(drm_connector_property_get_value);
  2426. int drm_mode_getproperty_ioctl(struct drm_device *dev,
  2427. void *data, struct drm_file *file_priv)
  2428. {
  2429. struct drm_mode_object *obj;
  2430. struct drm_mode_get_property *out_resp = data;
  2431. struct drm_property *property;
  2432. int enum_count = 0;
  2433. int blob_count = 0;
  2434. int value_count = 0;
  2435. int ret = 0, i;
  2436. int copied;
  2437. struct drm_property_enum *prop_enum;
  2438. struct drm_mode_property_enum __user *enum_ptr;
  2439. struct drm_property_blob *prop_blob;
  2440. uint32_t __user *blob_id_ptr;
  2441. uint64_t __user *values_ptr;
  2442. uint32_t __user *blob_length_ptr;
  2443. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2444. return -EINVAL;
  2445. mutex_lock(&dev->mode_config.mutex);
  2446. obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
  2447. if (!obj) {
  2448. ret = -EINVAL;
  2449. goto done;
  2450. }
  2451. property = obj_to_property(obj);
  2452. if (property->flags & DRM_MODE_PROP_ENUM) {
  2453. list_for_each_entry(prop_enum, &property->enum_blob_list, head)
  2454. enum_count++;
  2455. } else if (property->flags & DRM_MODE_PROP_BLOB) {
  2456. list_for_each_entry(prop_blob, &property->enum_blob_list, head)
  2457. blob_count++;
  2458. }
  2459. value_count = property->num_values;
  2460. strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
  2461. out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
  2462. out_resp->flags = property->flags;
  2463. if ((out_resp->count_values >= value_count) && value_count) {
  2464. values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
  2465. for (i = 0; i < value_count; i++) {
  2466. if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
  2467. ret = -EFAULT;
  2468. goto done;
  2469. }
  2470. }
  2471. }
  2472. out_resp->count_values = value_count;
  2473. if (property->flags & DRM_MODE_PROP_ENUM) {
  2474. if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
  2475. copied = 0;
  2476. enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
  2477. list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
  2478. if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
  2479. ret = -EFAULT;
  2480. goto done;
  2481. }
  2482. if (copy_to_user(&enum_ptr[copied].name,
  2483. &prop_enum->name, DRM_PROP_NAME_LEN)) {
  2484. ret = -EFAULT;
  2485. goto done;
  2486. }
  2487. copied++;
  2488. }
  2489. }
  2490. out_resp->count_enum_blobs = enum_count;
  2491. }
  2492. if (property->flags & DRM_MODE_PROP_BLOB) {
  2493. if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
  2494. copied = 0;
  2495. blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
  2496. blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
  2497. list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
  2498. if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
  2499. ret = -EFAULT;
  2500. goto done;
  2501. }
  2502. if (put_user(prop_blob->length, blob_length_ptr + copied)) {
  2503. ret = -EFAULT;
  2504. goto done;
  2505. }
  2506. copied++;
  2507. }
  2508. }
  2509. out_resp->count_enum_blobs = blob_count;
  2510. }
  2511. done:
  2512. mutex_unlock(&dev->mode_config.mutex);
  2513. return ret;
  2514. }
  2515. static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
  2516. void *data)
  2517. {
  2518. struct drm_property_blob *blob;
  2519. if (!length || !data)
  2520. return NULL;
  2521. blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
  2522. if (!blob)
  2523. return NULL;
  2524. blob->data = (void *)((char *)blob + sizeof(struct drm_property_blob));
  2525. blob->length = length;
  2526. memcpy(blob->data, data, length);
  2527. drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
  2528. list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
  2529. return blob;
  2530. }
  2531. static void drm_property_destroy_blob(struct drm_device *dev,
  2532. struct drm_property_blob *blob)
  2533. {
  2534. drm_mode_object_put(dev, &blob->base);
  2535. list_del(&blob->head);
  2536. kfree(blob);
  2537. }
  2538. int drm_mode_getblob_ioctl(struct drm_device *dev,
  2539. void *data, struct drm_file *file_priv)
  2540. {
  2541. struct drm_mode_object *obj;
  2542. struct drm_mode_get_blob *out_resp = data;
  2543. struct drm_property_blob *blob;
  2544. int ret = 0;
  2545. void __user *blob_ptr;
  2546. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2547. return -EINVAL;
  2548. mutex_lock(&dev->mode_config.mutex);
  2549. obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
  2550. if (!obj) {
  2551. ret = -EINVAL;
  2552. goto done;
  2553. }
  2554. blob = obj_to_blob(obj);
  2555. if (out_resp->length == blob->length) {
  2556. blob_ptr = (void __user *)(unsigned long)out_resp->data;
  2557. if (copy_to_user(blob_ptr, blob->data, blob->length)){
  2558. ret = -EFAULT;
  2559. goto done;
  2560. }
  2561. }
  2562. out_resp->length = blob->length;
  2563. done:
  2564. mutex_unlock(&dev->mode_config.mutex);
  2565. return ret;
  2566. }
  2567. int drm_mode_connector_update_edid_property(struct drm_connector *connector,
  2568. struct edid *edid)
  2569. {
  2570. struct drm_device *dev = connector->dev;
  2571. int ret = 0, size;
  2572. if (connector->edid_blob_ptr)
  2573. drm_property_destroy_blob(dev, connector->edid_blob_ptr);
  2574. /* Delete edid, when there is none. */
  2575. if (!edid) {
  2576. connector->edid_blob_ptr = NULL;
  2577. ret = drm_connector_property_set_value(connector, dev->mode_config.edid_property, 0);
  2578. return ret;
  2579. }
  2580. size = EDID_LENGTH * (1 + edid->extensions);
  2581. connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
  2582. size, edid);
  2583. ret = drm_connector_property_set_value(connector,
  2584. dev->mode_config.edid_property,
  2585. connector->edid_blob_ptr->base.id);
  2586. return ret;
  2587. }
  2588. EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
  2589. int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
  2590. void *data, struct drm_file *file_priv)
  2591. {
  2592. struct drm_mode_connector_set_property *out_resp = data;
  2593. struct drm_mode_object *obj;
  2594. struct drm_property *property;
  2595. struct drm_connector *connector;
  2596. int ret = -EINVAL;
  2597. int i;
  2598. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2599. return -EINVAL;
  2600. mutex_lock(&dev->mode_config.mutex);
  2601. obj = drm_mode_object_find(dev, out_resp->connector_id, DRM_MODE_OBJECT_CONNECTOR);
  2602. if (!obj) {
  2603. goto out;
  2604. }
  2605. connector = obj_to_connector(obj);
  2606. for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
  2607. if (connector->property_ids[i] == out_resp->prop_id)
  2608. break;
  2609. }
  2610. if (i == DRM_CONNECTOR_MAX_PROPERTY) {
  2611. goto out;
  2612. }
  2613. obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
  2614. if (!obj) {
  2615. goto out;
  2616. }
  2617. property = obj_to_property(obj);
  2618. if (property->flags & DRM_MODE_PROP_IMMUTABLE)
  2619. goto out;
  2620. if (property->flags & DRM_MODE_PROP_RANGE) {
  2621. if (out_resp->value < property->values[0])
  2622. goto out;
  2623. if (out_resp->value > property->values[1])
  2624. goto out;
  2625. } else {
  2626. int found = 0;
  2627. for (i = 0; i < property->num_values; i++) {
  2628. if (property->values[i] == out_resp->value) {
  2629. found = 1;
  2630. break;
  2631. }
  2632. }
  2633. if (!found) {
  2634. goto out;
  2635. }
  2636. }
  2637. /* Do DPMS ourselves */
  2638. if (property == connector->dev->mode_config.dpms_property) {
  2639. if (connector->funcs->dpms)
  2640. (*connector->funcs->dpms)(connector, (int) out_resp->value);
  2641. ret = 0;
  2642. } else if (connector->funcs->set_property)
  2643. ret = connector->funcs->set_property(connector, property, out_resp->value);
  2644. /* store the property value if successful */
  2645. if (!ret)
  2646. drm_connector_property_set_value(connector, property, out_resp->value);
  2647. out:
  2648. mutex_unlock(&dev->mode_config.mutex);
  2649. return ret;
  2650. }
  2651. int drm_mode_connector_attach_encoder(struct drm_connector *connector,
  2652. struct drm_encoder *encoder)
  2653. {
  2654. int i;
  2655. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  2656. if (connector->encoder_ids[i] == 0) {
  2657. connector->encoder_ids[i] = encoder->base.id;
  2658. return 0;
  2659. }
  2660. }
  2661. return -ENOMEM;
  2662. }
  2663. EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
  2664. void drm_mode_connector_detach_encoder(struct drm_connector *connector,
  2665. struct drm_encoder *encoder)
  2666. {
  2667. int i;
  2668. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  2669. if (connector->encoder_ids[i] == encoder->base.id) {
  2670. connector->encoder_ids[i] = 0;
  2671. if (connector->encoder == encoder)
  2672. connector->encoder = NULL;
  2673. break;
  2674. }
  2675. }
  2676. }
  2677. EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
  2678. int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
  2679. int gamma_size)
  2680. {
  2681. crtc->gamma_size = gamma_size;
  2682. crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
  2683. if (!crtc->gamma_store) {
  2684. crtc->gamma_size = 0;
  2685. return -ENOMEM;
  2686. }
  2687. return 0;
  2688. }
  2689. EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
  2690. int drm_mode_gamma_set_ioctl(struct drm_device *dev,
  2691. void *data, struct drm_file *file_priv)
  2692. {
  2693. struct drm_mode_crtc_lut *crtc_lut = data;
  2694. struct drm_mode_object *obj;
  2695. struct drm_crtc *crtc;
  2696. void *r_base, *g_base, *b_base;
  2697. int size;
  2698. int ret = 0;
  2699. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2700. return -EINVAL;
  2701. mutex_lock(&dev->mode_config.mutex);
  2702. obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
  2703. if (!obj) {
  2704. ret = -EINVAL;
  2705. goto out;
  2706. }
  2707. crtc = obj_to_crtc(obj);
  2708. /* memcpy into gamma store */
  2709. if (crtc_lut->gamma_size != crtc->gamma_size) {
  2710. ret = -EINVAL;
  2711. goto out;
  2712. }
  2713. size = crtc_lut->gamma_size * (sizeof(uint16_t));
  2714. r_base = crtc->gamma_store;
  2715. if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
  2716. ret = -EFAULT;
  2717. goto out;
  2718. }
  2719. g_base = r_base + size;
  2720. if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
  2721. ret = -EFAULT;
  2722. goto out;
  2723. }
  2724. b_base = g_base + size;
  2725. if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
  2726. ret = -EFAULT;
  2727. goto out;
  2728. }
  2729. crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
  2730. out:
  2731. mutex_unlock(&dev->mode_config.mutex);
  2732. return ret;
  2733. }
  2734. int drm_mode_gamma_get_ioctl(struct drm_device *dev,
  2735. void *data, struct drm_file *file_priv)
  2736. {
  2737. struct drm_mode_crtc_lut *crtc_lut = data;
  2738. struct drm_mode_object *obj;
  2739. struct drm_crtc *crtc;
  2740. void *r_base, *g_base, *b_base;
  2741. int size;
  2742. int ret = 0;
  2743. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2744. return -EINVAL;
  2745. mutex_lock(&dev->mode_config.mutex);
  2746. obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
  2747. if (!obj) {
  2748. ret = -EINVAL;
  2749. goto out;
  2750. }
  2751. crtc = obj_to_crtc(obj);
  2752. /* memcpy into gamma store */
  2753. if (crtc_lut->gamma_size != crtc->gamma_size) {
  2754. ret = -EINVAL;
  2755. goto out;
  2756. }
  2757. size = crtc_lut->gamma_size * (sizeof(uint16_t));
  2758. r_base = crtc->gamma_store;
  2759. if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
  2760. ret = -EFAULT;
  2761. goto out;
  2762. }
  2763. g_base = r_base + size;
  2764. if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
  2765. ret = -EFAULT;
  2766. goto out;
  2767. }
  2768. b_base = g_base + size;
  2769. if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
  2770. ret = -EFAULT;
  2771. goto out;
  2772. }
  2773. out:
  2774. mutex_unlock(&dev->mode_config.mutex);
  2775. return ret;
  2776. }
  2777. int drm_mode_page_flip_ioctl(struct drm_device *dev,
  2778. void *data, struct drm_file *file_priv)
  2779. {
  2780. struct drm_mode_crtc_page_flip *page_flip = data;
  2781. struct drm_mode_object *obj;
  2782. struct drm_crtc *crtc;
  2783. struct drm_framebuffer *fb;
  2784. struct drm_pending_vblank_event *e = NULL;
  2785. unsigned long flags;
  2786. int ret = -EINVAL;
  2787. if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
  2788. page_flip->reserved != 0)
  2789. return -EINVAL;
  2790. mutex_lock(&dev->mode_config.mutex);
  2791. obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
  2792. if (!obj)
  2793. goto out;
  2794. crtc = obj_to_crtc(obj);
  2795. if (crtc->fb == NULL) {
  2796. /* The framebuffer is currently unbound, presumably
  2797. * due to a hotplug event, that userspace has not
  2798. * yet discovered.
  2799. */
  2800. ret = -EBUSY;
  2801. goto out;
  2802. }
  2803. if (crtc->funcs->page_flip == NULL)
  2804. goto out;
  2805. obj = drm_mode_object_find(dev, page_flip->fb_id, DRM_MODE_OBJECT_FB);
  2806. if (!obj)
  2807. goto out;
  2808. fb = obj_to_fb(obj);
  2809. if (crtc->mode.hdisplay > fb->width ||
  2810. crtc->mode.vdisplay > fb->height ||
  2811. crtc->x > fb->width - crtc->mode.hdisplay ||
  2812. crtc->y > fb->height - crtc->mode.vdisplay) {
  2813. DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d.\n",
  2814. fb->width, fb->height,
  2815. crtc->mode.hdisplay, crtc->mode.vdisplay,
  2816. crtc->x, crtc->y);
  2817. ret = -ENOSPC;
  2818. goto out;
  2819. }
  2820. if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
  2821. ret = -ENOMEM;
  2822. spin_lock_irqsave(&dev->event_lock, flags);
  2823. if (file_priv->event_space < sizeof e->event) {
  2824. spin_unlock_irqrestore(&dev->event_lock, flags);
  2825. goto out;
  2826. }
  2827. file_priv->event_space -= sizeof e->event;
  2828. spin_unlock_irqrestore(&dev->event_lock, flags);
  2829. e = kzalloc(sizeof *e, GFP_KERNEL);
  2830. if (e == NULL) {
  2831. spin_lock_irqsave(&dev->event_lock, flags);
  2832. file_priv->event_space += sizeof e->event;
  2833. spin_unlock_irqrestore(&dev->event_lock, flags);
  2834. goto out;
  2835. }
  2836. e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
  2837. e->event.base.length = sizeof e->event;
  2838. e->event.user_data = page_flip->user_data;
  2839. e->base.event = &e->event.base;
  2840. e->base.file_priv = file_priv;
  2841. e->base.destroy =
  2842. (void (*) (struct drm_pending_event *)) kfree;
  2843. }
  2844. ret = crtc->funcs->page_flip(crtc, fb, e);
  2845. if (ret) {
  2846. spin_lock_irqsave(&dev->event_lock, flags);
  2847. file_priv->event_space += sizeof e->event;
  2848. spin_unlock_irqrestore(&dev->event_lock, flags);
  2849. kfree(e);
  2850. }
  2851. out:
  2852. mutex_unlock(&dev->mode_config.mutex);
  2853. return ret;
  2854. }
  2855. void drm_mode_config_reset(struct drm_device *dev)
  2856. {
  2857. struct drm_crtc *crtc;
  2858. struct drm_encoder *encoder;
  2859. struct drm_connector *connector;
  2860. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
  2861. if (crtc->funcs->reset)
  2862. crtc->funcs->reset(crtc);
  2863. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  2864. if (encoder->funcs->reset)
  2865. encoder->funcs->reset(encoder);
  2866. list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  2867. if (connector->funcs->reset)
  2868. connector->funcs->reset(connector);
  2869. }
  2870. EXPORT_SYMBOL(drm_mode_config_reset);
  2871. int drm_mode_create_dumb_ioctl(struct drm_device *dev,
  2872. void *data, struct drm_file *file_priv)
  2873. {
  2874. struct drm_mode_create_dumb *args = data;
  2875. if (!dev->driver->dumb_create)
  2876. return -ENOSYS;
  2877. return dev->driver->dumb_create(file_priv, dev, args);
  2878. }
  2879. int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
  2880. void *data, struct drm_file *file_priv)
  2881. {
  2882. struct drm_mode_map_dumb *args = data;
  2883. /* call driver ioctl to get mmap offset */
  2884. if (!dev->driver->dumb_map_offset)
  2885. return -ENOSYS;
  2886. return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
  2887. }
  2888. int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
  2889. void *data, struct drm_file *file_priv)
  2890. {
  2891. struct drm_mode_destroy_dumb *args = data;
  2892. if (!dev->driver->dumb_destroy)
  2893. return -ENOSYS;
  2894. return dev->driver->dumb_destroy(file_priv, dev, args->handle);
  2895. }
  2896. /*
  2897. * Just need to support RGB formats here for compat with code that doesn't
  2898. * use pixel formats directly yet.
  2899. */
  2900. void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
  2901. int *bpp)
  2902. {
  2903. switch (format) {
  2904. case DRM_FORMAT_RGB332:
  2905. case DRM_FORMAT_BGR233:
  2906. *depth = 8;
  2907. *bpp = 8;
  2908. break;
  2909. case DRM_FORMAT_XRGB1555:
  2910. case DRM_FORMAT_XBGR1555:
  2911. case DRM_FORMAT_RGBX5551:
  2912. case DRM_FORMAT_BGRX5551:
  2913. case DRM_FORMAT_ARGB1555:
  2914. case DRM_FORMAT_ABGR1555:
  2915. case DRM_FORMAT_RGBA5551:
  2916. case DRM_FORMAT_BGRA5551:
  2917. *depth = 15;
  2918. *bpp = 16;
  2919. break;
  2920. case DRM_FORMAT_RGB565:
  2921. case DRM_FORMAT_BGR565:
  2922. *depth = 16;
  2923. *bpp = 16;
  2924. break;
  2925. case DRM_FORMAT_RGB888:
  2926. case DRM_FORMAT_BGR888:
  2927. *depth = 24;
  2928. *bpp = 24;
  2929. break;
  2930. case DRM_FORMAT_XRGB8888:
  2931. case DRM_FORMAT_XBGR8888:
  2932. case DRM_FORMAT_RGBX8888:
  2933. case DRM_FORMAT_BGRX8888:
  2934. *depth = 24;
  2935. *bpp = 32;
  2936. break;
  2937. case DRM_FORMAT_XRGB2101010:
  2938. case DRM_FORMAT_XBGR2101010:
  2939. case DRM_FORMAT_RGBX1010102:
  2940. case DRM_FORMAT_BGRX1010102:
  2941. case DRM_FORMAT_ARGB2101010:
  2942. case DRM_FORMAT_ABGR2101010:
  2943. case DRM_FORMAT_RGBA1010102:
  2944. case DRM_FORMAT_BGRA1010102:
  2945. *depth = 30;
  2946. *bpp = 32;
  2947. break;
  2948. case DRM_FORMAT_ARGB8888:
  2949. case DRM_FORMAT_ABGR8888:
  2950. case DRM_FORMAT_RGBA8888:
  2951. case DRM_FORMAT_BGRA8888:
  2952. *depth = 32;
  2953. *bpp = 32;
  2954. break;
  2955. default:
  2956. DRM_DEBUG_KMS("unsupported pixel format\n");
  2957. *depth = 0;
  2958. *bpp = 0;
  2959. break;
  2960. }
  2961. }
  2962. EXPORT_SYMBOL(drm_fb_get_bpp_depth);