ide-floppy_ioctl.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * ide-floppy IOCTLs handling.
  3. */
  4. #include <linux/kernel.h>
  5. #include <linux/ide.h>
  6. #include <linux/cdrom.h>
  7. #include <asm/unaligned.h>
  8. #include <scsi/scsi_ioctl.h>
  9. #include "ide-floppy.h"
  10. /*
  11. * Obtain the list of formattable capacities.
  12. * Very similar to ide_floppy_get_capacity, except that we push the capacity
  13. * descriptors to userland, instead of our own structures.
  14. *
  15. * Userland gives us the following structure:
  16. *
  17. * struct idefloppy_format_capacities {
  18. * int nformats;
  19. * struct {
  20. * int nblocks;
  21. * int blocksize;
  22. * } formats[];
  23. * };
  24. *
  25. * userland initializes nformats to the number of allocated formats[] records.
  26. * On exit we set nformats to the number of records we've actually initialized.
  27. */
  28. static int ide_floppy_get_format_capacities(ide_drive_t *drive, int __user *arg)
  29. {
  30. struct ide_disk_obj *floppy = drive->driver_data;
  31. struct ide_atapi_pc pc;
  32. u8 header_len, desc_cnt;
  33. int i, blocks, length, u_array_size, u_index;
  34. int __user *argp;
  35. if (get_user(u_array_size, arg))
  36. return -EFAULT;
  37. if (u_array_size <= 0)
  38. return -EINVAL;
  39. ide_floppy_create_read_capacity_cmd(&pc);
  40. if (ide_queue_pc_tail(drive, floppy->disk, &pc)) {
  41. printk(KERN_ERR "ide-floppy: Can't get floppy parameters\n");
  42. return -EIO;
  43. }
  44. header_len = pc.buf[3];
  45. desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */
  46. u_index = 0;
  47. argp = arg + 1;
  48. /*
  49. * We always skip the first capacity descriptor. That's the current
  50. * capacity. We are interested in the remaining descriptors, the
  51. * formattable capacities.
  52. */
  53. for (i = 1; i < desc_cnt; i++) {
  54. unsigned int desc_start = 4 + i*8;
  55. if (u_index >= u_array_size)
  56. break; /* User-supplied buffer too small */
  57. blocks = be32_to_cpup((__be32 *)&pc.buf[desc_start]);
  58. length = be16_to_cpup((__be16 *)&pc.buf[desc_start + 6]);
  59. if (put_user(blocks, argp))
  60. return -EFAULT;
  61. ++argp;
  62. if (put_user(length, argp))
  63. return -EFAULT;
  64. ++argp;
  65. ++u_index;
  66. }
  67. if (put_user(u_index, arg))
  68. return -EFAULT;
  69. return 0;
  70. }
  71. static void ide_floppy_create_format_unit_cmd(struct ide_atapi_pc *pc, int b,
  72. int l, int flags)
  73. {
  74. ide_init_pc(pc);
  75. pc->c[0] = GPCMD_FORMAT_UNIT;
  76. pc->c[1] = 0x17;
  77. memset(pc->buf, 0, 12);
  78. pc->buf[1] = 0xA2;
  79. /* Default format list header, u8 1: FOV/DCRT/IMM bits set */
  80. if (flags & 1) /* Verify bit on... */
  81. pc->buf[1] ^= 0x20; /* ... turn off DCRT bit */
  82. pc->buf[3] = 8;
  83. put_unaligned(cpu_to_be32(b), (unsigned int *)(&pc->buf[4]));
  84. put_unaligned(cpu_to_be32(l), (unsigned int *)(&pc->buf[8]));
  85. pc->buf_size = 12;
  86. pc->flags |= PC_FLAG_WRITING;
  87. }
  88. static int ide_floppy_get_sfrp_bit(ide_drive_t *drive)
  89. {
  90. struct ide_disk_obj *floppy = drive->driver_data;
  91. struct ide_atapi_pc pc;
  92. drive->atapi_flags &= ~IDE_AFLAG_SRFP;
  93. ide_floppy_create_mode_sense_cmd(&pc, IDEFLOPPY_CAPABILITIES_PAGE);
  94. pc.flags |= PC_FLAG_SUPPRESS_ERROR;
  95. if (ide_queue_pc_tail(drive, floppy->disk, &pc))
  96. return 1;
  97. if (pc.buf[8 + 2] & 0x40)
  98. drive->atapi_flags |= IDE_AFLAG_SRFP;
  99. return 0;
  100. }
  101. static int ide_floppy_format_unit(ide_drive_t *drive, int __user *arg)
  102. {
  103. struct ide_disk_obj *floppy = drive->driver_data;
  104. struct ide_atapi_pc pc;
  105. int blocks, length, flags, err = 0;
  106. if (floppy->openers > 1) {
  107. /* Don't format if someone is using the disk */
  108. drive->dev_flags &= ~IDE_DFLAG_FORMAT_IN_PROGRESS;
  109. return -EBUSY;
  110. }
  111. drive->dev_flags |= IDE_DFLAG_FORMAT_IN_PROGRESS;
  112. /*
  113. * Send ATAPI_FORMAT_UNIT to the drive.
  114. *
  115. * Userland gives us the following structure:
  116. *
  117. * struct idefloppy_format_command {
  118. * int nblocks;
  119. * int blocksize;
  120. * int flags;
  121. * } ;
  122. *
  123. * flags is a bitmask, currently, the only defined flag is:
  124. *
  125. * 0x01 - verify media after format.
  126. */
  127. if (get_user(blocks, arg) ||
  128. get_user(length, arg+1) ||
  129. get_user(flags, arg+2)) {
  130. err = -EFAULT;
  131. goto out;
  132. }
  133. (void)ide_floppy_get_sfrp_bit(drive);
  134. ide_floppy_create_format_unit_cmd(&pc, blocks, length, flags);
  135. if (ide_queue_pc_tail(drive, floppy->disk, &pc))
  136. err = -EIO;
  137. out:
  138. if (err)
  139. drive->dev_flags &= ~IDE_DFLAG_FORMAT_IN_PROGRESS;
  140. return err;
  141. }
  142. /*
  143. * Get ATAPI_FORMAT_UNIT progress indication.
  144. *
  145. * Userland gives a pointer to an int. The int is set to a progress
  146. * indicator 0-65536, with 65536=100%.
  147. *
  148. * If the drive does not support format progress indication, we just check
  149. * the dsc bit, and return either 0 or 65536.
  150. */
  151. static int ide_floppy_get_format_progress(ide_drive_t *drive, int __user *arg)
  152. {
  153. struct ide_disk_obj *floppy = drive->driver_data;
  154. struct ide_atapi_pc pc;
  155. int progress_indication = 0x10000;
  156. if (drive->atapi_flags & IDE_AFLAG_SRFP) {
  157. ide_create_request_sense_cmd(drive, &pc);
  158. if (ide_queue_pc_tail(drive, floppy->disk, &pc))
  159. return -EIO;
  160. if (floppy->sense_key == 2 &&
  161. floppy->asc == 4 &&
  162. floppy->ascq == 4)
  163. progress_indication = floppy->progress_indication;
  164. /* Else assume format_unit has finished, and we're at 0x10000 */
  165. } else {
  166. ide_hwif_t *hwif = drive->hwif;
  167. unsigned long flags;
  168. u8 stat;
  169. local_irq_save(flags);
  170. stat = hwif->tp_ops->read_status(hwif);
  171. local_irq_restore(flags);
  172. progress_indication = ((stat & ATA_DSC) == 0) ? 0 : 0x10000;
  173. }
  174. if (put_user(progress_indication, arg))
  175. return -EFAULT;
  176. return 0;
  177. }
  178. static int ide_floppy_lockdoor(ide_drive_t *drive, struct ide_atapi_pc *pc,
  179. unsigned long arg, unsigned int cmd)
  180. {
  181. struct ide_disk_obj *floppy = drive->driver_data;
  182. struct gendisk *disk = floppy->disk;
  183. int prevent = (arg && cmd != CDROMEJECT) ? 1 : 0;
  184. if (floppy->openers > 1)
  185. return -EBUSY;
  186. ide_set_media_lock(drive, disk, prevent);
  187. if (cmd == CDROMEJECT)
  188. ide_do_start_stop(drive, disk, 2);
  189. return 0;
  190. }
  191. static int ide_floppy_format_ioctl(ide_drive_t *drive, fmode_t mode,
  192. unsigned int cmd, void __user *argp)
  193. {
  194. switch (cmd) {
  195. case IDEFLOPPY_IOCTL_FORMAT_SUPPORTED:
  196. return 0;
  197. case IDEFLOPPY_IOCTL_FORMAT_GET_CAPACITY:
  198. return ide_floppy_get_format_capacities(drive, argp);
  199. case IDEFLOPPY_IOCTL_FORMAT_START:
  200. if (!(mode & FMODE_WRITE))
  201. return -EPERM;
  202. return ide_floppy_format_unit(drive, (int __user *)argp);
  203. case IDEFLOPPY_IOCTL_FORMAT_GET_PROGRESS:
  204. return ide_floppy_get_format_progress(drive, argp);
  205. default:
  206. return -ENOTTY;
  207. }
  208. }
  209. int ide_floppy_ioctl(ide_drive_t *drive, struct block_device *bdev,
  210. fmode_t mode, unsigned int cmd, unsigned long arg)
  211. {
  212. struct ide_atapi_pc pc;
  213. void __user *argp = (void __user *)arg;
  214. int err;
  215. if (cmd == CDROMEJECT || cmd == CDROM_LOCKDOOR)
  216. return ide_floppy_lockdoor(drive, &pc, arg, cmd);
  217. err = ide_floppy_format_ioctl(drive, mode, cmd, argp);
  218. if (err != -ENOTTY)
  219. return err;
  220. /*
  221. * skip SCSI_IOCTL_SEND_COMMAND (deprecated)
  222. * and CDROM_SEND_PACKET (legacy) ioctls
  223. */
  224. if (cmd != CDROM_SEND_PACKET && cmd != SCSI_IOCTL_SEND_COMMAND)
  225. err = scsi_cmd_ioctl(bdev->bd_disk->queue, bdev->bd_disk,
  226. mode, cmd, argp);
  227. if (err == -ENOTTY)
  228. err = generic_ide_ioctl(drive, bdev, cmd, arg);
  229. return err;
  230. }