drm_crtc.c 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786
  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. struct drm_prop_enum_list {
  40. int type;
  41. char *name;
  42. };
  43. /* Avoid boilerplate. I'm tired of typing. */
  44. #define DRM_ENUM_NAME_FN(fnname, list) \
  45. char *fnname(int val) \
  46. { \
  47. int i; \
  48. for (i = 0; i < ARRAY_SIZE(list); i++) { \
  49. if (list[i].type == val) \
  50. return list[i].name; \
  51. } \
  52. return "(unknown)"; \
  53. }
  54. /*
  55. * Global properties
  56. */
  57. static struct drm_prop_enum_list drm_dpms_enum_list[] =
  58. { { DRM_MODE_DPMS_ON, "On" },
  59. { DRM_MODE_DPMS_STANDBY, "Standby" },
  60. { DRM_MODE_DPMS_SUSPEND, "Suspend" },
  61. { DRM_MODE_DPMS_OFF, "Off" }
  62. };
  63. DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
  64. /*
  65. * Optional properties
  66. */
  67. static struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
  68. {
  69. { DRM_MODE_SCALE_NONE, "None" },
  70. { DRM_MODE_SCALE_FULLSCREEN, "Full" },
  71. { DRM_MODE_SCALE_CENTER, "Center" },
  72. { DRM_MODE_SCALE_ASPECT, "Full aspect" },
  73. };
  74. static struct drm_prop_enum_list drm_dithering_mode_enum_list[] =
  75. {
  76. { DRM_MODE_DITHERING_OFF, "Off" },
  77. { DRM_MODE_DITHERING_ON, "On" },
  78. { DRM_MODE_DITHERING_AUTO, "Automatic" },
  79. };
  80. /*
  81. * Non-global properties, but "required" for certain connectors.
  82. */
  83. static struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
  84. {
  85. { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
  86. { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
  87. { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
  88. };
  89. DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
  90. static struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
  91. {
  92. { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
  93. { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
  94. { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
  95. };
  96. DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
  97. drm_dvi_i_subconnector_enum_list)
  98. static struct drm_prop_enum_list drm_tv_select_enum_list[] =
  99. {
  100. { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
  101. { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
  102. { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
  103. { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
  104. { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
  105. };
  106. DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
  107. static struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
  108. {
  109. { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
  110. { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
  111. { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
  112. { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
  113. { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
  114. };
  115. DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
  116. drm_tv_subconnector_enum_list)
  117. static struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
  118. { DRM_MODE_DIRTY_OFF, "Off" },
  119. { DRM_MODE_DIRTY_ON, "On" },
  120. { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
  121. };
  122. DRM_ENUM_NAME_FN(drm_get_dirty_info_name,
  123. drm_dirty_info_enum_list)
  124. struct drm_conn_prop_enum_list {
  125. int type;
  126. char *name;
  127. int count;
  128. };
  129. /*
  130. * Connector and encoder types.
  131. */
  132. static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
  133. { { DRM_MODE_CONNECTOR_Unknown, "Unknown", 0 },
  134. { DRM_MODE_CONNECTOR_VGA, "VGA", 0 },
  135. { DRM_MODE_CONNECTOR_DVII, "DVI-I", 0 },
  136. { DRM_MODE_CONNECTOR_DVID, "DVI-D", 0 },
  137. { DRM_MODE_CONNECTOR_DVIA, "DVI-A", 0 },
  138. { DRM_MODE_CONNECTOR_Composite, "Composite", 0 },
  139. { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO", 0 },
  140. { DRM_MODE_CONNECTOR_LVDS, "LVDS", 0 },
  141. { DRM_MODE_CONNECTOR_Component, "Component", 0 },
  142. { DRM_MODE_CONNECTOR_9PinDIN, "DIN", 0 },
  143. { DRM_MODE_CONNECTOR_DisplayPort, "DP", 0 },
  144. { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A", 0 },
  145. { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B", 0 },
  146. { DRM_MODE_CONNECTOR_TV, "TV", 0 },
  147. { DRM_MODE_CONNECTOR_eDP, "eDP", 0 },
  148. { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual", 0},
  149. };
  150. static struct drm_prop_enum_list drm_encoder_enum_list[] =
  151. { { DRM_MODE_ENCODER_NONE, "None" },
  152. { DRM_MODE_ENCODER_DAC, "DAC" },
  153. { DRM_MODE_ENCODER_TMDS, "TMDS" },
  154. { DRM_MODE_ENCODER_LVDS, "LVDS" },
  155. { DRM_MODE_ENCODER_TVDAC, "TV" },
  156. { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
  157. };
  158. char *drm_get_encoder_name(struct drm_encoder *encoder)
  159. {
  160. static char buf[32];
  161. snprintf(buf, 32, "%s-%d",
  162. drm_encoder_enum_list[encoder->encoder_type].name,
  163. encoder->base.id);
  164. return buf;
  165. }
  166. EXPORT_SYMBOL(drm_get_encoder_name);
  167. char *drm_get_connector_name(struct drm_connector *connector)
  168. {
  169. static char buf[32];
  170. snprintf(buf, 32, "%s-%d",
  171. drm_connector_enum_list[connector->connector_type].name,
  172. connector->connector_type_id);
  173. return buf;
  174. }
  175. EXPORT_SYMBOL(drm_get_connector_name);
  176. char *drm_get_connector_status_name(enum drm_connector_status status)
  177. {
  178. if (status == connector_status_connected)
  179. return "connected";
  180. else if (status == connector_status_disconnected)
  181. return "disconnected";
  182. else
  183. return "unknown";
  184. }
  185. /**
  186. * drm_mode_object_get - allocate a new identifier
  187. * @dev: DRM device
  188. * @ptr: object pointer, used to generate unique ID
  189. * @type: object type
  190. *
  191. * LOCKING:
  192. *
  193. * Create a unique identifier based on @ptr in @dev's identifier space. Used
  194. * for tracking modes, CRTCs and connectors.
  195. *
  196. * RETURNS:
  197. * New unique (relative to other objects in @dev) integer identifier for the
  198. * object.
  199. */
  200. static int drm_mode_object_get(struct drm_device *dev,
  201. struct drm_mode_object *obj, uint32_t obj_type)
  202. {
  203. int new_id = 0;
  204. int ret;
  205. again:
  206. if (idr_pre_get(&dev->mode_config.crtc_idr, GFP_KERNEL) == 0) {
  207. DRM_ERROR("Ran out memory getting a mode number\n");
  208. return -EINVAL;
  209. }
  210. mutex_lock(&dev->mode_config.idr_mutex);
  211. ret = idr_get_new_above(&dev->mode_config.crtc_idr, obj, 1, &new_id);
  212. mutex_unlock(&dev->mode_config.idr_mutex);
  213. if (ret == -EAGAIN)
  214. goto again;
  215. obj->id = new_id;
  216. obj->type = obj_type;
  217. return 0;
  218. }
  219. /**
  220. * drm_mode_object_put - free an identifer
  221. * @dev: DRM device
  222. * @id: ID to free
  223. *
  224. * LOCKING:
  225. * Caller must hold DRM mode_config lock.
  226. *
  227. * Free @id from @dev's unique identifier pool.
  228. */
  229. static void drm_mode_object_put(struct drm_device *dev,
  230. struct drm_mode_object *object)
  231. {
  232. mutex_lock(&dev->mode_config.idr_mutex);
  233. idr_remove(&dev->mode_config.crtc_idr, object->id);
  234. mutex_unlock(&dev->mode_config.idr_mutex);
  235. }
  236. struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
  237. uint32_t id, uint32_t type)
  238. {
  239. struct drm_mode_object *obj = NULL;
  240. mutex_lock(&dev->mode_config.idr_mutex);
  241. obj = idr_find(&dev->mode_config.crtc_idr, id);
  242. if (!obj || (obj->type != type) || (obj->id != id))
  243. obj = NULL;
  244. mutex_unlock(&dev->mode_config.idr_mutex);
  245. return obj;
  246. }
  247. EXPORT_SYMBOL(drm_mode_object_find);
  248. /**
  249. * drm_framebuffer_init - initialize a framebuffer
  250. * @dev: DRM device
  251. *
  252. * LOCKING:
  253. * Caller must hold mode config lock.
  254. *
  255. * Allocates an ID for the framebuffer's parent mode object, sets its mode
  256. * functions & device file and adds it to the master fd list.
  257. *
  258. * RETURNS:
  259. * Zero on success, error code on failure.
  260. */
  261. int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
  262. const struct drm_framebuffer_funcs *funcs)
  263. {
  264. int ret;
  265. ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
  266. if (ret) {
  267. return ret;
  268. }
  269. fb->dev = dev;
  270. fb->funcs = funcs;
  271. dev->mode_config.num_fb++;
  272. list_add(&fb->head, &dev->mode_config.fb_list);
  273. return 0;
  274. }
  275. EXPORT_SYMBOL(drm_framebuffer_init);
  276. /**
  277. * drm_framebuffer_cleanup - remove a framebuffer object
  278. * @fb: framebuffer to remove
  279. *
  280. * LOCKING:
  281. * Caller must hold mode config lock.
  282. *
  283. * Scans all the CRTCs in @dev's mode_config. If they're using @fb, removes
  284. * it, setting it to NULL.
  285. */
  286. void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
  287. {
  288. struct drm_device *dev = fb->dev;
  289. struct drm_crtc *crtc;
  290. struct drm_mode_set set;
  291. int ret;
  292. /* remove from any CRTC */
  293. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  294. if (crtc->fb == fb) {
  295. /* should turn off the crtc */
  296. memset(&set, 0, sizeof(struct drm_mode_set));
  297. set.crtc = crtc;
  298. set.fb = NULL;
  299. ret = crtc->funcs->set_config(&set);
  300. if (ret)
  301. DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
  302. }
  303. }
  304. drm_mode_object_put(dev, &fb->base);
  305. list_del(&fb->head);
  306. dev->mode_config.num_fb--;
  307. }
  308. EXPORT_SYMBOL(drm_framebuffer_cleanup);
  309. /**
  310. * drm_crtc_init - Initialise a new CRTC object
  311. * @dev: DRM device
  312. * @crtc: CRTC object to init
  313. * @funcs: callbacks for the new CRTC
  314. *
  315. * LOCKING:
  316. * Caller must hold mode config lock.
  317. *
  318. * Inits a new object created as base part of an driver crtc object.
  319. */
  320. void drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
  321. const struct drm_crtc_funcs *funcs)
  322. {
  323. crtc->dev = dev;
  324. crtc->funcs = funcs;
  325. mutex_lock(&dev->mode_config.mutex);
  326. drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
  327. list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
  328. dev->mode_config.num_crtc++;
  329. mutex_unlock(&dev->mode_config.mutex);
  330. }
  331. EXPORT_SYMBOL(drm_crtc_init);
  332. /**
  333. * drm_crtc_cleanup - Cleans up the core crtc usage.
  334. * @crtc: CRTC to cleanup
  335. *
  336. * LOCKING:
  337. * Caller must hold mode config lock.
  338. *
  339. * Cleanup @crtc. Removes from drm modesetting space
  340. * does NOT free object, caller does that.
  341. */
  342. void drm_crtc_cleanup(struct drm_crtc *crtc)
  343. {
  344. struct drm_device *dev = crtc->dev;
  345. if (crtc->gamma_store) {
  346. kfree(crtc->gamma_store);
  347. crtc->gamma_store = NULL;
  348. }
  349. drm_mode_object_put(dev, &crtc->base);
  350. list_del(&crtc->head);
  351. dev->mode_config.num_crtc--;
  352. }
  353. EXPORT_SYMBOL(drm_crtc_cleanup);
  354. /**
  355. * drm_mode_probed_add - add a mode to a connector's probed mode list
  356. * @connector: connector the new mode
  357. * @mode: mode data
  358. *
  359. * LOCKING:
  360. * Caller must hold mode config lock.
  361. *
  362. * Add @mode to @connector's mode list for later use.
  363. */
  364. void drm_mode_probed_add(struct drm_connector *connector,
  365. struct drm_display_mode *mode)
  366. {
  367. list_add(&mode->head, &connector->probed_modes);
  368. }
  369. EXPORT_SYMBOL(drm_mode_probed_add);
  370. /**
  371. * drm_mode_remove - remove and free a mode
  372. * @connector: connector list to modify
  373. * @mode: mode to remove
  374. *
  375. * LOCKING:
  376. * Caller must hold mode config lock.
  377. *
  378. * Remove @mode from @connector's mode list, then free it.
  379. */
  380. void drm_mode_remove(struct drm_connector *connector,
  381. struct drm_display_mode *mode)
  382. {
  383. list_del(&mode->head);
  384. kfree(mode);
  385. }
  386. EXPORT_SYMBOL(drm_mode_remove);
  387. /**
  388. * drm_connector_init - Init a preallocated connector
  389. * @dev: DRM device
  390. * @connector: the connector to init
  391. * @funcs: callbacks for this connector
  392. * @name: user visible name of the connector
  393. *
  394. * LOCKING:
  395. * Caller must hold @dev's mode_config lock.
  396. *
  397. * Initialises a preallocated connector. Connectors should be
  398. * subclassed as part of driver connector objects.
  399. */
  400. void drm_connector_init(struct drm_device *dev,
  401. struct drm_connector *connector,
  402. const struct drm_connector_funcs *funcs,
  403. int connector_type)
  404. {
  405. mutex_lock(&dev->mode_config.mutex);
  406. connector->dev = dev;
  407. connector->funcs = funcs;
  408. drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
  409. connector->connector_type = connector_type;
  410. connector->connector_type_id =
  411. ++drm_connector_enum_list[connector_type].count; /* TODO */
  412. INIT_LIST_HEAD(&connector->user_modes);
  413. INIT_LIST_HEAD(&connector->probed_modes);
  414. INIT_LIST_HEAD(&connector->modes);
  415. connector->edid_blob_ptr = NULL;
  416. list_add_tail(&connector->head, &dev->mode_config.connector_list);
  417. dev->mode_config.num_connector++;
  418. if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
  419. drm_connector_attach_property(connector,
  420. dev->mode_config.edid_property,
  421. 0);
  422. drm_connector_attach_property(connector,
  423. dev->mode_config.dpms_property, 0);
  424. mutex_unlock(&dev->mode_config.mutex);
  425. }
  426. EXPORT_SYMBOL(drm_connector_init);
  427. /**
  428. * drm_connector_cleanup - cleans up an initialised connector
  429. * @connector: connector to cleanup
  430. *
  431. * LOCKING:
  432. * Caller must hold @dev's mode_config lock.
  433. *
  434. * Cleans up the connector but doesn't free the object.
  435. */
  436. void drm_connector_cleanup(struct drm_connector *connector)
  437. {
  438. struct drm_device *dev = connector->dev;
  439. struct drm_display_mode *mode, *t;
  440. list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
  441. drm_mode_remove(connector, mode);
  442. list_for_each_entry_safe(mode, t, &connector->modes, head)
  443. drm_mode_remove(connector, mode);
  444. list_for_each_entry_safe(mode, t, &connector->user_modes, head)
  445. drm_mode_remove(connector, mode);
  446. mutex_lock(&dev->mode_config.mutex);
  447. drm_mode_object_put(dev, &connector->base);
  448. list_del(&connector->head);
  449. dev->mode_config.num_connector--;
  450. mutex_unlock(&dev->mode_config.mutex);
  451. }
  452. EXPORT_SYMBOL(drm_connector_cleanup);
  453. void drm_encoder_init(struct drm_device *dev,
  454. struct drm_encoder *encoder,
  455. const struct drm_encoder_funcs *funcs,
  456. int encoder_type)
  457. {
  458. mutex_lock(&dev->mode_config.mutex);
  459. encoder->dev = dev;
  460. drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
  461. encoder->encoder_type = encoder_type;
  462. encoder->funcs = funcs;
  463. list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
  464. dev->mode_config.num_encoder++;
  465. mutex_unlock(&dev->mode_config.mutex);
  466. }
  467. EXPORT_SYMBOL(drm_encoder_init);
  468. void drm_encoder_cleanup(struct drm_encoder *encoder)
  469. {
  470. struct drm_device *dev = encoder->dev;
  471. mutex_lock(&dev->mode_config.mutex);
  472. drm_mode_object_put(dev, &encoder->base);
  473. list_del(&encoder->head);
  474. dev->mode_config.num_encoder--;
  475. mutex_unlock(&dev->mode_config.mutex);
  476. }
  477. EXPORT_SYMBOL(drm_encoder_cleanup);
  478. /**
  479. * drm_mode_create - create a new display mode
  480. * @dev: DRM device
  481. *
  482. * LOCKING:
  483. * Caller must hold DRM mode_config lock.
  484. *
  485. * Create a new drm_display_mode, give it an ID, and return it.
  486. *
  487. * RETURNS:
  488. * Pointer to new mode on success, NULL on error.
  489. */
  490. struct drm_display_mode *drm_mode_create(struct drm_device *dev)
  491. {
  492. struct drm_display_mode *nmode;
  493. nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
  494. if (!nmode)
  495. return NULL;
  496. drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE);
  497. return nmode;
  498. }
  499. EXPORT_SYMBOL(drm_mode_create);
  500. /**
  501. * drm_mode_destroy - remove a mode
  502. * @dev: DRM device
  503. * @mode: mode to remove
  504. *
  505. * LOCKING:
  506. * Caller must hold mode config lock.
  507. *
  508. * Free @mode's unique identifier, then free it.
  509. */
  510. void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
  511. {
  512. drm_mode_object_put(dev, &mode->base);
  513. kfree(mode);
  514. }
  515. EXPORT_SYMBOL(drm_mode_destroy);
  516. static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
  517. {
  518. struct drm_property *edid;
  519. struct drm_property *dpms;
  520. int i;
  521. /*
  522. * Standard properties (apply to all connectors)
  523. */
  524. edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
  525. DRM_MODE_PROP_IMMUTABLE,
  526. "EDID", 0);
  527. dev->mode_config.edid_property = edid;
  528. dpms = drm_property_create(dev, DRM_MODE_PROP_ENUM,
  529. "DPMS", ARRAY_SIZE(drm_dpms_enum_list));
  530. for (i = 0; i < ARRAY_SIZE(drm_dpms_enum_list); i++)
  531. drm_property_add_enum(dpms, i, drm_dpms_enum_list[i].type,
  532. drm_dpms_enum_list[i].name);
  533. dev->mode_config.dpms_property = dpms;
  534. return 0;
  535. }
  536. /**
  537. * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
  538. * @dev: DRM device
  539. *
  540. * Called by a driver the first time a DVI-I connector is made.
  541. */
  542. int drm_mode_create_dvi_i_properties(struct drm_device *dev)
  543. {
  544. struct drm_property *dvi_i_selector;
  545. struct drm_property *dvi_i_subconnector;
  546. int i;
  547. if (dev->mode_config.dvi_i_select_subconnector_property)
  548. return 0;
  549. dvi_i_selector =
  550. drm_property_create(dev, DRM_MODE_PROP_ENUM,
  551. "select subconnector",
  552. ARRAY_SIZE(drm_dvi_i_select_enum_list));
  553. for (i = 0; i < ARRAY_SIZE(drm_dvi_i_select_enum_list); i++)
  554. drm_property_add_enum(dvi_i_selector, i,
  555. drm_dvi_i_select_enum_list[i].type,
  556. drm_dvi_i_select_enum_list[i].name);
  557. dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
  558. dvi_i_subconnector =
  559. drm_property_create(dev, DRM_MODE_PROP_ENUM |
  560. DRM_MODE_PROP_IMMUTABLE,
  561. "subconnector",
  562. ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
  563. for (i = 0; i < ARRAY_SIZE(drm_dvi_i_subconnector_enum_list); i++)
  564. drm_property_add_enum(dvi_i_subconnector, i,
  565. drm_dvi_i_subconnector_enum_list[i].type,
  566. drm_dvi_i_subconnector_enum_list[i].name);
  567. dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
  568. return 0;
  569. }
  570. EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
  571. /**
  572. * drm_create_tv_properties - create TV specific connector properties
  573. * @dev: DRM device
  574. * @num_modes: number of different TV formats (modes) supported
  575. * @modes: array of pointers to strings containing name of each format
  576. *
  577. * Called by a driver's TV initialization routine, this function creates
  578. * the TV specific connector properties for a given device. Caller is
  579. * responsible for allocating a list of format names and passing them to
  580. * this routine.
  581. */
  582. int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
  583. char *modes[])
  584. {
  585. struct drm_property *tv_selector;
  586. struct drm_property *tv_subconnector;
  587. int i;
  588. if (dev->mode_config.tv_select_subconnector_property)
  589. return 0;
  590. /*
  591. * Basic connector properties
  592. */
  593. tv_selector = drm_property_create(dev, DRM_MODE_PROP_ENUM,
  594. "select subconnector",
  595. ARRAY_SIZE(drm_tv_select_enum_list));
  596. for (i = 0; i < ARRAY_SIZE(drm_tv_select_enum_list); i++)
  597. drm_property_add_enum(tv_selector, i,
  598. drm_tv_select_enum_list[i].type,
  599. drm_tv_select_enum_list[i].name);
  600. dev->mode_config.tv_select_subconnector_property = tv_selector;
  601. tv_subconnector =
  602. drm_property_create(dev, DRM_MODE_PROP_ENUM |
  603. DRM_MODE_PROP_IMMUTABLE, "subconnector",
  604. ARRAY_SIZE(drm_tv_subconnector_enum_list));
  605. for (i = 0; i < ARRAY_SIZE(drm_tv_subconnector_enum_list); i++)
  606. drm_property_add_enum(tv_subconnector, i,
  607. drm_tv_subconnector_enum_list[i].type,
  608. drm_tv_subconnector_enum_list[i].name);
  609. dev->mode_config.tv_subconnector_property = tv_subconnector;
  610. /*
  611. * Other, TV specific properties: margins & TV modes.
  612. */
  613. dev->mode_config.tv_left_margin_property =
  614. drm_property_create(dev, DRM_MODE_PROP_RANGE,
  615. "left margin", 2);
  616. dev->mode_config.tv_left_margin_property->values[0] = 0;
  617. dev->mode_config.tv_left_margin_property->values[1] = 100;
  618. dev->mode_config.tv_right_margin_property =
  619. drm_property_create(dev, DRM_MODE_PROP_RANGE,
  620. "right margin", 2);
  621. dev->mode_config.tv_right_margin_property->values[0] = 0;
  622. dev->mode_config.tv_right_margin_property->values[1] = 100;
  623. dev->mode_config.tv_top_margin_property =
  624. drm_property_create(dev, DRM_MODE_PROP_RANGE,
  625. "top margin", 2);
  626. dev->mode_config.tv_top_margin_property->values[0] = 0;
  627. dev->mode_config.tv_top_margin_property->values[1] = 100;
  628. dev->mode_config.tv_bottom_margin_property =
  629. drm_property_create(dev, DRM_MODE_PROP_RANGE,
  630. "bottom margin", 2);
  631. dev->mode_config.tv_bottom_margin_property->values[0] = 0;
  632. dev->mode_config.tv_bottom_margin_property->values[1] = 100;
  633. dev->mode_config.tv_mode_property =
  634. drm_property_create(dev, DRM_MODE_PROP_ENUM,
  635. "mode", num_modes);
  636. for (i = 0; i < num_modes; i++)
  637. drm_property_add_enum(dev->mode_config.tv_mode_property, i,
  638. i, modes[i]);
  639. dev->mode_config.tv_brightness_property =
  640. drm_property_create(dev, DRM_MODE_PROP_RANGE,
  641. "brightness", 2);
  642. dev->mode_config.tv_brightness_property->values[0] = 0;
  643. dev->mode_config.tv_brightness_property->values[1] = 100;
  644. dev->mode_config.tv_contrast_property =
  645. drm_property_create(dev, DRM_MODE_PROP_RANGE,
  646. "contrast", 2);
  647. dev->mode_config.tv_contrast_property->values[0] = 0;
  648. dev->mode_config.tv_contrast_property->values[1] = 100;
  649. dev->mode_config.tv_flicker_reduction_property =
  650. drm_property_create(dev, DRM_MODE_PROP_RANGE,
  651. "flicker reduction", 2);
  652. dev->mode_config.tv_flicker_reduction_property->values[0] = 0;
  653. dev->mode_config.tv_flicker_reduction_property->values[1] = 100;
  654. dev->mode_config.tv_overscan_property =
  655. drm_property_create(dev, DRM_MODE_PROP_RANGE,
  656. "overscan", 2);
  657. dev->mode_config.tv_overscan_property->values[0] = 0;
  658. dev->mode_config.tv_overscan_property->values[1] = 100;
  659. dev->mode_config.tv_saturation_property =
  660. drm_property_create(dev, DRM_MODE_PROP_RANGE,
  661. "saturation", 2);
  662. dev->mode_config.tv_saturation_property->values[0] = 0;
  663. dev->mode_config.tv_saturation_property->values[1] = 100;
  664. dev->mode_config.tv_hue_property =
  665. drm_property_create(dev, DRM_MODE_PROP_RANGE,
  666. "hue", 2);
  667. dev->mode_config.tv_hue_property->values[0] = 0;
  668. dev->mode_config.tv_hue_property->values[1] = 100;
  669. return 0;
  670. }
  671. EXPORT_SYMBOL(drm_mode_create_tv_properties);
  672. /**
  673. * drm_mode_create_scaling_mode_property - create scaling mode property
  674. * @dev: DRM device
  675. *
  676. * Called by a driver the first time it's needed, must be attached to desired
  677. * connectors.
  678. */
  679. int drm_mode_create_scaling_mode_property(struct drm_device *dev)
  680. {
  681. struct drm_property *scaling_mode;
  682. int i;
  683. if (dev->mode_config.scaling_mode_property)
  684. return 0;
  685. scaling_mode =
  686. drm_property_create(dev, DRM_MODE_PROP_ENUM, "scaling mode",
  687. ARRAY_SIZE(drm_scaling_mode_enum_list));
  688. for (i = 0; i < ARRAY_SIZE(drm_scaling_mode_enum_list); i++)
  689. drm_property_add_enum(scaling_mode, i,
  690. drm_scaling_mode_enum_list[i].type,
  691. drm_scaling_mode_enum_list[i].name);
  692. dev->mode_config.scaling_mode_property = scaling_mode;
  693. return 0;
  694. }
  695. EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
  696. /**
  697. * drm_mode_create_dithering_property - create dithering property
  698. * @dev: DRM device
  699. *
  700. * Called by a driver the first time it's needed, must be attached to desired
  701. * connectors.
  702. */
  703. int drm_mode_create_dithering_property(struct drm_device *dev)
  704. {
  705. struct drm_property *dithering_mode;
  706. int i;
  707. if (dev->mode_config.dithering_mode_property)
  708. return 0;
  709. dithering_mode =
  710. drm_property_create(dev, DRM_MODE_PROP_ENUM, "dithering",
  711. ARRAY_SIZE(drm_dithering_mode_enum_list));
  712. for (i = 0; i < ARRAY_SIZE(drm_dithering_mode_enum_list); i++)
  713. drm_property_add_enum(dithering_mode, i,
  714. drm_dithering_mode_enum_list[i].type,
  715. drm_dithering_mode_enum_list[i].name);
  716. dev->mode_config.dithering_mode_property = dithering_mode;
  717. return 0;
  718. }
  719. EXPORT_SYMBOL(drm_mode_create_dithering_property);
  720. /**
  721. * drm_mode_create_dirty_property - create dirty property
  722. * @dev: DRM device
  723. *
  724. * Called by a driver the first time it's needed, must be attached to desired
  725. * connectors.
  726. */
  727. int drm_mode_create_dirty_info_property(struct drm_device *dev)
  728. {
  729. struct drm_property *dirty_info;
  730. int i;
  731. if (dev->mode_config.dirty_info_property)
  732. return 0;
  733. dirty_info =
  734. drm_property_create(dev, DRM_MODE_PROP_ENUM |
  735. DRM_MODE_PROP_IMMUTABLE,
  736. "dirty",
  737. ARRAY_SIZE(drm_dirty_info_enum_list));
  738. for (i = 0; i < ARRAY_SIZE(drm_dirty_info_enum_list); i++)
  739. drm_property_add_enum(dirty_info, i,
  740. drm_dirty_info_enum_list[i].type,
  741. drm_dirty_info_enum_list[i].name);
  742. dev->mode_config.dirty_info_property = dirty_info;
  743. return 0;
  744. }
  745. EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
  746. /**
  747. * drm_mode_config_init - initialize DRM mode_configuration structure
  748. * @dev: DRM device
  749. *
  750. * LOCKING:
  751. * None, should happen single threaded at init time.
  752. *
  753. * Initialize @dev's mode_config structure, used for tracking the graphics
  754. * configuration of @dev.
  755. */
  756. void drm_mode_config_init(struct drm_device *dev)
  757. {
  758. mutex_init(&dev->mode_config.mutex);
  759. mutex_init(&dev->mode_config.idr_mutex);
  760. INIT_LIST_HEAD(&dev->mode_config.fb_list);
  761. INIT_LIST_HEAD(&dev->mode_config.crtc_list);
  762. INIT_LIST_HEAD(&dev->mode_config.connector_list);
  763. INIT_LIST_HEAD(&dev->mode_config.encoder_list);
  764. INIT_LIST_HEAD(&dev->mode_config.property_list);
  765. INIT_LIST_HEAD(&dev->mode_config.property_blob_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. list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
  830. head) {
  831. encoder->funcs->destroy(encoder);
  832. }
  833. list_for_each_entry_safe(connector, ot,
  834. &dev->mode_config.connector_list, head) {
  835. connector->funcs->destroy(connector);
  836. }
  837. list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
  838. head) {
  839. drm_property_destroy(dev, property);
  840. }
  841. list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
  842. fb->funcs->destroy(fb);
  843. }
  844. list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
  845. crtc->funcs->destroy(crtc);
  846. }
  847. }
  848. EXPORT_SYMBOL(drm_mode_config_cleanup);
  849. /**
  850. * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
  851. * @out: drm_mode_modeinfo struct to return to the user
  852. * @in: drm_display_mode to use
  853. *
  854. * LOCKING:
  855. * None.
  856. *
  857. * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
  858. * the user.
  859. */
  860. void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
  861. struct drm_display_mode *in)
  862. {
  863. out->clock = in->clock;
  864. out->hdisplay = in->hdisplay;
  865. out->hsync_start = in->hsync_start;
  866. out->hsync_end = in->hsync_end;
  867. out->htotal = in->htotal;
  868. out->hskew = in->hskew;
  869. out->vdisplay = in->vdisplay;
  870. out->vsync_start = in->vsync_start;
  871. out->vsync_end = in->vsync_end;
  872. out->vtotal = in->vtotal;
  873. out->vscan = in->vscan;
  874. out->vrefresh = in->vrefresh;
  875. out->flags = in->flags;
  876. out->type = in->type;
  877. strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
  878. out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
  879. }
  880. /**
  881. * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
  882. * @out: drm_display_mode to return to the user
  883. * @in: drm_mode_modeinfo to use
  884. *
  885. * LOCKING:
  886. * None.
  887. *
  888. * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
  889. * the caller.
  890. */
  891. void drm_crtc_convert_umode(struct drm_display_mode *out,
  892. struct drm_mode_modeinfo *in)
  893. {
  894. out->clock = in->clock;
  895. out->hdisplay = in->hdisplay;
  896. out->hsync_start = in->hsync_start;
  897. out->hsync_end = in->hsync_end;
  898. out->htotal = in->htotal;
  899. out->hskew = in->hskew;
  900. out->vdisplay = in->vdisplay;
  901. out->vsync_start = in->vsync_start;
  902. out->vsync_end = in->vsync_end;
  903. out->vtotal = in->vtotal;
  904. out->vscan = in->vscan;
  905. out->vrefresh = in->vrefresh;
  906. out->flags = in->flags;
  907. out->type = in->type;
  908. strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
  909. out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
  910. }
  911. /**
  912. * drm_mode_getresources - get graphics configuration
  913. * @inode: inode from the ioctl
  914. * @filp: file * from the ioctl
  915. * @cmd: cmd from ioctl
  916. * @arg: arg from ioctl
  917. *
  918. * LOCKING:
  919. * Takes mode config lock.
  920. *
  921. * Construct a set of configuration description structures and return
  922. * them to the user, including CRTC, connector and framebuffer configuration.
  923. *
  924. * Called by the user via ioctl.
  925. *
  926. * RETURNS:
  927. * Zero on success, errno on failure.
  928. */
  929. int drm_mode_getresources(struct drm_device *dev, void *data,
  930. struct drm_file *file_priv)
  931. {
  932. struct drm_mode_card_res *card_res = data;
  933. struct list_head *lh;
  934. struct drm_framebuffer *fb;
  935. struct drm_connector *connector;
  936. struct drm_crtc *crtc;
  937. struct drm_encoder *encoder;
  938. int ret = 0;
  939. int connector_count = 0;
  940. int crtc_count = 0;
  941. int fb_count = 0;
  942. int encoder_count = 0;
  943. int copied = 0, i;
  944. uint32_t __user *fb_id;
  945. uint32_t __user *crtc_id;
  946. uint32_t __user *connector_id;
  947. uint32_t __user *encoder_id;
  948. struct drm_mode_group *mode_group;
  949. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  950. return -EINVAL;
  951. mutex_lock(&dev->mode_config.mutex);
  952. /*
  953. * For the non-control nodes we need to limit the list of resources
  954. * by IDs in the group list for this node
  955. */
  956. list_for_each(lh, &file_priv->fbs)
  957. fb_count++;
  958. mode_group = &file_priv->master->minor->mode_group;
  959. if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
  960. list_for_each(lh, &dev->mode_config.crtc_list)
  961. crtc_count++;
  962. list_for_each(lh, &dev->mode_config.connector_list)
  963. connector_count++;
  964. list_for_each(lh, &dev->mode_config.encoder_list)
  965. encoder_count++;
  966. } else {
  967. crtc_count = mode_group->num_crtcs;
  968. connector_count = mode_group->num_connectors;
  969. encoder_count = mode_group->num_encoders;
  970. }
  971. card_res->max_height = dev->mode_config.max_height;
  972. card_res->min_height = dev->mode_config.min_height;
  973. card_res->max_width = dev->mode_config.max_width;
  974. card_res->min_width = dev->mode_config.min_width;
  975. /* handle this in 4 parts */
  976. /* FBs */
  977. if (card_res->count_fbs >= fb_count) {
  978. copied = 0;
  979. fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
  980. list_for_each_entry(fb, &file_priv->fbs, filp_head) {
  981. if (put_user(fb->base.id, fb_id + copied)) {
  982. ret = -EFAULT;
  983. goto out;
  984. }
  985. copied++;
  986. }
  987. }
  988. card_res->count_fbs = fb_count;
  989. /* CRTCs */
  990. if (card_res->count_crtcs >= crtc_count) {
  991. copied = 0;
  992. crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
  993. if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
  994. list_for_each_entry(crtc, &dev->mode_config.crtc_list,
  995. head) {
  996. DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
  997. if (put_user(crtc->base.id, crtc_id + copied)) {
  998. ret = -EFAULT;
  999. goto out;
  1000. }
  1001. copied++;
  1002. }
  1003. } else {
  1004. for (i = 0; i < mode_group->num_crtcs; i++) {
  1005. if (put_user(mode_group->id_list[i],
  1006. crtc_id + copied)) {
  1007. ret = -EFAULT;
  1008. goto out;
  1009. }
  1010. copied++;
  1011. }
  1012. }
  1013. }
  1014. card_res->count_crtcs = crtc_count;
  1015. /* Encoders */
  1016. if (card_res->count_encoders >= encoder_count) {
  1017. copied = 0;
  1018. encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
  1019. if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
  1020. list_for_each_entry(encoder,
  1021. &dev->mode_config.encoder_list,
  1022. head) {
  1023. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
  1024. drm_get_encoder_name(encoder));
  1025. if (put_user(encoder->base.id, encoder_id +
  1026. copied)) {
  1027. ret = -EFAULT;
  1028. goto out;
  1029. }
  1030. copied++;
  1031. }
  1032. } else {
  1033. for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
  1034. if (put_user(mode_group->id_list[i],
  1035. encoder_id + copied)) {
  1036. ret = -EFAULT;
  1037. goto out;
  1038. }
  1039. copied++;
  1040. }
  1041. }
  1042. }
  1043. card_res->count_encoders = encoder_count;
  1044. /* Connectors */
  1045. if (card_res->count_connectors >= connector_count) {
  1046. copied = 0;
  1047. connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
  1048. if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
  1049. list_for_each_entry(connector,
  1050. &dev->mode_config.connector_list,
  1051. head) {
  1052. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  1053. connector->base.id,
  1054. drm_get_connector_name(connector));
  1055. if (put_user(connector->base.id,
  1056. connector_id + copied)) {
  1057. ret = -EFAULT;
  1058. goto out;
  1059. }
  1060. copied++;
  1061. }
  1062. } else {
  1063. int start = mode_group->num_crtcs +
  1064. mode_group->num_encoders;
  1065. for (i = start; i < start + mode_group->num_connectors; i++) {
  1066. if (put_user(mode_group->id_list[i],
  1067. connector_id + copied)) {
  1068. ret = -EFAULT;
  1069. goto out;
  1070. }
  1071. copied++;
  1072. }
  1073. }
  1074. }
  1075. card_res->count_connectors = connector_count;
  1076. DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
  1077. card_res->count_connectors, card_res->count_encoders);
  1078. out:
  1079. mutex_unlock(&dev->mode_config.mutex);
  1080. return ret;
  1081. }
  1082. /**
  1083. * drm_mode_getcrtc - get CRTC configuration
  1084. * @inode: inode from the ioctl
  1085. * @filp: file * from the ioctl
  1086. * @cmd: cmd from ioctl
  1087. * @arg: arg from ioctl
  1088. *
  1089. * LOCKING:
  1090. * Caller? (FIXME)
  1091. *
  1092. * Construct a CRTC configuration structure to return to the user.
  1093. *
  1094. * Called by the user via ioctl.
  1095. *
  1096. * RETURNS:
  1097. * Zero on success, errno on failure.
  1098. */
  1099. int drm_mode_getcrtc(struct drm_device *dev,
  1100. void *data, struct drm_file *file_priv)
  1101. {
  1102. struct drm_mode_crtc *crtc_resp = data;
  1103. struct drm_crtc *crtc;
  1104. struct drm_mode_object *obj;
  1105. int ret = 0;
  1106. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1107. return -EINVAL;
  1108. mutex_lock(&dev->mode_config.mutex);
  1109. obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
  1110. DRM_MODE_OBJECT_CRTC);
  1111. if (!obj) {
  1112. ret = -EINVAL;
  1113. goto out;
  1114. }
  1115. crtc = obj_to_crtc(obj);
  1116. crtc_resp->x = crtc->x;
  1117. crtc_resp->y = crtc->y;
  1118. crtc_resp->gamma_size = crtc->gamma_size;
  1119. if (crtc->fb)
  1120. crtc_resp->fb_id = crtc->fb->base.id;
  1121. else
  1122. crtc_resp->fb_id = 0;
  1123. if (crtc->enabled) {
  1124. drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
  1125. crtc_resp->mode_valid = 1;
  1126. } else {
  1127. crtc_resp->mode_valid = 0;
  1128. }
  1129. out:
  1130. mutex_unlock(&dev->mode_config.mutex);
  1131. return ret;
  1132. }
  1133. /**
  1134. * drm_mode_getconnector - get connector configuration
  1135. * @inode: inode from the ioctl
  1136. * @filp: file * from the ioctl
  1137. * @cmd: cmd from ioctl
  1138. * @arg: arg from ioctl
  1139. *
  1140. * LOCKING:
  1141. * Caller? (FIXME)
  1142. *
  1143. * Construct a connector configuration structure to return to the user.
  1144. *
  1145. * Called by the user via ioctl.
  1146. *
  1147. * RETURNS:
  1148. * Zero on success, errno on failure.
  1149. */
  1150. int drm_mode_getconnector(struct drm_device *dev, void *data,
  1151. struct drm_file *file_priv)
  1152. {
  1153. struct drm_mode_get_connector *out_resp = data;
  1154. struct drm_mode_object *obj;
  1155. struct drm_connector *connector;
  1156. struct drm_display_mode *mode;
  1157. int mode_count = 0;
  1158. int props_count = 0;
  1159. int encoders_count = 0;
  1160. int ret = 0;
  1161. int copied = 0;
  1162. int i;
  1163. struct drm_mode_modeinfo u_mode;
  1164. struct drm_mode_modeinfo __user *mode_ptr;
  1165. uint32_t __user *prop_ptr;
  1166. uint64_t __user *prop_values;
  1167. uint32_t __user *encoder_ptr;
  1168. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1169. return -EINVAL;
  1170. memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
  1171. DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
  1172. mutex_lock(&dev->mode_config.mutex);
  1173. obj = drm_mode_object_find(dev, out_resp->connector_id,
  1174. DRM_MODE_OBJECT_CONNECTOR);
  1175. if (!obj) {
  1176. ret = -EINVAL;
  1177. goto out;
  1178. }
  1179. connector = obj_to_connector(obj);
  1180. for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
  1181. if (connector->property_ids[i] != 0) {
  1182. props_count++;
  1183. }
  1184. }
  1185. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  1186. if (connector->encoder_ids[i] != 0) {
  1187. encoders_count++;
  1188. }
  1189. }
  1190. if (out_resp->count_modes == 0) {
  1191. connector->funcs->fill_modes(connector,
  1192. dev->mode_config.max_width,
  1193. dev->mode_config.max_height);
  1194. }
  1195. /* delayed so we get modes regardless of pre-fill_modes state */
  1196. list_for_each_entry(mode, &connector->modes, head)
  1197. mode_count++;
  1198. out_resp->connector_id = connector->base.id;
  1199. out_resp->connector_type = connector->connector_type;
  1200. out_resp->connector_type_id = connector->connector_type_id;
  1201. out_resp->mm_width = connector->display_info.width_mm;
  1202. out_resp->mm_height = connector->display_info.height_mm;
  1203. out_resp->subpixel = connector->display_info.subpixel_order;
  1204. out_resp->connection = connector->status;
  1205. if (connector->encoder)
  1206. out_resp->encoder_id = connector->encoder->base.id;
  1207. else
  1208. out_resp->encoder_id = 0;
  1209. /*
  1210. * This ioctl is called twice, once to determine how much space is
  1211. * needed, and the 2nd time to fill it.
  1212. */
  1213. if ((out_resp->count_modes >= mode_count) && mode_count) {
  1214. copied = 0;
  1215. mode_ptr = (struct drm_mode_modeinfo *)(unsigned long)out_resp->modes_ptr;
  1216. list_for_each_entry(mode, &connector->modes, head) {
  1217. drm_crtc_convert_to_umode(&u_mode, mode);
  1218. if (copy_to_user(mode_ptr + copied,
  1219. &u_mode, sizeof(u_mode))) {
  1220. ret = -EFAULT;
  1221. goto out;
  1222. }
  1223. copied++;
  1224. }
  1225. }
  1226. out_resp->count_modes = mode_count;
  1227. if ((out_resp->count_props >= props_count) && props_count) {
  1228. copied = 0;
  1229. prop_ptr = (uint32_t *)(unsigned long)(out_resp->props_ptr);
  1230. prop_values = (uint64_t *)(unsigned long)(out_resp->prop_values_ptr);
  1231. for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
  1232. if (connector->property_ids[i] != 0) {
  1233. if (put_user(connector->property_ids[i],
  1234. prop_ptr + copied)) {
  1235. ret = -EFAULT;
  1236. goto out;
  1237. }
  1238. if (put_user(connector->property_values[i],
  1239. prop_values + copied)) {
  1240. ret = -EFAULT;
  1241. goto out;
  1242. }
  1243. copied++;
  1244. }
  1245. }
  1246. }
  1247. out_resp->count_props = props_count;
  1248. if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
  1249. copied = 0;
  1250. encoder_ptr = (uint32_t *)(unsigned long)(out_resp->encoders_ptr);
  1251. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  1252. if (connector->encoder_ids[i] != 0) {
  1253. if (put_user(connector->encoder_ids[i],
  1254. encoder_ptr + copied)) {
  1255. ret = -EFAULT;
  1256. goto out;
  1257. }
  1258. copied++;
  1259. }
  1260. }
  1261. }
  1262. out_resp->count_encoders = encoders_count;
  1263. out:
  1264. mutex_unlock(&dev->mode_config.mutex);
  1265. return ret;
  1266. }
  1267. int drm_mode_getencoder(struct drm_device *dev, void *data,
  1268. struct drm_file *file_priv)
  1269. {
  1270. struct drm_mode_get_encoder *enc_resp = data;
  1271. struct drm_mode_object *obj;
  1272. struct drm_encoder *encoder;
  1273. int ret = 0;
  1274. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1275. return -EINVAL;
  1276. mutex_lock(&dev->mode_config.mutex);
  1277. obj = drm_mode_object_find(dev, enc_resp->encoder_id,
  1278. DRM_MODE_OBJECT_ENCODER);
  1279. if (!obj) {
  1280. ret = -EINVAL;
  1281. goto out;
  1282. }
  1283. encoder = obj_to_encoder(obj);
  1284. if (encoder->crtc)
  1285. enc_resp->crtc_id = encoder->crtc->base.id;
  1286. else
  1287. enc_resp->crtc_id = 0;
  1288. enc_resp->encoder_type = encoder->encoder_type;
  1289. enc_resp->encoder_id = encoder->base.id;
  1290. enc_resp->possible_crtcs = encoder->possible_crtcs;
  1291. enc_resp->possible_clones = encoder->possible_clones;
  1292. out:
  1293. mutex_unlock(&dev->mode_config.mutex);
  1294. return ret;
  1295. }
  1296. /**
  1297. * drm_mode_setcrtc - set CRTC configuration
  1298. * @inode: inode from the ioctl
  1299. * @filp: file * from the ioctl
  1300. * @cmd: cmd from ioctl
  1301. * @arg: arg from ioctl
  1302. *
  1303. * LOCKING:
  1304. * Caller? (FIXME)
  1305. *
  1306. * Build a new CRTC configuration based on user request.
  1307. *
  1308. * Called by the user via ioctl.
  1309. *
  1310. * RETURNS:
  1311. * Zero on success, errno on failure.
  1312. */
  1313. int drm_mode_setcrtc(struct drm_device *dev, void *data,
  1314. struct drm_file *file_priv)
  1315. {
  1316. struct drm_mode_config *config = &dev->mode_config;
  1317. struct drm_mode_crtc *crtc_req = data;
  1318. struct drm_mode_object *obj;
  1319. struct drm_crtc *crtc, *crtcfb;
  1320. struct drm_connector **connector_set = NULL, *connector;
  1321. struct drm_framebuffer *fb = NULL;
  1322. struct drm_display_mode *mode = NULL;
  1323. struct drm_mode_set set;
  1324. uint32_t __user *set_connectors_ptr;
  1325. int ret = 0;
  1326. int i;
  1327. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1328. return -EINVAL;
  1329. mutex_lock(&dev->mode_config.mutex);
  1330. obj = drm_mode_object_find(dev, crtc_req->crtc_id,
  1331. DRM_MODE_OBJECT_CRTC);
  1332. if (!obj) {
  1333. DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
  1334. ret = -EINVAL;
  1335. goto out;
  1336. }
  1337. crtc = obj_to_crtc(obj);
  1338. DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
  1339. if (crtc_req->mode_valid) {
  1340. /* If we have a mode we need a framebuffer. */
  1341. /* If we pass -1, set the mode with the currently bound fb */
  1342. if (crtc_req->fb_id == -1) {
  1343. list_for_each_entry(crtcfb,
  1344. &dev->mode_config.crtc_list, head) {
  1345. if (crtcfb == crtc) {
  1346. DRM_DEBUG_KMS("Using current fb for "
  1347. "setmode\n");
  1348. fb = crtc->fb;
  1349. }
  1350. }
  1351. } else {
  1352. obj = drm_mode_object_find(dev, crtc_req->fb_id,
  1353. DRM_MODE_OBJECT_FB);
  1354. if (!obj) {
  1355. DRM_DEBUG_KMS("Unknown FB ID%d\n",
  1356. crtc_req->fb_id);
  1357. ret = -EINVAL;
  1358. goto out;
  1359. }
  1360. fb = obj_to_fb(obj);
  1361. }
  1362. mode = drm_mode_create(dev);
  1363. drm_crtc_convert_umode(mode, &crtc_req->mode);
  1364. drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
  1365. }
  1366. if (crtc_req->count_connectors == 0 && mode) {
  1367. DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
  1368. ret = -EINVAL;
  1369. goto out;
  1370. }
  1371. if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
  1372. DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
  1373. crtc_req->count_connectors);
  1374. ret = -EINVAL;
  1375. goto out;
  1376. }
  1377. if (crtc_req->count_connectors > 0) {
  1378. u32 out_id;
  1379. /* Avoid unbounded kernel memory allocation */
  1380. if (crtc_req->count_connectors > config->num_connector) {
  1381. ret = -EINVAL;
  1382. goto out;
  1383. }
  1384. connector_set = kmalloc(crtc_req->count_connectors *
  1385. sizeof(struct drm_connector *),
  1386. GFP_KERNEL);
  1387. if (!connector_set) {
  1388. ret = -ENOMEM;
  1389. goto out;
  1390. }
  1391. for (i = 0; i < crtc_req->count_connectors; i++) {
  1392. set_connectors_ptr = (uint32_t *)(unsigned long)crtc_req->set_connectors_ptr;
  1393. if (get_user(out_id, &set_connectors_ptr[i])) {
  1394. ret = -EFAULT;
  1395. goto out;
  1396. }
  1397. obj = drm_mode_object_find(dev, out_id,
  1398. DRM_MODE_OBJECT_CONNECTOR);
  1399. if (!obj) {
  1400. DRM_DEBUG_KMS("Connector id %d unknown\n",
  1401. out_id);
  1402. ret = -EINVAL;
  1403. goto out;
  1404. }
  1405. connector = obj_to_connector(obj);
  1406. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  1407. connector->base.id,
  1408. drm_get_connector_name(connector));
  1409. connector_set[i] = connector;
  1410. }
  1411. }
  1412. set.crtc = crtc;
  1413. set.x = crtc_req->x;
  1414. set.y = crtc_req->y;
  1415. set.mode = mode;
  1416. set.connectors = connector_set;
  1417. set.num_connectors = crtc_req->count_connectors;
  1418. set.fb = fb;
  1419. ret = crtc->funcs->set_config(&set);
  1420. out:
  1421. kfree(connector_set);
  1422. mutex_unlock(&dev->mode_config.mutex);
  1423. return ret;
  1424. }
  1425. int drm_mode_cursor_ioctl(struct drm_device *dev,
  1426. void *data, struct drm_file *file_priv)
  1427. {
  1428. struct drm_mode_cursor *req = data;
  1429. struct drm_mode_object *obj;
  1430. struct drm_crtc *crtc;
  1431. int ret = 0;
  1432. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1433. return -EINVAL;
  1434. if (!req->flags) {
  1435. DRM_ERROR("no operation set\n");
  1436. return -EINVAL;
  1437. }
  1438. mutex_lock(&dev->mode_config.mutex);
  1439. obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
  1440. if (!obj) {
  1441. DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
  1442. ret = -EINVAL;
  1443. goto out;
  1444. }
  1445. crtc = obj_to_crtc(obj);
  1446. if (req->flags & DRM_MODE_CURSOR_BO) {
  1447. if (!crtc->funcs->cursor_set) {
  1448. DRM_ERROR("crtc does not support cursor\n");
  1449. ret = -ENXIO;
  1450. goto out;
  1451. }
  1452. /* Turns off the cursor if handle is 0 */
  1453. ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
  1454. req->width, req->height);
  1455. }
  1456. if (req->flags & DRM_MODE_CURSOR_MOVE) {
  1457. if (crtc->funcs->cursor_move) {
  1458. ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
  1459. } else {
  1460. DRM_ERROR("crtc does not support cursor\n");
  1461. ret = -EFAULT;
  1462. goto out;
  1463. }
  1464. }
  1465. out:
  1466. mutex_unlock(&dev->mode_config.mutex);
  1467. return ret;
  1468. }
  1469. /**
  1470. * drm_mode_addfb - add an FB to the graphics configuration
  1471. * @inode: inode from the ioctl
  1472. * @filp: file * from the ioctl
  1473. * @cmd: cmd from ioctl
  1474. * @arg: arg from ioctl
  1475. *
  1476. * LOCKING:
  1477. * Takes mode config lock.
  1478. *
  1479. * Add a new FB to the specified CRTC, given a user request.
  1480. *
  1481. * Called by the user via ioctl.
  1482. *
  1483. * RETURNS:
  1484. * Zero on success, errno on failure.
  1485. */
  1486. int drm_mode_addfb(struct drm_device *dev,
  1487. void *data, struct drm_file *file_priv)
  1488. {
  1489. struct drm_mode_fb_cmd *r = data;
  1490. struct drm_mode_config *config = &dev->mode_config;
  1491. struct drm_framebuffer *fb;
  1492. int ret = 0;
  1493. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1494. return -EINVAL;
  1495. if ((config->min_width > r->width) || (r->width > config->max_width)) {
  1496. DRM_ERROR("mode new framebuffer width not within limits\n");
  1497. return -EINVAL;
  1498. }
  1499. if ((config->min_height > r->height) || (r->height > config->max_height)) {
  1500. DRM_ERROR("mode new framebuffer height not within limits\n");
  1501. return -EINVAL;
  1502. }
  1503. mutex_lock(&dev->mode_config.mutex);
  1504. /* TODO check buffer is sufficiently large */
  1505. /* TODO setup destructor callback */
  1506. fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
  1507. if (IS_ERR(fb)) {
  1508. DRM_ERROR("could not create framebuffer\n");
  1509. ret = PTR_ERR(fb);
  1510. goto out;
  1511. }
  1512. r->fb_id = fb->base.id;
  1513. list_add(&fb->filp_head, &file_priv->fbs);
  1514. DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
  1515. out:
  1516. mutex_unlock(&dev->mode_config.mutex);
  1517. return ret;
  1518. }
  1519. /**
  1520. * drm_mode_rmfb - remove an FB from the configuration
  1521. * @inode: inode from the ioctl
  1522. * @filp: file * from the ioctl
  1523. * @cmd: cmd from ioctl
  1524. * @arg: arg from ioctl
  1525. *
  1526. * LOCKING:
  1527. * Takes mode config lock.
  1528. *
  1529. * Remove the FB specified by the user.
  1530. *
  1531. * Called by the user via ioctl.
  1532. *
  1533. * RETURNS:
  1534. * Zero on success, errno on failure.
  1535. */
  1536. int drm_mode_rmfb(struct drm_device *dev,
  1537. void *data, struct drm_file *file_priv)
  1538. {
  1539. struct drm_mode_object *obj;
  1540. struct drm_framebuffer *fb = NULL;
  1541. struct drm_framebuffer *fbl = NULL;
  1542. uint32_t *id = data;
  1543. int ret = 0;
  1544. int found = 0;
  1545. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1546. return -EINVAL;
  1547. mutex_lock(&dev->mode_config.mutex);
  1548. obj = drm_mode_object_find(dev, *id, DRM_MODE_OBJECT_FB);
  1549. /* TODO check that we really get a framebuffer back. */
  1550. if (!obj) {
  1551. DRM_ERROR("mode invalid framebuffer id\n");
  1552. ret = -EINVAL;
  1553. goto out;
  1554. }
  1555. fb = obj_to_fb(obj);
  1556. list_for_each_entry(fbl, &file_priv->fbs, filp_head)
  1557. if (fb == fbl)
  1558. found = 1;
  1559. if (!found) {
  1560. DRM_ERROR("tried to remove a fb that we didn't own\n");
  1561. ret = -EINVAL;
  1562. goto out;
  1563. }
  1564. /* TODO release all crtc connected to the framebuffer */
  1565. /* TODO unhock the destructor from the buffer object */
  1566. list_del(&fb->filp_head);
  1567. fb->funcs->destroy(fb);
  1568. out:
  1569. mutex_unlock(&dev->mode_config.mutex);
  1570. return ret;
  1571. }
  1572. /**
  1573. * drm_mode_getfb - get FB info
  1574. * @inode: inode from the ioctl
  1575. * @filp: file * from the ioctl
  1576. * @cmd: cmd from ioctl
  1577. * @arg: arg from ioctl
  1578. *
  1579. * LOCKING:
  1580. * Caller? (FIXME)
  1581. *
  1582. * Lookup the FB given its ID and return info about it.
  1583. *
  1584. * Called by the user via ioctl.
  1585. *
  1586. * RETURNS:
  1587. * Zero on success, errno on failure.
  1588. */
  1589. int drm_mode_getfb(struct drm_device *dev,
  1590. void *data, struct drm_file *file_priv)
  1591. {
  1592. struct drm_mode_fb_cmd *r = data;
  1593. struct drm_mode_object *obj;
  1594. struct drm_framebuffer *fb;
  1595. int ret = 0;
  1596. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1597. return -EINVAL;
  1598. mutex_lock(&dev->mode_config.mutex);
  1599. obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
  1600. if (!obj) {
  1601. DRM_ERROR("invalid framebuffer id\n");
  1602. ret = -EINVAL;
  1603. goto out;
  1604. }
  1605. fb = obj_to_fb(obj);
  1606. r->height = fb->height;
  1607. r->width = fb->width;
  1608. r->depth = fb->depth;
  1609. r->bpp = fb->bits_per_pixel;
  1610. r->pitch = fb->pitch;
  1611. fb->funcs->create_handle(fb, file_priv, &r->handle);
  1612. out:
  1613. mutex_unlock(&dev->mode_config.mutex);
  1614. return ret;
  1615. }
  1616. int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
  1617. void *data, struct drm_file *file_priv)
  1618. {
  1619. struct drm_clip_rect __user *clips_ptr;
  1620. struct drm_clip_rect *clips = NULL;
  1621. struct drm_mode_fb_dirty_cmd *r = data;
  1622. struct drm_mode_object *obj;
  1623. struct drm_framebuffer *fb;
  1624. unsigned flags;
  1625. int num_clips;
  1626. int ret = 0;
  1627. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1628. return -EINVAL;
  1629. mutex_lock(&dev->mode_config.mutex);
  1630. obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
  1631. if (!obj) {
  1632. DRM_ERROR("invalid framebuffer id\n");
  1633. ret = -EINVAL;
  1634. goto out_err1;
  1635. }
  1636. fb = obj_to_fb(obj);
  1637. num_clips = r->num_clips;
  1638. clips_ptr = (struct drm_clip_rect *)(unsigned long)r->clips_ptr;
  1639. if (!num_clips != !clips_ptr) {
  1640. ret = -EINVAL;
  1641. goto out_err1;
  1642. }
  1643. flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
  1644. /* If userspace annotates copy, clips must come in pairs */
  1645. if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
  1646. ret = -EINVAL;
  1647. goto out_err1;
  1648. }
  1649. if (num_clips && clips_ptr) {
  1650. clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
  1651. if (!clips) {
  1652. ret = -ENOMEM;
  1653. goto out_err1;
  1654. }
  1655. ret = copy_from_user(clips, clips_ptr,
  1656. num_clips * sizeof(*clips));
  1657. if (ret) {
  1658. ret = -EFAULT;
  1659. goto out_err2;
  1660. }
  1661. }
  1662. if (fb->funcs->dirty) {
  1663. ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
  1664. clips, num_clips);
  1665. } else {
  1666. ret = -ENOSYS;
  1667. goto out_err2;
  1668. }
  1669. out_err2:
  1670. kfree(clips);
  1671. out_err1:
  1672. mutex_unlock(&dev->mode_config.mutex);
  1673. return ret;
  1674. }
  1675. /**
  1676. * drm_fb_release - remove and free the FBs on this file
  1677. * @filp: file * from the ioctl
  1678. *
  1679. * LOCKING:
  1680. * Takes mode config lock.
  1681. *
  1682. * Destroy all the FBs associated with @filp.
  1683. *
  1684. * Called by the user via ioctl.
  1685. *
  1686. * RETURNS:
  1687. * Zero on success, errno on failure.
  1688. */
  1689. void drm_fb_release(struct drm_file *priv)
  1690. {
  1691. struct drm_device *dev = priv->minor->dev;
  1692. struct drm_framebuffer *fb, *tfb;
  1693. mutex_lock(&dev->mode_config.mutex);
  1694. list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
  1695. list_del(&fb->filp_head);
  1696. fb->funcs->destroy(fb);
  1697. }
  1698. mutex_unlock(&dev->mode_config.mutex);
  1699. }
  1700. /**
  1701. * drm_mode_attachmode - add a mode to the user mode list
  1702. * @dev: DRM device
  1703. * @connector: connector to add the mode to
  1704. * @mode: mode to add
  1705. *
  1706. * Add @mode to @connector's user mode list.
  1707. */
  1708. static int drm_mode_attachmode(struct drm_device *dev,
  1709. struct drm_connector *connector,
  1710. struct drm_display_mode *mode)
  1711. {
  1712. int ret = 0;
  1713. list_add_tail(&mode->head, &connector->user_modes);
  1714. return ret;
  1715. }
  1716. int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
  1717. struct drm_display_mode *mode)
  1718. {
  1719. struct drm_connector *connector;
  1720. int ret = 0;
  1721. struct drm_display_mode *dup_mode;
  1722. int need_dup = 0;
  1723. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  1724. if (!connector->encoder)
  1725. break;
  1726. if (connector->encoder->crtc == crtc) {
  1727. if (need_dup)
  1728. dup_mode = drm_mode_duplicate(dev, mode);
  1729. else
  1730. dup_mode = mode;
  1731. ret = drm_mode_attachmode(dev, connector, dup_mode);
  1732. if (ret)
  1733. return ret;
  1734. need_dup = 1;
  1735. }
  1736. }
  1737. return 0;
  1738. }
  1739. EXPORT_SYMBOL(drm_mode_attachmode_crtc);
  1740. static int drm_mode_detachmode(struct drm_device *dev,
  1741. struct drm_connector *connector,
  1742. struct drm_display_mode *mode)
  1743. {
  1744. int found = 0;
  1745. int ret = 0;
  1746. struct drm_display_mode *match_mode, *t;
  1747. list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
  1748. if (drm_mode_equal(match_mode, mode)) {
  1749. list_del(&match_mode->head);
  1750. drm_mode_destroy(dev, match_mode);
  1751. found = 1;
  1752. break;
  1753. }
  1754. }
  1755. if (!found)
  1756. ret = -EINVAL;
  1757. return ret;
  1758. }
  1759. int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
  1760. {
  1761. struct drm_connector *connector;
  1762. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  1763. drm_mode_detachmode(dev, connector, mode);
  1764. }
  1765. return 0;
  1766. }
  1767. EXPORT_SYMBOL(drm_mode_detachmode_crtc);
  1768. /**
  1769. * drm_fb_attachmode - Attach a user mode to an connector
  1770. * @inode: inode from the ioctl
  1771. * @filp: file * from the ioctl
  1772. * @cmd: cmd from ioctl
  1773. * @arg: arg from ioctl
  1774. *
  1775. * This attaches a user specified mode to an connector.
  1776. * Called by the user via ioctl.
  1777. *
  1778. * RETURNS:
  1779. * Zero on success, errno on failure.
  1780. */
  1781. int drm_mode_attachmode_ioctl(struct drm_device *dev,
  1782. void *data, struct drm_file *file_priv)
  1783. {
  1784. struct drm_mode_mode_cmd *mode_cmd = data;
  1785. struct drm_connector *connector;
  1786. struct drm_display_mode *mode;
  1787. struct drm_mode_object *obj;
  1788. struct drm_mode_modeinfo *umode = &mode_cmd->mode;
  1789. int ret = 0;
  1790. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1791. return -EINVAL;
  1792. mutex_lock(&dev->mode_config.mutex);
  1793. obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
  1794. if (!obj) {
  1795. ret = -EINVAL;
  1796. goto out;
  1797. }
  1798. connector = obj_to_connector(obj);
  1799. mode = drm_mode_create(dev);
  1800. if (!mode) {
  1801. ret = -ENOMEM;
  1802. goto out;
  1803. }
  1804. drm_crtc_convert_umode(mode, umode);
  1805. ret = drm_mode_attachmode(dev, connector, mode);
  1806. out:
  1807. mutex_unlock(&dev->mode_config.mutex);
  1808. return ret;
  1809. }
  1810. /**
  1811. * drm_fb_detachmode - Detach a user specified mode from an connector
  1812. * @inode: inode from the ioctl
  1813. * @filp: file * from the ioctl
  1814. * @cmd: cmd from ioctl
  1815. * @arg: arg from ioctl
  1816. *
  1817. * Called by the user via ioctl.
  1818. *
  1819. * RETURNS:
  1820. * Zero on success, errno on failure.
  1821. */
  1822. int drm_mode_detachmode_ioctl(struct drm_device *dev,
  1823. void *data, struct drm_file *file_priv)
  1824. {
  1825. struct drm_mode_object *obj;
  1826. struct drm_mode_mode_cmd *mode_cmd = data;
  1827. struct drm_connector *connector;
  1828. struct drm_display_mode mode;
  1829. struct drm_mode_modeinfo *umode = &mode_cmd->mode;
  1830. int ret = 0;
  1831. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1832. return -EINVAL;
  1833. mutex_lock(&dev->mode_config.mutex);
  1834. obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
  1835. if (!obj) {
  1836. ret = -EINVAL;
  1837. goto out;
  1838. }
  1839. connector = obj_to_connector(obj);
  1840. drm_crtc_convert_umode(&mode, umode);
  1841. ret = drm_mode_detachmode(dev, connector, &mode);
  1842. out:
  1843. mutex_unlock(&dev->mode_config.mutex);
  1844. return ret;
  1845. }
  1846. struct drm_property *drm_property_create(struct drm_device *dev, int flags,
  1847. const char *name, int num_values)
  1848. {
  1849. struct drm_property *property = NULL;
  1850. property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
  1851. if (!property)
  1852. return NULL;
  1853. if (num_values) {
  1854. property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
  1855. if (!property->values)
  1856. goto fail;
  1857. }
  1858. drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
  1859. property->flags = flags;
  1860. property->num_values = num_values;
  1861. INIT_LIST_HEAD(&property->enum_blob_list);
  1862. if (name) {
  1863. strncpy(property->name, name, DRM_PROP_NAME_LEN);
  1864. property->name[DRM_PROP_NAME_LEN-1] = '\0';
  1865. }
  1866. list_add_tail(&property->head, &dev->mode_config.property_list);
  1867. return property;
  1868. fail:
  1869. kfree(property);
  1870. return NULL;
  1871. }
  1872. EXPORT_SYMBOL(drm_property_create);
  1873. int drm_property_add_enum(struct drm_property *property, int index,
  1874. uint64_t value, const char *name)
  1875. {
  1876. struct drm_property_enum *prop_enum;
  1877. if (!(property->flags & DRM_MODE_PROP_ENUM))
  1878. return -EINVAL;
  1879. if (!list_empty(&property->enum_blob_list)) {
  1880. list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
  1881. if (prop_enum->value == value) {
  1882. strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
  1883. prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
  1884. return 0;
  1885. }
  1886. }
  1887. }
  1888. prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
  1889. if (!prop_enum)
  1890. return -ENOMEM;
  1891. strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
  1892. prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
  1893. prop_enum->value = value;
  1894. property->values[index] = value;
  1895. list_add_tail(&prop_enum->head, &property->enum_blob_list);
  1896. return 0;
  1897. }
  1898. EXPORT_SYMBOL(drm_property_add_enum);
  1899. void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
  1900. {
  1901. struct drm_property_enum *prop_enum, *pt;
  1902. list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
  1903. list_del(&prop_enum->head);
  1904. kfree(prop_enum);
  1905. }
  1906. if (property->num_values)
  1907. kfree(property->values);
  1908. drm_mode_object_put(dev, &property->base);
  1909. list_del(&property->head);
  1910. kfree(property);
  1911. }
  1912. EXPORT_SYMBOL(drm_property_destroy);
  1913. int drm_connector_attach_property(struct drm_connector *connector,
  1914. struct drm_property *property, uint64_t init_val)
  1915. {
  1916. int i;
  1917. for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
  1918. if (connector->property_ids[i] == 0) {
  1919. connector->property_ids[i] = property->base.id;
  1920. connector->property_values[i] = init_val;
  1921. break;
  1922. }
  1923. }
  1924. if (i == DRM_CONNECTOR_MAX_PROPERTY)
  1925. return -EINVAL;
  1926. return 0;
  1927. }
  1928. EXPORT_SYMBOL(drm_connector_attach_property);
  1929. int drm_connector_property_set_value(struct drm_connector *connector,
  1930. struct drm_property *property, uint64_t value)
  1931. {
  1932. int i;
  1933. for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
  1934. if (connector->property_ids[i] == property->base.id) {
  1935. connector->property_values[i] = value;
  1936. break;
  1937. }
  1938. }
  1939. if (i == DRM_CONNECTOR_MAX_PROPERTY)
  1940. return -EINVAL;
  1941. return 0;
  1942. }
  1943. EXPORT_SYMBOL(drm_connector_property_set_value);
  1944. int drm_connector_property_get_value(struct drm_connector *connector,
  1945. struct drm_property *property, uint64_t *val)
  1946. {
  1947. int i;
  1948. for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
  1949. if (connector->property_ids[i] == property->base.id) {
  1950. *val = connector->property_values[i];
  1951. break;
  1952. }
  1953. }
  1954. if (i == DRM_CONNECTOR_MAX_PROPERTY)
  1955. return -EINVAL;
  1956. return 0;
  1957. }
  1958. EXPORT_SYMBOL(drm_connector_property_get_value);
  1959. int drm_mode_getproperty_ioctl(struct drm_device *dev,
  1960. void *data, struct drm_file *file_priv)
  1961. {
  1962. struct drm_mode_object *obj;
  1963. struct drm_mode_get_property *out_resp = data;
  1964. struct drm_property *property;
  1965. int enum_count = 0;
  1966. int blob_count = 0;
  1967. int value_count = 0;
  1968. int ret = 0, i;
  1969. int copied;
  1970. struct drm_property_enum *prop_enum;
  1971. struct drm_mode_property_enum __user *enum_ptr;
  1972. struct drm_property_blob *prop_blob;
  1973. uint32_t *blob_id_ptr;
  1974. uint64_t __user *values_ptr;
  1975. uint32_t __user *blob_length_ptr;
  1976. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1977. return -EINVAL;
  1978. mutex_lock(&dev->mode_config.mutex);
  1979. obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
  1980. if (!obj) {
  1981. ret = -EINVAL;
  1982. goto done;
  1983. }
  1984. property = obj_to_property(obj);
  1985. if (property->flags & DRM_MODE_PROP_ENUM) {
  1986. list_for_each_entry(prop_enum, &property->enum_blob_list, head)
  1987. enum_count++;
  1988. } else if (property->flags & DRM_MODE_PROP_BLOB) {
  1989. list_for_each_entry(prop_blob, &property->enum_blob_list, head)
  1990. blob_count++;
  1991. }
  1992. value_count = property->num_values;
  1993. strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
  1994. out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
  1995. out_resp->flags = property->flags;
  1996. if ((out_resp->count_values >= value_count) && value_count) {
  1997. values_ptr = (uint64_t *)(unsigned long)out_resp->values_ptr;
  1998. for (i = 0; i < value_count; i++) {
  1999. if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
  2000. ret = -EFAULT;
  2001. goto done;
  2002. }
  2003. }
  2004. }
  2005. out_resp->count_values = value_count;
  2006. if (property->flags & DRM_MODE_PROP_ENUM) {
  2007. if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
  2008. copied = 0;
  2009. enum_ptr = (struct drm_mode_property_enum *)(unsigned long)out_resp->enum_blob_ptr;
  2010. list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
  2011. if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
  2012. ret = -EFAULT;
  2013. goto done;
  2014. }
  2015. if (copy_to_user(&enum_ptr[copied].name,
  2016. &prop_enum->name, DRM_PROP_NAME_LEN)) {
  2017. ret = -EFAULT;
  2018. goto done;
  2019. }
  2020. copied++;
  2021. }
  2022. }
  2023. out_resp->count_enum_blobs = enum_count;
  2024. }
  2025. if (property->flags & DRM_MODE_PROP_BLOB) {
  2026. if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
  2027. copied = 0;
  2028. blob_id_ptr = (uint32_t *)(unsigned long)out_resp->enum_blob_ptr;
  2029. blob_length_ptr = (uint32_t *)(unsigned long)out_resp->values_ptr;
  2030. list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
  2031. if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
  2032. ret = -EFAULT;
  2033. goto done;
  2034. }
  2035. if (put_user(prop_blob->length, blob_length_ptr + copied)) {
  2036. ret = -EFAULT;
  2037. goto done;
  2038. }
  2039. copied++;
  2040. }
  2041. }
  2042. out_resp->count_enum_blobs = blob_count;
  2043. }
  2044. done:
  2045. mutex_unlock(&dev->mode_config.mutex);
  2046. return ret;
  2047. }
  2048. static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
  2049. void *data)
  2050. {
  2051. struct drm_property_blob *blob;
  2052. if (!length || !data)
  2053. return NULL;
  2054. blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
  2055. if (!blob)
  2056. return NULL;
  2057. blob->data = (void *)((char *)blob + sizeof(struct drm_property_blob));
  2058. blob->length = length;
  2059. memcpy(blob->data, data, length);
  2060. drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
  2061. list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
  2062. return blob;
  2063. }
  2064. static void drm_property_destroy_blob(struct drm_device *dev,
  2065. struct drm_property_blob *blob)
  2066. {
  2067. drm_mode_object_put(dev, &blob->base);
  2068. list_del(&blob->head);
  2069. kfree(blob);
  2070. }
  2071. int drm_mode_getblob_ioctl(struct drm_device *dev,
  2072. void *data, struct drm_file *file_priv)
  2073. {
  2074. struct drm_mode_object *obj;
  2075. struct drm_mode_get_blob *out_resp = data;
  2076. struct drm_property_blob *blob;
  2077. int ret = 0;
  2078. void *blob_ptr;
  2079. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2080. return -EINVAL;
  2081. mutex_lock(&dev->mode_config.mutex);
  2082. obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
  2083. if (!obj) {
  2084. ret = -EINVAL;
  2085. goto done;
  2086. }
  2087. blob = obj_to_blob(obj);
  2088. if (out_resp->length == blob->length) {
  2089. blob_ptr = (void *)(unsigned long)out_resp->data;
  2090. if (copy_to_user(blob_ptr, blob->data, blob->length)){
  2091. ret = -EFAULT;
  2092. goto done;
  2093. }
  2094. }
  2095. out_resp->length = blob->length;
  2096. done:
  2097. mutex_unlock(&dev->mode_config.mutex);
  2098. return ret;
  2099. }
  2100. int drm_mode_connector_update_edid_property(struct drm_connector *connector,
  2101. struct edid *edid)
  2102. {
  2103. struct drm_device *dev = connector->dev;
  2104. int ret = 0, size;
  2105. if (connector->edid_blob_ptr)
  2106. drm_property_destroy_blob(dev, connector->edid_blob_ptr);
  2107. /* Delete edid, when there is none. */
  2108. if (!edid) {
  2109. connector->edid_blob_ptr = NULL;
  2110. ret = drm_connector_property_set_value(connector, dev->mode_config.edid_property, 0);
  2111. return ret;
  2112. }
  2113. size = EDID_LENGTH * (1 + edid->extensions);
  2114. connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
  2115. size, edid);
  2116. ret = drm_connector_property_set_value(connector,
  2117. dev->mode_config.edid_property,
  2118. connector->edid_blob_ptr->base.id);
  2119. return ret;
  2120. }
  2121. EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
  2122. int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
  2123. void *data, struct drm_file *file_priv)
  2124. {
  2125. struct drm_mode_connector_set_property *out_resp = data;
  2126. struct drm_mode_object *obj;
  2127. struct drm_property *property;
  2128. struct drm_connector *connector;
  2129. int ret = -EINVAL;
  2130. int i;
  2131. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2132. return -EINVAL;
  2133. mutex_lock(&dev->mode_config.mutex);
  2134. obj = drm_mode_object_find(dev, out_resp->connector_id, DRM_MODE_OBJECT_CONNECTOR);
  2135. if (!obj) {
  2136. goto out;
  2137. }
  2138. connector = obj_to_connector(obj);
  2139. for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
  2140. if (connector->property_ids[i] == out_resp->prop_id)
  2141. break;
  2142. }
  2143. if (i == DRM_CONNECTOR_MAX_PROPERTY) {
  2144. goto out;
  2145. }
  2146. obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
  2147. if (!obj) {
  2148. goto out;
  2149. }
  2150. property = obj_to_property(obj);
  2151. if (property->flags & DRM_MODE_PROP_IMMUTABLE)
  2152. goto out;
  2153. if (property->flags & DRM_MODE_PROP_RANGE) {
  2154. if (out_resp->value < property->values[0])
  2155. goto out;
  2156. if (out_resp->value > property->values[1])
  2157. goto out;
  2158. } else {
  2159. int found = 0;
  2160. for (i = 0; i < property->num_values; i++) {
  2161. if (property->values[i] == out_resp->value) {
  2162. found = 1;
  2163. break;
  2164. }
  2165. }
  2166. if (!found) {
  2167. goto out;
  2168. }
  2169. }
  2170. /* Do DPMS ourselves */
  2171. if (property == connector->dev->mode_config.dpms_property) {
  2172. if (connector->funcs->dpms)
  2173. (*connector->funcs->dpms)(connector, (int) out_resp->value);
  2174. ret = 0;
  2175. } else if (connector->funcs->set_property)
  2176. ret = connector->funcs->set_property(connector, property, out_resp->value);
  2177. /* store the property value if successful */
  2178. if (!ret)
  2179. drm_connector_property_set_value(connector, property, out_resp->value);
  2180. out:
  2181. mutex_unlock(&dev->mode_config.mutex);
  2182. return ret;
  2183. }
  2184. int drm_mode_connector_attach_encoder(struct drm_connector *connector,
  2185. struct drm_encoder *encoder)
  2186. {
  2187. int i;
  2188. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  2189. if (connector->encoder_ids[i] == 0) {
  2190. connector->encoder_ids[i] = encoder->base.id;
  2191. return 0;
  2192. }
  2193. }
  2194. return -ENOMEM;
  2195. }
  2196. EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
  2197. void drm_mode_connector_detach_encoder(struct drm_connector *connector,
  2198. struct drm_encoder *encoder)
  2199. {
  2200. int i;
  2201. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  2202. if (connector->encoder_ids[i] == encoder->base.id) {
  2203. connector->encoder_ids[i] = 0;
  2204. if (connector->encoder == encoder)
  2205. connector->encoder = NULL;
  2206. break;
  2207. }
  2208. }
  2209. }
  2210. EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
  2211. bool drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
  2212. int gamma_size)
  2213. {
  2214. crtc->gamma_size = gamma_size;
  2215. crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
  2216. if (!crtc->gamma_store) {
  2217. crtc->gamma_size = 0;
  2218. return false;
  2219. }
  2220. return true;
  2221. }
  2222. EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
  2223. int drm_mode_gamma_set_ioctl(struct drm_device *dev,
  2224. void *data, struct drm_file *file_priv)
  2225. {
  2226. struct drm_mode_crtc_lut *crtc_lut = data;
  2227. struct drm_mode_object *obj;
  2228. struct drm_crtc *crtc;
  2229. void *r_base, *g_base, *b_base;
  2230. int size;
  2231. int ret = 0;
  2232. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2233. return -EINVAL;
  2234. mutex_lock(&dev->mode_config.mutex);
  2235. obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
  2236. if (!obj) {
  2237. ret = -EINVAL;
  2238. goto out;
  2239. }
  2240. crtc = obj_to_crtc(obj);
  2241. /* memcpy into gamma store */
  2242. if (crtc_lut->gamma_size != crtc->gamma_size) {
  2243. ret = -EINVAL;
  2244. goto out;
  2245. }
  2246. size = crtc_lut->gamma_size * (sizeof(uint16_t));
  2247. r_base = crtc->gamma_store;
  2248. if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
  2249. ret = -EFAULT;
  2250. goto out;
  2251. }
  2252. g_base = r_base + size;
  2253. if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
  2254. ret = -EFAULT;
  2255. goto out;
  2256. }
  2257. b_base = g_base + size;
  2258. if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
  2259. ret = -EFAULT;
  2260. goto out;
  2261. }
  2262. crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
  2263. out:
  2264. mutex_unlock(&dev->mode_config.mutex);
  2265. return ret;
  2266. }
  2267. int drm_mode_gamma_get_ioctl(struct drm_device *dev,
  2268. void *data, struct drm_file *file_priv)
  2269. {
  2270. struct drm_mode_crtc_lut *crtc_lut = data;
  2271. struct drm_mode_object *obj;
  2272. struct drm_crtc *crtc;
  2273. void *r_base, *g_base, *b_base;
  2274. int size;
  2275. int ret = 0;
  2276. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2277. return -EINVAL;
  2278. mutex_lock(&dev->mode_config.mutex);
  2279. obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
  2280. if (!obj) {
  2281. ret = -EINVAL;
  2282. goto out;
  2283. }
  2284. crtc = obj_to_crtc(obj);
  2285. /* memcpy into gamma store */
  2286. if (crtc_lut->gamma_size != crtc->gamma_size) {
  2287. ret = -EINVAL;
  2288. goto out;
  2289. }
  2290. size = crtc_lut->gamma_size * (sizeof(uint16_t));
  2291. r_base = crtc->gamma_store;
  2292. if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
  2293. ret = -EFAULT;
  2294. goto out;
  2295. }
  2296. g_base = r_base + size;
  2297. if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
  2298. ret = -EFAULT;
  2299. goto out;
  2300. }
  2301. b_base = g_base + size;
  2302. if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
  2303. ret = -EFAULT;
  2304. goto out;
  2305. }
  2306. out:
  2307. mutex_unlock(&dev->mode_config.mutex);
  2308. return ret;
  2309. }
  2310. int drm_mode_page_flip_ioctl(struct drm_device *dev,
  2311. void *data, struct drm_file *file_priv)
  2312. {
  2313. struct drm_mode_crtc_page_flip *page_flip = data;
  2314. struct drm_mode_object *obj;
  2315. struct drm_crtc *crtc;
  2316. struct drm_framebuffer *fb;
  2317. struct drm_pending_vblank_event *e = NULL;
  2318. unsigned long flags;
  2319. int ret = -EINVAL;
  2320. if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
  2321. page_flip->reserved != 0)
  2322. return -EINVAL;
  2323. mutex_lock(&dev->mode_config.mutex);
  2324. obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
  2325. if (!obj)
  2326. goto out;
  2327. crtc = obj_to_crtc(obj);
  2328. if (crtc->fb == NULL) {
  2329. /* The framebuffer is currently unbound, presumably
  2330. * due to a hotplug event, that userspace has not
  2331. * yet discovered.
  2332. */
  2333. ret = -EBUSY;
  2334. goto out;
  2335. }
  2336. if (crtc->funcs->page_flip == NULL)
  2337. goto out;
  2338. obj = drm_mode_object_find(dev, page_flip->fb_id, DRM_MODE_OBJECT_FB);
  2339. if (!obj)
  2340. goto out;
  2341. fb = obj_to_fb(obj);
  2342. if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
  2343. ret = -ENOMEM;
  2344. spin_lock_irqsave(&dev->event_lock, flags);
  2345. if (file_priv->event_space < sizeof e->event) {
  2346. spin_unlock_irqrestore(&dev->event_lock, flags);
  2347. goto out;
  2348. }
  2349. file_priv->event_space -= sizeof e->event;
  2350. spin_unlock_irqrestore(&dev->event_lock, flags);
  2351. e = kzalloc(sizeof *e, GFP_KERNEL);
  2352. if (e == NULL) {
  2353. spin_lock_irqsave(&dev->event_lock, flags);
  2354. file_priv->event_space += sizeof e->event;
  2355. spin_unlock_irqrestore(&dev->event_lock, flags);
  2356. goto out;
  2357. }
  2358. e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
  2359. e->event.base.length = sizeof e->event;
  2360. e->event.user_data = page_flip->user_data;
  2361. e->base.event = &e->event.base;
  2362. e->base.file_priv = file_priv;
  2363. e->base.destroy =
  2364. (void (*) (struct drm_pending_event *)) kfree;
  2365. }
  2366. ret = crtc->funcs->page_flip(crtc, fb, e);
  2367. if (ret) {
  2368. spin_lock_irqsave(&dev->event_lock, flags);
  2369. file_priv->event_space += sizeof e->event;
  2370. spin_unlock_irqrestore(&dev->event_lock, flags);
  2371. kfree(e);
  2372. }
  2373. out:
  2374. mutex_unlock(&dev->mode_config.mutex);
  2375. return ret;
  2376. }
  2377. void drm_mode_config_reset(struct drm_device *dev)
  2378. {
  2379. struct drm_crtc *crtc;
  2380. struct drm_encoder *encoder;
  2381. struct drm_connector *connector;
  2382. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
  2383. if (crtc->funcs->reset)
  2384. crtc->funcs->reset(crtc);
  2385. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  2386. if (encoder->funcs->reset)
  2387. encoder->funcs->reset(encoder);
  2388. list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  2389. if (connector->funcs->reset)
  2390. connector->funcs->reset(connector);
  2391. }
  2392. EXPORT_SYMBOL(drm_mode_config_reset);
  2393. int drm_mode_create_dumb_ioctl(struct drm_device *dev,
  2394. void *data, struct drm_file *file_priv)
  2395. {
  2396. struct drm_mode_create_dumb *args = data;
  2397. if (!dev->driver->dumb_create)
  2398. return -ENOSYS;
  2399. return dev->driver->dumb_create(file_priv, dev, args);
  2400. }
  2401. int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
  2402. void *data, struct drm_file *file_priv)
  2403. {
  2404. struct drm_mode_map_dumb *args = data;
  2405. /* call driver ioctl to get mmap offset */
  2406. if (!dev->driver->dumb_map_offset)
  2407. return -ENOSYS;
  2408. return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
  2409. }
  2410. int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
  2411. void *data, struct drm_file *file_priv)
  2412. {
  2413. struct drm_mode_destroy_dumb *args = data;
  2414. if (!dev->driver->dumb_destroy)
  2415. return -ENOSYS;
  2416. return dev->driver->dumb_destroy(file_priv, dev, args->handle);
  2417. }