drm_crtc.c 84 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342
  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. drm_mode_object_put(dev, &mode->base);
  571. kfree(mode);
  572. }
  573. EXPORT_SYMBOL(drm_mode_destroy);
  574. static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
  575. {
  576. struct drm_property *edid;
  577. struct drm_property *dpms;
  578. /*
  579. * Standard properties (apply to all connectors)
  580. */
  581. edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
  582. DRM_MODE_PROP_IMMUTABLE,
  583. "EDID", 0);
  584. dev->mode_config.edid_property = edid;
  585. dpms = drm_property_create_enum(dev, 0,
  586. "DPMS", drm_dpms_enum_list,
  587. ARRAY_SIZE(drm_dpms_enum_list));
  588. dev->mode_config.dpms_property = dpms;
  589. return 0;
  590. }
  591. /**
  592. * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
  593. * @dev: DRM device
  594. *
  595. * Called by a driver the first time a DVI-I connector is made.
  596. */
  597. int drm_mode_create_dvi_i_properties(struct drm_device *dev)
  598. {
  599. struct drm_property *dvi_i_selector;
  600. struct drm_property *dvi_i_subconnector;
  601. if (dev->mode_config.dvi_i_select_subconnector_property)
  602. return 0;
  603. dvi_i_selector =
  604. drm_property_create_enum(dev, 0,
  605. "select subconnector",
  606. drm_dvi_i_select_enum_list,
  607. ARRAY_SIZE(drm_dvi_i_select_enum_list));
  608. dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
  609. dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  610. "subconnector",
  611. drm_dvi_i_subconnector_enum_list,
  612. ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
  613. dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
  614. return 0;
  615. }
  616. EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
  617. /**
  618. * drm_create_tv_properties - create TV specific connector properties
  619. * @dev: DRM device
  620. * @num_modes: number of different TV formats (modes) supported
  621. * @modes: array of pointers to strings containing name of each format
  622. *
  623. * Called by a driver's TV initialization routine, this function creates
  624. * the TV specific connector properties for a given device. Caller is
  625. * responsible for allocating a list of format names and passing them to
  626. * this routine.
  627. */
  628. int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
  629. char *modes[])
  630. {
  631. struct drm_property *tv_selector;
  632. struct drm_property *tv_subconnector;
  633. int i;
  634. if (dev->mode_config.tv_select_subconnector_property)
  635. return 0;
  636. /*
  637. * Basic connector properties
  638. */
  639. tv_selector = drm_property_create_enum(dev, 0,
  640. "select subconnector",
  641. drm_tv_select_enum_list,
  642. ARRAY_SIZE(drm_tv_select_enum_list));
  643. dev->mode_config.tv_select_subconnector_property = tv_selector;
  644. tv_subconnector =
  645. drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  646. "subconnector",
  647. drm_tv_subconnector_enum_list,
  648. ARRAY_SIZE(drm_tv_subconnector_enum_list));
  649. dev->mode_config.tv_subconnector_property = tv_subconnector;
  650. /*
  651. * Other, TV specific properties: margins & TV modes.
  652. */
  653. dev->mode_config.tv_left_margin_property =
  654. drm_property_create_range(dev, 0, "left margin", 0, 100);
  655. dev->mode_config.tv_right_margin_property =
  656. drm_property_create_range(dev, 0, "right margin", 0, 100);
  657. dev->mode_config.tv_top_margin_property =
  658. drm_property_create_range(dev, 0, "top margin", 0, 100);
  659. dev->mode_config.tv_bottom_margin_property =
  660. drm_property_create_range(dev, 0, "bottom margin", 0, 100);
  661. dev->mode_config.tv_mode_property =
  662. drm_property_create(dev, DRM_MODE_PROP_ENUM,
  663. "mode", num_modes);
  664. for (i = 0; i < num_modes; i++)
  665. drm_property_add_enum(dev->mode_config.tv_mode_property, i,
  666. i, modes[i]);
  667. dev->mode_config.tv_brightness_property =
  668. drm_property_create_range(dev, 0, "brightness", 0, 100);
  669. dev->mode_config.tv_contrast_property =
  670. drm_property_create_range(dev, 0, "contrast", 0, 100);
  671. dev->mode_config.tv_flicker_reduction_property =
  672. drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
  673. dev->mode_config.tv_overscan_property =
  674. drm_property_create_range(dev, 0, "overscan", 0, 100);
  675. dev->mode_config.tv_saturation_property =
  676. drm_property_create_range(dev, 0, "saturation", 0, 100);
  677. dev->mode_config.tv_hue_property =
  678. drm_property_create_range(dev, 0, "hue", 0, 100);
  679. return 0;
  680. }
  681. EXPORT_SYMBOL(drm_mode_create_tv_properties);
  682. /**
  683. * drm_mode_create_scaling_mode_property - create scaling mode property
  684. * @dev: DRM device
  685. *
  686. * Called by a driver the first time it's needed, must be attached to desired
  687. * connectors.
  688. */
  689. int drm_mode_create_scaling_mode_property(struct drm_device *dev)
  690. {
  691. struct drm_property *scaling_mode;
  692. if (dev->mode_config.scaling_mode_property)
  693. return 0;
  694. scaling_mode =
  695. drm_property_create_enum(dev, 0, "scaling mode",
  696. drm_scaling_mode_enum_list,
  697. ARRAY_SIZE(drm_scaling_mode_enum_list));
  698. dev->mode_config.scaling_mode_property = scaling_mode;
  699. return 0;
  700. }
  701. EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
  702. /**
  703. * drm_mode_create_dithering_property - create dithering property
  704. * @dev: DRM device
  705. *
  706. * Called by a driver the first time it's needed, must be attached to desired
  707. * connectors.
  708. */
  709. int drm_mode_create_dithering_property(struct drm_device *dev)
  710. {
  711. struct drm_property *dithering_mode;
  712. if (dev->mode_config.dithering_mode_property)
  713. return 0;
  714. dithering_mode =
  715. drm_property_create_enum(dev, 0, "dithering",
  716. drm_dithering_mode_enum_list,
  717. ARRAY_SIZE(drm_dithering_mode_enum_list));
  718. dev->mode_config.dithering_mode_property = dithering_mode;
  719. return 0;
  720. }
  721. EXPORT_SYMBOL(drm_mode_create_dithering_property);
  722. /**
  723. * drm_mode_create_dirty_property - create dirty property
  724. * @dev: DRM device
  725. *
  726. * Called by a driver the first time it's needed, must be attached to desired
  727. * connectors.
  728. */
  729. int drm_mode_create_dirty_info_property(struct drm_device *dev)
  730. {
  731. struct drm_property *dirty_info;
  732. if (dev->mode_config.dirty_info_property)
  733. return 0;
  734. dirty_info =
  735. drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  736. "dirty",
  737. drm_dirty_info_enum_list,
  738. ARRAY_SIZE(drm_dirty_info_enum_list));
  739. dev->mode_config.dirty_info_property = dirty_info;
  740. return 0;
  741. }
  742. EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
  743. /**
  744. * drm_mode_config_init - initialize DRM mode_configuration structure
  745. * @dev: DRM device
  746. *
  747. * LOCKING:
  748. * None, should happen single threaded at init time.
  749. *
  750. * Initialize @dev's mode_config structure, used for tracking the graphics
  751. * configuration of @dev.
  752. */
  753. void drm_mode_config_init(struct drm_device *dev)
  754. {
  755. mutex_init(&dev->mode_config.mutex);
  756. mutex_init(&dev->mode_config.idr_mutex);
  757. INIT_LIST_HEAD(&dev->mode_config.fb_list);
  758. INIT_LIST_HEAD(&dev->mode_config.crtc_list);
  759. INIT_LIST_HEAD(&dev->mode_config.connector_list);
  760. INIT_LIST_HEAD(&dev->mode_config.encoder_list);
  761. INIT_LIST_HEAD(&dev->mode_config.property_list);
  762. INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
  763. INIT_LIST_HEAD(&dev->mode_config.plane_list);
  764. idr_init(&dev->mode_config.crtc_idr);
  765. mutex_lock(&dev->mode_config.mutex);
  766. drm_mode_create_standard_connector_properties(dev);
  767. mutex_unlock(&dev->mode_config.mutex);
  768. /* Just to be sure */
  769. dev->mode_config.num_fb = 0;
  770. dev->mode_config.num_connector = 0;
  771. dev->mode_config.num_crtc = 0;
  772. dev->mode_config.num_encoder = 0;
  773. }
  774. EXPORT_SYMBOL(drm_mode_config_init);
  775. int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
  776. {
  777. uint32_t total_objects = 0;
  778. total_objects += dev->mode_config.num_crtc;
  779. total_objects += dev->mode_config.num_connector;
  780. total_objects += dev->mode_config.num_encoder;
  781. group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
  782. if (!group->id_list)
  783. return -ENOMEM;
  784. group->num_crtcs = 0;
  785. group->num_connectors = 0;
  786. group->num_encoders = 0;
  787. return 0;
  788. }
  789. int drm_mode_group_init_legacy_group(struct drm_device *dev,
  790. struct drm_mode_group *group)
  791. {
  792. struct drm_crtc *crtc;
  793. struct drm_encoder *encoder;
  794. struct drm_connector *connector;
  795. int ret;
  796. if ((ret = drm_mode_group_init(dev, group)))
  797. return ret;
  798. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
  799. group->id_list[group->num_crtcs++] = crtc->base.id;
  800. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  801. group->id_list[group->num_crtcs + group->num_encoders++] =
  802. encoder->base.id;
  803. list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  804. group->id_list[group->num_crtcs + group->num_encoders +
  805. group->num_connectors++] = connector->base.id;
  806. return 0;
  807. }
  808. /**
  809. * drm_mode_config_cleanup - free up DRM mode_config info
  810. * @dev: DRM device
  811. *
  812. * LOCKING:
  813. * Caller must hold mode config lock.
  814. *
  815. * Free up all the connectors and CRTCs associated with this DRM device, then
  816. * free up the framebuffers and associated buffer objects.
  817. *
  818. * FIXME: cleanup any dangling user buffer objects too
  819. */
  820. void drm_mode_config_cleanup(struct drm_device *dev)
  821. {
  822. struct drm_connector *connector, *ot;
  823. struct drm_crtc *crtc, *ct;
  824. struct drm_encoder *encoder, *enct;
  825. struct drm_framebuffer *fb, *fbt;
  826. struct drm_property *property, *pt;
  827. struct drm_plane *plane, *plt;
  828. list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
  829. head) {
  830. encoder->funcs->destroy(encoder);
  831. }
  832. list_for_each_entry_safe(connector, ot,
  833. &dev->mode_config.connector_list, head) {
  834. connector->funcs->destroy(connector);
  835. }
  836. list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
  837. head) {
  838. drm_property_destroy(dev, property);
  839. }
  840. list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
  841. fb->funcs->destroy(fb);
  842. }
  843. list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
  844. crtc->funcs->destroy(crtc);
  845. }
  846. list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
  847. head) {
  848. plane->funcs->destroy(plane);
  849. }
  850. idr_remove_all(&dev->mode_config.crtc_idr);
  851. idr_destroy(&dev->mode_config.crtc_idr);
  852. }
  853. EXPORT_SYMBOL(drm_mode_config_cleanup);
  854. /**
  855. * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
  856. * @out: drm_mode_modeinfo struct to return to the user
  857. * @in: drm_display_mode to use
  858. *
  859. * LOCKING:
  860. * None.
  861. *
  862. * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
  863. * the user.
  864. */
  865. void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
  866. struct drm_display_mode *in)
  867. {
  868. WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
  869. in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
  870. in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
  871. in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
  872. in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
  873. "timing values too large for mode info\n");
  874. out->clock = in->clock;
  875. out->hdisplay = in->hdisplay;
  876. out->hsync_start = in->hsync_start;
  877. out->hsync_end = in->hsync_end;
  878. out->htotal = in->htotal;
  879. out->hskew = in->hskew;
  880. out->vdisplay = in->vdisplay;
  881. out->vsync_start = in->vsync_start;
  882. out->vsync_end = in->vsync_end;
  883. out->vtotal = in->vtotal;
  884. out->vscan = in->vscan;
  885. out->vrefresh = in->vrefresh;
  886. out->flags = in->flags;
  887. out->type = in->type;
  888. strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
  889. out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
  890. }
  891. /**
  892. * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
  893. * @out: drm_display_mode to return to the user
  894. * @in: drm_mode_modeinfo to use
  895. *
  896. * LOCKING:
  897. * None.
  898. *
  899. * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
  900. * the caller.
  901. */
  902. void drm_crtc_convert_umode(struct drm_display_mode *out,
  903. struct drm_mode_modeinfo *in)
  904. {
  905. out->clock = in->clock;
  906. out->hdisplay = in->hdisplay;
  907. out->hsync_start = in->hsync_start;
  908. out->hsync_end = in->hsync_end;
  909. out->htotal = in->htotal;
  910. out->hskew = in->hskew;
  911. out->vdisplay = in->vdisplay;
  912. out->vsync_start = in->vsync_start;
  913. out->vsync_end = in->vsync_end;
  914. out->vtotal = in->vtotal;
  915. out->vscan = in->vscan;
  916. out->vrefresh = in->vrefresh;
  917. out->flags = in->flags;
  918. out->type = in->type;
  919. strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
  920. out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
  921. }
  922. /**
  923. * drm_mode_getresources - get graphics configuration
  924. * @inode: inode from the ioctl
  925. * @filp: file * from the ioctl
  926. * @cmd: cmd from ioctl
  927. * @arg: arg from ioctl
  928. *
  929. * LOCKING:
  930. * Takes mode config lock.
  931. *
  932. * Construct a set of configuration description structures and return
  933. * them to the user, including CRTC, connector and framebuffer configuration.
  934. *
  935. * Called by the user via ioctl.
  936. *
  937. * RETURNS:
  938. * Zero on success, errno on failure.
  939. */
  940. int drm_mode_getresources(struct drm_device *dev, void *data,
  941. struct drm_file *file_priv)
  942. {
  943. struct drm_mode_card_res *card_res = data;
  944. struct list_head *lh;
  945. struct drm_framebuffer *fb;
  946. struct drm_connector *connector;
  947. struct drm_crtc *crtc;
  948. struct drm_encoder *encoder;
  949. int ret = 0;
  950. int connector_count = 0;
  951. int crtc_count = 0;
  952. int fb_count = 0;
  953. int encoder_count = 0;
  954. int copied = 0, i;
  955. uint32_t __user *fb_id;
  956. uint32_t __user *crtc_id;
  957. uint32_t __user *connector_id;
  958. uint32_t __user *encoder_id;
  959. struct drm_mode_group *mode_group;
  960. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  961. return -EINVAL;
  962. mutex_lock(&dev->mode_config.mutex);
  963. /*
  964. * For the non-control nodes we need to limit the list of resources
  965. * by IDs in the group list for this node
  966. */
  967. list_for_each(lh, &file_priv->fbs)
  968. fb_count++;
  969. mode_group = &file_priv->master->minor->mode_group;
  970. if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
  971. list_for_each(lh, &dev->mode_config.crtc_list)
  972. crtc_count++;
  973. list_for_each(lh, &dev->mode_config.connector_list)
  974. connector_count++;
  975. list_for_each(lh, &dev->mode_config.encoder_list)
  976. encoder_count++;
  977. } else {
  978. crtc_count = mode_group->num_crtcs;
  979. connector_count = mode_group->num_connectors;
  980. encoder_count = mode_group->num_encoders;
  981. }
  982. card_res->max_height = dev->mode_config.max_height;
  983. card_res->min_height = dev->mode_config.min_height;
  984. card_res->max_width = dev->mode_config.max_width;
  985. card_res->min_width = dev->mode_config.min_width;
  986. /* handle this in 4 parts */
  987. /* FBs */
  988. if (card_res->count_fbs >= fb_count) {
  989. copied = 0;
  990. fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
  991. list_for_each_entry(fb, &file_priv->fbs, filp_head) {
  992. if (put_user(fb->base.id, fb_id + copied)) {
  993. ret = -EFAULT;
  994. goto out;
  995. }
  996. copied++;
  997. }
  998. }
  999. card_res->count_fbs = fb_count;
  1000. /* CRTCs */
  1001. if (card_res->count_crtcs >= crtc_count) {
  1002. copied = 0;
  1003. crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
  1004. if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
  1005. list_for_each_entry(crtc, &dev->mode_config.crtc_list,
  1006. head) {
  1007. DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
  1008. if (put_user(crtc->base.id, crtc_id + copied)) {
  1009. ret = -EFAULT;
  1010. goto out;
  1011. }
  1012. copied++;
  1013. }
  1014. } else {
  1015. for (i = 0; i < mode_group->num_crtcs; i++) {
  1016. if (put_user(mode_group->id_list[i],
  1017. crtc_id + copied)) {
  1018. ret = -EFAULT;
  1019. goto out;
  1020. }
  1021. copied++;
  1022. }
  1023. }
  1024. }
  1025. card_res->count_crtcs = crtc_count;
  1026. /* Encoders */
  1027. if (card_res->count_encoders >= encoder_count) {
  1028. copied = 0;
  1029. encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
  1030. if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
  1031. list_for_each_entry(encoder,
  1032. &dev->mode_config.encoder_list,
  1033. head) {
  1034. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
  1035. drm_get_encoder_name(encoder));
  1036. if (put_user(encoder->base.id, encoder_id +
  1037. copied)) {
  1038. ret = -EFAULT;
  1039. goto out;
  1040. }
  1041. copied++;
  1042. }
  1043. } else {
  1044. for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
  1045. if (put_user(mode_group->id_list[i],
  1046. encoder_id + copied)) {
  1047. ret = -EFAULT;
  1048. goto out;
  1049. }
  1050. copied++;
  1051. }
  1052. }
  1053. }
  1054. card_res->count_encoders = encoder_count;
  1055. /* Connectors */
  1056. if (card_res->count_connectors >= connector_count) {
  1057. copied = 0;
  1058. connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
  1059. if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
  1060. list_for_each_entry(connector,
  1061. &dev->mode_config.connector_list,
  1062. head) {
  1063. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  1064. connector->base.id,
  1065. drm_get_connector_name(connector));
  1066. if (put_user(connector->base.id,
  1067. connector_id + copied)) {
  1068. ret = -EFAULT;
  1069. goto out;
  1070. }
  1071. copied++;
  1072. }
  1073. } else {
  1074. int start = mode_group->num_crtcs +
  1075. mode_group->num_encoders;
  1076. for (i = start; i < start + mode_group->num_connectors; i++) {
  1077. if (put_user(mode_group->id_list[i],
  1078. connector_id + copied)) {
  1079. ret = -EFAULT;
  1080. goto out;
  1081. }
  1082. copied++;
  1083. }
  1084. }
  1085. }
  1086. card_res->count_connectors = connector_count;
  1087. DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
  1088. card_res->count_connectors, card_res->count_encoders);
  1089. out:
  1090. mutex_unlock(&dev->mode_config.mutex);
  1091. return ret;
  1092. }
  1093. /**
  1094. * drm_mode_getcrtc - get CRTC configuration
  1095. * @inode: inode from the ioctl
  1096. * @filp: file * from the ioctl
  1097. * @cmd: cmd from ioctl
  1098. * @arg: arg from ioctl
  1099. *
  1100. * LOCKING:
  1101. * Takes mode config lock.
  1102. *
  1103. * Construct a CRTC configuration structure to return to the user.
  1104. *
  1105. * Called by the user via ioctl.
  1106. *
  1107. * RETURNS:
  1108. * Zero on success, errno on failure.
  1109. */
  1110. int drm_mode_getcrtc(struct drm_device *dev,
  1111. void *data, struct drm_file *file_priv)
  1112. {
  1113. struct drm_mode_crtc *crtc_resp = data;
  1114. struct drm_crtc *crtc;
  1115. struct drm_mode_object *obj;
  1116. int ret = 0;
  1117. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1118. return -EINVAL;
  1119. mutex_lock(&dev->mode_config.mutex);
  1120. obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
  1121. DRM_MODE_OBJECT_CRTC);
  1122. if (!obj) {
  1123. ret = -EINVAL;
  1124. goto out;
  1125. }
  1126. crtc = obj_to_crtc(obj);
  1127. crtc_resp->x = crtc->x;
  1128. crtc_resp->y = crtc->y;
  1129. crtc_resp->gamma_size = crtc->gamma_size;
  1130. if (crtc->fb)
  1131. crtc_resp->fb_id = crtc->fb->base.id;
  1132. else
  1133. crtc_resp->fb_id = 0;
  1134. if (crtc->enabled) {
  1135. drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
  1136. crtc_resp->mode_valid = 1;
  1137. } else {
  1138. crtc_resp->mode_valid = 0;
  1139. }
  1140. out:
  1141. mutex_unlock(&dev->mode_config.mutex);
  1142. return ret;
  1143. }
  1144. /**
  1145. * drm_mode_getconnector - get connector configuration
  1146. * @inode: inode from the ioctl
  1147. * @filp: file * from the ioctl
  1148. * @cmd: cmd from ioctl
  1149. * @arg: arg from ioctl
  1150. *
  1151. * LOCKING:
  1152. * Takes mode config lock.
  1153. *
  1154. * Construct a connector configuration structure to return to the user.
  1155. *
  1156. * Called by the user via ioctl.
  1157. *
  1158. * RETURNS:
  1159. * Zero on success, errno on failure.
  1160. */
  1161. int drm_mode_getconnector(struct drm_device *dev, void *data,
  1162. struct drm_file *file_priv)
  1163. {
  1164. struct drm_mode_get_connector *out_resp = data;
  1165. struct drm_mode_object *obj;
  1166. struct drm_connector *connector;
  1167. struct drm_display_mode *mode;
  1168. int mode_count = 0;
  1169. int props_count = 0;
  1170. int encoders_count = 0;
  1171. int ret = 0;
  1172. int copied = 0;
  1173. int i;
  1174. struct drm_mode_modeinfo u_mode;
  1175. struct drm_mode_modeinfo __user *mode_ptr;
  1176. uint32_t __user *prop_ptr;
  1177. uint64_t __user *prop_values;
  1178. uint32_t __user *encoder_ptr;
  1179. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1180. return -EINVAL;
  1181. memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
  1182. DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
  1183. mutex_lock(&dev->mode_config.mutex);
  1184. obj = drm_mode_object_find(dev, out_resp->connector_id,
  1185. DRM_MODE_OBJECT_CONNECTOR);
  1186. if (!obj) {
  1187. ret = -EINVAL;
  1188. goto out;
  1189. }
  1190. connector = obj_to_connector(obj);
  1191. for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
  1192. if (connector->property_ids[i] != 0) {
  1193. props_count++;
  1194. }
  1195. }
  1196. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  1197. if (connector->encoder_ids[i] != 0) {
  1198. encoders_count++;
  1199. }
  1200. }
  1201. if (out_resp->count_modes == 0) {
  1202. connector->funcs->fill_modes(connector,
  1203. dev->mode_config.max_width,
  1204. dev->mode_config.max_height);
  1205. }
  1206. /* delayed so we get modes regardless of pre-fill_modes state */
  1207. list_for_each_entry(mode, &connector->modes, head)
  1208. mode_count++;
  1209. out_resp->connector_id = connector->base.id;
  1210. out_resp->connector_type = connector->connector_type;
  1211. out_resp->connector_type_id = connector->connector_type_id;
  1212. out_resp->mm_width = connector->display_info.width_mm;
  1213. out_resp->mm_height = connector->display_info.height_mm;
  1214. out_resp->subpixel = connector->display_info.subpixel_order;
  1215. out_resp->connection = connector->status;
  1216. if (connector->encoder)
  1217. out_resp->encoder_id = connector->encoder->base.id;
  1218. else
  1219. out_resp->encoder_id = 0;
  1220. /*
  1221. * This ioctl is called twice, once to determine how much space is
  1222. * needed, and the 2nd time to fill it.
  1223. */
  1224. if ((out_resp->count_modes >= mode_count) && mode_count) {
  1225. copied = 0;
  1226. mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
  1227. list_for_each_entry(mode, &connector->modes, head) {
  1228. drm_crtc_convert_to_umode(&u_mode, mode);
  1229. if (copy_to_user(mode_ptr + copied,
  1230. &u_mode, sizeof(u_mode))) {
  1231. ret = -EFAULT;
  1232. goto out;
  1233. }
  1234. copied++;
  1235. }
  1236. }
  1237. out_resp->count_modes = mode_count;
  1238. if ((out_resp->count_props >= props_count) && props_count) {
  1239. copied = 0;
  1240. prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
  1241. prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
  1242. for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
  1243. if (connector->property_ids[i] != 0) {
  1244. if (put_user(connector->property_ids[i],
  1245. prop_ptr + copied)) {
  1246. ret = -EFAULT;
  1247. goto out;
  1248. }
  1249. if (put_user(connector->property_values[i],
  1250. prop_values + copied)) {
  1251. ret = -EFAULT;
  1252. goto out;
  1253. }
  1254. copied++;
  1255. }
  1256. }
  1257. }
  1258. out_resp->count_props = props_count;
  1259. if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
  1260. copied = 0;
  1261. encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
  1262. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  1263. if (connector->encoder_ids[i] != 0) {
  1264. if (put_user(connector->encoder_ids[i],
  1265. encoder_ptr + copied)) {
  1266. ret = -EFAULT;
  1267. goto out;
  1268. }
  1269. copied++;
  1270. }
  1271. }
  1272. }
  1273. out_resp->count_encoders = encoders_count;
  1274. out:
  1275. mutex_unlock(&dev->mode_config.mutex);
  1276. return ret;
  1277. }
  1278. int drm_mode_getencoder(struct drm_device *dev, void *data,
  1279. struct drm_file *file_priv)
  1280. {
  1281. struct drm_mode_get_encoder *enc_resp = data;
  1282. struct drm_mode_object *obj;
  1283. struct drm_encoder *encoder;
  1284. int ret = 0;
  1285. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1286. return -EINVAL;
  1287. mutex_lock(&dev->mode_config.mutex);
  1288. obj = drm_mode_object_find(dev, enc_resp->encoder_id,
  1289. DRM_MODE_OBJECT_ENCODER);
  1290. if (!obj) {
  1291. ret = -EINVAL;
  1292. goto out;
  1293. }
  1294. encoder = obj_to_encoder(obj);
  1295. if (encoder->crtc)
  1296. enc_resp->crtc_id = encoder->crtc->base.id;
  1297. else
  1298. enc_resp->crtc_id = 0;
  1299. enc_resp->encoder_type = encoder->encoder_type;
  1300. enc_resp->encoder_id = encoder->base.id;
  1301. enc_resp->possible_crtcs = encoder->possible_crtcs;
  1302. enc_resp->possible_clones = encoder->possible_clones;
  1303. out:
  1304. mutex_unlock(&dev->mode_config.mutex);
  1305. return ret;
  1306. }
  1307. /**
  1308. * drm_mode_getplane_res - get plane info
  1309. * @dev: DRM device
  1310. * @data: ioctl data
  1311. * @file_priv: DRM file info
  1312. *
  1313. * LOCKING:
  1314. * Takes mode config lock.
  1315. *
  1316. * Return an plane count and set of IDs.
  1317. */
  1318. int drm_mode_getplane_res(struct drm_device *dev, void *data,
  1319. struct drm_file *file_priv)
  1320. {
  1321. struct drm_mode_get_plane_res *plane_resp = data;
  1322. struct drm_mode_config *config;
  1323. struct drm_plane *plane;
  1324. uint32_t __user *plane_ptr;
  1325. int copied = 0, ret = 0;
  1326. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1327. return -EINVAL;
  1328. mutex_lock(&dev->mode_config.mutex);
  1329. config = &dev->mode_config;
  1330. /*
  1331. * This ioctl is called twice, once to determine how much space is
  1332. * needed, and the 2nd time to fill it.
  1333. */
  1334. if (config->num_plane &&
  1335. (plane_resp->count_planes >= config->num_plane)) {
  1336. plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
  1337. list_for_each_entry(plane, &config->plane_list, head) {
  1338. if (put_user(plane->base.id, plane_ptr + copied)) {
  1339. ret = -EFAULT;
  1340. goto out;
  1341. }
  1342. copied++;
  1343. }
  1344. }
  1345. plane_resp->count_planes = config->num_plane;
  1346. out:
  1347. mutex_unlock(&dev->mode_config.mutex);
  1348. return ret;
  1349. }
  1350. /**
  1351. * drm_mode_getplane - get plane info
  1352. * @dev: DRM device
  1353. * @data: ioctl data
  1354. * @file_priv: DRM file info
  1355. *
  1356. * LOCKING:
  1357. * Takes mode config lock.
  1358. *
  1359. * Return plane info, including formats supported, gamma size, any
  1360. * current fb, etc.
  1361. */
  1362. int drm_mode_getplane(struct drm_device *dev, void *data,
  1363. struct drm_file *file_priv)
  1364. {
  1365. struct drm_mode_get_plane *plane_resp = data;
  1366. struct drm_mode_object *obj;
  1367. struct drm_plane *plane;
  1368. uint32_t __user *format_ptr;
  1369. int ret = 0;
  1370. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1371. return -EINVAL;
  1372. mutex_lock(&dev->mode_config.mutex);
  1373. obj = drm_mode_object_find(dev, plane_resp->plane_id,
  1374. DRM_MODE_OBJECT_PLANE);
  1375. if (!obj) {
  1376. ret = -ENOENT;
  1377. goto out;
  1378. }
  1379. plane = obj_to_plane(obj);
  1380. if (plane->crtc)
  1381. plane_resp->crtc_id = plane->crtc->base.id;
  1382. else
  1383. plane_resp->crtc_id = 0;
  1384. if (plane->fb)
  1385. plane_resp->fb_id = plane->fb->base.id;
  1386. else
  1387. plane_resp->fb_id = 0;
  1388. plane_resp->plane_id = plane->base.id;
  1389. plane_resp->possible_crtcs = plane->possible_crtcs;
  1390. plane_resp->gamma_size = plane->gamma_size;
  1391. /*
  1392. * This ioctl is called twice, once to determine how much space is
  1393. * needed, and the 2nd time to fill it.
  1394. */
  1395. if (plane->format_count &&
  1396. (plane_resp->count_format_types >= plane->format_count)) {
  1397. format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
  1398. if (copy_to_user(format_ptr,
  1399. plane->format_types,
  1400. sizeof(uint32_t) * plane->format_count)) {
  1401. ret = -EFAULT;
  1402. goto out;
  1403. }
  1404. }
  1405. plane_resp->count_format_types = plane->format_count;
  1406. out:
  1407. mutex_unlock(&dev->mode_config.mutex);
  1408. return ret;
  1409. }
  1410. /**
  1411. * drm_mode_setplane - set up or tear down an plane
  1412. * @dev: DRM device
  1413. * @data: ioctl data*
  1414. * @file_prive: DRM file info
  1415. *
  1416. * LOCKING:
  1417. * Takes mode config lock.
  1418. *
  1419. * Set plane info, including placement, fb, scaling, and other factors.
  1420. * Or pass a NULL fb to disable.
  1421. */
  1422. int drm_mode_setplane(struct drm_device *dev, void *data,
  1423. struct drm_file *file_priv)
  1424. {
  1425. struct drm_mode_set_plane *plane_req = data;
  1426. struct drm_mode_object *obj;
  1427. struct drm_plane *plane;
  1428. struct drm_crtc *crtc;
  1429. struct drm_framebuffer *fb;
  1430. int ret = 0;
  1431. unsigned int fb_width, fb_height;
  1432. int i;
  1433. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1434. return -EINVAL;
  1435. mutex_lock(&dev->mode_config.mutex);
  1436. /*
  1437. * First, find the plane, crtc, and fb objects. If not available,
  1438. * we don't bother to call the driver.
  1439. */
  1440. obj = drm_mode_object_find(dev, plane_req->plane_id,
  1441. DRM_MODE_OBJECT_PLANE);
  1442. if (!obj) {
  1443. DRM_DEBUG_KMS("Unknown plane ID %d\n",
  1444. plane_req->plane_id);
  1445. ret = -ENOENT;
  1446. goto out;
  1447. }
  1448. plane = obj_to_plane(obj);
  1449. /* No fb means shut it down */
  1450. if (!plane_req->fb_id) {
  1451. plane->funcs->disable_plane(plane);
  1452. plane->crtc = NULL;
  1453. plane->fb = NULL;
  1454. goto out;
  1455. }
  1456. obj = drm_mode_object_find(dev, plane_req->crtc_id,
  1457. DRM_MODE_OBJECT_CRTC);
  1458. if (!obj) {
  1459. DRM_DEBUG_KMS("Unknown crtc ID %d\n",
  1460. plane_req->crtc_id);
  1461. ret = -ENOENT;
  1462. goto out;
  1463. }
  1464. crtc = obj_to_crtc(obj);
  1465. obj = drm_mode_object_find(dev, plane_req->fb_id,
  1466. DRM_MODE_OBJECT_FB);
  1467. if (!obj) {
  1468. DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
  1469. plane_req->fb_id);
  1470. ret = -ENOENT;
  1471. goto out;
  1472. }
  1473. fb = obj_to_fb(obj);
  1474. /* Check whether this plane supports the fb pixel format. */
  1475. for (i = 0; i < plane->format_count; i++)
  1476. if (fb->pixel_format == plane->format_types[i])
  1477. break;
  1478. if (i == plane->format_count) {
  1479. DRM_DEBUG_KMS("Invalid pixel format 0x%08x\n", fb->pixel_format);
  1480. ret = -EINVAL;
  1481. goto out;
  1482. }
  1483. fb_width = fb->width << 16;
  1484. fb_height = fb->height << 16;
  1485. /* Make sure source coordinates are inside the fb. */
  1486. if (plane_req->src_w > fb_width ||
  1487. plane_req->src_x > fb_width - plane_req->src_w ||
  1488. plane_req->src_h > fb_height ||
  1489. plane_req->src_y > fb_height - plane_req->src_h) {
  1490. DRM_DEBUG_KMS("Invalid source coordinates "
  1491. "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
  1492. plane_req->src_w >> 16,
  1493. ((plane_req->src_w & 0xffff) * 15625) >> 10,
  1494. plane_req->src_h >> 16,
  1495. ((plane_req->src_h & 0xffff) * 15625) >> 10,
  1496. plane_req->src_x >> 16,
  1497. ((plane_req->src_x & 0xffff) * 15625) >> 10,
  1498. plane_req->src_y >> 16,
  1499. ((plane_req->src_y & 0xffff) * 15625) >> 10);
  1500. ret = -ENOSPC;
  1501. goto out;
  1502. }
  1503. /* Give drivers some help against integer overflows */
  1504. if (plane_req->crtc_w > INT_MAX ||
  1505. plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
  1506. plane_req->crtc_h > INT_MAX ||
  1507. plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
  1508. DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
  1509. plane_req->crtc_w, plane_req->crtc_h,
  1510. plane_req->crtc_x, plane_req->crtc_y);
  1511. ret = -ERANGE;
  1512. goto out;
  1513. }
  1514. ret = plane->funcs->update_plane(plane, crtc, fb,
  1515. plane_req->crtc_x, plane_req->crtc_y,
  1516. plane_req->crtc_w, plane_req->crtc_h,
  1517. plane_req->src_x, plane_req->src_y,
  1518. plane_req->src_w, plane_req->src_h);
  1519. if (!ret) {
  1520. plane->crtc = crtc;
  1521. plane->fb = fb;
  1522. }
  1523. out:
  1524. mutex_unlock(&dev->mode_config.mutex);
  1525. return ret;
  1526. }
  1527. /**
  1528. * drm_mode_setcrtc - set CRTC configuration
  1529. * @inode: inode from the ioctl
  1530. * @filp: file * from the ioctl
  1531. * @cmd: cmd from ioctl
  1532. * @arg: arg from ioctl
  1533. *
  1534. * LOCKING:
  1535. * Takes mode config lock.
  1536. *
  1537. * Build a new CRTC configuration based on user request.
  1538. *
  1539. * Called by the user via ioctl.
  1540. *
  1541. * RETURNS:
  1542. * Zero on success, errno on failure.
  1543. */
  1544. int drm_mode_setcrtc(struct drm_device *dev, void *data,
  1545. struct drm_file *file_priv)
  1546. {
  1547. struct drm_mode_config *config = &dev->mode_config;
  1548. struct drm_mode_crtc *crtc_req = data;
  1549. struct drm_mode_object *obj;
  1550. struct drm_crtc *crtc;
  1551. struct drm_connector **connector_set = NULL, *connector;
  1552. struct drm_framebuffer *fb = NULL;
  1553. struct drm_display_mode *mode = NULL;
  1554. struct drm_mode_set set;
  1555. uint32_t __user *set_connectors_ptr;
  1556. int ret = 0;
  1557. int i;
  1558. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1559. return -EINVAL;
  1560. /* For some reason crtc x/y offsets are signed internally. */
  1561. if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
  1562. return -ERANGE;
  1563. mutex_lock(&dev->mode_config.mutex);
  1564. obj = drm_mode_object_find(dev, crtc_req->crtc_id,
  1565. DRM_MODE_OBJECT_CRTC);
  1566. if (!obj) {
  1567. DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
  1568. ret = -EINVAL;
  1569. goto out;
  1570. }
  1571. crtc = obj_to_crtc(obj);
  1572. DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
  1573. if (crtc_req->mode_valid) {
  1574. /* If we have a mode we need a framebuffer. */
  1575. /* If we pass -1, set the mode with the currently bound fb */
  1576. if (crtc_req->fb_id == -1) {
  1577. if (!crtc->fb) {
  1578. DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
  1579. ret = -EINVAL;
  1580. goto out;
  1581. }
  1582. fb = crtc->fb;
  1583. } else {
  1584. obj = drm_mode_object_find(dev, crtc_req->fb_id,
  1585. DRM_MODE_OBJECT_FB);
  1586. if (!obj) {
  1587. DRM_DEBUG_KMS("Unknown FB ID%d\n",
  1588. crtc_req->fb_id);
  1589. ret = -EINVAL;
  1590. goto out;
  1591. }
  1592. fb = obj_to_fb(obj);
  1593. }
  1594. mode = drm_mode_create(dev);
  1595. drm_crtc_convert_umode(mode, &crtc_req->mode);
  1596. drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
  1597. }
  1598. if (crtc_req->count_connectors == 0 && mode) {
  1599. DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
  1600. ret = -EINVAL;
  1601. goto out;
  1602. }
  1603. if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
  1604. DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
  1605. crtc_req->count_connectors);
  1606. ret = -EINVAL;
  1607. goto out;
  1608. }
  1609. if (crtc_req->count_connectors > 0) {
  1610. u32 out_id;
  1611. /* Avoid unbounded kernel memory allocation */
  1612. if (crtc_req->count_connectors > config->num_connector) {
  1613. ret = -EINVAL;
  1614. goto out;
  1615. }
  1616. connector_set = kmalloc(crtc_req->count_connectors *
  1617. sizeof(struct drm_connector *),
  1618. GFP_KERNEL);
  1619. if (!connector_set) {
  1620. ret = -ENOMEM;
  1621. goto out;
  1622. }
  1623. for (i = 0; i < crtc_req->count_connectors; i++) {
  1624. set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
  1625. if (get_user(out_id, &set_connectors_ptr[i])) {
  1626. ret = -EFAULT;
  1627. goto out;
  1628. }
  1629. obj = drm_mode_object_find(dev, out_id,
  1630. DRM_MODE_OBJECT_CONNECTOR);
  1631. if (!obj) {
  1632. DRM_DEBUG_KMS("Connector id %d unknown\n",
  1633. out_id);
  1634. ret = -EINVAL;
  1635. goto out;
  1636. }
  1637. connector = obj_to_connector(obj);
  1638. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  1639. connector->base.id,
  1640. drm_get_connector_name(connector));
  1641. connector_set[i] = connector;
  1642. }
  1643. }
  1644. set.crtc = crtc;
  1645. set.x = crtc_req->x;
  1646. set.y = crtc_req->y;
  1647. set.mode = mode;
  1648. set.connectors = connector_set;
  1649. set.num_connectors = crtc_req->count_connectors;
  1650. set.fb = fb;
  1651. ret = crtc->funcs->set_config(&set);
  1652. out:
  1653. kfree(connector_set);
  1654. mutex_unlock(&dev->mode_config.mutex);
  1655. return ret;
  1656. }
  1657. int drm_mode_cursor_ioctl(struct drm_device *dev,
  1658. void *data, struct drm_file *file_priv)
  1659. {
  1660. struct drm_mode_cursor *req = data;
  1661. struct drm_mode_object *obj;
  1662. struct drm_crtc *crtc;
  1663. int ret = 0;
  1664. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1665. return -EINVAL;
  1666. if (!req->flags)
  1667. return -EINVAL;
  1668. mutex_lock(&dev->mode_config.mutex);
  1669. obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
  1670. if (!obj) {
  1671. DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
  1672. ret = -EINVAL;
  1673. goto out;
  1674. }
  1675. crtc = obj_to_crtc(obj);
  1676. if (req->flags & DRM_MODE_CURSOR_BO) {
  1677. if (!crtc->funcs->cursor_set) {
  1678. ret = -ENXIO;
  1679. goto out;
  1680. }
  1681. /* Turns off the cursor if handle is 0 */
  1682. ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
  1683. req->width, req->height);
  1684. }
  1685. if (req->flags & DRM_MODE_CURSOR_MOVE) {
  1686. if (crtc->funcs->cursor_move) {
  1687. ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
  1688. } else {
  1689. ret = -EFAULT;
  1690. goto out;
  1691. }
  1692. }
  1693. out:
  1694. mutex_unlock(&dev->mode_config.mutex);
  1695. return ret;
  1696. }
  1697. /* Original addfb only supported RGB formats, so figure out which one */
  1698. uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
  1699. {
  1700. uint32_t fmt;
  1701. switch (bpp) {
  1702. case 8:
  1703. fmt = DRM_FORMAT_RGB332;
  1704. break;
  1705. case 16:
  1706. if (depth == 15)
  1707. fmt = DRM_FORMAT_XRGB1555;
  1708. else
  1709. fmt = DRM_FORMAT_RGB565;
  1710. break;
  1711. case 24:
  1712. fmt = DRM_FORMAT_RGB888;
  1713. break;
  1714. case 32:
  1715. if (depth == 24)
  1716. fmt = DRM_FORMAT_XRGB8888;
  1717. else if (depth == 30)
  1718. fmt = DRM_FORMAT_XRGB2101010;
  1719. else
  1720. fmt = DRM_FORMAT_ARGB8888;
  1721. break;
  1722. default:
  1723. DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
  1724. fmt = DRM_FORMAT_XRGB8888;
  1725. break;
  1726. }
  1727. return fmt;
  1728. }
  1729. EXPORT_SYMBOL(drm_mode_legacy_fb_format);
  1730. /**
  1731. * drm_mode_addfb - add an FB to the graphics configuration
  1732. * @inode: inode from the ioctl
  1733. * @filp: file * from the ioctl
  1734. * @cmd: cmd from ioctl
  1735. * @arg: arg from ioctl
  1736. *
  1737. * LOCKING:
  1738. * Takes mode config lock.
  1739. *
  1740. * Add a new FB to the specified CRTC, given a user request.
  1741. *
  1742. * Called by the user via ioctl.
  1743. *
  1744. * RETURNS:
  1745. * Zero on success, errno on failure.
  1746. */
  1747. int drm_mode_addfb(struct drm_device *dev,
  1748. void *data, struct drm_file *file_priv)
  1749. {
  1750. struct drm_mode_fb_cmd *or = data;
  1751. struct drm_mode_fb_cmd2 r = {};
  1752. struct drm_mode_config *config = &dev->mode_config;
  1753. struct drm_framebuffer *fb;
  1754. int ret = 0;
  1755. /* Use new struct with format internally */
  1756. r.fb_id = or->fb_id;
  1757. r.width = or->width;
  1758. r.height = or->height;
  1759. r.pitches[0] = or->pitch;
  1760. r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
  1761. r.handles[0] = or->handle;
  1762. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1763. return -EINVAL;
  1764. if ((config->min_width > r.width) || (r.width > config->max_width))
  1765. return -EINVAL;
  1766. if ((config->min_height > r.height) || (r.height > config->max_height))
  1767. return -EINVAL;
  1768. mutex_lock(&dev->mode_config.mutex);
  1769. /* TODO check buffer is sufficiently large */
  1770. /* TODO setup destructor callback */
  1771. fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
  1772. if (IS_ERR(fb)) {
  1773. DRM_ERROR("could not create framebuffer\n");
  1774. ret = PTR_ERR(fb);
  1775. goto out;
  1776. }
  1777. or->fb_id = fb->base.id;
  1778. list_add(&fb->filp_head, &file_priv->fbs);
  1779. DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
  1780. out:
  1781. mutex_unlock(&dev->mode_config.mutex);
  1782. return ret;
  1783. }
  1784. static int format_check(struct drm_mode_fb_cmd2 *r)
  1785. {
  1786. uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
  1787. switch (format) {
  1788. case DRM_FORMAT_C8:
  1789. case DRM_FORMAT_RGB332:
  1790. case DRM_FORMAT_BGR233:
  1791. case DRM_FORMAT_XRGB4444:
  1792. case DRM_FORMAT_XBGR4444:
  1793. case DRM_FORMAT_RGBX4444:
  1794. case DRM_FORMAT_BGRX4444:
  1795. case DRM_FORMAT_ARGB4444:
  1796. case DRM_FORMAT_ABGR4444:
  1797. case DRM_FORMAT_RGBA4444:
  1798. case DRM_FORMAT_BGRA4444:
  1799. case DRM_FORMAT_XRGB1555:
  1800. case DRM_FORMAT_XBGR1555:
  1801. case DRM_FORMAT_RGBX5551:
  1802. case DRM_FORMAT_BGRX5551:
  1803. case DRM_FORMAT_ARGB1555:
  1804. case DRM_FORMAT_ABGR1555:
  1805. case DRM_FORMAT_RGBA5551:
  1806. case DRM_FORMAT_BGRA5551:
  1807. case DRM_FORMAT_RGB565:
  1808. case DRM_FORMAT_BGR565:
  1809. case DRM_FORMAT_RGB888:
  1810. case DRM_FORMAT_BGR888:
  1811. case DRM_FORMAT_XRGB8888:
  1812. case DRM_FORMAT_XBGR8888:
  1813. case DRM_FORMAT_RGBX8888:
  1814. case DRM_FORMAT_BGRX8888:
  1815. case DRM_FORMAT_ARGB8888:
  1816. case DRM_FORMAT_ABGR8888:
  1817. case DRM_FORMAT_RGBA8888:
  1818. case DRM_FORMAT_BGRA8888:
  1819. case DRM_FORMAT_XRGB2101010:
  1820. case DRM_FORMAT_XBGR2101010:
  1821. case DRM_FORMAT_RGBX1010102:
  1822. case DRM_FORMAT_BGRX1010102:
  1823. case DRM_FORMAT_ARGB2101010:
  1824. case DRM_FORMAT_ABGR2101010:
  1825. case DRM_FORMAT_RGBA1010102:
  1826. case DRM_FORMAT_BGRA1010102:
  1827. case DRM_FORMAT_YUYV:
  1828. case DRM_FORMAT_YVYU:
  1829. case DRM_FORMAT_UYVY:
  1830. case DRM_FORMAT_VYUY:
  1831. case DRM_FORMAT_AYUV:
  1832. case DRM_FORMAT_NV12:
  1833. case DRM_FORMAT_NV21:
  1834. case DRM_FORMAT_NV16:
  1835. case DRM_FORMAT_NV61:
  1836. case DRM_FORMAT_YUV410:
  1837. case DRM_FORMAT_YVU410:
  1838. case DRM_FORMAT_YUV411:
  1839. case DRM_FORMAT_YVU411:
  1840. case DRM_FORMAT_YUV420:
  1841. case DRM_FORMAT_YVU420:
  1842. case DRM_FORMAT_YUV422:
  1843. case DRM_FORMAT_YVU422:
  1844. case DRM_FORMAT_YUV444:
  1845. case DRM_FORMAT_YVU444:
  1846. return 0;
  1847. default:
  1848. return -EINVAL;
  1849. }
  1850. }
  1851. /**
  1852. * drm_mode_addfb2 - add an FB to the graphics configuration
  1853. * @inode: inode from the ioctl
  1854. * @filp: file * from the ioctl
  1855. * @cmd: cmd from ioctl
  1856. * @arg: arg from ioctl
  1857. *
  1858. * LOCKING:
  1859. * Takes mode config lock.
  1860. *
  1861. * Add a new FB to the specified CRTC, given a user request with format.
  1862. *
  1863. * Called by the user via ioctl.
  1864. *
  1865. * RETURNS:
  1866. * Zero on success, errno on failure.
  1867. */
  1868. int drm_mode_addfb2(struct drm_device *dev,
  1869. void *data, struct drm_file *file_priv)
  1870. {
  1871. struct drm_mode_fb_cmd2 *r = data;
  1872. struct drm_mode_config *config = &dev->mode_config;
  1873. struct drm_framebuffer *fb;
  1874. int ret = 0;
  1875. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1876. return -EINVAL;
  1877. if ((config->min_width > r->width) || (r->width > config->max_width)) {
  1878. DRM_ERROR("bad framebuffer width %d, should be >= %d && <= %d\n",
  1879. r->width, config->min_width, config->max_width);
  1880. return -EINVAL;
  1881. }
  1882. if ((config->min_height > r->height) || (r->height > config->max_height)) {
  1883. DRM_ERROR("bad framebuffer height %d, should be >= %d && <= %d\n",
  1884. r->height, config->min_height, config->max_height);
  1885. return -EINVAL;
  1886. }
  1887. ret = format_check(r);
  1888. if (ret) {
  1889. DRM_ERROR("bad framebuffer format 0x%08x\n", r->pixel_format);
  1890. return ret;
  1891. }
  1892. mutex_lock(&dev->mode_config.mutex);
  1893. fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
  1894. if (IS_ERR(fb)) {
  1895. DRM_ERROR("could not create framebuffer\n");
  1896. ret = PTR_ERR(fb);
  1897. goto out;
  1898. }
  1899. r->fb_id = fb->base.id;
  1900. list_add(&fb->filp_head, &file_priv->fbs);
  1901. DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
  1902. out:
  1903. mutex_unlock(&dev->mode_config.mutex);
  1904. return ret;
  1905. }
  1906. /**
  1907. * drm_mode_rmfb - remove an FB from the configuration
  1908. * @inode: inode from the ioctl
  1909. * @filp: file * from the ioctl
  1910. * @cmd: cmd from ioctl
  1911. * @arg: arg from ioctl
  1912. *
  1913. * LOCKING:
  1914. * Takes mode config lock.
  1915. *
  1916. * Remove the FB specified by the user.
  1917. *
  1918. * Called by the user via ioctl.
  1919. *
  1920. * RETURNS:
  1921. * Zero on success, errno on failure.
  1922. */
  1923. int drm_mode_rmfb(struct drm_device *dev,
  1924. void *data, struct drm_file *file_priv)
  1925. {
  1926. struct drm_mode_object *obj;
  1927. struct drm_framebuffer *fb = NULL;
  1928. struct drm_framebuffer *fbl = NULL;
  1929. uint32_t *id = data;
  1930. int ret = 0;
  1931. int found = 0;
  1932. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1933. return -EINVAL;
  1934. mutex_lock(&dev->mode_config.mutex);
  1935. obj = drm_mode_object_find(dev, *id, DRM_MODE_OBJECT_FB);
  1936. /* TODO check that we really get a framebuffer back. */
  1937. if (!obj) {
  1938. ret = -EINVAL;
  1939. goto out;
  1940. }
  1941. fb = obj_to_fb(obj);
  1942. list_for_each_entry(fbl, &file_priv->fbs, filp_head)
  1943. if (fb == fbl)
  1944. found = 1;
  1945. if (!found) {
  1946. ret = -EINVAL;
  1947. goto out;
  1948. }
  1949. /* TODO release all crtc connected to the framebuffer */
  1950. /* TODO unhock the destructor from the buffer object */
  1951. list_del(&fb->filp_head);
  1952. fb->funcs->destroy(fb);
  1953. out:
  1954. mutex_unlock(&dev->mode_config.mutex);
  1955. return ret;
  1956. }
  1957. /**
  1958. * drm_mode_getfb - get FB info
  1959. * @inode: inode from the ioctl
  1960. * @filp: file * from the ioctl
  1961. * @cmd: cmd from ioctl
  1962. * @arg: arg from ioctl
  1963. *
  1964. * LOCKING:
  1965. * Takes mode config lock.
  1966. *
  1967. * Lookup the FB given its ID and return info about it.
  1968. *
  1969. * Called by the user via ioctl.
  1970. *
  1971. * RETURNS:
  1972. * Zero on success, errno on failure.
  1973. */
  1974. int drm_mode_getfb(struct drm_device *dev,
  1975. void *data, struct drm_file *file_priv)
  1976. {
  1977. struct drm_mode_fb_cmd *r = data;
  1978. struct drm_mode_object *obj;
  1979. struct drm_framebuffer *fb;
  1980. int ret = 0;
  1981. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1982. return -EINVAL;
  1983. mutex_lock(&dev->mode_config.mutex);
  1984. obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
  1985. if (!obj) {
  1986. ret = -EINVAL;
  1987. goto out;
  1988. }
  1989. fb = obj_to_fb(obj);
  1990. r->height = fb->height;
  1991. r->width = fb->width;
  1992. r->depth = fb->depth;
  1993. r->bpp = fb->bits_per_pixel;
  1994. r->pitch = fb->pitches[0];
  1995. fb->funcs->create_handle(fb, file_priv, &r->handle);
  1996. out:
  1997. mutex_unlock(&dev->mode_config.mutex);
  1998. return ret;
  1999. }
  2000. int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
  2001. void *data, struct drm_file *file_priv)
  2002. {
  2003. struct drm_clip_rect __user *clips_ptr;
  2004. struct drm_clip_rect *clips = NULL;
  2005. struct drm_mode_fb_dirty_cmd *r = data;
  2006. struct drm_mode_object *obj;
  2007. struct drm_framebuffer *fb;
  2008. unsigned flags;
  2009. int num_clips;
  2010. int ret = 0;
  2011. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2012. return -EINVAL;
  2013. mutex_lock(&dev->mode_config.mutex);
  2014. obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
  2015. if (!obj) {
  2016. ret = -EINVAL;
  2017. goto out_err1;
  2018. }
  2019. fb = obj_to_fb(obj);
  2020. num_clips = r->num_clips;
  2021. clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
  2022. if (!num_clips != !clips_ptr) {
  2023. ret = -EINVAL;
  2024. goto out_err1;
  2025. }
  2026. flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
  2027. /* If userspace annotates copy, clips must come in pairs */
  2028. if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
  2029. ret = -EINVAL;
  2030. goto out_err1;
  2031. }
  2032. if (num_clips && clips_ptr) {
  2033. if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
  2034. ret = -EINVAL;
  2035. goto out_err1;
  2036. }
  2037. clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
  2038. if (!clips) {
  2039. ret = -ENOMEM;
  2040. goto out_err1;
  2041. }
  2042. ret = copy_from_user(clips, clips_ptr,
  2043. num_clips * sizeof(*clips));
  2044. if (ret) {
  2045. ret = -EFAULT;
  2046. goto out_err2;
  2047. }
  2048. }
  2049. if (fb->funcs->dirty) {
  2050. ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
  2051. clips, num_clips);
  2052. } else {
  2053. ret = -ENOSYS;
  2054. goto out_err2;
  2055. }
  2056. out_err2:
  2057. kfree(clips);
  2058. out_err1:
  2059. mutex_unlock(&dev->mode_config.mutex);
  2060. return ret;
  2061. }
  2062. /**
  2063. * drm_fb_release - remove and free the FBs on this file
  2064. * @filp: file * from the ioctl
  2065. *
  2066. * LOCKING:
  2067. * Takes mode config lock.
  2068. *
  2069. * Destroy all the FBs associated with @filp.
  2070. *
  2071. * Called by the user via ioctl.
  2072. *
  2073. * RETURNS:
  2074. * Zero on success, errno on failure.
  2075. */
  2076. void drm_fb_release(struct drm_file *priv)
  2077. {
  2078. struct drm_device *dev = priv->minor->dev;
  2079. struct drm_framebuffer *fb, *tfb;
  2080. mutex_lock(&dev->mode_config.mutex);
  2081. list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
  2082. list_del(&fb->filp_head);
  2083. fb->funcs->destroy(fb);
  2084. }
  2085. mutex_unlock(&dev->mode_config.mutex);
  2086. }
  2087. /**
  2088. * drm_mode_attachmode - add a mode to the user mode list
  2089. * @dev: DRM device
  2090. * @connector: connector to add the mode to
  2091. * @mode: mode to add
  2092. *
  2093. * Add @mode to @connector's user mode list.
  2094. */
  2095. static int drm_mode_attachmode(struct drm_device *dev,
  2096. struct drm_connector *connector,
  2097. struct drm_display_mode *mode)
  2098. {
  2099. int ret = 0;
  2100. list_add_tail(&mode->head, &connector->user_modes);
  2101. return ret;
  2102. }
  2103. int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
  2104. struct drm_display_mode *mode)
  2105. {
  2106. struct drm_connector *connector;
  2107. int ret = 0;
  2108. struct drm_display_mode *dup_mode;
  2109. int need_dup = 0;
  2110. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  2111. if (!connector->encoder)
  2112. break;
  2113. if (connector->encoder->crtc == crtc) {
  2114. if (need_dup)
  2115. dup_mode = drm_mode_duplicate(dev, mode);
  2116. else
  2117. dup_mode = mode;
  2118. ret = drm_mode_attachmode(dev, connector, dup_mode);
  2119. if (ret)
  2120. return ret;
  2121. need_dup = 1;
  2122. }
  2123. }
  2124. return 0;
  2125. }
  2126. EXPORT_SYMBOL(drm_mode_attachmode_crtc);
  2127. static int drm_mode_detachmode(struct drm_device *dev,
  2128. struct drm_connector *connector,
  2129. struct drm_display_mode *mode)
  2130. {
  2131. int found = 0;
  2132. int ret = 0;
  2133. struct drm_display_mode *match_mode, *t;
  2134. list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
  2135. if (drm_mode_equal(match_mode, mode)) {
  2136. list_del(&match_mode->head);
  2137. drm_mode_destroy(dev, match_mode);
  2138. found = 1;
  2139. break;
  2140. }
  2141. }
  2142. if (!found)
  2143. ret = -EINVAL;
  2144. return ret;
  2145. }
  2146. int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
  2147. {
  2148. struct drm_connector *connector;
  2149. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  2150. drm_mode_detachmode(dev, connector, mode);
  2151. }
  2152. return 0;
  2153. }
  2154. EXPORT_SYMBOL(drm_mode_detachmode_crtc);
  2155. /**
  2156. * drm_fb_attachmode - Attach a user mode to an connector
  2157. * @inode: inode from the ioctl
  2158. * @filp: file * from the ioctl
  2159. * @cmd: cmd from ioctl
  2160. * @arg: arg from ioctl
  2161. *
  2162. * This attaches a user specified mode to an connector.
  2163. * Called by the user via ioctl.
  2164. *
  2165. * RETURNS:
  2166. * Zero on success, errno on failure.
  2167. */
  2168. int drm_mode_attachmode_ioctl(struct drm_device *dev,
  2169. void *data, struct drm_file *file_priv)
  2170. {
  2171. struct drm_mode_mode_cmd *mode_cmd = data;
  2172. struct drm_connector *connector;
  2173. struct drm_display_mode *mode;
  2174. struct drm_mode_object *obj;
  2175. struct drm_mode_modeinfo *umode = &mode_cmd->mode;
  2176. int ret = 0;
  2177. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2178. return -EINVAL;
  2179. mutex_lock(&dev->mode_config.mutex);
  2180. obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
  2181. if (!obj) {
  2182. ret = -EINVAL;
  2183. goto out;
  2184. }
  2185. connector = obj_to_connector(obj);
  2186. mode = drm_mode_create(dev);
  2187. if (!mode) {
  2188. ret = -ENOMEM;
  2189. goto out;
  2190. }
  2191. drm_crtc_convert_umode(mode, umode);
  2192. ret = drm_mode_attachmode(dev, connector, mode);
  2193. out:
  2194. mutex_unlock(&dev->mode_config.mutex);
  2195. return ret;
  2196. }
  2197. /**
  2198. * drm_fb_detachmode - Detach a user specified mode from an connector
  2199. * @inode: inode from the ioctl
  2200. * @filp: file * from the ioctl
  2201. * @cmd: cmd from ioctl
  2202. * @arg: arg from ioctl
  2203. *
  2204. * Called by the user via ioctl.
  2205. *
  2206. * RETURNS:
  2207. * Zero on success, errno on failure.
  2208. */
  2209. int drm_mode_detachmode_ioctl(struct drm_device *dev,
  2210. void *data, struct drm_file *file_priv)
  2211. {
  2212. struct drm_mode_object *obj;
  2213. struct drm_mode_mode_cmd *mode_cmd = data;
  2214. struct drm_connector *connector;
  2215. struct drm_display_mode mode;
  2216. struct drm_mode_modeinfo *umode = &mode_cmd->mode;
  2217. int ret = 0;
  2218. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2219. return -EINVAL;
  2220. mutex_lock(&dev->mode_config.mutex);
  2221. obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
  2222. if (!obj) {
  2223. ret = -EINVAL;
  2224. goto out;
  2225. }
  2226. connector = obj_to_connector(obj);
  2227. drm_crtc_convert_umode(&mode, umode);
  2228. ret = drm_mode_detachmode(dev, connector, &mode);
  2229. out:
  2230. mutex_unlock(&dev->mode_config.mutex);
  2231. return ret;
  2232. }
  2233. struct drm_property *drm_property_create(struct drm_device *dev, int flags,
  2234. const char *name, int num_values)
  2235. {
  2236. struct drm_property *property = NULL;
  2237. property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
  2238. if (!property)
  2239. return NULL;
  2240. if (num_values) {
  2241. property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
  2242. if (!property->values)
  2243. goto fail;
  2244. }
  2245. drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
  2246. property->flags = flags;
  2247. property->num_values = num_values;
  2248. INIT_LIST_HEAD(&property->enum_blob_list);
  2249. if (name) {
  2250. strncpy(property->name, name, DRM_PROP_NAME_LEN);
  2251. property->name[DRM_PROP_NAME_LEN-1] = '\0';
  2252. }
  2253. list_add_tail(&property->head, &dev->mode_config.property_list);
  2254. return property;
  2255. fail:
  2256. kfree(property);
  2257. return NULL;
  2258. }
  2259. EXPORT_SYMBOL(drm_property_create);
  2260. struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
  2261. const char *name,
  2262. const struct drm_prop_enum_list *props,
  2263. int num_values)
  2264. {
  2265. struct drm_property *property;
  2266. int i, ret;
  2267. flags |= DRM_MODE_PROP_ENUM;
  2268. property = drm_property_create(dev, flags, name, num_values);
  2269. if (!property)
  2270. return NULL;
  2271. for (i = 0; i < num_values; i++) {
  2272. ret = drm_property_add_enum(property, i,
  2273. props[i].type,
  2274. props[i].name);
  2275. if (ret) {
  2276. drm_property_destroy(dev, property);
  2277. return NULL;
  2278. }
  2279. }
  2280. return property;
  2281. }
  2282. EXPORT_SYMBOL(drm_property_create_enum);
  2283. struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
  2284. const char *name,
  2285. uint64_t min, uint64_t max)
  2286. {
  2287. struct drm_property *property;
  2288. flags |= DRM_MODE_PROP_RANGE;
  2289. property = drm_property_create(dev, flags, name, 2);
  2290. if (!property)
  2291. return NULL;
  2292. property->values[0] = min;
  2293. property->values[1] = max;
  2294. return property;
  2295. }
  2296. EXPORT_SYMBOL(drm_property_create_range);
  2297. int drm_property_add_enum(struct drm_property *property, int index,
  2298. uint64_t value, const char *name)
  2299. {
  2300. struct drm_property_enum *prop_enum;
  2301. if (!(property->flags & DRM_MODE_PROP_ENUM))
  2302. return -EINVAL;
  2303. if (!list_empty(&property->enum_blob_list)) {
  2304. list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
  2305. if (prop_enum->value == value) {
  2306. strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
  2307. prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
  2308. return 0;
  2309. }
  2310. }
  2311. }
  2312. prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
  2313. if (!prop_enum)
  2314. return -ENOMEM;
  2315. strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
  2316. prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
  2317. prop_enum->value = value;
  2318. property->values[index] = value;
  2319. list_add_tail(&prop_enum->head, &property->enum_blob_list);
  2320. return 0;
  2321. }
  2322. EXPORT_SYMBOL(drm_property_add_enum);
  2323. void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
  2324. {
  2325. struct drm_property_enum *prop_enum, *pt;
  2326. list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
  2327. list_del(&prop_enum->head);
  2328. kfree(prop_enum);
  2329. }
  2330. if (property->num_values)
  2331. kfree(property->values);
  2332. drm_mode_object_put(dev, &property->base);
  2333. list_del(&property->head);
  2334. kfree(property);
  2335. }
  2336. EXPORT_SYMBOL(drm_property_destroy);
  2337. int drm_connector_attach_property(struct drm_connector *connector,
  2338. struct drm_property *property, uint64_t init_val)
  2339. {
  2340. int i;
  2341. for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
  2342. if (connector->property_ids[i] == 0) {
  2343. connector->property_ids[i] = property->base.id;
  2344. connector->property_values[i] = init_val;
  2345. break;
  2346. }
  2347. }
  2348. if (i == DRM_CONNECTOR_MAX_PROPERTY)
  2349. return -EINVAL;
  2350. return 0;
  2351. }
  2352. EXPORT_SYMBOL(drm_connector_attach_property);
  2353. int drm_connector_property_set_value(struct drm_connector *connector,
  2354. struct drm_property *property, uint64_t value)
  2355. {
  2356. int i;
  2357. for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
  2358. if (connector->property_ids[i] == property->base.id) {
  2359. connector->property_values[i] = value;
  2360. break;
  2361. }
  2362. }
  2363. if (i == DRM_CONNECTOR_MAX_PROPERTY)
  2364. return -EINVAL;
  2365. return 0;
  2366. }
  2367. EXPORT_SYMBOL(drm_connector_property_set_value);
  2368. int drm_connector_property_get_value(struct drm_connector *connector,
  2369. struct drm_property *property, uint64_t *val)
  2370. {
  2371. int i;
  2372. for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
  2373. if (connector->property_ids[i] == property->base.id) {
  2374. *val = connector->property_values[i];
  2375. break;
  2376. }
  2377. }
  2378. if (i == DRM_CONNECTOR_MAX_PROPERTY)
  2379. return -EINVAL;
  2380. return 0;
  2381. }
  2382. EXPORT_SYMBOL(drm_connector_property_get_value);
  2383. int drm_mode_getproperty_ioctl(struct drm_device *dev,
  2384. void *data, struct drm_file *file_priv)
  2385. {
  2386. struct drm_mode_object *obj;
  2387. struct drm_mode_get_property *out_resp = data;
  2388. struct drm_property *property;
  2389. int enum_count = 0;
  2390. int blob_count = 0;
  2391. int value_count = 0;
  2392. int ret = 0, i;
  2393. int copied;
  2394. struct drm_property_enum *prop_enum;
  2395. struct drm_mode_property_enum __user *enum_ptr;
  2396. struct drm_property_blob *prop_blob;
  2397. uint32_t __user *blob_id_ptr;
  2398. uint64_t __user *values_ptr;
  2399. uint32_t __user *blob_length_ptr;
  2400. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2401. return -EINVAL;
  2402. mutex_lock(&dev->mode_config.mutex);
  2403. obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
  2404. if (!obj) {
  2405. ret = -EINVAL;
  2406. goto done;
  2407. }
  2408. property = obj_to_property(obj);
  2409. if (property->flags & DRM_MODE_PROP_ENUM) {
  2410. list_for_each_entry(prop_enum, &property->enum_blob_list, head)
  2411. enum_count++;
  2412. } else if (property->flags & DRM_MODE_PROP_BLOB) {
  2413. list_for_each_entry(prop_blob, &property->enum_blob_list, head)
  2414. blob_count++;
  2415. }
  2416. value_count = property->num_values;
  2417. strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
  2418. out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
  2419. out_resp->flags = property->flags;
  2420. if ((out_resp->count_values >= value_count) && value_count) {
  2421. values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
  2422. for (i = 0; i < value_count; i++) {
  2423. if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
  2424. ret = -EFAULT;
  2425. goto done;
  2426. }
  2427. }
  2428. }
  2429. out_resp->count_values = value_count;
  2430. if (property->flags & DRM_MODE_PROP_ENUM) {
  2431. if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
  2432. copied = 0;
  2433. enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
  2434. list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
  2435. if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
  2436. ret = -EFAULT;
  2437. goto done;
  2438. }
  2439. if (copy_to_user(&enum_ptr[copied].name,
  2440. &prop_enum->name, DRM_PROP_NAME_LEN)) {
  2441. ret = -EFAULT;
  2442. goto done;
  2443. }
  2444. copied++;
  2445. }
  2446. }
  2447. out_resp->count_enum_blobs = enum_count;
  2448. }
  2449. if (property->flags & DRM_MODE_PROP_BLOB) {
  2450. if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
  2451. copied = 0;
  2452. blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
  2453. blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
  2454. list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
  2455. if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
  2456. ret = -EFAULT;
  2457. goto done;
  2458. }
  2459. if (put_user(prop_blob->length, blob_length_ptr + copied)) {
  2460. ret = -EFAULT;
  2461. goto done;
  2462. }
  2463. copied++;
  2464. }
  2465. }
  2466. out_resp->count_enum_blobs = blob_count;
  2467. }
  2468. done:
  2469. mutex_unlock(&dev->mode_config.mutex);
  2470. return ret;
  2471. }
  2472. static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
  2473. void *data)
  2474. {
  2475. struct drm_property_blob *blob;
  2476. if (!length || !data)
  2477. return NULL;
  2478. blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
  2479. if (!blob)
  2480. return NULL;
  2481. blob->data = (void *)((char *)blob + sizeof(struct drm_property_blob));
  2482. blob->length = length;
  2483. memcpy(blob->data, data, length);
  2484. drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
  2485. list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
  2486. return blob;
  2487. }
  2488. static void drm_property_destroy_blob(struct drm_device *dev,
  2489. struct drm_property_blob *blob)
  2490. {
  2491. drm_mode_object_put(dev, &blob->base);
  2492. list_del(&blob->head);
  2493. kfree(blob);
  2494. }
  2495. int drm_mode_getblob_ioctl(struct drm_device *dev,
  2496. void *data, struct drm_file *file_priv)
  2497. {
  2498. struct drm_mode_object *obj;
  2499. struct drm_mode_get_blob *out_resp = data;
  2500. struct drm_property_blob *blob;
  2501. int ret = 0;
  2502. void __user *blob_ptr;
  2503. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2504. return -EINVAL;
  2505. mutex_lock(&dev->mode_config.mutex);
  2506. obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
  2507. if (!obj) {
  2508. ret = -EINVAL;
  2509. goto done;
  2510. }
  2511. blob = obj_to_blob(obj);
  2512. if (out_resp->length == blob->length) {
  2513. blob_ptr = (void __user *)(unsigned long)out_resp->data;
  2514. if (copy_to_user(blob_ptr, blob->data, blob->length)){
  2515. ret = -EFAULT;
  2516. goto done;
  2517. }
  2518. }
  2519. out_resp->length = blob->length;
  2520. done:
  2521. mutex_unlock(&dev->mode_config.mutex);
  2522. return ret;
  2523. }
  2524. int drm_mode_connector_update_edid_property(struct drm_connector *connector,
  2525. struct edid *edid)
  2526. {
  2527. struct drm_device *dev = connector->dev;
  2528. int ret = 0, size;
  2529. if (connector->edid_blob_ptr)
  2530. drm_property_destroy_blob(dev, connector->edid_blob_ptr);
  2531. /* Delete edid, when there is none. */
  2532. if (!edid) {
  2533. connector->edid_blob_ptr = NULL;
  2534. ret = drm_connector_property_set_value(connector, dev->mode_config.edid_property, 0);
  2535. return ret;
  2536. }
  2537. size = EDID_LENGTH * (1 + edid->extensions);
  2538. connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
  2539. size, edid);
  2540. ret = drm_connector_property_set_value(connector,
  2541. dev->mode_config.edid_property,
  2542. connector->edid_blob_ptr->base.id);
  2543. return ret;
  2544. }
  2545. EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
  2546. int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
  2547. void *data, struct drm_file *file_priv)
  2548. {
  2549. struct drm_mode_connector_set_property *out_resp = data;
  2550. struct drm_mode_object *obj;
  2551. struct drm_property *property;
  2552. struct drm_connector *connector;
  2553. int ret = -EINVAL;
  2554. int i;
  2555. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2556. return -EINVAL;
  2557. mutex_lock(&dev->mode_config.mutex);
  2558. obj = drm_mode_object_find(dev, out_resp->connector_id, DRM_MODE_OBJECT_CONNECTOR);
  2559. if (!obj) {
  2560. goto out;
  2561. }
  2562. connector = obj_to_connector(obj);
  2563. for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
  2564. if (connector->property_ids[i] == out_resp->prop_id)
  2565. break;
  2566. }
  2567. if (i == DRM_CONNECTOR_MAX_PROPERTY) {
  2568. goto out;
  2569. }
  2570. obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
  2571. if (!obj) {
  2572. goto out;
  2573. }
  2574. property = obj_to_property(obj);
  2575. if (property->flags & DRM_MODE_PROP_IMMUTABLE)
  2576. goto out;
  2577. if (property->flags & DRM_MODE_PROP_RANGE) {
  2578. if (out_resp->value < property->values[0])
  2579. goto out;
  2580. if (out_resp->value > property->values[1])
  2581. goto out;
  2582. } else {
  2583. int found = 0;
  2584. for (i = 0; i < property->num_values; i++) {
  2585. if (property->values[i] == out_resp->value) {
  2586. found = 1;
  2587. break;
  2588. }
  2589. }
  2590. if (!found) {
  2591. goto out;
  2592. }
  2593. }
  2594. /* Do DPMS ourselves */
  2595. if (property == connector->dev->mode_config.dpms_property) {
  2596. if (connector->funcs->dpms)
  2597. (*connector->funcs->dpms)(connector, (int) out_resp->value);
  2598. ret = 0;
  2599. } else if (connector->funcs->set_property)
  2600. ret = connector->funcs->set_property(connector, property, out_resp->value);
  2601. /* store the property value if successful */
  2602. if (!ret)
  2603. drm_connector_property_set_value(connector, property, out_resp->value);
  2604. out:
  2605. mutex_unlock(&dev->mode_config.mutex);
  2606. return ret;
  2607. }
  2608. int drm_mode_connector_attach_encoder(struct drm_connector *connector,
  2609. struct drm_encoder *encoder)
  2610. {
  2611. int i;
  2612. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  2613. if (connector->encoder_ids[i] == 0) {
  2614. connector->encoder_ids[i] = encoder->base.id;
  2615. return 0;
  2616. }
  2617. }
  2618. return -ENOMEM;
  2619. }
  2620. EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
  2621. void drm_mode_connector_detach_encoder(struct drm_connector *connector,
  2622. struct drm_encoder *encoder)
  2623. {
  2624. int i;
  2625. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  2626. if (connector->encoder_ids[i] == encoder->base.id) {
  2627. connector->encoder_ids[i] = 0;
  2628. if (connector->encoder == encoder)
  2629. connector->encoder = NULL;
  2630. break;
  2631. }
  2632. }
  2633. }
  2634. EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
  2635. int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
  2636. int gamma_size)
  2637. {
  2638. crtc->gamma_size = gamma_size;
  2639. crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
  2640. if (!crtc->gamma_store) {
  2641. crtc->gamma_size = 0;
  2642. return -ENOMEM;
  2643. }
  2644. return 0;
  2645. }
  2646. EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
  2647. int drm_mode_gamma_set_ioctl(struct drm_device *dev,
  2648. void *data, struct drm_file *file_priv)
  2649. {
  2650. struct drm_mode_crtc_lut *crtc_lut = data;
  2651. struct drm_mode_object *obj;
  2652. struct drm_crtc *crtc;
  2653. void *r_base, *g_base, *b_base;
  2654. int size;
  2655. int ret = 0;
  2656. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2657. return -EINVAL;
  2658. mutex_lock(&dev->mode_config.mutex);
  2659. obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
  2660. if (!obj) {
  2661. ret = -EINVAL;
  2662. goto out;
  2663. }
  2664. crtc = obj_to_crtc(obj);
  2665. /* memcpy into gamma store */
  2666. if (crtc_lut->gamma_size != crtc->gamma_size) {
  2667. ret = -EINVAL;
  2668. goto out;
  2669. }
  2670. size = crtc_lut->gamma_size * (sizeof(uint16_t));
  2671. r_base = crtc->gamma_store;
  2672. if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
  2673. ret = -EFAULT;
  2674. goto out;
  2675. }
  2676. g_base = r_base + size;
  2677. if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
  2678. ret = -EFAULT;
  2679. goto out;
  2680. }
  2681. b_base = g_base + size;
  2682. if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
  2683. ret = -EFAULT;
  2684. goto out;
  2685. }
  2686. crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
  2687. out:
  2688. mutex_unlock(&dev->mode_config.mutex);
  2689. return ret;
  2690. }
  2691. int drm_mode_gamma_get_ioctl(struct drm_device *dev,
  2692. void *data, struct drm_file *file_priv)
  2693. {
  2694. struct drm_mode_crtc_lut *crtc_lut = data;
  2695. struct drm_mode_object *obj;
  2696. struct drm_crtc *crtc;
  2697. void *r_base, *g_base, *b_base;
  2698. int size;
  2699. int ret = 0;
  2700. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2701. return -EINVAL;
  2702. mutex_lock(&dev->mode_config.mutex);
  2703. obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
  2704. if (!obj) {
  2705. ret = -EINVAL;
  2706. goto out;
  2707. }
  2708. crtc = obj_to_crtc(obj);
  2709. /* memcpy into gamma store */
  2710. if (crtc_lut->gamma_size != crtc->gamma_size) {
  2711. ret = -EINVAL;
  2712. goto out;
  2713. }
  2714. size = crtc_lut->gamma_size * (sizeof(uint16_t));
  2715. r_base = crtc->gamma_store;
  2716. if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
  2717. ret = -EFAULT;
  2718. goto out;
  2719. }
  2720. g_base = r_base + size;
  2721. if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
  2722. ret = -EFAULT;
  2723. goto out;
  2724. }
  2725. b_base = g_base + size;
  2726. if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
  2727. ret = -EFAULT;
  2728. goto out;
  2729. }
  2730. out:
  2731. mutex_unlock(&dev->mode_config.mutex);
  2732. return ret;
  2733. }
  2734. int drm_mode_page_flip_ioctl(struct drm_device *dev,
  2735. void *data, struct drm_file *file_priv)
  2736. {
  2737. struct drm_mode_crtc_page_flip *page_flip = data;
  2738. struct drm_mode_object *obj;
  2739. struct drm_crtc *crtc;
  2740. struct drm_framebuffer *fb;
  2741. struct drm_pending_vblank_event *e = NULL;
  2742. unsigned long flags;
  2743. int ret = -EINVAL;
  2744. if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
  2745. page_flip->reserved != 0)
  2746. return -EINVAL;
  2747. mutex_lock(&dev->mode_config.mutex);
  2748. obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
  2749. if (!obj)
  2750. goto out;
  2751. crtc = obj_to_crtc(obj);
  2752. if (crtc->fb == NULL) {
  2753. /* The framebuffer is currently unbound, presumably
  2754. * due to a hotplug event, that userspace has not
  2755. * yet discovered.
  2756. */
  2757. ret = -EBUSY;
  2758. goto out;
  2759. }
  2760. if (crtc->funcs->page_flip == NULL)
  2761. goto out;
  2762. obj = drm_mode_object_find(dev, page_flip->fb_id, DRM_MODE_OBJECT_FB);
  2763. if (!obj)
  2764. goto out;
  2765. fb = obj_to_fb(obj);
  2766. if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
  2767. ret = -ENOMEM;
  2768. spin_lock_irqsave(&dev->event_lock, flags);
  2769. if (file_priv->event_space < sizeof e->event) {
  2770. spin_unlock_irqrestore(&dev->event_lock, flags);
  2771. goto out;
  2772. }
  2773. file_priv->event_space -= sizeof e->event;
  2774. spin_unlock_irqrestore(&dev->event_lock, flags);
  2775. e = kzalloc(sizeof *e, GFP_KERNEL);
  2776. if (e == NULL) {
  2777. spin_lock_irqsave(&dev->event_lock, flags);
  2778. file_priv->event_space += sizeof e->event;
  2779. spin_unlock_irqrestore(&dev->event_lock, flags);
  2780. goto out;
  2781. }
  2782. e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
  2783. e->event.base.length = sizeof e->event;
  2784. e->event.user_data = page_flip->user_data;
  2785. e->base.event = &e->event.base;
  2786. e->base.file_priv = file_priv;
  2787. e->base.destroy =
  2788. (void (*) (struct drm_pending_event *)) kfree;
  2789. }
  2790. ret = crtc->funcs->page_flip(crtc, fb, e);
  2791. if (ret) {
  2792. spin_lock_irqsave(&dev->event_lock, flags);
  2793. file_priv->event_space += sizeof e->event;
  2794. spin_unlock_irqrestore(&dev->event_lock, flags);
  2795. kfree(e);
  2796. }
  2797. out:
  2798. mutex_unlock(&dev->mode_config.mutex);
  2799. return ret;
  2800. }
  2801. void drm_mode_config_reset(struct drm_device *dev)
  2802. {
  2803. struct drm_crtc *crtc;
  2804. struct drm_encoder *encoder;
  2805. struct drm_connector *connector;
  2806. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
  2807. if (crtc->funcs->reset)
  2808. crtc->funcs->reset(crtc);
  2809. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  2810. if (encoder->funcs->reset)
  2811. encoder->funcs->reset(encoder);
  2812. list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  2813. if (connector->funcs->reset)
  2814. connector->funcs->reset(connector);
  2815. }
  2816. EXPORT_SYMBOL(drm_mode_config_reset);
  2817. int drm_mode_create_dumb_ioctl(struct drm_device *dev,
  2818. void *data, struct drm_file *file_priv)
  2819. {
  2820. struct drm_mode_create_dumb *args = data;
  2821. if (!dev->driver->dumb_create)
  2822. return -ENOSYS;
  2823. return dev->driver->dumb_create(file_priv, dev, args);
  2824. }
  2825. int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
  2826. void *data, struct drm_file *file_priv)
  2827. {
  2828. struct drm_mode_map_dumb *args = data;
  2829. /* call driver ioctl to get mmap offset */
  2830. if (!dev->driver->dumb_map_offset)
  2831. return -ENOSYS;
  2832. return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
  2833. }
  2834. int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
  2835. void *data, struct drm_file *file_priv)
  2836. {
  2837. struct drm_mode_destroy_dumb *args = data;
  2838. if (!dev->driver->dumb_destroy)
  2839. return -ENOSYS;
  2840. return dev->driver->dumb_destroy(file_priv, dev, args->handle);
  2841. }
  2842. /*
  2843. * Just need to support RGB formats here for compat with code that doesn't
  2844. * use pixel formats directly yet.
  2845. */
  2846. void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
  2847. int *bpp)
  2848. {
  2849. switch (format) {
  2850. case DRM_FORMAT_RGB332:
  2851. case DRM_FORMAT_BGR233:
  2852. *depth = 8;
  2853. *bpp = 8;
  2854. break;
  2855. case DRM_FORMAT_XRGB1555:
  2856. case DRM_FORMAT_XBGR1555:
  2857. case DRM_FORMAT_RGBX5551:
  2858. case DRM_FORMAT_BGRX5551:
  2859. case DRM_FORMAT_ARGB1555:
  2860. case DRM_FORMAT_ABGR1555:
  2861. case DRM_FORMAT_RGBA5551:
  2862. case DRM_FORMAT_BGRA5551:
  2863. *depth = 15;
  2864. *bpp = 16;
  2865. break;
  2866. case DRM_FORMAT_RGB565:
  2867. case DRM_FORMAT_BGR565:
  2868. *depth = 16;
  2869. *bpp = 16;
  2870. break;
  2871. case DRM_FORMAT_RGB888:
  2872. case DRM_FORMAT_BGR888:
  2873. *depth = 24;
  2874. *bpp = 24;
  2875. break;
  2876. case DRM_FORMAT_XRGB8888:
  2877. case DRM_FORMAT_XBGR8888:
  2878. case DRM_FORMAT_RGBX8888:
  2879. case DRM_FORMAT_BGRX8888:
  2880. *depth = 24;
  2881. *bpp = 32;
  2882. break;
  2883. case DRM_FORMAT_XRGB2101010:
  2884. case DRM_FORMAT_XBGR2101010:
  2885. case DRM_FORMAT_RGBX1010102:
  2886. case DRM_FORMAT_BGRX1010102:
  2887. case DRM_FORMAT_ARGB2101010:
  2888. case DRM_FORMAT_ABGR2101010:
  2889. case DRM_FORMAT_RGBA1010102:
  2890. case DRM_FORMAT_BGRA1010102:
  2891. *depth = 30;
  2892. *bpp = 32;
  2893. break;
  2894. case DRM_FORMAT_ARGB8888:
  2895. case DRM_FORMAT_ABGR8888:
  2896. case DRM_FORMAT_RGBA8888:
  2897. case DRM_FORMAT_BGRA8888:
  2898. *depth = 32;
  2899. *bpp = 32;
  2900. break;
  2901. default:
  2902. DRM_DEBUG_KMS("unsupported pixel format\n");
  2903. *depth = 0;
  2904. *bpp = 0;
  2905. break;
  2906. }
  2907. }
  2908. EXPORT_SYMBOL(drm_fb_get_bpp_depth);