drm_sysfs.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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. static char *drm_devnode(struct device *dev, mode_t *mode)
  64. {
  65. return kasprintf(GFP_KERNEL, "dri/%s", dev_name(dev));
  66. }
  67. static CLASS_ATTR_STRING(version, S_IRUGO,
  68. CORE_NAME " "
  69. __stringify(CORE_MAJOR) "."
  70. __stringify(CORE_MINOR) "."
  71. __stringify(CORE_PATCHLEVEL) " "
  72. CORE_DATE);
  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.attr);
  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.attr);
  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. case DRM_MODE_CONNECTOR_TV:
  218. prop = dev->mode_config.tv_subconnector_property;
  219. is_tv = 1;
  220. break;
  221. default:
  222. DRM_ERROR("Wrong connector type for this property\n");
  223. return 0;
  224. }
  225. if (!prop) {
  226. DRM_ERROR("Unable to find subconnector property\n");
  227. return 0;
  228. }
  229. ret = drm_connector_property_get_value(connector, prop, &subconnector);
  230. if (ret)
  231. return 0;
  232. return snprintf(buf, PAGE_SIZE, "%s", is_tv ?
  233. drm_get_tv_subconnector_name((int)subconnector) :
  234. drm_get_dvi_i_subconnector_name((int)subconnector));
  235. }
  236. static ssize_t select_subconnector_show(struct device *device,
  237. struct device_attribute *attr,
  238. char *buf)
  239. {
  240. struct drm_connector *connector = to_drm_connector(device);
  241. struct drm_device *dev = connector->dev;
  242. struct drm_property *prop = NULL;
  243. uint64_t subconnector;
  244. int is_tv = 0;
  245. int ret;
  246. switch (connector->connector_type) {
  247. case DRM_MODE_CONNECTOR_DVII:
  248. prop = dev->mode_config.dvi_i_select_subconnector_property;
  249. break;
  250. case DRM_MODE_CONNECTOR_Composite:
  251. case DRM_MODE_CONNECTOR_SVIDEO:
  252. case DRM_MODE_CONNECTOR_Component:
  253. case DRM_MODE_CONNECTOR_TV:
  254. prop = dev->mode_config.tv_select_subconnector_property;
  255. is_tv = 1;
  256. break;
  257. default:
  258. DRM_ERROR("Wrong connector type for this property\n");
  259. return 0;
  260. }
  261. if (!prop) {
  262. DRM_ERROR("Unable to find select subconnector property\n");
  263. return 0;
  264. }
  265. ret = drm_connector_property_get_value(connector, prop, &subconnector);
  266. if (ret)
  267. return 0;
  268. return snprintf(buf, PAGE_SIZE, "%s", is_tv ?
  269. drm_get_tv_select_name((int)subconnector) :
  270. drm_get_dvi_i_select_name((int)subconnector));
  271. }
  272. static struct device_attribute connector_attrs[] = {
  273. __ATTR_RO(status),
  274. __ATTR_RO(enabled),
  275. __ATTR_RO(dpms),
  276. __ATTR_RO(modes),
  277. };
  278. /* These attributes are for both DVI-I connectors and all types of tv-out. */
  279. static struct device_attribute connector_attrs_opt1[] = {
  280. __ATTR_RO(subconnector),
  281. __ATTR_RO(select_subconnector),
  282. };
  283. static struct bin_attribute edid_attr = {
  284. .attr.name = "edid",
  285. .attr.mode = 0444,
  286. .size = 128,
  287. .read = edid_show,
  288. };
  289. /**
  290. * drm_sysfs_connector_add - add an connector to sysfs
  291. * @connector: connector to add
  292. *
  293. * Create an connector device in sysfs, along with its associated connector
  294. * properties (so far, connection status, dpms, mode list & edid) and
  295. * generate a hotplug event so userspace knows there's a new connector
  296. * available.
  297. *
  298. * Note:
  299. * This routine should only be called *once* for each DRM minor registered.
  300. * A second call for an already registered device will trigger the BUG_ON
  301. * below.
  302. */
  303. int drm_sysfs_connector_add(struct drm_connector *connector)
  304. {
  305. struct drm_device *dev = connector->dev;
  306. int ret = 0, i, j;
  307. /* We shouldn't get called more than once for the same connector */
  308. BUG_ON(device_is_registered(&connector->kdev));
  309. connector->kdev.parent = &dev->primary->kdev;
  310. connector->kdev.class = drm_class;
  311. connector->kdev.release = drm_sysfs_device_release;
  312. DRM_DEBUG("adding \"%s\" to sysfs\n",
  313. drm_get_connector_name(connector));
  314. dev_set_name(&connector->kdev, "card%d-%s",
  315. dev->primary->index, drm_get_connector_name(connector));
  316. ret = device_register(&connector->kdev);
  317. if (ret) {
  318. DRM_ERROR("failed to register connector device: %d\n", ret);
  319. goto out;
  320. }
  321. /* Standard attributes */
  322. for (i = 0; i < ARRAY_SIZE(connector_attrs); i++) {
  323. ret = device_create_file(&connector->kdev, &connector_attrs[i]);
  324. if (ret)
  325. goto err_out_files;
  326. }
  327. /* Optional attributes */
  328. /*
  329. * In the long run it maybe a good idea to make one set of
  330. * optionals per connector type.
  331. */
  332. switch (connector->connector_type) {
  333. case DRM_MODE_CONNECTOR_DVII:
  334. case DRM_MODE_CONNECTOR_Composite:
  335. case DRM_MODE_CONNECTOR_SVIDEO:
  336. case DRM_MODE_CONNECTOR_Component:
  337. case DRM_MODE_CONNECTOR_TV:
  338. for (i = 0; i < ARRAY_SIZE(connector_attrs_opt1); i++) {
  339. ret = device_create_file(&connector->kdev, &connector_attrs_opt1[i]);
  340. if (ret)
  341. goto err_out_files;
  342. }
  343. break;
  344. default:
  345. break;
  346. }
  347. ret = sysfs_create_bin_file(&connector->kdev.kobj, &edid_attr);
  348. if (ret)
  349. goto err_out_files;
  350. /* Let userspace know we have a new connector */
  351. drm_sysfs_hotplug_event(dev);
  352. return 0;
  353. err_out_files:
  354. if (i > 0)
  355. for (j = 0; j < i; j++)
  356. device_remove_file(&connector->kdev,
  357. &connector_attrs[i]);
  358. device_unregister(&connector->kdev);
  359. out:
  360. return ret;
  361. }
  362. EXPORT_SYMBOL(drm_sysfs_connector_add);
  363. /**
  364. * drm_sysfs_connector_remove - remove an connector device from sysfs
  365. * @connector: connector to remove
  366. *
  367. * Remove @connector and its associated attributes from sysfs. Note that
  368. * the device model core will take care of sending the "remove" uevent
  369. * at this time, so we don't need to do it.
  370. *
  371. * Note:
  372. * This routine should only be called if the connector was previously
  373. * successfully registered. If @connector hasn't been registered yet,
  374. * you'll likely see a panic somewhere deep in sysfs code when called.
  375. */
  376. void drm_sysfs_connector_remove(struct drm_connector *connector)
  377. {
  378. int i;
  379. DRM_DEBUG("removing \"%s\" from sysfs\n",
  380. drm_get_connector_name(connector));
  381. for (i = 0; i < ARRAY_SIZE(connector_attrs); i++)
  382. device_remove_file(&connector->kdev, &connector_attrs[i]);
  383. sysfs_remove_bin_file(&connector->kdev.kobj, &edid_attr);
  384. device_unregister(&connector->kdev);
  385. }
  386. EXPORT_SYMBOL(drm_sysfs_connector_remove);
  387. /**
  388. * drm_sysfs_hotplug_event - generate a DRM uevent
  389. * @dev: DRM device
  390. *
  391. * Send a uevent for the DRM device specified by @dev. Currently we only
  392. * set HOTPLUG=1 in the uevent environment, but this could be expanded to
  393. * deal with other types of events.
  394. */
  395. void drm_sysfs_hotplug_event(struct drm_device *dev)
  396. {
  397. char *event_string = "HOTPLUG=1";
  398. char *envp[] = { event_string, NULL };
  399. DRM_DEBUG("generating hotplug event\n");
  400. kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, envp);
  401. }
  402. EXPORT_SYMBOL(drm_sysfs_hotplug_event);
  403. /**
  404. * drm_sysfs_device_add - adds a class device to sysfs for a character driver
  405. * @dev: DRM device to be added
  406. * @head: DRM head in question
  407. *
  408. * Add a DRM device to the DRM's device model class. We use @dev's PCI device
  409. * as the parent for the Linux device, and make sure it has a file containing
  410. * the driver we're using (for userspace compatibility).
  411. */
  412. int drm_sysfs_device_add(struct drm_minor *minor)
  413. {
  414. int err;
  415. char *minor_str;
  416. minor->kdev.parent = &minor->dev->pdev->dev;
  417. minor->kdev.class = drm_class;
  418. minor->kdev.release = drm_sysfs_device_release;
  419. minor->kdev.devt = minor->device;
  420. minor->kdev.type = &drm_sysfs_device_minor;
  421. if (minor->type == DRM_MINOR_CONTROL)
  422. minor_str = "controlD%d";
  423. else if (minor->type == DRM_MINOR_RENDER)
  424. minor_str = "renderD%d";
  425. else
  426. minor_str = "card%d";
  427. dev_set_name(&minor->kdev, minor_str, minor->index);
  428. err = device_register(&minor->kdev);
  429. if (err) {
  430. DRM_ERROR("device add failed: %d\n", err);
  431. goto err_out;
  432. }
  433. return 0;
  434. err_out:
  435. return err;
  436. }
  437. /**
  438. * drm_sysfs_device_remove - remove DRM device
  439. * @dev: DRM device to remove
  440. *
  441. * This call unregisters and cleans up a class device that was created with a
  442. * call to drm_sysfs_device_add()
  443. */
  444. void drm_sysfs_device_remove(struct drm_minor *minor)
  445. {
  446. device_unregister(&minor->kdev);
  447. }
  448. /**
  449. * drm_class_device_register - Register a struct device in the drm class.
  450. *
  451. * @dev: pointer to struct device to register.
  452. *
  453. * @dev should have all relevant members pre-filled with the exception
  454. * of the class member. In particular, the device_type member must
  455. * be set.
  456. */
  457. int drm_class_device_register(struct device *dev)
  458. {
  459. dev->class = drm_class;
  460. return device_register(dev);
  461. }
  462. EXPORT_SYMBOL_GPL(drm_class_device_register);
  463. void drm_class_device_unregister(struct device *dev)
  464. {
  465. return device_unregister(dev);
  466. }
  467. EXPORT_SYMBOL_GPL(drm_class_device_unregister);