ide-floppy_ioctl.c 7.1 KB

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