drm_crtc.c 64 KB

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