ide-ioctls.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. unsigned long flags;
  20. int err = -EOPNOTSUPP;
  21. for (; (ds = s->setting); s++) {
  22. if (ds->get && s->get_ioctl == cmd)
  23. goto read_val;
  24. else if (ds->set && s->set_ioctl == cmd)
  25. goto set_val;
  26. }
  27. return err;
  28. read_val:
  29. mutex_lock(&ide_setting_mtx);
  30. spin_lock_irqsave(&ide_lock, flags);
  31. err = ds->get(drive);
  32. spin_unlock_irqrestore(&ide_lock, flags);
  33. mutex_unlock(&ide_setting_mtx);
  34. return err >= 0 ? put_user(err, (long __user *)arg) : err;
  35. set_val:
  36. if (bdev != bdev->bd_contains)
  37. err = -EINVAL;
  38. else {
  39. if (!capable(CAP_SYS_ADMIN))
  40. err = -EACCES;
  41. else {
  42. mutex_lock(&ide_setting_mtx);
  43. err = ide_devset_execute(drive, ds, arg);
  44. mutex_unlock(&ide_setting_mtx);
  45. }
  46. }
  47. return err;
  48. }
  49. EXPORT_SYMBOL_GPL(ide_setting_ioctl);
  50. static int ide_get_identity_ioctl(ide_drive_t *drive, unsigned int cmd,
  51. unsigned long arg)
  52. {
  53. u16 *id = NULL;
  54. int size = (cmd == HDIO_GET_IDENTITY) ? (ATA_ID_WORDS * 2) : 142;
  55. int rc = 0;
  56. if ((drive->dev_flags & IDE_DFLAG_ID_READ) == 0) {
  57. rc = -ENOMSG;
  58. goto out;
  59. }
  60. id = kmalloc(size, 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_disk || drive->media == ide_floppy ||
  86. (drive->dev_flags & IDE_DFLAG_SCSI)))
  87. return -EPERM;
  88. if ((arg >> IDE_NICE_DSC_OVERLAP) & 1)
  89. drive->dev_flags |= IDE_DFLAG_DSC_OVERLAP;
  90. else
  91. drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
  92. if ((arg >> IDE_NICE_1) & 1)
  93. drive->dev_flags |= IDE_DFLAG_NICE1;
  94. else
  95. drive->dev_flags &= ~IDE_DFLAG_NICE1;
  96. return 0;
  97. }
  98. static int ide_cmd_ioctl(ide_drive_t *drive, unsigned cmd, unsigned long arg)
  99. {
  100. u8 *buf = NULL;
  101. int bufsize = 0, err = 0;
  102. u8 args[4], xfer_rate = 0;
  103. ide_task_t tfargs;
  104. struct ide_taskfile *tf = &tfargs.tf;
  105. u16 *id = drive->id;
  106. if (NULL == (void *) arg) {
  107. struct request *rq;
  108. rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
  109. rq->cmd_type = REQ_TYPE_ATA_TASKFILE;
  110. err = blk_execute_rq(drive->queue, NULL, rq, 0);
  111. blk_put_request(rq);
  112. return err;
  113. }
  114. if (copy_from_user(args, (void __user *)arg, 4))
  115. return -EFAULT;
  116. memset(&tfargs, 0, sizeof(ide_task_t));
  117. tf->feature = args[2];
  118. if (args[0] == ATA_CMD_SMART) {
  119. tf->nsect = args[3];
  120. tf->lbal = args[1];
  121. tf->lbam = 0x4f;
  122. tf->lbah = 0xc2;
  123. tfargs.tf_flags = IDE_TFLAG_OUT_TF | IDE_TFLAG_IN_NSECT;
  124. } else {
  125. tf->nsect = args[1];
  126. tfargs.tf_flags = IDE_TFLAG_OUT_FEATURE |
  127. IDE_TFLAG_OUT_NSECT | IDE_TFLAG_IN_NSECT;
  128. }
  129. tf->command = args[0];
  130. tfargs.data_phase = args[3] ? TASKFILE_IN : TASKFILE_NO_DATA;
  131. if (args[3]) {
  132. tfargs.tf_flags |= IDE_TFLAG_IO_16BIT;
  133. bufsize = SECTOR_SIZE * args[3];
  134. buf = kzalloc(bufsize, GFP_KERNEL);
  135. if (buf == NULL)
  136. return -ENOMEM;
  137. }
  138. if (tf->command == ATA_CMD_SET_FEATURES &&
  139. tf->feature == SETFEATURES_XFER &&
  140. tf->nsect >= XFER_SW_DMA_0 &&
  141. (id[ATA_ID_UDMA_MODES] ||
  142. id[ATA_ID_MWDMA_MODES] ||
  143. id[ATA_ID_SWDMA_MODES])) {
  144. xfer_rate = args[1];
  145. if (tf->nsect > XFER_UDMA_2 && !eighty_ninty_three(drive)) {
  146. printk(KERN_WARNING "%s: UDMA speeds >UDMA33 cannot "
  147. "be set\n", drive->name);
  148. goto abort;
  149. }
  150. }
  151. err = ide_raw_taskfile(drive, &tfargs, buf, args[3]);
  152. args[0] = tf->status;
  153. args[1] = tf->error;
  154. args[2] = tf->nsect;
  155. if (!err && xfer_rate) {
  156. /* active-retuning-calls future */
  157. ide_set_xfer_rate(drive, xfer_rate);
  158. ide_driveid_update(drive);
  159. }
  160. abort:
  161. if (copy_to_user((void __user *)arg, &args, 4))
  162. err = -EFAULT;
  163. if (buf) {
  164. if (copy_to_user((void __user *)(arg + 4), buf, bufsize))
  165. err = -EFAULT;
  166. kfree(buf);
  167. }
  168. return err;
  169. }
  170. static int ide_task_ioctl(ide_drive_t *drive, unsigned cmd, unsigned long arg)
  171. {
  172. void __user *p = (void __user *)arg;
  173. int err = 0;
  174. u8 args[7];
  175. ide_task_t task;
  176. if (copy_from_user(args, p, 7))
  177. return -EFAULT;
  178. memset(&task, 0, sizeof(task));
  179. memcpy(&task.tf_array[7], &args[1], 6);
  180. task.tf.command = args[0];
  181. task.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
  182. err = ide_no_data_taskfile(drive, &task);
  183. args[0] = task.tf.command;
  184. memcpy(&args[1], &task.tf_array[7], 6);
  185. if (copy_to_user(p, args, 7))
  186. err = -EFAULT;
  187. return err;
  188. }
  189. static int generic_drive_reset(ide_drive_t *drive)
  190. {
  191. struct request *rq;
  192. int ret = 0;
  193. rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
  194. rq->cmd_type = REQ_TYPE_SPECIAL;
  195. rq->cmd_len = 1;
  196. rq->cmd[0] = REQ_DRIVE_RESET;
  197. rq->cmd_flags |= REQ_SOFTBARRIER;
  198. if (blk_execute_rq(drive->queue, NULL, rq, 1))
  199. ret = rq->errors;
  200. blk_put_request(rq);
  201. return ret;
  202. }
  203. int generic_ide_ioctl(ide_drive_t *drive, struct block_device *bdev,
  204. unsigned int cmd, unsigned long arg)
  205. {
  206. int err;
  207. err = ide_setting_ioctl(drive, bdev, cmd, arg, ide_ioctl_settings);
  208. if (err != -EOPNOTSUPP)
  209. return err;
  210. switch (cmd) {
  211. case HDIO_OBSOLETE_IDENTITY:
  212. case HDIO_GET_IDENTITY:
  213. if (bdev != bdev->bd_contains)
  214. return -EINVAL;
  215. return ide_get_identity_ioctl(drive, cmd, arg);
  216. case HDIO_GET_NICE:
  217. return ide_get_nice_ioctl(drive, arg);
  218. case HDIO_SET_NICE:
  219. if (!capable(CAP_SYS_ADMIN))
  220. return -EACCES;
  221. return ide_set_nice_ioctl(drive, arg);
  222. #ifdef CONFIG_IDE_TASK_IOCTL
  223. case HDIO_DRIVE_TASKFILE:
  224. if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
  225. return -EACCES;
  226. if (drive->media == ide_disk)
  227. return ide_taskfile_ioctl(drive, cmd, arg);
  228. return -ENOMSG;
  229. #endif
  230. case HDIO_DRIVE_CMD:
  231. if (!capable(CAP_SYS_RAWIO))
  232. return -EACCES;
  233. return ide_cmd_ioctl(drive, cmd, arg);
  234. case HDIO_DRIVE_TASK:
  235. if (!capable(CAP_SYS_RAWIO))
  236. return -EACCES;
  237. return ide_task_ioctl(drive, cmd, arg);
  238. case HDIO_DRIVE_RESET:
  239. if (!capable(CAP_SYS_ADMIN))
  240. return -EACCES;
  241. return generic_drive_reset(drive);
  242. case HDIO_GET_BUSSTATE:
  243. if (!capable(CAP_SYS_ADMIN))
  244. return -EACCES;
  245. if (put_user(BUSSTATE_ON, (long __user *)arg))
  246. return -EFAULT;
  247. return 0;
  248. case HDIO_SET_BUSSTATE:
  249. if (!capable(CAP_SYS_ADMIN))
  250. return -EACCES;
  251. return -EOPNOTSUPP;
  252. default:
  253. return -EINVAL;
  254. }
  255. }
  256. EXPORT_SYMBOL(generic_ide_ioctl);