ide-ioctls.c 6.7 KB

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