drm_sysfs.c 15 KB

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