Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[linux-block.git] / drivers / ide / ide-park.c
CommitLineData
4abdc6ee
EO
1#include <linux/kernel.h>
2#include <linux/ide.h>
3#include <linux/jiffies.h>
4#include <linux/blkdev.h>
5
6DECLARE_WAIT_QUEUE_HEAD(ide_park_wq);
7
8static void issue_park_cmd(ide_drive_t *drive, unsigned long timeout)
9{
b65fac32 10 ide_hwif_t *hwif = drive->hwif;
4abdc6ee
EO
11 struct request_queue *q = drive->queue;
12 struct request *rq;
13 int rc;
14
15 timeout += jiffies;
b65fac32 16 spin_lock_irq(&hwif->lock);
4abdc6ee 17 if (drive->dev_flags & IDE_DFLAG_PARKED) {
2a2ca6a9 18 int reset_timer = time_before(timeout, drive->sleep);
201bffa4 19 int start_queue = 0;
4abdc6ee 20
4abdc6ee
EO
21 drive->sleep = timeout;
22 wake_up_all(&ide_park_wq);
b65fac32 23 if (reset_timer && del_timer(&hwif->timer))
201bffa4 24 start_queue = 1;
b65fac32 25 spin_unlock_irq(&hwif->lock);
201bffa4 26
853280a4
TH
27 if (start_queue)
28 blk_run_queue(q);
4abdc6ee
EO
29 return;
30 }
b65fac32 31 spin_unlock_irq(&hwif->lock);
4abdc6ee
EO
32
33 rq = blk_get_request(q, READ, __GFP_WAIT);
34 rq->cmd[0] = REQ_PARK_HEADS;
35 rq->cmd_len = 1;
36 rq->cmd_type = REQ_TYPE_SPECIAL;
37 rq->special = &timeout;
38 rc = blk_execute_rq(q, NULL, rq, 1);
39 blk_put_request(rq);
40 if (rc)
41 goto out;
42
43 /*
44 * Make sure that *some* command is sent to the drive after the
45 * timeout has expired, so power management will be reenabled.
46 */
47 rq = blk_get_request(q, READ, GFP_NOWAIT);
48 if (unlikely(!rq))
49 goto out;
50
51 rq->cmd[0] = REQ_UNPARK_HEADS;
52 rq->cmd_len = 1;
53 rq->cmd_type = REQ_TYPE_SPECIAL;
54 elv_add_request(q, rq, ELEVATOR_INSERT_FRONT, 1);
55
56out:
57 return;
58}
59
c4e66c36
BZ
60ide_startstop_t ide_do_park_unpark(ide_drive_t *drive, struct request *rq)
61{
22aa4b32
BZ
62 struct ide_cmd cmd;
63 struct ide_taskfile *tf = &cmd.tf;
c4e66c36 64
22aa4b32 65 memset(&cmd, 0, sizeof(cmd));
c4e66c36
BZ
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;
70 tf->feature = 0x44;
71 tf->lbal = 0x4c;
72 tf->lbam = 0x4e;
73 tf->lbah = 0x55;
60f85019
SS
74 cmd.valid.out.tf = IDE_VALID_OUT_TF | IDE_VALID_DEVICE;
75 cmd.valid.in.tf = IDE_VALID_IN_TF | IDE_VALID_DEVICE;
c4e66c36
BZ
76 } else /* cmd == REQ_UNPARK_HEADS */
77 tf->command = ATA_CMD_CHK_POWER;
78
d364c7f5 79 cmd.tf_flags |= IDE_TFLAG_CUSTOM_HANDLER;
0dfb991c
BZ
80 cmd.protocol = ATA_PROT_NODATA;
81
22aa4b32 82 cmd.rq = rq;
22aa4b32
BZ
83
84 return do_rw_taskfile(drive, &cmd);
c4e66c36
BZ
85}
86
4abdc6ee
EO
87ssize_t ide_park_show(struct device *dev, struct device_attribute *attr,
88 char *buf)
89{
90 ide_drive_t *drive = to_ide_device(dev);
b65fac32 91 ide_hwif_t *hwif = drive->hwif;
4abdc6ee
EO
92 unsigned long now;
93 unsigned int msecs;
94
95 if (drive->dev_flags & IDE_DFLAG_NO_UNLOAD)
96 return -EOPNOTSUPP;
97
b65fac32 98 spin_lock_irq(&hwif->lock);
4abdc6ee
EO
99 now = jiffies;
100 if (drive->dev_flags & IDE_DFLAG_PARKED &&
101 time_after(drive->sleep, now))
102 msecs = jiffies_to_msecs(drive->sleep - now);
103 else
104 msecs = 0;
b65fac32 105 spin_unlock_irq(&hwif->lock);
4abdc6ee
EO
106
107 return snprintf(buf, 20, "%u\n", msecs);
108}
109
110ssize_t ide_park_store(struct device *dev, struct device_attribute *attr,
111 const char *buf, size_t len)
112{
113#define MAX_PARK_TIMEOUT 30000
114 ide_drive_t *drive = to_ide_device(dev);
115 long int input;
116 int rc;
117
118 rc = strict_strtol(buf, 10, &input);
119 if (rc || input < -2)
120 return -EINVAL;
121 if (input > MAX_PARK_TIMEOUT) {
122 input = MAX_PARK_TIMEOUT;
123 rc = -EOVERFLOW;
124 }
125
126 mutex_lock(&ide_setting_mtx);
127 if (input >= 0) {
128 if (drive->dev_flags & IDE_DFLAG_NO_UNLOAD)
129 rc = -EOPNOTSUPP;
130 else if (input || drive->dev_flags & IDE_DFLAG_PARKED)
131 issue_park_cmd(drive, msecs_to_jiffies(input));
132 } else {
133 if (drive->media == ide_disk)
134 switch (input) {
135 case -1:
136 drive->dev_flags &= ~IDE_DFLAG_NO_UNLOAD;
137 break;
138 case -2:
139 drive->dev_flags |= IDE_DFLAG_NO_UNLOAD;
140 break;
141 }
142 else
143 rc = -EOPNOTSUPP;
144 }
145 mutex_unlock(&ide_setting_mtx);
146
147 return rc ? rc : len;
148}