drm_sysfs.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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 "drm_sysfs.h"
  18. #include "drm_core.h"
  19. #include "drmP.h"
  20. #define to_drm_minor(d) container_of(d, struct drm_minor, kdev)
  21. #define to_drm_connector(d) container_of(d, struct drm_connector, kdev)
  22. static struct device_type drm_sysfs_device_minor = {
  23. .name = "drm_minor"
  24. };
  25. /**
  26. * drm_class_suspend - DRM class suspend hook
  27. * @dev: Linux device to suspend
  28. * @state: power state to enter
  29. *
  30. * Just figures out what the actual struct drm_device associated with
  31. * @dev is and calls its suspend hook, if present.
  32. */
  33. static int drm_class_suspend(struct device *dev, pm_message_t state)
  34. {
  35. if (dev->type == &drm_sysfs_device_minor) {
  36. struct drm_minor *drm_minor = to_drm_minor(dev);
  37. struct drm_device *drm_dev = drm_minor->dev;
  38. if (drm_minor->type == DRM_MINOR_LEGACY &&
  39. !drm_core_check_feature(drm_dev, DRIVER_MODESET) &&
  40. drm_dev->driver->suspend)
  41. return drm_dev->driver->suspend(drm_dev, state);
  42. }
  43. return 0;
  44. }
  45. /**
  46. * drm_class_resume - DRM class resume hook
  47. * @dev: Linux device to resume
  48. *
  49. * Just figures out what the actual struct drm_device associated with
  50. * @dev is and calls its resume hook, if present.
  51. */
  52. static int drm_class_resume(struct device *dev)
  53. {
  54. if (dev->type == &drm_sysfs_device_minor) {
  55. struct drm_minor *drm_minor = to_drm_minor(dev);
  56. struct drm_device *drm_dev = drm_minor->dev;
  57. if (drm_minor->type == DRM_MINOR_LEGACY &&
  58. !drm_core_check_feature(drm_dev, DRIVER_MODESET) &&
  59. drm_dev->driver->resume)
  60. return drm_dev->driver->resume(drm_dev);
  61. }
  62. return 0;
  63. }
  64. static char *drm_devnode(struct device *dev, mode_t *mode)
  65. {
  66. return kasprintf(GFP_KERNEL, "dri/%s", dev_name(dev));
  67. }
  68. static CLASS_ATTR_STRING(version, S_IRUGO,
  69. CORE_NAME " "
  70. __stringify(CORE_MAJOR) "."
  71. __stringify(CORE_MINOR) "."
  72. __stringify(CORE_PATCHLEVEL) " "
  73. CORE_DATE);
  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.attr);
  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.attr);
  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 attr_cnt = 0;
  308. int opt_cnt = 0;
  309. int i;
  310. int ret = 0;
  311. /* We shouldn't get called more than once for the same connector */
  312. BUG_ON(device_is_registered(&connector->kdev));
  313. connector->kdev.parent = &dev->primary->kdev;
  314. connector->kdev.class = drm_class;
  315. connector->kdev.release = drm_sysfs_device_release;
  316. DRM_DEBUG("adding \"%s\" to sysfs\n",
  317. drm_get_connector_name(connector));
  318. dev_set_name(&connector->kdev, "card%d-%s",
  319. dev->primary->index, drm_get_connector_name(connector));
  320. ret = device_register(&connector->kdev);
  321. if (ret) {
  322. DRM_ERROR("failed to register connector device: %d\n", ret);
  323. goto out;
  324. }
  325. /* Standard attributes */
  326. for (attr_cnt = 0; attr_cnt < ARRAY_SIZE(connector_attrs); attr_cnt++) {
  327. ret = device_create_file(&connector->kdev, &connector_attrs[attr_cnt]);
  328. if (ret)
  329. goto err_out_files;
  330. }
  331. /* Optional attributes */
  332. /*
  333. * In the long run it maybe a good idea to make one set of
  334. * optionals per connector type.
  335. */
  336. switch (connector->connector_type) {
  337. case DRM_MODE_CONNECTOR_DVII:
  338. case DRM_MODE_CONNECTOR_Composite:
  339. case DRM_MODE_CONNECTOR_SVIDEO:
  340. case DRM_MODE_CONNECTOR_Component:
  341. case DRM_MODE_CONNECTOR_TV:
  342. for (opt_cnt = 0; opt_cnt < ARRAY_SIZE(connector_attrs_opt1); opt_cnt++) {
  343. ret = device_create_file(&connector->kdev, &connector_attrs_opt1[opt_cnt]);
  344. if (ret)
  345. goto err_out_files;
  346. }
  347. break;
  348. default:
  349. break;
  350. }
  351. ret = sysfs_create_bin_file(&connector->kdev.kobj, &edid_attr);
  352. if (ret)
  353. goto err_out_files;
  354. /* Let userspace know we have a new connector */
  355. drm_sysfs_hotplug_event(dev);
  356. return 0;
  357. err_out_files:
  358. for (i = 0; i < opt_cnt; i++)
  359. device_remove_file(&connector->kdev, &connector_attrs_opt1[i]);
  360. for (i = 0; i < attr_cnt; i++)
  361. device_remove_file(&connector->kdev, &connector_attrs[i]);
  362. device_unregister(&connector->kdev);
  363. out:
  364. return ret;
  365. }
  366. EXPORT_SYMBOL(drm_sysfs_connector_add);
  367. /**
  368. * drm_sysfs_connector_remove - remove an connector device from sysfs
  369. * @connector: connector to remove
  370. *
  371. * Remove @connector and its associated attributes from sysfs. Note that
  372. * the device model core will take care of sending the "remove" uevent
  373. * at this time, so we don't need to do it.
  374. *
  375. * Note:
  376. * This routine should only be called if the connector was previously
  377. * successfully registered. If @connector hasn't been registered yet,
  378. * you'll likely see a panic somewhere deep in sysfs code when called.
  379. */
  380. void drm_sysfs_connector_remove(struct drm_connector *connector)
  381. {
  382. int i;
  383. DRM_DEBUG("removing \"%s\" from sysfs\n",
  384. drm_get_connector_name(connector));
  385. for (i = 0; i < ARRAY_SIZE(connector_attrs); i++)
  386. device_remove_file(&connector->kdev, &connector_attrs[i]);
  387. sysfs_remove_bin_file(&connector->kdev.kobj, &edid_attr);
  388. device_unregister(&connector->kdev);
  389. }
  390. EXPORT_SYMBOL(drm_sysfs_connector_remove);
  391. /**
  392. * drm_sysfs_hotplug_event - generate a DRM uevent
  393. * @dev: DRM device
  394. *
  395. * Send a uevent for the DRM device specified by @dev. Currently we only
  396. * set HOTPLUG=1 in the uevent environment, but this could be expanded to
  397. * deal with other types of events.
  398. */
  399. void drm_sysfs_hotplug_event(struct drm_device *dev)
  400. {
  401. char *event_string = "HOTPLUG=1";
  402. char *envp[] = { event_string, NULL };
  403. DRM_DEBUG("generating hotplug event\n");
  404. kobject_uevent_env(&dev->primary->kdev.kobj, KOBJ_CHANGE, envp);
  405. }
  406. EXPORT_SYMBOL(drm_sysfs_hotplug_event);
  407. /**
  408. * drm_sysfs_device_add - adds a class device to sysfs for a character driver
  409. * @dev: DRM device to be added
  410. * @head: DRM head in question
  411. *
  412. * Add a DRM device to the DRM's device model class. We use @dev's PCI device
  413. * as the parent for the Linux device, and make sure it has a file containing
  414. * the driver we're using (for userspace compatibility).
  415. */
  416. int drm_sysfs_device_add(struct drm_minor *minor)
  417. {
  418. int err;
  419. char *minor_str;
  420. minor->kdev.parent = &minor->dev->pdev->dev;
  421. minor->kdev.class = drm_class;
  422. minor->kdev.release = drm_sysfs_device_release;
  423. minor->kdev.devt = minor->device;
  424. minor->kdev.type = &drm_sysfs_device_minor;
  425. if (minor->type == DRM_MINOR_CONTROL)
  426. minor_str = "controlD%d";
  427. else if (minor->type == DRM_MINOR_RENDER)
  428. minor_str = "renderD%d";
  429. else
  430. minor_str = "card%d";
  431. dev_set_name(&minor->kdev, minor_str, minor->index);
  432. err = device_register(&minor->kdev);
  433. if (err) {
  434. DRM_ERROR("device add failed: %d\n", err);
  435. goto err_out;
  436. }
  437. return 0;
  438. err_out:
  439. return err;
  440. }
  441. /**
  442. * drm_sysfs_device_remove - remove DRM device
  443. * @dev: DRM device to remove
  444. *
  445. * This call unregisters and cleans up a class device that was created with a
  446. * call to drm_sysfs_device_add()
  447. */
  448. void drm_sysfs_device_remove(struct drm_minor *minor)
  449. {
  450. device_unregister(&minor->kdev);
  451. }
  452. /**
  453. * drm_class_device_register - Register a struct device in the drm class.
  454. *
  455. * @dev: pointer to struct device to register.
  456. *
  457. * @dev should have all relevant members pre-filled with the exception
  458. * of the class member. In particular, the device_type member must
  459. * be set.
  460. */
  461. int drm_class_device_register(struct device *dev)
  462. {
  463. dev->class = drm_class;
  464. return device_register(dev);
  465. }
  466. EXPORT_SYMBOL_GPL(drm_class_device_register);
  467. void drm_class_device_unregister(struct device *dev)
  468. {
  469. return device_unregister(dev);
  470. }
  471. EXPORT_SYMBOL_GPL(drm_class_device_unregister);