drm_connector.c 47 KB

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