drm_sysfs.c 13 KB

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