tape_class.c 2.9 KB

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