ide-floppy_ioctl.c 7.2 KB

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