drm_crtc.c 84 KB

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