drm_crtc.c 62 KB

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