drm_sysfs.c 14 KB

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