drm_connector.c 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934
  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/drm_connector.h>
  23. #include <drm/drm_edid.h>
  24. #include <drm/drm_encoder.h>
  25. #include <drm/drm_utils.h>
  26. #include <drm/drm_print.h>
  27. #include <drm/drm_drv.h>
  28. #include <drm/drm_file.h>
  29. #include <linux/uaccess.h>
  30. #include "drm_crtc_internal.h"
  31. #include "drm_internal.h"
  32. /**
  33. * DOC: overview
  34. *
  35. * In DRM connectors are the general abstraction for display sinks, and include
  36. * als fixed panels or anything else that can display pixels in some form. As
  37. * opposed to all other KMS objects representing hardware (like CRTC, encoder or
  38. * plane abstractions) connectors can be hotplugged and unplugged at runtime.
  39. * Hence they are reference-counted using drm_connector_get() and
  40. * drm_connector_put().
  41. *
  42. * KMS driver must create, initialize, register and attach at a &struct
  43. * drm_connector for each such sink. The instance is created as other KMS
  44. * objects and initialized by setting the following fields. The connector is
  45. * initialized with a call to drm_connector_init() with a pointer to the
  46. * &struct drm_connector_funcs and a connector type, and then exposed to
  47. * userspace with a call to drm_connector_register().
  48. *
  49. * Connectors must be attached to an encoder to be used. For devices that map
  50. * connectors to encoders 1:1, the connector should be attached at
  51. * initialization time with a call to drm_connector_attach_encoder(). The
  52. * driver must also set the &drm_connector.encoder field to point to the
  53. * attached encoder.
  54. *
  55. * For connectors which are not fixed (like built-in panels) the driver needs to
  56. * support hotplug notifications. The simplest way to do that is by using the
  57. * probe helpers, see drm_kms_helper_poll_init() for connectors which don't have
  58. * hardware support for hotplug interrupts. Connectors with hardware hotplug
  59. * support can instead use e.g. drm_helper_hpd_irq_event().
  60. */
  61. struct drm_conn_prop_enum_list {
  62. int type;
  63. const char *name;
  64. struct ida ida;
  65. };
  66. /*
  67. * Connector and encoder types.
  68. */
  69. static struct drm_conn_prop_enum_list drm_connector_enum_list[] = {
  70. { DRM_MODE_CONNECTOR_Unknown, "Unknown" },
  71. { DRM_MODE_CONNECTOR_VGA, "VGA" },
  72. { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
  73. { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
  74. { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
  75. { DRM_MODE_CONNECTOR_Composite, "Composite" },
  76. { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
  77. { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
  78. { DRM_MODE_CONNECTOR_Component, "Component" },
  79. { DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
  80. { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
  81. { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
  82. { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
  83. { DRM_MODE_CONNECTOR_TV, "TV" },
  84. { DRM_MODE_CONNECTOR_eDP, "eDP" },
  85. { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
  86. { DRM_MODE_CONNECTOR_DSI, "DSI" },
  87. { DRM_MODE_CONNECTOR_DPI, "DPI" },
  88. { DRM_MODE_CONNECTOR_WRITEBACK, "Writeback" },
  89. };
  90. void drm_connector_ida_init(void)
  91. {
  92. int i;
  93. for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
  94. ida_init(&drm_connector_enum_list[i].ida);
  95. }
  96. void drm_connector_ida_destroy(void)
  97. {
  98. int i;
  99. for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
  100. ida_destroy(&drm_connector_enum_list[i].ida);
  101. }
  102. /**
  103. * drm_connector_get_cmdline_mode - reads the user's cmdline mode
  104. * @connector: connector to quwery
  105. *
  106. * The kernel supports per-connector configuration of its consoles through
  107. * use of the video= parameter. This function parses that option and
  108. * extracts the user's specified mode (or enable/disable status) for a
  109. * particular connector. This is typically only used during the early fbdev
  110. * setup.
  111. */
  112. static void drm_connector_get_cmdline_mode(struct drm_connector *connector)
  113. {
  114. struct drm_cmdline_mode *mode = &connector->cmdline_mode;
  115. char *option = NULL;
  116. if (fb_get_options(connector->name, &option))
  117. return;
  118. if (!drm_mode_parse_command_line_for_connector(option,
  119. connector,
  120. mode))
  121. return;
  122. if (mode->force) {
  123. DRM_INFO("forcing %s connector %s\n", connector->name,
  124. drm_get_connector_force_name(mode->force));
  125. connector->force = mode->force;
  126. }
  127. DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
  128. connector->name,
  129. mode->xres, mode->yres,
  130. mode->refresh_specified ? mode->refresh : 60,
  131. mode->rb ? " reduced blanking" : "",
  132. mode->margins ? " with margins" : "",
  133. mode->interlace ? " interlaced" : "");
  134. }
  135. static void drm_connector_free(struct kref *kref)
  136. {
  137. struct drm_connector *connector =
  138. container_of(kref, struct drm_connector, base.refcount);
  139. struct drm_device *dev = connector->dev;
  140. drm_mode_object_unregister(dev, &connector->base);
  141. connector->funcs->destroy(connector);
  142. }
  143. void drm_connector_free_work_fn(struct work_struct *work)
  144. {
  145. struct drm_connector *connector, *n;
  146. struct drm_device *dev =
  147. container_of(work, struct drm_device, mode_config.connector_free_work);
  148. struct drm_mode_config *config = &dev->mode_config;
  149. unsigned long flags;
  150. struct llist_node *freed;
  151. spin_lock_irqsave(&config->connector_list_lock, flags);
  152. freed = llist_del_all(&config->connector_free_list);
  153. spin_unlock_irqrestore(&config->connector_list_lock, flags);
  154. llist_for_each_entry_safe(connector, n, freed, free_node) {
  155. drm_mode_object_unregister(dev, &connector->base);
  156. connector->funcs->destroy(connector);
  157. }
  158. }
  159. /**
  160. * drm_connector_init - Init a preallocated connector
  161. * @dev: DRM device
  162. * @connector: the connector to init
  163. * @funcs: callbacks for this connector
  164. * @connector_type: user visible type of the connector
  165. *
  166. * Initialises a preallocated connector. Connectors should be
  167. * subclassed as part of driver connector objects.
  168. *
  169. * Returns:
  170. * Zero on success, error code on failure.
  171. */
  172. int drm_connector_init(struct drm_device *dev,
  173. struct drm_connector *connector,
  174. const struct drm_connector_funcs *funcs,
  175. int connector_type)
  176. {
  177. struct drm_mode_config *config = &dev->mode_config;
  178. int ret;
  179. struct ida *connector_ida =
  180. &drm_connector_enum_list[connector_type].ida;
  181. WARN_ON(drm_drv_uses_atomic_modeset(dev) &&
  182. (!funcs->atomic_destroy_state ||
  183. !funcs->atomic_duplicate_state));
  184. ret = __drm_mode_object_add(dev, &connector->base,
  185. DRM_MODE_OBJECT_CONNECTOR,
  186. false, drm_connector_free);
  187. if (ret)
  188. return ret;
  189. connector->base.properties = &connector->properties;
  190. connector->dev = dev;
  191. connector->funcs = funcs;
  192. /* connector index is used with 32bit bitmasks */
  193. ret = ida_simple_get(&config->connector_ida, 0, 32, GFP_KERNEL);
  194. if (ret < 0) {
  195. DRM_DEBUG_KMS("Failed to allocate %s connector index: %d\n",
  196. drm_connector_enum_list[connector_type].name,
  197. ret);
  198. goto out_put;
  199. }
  200. connector->index = ret;
  201. ret = 0;
  202. connector->connector_type = connector_type;
  203. connector->connector_type_id =
  204. ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
  205. if (connector->connector_type_id < 0) {
  206. ret = connector->connector_type_id;
  207. goto out_put_id;
  208. }
  209. connector->name =
  210. kasprintf(GFP_KERNEL, "%s-%d",
  211. drm_connector_enum_list[connector_type].name,
  212. connector->connector_type_id);
  213. if (!connector->name) {
  214. ret = -ENOMEM;
  215. goto out_put_type_id;
  216. }
  217. INIT_LIST_HEAD(&connector->probed_modes);
  218. INIT_LIST_HEAD(&connector->modes);
  219. mutex_init(&connector->mutex);
  220. connector->edid_blob_ptr = NULL;
  221. connector->status = connector_status_unknown;
  222. connector->display_info.panel_orientation =
  223. DRM_MODE_PANEL_ORIENTATION_UNKNOWN;
  224. drm_connector_get_cmdline_mode(connector);
  225. /* We should add connectors at the end to avoid upsetting the connector
  226. * index too much. */
  227. spin_lock_irq(&config->connector_list_lock);
  228. list_add_tail(&connector->head, &config->connector_list);
  229. config->num_connector++;
  230. spin_unlock_irq(&config->connector_list_lock);
  231. if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL &&
  232. connector_type != DRM_MODE_CONNECTOR_WRITEBACK)
  233. drm_object_attach_property(&connector->base,
  234. config->edid_property,
  235. 0);
  236. drm_object_attach_property(&connector->base,
  237. config->dpms_property, 0);
  238. drm_object_attach_property(&connector->base,
  239. config->link_status_property,
  240. 0);
  241. drm_object_attach_property(&connector->base,
  242. config->non_desktop_property,
  243. 0);
  244. if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
  245. drm_object_attach_property(&connector->base, config->prop_crtc_id, 0);
  246. }
  247. connector->debugfs_entry = NULL;
  248. out_put_type_id:
  249. if (ret)
  250. ida_simple_remove(connector_ida, connector->connector_type_id);
  251. out_put_id:
  252. if (ret)
  253. ida_simple_remove(&config->connector_ida, connector->index);
  254. out_put:
  255. if (ret)
  256. drm_mode_object_unregister(dev, &connector->base);
  257. return ret;
  258. }
  259. EXPORT_SYMBOL(drm_connector_init);
  260. /**
  261. * drm_connector_attach_encoder - attach a connector to an encoder
  262. * @connector: connector to attach
  263. * @encoder: encoder to attach @connector to
  264. *
  265. * This function links up a connector to an encoder. Note that the routing
  266. * restrictions between encoders and crtcs are exposed to userspace through the
  267. * possible_clones and possible_crtcs bitmasks.
  268. *
  269. * Returns:
  270. * Zero on success, negative errno on failure.
  271. */
  272. int drm_connector_attach_encoder(struct drm_connector *connector,
  273. struct drm_encoder *encoder)
  274. {
  275. int i;
  276. /*
  277. * In the past, drivers have attempted to model the static association
  278. * of connector to encoder in simple connector/encoder devices using a
  279. * direct assignment of connector->encoder = encoder. This connection
  280. * is a logical one and the responsibility of the core, so drivers are
  281. * expected not to mess with this.
  282. *
  283. * Note that the error return should've been enough here, but a large
  284. * majority of drivers ignores the return value, so add in a big WARN
  285. * to get people's attention.
  286. */
  287. if (WARN_ON(connector->encoder))
  288. return -EINVAL;
  289. for (i = 0; i < ARRAY_SIZE(connector->encoder_ids); i++) {
  290. if (connector->encoder_ids[i] == 0) {
  291. connector->encoder_ids[i] = encoder->base.id;
  292. return 0;
  293. }
  294. }
  295. return -ENOMEM;
  296. }
  297. EXPORT_SYMBOL(drm_connector_attach_encoder);
  298. /**
  299. * drm_connector_has_possible_encoder - check if the connector and encoder are assosicated with each other
  300. * @connector: the connector
  301. * @encoder: the encoder
  302. *
  303. * Returns:
  304. * True if @encoder is one of the possible encoders for @connector.
  305. */
  306. bool drm_connector_has_possible_encoder(struct drm_connector *connector,
  307. struct drm_encoder *encoder)
  308. {
  309. struct drm_encoder *enc;
  310. int i;
  311. drm_connector_for_each_possible_encoder(connector, enc, i) {
  312. if (enc == encoder)
  313. return true;
  314. }
  315. return false;
  316. }
  317. EXPORT_SYMBOL(drm_connector_has_possible_encoder);
  318. static void drm_mode_remove(struct drm_connector *connector,
  319. struct drm_display_mode *mode)
  320. {
  321. list_del(&mode->head);
  322. drm_mode_destroy(connector->dev, mode);
  323. }
  324. /**
  325. * drm_connector_cleanup - cleans up an initialised connector
  326. * @connector: connector to cleanup
  327. *
  328. * Cleans up the connector but doesn't free the object.
  329. */
  330. void drm_connector_cleanup(struct drm_connector *connector)
  331. {
  332. struct drm_device *dev = connector->dev;
  333. struct drm_display_mode *mode, *t;
  334. /* The connector should have been removed from userspace long before
  335. * it is finally destroyed.
  336. */
  337. if (WARN_ON(connector->registration_state ==
  338. DRM_CONNECTOR_REGISTERED))
  339. drm_connector_unregister(connector);
  340. if (connector->tile_group) {
  341. drm_mode_put_tile_group(dev, connector->tile_group);
  342. connector->tile_group = NULL;
  343. }
  344. list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
  345. drm_mode_remove(connector, mode);
  346. list_for_each_entry_safe(mode, t, &connector->modes, head)
  347. drm_mode_remove(connector, mode);
  348. ida_simple_remove(&drm_connector_enum_list[connector->connector_type].ida,
  349. connector->connector_type_id);
  350. ida_simple_remove(&dev->mode_config.connector_ida,
  351. connector->index);
  352. kfree(connector->display_info.bus_formats);
  353. drm_mode_object_unregister(dev, &connector->base);
  354. kfree(connector->name);
  355. connector->name = NULL;
  356. spin_lock_irq(&dev->mode_config.connector_list_lock);
  357. list_del(&connector->head);
  358. dev->mode_config.num_connector--;
  359. spin_unlock_irq(&dev->mode_config.connector_list_lock);
  360. WARN_ON(connector->state && !connector->funcs->atomic_destroy_state);
  361. if (connector->state && connector->funcs->atomic_destroy_state)
  362. connector->funcs->atomic_destroy_state(connector,
  363. connector->state);
  364. mutex_destroy(&connector->mutex);
  365. memset(connector, 0, sizeof(*connector));
  366. }
  367. EXPORT_SYMBOL(drm_connector_cleanup);
  368. /**
  369. * drm_connector_register - register a connector
  370. * @connector: the connector to register
  371. *
  372. * Register userspace interfaces for a connector
  373. *
  374. * Returns:
  375. * Zero on success, error code on failure.
  376. */
  377. int drm_connector_register(struct drm_connector *connector)
  378. {
  379. int ret = 0;
  380. if (!connector->dev->registered)
  381. return 0;
  382. mutex_lock(&connector->mutex);
  383. if (connector->registration_state != DRM_CONNECTOR_INITIALIZING)
  384. goto unlock;
  385. ret = drm_sysfs_connector_add(connector);
  386. if (ret)
  387. goto unlock;
  388. ret = drm_debugfs_connector_add(connector);
  389. if (ret) {
  390. goto err_sysfs;
  391. }
  392. if (connector->funcs->late_register) {
  393. ret = connector->funcs->late_register(connector);
  394. if (ret)
  395. goto err_debugfs;
  396. }
  397. drm_mode_object_register(connector->dev, &connector->base);
  398. connector->registration_state = DRM_CONNECTOR_REGISTERED;
  399. goto unlock;
  400. err_debugfs:
  401. drm_debugfs_connector_remove(connector);
  402. err_sysfs:
  403. drm_sysfs_connector_remove(connector);
  404. unlock:
  405. mutex_unlock(&connector->mutex);
  406. return ret;
  407. }
  408. EXPORT_SYMBOL(drm_connector_register);
  409. /**
  410. * drm_connector_unregister - unregister a connector
  411. * @connector: the connector to unregister
  412. *
  413. * Unregister userspace interfaces for a connector
  414. */
  415. void drm_connector_unregister(struct drm_connector *connector)
  416. {
  417. mutex_lock(&connector->mutex);
  418. if (connector->registration_state != DRM_CONNECTOR_REGISTERED) {
  419. mutex_unlock(&connector->mutex);
  420. return;
  421. }
  422. if (connector->funcs->early_unregister)
  423. connector->funcs->early_unregister(connector);
  424. drm_sysfs_connector_remove(connector);
  425. drm_debugfs_connector_remove(connector);
  426. connector->registration_state = DRM_CONNECTOR_UNREGISTERED;
  427. mutex_unlock(&connector->mutex);
  428. }
  429. EXPORT_SYMBOL(drm_connector_unregister);
  430. void drm_connector_unregister_all(struct drm_device *dev)
  431. {
  432. struct drm_connector *connector;
  433. struct drm_connector_list_iter conn_iter;
  434. drm_connector_list_iter_begin(dev, &conn_iter);
  435. drm_for_each_connector_iter(connector, &conn_iter)
  436. drm_connector_unregister(connector);
  437. drm_connector_list_iter_end(&conn_iter);
  438. }
  439. int drm_connector_register_all(struct drm_device *dev)
  440. {
  441. struct drm_connector *connector;
  442. struct drm_connector_list_iter conn_iter;
  443. int ret = 0;
  444. drm_connector_list_iter_begin(dev, &conn_iter);
  445. drm_for_each_connector_iter(connector, &conn_iter) {
  446. ret = drm_connector_register(connector);
  447. if (ret)
  448. break;
  449. }
  450. drm_connector_list_iter_end(&conn_iter);
  451. if (ret)
  452. drm_connector_unregister_all(dev);
  453. return ret;
  454. }
  455. /**
  456. * drm_get_connector_status_name - return a string for connector status
  457. * @status: connector status to compute name of
  458. *
  459. * In contrast to the other drm_get_*_name functions this one here returns a
  460. * const pointer and hence is threadsafe.
  461. */
  462. const char *drm_get_connector_status_name(enum drm_connector_status status)
  463. {
  464. if (status == connector_status_connected)
  465. return "connected";
  466. else if (status == connector_status_disconnected)
  467. return "disconnected";
  468. else
  469. return "unknown";
  470. }
  471. EXPORT_SYMBOL(drm_get_connector_status_name);
  472. /**
  473. * drm_get_connector_force_name - return a string for connector force
  474. * @force: connector force to get name of
  475. *
  476. * Returns: const pointer to name.
  477. */
  478. const char *drm_get_connector_force_name(enum drm_connector_force force)
  479. {
  480. switch (force) {
  481. case DRM_FORCE_UNSPECIFIED:
  482. return "unspecified";
  483. case DRM_FORCE_OFF:
  484. return "off";
  485. case DRM_FORCE_ON:
  486. return "on";
  487. case DRM_FORCE_ON_DIGITAL:
  488. return "digital";
  489. default:
  490. return "unknown";
  491. }
  492. }
  493. #ifdef CONFIG_LOCKDEP
  494. static struct lockdep_map connector_list_iter_dep_map = {
  495. .name = "drm_connector_list_iter"
  496. };
  497. #endif
  498. /**
  499. * drm_connector_list_iter_begin - initialize a connector_list iterator
  500. * @dev: DRM device
  501. * @iter: connector_list iterator
  502. *
  503. * Sets @iter up to walk the &drm_mode_config.connector_list of @dev. @iter
  504. * must always be cleaned up again by calling drm_connector_list_iter_end().
  505. * Iteration itself happens using drm_connector_list_iter_next() or
  506. * drm_for_each_connector_iter().
  507. */
  508. void drm_connector_list_iter_begin(struct drm_device *dev,
  509. struct drm_connector_list_iter *iter)
  510. {
  511. iter->dev = dev;
  512. iter->conn = NULL;
  513. lock_acquire_shared_recursive(&connector_list_iter_dep_map, 0, 1, NULL, _RET_IP_);
  514. }
  515. EXPORT_SYMBOL(drm_connector_list_iter_begin);
  516. /*
  517. * Extra-safe connector put function that works in any context. Should only be
  518. * used from the connector_iter functions, where we never really expect to
  519. * actually release the connector when dropping our final reference.
  520. */
  521. static void
  522. __drm_connector_put_safe(struct drm_connector *conn)
  523. {
  524. struct drm_mode_config *config = &conn->dev->mode_config;
  525. lockdep_assert_held(&config->connector_list_lock);
  526. if (!refcount_dec_and_test(&conn->base.refcount.refcount))
  527. return;
  528. llist_add(&conn->free_node, &config->connector_free_list);
  529. schedule_work(&config->connector_free_work);
  530. }
  531. /**
  532. * drm_connector_list_iter_next - return next connector
  533. * @iter: connector_list iterator
  534. *
  535. * Returns the next connector for @iter, or NULL when the list walk has
  536. * completed.
  537. */
  538. struct drm_connector *
  539. drm_connector_list_iter_next(struct drm_connector_list_iter *iter)
  540. {
  541. struct drm_connector *old_conn = iter->conn;
  542. struct drm_mode_config *config = &iter->dev->mode_config;
  543. struct list_head *lhead;
  544. unsigned long flags;
  545. spin_lock_irqsave(&config->connector_list_lock, flags);
  546. lhead = old_conn ? &old_conn->head : &config->connector_list;
  547. do {
  548. if (lhead->next == &config->connector_list) {
  549. iter->conn = NULL;
  550. break;
  551. }
  552. lhead = lhead->next;
  553. iter->conn = list_entry(lhead, struct drm_connector, head);
  554. /* loop until it's not a zombie connector */
  555. } while (!kref_get_unless_zero(&iter->conn->base.refcount));
  556. if (old_conn)
  557. __drm_connector_put_safe(old_conn);
  558. spin_unlock_irqrestore(&config->connector_list_lock, flags);
  559. return iter->conn;
  560. }
  561. EXPORT_SYMBOL(drm_connector_list_iter_next);
  562. /**
  563. * drm_connector_list_iter_end - tear down a connector_list iterator
  564. * @iter: connector_list iterator
  565. *
  566. * Tears down @iter and releases any resources (like &drm_connector references)
  567. * acquired while walking the list. This must always be called, both when the
  568. * iteration completes fully or when it was aborted without walking the entire
  569. * list.
  570. */
  571. void drm_connector_list_iter_end(struct drm_connector_list_iter *iter)
  572. {
  573. struct drm_mode_config *config = &iter->dev->mode_config;
  574. unsigned long flags;
  575. iter->dev = NULL;
  576. if (iter->conn) {
  577. spin_lock_irqsave(&config->connector_list_lock, flags);
  578. __drm_connector_put_safe(iter->conn);
  579. spin_unlock_irqrestore(&config->connector_list_lock, flags);
  580. }
  581. lock_release(&connector_list_iter_dep_map, 0, _RET_IP_);
  582. }
  583. EXPORT_SYMBOL(drm_connector_list_iter_end);
  584. static const struct drm_prop_enum_list drm_subpixel_enum_list[] = {
  585. { SubPixelUnknown, "Unknown" },
  586. { SubPixelHorizontalRGB, "Horizontal RGB" },
  587. { SubPixelHorizontalBGR, "Horizontal BGR" },
  588. { SubPixelVerticalRGB, "Vertical RGB" },
  589. { SubPixelVerticalBGR, "Vertical BGR" },
  590. { SubPixelNone, "None" },
  591. };
  592. /**
  593. * drm_get_subpixel_order_name - return a string for a given subpixel enum
  594. * @order: enum of subpixel_order
  595. *
  596. * Note you could abuse this and return something out of bounds, but that
  597. * would be a caller error. No unscrubbed user data should make it here.
  598. */
  599. const char *drm_get_subpixel_order_name(enum subpixel_order order)
  600. {
  601. return drm_subpixel_enum_list[order].name;
  602. }
  603. EXPORT_SYMBOL(drm_get_subpixel_order_name);
  604. static const struct drm_prop_enum_list drm_dpms_enum_list[] = {
  605. { DRM_MODE_DPMS_ON, "On" },
  606. { DRM_MODE_DPMS_STANDBY, "Standby" },
  607. { DRM_MODE_DPMS_SUSPEND, "Suspend" },
  608. { DRM_MODE_DPMS_OFF, "Off" }
  609. };
  610. DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
  611. static const struct drm_prop_enum_list drm_link_status_enum_list[] = {
  612. { DRM_MODE_LINK_STATUS_GOOD, "Good" },
  613. { DRM_MODE_LINK_STATUS_BAD, "Bad" },
  614. };
  615. /**
  616. * drm_display_info_set_bus_formats - set the supported bus formats
  617. * @info: display info to store bus formats in
  618. * @formats: array containing the supported bus formats
  619. * @num_formats: the number of entries in the fmts array
  620. *
  621. * Store the supported bus formats in display info structure.
  622. * See MEDIA_BUS_FMT_* definitions in include/uapi/linux/media-bus-format.h for
  623. * a full list of available formats.
  624. */
  625. int drm_display_info_set_bus_formats(struct drm_display_info *info,
  626. const u32 *formats,
  627. unsigned int num_formats)
  628. {
  629. u32 *fmts = NULL;
  630. if (!formats && num_formats)
  631. return -EINVAL;
  632. if (formats && num_formats) {
  633. fmts = kmemdup(formats, sizeof(*formats) * num_formats,
  634. GFP_KERNEL);
  635. if (!fmts)
  636. return -ENOMEM;
  637. }
  638. kfree(info->bus_formats);
  639. info->bus_formats = fmts;
  640. info->num_bus_formats = num_formats;
  641. return 0;
  642. }
  643. EXPORT_SYMBOL(drm_display_info_set_bus_formats);
  644. /* Optional connector properties. */
  645. static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] = {
  646. { DRM_MODE_SCALE_NONE, "None" },
  647. { DRM_MODE_SCALE_FULLSCREEN, "Full" },
  648. { DRM_MODE_SCALE_CENTER, "Center" },
  649. { DRM_MODE_SCALE_ASPECT, "Full aspect" },
  650. };
  651. static const struct drm_prop_enum_list drm_aspect_ratio_enum_list[] = {
  652. { DRM_MODE_PICTURE_ASPECT_NONE, "Automatic" },
  653. { DRM_MODE_PICTURE_ASPECT_4_3, "4:3" },
  654. { DRM_MODE_PICTURE_ASPECT_16_9, "16:9" },
  655. };
  656. static const struct drm_prop_enum_list drm_content_type_enum_list[] = {
  657. { DRM_MODE_CONTENT_TYPE_NO_DATA, "No Data" },
  658. { DRM_MODE_CONTENT_TYPE_GRAPHICS, "Graphics" },
  659. { DRM_MODE_CONTENT_TYPE_PHOTO, "Photo" },
  660. { DRM_MODE_CONTENT_TYPE_CINEMA, "Cinema" },
  661. { DRM_MODE_CONTENT_TYPE_GAME, "Game" },
  662. };
  663. static const struct drm_prop_enum_list drm_panel_orientation_enum_list[] = {
  664. { DRM_MODE_PANEL_ORIENTATION_NORMAL, "Normal" },
  665. { DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP, "Upside Down" },
  666. { DRM_MODE_PANEL_ORIENTATION_LEFT_UP, "Left Side Up" },
  667. { DRM_MODE_PANEL_ORIENTATION_RIGHT_UP, "Right Side Up" },
  668. };
  669. static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] = {
  670. { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
  671. { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
  672. { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
  673. };
  674. DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
  675. static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] = {
  676. { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
  677. { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
  678. { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
  679. };
  680. DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
  681. drm_dvi_i_subconnector_enum_list)
  682. static const struct drm_prop_enum_list drm_tv_select_enum_list[] = {
  683. { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
  684. { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
  685. { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
  686. { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
  687. { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
  688. };
  689. DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
  690. static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] = {
  691. { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
  692. { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
  693. { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
  694. { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
  695. { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
  696. };
  697. DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
  698. drm_tv_subconnector_enum_list)
  699. static struct drm_prop_enum_list drm_cp_enum_list[] = {
  700. { DRM_MODE_CONTENT_PROTECTION_UNDESIRED, "Undesired" },
  701. { DRM_MODE_CONTENT_PROTECTION_DESIRED, "Desired" },
  702. { DRM_MODE_CONTENT_PROTECTION_ENABLED, "Enabled" },
  703. };
  704. DRM_ENUM_NAME_FN(drm_get_content_protection_name, drm_cp_enum_list)
  705. /**
  706. * DOC: standard connector properties
  707. *
  708. * DRM connectors have a few standardized properties:
  709. *
  710. * EDID:
  711. * Blob property which contains the current EDID read from the sink. This
  712. * is useful to parse sink identification information like vendor, model
  713. * and serial. Drivers should update this property by calling
  714. * drm_connector_update_edid_property(), usually after having parsed
  715. * the EDID using drm_add_edid_modes(). Userspace cannot change this
  716. * property.
  717. * DPMS:
  718. * Legacy property for setting the power state of the connector. For atomic
  719. * drivers this is only provided for backwards compatibility with existing
  720. * drivers, it remaps to controlling the "ACTIVE" property on the CRTC the
  721. * connector is linked to. Drivers should never set this property directly,
  722. * it is handled by the DRM core by calling the &drm_connector_funcs.dpms
  723. * callback. For atomic drivers the remapping to the "ACTIVE" property is
  724. * implemented in the DRM core. This is the only standard connector
  725. * property that userspace can change.
  726. *
  727. * Note that this property cannot be set through the MODE_ATOMIC ioctl,
  728. * userspace must use "ACTIVE" on the CRTC instead.
  729. *
  730. * WARNING:
  731. *
  732. * For userspace also running on legacy drivers the "DPMS" semantics are a
  733. * lot more complicated. First, userspace cannot rely on the "DPMS" value
  734. * returned by the GETCONNECTOR actually reflecting reality, because many
  735. * drivers fail to update it. For atomic drivers this is taken care of in
  736. * drm_atomic_helper_update_legacy_modeset_state().
  737. *
  738. * The second issue is that the DPMS state is only well-defined when the
  739. * connector is connected to a CRTC. In atomic the DRM core enforces that
  740. * "ACTIVE" is off in such a case, no such checks exists for "DPMS".
  741. *
  742. * Finally, when enabling an output using the legacy SETCONFIG ioctl then
  743. * "DPMS" is forced to ON. But see above, that might not be reflected in
  744. * the software value on legacy drivers.
  745. *
  746. * Summarizing: Only set "DPMS" when the connector is known to be enabled,
  747. * assume that a successful SETCONFIG call also sets "DPMS" to on, and
  748. * never read back the value of "DPMS" because it can be incorrect.
  749. * PATH:
  750. * Connector path property to identify how this sink is physically
  751. * connected. Used by DP MST. This should be set by calling
  752. * drm_connector_set_path_property(), in the case of DP MST with the
  753. * path property the MST manager created. Userspace cannot change this
  754. * property.
  755. * TILE:
  756. * Connector tile group property to indicate how a set of DRM connector
  757. * compose together into one logical screen. This is used by both high-res
  758. * external screens (often only using a single cable, but exposing multiple
  759. * DP MST sinks), or high-res integrated panels (like dual-link DSI) which
  760. * are not gen-locked. Note that for tiled panels which are genlocked, like
  761. * dual-link LVDS or dual-link DSI, the driver should try to not expose the
  762. * tiling and virtualize both &drm_crtc and &drm_plane if needed. Drivers
  763. * should update this value using drm_connector_set_tile_property().
  764. * Userspace cannot change this property.
  765. * link-status:
  766. * Connector link-status property to indicate the status of link. The
  767. * default value of link-status is "GOOD". If something fails during or
  768. * after modeset, the kernel driver may set this to "BAD" and issue a
  769. * hotplug uevent. Drivers should update this value using
  770. * drm_connector_set_link_status_property().
  771. * non_desktop:
  772. * Indicates the output should be ignored for purposes of displaying a
  773. * standard desktop environment or console. This is most likely because
  774. * the output device is not rectilinear.
  775. * Content Protection:
  776. * This property is used by userspace to request the kernel protect future
  777. * content communicated over the link. When requested, kernel will apply
  778. * the appropriate means of protection (most often HDCP), and use the
  779. * property to tell userspace the protection is active.
  780. *
  781. * Drivers can set this up by calling
  782. * drm_connector_attach_content_protection_property() on initialization.
  783. *
  784. * The value of this property can be one of the following:
  785. *
  786. * DRM_MODE_CONTENT_PROTECTION_UNDESIRED = 0
  787. * The link is not protected, content is transmitted in the clear.
  788. * DRM_MODE_CONTENT_PROTECTION_DESIRED = 1
  789. * Userspace has requested content protection, but the link is not
  790. * currently protected. When in this state, kernel should enable
  791. * Content Protection as soon as possible.
  792. * DRM_MODE_CONTENT_PROTECTION_ENABLED = 2
  793. * Userspace has requested content protection, and the link is
  794. * protected. Only the driver can set the property to this value.
  795. * If userspace attempts to set to ENABLED, kernel will return
  796. * -EINVAL.
  797. *
  798. * A few guidelines:
  799. *
  800. * - DESIRED state should be preserved until userspace de-asserts it by
  801. * setting the property to UNDESIRED. This means ENABLED should only
  802. * transition to UNDESIRED when the user explicitly requests it.
  803. * - If the state is DESIRED, kernel should attempt to re-authenticate the
  804. * link whenever possible. This includes across disable/enable, dpms,
  805. * hotplug, downstream device changes, link status failures, etc..
  806. * - Userspace is responsible for polling the property to determine when
  807. * the value transitions from ENABLED to DESIRED. This signifies the link
  808. * is no longer protected and userspace should take appropriate action
  809. * (whatever that might be).
  810. *
  811. * Connectors also have one standardized atomic property:
  812. *
  813. * CRTC_ID:
  814. * Mode object ID of the &drm_crtc this connector should be connected to.
  815. *
  816. * Connectors for LCD panels may also have one standardized property:
  817. *
  818. * panel orientation:
  819. * On some devices the LCD panel is mounted in the casing in such a way
  820. * that the up/top side of the panel does not match with the top side of
  821. * the device. Userspace can use this property to check for this.
  822. * Note that input coordinates from touchscreens (input devices with
  823. * INPUT_PROP_DIRECT) will still map 1:1 to the actual LCD panel
  824. * coordinates, so if userspace rotates the picture to adjust for
  825. * the orientation it must also apply the same transformation to the
  826. * touchscreen input coordinates. This property is initialized by calling
  827. * drm_connector_init_panel_orientation_property().
  828. *
  829. * scaling mode:
  830. * This property defines how a non-native mode is upscaled to the native
  831. * mode of an LCD panel:
  832. *
  833. * None:
  834. * No upscaling happens, scaling is left to the panel. Not all
  835. * drivers expose this mode.
  836. * Full:
  837. * The output is upscaled to the full resolution of the panel,
  838. * ignoring the aspect ratio.
  839. * Center:
  840. * No upscaling happens, the output is centered within the native
  841. * resolution the panel.
  842. * Full aspect:
  843. * The output is upscaled to maximize either the width or height
  844. * while retaining the aspect ratio.
  845. *
  846. * This property should be set up by calling
  847. * drm_connector_attach_scaling_mode_property(). Note that drivers
  848. * can also expose this property to external outputs, in which case they
  849. * must support "None", which should be the default (since external screens
  850. * have a built-in scaler).
  851. */
  852. int drm_connector_create_standard_properties(struct drm_device *dev)
  853. {
  854. struct drm_property *prop;
  855. prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
  856. DRM_MODE_PROP_IMMUTABLE,
  857. "EDID", 0);
  858. if (!prop)
  859. return -ENOMEM;
  860. dev->mode_config.edid_property = prop;
  861. prop = drm_property_create_enum(dev, 0,
  862. "DPMS", drm_dpms_enum_list,
  863. ARRAY_SIZE(drm_dpms_enum_list));
  864. if (!prop)
  865. return -ENOMEM;
  866. dev->mode_config.dpms_property = prop;
  867. prop = drm_property_create(dev,
  868. DRM_MODE_PROP_BLOB |
  869. DRM_MODE_PROP_IMMUTABLE,
  870. "PATH", 0);
  871. if (!prop)
  872. return -ENOMEM;
  873. dev->mode_config.path_property = prop;
  874. prop = drm_property_create(dev,
  875. DRM_MODE_PROP_BLOB |
  876. DRM_MODE_PROP_IMMUTABLE,
  877. "TILE", 0);
  878. if (!prop)
  879. return -ENOMEM;
  880. dev->mode_config.tile_property = prop;
  881. prop = drm_property_create_enum(dev, 0, "link-status",
  882. drm_link_status_enum_list,
  883. ARRAY_SIZE(drm_link_status_enum_list));
  884. if (!prop)
  885. return -ENOMEM;
  886. dev->mode_config.link_status_property = prop;
  887. prop = drm_property_create_bool(dev, DRM_MODE_PROP_IMMUTABLE, "non-desktop");
  888. if (!prop)
  889. return -ENOMEM;
  890. dev->mode_config.non_desktop_property = prop;
  891. return 0;
  892. }
  893. /**
  894. * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
  895. * @dev: DRM device
  896. *
  897. * Called by a driver the first time a DVI-I connector is made.
  898. */
  899. int drm_mode_create_dvi_i_properties(struct drm_device *dev)
  900. {
  901. struct drm_property *dvi_i_selector;
  902. struct drm_property *dvi_i_subconnector;
  903. if (dev->mode_config.dvi_i_select_subconnector_property)
  904. return 0;
  905. dvi_i_selector =
  906. drm_property_create_enum(dev, 0,
  907. "select subconnector",
  908. drm_dvi_i_select_enum_list,
  909. ARRAY_SIZE(drm_dvi_i_select_enum_list));
  910. dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
  911. dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  912. "subconnector",
  913. drm_dvi_i_subconnector_enum_list,
  914. ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
  915. dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
  916. return 0;
  917. }
  918. EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
  919. /**
  920. * DOC: HDMI connector properties
  921. *
  922. * content type (HDMI specific):
  923. * Indicates content type setting to be used in HDMI infoframes to indicate
  924. * content type for the external device, so that it adjusts it's display
  925. * settings accordingly.
  926. *
  927. * The value of this property can be one of the following:
  928. *
  929. * No Data:
  930. * Content type is unknown
  931. * Graphics:
  932. * Content type is graphics
  933. * Photo:
  934. * Content type is photo
  935. * Cinema:
  936. * Content type is cinema
  937. * Game:
  938. * Content type is game
  939. *
  940. * Drivers can set up this property by calling
  941. * drm_connector_attach_content_type_property(). Decoding to
  942. * infoframe values is done through drm_hdmi_avi_infoframe_content_type().
  943. */
  944. /**
  945. * drm_connector_attach_content_type_property - attach content-type property
  946. * @connector: connector to attach content type property on.
  947. *
  948. * Called by a driver the first time a HDMI connector is made.
  949. */
  950. int drm_connector_attach_content_type_property(struct drm_connector *connector)
  951. {
  952. if (!drm_mode_create_content_type_property(connector->dev))
  953. drm_object_attach_property(&connector->base,
  954. connector->dev->mode_config.content_type_property,
  955. DRM_MODE_CONTENT_TYPE_NO_DATA);
  956. return 0;
  957. }
  958. EXPORT_SYMBOL(drm_connector_attach_content_type_property);
  959. /**
  960. * drm_hdmi_avi_infoframe_content_type() - fill the HDMI AVI infoframe
  961. * content type information, based
  962. * on correspondent DRM property.
  963. * @frame: HDMI AVI infoframe
  964. * @conn_state: DRM display connector state
  965. *
  966. */
  967. void drm_hdmi_avi_infoframe_content_type(struct hdmi_avi_infoframe *frame,
  968. const struct drm_connector_state *conn_state)
  969. {
  970. switch (conn_state->content_type) {
  971. case DRM_MODE_CONTENT_TYPE_GRAPHICS:
  972. frame->content_type = HDMI_CONTENT_TYPE_GRAPHICS;
  973. break;
  974. case DRM_MODE_CONTENT_TYPE_CINEMA:
  975. frame->content_type = HDMI_CONTENT_TYPE_CINEMA;
  976. break;
  977. case DRM_MODE_CONTENT_TYPE_GAME:
  978. frame->content_type = HDMI_CONTENT_TYPE_GAME;
  979. break;
  980. case DRM_MODE_CONTENT_TYPE_PHOTO:
  981. frame->content_type = HDMI_CONTENT_TYPE_PHOTO;
  982. break;
  983. default:
  984. /* Graphics is the default(0) */
  985. frame->content_type = HDMI_CONTENT_TYPE_GRAPHICS;
  986. }
  987. frame->itc = conn_state->content_type != DRM_MODE_CONTENT_TYPE_NO_DATA;
  988. }
  989. EXPORT_SYMBOL(drm_hdmi_avi_infoframe_content_type);
  990. /**
  991. * drm_create_tv_properties - create TV specific connector properties
  992. * @dev: DRM device
  993. * @num_modes: number of different TV formats (modes) supported
  994. * @modes: array of pointers to strings containing name of each format
  995. *
  996. * Called by a driver's TV initialization routine, this function creates
  997. * the TV specific connector properties for a given device. Caller is
  998. * responsible for allocating a list of format names and passing them to
  999. * this routine.
  1000. */
  1001. int drm_mode_create_tv_properties(struct drm_device *dev,
  1002. unsigned int num_modes,
  1003. const char * const modes[])
  1004. {
  1005. struct drm_property *tv_selector;
  1006. struct drm_property *tv_subconnector;
  1007. unsigned int i;
  1008. if (dev->mode_config.tv_select_subconnector_property)
  1009. return 0;
  1010. /*
  1011. * Basic connector properties
  1012. */
  1013. tv_selector = drm_property_create_enum(dev, 0,
  1014. "select subconnector",
  1015. drm_tv_select_enum_list,
  1016. ARRAY_SIZE(drm_tv_select_enum_list));
  1017. if (!tv_selector)
  1018. goto nomem;
  1019. dev->mode_config.tv_select_subconnector_property = tv_selector;
  1020. tv_subconnector =
  1021. drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  1022. "subconnector",
  1023. drm_tv_subconnector_enum_list,
  1024. ARRAY_SIZE(drm_tv_subconnector_enum_list));
  1025. if (!tv_subconnector)
  1026. goto nomem;
  1027. dev->mode_config.tv_subconnector_property = tv_subconnector;
  1028. /*
  1029. * Other, TV specific properties: margins & TV modes.
  1030. */
  1031. dev->mode_config.tv_left_margin_property =
  1032. drm_property_create_range(dev, 0, "left margin", 0, 100);
  1033. if (!dev->mode_config.tv_left_margin_property)
  1034. goto nomem;
  1035. dev->mode_config.tv_right_margin_property =
  1036. drm_property_create_range(dev, 0, "right margin", 0, 100);
  1037. if (!dev->mode_config.tv_right_margin_property)
  1038. goto nomem;
  1039. dev->mode_config.tv_top_margin_property =
  1040. drm_property_create_range(dev, 0, "top margin", 0, 100);
  1041. if (!dev->mode_config.tv_top_margin_property)
  1042. goto nomem;
  1043. dev->mode_config.tv_bottom_margin_property =
  1044. drm_property_create_range(dev, 0, "bottom margin", 0, 100);
  1045. if (!dev->mode_config.tv_bottom_margin_property)
  1046. goto nomem;
  1047. dev->mode_config.tv_mode_property =
  1048. drm_property_create(dev, DRM_MODE_PROP_ENUM,
  1049. "mode", num_modes);
  1050. if (!dev->mode_config.tv_mode_property)
  1051. goto nomem;
  1052. for (i = 0; i < num_modes; i++)
  1053. drm_property_add_enum(dev->mode_config.tv_mode_property,
  1054. i, modes[i]);
  1055. dev->mode_config.tv_brightness_property =
  1056. drm_property_create_range(dev, 0, "brightness", 0, 100);
  1057. if (!dev->mode_config.tv_brightness_property)
  1058. goto nomem;
  1059. dev->mode_config.tv_contrast_property =
  1060. drm_property_create_range(dev, 0, "contrast", 0, 100);
  1061. if (!dev->mode_config.tv_contrast_property)
  1062. goto nomem;
  1063. dev->mode_config.tv_flicker_reduction_property =
  1064. drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
  1065. if (!dev->mode_config.tv_flicker_reduction_property)
  1066. goto nomem;
  1067. dev->mode_config.tv_overscan_property =
  1068. drm_property_create_range(dev, 0, "overscan", 0, 100);
  1069. if (!dev->mode_config.tv_overscan_property)
  1070. goto nomem;
  1071. dev->mode_config.tv_saturation_property =
  1072. drm_property_create_range(dev, 0, "saturation", 0, 100);
  1073. if (!dev->mode_config.tv_saturation_property)
  1074. goto nomem;
  1075. dev->mode_config.tv_hue_property =
  1076. drm_property_create_range(dev, 0, "hue", 0, 100);
  1077. if (!dev->mode_config.tv_hue_property)
  1078. goto nomem;
  1079. return 0;
  1080. nomem:
  1081. return -ENOMEM;
  1082. }
  1083. EXPORT_SYMBOL(drm_mode_create_tv_properties);
  1084. /**
  1085. * drm_mode_create_scaling_mode_property - create scaling mode property
  1086. * @dev: DRM device
  1087. *
  1088. * Called by a driver the first time it's needed, must be attached to desired
  1089. * connectors.
  1090. *
  1091. * Atomic drivers should use drm_connector_attach_scaling_mode_property()
  1092. * instead to correctly assign &drm_connector_state.picture_aspect_ratio
  1093. * in the atomic state.
  1094. */
  1095. int drm_mode_create_scaling_mode_property(struct drm_device *dev)
  1096. {
  1097. struct drm_property *scaling_mode;
  1098. if (dev->mode_config.scaling_mode_property)
  1099. return 0;
  1100. scaling_mode =
  1101. drm_property_create_enum(dev, 0, "scaling mode",
  1102. drm_scaling_mode_enum_list,
  1103. ARRAY_SIZE(drm_scaling_mode_enum_list));
  1104. dev->mode_config.scaling_mode_property = scaling_mode;
  1105. return 0;
  1106. }
  1107. EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
  1108. /**
  1109. * drm_connector_attach_scaling_mode_property - attach atomic scaling mode property
  1110. * @connector: connector to attach scaling mode property on.
  1111. * @scaling_mode_mask: or'ed mask of BIT(%DRM_MODE_SCALE_\*).
  1112. *
  1113. * This is used to add support for scaling mode to atomic drivers.
  1114. * The scaling mode will be set to &drm_connector_state.picture_aspect_ratio
  1115. * and can be used from &drm_connector_helper_funcs->atomic_check for validation.
  1116. *
  1117. * This is the atomic version of drm_mode_create_scaling_mode_property().
  1118. *
  1119. * Returns:
  1120. * Zero on success, negative errno on failure.
  1121. */
  1122. int drm_connector_attach_scaling_mode_property(struct drm_connector *connector,
  1123. u32 scaling_mode_mask)
  1124. {
  1125. struct drm_device *dev = connector->dev;
  1126. struct drm_property *scaling_mode_property;
  1127. int i;
  1128. const unsigned valid_scaling_mode_mask =
  1129. (1U << ARRAY_SIZE(drm_scaling_mode_enum_list)) - 1;
  1130. if (WARN_ON(hweight32(scaling_mode_mask) < 2 ||
  1131. scaling_mode_mask & ~valid_scaling_mode_mask))
  1132. return -EINVAL;
  1133. scaling_mode_property =
  1134. drm_property_create(dev, DRM_MODE_PROP_ENUM, "scaling mode",
  1135. hweight32(scaling_mode_mask));
  1136. if (!scaling_mode_property)
  1137. return -ENOMEM;
  1138. for (i = 0; i < ARRAY_SIZE(drm_scaling_mode_enum_list); i++) {
  1139. int ret;
  1140. if (!(BIT(i) & scaling_mode_mask))
  1141. continue;
  1142. ret = drm_property_add_enum(scaling_mode_property,
  1143. drm_scaling_mode_enum_list[i].type,
  1144. drm_scaling_mode_enum_list[i].name);
  1145. if (ret) {
  1146. drm_property_destroy(dev, scaling_mode_property);
  1147. return ret;
  1148. }
  1149. }
  1150. drm_object_attach_property(&connector->base,
  1151. scaling_mode_property, 0);
  1152. connector->scaling_mode_property = scaling_mode_property;
  1153. return 0;
  1154. }
  1155. EXPORT_SYMBOL(drm_connector_attach_scaling_mode_property);
  1156. /**
  1157. * drm_connector_attach_content_protection_property - attach content protection
  1158. * property
  1159. *
  1160. * @connector: connector to attach CP property on.
  1161. *
  1162. * This is used to add support for content protection on select connectors.
  1163. * Content Protection is intentionally vague to allow for different underlying
  1164. * technologies, however it is most implemented by HDCP.
  1165. *
  1166. * The content protection will be set to &drm_connector_state.content_protection
  1167. *
  1168. * Returns:
  1169. * Zero on success, negative errno on failure.
  1170. */
  1171. int drm_connector_attach_content_protection_property(
  1172. struct drm_connector *connector)
  1173. {
  1174. struct drm_device *dev = connector->dev;
  1175. struct drm_property *prop;
  1176. prop = drm_property_create_enum(dev, 0, "Content Protection",
  1177. drm_cp_enum_list,
  1178. ARRAY_SIZE(drm_cp_enum_list));
  1179. if (!prop)
  1180. return -ENOMEM;
  1181. drm_object_attach_property(&connector->base, prop,
  1182. DRM_MODE_CONTENT_PROTECTION_UNDESIRED);
  1183. connector->content_protection_property = prop;
  1184. return 0;
  1185. }
  1186. EXPORT_SYMBOL(drm_connector_attach_content_protection_property);
  1187. /**
  1188. * drm_mode_create_aspect_ratio_property - create aspect ratio property
  1189. * @dev: DRM device
  1190. *
  1191. * Called by a driver the first time it's needed, must be attached to desired
  1192. * connectors.
  1193. *
  1194. * Returns:
  1195. * Zero on success, negative errno on failure.
  1196. */
  1197. int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
  1198. {
  1199. if (dev->mode_config.aspect_ratio_property)
  1200. return 0;
  1201. dev->mode_config.aspect_ratio_property =
  1202. drm_property_create_enum(dev, 0, "aspect ratio",
  1203. drm_aspect_ratio_enum_list,
  1204. ARRAY_SIZE(drm_aspect_ratio_enum_list));
  1205. if (dev->mode_config.aspect_ratio_property == NULL)
  1206. return -ENOMEM;
  1207. return 0;
  1208. }
  1209. EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
  1210. /**
  1211. * drm_mode_create_content_type_property - create content type property
  1212. * @dev: DRM device
  1213. *
  1214. * Called by a driver the first time it's needed, must be attached to desired
  1215. * connectors.
  1216. *
  1217. * Returns:
  1218. * Zero on success, negative errno on failure.
  1219. */
  1220. int drm_mode_create_content_type_property(struct drm_device *dev)
  1221. {
  1222. if (dev->mode_config.content_type_property)
  1223. return 0;
  1224. dev->mode_config.content_type_property =
  1225. drm_property_create_enum(dev, 0, "content type",
  1226. drm_content_type_enum_list,
  1227. ARRAY_SIZE(drm_content_type_enum_list));
  1228. if (dev->mode_config.content_type_property == NULL)
  1229. return -ENOMEM;
  1230. return 0;
  1231. }
  1232. EXPORT_SYMBOL(drm_mode_create_content_type_property);
  1233. /**
  1234. * drm_mode_create_suggested_offset_properties - create suggests offset properties
  1235. * @dev: DRM device
  1236. *
  1237. * Create the the suggested x/y offset property for connectors.
  1238. */
  1239. int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
  1240. {
  1241. if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
  1242. return 0;
  1243. dev->mode_config.suggested_x_property =
  1244. drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
  1245. dev->mode_config.suggested_y_property =
  1246. drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
  1247. if (dev->mode_config.suggested_x_property == NULL ||
  1248. dev->mode_config.suggested_y_property == NULL)
  1249. return -ENOMEM;
  1250. return 0;
  1251. }
  1252. EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
  1253. /**
  1254. * drm_connector_set_path_property - set tile property on connector
  1255. * @connector: connector to set property on.
  1256. * @path: path to use for property; must not be NULL.
  1257. *
  1258. * This creates a property to expose to userspace to specify a
  1259. * connector path. This is mainly used for DisplayPort MST where
  1260. * connectors have a topology and we want to allow userspace to give
  1261. * them more meaningful names.
  1262. *
  1263. * Returns:
  1264. * Zero on success, negative errno on failure.
  1265. */
  1266. int drm_connector_set_path_property(struct drm_connector *connector,
  1267. const char *path)
  1268. {
  1269. struct drm_device *dev = connector->dev;
  1270. int ret;
  1271. ret = drm_property_replace_global_blob(dev,
  1272. &connector->path_blob_ptr,
  1273. strlen(path) + 1,
  1274. path,
  1275. &connector->base,
  1276. dev->mode_config.path_property);
  1277. return ret;
  1278. }
  1279. EXPORT_SYMBOL(drm_connector_set_path_property);
  1280. /**
  1281. * drm_connector_set_tile_property - set tile property on connector
  1282. * @connector: connector to set property on.
  1283. *
  1284. * This looks up the tile information for a connector, and creates a
  1285. * property for userspace to parse if it exists. The property is of
  1286. * the form of 8 integers using ':' as a separator.
  1287. *
  1288. * Returns:
  1289. * Zero on success, errno on failure.
  1290. */
  1291. int drm_connector_set_tile_property(struct drm_connector *connector)
  1292. {
  1293. struct drm_device *dev = connector->dev;
  1294. char tile[256];
  1295. int ret;
  1296. if (!connector->has_tile) {
  1297. ret = drm_property_replace_global_blob(dev,
  1298. &connector->tile_blob_ptr,
  1299. 0,
  1300. NULL,
  1301. &connector->base,
  1302. dev->mode_config.tile_property);
  1303. return ret;
  1304. }
  1305. snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
  1306. connector->tile_group->id, connector->tile_is_single_monitor,
  1307. connector->num_h_tile, connector->num_v_tile,
  1308. connector->tile_h_loc, connector->tile_v_loc,
  1309. connector->tile_h_size, connector->tile_v_size);
  1310. ret = drm_property_replace_global_blob(dev,
  1311. &connector->tile_blob_ptr,
  1312. strlen(tile) + 1,
  1313. tile,
  1314. &connector->base,
  1315. dev->mode_config.tile_property);
  1316. return ret;
  1317. }
  1318. EXPORT_SYMBOL(drm_connector_set_tile_property);
  1319. /**
  1320. * drm_connector_update_edid_property - update the edid property of a connector
  1321. * @connector: drm connector
  1322. * @edid: new value of the edid property
  1323. *
  1324. * This function creates a new blob modeset object and assigns its id to the
  1325. * connector's edid property.
  1326. *
  1327. * Returns:
  1328. * Zero on success, negative errno on failure.
  1329. */
  1330. int drm_connector_update_edid_property(struct drm_connector *connector,
  1331. const struct edid *edid)
  1332. {
  1333. struct drm_device *dev = connector->dev;
  1334. size_t size = 0;
  1335. int ret;
  1336. /* ignore requests to set edid when overridden */
  1337. if (connector->override_edid)
  1338. return 0;
  1339. if (edid)
  1340. size = EDID_LENGTH * (1 + edid->extensions);
  1341. /* Set the display info, using edid if available, otherwise
  1342. * reseting the values to defaults. This duplicates the work
  1343. * done in drm_add_edid_modes, but that function is not
  1344. * consistently called before this one in all drivers and the
  1345. * computation is cheap enough that it seems better to
  1346. * duplicate it rather than attempt to ensure some arbitrary
  1347. * ordering of calls.
  1348. */
  1349. if (edid)
  1350. drm_add_display_info(connector, edid);
  1351. else
  1352. drm_reset_display_info(connector);
  1353. drm_object_property_set_value(&connector->base,
  1354. dev->mode_config.non_desktop_property,
  1355. connector->display_info.non_desktop);
  1356. ret = drm_property_replace_global_blob(dev,
  1357. &connector->edid_blob_ptr,
  1358. size,
  1359. edid,
  1360. &connector->base,
  1361. dev->mode_config.edid_property);
  1362. return ret;
  1363. }
  1364. EXPORT_SYMBOL(drm_connector_update_edid_property);
  1365. /**
  1366. * drm_connector_set_link_status_property - Set link status property of a connector
  1367. * @connector: drm connector
  1368. * @link_status: new value of link status property (0: Good, 1: Bad)
  1369. *
  1370. * In usual working scenario, this link status property will always be set to
  1371. * "GOOD". If something fails during or after a mode set, the kernel driver
  1372. * may set this link status property to "BAD". The caller then needs to send a
  1373. * hotplug uevent for userspace to re-check the valid modes through
  1374. * GET_CONNECTOR_IOCTL and retry modeset.
  1375. *
  1376. * Note: Drivers cannot rely on userspace to support this property and
  1377. * issue a modeset. As such, they may choose to handle issues (like
  1378. * re-training a link) without userspace's intervention.
  1379. *
  1380. * The reason for adding this property is to handle link training failures, but
  1381. * it is not limited to DP or link training. For example, if we implement
  1382. * asynchronous setcrtc, this property can be used to report any failures in that.
  1383. */
  1384. void drm_connector_set_link_status_property(struct drm_connector *connector,
  1385. uint64_t link_status)
  1386. {
  1387. struct drm_device *dev = connector->dev;
  1388. drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
  1389. connector->state->link_status = link_status;
  1390. drm_modeset_unlock(&dev->mode_config.connection_mutex);
  1391. }
  1392. EXPORT_SYMBOL(drm_connector_set_link_status_property);
  1393. /**
  1394. * drm_connector_init_panel_orientation_property -
  1395. * initialize the connecters panel_orientation property
  1396. * @connector: connector for which to init the panel-orientation property.
  1397. * @width: width in pixels of the panel, used for panel quirk detection
  1398. * @height: height in pixels of the panel, used for panel quirk detection
  1399. *
  1400. * This function should only be called for built-in panels, after setting
  1401. * connector->display_info.panel_orientation first (if known).
  1402. *
  1403. * This function will check for platform specific (e.g. DMI based) quirks
  1404. * overriding display_info.panel_orientation first, then if panel_orientation
  1405. * is not DRM_MODE_PANEL_ORIENTATION_UNKNOWN it will attach the
  1406. * "panel orientation" property to the connector.
  1407. *
  1408. * Returns:
  1409. * Zero on success, negative errno on failure.
  1410. */
  1411. int drm_connector_init_panel_orientation_property(
  1412. struct drm_connector *connector, int width, int height)
  1413. {
  1414. struct drm_device *dev = connector->dev;
  1415. struct drm_display_info *info = &connector->display_info;
  1416. struct drm_property *prop;
  1417. int orientation_quirk;
  1418. orientation_quirk = drm_get_panel_orientation_quirk(width, height);
  1419. if (orientation_quirk != DRM_MODE_PANEL_ORIENTATION_UNKNOWN)
  1420. info->panel_orientation = orientation_quirk;
  1421. if (info->panel_orientation == DRM_MODE_PANEL_ORIENTATION_UNKNOWN)
  1422. return 0;
  1423. prop = dev->mode_config.panel_orientation_property;
  1424. if (!prop) {
  1425. prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  1426. "panel orientation",
  1427. drm_panel_orientation_enum_list,
  1428. ARRAY_SIZE(drm_panel_orientation_enum_list));
  1429. if (!prop)
  1430. return -ENOMEM;
  1431. dev->mode_config.panel_orientation_property = prop;
  1432. }
  1433. drm_object_attach_property(&connector->base, prop,
  1434. info->panel_orientation);
  1435. return 0;
  1436. }
  1437. EXPORT_SYMBOL(drm_connector_init_panel_orientation_property);
  1438. int drm_connector_set_obj_prop(struct drm_mode_object *obj,
  1439. struct drm_property *property,
  1440. uint64_t value)
  1441. {
  1442. int ret = -EINVAL;
  1443. struct drm_connector *connector = obj_to_connector(obj);
  1444. /* Do DPMS ourselves */
  1445. if (property == connector->dev->mode_config.dpms_property) {
  1446. ret = (*connector->funcs->dpms)(connector, (int)value);
  1447. } else if (connector->funcs->set_property)
  1448. ret = connector->funcs->set_property(connector, property, value);
  1449. if (!ret)
  1450. drm_object_property_set_value(&connector->base, property, value);
  1451. return ret;
  1452. }
  1453. int drm_connector_property_set_ioctl(struct drm_device *dev,
  1454. void *data, struct drm_file *file_priv)
  1455. {
  1456. struct drm_mode_connector_set_property *conn_set_prop = data;
  1457. struct drm_mode_obj_set_property obj_set_prop = {
  1458. .value = conn_set_prop->value,
  1459. .prop_id = conn_set_prop->prop_id,
  1460. .obj_id = conn_set_prop->connector_id,
  1461. .obj_type = DRM_MODE_OBJECT_CONNECTOR
  1462. };
  1463. /* It does all the locking and checking we need */
  1464. return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
  1465. }
  1466. static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
  1467. {
  1468. /* For atomic drivers only state objects are synchronously updated and
  1469. * protected by modeset locks, so check those first. */
  1470. if (connector->state)
  1471. return connector->state->best_encoder;
  1472. return connector->encoder;
  1473. }
  1474. static bool
  1475. drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
  1476. const struct list_head *export_list,
  1477. const struct drm_file *file_priv)
  1478. {
  1479. /*
  1480. * If user-space hasn't configured the driver to expose the stereo 3D
  1481. * modes, don't expose them.
  1482. */
  1483. if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
  1484. return false;
  1485. /*
  1486. * If user-space hasn't configured the driver to expose the modes
  1487. * with aspect-ratio, don't expose them. However if such a mode
  1488. * is unique, let it be exposed, but reset the aspect-ratio flags
  1489. * while preparing the list of user-modes.
  1490. */
  1491. if (!file_priv->aspect_ratio_allowed) {
  1492. struct drm_display_mode *mode_itr;
  1493. list_for_each_entry(mode_itr, export_list, export_head)
  1494. if (drm_mode_match(mode_itr, mode,
  1495. DRM_MODE_MATCH_TIMINGS |
  1496. DRM_MODE_MATCH_CLOCK |
  1497. DRM_MODE_MATCH_FLAGS |
  1498. DRM_MODE_MATCH_3D_FLAGS))
  1499. return false;
  1500. }
  1501. return true;
  1502. }
  1503. int drm_mode_getconnector(struct drm_device *dev, void *data,
  1504. struct drm_file *file_priv)
  1505. {
  1506. struct drm_mode_get_connector *out_resp = data;
  1507. struct drm_connector *connector;
  1508. struct drm_encoder *encoder;
  1509. struct drm_display_mode *mode;
  1510. int mode_count = 0;
  1511. int encoders_count = 0;
  1512. int ret = 0;
  1513. int copied = 0;
  1514. int i;
  1515. struct drm_mode_modeinfo u_mode;
  1516. struct drm_mode_modeinfo __user *mode_ptr;
  1517. uint32_t __user *encoder_ptr;
  1518. LIST_HEAD(export_list);
  1519. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1520. return -EOPNOTSUPP;
  1521. memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
  1522. connector = drm_connector_lookup(dev, file_priv, out_resp->connector_id);
  1523. if (!connector)
  1524. return -ENOENT;
  1525. drm_connector_for_each_possible_encoder(connector, encoder, i)
  1526. encoders_count++;
  1527. if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
  1528. copied = 0;
  1529. encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
  1530. drm_connector_for_each_possible_encoder(connector, encoder, i) {
  1531. if (put_user(encoder->base.id, encoder_ptr + copied)) {
  1532. ret = -EFAULT;
  1533. goto out;
  1534. }
  1535. copied++;
  1536. }
  1537. }
  1538. out_resp->count_encoders = encoders_count;
  1539. out_resp->connector_id = connector->base.id;
  1540. out_resp->connector_type = connector->connector_type;
  1541. out_resp->connector_type_id = connector->connector_type_id;
  1542. mutex_lock(&dev->mode_config.mutex);
  1543. if (out_resp->count_modes == 0) {
  1544. connector->funcs->fill_modes(connector,
  1545. dev->mode_config.max_width,
  1546. dev->mode_config.max_height);
  1547. }
  1548. out_resp->mm_width = connector->display_info.width_mm;
  1549. out_resp->mm_height = connector->display_info.height_mm;
  1550. out_resp->subpixel = connector->display_info.subpixel_order;
  1551. out_resp->connection = connector->status;
  1552. /* delayed so we get modes regardless of pre-fill_modes state */
  1553. list_for_each_entry(mode, &connector->modes, head)
  1554. if (drm_mode_expose_to_userspace(mode, &export_list,
  1555. file_priv)) {
  1556. list_add_tail(&mode->export_head, &export_list);
  1557. mode_count++;
  1558. }
  1559. /*
  1560. * This ioctl is called twice, once to determine how much space is
  1561. * needed, and the 2nd time to fill it.
  1562. * The modes that need to be exposed to the user are maintained in the
  1563. * 'export_list'. When the ioctl is called first time to determine the,
  1564. * space, the export_list gets filled, to find the no.of modes. In the
  1565. * 2nd time, the user modes are filled, one by one from the export_list.
  1566. */
  1567. if ((out_resp->count_modes >= mode_count) && mode_count) {
  1568. copied = 0;
  1569. mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
  1570. list_for_each_entry(mode, &export_list, export_head) {
  1571. drm_mode_convert_to_umode(&u_mode, mode);
  1572. /*
  1573. * Reset aspect ratio flags of user-mode, if modes with
  1574. * aspect-ratio are not supported.
  1575. */
  1576. if (!file_priv->aspect_ratio_allowed)
  1577. u_mode.flags &= ~DRM_MODE_FLAG_PIC_AR_MASK;
  1578. if (copy_to_user(mode_ptr + copied,
  1579. &u_mode, sizeof(u_mode))) {
  1580. ret = -EFAULT;
  1581. mutex_unlock(&dev->mode_config.mutex);
  1582. goto out;
  1583. }
  1584. copied++;
  1585. }
  1586. }
  1587. out_resp->count_modes = mode_count;
  1588. mutex_unlock(&dev->mode_config.mutex);
  1589. drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
  1590. encoder = drm_connector_get_encoder(connector);
  1591. if (encoder)
  1592. out_resp->encoder_id = encoder->base.id;
  1593. else
  1594. out_resp->encoder_id = 0;
  1595. /* Only grab properties after probing, to make sure EDID and other
  1596. * properties reflect the latest status. */
  1597. ret = drm_mode_object_get_properties(&connector->base, file_priv->atomic,
  1598. (uint32_t __user *)(unsigned long)(out_resp->props_ptr),
  1599. (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
  1600. &out_resp->count_props);
  1601. drm_modeset_unlock(&dev->mode_config.connection_mutex);
  1602. out:
  1603. drm_connector_put(connector);
  1604. return ret;
  1605. }
  1606. /**
  1607. * DOC: Tile group
  1608. *
  1609. * Tile groups are used to represent tiled monitors with a unique integer
  1610. * identifier. Tiled monitors using DisplayID v1.3 have a unique 8-byte handle,
  1611. * we store this in a tile group, so we have a common identifier for all tiles
  1612. * in a monitor group. The property is called "TILE". Drivers can manage tile
  1613. * groups using drm_mode_create_tile_group(), drm_mode_put_tile_group() and
  1614. * drm_mode_get_tile_group(). But this is only needed for internal panels where
  1615. * the tile group information is exposed through a non-standard way.
  1616. */
  1617. static void drm_tile_group_free(struct kref *kref)
  1618. {
  1619. struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
  1620. struct drm_device *dev = tg->dev;
  1621. mutex_lock(&dev->mode_config.idr_mutex);
  1622. idr_remove(&dev->mode_config.tile_idr, tg->id);
  1623. mutex_unlock(&dev->mode_config.idr_mutex);
  1624. kfree(tg);
  1625. }
  1626. /**
  1627. * drm_mode_put_tile_group - drop a reference to a tile group.
  1628. * @dev: DRM device
  1629. * @tg: tile group to drop reference to.
  1630. *
  1631. * drop reference to tile group and free if 0.
  1632. */
  1633. void drm_mode_put_tile_group(struct drm_device *dev,
  1634. struct drm_tile_group *tg)
  1635. {
  1636. kref_put(&tg->refcount, drm_tile_group_free);
  1637. }
  1638. EXPORT_SYMBOL(drm_mode_put_tile_group);
  1639. /**
  1640. * drm_mode_get_tile_group - get a reference to an existing tile group
  1641. * @dev: DRM device
  1642. * @topology: 8-bytes unique per monitor.
  1643. *
  1644. * Use the unique bytes to get a reference to an existing tile group.
  1645. *
  1646. * RETURNS:
  1647. * tile group or NULL if not found.
  1648. */
  1649. struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
  1650. char topology[8])
  1651. {
  1652. struct drm_tile_group *tg;
  1653. int id;
  1654. mutex_lock(&dev->mode_config.idr_mutex);
  1655. idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
  1656. if (!memcmp(tg->group_data, topology, 8)) {
  1657. if (!kref_get_unless_zero(&tg->refcount))
  1658. tg = NULL;
  1659. mutex_unlock(&dev->mode_config.idr_mutex);
  1660. return tg;
  1661. }
  1662. }
  1663. mutex_unlock(&dev->mode_config.idr_mutex);
  1664. return NULL;
  1665. }
  1666. EXPORT_SYMBOL(drm_mode_get_tile_group);
  1667. /**
  1668. * drm_mode_create_tile_group - create a tile group from a displayid description
  1669. * @dev: DRM device
  1670. * @topology: 8-bytes unique per monitor.
  1671. *
  1672. * Create a tile group for the unique monitor, and get a unique
  1673. * identifier for the tile group.
  1674. *
  1675. * RETURNS:
  1676. * new tile group or error.
  1677. */
  1678. struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
  1679. char topology[8])
  1680. {
  1681. struct drm_tile_group *tg;
  1682. int ret;
  1683. tg = kzalloc(sizeof(*tg), GFP_KERNEL);
  1684. if (!tg)
  1685. return ERR_PTR(-ENOMEM);
  1686. kref_init(&tg->refcount);
  1687. memcpy(tg->group_data, topology, 8);
  1688. tg->dev = dev;
  1689. mutex_lock(&dev->mode_config.idr_mutex);
  1690. ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
  1691. if (ret >= 0) {
  1692. tg->id = ret;
  1693. } else {
  1694. kfree(tg);
  1695. tg = ERR_PTR(ret);
  1696. }
  1697. mutex_unlock(&dev->mode_config.idr_mutex);
  1698. return tg;
  1699. }
  1700. EXPORT_SYMBOL(drm_mode_create_tile_group);