drm_connector.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597
  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_mode_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. };
  86. void drm_connector_ida_init(void)
  87. {
  88. int i;
  89. for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
  90. ida_init(&drm_connector_enum_list[i].ida);
  91. }
  92. void drm_connector_ida_destroy(void)
  93. {
  94. int i;
  95. for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
  96. ida_destroy(&drm_connector_enum_list[i].ida);
  97. }
  98. /**
  99. * drm_connector_get_cmdline_mode - reads the user's cmdline mode
  100. * @connector: connector to quwery
  101. *
  102. * The kernel supports per-connector configuration of its consoles through
  103. * use of the video= parameter. This function parses that option and
  104. * extracts the user's specified mode (or enable/disable status) for a
  105. * particular connector. This is typically only used during the early fbdev
  106. * setup.
  107. */
  108. static void drm_connector_get_cmdline_mode(struct drm_connector *connector)
  109. {
  110. struct drm_cmdline_mode *mode = &connector->cmdline_mode;
  111. char *option = NULL;
  112. if (fb_get_options(connector->name, &option))
  113. return;
  114. if (!drm_mode_parse_command_line_for_connector(option,
  115. connector,
  116. mode))
  117. return;
  118. if (mode->force) {
  119. DRM_INFO("forcing %s connector %s\n", connector->name,
  120. drm_get_connector_force_name(mode->force));
  121. connector->force = mode->force;
  122. }
  123. DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
  124. connector->name,
  125. mode->xres, mode->yres,
  126. mode->refresh_specified ? mode->refresh : 60,
  127. mode->rb ? " reduced blanking" : "",
  128. mode->margins ? " with margins" : "",
  129. mode->interlace ? " interlaced" : "");
  130. }
  131. static void drm_connector_free(struct kref *kref)
  132. {
  133. struct drm_connector *connector =
  134. container_of(kref, struct drm_connector, base.refcount);
  135. struct drm_device *dev = connector->dev;
  136. drm_mode_object_unregister(dev, &connector->base);
  137. connector->funcs->destroy(connector);
  138. }
  139. /**
  140. * drm_connector_init - Init a preallocated connector
  141. * @dev: DRM device
  142. * @connector: the connector to init
  143. * @funcs: callbacks for this connector
  144. * @connector_type: user visible type of the connector
  145. *
  146. * Initialises a preallocated connector. Connectors should be
  147. * subclassed as part of driver connector objects.
  148. *
  149. * Returns:
  150. * Zero on success, error code on failure.
  151. */
  152. int drm_connector_init(struct drm_device *dev,
  153. struct drm_connector *connector,
  154. const struct drm_connector_funcs *funcs,
  155. int connector_type)
  156. {
  157. struct drm_mode_config *config = &dev->mode_config;
  158. int ret;
  159. struct ida *connector_ida =
  160. &drm_connector_enum_list[connector_type].ida;
  161. ret = __drm_mode_object_add(dev, &connector->base,
  162. DRM_MODE_OBJECT_CONNECTOR,
  163. false, drm_connector_free);
  164. if (ret)
  165. return ret;
  166. connector->base.properties = &connector->properties;
  167. connector->dev = dev;
  168. connector->funcs = funcs;
  169. ret = ida_simple_get(&config->connector_ida, 0, 0, GFP_KERNEL);
  170. if (ret < 0)
  171. goto out_put;
  172. connector->index = ret;
  173. ret = 0;
  174. connector->connector_type = connector_type;
  175. connector->connector_type_id =
  176. ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
  177. if (connector->connector_type_id < 0) {
  178. ret = connector->connector_type_id;
  179. goto out_put_id;
  180. }
  181. connector->name =
  182. kasprintf(GFP_KERNEL, "%s-%d",
  183. drm_connector_enum_list[connector_type].name,
  184. connector->connector_type_id);
  185. if (!connector->name) {
  186. ret = -ENOMEM;
  187. goto out_put_type_id;
  188. }
  189. INIT_LIST_HEAD(&connector->probed_modes);
  190. INIT_LIST_HEAD(&connector->modes);
  191. mutex_init(&connector->mutex);
  192. connector->edid_blob_ptr = NULL;
  193. connector->status = connector_status_unknown;
  194. connector->display_info.panel_orientation =
  195. DRM_MODE_PANEL_ORIENTATION_UNKNOWN;
  196. drm_connector_get_cmdline_mode(connector);
  197. /* We should add connectors at the end to avoid upsetting the connector
  198. * index too much. */
  199. spin_lock_irq(&config->connector_list_lock);
  200. list_add_tail(&connector->head, &config->connector_list);
  201. config->num_connector++;
  202. spin_unlock_irq(&config->connector_list_lock);
  203. if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
  204. drm_object_attach_property(&connector->base,
  205. config->edid_property,
  206. 0);
  207. drm_object_attach_property(&connector->base,
  208. config->dpms_property, 0);
  209. drm_object_attach_property(&connector->base,
  210. config->link_status_property,
  211. 0);
  212. drm_object_attach_property(&connector->base,
  213. config->non_desktop_property,
  214. 0);
  215. if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
  216. drm_object_attach_property(&connector->base, config->prop_crtc_id, 0);
  217. }
  218. connector->debugfs_entry = NULL;
  219. out_put_type_id:
  220. if (ret)
  221. ida_simple_remove(connector_ida, connector->connector_type_id);
  222. out_put_id:
  223. if (ret)
  224. ida_simple_remove(&config->connector_ida, connector->index);
  225. out_put:
  226. if (ret)
  227. drm_mode_object_unregister(dev, &connector->base);
  228. return ret;
  229. }
  230. EXPORT_SYMBOL(drm_connector_init);
  231. /**
  232. * drm_mode_connector_attach_encoder - attach a connector to an encoder
  233. * @connector: connector to attach
  234. * @encoder: encoder to attach @connector to
  235. *
  236. * This function links up a connector to an encoder. Note that the routing
  237. * restrictions between encoders and crtcs are exposed to userspace through the
  238. * possible_clones and possible_crtcs bitmasks.
  239. *
  240. * Returns:
  241. * Zero on success, negative errno on failure.
  242. */
  243. int drm_mode_connector_attach_encoder(struct drm_connector *connector,
  244. struct drm_encoder *encoder)
  245. {
  246. int i;
  247. /*
  248. * In the past, drivers have attempted to model the static association
  249. * of connector to encoder in simple connector/encoder devices using a
  250. * direct assignment of connector->encoder = encoder. This connection
  251. * is a logical one and the responsibility of the core, so drivers are
  252. * expected not to mess with this.
  253. *
  254. * Note that the error return should've been enough here, but a large
  255. * majority of drivers ignores the return value, so add in a big WARN
  256. * to get people's attention.
  257. */
  258. if (WARN_ON(connector->encoder))
  259. return -EINVAL;
  260. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  261. if (connector->encoder_ids[i] == 0) {
  262. connector->encoder_ids[i] = encoder->base.id;
  263. return 0;
  264. }
  265. }
  266. return -ENOMEM;
  267. }
  268. EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
  269. static void drm_mode_remove(struct drm_connector *connector,
  270. struct drm_display_mode *mode)
  271. {
  272. list_del(&mode->head);
  273. drm_mode_destroy(connector->dev, mode);
  274. }
  275. /**
  276. * drm_connector_cleanup - cleans up an initialised connector
  277. * @connector: connector to cleanup
  278. *
  279. * Cleans up the connector but doesn't free the object.
  280. */
  281. void drm_connector_cleanup(struct drm_connector *connector)
  282. {
  283. struct drm_device *dev = connector->dev;
  284. struct drm_display_mode *mode, *t;
  285. /* The connector should have been removed from userspace long before
  286. * it is finally destroyed.
  287. */
  288. if (WARN_ON(connector->registered))
  289. drm_connector_unregister(connector);
  290. if (connector->tile_group) {
  291. drm_mode_put_tile_group(dev, connector->tile_group);
  292. connector->tile_group = NULL;
  293. }
  294. list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
  295. drm_mode_remove(connector, mode);
  296. list_for_each_entry_safe(mode, t, &connector->modes, head)
  297. drm_mode_remove(connector, mode);
  298. ida_simple_remove(&drm_connector_enum_list[connector->connector_type].ida,
  299. connector->connector_type_id);
  300. ida_simple_remove(&dev->mode_config.connector_ida,
  301. connector->index);
  302. kfree(connector->display_info.bus_formats);
  303. drm_mode_object_unregister(dev, &connector->base);
  304. kfree(connector->name);
  305. connector->name = NULL;
  306. spin_lock_irq(&dev->mode_config.connector_list_lock);
  307. list_del(&connector->head);
  308. dev->mode_config.num_connector--;
  309. spin_unlock_irq(&dev->mode_config.connector_list_lock);
  310. WARN_ON(connector->state && !connector->funcs->atomic_destroy_state);
  311. if (connector->state && connector->funcs->atomic_destroy_state)
  312. connector->funcs->atomic_destroy_state(connector,
  313. connector->state);
  314. mutex_destroy(&connector->mutex);
  315. memset(connector, 0, sizeof(*connector));
  316. }
  317. EXPORT_SYMBOL(drm_connector_cleanup);
  318. /**
  319. * drm_connector_register - register a connector
  320. * @connector: the connector to register
  321. *
  322. * Register userspace interfaces for a connector
  323. *
  324. * Returns:
  325. * Zero on success, error code on failure.
  326. */
  327. int drm_connector_register(struct drm_connector *connector)
  328. {
  329. int ret = 0;
  330. if (!connector->dev->registered)
  331. return 0;
  332. mutex_lock(&connector->mutex);
  333. if (connector->registered)
  334. goto unlock;
  335. ret = drm_sysfs_connector_add(connector);
  336. if (ret)
  337. goto unlock;
  338. ret = drm_debugfs_connector_add(connector);
  339. if (ret) {
  340. goto err_sysfs;
  341. }
  342. if (connector->funcs->late_register) {
  343. ret = connector->funcs->late_register(connector);
  344. if (ret)
  345. goto err_debugfs;
  346. }
  347. drm_mode_object_register(connector->dev, &connector->base);
  348. connector->registered = true;
  349. goto unlock;
  350. err_debugfs:
  351. drm_debugfs_connector_remove(connector);
  352. err_sysfs:
  353. drm_sysfs_connector_remove(connector);
  354. unlock:
  355. mutex_unlock(&connector->mutex);
  356. return ret;
  357. }
  358. EXPORT_SYMBOL(drm_connector_register);
  359. /**
  360. * drm_connector_unregister - unregister a connector
  361. * @connector: the connector to unregister
  362. *
  363. * Unregister userspace interfaces for a connector
  364. */
  365. void drm_connector_unregister(struct drm_connector *connector)
  366. {
  367. mutex_lock(&connector->mutex);
  368. if (!connector->registered) {
  369. mutex_unlock(&connector->mutex);
  370. return;
  371. }
  372. if (connector->funcs->early_unregister)
  373. connector->funcs->early_unregister(connector);
  374. drm_sysfs_connector_remove(connector);
  375. drm_debugfs_connector_remove(connector);
  376. connector->registered = false;
  377. mutex_unlock(&connector->mutex);
  378. }
  379. EXPORT_SYMBOL(drm_connector_unregister);
  380. void drm_connector_unregister_all(struct drm_device *dev)
  381. {
  382. struct drm_connector *connector;
  383. struct drm_connector_list_iter conn_iter;
  384. drm_connector_list_iter_begin(dev, &conn_iter);
  385. drm_for_each_connector_iter(connector, &conn_iter)
  386. drm_connector_unregister(connector);
  387. drm_connector_list_iter_end(&conn_iter);
  388. }
  389. int drm_connector_register_all(struct drm_device *dev)
  390. {
  391. struct drm_connector *connector;
  392. struct drm_connector_list_iter conn_iter;
  393. int ret = 0;
  394. drm_connector_list_iter_begin(dev, &conn_iter);
  395. drm_for_each_connector_iter(connector, &conn_iter) {
  396. ret = drm_connector_register(connector);
  397. if (ret)
  398. break;
  399. }
  400. drm_connector_list_iter_end(&conn_iter);
  401. if (ret)
  402. drm_connector_unregister_all(dev);
  403. return ret;
  404. }
  405. /**
  406. * drm_get_connector_status_name - return a string for connector status
  407. * @status: connector status to compute name of
  408. *
  409. * In contrast to the other drm_get_*_name functions this one here returns a
  410. * const pointer and hence is threadsafe.
  411. */
  412. const char *drm_get_connector_status_name(enum drm_connector_status status)
  413. {
  414. if (status == connector_status_connected)
  415. return "connected";
  416. else if (status == connector_status_disconnected)
  417. return "disconnected";
  418. else
  419. return "unknown";
  420. }
  421. EXPORT_SYMBOL(drm_get_connector_status_name);
  422. /**
  423. * drm_get_connector_force_name - return a string for connector force
  424. * @force: connector force to get name of
  425. *
  426. * Returns: const pointer to name.
  427. */
  428. const char *drm_get_connector_force_name(enum drm_connector_force force)
  429. {
  430. switch (force) {
  431. case DRM_FORCE_UNSPECIFIED:
  432. return "unspecified";
  433. case DRM_FORCE_OFF:
  434. return "off";
  435. case DRM_FORCE_ON:
  436. return "on";
  437. case DRM_FORCE_ON_DIGITAL:
  438. return "digital";
  439. default:
  440. return "unknown";
  441. }
  442. }
  443. #ifdef CONFIG_LOCKDEP
  444. static struct lockdep_map connector_list_iter_dep_map = {
  445. .name = "drm_connector_list_iter"
  446. };
  447. #endif
  448. /**
  449. * drm_connector_list_iter_begin - initialize a connector_list iterator
  450. * @dev: DRM device
  451. * @iter: connector_list iterator
  452. *
  453. * Sets @iter up to walk the &drm_mode_config.connector_list of @dev. @iter
  454. * must always be cleaned up again by calling drm_connector_list_iter_end().
  455. * Iteration itself happens using drm_connector_list_iter_next() or
  456. * drm_for_each_connector_iter().
  457. */
  458. void drm_connector_list_iter_begin(struct drm_device *dev,
  459. struct drm_connector_list_iter *iter)
  460. {
  461. iter->dev = dev;
  462. iter->conn = NULL;
  463. lock_acquire_shared_recursive(&connector_list_iter_dep_map, 0, 1, NULL, _RET_IP_);
  464. }
  465. EXPORT_SYMBOL(drm_connector_list_iter_begin);
  466. /**
  467. * drm_connector_list_iter_next - return next connector
  468. * @iter: connectr_list iterator
  469. *
  470. * Returns the next connector for @iter, or NULL when the list walk has
  471. * completed.
  472. */
  473. struct drm_connector *
  474. drm_connector_list_iter_next(struct drm_connector_list_iter *iter)
  475. {
  476. struct drm_connector *old_conn = iter->conn;
  477. struct drm_mode_config *config = &iter->dev->mode_config;
  478. struct list_head *lhead;
  479. unsigned long flags;
  480. spin_lock_irqsave(&config->connector_list_lock, flags);
  481. lhead = old_conn ? &old_conn->head : &config->connector_list;
  482. do {
  483. if (lhead->next == &config->connector_list) {
  484. iter->conn = NULL;
  485. break;
  486. }
  487. lhead = lhead->next;
  488. iter->conn = list_entry(lhead, struct drm_connector, head);
  489. /* loop until it's not a zombie connector */
  490. } while (!kref_get_unless_zero(&iter->conn->base.refcount));
  491. spin_unlock_irqrestore(&config->connector_list_lock, flags);
  492. if (old_conn)
  493. drm_connector_put(old_conn);
  494. return iter->conn;
  495. }
  496. EXPORT_SYMBOL(drm_connector_list_iter_next);
  497. /**
  498. * drm_connector_list_iter_end - tear down a connector_list iterator
  499. * @iter: connector_list iterator
  500. *
  501. * Tears down @iter and releases any resources (like &drm_connector references)
  502. * acquired while walking the list. This must always be called, both when the
  503. * iteration completes fully or when it was aborted without walking the entire
  504. * list.
  505. */
  506. void drm_connector_list_iter_end(struct drm_connector_list_iter *iter)
  507. {
  508. iter->dev = NULL;
  509. if (iter->conn)
  510. drm_connector_put(iter->conn);
  511. lock_release(&connector_list_iter_dep_map, 0, _RET_IP_);
  512. }
  513. EXPORT_SYMBOL(drm_connector_list_iter_end);
  514. static const struct drm_prop_enum_list drm_subpixel_enum_list[] = {
  515. { SubPixelUnknown, "Unknown" },
  516. { SubPixelHorizontalRGB, "Horizontal RGB" },
  517. { SubPixelHorizontalBGR, "Horizontal BGR" },
  518. { SubPixelVerticalRGB, "Vertical RGB" },
  519. { SubPixelVerticalBGR, "Vertical BGR" },
  520. { SubPixelNone, "None" },
  521. };
  522. /**
  523. * drm_get_subpixel_order_name - return a string for a given subpixel enum
  524. * @order: enum of subpixel_order
  525. *
  526. * Note you could abuse this and return something out of bounds, but that
  527. * would be a caller error. No unscrubbed user data should make it here.
  528. */
  529. const char *drm_get_subpixel_order_name(enum subpixel_order order)
  530. {
  531. return drm_subpixel_enum_list[order].name;
  532. }
  533. EXPORT_SYMBOL(drm_get_subpixel_order_name);
  534. static const struct drm_prop_enum_list drm_dpms_enum_list[] = {
  535. { DRM_MODE_DPMS_ON, "On" },
  536. { DRM_MODE_DPMS_STANDBY, "Standby" },
  537. { DRM_MODE_DPMS_SUSPEND, "Suspend" },
  538. { DRM_MODE_DPMS_OFF, "Off" }
  539. };
  540. DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
  541. static const struct drm_prop_enum_list drm_link_status_enum_list[] = {
  542. { DRM_MODE_LINK_STATUS_GOOD, "Good" },
  543. { DRM_MODE_LINK_STATUS_BAD, "Bad" },
  544. };
  545. /**
  546. * drm_display_info_set_bus_formats - set the supported bus formats
  547. * @info: display info to store bus formats in
  548. * @formats: array containing the supported bus formats
  549. * @num_formats: the number of entries in the fmts array
  550. *
  551. * Store the supported bus formats in display info structure.
  552. * See MEDIA_BUS_FMT_* definitions in include/uapi/linux/media-bus-format.h for
  553. * a full list of available formats.
  554. */
  555. int drm_display_info_set_bus_formats(struct drm_display_info *info,
  556. const u32 *formats,
  557. unsigned int num_formats)
  558. {
  559. u32 *fmts = NULL;
  560. if (!formats && num_formats)
  561. return -EINVAL;
  562. if (formats && num_formats) {
  563. fmts = kmemdup(formats, sizeof(*formats) * num_formats,
  564. GFP_KERNEL);
  565. if (!fmts)
  566. return -ENOMEM;
  567. }
  568. kfree(info->bus_formats);
  569. info->bus_formats = fmts;
  570. info->num_bus_formats = num_formats;
  571. return 0;
  572. }
  573. EXPORT_SYMBOL(drm_display_info_set_bus_formats);
  574. /* Optional connector properties. */
  575. static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] = {
  576. { DRM_MODE_SCALE_NONE, "None" },
  577. { DRM_MODE_SCALE_FULLSCREEN, "Full" },
  578. { DRM_MODE_SCALE_CENTER, "Center" },
  579. { DRM_MODE_SCALE_ASPECT, "Full aspect" },
  580. };
  581. static const struct drm_prop_enum_list drm_aspect_ratio_enum_list[] = {
  582. { DRM_MODE_PICTURE_ASPECT_NONE, "Automatic" },
  583. { DRM_MODE_PICTURE_ASPECT_4_3, "4:3" },
  584. { DRM_MODE_PICTURE_ASPECT_16_9, "16:9" },
  585. };
  586. static const struct drm_prop_enum_list drm_panel_orientation_enum_list[] = {
  587. { DRM_MODE_PANEL_ORIENTATION_NORMAL, "Normal" },
  588. { DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP, "Upside Down" },
  589. { DRM_MODE_PANEL_ORIENTATION_LEFT_UP, "Left Side Up" },
  590. { DRM_MODE_PANEL_ORIENTATION_RIGHT_UP, "Right Side Up" },
  591. };
  592. static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] = {
  593. { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
  594. { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
  595. { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
  596. };
  597. DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
  598. static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] = {
  599. { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
  600. { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
  601. { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
  602. };
  603. DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
  604. drm_dvi_i_subconnector_enum_list)
  605. static const struct drm_prop_enum_list drm_tv_select_enum_list[] = {
  606. { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
  607. { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
  608. { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
  609. { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
  610. { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
  611. };
  612. DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
  613. static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] = {
  614. { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
  615. { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
  616. { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
  617. { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
  618. { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
  619. };
  620. DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
  621. drm_tv_subconnector_enum_list)
  622. /**
  623. * DOC: standard connector properties
  624. *
  625. * DRM connectors have a few standardized properties:
  626. *
  627. * EDID:
  628. * Blob property which contains the current EDID read from the sink. This
  629. * is useful to parse sink identification information like vendor, model
  630. * and serial. Drivers should update this property by calling
  631. * drm_mode_connector_update_edid_property(), usually after having parsed
  632. * the EDID using drm_add_edid_modes(). Userspace cannot change this
  633. * property.
  634. * DPMS:
  635. * Legacy property for setting the power state of the connector. For atomic
  636. * drivers this is only provided for backwards compatibility with existing
  637. * drivers, it remaps to controlling the "ACTIVE" property on the CRTC the
  638. * connector is linked to. Drivers should never set this property directly,
  639. * it is handled by the DRM core by calling the &drm_connector_funcs.dpms
  640. * callback. For atomic drivers the remapping to the "ACTIVE" property is
  641. * implemented in the DRM core. This is the only standard connector
  642. * property that userspace can change.
  643. *
  644. * Note that this property cannot be set through the MODE_ATOMIC ioctl,
  645. * userspace must use "ACTIVE" on the CRTC instead.
  646. *
  647. * WARNING:
  648. *
  649. * For userspace also running on legacy drivers the "DPMS" semantics are a
  650. * lot more complicated. First, userspace cannot rely on the "DPMS" value
  651. * returned by the GETCONNECTOR actually reflecting reality, because many
  652. * drivers fail to update it. For atomic drivers this is taken care of in
  653. * drm_atomic_helper_update_legacy_modeset_state().
  654. *
  655. * The second issue is that the DPMS state is only well-defined when the
  656. * connector is connected to a CRTC. In atomic the DRM core enforces that
  657. * "ACTIVE" is off in such a case, no such checks exists for "DPMS".
  658. *
  659. * Finally, when enabling an output using the legacy SETCONFIG ioctl then
  660. * "DPMS" is forced to ON. But see above, that might not be reflected in
  661. * the software value on legacy drivers.
  662. *
  663. * Summarizing: Only set "DPMS" when the connector is known to be enabled,
  664. * assume that a successful SETCONFIG call also sets "DPMS" to on, and
  665. * never read back the value of "DPMS" because it can be incorrect.
  666. * PATH:
  667. * Connector path property to identify how this sink is physically
  668. * connected. Used by DP MST. This should be set by calling
  669. * drm_mode_connector_set_path_property(), in the case of DP MST with the
  670. * path property the MST manager created. Userspace cannot change this
  671. * property.
  672. * TILE:
  673. * Connector tile group property to indicate how a set of DRM connector
  674. * compose together into one logical screen. This is used by both high-res
  675. * external screens (often only using a single cable, but exposing multiple
  676. * DP MST sinks), or high-res integrated panels (like dual-link DSI) which
  677. * are not gen-locked. Note that for tiled panels which are genlocked, like
  678. * dual-link LVDS or dual-link DSI, the driver should try to not expose the
  679. * tiling and virtualize both &drm_crtc and &drm_plane if needed. Drivers
  680. * should update this value using drm_mode_connector_set_tile_property().
  681. * Userspace cannot change this property.
  682. * link-status:
  683. * Connector link-status property to indicate the status of link. The default
  684. * value of link-status is "GOOD". If something fails during or after modeset,
  685. * the kernel driver may set this to "BAD" and issue a hotplug uevent. Drivers
  686. * should update this value using drm_mode_connector_set_link_status_property().
  687. * non_desktop:
  688. * Indicates the output should be ignored for purposes of displaying a
  689. * standard desktop environment or console. This is most likely because
  690. * the output device is not rectilinear.
  691. *
  692. * Connectors also have one standardized atomic property:
  693. *
  694. * CRTC_ID:
  695. * Mode object ID of the &drm_crtc this connector should be connected to.
  696. *
  697. * Connectors for LCD panels may also have one standardized property:
  698. *
  699. * panel orientation:
  700. * On some devices the LCD panel is mounted in the casing in such a way
  701. * that the up/top side of the panel does not match with the top side of
  702. * the device. Userspace can use this property to check for this.
  703. * Note that input coordinates from touchscreens (input devices with
  704. * INPUT_PROP_DIRECT) will still map 1:1 to the actual LCD panel
  705. * coordinates, so if userspace rotates the picture to adjust for
  706. * the orientation it must also apply the same transformation to the
  707. * touchscreen input coordinates.
  708. */
  709. int drm_connector_create_standard_properties(struct drm_device *dev)
  710. {
  711. struct drm_property *prop;
  712. prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
  713. DRM_MODE_PROP_IMMUTABLE,
  714. "EDID", 0);
  715. if (!prop)
  716. return -ENOMEM;
  717. dev->mode_config.edid_property = prop;
  718. prop = drm_property_create_enum(dev, 0,
  719. "DPMS", drm_dpms_enum_list,
  720. ARRAY_SIZE(drm_dpms_enum_list));
  721. if (!prop)
  722. return -ENOMEM;
  723. dev->mode_config.dpms_property = prop;
  724. prop = drm_property_create(dev,
  725. DRM_MODE_PROP_BLOB |
  726. DRM_MODE_PROP_IMMUTABLE,
  727. "PATH", 0);
  728. if (!prop)
  729. return -ENOMEM;
  730. dev->mode_config.path_property = prop;
  731. prop = drm_property_create(dev,
  732. DRM_MODE_PROP_BLOB |
  733. DRM_MODE_PROP_IMMUTABLE,
  734. "TILE", 0);
  735. if (!prop)
  736. return -ENOMEM;
  737. dev->mode_config.tile_property = prop;
  738. prop = drm_property_create_enum(dev, 0, "link-status",
  739. drm_link_status_enum_list,
  740. ARRAY_SIZE(drm_link_status_enum_list));
  741. if (!prop)
  742. return -ENOMEM;
  743. dev->mode_config.link_status_property = prop;
  744. prop = drm_property_create_bool(dev, DRM_MODE_PROP_IMMUTABLE, "non-desktop");
  745. if (!prop)
  746. return -ENOMEM;
  747. dev->mode_config.non_desktop_property = prop;
  748. return 0;
  749. }
  750. /**
  751. * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
  752. * @dev: DRM device
  753. *
  754. * Called by a driver the first time a DVI-I connector is made.
  755. */
  756. int drm_mode_create_dvi_i_properties(struct drm_device *dev)
  757. {
  758. struct drm_property *dvi_i_selector;
  759. struct drm_property *dvi_i_subconnector;
  760. if (dev->mode_config.dvi_i_select_subconnector_property)
  761. return 0;
  762. dvi_i_selector =
  763. drm_property_create_enum(dev, 0,
  764. "select subconnector",
  765. drm_dvi_i_select_enum_list,
  766. ARRAY_SIZE(drm_dvi_i_select_enum_list));
  767. dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
  768. dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  769. "subconnector",
  770. drm_dvi_i_subconnector_enum_list,
  771. ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
  772. dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
  773. return 0;
  774. }
  775. EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
  776. /**
  777. * drm_create_tv_properties - create TV specific connector properties
  778. * @dev: DRM device
  779. * @num_modes: number of different TV formats (modes) supported
  780. * @modes: array of pointers to strings containing name of each format
  781. *
  782. * Called by a driver's TV initialization routine, this function creates
  783. * the TV specific connector properties for a given device. Caller is
  784. * responsible for allocating a list of format names and passing them to
  785. * this routine.
  786. */
  787. int drm_mode_create_tv_properties(struct drm_device *dev,
  788. unsigned int num_modes,
  789. const char * const modes[])
  790. {
  791. struct drm_property *tv_selector;
  792. struct drm_property *tv_subconnector;
  793. unsigned int i;
  794. if (dev->mode_config.tv_select_subconnector_property)
  795. return 0;
  796. /*
  797. * Basic connector properties
  798. */
  799. tv_selector = drm_property_create_enum(dev, 0,
  800. "select subconnector",
  801. drm_tv_select_enum_list,
  802. ARRAY_SIZE(drm_tv_select_enum_list));
  803. if (!tv_selector)
  804. goto nomem;
  805. dev->mode_config.tv_select_subconnector_property = tv_selector;
  806. tv_subconnector =
  807. drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  808. "subconnector",
  809. drm_tv_subconnector_enum_list,
  810. ARRAY_SIZE(drm_tv_subconnector_enum_list));
  811. if (!tv_subconnector)
  812. goto nomem;
  813. dev->mode_config.tv_subconnector_property = tv_subconnector;
  814. /*
  815. * Other, TV specific properties: margins & TV modes.
  816. */
  817. dev->mode_config.tv_left_margin_property =
  818. drm_property_create_range(dev, 0, "left margin", 0, 100);
  819. if (!dev->mode_config.tv_left_margin_property)
  820. goto nomem;
  821. dev->mode_config.tv_right_margin_property =
  822. drm_property_create_range(dev, 0, "right margin", 0, 100);
  823. if (!dev->mode_config.tv_right_margin_property)
  824. goto nomem;
  825. dev->mode_config.tv_top_margin_property =
  826. drm_property_create_range(dev, 0, "top margin", 0, 100);
  827. if (!dev->mode_config.tv_top_margin_property)
  828. goto nomem;
  829. dev->mode_config.tv_bottom_margin_property =
  830. drm_property_create_range(dev, 0, "bottom margin", 0, 100);
  831. if (!dev->mode_config.tv_bottom_margin_property)
  832. goto nomem;
  833. dev->mode_config.tv_mode_property =
  834. drm_property_create(dev, DRM_MODE_PROP_ENUM,
  835. "mode", num_modes);
  836. if (!dev->mode_config.tv_mode_property)
  837. goto nomem;
  838. for (i = 0; i < num_modes; i++)
  839. drm_property_add_enum(dev->mode_config.tv_mode_property, i,
  840. i, modes[i]);
  841. dev->mode_config.tv_brightness_property =
  842. drm_property_create_range(dev, 0, "brightness", 0, 100);
  843. if (!dev->mode_config.tv_brightness_property)
  844. goto nomem;
  845. dev->mode_config.tv_contrast_property =
  846. drm_property_create_range(dev, 0, "contrast", 0, 100);
  847. if (!dev->mode_config.tv_contrast_property)
  848. goto nomem;
  849. dev->mode_config.tv_flicker_reduction_property =
  850. drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
  851. if (!dev->mode_config.tv_flicker_reduction_property)
  852. goto nomem;
  853. dev->mode_config.tv_overscan_property =
  854. drm_property_create_range(dev, 0, "overscan", 0, 100);
  855. if (!dev->mode_config.tv_overscan_property)
  856. goto nomem;
  857. dev->mode_config.tv_saturation_property =
  858. drm_property_create_range(dev, 0, "saturation", 0, 100);
  859. if (!dev->mode_config.tv_saturation_property)
  860. goto nomem;
  861. dev->mode_config.tv_hue_property =
  862. drm_property_create_range(dev, 0, "hue", 0, 100);
  863. if (!dev->mode_config.tv_hue_property)
  864. goto nomem;
  865. return 0;
  866. nomem:
  867. return -ENOMEM;
  868. }
  869. EXPORT_SYMBOL(drm_mode_create_tv_properties);
  870. /**
  871. * drm_mode_create_scaling_mode_property - create scaling mode property
  872. * @dev: DRM device
  873. *
  874. * Called by a driver the first time it's needed, must be attached to desired
  875. * connectors.
  876. *
  877. * Atomic drivers should use drm_connector_attach_scaling_mode_property()
  878. * instead to correctly assign &drm_connector_state.picture_aspect_ratio
  879. * in the atomic state.
  880. */
  881. int drm_mode_create_scaling_mode_property(struct drm_device *dev)
  882. {
  883. struct drm_property *scaling_mode;
  884. if (dev->mode_config.scaling_mode_property)
  885. return 0;
  886. scaling_mode =
  887. drm_property_create_enum(dev, 0, "scaling mode",
  888. drm_scaling_mode_enum_list,
  889. ARRAY_SIZE(drm_scaling_mode_enum_list));
  890. dev->mode_config.scaling_mode_property = scaling_mode;
  891. return 0;
  892. }
  893. EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
  894. /**
  895. * drm_connector_attach_scaling_mode_property - attach atomic scaling mode property
  896. * @connector: connector to attach scaling mode property on.
  897. * @scaling_mode_mask: or'ed mask of BIT(%DRM_MODE_SCALE_\*).
  898. *
  899. * This is used to add support for scaling mode to atomic drivers.
  900. * The scaling mode will be set to &drm_connector_state.picture_aspect_ratio
  901. * and can be used from &drm_connector_helper_funcs->atomic_check for validation.
  902. *
  903. * This is the atomic version of drm_mode_create_scaling_mode_property().
  904. *
  905. * Returns:
  906. * Zero on success, negative errno on failure.
  907. */
  908. int drm_connector_attach_scaling_mode_property(struct drm_connector *connector,
  909. u32 scaling_mode_mask)
  910. {
  911. struct drm_device *dev = connector->dev;
  912. struct drm_property *scaling_mode_property;
  913. int i, j = 0;
  914. const unsigned valid_scaling_mode_mask =
  915. (1U << ARRAY_SIZE(drm_scaling_mode_enum_list)) - 1;
  916. if (WARN_ON(hweight32(scaling_mode_mask) < 2 ||
  917. scaling_mode_mask & ~valid_scaling_mode_mask))
  918. return -EINVAL;
  919. scaling_mode_property =
  920. drm_property_create(dev, DRM_MODE_PROP_ENUM, "scaling mode",
  921. hweight32(scaling_mode_mask));
  922. if (!scaling_mode_property)
  923. return -ENOMEM;
  924. for (i = 0; i < ARRAY_SIZE(drm_scaling_mode_enum_list); i++) {
  925. int ret;
  926. if (!(BIT(i) & scaling_mode_mask))
  927. continue;
  928. ret = drm_property_add_enum(scaling_mode_property, j++,
  929. drm_scaling_mode_enum_list[i].type,
  930. drm_scaling_mode_enum_list[i].name);
  931. if (ret) {
  932. drm_property_destroy(dev, scaling_mode_property);
  933. return ret;
  934. }
  935. }
  936. drm_object_attach_property(&connector->base,
  937. scaling_mode_property, 0);
  938. connector->scaling_mode_property = scaling_mode_property;
  939. return 0;
  940. }
  941. EXPORT_SYMBOL(drm_connector_attach_scaling_mode_property);
  942. /**
  943. * drm_mode_create_aspect_ratio_property - create aspect ratio property
  944. * @dev: DRM device
  945. *
  946. * Called by a driver the first time it's needed, must be attached to desired
  947. * connectors.
  948. *
  949. * Returns:
  950. * Zero on success, negative errno on failure.
  951. */
  952. int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
  953. {
  954. if (dev->mode_config.aspect_ratio_property)
  955. return 0;
  956. dev->mode_config.aspect_ratio_property =
  957. drm_property_create_enum(dev, 0, "aspect ratio",
  958. drm_aspect_ratio_enum_list,
  959. ARRAY_SIZE(drm_aspect_ratio_enum_list));
  960. if (dev->mode_config.aspect_ratio_property == NULL)
  961. return -ENOMEM;
  962. return 0;
  963. }
  964. EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
  965. /**
  966. * drm_mode_create_suggested_offset_properties - create suggests offset properties
  967. * @dev: DRM device
  968. *
  969. * Create the the suggested x/y offset property for connectors.
  970. */
  971. int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
  972. {
  973. if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
  974. return 0;
  975. dev->mode_config.suggested_x_property =
  976. drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
  977. dev->mode_config.suggested_y_property =
  978. drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
  979. if (dev->mode_config.suggested_x_property == NULL ||
  980. dev->mode_config.suggested_y_property == NULL)
  981. return -ENOMEM;
  982. return 0;
  983. }
  984. EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
  985. /**
  986. * drm_mode_connector_set_path_property - set tile property on connector
  987. * @connector: connector to set property on.
  988. * @path: path to use for property; must not be NULL.
  989. *
  990. * This creates a property to expose to userspace to specify a
  991. * connector path. This is mainly used for DisplayPort MST where
  992. * connectors have a topology and we want to allow userspace to give
  993. * them more meaningful names.
  994. *
  995. * Returns:
  996. * Zero on success, negative errno on failure.
  997. */
  998. int drm_mode_connector_set_path_property(struct drm_connector *connector,
  999. const char *path)
  1000. {
  1001. struct drm_device *dev = connector->dev;
  1002. int ret;
  1003. ret = drm_property_replace_global_blob(dev,
  1004. &connector->path_blob_ptr,
  1005. strlen(path) + 1,
  1006. path,
  1007. &connector->base,
  1008. dev->mode_config.path_property);
  1009. return ret;
  1010. }
  1011. EXPORT_SYMBOL(drm_mode_connector_set_path_property);
  1012. /**
  1013. * drm_mode_connector_set_tile_property - set tile property on connector
  1014. * @connector: connector to set property on.
  1015. *
  1016. * This looks up the tile information for a connector, and creates a
  1017. * property for userspace to parse if it exists. The property is of
  1018. * the form of 8 integers using ':' as a separator.
  1019. *
  1020. * Returns:
  1021. * Zero on success, errno on failure.
  1022. */
  1023. int drm_mode_connector_set_tile_property(struct drm_connector *connector)
  1024. {
  1025. struct drm_device *dev = connector->dev;
  1026. char tile[256];
  1027. int ret;
  1028. if (!connector->has_tile) {
  1029. ret = drm_property_replace_global_blob(dev,
  1030. &connector->tile_blob_ptr,
  1031. 0,
  1032. NULL,
  1033. &connector->base,
  1034. dev->mode_config.tile_property);
  1035. return ret;
  1036. }
  1037. snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
  1038. connector->tile_group->id, connector->tile_is_single_monitor,
  1039. connector->num_h_tile, connector->num_v_tile,
  1040. connector->tile_h_loc, connector->tile_v_loc,
  1041. connector->tile_h_size, connector->tile_v_size);
  1042. ret = drm_property_replace_global_blob(dev,
  1043. &connector->tile_blob_ptr,
  1044. strlen(tile) + 1,
  1045. tile,
  1046. &connector->base,
  1047. dev->mode_config.tile_property);
  1048. return ret;
  1049. }
  1050. EXPORT_SYMBOL(drm_mode_connector_set_tile_property);
  1051. /**
  1052. * drm_mode_connector_update_edid_property - update the edid property of a connector
  1053. * @connector: drm connector
  1054. * @edid: new value of the edid property
  1055. *
  1056. * This function creates a new blob modeset object and assigns its id to the
  1057. * connector's edid property.
  1058. *
  1059. * Returns:
  1060. * Zero on success, negative errno on failure.
  1061. */
  1062. int drm_mode_connector_update_edid_property(struct drm_connector *connector,
  1063. const struct edid *edid)
  1064. {
  1065. struct drm_device *dev = connector->dev;
  1066. size_t size = 0;
  1067. int ret;
  1068. /* ignore requests to set edid when overridden */
  1069. if (connector->override_edid)
  1070. return 0;
  1071. if (edid)
  1072. size = EDID_LENGTH * (1 + edid->extensions);
  1073. drm_object_property_set_value(&connector->base,
  1074. dev->mode_config.non_desktop_property,
  1075. connector->display_info.non_desktop);
  1076. ret = drm_property_replace_global_blob(dev,
  1077. &connector->edid_blob_ptr,
  1078. size,
  1079. edid,
  1080. &connector->base,
  1081. dev->mode_config.edid_property);
  1082. return ret;
  1083. }
  1084. EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
  1085. /**
  1086. * drm_mode_connector_set_link_status_property - Set link status property of a connector
  1087. * @connector: drm connector
  1088. * @link_status: new value of link status property (0: Good, 1: Bad)
  1089. *
  1090. * In usual working scenario, this link status property will always be set to
  1091. * "GOOD". If something fails during or after a mode set, the kernel driver
  1092. * may set this link status property to "BAD". The caller then needs to send a
  1093. * hotplug uevent for userspace to re-check the valid modes through
  1094. * GET_CONNECTOR_IOCTL and retry modeset.
  1095. *
  1096. * Note: Drivers cannot rely on userspace to support this property and
  1097. * issue a modeset. As such, they may choose to handle issues (like
  1098. * re-training a link) without userspace's intervention.
  1099. *
  1100. * The reason for adding this property is to handle link training failures, but
  1101. * it is not limited to DP or link training. For example, if we implement
  1102. * asynchronous setcrtc, this property can be used to report any failures in that.
  1103. */
  1104. void drm_mode_connector_set_link_status_property(struct drm_connector *connector,
  1105. uint64_t link_status)
  1106. {
  1107. struct drm_device *dev = connector->dev;
  1108. drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
  1109. connector->state->link_status = link_status;
  1110. drm_modeset_unlock(&dev->mode_config.connection_mutex);
  1111. }
  1112. EXPORT_SYMBOL(drm_mode_connector_set_link_status_property);
  1113. /**
  1114. * drm_connector_init_panel_orientation_property -
  1115. * initialize the connecters panel_orientation property
  1116. * @connector: connector for which to init the panel-orientation property.
  1117. * @width: width in pixels of the panel, used for panel quirk detection
  1118. * @height: height in pixels of the panel, used for panel quirk detection
  1119. *
  1120. * This function should only be called for built-in panels, after setting
  1121. * connector->display_info.panel_orientation first (if known).
  1122. *
  1123. * This function will check for platform specific (e.g. DMI based) quirks
  1124. * overriding display_info.panel_orientation first, then if panel_orientation
  1125. * is not DRM_MODE_PANEL_ORIENTATION_UNKNOWN it will attach the
  1126. * "panel orientation" property to the connector.
  1127. *
  1128. * Returns:
  1129. * Zero on success, negative errno on failure.
  1130. */
  1131. int drm_connector_init_panel_orientation_property(
  1132. struct drm_connector *connector, int width, int height)
  1133. {
  1134. struct drm_device *dev = connector->dev;
  1135. struct drm_display_info *info = &connector->display_info;
  1136. struct drm_property *prop;
  1137. int orientation_quirk;
  1138. orientation_quirk = drm_get_panel_orientation_quirk(width, height);
  1139. if (orientation_quirk != DRM_MODE_PANEL_ORIENTATION_UNKNOWN)
  1140. info->panel_orientation = orientation_quirk;
  1141. if (info->panel_orientation == DRM_MODE_PANEL_ORIENTATION_UNKNOWN)
  1142. return 0;
  1143. prop = dev->mode_config.panel_orientation_property;
  1144. if (!prop) {
  1145. prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  1146. "panel orientation",
  1147. drm_panel_orientation_enum_list,
  1148. ARRAY_SIZE(drm_panel_orientation_enum_list));
  1149. if (!prop)
  1150. return -ENOMEM;
  1151. dev->mode_config.panel_orientation_property = prop;
  1152. }
  1153. drm_object_attach_property(&connector->base, prop,
  1154. info->panel_orientation);
  1155. return 0;
  1156. }
  1157. EXPORT_SYMBOL(drm_connector_init_panel_orientation_property);
  1158. int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
  1159. struct drm_property *property,
  1160. uint64_t value)
  1161. {
  1162. int ret = -EINVAL;
  1163. struct drm_connector *connector = obj_to_connector(obj);
  1164. /* Do DPMS ourselves */
  1165. if (property == connector->dev->mode_config.dpms_property) {
  1166. ret = (*connector->funcs->dpms)(connector, (int)value);
  1167. } else if (connector->funcs->set_property)
  1168. ret = connector->funcs->set_property(connector, property, value);
  1169. if (!ret)
  1170. drm_object_property_set_value(&connector->base, property, value);
  1171. return ret;
  1172. }
  1173. int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
  1174. void *data, struct drm_file *file_priv)
  1175. {
  1176. struct drm_mode_connector_set_property *conn_set_prop = data;
  1177. struct drm_mode_obj_set_property obj_set_prop = {
  1178. .value = conn_set_prop->value,
  1179. .prop_id = conn_set_prop->prop_id,
  1180. .obj_id = conn_set_prop->connector_id,
  1181. .obj_type = DRM_MODE_OBJECT_CONNECTOR
  1182. };
  1183. /* It does all the locking and checking we need */
  1184. return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
  1185. }
  1186. static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
  1187. {
  1188. /* For atomic drivers only state objects are synchronously updated and
  1189. * protected by modeset locks, so check those first. */
  1190. if (connector->state)
  1191. return connector->state->best_encoder;
  1192. return connector->encoder;
  1193. }
  1194. static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
  1195. const struct drm_file *file_priv)
  1196. {
  1197. /*
  1198. * If user-space hasn't configured the driver to expose the stereo 3D
  1199. * modes, don't expose them.
  1200. */
  1201. if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
  1202. return false;
  1203. return true;
  1204. }
  1205. int drm_mode_getconnector(struct drm_device *dev, void *data,
  1206. struct drm_file *file_priv)
  1207. {
  1208. struct drm_mode_get_connector *out_resp = data;
  1209. struct drm_connector *connector;
  1210. struct drm_encoder *encoder;
  1211. struct drm_display_mode *mode;
  1212. int mode_count = 0;
  1213. int encoders_count = 0;
  1214. int ret = 0;
  1215. int copied = 0;
  1216. int i;
  1217. struct drm_mode_modeinfo u_mode;
  1218. struct drm_mode_modeinfo __user *mode_ptr;
  1219. uint32_t __user *encoder_ptr;
  1220. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1221. return -EINVAL;
  1222. memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
  1223. connector = drm_connector_lookup(dev, file_priv, out_resp->connector_id);
  1224. if (!connector)
  1225. return -ENOENT;
  1226. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++)
  1227. if (connector->encoder_ids[i] != 0)
  1228. encoders_count++;
  1229. if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
  1230. copied = 0;
  1231. encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
  1232. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  1233. if (connector->encoder_ids[i] != 0) {
  1234. if (put_user(connector->encoder_ids[i],
  1235. encoder_ptr + copied)) {
  1236. ret = -EFAULT;
  1237. goto out;
  1238. }
  1239. copied++;
  1240. }
  1241. }
  1242. }
  1243. out_resp->count_encoders = encoders_count;
  1244. out_resp->connector_id = connector->base.id;
  1245. out_resp->connector_type = connector->connector_type;
  1246. out_resp->connector_type_id = connector->connector_type_id;
  1247. mutex_lock(&dev->mode_config.mutex);
  1248. if (out_resp->count_modes == 0) {
  1249. connector->funcs->fill_modes(connector,
  1250. dev->mode_config.max_width,
  1251. dev->mode_config.max_height);
  1252. }
  1253. out_resp->mm_width = connector->display_info.width_mm;
  1254. out_resp->mm_height = connector->display_info.height_mm;
  1255. out_resp->subpixel = connector->display_info.subpixel_order;
  1256. out_resp->connection = connector->status;
  1257. /* delayed so we get modes regardless of pre-fill_modes state */
  1258. list_for_each_entry(mode, &connector->modes, head)
  1259. if (drm_mode_expose_to_userspace(mode, file_priv))
  1260. mode_count++;
  1261. /*
  1262. * This ioctl is called twice, once to determine how much space is
  1263. * needed, and the 2nd time to fill it.
  1264. */
  1265. if ((out_resp->count_modes >= mode_count) && mode_count) {
  1266. copied = 0;
  1267. mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
  1268. list_for_each_entry(mode, &connector->modes, head) {
  1269. if (!drm_mode_expose_to_userspace(mode, file_priv))
  1270. continue;
  1271. drm_mode_convert_to_umode(&u_mode, mode);
  1272. if (copy_to_user(mode_ptr + copied,
  1273. &u_mode, sizeof(u_mode))) {
  1274. ret = -EFAULT;
  1275. mutex_unlock(&dev->mode_config.mutex);
  1276. goto out;
  1277. }
  1278. copied++;
  1279. }
  1280. }
  1281. out_resp->count_modes = mode_count;
  1282. mutex_unlock(&dev->mode_config.mutex);
  1283. drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
  1284. encoder = drm_connector_get_encoder(connector);
  1285. if (encoder)
  1286. out_resp->encoder_id = encoder->base.id;
  1287. else
  1288. out_resp->encoder_id = 0;
  1289. /* Only grab properties after probing, to make sure EDID and other
  1290. * properties reflect the latest status. */
  1291. ret = drm_mode_object_get_properties(&connector->base, file_priv->atomic,
  1292. (uint32_t __user *)(unsigned long)(out_resp->props_ptr),
  1293. (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
  1294. &out_resp->count_props);
  1295. drm_modeset_unlock(&dev->mode_config.connection_mutex);
  1296. out:
  1297. drm_connector_put(connector);
  1298. return ret;
  1299. }
  1300. /**
  1301. * DOC: Tile group
  1302. *
  1303. * Tile groups are used to represent tiled monitors with a unique integer
  1304. * identifier. Tiled monitors using DisplayID v1.3 have a unique 8-byte handle,
  1305. * we store this in a tile group, so we have a common identifier for all tiles
  1306. * in a monitor group. The property is called "TILE". Drivers can manage tile
  1307. * groups using drm_mode_create_tile_group(), drm_mode_put_tile_group() and
  1308. * drm_mode_get_tile_group(). But this is only needed for internal panels where
  1309. * the tile group information is exposed through a non-standard way.
  1310. */
  1311. static void drm_tile_group_free(struct kref *kref)
  1312. {
  1313. struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
  1314. struct drm_device *dev = tg->dev;
  1315. mutex_lock(&dev->mode_config.idr_mutex);
  1316. idr_remove(&dev->mode_config.tile_idr, tg->id);
  1317. mutex_unlock(&dev->mode_config.idr_mutex);
  1318. kfree(tg);
  1319. }
  1320. /**
  1321. * drm_mode_put_tile_group - drop a reference to a tile group.
  1322. * @dev: DRM device
  1323. * @tg: tile group to drop reference to.
  1324. *
  1325. * drop reference to tile group and free if 0.
  1326. */
  1327. void drm_mode_put_tile_group(struct drm_device *dev,
  1328. struct drm_tile_group *tg)
  1329. {
  1330. kref_put(&tg->refcount, drm_tile_group_free);
  1331. }
  1332. EXPORT_SYMBOL(drm_mode_put_tile_group);
  1333. /**
  1334. * drm_mode_get_tile_group - get a reference to an existing tile group
  1335. * @dev: DRM device
  1336. * @topology: 8-bytes unique per monitor.
  1337. *
  1338. * Use the unique bytes to get a reference to an existing tile group.
  1339. *
  1340. * RETURNS:
  1341. * tile group or NULL if not found.
  1342. */
  1343. struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
  1344. char topology[8])
  1345. {
  1346. struct drm_tile_group *tg;
  1347. int id;
  1348. mutex_lock(&dev->mode_config.idr_mutex);
  1349. idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
  1350. if (!memcmp(tg->group_data, topology, 8)) {
  1351. if (!kref_get_unless_zero(&tg->refcount))
  1352. tg = NULL;
  1353. mutex_unlock(&dev->mode_config.idr_mutex);
  1354. return tg;
  1355. }
  1356. }
  1357. mutex_unlock(&dev->mode_config.idr_mutex);
  1358. return NULL;
  1359. }
  1360. EXPORT_SYMBOL(drm_mode_get_tile_group);
  1361. /**
  1362. * drm_mode_create_tile_group - create a tile group from a displayid description
  1363. * @dev: DRM device
  1364. * @topology: 8-bytes unique per monitor.
  1365. *
  1366. * Create a tile group for the unique monitor, and get a unique
  1367. * identifier for the tile group.
  1368. *
  1369. * RETURNS:
  1370. * new tile group or error.
  1371. */
  1372. struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
  1373. char topology[8])
  1374. {
  1375. struct drm_tile_group *tg;
  1376. int ret;
  1377. tg = kzalloc(sizeof(*tg), GFP_KERNEL);
  1378. if (!tg)
  1379. return ERR_PTR(-ENOMEM);
  1380. kref_init(&tg->refcount);
  1381. memcpy(tg->group_data, topology, 8);
  1382. tg->dev = dev;
  1383. mutex_lock(&dev->mode_config.idr_mutex);
  1384. ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
  1385. if (ret >= 0) {
  1386. tg->id = ret;
  1387. } else {
  1388. kfree(tg);
  1389. tg = ERR_PTR(ret);
  1390. }
  1391. mutex_unlock(&dev->mode_config.idr_mutex);
  1392. return tg;
  1393. }
  1394. EXPORT_SYMBOL(drm_mode_create_tile_group);