ide-floppy_ioctl.c 7.2 KB

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