drm_crtc.c 62 KB

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