ide-ioctls.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * IDE ioctls handling.
  3. */
  4. #include <linux/export.h>
  5. #include <linux/hdreg.h>
  6. #include <linux/ide.h>
  7. #include <linux/slab.h>
  8. static const struct ide_ioctl_devset ide_ioctl_settings[] = {
  9. { HDIO_GET_32BIT, HDIO_SET_32BIT, &ide_devset_io_32bit },
  10. { HDIO_GET_KEEPSETTINGS, HDIO_SET_KEEPSETTINGS, &ide_devset_keepsettings },
  11. { HDIO_GET_UNMASKINTR, HDIO_SET_UNMASKINTR, &ide_devset_unmaskirq },
  12. { HDIO_GET_DMA, HDIO_SET_DMA, &ide_devset_using_dma },
  13. { -1, HDIO_SET_PIO_MODE, &ide_devset_pio_mode },
  14. { 0 }
  15. };
  16. int ide_setting_ioctl(ide_drive_t *drive, struct block_device *bdev,
  17. unsigned int cmd, unsigned long arg,
  18. const struct ide_ioctl_devset *s)
  19. {
  20. const struct ide_devset *ds;
  21. int err = -EOPNOTSUPP;
  22. for (; (ds = s->setting); s++) {
  23. if (ds->get && s->get_ioctl == cmd)
  24. goto read_val;
  25. else if (ds->set && s->set_ioctl == cmd)
  26. goto set_val;
  27. }
  28. return err;
  29. read_val:
  30. mutex_lock(&ide_setting_mtx);
  31. err = ds->get(drive);
  32. mutex_unlock(&ide_setting_mtx);
  33. return err >= 0 ? put_user(err, (long __user *)arg) : err;
  34. set_val:
  35. if (bdev != bdev->bd_contains)
  36. err = -EINVAL;
  37. else {
  38. if (!capable(CAP_SYS_ADMIN))
  39. err = -EACCES;
  40. else {
  41. mutex_lock(&ide_setting_mtx);
  42. err = ide_devset_execute(drive, ds, arg);
  43. mutex_unlock(&ide_setting_mtx);
  44. }
  45. }
  46. return err;
  47. }
  48. EXPORT_SYMBOL_GPL(ide_setting_ioctl);
  49. static int ide_get_identity_ioctl(ide_drive_t *drive, unsigned int cmd,
  50. unsigned long arg)
  51. {
  52. u16 *id = NULL;
  53. int size = (cmd == HDIO_GET_IDENTITY) ? (ATA_ID_WORDS * 2) : 142;
  54. int rc = 0;
  55. if ((drive->dev_flags & IDE_DFLAG_ID_READ) == 0) {
  56. rc = -ENOMSG;
  57. goto out;
  58. }
  59. /* ata_id_to_hd_driveid() relies on 'id' to be fully allocated. */
  60. id = kmalloc(ATA_ID_WORDS * 2, GFP_KERNEL);
  61. if (id == NULL) {
  62. rc = -ENOMEM;
  63. goto out;
  64. }
  65. memcpy(id, drive->id, size);
  66. ata_id_to_hd_driveid(id);
  67. if (copy_to_user((void __user *)arg, id, size))
  68. rc = -EFAULT;
  69. kfree(id);
  70. out:
  71. return rc;
  72. }
  73. static int ide_get_nice_ioctl(ide_drive_t *drive, unsigned long arg)
  74. {
  75. return put_user((!!(drive->dev_flags & IDE_DFLAG_DSC_OVERLAP)
  76. << IDE_NICE_DSC_OVERLAP) |
  77. (!!(drive->dev_flags & IDE_DFLAG_NICE1)
  78. << IDE_NICE_1), (long __user *)arg);
  79. }
  80. static int ide_set_nice_ioctl(ide_drive_t *drive, unsigned long arg)
  81. {
  82. if (arg != (arg & ((1 << IDE_NICE_DSC_OVERLAP) | (1 << IDE_NICE_1))))
  83. return -EPERM;
  84. if (((arg >> IDE_NICE_DSC_OVERLAP) & 1) &&
  85. (drive->media != ide_tape))
  86. return -EPERM;
  87. if ((arg >> IDE_NICE_DSC_OVERLAP) & 1)
  88. drive->dev_flags |= IDE_DFLAG_DSC_OVERLAP;
  89. else
  90. drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
  91. if ((arg >> IDE_NICE_1) & 1)
  92. drive->dev_flags |= IDE_DFLAG_NICE1;
  93. else
  94. drive->dev_flags &= ~IDE_DFLAG_NICE1;
  95. return 0;
  96. }
  97. static int ide_cmd_ioctl(ide_drive_t *drive, unsigned long arg)
  98. {
  99. u8 *buf = NULL;
  100. int bufsize = 0, err = 0;
  101. u8 args[4], xfer_rate = 0;
  102. struct ide_cmd cmd;
  103. struct ide_taskfile *tf = &cmd.tf;
  104. if (NULL == (void *) arg) {
  105. struct request *rq;
  106. rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
  107. rq->cmd_type = REQ_TYPE_ATA_TASKFILE;
  108. err = blk_execute_rq(drive->queue, NULL, rq, 0);
  109. blk_put_request(rq);
  110. return err;
  111. }
  112. if (copy_from_user(args, (void __user *)arg, 4))
  113. return -EFAULT;
  114. memset(&cmd, 0, sizeof(cmd));
  115. tf->feature = args[2];
  116. if (args[0] == ATA_CMD_SMART) {
  117. tf->nsect = args[3];
  118. tf->lbal = args[1];
  119. tf->lbam = ATA_SMART_LBAM_PASS;
  120. tf->lbah = ATA_SMART_LBAH_PASS;
  121. cmd.valid.out.tf = IDE_VALID_OUT_TF;
  122. cmd.valid.in.tf = IDE_VALID_NSECT;
  123. } else {
  124. tf->nsect = args[1];
  125. cmd.valid.out.tf = IDE_VALID_FEATURE | IDE_VALID_NSECT;
  126. cmd.valid.in.tf = IDE_VALID_NSECT;
  127. }
  128. tf->command = args[0];
  129. cmd.protocol = args[3] ? ATA_PROT_PIO : ATA_PROT_NODATA;
  130. if (args[3]) {
  131. cmd.tf_flags |= IDE_TFLAG_IO_16BIT;
  132. bufsize = SECTOR_SIZE * args[3];
  133. buf = kzalloc(bufsize, GFP_KERNEL);
  134. if (buf == NULL)
  135. return -ENOMEM;
  136. }
  137. if (tf->command == ATA_CMD_SET_FEATURES &&
  138. tf->feature == SETFEATURES_XFER &&
  139. tf->nsect >= XFER_SW_DMA_0) {
  140. xfer_rate = ide_find_dma_mode(drive, tf->nsect);
  141. if (xfer_rate != tf->nsect) {
  142. err = -EINVAL;
  143. goto abort;
  144. }
  145. cmd.tf_flags |= IDE_TFLAG_SET_XFER;
  146. }
  147. err = ide_raw_taskfile(drive, &cmd, buf, args[3]);
  148. args[0] = tf->status;
  149. args[1] = tf->error;
  150. args[2] = tf->nsect;
  151. abort:
  152. if (copy_to_user((void __user *)arg, &args, 4))
  153. err = -EFAULT;
  154. if (buf) {
  155. if (copy_to_user((void __user *)(arg + 4), buf, bufsize))
  156. err = -EFAULT;
  157. kfree(buf);
  158. }
  159. return err;
  160. }
  161. static int ide_task_ioctl(ide_drive_t *drive, unsigned long arg)
  162. {
  163. void __user *p = (void __user *)arg;
  164. int err = 0;
  165. u8 args[7];
  166. struct ide_cmd cmd;
  167. if (copy_from_user(args, p, 7))
  168. return -EFAULT;
  169. memset(&cmd, 0, sizeof(cmd));
  170. memcpy(&cmd.tf.feature, &args[1], 6);
  171. cmd.tf.command = args[0];
  172. cmd.valid.out.tf = IDE_VALID_OUT_TF | IDE_VALID_DEVICE;
  173. cmd.valid.in.tf = IDE_VALID_IN_TF | IDE_VALID_DEVICE;
  174. err = ide_no_data_taskfile(drive, &cmd);
  175. args[0] = cmd.tf.command;
  176. memcpy(&args[1], &cmd.tf.feature, 6);
  177. if (copy_to_user(p, args, 7))
  178. err = -EFAULT;
  179. return err;
  180. }
  181. static int generic_drive_reset(ide_drive_t *drive)
  182. {
  183. struct request *rq;
  184. int ret = 0;
  185. rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
  186. rq->cmd_type = REQ_TYPE_DRV_PRIV;
  187. rq->cmd_len = 1;
  188. rq->cmd[0] = REQ_DRIVE_RESET;
  189. if (blk_execute_rq(drive->queue, NULL, rq, 1))
  190. ret = rq->errors;
  191. blk_put_request(rq);
  192. return ret;
  193. }
  194. int generic_ide_ioctl(ide_drive_t *drive, struct block_device *bdev,
  195. unsigned int cmd, unsigned long arg)
  196. {
  197. int err;
  198. err = ide_setting_ioctl(drive, bdev, cmd, arg, ide_ioctl_settings);
  199. if (err != -EOPNOTSUPP)
  200. return err;
  201. switch (cmd) {
  202. case HDIO_OBSOLETE_IDENTITY:
  203. case HDIO_GET_IDENTITY:
  204. if (bdev != bdev->bd_contains)
  205. return -EINVAL;
  206. return ide_get_identity_ioctl(drive, cmd, arg);
  207. case HDIO_GET_NICE:
  208. return ide_get_nice_ioctl(drive, arg);
  209. case HDIO_SET_NICE:
  210. if (!capable(CAP_SYS_ADMIN))
  211. return -EACCES;
  212. return ide_set_nice_ioctl(drive, arg);
  213. #ifdef CONFIG_IDE_TASK_IOCTL
  214. case HDIO_DRIVE_TASKFILE:
  215. if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
  216. return -EACCES;
  217. if (drive->media == ide_disk)
  218. return ide_taskfile_ioctl(drive, arg);
  219. return -ENOMSG;
  220. #endif
  221. case HDIO_DRIVE_CMD:
  222. if (!capable(CAP_SYS_RAWIO))
  223. return -EACCES;
  224. return ide_cmd_ioctl(drive, arg);
  225. case HDIO_DRIVE_TASK:
  226. if (!capable(CAP_SYS_RAWIO))
  227. return -EACCES;
  228. return ide_task_ioctl(drive, arg);
  229. case HDIO_DRIVE_RESET:
  230. if (!capable(CAP_SYS_ADMIN))
  231. return -EACCES;
  232. return generic_drive_reset(drive);
  233. case HDIO_GET_BUSSTATE:
  234. if (!capable(CAP_SYS_ADMIN))
  235. return -EACCES;
  236. if (put_user(BUSSTATE_ON, (long __user *)arg))
  237. return -EFAULT;
  238. return 0;
  239. case HDIO_SET_BUSSTATE:
  240. if (!capable(CAP_SYS_ADMIN))
  241. return -EACCES;
  242. return -EOPNOTSUPP;
  243. default:
  244. return -EINVAL;
  245. }
  246. }
  247. EXPORT_SYMBOL(generic_ide_ioctl);