media-device.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Media device
  3. *
  4. * Copyright (C) 2010 Nokia Corporation
  5. *
  6. * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
  7. * Sakari Ailus <sakari.ailus@iki.fi>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <linux/types.h>
  23. #include <linux/ioctl.h>
  24. #include <media/media-device.h>
  25. #include <media/media-devnode.h>
  26. static const struct media_file_operations media_device_fops = {
  27. .owner = THIS_MODULE,
  28. };
  29. /* -----------------------------------------------------------------------------
  30. * sysfs
  31. */
  32. static ssize_t show_model(struct device *cd,
  33. struct device_attribute *attr, char *buf)
  34. {
  35. struct media_device *mdev = to_media_device(to_media_devnode(cd));
  36. return sprintf(buf, "%.*s\n", (int)sizeof(mdev->model), mdev->model);
  37. }
  38. static DEVICE_ATTR(model, S_IRUGO, show_model, NULL);
  39. /* -----------------------------------------------------------------------------
  40. * Registration/unregistration
  41. */
  42. static void media_device_release(struct media_devnode *mdev)
  43. {
  44. }
  45. /**
  46. * media_device_register - register a media device
  47. * @mdev: The media device
  48. *
  49. * The caller is responsible for initializing the media device before
  50. * registration. The following fields must be set:
  51. *
  52. * - dev must point to the parent device
  53. * - model must be filled with the device model name
  54. */
  55. int __must_check media_device_register(struct media_device *mdev)
  56. {
  57. int ret;
  58. if (WARN_ON(mdev->dev == NULL || mdev->model[0] == 0))
  59. return -EINVAL;
  60. /* Register the device node. */
  61. mdev->devnode.fops = &media_device_fops;
  62. mdev->devnode.parent = mdev->dev;
  63. mdev->devnode.release = media_device_release;
  64. ret = media_devnode_register(&mdev->devnode);
  65. if (ret < 0)
  66. return ret;
  67. ret = device_create_file(&mdev->devnode.dev, &dev_attr_model);
  68. if (ret < 0) {
  69. media_devnode_unregister(&mdev->devnode);
  70. return ret;
  71. }
  72. return 0;
  73. }
  74. EXPORT_SYMBOL_GPL(media_device_register);
  75. /**
  76. * media_device_unregister - unregister a media device
  77. * @mdev: The media device
  78. *
  79. */
  80. void media_device_unregister(struct media_device *mdev)
  81. {
  82. device_remove_file(&mdev->devnode.dev, &dev_attr_model);
  83. media_devnode_unregister(&mdev->devnode);
  84. }
  85. EXPORT_SYMBOL_GPL(media_device_unregister);