ide-floppy_ioctl.c 8.1 KB

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