drm_sysfs.c 14 KB

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