drm_sysfs.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. /*
  2. * drm_sysfs.c - Modifications to drm_sysfs_class.c to support
  3. * extra sysfs attribute from DRM. Normal drm_sysfs_class
  4. * does not allow adding attributes.
  5. *
  6. * Copyright (c) 2004 Jon Smirl <jonsmirl@gmail.com>
  7. * Copyright (c) 2003-2004 Greg Kroah-Hartman <greg@kroah.com>
  8. * Copyright (c) 2003-2004 IBM Corp.
  9. *
  10. * This file is released under the GPLv2
  11. *
  12. */
  13. #include <linux/device.h>
  14. #include <linux/kdev_t.h>
  15. #include <linux/err.h>
  16. #include "drm_core.h"
  17. #include "drmP.h"
  18. #define to_drm_minor(d) container_of(d, struct drm_minor, kdev)
  19. #define to_drm_connector(d) container_of(d, struct drm_connector, kdev)
  20. /**
  21. * drm_sysfs_suspend - DRM class suspend hook
  22. * @dev: Linux device to suspend
  23. * @state: power state to enter
  24. *
  25. * Just figures out what the actual struct drm_device associated with
  26. * @dev is and calls its suspend hook, if present.
  27. */
  28. static int drm_sysfs_suspend(struct device *dev, pm_message_t state)
  29. {
  30. struct drm_minor *drm_minor = to_drm_minor(dev);
  31. struct drm_device *drm_dev = drm_minor->dev;
  32. if (drm_minor->type == DRM_MINOR_LEGACY &&
  33. !drm_core_check_feature(drm_dev, DRIVER_MODESET) &&
  34. drm_dev->driver->suspend)
  35. return drm_dev->driver->suspend(drm_dev, state);
  36. return 0;
  37. }
  38. /**
  39. * drm_sysfs_resume - DRM class resume hook
  40. * @dev: Linux device to resume
  41. *
  42. * Just figures out what the actual struct drm_device associated with
  43. * @dev is and calls its resume hook, if present.
  44. */
  45. static int drm_sysfs_resume(struct device *dev)
  46. {
  47. struct drm_minor *drm_minor = to_drm_minor(dev);
  48. struct drm_device *drm_dev = drm_minor->dev;
  49. if (drm_minor->type == DRM_MINOR_LEGACY &&
  50. !drm_core_check_feature(drm_dev, DRIVER_MODESET) &&
  51. drm_dev->driver->resume)
  52. return drm_dev->driver->resume(drm_dev);
  53. return 0;
  54. }
  55. /* Display the version of drm_core. This doesn't work right in current design */
  56. static ssize_t version_show(struct class *dev, char *buf)
  57. {
  58. return sprintf(buf, "%s %d.%d.%d %s\n", CORE_NAME, CORE_MAJOR,
  59. CORE_MINOR, CORE_PATCHLEVEL, CORE_DATE);
  60. }
  61. static CLASS_ATTR(version, S_IRUGO, version_show, NULL);
  62. /**
  63. * drm_sysfs_create - create a struct drm_sysfs_class structure
  64. * @owner: pointer to the module that is to "own" this struct drm_sysfs_class
  65. * @name: pointer to a string for the name of this class.
  66. *
  67. * This is used to create DRM class pointer that can then be used
  68. * in calls to drm_sysfs_device_add().
  69. *
  70. * Note, the pointer created here is to be destroyed when finished by making a
  71. * call to drm_sysfs_destroy().
  72. */
  73. struct class *drm_sysfs_create(struct module *owner, char *name)
  74. {
  75. struct class *class;
  76. int err;
  77. class = class_create(owner, name);
  78. if (IS_ERR(class)) {
  79. err = PTR_ERR(class);
  80. goto err_out;
  81. }
  82. class->suspend = drm_sysfs_suspend;
  83. class->resume = drm_sysfs_resume;
  84. err = class_create_file(class, &class_attr_version);
  85. if (err)
  86. goto err_out_class;
  87. return class;
  88. err_out_class:
  89. class_destroy(class);
  90. err_out:
  91. return ERR_PTR(err);
  92. }
  93. /**
  94. * drm_sysfs_destroy - destroys DRM class
  95. *
  96. * Destroy the DRM device class.
  97. */
  98. void drm_sysfs_destroy(void)
  99. {
  100. if ((drm_class == NULL) || (IS_ERR(drm_class)))
  101. return;
  102. class_remove_file(drm_class, &class_attr_version);
  103. class_destroy(drm_class);
  104. }
  105. /**
  106. * drm_sysfs_device_release - do nothing
  107. * @dev: Linux device
  108. *
  109. * Normally, this would free the DRM device associated with @dev, along
  110. * with cleaning up any other stuff. But we do that in the DRM core, so
  111. * this function can just return and hope that the core does its job.
  112. */
  113. static void drm_sysfs_device_release(struct device *dev)
  114. {
  115. memset(dev, 0, sizeof(struct device));
  116. return;
  117. }
  118. /*
  119. * Connector properties
  120. */
  121. static ssize_t status_show(struct device *device,
  122. struct device_attribute *attr,
  123. char *buf)
  124. {
  125. struct drm_connector *connector = to_drm_connector(device);
  126. enum drm_connector_status status;
  127. status = connector->funcs->detect(connector);
  128. return snprintf(buf, PAGE_SIZE, "%s",
  129. drm_get_connector_status_name(status));
  130. }
  131. static ssize_t dpms_show(struct device *device,
  132. struct device_attribute *attr,
  133. char *buf)
  134. {
  135. struct drm_connector *connector = to_drm_connector(device);
  136. struct drm_device *dev = connector->dev;
  137. uint64_t dpms_status;
  138. int ret;
  139. ret = drm_connector_property_get_value(connector,
  140. dev->mode_config.dpms_property,
  141. &dpms_status);
  142. if (ret)
  143. return 0;
  144. return snprintf(buf, PAGE_SIZE, "%s",
  145. drm_get_dpms_name((int)dpms_status));
  146. }
  147. static ssize_t enabled_show(struct device *device,
  148. struct device_attribute *attr,
  149. char *buf)
  150. {
  151. struct drm_connector *connector = to_drm_connector(device);
  152. return snprintf(buf, PAGE_SIZE, connector->encoder ? "enabled" :
  153. "disabled");
  154. }
  155. static ssize_t edid_show(struct kobject *kobj, struct bin_attribute *attr,
  156. char *buf, loff_t off, size_t count)
  157. {
  158. struct device *connector_dev = container_of(kobj, struct device, kobj);
  159. struct drm_connector *connector = to_drm_connector(connector_dev);
  160. unsigned char *edid;
  161. size_t size;
  162. if (!connector->edid_blob_ptr)
  163. return 0;
  164. edid = connector->edid_blob_ptr->data;
  165. size = connector->edid_blob_ptr->length;
  166. if (!edid)
  167. return 0;
  168. if (off >= size)
  169. return 0;
  170. if (off + count > size)
  171. count = size - off;
  172. memcpy(buf, edid + off, count);
  173. return count;
  174. }
  175. static ssize_t modes_show(struct device *device,
  176. struct device_attribute *attr,
  177. char *buf)
  178. {
  179. struct drm_connector *connector = to_drm_connector(device);
  180. struct drm_display_mode *mode;
  181. int written = 0;
  182. list_for_each_entry(mode, &connector->modes, head) {
  183. written += snprintf(buf + written, PAGE_SIZE - written, "%s\n",
  184. mode->name);
  185. }
  186. return written;
  187. }
  188. static ssize_t subconnector_show(struct device *device,
  189. struct device_attribute *attr,
  190. char *buf)
  191. {
  192. struct drm_connector *connector = to_drm_connector(device);
  193. struct drm_device *dev = connector->dev;
  194. struct drm_property *prop = NULL;
  195. uint64_t subconnector;
  196. int is_tv = 0;
  197. int ret;
  198. switch (connector->connector_type) {
  199. case DRM_MODE_CONNECTOR_DVII:
  200. prop = dev->mode_config.dvi_i_subconnector_property;
  201. break;
  202. case DRM_MODE_CONNECTOR_Composite:
  203. case DRM_MODE_CONNECTOR_SVIDEO:
  204. case DRM_MODE_CONNECTOR_Component:
  205. prop = dev->mode_config.tv_subconnector_property;
  206. is_tv = 1;
  207. break;
  208. default:
  209. DRM_ERROR("Wrong connector type for this property\n");
  210. return 0;
  211. }
  212. if (!prop) {
  213. DRM_ERROR("Unable to find subconnector property\n");
  214. return 0;
  215. }
  216. ret = drm_connector_property_get_value(connector, prop, &subconnector);
  217. if (ret)
  218. return 0;
  219. return snprintf(buf, PAGE_SIZE, "%s", is_tv ?
  220. drm_get_tv_subconnector_name((int)subconnector) :
  221. drm_get_dvi_i_subconnector_name((int)subconnector));
  222. }
  223. static ssize_t select_subconnector_show(struct device *device,
  224. struct device_attribute *attr,
  225. char *buf)
  226. {
  227. struct drm_connector *connector = to_drm_connector(device);
  228. struct drm_device *dev = connector->dev;
  229. struct drm_property *prop = NULL;
  230. uint64_t subconnector;
  231. int is_tv = 0;
  232. int ret;
  233. switch (connector->connector_type) {
  234. case DRM_MODE_CONNECTOR_DVII:
  235. prop = dev->mode_config.dvi_i_select_subconnector_property;
  236. break;
  237. case DRM_MODE_CONNECTOR_Composite:
  238. case DRM_MODE_CONNECTOR_SVIDEO:
  239. case DRM_MODE_CONNECTOR_Component:
  240. prop = dev->mode_config.tv_select_subconnector_property;
  241. is_tv = 1;
  242. break;
  243. default:
  244. DRM_ERROR("Wrong connector type for this property\n");
  245. return 0;
  246. }
  247. if (!prop) {
  248. DRM_ERROR("Unable to find select subconnector property\n");
  249. return 0;
  250. }
  251. ret = drm_connector_property_get_value(connector, prop, &subconnector);
  252. if (ret)
  253. return 0;
  254. return snprintf(buf, PAGE_SIZE, "%s", is_tv ?
  255. drm_get_tv_select_name((int)subconnector) :
  256. drm_get_dvi_i_select_name((int)subconnector));
  257. }
  258. static struct device_attribute connector_attrs[] = {
  259. __ATTR_RO(status),
  260. __ATTR_RO(enabled),
  261. __ATTR_RO(dpms),
  262. __ATTR_RO(modes),
  263. };
  264. /* These attributes are for both DVI-I connectors and all types of tv-out. */
  265. static struct device_attribute connector_attrs_opt1[] = {
  266. __ATTR_RO(subconnector),
  267. __ATTR_RO(select_subconnector),
  268. };
  269. static struct bin_attribute edid_attr = {
  270. .attr.name = "edid",
  271. .size = 128,
  272. .read = edid_show,
  273. };
  274. /**
  275. * drm_sysfs_connector_add - add an connector to sysfs
  276. * @connector: connector to add
  277. *
  278. * Create an connector device in sysfs, along with its associated connector
  279. * properties (so far, connection status, dpms, mode list & edid) and
  280. * generate a hotplug event so userspace knows there's a new connector
  281. * available.
  282. *
  283. * Note:
  284. * This routine should only be called *once* for each DRM minor registered.
  285. * A second call for an already registered device will trigger the BUG_ON
  286. * below.
  287. */
  288. int drm_sysfs_connector_add(struct drm_connector *connector)
  289. {
  290. struct drm_device *dev = connector->dev;
  291. int ret = 0, i, j;
  292. /* We shouldn't get called more than once for the same connector */
  293. BUG_ON(device_is_registered(&connector->kdev));
  294. connector->kdev.parent = &dev->primary->kdev;
  295. connector->kdev.class = drm_class;
  296. connector->kdev.release = drm_sysfs_device_release;
  297. DRM_DEBUG("adding \"%s\" to sysfs\n",
  298. drm_get_connector_name(connector));
  299. dev_set_name(&connector->kdev, "card%d-%s",
  300. dev->primary->index, drm_get_connector_name(connector));
  301. ret = device_register(&connector->kdev);
  302. if (ret) {
  303. DRM_ERROR("failed to register connector device: %d\n", ret);
  304. goto out;
  305. }
  306. /* Standard attributes */
  307. for (i = 0; i < ARRAY_SIZE(connector_attrs); i++) {
  308. ret = device_create_file(&connector->kdev, &connector_attrs[i]);
  309. if (ret)
  310. goto err_out_files;
  311. }
  312. /* Optional attributes */
  313. /*
  314. * In the long run it maybe a good idea to make one set of
  315. * optionals per connector type.
  316. */
  317. switch (connector->connector_type) {
  318. case DRM_MODE_CONNECTOR_DVII:
  319. case DRM_MODE_CONNECTOR_Composite:
  320. case DRM_MODE_CONNECTOR_SVIDEO:
  321. case DRM_MODE_CONNECTOR_Component:
  322. for (i = 0; i < ARRAY_SIZE(connector_attrs_opt1); i++) {
  323. ret = device_create_file(&connector->kdev, &connector_attrs_opt1[i]);
  324. if (ret)
  325. goto err_out_files;
  326. }
  327. break;
  328. default:
  329. break;
  330. }
  331. ret = sysfs_create_bin_file(&connector->kdev.kobj, &edid_attr);
  332. if (ret)
  333. goto err_out_files;
  334. /* Let userspace know we have a new connector */
  335. drm_sysfs_hotplug_event(dev);
  336. return 0;
  337. err_out_files:
  338. if (i > 0)
  339. for (j = 0; j < i; j++)
  340. device_remove_file(&connector->kdev,
  341. &connector_attrs[i]);
  342. device_unregister(&connector->kdev);
  343. out:
  344. return ret;
  345. }
  346. EXPORT_SYMBOL(drm_sysfs_connector_add);
  347. /**
  348. * drm_sysfs_connector_remove - remove an connector device from sysfs
  349. * @connector: connector to remove
  350. *
  351. * Remove @connector and its associated attributes from sysfs. Note that
  352. * the device model core will take care of sending the "remove" uevent
  353. * at this time, so we don't need to do it.
  354. *
  355. * Note:
  356. * This routine should only be called if the connector was previously
  357. * successfully registered. If @connector hasn't been registered yet,
  358. * you'll likely see a panic somewhere deep in sysfs code when called.
  359. */
  360. void drm_sysfs_connector_remove(struct drm_connector *connector)
  361. {
  362. int i;
  363. DRM_DEBUG("removing \"%s\" from sysfs\n",
  364. drm_get_connector_name(connector));
  365. for (i = 0; i < ARRAY_SIZE(connector_attrs); i++)
  366. device_remove_file(&connector->kdev, &connector_attrs[i]);
  367. sysfs_remove_bin_file(&connector->kdev.kobj, &edid_attr);
  368. device_unregister(&connector->kdev);
  369. }
  370. EXPORT_SYMBOL(drm_sysfs_connector_remove);
  371. /**
  372. * drm_sysfs_hotplug_event - generate a DRM uevent
  373. * @dev: DRM device
  374. *
  375. * Send a uevent for the DRM device specified by @dev. Currently we only
  376. * set HOTPLUG=1 in the uevent environment, but this could be expanded to
  377. * deal with other types of events.
  378. */
  379. void drm_sysfs_hotplug_event(struct drm_device *dev)
  380. {
  381. char *event_string = "HOTPLUG=1";
  382. char *envp[] = { event_string, NULL };
  383. DRM_DEBUG("generating hotplug event\n");
  384. kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, envp);
  385. }
  386. EXPORT_SYMBOL(drm_sysfs_hotplug_event);
  387. /**
  388. * drm_sysfs_device_add - adds a class device to sysfs for a character driver
  389. * @dev: DRM device to be added
  390. * @head: DRM head in question
  391. *
  392. * Add a DRM device to the DRM's device model class. We use @dev's PCI device
  393. * as the parent for the Linux device, and make sure it has a file containing
  394. * the driver we're using (for userspace compatibility).
  395. */
  396. int drm_sysfs_device_add(struct drm_minor *minor)
  397. {
  398. int err;
  399. char *minor_str;
  400. minor->kdev.parent = &minor->dev->pdev->dev;
  401. minor->kdev.class = drm_class;
  402. minor->kdev.release = drm_sysfs_device_release;
  403. minor->kdev.devt = minor->device;
  404. if (minor->type == DRM_MINOR_CONTROL)
  405. minor_str = "controlD%d";
  406. else if (minor->type == DRM_MINOR_RENDER)
  407. minor_str = "renderD%d";
  408. else
  409. minor_str = "card%d";
  410. dev_set_name(&minor->kdev, minor_str, minor->index);
  411. err = device_register(&minor->kdev);
  412. if (err) {
  413. DRM_ERROR("device add failed: %d\n", err);
  414. goto err_out;
  415. }
  416. return 0;
  417. err_out:
  418. return err;
  419. }
  420. /**
  421. * drm_sysfs_device_remove - remove DRM device
  422. * @dev: DRM device to remove
  423. *
  424. * This call unregisters and cleans up a class device that was created with a
  425. * call to drm_sysfs_device_add()
  426. */
  427. void drm_sysfs_device_remove(struct drm_minor *minor)
  428. {
  429. device_unregister(&minor->kdev);
  430. }