drm_crtc.c 84 KB

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