drm_crtc.c 64 KB

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