drm_sysfs.c 15 KB

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