drm_sysfs.c 13 KB

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