drm_connector.c 46 KB

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