tape_class.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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_drvdata(tape_class, device,
  64. tcd->char_device->dev,
  65. NULL, "%s", tcd->device_name);
  66. rc = IS_ERR(tcd->class_device) ? PTR_ERR(tcd->class_device) : 0;
  67. if (rc)
  68. goto fail_with_cdev;
  69. rc = sysfs_create_link(
  70. &device->kobj,
  71. &tcd->class_device->kobj,
  72. tcd->mode_name
  73. );
  74. if (rc)
  75. goto fail_with_class_device;
  76. return tcd;
  77. fail_with_class_device:
  78. device_destroy(tape_class, tcd->char_device->dev);
  79. fail_with_cdev:
  80. cdev_del(tcd->char_device);
  81. fail_with_tcd:
  82. kfree(tcd);
  83. return ERR_PTR(rc);
  84. }
  85. EXPORT_SYMBOL(register_tape_dev);
  86. void unregister_tape_dev(struct device *device, struct tape_class_device *tcd)
  87. {
  88. if (tcd != NULL && !IS_ERR(tcd)) {
  89. sysfs_remove_link(&device->kobj, tcd->mode_name);
  90. device_destroy(tape_class, tcd->char_device->dev);
  91. cdev_del(tcd->char_device);
  92. kfree(tcd);
  93. }
  94. }
  95. EXPORT_SYMBOL(unregister_tape_dev);
  96. static int __init tape_init(void)
  97. {
  98. tape_class = class_create(THIS_MODULE, "tape390");
  99. return 0;
  100. }
  101. static void __exit tape_exit(void)
  102. {
  103. class_destroy(tape_class);
  104. tape_class = NULL;
  105. }
  106. postcore_initcall(tape_init);
  107. module_exit(tape_exit);