drm_crtc.c 62 KB

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