drm_crtc.c 64 KB

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