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