ide-gd-floppy.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. #include <linux/module.h>
  2. #include <linux/types.h>
  3. #include <linux/string.h>
  4. #include <linux/kernel.h>
  5. #include <linux/errno.h>
  6. #include <linux/genhd.h>
  7. #include <linux/mutex.h>
  8. #include <linux/ide.h>
  9. #include <linux/hdreg.h>
  10. #include "ide-floppy.h"
  11. #define IDEFLOPPY_VERSION "1.00"
  12. /* module parameters */
  13. static unsigned long debug_mask;
  14. module_param(debug_mask, ulong, 0644);
  15. static DEFINE_MUTEX(ide_disk_ref_mutex);
  16. static void ide_disk_release(struct kref *);
  17. static struct ide_floppy_obj *ide_disk_get(struct gendisk *disk)
  18. {
  19. struct ide_floppy_obj *idkp = NULL;
  20. mutex_lock(&ide_disk_ref_mutex);
  21. idkp = ide_drv_g(disk, ide_floppy_obj);
  22. if (idkp) {
  23. if (ide_device_get(idkp->drive))
  24. idkp = NULL;
  25. else
  26. kref_get(&idkp->kref);
  27. }
  28. mutex_unlock(&ide_disk_ref_mutex);
  29. return idkp;
  30. }
  31. static void ide_disk_put(struct ide_floppy_obj *idkp)
  32. {
  33. ide_drive_t *drive = idkp->drive;
  34. mutex_lock(&ide_disk_ref_mutex);
  35. kref_put(&idkp->kref, ide_disk_release);
  36. ide_device_put(drive);
  37. mutex_unlock(&ide_disk_ref_mutex);
  38. }
  39. sector_t ide_gd_capacity(ide_drive_t *drive)
  40. {
  41. return drive->capacity64;
  42. }
  43. static int ide_gd_probe(ide_drive_t *);
  44. static void ide_gd_remove(ide_drive_t *drive)
  45. {
  46. struct ide_floppy_obj *idkp = drive->driver_data;
  47. struct gendisk *g = idkp->disk;
  48. ide_proc_unregister_driver(drive, idkp->driver);
  49. del_gendisk(g);
  50. ide_disk_put(idkp);
  51. }
  52. static void ide_disk_release(struct kref *kref)
  53. {
  54. struct ide_floppy_obj *idkp = to_ide_drv(kref, ide_floppy_obj);
  55. ide_drive_t *drive = idkp->drive;
  56. struct gendisk *g = idkp->disk;
  57. drive->driver_data = NULL;
  58. g->private_data = NULL;
  59. put_disk(g);
  60. kfree(idkp);
  61. }
  62. #ifdef CONFIG_IDE_PROC_FS
  63. static ide_proc_entry_t *ide_floppy_proc_entries(ide_drive_t *drive)
  64. {
  65. return ide_floppy_proc;
  66. }
  67. static const struct ide_proc_devset *ide_floppy_proc_devsets(ide_drive_t *drive)
  68. {
  69. return ide_floppy_settings;
  70. }
  71. #endif
  72. static ide_driver_t ide_gd_driver = {
  73. .gen_driver = {
  74. .owner = THIS_MODULE,
  75. .name = "ide-floppy",
  76. .bus = &ide_bus_type,
  77. },
  78. .probe = ide_gd_probe,
  79. .remove = ide_gd_remove,
  80. .version = IDEFLOPPY_VERSION,
  81. .do_request = ide_floppy_do_request,
  82. .end_request = ide_floppy_end_request,
  83. .error = __ide_error,
  84. #ifdef CONFIG_IDE_PROC_FS
  85. .proc_entries = ide_floppy_proc_entries,
  86. .proc_devsets = ide_floppy_proc_devsets,
  87. #endif
  88. };
  89. static int ide_gd_open(struct inode *inode, struct file *filp)
  90. {
  91. struct gendisk *disk = inode->i_bdev->bd_disk;
  92. struct ide_floppy_obj *idkp;
  93. ide_drive_t *drive;
  94. int ret = 0;
  95. idkp = ide_disk_get(disk);
  96. if (idkp == NULL)
  97. return -ENXIO;
  98. drive = idkp->drive;
  99. ide_debug_log(IDE_DBG_FUNC, "Call %s\n", __func__);
  100. idkp->openers++;
  101. if (idkp->openers == 1) {
  102. drive->dev_flags &= ~IDE_DFLAG_FORMAT_IN_PROGRESS;
  103. /* Just in case */
  104. if (ide_do_test_unit_ready(drive, disk))
  105. ide_do_start_stop(drive, disk, 1);
  106. ret = ide_floppy_get_capacity(drive);
  107. set_capacity(disk, ide_gd_capacity(drive));
  108. if (ret && (filp->f_flags & O_NDELAY) == 0) {
  109. /*
  110. * Allow O_NDELAY to open a drive without a disk, or with an
  111. * unreadable disk, so that we can get the format capacity
  112. * of the drive or begin the format - Sam
  113. */
  114. ret = -EIO;
  115. goto out_put_idkp;
  116. }
  117. if ((drive->dev_flags & IDE_DFLAG_WP) && (filp->f_mode & 2)) {
  118. ret = -EROFS;
  119. goto out_put_idkp;
  120. }
  121. ide_set_media_lock(drive, disk, 1);
  122. drive->dev_flags |= IDE_DFLAG_MEDIA_CHANGED;
  123. check_disk_change(inode->i_bdev);
  124. } else if (drive->dev_flags & IDE_DFLAG_FORMAT_IN_PROGRESS) {
  125. ret = -EBUSY;
  126. goto out_put_idkp;
  127. }
  128. return 0;
  129. out_put_idkp:
  130. idkp->openers--;
  131. ide_disk_put(idkp);
  132. return ret;
  133. }
  134. static int ide_gd_release(struct inode *inode, struct file *filp)
  135. {
  136. struct gendisk *disk = inode->i_bdev->bd_disk;
  137. struct ide_floppy_obj *idkp = ide_drv_g(disk, ide_floppy_obj);
  138. ide_drive_t *drive = idkp->drive;
  139. ide_debug_log(IDE_DBG_FUNC, "Call %s\n", __func__);
  140. if (idkp->openers == 1) {
  141. ide_set_media_lock(drive, disk, 0);
  142. drive->dev_flags &= ~IDE_DFLAG_FORMAT_IN_PROGRESS;
  143. }
  144. idkp->openers--;
  145. ide_disk_put(idkp);
  146. return 0;
  147. }
  148. static int ide_gd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  149. {
  150. struct ide_floppy_obj *idkp = ide_drv_g(bdev->bd_disk, ide_floppy_obj);
  151. ide_drive_t *drive = idkp->drive;
  152. geo->heads = drive->bios_head;
  153. geo->sectors = drive->bios_sect;
  154. geo->cylinders = (u16)drive->bios_cyl; /* truncate */
  155. return 0;
  156. }
  157. static int ide_gd_media_changed(struct gendisk *disk)
  158. {
  159. struct ide_floppy_obj *idkp = ide_drv_g(disk, ide_floppy_obj);
  160. ide_drive_t *drive = idkp->drive;
  161. int ret;
  162. /* do not scan partitions twice if this is a removable device */
  163. if (drive->dev_flags & IDE_DFLAG_ATTACH) {
  164. drive->dev_flags &= ~IDE_DFLAG_ATTACH;
  165. return 0;
  166. }
  167. ret = !!(drive->dev_flags & IDE_DFLAG_MEDIA_CHANGED);
  168. drive->dev_flags &= ~IDE_DFLAG_MEDIA_CHANGED;
  169. return ret;
  170. }
  171. static int ide_gd_revalidate_disk(struct gendisk *disk)
  172. {
  173. struct ide_floppy_obj *idkp = ide_drv_g(disk, ide_floppy_obj);
  174. set_capacity(disk, ide_gd_capacity(idkp->drive));
  175. return 0;
  176. }
  177. static struct block_device_operations ide_gd_ops = {
  178. .owner = THIS_MODULE,
  179. .open = ide_gd_open,
  180. .release = ide_gd_release,
  181. .ioctl = ide_floppy_ioctl,
  182. .getgeo = ide_gd_getgeo,
  183. .media_changed = ide_gd_media_changed,
  184. .revalidate_disk = ide_gd_revalidate_disk
  185. };
  186. static int ide_gd_probe(ide_drive_t *drive)
  187. {
  188. struct ide_floppy_obj *idkp;
  189. struct gendisk *g;
  190. if (!strstr("ide-floppy", drive->driver_req))
  191. goto failed;
  192. if (drive->media != ide_floppy)
  193. goto failed;
  194. if (!ide_check_atapi_device(drive, DRV_NAME)) {
  195. printk(KERN_ERR PFX "%s: not supported by this version of "
  196. DRV_NAME "\n", drive->name);
  197. goto failed;
  198. }
  199. idkp = kzalloc(sizeof(*idkp), GFP_KERNEL);
  200. if (!idkp) {
  201. printk(KERN_ERR PFX "%s: Can't allocate a floppy structure\n",
  202. drive->name);
  203. goto failed;
  204. }
  205. g = alloc_disk_node(1 << PARTN_BITS, hwif_to_node(drive->hwif));
  206. if (!g)
  207. goto out_free_idkp;
  208. ide_init_disk(g, drive);
  209. kref_init(&idkp->kref);
  210. idkp->drive = drive;
  211. idkp->driver = &ide_gd_driver;
  212. idkp->disk = g;
  213. g->private_data = &idkp->driver;
  214. drive->driver_data = idkp;
  215. drive->debug_mask = debug_mask;
  216. ide_floppy_setup(drive);
  217. set_capacity(g, ide_gd_capacity(drive));
  218. g->minors = 1 << PARTN_BITS;
  219. g->driverfs_dev = &drive->gendev;
  220. if (drive->dev_flags & IDE_DFLAG_REMOVABLE)
  221. g->flags = GENHD_FL_REMOVABLE;
  222. g->fops = &ide_gd_ops;
  223. add_disk(g);
  224. return 0;
  225. out_free_idkp:
  226. kfree(idkp);
  227. failed:
  228. return -ENODEV;
  229. }
  230. static int __init ide_gd_init(void)
  231. {
  232. printk(KERN_INFO DRV_NAME " driver " IDEFLOPPY_VERSION "\n");
  233. return driver_register(&ide_gd_driver.gen_driver);
  234. }
  235. static void __exit ide_gd_exit(void)
  236. {
  237. driver_unregister(&ide_gd_driver.gen_driver);
  238. }
  239. MODULE_ALIAS("ide:*m-floppy*");
  240. MODULE_ALIAS("ide-floppy");
  241. module_init(ide_gd_init);
  242. module_exit(ide_gd_exit);
  243. MODULE_LICENSE("GPL");
  244. MODULE_DESCRIPTION("ATAPI FLOPPY Driver");