drm_crtc.c 62 KB

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