drm_crtc.c 84 KB

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