ide-devsets.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #include <linux/kernel.h>
  2. #include <linux/ide.h>
  3. DEFINE_MUTEX(ide_setting_mtx);
  4. ide_devset_get(io_32bit, io_32bit);
  5. static int set_io_32bit(ide_drive_t *drive, int arg)
  6. {
  7. if (drive->dev_flags & IDE_DFLAG_NO_IO_32BIT)
  8. return -EPERM;
  9. if (arg < 0 || arg > 1 + (SUPPORT_VLB_SYNC << 1))
  10. return -EINVAL;
  11. drive->io_32bit = arg;
  12. return 0;
  13. }
  14. ide_devset_get_flag(ksettings, IDE_DFLAG_KEEP_SETTINGS);
  15. static int set_ksettings(ide_drive_t *drive, int arg)
  16. {
  17. if (arg < 0 || arg > 1)
  18. return -EINVAL;
  19. if (arg)
  20. drive->dev_flags |= IDE_DFLAG_KEEP_SETTINGS;
  21. else
  22. drive->dev_flags &= ~IDE_DFLAG_KEEP_SETTINGS;
  23. return 0;
  24. }
  25. ide_devset_get_flag(using_dma, IDE_DFLAG_USING_DMA);
  26. static int set_using_dma(ide_drive_t *drive, int arg)
  27. {
  28. #ifdef CONFIG_BLK_DEV_IDEDMA
  29. int err = -EPERM;
  30. if (arg < 0 || arg > 1)
  31. return -EINVAL;
  32. if (ata_id_has_dma(drive->id) == 0)
  33. goto out;
  34. if (drive->hwif->dma_ops == NULL)
  35. goto out;
  36. err = 0;
  37. if (arg) {
  38. if (ide_set_dma(drive))
  39. err = -EIO;
  40. } else
  41. ide_dma_off(drive);
  42. out:
  43. return err;
  44. #else
  45. if (arg < 0 || arg > 1)
  46. return -EINVAL;
  47. return -EPERM;
  48. #endif
  49. }
  50. /*
  51. * handle HDIO_SET_PIO_MODE ioctl abusers here, eventually it will go away
  52. */
  53. static int set_pio_mode_abuse(ide_hwif_t *hwif, u8 req_pio)
  54. {
  55. switch (req_pio) {
  56. case 202:
  57. case 201:
  58. case 200:
  59. case 102:
  60. case 101:
  61. case 100:
  62. return (hwif->host_flags & IDE_HFLAG_ABUSE_DMA_MODES) ? 1 : 0;
  63. case 9:
  64. case 8:
  65. return (hwif->host_flags & IDE_HFLAG_ABUSE_PREFETCH) ? 1 : 0;
  66. case 7:
  67. case 6:
  68. return (hwif->host_flags & IDE_HFLAG_ABUSE_FAST_DEVSEL) ? 1 : 0;
  69. default:
  70. return 0;
  71. }
  72. }
  73. static int set_pio_mode(ide_drive_t *drive, int arg)
  74. {
  75. ide_hwif_t *hwif = drive->hwif;
  76. const struct ide_port_ops *port_ops = hwif->port_ops;
  77. if (arg < 0 || arg > 255)
  78. return -EINVAL;
  79. if (port_ops == NULL || port_ops->set_pio_mode == NULL ||
  80. (hwif->host_flags & IDE_HFLAG_NO_SET_MODE))
  81. return -ENOSYS;
  82. if (set_pio_mode_abuse(drive->hwif, arg)) {
  83. if (arg == 8 || arg == 9) {
  84. unsigned long flags;
  85. /* take lock for IDE_DFLAG_[NO_]UNMASK/[NO_]IO_32BIT */
  86. spin_lock_irqsave(&hwif->lock, flags);
  87. port_ops->set_pio_mode(drive, arg);
  88. spin_unlock_irqrestore(&hwif->lock, flags);
  89. } else
  90. port_ops->set_pio_mode(drive, arg);
  91. } else {
  92. int keep_dma = !!(drive->dev_flags & IDE_DFLAG_USING_DMA);
  93. ide_set_pio(drive, arg);
  94. if (hwif->host_flags & IDE_HFLAG_SET_PIO_MODE_KEEP_DMA) {
  95. if (keep_dma)
  96. ide_dma_on(drive);
  97. }
  98. }
  99. return 0;
  100. }
  101. ide_devset_get_flag(unmaskirq, IDE_DFLAG_UNMASK);
  102. static int set_unmaskirq(ide_drive_t *drive, int arg)
  103. {
  104. if (drive->dev_flags & IDE_DFLAG_NO_UNMASK)
  105. return -EPERM;
  106. if (arg < 0 || arg > 1)
  107. return -EINVAL;
  108. if (arg)
  109. drive->dev_flags |= IDE_DFLAG_UNMASK;
  110. else
  111. drive->dev_flags &= ~IDE_DFLAG_UNMASK;
  112. return 0;
  113. }
  114. ide_ext_devset_rw_sync(io_32bit, io_32bit);
  115. ide_ext_devset_rw_sync(keepsettings, ksettings);
  116. ide_ext_devset_rw_sync(unmaskirq, unmaskirq);
  117. ide_ext_devset_rw_sync(using_dma, using_dma);
  118. __IDE_DEVSET(pio_mode, DS_SYNC, NULL, set_pio_mode);
  119. int ide_devset_execute(ide_drive_t *drive, const struct ide_devset *setting,
  120. int arg)
  121. {
  122. struct request_queue *q = drive->queue;
  123. struct request *rq;
  124. int ret = 0;
  125. if (!(setting->flags & DS_SYNC))
  126. return setting->set(drive, arg);
  127. rq = blk_get_request(q, READ, __GFP_WAIT);
  128. rq->cmd_type = REQ_TYPE_SPECIAL;
  129. rq->cmd_len = 5;
  130. rq->cmd[0] = REQ_DEVSET_EXEC;
  131. *(int *)&rq->cmd[1] = arg;
  132. rq->special = setting->set;
  133. if (blk_execute_rq(q, NULL, rq, 0))
  134. ret = rq->errors;
  135. blk_put_request(rq);
  136. return ret;
  137. }
  138. ide_startstop_t ide_do_devset(ide_drive_t *drive, struct request *rq)
  139. {
  140. int err, (*setfunc)(ide_drive_t *, int) = rq->special;
  141. err = setfunc(drive, *(int *)&rq->cmd[1]);
  142. if (err)
  143. rq->errors = err;
  144. ide_complete_rq(drive, err, blk_rq_bytes(rq));
  145. return ide_stopped;
  146. }