drm_connector.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137
  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_crtc_internal.h"
  26. #include "drm_internal.h"
  27. /**
  28. * DOC: overview
  29. *
  30. * In DRM connectors are the general abstraction for display sinks, and include
  31. * als fixed panels or anything else that can display pixels in some form. As
  32. * opposed to all other KMS objects representing hardware (like CRTC, encoder or
  33. * plane abstractions) connectors can be hotplugged and unplugged at runtime.
  34. * Hence they are reference-counted using drm_connector_reference() and
  35. * drm_connector_unreference().
  36. *
  37. * KMS driver must create, initialize, register and attach at a struct
  38. * &drm_connector for each such sink. The instance is created as other KMS
  39. * objects and initialized by setting the following fields.
  40. *
  41. * The connector is then registered with a call to drm_connector_init() with a
  42. * pointer to the connector functions and a connector type, and exposed through
  43. * sysfs 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 struct &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. drm_modeset_lock_all(dev);
  173. ret = drm_mode_object_get_reg(dev, &connector->base,
  174. DRM_MODE_OBJECT_CONNECTOR,
  175. false, drm_connector_free);
  176. if (ret)
  177. goto out_unlock;
  178. connector->base.properties = &connector->properties;
  179. connector->dev = dev;
  180. connector->funcs = funcs;
  181. ret = ida_simple_get(&config->connector_ida, 0, 0, GFP_KERNEL);
  182. if (ret < 0)
  183. goto out_put;
  184. connector->index = ret;
  185. ret = 0;
  186. connector->connector_type = connector_type;
  187. connector->connector_type_id =
  188. ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
  189. if (connector->connector_type_id < 0) {
  190. ret = connector->connector_type_id;
  191. goto out_put_id;
  192. }
  193. connector->name =
  194. kasprintf(GFP_KERNEL, "%s-%d",
  195. drm_connector_enum_list[connector_type].name,
  196. connector->connector_type_id);
  197. if (!connector->name) {
  198. ret = -ENOMEM;
  199. goto out_put_type_id;
  200. }
  201. INIT_LIST_HEAD(&connector->probed_modes);
  202. INIT_LIST_HEAD(&connector->modes);
  203. mutex_init(&connector->mutex);
  204. connector->edid_blob_ptr = NULL;
  205. connector->status = connector_status_unknown;
  206. drm_connector_get_cmdline_mode(connector);
  207. /* We should add connectors at the end to avoid upsetting the connector
  208. * index too much. */
  209. list_add_tail(&connector->head, &config->connector_list);
  210. config->num_connector++;
  211. if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
  212. drm_object_attach_property(&connector->base,
  213. config->edid_property,
  214. 0);
  215. drm_object_attach_property(&connector->base,
  216. config->dpms_property, 0);
  217. if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
  218. drm_object_attach_property(&connector->base, config->prop_crtc_id, 0);
  219. }
  220. connector->debugfs_entry = NULL;
  221. out_put_type_id:
  222. if (ret)
  223. ida_simple_remove(connector_ida, connector->connector_type_id);
  224. out_put_id:
  225. if (ret)
  226. ida_simple_remove(&config->connector_ida, connector->index);
  227. out_put:
  228. if (ret)
  229. drm_mode_object_unregister(dev, &connector->base);
  230. out_unlock:
  231. drm_modeset_unlock_all(dev);
  232. return ret;
  233. }
  234. EXPORT_SYMBOL(drm_connector_init);
  235. /**
  236. * drm_mode_connector_attach_encoder - attach a connector to an encoder
  237. * @connector: connector to attach
  238. * @encoder: encoder to attach @connector to
  239. *
  240. * This function links up a connector to an encoder. Note that the routing
  241. * restrictions between encoders and crtcs are exposed to userspace through the
  242. * possible_clones and possible_crtcs bitmasks.
  243. *
  244. * Returns:
  245. * Zero on success, negative errno on failure.
  246. */
  247. int drm_mode_connector_attach_encoder(struct drm_connector *connector,
  248. struct drm_encoder *encoder)
  249. {
  250. int i;
  251. /*
  252. * In the past, drivers have attempted to model the static association
  253. * of connector to encoder in simple connector/encoder devices using a
  254. * direct assignment of connector->encoder = encoder. This connection
  255. * is a logical one and the responsibility of the core, so drivers are
  256. * expected not to mess with this.
  257. *
  258. * Note that the error return should've been enough here, but a large
  259. * majority of drivers ignores the return value, so add in a big WARN
  260. * to get people's attention.
  261. */
  262. if (WARN_ON(connector->encoder))
  263. return -EINVAL;
  264. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  265. if (connector->encoder_ids[i] == 0) {
  266. connector->encoder_ids[i] = encoder->base.id;
  267. return 0;
  268. }
  269. }
  270. return -ENOMEM;
  271. }
  272. EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
  273. static void drm_mode_remove(struct drm_connector *connector,
  274. struct drm_display_mode *mode)
  275. {
  276. list_del(&mode->head);
  277. drm_mode_destroy(connector->dev, mode);
  278. }
  279. /**
  280. * drm_connector_cleanup - cleans up an initialised connector
  281. * @connector: connector to cleanup
  282. *
  283. * Cleans up the connector but doesn't free the object.
  284. */
  285. void drm_connector_cleanup(struct drm_connector *connector)
  286. {
  287. struct drm_device *dev = connector->dev;
  288. struct drm_display_mode *mode, *t;
  289. /* The connector should have been removed from userspace long before
  290. * it is finally destroyed.
  291. */
  292. if (WARN_ON(connector->registered))
  293. drm_connector_unregister(connector);
  294. if (connector->tile_group) {
  295. drm_mode_put_tile_group(dev, connector->tile_group);
  296. connector->tile_group = NULL;
  297. }
  298. list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
  299. drm_mode_remove(connector, mode);
  300. list_for_each_entry_safe(mode, t, &connector->modes, head)
  301. drm_mode_remove(connector, mode);
  302. ida_simple_remove(&drm_connector_enum_list[connector->connector_type].ida,
  303. connector->connector_type_id);
  304. ida_simple_remove(&dev->mode_config.connector_ida,
  305. connector->index);
  306. kfree(connector->display_info.bus_formats);
  307. drm_mode_object_unregister(dev, &connector->base);
  308. kfree(connector->name);
  309. connector->name = NULL;
  310. list_del(&connector->head);
  311. dev->mode_config.num_connector--;
  312. WARN_ON(connector->state && !connector->funcs->atomic_destroy_state);
  313. if (connector->state && connector->funcs->atomic_destroy_state)
  314. connector->funcs->atomic_destroy_state(connector,
  315. connector->state);
  316. mutex_destroy(&connector->mutex);
  317. memset(connector, 0, sizeof(*connector));
  318. }
  319. EXPORT_SYMBOL(drm_connector_cleanup);
  320. /**
  321. * drm_connector_register - register a connector
  322. * @connector: the connector to register
  323. *
  324. * Register userspace interfaces for a connector
  325. *
  326. * Returns:
  327. * Zero on success, error code on failure.
  328. */
  329. int drm_connector_register(struct drm_connector *connector)
  330. {
  331. int ret = 0;
  332. if (!connector->dev->registered)
  333. return 0;
  334. mutex_lock(&connector->mutex);
  335. if (connector->registered)
  336. goto unlock;
  337. ret = drm_sysfs_connector_add(connector);
  338. if (ret)
  339. goto unlock;
  340. ret = drm_debugfs_connector_add(connector);
  341. if (ret) {
  342. goto err_sysfs;
  343. }
  344. if (connector->funcs->late_register) {
  345. ret = connector->funcs->late_register(connector);
  346. if (ret)
  347. goto err_debugfs;
  348. }
  349. drm_mode_object_register(connector->dev, &connector->base);
  350. connector->registered = true;
  351. goto unlock;
  352. err_debugfs:
  353. drm_debugfs_connector_remove(connector);
  354. err_sysfs:
  355. drm_sysfs_connector_remove(connector);
  356. unlock:
  357. mutex_unlock(&connector->mutex);
  358. return ret;
  359. }
  360. EXPORT_SYMBOL(drm_connector_register);
  361. /**
  362. * drm_connector_unregister - unregister a connector
  363. * @connector: the connector to unregister
  364. *
  365. * Unregister userspace interfaces for a connector
  366. */
  367. void drm_connector_unregister(struct drm_connector *connector)
  368. {
  369. mutex_lock(&connector->mutex);
  370. if (!connector->registered) {
  371. mutex_unlock(&connector->mutex);
  372. return;
  373. }
  374. if (connector->funcs->early_unregister)
  375. connector->funcs->early_unregister(connector);
  376. drm_sysfs_connector_remove(connector);
  377. drm_debugfs_connector_remove(connector);
  378. connector->registered = false;
  379. mutex_unlock(&connector->mutex);
  380. }
  381. EXPORT_SYMBOL(drm_connector_unregister);
  382. void drm_connector_unregister_all(struct drm_device *dev)
  383. {
  384. struct drm_connector *connector;
  385. /* FIXME: taking the mode config mutex ends up in a clash with sysfs */
  386. list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  387. drm_connector_unregister(connector);
  388. }
  389. int drm_connector_register_all(struct drm_device *dev)
  390. {
  391. struct drm_connector *connector;
  392. int ret;
  393. /* FIXME: taking the mode config mutex ends up in a clash with
  394. * fbcon/backlight registration */
  395. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  396. ret = drm_connector_register(connector);
  397. if (ret)
  398. goto err;
  399. }
  400. return 0;
  401. err:
  402. mutex_unlock(&dev->mode_config.mutex);
  403. drm_connector_unregister_all(dev);
  404. return ret;
  405. }
  406. /**
  407. * drm_get_connector_status_name - return a string for connector status
  408. * @status: connector status to compute name of
  409. *
  410. * In contrast to the other drm_get_*_name functions this one here returns a
  411. * const pointer and hence is threadsafe.
  412. */
  413. const char *drm_get_connector_status_name(enum drm_connector_status status)
  414. {
  415. if (status == connector_status_connected)
  416. return "connected";
  417. else if (status == connector_status_disconnected)
  418. return "disconnected";
  419. else
  420. return "unknown";
  421. }
  422. EXPORT_SYMBOL(drm_get_connector_status_name);
  423. static const struct drm_prop_enum_list drm_subpixel_enum_list[] = {
  424. { SubPixelUnknown, "Unknown" },
  425. { SubPixelHorizontalRGB, "Horizontal RGB" },
  426. { SubPixelHorizontalBGR, "Horizontal BGR" },
  427. { SubPixelVerticalRGB, "Vertical RGB" },
  428. { SubPixelVerticalBGR, "Vertical BGR" },
  429. { SubPixelNone, "None" },
  430. };
  431. /**
  432. * drm_get_subpixel_order_name - return a string for a given subpixel enum
  433. * @order: enum of subpixel_order
  434. *
  435. * Note you could abuse this and return something out of bounds, but that
  436. * would be a caller error. No unscrubbed user data should make it here.
  437. */
  438. const char *drm_get_subpixel_order_name(enum subpixel_order order)
  439. {
  440. return drm_subpixel_enum_list[order].name;
  441. }
  442. EXPORT_SYMBOL(drm_get_subpixel_order_name);
  443. static const struct drm_prop_enum_list drm_dpms_enum_list[] = {
  444. { DRM_MODE_DPMS_ON, "On" },
  445. { DRM_MODE_DPMS_STANDBY, "Standby" },
  446. { DRM_MODE_DPMS_SUSPEND, "Suspend" },
  447. { DRM_MODE_DPMS_OFF, "Off" }
  448. };
  449. DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
  450. /**
  451. * drm_display_info_set_bus_formats - set the supported bus formats
  452. * @info: display info to store bus formats in
  453. * @formats: array containing the supported bus formats
  454. * @num_formats: the number of entries in the fmts array
  455. *
  456. * Store the supported bus formats in display info structure.
  457. * See MEDIA_BUS_FMT_* definitions in include/uapi/linux/media-bus-format.h for
  458. * a full list of available formats.
  459. */
  460. int drm_display_info_set_bus_formats(struct drm_display_info *info,
  461. const u32 *formats,
  462. unsigned int num_formats)
  463. {
  464. u32 *fmts = NULL;
  465. if (!formats && num_formats)
  466. return -EINVAL;
  467. if (formats && num_formats) {
  468. fmts = kmemdup(formats, sizeof(*formats) * num_formats,
  469. GFP_KERNEL);
  470. if (!fmts)
  471. return -ENOMEM;
  472. }
  473. kfree(info->bus_formats);
  474. info->bus_formats = fmts;
  475. info->num_bus_formats = num_formats;
  476. return 0;
  477. }
  478. EXPORT_SYMBOL(drm_display_info_set_bus_formats);
  479. /* Optional connector properties. */
  480. static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] = {
  481. { DRM_MODE_SCALE_NONE, "None" },
  482. { DRM_MODE_SCALE_FULLSCREEN, "Full" },
  483. { DRM_MODE_SCALE_CENTER, "Center" },
  484. { DRM_MODE_SCALE_ASPECT, "Full aspect" },
  485. };
  486. static const struct drm_prop_enum_list drm_aspect_ratio_enum_list[] = {
  487. { DRM_MODE_PICTURE_ASPECT_NONE, "Automatic" },
  488. { DRM_MODE_PICTURE_ASPECT_4_3, "4:3" },
  489. { DRM_MODE_PICTURE_ASPECT_16_9, "16:9" },
  490. };
  491. static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] = {
  492. { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
  493. { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
  494. { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
  495. };
  496. DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
  497. static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] = {
  498. { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
  499. { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
  500. { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
  501. };
  502. DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
  503. drm_dvi_i_subconnector_enum_list)
  504. static const struct drm_prop_enum_list drm_tv_select_enum_list[] = {
  505. { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
  506. { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
  507. { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
  508. { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
  509. { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
  510. };
  511. DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
  512. static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] = {
  513. { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
  514. { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
  515. { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
  516. { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
  517. { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
  518. };
  519. DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
  520. drm_tv_subconnector_enum_list)
  521. int drm_connector_create_standard_properties(struct drm_device *dev)
  522. {
  523. struct drm_property *prop;
  524. prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
  525. DRM_MODE_PROP_IMMUTABLE,
  526. "EDID", 0);
  527. if (!prop)
  528. return -ENOMEM;
  529. dev->mode_config.edid_property = prop;
  530. prop = drm_property_create_enum(dev, 0,
  531. "DPMS", drm_dpms_enum_list,
  532. ARRAY_SIZE(drm_dpms_enum_list));
  533. if (!prop)
  534. return -ENOMEM;
  535. dev->mode_config.dpms_property = prop;
  536. prop = drm_property_create(dev,
  537. DRM_MODE_PROP_BLOB |
  538. DRM_MODE_PROP_IMMUTABLE,
  539. "PATH", 0);
  540. if (!prop)
  541. return -ENOMEM;
  542. dev->mode_config.path_property = prop;
  543. prop = drm_property_create(dev,
  544. DRM_MODE_PROP_BLOB |
  545. DRM_MODE_PROP_IMMUTABLE,
  546. "TILE", 0);
  547. if (!prop)
  548. return -ENOMEM;
  549. dev->mode_config.tile_property = prop;
  550. return 0;
  551. }
  552. /**
  553. * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
  554. * @dev: DRM device
  555. *
  556. * Called by a driver the first time a DVI-I connector is made.
  557. */
  558. int drm_mode_create_dvi_i_properties(struct drm_device *dev)
  559. {
  560. struct drm_property *dvi_i_selector;
  561. struct drm_property *dvi_i_subconnector;
  562. if (dev->mode_config.dvi_i_select_subconnector_property)
  563. return 0;
  564. dvi_i_selector =
  565. drm_property_create_enum(dev, 0,
  566. "select subconnector",
  567. drm_dvi_i_select_enum_list,
  568. ARRAY_SIZE(drm_dvi_i_select_enum_list));
  569. dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
  570. dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  571. "subconnector",
  572. drm_dvi_i_subconnector_enum_list,
  573. ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
  574. dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
  575. return 0;
  576. }
  577. EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
  578. /**
  579. * drm_create_tv_properties - create TV specific connector properties
  580. * @dev: DRM device
  581. * @num_modes: number of different TV formats (modes) supported
  582. * @modes: array of pointers to strings containing name of each format
  583. *
  584. * Called by a driver's TV initialization routine, this function creates
  585. * the TV specific connector properties for a given device. Caller is
  586. * responsible for allocating a list of format names and passing them to
  587. * this routine.
  588. */
  589. int drm_mode_create_tv_properties(struct drm_device *dev,
  590. unsigned int num_modes,
  591. const char * const modes[])
  592. {
  593. struct drm_property *tv_selector;
  594. struct drm_property *tv_subconnector;
  595. unsigned int i;
  596. if (dev->mode_config.tv_select_subconnector_property)
  597. return 0;
  598. /*
  599. * Basic connector properties
  600. */
  601. tv_selector = drm_property_create_enum(dev, 0,
  602. "select subconnector",
  603. drm_tv_select_enum_list,
  604. ARRAY_SIZE(drm_tv_select_enum_list));
  605. if (!tv_selector)
  606. goto nomem;
  607. dev->mode_config.tv_select_subconnector_property = tv_selector;
  608. tv_subconnector =
  609. drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  610. "subconnector",
  611. drm_tv_subconnector_enum_list,
  612. ARRAY_SIZE(drm_tv_subconnector_enum_list));
  613. if (!tv_subconnector)
  614. goto nomem;
  615. dev->mode_config.tv_subconnector_property = tv_subconnector;
  616. /*
  617. * Other, TV specific properties: margins & TV modes.
  618. */
  619. dev->mode_config.tv_left_margin_property =
  620. drm_property_create_range(dev, 0, "left margin", 0, 100);
  621. if (!dev->mode_config.tv_left_margin_property)
  622. goto nomem;
  623. dev->mode_config.tv_right_margin_property =
  624. drm_property_create_range(dev, 0, "right margin", 0, 100);
  625. if (!dev->mode_config.tv_right_margin_property)
  626. goto nomem;
  627. dev->mode_config.tv_top_margin_property =
  628. drm_property_create_range(dev, 0, "top margin", 0, 100);
  629. if (!dev->mode_config.tv_top_margin_property)
  630. goto nomem;
  631. dev->mode_config.tv_bottom_margin_property =
  632. drm_property_create_range(dev, 0, "bottom margin", 0, 100);
  633. if (!dev->mode_config.tv_bottom_margin_property)
  634. goto nomem;
  635. dev->mode_config.tv_mode_property =
  636. drm_property_create(dev, DRM_MODE_PROP_ENUM,
  637. "mode", num_modes);
  638. if (!dev->mode_config.tv_mode_property)
  639. goto nomem;
  640. for (i = 0; i < num_modes; i++)
  641. drm_property_add_enum(dev->mode_config.tv_mode_property, i,
  642. i, modes[i]);
  643. dev->mode_config.tv_brightness_property =
  644. drm_property_create_range(dev, 0, "brightness", 0, 100);
  645. if (!dev->mode_config.tv_brightness_property)
  646. goto nomem;
  647. dev->mode_config.tv_contrast_property =
  648. drm_property_create_range(dev, 0, "contrast", 0, 100);
  649. if (!dev->mode_config.tv_contrast_property)
  650. goto nomem;
  651. dev->mode_config.tv_flicker_reduction_property =
  652. drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
  653. if (!dev->mode_config.tv_flicker_reduction_property)
  654. goto nomem;
  655. dev->mode_config.tv_overscan_property =
  656. drm_property_create_range(dev, 0, "overscan", 0, 100);
  657. if (!dev->mode_config.tv_overscan_property)
  658. goto nomem;
  659. dev->mode_config.tv_saturation_property =
  660. drm_property_create_range(dev, 0, "saturation", 0, 100);
  661. if (!dev->mode_config.tv_saturation_property)
  662. goto nomem;
  663. dev->mode_config.tv_hue_property =
  664. drm_property_create_range(dev, 0, "hue", 0, 100);
  665. if (!dev->mode_config.tv_hue_property)
  666. goto nomem;
  667. return 0;
  668. nomem:
  669. return -ENOMEM;
  670. }
  671. EXPORT_SYMBOL(drm_mode_create_tv_properties);
  672. /**
  673. * drm_mode_create_scaling_mode_property - create scaling mode property
  674. * @dev: DRM device
  675. *
  676. * Called by a driver the first time it's needed, must be attached to desired
  677. * connectors.
  678. */
  679. int drm_mode_create_scaling_mode_property(struct drm_device *dev)
  680. {
  681. struct drm_property *scaling_mode;
  682. if (dev->mode_config.scaling_mode_property)
  683. return 0;
  684. scaling_mode =
  685. drm_property_create_enum(dev, 0, "scaling mode",
  686. drm_scaling_mode_enum_list,
  687. ARRAY_SIZE(drm_scaling_mode_enum_list));
  688. dev->mode_config.scaling_mode_property = scaling_mode;
  689. return 0;
  690. }
  691. EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
  692. /**
  693. * drm_mode_create_aspect_ratio_property - create aspect ratio property
  694. * @dev: DRM device
  695. *
  696. * Called by a driver the first time it's needed, must be attached to desired
  697. * connectors.
  698. *
  699. * Returns:
  700. * Zero on success, negative errno on failure.
  701. */
  702. int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
  703. {
  704. if (dev->mode_config.aspect_ratio_property)
  705. return 0;
  706. dev->mode_config.aspect_ratio_property =
  707. drm_property_create_enum(dev, 0, "aspect ratio",
  708. drm_aspect_ratio_enum_list,
  709. ARRAY_SIZE(drm_aspect_ratio_enum_list));
  710. if (dev->mode_config.aspect_ratio_property == NULL)
  711. return -ENOMEM;
  712. return 0;
  713. }
  714. EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
  715. /**
  716. * drm_mode_create_suggested_offset_properties - create suggests offset properties
  717. * @dev: DRM device
  718. *
  719. * Create the the suggested x/y offset property for connectors.
  720. */
  721. int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
  722. {
  723. if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
  724. return 0;
  725. dev->mode_config.suggested_x_property =
  726. drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
  727. dev->mode_config.suggested_y_property =
  728. drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
  729. if (dev->mode_config.suggested_x_property == NULL ||
  730. dev->mode_config.suggested_y_property == NULL)
  731. return -ENOMEM;
  732. return 0;
  733. }
  734. EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
  735. /**
  736. * drm_mode_connector_set_path_property - set tile property on connector
  737. * @connector: connector to set property on.
  738. * @path: path to use for property; must not be NULL.
  739. *
  740. * This creates a property to expose to userspace to specify a
  741. * connector path. This is mainly used for DisplayPort MST where
  742. * connectors have a topology and we want to allow userspace to give
  743. * them more meaningful names.
  744. *
  745. * Returns:
  746. * Zero on success, negative errno on failure.
  747. */
  748. int drm_mode_connector_set_path_property(struct drm_connector *connector,
  749. const char *path)
  750. {
  751. struct drm_device *dev = connector->dev;
  752. int ret;
  753. ret = drm_property_replace_global_blob(dev,
  754. &connector->path_blob_ptr,
  755. strlen(path) + 1,
  756. path,
  757. &connector->base,
  758. dev->mode_config.path_property);
  759. return ret;
  760. }
  761. EXPORT_SYMBOL(drm_mode_connector_set_path_property);
  762. /**
  763. * drm_mode_connector_set_tile_property - set tile property on connector
  764. * @connector: connector to set property on.
  765. *
  766. * This looks up the tile information for a connector, and creates a
  767. * property for userspace to parse if it exists. The property is of
  768. * the form of 8 integers using ':' as a separator.
  769. *
  770. * Returns:
  771. * Zero on success, errno on failure.
  772. */
  773. int drm_mode_connector_set_tile_property(struct drm_connector *connector)
  774. {
  775. struct drm_device *dev = connector->dev;
  776. char tile[256];
  777. int ret;
  778. if (!connector->has_tile) {
  779. ret = drm_property_replace_global_blob(dev,
  780. &connector->tile_blob_ptr,
  781. 0,
  782. NULL,
  783. &connector->base,
  784. dev->mode_config.tile_property);
  785. return ret;
  786. }
  787. snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
  788. connector->tile_group->id, connector->tile_is_single_monitor,
  789. connector->num_h_tile, connector->num_v_tile,
  790. connector->tile_h_loc, connector->tile_v_loc,
  791. connector->tile_h_size, connector->tile_v_size);
  792. ret = drm_property_replace_global_blob(dev,
  793. &connector->tile_blob_ptr,
  794. strlen(tile) + 1,
  795. tile,
  796. &connector->base,
  797. dev->mode_config.tile_property);
  798. return ret;
  799. }
  800. EXPORT_SYMBOL(drm_mode_connector_set_tile_property);
  801. /**
  802. * drm_mode_connector_update_edid_property - update the edid property of a connector
  803. * @connector: drm connector
  804. * @edid: new value of the edid property
  805. *
  806. * This function creates a new blob modeset object and assigns its id to the
  807. * connector's edid property.
  808. *
  809. * Returns:
  810. * Zero on success, negative errno on failure.
  811. */
  812. int drm_mode_connector_update_edid_property(struct drm_connector *connector,
  813. const struct edid *edid)
  814. {
  815. struct drm_device *dev = connector->dev;
  816. size_t size = 0;
  817. int ret;
  818. /* ignore requests to set edid when overridden */
  819. if (connector->override_edid)
  820. return 0;
  821. if (edid)
  822. size = EDID_LENGTH * (1 + edid->extensions);
  823. ret = drm_property_replace_global_blob(dev,
  824. &connector->edid_blob_ptr,
  825. size,
  826. edid,
  827. &connector->base,
  828. dev->mode_config.edid_property);
  829. return ret;
  830. }
  831. EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
  832. int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
  833. struct drm_property *property,
  834. uint64_t value)
  835. {
  836. int ret = -EINVAL;
  837. struct drm_connector *connector = obj_to_connector(obj);
  838. /* Do DPMS ourselves */
  839. if (property == connector->dev->mode_config.dpms_property) {
  840. ret = (*connector->funcs->dpms)(connector, (int)value);
  841. } else if (connector->funcs->set_property)
  842. ret = connector->funcs->set_property(connector, property, value);
  843. /* store the property value if successful */
  844. if (!ret)
  845. drm_object_property_set_value(&connector->base, property, value);
  846. return ret;
  847. }
  848. int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
  849. void *data, struct drm_file *file_priv)
  850. {
  851. struct drm_mode_connector_set_property *conn_set_prop = data;
  852. struct drm_mode_obj_set_property obj_set_prop = {
  853. .value = conn_set_prop->value,
  854. .prop_id = conn_set_prop->prop_id,
  855. .obj_id = conn_set_prop->connector_id,
  856. .obj_type = DRM_MODE_OBJECT_CONNECTOR
  857. };
  858. /* It does all the locking and checking we need */
  859. return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
  860. }
  861. static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
  862. {
  863. /* For atomic drivers only state objects are synchronously updated and
  864. * protected by modeset locks, so check those first. */
  865. if (connector->state)
  866. return connector->state->best_encoder;
  867. return connector->encoder;
  868. }
  869. static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
  870. const struct drm_file *file_priv)
  871. {
  872. /*
  873. * If user-space hasn't configured the driver to expose the stereo 3D
  874. * modes, don't expose them.
  875. */
  876. if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
  877. return false;
  878. return true;
  879. }
  880. int drm_mode_getconnector(struct drm_device *dev, void *data,
  881. struct drm_file *file_priv)
  882. {
  883. struct drm_mode_get_connector *out_resp = data;
  884. struct drm_connector *connector;
  885. struct drm_encoder *encoder;
  886. struct drm_display_mode *mode;
  887. int mode_count = 0;
  888. int encoders_count = 0;
  889. int ret = 0;
  890. int copied = 0;
  891. int i;
  892. struct drm_mode_modeinfo u_mode;
  893. struct drm_mode_modeinfo __user *mode_ptr;
  894. uint32_t __user *encoder_ptr;
  895. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  896. return -EINVAL;
  897. memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
  898. mutex_lock(&dev->mode_config.mutex);
  899. connector = drm_connector_lookup(dev, out_resp->connector_id);
  900. if (!connector) {
  901. ret = -ENOENT;
  902. goto out_unlock;
  903. }
  904. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++)
  905. if (connector->encoder_ids[i] != 0)
  906. encoders_count++;
  907. if (out_resp->count_modes == 0) {
  908. connector->funcs->fill_modes(connector,
  909. dev->mode_config.max_width,
  910. dev->mode_config.max_height);
  911. }
  912. /* delayed so we get modes regardless of pre-fill_modes state */
  913. list_for_each_entry(mode, &connector->modes, head)
  914. if (drm_mode_expose_to_userspace(mode, file_priv))
  915. mode_count++;
  916. out_resp->connector_id = connector->base.id;
  917. out_resp->connector_type = connector->connector_type;
  918. out_resp->connector_type_id = connector->connector_type_id;
  919. out_resp->mm_width = connector->display_info.width_mm;
  920. out_resp->mm_height = connector->display_info.height_mm;
  921. out_resp->subpixel = connector->display_info.subpixel_order;
  922. out_resp->connection = connector->status;
  923. drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
  924. encoder = drm_connector_get_encoder(connector);
  925. if (encoder)
  926. out_resp->encoder_id = encoder->base.id;
  927. else
  928. out_resp->encoder_id = 0;
  929. /*
  930. * This ioctl is called twice, once to determine how much space is
  931. * needed, and the 2nd time to fill it.
  932. */
  933. if ((out_resp->count_modes >= mode_count) && mode_count) {
  934. copied = 0;
  935. mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
  936. list_for_each_entry(mode, &connector->modes, head) {
  937. if (!drm_mode_expose_to_userspace(mode, file_priv))
  938. continue;
  939. drm_mode_convert_to_umode(&u_mode, mode);
  940. if (copy_to_user(mode_ptr + copied,
  941. &u_mode, sizeof(u_mode))) {
  942. ret = -EFAULT;
  943. goto out;
  944. }
  945. copied++;
  946. }
  947. }
  948. out_resp->count_modes = mode_count;
  949. ret = drm_mode_object_get_properties(&connector->base, file_priv->atomic,
  950. (uint32_t __user *)(unsigned long)(out_resp->props_ptr),
  951. (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
  952. &out_resp->count_props);
  953. if (ret)
  954. goto out;
  955. if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
  956. copied = 0;
  957. encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
  958. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  959. if (connector->encoder_ids[i] != 0) {
  960. if (put_user(connector->encoder_ids[i],
  961. encoder_ptr + copied)) {
  962. ret = -EFAULT;
  963. goto out;
  964. }
  965. copied++;
  966. }
  967. }
  968. }
  969. out_resp->count_encoders = encoders_count;
  970. out:
  971. drm_modeset_unlock(&dev->mode_config.connection_mutex);
  972. drm_connector_unreference(connector);
  973. out_unlock:
  974. mutex_unlock(&dev->mode_config.mutex);
  975. return ret;
  976. }