drm_sysfs.c 14 KB

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