drm_connector.c 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929
  1. /*
  2. * Copyright (c) 2016 Intel Corporation
  3. *
  4. * Permission to use, copy, modify, distribute, and sell this software and its
  5. * documentation for any purpose is hereby granted without fee, provided that
  6. * the above copyright notice appear in all copies and that both that copyright
  7. * notice and this permission notice appear in supporting documentation, and
  8. * that the name of the copyright holders not be used in advertising or
  9. * publicity pertaining to distribution of the software without specific,
  10. * written prior permission. The copyright holders make no representations
  11. * about the suitability of this software for any purpose. It is provided "as
  12. * is" without express or implied warranty.
  13. *
  14. * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  15. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  16. * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  17. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  18. * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  19. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  20. * OF THIS SOFTWARE.
  21. */
  22. #include <drm/drmP.h>
  23. #include <drm/drm_connector.h>
  24. #include <drm/drm_edid.h>
  25. #include <drm/drm_encoder.h>
  26. #include <drm/drm_utils.h>
  27. #include "drm_crtc_internal.h"
  28. #include "drm_internal.h"
  29. /**
  30. * DOC: overview
  31. *
  32. * In DRM connectors are the general abstraction for display sinks, and include
  33. * als fixed panels or anything else that can display pixels in some form. As
  34. * opposed to all other KMS objects representing hardware (like CRTC, encoder or
  35. * plane abstractions) connectors can be hotplugged and unplugged at runtime.
  36. * Hence they are reference-counted using drm_connector_get() and
  37. * drm_connector_put().
  38. *
  39. * KMS driver must create, initialize, register and attach at a &struct
  40. * drm_connector for each such sink. The instance is created as other KMS
  41. * objects and initialized by setting the following fields. The connector is
  42. * initialized with a call to drm_connector_init() with a pointer to the
  43. * &struct drm_connector_funcs and a connector type, and then exposed to
  44. * userspace with a call to drm_connector_register().
  45. *
  46. * Connectors must be attached to an encoder to be used. For devices that map
  47. * connectors to encoders 1:1, the connector should be attached at
  48. * initialization time with a call to drm_connector_attach_encoder(). The
  49. * driver must also set the &drm_connector.encoder field to point to the
  50. * attached encoder.
  51. *
  52. * For connectors which are not fixed (like built-in panels) the driver needs to
  53. * support hotplug notifications. The simplest way to do that is by using the
  54. * probe helpers, see drm_kms_helper_poll_init() for connectors which don't have
  55. * hardware support for hotplug interrupts. Connectors with hardware hotplug
  56. * support can instead use e.g. drm_helper_hpd_irq_event().
  57. */
  58. struct drm_conn_prop_enum_list {
  59. int type;
  60. const char *name;
  61. struct ida ida;
  62. };
  63. /*
  64. * Connector and encoder types.
  65. */
  66. static struct drm_conn_prop_enum_list drm_connector_enum_list[] = {
  67. { DRM_MODE_CONNECTOR_Unknown, "Unknown" },
  68. { DRM_MODE_CONNECTOR_VGA, "VGA" },
  69. { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
  70. { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
  71. { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
  72. { DRM_MODE_CONNECTOR_Composite, "Composite" },
  73. { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
  74. { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
  75. { DRM_MODE_CONNECTOR_Component, "Component" },
  76. { DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
  77. { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
  78. { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
  79. { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
  80. { DRM_MODE_CONNECTOR_TV, "TV" },
  81. { DRM_MODE_CONNECTOR_eDP, "eDP" },
  82. { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
  83. { DRM_MODE_CONNECTOR_DSI, "DSI" },
  84. { DRM_MODE_CONNECTOR_DPI, "DPI" },
  85. { DRM_MODE_CONNECTOR_WRITEBACK, "Writeback" },
  86. };
  87. void drm_connector_ida_init(void)
  88. {
  89. int i;
  90. for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
  91. ida_init(&drm_connector_enum_list[i].ida);
  92. }
  93. void drm_connector_ida_destroy(void)
  94. {
  95. int i;
  96. for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
  97. ida_destroy(&drm_connector_enum_list[i].ida);
  98. }
  99. /**
  100. * drm_connector_get_cmdline_mode - reads the user's cmdline mode
  101. * @connector: connector to quwery
  102. *
  103. * The kernel supports per-connector configuration of its consoles through
  104. * use of the video= parameter. This function parses that option and
  105. * extracts the user's specified mode (or enable/disable status) for a
  106. * particular connector. This is typically only used during the early fbdev
  107. * setup.
  108. */
  109. static void drm_connector_get_cmdline_mode(struct drm_connector *connector)
  110. {
  111. struct drm_cmdline_mode *mode = &connector->cmdline_mode;
  112. char *option = NULL;
  113. if (fb_get_options(connector->name, &option))
  114. return;
  115. if (!drm_mode_parse_command_line_for_connector(option,
  116. connector,
  117. mode))
  118. return;
  119. if (mode->force) {
  120. DRM_INFO("forcing %s connector %s\n", connector->name,
  121. drm_get_connector_force_name(mode->force));
  122. connector->force = mode->force;
  123. }
  124. DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
  125. connector->name,
  126. mode->xres, mode->yres,
  127. mode->refresh_specified ? mode->refresh : 60,
  128. mode->rb ? " reduced blanking" : "",
  129. mode->margins ? " with margins" : "",
  130. mode->interlace ? " interlaced" : "");
  131. }
  132. static void drm_connector_free(struct kref *kref)
  133. {
  134. struct drm_connector *connector =
  135. container_of(kref, struct drm_connector, base.refcount);
  136. struct drm_device *dev = connector->dev;
  137. drm_mode_object_unregister(dev, &connector->base);
  138. connector->funcs->destroy(connector);
  139. }
  140. void drm_connector_free_work_fn(struct work_struct *work)
  141. {
  142. struct drm_connector *connector, *n;
  143. struct drm_device *dev =
  144. container_of(work, struct drm_device, mode_config.connector_free_work);
  145. struct drm_mode_config *config = &dev->mode_config;
  146. unsigned long flags;
  147. struct llist_node *freed;
  148. spin_lock_irqsave(&config->connector_list_lock, flags);
  149. freed = llist_del_all(&config->connector_free_list);
  150. spin_unlock_irqrestore(&config->connector_list_lock, flags);
  151. llist_for_each_entry_safe(connector, n, freed, free_node) {
  152. drm_mode_object_unregister(dev, &connector->base);
  153. connector->funcs->destroy(connector);
  154. }
  155. }
  156. /**
  157. * drm_connector_init - Init a preallocated connector
  158. * @dev: DRM device
  159. * @connector: the connector to init
  160. * @funcs: callbacks for this connector
  161. * @connector_type: user visible type of the connector
  162. *
  163. * Initialises a preallocated connector. Connectors should be
  164. * subclassed as part of driver connector objects.
  165. *
  166. * Returns:
  167. * Zero on success, error code on failure.
  168. */
  169. int drm_connector_init(struct drm_device *dev,
  170. struct drm_connector *connector,
  171. const struct drm_connector_funcs *funcs,
  172. int connector_type)
  173. {
  174. struct drm_mode_config *config = &dev->mode_config;
  175. int ret;
  176. struct ida *connector_ida =
  177. &drm_connector_enum_list[connector_type].ida;
  178. WARN_ON(drm_drv_uses_atomic_modeset(dev) &&
  179. (!funcs->atomic_destroy_state ||
  180. !funcs->atomic_duplicate_state));
  181. ret = __drm_mode_object_add(dev, &connector->base,
  182. DRM_MODE_OBJECT_CONNECTOR,
  183. false, drm_connector_free);
  184. if (ret)
  185. return ret;
  186. connector->base.properties = &connector->properties;
  187. connector->dev = dev;
  188. connector->funcs = funcs;
  189. /* connector index is used with 32bit bitmasks */
  190. ret = ida_simple_get(&config->connector_ida, 0, 32, GFP_KERNEL);
  191. if (ret < 0) {
  192. DRM_DEBUG_KMS("Failed to allocate %s connector index: %d\n",
  193. drm_connector_enum_list[connector_type].name,
  194. ret);
  195. goto out_put;
  196. }
  197. connector->index = ret;
  198. ret = 0;
  199. connector->connector_type = connector_type;
  200. connector->connector_type_id =
  201. ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
  202. if (connector->connector_type_id < 0) {
  203. ret = connector->connector_type_id;
  204. goto out_put_id;
  205. }
  206. connector->name =
  207. kasprintf(GFP_KERNEL, "%s-%d",
  208. drm_connector_enum_list[connector_type].name,
  209. connector->connector_type_id);
  210. if (!connector->name) {
  211. ret = -ENOMEM;
  212. goto out_put_type_id;
  213. }
  214. INIT_LIST_HEAD(&connector->probed_modes);
  215. INIT_LIST_HEAD(&connector->modes);
  216. mutex_init(&connector->mutex);
  217. connector->edid_blob_ptr = NULL;
  218. connector->status = connector_status_unknown;
  219. connector->display_info.panel_orientation =
  220. DRM_MODE_PANEL_ORIENTATION_UNKNOWN;
  221. drm_connector_get_cmdline_mode(connector);
  222. /* We should add connectors at the end to avoid upsetting the connector
  223. * index too much. */
  224. spin_lock_irq(&config->connector_list_lock);
  225. list_add_tail(&connector->head, &config->connector_list);
  226. config->num_connector++;
  227. spin_unlock_irq(&config->connector_list_lock);
  228. if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL &&
  229. connector_type != DRM_MODE_CONNECTOR_WRITEBACK)
  230. drm_object_attach_property(&connector->base,
  231. config->edid_property,
  232. 0);
  233. drm_object_attach_property(&connector->base,
  234. config->dpms_property, 0);
  235. drm_object_attach_property(&connector->base,
  236. config->link_status_property,
  237. 0);
  238. drm_object_attach_property(&connector->base,
  239. config->non_desktop_property,
  240. 0);
  241. if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
  242. drm_object_attach_property(&connector->base, config->prop_crtc_id, 0);
  243. }
  244. connector->debugfs_entry = NULL;
  245. out_put_type_id:
  246. if (ret)
  247. ida_simple_remove(connector_ida, connector->connector_type_id);
  248. out_put_id:
  249. if (ret)
  250. ida_simple_remove(&config->connector_ida, connector->index);
  251. out_put:
  252. if (ret)
  253. drm_mode_object_unregister(dev, &connector->base);
  254. return ret;
  255. }
  256. EXPORT_SYMBOL(drm_connector_init);
  257. /**
  258. * drm_connector_attach_encoder - attach a connector to an encoder
  259. * @connector: connector to attach
  260. * @encoder: encoder to attach @connector to
  261. *
  262. * This function links up a connector to an encoder. Note that the routing
  263. * restrictions between encoders and crtcs are exposed to userspace through the
  264. * possible_clones and possible_crtcs bitmasks.
  265. *
  266. * Returns:
  267. * Zero on success, negative errno on failure.
  268. */
  269. int drm_connector_attach_encoder(struct drm_connector *connector,
  270. struct drm_encoder *encoder)
  271. {
  272. int i;
  273. /*
  274. * In the past, drivers have attempted to model the static association
  275. * of connector to encoder in simple connector/encoder devices using a
  276. * direct assignment of connector->encoder = encoder. This connection
  277. * is a logical one and the responsibility of the core, so drivers are
  278. * expected not to mess with this.
  279. *
  280. * Note that the error return should've been enough here, but a large
  281. * majority of drivers ignores the return value, so add in a big WARN
  282. * to get people's attention.
  283. */
  284. if (WARN_ON(connector->encoder))
  285. return -EINVAL;
  286. for (i = 0; i < ARRAY_SIZE(connector->encoder_ids); i++) {
  287. if (connector->encoder_ids[i] == 0) {
  288. connector->encoder_ids[i] = encoder->base.id;
  289. return 0;
  290. }
  291. }
  292. return -ENOMEM;
  293. }
  294. EXPORT_SYMBOL(drm_connector_attach_encoder);
  295. /**
  296. * drm_connector_has_possible_encoder - check if the connector and encoder are assosicated with each other
  297. * @connector: the connector
  298. * @encoder: the encoder
  299. *
  300. * Returns:
  301. * True if @encoder is one of the possible encoders for @connector.
  302. */
  303. bool drm_connector_has_possible_encoder(struct drm_connector *connector,
  304. struct drm_encoder *encoder)
  305. {
  306. struct drm_encoder *enc;
  307. int i;
  308. drm_connector_for_each_possible_encoder(connector, enc, i) {
  309. if (enc == encoder)
  310. return true;
  311. }
  312. return false;
  313. }
  314. EXPORT_SYMBOL(drm_connector_has_possible_encoder);
  315. static void drm_mode_remove(struct drm_connector *connector,
  316. struct drm_display_mode *mode)
  317. {
  318. list_del(&mode->head);
  319. drm_mode_destroy(connector->dev, mode);
  320. }
  321. /**
  322. * drm_connector_cleanup - cleans up an initialised connector
  323. * @connector: connector to cleanup
  324. *
  325. * Cleans up the connector but doesn't free the object.
  326. */
  327. void drm_connector_cleanup(struct drm_connector *connector)
  328. {
  329. struct drm_device *dev = connector->dev;
  330. struct drm_display_mode *mode, *t;
  331. /* The connector should have been removed from userspace long before
  332. * it is finally destroyed.
  333. */
  334. if (WARN_ON(connector->registered))
  335. drm_connector_unregister(connector);
  336. if (connector->tile_group) {
  337. drm_mode_put_tile_group(dev, connector->tile_group);
  338. connector->tile_group = NULL;
  339. }
  340. list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
  341. drm_mode_remove(connector, mode);
  342. list_for_each_entry_safe(mode, t, &connector->modes, head)
  343. drm_mode_remove(connector, mode);
  344. ida_simple_remove(&drm_connector_enum_list[connector->connector_type].ida,
  345. connector->connector_type_id);
  346. ida_simple_remove(&dev->mode_config.connector_ida,
  347. connector->index);
  348. kfree(connector->display_info.bus_formats);
  349. drm_mode_object_unregister(dev, &connector->base);
  350. kfree(connector->name);
  351. connector->name = NULL;
  352. spin_lock_irq(&dev->mode_config.connector_list_lock);
  353. list_del(&connector->head);
  354. dev->mode_config.num_connector--;
  355. spin_unlock_irq(&dev->mode_config.connector_list_lock);
  356. WARN_ON(connector->state && !connector->funcs->atomic_destroy_state);
  357. if (connector->state && connector->funcs->atomic_destroy_state)
  358. connector->funcs->atomic_destroy_state(connector,
  359. connector->state);
  360. mutex_destroy(&connector->mutex);
  361. memset(connector, 0, sizeof(*connector));
  362. }
  363. EXPORT_SYMBOL(drm_connector_cleanup);
  364. /**
  365. * drm_connector_register - register a connector
  366. * @connector: the connector to register
  367. *
  368. * Register userspace interfaces for a connector
  369. *
  370. * Returns:
  371. * Zero on success, error code on failure.
  372. */
  373. int drm_connector_register(struct drm_connector *connector)
  374. {
  375. int ret = 0;
  376. if (!connector->dev->registered)
  377. return 0;
  378. mutex_lock(&connector->mutex);
  379. if (connector->registered)
  380. goto unlock;
  381. ret = drm_sysfs_connector_add(connector);
  382. if (ret)
  383. goto unlock;
  384. ret = drm_debugfs_connector_add(connector);
  385. if (ret) {
  386. goto err_sysfs;
  387. }
  388. if (connector->funcs->late_register) {
  389. ret = connector->funcs->late_register(connector);
  390. if (ret)
  391. goto err_debugfs;
  392. }
  393. drm_mode_object_register(connector->dev, &connector->base);
  394. connector->registered = true;
  395. goto unlock;
  396. err_debugfs:
  397. drm_debugfs_connector_remove(connector);
  398. err_sysfs:
  399. drm_sysfs_connector_remove(connector);
  400. unlock:
  401. mutex_unlock(&connector->mutex);
  402. return ret;
  403. }
  404. EXPORT_SYMBOL(drm_connector_register);
  405. /**
  406. * drm_connector_unregister - unregister a connector
  407. * @connector: the connector to unregister
  408. *
  409. * Unregister userspace interfaces for a connector
  410. */
  411. void drm_connector_unregister(struct drm_connector *connector)
  412. {
  413. mutex_lock(&connector->mutex);
  414. if (!connector->registered) {
  415. mutex_unlock(&connector->mutex);
  416. return;
  417. }
  418. if (connector->funcs->early_unregister)
  419. connector->funcs->early_unregister(connector);
  420. drm_sysfs_connector_remove(connector);
  421. drm_debugfs_connector_remove(connector);
  422. connector->registered = false;
  423. mutex_unlock(&connector->mutex);
  424. }
  425. EXPORT_SYMBOL(drm_connector_unregister);
  426. void drm_connector_unregister_all(struct drm_device *dev)
  427. {
  428. struct drm_connector *connector;
  429. struct drm_connector_list_iter conn_iter;
  430. drm_connector_list_iter_begin(dev, &conn_iter);
  431. drm_for_each_connector_iter(connector, &conn_iter)
  432. drm_connector_unregister(connector);
  433. drm_connector_list_iter_end(&conn_iter);
  434. }
  435. int drm_connector_register_all(struct drm_device *dev)
  436. {
  437. struct drm_connector *connector;
  438. struct drm_connector_list_iter conn_iter;
  439. int ret = 0;
  440. drm_connector_list_iter_begin(dev, &conn_iter);
  441. drm_for_each_connector_iter(connector, &conn_iter) {
  442. ret = drm_connector_register(connector);
  443. if (ret)
  444. break;
  445. }
  446. drm_connector_list_iter_end(&conn_iter);
  447. if (ret)
  448. drm_connector_unregister_all(dev);
  449. return ret;
  450. }
  451. /**
  452. * drm_get_connector_status_name - return a string for connector status
  453. * @status: connector status to compute name of
  454. *
  455. * In contrast to the other drm_get_*_name functions this one here returns a
  456. * const pointer and hence is threadsafe.
  457. */
  458. const char *drm_get_connector_status_name(enum drm_connector_status status)
  459. {
  460. if (status == connector_status_connected)
  461. return "connected";
  462. else if (status == connector_status_disconnected)
  463. return "disconnected";
  464. else
  465. return "unknown";
  466. }
  467. EXPORT_SYMBOL(drm_get_connector_status_name);
  468. /**
  469. * drm_get_connector_force_name - return a string for connector force
  470. * @force: connector force to get name of
  471. *
  472. * Returns: const pointer to name.
  473. */
  474. const char *drm_get_connector_force_name(enum drm_connector_force force)
  475. {
  476. switch (force) {
  477. case DRM_FORCE_UNSPECIFIED:
  478. return "unspecified";
  479. case DRM_FORCE_OFF:
  480. return "off";
  481. case DRM_FORCE_ON:
  482. return "on";
  483. case DRM_FORCE_ON_DIGITAL:
  484. return "digital";
  485. default:
  486. return "unknown";
  487. }
  488. }
  489. #ifdef CONFIG_LOCKDEP
  490. static struct lockdep_map connector_list_iter_dep_map = {
  491. .name = "drm_connector_list_iter"
  492. };
  493. #endif
  494. /**
  495. * drm_connector_list_iter_begin - initialize a connector_list iterator
  496. * @dev: DRM device
  497. * @iter: connector_list iterator
  498. *
  499. * Sets @iter up to walk the &drm_mode_config.connector_list of @dev. @iter
  500. * must always be cleaned up again by calling drm_connector_list_iter_end().
  501. * Iteration itself happens using drm_connector_list_iter_next() or
  502. * drm_for_each_connector_iter().
  503. */
  504. void drm_connector_list_iter_begin(struct drm_device *dev,
  505. struct drm_connector_list_iter *iter)
  506. {
  507. iter->dev = dev;
  508. iter->conn = NULL;
  509. lock_acquire_shared_recursive(&connector_list_iter_dep_map, 0, 1, NULL, _RET_IP_);
  510. }
  511. EXPORT_SYMBOL(drm_connector_list_iter_begin);
  512. /*
  513. * Extra-safe connector put function that works in any context. Should only be
  514. * used from the connector_iter functions, where we never really expect to
  515. * actually release the connector when dropping our final reference.
  516. */
  517. static void
  518. __drm_connector_put_safe(struct drm_connector *conn)
  519. {
  520. struct drm_mode_config *config = &conn->dev->mode_config;
  521. lockdep_assert_held(&config->connector_list_lock);
  522. if (!refcount_dec_and_test(&conn->base.refcount.refcount))
  523. return;
  524. llist_add(&conn->free_node, &config->connector_free_list);
  525. schedule_work(&config->connector_free_work);
  526. }
  527. /**
  528. * drm_connector_list_iter_next - return next connector
  529. * @iter: connector_list iterator
  530. *
  531. * Returns the next connector for @iter, or NULL when the list walk has
  532. * completed.
  533. */
  534. struct drm_connector *
  535. drm_connector_list_iter_next(struct drm_connector_list_iter *iter)
  536. {
  537. struct drm_connector *old_conn = iter->conn;
  538. struct drm_mode_config *config = &iter->dev->mode_config;
  539. struct list_head *lhead;
  540. unsigned long flags;
  541. spin_lock_irqsave(&config->connector_list_lock, flags);
  542. lhead = old_conn ? &old_conn->head : &config->connector_list;
  543. do {
  544. if (lhead->next == &config->connector_list) {
  545. iter->conn = NULL;
  546. break;
  547. }
  548. lhead = lhead->next;
  549. iter->conn = list_entry(lhead, struct drm_connector, head);
  550. /* loop until it's not a zombie connector */
  551. } while (!kref_get_unless_zero(&iter->conn->base.refcount));
  552. if (old_conn)
  553. __drm_connector_put_safe(old_conn);
  554. spin_unlock_irqrestore(&config->connector_list_lock, flags);
  555. return iter->conn;
  556. }
  557. EXPORT_SYMBOL(drm_connector_list_iter_next);
  558. /**
  559. * drm_connector_list_iter_end - tear down a connector_list iterator
  560. * @iter: connector_list iterator
  561. *
  562. * Tears down @iter and releases any resources (like &drm_connector references)
  563. * acquired while walking the list. This must always be called, both when the
  564. * iteration completes fully or when it was aborted without walking the entire
  565. * list.
  566. */
  567. void drm_connector_list_iter_end(struct drm_connector_list_iter *iter)
  568. {
  569. struct drm_mode_config *config = &iter->dev->mode_config;
  570. unsigned long flags;
  571. iter->dev = NULL;
  572. if (iter->conn) {
  573. spin_lock_irqsave(&config->connector_list_lock, flags);
  574. __drm_connector_put_safe(iter->conn);
  575. spin_unlock_irqrestore(&config->connector_list_lock, flags);
  576. }
  577. lock_release(&connector_list_iter_dep_map, 0, _RET_IP_);
  578. }
  579. EXPORT_SYMBOL(drm_connector_list_iter_end);
  580. static const struct drm_prop_enum_list drm_subpixel_enum_list[] = {
  581. { SubPixelUnknown, "Unknown" },
  582. { SubPixelHorizontalRGB, "Horizontal RGB" },
  583. { SubPixelHorizontalBGR, "Horizontal BGR" },
  584. { SubPixelVerticalRGB, "Vertical RGB" },
  585. { SubPixelVerticalBGR, "Vertical BGR" },
  586. { SubPixelNone, "None" },
  587. };
  588. /**
  589. * drm_get_subpixel_order_name - return a string for a given subpixel enum
  590. * @order: enum of subpixel_order
  591. *
  592. * Note you could abuse this and return something out of bounds, but that
  593. * would be a caller error. No unscrubbed user data should make it here.
  594. */
  595. const char *drm_get_subpixel_order_name(enum subpixel_order order)
  596. {
  597. return drm_subpixel_enum_list[order].name;
  598. }
  599. EXPORT_SYMBOL(drm_get_subpixel_order_name);
  600. static const struct drm_prop_enum_list drm_dpms_enum_list[] = {
  601. { DRM_MODE_DPMS_ON, "On" },
  602. { DRM_MODE_DPMS_STANDBY, "Standby" },
  603. { DRM_MODE_DPMS_SUSPEND, "Suspend" },
  604. { DRM_MODE_DPMS_OFF, "Off" }
  605. };
  606. DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
  607. static const struct drm_prop_enum_list drm_link_status_enum_list[] = {
  608. { DRM_MODE_LINK_STATUS_GOOD, "Good" },
  609. { DRM_MODE_LINK_STATUS_BAD, "Bad" },
  610. };
  611. /**
  612. * drm_display_info_set_bus_formats - set the supported bus formats
  613. * @info: display info to store bus formats in
  614. * @formats: array containing the supported bus formats
  615. * @num_formats: the number of entries in the fmts array
  616. *
  617. * Store the supported bus formats in display info structure.
  618. * See MEDIA_BUS_FMT_* definitions in include/uapi/linux/media-bus-format.h for
  619. * a full list of available formats.
  620. */
  621. int drm_display_info_set_bus_formats(struct drm_display_info *info,
  622. const u32 *formats,
  623. unsigned int num_formats)
  624. {
  625. u32 *fmts = NULL;
  626. if (!formats && num_formats)
  627. return -EINVAL;
  628. if (formats && num_formats) {
  629. fmts = kmemdup(formats, sizeof(*formats) * num_formats,
  630. GFP_KERNEL);
  631. if (!fmts)
  632. return -ENOMEM;
  633. }
  634. kfree(info->bus_formats);
  635. info->bus_formats = fmts;
  636. info->num_bus_formats = num_formats;
  637. return 0;
  638. }
  639. EXPORT_SYMBOL(drm_display_info_set_bus_formats);
  640. /* Optional connector properties. */
  641. static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] = {
  642. { DRM_MODE_SCALE_NONE, "None" },
  643. { DRM_MODE_SCALE_FULLSCREEN, "Full" },
  644. { DRM_MODE_SCALE_CENTER, "Center" },
  645. { DRM_MODE_SCALE_ASPECT, "Full aspect" },
  646. };
  647. static const struct drm_prop_enum_list drm_aspect_ratio_enum_list[] = {
  648. { DRM_MODE_PICTURE_ASPECT_NONE, "Automatic" },
  649. { DRM_MODE_PICTURE_ASPECT_4_3, "4:3" },
  650. { DRM_MODE_PICTURE_ASPECT_16_9, "16:9" },
  651. };
  652. static const struct drm_prop_enum_list drm_content_type_enum_list[] = {
  653. { DRM_MODE_CONTENT_TYPE_NO_DATA, "No Data" },
  654. { DRM_MODE_CONTENT_TYPE_GRAPHICS, "Graphics" },
  655. { DRM_MODE_CONTENT_TYPE_PHOTO, "Photo" },
  656. { DRM_MODE_CONTENT_TYPE_CINEMA, "Cinema" },
  657. { DRM_MODE_CONTENT_TYPE_GAME, "Game" },
  658. };
  659. static const struct drm_prop_enum_list drm_panel_orientation_enum_list[] = {
  660. { DRM_MODE_PANEL_ORIENTATION_NORMAL, "Normal" },
  661. { DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP, "Upside Down" },
  662. { DRM_MODE_PANEL_ORIENTATION_LEFT_UP, "Left Side Up" },
  663. { DRM_MODE_PANEL_ORIENTATION_RIGHT_UP, "Right Side Up" },
  664. };
  665. static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] = {
  666. { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
  667. { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
  668. { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
  669. };
  670. DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
  671. static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] = {
  672. { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
  673. { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
  674. { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
  675. };
  676. DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
  677. drm_dvi_i_subconnector_enum_list)
  678. static const struct drm_prop_enum_list drm_tv_select_enum_list[] = {
  679. { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
  680. { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
  681. { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
  682. { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
  683. { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
  684. };
  685. DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
  686. static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] = {
  687. { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
  688. { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
  689. { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
  690. { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
  691. { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
  692. };
  693. DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
  694. drm_tv_subconnector_enum_list)
  695. static struct drm_prop_enum_list drm_cp_enum_list[] = {
  696. { DRM_MODE_CONTENT_PROTECTION_UNDESIRED, "Undesired" },
  697. { DRM_MODE_CONTENT_PROTECTION_DESIRED, "Desired" },
  698. { DRM_MODE_CONTENT_PROTECTION_ENABLED, "Enabled" },
  699. };
  700. DRM_ENUM_NAME_FN(drm_get_content_protection_name, drm_cp_enum_list)
  701. /**
  702. * DOC: standard connector properties
  703. *
  704. * DRM connectors have a few standardized properties:
  705. *
  706. * EDID:
  707. * Blob property which contains the current EDID read from the sink. This
  708. * is useful to parse sink identification information like vendor, model
  709. * and serial. Drivers should update this property by calling
  710. * drm_connector_update_edid_property(), usually after having parsed
  711. * the EDID using drm_add_edid_modes(). Userspace cannot change this
  712. * property.
  713. * DPMS:
  714. * Legacy property for setting the power state of the connector. For atomic
  715. * drivers this is only provided for backwards compatibility with existing
  716. * drivers, it remaps to controlling the "ACTIVE" property on the CRTC the
  717. * connector is linked to. Drivers should never set this property directly,
  718. * it is handled by the DRM core by calling the &drm_connector_funcs.dpms
  719. * callback. For atomic drivers the remapping to the "ACTIVE" property is
  720. * implemented in the DRM core. This is the only standard connector
  721. * property that userspace can change.
  722. *
  723. * Note that this property cannot be set through the MODE_ATOMIC ioctl,
  724. * userspace must use "ACTIVE" on the CRTC instead.
  725. *
  726. * WARNING:
  727. *
  728. * For userspace also running on legacy drivers the "DPMS" semantics are a
  729. * lot more complicated. First, userspace cannot rely on the "DPMS" value
  730. * returned by the GETCONNECTOR actually reflecting reality, because many
  731. * drivers fail to update it. For atomic drivers this is taken care of in
  732. * drm_atomic_helper_update_legacy_modeset_state().
  733. *
  734. * The second issue is that the DPMS state is only well-defined when the
  735. * connector is connected to a CRTC. In atomic the DRM core enforces that
  736. * "ACTIVE" is off in such a case, no such checks exists for "DPMS".
  737. *
  738. * Finally, when enabling an output using the legacy SETCONFIG ioctl then
  739. * "DPMS" is forced to ON. But see above, that might not be reflected in
  740. * the software value on legacy drivers.
  741. *
  742. * Summarizing: Only set "DPMS" when the connector is known to be enabled,
  743. * assume that a successful SETCONFIG call also sets "DPMS" to on, and
  744. * never read back the value of "DPMS" because it can be incorrect.
  745. * PATH:
  746. * Connector path property to identify how this sink is physically
  747. * connected. Used by DP MST. This should be set by calling
  748. * drm_connector_set_path_property(), in the case of DP MST with the
  749. * path property the MST manager created. Userspace cannot change this
  750. * property.
  751. * TILE:
  752. * Connector tile group property to indicate how a set of DRM connector
  753. * compose together into one logical screen. This is used by both high-res
  754. * external screens (often only using a single cable, but exposing multiple
  755. * DP MST sinks), or high-res integrated panels (like dual-link DSI) which
  756. * are not gen-locked. Note that for tiled panels which are genlocked, like
  757. * dual-link LVDS or dual-link DSI, the driver should try to not expose the
  758. * tiling and virtualize both &drm_crtc and &drm_plane if needed. Drivers
  759. * should update this value using drm_connector_set_tile_property().
  760. * Userspace cannot change this property.
  761. * link-status:
  762. * Connector link-status property to indicate the status of link. The
  763. * default value of link-status is "GOOD". If something fails during or
  764. * after modeset, the kernel driver may set this to "BAD" and issue a
  765. * hotplug uevent. Drivers should update this value using
  766. * drm_connector_set_link_status_property().
  767. * non_desktop:
  768. * Indicates the output should be ignored for purposes of displaying a
  769. * standard desktop environment or console. This is most likely because
  770. * the output device is not rectilinear.
  771. * Content Protection:
  772. * This property is used by userspace to request the kernel protect future
  773. * content communicated over the link. When requested, kernel will apply
  774. * the appropriate means of protection (most often HDCP), and use the
  775. * property to tell userspace the protection is active.
  776. *
  777. * Drivers can set this up by calling
  778. * drm_connector_attach_content_protection_property() on initialization.
  779. *
  780. * The value of this property can be one of the following:
  781. *
  782. * DRM_MODE_CONTENT_PROTECTION_UNDESIRED = 0
  783. * The link is not protected, content is transmitted in the clear.
  784. * DRM_MODE_CONTENT_PROTECTION_DESIRED = 1
  785. * Userspace has requested content protection, but the link is not
  786. * currently protected. When in this state, kernel should enable
  787. * Content Protection as soon as possible.
  788. * DRM_MODE_CONTENT_PROTECTION_ENABLED = 2
  789. * Userspace has requested content protection, and the link is
  790. * protected. Only the driver can set the property to this value.
  791. * If userspace attempts to set to ENABLED, kernel will return
  792. * -EINVAL.
  793. *
  794. * A few guidelines:
  795. *
  796. * - DESIRED state should be preserved until userspace de-asserts it by
  797. * setting the property to UNDESIRED. This means ENABLED should only
  798. * transition to UNDESIRED when the user explicitly requests it.
  799. * - If the state is DESIRED, kernel should attempt to re-authenticate the
  800. * link whenever possible. This includes across disable/enable, dpms,
  801. * hotplug, downstream device changes, link status failures, etc..
  802. * - Userspace is responsible for polling the property to determine when
  803. * the value transitions from ENABLED to DESIRED. This signifies the link
  804. * is no longer protected and userspace should take appropriate action
  805. * (whatever that might be).
  806. *
  807. * Connectors also have one standardized atomic property:
  808. *
  809. * CRTC_ID:
  810. * Mode object ID of the &drm_crtc this connector should be connected to.
  811. *
  812. * Connectors for LCD panels may also have one standardized property:
  813. *
  814. * panel orientation:
  815. * On some devices the LCD panel is mounted in the casing in such a way
  816. * that the up/top side of the panel does not match with the top side of
  817. * the device. Userspace can use this property to check for this.
  818. * Note that input coordinates from touchscreens (input devices with
  819. * INPUT_PROP_DIRECT) will still map 1:1 to the actual LCD panel
  820. * coordinates, so if userspace rotates the picture to adjust for
  821. * the orientation it must also apply the same transformation to the
  822. * touchscreen input coordinates. This property is initialized by calling
  823. * drm_connector_init_panel_orientation_property().
  824. *
  825. * scaling mode:
  826. * This property defines how a non-native mode is upscaled to the native
  827. * mode of an LCD panel:
  828. *
  829. * None:
  830. * No upscaling happens, scaling is left to the panel. Not all
  831. * drivers expose this mode.
  832. * Full:
  833. * The output is upscaled to the full resolution of the panel,
  834. * ignoring the aspect ratio.
  835. * Center:
  836. * No upscaling happens, the output is centered within the native
  837. * resolution the panel.
  838. * Full aspect:
  839. * The output is upscaled to maximize either the width or height
  840. * while retaining the aspect ratio.
  841. *
  842. * This property should be set up by calling
  843. * drm_connector_attach_scaling_mode_property(). Note that drivers
  844. * can also expose this property to external outputs, in which case they
  845. * must support "None", which should be the default (since external screens
  846. * have a built-in scaler).
  847. */
  848. int drm_connector_create_standard_properties(struct drm_device *dev)
  849. {
  850. struct drm_property *prop;
  851. prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
  852. DRM_MODE_PROP_IMMUTABLE,
  853. "EDID", 0);
  854. if (!prop)
  855. return -ENOMEM;
  856. dev->mode_config.edid_property = prop;
  857. prop = drm_property_create_enum(dev, 0,
  858. "DPMS", drm_dpms_enum_list,
  859. ARRAY_SIZE(drm_dpms_enum_list));
  860. if (!prop)
  861. return -ENOMEM;
  862. dev->mode_config.dpms_property = prop;
  863. prop = drm_property_create(dev,
  864. DRM_MODE_PROP_BLOB |
  865. DRM_MODE_PROP_IMMUTABLE,
  866. "PATH", 0);
  867. if (!prop)
  868. return -ENOMEM;
  869. dev->mode_config.path_property = prop;
  870. prop = drm_property_create(dev,
  871. DRM_MODE_PROP_BLOB |
  872. DRM_MODE_PROP_IMMUTABLE,
  873. "TILE", 0);
  874. if (!prop)
  875. return -ENOMEM;
  876. dev->mode_config.tile_property = prop;
  877. prop = drm_property_create_enum(dev, 0, "link-status",
  878. drm_link_status_enum_list,
  879. ARRAY_SIZE(drm_link_status_enum_list));
  880. if (!prop)
  881. return -ENOMEM;
  882. dev->mode_config.link_status_property = prop;
  883. prop = drm_property_create_bool(dev, DRM_MODE_PROP_IMMUTABLE, "non-desktop");
  884. if (!prop)
  885. return -ENOMEM;
  886. dev->mode_config.non_desktop_property = prop;
  887. return 0;
  888. }
  889. /**
  890. * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
  891. * @dev: DRM device
  892. *
  893. * Called by a driver the first time a DVI-I connector is made.
  894. */
  895. int drm_mode_create_dvi_i_properties(struct drm_device *dev)
  896. {
  897. struct drm_property *dvi_i_selector;
  898. struct drm_property *dvi_i_subconnector;
  899. if (dev->mode_config.dvi_i_select_subconnector_property)
  900. return 0;
  901. dvi_i_selector =
  902. drm_property_create_enum(dev, 0,
  903. "select subconnector",
  904. drm_dvi_i_select_enum_list,
  905. ARRAY_SIZE(drm_dvi_i_select_enum_list));
  906. dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
  907. dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  908. "subconnector",
  909. drm_dvi_i_subconnector_enum_list,
  910. ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
  911. dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
  912. return 0;
  913. }
  914. EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
  915. /**
  916. * DOC: HDMI connector properties
  917. *
  918. * content type (HDMI specific):
  919. * Indicates content type setting to be used in HDMI infoframes to indicate
  920. * content type for the external device, so that it adjusts it's display
  921. * settings accordingly.
  922. *
  923. * The value of this property can be one of the following:
  924. *
  925. * No Data:
  926. * Content type is unknown
  927. * Graphics:
  928. * Content type is graphics
  929. * Photo:
  930. * Content type is photo
  931. * Cinema:
  932. * Content type is cinema
  933. * Game:
  934. * Content type is game
  935. *
  936. * Drivers can set up this property by calling
  937. * drm_connector_attach_content_type_property(). Decoding to
  938. * infoframe values is done through drm_hdmi_avi_infoframe_content_type().
  939. */
  940. /**
  941. * drm_connector_attach_content_type_property - attach content-type property
  942. * @connector: connector to attach content type property on.
  943. *
  944. * Called by a driver the first time a HDMI connector is made.
  945. */
  946. int drm_connector_attach_content_type_property(struct drm_connector *connector)
  947. {
  948. if (!drm_mode_create_content_type_property(connector->dev))
  949. drm_object_attach_property(&connector->base,
  950. connector->dev->mode_config.content_type_property,
  951. DRM_MODE_CONTENT_TYPE_NO_DATA);
  952. return 0;
  953. }
  954. EXPORT_SYMBOL(drm_connector_attach_content_type_property);
  955. /**
  956. * drm_hdmi_avi_infoframe_content_type() - fill the HDMI AVI infoframe
  957. * content type information, based
  958. * on correspondent DRM property.
  959. * @frame: HDMI AVI infoframe
  960. * @conn_state: DRM display connector state
  961. *
  962. */
  963. void drm_hdmi_avi_infoframe_content_type(struct hdmi_avi_infoframe *frame,
  964. const struct drm_connector_state *conn_state)
  965. {
  966. switch (conn_state->content_type) {
  967. case DRM_MODE_CONTENT_TYPE_GRAPHICS:
  968. frame->content_type = HDMI_CONTENT_TYPE_GRAPHICS;
  969. break;
  970. case DRM_MODE_CONTENT_TYPE_CINEMA:
  971. frame->content_type = HDMI_CONTENT_TYPE_CINEMA;
  972. break;
  973. case DRM_MODE_CONTENT_TYPE_GAME:
  974. frame->content_type = HDMI_CONTENT_TYPE_GAME;
  975. break;
  976. case DRM_MODE_CONTENT_TYPE_PHOTO:
  977. frame->content_type = HDMI_CONTENT_TYPE_PHOTO;
  978. break;
  979. default:
  980. /* Graphics is the default(0) */
  981. frame->content_type = HDMI_CONTENT_TYPE_GRAPHICS;
  982. }
  983. frame->itc = conn_state->content_type != DRM_MODE_CONTENT_TYPE_NO_DATA;
  984. }
  985. EXPORT_SYMBOL(drm_hdmi_avi_infoframe_content_type);
  986. /**
  987. * drm_create_tv_properties - create TV specific connector properties
  988. * @dev: DRM device
  989. * @num_modes: number of different TV formats (modes) supported
  990. * @modes: array of pointers to strings containing name of each format
  991. *
  992. * Called by a driver's TV initialization routine, this function creates
  993. * the TV specific connector properties for a given device. Caller is
  994. * responsible for allocating a list of format names and passing them to
  995. * this routine.
  996. */
  997. int drm_mode_create_tv_properties(struct drm_device *dev,
  998. unsigned int num_modes,
  999. const char * const modes[])
  1000. {
  1001. struct drm_property *tv_selector;
  1002. struct drm_property *tv_subconnector;
  1003. unsigned int i;
  1004. if (dev->mode_config.tv_select_subconnector_property)
  1005. return 0;
  1006. /*
  1007. * Basic connector properties
  1008. */
  1009. tv_selector = drm_property_create_enum(dev, 0,
  1010. "select subconnector",
  1011. drm_tv_select_enum_list,
  1012. ARRAY_SIZE(drm_tv_select_enum_list));
  1013. if (!tv_selector)
  1014. goto nomem;
  1015. dev->mode_config.tv_select_subconnector_property = tv_selector;
  1016. tv_subconnector =
  1017. drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  1018. "subconnector",
  1019. drm_tv_subconnector_enum_list,
  1020. ARRAY_SIZE(drm_tv_subconnector_enum_list));
  1021. if (!tv_subconnector)
  1022. goto nomem;
  1023. dev->mode_config.tv_subconnector_property = tv_subconnector;
  1024. /*
  1025. * Other, TV specific properties: margins & TV modes.
  1026. */
  1027. dev->mode_config.tv_left_margin_property =
  1028. drm_property_create_range(dev, 0, "left margin", 0, 100);
  1029. if (!dev->mode_config.tv_left_margin_property)
  1030. goto nomem;
  1031. dev->mode_config.tv_right_margin_property =
  1032. drm_property_create_range(dev, 0, "right margin", 0, 100);
  1033. if (!dev->mode_config.tv_right_margin_property)
  1034. goto nomem;
  1035. dev->mode_config.tv_top_margin_property =
  1036. drm_property_create_range(dev, 0, "top margin", 0, 100);
  1037. if (!dev->mode_config.tv_top_margin_property)
  1038. goto nomem;
  1039. dev->mode_config.tv_bottom_margin_property =
  1040. drm_property_create_range(dev, 0, "bottom margin", 0, 100);
  1041. if (!dev->mode_config.tv_bottom_margin_property)
  1042. goto nomem;
  1043. dev->mode_config.tv_mode_property =
  1044. drm_property_create(dev, DRM_MODE_PROP_ENUM,
  1045. "mode", num_modes);
  1046. if (!dev->mode_config.tv_mode_property)
  1047. goto nomem;
  1048. for (i = 0; i < num_modes; i++)
  1049. drm_property_add_enum(dev->mode_config.tv_mode_property,
  1050. i, modes[i]);
  1051. dev->mode_config.tv_brightness_property =
  1052. drm_property_create_range(dev, 0, "brightness", 0, 100);
  1053. if (!dev->mode_config.tv_brightness_property)
  1054. goto nomem;
  1055. dev->mode_config.tv_contrast_property =
  1056. drm_property_create_range(dev, 0, "contrast", 0, 100);
  1057. if (!dev->mode_config.tv_contrast_property)
  1058. goto nomem;
  1059. dev->mode_config.tv_flicker_reduction_property =
  1060. drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
  1061. if (!dev->mode_config.tv_flicker_reduction_property)
  1062. goto nomem;
  1063. dev->mode_config.tv_overscan_property =
  1064. drm_property_create_range(dev, 0, "overscan", 0, 100);
  1065. if (!dev->mode_config.tv_overscan_property)
  1066. goto nomem;
  1067. dev->mode_config.tv_saturation_property =
  1068. drm_property_create_range(dev, 0, "saturation", 0, 100);
  1069. if (!dev->mode_config.tv_saturation_property)
  1070. goto nomem;
  1071. dev->mode_config.tv_hue_property =
  1072. drm_property_create_range(dev, 0, "hue", 0, 100);
  1073. if (!dev->mode_config.tv_hue_property)
  1074. goto nomem;
  1075. return 0;
  1076. nomem:
  1077. return -ENOMEM;
  1078. }
  1079. EXPORT_SYMBOL(drm_mode_create_tv_properties);
  1080. /**
  1081. * drm_mode_create_scaling_mode_property - create scaling mode property
  1082. * @dev: DRM device
  1083. *
  1084. * Called by a driver the first time it's needed, must be attached to desired
  1085. * connectors.
  1086. *
  1087. * Atomic drivers should use drm_connector_attach_scaling_mode_property()
  1088. * instead to correctly assign &drm_connector_state.picture_aspect_ratio
  1089. * in the atomic state.
  1090. */
  1091. int drm_mode_create_scaling_mode_property(struct drm_device *dev)
  1092. {
  1093. struct drm_property *scaling_mode;
  1094. if (dev->mode_config.scaling_mode_property)
  1095. return 0;
  1096. scaling_mode =
  1097. drm_property_create_enum(dev, 0, "scaling mode",
  1098. drm_scaling_mode_enum_list,
  1099. ARRAY_SIZE(drm_scaling_mode_enum_list));
  1100. dev->mode_config.scaling_mode_property = scaling_mode;
  1101. return 0;
  1102. }
  1103. EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
  1104. /**
  1105. * drm_connector_attach_scaling_mode_property - attach atomic scaling mode property
  1106. * @connector: connector to attach scaling mode property on.
  1107. * @scaling_mode_mask: or'ed mask of BIT(%DRM_MODE_SCALE_\*).
  1108. *
  1109. * This is used to add support for scaling mode to atomic drivers.
  1110. * The scaling mode will be set to &drm_connector_state.picture_aspect_ratio
  1111. * and can be used from &drm_connector_helper_funcs->atomic_check for validation.
  1112. *
  1113. * This is the atomic version of drm_mode_create_scaling_mode_property().
  1114. *
  1115. * Returns:
  1116. * Zero on success, negative errno on failure.
  1117. */
  1118. int drm_connector_attach_scaling_mode_property(struct drm_connector *connector,
  1119. u32 scaling_mode_mask)
  1120. {
  1121. struct drm_device *dev = connector->dev;
  1122. struct drm_property *scaling_mode_property;
  1123. int i;
  1124. const unsigned valid_scaling_mode_mask =
  1125. (1U << ARRAY_SIZE(drm_scaling_mode_enum_list)) - 1;
  1126. if (WARN_ON(hweight32(scaling_mode_mask) < 2 ||
  1127. scaling_mode_mask & ~valid_scaling_mode_mask))
  1128. return -EINVAL;
  1129. scaling_mode_property =
  1130. drm_property_create(dev, DRM_MODE_PROP_ENUM, "scaling mode",
  1131. hweight32(scaling_mode_mask));
  1132. if (!scaling_mode_property)
  1133. return -ENOMEM;
  1134. for (i = 0; i < ARRAY_SIZE(drm_scaling_mode_enum_list); i++) {
  1135. int ret;
  1136. if (!(BIT(i) & scaling_mode_mask))
  1137. continue;
  1138. ret = drm_property_add_enum(scaling_mode_property,
  1139. drm_scaling_mode_enum_list[i].type,
  1140. drm_scaling_mode_enum_list[i].name);
  1141. if (ret) {
  1142. drm_property_destroy(dev, scaling_mode_property);
  1143. return ret;
  1144. }
  1145. }
  1146. drm_object_attach_property(&connector->base,
  1147. scaling_mode_property, 0);
  1148. connector->scaling_mode_property = scaling_mode_property;
  1149. return 0;
  1150. }
  1151. EXPORT_SYMBOL(drm_connector_attach_scaling_mode_property);
  1152. /**
  1153. * drm_connector_attach_content_protection_property - attach content protection
  1154. * property
  1155. *
  1156. * @connector: connector to attach CP property on.
  1157. *
  1158. * This is used to add support for content protection on select connectors.
  1159. * Content Protection is intentionally vague to allow for different underlying
  1160. * technologies, however it is most implemented by HDCP.
  1161. *
  1162. * The content protection will be set to &drm_connector_state.content_protection
  1163. *
  1164. * Returns:
  1165. * Zero on success, negative errno on failure.
  1166. */
  1167. int drm_connector_attach_content_protection_property(
  1168. struct drm_connector *connector)
  1169. {
  1170. struct drm_device *dev = connector->dev;
  1171. struct drm_property *prop;
  1172. prop = drm_property_create_enum(dev, 0, "Content Protection",
  1173. drm_cp_enum_list,
  1174. ARRAY_SIZE(drm_cp_enum_list));
  1175. if (!prop)
  1176. return -ENOMEM;
  1177. drm_object_attach_property(&connector->base, prop,
  1178. DRM_MODE_CONTENT_PROTECTION_UNDESIRED);
  1179. connector->content_protection_property = prop;
  1180. return 0;
  1181. }
  1182. EXPORT_SYMBOL(drm_connector_attach_content_protection_property);
  1183. /**
  1184. * drm_mode_create_aspect_ratio_property - create aspect ratio property
  1185. * @dev: DRM device
  1186. *
  1187. * Called by a driver the first time it's needed, must be attached to desired
  1188. * connectors.
  1189. *
  1190. * Returns:
  1191. * Zero on success, negative errno on failure.
  1192. */
  1193. int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
  1194. {
  1195. if (dev->mode_config.aspect_ratio_property)
  1196. return 0;
  1197. dev->mode_config.aspect_ratio_property =
  1198. drm_property_create_enum(dev, 0, "aspect ratio",
  1199. drm_aspect_ratio_enum_list,
  1200. ARRAY_SIZE(drm_aspect_ratio_enum_list));
  1201. if (dev->mode_config.aspect_ratio_property == NULL)
  1202. return -ENOMEM;
  1203. return 0;
  1204. }
  1205. EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
  1206. /**
  1207. * drm_mode_create_content_type_property - create content type property
  1208. * @dev: DRM device
  1209. *
  1210. * Called by a driver the first time it's needed, must be attached to desired
  1211. * connectors.
  1212. *
  1213. * Returns:
  1214. * Zero on success, negative errno on failure.
  1215. */
  1216. int drm_mode_create_content_type_property(struct drm_device *dev)
  1217. {
  1218. if (dev->mode_config.content_type_property)
  1219. return 0;
  1220. dev->mode_config.content_type_property =
  1221. drm_property_create_enum(dev, 0, "content type",
  1222. drm_content_type_enum_list,
  1223. ARRAY_SIZE(drm_content_type_enum_list));
  1224. if (dev->mode_config.content_type_property == NULL)
  1225. return -ENOMEM;
  1226. return 0;
  1227. }
  1228. EXPORT_SYMBOL(drm_mode_create_content_type_property);
  1229. /**
  1230. * drm_mode_create_suggested_offset_properties - create suggests offset properties
  1231. * @dev: DRM device
  1232. *
  1233. * Create the the suggested x/y offset property for connectors.
  1234. */
  1235. int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
  1236. {
  1237. if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
  1238. return 0;
  1239. dev->mode_config.suggested_x_property =
  1240. drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
  1241. dev->mode_config.suggested_y_property =
  1242. drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
  1243. if (dev->mode_config.suggested_x_property == NULL ||
  1244. dev->mode_config.suggested_y_property == NULL)
  1245. return -ENOMEM;
  1246. return 0;
  1247. }
  1248. EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
  1249. /**
  1250. * drm_connector_set_path_property - set tile property on connector
  1251. * @connector: connector to set property on.
  1252. * @path: path to use for property; must not be NULL.
  1253. *
  1254. * This creates a property to expose to userspace to specify a
  1255. * connector path. This is mainly used for DisplayPort MST where
  1256. * connectors have a topology and we want to allow userspace to give
  1257. * them more meaningful names.
  1258. *
  1259. * Returns:
  1260. * Zero on success, negative errno on failure.
  1261. */
  1262. int drm_connector_set_path_property(struct drm_connector *connector,
  1263. const char *path)
  1264. {
  1265. struct drm_device *dev = connector->dev;
  1266. int ret;
  1267. ret = drm_property_replace_global_blob(dev,
  1268. &connector->path_blob_ptr,
  1269. strlen(path) + 1,
  1270. path,
  1271. &connector->base,
  1272. dev->mode_config.path_property);
  1273. return ret;
  1274. }
  1275. EXPORT_SYMBOL(drm_connector_set_path_property);
  1276. /**
  1277. * drm_connector_set_tile_property - set tile property on connector
  1278. * @connector: connector to set property on.
  1279. *
  1280. * This looks up the tile information for a connector, and creates a
  1281. * property for userspace to parse if it exists. The property is of
  1282. * the form of 8 integers using ':' as a separator.
  1283. *
  1284. * Returns:
  1285. * Zero on success, errno on failure.
  1286. */
  1287. int drm_connector_set_tile_property(struct drm_connector *connector)
  1288. {
  1289. struct drm_device *dev = connector->dev;
  1290. char tile[256];
  1291. int ret;
  1292. if (!connector->has_tile) {
  1293. ret = drm_property_replace_global_blob(dev,
  1294. &connector->tile_blob_ptr,
  1295. 0,
  1296. NULL,
  1297. &connector->base,
  1298. dev->mode_config.tile_property);
  1299. return ret;
  1300. }
  1301. snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
  1302. connector->tile_group->id, connector->tile_is_single_monitor,
  1303. connector->num_h_tile, connector->num_v_tile,
  1304. connector->tile_h_loc, connector->tile_v_loc,
  1305. connector->tile_h_size, connector->tile_v_size);
  1306. ret = drm_property_replace_global_blob(dev,
  1307. &connector->tile_blob_ptr,
  1308. strlen(tile) + 1,
  1309. tile,
  1310. &connector->base,
  1311. dev->mode_config.tile_property);
  1312. return ret;
  1313. }
  1314. EXPORT_SYMBOL(drm_connector_set_tile_property);
  1315. /**
  1316. * drm_connector_update_edid_property - update the edid property of a connector
  1317. * @connector: drm connector
  1318. * @edid: new value of the edid property
  1319. *
  1320. * This function creates a new blob modeset object and assigns its id to the
  1321. * connector's edid property.
  1322. *
  1323. * Returns:
  1324. * Zero on success, negative errno on failure.
  1325. */
  1326. int drm_connector_update_edid_property(struct drm_connector *connector,
  1327. const struct edid *edid)
  1328. {
  1329. struct drm_device *dev = connector->dev;
  1330. size_t size = 0;
  1331. int ret;
  1332. /* ignore requests to set edid when overridden */
  1333. if (connector->override_edid)
  1334. return 0;
  1335. if (edid)
  1336. size = EDID_LENGTH * (1 + edid->extensions);
  1337. /* Set the display info, using edid if available, otherwise
  1338. * reseting the values to defaults. This duplicates the work
  1339. * done in drm_add_edid_modes, but that function is not
  1340. * consistently called before this one in all drivers and the
  1341. * computation is cheap enough that it seems better to
  1342. * duplicate it rather than attempt to ensure some arbitrary
  1343. * ordering of calls.
  1344. */
  1345. if (edid)
  1346. drm_add_display_info(connector, edid);
  1347. else
  1348. drm_reset_display_info(connector);
  1349. drm_object_property_set_value(&connector->base,
  1350. dev->mode_config.non_desktop_property,
  1351. connector->display_info.non_desktop);
  1352. ret = drm_property_replace_global_blob(dev,
  1353. &connector->edid_blob_ptr,
  1354. size,
  1355. edid,
  1356. &connector->base,
  1357. dev->mode_config.edid_property);
  1358. return ret;
  1359. }
  1360. EXPORT_SYMBOL(drm_connector_update_edid_property);
  1361. /**
  1362. * drm_connector_set_link_status_property - Set link status property of a connector
  1363. * @connector: drm connector
  1364. * @link_status: new value of link status property (0: Good, 1: Bad)
  1365. *
  1366. * In usual working scenario, this link status property will always be set to
  1367. * "GOOD". If something fails during or after a mode set, the kernel driver
  1368. * may set this link status property to "BAD". The caller then needs to send a
  1369. * hotplug uevent for userspace to re-check the valid modes through
  1370. * GET_CONNECTOR_IOCTL and retry modeset.
  1371. *
  1372. * Note: Drivers cannot rely on userspace to support this property and
  1373. * issue a modeset. As such, they may choose to handle issues (like
  1374. * re-training a link) without userspace's intervention.
  1375. *
  1376. * The reason for adding this property is to handle link training failures, but
  1377. * it is not limited to DP or link training. For example, if we implement
  1378. * asynchronous setcrtc, this property can be used to report any failures in that.
  1379. */
  1380. void drm_connector_set_link_status_property(struct drm_connector *connector,
  1381. uint64_t link_status)
  1382. {
  1383. struct drm_device *dev = connector->dev;
  1384. drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
  1385. connector->state->link_status = link_status;
  1386. drm_modeset_unlock(&dev->mode_config.connection_mutex);
  1387. }
  1388. EXPORT_SYMBOL(drm_connector_set_link_status_property);
  1389. /**
  1390. * drm_connector_init_panel_orientation_property -
  1391. * initialize the connecters panel_orientation property
  1392. * @connector: connector for which to init the panel-orientation property.
  1393. * @width: width in pixels of the panel, used for panel quirk detection
  1394. * @height: height in pixels of the panel, used for panel quirk detection
  1395. *
  1396. * This function should only be called for built-in panels, after setting
  1397. * connector->display_info.panel_orientation first (if known).
  1398. *
  1399. * This function will check for platform specific (e.g. DMI based) quirks
  1400. * overriding display_info.panel_orientation first, then if panel_orientation
  1401. * is not DRM_MODE_PANEL_ORIENTATION_UNKNOWN it will attach the
  1402. * "panel orientation" property to the connector.
  1403. *
  1404. * Returns:
  1405. * Zero on success, negative errno on failure.
  1406. */
  1407. int drm_connector_init_panel_orientation_property(
  1408. struct drm_connector *connector, int width, int height)
  1409. {
  1410. struct drm_device *dev = connector->dev;
  1411. struct drm_display_info *info = &connector->display_info;
  1412. struct drm_property *prop;
  1413. int orientation_quirk;
  1414. orientation_quirk = drm_get_panel_orientation_quirk(width, height);
  1415. if (orientation_quirk != DRM_MODE_PANEL_ORIENTATION_UNKNOWN)
  1416. info->panel_orientation = orientation_quirk;
  1417. if (info->panel_orientation == DRM_MODE_PANEL_ORIENTATION_UNKNOWN)
  1418. return 0;
  1419. prop = dev->mode_config.panel_orientation_property;
  1420. if (!prop) {
  1421. prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  1422. "panel orientation",
  1423. drm_panel_orientation_enum_list,
  1424. ARRAY_SIZE(drm_panel_orientation_enum_list));
  1425. if (!prop)
  1426. return -ENOMEM;
  1427. dev->mode_config.panel_orientation_property = prop;
  1428. }
  1429. drm_object_attach_property(&connector->base, prop,
  1430. info->panel_orientation);
  1431. return 0;
  1432. }
  1433. EXPORT_SYMBOL(drm_connector_init_panel_orientation_property);
  1434. int drm_connector_set_obj_prop(struct drm_mode_object *obj,
  1435. struct drm_property *property,
  1436. uint64_t value)
  1437. {
  1438. int ret = -EINVAL;
  1439. struct drm_connector *connector = obj_to_connector(obj);
  1440. /* Do DPMS ourselves */
  1441. if (property == connector->dev->mode_config.dpms_property) {
  1442. ret = (*connector->funcs->dpms)(connector, (int)value);
  1443. } else if (connector->funcs->set_property)
  1444. ret = connector->funcs->set_property(connector, property, value);
  1445. if (!ret)
  1446. drm_object_property_set_value(&connector->base, property, value);
  1447. return ret;
  1448. }
  1449. int drm_connector_property_set_ioctl(struct drm_device *dev,
  1450. void *data, struct drm_file *file_priv)
  1451. {
  1452. struct drm_mode_connector_set_property *conn_set_prop = data;
  1453. struct drm_mode_obj_set_property obj_set_prop = {
  1454. .value = conn_set_prop->value,
  1455. .prop_id = conn_set_prop->prop_id,
  1456. .obj_id = conn_set_prop->connector_id,
  1457. .obj_type = DRM_MODE_OBJECT_CONNECTOR
  1458. };
  1459. /* It does all the locking and checking we need */
  1460. return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
  1461. }
  1462. static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
  1463. {
  1464. /* For atomic drivers only state objects are synchronously updated and
  1465. * protected by modeset locks, so check those first. */
  1466. if (connector->state)
  1467. return connector->state->best_encoder;
  1468. return connector->encoder;
  1469. }
  1470. static bool
  1471. drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
  1472. const struct list_head *export_list,
  1473. const struct drm_file *file_priv)
  1474. {
  1475. /*
  1476. * If user-space hasn't configured the driver to expose the stereo 3D
  1477. * modes, don't expose them.
  1478. */
  1479. if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
  1480. return false;
  1481. /*
  1482. * If user-space hasn't configured the driver to expose the modes
  1483. * with aspect-ratio, don't expose them. However if such a mode
  1484. * is unique, let it be exposed, but reset the aspect-ratio flags
  1485. * while preparing the list of user-modes.
  1486. */
  1487. if (!file_priv->aspect_ratio_allowed) {
  1488. struct drm_display_mode *mode_itr;
  1489. list_for_each_entry(mode_itr, export_list, export_head)
  1490. if (drm_mode_match(mode_itr, mode,
  1491. DRM_MODE_MATCH_TIMINGS |
  1492. DRM_MODE_MATCH_CLOCK |
  1493. DRM_MODE_MATCH_FLAGS |
  1494. DRM_MODE_MATCH_3D_FLAGS))
  1495. return false;
  1496. }
  1497. return true;
  1498. }
  1499. int drm_mode_getconnector(struct drm_device *dev, void *data,
  1500. struct drm_file *file_priv)
  1501. {
  1502. struct drm_mode_get_connector *out_resp = data;
  1503. struct drm_connector *connector;
  1504. struct drm_encoder *encoder;
  1505. struct drm_display_mode *mode;
  1506. int mode_count = 0;
  1507. int encoders_count = 0;
  1508. int ret = 0;
  1509. int copied = 0;
  1510. int i;
  1511. struct drm_mode_modeinfo u_mode;
  1512. struct drm_mode_modeinfo __user *mode_ptr;
  1513. uint32_t __user *encoder_ptr;
  1514. LIST_HEAD(export_list);
  1515. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1516. return -EINVAL;
  1517. memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
  1518. connector = drm_connector_lookup(dev, file_priv, out_resp->connector_id);
  1519. if (!connector)
  1520. return -ENOENT;
  1521. drm_connector_for_each_possible_encoder(connector, encoder, i)
  1522. encoders_count++;
  1523. if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
  1524. copied = 0;
  1525. encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
  1526. drm_connector_for_each_possible_encoder(connector, encoder, i) {
  1527. if (put_user(encoder->base.id, encoder_ptr + copied)) {
  1528. ret = -EFAULT;
  1529. goto out;
  1530. }
  1531. copied++;
  1532. }
  1533. }
  1534. out_resp->count_encoders = encoders_count;
  1535. out_resp->connector_id = connector->base.id;
  1536. out_resp->connector_type = connector->connector_type;
  1537. out_resp->connector_type_id = connector->connector_type_id;
  1538. mutex_lock(&dev->mode_config.mutex);
  1539. if (out_resp->count_modes == 0) {
  1540. connector->funcs->fill_modes(connector,
  1541. dev->mode_config.max_width,
  1542. dev->mode_config.max_height);
  1543. }
  1544. out_resp->mm_width = connector->display_info.width_mm;
  1545. out_resp->mm_height = connector->display_info.height_mm;
  1546. out_resp->subpixel = connector->display_info.subpixel_order;
  1547. out_resp->connection = connector->status;
  1548. /* delayed so we get modes regardless of pre-fill_modes state */
  1549. list_for_each_entry(mode, &connector->modes, head)
  1550. if (drm_mode_expose_to_userspace(mode, &export_list,
  1551. file_priv)) {
  1552. list_add_tail(&mode->export_head, &export_list);
  1553. mode_count++;
  1554. }
  1555. /*
  1556. * This ioctl is called twice, once to determine how much space is
  1557. * needed, and the 2nd time to fill it.
  1558. * The modes that need to be exposed to the user are maintained in the
  1559. * 'export_list'. When the ioctl is called first time to determine the,
  1560. * space, the export_list gets filled, to find the no.of modes. In the
  1561. * 2nd time, the user modes are filled, one by one from the export_list.
  1562. */
  1563. if ((out_resp->count_modes >= mode_count) && mode_count) {
  1564. copied = 0;
  1565. mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
  1566. list_for_each_entry(mode, &export_list, export_head) {
  1567. drm_mode_convert_to_umode(&u_mode, mode);
  1568. /*
  1569. * Reset aspect ratio flags of user-mode, if modes with
  1570. * aspect-ratio are not supported.
  1571. */
  1572. if (!file_priv->aspect_ratio_allowed)
  1573. u_mode.flags &= ~DRM_MODE_FLAG_PIC_AR_MASK;
  1574. if (copy_to_user(mode_ptr + copied,
  1575. &u_mode, sizeof(u_mode))) {
  1576. ret = -EFAULT;
  1577. mutex_unlock(&dev->mode_config.mutex);
  1578. goto out;
  1579. }
  1580. copied++;
  1581. }
  1582. }
  1583. out_resp->count_modes = mode_count;
  1584. mutex_unlock(&dev->mode_config.mutex);
  1585. drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
  1586. encoder = drm_connector_get_encoder(connector);
  1587. if (encoder)
  1588. out_resp->encoder_id = encoder->base.id;
  1589. else
  1590. out_resp->encoder_id = 0;
  1591. /* Only grab properties after probing, to make sure EDID and other
  1592. * properties reflect the latest status. */
  1593. ret = drm_mode_object_get_properties(&connector->base, file_priv->atomic,
  1594. (uint32_t __user *)(unsigned long)(out_resp->props_ptr),
  1595. (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
  1596. &out_resp->count_props);
  1597. drm_modeset_unlock(&dev->mode_config.connection_mutex);
  1598. out:
  1599. drm_connector_put(connector);
  1600. return ret;
  1601. }
  1602. /**
  1603. * DOC: Tile group
  1604. *
  1605. * Tile groups are used to represent tiled monitors with a unique integer
  1606. * identifier. Tiled monitors using DisplayID v1.3 have a unique 8-byte handle,
  1607. * we store this in a tile group, so we have a common identifier for all tiles
  1608. * in a monitor group. The property is called "TILE". Drivers can manage tile
  1609. * groups using drm_mode_create_tile_group(), drm_mode_put_tile_group() and
  1610. * drm_mode_get_tile_group(). But this is only needed for internal panels where
  1611. * the tile group information is exposed through a non-standard way.
  1612. */
  1613. static void drm_tile_group_free(struct kref *kref)
  1614. {
  1615. struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
  1616. struct drm_device *dev = tg->dev;
  1617. mutex_lock(&dev->mode_config.idr_mutex);
  1618. idr_remove(&dev->mode_config.tile_idr, tg->id);
  1619. mutex_unlock(&dev->mode_config.idr_mutex);
  1620. kfree(tg);
  1621. }
  1622. /**
  1623. * drm_mode_put_tile_group - drop a reference to a tile group.
  1624. * @dev: DRM device
  1625. * @tg: tile group to drop reference to.
  1626. *
  1627. * drop reference to tile group and free if 0.
  1628. */
  1629. void drm_mode_put_tile_group(struct drm_device *dev,
  1630. struct drm_tile_group *tg)
  1631. {
  1632. kref_put(&tg->refcount, drm_tile_group_free);
  1633. }
  1634. EXPORT_SYMBOL(drm_mode_put_tile_group);
  1635. /**
  1636. * drm_mode_get_tile_group - get a reference to an existing tile group
  1637. * @dev: DRM device
  1638. * @topology: 8-bytes unique per monitor.
  1639. *
  1640. * Use the unique bytes to get a reference to an existing tile group.
  1641. *
  1642. * RETURNS:
  1643. * tile group or NULL if not found.
  1644. */
  1645. struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
  1646. char topology[8])
  1647. {
  1648. struct drm_tile_group *tg;
  1649. int id;
  1650. mutex_lock(&dev->mode_config.idr_mutex);
  1651. idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
  1652. if (!memcmp(tg->group_data, topology, 8)) {
  1653. if (!kref_get_unless_zero(&tg->refcount))
  1654. tg = NULL;
  1655. mutex_unlock(&dev->mode_config.idr_mutex);
  1656. return tg;
  1657. }
  1658. }
  1659. mutex_unlock(&dev->mode_config.idr_mutex);
  1660. return NULL;
  1661. }
  1662. EXPORT_SYMBOL(drm_mode_get_tile_group);
  1663. /**
  1664. * drm_mode_create_tile_group - create a tile group from a displayid description
  1665. * @dev: DRM device
  1666. * @topology: 8-bytes unique per monitor.
  1667. *
  1668. * Create a tile group for the unique monitor, and get a unique
  1669. * identifier for the tile group.
  1670. *
  1671. * RETURNS:
  1672. * new tile group or error.
  1673. */
  1674. struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
  1675. char topology[8])
  1676. {
  1677. struct drm_tile_group *tg;
  1678. int ret;
  1679. tg = kzalloc(sizeof(*tg), GFP_KERNEL);
  1680. if (!tg)
  1681. return ERR_PTR(-ENOMEM);
  1682. kref_init(&tg->refcount);
  1683. memcpy(tg->group_data, topology, 8);
  1684. tg->dev = dev;
  1685. mutex_lock(&dev->mode_config.idr_mutex);
  1686. ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
  1687. if (ret >= 0) {
  1688. tg->id = ret;
  1689. } else {
  1690. kfree(tg);
  1691. tg = ERR_PTR(ret);
  1692. }
  1693. mutex_unlock(&dev->mode_config.idr_mutex);
  1694. return tg;
  1695. }
  1696. EXPORT_SYMBOL(drm_mode_create_tile_group);