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. 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\n",
  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\n",
  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, "%s\n", 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. .attr.mode = 0444,
  272. .size = 128,
  273. .read = edid_show,
  274. };
  275. /**
  276. * drm_sysfs_connector_add - add an connector to sysfs
  277. * @connector: connector to add
  278. *
  279. * Create an connector device in sysfs, along with its associated connector
  280. * properties (so far, connection status, dpms, mode list & edid) and
  281. * generate a hotplug event so userspace knows there's a new connector
  282. * available.
  283. *
  284. * Note:
  285. * This routine should only be called *once* for each DRM minor registered.
  286. * A second call for an already registered device will trigger the BUG_ON
  287. * below.
  288. */
  289. int drm_sysfs_connector_add(struct drm_connector *connector)
  290. {
  291. struct drm_device *dev = connector->dev;
  292. int ret = 0, i, j;
  293. /* We shouldn't get called more than once for the same connector */
  294. BUG_ON(device_is_registered(&connector->kdev));
  295. connector->kdev.parent = &dev->primary->kdev;
  296. connector->kdev.class = drm_class;
  297. connector->kdev.release = drm_sysfs_device_release;
  298. DRM_DEBUG("adding \"%s\" to sysfs\n",
  299. drm_get_connector_name(connector));
  300. dev_set_name(&connector->kdev, "card%d-%s",
  301. dev->primary->index, drm_get_connector_name(connector));
  302. ret = device_register(&connector->kdev);
  303. if (ret) {
  304. DRM_ERROR("failed to register connector device: %d\n", ret);
  305. goto out;
  306. }
  307. /* Standard attributes */
  308. for (i = 0; i < ARRAY_SIZE(connector_attrs); i++) {
  309. ret = device_create_file(&connector->kdev, &connector_attrs[i]);
  310. if (ret)
  311. goto err_out_files;
  312. }
  313. /* Optional attributes */
  314. /*
  315. * In the long run it maybe a good idea to make one set of
  316. * optionals per connector type.
  317. */
  318. switch (connector->connector_type) {
  319. case DRM_MODE_CONNECTOR_DVII:
  320. case DRM_MODE_CONNECTOR_Composite:
  321. case DRM_MODE_CONNECTOR_SVIDEO:
  322. case DRM_MODE_CONNECTOR_Component:
  323. for (i = 0; i < ARRAY_SIZE(connector_attrs_opt1); i++) {
  324. ret = device_create_file(&connector->kdev, &connector_attrs_opt1[i]);
  325. if (ret)
  326. goto err_out_files;
  327. }
  328. break;
  329. default:
  330. break;
  331. }
  332. ret = sysfs_create_bin_file(&connector->kdev.kobj, &edid_attr);
  333. if (ret)
  334. goto err_out_files;
  335. /* Let userspace know we have a new connector */
  336. drm_sysfs_hotplug_event(dev);
  337. return 0;
  338. err_out_files:
  339. if (i > 0)
  340. for (j = 0; j < i; j++)
  341. device_remove_file(&connector->kdev,
  342. &connector_attrs[i]);
  343. device_unregister(&connector->kdev);
  344. out:
  345. return ret;
  346. }
  347. EXPORT_SYMBOL(drm_sysfs_connector_add);
  348. /**
  349. * drm_sysfs_connector_remove - remove an connector device from sysfs
  350. * @connector: connector to remove
  351. *
  352. * Remove @connector and its associated attributes from sysfs. Note that
  353. * the device model core will take care of sending the "remove" uevent
  354. * at this time, so we don't need to do it.
  355. *
  356. * Note:
  357. * This routine should only be called if the connector was previously
  358. * successfully registered. If @connector hasn't been registered yet,
  359. * you'll likely see a panic somewhere deep in sysfs code when called.
  360. */
  361. void drm_sysfs_connector_remove(struct drm_connector *connector)
  362. {
  363. int i;
  364. DRM_DEBUG("removing \"%s\" from sysfs\n",
  365. drm_get_connector_name(connector));
  366. for (i = 0; i < ARRAY_SIZE(connector_attrs); i++)
  367. device_remove_file(&connector->kdev, &connector_attrs[i]);
  368. sysfs_remove_bin_file(&connector->kdev.kobj, &edid_attr);
  369. device_unregister(&connector->kdev);
  370. }
  371. EXPORT_SYMBOL(drm_sysfs_connector_remove);
  372. /**
  373. * drm_sysfs_hotplug_event - generate a DRM uevent
  374. * @dev: DRM device
  375. *
  376. * Send a uevent for the DRM device specified by @dev. Currently we only
  377. * set HOTPLUG=1 in the uevent environment, but this could be expanded to
  378. * deal with other types of events.
  379. */
  380. void drm_sysfs_hotplug_event(struct drm_device *dev)
  381. {
  382. char *event_string = "HOTPLUG=1";
  383. char *envp[] = { event_string, NULL };
  384. DRM_DEBUG("generating hotplug event\n");
  385. kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, envp);
  386. }
  387. EXPORT_SYMBOL(drm_sysfs_hotplug_event);
  388. /**
  389. * drm_sysfs_device_add - adds a class device to sysfs for a character driver
  390. * @dev: DRM device to be added
  391. * @head: DRM head in question
  392. *
  393. * Add a DRM device to the DRM's device model class. We use @dev's PCI device
  394. * as the parent for the Linux device, and make sure it has a file containing
  395. * the driver we're using (for userspace compatibility).
  396. */
  397. int drm_sysfs_device_add(struct drm_minor *minor)
  398. {
  399. int err;
  400. char *minor_str;
  401. minor->kdev.parent = &minor->dev->pdev->dev;
  402. minor->kdev.class = drm_class;
  403. minor->kdev.release = drm_sysfs_device_release;
  404. minor->kdev.devt = minor->device;
  405. if (minor->type == DRM_MINOR_CONTROL)
  406. minor_str = "controlD%d";
  407. else if (minor->type == DRM_MINOR_RENDER)
  408. minor_str = "renderD%d";
  409. else
  410. minor_str = "card%d";
  411. dev_set_name(&minor->kdev, minor_str, minor->index);
  412. err = device_register(&minor->kdev);
  413. if (err) {
  414. DRM_ERROR("device add failed: %d\n", err);
  415. goto err_out;
  416. }
  417. return 0;
  418. err_out:
  419. return err;
  420. }
  421. /**
  422. * drm_sysfs_device_remove - remove DRM device
  423. * @dev: DRM device to remove
  424. *
  425. * This call unregisters and cleans up a class device that was created with a
  426. * call to drm_sysfs_device_add()
  427. */
  428. void drm_sysfs_device_remove(struct drm_minor *minor)
  429. {
  430. device_unregister(&minor->kdev);
  431. }