drm_connector.c 47 KB

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