drm_sysfs.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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. return;
  116. }
  117. /*
  118. * Connector properties
  119. */
  120. static ssize_t status_show(struct device *device,
  121. struct device_attribute *attr,
  122. char *buf)
  123. {
  124. struct drm_connector *connector = to_drm_connector(device);
  125. enum drm_connector_status status;
  126. status = connector->funcs->detect(connector);
  127. return snprintf(buf, PAGE_SIZE, "%s",
  128. drm_get_connector_status_name(status));
  129. }
  130. static ssize_t dpms_show(struct device *device,
  131. struct device_attribute *attr,
  132. char *buf)
  133. {
  134. struct drm_connector *connector = to_drm_connector(device);
  135. struct drm_device *dev = connector->dev;
  136. uint64_t dpms_status;
  137. int ret;
  138. ret = drm_connector_property_get_value(connector,
  139. dev->mode_config.dpms_property,
  140. &dpms_status);
  141. if (ret)
  142. return 0;
  143. return snprintf(buf, PAGE_SIZE, "%s",
  144. drm_get_dpms_name((int)dpms_status));
  145. }
  146. static ssize_t enabled_show(struct device *device,
  147. struct device_attribute *attr,
  148. char *buf)
  149. {
  150. struct drm_connector *connector = to_drm_connector(device);
  151. return snprintf(buf, PAGE_SIZE, connector->encoder ? "enabled" :
  152. "disabled");
  153. }
  154. static ssize_t edid_show(struct kobject *kobj, struct bin_attribute *attr,
  155. char *buf, loff_t off, size_t count)
  156. {
  157. struct device *connector_dev = container_of(kobj, struct device, kobj);
  158. struct drm_connector *connector = to_drm_connector(connector_dev);
  159. unsigned char *edid;
  160. size_t size;
  161. if (!connector->edid_blob_ptr)
  162. return 0;
  163. edid = connector->edid_blob_ptr->data;
  164. size = connector->edid_blob_ptr->length;
  165. if (!edid)
  166. return 0;
  167. if (off >= size)
  168. return 0;
  169. if (off + count > size)
  170. count = size - off;
  171. memcpy(buf, edid + off, count);
  172. return count;
  173. }
  174. static ssize_t modes_show(struct device *device,
  175. struct device_attribute *attr,
  176. char *buf)
  177. {
  178. struct drm_connector *connector = to_drm_connector(device);
  179. struct drm_display_mode *mode;
  180. int written = 0;
  181. list_for_each_entry(mode, &connector->modes, head) {
  182. written += snprintf(buf + written, PAGE_SIZE - written, "%s\n",
  183. mode->name);
  184. }
  185. return written;
  186. }
  187. static ssize_t subconnector_show(struct device *device,
  188. struct device_attribute *attr,
  189. char *buf)
  190. {
  191. struct drm_connector *connector = to_drm_connector(device);
  192. struct drm_device *dev = connector->dev;
  193. struct drm_property *prop = NULL;
  194. uint64_t subconnector;
  195. int is_tv = 0;
  196. int ret;
  197. switch (connector->connector_type) {
  198. case DRM_MODE_CONNECTOR_DVII:
  199. prop = dev->mode_config.dvi_i_subconnector_property;
  200. break;
  201. case DRM_MODE_CONNECTOR_Composite:
  202. case DRM_MODE_CONNECTOR_SVIDEO:
  203. case DRM_MODE_CONNECTOR_Component:
  204. prop = dev->mode_config.tv_subconnector_property;
  205. is_tv = 1;
  206. break;
  207. default:
  208. DRM_ERROR("Wrong connector type for this property\n");
  209. return 0;
  210. }
  211. if (!prop) {
  212. DRM_ERROR("Unable to find subconnector property\n");
  213. return 0;
  214. }
  215. ret = drm_connector_property_get_value(connector, prop, &subconnector);
  216. if (ret)
  217. return 0;
  218. return snprintf(buf, PAGE_SIZE, "%s", is_tv ?
  219. drm_get_tv_subconnector_name((int)subconnector) :
  220. drm_get_dvi_i_subconnector_name((int)subconnector));
  221. }
  222. static ssize_t select_subconnector_show(struct device *device,
  223. struct device_attribute *attr,
  224. char *buf)
  225. {
  226. struct drm_connector *connector = to_drm_connector(device);
  227. struct drm_device *dev = connector->dev;
  228. struct drm_property *prop = NULL;
  229. uint64_t subconnector;
  230. int is_tv = 0;
  231. int ret;
  232. switch (connector->connector_type) {
  233. case DRM_MODE_CONNECTOR_DVII:
  234. prop = dev->mode_config.dvi_i_select_subconnector_property;
  235. break;
  236. case DRM_MODE_CONNECTOR_Composite:
  237. case DRM_MODE_CONNECTOR_SVIDEO:
  238. case DRM_MODE_CONNECTOR_Component:
  239. prop = dev->mode_config.tv_select_subconnector_property;
  240. is_tv = 1;
  241. break;
  242. default:
  243. DRM_ERROR("Wrong connector type for this property\n");
  244. return 0;
  245. }
  246. if (!prop) {
  247. DRM_ERROR("Unable to find select subconnector property\n");
  248. return 0;
  249. }
  250. ret = drm_connector_property_get_value(connector, prop, &subconnector);
  251. if (ret)
  252. return 0;
  253. return snprintf(buf, PAGE_SIZE, "%s", is_tv ?
  254. drm_get_tv_select_name((int)subconnector) :
  255. drm_get_dvi_i_select_name((int)subconnector));
  256. }
  257. static struct device_attribute connector_attrs[] = {
  258. __ATTR_RO(status),
  259. __ATTR_RO(enabled),
  260. __ATTR_RO(dpms),
  261. __ATTR_RO(modes),
  262. };
  263. /* These attributes are for both DVI-I connectors and all types of tv-out. */
  264. static struct device_attribute connector_attrs_opt1[] = {
  265. __ATTR_RO(subconnector),
  266. __ATTR_RO(select_subconnector),
  267. };
  268. static struct bin_attribute edid_attr = {
  269. .attr.name = "edid",
  270. .size = 128,
  271. .read = edid_show,
  272. };
  273. /**
  274. * drm_sysfs_connector_add - add an connector to sysfs
  275. * @connector: connector to add
  276. *
  277. * Create an connector device in sysfs, along with its associated connector
  278. * properties (so far, connection status, dpms, mode list & edid) and
  279. * generate a hotplug event so userspace knows there's a new connector
  280. * available.
  281. *
  282. * Note:
  283. * This routine should only be called *once* for each DRM minor registered.
  284. * A second call for an already registered device will trigger the BUG_ON
  285. * below.
  286. */
  287. int drm_sysfs_connector_add(struct drm_connector *connector)
  288. {
  289. struct drm_device *dev = connector->dev;
  290. int ret = 0, i, j;
  291. /* We shouldn't get called more than once for the same connector */
  292. BUG_ON(device_is_registered(&connector->kdev));
  293. connector->kdev.parent = &dev->primary->kdev;
  294. connector->kdev.class = drm_class;
  295. connector->kdev.release = drm_sysfs_device_release;
  296. DRM_DEBUG("adding \"%s\" to sysfs\n",
  297. drm_get_connector_name(connector));
  298. dev_set_name(&connector->kdev, "card%d-%s",
  299. dev->primary->index, drm_get_connector_name(connector));
  300. ret = device_register(&connector->kdev);
  301. if (ret) {
  302. DRM_ERROR("failed to register connector device: %d\n", ret);
  303. goto out;
  304. }
  305. /* Standard attributes */
  306. for (i = 0; i < ARRAY_SIZE(connector_attrs); i++) {
  307. ret = device_create_file(&connector->kdev, &connector_attrs[i]);
  308. if (ret)
  309. goto err_out_files;
  310. }
  311. /* Optional attributes */
  312. /*
  313. * In the long run it maybe a good idea to make one set of
  314. * optionals per connector type.
  315. */
  316. switch (connector->connector_type) {
  317. case DRM_MODE_CONNECTOR_DVII:
  318. case DRM_MODE_CONNECTOR_Composite:
  319. case DRM_MODE_CONNECTOR_SVIDEO:
  320. case DRM_MODE_CONNECTOR_Component:
  321. for (i = 0; i < ARRAY_SIZE(connector_attrs_opt1); i++) {
  322. ret = device_create_file(&connector->kdev, &connector_attrs_opt1[i]);
  323. if (ret)
  324. goto err_out_files;
  325. }
  326. break;
  327. default:
  328. break;
  329. }
  330. ret = sysfs_create_bin_file(&connector->kdev.kobj, &edid_attr);
  331. if (ret)
  332. goto err_out_files;
  333. /* Let userspace know we have a new connector */
  334. drm_sysfs_hotplug_event(dev);
  335. return 0;
  336. err_out_files:
  337. if (i > 0)
  338. for (j = 0; j < i; j++)
  339. device_remove_file(&connector->kdev,
  340. &connector_attrs[i]);
  341. device_unregister(&connector->kdev);
  342. out:
  343. return ret;
  344. }
  345. EXPORT_SYMBOL(drm_sysfs_connector_add);
  346. /**
  347. * drm_sysfs_connector_remove - remove an connector device from sysfs
  348. * @connector: connector to remove
  349. *
  350. * Remove @connector and its associated attributes from sysfs. Note that
  351. * the device model core will take care of sending the "remove" uevent
  352. * at this time, so we don't need to do it.
  353. *
  354. * Note:
  355. * This routine should only be called if the connector was previously
  356. * successfully registered. If @connector hasn't been registered yet,
  357. * you'll likely see a panic somewhere deep in sysfs code when called.
  358. */
  359. void drm_sysfs_connector_remove(struct drm_connector *connector)
  360. {
  361. int i;
  362. DRM_DEBUG("removing \"%s\" from sysfs\n",
  363. drm_get_connector_name(connector));
  364. for (i = 0; i < ARRAY_SIZE(connector_attrs); i++)
  365. device_remove_file(&connector->kdev, &connector_attrs[i]);
  366. sysfs_remove_bin_file(&connector->kdev.kobj, &edid_attr);
  367. device_unregister(&connector->kdev);
  368. }
  369. EXPORT_SYMBOL(drm_sysfs_connector_remove);
  370. /**
  371. * drm_sysfs_hotplug_event - generate a DRM uevent
  372. * @dev: DRM device
  373. *
  374. * Send a uevent for the DRM device specified by @dev. Currently we only
  375. * set HOTPLUG=1 in the uevent environment, but this could be expanded to
  376. * deal with other types of events.
  377. */
  378. void drm_sysfs_hotplug_event(struct drm_device *dev)
  379. {
  380. char *event_string = "HOTPLUG=1";
  381. char *envp[] = { event_string, NULL };
  382. DRM_DEBUG("generating hotplug event\n");
  383. kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, envp);
  384. }
  385. EXPORT_SYMBOL(drm_sysfs_hotplug_event);
  386. /**
  387. * drm_sysfs_device_add - adds a class device to sysfs for a character driver
  388. * @dev: DRM device to be added
  389. * @head: DRM head in question
  390. *
  391. * Add a DRM device to the DRM's device model class. We use @dev's PCI device
  392. * as the parent for the Linux device, and make sure it has a file containing
  393. * the driver we're using (for userspace compatibility).
  394. */
  395. int drm_sysfs_device_add(struct drm_minor *minor)
  396. {
  397. int err;
  398. char *minor_str;
  399. minor->kdev.parent = &minor->dev->pdev->dev;
  400. minor->kdev.class = drm_class;
  401. minor->kdev.release = drm_sysfs_device_release;
  402. minor->kdev.devt = minor->device;
  403. if (minor->type == DRM_MINOR_CONTROL)
  404. minor_str = "controlD%d";
  405. else if (minor->type == DRM_MINOR_RENDER)
  406. minor_str = "renderD%d";
  407. else
  408. minor_str = "card%d";
  409. dev_set_name(&minor->kdev, minor_str, minor->index);
  410. err = device_register(&minor->kdev);
  411. if (err) {
  412. DRM_ERROR("device add failed: %d\n", err);
  413. goto err_out;
  414. }
  415. return 0;
  416. device_unregister(&minor->kdev);
  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. }