drm_sysfs.c 15 KB

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