tape_class.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * (C) Copyright IBM Corp. 2004
  3. * tape_class.c
  4. *
  5. * Tape class device support
  6. *
  7. * Author: Stefan Bader <shbader@de.ibm.com>
  8. * Based on simple class device code by Greg K-H
  9. */
  10. #include "tape_class.h"
  11. MODULE_AUTHOR("Stefan Bader <shbader@de.ibm.com>");
  12. MODULE_DESCRIPTION(
  13. "(C) Copyright IBM Corp. 2004 All Rights Reserved.\n"
  14. "tape_class.c"
  15. );
  16. MODULE_LICENSE("GPL");
  17. static struct class *tape_class;
  18. /*
  19. * Register a tape device and return a pointer to the cdev structure.
  20. *
  21. * device
  22. * The pointer to the struct device of the physical (base) device.
  23. * drivername
  24. * The pointer to the drivers name for it's character devices.
  25. * dev
  26. * The intended major/minor number. The major number may be 0 to
  27. * get a dynamic major number.
  28. * fops
  29. * The pointer to the drivers file operations for the tape device.
  30. * devname
  31. * The pointer to the name of the character device.
  32. */
  33. struct tape_class_device *register_tape_dev(
  34. struct device * device,
  35. dev_t dev,
  36. const struct file_operations *fops,
  37. char * device_name,
  38. char * mode_name)
  39. {
  40. struct tape_class_device * tcd;
  41. int rc;
  42. char * s;
  43. tcd = kzalloc(sizeof(struct tape_class_device), GFP_KERNEL);
  44. if (!tcd)
  45. return ERR_PTR(-ENOMEM);
  46. strncpy(tcd->device_name, device_name, TAPECLASS_NAME_LEN);
  47. for (s = strchr(tcd->device_name, '/'); s; s = strchr(s, '/'))
  48. *s = '!';
  49. strncpy(tcd->mode_name, mode_name, TAPECLASS_NAME_LEN);
  50. for (s = strchr(tcd->mode_name, '/'); s; s = strchr(s, '/'))
  51. *s = '!';
  52. tcd->char_device = cdev_alloc();
  53. if (!tcd->char_device) {
  54. rc = -ENOMEM;
  55. goto fail_with_tcd;
  56. }
  57. tcd->char_device->owner = fops->owner;
  58. tcd->char_device->ops = fops;
  59. tcd->char_device->dev = dev;
  60. rc = cdev_add(tcd->char_device, tcd->char_device->dev, 1);
  61. if (rc)
  62. goto fail_with_cdev;
  63. tcd->class_device = device_create(tape_class, device,
  64. tcd->char_device->dev,
  65. "%s", tcd->device_name
  66. );
  67. rc = IS_ERR(tcd->class_device) ? PTR_ERR(tcd->class_device) : 0;
  68. if (rc)
  69. goto fail_with_cdev;
  70. rc = sysfs_create_link(
  71. &device->kobj,
  72. &tcd->class_device->kobj,
  73. tcd->mode_name
  74. );
  75. if (rc)
  76. goto fail_with_class_device;
  77. return tcd;
  78. fail_with_class_device:
  79. device_destroy(tape_class, tcd->char_device->dev);
  80. fail_with_cdev:
  81. cdev_del(tcd->char_device);
  82. fail_with_tcd:
  83. kfree(tcd);
  84. return ERR_PTR(rc);
  85. }
  86. EXPORT_SYMBOL(register_tape_dev);
  87. void unregister_tape_dev(struct tape_class_device *tcd)
  88. {
  89. if (tcd != NULL && !IS_ERR(tcd)) {
  90. sysfs_remove_link(&tcd->class_device->kobj,
  91. tcd->mode_name);
  92. device_destroy(tape_class, tcd->char_device->dev);
  93. cdev_del(tcd->char_device);
  94. kfree(tcd);
  95. }
  96. }
  97. EXPORT_SYMBOL(unregister_tape_dev);
  98. static int __init tape_init(void)
  99. {
  100. tape_class = class_create(THIS_MODULE, "tape390");
  101. return 0;
  102. }
  103. static void __exit tape_exit(void)
  104. {
  105. class_destroy(tape_class);
  106. tape_class = NULL;
  107. }
  108. postcore_initcall(tape_init);
  109. module_exit(tape_exit);