mmc: core: Fix use of uninitialized data in mmc_send_if_cond.
[pandora-kernel.git] / drivers / mmc / core / sd_ops.c
1 /*
2  *  linux/drivers/mmc/core/sd_ops.h
3  *
4  *  Copyright 2006-2007 Pierre Ossman
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or (at
9  * your option) any later version.
10  */
11
12 #include <linux/slab.h>
13 #include <linux/types.h>
14 #include <linux/scatterlist.h>
15
16 #include <linux/mmc/host.h>
17 #include <linux/mmc/card.h>
18 #include <linux/mmc/mmc.h>
19 #include <linux/mmc/sd.h>
20
21 #include "core.h"
22 #include "sd_ops.h"
23
24 static int mmc_app_cmd(struct mmc_host *host, struct mmc_card *card)
25 {
26         int err;
27         struct mmc_command cmd;
28
29         BUG_ON(!host);
30         BUG_ON(card && (card->host != host));
31
32         memset(&cmd, 0, sizeof(struct mmc_command));
33
34         cmd.opcode = MMC_APP_CMD;
35
36         if (card) {
37                 cmd.arg = card->rca << 16;
38                 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
39         } else {
40                 cmd.arg = 0;
41                 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_BCR;
42         }
43
44         err = mmc_wait_for_cmd(host, &cmd, 0);
45         if (err)
46                 return err;
47
48         /* Check that card supported application commands */
49         if (!mmc_host_is_spi(host) && !(cmd.resp[0] & R1_APP_CMD))
50                 return -EOPNOTSUPP;
51
52         return 0;
53 }
54
55 /**
56  *      mmc_wait_for_app_cmd - start an application command and wait for
57                                completion
58  *      @host: MMC host to start command
59  *      @card: Card to send MMC_APP_CMD to
60  *      @cmd: MMC command to start
61  *      @retries: maximum number of retries
62  *
63  *      Sends a MMC_APP_CMD, checks the card response, sends the command
64  *      in the parameter and waits for it to complete. Return any error
65  *      that occurred while the command was executing.  Do not attempt to
66  *      parse the response.
67  */
68 int mmc_wait_for_app_cmd(struct mmc_host *host, struct mmc_card *card,
69         struct mmc_command *cmd, int retries)
70 {
71         struct mmc_request mrq;
72
73         int i, err;
74
75         BUG_ON(!cmd);
76         BUG_ON(retries < 0);
77
78         err = -EIO;
79
80         /*
81          * We have to resend MMC_APP_CMD for each attempt so
82          * we cannot use the retries field in mmc_command.
83          */
84         for (i = 0;i <= retries;i++) {
85                 err = mmc_app_cmd(host, card);
86                 if (err) {
87                         /* no point in retrying; no APP commands allowed */
88                         if (mmc_host_is_spi(host)) {
89                                 if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
90                                         break;
91                         }
92                         continue;
93                 }
94
95                 memset(&mrq, 0, sizeof(struct mmc_request));
96
97                 memset(cmd->resp, 0, sizeof(cmd->resp));
98                 cmd->retries = 0;
99
100                 mrq.cmd = cmd;
101                 cmd->data = NULL;
102
103                 mmc_wait_for_req(host, &mrq);
104
105                 err = cmd->error;
106                 if (!cmd->error)
107                         break;
108
109                 /* no point in retrying illegal APP commands */
110                 if (mmc_host_is_spi(host)) {
111                         if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
112                                 break;
113                 }
114         }
115
116         return err;
117 }
118
119 EXPORT_SYMBOL(mmc_wait_for_app_cmd);
120
121 int mmc_app_set_bus_width(struct mmc_card *card, int width)
122 {
123         int err;
124         struct mmc_command cmd;
125
126         BUG_ON(!card);
127         BUG_ON(!card->host);
128
129         memset(&cmd, 0, sizeof(struct mmc_command));
130
131         cmd.opcode = SD_APP_SET_BUS_WIDTH;
132         cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
133
134         switch (width) {
135         case MMC_BUS_WIDTH_1:
136                 cmd.arg = SD_BUS_WIDTH_1;
137                 break;
138         case MMC_BUS_WIDTH_4:
139                 cmd.arg = SD_BUS_WIDTH_4;
140                 break;
141         default:
142                 return -EINVAL;
143         }
144
145         err = mmc_wait_for_app_cmd(card->host, card, &cmd, MMC_CMD_RETRIES);
146         if (err)
147                 return err;
148
149         return 0;
150 }
151
152 int mmc_send_app_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
153 {
154         struct mmc_command cmd;
155         int i, err = 0;
156
157         BUG_ON(!host);
158
159         memset(&cmd, 0, sizeof(struct mmc_command));
160
161         cmd.opcode = SD_APP_OP_COND;
162         if (mmc_host_is_spi(host))
163                 cmd.arg = ocr & (1 << 30); /* SPI only defines one bit */
164         else
165                 cmd.arg = ocr;
166         cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R3 | MMC_CMD_BCR;
167
168         for (i = 100; i; i--) {
169                 err = mmc_wait_for_app_cmd(host, NULL, &cmd, MMC_CMD_RETRIES);
170                 if (err)
171                         break;
172
173                 /* if we're just probing, do a single pass */
174                 if (ocr == 0)
175                         break;
176
177                 /* otherwise wait until reset completes */
178                 if (mmc_host_is_spi(host)) {
179                         if (!(cmd.resp[0] & R1_SPI_IDLE))
180                                 break;
181                 } else {
182                         if (cmd.resp[0] & MMC_CARD_BUSY)
183                                 break;
184                 }
185
186                 err = -ETIMEDOUT;
187
188                 mmc_delay(10);
189         }
190
191         if (rocr && !mmc_host_is_spi(host))
192                 *rocr = cmd.resp[0];
193
194         return err;
195 }
196
197 int mmc_send_if_cond(struct mmc_host *host, u32 ocr)
198 {
199         struct mmc_command cmd;
200         int err;
201         static const u8 test_pattern = 0xAA;
202         u8 result_pattern;
203
204         memset(&cmd, 0, sizeof(struct mmc_command));
205
206         /*
207          * To support SD 2.0 cards, we must always invoke SD_SEND_IF_COND
208          * before SD_APP_OP_COND. This command will harmlessly fail for
209          * SD 1.0 cards.
210          */
211         cmd.opcode = SD_SEND_IF_COND;
212         cmd.arg = ((ocr & 0xFF8000) != 0) << 8 | test_pattern;
213         cmd.flags = MMC_RSP_SPI_R7 | MMC_RSP_R7 | MMC_CMD_BCR;
214
215         err = mmc_wait_for_cmd(host, &cmd, 0);
216         if (err)
217                 return err;
218
219         if (mmc_host_is_spi(host))
220                 result_pattern = cmd.resp[1] & 0xFF;
221         else
222                 result_pattern = cmd.resp[0] & 0xFF;
223
224         if (result_pattern != test_pattern)
225                 return -EIO;
226
227         return 0;
228 }
229
230 int mmc_send_relative_addr(struct mmc_host *host, unsigned int *rca)
231 {
232         int err;
233         struct mmc_command cmd;
234
235         BUG_ON(!host);
236         BUG_ON(!rca);
237
238         memset(&cmd, 0, sizeof(struct mmc_command));
239
240         cmd.opcode = SD_SEND_RELATIVE_ADDR;
241         cmd.arg = 0;
242         cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
243
244         err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
245         if (err)
246                 return err;
247
248         *rca = cmd.resp[0] >> 16;
249
250         return 0;
251 }
252
253 int mmc_app_send_scr(struct mmc_card *card, u32 *scr)
254 {
255         int err;
256         struct mmc_request mrq;
257         struct mmc_command cmd;
258         struct mmc_data data;
259         struct scatterlist sg;
260         void *data_buf;
261
262         BUG_ON(!card);
263         BUG_ON(!card->host);
264         BUG_ON(!scr);
265
266         /* NOTE: caller guarantees scr is heap-allocated */
267
268         err = mmc_app_cmd(card->host, card);
269         if (err)
270                 return err;
271
272         /* dma onto stack is unsafe/nonportable, but callers to this
273          * routine normally provide temporary on-stack buffers ...
274          */
275         data_buf = kmalloc(sizeof(card->raw_scr), GFP_KERNEL);
276         if (data_buf == NULL)
277                 return -ENOMEM;
278
279         memset(&mrq, 0, sizeof(struct mmc_request));
280         memset(&cmd, 0, sizeof(struct mmc_command));
281         memset(&data, 0, sizeof(struct mmc_data));
282
283         mrq.cmd = &cmd;
284         mrq.data = &data;
285
286         cmd.opcode = SD_APP_SEND_SCR;
287         cmd.arg = 0;
288         cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
289
290         data.blksz = 8;
291         data.blocks = 1;
292         data.flags = MMC_DATA_READ;
293         data.sg = &sg;
294         data.sg_len = 1;
295
296         sg_init_one(&sg, data_buf, 8);
297
298         mmc_set_data_timeout(&data, card);
299
300         mmc_wait_for_req(card->host, &mrq);
301
302         memcpy(scr, data_buf, sizeof(card->raw_scr));
303         kfree(data_buf);
304
305         if (cmd.error)
306                 return cmd.error;
307         if (data.error)
308                 return data.error;
309
310         scr[0] = be32_to_cpu(scr[0]);
311         scr[1] = be32_to_cpu(scr[1]);
312
313         return 0;
314 }
315
316 int mmc_sd_switch(struct mmc_card *card, int mode, int group,
317         u8 value, u8 *resp)
318 {
319         struct mmc_request mrq;
320         struct mmc_command cmd;
321         struct mmc_data data;
322         struct scatterlist sg;
323
324         BUG_ON(!card);
325         BUG_ON(!card->host);
326
327         /* NOTE: caller guarantees resp is heap-allocated */
328
329         mode = !!mode;
330         value &= 0xF;
331
332         memset(&mrq, 0, sizeof(struct mmc_request));
333         memset(&cmd, 0, sizeof(struct mmc_command));
334         memset(&data, 0, sizeof(struct mmc_data));
335
336         mrq.cmd = &cmd;
337         mrq.data = &data;
338
339         cmd.opcode = SD_SWITCH;
340         cmd.arg = mode << 31 | 0x00FFFFFF;
341         cmd.arg &= ~(0xF << (group * 4));
342         cmd.arg |= value << (group * 4);
343         cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
344
345         data.blksz = 64;
346         data.blocks = 1;
347         data.flags = MMC_DATA_READ;
348         data.sg = &sg;
349         data.sg_len = 1;
350
351         sg_init_one(&sg, resp, 64);
352
353         mmc_set_data_timeout(&data, card);
354
355         mmc_wait_for_req(card->host, &mrq);
356
357         if (cmd.error)
358                 return cmd.error;
359         if (data.error)
360                 return data.error;
361
362         return 0;
363 }
364
365 int mmc_app_sd_status(struct mmc_card *card, void *ssr)
366 {
367         int err;
368         struct mmc_request mrq;
369         struct mmc_command cmd;
370         struct mmc_data data;
371         struct scatterlist sg;
372
373         BUG_ON(!card);
374         BUG_ON(!card->host);
375         BUG_ON(!ssr);
376
377         /* NOTE: caller guarantees ssr is heap-allocated */
378
379         err = mmc_app_cmd(card->host, card);
380         if (err)
381                 return err;
382
383         memset(&mrq, 0, sizeof(struct mmc_request));
384         memset(&cmd, 0, sizeof(struct mmc_command));
385         memset(&data, 0, sizeof(struct mmc_data));
386
387         mrq.cmd = &cmd;
388         mrq.data = &data;
389
390         cmd.opcode = SD_APP_SD_STATUS;
391         cmd.arg = 0;
392         cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_ADTC;
393
394         data.blksz = 64;
395         data.blocks = 1;
396         data.flags = MMC_DATA_READ;
397         data.sg = &sg;
398         data.sg_len = 1;
399
400         sg_init_one(&sg, ssr, 64);
401
402         mmc_set_data_timeout(&data, card);
403
404         mmc_wait_for_req(card->host, &mrq);
405
406         if (cmd.error)
407                 return cmd.error;
408         if (data.error)
409                 return data.error;
410
411         return 0;
412 }