drm_connector.c 41 KB

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