ide-floppy_ioctl.c 7.2 KB

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