1 #include <linux/kernel.h>
3 #include <linux/jiffies.h>
4 #include <linux/blkdev.h>
6 DECLARE_WAIT_QUEUE_HEAD(ide_park_wq);
8 static void issue_park_cmd(ide_drive_t *drive, unsigned long timeout)
10 ide_hwif_t *hwif = drive->hwif;
11 struct request_queue *q = drive->queue;
16 spin_lock_irq(&hwif->lock);
17 if (drive->dev_flags & IDE_DFLAG_PARKED) {
18 int reset_timer = time_before(timeout, drive->sleep);
21 drive->sleep = timeout;
22 wake_up_all(&ide_park_wq);
23 if (reset_timer && del_timer(&hwif->timer))
25 spin_unlock_irq(&hwif->lock);
31 spin_unlock_irq(&hwif->lock);
33 rq = blk_get_request(q, READ, __GFP_WAIT);
34 rq->cmd[0] = REQ_PARK_HEADS;
36 rq->cmd_type = REQ_TYPE_SPECIAL;
37 rq->special = &timeout;
38 rc = blk_execute_rq(q, NULL, rq, 1);
44 * Make sure that *some* command is sent to the drive after the
45 * timeout has expired, so power management will be reenabled.
47 rq = blk_get_request(q, READ, GFP_NOWAIT);
51 rq->cmd[0] = REQ_UNPARK_HEADS;
53 rq->cmd_type = REQ_TYPE_SPECIAL;
54 elv_add_request(q, rq, ELEVATOR_INSERT_FRONT, 1);
60 ide_startstop_t ide_do_park_unpark(ide_drive_t *drive, struct request *rq)
63 struct ide_taskfile *tf = &cmd.tf;
65 memset(&cmd, 0, sizeof(cmd));
66 if (rq->cmd[0] == REQ_PARK_HEADS) {
67 drive->sleep = *(unsigned long *)rq->special;
68 drive->dev_flags |= IDE_DFLAG_SLEEPING;
69 tf->command = ATA_CMD_IDLEIMMEDIATE;
74 cmd.valid.out.tf = IDE_VALID_OUT_TF | IDE_VALID_DEVICE;
75 cmd.valid.in.tf = IDE_VALID_IN_TF | IDE_VALID_DEVICE;
76 } else /* cmd == REQ_UNPARK_HEADS */
77 tf->command = ATA_CMD_CHK_POWER;
79 cmd.tf_flags |= IDE_TFLAG_CUSTOM_HANDLER;
80 cmd.protocol = ATA_PROT_NODATA;
84 return do_rw_taskfile(drive, &cmd);
87 ssize_t ide_park_show(struct device *dev, struct device_attribute *attr,
90 ide_drive_t *drive = to_ide_device(dev);
91 ide_hwif_t *hwif = drive->hwif;
95 if (drive->dev_flags & IDE_DFLAG_NO_UNLOAD)
98 spin_lock_irq(&hwif->lock);
100 if (drive->dev_flags & IDE_DFLAG_PARKED &&
101 time_after(drive->sleep, now))
102 msecs = jiffies_to_msecs(drive->sleep - now);
105 spin_unlock_irq(&hwif->lock);
107 return snprintf(buf, 20, "%u\n", msecs);
110 ssize_t ide_park_store(struct device *dev, struct device_attribute *attr,
111 const char *buf, size_t len)
113 #define MAX_PARK_TIMEOUT 30000
114 ide_drive_t *drive = to_ide_device(dev);
118 rc = strict_strtol(buf, 10, &input);
119 if (rc || input < -2)
121 if (input > MAX_PARK_TIMEOUT) {
122 input = MAX_PARK_TIMEOUT;
126 mutex_lock(&ide_setting_mtx);
128 if (drive->dev_flags & IDE_DFLAG_NO_UNLOAD)
130 else if (input || drive->dev_flags & IDE_DFLAG_PARKED)
131 issue_park_cmd(drive, msecs_to_jiffies(input));
133 if (drive->media == ide_disk)
136 drive->dev_flags &= ~IDE_DFLAG_NO_UNLOAD;
139 drive->dev_flags |= IDE_DFLAG_NO_UNLOAD;
145 mutex_unlock(&ide_setting_mtx);
147 return rc ? rc : len;