drm_sysfs.c 13 KB

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