drm_sysfs.c 14 KB

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