drm_sysfs.c 14 KB

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