refresh generic 2.6.23 patches
[oweals/openwrt.git] / target / linux / generic-2.6 / patches-2.6.23 / 300-add-mmc-spi-driver.patch
1 This is a port of the MMC-SPI driver from 2.6.24.3
2 --mb
3
4
5 Index: linux-2.6.23.17/drivers/mmc/host/Kconfig
6 ===================================================================
7 --- linux-2.6.23.17.orig/drivers/mmc/host/Kconfig
8 +++ linux-2.6.23.17/drivers/mmc/host/Kconfig
9 @@ -100,3 +100,16 @@ config MMC_TIFM_SD
10            To compile this driver as a module, choose M here: the
11           module will be called tifm_sd.
12  
13 +config MMC_SPI
14 +       tristate "MMC/SD over SPI (EXPERIMENTAL)"
15 +       depends on MMC && SPI_MASTER && !HIGHMEM && EXPERIMENTAL
16 +       select CRC7
17 +       select CRC_ITU_T
18 +       help
19 +         Some systems accss MMC/SD cards using a SPI controller instead of
20 +         using a "native" MMC/SD controller.  This has a disadvantage of
21 +         being relatively high overhead, but a compensating advantage of
22 +         working on many systems without dedicated MMC/SD controllers.
23 +
24 +         If unsure, or if your system has no SPI master driver, say N.
25 +
26 Index: linux-2.6.23.17/drivers/mmc/host/Makefile
27 ===================================================================
28 --- linux-2.6.23.17.orig/drivers/mmc/host/Makefile
29 +++ linux-2.6.23.17/drivers/mmc/host/Makefile
30 @@ -15,4 +15,5 @@ obj-$(CONFIG_MMC_AU1X)                += au1xmmc.o
31  obj-$(CONFIG_MMC_OMAP)         += omap.o
32  obj-$(CONFIG_MMC_AT91)         += at91_mci.o
33  obj-$(CONFIG_MMC_TIFM_SD)      += tifm_sd.o
34 +obj-$(CONFIG_MMC_SPI)          += mmc_spi.o
35  
36 Index: linux-2.6.23.17/drivers/mmc/host/mmc_spi.c
37 ===================================================================
38 --- /dev/null
39 +++ linux-2.6.23.17/drivers/mmc/host/mmc_spi.c
40 @@ -0,0 +1,1419 @@
41 +/*
42 + * mmc_spi.c - Access SD/MMC cards through SPI master controllers
43 + *
44 + * (C) Copyright 2005, Intec Automation,
45 + *             Mike Lavender (mike@steroidmicros)
46 + * (C) Copyright 2006-2007, David Brownell
47 + * (C) Copyright 2007, Axis Communications,
48 + *             Hans-Peter Nilsson (hp@axis.com)
49 + * (C) Copyright 2007, ATRON electronic GmbH,
50 + *             Jan Nikitenko <jan.nikitenko@gmail.com>
51 + *
52 + *
53 + * This program is free software; you can redistribute it and/or modify
54 + * it under the terms of the GNU General Public License as published by
55 + * the Free Software Foundation; either version 2 of the License, or
56 + * (at your option) any later version.
57 + *
58 + * This program is distributed in the hope that it will be useful,
59 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
60 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
61 + * GNU General Public License for more details.
62 + *
63 + * You should have received a copy of the GNU General Public License
64 + * along with this program; if not, write to the Free Software
65 + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
66 + */
67 +#include <linux/hrtimer.h>
68 +#include <linux/delay.h>
69 +#include <linux/bio.h>
70 +#include <linux/dma-mapping.h>
71 +#include <linux/crc7.h>
72 +#include <linux/crc-itu-t.h>
73 +#include <linux/scatterlist.h>
74 +
75 +#include <linux/mmc/host.h>
76 +#include <linux/mmc/mmc.h>             /* for R1_SPI_* bit values */
77 +
78 +#include <linux/spi/spi.h>
79 +#include <linux/spi/mmc_spi.h>
80 +
81 +#include <asm/unaligned.h>
82 +
83 +
84 +#define sg_page(sg)    (sg)->page
85 +
86 +
87 +/* NOTES:
88 + *
89 + * - For now, we won't try to interoperate with a real mmc/sd/sdio
90 + *   controller, although some of them do have hardware support for
91 + *   SPI protocol.  The main reason for such configs would be mmc-ish
92 + *   cards like DataFlash, which don't support that "native" protocol.
93 + *
94 + *   We don't have a "DataFlash/MMC/SD/SDIO card slot" abstraction to
95 + *   switch between driver stacks, and in any case if "native" mode
96 + *   is available, it will be faster and hence preferable.
97 + *
98 + * - MMC depends on a different chipselect management policy than the
99 + *   SPI interface currently supports for shared bus segments:  it needs
100 + *   to issue multiple spi_message requests with the chipselect active,
101 + *   using the results of one message to decide the next one to issue.
102 + *
103 + *   Pending updates to the programming interface, this driver expects
104 + *   that it not share the bus with other drivers (precluding conflicts).
105 + *
106 + * - We tell the controller to keep the chipselect active from the
107 + *   beginning of an mmc_host_ops.request until the end.  So beware
108 + *   of SPI controller drivers that mis-handle the cs_change flag!
109 + *
110 + *   However, many cards seem OK with chipselect flapping up/down
111 + *   during that time ... at least on unshared bus segments.
112 + */
113 +
114 +
115 +/*
116 + * Local protocol constants, internal to data block protocols.
117 + */
118 +
119 +/* Response tokens used to ack each block written: */
120 +#define SPI_MMC_RESPONSE_CODE(x)       ((x) & 0x1f)
121 +#define SPI_RESPONSE_ACCEPTED          ((2 << 1)|1)
122 +#define SPI_RESPONSE_CRC_ERR           ((5 << 1)|1)
123 +#define SPI_RESPONSE_WRITE_ERR         ((6 << 1)|1)
124 +
125 +/* Read and write blocks start with these tokens and end with crc;
126 + * on error, read tokens act like a subset of R2_SPI_* values.
127 + */
128 +#define SPI_TOKEN_SINGLE       0xfe    /* single block r/w, multiblock read */
129 +#define SPI_TOKEN_MULTI_WRITE  0xfc    /* multiblock write */
130 +#define SPI_TOKEN_STOP_TRAN    0xfd    /* terminate multiblock write */
131 +
132 +#define MMC_SPI_BLOCKSIZE      512
133 +
134 +
135 +/* These fixed timeouts come from the latest SD specs, which say to ignore
136 + * the CSD values.  The R1B value is for card erase (e.g. the "I forgot the
137 + * card's password" scenario); it's mostly applied to STOP_TRANSMISSION after
138 + * reads which takes nowhere near that long.  Older cards may be able to use
139 + * shorter timeouts ... but why bother?
140 + */
141 +#define readblock_timeout      ktime_set(0, 100 * 1000 * 1000)
142 +#define writeblock_timeout     ktime_set(0, 250 * 1000 * 1000)
143 +#define r1b_timeout            ktime_set(3, 0)
144 +
145 +
146 +/****************************************************************************/
147 +
148 +/*
149 + * Local Data Structures
150 + */
151 +
152 +/* "scratch" is per-{command,block} data exchanged with the card */
153 +struct scratch {
154 +       u8                      status[29];
155 +       u8                      data_token;
156 +       __be16                  crc_val;
157 +};
158 +
159 +struct mmc_spi_host {
160 +       struct mmc_host         *mmc;
161 +       struct spi_device       *spi;
162 +
163 +       unsigned char           power_mode;
164 +       u16                     powerup_msecs;
165 +
166 +       struct mmc_spi_platform_data    *pdata;
167 +
168 +       /* for bulk data transfers */
169 +       struct spi_transfer     token, t, crc, early_status;
170 +       struct spi_message      m;
171 +
172 +       /* for status readback */
173 +       struct spi_transfer     status;
174 +       struct spi_message      readback;
175 +
176 +       /* underlying DMA-aware controller, or null */
177 +       struct device           *dma_dev;
178 +
179 +       /* buffer used for commands and for message "overhead" */
180 +       struct scratch          *data;
181 +       dma_addr_t              data_dma;
182 +
183 +       /* Specs say to write ones most of the time, even when the card
184 +        * has no need to read its input data; and many cards won't care.
185 +        * This is our source of those ones.
186 +        */
187 +       void                    *ones;
188 +       dma_addr_t              ones_dma;
189 +};
190 +
191 +
192 +/****************************************************************************/
193 +
194 +/*
195 + * MMC-over-SPI protocol glue, used by the MMC stack interface
196 + */
197 +
198 +static inline int mmc_cs_off(struct mmc_spi_host *host)
199 +{
200 +       /* chipselect will always be inactive after setup() */
201 +       return spi_setup(host->spi);
202 +}
203 +
204 +static int
205 +mmc_spi_readbytes(struct mmc_spi_host *host, unsigned len)
206 +{
207 +       int status;
208 +
209 +       if (len > sizeof(*host->data)) {
210 +               WARN_ON(1);
211 +               return -EIO;
212 +       }
213 +
214 +       host->status.len = len;
215 +
216 +       if (host->dma_dev)
217 +               dma_sync_single_for_device(host->dma_dev,
218 +                               host->data_dma, sizeof(*host->data),
219 +                               DMA_FROM_DEVICE);
220 +
221 +       status = spi_sync(host->spi, &host->readback);
222 +
223 +       if (host->dma_dev)
224 +               dma_sync_single_for_cpu(host->dma_dev,
225 +                               host->data_dma, sizeof(*host->data),
226 +                               DMA_FROM_DEVICE);
227 +
228 +       return status;
229 +}
230 +
231 +static int
232 +mmc_spi_skip(struct mmc_spi_host *host, ktime_t timeout, unsigned n, u8 byte)
233 +{
234 +       u8              *cp = host->data->status;
235 +
236 +       timeout = ktime_add(timeout, ktime_get());
237 +
238 +       while (1) {
239 +               int             status;
240 +               unsigned        i;
241 +
242 +               status = mmc_spi_readbytes(host, n);
243 +               if (status < 0)
244 +                       return status;
245 +
246 +               for (i = 0; i < n; i++) {
247 +                       if (cp[i] != byte)
248 +                               return cp[i];
249 +               }
250 +
251 +               /* REVISIT investigate msleep() to avoid busy-wait I/O
252 +                * in at least some cases.
253 +                */
254 +               if (ktime_to_ns(ktime_sub(ktime_get(), timeout)) > 0)
255 +                       break;
256 +       }
257 +       return -ETIMEDOUT;
258 +}
259 +
260 +static inline int
261 +mmc_spi_wait_unbusy(struct mmc_spi_host *host, ktime_t timeout)
262 +{
263 +       return mmc_spi_skip(host, timeout, sizeof(host->data->status), 0);
264 +}
265 +
266 +static int mmc_spi_readtoken(struct mmc_spi_host *host)
267 +{
268 +       return mmc_spi_skip(host, readblock_timeout, 1, 0xff);
269 +}
270 +
271 +
272 +/*
273 + * Note that for SPI, cmd->resp[0] is not the same data as "native" protocol
274 + * hosts return!  The low byte holds R1_SPI bits.  The next byte may hold
275 + * R2_SPI bits ... for SEND_STATUS, or after data read errors.
276 + *
277 + * cmd->resp[1] holds any four-byte response, for R3 (READ_OCR) and on
278 + * newer cards R7 (IF_COND).
279 + */
280 +
281 +static char *maptype(struct mmc_command *cmd)
282 +{
283 +       switch (mmc_spi_resp_type(cmd)) {
284 +       case MMC_RSP_SPI_R1:    return "R1";
285 +       case MMC_RSP_SPI_R1B:   return "R1B";
286 +       case MMC_RSP_SPI_R2:    return "R2/R5";
287 +       case MMC_RSP_SPI_R3:    return "R3/R4/R7";
288 +       default:                return "?";
289 +       }
290 +}
291 +
292 +/* return zero, else negative errno after setting cmd->error */
293 +static int mmc_spi_response_get(struct mmc_spi_host *host,
294 +               struct mmc_command *cmd, int cs_on)
295 +{
296 +       u8      *cp = host->data->status;
297 +       u8      *end = cp + host->t.len;
298 +       int     value = 0;
299 +       char    tag[32];
300 +
301 +       snprintf(tag, sizeof(tag), "  ... CMD%d response SPI_%s",
302 +               cmd->opcode, maptype(cmd));
303 +
304 +       /* Except for data block reads, the whole response will already
305 +        * be stored in the scratch buffer.  It's somewhere after the
306 +        * command and the first byte we read after it.  We ignore that
307 +        * first byte.  After STOP_TRANSMISSION command it may include
308 +        * two data bits, but otherwise it's all ones.
309 +        */
310 +       cp += 8;
311 +       while (cp < end && *cp == 0xff)
312 +               cp++;
313 +
314 +       /* Data block reads (R1 response types) may need more data... */
315 +       if (cp == end) {
316 +               unsigned        i;
317 +
318 +               cp = host->data->status;
319 +
320 +               /* Card sends N(CR) (== 1..8) bytes of all-ones then one
321 +                * status byte ... and we already scanned 2 bytes.
322 +                *
323 +                * REVISIT block read paths use nasty byte-at-a-time I/O
324 +                * so it can always DMA directly into the target buffer.
325 +                * It'd probably be better to memcpy() the first chunk and
326 +                * avoid extra i/o calls...
327 +                */
328 +               for (i = 2; i < 9; i++) {
329 +                       value = mmc_spi_readbytes(host, 1);
330 +                       if (value < 0)
331 +                               goto done;
332 +                       if (*cp != 0xff)
333 +                               goto checkstatus;
334 +               }
335 +               value = -ETIMEDOUT;
336 +               goto done;
337 +       }
338 +
339 +checkstatus:
340 +       if (*cp & 0x80) {
341 +               dev_dbg(&host->spi->dev, "%s: INVALID RESPONSE, %02x\n",
342 +                                       tag, *cp);
343 +               value = -EBADR;
344 +               goto done;
345 +       }
346 +
347 +       cmd->resp[0] = *cp++;
348 +       cmd->error = 0;
349 +
350 +       /* Status byte: the entire seven-bit R1 response.  */
351 +       if (cmd->resp[0] != 0) {
352 +               if ((R1_SPI_PARAMETER | R1_SPI_ADDRESS
353 +                                       | R1_SPI_ILLEGAL_COMMAND)
354 +                               & cmd->resp[0])
355 +                       value = -EINVAL;
356 +               else if (R1_SPI_COM_CRC & cmd->resp[0])
357 +                       value = -EILSEQ;
358 +               else if ((R1_SPI_ERASE_SEQ | R1_SPI_ERASE_RESET)
359 +                               & cmd->resp[0])
360 +                       value = -EIO;
361 +               /* else R1_SPI_IDLE, "it's resetting" */
362 +       }
363 +
364 +       switch (mmc_spi_resp_type(cmd)) {
365 +
366 +       /* SPI R1B == R1 + busy; STOP_TRANSMISSION (for multiblock reads)
367 +        * and less-common stuff like various erase operations.
368 +        */
369 +       case MMC_RSP_SPI_R1B:
370 +               /* maybe we read all the busy tokens already */
371 +               while (cp < end && *cp == 0)
372 +                       cp++;
373 +               if (cp == end)
374 +                       mmc_spi_wait_unbusy(host, r1b_timeout);
375 +               break;
376 +
377 +       /* SPI R2 == R1 + second status byte; SEND_STATUS
378 +        * SPI R5 == R1 + data byte; IO_RW_DIRECT
379 +        */
380 +       case MMC_RSP_SPI_R2:
381 +               cmd->resp[0] |= *cp << 8;
382 +               break;
383 +
384 +       /* SPI R3, R4, or R7 == R1 + 4 bytes */
385 +       case MMC_RSP_SPI_R3:
386 +               cmd->resp[1] = be32_to_cpu(get_unaligned((u32 *)cp));
387 +               break;
388 +
389 +       /* SPI R1 == just one status byte */
390 +       case MMC_RSP_SPI_R1:
391 +               break;
392 +
393 +       default:
394 +               dev_dbg(&host->spi->dev, "bad response type %04x\n",
395 +                               mmc_spi_resp_type(cmd));
396 +               if (value >= 0)
397 +                       value = -EINVAL;
398 +               goto done;
399 +       }
400 +
401 +       if (value < 0)
402 +               dev_dbg(&host->spi->dev, "%s: resp %04x %08x\n",
403 +                       tag, cmd->resp[0], cmd->resp[1]);
404 +
405 +       /* disable chipselect on errors and some success cases */
406 +       if (value >= 0 && cs_on)
407 +               return value;
408 +done:
409 +       if (value < 0)
410 +               cmd->error = value;
411 +       mmc_cs_off(host);
412 +       return value;
413 +}
414 +
415 +/* Issue command and read its response.
416 + * Returns zero on success, negative for error.
417 + *
418 + * On error, caller must cope with mmc core retry mechanism.  That
419 + * means immediate low-level resubmit, which affects the bus lock...
420 + */
421 +static int
422 +mmc_spi_command_send(struct mmc_spi_host *host,
423 +               struct mmc_request *mrq,
424 +               struct mmc_command *cmd, int cs_on)
425 +{
426 +       struct scratch          *data = host->data;
427 +       u8                      *cp = data->status;
428 +       u32                     arg = cmd->arg;
429 +       int                     status;
430 +       struct spi_transfer     *t;
431 +
432 +       /* We can handle most commands (except block reads) in one full
433 +        * duplex I/O operation before either starting the next transfer
434 +        * (data block or command) or else deselecting the card.
435 +        *
436 +        * First, write 7 bytes:
437 +        *  - an all-ones byte to ensure the card is ready
438 +        *  - opcode byte (plus start and transmission bits)
439 +        *  - four bytes of big-endian argument
440 +        *  - crc7 (plus end bit) ... always computed, it's cheap
441 +        *
442 +        * We init the whole buffer to all-ones, which is what we need
443 +        * to write while we're reading (later) response data.
444 +        */
445 +       memset(cp++, 0xff, sizeof(data->status));
446 +
447 +       *cp++ = 0x40 | cmd->opcode;
448 +       *cp++ = (u8)(arg >> 24);
449 +       *cp++ = (u8)(arg >> 16);
450 +       *cp++ = (u8)(arg >> 8);
451 +       *cp++ = (u8)arg;
452 +       *cp++ = (crc7(0, &data->status[1], 5) << 1) | 0x01;
453 +
454 +       /* Then, read up to 13 bytes (while writing all-ones):
455 +        *  - N(CR) (== 1..8) bytes of all-ones
456 +        *  - status byte (for all response types)
457 +        *  - the rest of the response, either:
458 +        *      + nothing, for R1 or R1B responses
459 +        *      + second status byte, for R2 responses
460 +        *      + four data bytes, for R3 and R7 responses
461 +        *
462 +        * Finally, read some more bytes ... in the nice cases we know in
463 +        * advance how many, and reading 1 more is always OK:
464 +        *  - N(EC) (== 0..N) bytes of all-ones, before deselect/finish
465 +        *  - N(RC) (== 1..N) bytes of all-ones, before next command
466 +        *  - N(WR) (== 1..N) bytes of all-ones, before data write
467 +        *
468 +        * So in those cases one full duplex I/O of at most 21 bytes will
469 +        * handle the whole command, leaving the card ready to receive a
470 +        * data block or new command.  We do that whenever we can, shaving
471 +        * CPU and IRQ costs (especially when using DMA or FIFOs).
472 +        *
473 +        * There are two other cases, where it's not generally practical
474 +        * to rely on a single I/O:
475 +        *
476 +        *  - R1B responses need at least N(EC) bytes of all-zeroes.
477 +        *
478 +        *    In this case we can *try* to fit it into one I/O, then
479 +        *    maybe read more data later.
480 +        *
481 +        *  - Data block reads are more troublesome, since a variable
482 +        *    number of padding bytes precede the token and data.
483 +        *      + N(CX) (== 0..8) bytes of all-ones, before CSD or CID
484 +        *      + N(AC) (== 1..many) bytes of all-ones
485 +        *
486 +        *    In this case we currently only have minimal speedups here:
487 +        *    when N(CR) == 1 we can avoid I/O in response_get().
488 +        */
489 +       if (cs_on && (mrq->data->flags & MMC_DATA_READ)) {
490 +               cp += 2;        /* min(N(CR)) + status */
491 +               /* R1 */
492 +       } else {
493 +               cp += 10;       /* max(N(CR)) + status + min(N(RC),N(WR)) */
494 +               if (cmd->flags & MMC_RSP_SPI_S2)        /* R2/R5 */
495 +                       cp++;
496 +               else if (cmd->flags & MMC_RSP_SPI_B4)   /* R3/R4/R7 */
497 +                       cp += 4;
498 +               else if (cmd->flags & MMC_RSP_BUSY)     /* R1B */
499 +                       cp = data->status + sizeof(data->status);
500 +               /* else:  R1 (most commands) */
501 +       }
502 +
503 +       dev_dbg(&host->spi->dev, "  mmc_spi: CMD%d, resp %s\n",
504 +               cmd->opcode, maptype(cmd));
505 +
506 +       /* send command, leaving chipselect active */
507 +       spi_message_init(&host->m);
508 +
509 +       t = &host->t;
510 +       memset(t, 0, sizeof(*t));
511 +       t->tx_buf = t->rx_buf = data->status;
512 +       t->tx_dma = t->rx_dma = host->data_dma;
513 +       t->len = cp - data->status;
514 +       t->cs_change = 1;
515 +       spi_message_add_tail(t, &host->m);
516 +
517 +       if (host->dma_dev) {
518 +               host->m.is_dma_mapped = 1;
519 +               dma_sync_single_for_device(host->dma_dev,
520 +                               host->data_dma, sizeof(*host->data),
521 +                               DMA_BIDIRECTIONAL);
522 +       }
523 +       status = spi_sync(host->spi, &host->m);
524 +
525 +       if (host->dma_dev)
526 +               dma_sync_single_for_cpu(host->dma_dev,
527 +                               host->data_dma, sizeof(*host->data),
528 +                               DMA_BIDIRECTIONAL);
529 +       if (status < 0) {
530 +               dev_dbg(&host->spi->dev, "  ... write returned %d\n", status);
531 +               cmd->error = status;
532 +               return status;
533 +       }
534 +
535 +       /* after no-data commands and STOP_TRANSMISSION, chipselect off */
536 +       return mmc_spi_response_get(host, cmd, cs_on);
537 +}
538 +
539 +/* Build data message with up to four separate transfers.  For TX, we
540 + * start by writing the data token.  And in most cases, we finish with
541 + * a status transfer.
542 + *
543 + * We always provide TX data for data and CRC.  The MMC/SD protocol
544 + * requires us to write ones; but Linux defaults to writing zeroes;
545 + * so we explicitly initialize it to all ones on RX paths.
546 + *
547 + * We also handle DMA mapping, so the underlying SPI controller does
548 + * not need to (re)do it for each message.
549 + */
550 +static void
551 +mmc_spi_setup_data_message(
552 +       struct mmc_spi_host     *host,
553 +       int                     multiple,
554 +       enum dma_data_direction direction)
555 +{
556 +       struct spi_transfer     *t;
557 +       struct scratch          *scratch = host->data;
558 +       dma_addr_t              dma = host->data_dma;
559 +
560 +       spi_message_init(&host->m);
561 +       if (dma)
562 +               host->m.is_dma_mapped = 1;
563 +
564 +       /* for reads, readblock() skips 0xff bytes before finding
565 +        * the token; for writes, this transfer issues that token.
566 +        */
567 +       if (direction == DMA_TO_DEVICE) {
568 +               t = &host->token;
569 +               memset(t, 0, sizeof(*t));
570 +               t->len = 1;
571 +               if (multiple)
572 +                       scratch->data_token = SPI_TOKEN_MULTI_WRITE;
573 +               else
574 +                       scratch->data_token = SPI_TOKEN_SINGLE;
575 +               t->tx_buf = &scratch->data_token;
576 +               if (dma)
577 +                       t->tx_dma = dma + offsetof(struct scratch, data_token);
578 +               spi_message_add_tail(t, &host->m);
579 +       }
580 +
581 +       /* Body of transfer is buffer, then CRC ...
582 +        * either TX-only, or RX with TX-ones.
583 +        */
584 +       t = &host->t;
585 +       memset(t, 0, sizeof(*t));
586 +       t->tx_buf = host->ones;
587 +       t->tx_dma = host->ones_dma;
588 +       /* length and actual buffer info are written later */
589 +       spi_message_add_tail(t, &host->m);
590 +
591 +       t = &host->crc;
592 +       memset(t, 0, sizeof(*t));
593 +       t->len = 2;
594 +       if (direction == DMA_TO_DEVICE) {
595 +               /* the actual CRC may get written later */
596 +               t->tx_buf = &scratch->crc_val;
597 +               if (dma)
598 +                       t->tx_dma = dma + offsetof(struct scratch, crc_val);
599 +       } else {
600 +               t->tx_buf = host->ones;
601 +               t->tx_dma = host->ones_dma;
602 +               t->rx_buf = &scratch->crc_val;
603 +               if (dma)
604 +                       t->rx_dma = dma + offsetof(struct scratch, crc_val);
605 +       }
606 +       spi_message_add_tail(t, &host->m);
607 +
608 +       /*
609 +        * A single block read is followed by N(EC) [0+] all-ones bytes
610 +        * before deselect ... don't bother.
611 +        *
612 +        * Multiblock reads are followed by N(AC) [1+] all-ones bytes before
613 +        * the next block is read, or a STOP_TRANSMISSION is issued.  We'll
614 +        * collect that single byte, so readblock() doesn't need to.
615 +        *
616 +        * For a write, the one-byte data response follows immediately, then
617 +        * come zero or more busy bytes, then N(WR) [1+] all-ones bytes.
618 +        * Then single block reads may deselect, and multiblock ones issue
619 +        * the next token (next data block, or STOP_TRAN).  We can try to
620 +        * minimize I/O ops by using a single read to collect end-of-busy.
621 +        */
622 +       if (multiple || direction == DMA_TO_DEVICE) {
623 +               t = &host->early_status;
624 +               memset(t, 0, sizeof(*t));
625 +               t->len = (direction == DMA_TO_DEVICE)
626 +                               ? sizeof(scratch->status)
627 +                               : 1;
628 +               t->tx_buf = host->ones;
629 +               t->tx_dma = host->ones_dma;
630 +               t->rx_buf = scratch->status;
631 +               if (dma)
632 +                       t->rx_dma = dma + offsetof(struct scratch, status);
633 +               t->cs_change = 1;
634 +               spi_message_add_tail(t, &host->m);
635 +       }
636 +}
637 +
638 +/*
639 + * Write one block:
640 + *  - caller handled preceding N(WR) [1+] all-ones bytes
641 + *  - data block
642 + *     + token
643 + *     + data bytes
644 + *     + crc16
645 + *  - an all-ones byte ... card writes a data-response byte
646 + *  - followed by N(EC) [0+] all-ones bytes, card writes zero/'busy'
647 + *
648 + * Return negative errno, else success.
649 + */
650 +static int
651 +mmc_spi_writeblock(struct mmc_spi_host *host, struct spi_transfer *t)
652 +{
653 +       struct spi_device       *spi = host->spi;
654 +       int                     status, i;
655 +       struct scratch          *scratch = host->data;
656 +
657 +       if (host->mmc->use_spi_crc)
658 +               scratch->crc_val = cpu_to_be16(
659 +                               crc_itu_t(0, t->tx_buf, t->len));
660 +       if (host->dma_dev)
661 +               dma_sync_single_for_device(host->dma_dev,
662 +                               host->data_dma, sizeof(*scratch),
663 +                               DMA_BIDIRECTIONAL);
664 +
665 +       status = spi_sync(spi, &host->m);
666 +
667 +       if (status != 0) {
668 +               dev_dbg(&spi->dev, "write error (%d)\n", status);
669 +               return status;
670 +       }
671 +
672 +       if (host->dma_dev)
673 +               dma_sync_single_for_cpu(host->dma_dev,
674 +                               host->data_dma, sizeof(*scratch),
675 +                               DMA_BIDIRECTIONAL);
676 +
677 +       /*
678 +        * Get the transmission data-response reply.  It must follow
679 +        * immediately after the data block we transferred.  This reply
680 +        * doesn't necessarily tell whether the write operation succeeded;
681 +        * it just says if the transmission was ok and whether *earlier*
682 +        * writes succeeded; see the standard.
683 +        */
684 +       switch (SPI_MMC_RESPONSE_CODE(scratch->status[0])) {
685 +       case SPI_RESPONSE_ACCEPTED:
686 +               status = 0;
687 +               break;
688 +       case SPI_RESPONSE_CRC_ERR:
689 +               /* host shall then issue MMC_STOP_TRANSMISSION */
690 +               status = -EILSEQ;
691 +               break;
692 +       case SPI_RESPONSE_WRITE_ERR:
693 +               /* host shall then issue MMC_STOP_TRANSMISSION,
694 +                * and should MMC_SEND_STATUS to sort it out
695 +                */
696 +               status = -EIO;
697 +               break;
698 +       default:
699 +               status = -EPROTO;
700 +               break;
701 +       }
702 +       if (status != 0) {
703 +               dev_dbg(&spi->dev, "write error %02x (%d)\n",
704 +                       scratch->status[0], status);
705 +               return status;
706 +       }
707 +
708 +       t->tx_buf += t->len;
709 +       if (host->dma_dev)
710 +               t->tx_dma += t->len;
711 +
712 +       /* Return when not busy.  If we didn't collect that status yet,
713 +        * we'll need some more I/O.
714 +        */
715 +       for (i = 1; i < sizeof(scratch->status); i++) {
716 +               if (scratch->status[i] != 0)
717 +                       return 0;
718 +       }
719 +       return mmc_spi_wait_unbusy(host, writeblock_timeout);
720 +}
721 +
722 +/*
723 + * Read one block:
724 + *  - skip leading all-ones bytes ... either
725 + *      + N(AC) [1..f(clock,CSD)] usually, else
726 + *      + N(CX) [0..8] when reading CSD or CID
727 + *  - data block
728 + *     + token ... if error token, no data or crc
729 + *     + data bytes
730 + *     + crc16
731 + *
732 + * After single block reads, we're done; N(EC) [0+] all-ones bytes follow
733 + * before dropping chipselect.
734 + *
735 + * For multiblock reads, caller either reads the next block or issues a
736 + * STOP_TRANSMISSION command.
737 + */
738 +static int
739 +mmc_spi_readblock(struct mmc_spi_host *host, struct spi_transfer *t)
740 +{
741 +       struct spi_device       *spi = host->spi;
742 +       int                     status;
743 +       struct scratch          *scratch = host->data;
744 +
745 +       /* At least one SD card sends an all-zeroes byte when N(CX)
746 +        * applies, before the all-ones bytes ... just cope with that.
747 +        */
748 +       status = mmc_spi_readbytes(host, 1);
749 +       if (status < 0)
750 +               return status;
751 +       status = scratch->status[0];
752 +       if (status == 0xff || status == 0)
753 +               status = mmc_spi_readtoken(host);
754 +
755 +       if (status == SPI_TOKEN_SINGLE) {
756 +               if (host->dma_dev) {
757 +                       dma_sync_single_for_device(host->dma_dev,
758 +                                       host->data_dma, sizeof(*scratch),
759 +                                       DMA_BIDIRECTIONAL);
760 +                       dma_sync_single_for_device(host->dma_dev,
761 +                                       t->rx_dma, t->len,
762 +                                       DMA_FROM_DEVICE);
763 +               }
764 +
765 +               status = spi_sync(spi, &host->m);
766 +
767 +               if (host->dma_dev) {
768 +                       dma_sync_single_for_cpu(host->dma_dev,
769 +                                       host->data_dma, sizeof(*scratch),
770 +                                       DMA_BIDIRECTIONAL);
771 +                       dma_sync_single_for_cpu(host->dma_dev,
772 +                                       t->rx_dma, t->len,
773 +                                       DMA_FROM_DEVICE);
774 +               }
775 +
776 +       } else {
777 +               dev_dbg(&spi->dev, "read error %02x (%d)\n", status, status);
778 +
779 +               /* we've read extra garbage, timed out, etc */
780 +               if (status < 0)
781 +                       return status;
782 +
783 +               /* low four bits are an R2 subset, fifth seems to be
784 +                * vendor specific ... map them all to generic error..
785 +                */
786 +               return -EIO;
787 +       }
788 +
789 +       if (host->mmc->use_spi_crc) {
790 +               u16 crc = crc_itu_t(0, t->rx_buf, t->len);
791 +
792 +               be16_to_cpus(&scratch->crc_val);
793 +               if (scratch->crc_val != crc) {
794 +                       dev_dbg(&spi->dev, "read - crc error: crc_val=0x%04x, "
795 +                                       "computed=0x%04x len=%d\n",
796 +                                       scratch->crc_val, crc, t->len);
797 +                       return -EILSEQ;
798 +               }
799 +       }
800 +
801 +       t->rx_buf += t->len;
802 +       if (host->dma_dev)
803 +               t->rx_dma += t->len;
804 +
805 +       return 0;
806 +}
807 +
808 +/*
809 + * An MMC/SD data stage includes one or more blocks, optional CRCs,
810 + * and inline handshaking.  That handhaking makes it unlike most
811 + * other SPI protocol stacks.
812 + */
813 +static void
814 +mmc_spi_data_do(struct mmc_spi_host *host, struct mmc_command *cmd,
815 +               struct mmc_data *data, u32 blk_size)
816 +{
817 +       struct spi_device       *spi = host->spi;
818 +       struct device           *dma_dev = host->dma_dev;
819 +       struct spi_transfer     *t;
820 +       enum dma_data_direction direction;
821 +       struct scatterlist      *sg;
822 +       unsigned                n_sg;
823 +       int                     multiple = (data->blocks > 1);
824 +
825 +       if (data->flags & MMC_DATA_READ)
826 +               direction = DMA_FROM_DEVICE;
827 +       else
828 +               direction = DMA_TO_DEVICE;
829 +       mmc_spi_setup_data_message(host, multiple, direction);
830 +       t = &host->t;
831 +
832 +       /* Handle scatterlist segments one at a time, with synch for
833 +        * each 512-byte block
834 +        */
835 +       for (sg = data->sg, n_sg = data->sg_len; n_sg; n_sg--, sg++) {
836 +               int                     status = 0;
837 +               dma_addr_t              dma_addr = 0;
838 +               void                    *kmap_addr;
839 +               unsigned                length = sg->length;
840 +               enum dma_data_direction dir = direction;
841 +
842 +               /* set up dma mapping for controller drivers that might
843 +                * use DMA ... though they may fall back to PIO
844 +                */
845 +               if (dma_dev) {
846 +                       /* never invalidate whole *shared* pages ... */
847 +                       if ((sg->offset != 0 || length != PAGE_SIZE)
848 +                                       && dir == DMA_FROM_DEVICE)
849 +                               dir = DMA_BIDIRECTIONAL;
850 +
851 +                       dma_addr = dma_map_page(dma_dev, sg_page(sg), 0,
852 +                                               PAGE_SIZE, dir);
853 +                       if (direction == DMA_TO_DEVICE)
854 +                               t->tx_dma = dma_addr + sg->offset;
855 +                       else
856 +                               t->rx_dma = dma_addr + sg->offset;
857 +               }
858 +
859 +               /* allow pio too; we don't allow highmem */
860 +               kmap_addr = kmap(sg_page(sg));
861 +               if (direction == DMA_TO_DEVICE)
862 +                       t->tx_buf = kmap_addr + sg->offset;
863 +               else
864 +                       t->rx_buf = kmap_addr + sg->offset;
865 +
866 +               /* transfer each block, and update request status */
867 +               while (length) {
868 +                       t->len = min(length, blk_size);
869 +
870 +                       dev_dbg(&host->spi->dev,
871 +                               "    mmc_spi: %s block, %d bytes\n",
872 +                               (direction == DMA_TO_DEVICE)
873 +                               ? "write"
874 +                               : "read",
875 +                               t->len);
876 +
877 +                       if (direction == DMA_TO_DEVICE)
878 +                               status = mmc_spi_writeblock(host, t);
879 +                       else
880 +                               status = mmc_spi_readblock(host, t);
881 +                       if (status < 0)
882 +                               break;
883 +
884 +                       data->bytes_xfered += t->len;
885 +                       length -= t->len;
886 +
887 +                       if (!multiple)
888 +                               break;
889 +               }
890 +
891 +               /* discard mappings */
892 +               if (direction == DMA_FROM_DEVICE)
893 +                       flush_kernel_dcache_page(sg_page(sg));
894 +               kunmap(sg_page(sg));
895 +               if (dma_dev)
896 +                       dma_unmap_page(dma_dev, dma_addr, PAGE_SIZE, dir);
897 +
898 +               if (status < 0) {
899 +                       data->error = status;
900 +                       dev_dbg(&spi->dev, "%s status %d\n",
901 +                               (direction == DMA_TO_DEVICE)
902 +                                       ? "write" : "read",
903 +                               status);
904 +                       break;
905 +               }
906 +       }
907 +
908 +       /* NOTE some docs describe an MMC-only SET_BLOCK_COUNT (CMD23) that
909 +        * can be issued before multiblock writes.  Unlike its more widely
910 +        * documented analogue for SD cards (SET_WR_BLK_ERASE_COUNT, ACMD23),
911 +        * that can affect the STOP_TRAN logic.   Complete (and current)
912 +        * MMC specs should sort that out before Linux starts using CMD23.
913 +        */
914 +       if (direction == DMA_TO_DEVICE && multiple) {
915 +               struct scratch  *scratch = host->data;
916 +               int             tmp;
917 +               const unsigned  statlen = sizeof(scratch->status);
918 +
919 +               dev_dbg(&spi->dev, "    mmc_spi: STOP_TRAN\n");
920 +
921 +               /* Tweak the per-block message we set up earlier by morphing
922 +                * it to hold single buffer with the token followed by some
923 +                * all-ones bytes ... skip N(BR) (0..1), scan the rest for
924 +                * "not busy any longer" status, and leave chip selected.
925 +                */
926 +               INIT_LIST_HEAD(&host->m.transfers);
927 +               list_add(&host->early_status.transfer_list,
928 +                               &host->m.transfers);
929 +
930 +               memset(scratch->status, 0xff, statlen);
931 +               scratch->status[0] = SPI_TOKEN_STOP_TRAN;
932 +
933 +               host->early_status.tx_buf = host->early_status.rx_buf;
934 +               host->early_status.tx_dma = host->early_status.rx_dma;
935 +               host->early_status.len = statlen;
936 +
937 +               if (host->dma_dev)
938 +                       dma_sync_single_for_device(host->dma_dev,
939 +                                       host->data_dma, sizeof(*scratch),
940 +                                       DMA_BIDIRECTIONAL);
941 +
942 +               tmp = spi_sync(spi, &host->m);
943 +
944 +               if (host->dma_dev)
945 +                       dma_sync_single_for_cpu(host->dma_dev,
946 +                                       host->data_dma, sizeof(*scratch),
947 +                                       DMA_BIDIRECTIONAL);
948 +
949 +               if (tmp < 0) {
950 +                       if (!data->error)
951 +                               data->error = tmp;
952 +                       return;
953 +               }
954 +
955 +               /* Ideally we collected "not busy" status with one I/O,
956 +                * avoiding wasteful byte-at-a-time scanning... but more
957 +                * I/O is often needed.
958 +                */
959 +               for (tmp = 2; tmp < statlen; tmp++) {
960 +                       if (scratch->status[tmp] != 0)
961 +                               return;
962 +               }
963 +               tmp = mmc_spi_wait_unbusy(host, writeblock_timeout);
964 +               if (tmp < 0 && !data->error)
965 +                       data->error = tmp;
966 +       }
967 +}
968 +
969 +/****************************************************************************/
970 +
971 +/*
972 + * MMC driver implementation -- the interface to the MMC stack
973 + */
974 +
975 +static void mmc_spi_request(struct mmc_host *mmc, struct mmc_request *mrq)
976 +{
977 +       struct mmc_spi_host     *host = mmc_priv(mmc);
978 +       int                     status = -EINVAL;
979 +
980 +#ifdef DEBUG
981 +       /* MMC core and layered drivers *MUST* issue SPI-aware commands */
982 +       {
983 +               struct mmc_command      *cmd;
984 +               int                     invalid = 0;
985 +
986 +               cmd = mrq->cmd;
987 +               if (!mmc_spi_resp_type(cmd)) {
988 +                       dev_dbg(&host->spi->dev, "bogus command\n");
989 +                       cmd->error = -EINVAL;
990 +                       invalid = 1;
991 +               }
992 +
993 +               cmd = mrq->stop;
994 +               if (cmd && !mmc_spi_resp_type(cmd)) {
995 +                       dev_dbg(&host->spi->dev, "bogus STOP command\n");
996 +                       cmd->error = -EINVAL;
997 +                       invalid = 1;
998 +               }
999 +
1000 +               if (invalid) {
1001 +                       dump_stack();
1002 +                       mmc_request_done(host->mmc, mrq);
1003 +                       return;
1004 +               }
1005 +       }
1006 +#endif
1007 +
1008 +       /* issue command; then optionally data and stop */
1009 +       status = mmc_spi_command_send(host, mrq, mrq->cmd, mrq->data != NULL);
1010 +       if (status == 0 && mrq->data) {
1011 +               mmc_spi_data_do(host, mrq->cmd, mrq->data, mrq->data->blksz);
1012 +               if (mrq->stop)
1013 +                       status = mmc_spi_command_send(host, mrq, mrq->stop, 0);
1014 +               else
1015 +                       mmc_cs_off(host);
1016 +       }
1017 +
1018 +       mmc_request_done(host->mmc, mrq);
1019 +}
1020 +
1021 +/* See Section 6.4.1, in SD "Simplified Physical Layer Specification 2.0"
1022 + *
1023 + * NOTE that here we can't know that the card has just been powered up;
1024 + * not all MMC/SD sockets support power switching.
1025 + *
1026 + * FIXME when the card is still in SPI mode, e.g. from a previous kernel,
1027 + * this doesn't seem to do the right thing at all...
1028 + */
1029 +static void mmc_spi_initsequence(struct mmc_spi_host *host)
1030 +{
1031 +       /* Try to be very sure any previous command has completed;
1032 +        * wait till not-busy, skip debris from any old commands.
1033 +        */
1034 +       mmc_spi_wait_unbusy(host, r1b_timeout);
1035 +       mmc_spi_readbytes(host, 10);
1036 +
1037 +       /*
1038 +        * Do a burst with chipselect active-high.  We need to do this to
1039 +        * meet the requirement of 74 clock cycles with both chipselect
1040 +        * and CMD (MOSI) high before CMD0 ... after the card has been
1041 +        * powered up to Vdd(min), and so is ready to take commands.
1042 +        *
1043 +        * Some cards are particularly needy of this (e.g. Viking "SD256")
1044 +        * while most others don't seem to care.
1045 +        *
1046 +        * Note that this is one of the places MMC/SD plays games with the
1047 +        * SPI protocol.  Another is that when chipselect is released while
1048 +        * the card returns BUSY status, the clock must issue several cycles
1049 +        * with chipselect high before the card will stop driving its output.
1050 +        */
1051 +       host->spi->mode |= SPI_CS_HIGH;
1052 +       if (spi_setup(host->spi) != 0) {
1053 +               /* Just warn; most cards work without it. */
1054 +               dev_warn(&host->spi->dev,
1055 +                               "can't change chip-select polarity\n");
1056 +               host->spi->mode &= ~SPI_CS_HIGH;
1057 +       } else {
1058 +               mmc_spi_readbytes(host, 18);
1059 +
1060 +               host->spi->mode &= ~SPI_CS_HIGH;
1061 +               if (spi_setup(host->spi) != 0) {
1062 +                       /* Wot, we can't get the same setup we had before? */
1063 +                       dev_err(&host->spi->dev,
1064 +                                       "can't restore chip-select polarity\n");
1065 +               }
1066 +       }
1067 +}
1068 +
1069 +static char *mmc_powerstring(u8 power_mode)
1070 +{
1071 +       switch (power_mode) {
1072 +       case MMC_POWER_OFF: return "off";
1073 +       case MMC_POWER_UP:  return "up";
1074 +       case MMC_POWER_ON:  return "on";
1075 +       }
1076 +       return "?";
1077 +}
1078 +
1079 +static void mmc_spi_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1080 +{
1081 +       struct mmc_spi_host *host = mmc_priv(mmc);
1082 +
1083 +       if (host->power_mode != ios->power_mode) {
1084 +               int             canpower;
1085 +
1086 +               canpower = host->pdata && host->pdata->setpower;
1087 +
1088 +               dev_dbg(&host->spi->dev, "mmc_spi: power %s (%d)%s\n",
1089 +                               mmc_powerstring(ios->power_mode),
1090 +                               ios->vdd,
1091 +                               canpower ? ", can switch" : "");
1092 +
1093 +               /* switch power on/off if possible, accounting for
1094 +                * max 250msec powerup time if needed.
1095 +                */
1096 +               if (canpower) {
1097 +                       switch (ios->power_mode) {
1098 +                       case MMC_POWER_OFF:
1099 +                       case MMC_POWER_UP:
1100 +                               host->pdata->setpower(&host->spi->dev,
1101 +                                               ios->vdd);
1102 +                               if (ios->power_mode == MMC_POWER_UP)
1103 +                                       msleep(host->powerup_msecs);
1104 +                       }
1105 +               }
1106 +
1107 +               /* See 6.4.1 in the simplified SD card physical spec 2.0 */
1108 +               if (ios->power_mode == MMC_POWER_ON)
1109 +                       mmc_spi_initsequence(host);
1110 +
1111 +               /* If powering down, ground all card inputs to avoid power
1112 +                * delivery from data lines!  On a shared SPI bus, this
1113 +                * will probably be temporary; 6.4.2 of the simplified SD
1114 +                * spec says this must last at least 1msec.
1115 +                *
1116 +                *   - Clock low means CPOL 0, e.g. mode 0
1117 +                *   - MOSI low comes from writing zero
1118 +                *   - Chipselect is usually active low...
1119 +                */
1120 +               if (canpower && ios->power_mode == MMC_POWER_OFF) {
1121 +                       int mres;
1122 +
1123 +                       host->spi->mode &= ~(SPI_CPOL|SPI_CPHA);
1124 +                       mres = spi_setup(host->spi);
1125 +                       if (mres < 0)
1126 +                               dev_dbg(&host->spi->dev,
1127 +                                       "switch to SPI mode 0 failed\n");
1128 +
1129 +                       if (spi_w8r8(host->spi, 0x00) < 0)
1130 +                               dev_dbg(&host->spi->dev,
1131 +                                       "put spi signals to low failed\n");
1132 +
1133 +                       /*
1134 +                        * Now clock should be low due to spi mode 0;
1135 +                        * MOSI should be low because of written 0x00;
1136 +                        * chipselect should be low (it is active low)
1137 +                        * power supply is off, so now MMC is off too!
1138 +                        *
1139 +                        * FIXME no, chipselect can be high since the
1140 +                        * device is inactive and SPI_CS_HIGH is clear...
1141 +                        */
1142 +                       msleep(10);
1143 +                       if (mres == 0) {
1144 +                               host->spi->mode |= (SPI_CPOL|SPI_CPHA);
1145 +                               mres = spi_setup(host->spi);
1146 +                               if (mres < 0)
1147 +                                       dev_dbg(&host->spi->dev,
1148 +                                               "switch back to SPI mode 3"
1149 +                                               " failed\n");
1150 +                       }
1151 +               }
1152 +
1153 +               host->power_mode = ios->power_mode;
1154 +       }
1155 +
1156 +       if (host->spi->max_speed_hz != ios->clock && ios->clock != 0) {
1157 +               int             status;
1158 +
1159 +               host->spi->max_speed_hz = ios->clock;
1160 +               status = spi_setup(host->spi);
1161 +               dev_dbg(&host->spi->dev,
1162 +                       "mmc_spi:  clock to %d Hz, %d\n",
1163 +                       host->spi->max_speed_hz, status);
1164 +       }
1165 +}
1166 +
1167 +static int mmc_spi_get_ro(struct mmc_host *mmc)
1168 +{
1169 +       struct mmc_spi_host *host = mmc_priv(mmc);
1170 +
1171 +       if (host->pdata && host->pdata->get_ro)
1172 +               return host->pdata->get_ro(mmc->parent);
1173 +       /* board doesn't support read only detection; assume writeable */
1174 +       return 0;
1175 +}
1176 +
1177 +
1178 +static const struct mmc_host_ops mmc_spi_ops = {
1179 +       .request        = mmc_spi_request,
1180 +       .set_ios        = mmc_spi_set_ios,
1181 +       .get_ro         = mmc_spi_get_ro,
1182 +};
1183 +
1184 +
1185 +/****************************************************************************/
1186 +
1187 +/*
1188 + * SPI driver implementation
1189 + */
1190 +
1191 +static irqreturn_t
1192 +mmc_spi_detect_irq(int irq, void *mmc)
1193 +{
1194 +       struct mmc_spi_host *host = mmc_priv(mmc);
1195 +       u16 delay_msec = max(host->pdata->detect_delay, (u16)100);
1196 +
1197 +       mmc_detect_change(mmc, msecs_to_jiffies(delay_msec));
1198 +       return IRQ_HANDLED;
1199 +}
1200 +
1201 +struct count_children {
1202 +       unsigned        n;
1203 +       struct bus_type *bus;
1204 +};
1205 +
1206 +static int maybe_count_child(struct device *dev, void *c)
1207 +{
1208 +       struct count_children *ccp = c;
1209 +
1210 +       if (dev->bus == ccp->bus) {
1211 +               if (ccp->n)
1212 +                       return -EBUSY;
1213 +               ccp->n++;
1214 +       }
1215 +       return 0;
1216 +}
1217 +
1218 +static int mmc_spi_probe(struct spi_device *spi)
1219 +{
1220 +       void                    *ones;
1221 +       struct mmc_host         *mmc;
1222 +       struct mmc_spi_host     *host;
1223 +       int                     status;
1224 +
1225 +       /* MMC and SD specs only seem to care that sampling is on the
1226 +        * rising edge ... meaning SPI modes 0 or 3.  So either SPI mode
1227 +        * should be legit.  We'll use mode 0 since it seems to be a
1228 +        * bit less troublesome on some hardware ... unclear why.
1229 +        */
1230 +       spi->mode = SPI_MODE_0;
1231 +       spi->bits_per_word = 8;
1232 +
1233 +       status = spi_setup(spi);
1234 +       if (status < 0) {
1235 +               dev_dbg(&spi->dev, "needs SPI mode %02x, %d KHz; %d\n",
1236 +                               spi->mode, spi->max_speed_hz / 1000,
1237 +                               status);
1238 +               return status;
1239 +       }
1240 +
1241 +       /* We can use the bus safely iff nobody else will interfere with us.
1242 +        * Most commands consist of one SPI message to issue a command, then
1243 +        * several more to collect its response, then possibly more for data
1244 +        * transfer.  Clocking access to other devices during that period will
1245 +        * corrupt the command execution.
1246 +        *
1247 +        * Until we have software primitives which guarantee non-interference,
1248 +        * we'll aim for a hardware-level guarantee.
1249 +        *
1250 +        * REVISIT we can't guarantee another device won't be added later...
1251 +        */
1252 +       if (spi->master->num_chipselect > 1) {
1253 +               struct count_children cc;
1254 +
1255 +               cc.n = 0;
1256 +               cc.bus = spi->dev.bus;
1257 +               status = device_for_each_child(spi->dev.parent, &cc,
1258 +                               maybe_count_child);
1259 +               if (status < 0) {
1260 +                       dev_err(&spi->dev, "can't share SPI bus\n");
1261 +                       return status;
1262 +               }
1263 +
1264 +               dev_warn(&spi->dev, "ASSUMING SPI bus stays unshared!\n");
1265 +       }
1266 +
1267 +       /* We need a supply of ones to transmit.  This is the only time
1268 +        * the CPU touches these, so cache coherency isn't a concern.
1269 +        *
1270 +        * NOTE if many systems use more than one MMC-over-SPI connector
1271 +        * it'd save some memory to share this.  That's evidently rare.
1272 +        */
1273 +       status = -ENOMEM;
1274 +       ones = kmalloc(MMC_SPI_BLOCKSIZE, GFP_KERNEL);
1275 +       if (!ones)
1276 +               goto nomem;
1277 +       memset(ones, 0xff, MMC_SPI_BLOCKSIZE);
1278 +
1279 +       mmc = mmc_alloc_host(sizeof(*host), &spi->dev);
1280 +       if (!mmc)
1281 +               goto nomem;
1282 +
1283 +       mmc->ops = &mmc_spi_ops;
1284 +       mmc->max_blk_size = MMC_SPI_BLOCKSIZE;
1285 +
1286 +       /* As long as we keep track of the number of successfully
1287 +        * transmitted blocks, we're good for multiwrite.
1288 +        */
1289 +       mmc->caps = MMC_CAP_SPI | MMC_CAP_MULTIWRITE;
1290 +
1291 +       /* SPI doesn't need the lowspeed device identification thing for
1292 +        * MMC or SD cards, since it never comes up in open drain mode.
1293 +        * That's good; some SPI masters can't handle very low speeds!
1294 +        *
1295 +        * However, low speed SDIO cards need not handle over 400 KHz;
1296 +        * that's the only reason not to use a few MHz for f_min (until
1297 +        * the upper layer reads the target frequency from the CSD).
1298 +        */
1299 +       mmc->f_min = 400000;
1300 +       mmc->f_max = spi->max_speed_hz;
1301 +
1302 +       host = mmc_priv(mmc);
1303 +       host->mmc = mmc;
1304 +       host->spi = spi;
1305 +
1306 +       host->ones = ones;
1307 +
1308 +       /* Platform data is used to hook up things like card sensing
1309 +        * and power switching gpios.
1310 +        */
1311 +       host->pdata = spi->dev.platform_data;
1312 +       if (host->pdata)
1313 +               mmc->ocr_avail = host->pdata->ocr_mask;
1314 +       if (!mmc->ocr_avail) {
1315 +               dev_warn(&spi->dev, "ASSUMING 3.2-3.4 V slot power\n");
1316 +               mmc->ocr_avail = MMC_VDD_32_33|MMC_VDD_33_34;
1317 +       }
1318 +       if (host->pdata && host->pdata->setpower) {
1319 +               host->powerup_msecs = host->pdata->powerup_msecs;
1320 +               if (!host->powerup_msecs || host->powerup_msecs > 250)
1321 +                       host->powerup_msecs = 250;
1322 +       }
1323 +
1324 +       dev_set_drvdata(&spi->dev, mmc);
1325 +
1326 +       /* preallocate dma buffers */
1327 +       host->data = kmalloc(sizeof(*host->data), GFP_KERNEL);
1328 +       if (!host->data)
1329 +               goto fail_nobuf1;
1330 +
1331 +//FIXME
1332 +#if 0
1333 +       if (spi->master->dev.parent->dma_mask) {
1334 +               struct device   *dev = spi->master->dev.parent;
1335 +
1336 +               host->dma_dev = dev;
1337 +               host->ones_dma = dma_map_single(dev, ones,
1338 +                               MMC_SPI_BLOCKSIZE, DMA_TO_DEVICE);
1339 +               host->data_dma = dma_map_single(dev, host->data,
1340 +                               sizeof(*host->data), DMA_BIDIRECTIONAL);
1341 +
1342 +               /* REVISIT in theory those map operations can fail... */
1343 +
1344 +               dma_sync_single_for_cpu(host->dma_dev,
1345 +                               host->data_dma, sizeof(*host->data),
1346 +                               DMA_BIDIRECTIONAL);
1347 +       }
1348 +#endif
1349 +
1350 +       /* setup message for status/busy readback */
1351 +       spi_message_init(&host->readback);
1352 +       host->readback.is_dma_mapped = (host->dma_dev != NULL);
1353 +
1354 +       spi_message_add_tail(&host->status, &host->readback);
1355 +       host->status.tx_buf = host->ones;
1356 +       host->status.tx_dma = host->ones_dma;
1357 +       host->status.rx_buf = &host->data->status;
1358 +       host->status.rx_dma = host->data_dma + offsetof(struct scratch, status);
1359 +       host->status.cs_change = 1;
1360 +
1361 +       /* register card detect irq */
1362 +       if (host->pdata && host->pdata->init) {
1363 +               status = host->pdata->init(&spi->dev, mmc_spi_detect_irq, mmc);
1364 +               if (status != 0)
1365 +                       goto fail_glue_init;
1366 +       }
1367 +
1368 +       status = mmc_add_host(mmc);
1369 +       if (status != 0)
1370 +               goto fail_add_host;
1371 +
1372 +       dev_info(&spi->dev, "SD/MMC host %s%s%s%s\n",
1373 +                       mmc->class_dev.bus_id,
1374 +                       host->dma_dev ? "" : ", no DMA",
1375 +                       (host->pdata && host->pdata->get_ro)
1376 +                               ? "" : ", no WP",
1377 +                       (host->pdata && host->pdata->setpower)
1378 +                               ? "" : ", no poweroff");
1379 +       return 0;
1380 +
1381 +fail_add_host:
1382 +       mmc_remove_host (mmc);
1383 +fail_glue_init:
1384 +       if (host->dma_dev)
1385 +               dma_unmap_single(host->dma_dev, host->data_dma,
1386 +                               sizeof(*host->data), DMA_BIDIRECTIONAL);
1387 +       kfree(host->data);
1388 +
1389 +fail_nobuf1:
1390 +       mmc_free_host(mmc);
1391 +       dev_set_drvdata(&spi->dev, NULL);
1392 +
1393 +nomem:
1394 +       kfree(ones);
1395 +       return status;
1396 +}
1397 +
1398 +
1399 +static int __devexit mmc_spi_remove(struct spi_device *spi)
1400 +{
1401 +       struct mmc_host         *mmc = dev_get_drvdata(&spi->dev);
1402 +       struct mmc_spi_host     *host;
1403 +
1404 +       if (mmc) {
1405 +               host = mmc_priv(mmc);
1406 +
1407 +               /* prevent new mmc_detect_change() calls */
1408 +               if (host->pdata && host->pdata->exit)
1409 +                       host->pdata->exit(&spi->dev, mmc);
1410 +
1411 +               mmc_remove_host(mmc);
1412 +
1413 +               if (host->dma_dev) {
1414 +                       dma_unmap_single(host->dma_dev, host->ones_dma,
1415 +                               MMC_SPI_BLOCKSIZE, DMA_TO_DEVICE);
1416 +                       dma_unmap_single(host->dma_dev, host->data_dma,
1417 +                               sizeof(*host->data), DMA_BIDIRECTIONAL);
1418 +               }
1419 +
1420 +               kfree(host->data);
1421 +               kfree(host->ones);
1422 +
1423 +               spi->max_speed_hz = mmc->f_max;
1424 +               mmc_free_host(mmc);
1425 +               dev_set_drvdata(&spi->dev, NULL);
1426 +       }
1427 +       return 0;
1428 +}
1429 +
1430 +
1431 +static struct spi_driver mmc_spi_driver = {
1432 +       .driver = {
1433 +               .name =         "mmc_spi",
1434 +               .bus =          &spi_bus_type,
1435 +               .owner =        THIS_MODULE,
1436 +       },
1437 +       .probe =        mmc_spi_probe,
1438 +       .remove =       __devexit_p(mmc_spi_remove),
1439 +};
1440 +
1441 +
1442 +static int __init mmc_spi_init(void)
1443 +{
1444 +       return spi_register_driver(&mmc_spi_driver);
1445 +}
1446 +module_init(mmc_spi_init);
1447 +
1448 +
1449 +static void __exit mmc_spi_exit(void)
1450 +{
1451 +       spi_unregister_driver(&mmc_spi_driver);
1452 +}
1453 +module_exit(mmc_spi_exit);
1454 +
1455 +
1456 +MODULE_AUTHOR("Mike Lavender, David Brownell, "
1457 +               "Hans-Peter Nilsson, Jan Nikitenko");
1458 +MODULE_DESCRIPTION("SPI SD/MMC host driver");
1459 +MODULE_LICENSE("GPL");
1460 Index: linux-2.6.23.17/include/linux/spi/mmc_spi.h
1461 ===================================================================
1462 --- /dev/null
1463 +++ linux-2.6.23.17/include/linux/spi/mmc_spi.h
1464 @@ -0,0 +1,33 @@
1465 +#ifndef __LINUX_SPI_MMC_SPI_H
1466 +#define __LINUX_SPI_MMC_SPI_H
1467 +
1468 +struct device;
1469 +struct mmc_host;
1470 +
1471 +/* Put this in platform_data of a device being used to manage an MMC/SD
1472 + * card slot.  (Modeled after PXA mmc glue; see that for usage examples.)
1473 + *
1474 + * REVISIT This is not a spi-specific notion.  Any card slot should be
1475 + * able to handle it.  If the MMC core doesn't adopt this kind of notion,
1476 + * switch the "struct device *" parameters over to "struct spi_device *".
1477 + */
1478 +struct mmc_spi_platform_data {
1479 +       /* driver activation and (optional) card detect irq hookup */
1480 +       int (*init)(struct device *,
1481 +               irqreturn_t (*)(int, void *),
1482 +               void *);
1483 +       void (*exit)(struct device *, void *);
1484 +
1485 +       /* sense switch on sd cards */
1486 +       int (*get_ro)(struct device *);
1487 +
1488 +       /* how long to debounce card detect, in msecs */
1489 +       u16 detect_delay;
1490 +
1491 +       /* power management */
1492 +       u16 powerup_msecs;              /* delay of up to 250 msec */
1493 +       u32 ocr_mask;                   /* available voltages */
1494 +       void (*setpower)(struct device *, unsigned int maskval);
1495 +};
1496 +
1497 +#endif /* __LINUX_SPI_MMC_SPI_H */
1498 Index: linux-2.6.23.17/drivers/mmc/core/bus.c
1499 ===================================================================
1500 --- linux-2.6.23.17.orig/drivers/mmc/core/bus.c
1501 +++ linux-2.6.23.17/drivers/mmc/core/bus.c
1502 @@ -19,6 +19,7 @@
1503  
1504  #include "sysfs.h"
1505  #include "core.h"
1506 +#include "sdio_cis.h"
1507  #include "bus.h"
1508  
1509  #define dev_to_mmc_card(d)     container_of(d, struct mmc_card, dev)
1510 @@ -34,6 +35,8 @@ static ssize_t mmc_type_show(struct devi
1511                 return sprintf(buf, "MMC\n");
1512         case MMC_TYPE_SD:
1513                 return sprintf(buf, "SD\n");
1514 +       case MMC_TYPE_SDIO:
1515 +               return sprintf(buf, "SDIO\n");
1516         default:
1517                 return -EFAULT;
1518         }
1519 @@ -55,36 +58,37 @@ static int mmc_bus_match(struct device *
1520  }
1521  
1522  static int
1523 -mmc_bus_uevent(struct device *dev, char **envp, int num_envp, char *buf,
1524 -               int buf_size)
1525 +mmc_bus_uevent(struct device *dev, char **envp,
1526 +               int num_envp, char *buffer, int buffer_size)
1527  {
1528         struct mmc_card *card = dev_to_mmc_card(dev);
1529 -       int retval = 0, i = 0, length = 0;
1530 -
1531 -#define add_env(fmt,val) do {                                  \
1532 -       retval = add_uevent_var(envp, num_envp, &i,             \
1533 -                               buf, buf_size, &length,         \
1534 -                               fmt, val);                      \
1535 -       if (retval)                                             \
1536 -               return retval;                                  \
1537 -} while (0);
1538 +       const char *type;
1539 +       int retval = 0;
1540 +       int i = 0, len = 0;
1541  
1542         switch (card->type) {
1543         case MMC_TYPE_MMC:
1544 -               add_env("MMC_TYPE=%s", "MMC");
1545 +               type = "MMC";
1546                 break;
1547         case MMC_TYPE_SD:
1548 -               add_env("MMC_TYPE=%s", "SD");
1549 +               type = "SD";
1550                 break;
1551 +       case MMC_TYPE_SDIO:
1552 +               type = "SDIO";
1553 +               break;
1554 +       default:
1555 +               type = NULL;
1556         }
1557  
1558 -       add_env("MMC_NAME=%s", mmc_card_name(card));
1559 -
1560 -#undef add_env
1561 +       if (type) {
1562 +               retval = add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len, "MMC_TYPE=%s", type);
1563 +               if (retval)
1564 +                       return retval;
1565 +       }
1566  
1567 -       envp[i] = NULL;
1568 +       retval = add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len, "MMC_NAME=%s", mmc_card_name(card));
1569  
1570 -       return 0;
1571 +       return retval;
1572  }
1573  
1574  static int mmc_bus_probe(struct device *dev)
1575 @@ -176,6 +180,11 @@ static void mmc_release_card(struct devi
1576  {
1577         struct mmc_card *card = dev_to_mmc_card(dev);
1578  
1579 +       sdio_free_common_cis(card);
1580 +
1581 +       if (card->info)
1582 +               kfree(card->info);
1583 +
1584         kfree(card);
1585  }
1586  
1587 @@ -221,15 +230,25 @@ int mmc_add_card(struct mmc_card *card)
1588                 if (mmc_card_blockaddr(card))
1589                         type = "SDHC";
1590                 break;
1591 +       case MMC_TYPE_SDIO:
1592 +               type = "SDIO";
1593 +               break;
1594         default:
1595                 type = "?";
1596                 break;
1597         }
1598  
1599 -       printk(KERN_INFO "%s: new %s%s card at address %04x\n",
1600 -               mmc_hostname(card->host),
1601 -               mmc_card_highspeed(card) ? "high speed " : "",
1602 -               type, card->rca);
1603 +       if (mmc_host_is_spi(card->host)) {
1604 +               printk(KERN_INFO "%s: new %s%s card on SPI\n",
1605 +                       mmc_hostname(card->host),
1606 +                       mmc_card_highspeed(card) ? "high speed " : "",
1607 +                       type);
1608 +       } else {
1609 +               printk(KERN_INFO "%s: new %s%s card at address %04x\n",
1610 +                       mmc_hostname(card->host),
1611 +                       mmc_card_highspeed(card) ? "high speed " : "",
1612 +                       type, card->rca);
1613 +       }
1614  
1615         card->dev.uevent_suppress = 1;
1616  
1617 @@ -261,8 +280,13 @@ int mmc_add_card(struct mmc_card *card)
1618  void mmc_remove_card(struct mmc_card *card)
1619  {
1620         if (mmc_card_present(card)) {
1621 -               printk(KERN_INFO "%s: card %04x removed\n",
1622 -                       mmc_hostname(card->host), card->rca);
1623 +               if (mmc_host_is_spi(card->host)) {
1624 +                       printk(KERN_INFO "%s: SPI card removed\n",
1625 +                               mmc_hostname(card->host));
1626 +               } else {
1627 +                       printk(KERN_INFO "%s: card %04x removed\n",
1628 +                               mmc_hostname(card->host), card->rca);
1629 +               }
1630  
1631                 if (card->host->bus_ops->sysfs_remove)
1632                         card->host->bus_ops->sysfs_remove(card->host, card);
1633 Index: linux-2.6.23.17/drivers/mmc/core/core.c
1634 ===================================================================
1635 --- linux-2.6.23.17.orig/drivers/mmc/core/core.c
1636 +++ linux-2.6.23.17/drivers/mmc/core/core.c
1637 @@ -18,7 +18,7 @@
1638  #include <linux/delay.h>
1639  #include <linux/pagemap.h>
1640  #include <linux/err.h>
1641 -#include <asm/scatterlist.h>
1642 +#include <linux/leds.h>
1643  #include <linux/scatterlist.h>
1644  
1645  #include <linux/mmc/card.h>
1646 @@ -29,16 +29,27 @@
1647  #include "core.h"
1648  #include "bus.h"
1649  #include "host.h"
1650 +#include "sdio_bus.h"
1651  
1652  #include "mmc_ops.h"
1653  #include "sd_ops.h"
1654 +#include "sdio_ops.h"
1655  
1656  extern int mmc_attach_mmc(struct mmc_host *host, u32 ocr);
1657  extern int mmc_attach_sd(struct mmc_host *host, u32 ocr);
1658 +extern int mmc_attach_sdio(struct mmc_host *host, u32 ocr);
1659  
1660  static struct workqueue_struct *workqueue;
1661  
1662  /*
1663 + * Enabling software CRCs on the data blocks can be a significant (30%)
1664 + * performance cost, and for other reasons may not always be desired.
1665 + * So we allow it it to be disabled.
1666 + */
1667 +int use_spi_crc = 1;
1668 +module_param(use_spi_crc, bool, 0);
1669 +
1670 +/*
1671   * Internal function. Schedule delayed work in the MMC work queue.
1672   */
1673  static int mmc_schedule_delayed_work(struct delayed_work *work,
1674 @@ -68,6 +79,11 @@ void mmc_request_done(struct mmc_host *h
1675         struct mmc_command *cmd = mrq->cmd;
1676         int err = cmd->error;
1677  
1678 +       if (err && cmd->retries && mmc_host_is_spi(host)) {
1679 +               if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
1680 +                       cmd->retries = 0;
1681 +       }
1682 +
1683         if (err && cmd->retries) {
1684                 pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
1685                         mmc_hostname(host), cmd->opcode, err);
1686 @@ -76,6 +92,8 @@ void mmc_request_done(struct mmc_host *h
1687                 cmd->error = 0;
1688                 host->ops->request(host, mrq);
1689         } else {
1690 +               led_trigger_event(host->led, LED_OFF);
1691 +
1692                 pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n",
1693                         mmc_hostname(host), cmd->opcode, err,
1694                         cmd->resp[0], cmd->resp[1],
1695 @@ -118,7 +136,7 @@ mmc_start_request(struct mmc_host *host,
1696                         "tsac %d ms nsac %d\n",
1697                         mmc_hostname(host), mrq->data->blksz,
1698                         mrq->data->blocks, mrq->data->flags,
1699 -                       mrq->data->timeout_ns / 10000000,
1700 +                       mrq->data->timeout_ns / 1000000,
1701                         mrq->data->timeout_clks);
1702         }
1703  
1704 @@ -130,6 +148,8 @@ mmc_start_request(struct mmc_host *host,
1705  
1706         WARN_ON(!host->claimed);
1707  
1708 +       led_trigger_event(host->led, LED_FULL);
1709 +
1710         mrq->cmd->error = 0;
1711         mrq->cmd->mrq = mrq;
1712         if (mrq->data) {
1713 @@ -199,7 +219,7 @@ int mmc_wait_for_cmd(struct mmc_host *ho
1714  {
1715         struct mmc_request mrq;
1716  
1717 -       BUG_ON(!host->claimed);
1718 +       WARN_ON(!host->claimed);
1719  
1720         memset(&mrq, 0, sizeof(struct mmc_request));
1721  
1722 @@ -220,17 +240,24 @@ EXPORT_SYMBOL(mmc_wait_for_cmd);
1723   *     mmc_set_data_timeout - set the timeout for a data command
1724   *     @data: data phase for command
1725   *     @card: the MMC card associated with the data transfer
1726 - *     @write: flag to differentiate reads from writes
1727   *
1728   *     Computes the data timeout parameters according to the
1729   *     correct algorithm given the card type.
1730   */
1731 -void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card,
1732 -                         int write)
1733 +void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
1734  {
1735         unsigned int mult;
1736  
1737         /*
1738 +        * SDIO cards only define an upper 1 s limit on access.
1739 +        */
1740 +       if (mmc_card_sdio(card)) {
1741 +               data->timeout_ns = 1000000000;
1742 +               data->timeout_clks = 0;
1743 +               return;
1744 +       }
1745 +
1746 +       /*
1747          * SD cards use a 100 multiplier rather than 10
1748          */
1749         mult = mmc_card_sd(card) ? 100 : 10;
1750 @@ -239,7 +266,7 @@ void mmc_set_data_timeout(struct mmc_dat
1751          * Scale up the multiplier (and therefore the timeout) by
1752          * the r2w factor for writes.
1753          */
1754 -       if (write)
1755 +       if (data->flags & MMC_DATA_WRITE)
1756                 mult <<= card->csd.r2w_factor;
1757  
1758         data->timeout_ns = card->csd.tacc_ns * mult;
1759 @@ -255,7 +282,7 @@ void mmc_set_data_timeout(struct mmc_dat
1760                 timeout_us += data->timeout_clks * 1000 /
1761                         (card->host->ios.clock / 1000);
1762  
1763 -               if (write)
1764 +               if (data->flags & MMC_DATA_WRITE)
1765                         limit_us = 250000;
1766                 else
1767                         limit_us = 100000;
1768 @@ -272,15 +299,20 @@ void mmc_set_data_timeout(struct mmc_dat
1769  EXPORT_SYMBOL(mmc_set_data_timeout);
1770  
1771  /**
1772 - *     mmc_claim_host - exclusively claim a host
1773 + *     __mmc_claim_host - exclusively claim a host
1774   *     @host: mmc host to claim
1775 + *     @abort: whether or not the operation should be aborted
1776   *
1777 - *     Claim a host for a set of operations.
1778 + *     Claim a host for a set of operations.  If @abort is non null and
1779 + *     dereference a non-zero value then this will return prematurely with
1780 + *     that non-zero value without acquiring the lock.  Returns zero
1781 + *     with the lock held otherwise.
1782   */
1783 -void mmc_claim_host(struct mmc_host *host)
1784 +int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
1785  {
1786         DECLARE_WAITQUEUE(wait, current);
1787         unsigned long flags;
1788 +       int stop;
1789  
1790         might_sleep();
1791  
1792 @@ -288,19 +320,24 @@ void mmc_claim_host(struct mmc_host *hos
1793         spin_lock_irqsave(&host->lock, flags);
1794         while (1) {
1795                 set_current_state(TASK_UNINTERRUPTIBLE);
1796 -               if (!host->claimed)
1797 +               stop = abort ? atomic_read(abort) : 0;
1798 +               if (stop || !host->claimed)
1799                         break;
1800                 spin_unlock_irqrestore(&host->lock, flags);
1801                 schedule();
1802                 spin_lock_irqsave(&host->lock, flags);
1803         }
1804         set_current_state(TASK_RUNNING);
1805 -       host->claimed = 1;
1806 +       if (!stop)
1807 +               host->claimed = 1;
1808 +       else
1809 +               wake_up(&host->wq);
1810         spin_unlock_irqrestore(&host->lock, flags);
1811         remove_wait_queue(&host->wq, &wait);
1812 +       return stop;
1813  }
1814  
1815 -EXPORT_SYMBOL(mmc_claim_host);
1816 +EXPORT_SYMBOL(__mmc_claim_host);
1817  
1818  /**
1819   *     mmc_release_host - release a host
1820 @@ -313,7 +350,7 @@ void mmc_release_host(struct mmc_host *h
1821  {
1822         unsigned long flags;
1823  
1824 -       BUG_ON(!host->claimed);
1825 +       WARN_ON(!host->claimed);
1826  
1827         spin_lock_irqsave(&host->lock, flags);
1828         host->claimed = 0;
1829 @@ -433,19 +470,32 @@ static void mmc_power_up(struct mmc_host
1830         int bit = fls(host->ocr_avail) - 1;
1831  
1832         host->ios.vdd = bit;
1833 -       host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
1834 -       host->ios.chip_select = MMC_CS_DONTCARE;
1835 +       if (mmc_host_is_spi(host)) {
1836 +               host->ios.chip_select = MMC_CS_HIGH;
1837 +               host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
1838 +       } else {
1839 +               host->ios.chip_select = MMC_CS_DONTCARE;
1840 +               host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
1841 +       }
1842         host->ios.power_mode = MMC_POWER_UP;
1843         host->ios.bus_width = MMC_BUS_WIDTH_1;
1844         host->ios.timing = MMC_TIMING_LEGACY;
1845         mmc_set_ios(host);
1846  
1847 -       mmc_delay(1);
1848 +       /*
1849 +        * This delay should be sufficient to allow the power supply
1850 +        * to reach the minimum voltage.
1851 +        */
1852 +       mmc_delay(2);
1853  
1854         host->ios.clock = host->f_min;
1855         host->ios.power_mode = MMC_POWER_ON;
1856         mmc_set_ios(host);
1857  
1858 +       /*
1859 +        * This delay must be at least 74 clock sizes, or 1 ms, or the
1860 +        * time required to reach a stable voltage.
1861 +        */
1862         mmc_delay(2);
1863  }
1864  
1865 @@ -453,8 +503,10 @@ static void mmc_power_off(struct mmc_hos
1866  {
1867         host->ios.clock = 0;
1868         host->ios.vdd = 0;
1869 -       host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
1870 -       host->ios.chip_select = MMC_CS_DONTCARE;
1871 +       if (!mmc_host_is_spi(host)) {
1872 +               host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
1873 +               host->ios.chip_select = MMC_CS_DONTCARE;
1874 +       }
1875         host->ios.power_mode = MMC_POWER_OFF;
1876         host->ios.bus_width = MMC_BUS_WIDTH_1;
1877         host->ios.timing = MMC_TIMING_LEGACY;
1878 @@ -511,7 +563,7 @@ void mmc_attach_bus(struct mmc_host *hos
1879         BUG_ON(!host);
1880         BUG_ON(!ops);
1881  
1882 -       BUG_ON(!host->claimed);
1883 +       WARN_ON(!host->claimed);
1884  
1885         spin_lock_irqsave(&host->lock, flags);
1886  
1887 @@ -535,8 +587,8 @@ void mmc_detach_bus(struct mmc_host *hos
1888  
1889         BUG_ON(!host);
1890  
1891 -       BUG_ON(!host->claimed);
1892 -       BUG_ON(!host->bus_ops);
1893 +       WARN_ON(!host->claimed);
1894 +       WARN_ON(!host->bus_ops);
1895  
1896         spin_lock_irqsave(&host->lock, flags);
1897  
1898 @@ -564,7 +616,7 @@ void mmc_detect_change(struct mmc_host *
1899  #ifdef CONFIG_MMC_DEBUG
1900         unsigned long flags;
1901         spin_lock_irqsave(&host->lock, flags);
1902 -       BUG_ON(host->removed);
1903 +       WARN_ON(host->removed);
1904         spin_unlock_irqrestore(&host->lock, flags);
1905  #endif
1906  
1907 @@ -597,24 +649,38 @@ void mmc_rescan(struct work_struct *work
1908  
1909                 mmc_send_if_cond(host, host->ocr_avail);
1910  
1911 +               /*
1912 +                * First we search for SDIO...
1913 +                */
1914 +               err = mmc_send_io_op_cond(host, 0, &ocr);
1915 +               if (!err) {
1916 +                       if (mmc_attach_sdio(host, ocr))
1917 +                               mmc_power_off(host);
1918 +                       return;
1919 +               }
1920 +
1921 +               /*
1922 +                * ...then normal SD...
1923 +                */
1924                 err = mmc_send_app_op_cond(host, 0, &ocr);
1925 -               if (err == MMC_ERR_NONE) {
1926 +               if (!err) {
1927                         if (mmc_attach_sd(host, ocr))
1928                                 mmc_power_off(host);
1929 -               } else {
1930 -                       /*
1931 -                        * If we fail to detect any SD cards then try
1932 -                        * searching for MMC cards.
1933 -                        */
1934 -                       err = mmc_send_op_cond(host, 0, &ocr);
1935 -                       if (err == MMC_ERR_NONE) {
1936 -                               if (mmc_attach_mmc(host, ocr))
1937 -                                       mmc_power_off(host);
1938 -                       } else {
1939 +                       return;
1940 +               }
1941 +
1942 +               /*
1943 +                * ...and finally MMC.
1944 +                */
1945 +               err = mmc_send_op_cond(host, 0, &ocr);
1946 +               if (!err) {
1947 +                       if (mmc_attach_mmc(host, ocr))
1948                                 mmc_power_off(host);
1949 -                               mmc_release_host(host);
1950 -                       }
1951 +                       return;
1952                 }
1953 +
1954 +               mmc_release_host(host);
1955 +               mmc_power_off(host);
1956         } else {
1957                 if (host->bus_ops->detect && !host->bus_dead)
1958                         host->bus_ops->detect(host);
1959 @@ -725,22 +791,38 @@ static int __init mmc_init(void)
1960                 return -ENOMEM;
1961  
1962         ret = mmc_register_bus();
1963 -       if (ret == 0) {
1964 -               ret = mmc_register_host_class();
1965 -               if (ret)
1966 -                       mmc_unregister_bus();
1967 -       }
1968 +       if (ret)
1969 +               goto destroy_workqueue;
1970 +
1971 +       ret = mmc_register_host_class();
1972 +       if (ret)
1973 +               goto unregister_bus;
1974 +
1975 +       ret = sdio_register_bus();
1976 +       if (ret)
1977 +               goto unregister_host_class;
1978 +
1979 +       return 0;
1980 +
1981 +unregister_host_class:
1982 +       mmc_unregister_host_class();
1983 +unregister_bus:
1984 +       mmc_unregister_bus();
1985 +destroy_workqueue:
1986 +       destroy_workqueue(workqueue);
1987 +
1988         return ret;
1989  }
1990  
1991  static void __exit mmc_exit(void)
1992  {
1993 +       sdio_unregister_bus();
1994         mmc_unregister_host_class();
1995         mmc_unregister_bus();
1996         destroy_workqueue(workqueue);
1997  }
1998  
1999 -module_init(mmc_init);
2000 +subsys_initcall(mmc_init);
2001  module_exit(mmc_exit);
2002  
2003  MODULE_LICENSE("GPL");
2004 Index: linux-2.6.23.17/drivers/mmc/core/core.h
2005 ===================================================================
2006 --- linux-2.6.23.17.orig/drivers/mmc/core/core.h
2007 +++ linux-2.6.23.17/drivers/mmc/core/core.h
2008 @@ -48,5 +48,7 @@ void mmc_rescan(struct work_struct *work
2009  void mmc_start_host(struct mmc_host *host);
2010  void mmc_stop_host(struct mmc_host *host);
2011  
2012 +extern int use_spi_crc;
2013 +
2014  #endif
2015  
2016 Index: linux-2.6.23.17/drivers/mmc/core/host.c
2017 ===================================================================
2018 --- linux-2.6.23.17.orig/drivers/mmc/core/host.c
2019 +++ linux-2.6.23.17/drivers/mmc/core/host.c
2020 @@ -15,6 +15,7 @@
2021  #include <linux/err.h>
2022  #include <linux/idr.h>
2023  #include <linux/pagemap.h>
2024 +#include <linux/leds.h>
2025  
2026  #include <linux/mmc/host.h>
2027  
2028 @@ -100,6 +101,9 @@ int mmc_add_host(struct mmc_host *host)
2029  {
2030         int err;
2031  
2032 +       WARN_ON((host->caps & MMC_CAP_SDIO_IRQ) &&
2033 +               !host->ops->enable_sdio_irq);
2034 +
2035         if (!idr_pre_get(&mmc_host_idr, GFP_KERNEL))
2036                 return -ENOMEM;
2037  
2038 @@ -112,6 +116,8 @@ int mmc_add_host(struct mmc_host *host)
2039         snprintf(host->class_dev.bus_id, BUS_ID_SIZE,
2040                  "mmc%d", host->index);
2041  
2042 +       led_trigger_register_simple(host->class_dev.bus_id, &host->led);
2043 +
2044         err = device_add(&host->class_dev);
2045         if (err)
2046                 return err;
2047 @@ -137,6 +143,8 @@ void mmc_remove_host(struct mmc_host *ho
2048  
2049         device_del(&host->class_dev);
2050  
2051 +       led_trigger_unregister_simple(host->led);
2052 +
2053         spin_lock(&mmc_host_lock);
2054         idr_remove(&mmc_host_idr, host->index);
2055         spin_unlock(&mmc_host_lock);
2056 Index: linux-2.6.23.17/drivers/mmc/core/mmc.c
2057 ===================================================================
2058 --- linux-2.6.23.17.orig/drivers/mmc/core/mmc.c
2059 +++ linux-2.6.23.17/drivers/mmc/core/mmc.c
2060 @@ -161,13 +161,12 @@ static int mmc_read_ext_csd(struct mmc_c
2061  {
2062         int err;
2063         u8 *ext_csd;
2064 +       unsigned int ext_csd_struct;
2065  
2066         BUG_ON(!card);
2067  
2068 -       err = MMC_ERR_FAILED;
2069 -
2070         if (card->csd.mmca_vsn < CSD_SPEC_VER_4)
2071 -               return MMC_ERR_NONE;
2072 +               return 0;
2073  
2074         /*
2075          * As the ext_csd is so large and mostly unused, we don't store the
2076 @@ -176,13 +175,19 @@ static int mmc_read_ext_csd(struct mmc_c
2077         ext_csd = kmalloc(512, GFP_KERNEL);
2078         if (!ext_csd) {
2079                 printk(KERN_ERR "%s: could not allocate a buffer to "
2080 -                       "receive the ext_csd. mmc v4 cards will be "
2081 -                       "treated as v3.\n", mmc_hostname(card->host));
2082 -               return MMC_ERR_FAILED;
2083 +                       "receive the ext_csd.\n", mmc_hostname(card->host));
2084 +               return -ENOMEM;
2085         }
2086  
2087         err = mmc_send_ext_csd(card, ext_csd);
2088 -       if (err != MMC_ERR_NONE) {
2089 +       if (err) {
2090 +               /*
2091 +                * We all hosts that cannot perform the command
2092 +                * to fail more gracefully
2093 +                */
2094 +               if (err != -EINVAL)
2095 +                       goto out;
2096 +
2097                 /*
2098                  * High capacity cards should have this "magic" size
2099                  * stored in their CSD.
2100 @@ -197,18 +202,30 @@ static int mmc_read_ext_csd(struct mmc_c
2101                                 "EXT_CSD, performance might "
2102                                 "suffer.\n",
2103                                 mmc_hostname(card->host));
2104 -                       err = MMC_ERR_NONE;
2105 +                       err = 0;
2106                 }
2107 +
2108                 goto out;
2109         }
2110  
2111 -       card->ext_csd.sectors =
2112 -               ext_csd[EXT_CSD_SEC_CNT + 0] << 0 |
2113 -               ext_csd[EXT_CSD_SEC_CNT + 1] << 8 |
2114 -               ext_csd[EXT_CSD_SEC_CNT + 2] << 16 |
2115 -               ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
2116 -       if (card->ext_csd.sectors)
2117 -               mmc_card_set_blockaddr(card);
2118 +       ext_csd_struct = ext_csd[EXT_CSD_REV];
2119 +       if (ext_csd_struct > 2) {
2120 +               printk(KERN_ERR "%s: unrecognised EXT_CSD structure "
2121 +                       "version %d\n", mmc_hostname(card->host),
2122 +                       ext_csd_struct);
2123 +               err = -EINVAL;
2124 +               goto out;
2125 +       }
2126 +
2127 +       if (ext_csd_struct >= 2) {
2128 +               card->ext_csd.sectors =
2129 +                       ext_csd[EXT_CSD_SEC_CNT + 0] << 0 |
2130 +                       ext_csd[EXT_CSD_SEC_CNT + 1] << 8 |
2131 +                       ext_csd[EXT_CSD_SEC_CNT + 2] << 16 |
2132 +                       ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
2133 +               if (card->ext_csd.sectors)
2134 +                       mmc_card_set_blockaddr(card);
2135 +       }
2136  
2137         switch (ext_csd[EXT_CSD_CARD_TYPE]) {
2138         case EXT_CSD_CARD_TYPE_52 | EXT_CSD_CARD_TYPE_26:
2139 @@ -246,7 +263,7 @@ static int mmc_init_card(struct mmc_host
2140         unsigned int max_dtr;
2141  
2142         BUG_ON(!host);
2143 -       BUG_ON(!host->claimed);
2144 +       WARN_ON(!host->claimed);
2145  
2146         /*
2147          * Since we're changing the OCR value, we seem to
2148 @@ -258,19 +275,33 @@ static int mmc_init_card(struct mmc_host
2149  
2150         /* The extra bit indicates that we support high capacity */
2151         err = mmc_send_op_cond(host, ocr | (1 << 30), NULL);
2152 -       if (err != MMC_ERR_NONE)
2153 +       if (err)
2154                 goto err;
2155  
2156         /*
2157 +        * For SPI, enable CRC as appropriate.
2158 +        */
2159 +       if (mmc_host_is_spi(host)) {
2160 +               err = mmc_spi_set_crc(host, use_spi_crc);
2161 +               if (err)
2162 +                       goto err;
2163 +       }
2164 +
2165 +       /*
2166          * Fetch CID from card.
2167          */
2168 -       err = mmc_all_send_cid(host, cid);
2169 -       if (err != MMC_ERR_NONE)
2170 +       if (mmc_host_is_spi(host))
2171 +               err = mmc_send_cid(host, cid);
2172 +       else
2173 +               err = mmc_all_send_cid(host, cid);
2174 +       if (err)
2175                 goto err;
2176  
2177         if (oldcard) {
2178 -               if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0)
2179 +               if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
2180 +                       err = -ENOENT;
2181                         goto err;
2182 +               }
2183  
2184                 card = oldcard;
2185         } else {
2186 @@ -278,8 +309,10 @@ static int mmc_init_card(struct mmc_host
2187                  * Allocate card structure.
2188                  */
2189                 card = mmc_alloc_card(host);
2190 -               if (IS_ERR(card))
2191 +               if (IS_ERR(card)) {
2192 +                       err = PTR_ERR(card);
2193                         goto err;
2194 +               }
2195  
2196                 card->type = MMC_TYPE_MMC;
2197                 card->rca = 1;
2198 @@ -287,43 +320,47 @@ static int mmc_init_card(struct mmc_host
2199         }
2200  
2201         /*
2202 -        * Set card RCA.
2203 +        * For native busses:  set card RCA and quit open drain mode.
2204          */
2205 -       err = mmc_set_relative_addr(card);
2206 -       if (err != MMC_ERR_NONE)
2207 -               goto free_card;
2208 +       if (!mmc_host_is_spi(host)) {
2209 +               err = mmc_set_relative_addr(card);
2210 +               if (err)
2211 +                       goto free_card;
2212  
2213 -       mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
2214 +               mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
2215 +       }
2216  
2217         if (!oldcard) {
2218                 /*
2219                  * Fetch CSD from card.
2220                  */
2221                 err = mmc_send_csd(card, card->raw_csd);
2222 -               if (err != MMC_ERR_NONE)
2223 +               if (err)
2224                         goto free_card;
2225  
2226                 err = mmc_decode_csd(card);
2227 -               if (err < 0)
2228 +               if (err)
2229                         goto free_card;
2230                 err = mmc_decode_cid(card);
2231 -               if (err < 0)
2232 +               if (err)
2233                         goto free_card;
2234         }
2235  
2236         /*
2237          * Select card, as all following commands rely on that.
2238          */
2239 -       err = mmc_select_card(card);
2240 -       if (err != MMC_ERR_NONE)
2241 -               goto free_card;
2242 +       if (!mmc_host_is_spi(host)) {
2243 +               err = mmc_select_card(card);
2244 +               if (err)
2245 +                       goto free_card;
2246 +       }
2247  
2248         if (!oldcard) {
2249                 /*
2250 -                * Fetch and process extened CSD.
2251 +                * Fetch and process extended CSD.
2252                  */
2253                 err = mmc_read_ext_csd(card);
2254 -               if (err != MMC_ERR_NONE)
2255 +               if (err)
2256                         goto free_card;
2257         }
2258  
2259 @@ -334,7 +371,7 @@ static int mmc_init_card(struct mmc_host
2260                 (host->caps & MMC_CAP_MMC_HIGHSPEED)) {
2261                 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
2262                         EXT_CSD_HS_TIMING, 1);
2263 -               if (err != MMC_ERR_NONE)
2264 +               if (err)
2265                         goto free_card;
2266  
2267                 mmc_card_set_highspeed(card);
2268 @@ -363,7 +400,7 @@ static int mmc_init_card(struct mmc_host
2269                 (host->caps & MMC_CAP_4_BIT_DATA)) {
2270                 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
2271                         EXT_CSD_BUS_WIDTH, EXT_CSD_BUS_WIDTH_4);
2272 -               if (err != MMC_ERR_NONE)
2273 +               if (err)
2274                         goto free_card;
2275  
2276                 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
2277 @@ -372,14 +409,14 @@ static int mmc_init_card(struct mmc_host
2278         if (!oldcard)
2279                 host->card = card;
2280  
2281 -       return MMC_ERR_NONE;
2282 +       return 0;
2283  
2284  free_card:
2285         if (!oldcard)
2286                 mmc_remove_card(card);
2287  err:
2288  
2289 -       return MMC_ERR_FAILED;
2290 +       return err;
2291  }
2292  
2293  /*
2294 @@ -413,7 +450,7 @@ static void mmc_detect(struct mmc_host *
2295  
2296         mmc_release_host(host);
2297  
2298 -       if (err != MMC_ERR_NONE) {
2299 +       if (err) {
2300                 mmc_remove(host);
2301  
2302                 mmc_claim_host(host);
2303 @@ -480,7 +517,8 @@ static void mmc_suspend(struct mmc_host 
2304         BUG_ON(!host->card);
2305  
2306         mmc_claim_host(host);
2307 -       mmc_deselect_cards(host);
2308 +       if (!mmc_host_is_spi(host))
2309 +               mmc_deselect_cards(host);
2310         host->card->state &= ~MMC_STATE_HIGHSPEED;
2311         mmc_release_host(host);
2312  }
2313 @@ -502,7 +540,7 @@ static void mmc_resume(struct mmc_host *
2314         err = mmc_init_card(host, host->ocr, host->card);
2315         mmc_release_host(host);
2316  
2317 -       if (err != MMC_ERR_NONE) {
2318 +       if (err) {
2319                 mmc_remove(host);
2320  
2321                 mmc_claim_host(host);
2322 @@ -536,11 +574,20 @@ int mmc_attach_mmc(struct mmc_host *host
2323         int err;
2324  
2325         BUG_ON(!host);
2326 -       BUG_ON(!host->claimed);
2327 +       WARN_ON(!host->claimed);
2328  
2329         mmc_attach_bus(host, &mmc_ops);
2330  
2331         /*
2332 +        * We need to get OCR a different way for SPI.
2333 +        */
2334 +       if (mmc_host_is_spi(host)) {
2335 +               err = mmc_spi_read_ocr(host, 1, &ocr);
2336 +               if (err)
2337 +                       goto err;
2338 +       }
2339 +
2340 +       /*
2341          * Sanity check the voltages that the card claims to
2342          * support.
2343          */
2344 @@ -565,7 +612,7 @@ int mmc_attach_mmc(struct mmc_host *host
2345          * Detect and init the card.
2346          */
2347         err = mmc_init_card(host, host->ocr, NULL);
2348 -       if (err != MMC_ERR_NONE)
2349 +       if (err)
2350                 goto err;
2351  
2352         mmc_release_host(host);
2353 @@ -587,6 +634,6 @@ err:
2354         printk(KERN_ERR "%s: error %d whilst initialising MMC card\n",
2355                 mmc_hostname(host), err);
2356  
2357 -       return 0;
2358 +       return err;
2359  }
2360  
2361 Index: linux-2.6.23.17/drivers/mmc/core/mmc_ops.c
2362 ===================================================================
2363 --- linux-2.6.23.17.orig/drivers/mmc/core/mmc_ops.c
2364 +++ linux-2.6.23.17/drivers/mmc/core/mmc_ops.c
2365 @@ -10,7 +10,6 @@
2366   */
2367  
2368  #include <linux/types.h>
2369 -#include <asm/scatterlist.h>
2370  #include <linux/scatterlist.h>
2371  
2372  #include <linux/mmc/host.h>
2373 @@ -40,10 +39,10 @@ static int _mmc_select_card(struct mmc_h
2374         }
2375  
2376         err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
2377 -       if (err != MMC_ERR_NONE)
2378 +       if (err)
2379                 return err;
2380  
2381 -       return MMC_ERR_NONE;
2382 +       return 0;
2383  }
2384  
2385  int mmc_select_card(struct mmc_card *card)
2386 @@ -63,23 +62,36 @@ int mmc_go_idle(struct mmc_host *host)
2387         int err;
2388         struct mmc_command cmd;
2389  
2390 -       mmc_set_chip_select(host, MMC_CS_HIGH);
2391 -
2392 -       mmc_delay(1);
2393 +       /*
2394 +        * Non-SPI hosts need to prevent chipselect going active during
2395 +        * GO_IDLE; that would put chips into SPI mode.  Remind them of
2396 +        * that in case of hardware that won't pull up DAT3/nCS otherwise.
2397 +        *
2398 +        * SPI hosts ignore ios.chip_select; it's managed according to
2399 +        * rules that must accomodate non-MMC slaves which this layer
2400 +        * won't even know about.
2401 +        */
2402 +       if (!mmc_host_is_spi(host)) {
2403 +               mmc_set_chip_select(host, MMC_CS_HIGH);
2404 +               mmc_delay(1);
2405 +       }
2406  
2407         memset(&cmd, 0, sizeof(struct mmc_command));
2408  
2409         cmd.opcode = MMC_GO_IDLE_STATE;
2410         cmd.arg = 0;
2411 -       cmd.flags = MMC_RSP_NONE | MMC_CMD_BC;
2412 +       cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_NONE | MMC_CMD_BC;
2413  
2414         err = mmc_wait_for_cmd(host, &cmd, 0);
2415  
2416         mmc_delay(1);
2417  
2418 -       mmc_set_chip_select(host, MMC_CS_DONTCARE);
2419 +       if (!mmc_host_is_spi(host)) {
2420 +               mmc_set_chip_select(host, MMC_CS_DONTCARE);
2421 +               mmc_delay(1);
2422 +       }
2423  
2424 -       mmc_delay(1);
2425 +       host->use_spi_crc = 0;
2426  
2427         return err;
2428  }
2429 @@ -94,23 +106,33 @@ int mmc_send_op_cond(struct mmc_host *ho
2430         memset(&cmd, 0, sizeof(struct mmc_command));
2431  
2432         cmd.opcode = MMC_SEND_OP_COND;
2433 -       cmd.arg = ocr;
2434 -       cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
2435 +       cmd.arg = mmc_host_is_spi(host) ? 0 : ocr;
2436 +       cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R3 | MMC_CMD_BCR;
2437  
2438         for (i = 100; i; i--) {
2439                 err = mmc_wait_for_cmd(host, &cmd, 0);
2440 -               if (err != MMC_ERR_NONE)
2441 +               if (err)
2442                         break;
2443  
2444 -               if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0)
2445 +               /* if we're just probing, do a single pass */
2446 +               if (ocr == 0)
2447                         break;
2448  
2449 -               err = MMC_ERR_TIMEOUT;
2450 +               /* otherwise wait until reset completes */
2451 +               if (mmc_host_is_spi(host)) {
2452 +                       if (!(cmd.resp[0] & R1_SPI_IDLE))
2453 +                               break;
2454 +               } else {
2455 +                       if (cmd.resp[0] & MMC_CARD_BUSY)
2456 +                               break;
2457 +               }
2458 +
2459 +               err = -ETIMEDOUT;
2460  
2461                 mmc_delay(10);
2462         }
2463  
2464 -       if (rocr)
2465 +       if (rocr && !mmc_host_is_spi(host))
2466                 *rocr = cmd.resp[0];
2467  
2468         return err;
2469 @@ -131,12 +153,12 @@ int mmc_all_send_cid(struct mmc_host *ho
2470         cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
2471  
2472         err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
2473 -       if (err != MMC_ERR_NONE)
2474 +       if (err)
2475                 return err;
2476  
2477         memcpy(cid, cmd.resp, sizeof(u32) * 4);
2478  
2479 -       return MMC_ERR_NONE;
2480 +       return 0;
2481  }
2482  
2483  int mmc_set_relative_addr(struct mmc_card *card)
2484 @@ -154,46 +176,52 @@ int mmc_set_relative_addr(struct mmc_car
2485         cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
2486  
2487         err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
2488 -       if (err != MMC_ERR_NONE)
2489 +       if (err)
2490                 return err;
2491  
2492 -       return MMC_ERR_NONE;
2493 +       return 0;
2494  }
2495  
2496 -int mmc_send_csd(struct mmc_card *card, u32 *csd)
2497 +static int
2498 +mmc_send_cxd_native(struct mmc_host *host, u32 arg, u32 *cxd, int opcode)
2499  {
2500         int err;
2501         struct mmc_command cmd;
2502  
2503 -       BUG_ON(!card);
2504 -       BUG_ON(!card->host);
2505 -       BUG_ON(!csd);
2506 +       BUG_ON(!host);
2507 +       BUG_ON(!cxd);
2508  
2509         memset(&cmd, 0, sizeof(struct mmc_command));
2510  
2511 -       cmd.opcode = MMC_SEND_CSD;
2512 -       cmd.arg = card->rca << 16;
2513 +       cmd.opcode = opcode;
2514 +       cmd.arg = arg;
2515         cmd.flags = MMC_RSP_R2 | MMC_CMD_AC;
2516  
2517 -       err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
2518 -       if (err != MMC_ERR_NONE)
2519 +       err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
2520 +       if (err)
2521                 return err;
2522  
2523 -       memcpy(csd, cmd.resp, sizeof(u32) * 4);
2524 +       memcpy(cxd, cmd.resp, sizeof(u32) * 4);
2525  
2526 -       return MMC_ERR_NONE;
2527 +       return 0;
2528  }
2529  
2530 -int mmc_send_ext_csd(struct mmc_card *card, u8 *ext_csd)
2531 +static int
2532 +mmc_send_cxd_data(struct mmc_card *card, struct mmc_host *host,
2533 +               u32 opcode, void *buf, unsigned len)
2534  {
2535         struct mmc_request mrq;
2536         struct mmc_command cmd;
2537         struct mmc_data data;
2538         struct scatterlist sg;
2539 +       void *data_buf;
2540  
2541 -       BUG_ON(!card);
2542 -       BUG_ON(!card->host);
2543 -       BUG_ON(!ext_csd);
2544 +       /* dma onto stack is unsafe/nonportable, but callers to this
2545 +        * routine normally provide temporary on-stack buffers ...
2546 +        */
2547 +       data_buf = kmalloc(len, GFP_KERNEL);
2548 +       if (data_buf == NULL)
2549 +               return -ENOMEM;
2550  
2551         memset(&mrq, 0, sizeof(struct mmc_request));
2552         memset(&cmd, 0, sizeof(struct mmc_command));
2553 @@ -202,28 +230,117 @@ int mmc_send_ext_csd(struct mmc_card *ca
2554         mrq.cmd = &cmd;
2555         mrq.data = &data;
2556  
2557 -       cmd.opcode = MMC_SEND_EXT_CSD;
2558 +       cmd.opcode = opcode;
2559         cmd.arg = 0;
2560 -       cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
2561  
2562 -       data.blksz = 512;
2563 +       /* NOTE HACK:  the MMC_RSP_SPI_R1 is always correct here, but we
2564 +        * rely on callers to never use this with "native" calls for reading
2565 +        * CSD or CID.  Native versions of those commands use the R2 type,
2566 +        * not R1 plus a data block.
2567 +        */
2568 +       cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
2569 +
2570 +       data.blksz = len;
2571         data.blocks = 1;
2572         data.flags = MMC_DATA_READ;
2573         data.sg = &sg;
2574         data.sg_len = 1;
2575  
2576 -       sg_init_one(&sg, ext_csd, 512);
2577 +       sg_init_one(&sg, data_buf, len);
2578 +
2579 +       if (card)
2580 +               mmc_set_data_timeout(&data, card);
2581  
2582 -       mmc_set_data_timeout(&data, card, 0);
2583 +       mmc_wait_for_req(host, &mrq);
2584  
2585 -       mmc_wait_for_req(card->host, &mrq);
2586 +       memcpy(buf, data_buf, len);
2587 +       kfree(data_buf);
2588  
2589 -       if (cmd.error != MMC_ERR_NONE)
2590 +       if (cmd.error)
2591                 return cmd.error;
2592 -       if (data.error != MMC_ERR_NONE)
2593 +       if (data.error)
2594                 return data.error;
2595  
2596 -       return MMC_ERR_NONE;
2597 +       return 0;
2598 +}
2599 +
2600 +int mmc_send_csd(struct mmc_card *card, u32 *csd)
2601 +{
2602 +       int ret, i;
2603 +
2604 +       if (!mmc_host_is_spi(card->host))
2605 +               return mmc_send_cxd_native(card->host, card->rca << 16,
2606 +                               csd, MMC_SEND_CSD);
2607 +
2608 +       ret = mmc_send_cxd_data(card, card->host, MMC_SEND_CSD, csd, 16);
2609 +       if (ret)
2610 +               return ret;
2611 +
2612 +       for (i = 0;i < 4;i++)
2613 +               csd[i] = be32_to_cpu(csd[i]);
2614 +
2615 +       return 0;
2616 +}
2617 +
2618 +int mmc_send_cid(struct mmc_host *host, u32 *cid)
2619 +{
2620 +       int ret, i;
2621 +
2622 +       if (!mmc_host_is_spi(host)) {
2623 +               if (!host->card)
2624 +                       return -EINVAL;
2625 +               return mmc_send_cxd_native(host, host->card->rca << 16,
2626 +                               cid, MMC_SEND_CID);
2627 +       }
2628 +
2629 +       ret = mmc_send_cxd_data(NULL, host, MMC_SEND_CID, cid, 16);
2630 +       if (ret)
2631 +               return ret;
2632 +
2633 +       for (i = 0;i < 4;i++)
2634 +               cid[i] = be32_to_cpu(cid[i]);
2635 +
2636 +       return 0;
2637 +}
2638 +
2639 +int mmc_send_ext_csd(struct mmc_card *card, u8 *ext_csd)
2640 +{
2641 +       return mmc_send_cxd_data(card, card->host, MMC_SEND_EXT_CSD,
2642 +                       ext_csd, 512);
2643 +}
2644 +
2645 +int mmc_spi_read_ocr(struct mmc_host *host, int highcap, u32 *ocrp)
2646 +{
2647 +       struct mmc_command cmd;
2648 +       int err;
2649 +
2650 +       memset(&cmd, 0, sizeof(struct mmc_command));
2651 +
2652 +       cmd.opcode = MMC_SPI_READ_OCR;
2653 +       cmd.arg = highcap ? (1 << 30) : 0;
2654 +       cmd.flags = MMC_RSP_SPI_R3;
2655 +
2656 +       err = mmc_wait_for_cmd(host, &cmd, 0);
2657 +
2658 +       *ocrp = cmd.resp[1];
2659 +       return err;
2660 +}
2661 +
2662 +int mmc_spi_set_crc(struct mmc_host *host, int use_crc)
2663 +{
2664 +       struct mmc_command cmd;
2665 +       int err;
2666 +
2667 +       memset(&cmd, 0, sizeof(struct mmc_command));
2668 +
2669 +       cmd.opcode = MMC_SPI_CRC_ON_OFF;
2670 +       cmd.flags = MMC_RSP_SPI_R1;
2671 +       cmd.arg = use_crc;
2672 +
2673 +       err = mmc_wait_for_cmd(host, &cmd, 0);
2674 +       if (!err)
2675 +               host->use_spi_crc = use_crc;
2676 +       return err;
2677  }
2678  
2679  int mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value)
2680 @@ -241,13 +358,13 @@ int mmc_switch(struct mmc_card *card, u8
2681                   (index << 16) |
2682                   (value << 8) |
2683                   set;
2684 -       cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
2685 +       cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
2686  
2687         err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
2688 -       if (err != MMC_ERR_NONE)
2689 +       if (err)
2690                 return err;
2691  
2692 -       return MMC_ERR_NONE;
2693 +       return 0;
2694  }
2695  
2696  int mmc_send_status(struct mmc_card *card, u32 *status)
2697 @@ -261,16 +378,20 @@ int mmc_send_status(struct mmc_card *car
2698         memset(&cmd, 0, sizeof(struct mmc_command));
2699  
2700         cmd.opcode = MMC_SEND_STATUS;
2701 -       cmd.arg = card->rca << 16;
2702 -       cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
2703 +       if (!mmc_host_is_spi(card->host))
2704 +               cmd.arg = card->rca << 16;
2705 +       cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
2706  
2707         err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES);
2708 -       if (err != MMC_ERR_NONE)
2709 +       if (err)
2710                 return err;
2711  
2712 +       /* NOTE: callers are required to understand the difference
2713 +        * between "native" and SPI format status words!
2714 +        */
2715         if (status)
2716                 *status = cmd.resp[0];
2717  
2718 -       return MMC_ERR_NONE;
2719 +       return 0;
2720  }
2721  
2722 Index: linux-2.6.23.17/drivers/mmc/core/mmc_ops.h
2723 ===================================================================
2724 --- linux-2.6.23.17.orig/drivers/mmc/core/mmc_ops.h
2725 +++ linux-2.6.23.17/drivers/mmc/core/mmc_ops.h
2726 @@ -22,6 +22,9 @@ int mmc_send_csd(struct mmc_card *card, 
2727  int mmc_send_ext_csd(struct mmc_card *card, u8 *ext_csd);
2728  int mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value);
2729  int mmc_send_status(struct mmc_card *card, u32 *status);
2730 +int mmc_send_cid(struct mmc_host *host, u32 *cid);
2731 +int mmc_spi_read_ocr(struct mmc_host *host, int highcap, u32 *ocrp);
2732 +int mmc_spi_set_crc(struct mmc_host *host, int use_crc);
2733  
2734  #endif
2735  
2736 Index: linux-2.6.23.17/drivers/mmc/core/sd.c
2737 ===================================================================
2738 --- linux-2.6.23.17.orig/drivers/mmc/core/sd.c
2739 +++ linux-2.6.23.17/drivers/mmc/core/sd.c
2740 @@ -166,8 +166,6 @@ static int mmc_decode_scr(struct mmc_car
2741         unsigned int scr_struct;
2742         u32 resp[4];
2743  
2744 -       BUG_ON(!mmc_card_sd(card));
2745 -
2746         resp[3] = card->raw_scr[1];
2747         resp[2] = card->raw_scr[0];
2748  
2749 @@ -193,30 +191,38 @@ static int mmc_read_switch(struct mmc_ca
2750         u8 *status;
2751  
2752         if (card->scr.sda_vsn < SCR_SPEC_VER_1)
2753 -               return MMC_ERR_NONE;
2754 +               return 0;
2755  
2756         if (!(card->csd.cmdclass & CCC_SWITCH)) {
2757                 printk(KERN_WARNING "%s: card lacks mandatory switch "
2758                         "function, performance might suffer.\n",
2759                         mmc_hostname(card->host));
2760 -               return MMC_ERR_NONE;
2761 +               return 0;
2762         }
2763  
2764 -       err = MMC_ERR_FAILED;
2765 +       err = -EIO;
2766  
2767         status = kmalloc(64, GFP_KERNEL);
2768         if (!status) {
2769                 printk(KERN_ERR "%s: could not allocate a buffer for "
2770                         "switch capabilities.\n", mmc_hostname(card->host));
2771 -               return err;
2772 +               return -ENOMEM;
2773         }
2774  
2775         err = mmc_sd_switch(card, 0, 0, 1, status);
2776 -       if (err != MMC_ERR_NONE) {
2777 +       if (err) {
2778 +               /*
2779 +                * We all hosts that cannot perform the command
2780 +                * to fail more gracefully
2781 +                */
2782 +               if (err != -EINVAL)
2783 +                       goto out;
2784 +
2785                 printk(KERN_WARNING "%s: problem reading switch "
2786                         "capabilities, performance might suffer.\n",
2787                         mmc_hostname(card->host));
2788 -               err = MMC_ERR_NONE;
2789 +               err = 0;
2790 +
2791                 goto out;
2792         }
2793  
2794 @@ -238,28 +244,28 @@ static int mmc_switch_hs(struct mmc_card
2795         u8 *status;
2796  
2797         if (card->scr.sda_vsn < SCR_SPEC_VER_1)
2798 -               return MMC_ERR_NONE;
2799 +               return 0;
2800  
2801         if (!(card->csd.cmdclass & CCC_SWITCH))
2802 -               return MMC_ERR_NONE;
2803 +               return 0;
2804  
2805         if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
2806 -               return MMC_ERR_NONE;
2807 +               return 0;
2808  
2809         if (card->sw_caps.hs_max_dtr == 0)
2810 -               return MMC_ERR_NONE;
2811 +               return 0;
2812  
2813 -       err = MMC_ERR_FAILED;
2814 +       err = -EIO;
2815  
2816         status = kmalloc(64, GFP_KERNEL);
2817         if (!status) {
2818                 printk(KERN_ERR "%s: could not allocate a buffer for "
2819                         "switch capabilities.\n", mmc_hostname(card->host));
2820 -               return err;
2821 +               return -ENOMEM;
2822         }
2823  
2824         err = mmc_sd_switch(card, 1, 0, 1, status);
2825 -       if (err != MMC_ERR_NONE)
2826 +       if (err)
2827                 goto out;
2828  
2829         if ((status[16] & 0xF) != 1) {
2830 @@ -292,7 +298,7 @@ static int mmc_sd_init_card(struct mmc_h
2831         unsigned int max_dtr;
2832  
2833         BUG_ON(!host);
2834 -       BUG_ON(!host->claimed);
2835 +       WARN_ON(!host->claimed);
2836  
2837         /*
2838          * Since we're changing the OCR value, we seem to
2839 @@ -309,23 +315,37 @@ static int mmc_sd_init_card(struct mmc_h
2840          * block-addressed SDHC cards.
2841          */
2842         err = mmc_send_if_cond(host, ocr);
2843 -       if (err == MMC_ERR_NONE)
2844 +       if (!err)
2845                 ocr |= 1 << 30;
2846  
2847         err = mmc_send_app_op_cond(host, ocr, NULL);
2848 -       if (err != MMC_ERR_NONE)
2849 +       if (err)
2850                 goto err;
2851  
2852         /*
2853 +        * For SPI, enable CRC as appropriate.
2854 +        */
2855 +       if (mmc_host_is_spi(host)) {
2856 +               err = mmc_spi_set_crc(host, use_spi_crc);
2857 +               if (err)
2858 +                       goto err;
2859 +       }
2860 +
2861 +       /*
2862          * Fetch CID from card.
2863          */
2864 -       err = mmc_all_send_cid(host, cid);
2865 -       if (err != MMC_ERR_NONE)
2866 +       if (mmc_host_is_spi(host))
2867 +               err = mmc_send_cid(host, cid);
2868 +       else
2869 +               err = mmc_all_send_cid(host, cid);
2870 +       if (err)
2871                 goto err;
2872  
2873         if (oldcard) {
2874 -               if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0)
2875 +               if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
2876 +                       err = -ENOENT;
2877                         goto err;
2878 +               }
2879  
2880                 card = oldcard;
2881         } else {
2882 @@ -333,32 +353,36 @@ static int mmc_sd_init_card(struct mmc_h
2883                  * Allocate card structure.
2884                  */
2885                 card = mmc_alloc_card(host);
2886 -               if (IS_ERR(card))
2887 +               if (IS_ERR(card)) {
2888 +                       err = PTR_ERR(card);
2889                         goto err;
2890 +               }
2891  
2892                 card->type = MMC_TYPE_SD;
2893                 memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
2894         }
2895  
2896         /*
2897 -        * Set card RCA.
2898 +        * For native busses:  get card RCA and quit open drain mode.
2899          */
2900 -       err = mmc_send_relative_addr(host, &card->rca);
2901 -       if (err != MMC_ERR_NONE)
2902 -               goto free_card;
2903 +       if (!mmc_host_is_spi(host)) {
2904 +               err = mmc_send_relative_addr(host, &card->rca);
2905 +               if (err)
2906 +                       goto free_card;
2907  
2908 -       mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
2909 +               mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
2910 +       }
2911  
2912         if (!oldcard) {
2913                 /*
2914                  * Fetch CSD from card.
2915                  */
2916                 err = mmc_send_csd(card, card->raw_csd);
2917 -               if (err != MMC_ERR_NONE)
2918 +               if (err)
2919                         goto free_card;
2920  
2921                 err = mmc_decode_csd(card);
2922 -               if (err < 0)
2923 +               if (err)
2924                         goto free_card;
2925  
2926                 mmc_decode_cid(card);
2927 @@ -367,16 +391,18 @@ static int mmc_sd_init_card(struct mmc_h
2928         /*
2929          * Select card, as all following commands rely on that.
2930          */
2931 -       err = mmc_select_card(card);
2932 -       if (err != MMC_ERR_NONE)
2933 -               goto free_card;
2934 +       if (!mmc_host_is_spi(host)) {
2935 +               err = mmc_select_card(card);
2936 +               if (err)
2937 +                       goto free_card;
2938 +       }
2939  
2940         if (!oldcard) {
2941                 /*
2942                  * Fetch SCR from card.
2943                  */
2944                 err = mmc_app_send_scr(card, card->raw_scr);
2945 -               if (err != MMC_ERR_NONE)
2946 +               if (err)
2947                         goto free_card;
2948  
2949                 err = mmc_decode_scr(card);
2950 @@ -387,7 +413,7 @@ static int mmc_sd_init_card(struct mmc_h
2951                  * Fetch switch information from card.
2952                  */
2953                 err = mmc_read_switch(card);
2954 -               if (err != MMC_ERR_NONE)
2955 +               if (err)
2956                         goto free_card;
2957         }
2958  
2959 @@ -395,7 +421,7 @@ static int mmc_sd_init_card(struct mmc_h
2960          * Attempt to change to high-speed (if supported)
2961          */
2962         err = mmc_switch_hs(card);
2963 -       if (err != MMC_ERR_NONE)
2964 +       if (err)
2965                 goto free_card;
2966  
2967         /*
2968 @@ -418,7 +444,7 @@ static int mmc_sd_init_card(struct mmc_h
2969         if ((host->caps & MMC_CAP_4_BIT_DATA) &&
2970                 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
2971                 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
2972 -               if (err != MMC_ERR_NONE)
2973 +               if (err)
2974                         goto free_card;
2975  
2976                 mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
2977 @@ -442,14 +468,14 @@ static int mmc_sd_init_card(struct mmc_h
2978         if (!oldcard)
2979                 host->card = card;
2980  
2981 -       return MMC_ERR_NONE;
2982 +       return 0;
2983  
2984  free_card:
2985         if (!oldcard)
2986                 mmc_remove_card(card);
2987  err:
2988  
2989 -       return MMC_ERR_FAILED;
2990 +       return err;
2991  }
2992  
2993  /*
2994 @@ -483,7 +509,7 @@ static void mmc_sd_detect(struct mmc_hos
2995  
2996         mmc_release_host(host);
2997  
2998 -       if (err != MMC_ERR_NONE) {
2999 +       if (err) {
3000                 mmc_sd_remove(host);
3001  
3002                 mmc_claim_host(host);
3003 @@ -552,7 +578,8 @@ static void mmc_sd_suspend(struct mmc_ho
3004         BUG_ON(!host->card);
3005  
3006         mmc_claim_host(host);
3007 -       mmc_deselect_cards(host);
3008 +       if (!mmc_host_is_spi(host))
3009 +               mmc_deselect_cards(host);
3010         host->card->state &= ~MMC_STATE_HIGHSPEED;
3011         mmc_release_host(host);
3012  }
3013 @@ -574,7 +601,7 @@ static void mmc_sd_resume(struct mmc_hos
3014         err = mmc_sd_init_card(host, host->ocr, host->card);
3015         mmc_release_host(host);
3016  
3017 -       if (err != MMC_ERR_NONE) {
3018 +       if (err) {
3019                 mmc_sd_remove(host);
3020  
3021                 mmc_claim_host(host);
3022 @@ -608,11 +635,22 @@ int mmc_attach_sd(struct mmc_host *host,
3023         int err;
3024  
3025         BUG_ON(!host);
3026 -       BUG_ON(!host->claimed);
3027 +       WARN_ON(!host->claimed);
3028  
3029         mmc_attach_bus(host, &mmc_sd_ops);
3030  
3031         /*
3032 +        * We need to get OCR a different way for SPI.
3033 +        */
3034 +       if (mmc_host_is_spi(host)) {
3035 +               mmc_go_idle(host);
3036 +
3037 +               err = mmc_spi_read_ocr(host, 0, &ocr);
3038 +               if (err)
3039 +                       goto err;
3040 +       }
3041 +
3042 +       /*
3043          * Sanity check the voltages that the card claims to
3044          * support.
3045          */
3046 @@ -644,7 +682,7 @@ int mmc_attach_sd(struct mmc_host *host,
3047          * Detect and init the card.
3048          */
3049         err = mmc_sd_init_card(host, host->ocr, NULL);
3050 -       if (err != MMC_ERR_NONE)
3051 +       if (err)
3052                 goto err;
3053  
3054         mmc_release_host(host);
3055 @@ -666,6 +704,6 @@ err:
3056         printk(KERN_ERR "%s: error %d whilst initialising SD card\n",
3057                 mmc_hostname(host), err);
3058  
3059 -       return 0;
3060 +       return err;
3061  }
3062  
3063 Index: linux-2.6.23.17/drivers/mmc/core/sd_ops.c
3064 ===================================================================
3065 --- linux-2.6.23.17.orig/drivers/mmc/core/sd_ops.c
3066 +++ linux-2.6.23.17/drivers/mmc/core/sd_ops.c
3067 @@ -10,7 +10,6 @@
3068   */
3069  
3070  #include <linux/types.h>
3071 -#include <asm/scatterlist.h>
3072  #include <linux/scatterlist.h>
3073  
3074  #include <linux/mmc/host.h>
3075 @@ -33,21 +32,21 @@ static int mmc_app_cmd(struct mmc_host *
3076  
3077         if (card) {
3078                 cmd.arg = card->rca << 16;
3079 -               cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
3080 +               cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
3081         } else {
3082                 cmd.arg = 0;
3083 -               cmd.flags = MMC_RSP_R1 | MMC_CMD_BCR;
3084 +               cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_BCR;
3085         }
3086  
3087         err = mmc_wait_for_cmd(host, &cmd, 0);
3088 -       if (err != MMC_ERR_NONE)
3089 +       if (err)
3090                 return err;
3091  
3092         /* Check that card supported application commands */
3093 -       if (!(cmd.resp[0] & R1_APP_CMD))
3094 -               return MMC_ERR_FAILED;
3095 +       if (!mmc_host_is_spi(host) && !(cmd.resp[0] & R1_APP_CMD))
3096 +               return -EOPNOTSUPP;
3097  
3098 -       return MMC_ERR_NONE;
3099 +       return 0;
3100  }
3101  
3102  /**
3103 @@ -73,7 +72,7 @@ int mmc_wait_for_app_cmd(struct mmc_host
3104         BUG_ON(!cmd);
3105         BUG_ON(retries < 0);
3106  
3107 -       err = MMC_ERR_INVALID;
3108 +       err = -EIO;
3109  
3110         /*
3111          * We have to resend MMC_APP_CMD for each attempt so
3112 @@ -83,8 +82,14 @@ int mmc_wait_for_app_cmd(struct mmc_host
3113                 memset(&mrq, 0, sizeof(struct mmc_request));
3114  
3115                 err = mmc_app_cmd(host, card);
3116 -               if (err != MMC_ERR_NONE)
3117 +               if (err) {
3118 +                       /* no point in retrying; no APP commands allowed */
3119 +                       if (mmc_host_is_spi(host)) {
3120 +                               if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
3121 +                                       break;
3122 +                       }
3123                         continue;
3124 +               }
3125  
3126                 memset(&mrq, 0, sizeof(struct mmc_request));
3127  
3128 @@ -97,8 +102,14 @@ int mmc_wait_for_app_cmd(struct mmc_host
3129                 mmc_wait_for_req(host, &mrq);
3130  
3131                 err = cmd->error;
3132 -               if (cmd->error == MMC_ERR_NONE)
3133 +               if (!cmd->error)
3134                         break;
3135 +
3136 +               /* no point in retrying illegal APP commands */
3137 +               if (mmc_host_is_spi(host)) {
3138 +                       if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
3139 +                               break;
3140 +               }
3141         }
3142  
3143         return err;
3144 @@ -127,14 +138,14 @@ int mmc_app_set_bus_width(struct mmc_car
3145                 cmd.arg = SD_BUS_WIDTH_4;
3146                 break;
3147         default:
3148 -               return MMC_ERR_INVALID;
3149 +               return -EINVAL;
3150         }
3151  
3152         err = mmc_wait_for_app_cmd(card->host, card, &cmd, MMC_CMD_RETRIES);
3153 -       if (err != MMC_ERR_NONE)
3154 +       if (err)
3155                 return err;
3156  
3157 -       return MMC_ERR_NONE;
3158 +       return 0;
3159  }
3160  
3161  int mmc_send_app_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
3162 @@ -147,23 +158,36 @@ int mmc_send_app_op_cond(struct mmc_host
3163         memset(&cmd, 0, sizeof(struct mmc_command));
3164  
3165         cmd.opcode = SD_APP_OP_COND;
3166 -       cmd.arg = ocr;
3167 -       cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
3168 +       if (mmc_host_is_spi(host))
3169 +               cmd.arg = ocr & (1 << 30); /* SPI only defines one bit */
3170 +       else
3171 +               cmd.arg = ocr;
3172 +       cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R3 | MMC_CMD_BCR;
3173  
3174         for (i = 100; i; i--) {
3175                 err = mmc_wait_for_app_cmd(host, NULL, &cmd, MMC_CMD_RETRIES);
3176 -               if (err != MMC_ERR_NONE)
3177 +               if (err)
3178                         break;
3179  
3180 -               if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0)
3181 +               /* if we're just probing, do a single pass */
3182 +               if (ocr == 0)
3183                         break;
3184  
3185 -               err = MMC_ERR_TIMEOUT;
3186 +               /* otherwise wait until reset completes */
3187 +               if (mmc_host_is_spi(host)) {
3188 +                       if (!(cmd.resp[0] & R1_SPI_IDLE))
3189 +                               break;
3190 +               } else {
3191 +                       if (cmd.resp[0] & MMC_CARD_BUSY)
3192 +                               break;
3193 +               }
3194 +
3195 +               err = -ETIMEDOUT;
3196  
3197                 mmc_delay(10);
3198         }
3199  
3200 -       if (rocr)
3201 +       if (rocr && !mmc_host_is_spi(host))
3202                 *rocr = cmd.resp[0];
3203  
3204         return err;
3205 @@ -174,6 +198,7 @@ int mmc_send_if_cond(struct mmc_host *ho
3206         struct mmc_command cmd;
3207         int err;
3208         static const u8 test_pattern = 0xAA;
3209 +       u8 result_pattern;
3210  
3211         /*
3212          * To support SD 2.0 cards, we must always invoke SD_SEND_IF_COND
3213 @@ -182,16 +207,21 @@ int mmc_send_if_cond(struct mmc_host *ho
3214          */
3215         cmd.opcode = SD_SEND_IF_COND;
3216         cmd.arg = ((ocr & 0xFF8000) != 0) << 8 | test_pattern;
3217 -       cmd.flags = MMC_RSP_R7 | MMC_CMD_BCR;
3218 +       cmd.flags = MMC_RSP_SPI_R7 | MMC_RSP_R7 | MMC_CMD_BCR;
3219  
3220         err = mmc_wait_for_cmd(host, &cmd, 0);
3221 -       if (err != MMC_ERR_NONE)
3222 +       if (err)
3223                 return err;
3224  
3225 -       if ((cmd.resp[0] & 0xFF) != test_pattern)
3226 -               return MMC_ERR_FAILED;
3227 +       if (mmc_host_is_spi(host))
3228 +               result_pattern = cmd.resp[1] & 0xFF;
3229 +       else
3230 +               result_pattern = cmd.resp[0] & 0xFF;
3231 +
3232 +       if (result_pattern != test_pattern)
3233 +               return -EIO;
3234  
3235 -       return MMC_ERR_NONE;
3236 +       return 0;
3237  }
3238  
3239  int mmc_send_relative_addr(struct mmc_host *host, unsigned int *rca)
3240 @@ -209,12 +239,12 @@ int mmc_send_relative_addr(struct mmc_ho
3241         cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
3242  
3243         err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
3244 -       if (err != MMC_ERR_NONE)
3245 +       if (err)
3246                 return err;
3247  
3248         *rca = cmd.resp[0] >> 16;
3249  
3250 -       return MMC_ERR_NONE;
3251 +       return 0;
3252  }
3253  
3254  int mmc_app_send_scr(struct mmc_card *card, u32 *scr)
3255 @@ -229,8 +259,10 @@ int mmc_app_send_scr(struct mmc_card *ca
3256         BUG_ON(!card->host);
3257         BUG_ON(!scr);
3258  
3259 +       /* NOTE: caller guarantees scr is heap-allocated */
3260 +
3261         err = mmc_app_cmd(card->host, card);
3262 -       if (err != MMC_ERR_NONE)
3263 +       if (err)
3264                 return err;
3265  
3266         memset(&mrq, 0, sizeof(struct mmc_request));
3267 @@ -242,7 +274,7 @@ int mmc_app_send_scr(struct mmc_card *ca
3268  
3269         cmd.opcode = SD_APP_SEND_SCR;
3270         cmd.arg = 0;
3271 -       cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
3272 +       cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
3273  
3274         data.blksz = 8;
3275         data.blocks = 1;
3276 @@ -252,19 +284,19 @@ int mmc_app_send_scr(struct mmc_card *ca
3277  
3278         sg_init_one(&sg, scr, 8);
3279  
3280 -       mmc_set_data_timeout(&data, card, 0);
3281 +       mmc_set_data_timeout(&data, card);
3282  
3283         mmc_wait_for_req(card->host, &mrq);
3284  
3285 -       if (cmd.error != MMC_ERR_NONE)
3286 +       if (cmd.error)
3287                 return cmd.error;
3288 -       if (data.error != MMC_ERR_NONE)
3289 +       if (data.error)
3290                 return data.error;
3291  
3292 -       scr[0] = ntohl(scr[0]);
3293 -       scr[1] = ntohl(scr[1]);
3294 +       scr[0] = be32_to_cpu(scr[0]);
3295 +       scr[1] = be32_to_cpu(scr[1]);
3296  
3297 -       return MMC_ERR_NONE;
3298 +       return 0;
3299  }
3300  
3301  int mmc_sd_switch(struct mmc_card *card, int mode, int group,
3302 @@ -278,6 +310,8 @@ int mmc_sd_switch(struct mmc_card *card,
3303         BUG_ON(!card);
3304         BUG_ON(!card->host);
3305  
3306 +       /* NOTE: caller guarantees resp is heap-allocated */
3307 +
3308         mode = !!mode;
3309         value &= 0xF;
3310  
3311 @@ -292,7 +326,7 @@ int mmc_sd_switch(struct mmc_card *card,
3312         cmd.arg = mode << 31 | 0x00FFFFFF;
3313         cmd.arg &= ~(0xF << (group * 4));
3314         cmd.arg |= value << (group * 4);
3315 -       cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
3316 +       cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
3317  
3318         data.blksz = 64;
3319         data.blocks = 1;
3320 @@ -302,15 +336,15 @@ int mmc_sd_switch(struct mmc_card *card,
3321  
3322         sg_init_one(&sg, resp, 64);
3323  
3324 -       mmc_set_data_timeout(&data, card, 0);
3325 +       mmc_set_data_timeout(&data, card);
3326  
3327         mmc_wait_for_req(card->host, &mrq);
3328  
3329 -       if (cmd.error != MMC_ERR_NONE)
3330 +       if (cmd.error)
3331                 return cmd.error;
3332 -       if (data.error != MMC_ERR_NONE)
3333 +       if (data.error)
3334                 return data.error;
3335  
3336 -       return MMC_ERR_NONE;
3337 +       return 0;
3338  }
3339  
3340 Index: linux-2.6.23.17/drivers/mmc/core/sdio.c
3341 ===================================================================
3342 --- /dev/null
3343 +++ linux-2.6.23.17/drivers/mmc/core/sdio.c
3344 @@ -0,0 +1,395 @@
3345 +/*
3346 + *  linux/drivers/mmc/sdio.c
3347 + *
3348 + *  Copyright 2006-2007 Pierre Ossman
3349 + *
3350 + * This program is free software; you can redistribute it and/or modify
3351 + * it under the terms of the GNU General Public License as published by
3352 + * the Free Software Foundation; either version 2 of the License, or (at
3353 + * your option) any later version.
3354 + */
3355 +
3356 +#include <linux/err.h>
3357 +
3358 +#include <linux/mmc/host.h>
3359 +#include <linux/mmc/card.h>
3360 +#include <linux/mmc/sdio.h>
3361 +#include <linux/mmc/sdio_func.h>
3362 +
3363 +#include "core.h"
3364 +#include "bus.h"
3365 +#include "sdio_bus.h"
3366 +#include "mmc_ops.h"
3367 +#include "sd_ops.h"
3368 +#include "sdio_ops.h"
3369 +#include "sdio_cis.h"
3370 +
3371 +static int sdio_read_fbr(struct sdio_func *func)
3372 +{
3373 +       int ret;
3374 +       unsigned char data;
3375 +
3376 +       ret = mmc_io_rw_direct(func->card, 0, 0,
3377 +               SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF, 0, &data);
3378 +       if (ret)
3379 +               goto out;
3380 +
3381 +       data &= 0x0f;
3382 +
3383 +       if (data == 0x0f) {
3384 +               ret = mmc_io_rw_direct(func->card, 0, 0,
3385 +                       SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF_EXT, 0, &data);
3386 +               if (ret)
3387 +                       goto out;
3388 +       }
3389 +
3390 +       func->class = data;
3391 +
3392 +out:
3393 +       return ret;
3394 +}
3395 +
3396 +static int sdio_init_func(struct mmc_card *card, unsigned int fn)
3397 +{
3398 +       int ret;
3399 +       struct sdio_func *func;
3400 +
3401 +       BUG_ON(fn > SDIO_MAX_FUNCS);
3402 +
3403 +       func = sdio_alloc_func(card);
3404 +       if (IS_ERR(func))
3405 +               return PTR_ERR(func);
3406 +
3407 +       func->num = fn;
3408 +
3409 +       ret = sdio_read_fbr(func);
3410 +       if (ret)
3411 +               goto fail;
3412 +
3413 +       ret = sdio_read_func_cis(func);
3414 +       if (ret)
3415 +               goto fail;
3416 +
3417 +       card->sdio_func[fn - 1] = func;
3418 +
3419 +       return 0;
3420 +
3421 +fail:
3422 +       /*
3423 +        * It is okay to remove the function here even though we hold
3424 +        * the host lock as we haven't registered the device yet.
3425 +        */
3426 +       sdio_remove_func(func);
3427 +       return ret;
3428 +}
3429 +
3430 +static int sdio_read_cccr(struct mmc_card *card)
3431 +{
3432 +       int ret;
3433 +       int cccr_vsn;
3434 +       unsigned char data;
3435 +
3436 +       memset(&card->cccr, 0, sizeof(struct sdio_cccr));
3437 +
3438 +       ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CCCR, 0, &data);
3439 +       if (ret)
3440 +               goto out;
3441 +
3442 +       cccr_vsn = data & 0x0f;
3443 +
3444 +       if (cccr_vsn > SDIO_CCCR_REV_1_20) {
3445 +               printk(KERN_ERR "%s: unrecognised CCCR structure version %d\n",
3446 +                       mmc_hostname(card->host), cccr_vsn);
3447 +               return -EINVAL;
3448 +       }
3449 +
3450 +       card->cccr.sdio_vsn = (data & 0xf0) >> 4;
3451 +
3452 +       ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CAPS, 0, &data);
3453 +       if (ret)
3454 +               goto out;
3455 +
3456 +       if (data & SDIO_CCCR_CAP_SMB)
3457 +               card->cccr.multi_block = 1;
3458 +       if (data & SDIO_CCCR_CAP_LSC)
3459 +               card->cccr.low_speed = 1;
3460 +       if (data & SDIO_CCCR_CAP_4BLS)
3461 +               card->cccr.wide_bus = 1;
3462 +
3463 +       if (cccr_vsn >= SDIO_CCCR_REV_1_10) {
3464 +               ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_POWER, 0, &data);
3465 +               if (ret)
3466 +                       goto out;
3467 +
3468 +               if (data & SDIO_POWER_SMPC)
3469 +                       card->cccr.high_power = 1;
3470 +       }
3471 +
3472 +       if (cccr_vsn >= SDIO_CCCR_REV_1_20) {
3473 +               ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &data);
3474 +               if (ret)
3475 +                       goto out;
3476 +
3477 +               if (data & SDIO_SPEED_SHS)
3478 +                       card->cccr.high_speed = 1;
3479 +       }
3480 +
3481 +out:
3482 +       return ret;
3483 +}
3484 +
3485 +static int sdio_enable_wide(struct mmc_card *card)
3486 +{
3487 +       int ret;
3488 +       u8 ctrl;
3489 +
3490 +       if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
3491 +               return 0;
3492 +
3493 +       if (card->cccr.low_speed && !card->cccr.wide_bus)
3494 +               return 0;
3495 +
3496 +       ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
3497 +       if (ret)
3498 +               return ret;
3499 +
3500 +       ctrl |= SDIO_BUS_WIDTH_4BIT;
3501 +
3502 +       ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
3503 +       if (ret)
3504 +               return ret;
3505 +
3506 +       mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
3507 +
3508 +       return 0;
3509 +}
3510 +
3511 +/*
3512 + * Host is being removed. Free up the current card.
3513 + */
3514 +static void mmc_sdio_remove(struct mmc_host *host)
3515 +{
3516 +       int i;
3517 +
3518 +       BUG_ON(!host);
3519 +       BUG_ON(!host->card);
3520 +
3521 +       for (i = 0;i < host->card->sdio_funcs;i++) {
3522 +               if (host->card->sdio_func[i]) {
3523 +                       sdio_remove_func(host->card->sdio_func[i]);
3524 +                       host->card->sdio_func[i] = NULL;
3525 +               }
3526 +       }
3527 +
3528 +       mmc_remove_card(host->card);
3529 +       host->card = NULL;
3530 +}
3531 +
3532 +/*
3533 + * Card detection callback from host.
3534 + */
3535 +static void mmc_sdio_detect(struct mmc_host *host)
3536 +{
3537 +       int err;
3538 +
3539 +       BUG_ON(!host);
3540 +       BUG_ON(!host->card);
3541 +
3542 +       mmc_claim_host(host);
3543 +
3544 +       /*
3545 +        * Just check if our card has been removed.
3546 +        */
3547 +       err = mmc_select_card(host->card);
3548 +
3549 +       mmc_release_host(host);
3550 +
3551 +       if (err) {
3552 +               mmc_sdio_remove(host);
3553 +
3554 +               mmc_claim_host(host);
3555 +               mmc_detach_bus(host);
3556 +               mmc_release_host(host);
3557 +       }
3558 +}
3559 +
3560 +
3561 +static const struct mmc_bus_ops mmc_sdio_ops = {
3562 +       .remove = mmc_sdio_remove,
3563 +       .detect = mmc_sdio_detect,
3564 +};
3565 +
3566 +
3567 +/*
3568 + * Starting point for SDIO card init.
3569 + */
3570 +int mmc_attach_sdio(struct mmc_host *host, u32 ocr)
3571 +{
3572 +       int err;
3573 +       int i, funcs;
3574 +       struct mmc_card *card;
3575 +
3576 +       BUG_ON(!host);
3577 +       WARN_ON(!host->claimed);
3578 +
3579 +       mmc_attach_bus(host, &mmc_sdio_ops);
3580 +
3581 +       /*
3582 +        * Sanity check the voltages that the card claims to
3583 +        * support.
3584 +        */
3585 +       if (ocr & 0x7F) {
3586 +               printk(KERN_WARNING "%s: card claims to support voltages "
3587 +                      "below the defined range. These will be ignored.\n",
3588 +                      mmc_hostname(host));
3589 +               ocr &= ~0x7F;
3590 +       }
3591 +
3592 +       if (ocr & MMC_VDD_165_195) {
3593 +               printk(KERN_WARNING "%s: SDIO card claims to support the "
3594 +                      "incompletely defined 'low voltage range'. This "
3595 +                      "will be ignored.\n", mmc_hostname(host));
3596 +               ocr &= ~MMC_VDD_165_195;
3597 +       }
3598 +
3599 +       host->ocr = mmc_select_voltage(host, ocr);
3600 +
3601 +       /*
3602 +        * Can we support the voltage(s) of the card(s)?
3603 +        */
3604 +       if (!host->ocr) {
3605 +               err = -EINVAL;
3606 +               goto err;
3607 +       }
3608 +
3609 +       /*
3610 +        * Inform the card of the voltage
3611 +        */
3612 +       err = mmc_send_io_op_cond(host, host->ocr, &ocr);
3613 +       if (err)
3614 +               goto err;
3615 +
3616 +       /*
3617 +        * For SPI, enable CRC as appropriate.
3618 +        */
3619 +       if (mmc_host_is_spi(host)) {
3620 +               err = mmc_spi_set_crc(host, use_spi_crc);
3621 +               if (err)
3622 +                       goto err;
3623 +       }
3624 +
3625 +       /*
3626 +        * The number of functions on the card is encoded inside
3627 +        * the ocr.
3628 +        */
3629 +       funcs = (ocr & 0x70000000) >> 28;
3630 +
3631 +       /*
3632 +        * Allocate card structure.
3633 +        */
3634 +       card = mmc_alloc_card(host);
3635 +       if (IS_ERR(card)) {
3636 +               err = PTR_ERR(card);
3637 +               goto err;
3638 +       }
3639 +
3640 +       card->type = MMC_TYPE_SDIO;
3641 +       card->sdio_funcs = funcs;
3642 +
3643 +       host->card = card;
3644 +
3645 +       /*
3646 +        * For native busses:  set card RCA and quit open drain mode.
3647 +        */
3648 +       if (!mmc_host_is_spi(host)) {
3649 +               err = mmc_send_relative_addr(host, &card->rca);
3650 +               if (err)
3651 +                       goto remove;
3652 +
3653 +               mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
3654 +       }
3655 +
3656 +       /*
3657 +        * Select card, as all following commands rely on that.
3658 +        */
3659 +       if (!mmc_host_is_spi(host)) {
3660 +               err = mmc_select_card(card);
3661 +               if (err)
3662 +                       goto remove;
3663 +       }
3664 +
3665 +       /*
3666 +        * Read the common registers.
3667 +        */
3668 +       err = sdio_read_cccr(card);
3669 +       if (err)
3670 +               goto remove;
3671 +
3672 +       /*
3673 +        * Read the common CIS tuples.
3674 +        */
3675 +       err = sdio_read_common_cis(card);
3676 +       if (err)
3677 +               goto remove;
3678 +
3679 +       /*
3680 +        * No support for high-speed yet, so just set
3681 +        * the card's maximum speed.
3682 +        */
3683 +       mmc_set_clock(host, card->cis.max_dtr);
3684 +
3685 +       /*
3686 +        * Switch to wider bus (if supported).
3687 +        */
3688 +       err = sdio_enable_wide(card);
3689 +       if (err)
3690 +               goto remove;
3691 +
3692 +       /*
3693 +        * Initialize (but don't add) all present functions.
3694 +        */
3695 +       for (i = 0;i < funcs;i++) {
3696 +               err = sdio_init_func(host->card, i + 1);
3697 +               if (err)
3698 +                       goto remove;
3699 +       }
3700 +
3701 +       mmc_release_host(host);
3702 +
3703 +       /*
3704 +        * First add the card to the driver model...
3705 +        */
3706 +       err = mmc_add_card(host->card);
3707 +       if (err)
3708 +               goto remove_added;
3709 +
3710 +       /*
3711 +        * ...then the SDIO functions.
3712 +        */
3713 +       for (i = 0;i < funcs;i++) {
3714 +               err = sdio_add_func(host->card->sdio_func[i]);
3715 +               if (err)
3716 +                       goto remove_added;
3717 +       }
3718 +
3719 +       return 0;
3720 +
3721 +
3722 +remove_added:
3723 +       /* Remove without lock if the device has been added. */
3724 +       mmc_sdio_remove(host);
3725 +       mmc_claim_host(host);
3726 +remove:
3727 +       /* And with lock if it hasn't been added. */
3728 +       if (host->card)
3729 +               mmc_sdio_remove(host);
3730 +err:
3731 +       mmc_detach_bus(host);
3732 +       mmc_release_host(host);
3733 +
3734 +       printk(KERN_ERR "%s: error %d whilst initialising SDIO card\n",
3735 +               mmc_hostname(host), err);
3736 +
3737 +       return err;
3738 +}
3739 +
3740 Index: linux-2.6.23.17/drivers/mmc/core/sdio_bus.c
3741 ===================================================================
3742 --- /dev/null
3743 +++ linux-2.6.23.17/drivers/mmc/core/sdio_bus.c
3744 @@ -0,0 +1,265 @@
3745 +/*
3746 + *  linux/drivers/mmc/core/sdio_bus.c
3747 + *
3748 + *  Copyright 2007 Pierre Ossman
3749 + *
3750 + * This program is free software; you can redistribute it and/or modify
3751 + * it under the terms of the GNU General Public License as published by
3752 + * the Free Software Foundation; either version 2 of the License, or (at
3753 + * your option) any later version.
3754 + *
3755 + * SDIO function driver model
3756 + */
3757 +
3758 +#include <linux/device.h>
3759 +#include <linux/err.h>
3760 +
3761 +#include <linux/mmc/card.h>
3762 +#include <linux/mmc/sdio_func.h>
3763 +
3764 +#include "sdio_cis.h"
3765 +#include "sdio_bus.h"
3766 +
3767 +#define dev_to_sdio_func(d)    container_of(d, struct sdio_func, dev)
3768 +#define to_sdio_driver(d)      container_of(d, struct sdio_driver, drv)
3769 +
3770 +/* show configuration fields */
3771 +#define sdio_config_attr(field, format_string)                         \
3772 +static ssize_t                                                         \
3773 +field##_show(struct device *dev, struct device_attribute *attr, char *buf)                             \
3774 +{                                                                      \
3775 +       struct sdio_func *func;                                         \
3776 +                                                                       \
3777 +       func = dev_to_sdio_func (dev);                                  \
3778 +       return sprintf (buf, format_string, func->field);               \
3779 +}
3780 +
3781 +sdio_config_attr(class, "0x%02x\n");
3782 +sdio_config_attr(vendor, "0x%04x\n");
3783 +sdio_config_attr(device, "0x%04x\n");
3784 +
3785 +static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
3786 +{
3787 +       struct sdio_func *func = dev_to_sdio_func (dev);
3788 +
3789 +       return sprintf(buf, "sdio:c%02Xv%04Xd%04X\n",
3790 +                       func->class, func->vendor, func->device);
3791 +}
3792 +
3793 +static struct device_attribute sdio_dev_attrs[] = {
3794 +       __ATTR_RO(class),
3795 +       __ATTR_RO(vendor),
3796 +       __ATTR_RO(device),
3797 +       __ATTR_RO(modalias),
3798 +       __ATTR_NULL,
3799 +};
3800 +
3801 +static const struct sdio_device_id *sdio_match_one(struct sdio_func *func,
3802 +       const struct sdio_device_id *id)
3803 +{
3804 +       if (id->class != (__u8)SDIO_ANY_ID && id->class != func->class)
3805 +               return NULL;
3806 +       if (id->vendor != (__u16)SDIO_ANY_ID && id->vendor != func->vendor)
3807 +               return NULL;
3808 +       if (id->device != (__u16)SDIO_ANY_ID && id->device != func->device)
3809 +               return NULL;
3810 +       return id;
3811 +}
3812 +
3813 +static const struct sdio_device_id *sdio_match_device(struct sdio_func *func,
3814 +       struct sdio_driver *sdrv)
3815 +{
3816 +       const struct sdio_device_id *ids;
3817 +
3818 +       ids = sdrv->id_table;
3819 +
3820 +       if (ids) {
3821 +               while (ids->class || ids->vendor || ids->device) {
3822 +                       if (sdio_match_one(func, ids))
3823 +                               return ids;
3824 +                       ids++;
3825 +               }
3826 +       }
3827 +
3828 +       return NULL;
3829 +}
3830 +
3831 +static int sdio_bus_match(struct device *dev, struct device_driver *drv)
3832 +{
3833 +       struct sdio_func *func = dev_to_sdio_func(dev);
3834 +       struct sdio_driver *sdrv = to_sdio_driver(drv);
3835 +
3836 +       if (sdio_match_device(func, sdrv))
3837 +               return 1;
3838 +
3839 +       return 0;
3840 +}
3841 +
3842 +static int
3843 +sdio_bus_uevent(struct device *dev, char **envp,
3844 +               int num_envp, char *buffer, int buffer_size)
3845 +{
3846 +       struct sdio_func *func = dev_to_sdio_func(dev);
3847 +       int i = 0, len = 0;
3848 +
3849 +       if (add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len,
3850 +                       "SDIO_CLASS=%02X", func->class))
3851 +               return -ENOMEM;
3852 +
3853 +       if (add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len,
3854 +                       "SDIO_ID=%04X:%04X", func->vendor, func->device))
3855 +               return -ENOMEM;
3856 +
3857 +       if (add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len,
3858 +                       "MODALIAS=sdio:c%02Xv%04Xd%04X",
3859 +                       func->class, func->vendor, func->device))
3860 +               return -ENOMEM;
3861 +
3862 +       return 0;
3863 +}
3864 +
3865 +static int sdio_bus_probe(struct device *dev)
3866 +{
3867 +       struct sdio_driver *drv = to_sdio_driver(dev->driver);
3868 +       struct sdio_func *func = dev_to_sdio_func(dev);
3869 +       const struct sdio_device_id *id;
3870 +       int ret;
3871 +
3872 +       id = sdio_match_device(func, drv);
3873 +       if (!id)
3874 +               return -ENODEV;
3875 +
3876 +       /* Set the default block size so the driver is sure it's something
3877 +        * sensible. */
3878 +       sdio_claim_host(func);
3879 +       ret = sdio_set_block_size(func, 0);
3880 +       sdio_release_host(func);
3881 +       if (ret)
3882 +               return ret;
3883 +
3884 +       return drv->probe(func, id);
3885 +}
3886 +
3887 +static int sdio_bus_remove(struct device *dev)
3888 +{
3889 +       struct sdio_driver *drv = to_sdio_driver(dev->driver);
3890 +       struct sdio_func *func = dev_to_sdio_func(dev);
3891 +
3892 +       drv->remove(func);
3893 +
3894 +       if (func->irq_handler) {
3895 +               printk(KERN_WARNING "WARNING: driver %s did not remove "
3896 +                       "its interrupt handler!\n", drv->name);
3897 +               sdio_claim_host(func);
3898 +               sdio_release_irq(func);
3899 +               sdio_release_host(func);
3900 +       }
3901 +
3902 +       return 0;
3903 +}
3904 +
3905 +static struct bus_type sdio_bus_type = {
3906 +       .name           = "sdio",
3907 +       .dev_attrs      = sdio_dev_attrs,
3908 +       .match          = sdio_bus_match,
3909 +       .uevent         = sdio_bus_uevent,
3910 +       .probe          = sdio_bus_probe,
3911 +       .remove         = sdio_bus_remove,
3912 +};
3913 +
3914 +int sdio_register_bus(void)
3915 +{
3916 +       return bus_register(&sdio_bus_type);
3917 +}
3918 +
3919 +void sdio_unregister_bus(void)
3920 +{
3921 +       bus_unregister(&sdio_bus_type);
3922 +}
3923 +
3924 +/**
3925 + *     sdio_register_driver - register a function driver
3926 + *     @drv: SDIO function driver
3927 + */
3928 +int sdio_register_driver(struct sdio_driver *drv)
3929 +{
3930 +       drv->drv.name = drv->name;
3931 +       drv->drv.bus = &sdio_bus_type;
3932 +       return driver_register(&drv->drv);
3933 +}
3934 +EXPORT_SYMBOL_GPL(sdio_register_driver);
3935 +
3936 +/**
3937 + *     sdio_unregister_driver - unregister a function driver
3938 + *     @drv: SDIO function driver
3939 + */
3940 +void sdio_unregister_driver(struct sdio_driver *drv)
3941 +{
3942 +       drv->drv.bus = &sdio_bus_type;
3943 +       driver_unregister(&drv->drv);
3944 +}
3945 +EXPORT_SYMBOL_GPL(sdio_unregister_driver);
3946 +
3947 +static void sdio_release_func(struct device *dev)
3948 +{
3949 +       struct sdio_func *func = dev_to_sdio_func(dev);
3950 +
3951 +       sdio_free_func_cis(func);
3952 +
3953 +       if (func->info)
3954 +               kfree(func->info);
3955 +
3956 +       kfree(func);
3957 +}
3958 +
3959 +/*
3960 + * Allocate and initialise a new SDIO function structure.
3961 + */
3962 +struct sdio_func *sdio_alloc_func(struct mmc_card *card)
3963 +{
3964 +       struct sdio_func *func;
3965 +
3966 +       func = kzalloc(sizeof(struct sdio_func), GFP_KERNEL);
3967 +       if (!func)
3968 +               return ERR_PTR(-ENOMEM);
3969 +
3970 +       func->card = card;
3971 +
3972 +       device_initialize(&func->dev);
3973 +
3974 +       func->dev.parent = &card->dev;
3975 +       func->dev.bus = &sdio_bus_type;
3976 +       func->dev.release = sdio_release_func;
3977 +
3978 +       return func;
3979 +}
3980 +
3981 +/*
3982 + * Register a new SDIO function with the driver model.
3983 + */
3984 +int sdio_add_func(struct sdio_func *func)
3985 +{
3986 +       int ret;
3987 +
3988 +       snprintf(func->dev.bus_id, sizeof(func->dev.bus_id),
3989 +                "%s:%d", mmc_card_id(func->card), func->num);
3990 +
3991 +       ret = device_add(&func->dev);
3992 +       if (ret == 0)
3993 +               sdio_func_set_present(func);
3994 +
3995 +       return ret;
3996 +}
3997 +
3998 +/*
3999 + * Unregister a SDIO function with the driver model, and
4000 + * (eventually) free it.
4001 + */
4002 +void sdio_remove_func(struct sdio_func *func)
4003 +{
4004 +       if (sdio_func_present(func))
4005 +               device_del(&func->dev);
4006 +
4007 +       put_device(&func->dev);
4008 +}
4009 +
4010 Index: linux-2.6.23.17/drivers/mmc/core/sdio_bus.h
4011 ===================================================================
4012 --- /dev/null
4013 +++ linux-2.6.23.17/drivers/mmc/core/sdio_bus.h
4014 @@ -0,0 +1,22 @@
4015 +/*
4016 + *  linux/drivers/mmc/core/sdio_bus.h
4017 + *
4018 + *  Copyright 2007 Pierre Ossman
4019 + *
4020 + * This program is free software; you can redistribute it and/or modify
4021 + * it under the terms of the GNU General Public License as published by
4022 + * the Free Software Foundation; either version 2 of the License, or (at
4023 + * your option) any later version.
4024 + */
4025 +#ifndef _MMC_CORE_SDIO_BUS_H
4026 +#define _MMC_CORE_SDIO_BUS_H
4027 +
4028 +struct sdio_func *sdio_alloc_func(struct mmc_card *card);
4029 +int sdio_add_func(struct sdio_func *func);
4030 +void sdio_remove_func(struct sdio_func *func);
4031 +
4032 +int sdio_register_bus(void);
4033 +void sdio_unregister_bus(void);
4034 +
4035 +#endif
4036 +
4037 Index: linux-2.6.23.17/drivers/mmc/core/sdio_cis.c
4038 ===================================================================
4039 --- /dev/null
4040 +++ linux-2.6.23.17/drivers/mmc/core/sdio_cis.c
4041 @@ -0,0 +1,346 @@
4042 +/*
4043 + * linux/drivers/mmc/core/sdio_cis.c
4044 + *
4045 + * Author:     Nicolas Pitre
4046 + * Created:    June 11, 2007
4047 + * Copyright:  MontaVista Software Inc.
4048 + *
4049 + * Copyright 2007 Pierre Ossman
4050 + *
4051 + * This program is free software; you can redistribute it and/or modify
4052 + * it under the terms of the GNU General Public License as published by
4053 + * the Free Software Foundation; either version 2 of the License, or (at
4054 + * your option) any later version.
4055 + */
4056 +
4057 +#include <linux/kernel.h>
4058 +
4059 +#include <linux/mmc/host.h>
4060 +#include <linux/mmc/card.h>
4061 +#include <linux/mmc/sdio.h>
4062 +#include <linux/mmc/sdio_func.h>
4063 +
4064 +#include "sdio_cis.h"
4065 +#include "sdio_ops.h"
4066 +
4067 +static int cistpl_vers_1(struct mmc_card *card, struct sdio_func *func,
4068 +                        const unsigned char *buf, unsigned size)
4069 +{
4070 +       unsigned i, nr_strings;
4071 +       char **buffer, *string;
4072 +
4073 +       buf += 2;
4074 +       size -= 2;
4075 +
4076 +       nr_strings = 0;
4077 +       for (i = 0; i < size; i++) {
4078 +               if (buf[i] == 0xff)
4079 +                       break;
4080 +               if (buf[i] == 0)
4081 +                       nr_strings++;
4082 +       }
4083 +
4084 +       if (buf[i-1] != '\0') {
4085 +               printk(KERN_WARNING "SDIO: ignoring broken CISTPL_VERS_1\n");
4086 +               return 0;
4087 +       }
4088 +
4089 +       size = i;
4090 +
4091 +       buffer = kzalloc(sizeof(char*) * nr_strings + size, GFP_KERNEL);
4092 +       if (!buffer)
4093 +               return -ENOMEM;
4094 +
4095 +       string = (char*)(buffer + nr_strings);
4096 +
4097 +       for (i = 0; i < nr_strings; i++) {
4098 +               buffer[i] = string;
4099 +               strcpy(string, buf);
4100 +               string += strlen(string) + 1;
4101 +               buf += strlen(buf) + 1;
4102 +       }
4103 +
4104 +       if (func) {
4105 +               func->num_info = nr_strings;
4106 +               func->info = (const char**)buffer;
4107 +       } else {
4108 +               card->num_info = nr_strings;
4109 +               card->info = (const char**)buffer;
4110 +       }
4111 +
4112 +       return 0;
4113 +}
4114 +
4115 +static int cistpl_manfid(struct mmc_card *card, struct sdio_func *func,
4116 +                        const unsigned char *buf, unsigned size)
4117 +{
4118 +       unsigned int vendor, device;
4119 +
4120 +       /* TPLMID_MANF */
4121 +       vendor = buf[0] | (buf[1] << 8);
4122 +
4123 +       /* TPLMID_CARD */
4124 +       device = buf[2] | (buf[3] << 8);
4125 +
4126 +       if (func) {
4127 +               func->vendor = vendor;
4128 +               func->device = device;
4129 +       } else {
4130 +               card->cis.vendor = vendor;
4131 +               card->cis.device = device;
4132 +       }
4133 +
4134 +       return 0;
4135 +}
4136 +
4137 +static const unsigned char speed_val[16] =
4138 +       { 0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80 };
4139 +static const unsigned int speed_unit[8] =
4140 +       { 10000, 100000, 1000000, 10000000, 0, 0, 0, 0 };
4141 +
4142 +static int cistpl_funce_common(struct mmc_card *card,
4143 +                              const unsigned char *buf, unsigned size)
4144 +{
4145 +       if (size < 0x04 || buf[0] != 0)
4146 +               return -EINVAL;
4147 +
4148 +       /* TPLFE_FN0_BLK_SIZE */
4149 +       card->cis.blksize = buf[1] | (buf[2] << 8);
4150 +
4151 +       /* TPLFE_MAX_TRAN_SPEED */
4152 +       card->cis.max_dtr = speed_val[(buf[3] >> 3) & 15] *
4153 +                           speed_unit[buf[3] & 7];
4154 +
4155 +       return 0;
4156 +}
4157 +
4158 +static int cistpl_funce_func(struct sdio_func *func,
4159 +                            const unsigned char *buf, unsigned size)
4160 +{
4161 +       unsigned vsn;
4162 +       unsigned min_size;
4163 +
4164 +       vsn = func->card->cccr.sdio_vsn;
4165 +       min_size = (vsn == SDIO_SDIO_REV_1_00) ? 28 : 42;
4166 +
4167 +       if (size < min_size || buf[0] != 1)
4168 +               return -EINVAL;
4169 +
4170 +       /* TPLFE_MAX_BLK_SIZE */
4171 +       func->max_blksize = buf[12] | (buf[13] << 8);
4172 +
4173 +       return 0;
4174 +}
4175 +
4176 +static int cistpl_funce(struct mmc_card *card, struct sdio_func *func,
4177 +                       const unsigned char *buf, unsigned size)
4178 +{
4179 +       int ret;
4180 +
4181 +       /*
4182 +        * There should be two versions of the CISTPL_FUNCE tuple,
4183 +        * one for the common CIS (function 0) and a version used by
4184 +        * the individual function's CIS (1-7). Yet, the later has a
4185 +        * different length depending on the SDIO spec version.
4186 +        */
4187 +       if (func)
4188 +               ret = cistpl_funce_func(func, buf, size);
4189 +       else
4190 +               ret = cistpl_funce_common(card, buf, size);
4191 +
4192 +       if (ret) {
4193 +               printk(KERN_ERR "%s: bad CISTPL_FUNCE size %u "
4194 +                      "type %u\n", mmc_hostname(card->host), size, buf[0]);
4195 +               return ret;
4196 +       }
4197 +
4198 +       return 0;
4199 +}
4200 +
4201 +typedef int (tpl_parse_t)(struct mmc_card *, struct sdio_func *,
4202 +                          const unsigned char *, unsigned);
4203 +
4204 +struct cis_tpl {
4205 +       unsigned char code;
4206 +       unsigned char min_size;
4207 +       tpl_parse_t *parse;
4208 +};
4209 +
4210 +static const struct cis_tpl cis_tpl_list[] = {
4211 +       {       0x15,   3,      cistpl_vers_1           },
4212 +       {       0x20,   4,      cistpl_manfid           },
4213 +       {       0x21,   2,      /* cistpl_funcid */     },
4214 +       {       0x22,   0,      cistpl_funce            },
4215 +};
4216 +
4217 +static int sdio_read_cis(struct mmc_card *card, struct sdio_func *func)
4218 +{
4219 +       int ret;
4220 +       struct sdio_func_tuple *this, **prev;
4221 +       unsigned i, ptr = 0;
4222 +
4223 +       /*
4224 +        * Note that this works for the common CIS (function number 0) as
4225 +        * well as a function's CIS * since SDIO_CCCR_CIS and SDIO_FBR_CIS
4226 +        * have the same offset.
4227 +        */
4228 +       for (i = 0; i < 3; i++) {
4229 +               unsigned char x, fn;
4230 +
4231 +               if (func)
4232 +                       fn = func->num;
4233 +               else
4234 +                       fn = 0;
4235 +
4236 +               ret = mmc_io_rw_direct(card, 0, 0,
4237 +                       SDIO_FBR_BASE(fn) + SDIO_FBR_CIS + i, 0, &x);
4238 +               if (ret)
4239 +                       return ret;
4240 +               ptr |= x << (i * 8);
4241 +       }
4242 +
4243 +       if (func)
4244 +               prev = &func->tuples;
4245 +       else
4246 +               prev = &card->tuples;
4247 +
4248 +       BUG_ON(*prev);
4249 +
4250 +       do {
4251 +               unsigned char tpl_code, tpl_link;
4252 +
4253 +               ret = mmc_io_rw_direct(card, 0, 0, ptr++, 0, &tpl_code);
4254 +               if (ret)
4255 +                       break;
4256 +
4257 +               /* 0xff means we're done */
4258 +               if (tpl_code == 0xff)
4259 +                       break;
4260 +
4261 +               ret = mmc_io_rw_direct(card, 0, 0, ptr++, 0, &tpl_link);
4262 +               if (ret)
4263 +                       break;
4264 +
4265 +               this = kmalloc(sizeof(*this) + tpl_link, GFP_KERNEL);
4266 +               if (!this)
4267 +                       return -ENOMEM;
4268 +
4269 +               for (i = 0; i < tpl_link; i++) {
4270 +                       ret = mmc_io_rw_direct(card, 0, 0,
4271 +                                              ptr + i, 0, &this->data[i]);
4272 +                       if (ret)
4273 +                               break;
4274 +               }
4275 +               if (ret) {
4276 +                       kfree(this);
4277 +                       break;
4278 +               }
4279 +
4280 +               for (i = 0; i < ARRAY_SIZE(cis_tpl_list); i++)
4281 +                       if (cis_tpl_list[i].code == tpl_code)
4282 +                               break;
4283 +               if (i >= ARRAY_SIZE(cis_tpl_list)) {
4284 +                       /* this tuple is unknown to the core */
4285 +                       this->next = NULL;
4286 +                       this->code = tpl_code;
4287 +                       this->size = tpl_link;
4288 +                       *prev = this;
4289 +                       prev = &this->next;
4290 +                       printk(KERN_DEBUG
4291 +                              "%s: queuing CIS tuple 0x%02x length %u\n",
4292 +                              mmc_hostname(card->host), tpl_code, tpl_link);
4293 +               } else {
4294 +                       const struct cis_tpl *tpl = cis_tpl_list + i;
4295 +                       if (tpl_link < tpl->min_size) {
4296 +                               printk(KERN_ERR
4297 +                                      "%s: bad CIS tuple 0x%02x (length = %u, expected >= %u)\n",
4298 +                                      mmc_hostname(card->host),
4299 +                                      tpl_code, tpl_link, tpl->min_size);
4300 +                               ret = -EINVAL;
4301 +                       } else if (tpl->parse) {
4302 +                               ret = tpl->parse(card, func,
4303 +                                                this->data, tpl_link);
4304 +                       }
4305 +                       kfree(this);
4306 +               }
4307 +
4308 +               ptr += tpl_link;
4309 +       } while (!ret);
4310 +
4311 +       /*
4312 +        * Link in all unknown tuples found in the common CIS so that
4313 +        * drivers don't have to go digging in two places.
4314 +        */
4315 +       if (func)
4316 +               *prev = card->tuples;
4317 +
4318 +       return ret;
4319 +}
4320 +
4321 +int sdio_read_common_cis(struct mmc_card *card)
4322 +{
4323 +       return sdio_read_cis(card, NULL);
4324 +}
4325 +
4326 +void sdio_free_common_cis(struct mmc_card *card)
4327 +{
4328 +       struct sdio_func_tuple *tuple, *victim;
4329 +
4330 +       tuple = card->tuples;
4331 +
4332 +       while (tuple) {
4333 +               victim = tuple;
4334 +               tuple = tuple->next;
4335 +               kfree(victim);
4336 +       }
4337 +
4338 +       card->tuples = NULL;
4339 +}
4340 +
4341 +int sdio_read_func_cis(struct sdio_func *func)
4342 +{
4343 +       int ret;
4344 +
4345 +       ret = sdio_read_cis(func->card, func);
4346 +       if (ret)
4347 +               return ret;
4348 +
4349 +       /*
4350 +        * Since we've linked to tuples in the card structure,
4351 +        * we must make sure we have a reference to it.
4352 +        */
4353 +       get_device(&func->card->dev);
4354 +
4355 +       /*
4356 +        * Vendor/device id is optional for function CIS, so
4357 +        * copy it from the card structure as needed.
4358 +        */
4359 +       if (func->vendor == 0) {
4360 +               func->vendor = func->card->cis.vendor;
4361 +               func->device = func->card->cis.device;
4362 +       }
4363 +
4364 +       return 0;
4365 +}
4366 +
4367 +void sdio_free_func_cis(struct sdio_func *func)
4368 +{
4369 +       struct sdio_func_tuple *tuple, *victim;
4370 +
4371 +       tuple = func->tuples;
4372 +
4373 +       while (tuple && tuple != func->card->tuples) {
4374 +               victim = tuple;
4375 +               tuple = tuple->next;
4376 +               kfree(victim);
4377 +       }
4378 +
4379 +       func->tuples = NULL;
4380 +
4381 +       /*
4382 +        * We have now removed the link to the tuples in the
4383 +        * card structure, so remove the reference.
4384 +        */
4385 +       put_device(&func->card->dev);
4386 +}
4387 +
4388 Index: linux-2.6.23.17/drivers/mmc/core/sdio_cis.h
4389 ===================================================================
4390 --- /dev/null
4391 +++ linux-2.6.23.17/drivers/mmc/core/sdio_cis.h
4392 @@ -0,0 +1,23 @@
4393 +/*
4394 + * linux/drivers/mmc/core/sdio_cis.h
4395 + *
4396 + * Author:     Nicolas Pitre
4397 + * Created:    June 11, 2007
4398 + * Copyright:  MontaVista Software Inc.
4399 + *
4400 + * This program is free software; you can redistribute it and/or modify
4401 + * it under the terms of the GNU General Public License as published by
4402 + * the Free Software Foundation; either version 2 of the License, or (at
4403 + * your option) any later version.
4404 + */
4405 +
4406 +#ifndef _MMC_SDIO_CIS_H
4407 +#define _MMC_SDIO_CIS_H
4408 +
4409 +int sdio_read_common_cis(struct mmc_card *card);
4410 +void sdio_free_common_cis(struct mmc_card *card);
4411 +
4412 +int sdio_read_func_cis(struct sdio_func *func);
4413 +void sdio_free_func_cis(struct sdio_func *func);
4414 +
4415 +#endif
4416 Index: linux-2.6.23.17/drivers/mmc/core/sdio_io.c
4417 ===================================================================
4418 --- /dev/null
4419 +++ linux-2.6.23.17/drivers/mmc/core/sdio_io.c
4420 @@ -0,0 +1,548 @@
4421 +/*
4422 + *  linux/drivers/mmc/core/sdio_io.c
4423 + *
4424 + *  Copyright 2007 Pierre Ossman
4425 + *
4426 + * This program is free software; you can redistribute it and/or modify
4427 + * it under the terms of the GNU General Public License as published by
4428 + * the Free Software Foundation; either version 2 of the License, or (at
4429 + * your option) any later version.
4430 + */
4431 +
4432 +#include <linux/mmc/host.h>
4433 +#include <linux/mmc/card.h>
4434 +#include <linux/mmc/sdio.h>
4435 +#include <linux/mmc/sdio_func.h>
4436 +
4437 +#include "sdio_ops.h"
4438 +
4439 +/**
4440 + *     sdio_claim_host - exclusively claim a bus for a certain SDIO function
4441 + *     @func: SDIO function that will be accessed
4442 + *
4443 + *     Claim a bus for a set of operations. The SDIO function given
4444 + *     is used to figure out which bus is relevant.
4445 + */
4446 +void sdio_claim_host(struct sdio_func *func)
4447 +{
4448 +       BUG_ON(!func);
4449 +       BUG_ON(!func->card);
4450 +
4451 +       mmc_claim_host(func->card->host);
4452 +}
4453 +EXPORT_SYMBOL_GPL(sdio_claim_host);
4454 +
4455 +/**
4456 + *     sdio_release_host - release a bus for a certain SDIO function
4457 + *     @func: SDIO function that was accessed
4458 + *
4459 + *     Release a bus, allowing others to claim the bus for their
4460 + *     operations.
4461 + */
4462 +void sdio_release_host(struct sdio_func *func)
4463 +{
4464 +       BUG_ON(!func);
4465 +       BUG_ON(!func->card);
4466 +
4467 +       mmc_release_host(func->card->host);
4468 +}
4469 +EXPORT_SYMBOL_GPL(sdio_release_host);
4470 +
4471 +/**
4472 + *     sdio_enable_func - enables a SDIO function for usage
4473 + *     @func: SDIO function to enable
4474 + *
4475 + *     Powers up and activates a SDIO function so that register
4476 + *     access is possible.
4477 + */
4478 +int sdio_enable_func(struct sdio_func *func)
4479 +{
4480 +       int ret;
4481 +       unsigned char reg;
4482 +       unsigned long timeout;
4483 +
4484 +       BUG_ON(!func);
4485 +       BUG_ON(!func->card);
4486 +
4487 +       pr_debug("SDIO: Enabling device %s...\n", sdio_func_id(func));
4488 +
4489 +       ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IOEx, 0, &reg);
4490 +       if (ret)
4491 +               goto err;
4492 +
4493 +       reg |= 1 << func->num;
4494 +
4495 +       ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IOEx, reg, NULL);
4496 +       if (ret)
4497 +               goto err;
4498 +
4499 +       /*
4500 +        * FIXME: This should timeout based on information in the CIS,
4501 +        * but we don't have card to parse that yet.
4502 +        */
4503 +       timeout = jiffies + HZ;
4504 +
4505 +       while (1) {
4506 +               ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IORx, 0, &reg);
4507 +               if (ret)
4508 +                       goto err;
4509 +               if (reg & (1 << func->num))
4510 +                       break;
4511 +               ret = -ETIME;
4512 +               if (time_after(jiffies, timeout))
4513 +                       goto err;
4514 +       }
4515 +
4516 +       pr_debug("SDIO: Enabled device %s\n", sdio_func_id(func));
4517 +
4518 +       return 0;
4519 +
4520 +err:
4521 +       pr_debug("SDIO: Failed to enable device %s\n", sdio_func_id(func));
4522 +       return ret;
4523 +}
4524 +EXPORT_SYMBOL_GPL(sdio_enable_func);
4525 +
4526 +/**
4527 + *     sdio_disable_func - disable a SDIO function
4528 + *     @func: SDIO function to disable
4529 + *
4530 + *     Powers down and deactivates a SDIO function. Register access
4531 + *     to this function will fail until the function is reenabled.
4532 + */
4533 +int sdio_disable_func(struct sdio_func *func)
4534 +{
4535 +       int ret;
4536 +       unsigned char reg;
4537 +
4538 +       BUG_ON(!func);
4539 +       BUG_ON(!func->card);
4540 +
4541 +       pr_debug("SDIO: Disabling device %s...\n", sdio_func_id(func));
4542 +
4543 +       ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IOEx, 0, &reg);
4544 +       if (ret)
4545 +               goto err;
4546 +
4547 +       reg &= ~(1 << func->num);
4548 +
4549 +       ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IOEx, reg, NULL);
4550 +       if (ret)
4551 +               goto err;
4552 +
4553 +       pr_debug("SDIO: Disabled device %s\n", sdio_func_id(func));
4554 +
4555 +       return 0;
4556 +
4557 +err:
4558 +       pr_debug("SDIO: Failed to disable device %s\n", sdio_func_id(func));
4559 +       return -EIO;
4560 +}
4561 +EXPORT_SYMBOL_GPL(sdio_disable_func);
4562 +
4563 +/**
4564 + *     sdio_set_block_size - set the block size of an SDIO function
4565 + *     @func: SDIO function to change
4566 + *     @blksz: new block size or 0 to use the default.
4567 + *
4568 + *     The default block size is the largest supported by both the function
4569 + *     and the host, with a maximum of 512 to ensure that arbitrarily sized
4570 + *     data transfer use the optimal (least) number of commands.
4571 + *
4572 + *     A driver may call this to override the default block size set by the
4573 + *     core. This can be used to set a block size greater than the maximum
4574 + *     that reported by the card; it is the driver's responsibility to ensure
4575 + *     it uses a value that the card supports.
4576 + *
4577 + *     Returns 0 on success, -EINVAL if the host does not support the
4578 + *     requested block size, or -EIO (etc.) if one of the resultant FBR block
4579 + *     size register writes failed.
4580 + *
4581 + */
4582 +int sdio_set_block_size(struct sdio_func *func, unsigned blksz)
4583 +{
4584 +       int ret;
4585 +
4586 +       if (blksz > func->card->host->max_blk_size)
4587 +               return -EINVAL;
4588 +
4589 +       if (blksz == 0) {
4590 +               blksz = min(min(
4591 +                       func->max_blksize,
4592 +                       func->card->host->max_blk_size),
4593 +                       512u);
4594 +       }
4595 +
4596 +       ret = mmc_io_rw_direct(func->card, 1, 0,
4597 +               SDIO_FBR_BASE(func->num) + SDIO_FBR_BLKSIZE,
4598 +               blksz & 0xff, NULL);
4599 +       if (ret)
4600 +               return ret;
4601 +       ret = mmc_io_rw_direct(func->card, 1, 0,
4602 +               SDIO_FBR_BASE(func->num) + SDIO_FBR_BLKSIZE + 1,
4603 +               (blksz >> 8) & 0xff, NULL);
4604 +       if (ret)
4605 +               return ret;
4606 +       func->cur_blksize = blksz;
4607 +       return 0;
4608 +}
4609 +
4610 +EXPORT_SYMBOL_GPL(sdio_set_block_size);
4611 +
4612 +/* Split an arbitrarily sized data transfer into several
4613 + * IO_RW_EXTENDED commands. */
4614 +static int sdio_io_rw_ext_helper(struct sdio_func *func, int write,
4615 +       unsigned addr, int incr_addr, u8 *buf, unsigned size)
4616 +{
4617 +       unsigned remainder = size;
4618 +       unsigned max_blocks;
4619 +       int ret;
4620 +
4621 +       /* Do the bulk of the transfer using block mode (if supported). */
4622 +       if (func->card->cccr.multi_block) {
4623 +               /* Blocks per command is limited by host count, host transfer
4624 +                * size (we only use a single sg entry) and the maximum for
4625 +                * IO_RW_EXTENDED of 511 blocks. */
4626 +               max_blocks = min(min(
4627 +                       func->card->host->max_blk_count,
4628 +                       func->card->host->max_seg_size / func->cur_blksize),
4629 +                       511u);
4630 +
4631 +               while (remainder > func->cur_blksize) {
4632 +                       unsigned blocks;
4633 +
4634 +                       blocks = remainder / func->cur_blksize;
4635 +                       if (blocks > max_blocks)
4636 +                               blocks = max_blocks;
4637 +                       size = blocks * func->cur_blksize;
4638 +
4639 +                       ret = mmc_io_rw_extended(func->card, write,
4640 +                               func->num, addr, incr_addr, buf,
4641 +                               blocks, func->cur_blksize);
4642 +                       if (ret)
4643 +                               return ret;
4644 +
4645 +                       remainder -= size;
4646 +                       buf += size;
4647 +                       if (incr_addr)
4648 +                               addr += size;
4649 +               }
4650 +       }
4651 +
4652 +       /* Write the remainder using byte mode. */
4653 +       while (remainder > 0) {
4654 +               size = remainder;
4655 +               if (size > func->cur_blksize)
4656 +                       size = func->cur_blksize;
4657 +               if (size > 512)
4658 +                       size = 512; /* maximum size for byte mode */
4659 +
4660 +               ret = mmc_io_rw_extended(func->card, write, func->num, addr,
4661 +                        incr_addr, buf, 1, size);
4662 +               if (ret)
4663 +                       return ret;
4664 +
4665 +               remainder -= size;
4666 +               buf += size;
4667 +               if (incr_addr)
4668 +                       addr += size;
4669 +       }
4670 +       return 0;
4671 +}
4672 +
4673 +/**
4674 + *     sdio_readb - read a single byte from a SDIO function
4675 + *     @func: SDIO function to access
4676 + *     @addr: address to read
4677 + *     @err_ret: optional status value from transfer
4678 + *
4679 + *     Reads a single byte from the address space of a given SDIO
4680 + *     function. If there is a problem reading the address, 0xff
4681 + *     is returned and @err_ret will contain the error code.
4682 + */
4683 +unsigned char sdio_readb(struct sdio_func *func, unsigned int addr,
4684 +       int *err_ret)
4685 +{
4686 +       int ret;
4687 +       unsigned char val;
4688 +
4689 +       BUG_ON(!func);
4690 +
4691 +       if (err_ret)
4692 +               *err_ret = 0;
4693 +
4694 +       ret = mmc_io_rw_direct(func->card, 0, func->num, addr, 0, &val);
4695 +       if (ret) {
4696 +               if (err_ret)
4697 +                       *err_ret = ret;
4698 +               return 0xFF;
4699 +       }
4700 +
4701 +       return val;
4702 +}
4703 +EXPORT_SYMBOL_GPL(sdio_readb);
4704 +
4705 +/**
4706 + *     sdio_writeb - write a single byte to a SDIO function
4707 + *     @func: SDIO function to access
4708 + *     @b: byte to write
4709 + *     @addr: address to write to
4710 + *     @err_ret: optional status value from transfer
4711 + *
4712 + *     Writes a single byte to the address space of a given SDIO
4713 + *     function. @err_ret will contain the status of the actual
4714 + *     transfer.
4715 + */
4716 +void sdio_writeb(struct sdio_func *func, unsigned char b, unsigned int addr,
4717 +       int *err_ret)
4718 +{
4719 +       int ret;
4720 +
4721 +       BUG_ON(!func);
4722 +
4723 +       ret = mmc_io_rw_direct(func->card, 1, func->num, addr, b, NULL);
4724 +       if (err_ret)
4725 +               *err_ret = ret;
4726 +}
4727 +EXPORT_SYMBOL_GPL(sdio_writeb);
4728 +
4729 +/**
4730 + *     sdio_memcpy_fromio - read a chunk of memory from a SDIO function
4731 + *     @func: SDIO function to access
4732 + *     @dst: buffer to store the data
4733 + *     @addr: address to begin reading from
4734 + *     @count: number of bytes to read
4735 + *
4736 + *     Reads from the address space of a given SDIO function. Return
4737 + *     value indicates if the transfer succeeded or not.
4738 + */
4739 +int sdio_memcpy_fromio(struct sdio_func *func, void *dst,
4740 +       unsigned int addr, int count)
4741 +{
4742 +       return sdio_io_rw_ext_helper(func, 0, addr, 1, dst, count);
4743 +}
4744 +EXPORT_SYMBOL_GPL(sdio_memcpy_fromio);
4745 +
4746 +/**
4747 + *     sdio_memcpy_toio - write a chunk of memory to a SDIO function
4748 + *     @func: SDIO function to access
4749 + *     @addr: address to start writing to
4750 + *     @src: buffer that contains the data to write
4751 + *     @count: number of bytes to write
4752 + *
4753 + *     Writes to the address space of a given SDIO function. Return
4754 + *     value indicates if the transfer succeeded or not.
4755 + */
4756 +int sdio_memcpy_toio(struct sdio_func *func, unsigned int addr,
4757 +       void *src, int count)
4758 +{
4759 +       return sdio_io_rw_ext_helper(func, 1, addr, 1, src, count);
4760 +}
4761 +EXPORT_SYMBOL_GPL(sdio_memcpy_toio);
4762 +
4763 +/**
4764 + *     sdio_readsb - read from a FIFO on a SDIO function
4765 + *     @func: SDIO function to access
4766 + *     @dst: buffer to store the data
4767 + *     @addr: address of (single byte) FIFO
4768 + *     @count: number of bytes to read
4769 + *
4770 + *     Reads from the specified FIFO of a given SDIO function. Return
4771 + *     value indicates if the transfer succeeded or not.
4772 + */
4773 +int sdio_readsb(struct sdio_func *func, void *dst, unsigned int addr,
4774 +       int count)
4775 +{
4776 +       return sdio_io_rw_ext_helper(func, 0, addr, 0, dst, count);
4777 +}
4778 +
4779 +EXPORT_SYMBOL_GPL(sdio_readsb);
4780 +
4781 +/**
4782 + *     sdio_writesb - write to a FIFO of a SDIO function
4783 + *     @func: SDIO function to access
4784 + *     @addr: address of (single byte) FIFO
4785 + *     @src: buffer that contains the data to write
4786 + *     @count: number of bytes to write
4787 + *
4788 + *     Writes to the specified FIFO of a given SDIO function. Return
4789 + *     value indicates if the transfer succeeded or not.
4790 + */
4791 +int sdio_writesb(struct sdio_func *func, unsigned int addr, void *src,
4792 +       int count)
4793 +{
4794 +       return sdio_io_rw_ext_helper(func, 1, addr, 0, src, count);
4795 +}
4796 +EXPORT_SYMBOL_GPL(sdio_writesb);
4797 +
4798 +/**
4799 + *     sdio_readw - read a 16 bit integer from a SDIO function
4800 + *     @func: SDIO function to access
4801 + *     @addr: address to read
4802 + *     @err_ret: optional status value from transfer
4803 + *
4804 + *     Reads a 16 bit integer from the address space of a given SDIO
4805 + *     function. If there is a problem reading the address, 0xffff
4806 + *     is returned and @err_ret will contain the error code.
4807 + */
4808 +unsigned short sdio_readw(struct sdio_func *func, unsigned int addr,
4809 +       int *err_ret)
4810 +{
4811 +       int ret;
4812 +
4813 +       if (err_ret)
4814 +               *err_ret = 0;
4815 +
4816 +       ret = sdio_memcpy_fromio(func, func->tmpbuf, addr, 2);
4817 +       if (ret) {
4818 +               if (err_ret)
4819 +                       *err_ret = ret;
4820 +               return 0xFFFF;
4821 +       }
4822 +
4823 +       return le16_to_cpu(*(u16*)func->tmpbuf);
4824 +}
4825 +EXPORT_SYMBOL_GPL(sdio_readw);
4826 +
4827 +/**
4828 + *     sdio_writew - write a 16 bit integer to a SDIO function
4829 + *     @func: SDIO function to access
4830 + *     @b: integer to write
4831 + *     @addr: address to write to
4832 + *     @err_ret: optional status value from transfer
4833 + *
4834 + *     Writes a 16 bit integer to the address space of a given SDIO
4835 + *     function. @err_ret will contain the status of the actual
4836 + *     transfer.
4837 + */
4838 +void sdio_writew(struct sdio_func *func, unsigned short b, unsigned int addr,
4839 +       int *err_ret)
4840 +{
4841 +       int ret;
4842 +
4843 +       *(u16*)func->tmpbuf = cpu_to_le16(b);
4844 +
4845 +       ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 2);
4846 +       if (err_ret)
4847 +               *err_ret = ret;
4848 +}
4849 +EXPORT_SYMBOL_GPL(sdio_writew);
4850 +
4851 +/**
4852 + *     sdio_readl - read a 32 bit integer from a SDIO function
4853 + *     @func: SDIO function to access
4854 + *     @addr: address to read
4855 + *     @err_ret: optional status value from transfer
4856 + *
4857 + *     Reads a 32 bit integer from the address space of a given SDIO
4858 + *     function. If there is a problem reading the address,
4859 + *     0xffffffff is returned and @err_ret will contain the error
4860 + *     code.
4861 + */
4862 +unsigned long sdio_readl(struct sdio_func *func, unsigned int addr,
4863 +       int *err_ret)
4864 +{
4865 +       int ret;
4866 +
4867 +       if (err_ret)
4868 +               *err_ret = 0;
4869 +
4870 +       ret = sdio_memcpy_fromio(func, func->tmpbuf, addr, 4);
4871 +       if (ret) {
4872 +               if (err_ret)
4873 +                       *err_ret = ret;
4874 +               return 0xFFFFFFFF;
4875 +       }
4876 +
4877 +       return le32_to_cpu(*(u32*)func->tmpbuf);
4878 +}
4879 +EXPORT_SYMBOL_GPL(sdio_readl);
4880 +
4881 +/**
4882 + *     sdio_writel - write a 32 bit integer to a SDIO function
4883 + *     @func: SDIO function to access
4884 + *     @b: integer to write
4885 + *     @addr: address to write to
4886 + *     @err_ret: optional status value from transfer
4887 + *
4888 + *     Writes a 32 bit integer to the address space of a given SDIO
4889 + *     function. @err_ret will contain the status of the actual
4890 + *     transfer.
4891 + */
4892 +void sdio_writel(struct sdio_func *func, unsigned long b, unsigned int addr,
4893 +       int *err_ret)
4894 +{
4895 +       int ret;
4896 +
4897 +       *(u32*)func->tmpbuf = cpu_to_le32(b);
4898 +
4899 +       ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 4);
4900 +       if (err_ret)
4901 +               *err_ret = ret;
4902 +}
4903 +EXPORT_SYMBOL_GPL(sdio_writel);
4904 +
4905 +/**
4906 + *     sdio_f0_readb - read a single byte from SDIO function 0
4907 + *     @func: an SDIO function of the card
4908 + *     @addr: address to read
4909 + *     @err_ret: optional status value from transfer
4910 + *
4911 + *     Reads a single byte from the address space of SDIO function 0.
4912 + *     If there is a problem reading the address, 0xff is returned
4913 + *     and @err_ret will contain the error code.
4914 + */
4915 +unsigned char sdio_f0_readb(struct sdio_func *func, unsigned int addr,
4916 +       int *err_ret)
4917 +{
4918 +       int ret;
4919 +       unsigned char val;
4920 +
4921 +       BUG_ON(!func);
4922 +
4923 +       if (err_ret)
4924 +               *err_ret = 0;
4925 +
4926 +       ret = mmc_io_rw_direct(func->card, 0, 0, addr, 0, &val);
4927 +       if (ret) {
4928 +               if (err_ret)
4929 +                       *err_ret = ret;
4930 +               return 0xFF;
4931 +       }
4932 +
4933 +       return val;
4934 +}
4935 +EXPORT_SYMBOL_GPL(sdio_f0_readb);
4936 +
4937 +/**
4938 + *     sdio_f0_writeb - write a single byte to SDIO function 0
4939 + *     @func: an SDIO function of the card
4940 + *     @b: byte to write
4941 + *     @addr: address to write to
4942 + *     @err_ret: optional status value from transfer
4943 + *
4944 + *     Writes a single byte to the address space of SDIO function 0.
4945 + *     @err_ret will contain the status of the actual transfer.
4946 + *
4947 + *     Only writes to the vendor specific CCCR registers (0xF0 -
4948 + *     0xFF) are permiited; @err_ret will be set to -EINVAL for *
4949 + *     writes outside this range.
4950 + */
4951 +void sdio_f0_writeb(struct sdio_func *func, unsigned char b, unsigned int addr,
4952 +       int *err_ret)
4953 +{
4954 +       int ret;
4955 +
4956 +       BUG_ON(!func);
4957 +
4958 +       if (addr < 0xF0 || addr > 0xFF) {
4959 +               if (err_ret)
4960 +                       *err_ret = -EINVAL;
4961 +               return;
4962 +       }
4963 +
4964 +       ret = mmc_io_rw_direct(func->card, 1, 0, addr, b, NULL);
4965 +       if (err_ret)
4966 +               *err_ret = ret;
4967 +}
4968 +EXPORT_SYMBOL_GPL(sdio_f0_writeb);
4969 Index: linux-2.6.23.17/drivers/mmc/core/sdio_irq.c
4970 ===================================================================
4971 --- /dev/null
4972 +++ linux-2.6.23.17/drivers/mmc/core/sdio_irq.c
4973 @@ -0,0 +1,267 @@
4974 +/*
4975 + * linux/drivers/mmc/core/sdio_irq.c
4976 + *
4977 + * Author:      Nicolas Pitre
4978 + * Created:     June 18, 2007
4979 + * Copyright:   MontaVista Software Inc.
4980 + *
4981 + * This program is free software; you can redistribute it and/or modify
4982 + * it under the terms of the GNU General Public License as published by
4983 + * the Free Software Foundation; either version 2 of the License, or (at
4984 + * your option) any later version.
4985 + */
4986 +
4987 +#include <linux/kernel.h>
4988 +#include <linux/sched.h>
4989 +#include <linux/kthread.h>
4990 +#include <linux/wait.h>
4991 +#include <linux/delay.h>
4992 +
4993 +#include <linux/mmc/core.h>
4994 +#include <linux/mmc/host.h>
4995 +#include <linux/mmc/card.h>
4996 +#include <linux/mmc/sdio.h>
4997 +#include <linux/mmc/sdio_func.h>
4998 +
4999 +#include "sdio_ops.h"
5000 +
5001 +static int process_sdio_pending_irqs(struct mmc_card *card)
5002 +{
5003 +       int i, ret, count;
5004 +       unsigned char pending;
5005 +
5006 +       ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_INTx, 0, &pending);
5007 +       if (ret) {
5008 +               printk(KERN_DEBUG "%s: error %d reading SDIO_CCCR_INTx\n",
5009 +                      mmc_card_id(card), ret);
5010 +               return ret;
5011 +       }
5012 +
5013 +       count = 0;
5014 +       for (i = 1; i <= 7; i++) {
5015 +               if (pending & (1 << i)) {
5016 +                       struct sdio_func *func = card->sdio_func[i - 1];
5017 +                       if (!func) {
5018 +                               printk(KERN_WARNING "%s: pending IRQ for "
5019 +                                       "non-existant function\n",
5020 +                                       mmc_card_id(card));
5021 +                               ret = -EINVAL;
5022 +                       } else if (func->irq_handler) {
5023 +                               func->irq_handler(func);
5024 +                               count++;
5025 +                       } else {
5026 +                               printk(KERN_WARNING "%s: pending IRQ with no handler\n",
5027 +                                      sdio_func_id(func));
5028 +                               ret = -EINVAL;
5029 +                       }
5030 +               }
5031 +       }
5032 +
5033 +       if (count)
5034 +               return count;
5035 +
5036 +       return ret;
5037 +}
5038 +
5039 +static int sdio_irq_thread(void *_host)
5040 +{
5041 +       struct mmc_host *host = _host;
5042 +       struct sched_param param = { .sched_priority = 1 };
5043 +       unsigned long period, idle_period;
5044 +       int ret;
5045 +
5046 +       sched_setscheduler(current, SCHED_FIFO, &param);
5047 +
5048 +       /*
5049 +        * We want to allow for SDIO cards to work even on non SDIO
5050 +        * aware hosts.  One thing that non SDIO host cannot do is
5051 +        * asynchronous notification of pending SDIO card interrupts
5052 +        * hence we poll for them in that case.
5053 +        */
5054 +       idle_period = msecs_to_jiffies(10);
5055 +       period = (host->caps & MMC_CAP_SDIO_IRQ) ?
5056 +               MAX_SCHEDULE_TIMEOUT : idle_period;
5057 +
5058 +       pr_debug("%s: IRQ thread started (poll period = %lu jiffies)\n",
5059 +                mmc_hostname(host), period);
5060 +
5061 +       do {
5062 +               /*
5063 +                * We claim the host here on drivers behalf for a couple
5064 +                * reasons:
5065 +                *
5066 +                * 1) it is already needed to retrieve the CCCR_INTx;
5067 +                * 2) we want the driver(s) to clear the IRQ condition ASAP;
5068 +                * 3) we need to control the abort condition locally.
5069 +                *
5070 +                * Just like traditional hard IRQ handlers, we expect SDIO
5071 +                * IRQ handlers to be quick and to the point, so that the
5072 +                * holding of the host lock does not cover too much work
5073 +                * that doesn't require that lock to be held.
5074 +                */
5075 +               ret = __mmc_claim_host(host, &host->sdio_irq_thread_abort);
5076 +               if (ret)
5077 +                       break;
5078 +               ret = process_sdio_pending_irqs(host->card);
5079 +               mmc_release_host(host);
5080 +
5081 +               /*
5082 +                * Give other threads a chance to run in the presence of
5083 +                * errors.  FIXME: determine if due to card removal and
5084 +                * possibly exit this thread if so.
5085 +                */
5086 +               if (ret < 0)
5087 +                       ssleep(1);
5088 +
5089 +               /*
5090 +                * Adaptive polling frequency based on the assumption
5091 +                * that an interrupt will be closely followed by more.
5092 +                * This has a substantial benefit for network devices.
5093 +                */
5094 +               if (!(host->caps & MMC_CAP_SDIO_IRQ)) {
5095 +                       if (ret > 0)
5096 +                               period /= 2;
5097 +                       else {
5098 +                               period++;
5099 +                               if (period > idle_period)
5100 +                                       period = idle_period;
5101 +                       }
5102 +               }
5103 +
5104 +               set_task_state(current, TASK_INTERRUPTIBLE);
5105 +               if (host->caps & MMC_CAP_SDIO_IRQ)
5106 +                       host->ops->enable_sdio_irq(host, 1);
5107 +               if (!kthread_should_stop())
5108 +                       schedule_timeout(period);
5109 +               set_task_state(current, TASK_RUNNING);
5110 +       } while (!kthread_should_stop());
5111 +
5112 +       if (host->caps & MMC_CAP_SDIO_IRQ)
5113 +               host->ops->enable_sdio_irq(host, 0);
5114 +
5115 +       pr_debug("%s: IRQ thread exiting with code %d\n",
5116 +                mmc_hostname(host), ret);
5117 +
5118 +       return ret;
5119 +}
5120 +
5121 +static int sdio_card_irq_get(struct mmc_card *card)
5122 +{
5123 +       struct mmc_host *host = card->host;
5124 +
5125 +       WARN_ON(!host->claimed);
5126 +
5127 +       if (!host->sdio_irqs++) {
5128 +               atomic_set(&host->sdio_irq_thread_abort, 0);
5129 +               host->sdio_irq_thread =
5130 +                       kthread_run(sdio_irq_thread, host, "ksdiorqd");
5131 +               if (IS_ERR(host->sdio_irq_thread)) {
5132 +                       int err = PTR_ERR(host->sdio_irq_thread);
5133 +                       host->sdio_irqs--;
5134 +                       return err;
5135 +               }
5136 +       }
5137 +
5138 +       return 0;
5139 +}
5140 +
5141 +static int sdio_card_irq_put(struct mmc_card *card)
5142 +{
5143 +       struct mmc_host *host = card->host;
5144 +
5145 +       WARN_ON(!host->claimed);
5146 +       BUG_ON(host->sdio_irqs < 1);
5147 +
5148 +       if (!--host->sdio_irqs) {
5149 +               atomic_set(&host->sdio_irq_thread_abort, 1);
5150 +               kthread_stop(host->sdio_irq_thread);
5151 +       }
5152 +
5153 +       return 0;
5154 +}
5155 +
5156 +/**
5157 + *     sdio_claim_irq - claim the IRQ for a SDIO function
5158 + *     @func: SDIO function
5159 + *     @handler: IRQ handler callback
5160 + *
5161 + *     Claim and activate the IRQ for the given SDIO function. The provided
5162 + *     handler will be called when that IRQ is asserted.  The host is always
5163 + *     claimed already when the handler is called so the handler must not
5164 + *     call sdio_claim_host() nor sdio_release_host().
5165 + */
5166 +int sdio_claim_irq(struct sdio_func *func, sdio_irq_handler_t *handler)
5167 +{
5168 +       int ret;
5169 +       unsigned char reg;
5170 +
5171 +       BUG_ON(!func);
5172 +       BUG_ON(!func->card);
5173 +
5174 +       pr_debug("SDIO: Enabling IRQ for %s...\n", sdio_func_id(func));
5175 +
5176 +       if (func->irq_handler) {
5177 +               pr_debug("SDIO: IRQ for %s already in use.\n", sdio_func_id(func));
5178 +               return -EBUSY;
5179 +       }
5180 +
5181 +       ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IENx, 0, &reg);
5182 +       if (ret)
5183 +               return ret;
5184 +
5185 +       reg |= 1 << func->num;
5186 +
5187 +       reg |= 1; /* Master interrupt enable */
5188 +
5189 +       ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IENx, reg, NULL);
5190 +       if (ret)
5191 +               return ret;
5192 +
5193 +       func->irq_handler = handler;
5194 +       ret = sdio_card_irq_get(func->card);
5195 +       if (ret)
5196 +               func->irq_handler = NULL;
5197 +
5198 +       return ret;
5199 +}
5200 +EXPORT_SYMBOL_GPL(sdio_claim_irq);
5201 +
5202 +/**
5203 + *     sdio_release_irq - release the IRQ for a SDIO function
5204 + *     @func: SDIO function
5205 + *
5206 + *     Disable and release the IRQ for the given SDIO function.
5207 + */
5208 +int sdio_release_irq(struct sdio_func *func)
5209 +{
5210 +       int ret;
5211 +       unsigned char reg;
5212 +
5213 +       BUG_ON(!func);
5214 +       BUG_ON(!func->card);
5215 +
5216 +       pr_debug("SDIO: Disabling IRQ for %s...\n", sdio_func_id(func));
5217 +
5218 +       if (func->irq_handler) {
5219 +               func->irq_handler = NULL;
5220 +               sdio_card_irq_put(func->card);
5221 +       }
5222 +
5223 +       ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IENx, 0, &reg);
5224 +       if (ret)
5225 +               return ret;
5226 +
5227 +       reg &= ~(1 << func->num);
5228 +
5229 +       /* Disable master interrupt with the last function interrupt */
5230 +       if (!(reg & 0xFE))
5231 +               reg = 0;
5232 +
5233 +       ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IENx, reg, NULL);
5234 +       if (ret)
5235 +               return ret;
5236 +
5237 +       return 0;
5238 +}
5239 +EXPORT_SYMBOL_GPL(sdio_release_irq);
5240 +
5241 Index: linux-2.6.23.17/drivers/mmc/core/sdio_ops.c
5242 ===================================================================
5243 --- /dev/null
5244 +++ linux-2.6.23.17/drivers/mmc/core/sdio_ops.c
5245 @@ -0,0 +1,175 @@
5246 +/*
5247 + *  linux/drivers/mmc/sdio_ops.c
5248 + *
5249 + *  Copyright 2006-2007 Pierre Ossman
5250 + *
5251 + * This program is free software; you can redistribute it and/or modify
5252 + * it under the terms of the GNU General Public License as published by
5253 + * the Free Software Foundation; either version 2 of the License, or (at
5254 + * your option) any later version.
5255 + */
5256 +
5257 +#include <linux/scatterlist.h>
5258 +
5259 +#include <linux/mmc/host.h>
5260 +#include <linux/mmc/card.h>
5261 +#include <linux/mmc/mmc.h>
5262 +#include <linux/mmc/sdio.h>
5263 +
5264 +#include "core.h"
5265 +
5266 +int mmc_send_io_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
5267 +{
5268 +       struct mmc_command cmd;
5269 +       int i, err = 0;
5270 +
5271 +       BUG_ON(!host);
5272 +
5273 +       memset(&cmd, 0, sizeof(struct mmc_command));
5274 +
5275 +       cmd.opcode = SD_IO_SEND_OP_COND;
5276 +       cmd.arg = ocr;
5277 +       cmd.flags = MMC_RSP_SPI_R4 | MMC_RSP_R4 | MMC_CMD_BCR;
5278 +
5279 +       for (i = 100; i; i--) {
5280 +               err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
5281 +               if (err)
5282 +                       break;
5283 +
5284 +               /* if we're just probing, do a single pass */
5285 +               if (ocr == 0)
5286 +                       break;
5287 +
5288 +               /* otherwise wait until reset completes */
5289 +               if (mmc_host_is_spi(host)) {
5290 +                       /*
5291 +                        * Both R1_SPI_IDLE and MMC_CARD_BUSY indicate
5292 +                        * an initialized card under SPI, but some cards
5293 +                        * (Marvell's) only behave when looking at this
5294 +                        * one.
5295 +                        */
5296 +                       if (cmd.resp[1] & MMC_CARD_BUSY)
5297 +                               break;
5298 +               } else {
5299 +                       if (cmd.resp[0] & MMC_CARD_BUSY)
5300 +                               break;
5301 +               }
5302 +
5303 +               err = -ETIMEDOUT;
5304 +
5305 +               mmc_delay(10);
5306 +       }
5307 +
5308 +       if (rocr)
5309 +               *rocr = cmd.resp[mmc_host_is_spi(host) ? 1 : 0];
5310 +
5311 +       return err;
5312 +}
5313 +
5314 +int mmc_io_rw_direct(struct mmc_card *card, int write, unsigned fn,
5315 +       unsigned addr, u8 in, u8* out)
5316 +{
5317 +       struct mmc_command cmd;
5318 +       int err;
5319 +
5320 +       BUG_ON(!card);
5321 +       BUG_ON(fn > 7);
5322 +
5323 +       memset(&cmd, 0, sizeof(struct mmc_command));
5324 +
5325 +       cmd.opcode = SD_IO_RW_DIRECT;
5326 +       cmd.arg = write ? 0x80000000 : 0x00000000;
5327 +       cmd.arg |= fn << 28;
5328 +       cmd.arg |= (write && out) ? 0x08000000 : 0x00000000;
5329 +       cmd.arg |= addr << 9;
5330 +       cmd.arg |= in;
5331 +       cmd.flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_AC;
5332 +
5333 +       err = mmc_wait_for_cmd(card->host, &cmd, 0);
5334 +       if (err)
5335 +               return err;
5336 +
5337 +       if (mmc_host_is_spi(card->host)) {
5338 +               /* host driver already reported errors */
5339 +       } else {
5340 +               if (cmd.resp[0] & R5_ERROR)
5341 +                       return -EIO;
5342 +               if (cmd.resp[0] & R5_FUNCTION_NUMBER)
5343 +                       return -EINVAL;
5344 +               if (cmd.resp[0] & R5_OUT_OF_RANGE)
5345 +                       return -ERANGE;
5346 +       }
5347 +
5348 +       if (out) {
5349 +               if (mmc_host_is_spi(card->host))
5350 +                       *out = (cmd.resp[0] >> 8) & 0xFF;
5351 +               else
5352 +                       *out = cmd.resp[0] & 0xFF;
5353 +       }
5354 +
5355 +       return 0;
5356 +}
5357 +
5358 +int mmc_io_rw_extended(struct mmc_card *card, int write, unsigned fn,
5359 +       unsigned addr, int incr_addr, u8 *buf, unsigned blocks, unsigned blksz)
5360 +{
5361 +       struct mmc_request mrq;
5362 +       struct mmc_command cmd;
5363 +       struct mmc_data data;
5364 +       struct scatterlist sg;
5365 +
5366 +       BUG_ON(!card);
5367 +       BUG_ON(fn > 7);
5368 +       BUG_ON(blocks == 1 && blksz > 512);
5369 +       WARN_ON(blocks == 0);
5370 +       WARN_ON(blksz == 0);
5371 +
5372 +       memset(&mrq, 0, sizeof(struct mmc_request));
5373 +       memset(&cmd, 0, sizeof(struct mmc_command));
5374 +       memset(&data, 0, sizeof(struct mmc_data));
5375 +
5376 +       mrq.cmd = &cmd;
5377 +       mrq.data = &data;
5378 +
5379 +       cmd.opcode = SD_IO_RW_EXTENDED;
5380 +       cmd.arg = write ? 0x80000000 : 0x00000000;
5381 +       cmd.arg |= fn << 28;
5382 +       cmd.arg |= incr_addr ? 0x04000000 : 0x00000000;
5383 +       cmd.arg |= addr << 9;
5384 +       if (blocks == 1 && blksz <= 512)
5385 +               cmd.arg |= (blksz == 512) ? 0 : blksz;  /* byte mode */
5386 +       else
5387 +               cmd.arg |= 0x08000000 | blocks;         /* block mode */
5388 +       cmd.flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_ADTC;
5389 +
5390 +       data.blksz = blksz;
5391 +       data.blocks = blocks;
5392 +       data.flags = write ? MMC_DATA_WRITE : MMC_DATA_READ;
5393 +       data.sg = &sg;
5394 +       data.sg_len = 1;
5395 +
5396 +       sg_init_one(&sg, buf, blksz * blocks);
5397 +
5398 +       mmc_set_data_timeout(&data, card);
5399 +
5400 +       mmc_wait_for_req(card->host, &mrq);
5401 +
5402 +       if (cmd.error)
5403 +               return cmd.error;
5404 +       if (data.error)
5405 +               return data.error;
5406 +
5407 +       if (mmc_host_is_spi(card->host)) {
5408 +               /* host driver already reported errors */
5409 +       } else {
5410 +               if (cmd.resp[0] & R5_ERROR)
5411 +                       return -EIO;
5412 +               if (cmd.resp[0] & R5_FUNCTION_NUMBER)
5413 +                       return -EINVAL;
5414 +               if (cmd.resp[0] & R5_OUT_OF_RANGE)
5415 +                       return -ERANGE;
5416 +       }
5417 +
5418 +       return 0;
5419 +}
5420 +
5421 Index: linux-2.6.23.17/drivers/mmc/core/sdio_ops.h
5422 ===================================================================
5423 --- /dev/null
5424 +++ linux-2.6.23.17/drivers/mmc/core/sdio_ops.h
5425 @@ -0,0 +1,22 @@
5426 +/*
5427 + *  linux/drivers/mmc/sdio_ops.c
5428 + *
5429 + *  Copyright 2006-2007 Pierre Ossman
5430 + *
5431 + * This program is free software; you can redistribute it and/or modify
5432 + * it under the terms of the GNU General Public License as published by
5433 + * the Free Software Foundation; either version 2 of the License, or (at
5434 + * your option) any later version.
5435 + */
5436 +
5437 +#ifndef _MMC_SDIO_OPS_H
5438 +#define _MMC_SDIO_OPS_H
5439 +
5440 +int mmc_send_io_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr);
5441 +int mmc_io_rw_direct(struct mmc_card *card, int write, unsigned fn,
5442 +       unsigned addr, u8 in, u8* out);
5443 +int mmc_io_rw_extended(struct mmc_card *card, int write, unsigned fn,
5444 +       unsigned addr, int incr_addr, u8 *buf, unsigned blocks, unsigned blksz);
5445 +
5446 +#endif
5447 +
5448 Index: linux-2.6.23.17/include/linux/mmc/card.h
5449 ===================================================================
5450 --- linux-2.6.23.17.orig/include/linux/mmc/card.h
5451 +++ linux-2.6.23.17/include/linux/mmc/card.h
5452 @@ -55,7 +55,28 @@ struct sd_switch_caps {
5453         unsigned int            hs_max_dtr;
5454  };
5455  
5456 +struct sdio_cccr {
5457 +       unsigned int            sdio_vsn;
5458 +       unsigned int            sd_vsn;
5459 +       unsigned int            multi_block:1,
5460 +                               low_speed:1,
5461 +                               wide_bus:1,
5462 +                               high_power:1,
5463 +                               high_speed:1;
5464 +};
5465 +
5466 +struct sdio_cis {
5467 +       unsigned short          vendor;
5468 +       unsigned short          device;
5469 +       unsigned short          blksize;
5470 +       unsigned int            max_dtr;
5471 +};
5472 +
5473  struct mmc_host;
5474 +struct sdio_func;
5475 +struct sdio_func_tuple;
5476 +
5477 +#define SDIO_MAX_FUNCS         7
5478  
5479  /*
5480   * MMC device
5481 @@ -67,11 +88,13 @@ struct mmc_card {
5482         unsigned int            type;           /* card type */
5483  #define MMC_TYPE_MMC           0               /* MMC card */
5484  #define MMC_TYPE_SD            1               /* SD card */
5485 +#define MMC_TYPE_SDIO          2               /* SDIO card */
5486         unsigned int            state;          /* (our) card state */
5487  #define MMC_STATE_PRESENT      (1<<0)          /* present in sysfs */
5488  #define MMC_STATE_READONLY     (1<<1)          /* card is read-only */
5489  #define MMC_STATE_HIGHSPEED    (1<<2)          /* card is in high speed mode */
5490  #define MMC_STATE_BLOCKADDR    (1<<3)          /* card uses block-addressing */
5491 +
5492         u32                     raw_cid[4];     /* raw card CID */
5493         u32                     raw_csd[4];     /* raw card CSD */
5494         u32                     raw_scr[2];     /* raw card SCR */
5495 @@ -80,10 +103,19 @@ struct mmc_card {
5496         struct mmc_ext_csd      ext_csd;        /* mmc v4 extended card specific */
5497         struct sd_scr           scr;            /* extra SD information */
5498         struct sd_switch_caps   sw_caps;        /* switch (CMD6) caps */
5499 +
5500 +       unsigned int            sdio_funcs;     /* number of SDIO functions */
5501 +       struct sdio_cccr        cccr;           /* common card info */
5502 +       struct sdio_cis         cis;            /* common tuple info */
5503 +       struct sdio_func        *sdio_func[SDIO_MAX_FUNCS]; /* SDIO functions (devices) */
5504 +       unsigned                num_info;       /* number of info strings */
5505 +       const char              **info;         /* info strings */
5506 +       struct sdio_func_tuple  *tuples;        /* unknown common tuples */
5507  };
5508  
5509  #define mmc_card_mmc(c)                ((c)->type == MMC_TYPE_MMC)
5510  #define mmc_card_sd(c)         ((c)->type == MMC_TYPE_SD)
5511 +#define mmc_card_sdio(c)       ((c)->type == MMC_TYPE_SDIO)
5512  
5513  #define mmc_card_present(c)    ((c)->state & MMC_STATE_PRESENT)
5514  #define mmc_card_readonly(c)   ((c)->state & MMC_STATE_READONLY)
5515 Index: linux-2.6.23.17/include/linux/mmc/core.h
5516 ===================================================================
5517 --- linux-2.6.23.17.orig/include/linux/mmc/core.h
5518 +++ linux-2.6.23.17/include/linux/mmc/core.h
5519 @@ -25,14 +25,20 @@ struct mmc_command {
5520  #define MMC_RSP_CRC    (1 << 2)                /* expect valid crc */
5521  #define MMC_RSP_BUSY   (1 << 3)                /* card may send busy */
5522  #define MMC_RSP_OPCODE (1 << 4)                /* response contains opcode */
5523 -#define MMC_CMD_MASK   (3 << 5)                /* command type */
5524 +
5525 +#define MMC_CMD_MASK   (3 << 5)                /* non-SPI command type */
5526  #define MMC_CMD_AC     (0 << 5)
5527  #define MMC_CMD_ADTC   (1 << 5)
5528  #define MMC_CMD_BC     (2 << 5)
5529  #define MMC_CMD_BCR    (3 << 5)
5530  
5531 +#define MMC_RSP_SPI_S1 (1 << 7)                /* one status byte */
5532 +#define MMC_RSP_SPI_S2 (1 << 8)                /* second byte */
5533 +#define MMC_RSP_SPI_B4 (1 << 9)                /* four data bytes */
5534 +#define MMC_RSP_SPI_BUSY (1 << 10)             /* card may send busy */
5535 +
5536  /*
5537 - * These are the response types, and correspond to valid bit
5538 + * These are the native response types, and correspond to valid bit
5539   * patterns of the above flags.  One additional valid pattern
5540   * is all zeros, which means we don't expect a response.
5541   */
5542 @@ -41,12 +47,30 @@ struct mmc_command {
5543  #define MMC_RSP_R1B    (MMC_RSP_PRESENT|MMC_RSP_CRC|MMC_RSP_OPCODE|MMC_RSP_BUSY)
5544  #define MMC_RSP_R2     (MMC_RSP_PRESENT|MMC_RSP_136|MMC_RSP_CRC)
5545  #define MMC_RSP_R3     (MMC_RSP_PRESENT)
5546 +#define MMC_RSP_R4     (MMC_RSP_PRESENT)
5547 +#define MMC_RSP_R5     (MMC_RSP_PRESENT|MMC_RSP_CRC|MMC_RSP_OPCODE)
5548  #define MMC_RSP_R6     (MMC_RSP_PRESENT|MMC_RSP_CRC|MMC_RSP_OPCODE)
5549  #define MMC_RSP_R7     (MMC_RSP_PRESENT|MMC_RSP_CRC|MMC_RSP_OPCODE)
5550  
5551  #define mmc_resp_type(cmd)     ((cmd)->flags & (MMC_RSP_PRESENT|MMC_RSP_136|MMC_RSP_CRC|MMC_RSP_BUSY|MMC_RSP_OPCODE))
5552  
5553  /*
5554 + * These are the SPI response types for MMC, SD, and SDIO cards.
5555 + * Commands return R1, with maybe more info.  Zero is an error type;
5556 + * callers must always provide the appropriate MMC_RSP_SPI_Rx flags.
5557 + */
5558 +#define MMC_RSP_SPI_R1 (MMC_RSP_SPI_S1)
5559 +#define MMC_RSP_SPI_R1B        (MMC_RSP_SPI_S1|MMC_RSP_SPI_BUSY)
5560 +#define MMC_RSP_SPI_R2 (MMC_RSP_SPI_S1|MMC_RSP_SPI_S2)
5561 +#define MMC_RSP_SPI_R3 (MMC_RSP_SPI_S1|MMC_RSP_SPI_B4)
5562 +#define MMC_RSP_SPI_R4 (MMC_RSP_SPI_S1|MMC_RSP_SPI_B4)
5563 +#define MMC_RSP_SPI_R5 (MMC_RSP_SPI_S1|MMC_RSP_SPI_S2)
5564 +#define MMC_RSP_SPI_R7 (MMC_RSP_SPI_S1|MMC_RSP_SPI_B4)
5565 +
5566 +#define mmc_spi_resp_type(cmd) ((cmd)->flags & \
5567 +               (MMC_RSP_SPI_S1|MMC_RSP_SPI_BUSY|MMC_RSP_SPI_S2|MMC_RSP_SPI_B4))
5568 +
5569 +/*
5570   * These are the command types.
5571   */
5572  #define mmc_cmd_type(cmd)      ((cmd)->flags & MMC_CMD_MASK)
5573 @@ -54,12 +78,19 @@ struct mmc_command {
5574         unsigned int            retries;        /* max number of retries */
5575         unsigned int            error;          /* command error */
5576  
5577 -#define MMC_ERR_NONE   0
5578 -#define MMC_ERR_TIMEOUT        1
5579 -#define MMC_ERR_BADCRC 2
5580 -#define MMC_ERR_FIFO   3
5581 -#define MMC_ERR_FAILED 4
5582 -#define MMC_ERR_INVALID        5
5583 +/*
5584 + * Standard errno values are used for errors, but some have specific
5585 + * meaning in the MMC layer:
5586 + *
5587 + * ETIMEDOUT    Card took too long to respond
5588 + * EILSEQ       Basic format problem with the received or sent data
5589 + *              (e.g. CRC check failed, incorrect opcode in response
5590 + *              or bad end bit)
5591 + * EINVAL       Request cannot be performed because of restrictions
5592 + *              in hardware and/or the driver
5593 + * ENOMEDIUM    Host can determine that the slot is empty and is
5594 + *              actively failing requests
5595 + */
5596  
5597         struct mmc_data         *data;          /* data segment associated with cmd */
5598         struct mmc_request      *mrq;           /* associated request */
5599 @@ -76,7 +107,6 @@ struct mmc_data {
5600  #define MMC_DATA_WRITE (1 << 8)
5601  #define MMC_DATA_READ  (1 << 9)
5602  #define MMC_DATA_STREAM        (1 << 10)
5603 -#define MMC_DATA_MULTI (1 << 11)
5604  
5605         unsigned int            bytes_xfered;
5606  
5607 @@ -104,9 +134,20 @@ extern int mmc_wait_for_cmd(struct mmc_h
5608  extern int mmc_wait_for_app_cmd(struct mmc_host *, struct mmc_card *,
5609         struct mmc_command *, int);
5610  
5611 -extern void mmc_set_data_timeout(struct mmc_data *, const struct mmc_card *, int);
5612 +extern void mmc_set_data_timeout(struct mmc_data *, const struct mmc_card *);
5613  
5614 -extern void mmc_claim_host(struct mmc_host *host);
5615 +extern int __mmc_claim_host(struct mmc_host *host, atomic_t *abort);
5616  extern void mmc_release_host(struct mmc_host *host);
5617  
5618 +/**
5619 + *     mmc_claim_host - exclusively claim a host
5620 + *     @host: mmc host to claim
5621 + *
5622 + *     Claim a host for a set of operations.
5623 + */
5624 +static inline void mmc_claim_host(struct mmc_host *host)
5625 +{
5626 +       __mmc_claim_host(host, NULL);
5627 +}
5628 +
5629  #endif
5630 Index: linux-2.6.23.17/include/linux/mmc/host.h
5631 ===================================================================
5632 --- linux-2.6.23.17.orig/include/linux/mmc/host.h
5633 +++ linux-2.6.23.17/include/linux/mmc/host.h
5634 @@ -10,6 +10,8 @@
5635  #ifndef LINUX_MMC_HOST_H
5636  #define LINUX_MMC_HOST_H
5637  
5638 +#include <linux/leds.h>
5639 +
5640  #include <linux/mmc/core.h>
5641  
5642  struct mmc_ios {
5643 @@ -51,6 +53,7 @@ struct mmc_host_ops {
5644         void    (*request)(struct mmc_host *host, struct mmc_request *req);
5645         void    (*set_ios)(struct mmc_host *host, struct mmc_ios *ios);
5646         int     (*get_ro)(struct mmc_host *host);
5647 +       void    (*enable_sdio_irq)(struct mmc_host *host, int enable);
5648  };
5649  
5650  struct mmc_card;
5651 @@ -87,9 +90,10 @@ struct mmc_host {
5652  
5653  #define MMC_CAP_4_BIT_DATA     (1 << 0)        /* Can the host do 4 bit transfers */
5654  #define MMC_CAP_MULTIWRITE     (1 << 1)        /* Can accurately report bytes sent to card on error */
5655 -#define MMC_CAP_BYTEBLOCK      (1 << 2)        /* Can do non-log2 block sizes */
5656 -#define MMC_CAP_MMC_HIGHSPEED  (1 << 3)        /* Can do MMC high-speed timing */
5657 -#define MMC_CAP_SD_HIGHSPEED   (1 << 4)        /* Can do SD high-speed timing */
5658 +#define MMC_CAP_MMC_HIGHSPEED  (1 << 2)        /* Can do MMC high-speed timing */
5659 +#define MMC_CAP_SD_HIGHSPEED   (1 << 3)        /* Can do SD high-speed timing */
5660 +#define MMC_CAP_SDIO_IRQ       (1 << 4)        /* Can signal pending SDIO IRQs */
5661 +#define MMC_CAP_SPI            (1 << 5)        /* Talks only SPI protocols */
5662  
5663         /* host specific block data */
5664         unsigned int            max_seg_size;   /* see blk_queue_max_segment_size */
5665 @@ -106,23 +110,30 @@ struct mmc_host {
5666         struct mmc_ios          ios;            /* current io bus settings */
5667         u32                     ocr;            /* the current OCR setting */
5668  
5669 -       unsigned int            mode;           /* current card mode of host */
5670 -#define MMC_MODE_MMC           0
5671 -#define MMC_MODE_SD            1
5672 +       /* group bitfields together to minimize padding */
5673 +       unsigned int            use_spi_crc:1;
5674 +       unsigned int            claimed:1;      /* host exclusively claimed */
5675 +       unsigned int            bus_dead:1;     /* bus has been released */
5676 +#ifdef CONFIG_MMC_DEBUG
5677 +       unsigned int            removed:1;      /* host is being removed */
5678 +#endif
5679  
5680         struct mmc_card         *card;          /* device attached to this host */
5681  
5682         wait_queue_head_t       wq;
5683 -       unsigned int            claimed:1;      /* host exclusively claimed */
5684  
5685         struct delayed_work     detect;
5686 -#ifdef CONFIG_MMC_DEBUG
5687 -       unsigned int            removed:1;      /* host is being removed */
5688 -#endif
5689  
5690         const struct mmc_bus_ops *bus_ops;      /* current bus driver */
5691         unsigned int            bus_refs;       /* reference counter */
5692 -       unsigned int            bus_dead:1;     /* bus has been released */
5693 +
5694 +       unsigned int            sdio_irqs;
5695 +       struct task_struct      *sdio_irq_thread;
5696 +       atomic_t                sdio_irq_thread_abort;
5697 +
5698 +#ifdef CONFIG_LEDS_TRIGGERS
5699 +       struct led_trigger      *led;           /* activity led */
5700 +#endif
5701  
5702         unsigned long           private[0] ____cacheline_aligned;
5703  };
5704 @@ -137,6 +148,8 @@ static inline void *mmc_priv(struct mmc_
5705         return (void *)host->private;
5706  }
5707  
5708 +#define mmc_host_is_spi(host)  ((host)->caps & MMC_CAP_SPI)
5709 +
5710  #define mmc_dev(x)     ((x)->parent)
5711  #define mmc_classdev(x)        (&(x)->class_dev)
5712  #define mmc_hostname(x)        ((x)->class_dev.bus_id)
5713 @@ -147,5 +160,11 @@ extern int mmc_resume_host(struct mmc_ho
5714  extern void mmc_detect_change(struct mmc_host *, unsigned long delay);
5715  extern void mmc_request_done(struct mmc_host *, struct mmc_request *);
5716  
5717 +static inline void mmc_signal_sdio_irq(struct mmc_host *host)
5718 +{
5719 +       host->ops->enable_sdio_irq(host, 0);
5720 +       wake_up_process(host->sdio_irq_thread);
5721 +}
5722 +
5723  #endif
5724  
5725 Index: linux-2.6.23.17/include/linux/mmc/mmc.h
5726 ===================================================================
5727 --- linux-2.6.23.17.orig/include/linux/mmc/mmc.h
5728 +++ linux-2.6.23.17/include/linux/mmc/mmc.h
5729 @@ -27,7 +27,7 @@
5730  
5731  /* Standard MMC commands (4.1)           type  argument     response */
5732     /* class 1 */
5733 -#define        MMC_GO_IDLE_STATE         0   /* bc                          */
5734 +#define MMC_GO_IDLE_STATE         0   /* bc                          */
5735  #define MMC_SEND_OP_COND          1   /* bcr  [31:0] OCR         R3  */
5736  #define MMC_ALL_SEND_CID          2   /* bcr                     R2  */
5737  #define MMC_SET_RELATIVE_ADDR     3   /* ac   [31:16] RCA        R1  */
5738 @@ -39,8 +39,10 @@
5739  #define MMC_SEND_CID             10   /* ac   [31:16] RCA        R2  */
5740  #define MMC_READ_DAT_UNTIL_STOP  11   /* adtc [31:0] dadr        R1  */
5741  #define MMC_STOP_TRANSMISSION    12   /* ac                      R1b */
5742 -#define MMC_SEND_STATUS                 13   /* ac   [31:16] RCA        R1  */
5743 +#define MMC_SEND_STATUS          13   /* ac   [31:16] RCA        R1  */
5744  #define MMC_GO_INACTIVE_STATE    15   /* ac   [31:16] RCA            */
5745 +#define MMC_SPI_READ_OCR         58   /* spi                  spi_R3 */
5746 +#define MMC_SPI_CRC_ON_OFF       59   /* spi  [0:0] flag      spi_R1 */
5747  
5748    /* class 2 */
5749  #define MMC_SET_BLOCKLEN         16   /* ac   [31:0] block len   R1  */
5750 @@ -90,15 +92,15 @@
5751   */
5752  
5753  /*
5754 -  MMC status in R1
5755 +  MMC status in R1, for native mode (SPI bits are different)
5756    Type
5757 -       e : error bit
5758 +       e : error bit
5759         s : status bit
5760         r : detected and set for the actual command response
5761         x : detected and set during command execution. the host must poll
5762              the card by sending status command in order to read these bits.
5763    Clear condition
5764 -       a : according to the card state
5765 +       a : according to the card state
5766         b : always related to the previous command. Reception of
5767              a valid command will clear it (with a delay of one command)
5768         c : clear by read
5769 @@ -124,10 +126,33 @@
5770  #define R1_CARD_ECC_DISABLED   (1 << 14)       /* sx, a */
5771  #define R1_ERASE_RESET         (1 << 13)       /* sr, c */
5772  #define R1_STATUS(x)            (x & 0xFFFFE000)
5773 -#define R1_CURRENT_STATE(x)            ((x & 0x00001E00) >> 9) /* sx, b (4 bits) */
5774 +#define R1_CURRENT_STATE(x)    ((x & 0x00001E00) >> 9) /* sx, b (4 bits) */
5775  #define R1_READY_FOR_DATA      (1 << 8)        /* sx, a */
5776  #define R1_APP_CMD             (1 << 5)        /* sr, c */
5777  
5778 +/*
5779 + * MMC/SD in SPI mode reports R1 status always, and R2 for SEND_STATUS
5780 + * R1 is the low order byte; R2 is the next highest byte, when present.
5781 + */
5782 +#define R1_SPI_IDLE            (1 << 0)
5783 +#define R1_SPI_ERASE_RESET     (1 << 1)
5784 +#define R1_SPI_ILLEGAL_COMMAND (1 << 2)
5785 +#define R1_SPI_COM_CRC         (1 << 3)
5786 +#define R1_SPI_ERASE_SEQ       (1 << 4)
5787 +#define R1_SPI_ADDRESS         (1 << 5)
5788 +#define R1_SPI_PARAMETER       (1 << 6)
5789 +/* R1 bit 7 is always zero */
5790 +#define R2_SPI_CARD_LOCKED     (1 << 8)
5791 +#define R2_SPI_WP_ERASE_SKIP   (1 << 9)        /* or lock/unlock fail */
5792 +#define R2_SPI_LOCK_UNLOCK_FAIL        R2_SPI_WP_ERASE_SKIP
5793 +#define R2_SPI_ERROR           (1 << 10)
5794 +#define R2_SPI_CC_ERROR                (1 << 11)
5795 +#define R2_SPI_CARD_ECC_ERROR  (1 << 12)
5796 +#define R2_SPI_WP_VIOLATION    (1 << 13)
5797 +#define R2_SPI_ERASE_PARAM     (1 << 14)
5798 +#define R2_SPI_OUT_OF_RANGE    (1 << 15)       /* or CSD overwrite */
5799 +#define R2_SPI_CSD_OVERWRITE   R2_SPI_OUT_OF_RANGE
5800 +
5801  /* These are unpacked versions of the actual responses */
5802  
5803  struct _mmc_csd {
5804 @@ -182,6 +207,7 @@ struct _mmc_csd {
5805   */
5806  #define CCC_BASIC              (1<<0)  /* (0) Basic protocol functions */
5807                                         /* (CMD0,1,2,3,4,7,9,10,12,13,15) */
5808 +                                       /* (and for SPI, CMD58,59) */
5809  #define CCC_STREAM_READ                (1<<1)  /* (1) Stream read commands */
5810                                         /* (CMD11) */
5811  #define CCC_BLOCK_READ         (1<<2)  /* (2) Block read commands */
5812 @@ -227,6 +253,7 @@ struct _mmc_csd {
5813  #define EXT_CSD_BUS_WIDTH      183     /* R/W */
5814  #define EXT_CSD_HS_TIMING      185     /* R/W */
5815  #define EXT_CSD_CARD_TYPE      196     /* RO */
5816 +#define EXT_CSD_REV            192     /* RO */
5817  #define EXT_CSD_SEC_CNT                212     /* RO, 4 bytes */
5818  
5819  /*
5820 Index: linux-2.6.23.17/include/linux/mmc/sdio.h
5821 ===================================================================
5822 --- /dev/null
5823 +++ linux-2.6.23.17/include/linux/mmc/sdio.h
5824 @@ -0,0 +1,159 @@
5825 +/*
5826 + *  include/linux/mmc/sdio.h
5827 + *
5828 + *  Copyright 2006-2007 Pierre Ossman
5829 + *
5830 + * This program is free software; you can redistribute it and/or modify
5831 + * it under the terms of the GNU General Public License as published by
5832 + * the Free Software Foundation; either version 2 of the License, or (at
5833 + * your option) any later version.
5834 + */
5835 +
5836 +#ifndef MMC_SDIO_H
5837 +#define MMC_SDIO_H
5838 +
5839 +/* SDIO commands                         type  argument     response */
5840 +#define SD_IO_SEND_OP_COND          5 /* bcr  [23:0] OCR         R4  */
5841 +#define SD_IO_RW_DIRECT            52 /* ac   [31:0] See below   R5  */
5842 +#define SD_IO_RW_EXTENDED          53 /* adtc [31:0] See below   R5  */
5843 +
5844 +/*
5845 + * SD_IO_RW_DIRECT argument format:
5846 + *
5847 + *      [31] R/W flag
5848 + *      [30:28] Function number
5849 + *      [27] RAW flag
5850 + *      [25:9] Register address
5851 + *      [7:0] Data
5852 + */
5853 +
5854 +/*
5855 + * SD_IO_RW_EXTENDED argument format:
5856 + *
5857 + *      [31] R/W flag
5858 + *      [30:28] Function number
5859 + *      [27] Block mode
5860 + *      [26] Increment address
5861 + *      [25:9] Register address
5862 + *      [8:0] Byte/block count
5863 + */
5864 +
5865 +/*
5866 +  SDIO status in R5
5867 +  Type
5868 +       e : error bit
5869 +       s : status bit
5870 +       r : detected and set for the actual command response
5871 +       x : detected and set during command execution. the host must poll
5872 +            the card by sending status command in order to read these bits.
5873 +  Clear condition
5874 +       a : according to the card state
5875 +       b : always related to the previous command. Reception of
5876 +            a valid command will clear it (with a delay of one command)
5877 +       c : clear by read
5878 + */
5879 +
5880 +#define R5_COM_CRC_ERROR       (1 << 15)       /* er, b */
5881 +#define R5_ILLEGAL_COMMAND     (1 << 14)       /* er, b */
5882 +#define R5_ERROR               (1 << 11)       /* erx, c */
5883 +#define R5_FUNCTION_NUMBER     (1 << 9)        /* er, c */
5884 +#define R5_OUT_OF_RANGE                (1 << 8)        /* er, c */
5885 +#define R5_STATUS(x)           (x & 0xCB00)
5886 +#define R5_IO_CURRENT_STATE(x) ((x & 0x3000) >> 12) /* s, b */
5887 +
5888 +/*
5889 + * Card Common Control Registers (CCCR)
5890 + */
5891 +
5892 +#define SDIO_CCCR_CCCR         0x00
5893 +
5894 +#define  SDIO_CCCR_REV_1_00    0       /* CCCR/FBR Version 1.00 */
5895 +#define  SDIO_CCCR_REV_1_10    1       /* CCCR/FBR Version 1.10 */
5896 +#define  SDIO_CCCR_REV_1_20    2       /* CCCR/FBR Version 1.20 */
5897 +
5898 +#define  SDIO_SDIO_REV_1_00    0       /* SDIO Spec Version 1.00 */
5899 +#define  SDIO_SDIO_REV_1_10    1       /* SDIO Spec Version 1.10 */
5900 +#define  SDIO_SDIO_REV_1_20    2       /* SDIO Spec Version 1.20 */
5901 +#define  SDIO_SDIO_REV_2_00    3       /* SDIO Spec Version 2.00 */
5902 +
5903 +#define SDIO_CCCR_SD           0x01
5904 +
5905 +#define  SDIO_SD_REV_1_01      0       /* SD Physical Spec Version 1.01 */
5906 +#define  SDIO_SD_REV_1_10      1       /* SD Physical Spec Version 1.10 */
5907 +#define  SDIO_SD_REV_2_00      2       /* SD Physical Spec Version 2.00 */
5908 +
5909 +#define SDIO_CCCR_IOEx         0x02
5910 +#define SDIO_CCCR_IORx         0x03
5911 +
5912 +#define SDIO_CCCR_IENx         0x04    /* Function/Master Interrupt Enable */
5913 +#define SDIO_CCCR_INTx         0x05    /* Function Interrupt Pending */
5914 +
5915 +#define SDIO_CCCR_ABORT                0x06    /* function abort/card reset */
5916 +
5917 +#define SDIO_CCCR_IF           0x07    /* bus interface controls */
5918 +
5919 +#define  SDIO_BUS_WIDTH_1BIT   0x00
5920 +#define  SDIO_BUS_WIDTH_4BIT   0x02
5921 +
5922 +#define  SDIO_BUS_CD_DISABLE     0x80  /* disable pull-up on DAT3 (pin 1) */
5923 +
5924 +#define SDIO_CCCR_CAPS         0x08
5925 +
5926 +#define  SDIO_CCCR_CAP_SDC     0x01    /* can do CMD52 while data transfer */
5927 +#define  SDIO_CCCR_CAP_SMB     0x02    /* can do multi-block xfers (CMD53) */
5928 +#define  SDIO_CCCR_CAP_SRW     0x04    /* supports read-wait protocol */
5929 +#define  SDIO_CCCR_CAP_SBS     0x08    /* supports suspend/resume */
5930 +#define  SDIO_CCCR_CAP_S4MI    0x10    /* interrupt during 4-bit CMD53 */
5931 +#define  SDIO_CCCR_CAP_E4MI    0x20    /* enable ints during 4-bit CMD53 */
5932 +#define  SDIO_CCCR_CAP_LSC     0x40    /* low speed card */
5933 +#define  SDIO_CCCR_CAP_4BLS    0x80    /* 4 bit low speed card */
5934 +
5935 +#define SDIO_CCCR_CIS          0x09    /* common CIS pointer (3 bytes) */
5936 +
5937 +/* Following 4 regs are valid only if SBS is set */
5938 +#define SDIO_CCCR_SUSPEND      0x0c
5939 +#define SDIO_CCCR_SELx         0x0d
5940 +#define SDIO_CCCR_EXECx                0x0e
5941 +#define SDIO_CCCR_READYx       0x0f
5942 +
5943 +#define SDIO_CCCR_BLKSIZE      0x10
5944 +
5945 +#define SDIO_CCCR_POWER                0x12
5946 +
5947 +#define  SDIO_POWER_SMPC       0x01    /* Supports Master Power Control */
5948 +#define  SDIO_POWER_EMPC       0x02    /* Enable Master Power Control */
5949 +
5950 +#define SDIO_CCCR_SPEED                0x13
5951 +
5952 +#define  SDIO_SPEED_SHS                0x01    /* Supports High-Speed mode */
5953 +#define  SDIO_SPEED_EHS                0x02    /* Enable High-Speed mode */
5954 +
5955 +/*
5956 + * Function Basic Registers (FBR)
5957 + */
5958 +
5959 +#define SDIO_FBR_BASE(f)       ((f) * 0x100) /* base of function f's FBRs */
5960 +
5961 +#define SDIO_FBR_STD_IF                0x00
5962 +
5963 +#define  SDIO_FBR_SUPPORTS_CSA 0x40    /* supports Code Storage Area */
5964 +#define  SDIO_FBR_ENABLE_CSA   0x80    /* enable Code Storage Area */
5965 +
5966 +#define SDIO_FBR_STD_IF_EXT    0x01
5967 +
5968 +#define SDIO_FBR_POWER         0x02
5969 +
5970 +#define  SDIO_FBR_POWER_SPS    0x01    /* Supports Power Selection */
5971 +#define  SDIO_FBR_POWER_EPS    0x02    /* Enable (low) Power Selection */
5972 +
5973 +#define SDIO_FBR_CIS           0x09    /* CIS pointer (3 bytes) */
5974 +
5975 +
5976 +#define SDIO_FBR_CSA           0x0C    /* CSA pointer (3 bytes) */
5977 +
5978 +#define SDIO_FBR_CSA_DATA      0x0F
5979 +
5980 +#define SDIO_FBR_BLKSIZE       0x10    /* block size (2 bytes) */
5981 +
5982 +#endif
5983 +
5984 Index: linux-2.6.23.17/include/linux/mmc/sdio_func.h
5985 ===================================================================
5986 --- /dev/null
5987 +++ linux-2.6.23.17/include/linux/mmc/sdio_func.h
5988 @@ -0,0 +1,153 @@
5989 +/*
5990 + *  include/linux/mmc/sdio_func.h
5991 + *
5992 + *  Copyright 2007 Pierre Ossman
5993 + *
5994 + * This program is free software; you can redistribute it and/or modify
5995 + * it under the terms of the GNU General Public License as published by
5996 + * the Free Software Foundation; either version 2 of the License, or (at
5997 + * your option) any later version.
5998 + */
5999 +
6000 +#ifndef MMC_SDIO_FUNC_H
6001 +#define MMC_SDIO_FUNC_H
6002 +
6003 +#include <linux/device.h>
6004 +#include <linux/mod_devicetable.h>
6005 +
6006 +struct mmc_card;
6007 +struct sdio_func;
6008 +
6009 +typedef void (sdio_irq_handler_t)(struct sdio_func *);
6010 +
6011 +/*
6012 + * SDIO function CIS tuple (unknown to the core)
6013 + */
6014 +struct sdio_func_tuple {
6015 +       struct sdio_func_tuple *next;
6016 +       unsigned char code;
6017 +       unsigned char size;
6018 +       unsigned char data[0];
6019 +};
6020 +
6021 +/*
6022 + * SDIO function devices
6023 + */
6024 +struct sdio_func {
6025 +       struct mmc_card         *card;          /* the card this device belongs to */
6026 +       struct device           dev;            /* the device */
6027 +       sdio_irq_handler_t      *irq_handler;   /* IRQ callback */
6028 +       unsigned int            num;            /* function number */
6029 +
6030 +       unsigned char           class;          /* standard interface class */
6031 +       unsigned short          vendor;         /* vendor id */
6032 +       unsigned short          device;         /* device id */
6033 +
6034 +       unsigned                max_blksize;    /* maximum block size */
6035 +       unsigned                cur_blksize;    /* current block size */
6036 +
6037 +       unsigned int            state;          /* function state */
6038 +#define SDIO_STATE_PRESENT     (1<<0)          /* present in sysfs */
6039 +
6040 +       u8                      tmpbuf[4];      /* DMA:able scratch buffer */
6041 +
6042 +       unsigned                num_info;       /* number of info strings */
6043 +       const char              **info;         /* info strings */
6044 +
6045 +       struct sdio_func_tuple *tuples;
6046 +};
6047 +
6048 +#define sdio_func_present(f)   ((f)->state & SDIO_STATE_PRESENT)
6049 +
6050 +#define sdio_func_set_present(f) ((f)->state |= SDIO_STATE_PRESENT)
6051 +
6052 +#define sdio_func_id(f)                ((f)->dev.bus_id)
6053 +
6054 +#define sdio_get_drvdata(f)    dev_get_drvdata(&(f)->dev)
6055 +#define sdio_set_drvdata(f,d)  dev_set_drvdata(&(f)->dev, d)
6056 +
6057 +/*
6058 + * SDIO function device driver
6059 + */
6060 +struct sdio_driver {
6061 +       char *name;
6062 +       const struct sdio_device_id *id_table;
6063 +
6064 +       int (*probe)(struct sdio_func *, const struct sdio_device_id *);
6065 +       void (*remove)(struct sdio_func *);
6066 +
6067 +       struct device_driver drv;
6068 +};
6069 +
6070 +/**
6071 + * SDIO_DEVICE - macro used to describe a specific SDIO device
6072 + * @vend: the 16 bit manufacturer code
6073 + * @dev: the 16 bit function id
6074 + *
6075 + * This macro is used to create a struct sdio_device_id that matches a
6076 + * specific device. The class field will be set to SDIO_ANY_ID.
6077 + */
6078 +#define SDIO_DEVICE(vend,dev) \
6079 +       .class = SDIO_ANY_ID, \
6080 +       .vendor = (vend), .device = (dev)
6081 +
6082 +/**
6083 + * SDIO_DEVICE_CLASS - macro used to describe a specific SDIO device class
6084 + * @dev_class: the 8 bit standard interface code
6085 + *
6086 + * This macro is used to create a struct sdio_device_id that matches a
6087 + * specific standard SDIO function type.  The vendor and device fields will
6088 + * be set to SDIO_ANY_ID.
6089 + */
6090 +#define SDIO_DEVICE_CLASS(dev_class) \
6091 +       .class = (dev_class), \
6092 +       .vendor = SDIO_ANY_ID, .device = SDIO_ANY_ID
6093 +
6094 +extern int sdio_register_driver(struct sdio_driver *);
6095 +extern void sdio_unregister_driver(struct sdio_driver *);
6096 +
6097 +/*
6098 + * SDIO I/O operations
6099 + */
6100 +extern void sdio_claim_host(struct sdio_func *func);
6101 +extern void sdio_release_host(struct sdio_func *func);
6102 +
6103 +extern int sdio_enable_func(struct sdio_func *func);
6104 +extern int sdio_disable_func(struct sdio_func *func);
6105 +
6106 +extern int sdio_set_block_size(struct sdio_func *func, unsigned blksz);
6107 +
6108 +extern int sdio_claim_irq(struct sdio_func *func, sdio_irq_handler_t *handler);
6109 +extern int sdio_release_irq(struct sdio_func *func);
6110 +
6111 +extern unsigned char sdio_readb(struct sdio_func *func,
6112 +       unsigned int addr, int *err_ret);
6113 +extern unsigned short sdio_readw(struct sdio_func *func,
6114 +       unsigned int addr, int *err_ret);
6115 +extern unsigned long sdio_readl(struct sdio_func *func,
6116 +       unsigned int addr, int *err_ret);
6117 +
6118 +extern int sdio_memcpy_fromio(struct sdio_func *func, void *dst,
6119 +       unsigned int addr, int count);
6120 +extern int sdio_readsb(struct sdio_func *func, void *dst,
6121 +       unsigned int addr, int count);
6122 +
6123 +extern void sdio_writeb(struct sdio_func *func, unsigned char b,
6124 +       unsigned int addr, int *err_ret);
6125 +extern void sdio_writew(struct sdio_func *func, unsigned short b,
6126 +       unsigned int addr, int *err_ret);
6127 +extern void sdio_writel(struct sdio_func *func, unsigned long b,
6128 +       unsigned int addr, int *err_ret);
6129 +
6130 +extern int sdio_memcpy_toio(struct sdio_func *func, unsigned int addr,
6131 +       void *src, int count);
6132 +extern int sdio_writesb(struct sdio_func *func, unsigned int addr,
6133 +       void *src, int count);
6134 +
6135 +extern unsigned char sdio_f0_readb(struct sdio_func *func,
6136 +       unsigned int addr, int *err_ret);
6137 +extern void sdio_f0_writeb(struct sdio_func *func, unsigned char b,
6138 +       unsigned int addr, int *err_ret);
6139 +
6140 +#endif
6141 +
6142 Index: linux-2.6.23.17/include/linux/mmc/sdio_ids.h
6143 ===================================================================
6144 --- /dev/null
6145 +++ linux-2.6.23.17/include/linux/mmc/sdio_ids.h
6146 @@ -0,0 +1,29 @@
6147 +/*
6148 + * SDIO Classes, Interface Types, Manufacturer IDs, etc.
6149 + */
6150 +
6151 +#ifndef MMC_SDIO_IDS_H
6152 +#define MMC_SDIO_IDS_H
6153 +
6154 +/*
6155 + * Standard SDIO Function Interfaces
6156 + */
6157 +
6158 +#define SDIO_CLASS_NONE                0x00    /* Not a SDIO standard interface */
6159 +#define SDIO_CLASS_UART                0x01    /* standard UART interface */
6160 +#define SDIO_CLASS_BT_A                0x02    /* Type-A BlueTooth std interface */
6161 +#define SDIO_CLASS_BT_B                0x03    /* Type-B BlueTooth std interface */
6162 +#define SDIO_CLASS_GPS         0x04    /* GPS standard interface */
6163 +#define SDIO_CLASS_CAMERA      0x05    /* Camera standard interface */
6164 +#define SDIO_CLASS_PHS         0x06    /* PHS standard interface */
6165 +#define SDIO_CLASS_WLAN                0x07    /* WLAN interface */
6166 +#define SDIO_CLASS_ATA         0x08    /* Embedded SDIO-ATA std interface */
6167 +
6168 +/*
6169 + * Vendors and devices.  Sort key: vendor first, device next.
6170 + */
6171 +
6172 +#define SDIO_VENDOR_ID_MARVELL                 0x02df
6173 +#define SDIO_DEVICE_ID_MARVELL_LIBERTAS                0x9103
6174 +
6175 +#endif
6176 Index: linux-2.6.23.17/include/linux/mod_devicetable.h
6177 ===================================================================
6178 --- linux-2.6.23.17.orig/include/linux/mod_devicetable.h
6179 +++ linux-2.6.23.17/include/linux/mod_devicetable.h
6180 @@ -22,6 +22,18 @@ struct pci_device_id {
6181  };
6182  
6183  
6184 +/* SDIO */
6185 +
6186 +#define SDIO_ANY_ID (~0)
6187 +
6188 +struct sdio_device_id {
6189 +       __u8    class;                  /* Standard interface or SDIO_ANY_ID */
6190 +       __u16   vendor;                 /* Vendor or SDIO_ANY_ID */
6191 +       __u16   device;                 /* Device ID or SDIO_ANY_ID */
6192 +       kernel_ulong_t driver_data;     /* Data private to the driver */
6193 +};
6194 +
6195 +
6196  #define IEEE1394_MATCH_VENDOR_ID       0x0001
6197  #define IEEE1394_MATCH_MODEL_ID                0x0002
6198  #define IEEE1394_MATCH_SPECIFIER_ID    0x0004
6199 Index: linux-2.6.23.17/drivers/mmc/card/Kconfig
6200 ===================================================================
6201 --- linux-2.6.23.17.orig/drivers/mmc/card/Kconfig
6202 +++ linux-2.6.23.17/drivers/mmc/card/Kconfig
6203 @@ -32,3 +32,10 @@ config MMC_BLOCK_BOUNCE
6204  
6205           If unsure, say Y here.
6206  
6207 +config SDIO_UART
6208 +       tristate "SDIO UART/GPS class support"
6209 +       depends on MMC
6210 +       help
6211 +         SDIO function driver for SDIO cards that implements the UART
6212 +         class, as well as the GPS class which appears like a UART.
6213 +
6214 Index: linux-2.6.23.17/drivers/mmc/card/Makefile
6215 ===================================================================
6216 --- linux-2.6.23.17.orig/drivers/mmc/card/Makefile
6217 +++ linux-2.6.23.17/drivers/mmc/card/Makefile
6218 @@ -9,3 +9,5 @@ endif
6219  obj-$(CONFIG_MMC_BLOCK)                += mmc_block.o
6220  mmc_block-objs                 := block.o queue.o
6221  
6222 +obj-$(CONFIG_SDIO_UART)                += sdio_uart.o
6223 +
6224 Index: linux-2.6.23.17/drivers/mmc/card/block.c
6225 ===================================================================
6226 --- linux-2.6.23.17.orig/drivers/mmc/card/block.c
6227 +++ linux-2.6.23.17/drivers/mmc/card/block.c
6228 @@ -44,6 +44,9 @@
6229   * max 8 partitions per card
6230   */
6231  #define MMC_SHIFT      3
6232 +#define MMC_NUM_MINORS (256 >> MMC_SHIFT)
6233 +
6234 +static unsigned long dev_use[MMC_NUM_MINORS/(8*sizeof(unsigned long))];
6235  
6236  /*
6237   * There is one mmc_blk_data per slot.
6238 @@ -80,6 +83,9 @@ static void mmc_blk_put(struct mmc_blk_d
6239         mutex_lock(&open_lock);
6240         md->usage--;
6241         if (md->usage == 0) {
6242 +               int devidx = md->disk->first_minor >> MMC_SHIFT;
6243 +               __clear_bit(devidx, dev_use);
6244 +
6245                 put_disk(md->disk);
6246                 kfree(md);
6247         }
6248 @@ -151,17 +157,19 @@ static u32 mmc_sd_num_wr_blocks(struct m
6249  
6250         cmd.opcode = MMC_APP_CMD;
6251         cmd.arg = card->rca << 16;
6252 -       cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
6253 +       cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
6254  
6255         err = mmc_wait_for_cmd(card->host, &cmd, 0);
6256 -       if ((err != MMC_ERR_NONE) || !(cmd.resp[0] & R1_APP_CMD))
6257 +       if (err)
6258 +               return (u32)-1;
6259 +       if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
6260                 return (u32)-1;
6261  
6262         memset(&cmd, 0, sizeof(struct mmc_command));
6263  
6264         cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
6265         cmd.arg = 0;
6266 -       cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
6267 +       cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
6268  
6269         memset(&data, 0, sizeof(struct mmc_data));
6270  
6271 @@ -192,7 +200,7 @@ static u32 mmc_sd_num_wr_blocks(struct m
6272  
6273         mmc_wait_for_req(card->host, &mrq);
6274  
6275 -       if (cmd.error != MMC_ERR_NONE || data.error != MMC_ERR_NONE)
6276 +       if (cmd.error || data.error)
6277                 return (u32)-1;
6278  
6279         blocks = ntohl(blocks);
6280 @@ -220,17 +228,15 @@ static int mmc_blk_issue_rq(struct mmc_q
6281                 brq.cmd.arg = req->sector;
6282                 if (!mmc_card_blockaddr(card))
6283                         brq.cmd.arg <<= 9;
6284 -               brq.cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
6285 +               brq.cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
6286                 brq.data.blksz = 1 << md->block_bits;
6287                 brq.stop.opcode = MMC_STOP_TRANSMISSION;
6288                 brq.stop.arg = 0;
6289 -               brq.stop.flags = MMC_RSP_R1B | MMC_CMD_AC;
6290 +               brq.stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
6291                 brq.data.blocks = req->nr_sectors >> (md->block_bits - 9);
6292                 if (brq.data.blocks > card->host->max_blk_count)
6293                         brq.data.blocks = card->host->max_blk_count;
6294  
6295 -               mmc_set_data_timeout(&brq.data, card, rq_data_dir(req) != READ);
6296 -
6297                 /*
6298                  * If the host doesn't support multiple block writes, force
6299                  * block writes to single block. SD cards are excepted from
6300 @@ -243,8 +249,12 @@ static int mmc_blk_issue_rq(struct mmc_q
6301                         brq.data.blocks = 1;
6302  
6303                 if (brq.data.blocks > 1) {
6304 -                       brq.data.flags |= MMC_DATA_MULTI;
6305 -                       brq.mrq.stop = &brq.stop;
6306 +                       /* SPI multiblock writes terminate using a special
6307 +                        * token, not a STOP_TRANSMISSION request.
6308 +                        */
6309 +                       if (!mmc_host_is_spi(card->host)
6310 +                                       || rq_data_dir(req) == READ)
6311 +                               brq.mrq.stop = &brq.stop;
6312                         readcmd = MMC_READ_MULTIPLE_BLOCK;
6313                         writecmd = MMC_WRITE_MULTIPLE_BLOCK;
6314                 } else {
6315 @@ -261,6 +271,8 @@ static int mmc_blk_issue_rq(struct mmc_q
6316                         brq.data.flags |= MMC_DATA_WRITE;
6317                 }
6318  
6319 +               mmc_set_data_timeout(&brq.data, card);
6320 +
6321                 brq.data.sg = mq->sg;
6322                 brq.data.sg_len = mmc_queue_map_sg(mq);
6323  
6324 @@ -302,7 +314,7 @@ static int mmc_blk_issue_rq(struct mmc_q
6325                         goto cmd_err;
6326                 }
6327  
6328 -               if (rq_data_dir(req) != READ) {
6329 +               if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
6330                         do {
6331                                 int err;
6332  
6333 @@ -315,7 +327,13 @@ static int mmc_blk_issue_rq(struct mmc_q
6334                                                req->rq_disk->disk_name, err);
6335                                         goto cmd_err;
6336                                 }
6337 -                       } while (!(cmd.resp[0] & R1_READY_FOR_DATA));
6338 +                               /*
6339 +                                * Some cards mishandle the status bits,
6340 +                                * so make sure to check both the busy
6341 +                                * indication and the card state.
6342 +                                */
6343 +                       } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
6344 +                               (R1_CURRENT_STATE(cmd.resp[0]) == 7));
6345  
6346  #if 0
6347                         if (cmd.resp[0] & ~0x00000900)
6348 @@ -394,9 +412,6 @@ static int mmc_blk_issue_rq(struct mmc_q
6349         return 0;
6350  }
6351  
6352 -#define MMC_NUM_MINORS (256 >> MMC_SHIFT)
6353 -
6354 -static unsigned long dev_use[MMC_NUM_MINORS/(8*sizeof(unsigned long))];
6355  
6356  static inline int mmc_blk_readonly(struct mmc_card *card)
6357  {
6358 @@ -510,7 +525,7 @@ mmc_blk_set_blksize(struct mmc_blk_data 
6359         mmc_claim_host(card->host);
6360         cmd.opcode = MMC_SET_BLOCKLEN;
6361         cmd.arg = 1 << md->block_bits;
6362 -       cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
6363 +       cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
6364         err = mmc_wait_for_cmd(card->host, &cmd, 5);
6365         mmc_release_host(card->host);
6366  
6367 @@ -562,17 +577,12 @@ static void mmc_blk_remove(struct mmc_ca
6368         struct mmc_blk_data *md = mmc_get_drvdata(card);
6369  
6370         if (md) {
6371 -               int devidx;
6372 -
6373                 /* Stop new requests from getting into the queue */
6374                 del_gendisk(md->disk);
6375  
6376                 /* Then flush out any already in there */
6377                 mmc_cleanup_queue(&md->queue);
6378  
6379 -               devidx = md->disk->first_minor >> MMC_SHIFT;
6380 -               __clear_bit(devidx, dev_use);
6381 -
6382                 mmc_blk_put(md);
6383         }
6384         mmc_set_drvdata(card, NULL);
6385 Index: linux-2.6.23.17/drivers/mmc/card/queue.c
6386 ===================================================================
6387 --- linux-2.6.23.17.orig/drivers/mmc/card/queue.c
6388 +++ linux-2.6.23.17/drivers/mmc/card/queue.c
6389 @@ -13,6 +13,7 @@
6390  #include <linux/blkdev.h>
6391  #include <linux/freezer.h>
6392  #include <linux/kthread.h>
6393 +#include <linux/scatterlist.h>
6394  
6395  #include <linux/mmc/card.h>
6396  #include <linux/mmc/host.h>
6397 @@ -22,6 +23,12 @@
6398  
6399  #define MMC_QUEUE_SUSPENDED    (1 << 0)
6400  
6401 +#define sg_init_table(sg, n) do { \
6402 +       memset(sg, 0, sizeof(*(sg)) * (n)); \
6403 +} while (0)
6404 +
6405 +#define sg_virt(sg)    (page_address((sg)->page) + (sg)->offset)
6406 +
6407  /*
6408   * Prepare a MMC request. This just filters out odd stuff.
6409   */
6410 @@ -159,6 +166,7 @@ int mmc_init_queue(struct mmc_queue *mq,
6411                                 ret = -ENOMEM;
6412                                 goto cleanup_queue;
6413                         }
6414 +                       sg_init_table(mq->sg, 1);
6415  
6416                         mq->bounce_sg = kmalloc(sizeof(struct scatterlist) *
6417                                 bouncesz / 512, GFP_KERNEL);
6418 @@ -166,6 +174,7 @@ int mmc_init_queue(struct mmc_queue *mq,
6419                                 ret = -ENOMEM;
6420                                 goto cleanup_queue;
6421                         }
6422 +                       sg_init_table(mq->bounce_sg, bouncesz / 512);
6423                 }
6424         }
6425  #endif
6426 @@ -183,6 +192,7 @@ int mmc_init_queue(struct mmc_queue *mq,
6427                         ret = -ENOMEM;
6428                         goto cleanup_queue;
6429                 }
6430 +               sg_init_table(mq->sg, host->max_phys_segs);
6431         }
6432  
6433         init_MUTEX(&mq->thread_sem);
6434 @@ -302,12 +312,12 @@ static void copy_sg(struct scatterlist *
6435                 BUG_ON(dst_len == 0);
6436  
6437                 if (dst_size == 0) {
6438 -                       dst_buf = page_address(dst->page) + dst->offset;
6439 +                       dst_buf = sg_virt(dst);
6440                         dst_size = dst->length;
6441                 }
6442  
6443                 if (src_size == 0) {
6444 -                       src_buf = page_address(src->page) + src->offset;
6445 +                       src_buf = sg_virt(src);
6446                         src_size = src->length;
6447                 }
6448  
6449 @@ -353,9 +363,7 @@ unsigned int mmc_queue_map_sg(struct mmc
6450                 return 1;
6451         }
6452  
6453 -       mq->sg[0].page = virt_to_page(mq->bounce_buf);
6454 -       mq->sg[0].offset = offset_in_page(mq->bounce_buf);
6455 -       mq->sg[0].length = 0;
6456 +       sg_init_one(mq->sg, mq->bounce_buf, 0);
6457  
6458         while (sg_len) {
6459                 mq->sg[0].length += mq->bounce_sg[sg_len - 1].length;
6460 Index: linux-2.6.23.17/drivers/mmc/card/sdio_uart.c
6461 ===================================================================
6462 --- /dev/null
6463 +++ linux-2.6.23.17/drivers/mmc/card/sdio_uart.c
6464 @@ -0,0 +1,1158 @@
6465 +/*
6466 + * linux/drivers/mmc/card/sdio_uart.c - SDIO UART/GPS driver
6467 + *
6468 + * Based on drivers/serial/8250.c and drivers/serial/serial_core.c
6469 + * by Russell King.
6470 + *
6471 + * Author:     Nicolas Pitre
6472 + * Created:    June 15, 2007
6473 + * Copyright:  MontaVista Software, Inc.
6474 + *
6475 + * This program is free software; you can redistribute it and/or modify
6476 + * it under the terms of the GNU General Public License as published by
6477 + * the Free Software Foundation; either version 2 of the License, or (at
6478 + * your option) any later version.
6479 + */
6480 +
6481 +/*
6482 + * Note: Although this driver assumes a 16550A-like UART implementation,
6483 + * it is not possible to leverage the common 8250/16550 driver, nor the
6484 + * core UART infrastructure, as they assumes direct access to the hardware
6485 + * registers, often under a spinlock.  This is not possible in the SDIO
6486 + * context as SDIO access functions must be able to sleep.
6487 + *
6488 + * Because we need to lock the SDIO host to ensure an exclusive access to
6489 + * the card, we simply rely on that lock to also prevent and serialize
6490 + * concurrent access to the same port.
6491 + */
6492 +
6493 +#include <linux/module.h>
6494 +#include <linux/init.h>
6495 +#include <linux/kernel.h>
6496 +#include <linux/mutex.h>
6497 +#include <linux/serial_reg.h>
6498 +#include <linux/circ_buf.h>
6499 +#include <linux/gfp.h>
6500 +#include <linux/tty.h>
6501 +#include <linux/tty_flip.h>
6502 +
6503 +#include <linux/mmc/core.h>
6504 +#include <linux/mmc/card.h>
6505 +#include <linux/mmc/sdio_func.h>
6506 +#include <linux/mmc/sdio_ids.h>
6507 +
6508 +
6509 +#define UART_NR                8       /* Number of UARTs this driver can handle */
6510 +
6511 +
6512 +#define UART_XMIT_SIZE PAGE_SIZE
6513 +#define WAKEUP_CHARS   256
6514 +
6515 +#define circ_empty(circ)       ((circ)->head == (circ)->tail)
6516 +#define circ_clear(circ)       ((circ)->head = (circ)->tail = 0)
6517 +
6518 +#define circ_chars_pending(circ) \
6519 +               (CIRC_CNT((circ)->head, (circ)->tail, UART_XMIT_SIZE))
6520 +
6521 +#define circ_chars_free(circ) \
6522 +               (CIRC_SPACE((circ)->head, (circ)->tail, UART_XMIT_SIZE))
6523 +
6524 +
6525 +struct uart_icount {
6526 +       __u32   cts;
6527 +       __u32   dsr;
6528 +       __u32   rng;
6529 +       __u32   dcd;
6530 +       __u32   rx;
6531 +       __u32   tx;
6532 +       __u32   frame;
6533 +       __u32   overrun;
6534 +       __u32   parity;
6535 +       __u32   brk;
6536 +};
6537 +
6538 +struct sdio_uart_port {
6539 +       struct kref             kref;
6540 +       struct tty_struct       *tty;
6541 +       unsigned int            index;
6542 +       unsigned int            opened;
6543 +       struct mutex            open_lock;
6544 +       struct sdio_func        *func;
6545 +       struct mutex            func_lock;
6546 +       struct task_struct      *in_sdio_uart_irq;
6547 +       unsigned int            regs_offset;
6548 +       struct circ_buf         xmit;
6549 +       spinlock_t              write_lock;
6550 +       struct uart_icount      icount;
6551 +       unsigned int            uartclk;
6552 +       unsigned int            mctrl;
6553 +       unsigned int            read_status_mask;
6554 +       unsigned int            ignore_status_mask;
6555 +       unsigned char           x_char;
6556 +       unsigned char           ier;
6557 +       unsigned char           lcr;
6558 +};
6559 +
6560 +static struct sdio_uart_port *sdio_uart_table[UART_NR];
6561 +static DEFINE_SPINLOCK(sdio_uart_table_lock);
6562 +
6563 +static int sdio_uart_add_port(struct sdio_uart_port *port)
6564 +{
6565 +       int index, ret = -EBUSY;
6566 +
6567 +       kref_init(&port->kref);
6568 +       mutex_init(&port->open_lock);
6569 +       mutex_init(&port->func_lock);
6570 +       spin_lock_init(&port->write_lock);
6571 +
6572 +       spin_lock(&sdio_uart_table_lock);
6573 +       for (index = 0; index < UART_NR; index++) {
6574 +               if (!sdio_uart_table[index]) {
6575 +                       port->index = index;
6576 +                       sdio_uart_table[index] = port;
6577 +                       ret = 0;
6578 +                       break;
6579 +               }
6580 +       }
6581 +       spin_unlock(&sdio_uart_table_lock);
6582 +
6583 +       return ret;
6584 +}
6585 +
6586 +static struct sdio_uart_port *sdio_uart_port_get(unsigned index)
6587 +{
6588 +       struct sdio_uart_port *port;
6589 +
6590 +       if (index >= UART_NR)
6591 +               return NULL;
6592 +
6593 +       spin_lock(&sdio_uart_table_lock);
6594 +       port = sdio_uart_table[index];
6595 +       if (port)
6596 +               kref_get(&port->kref);
6597 +       spin_unlock(&sdio_uart_table_lock);
6598 +
6599 +       return port;
6600 +}
6601 +
6602 +static void sdio_uart_port_destroy(struct kref *kref)
6603 +{
6604 +       struct sdio_uart_port *port =
6605 +               container_of(kref, struct sdio_uart_port, kref);
6606 +       kfree(port);
6607 +}
6608 +
6609 +static void sdio_uart_port_put(struct sdio_uart_port *port)
6610 +{
6611 +       kref_put(&port->kref, sdio_uart_port_destroy);
6612 +}
6613 +
6614 +static void sdio_uart_port_remove(struct sdio_uart_port *port)
6615 +{
6616 +       struct sdio_func *func;
6617 +
6618 +       BUG_ON(sdio_uart_table[port->index] != port);
6619 +
6620 +       spin_lock(&sdio_uart_table_lock);
6621 +       sdio_uart_table[port->index] = NULL;
6622 +       spin_unlock(&sdio_uart_table_lock);
6623 +
6624 +       /*
6625 +        * We're killing a port that potentially still is in use by
6626 +        * the tty layer. Be careful to prevent any further access
6627 +        * to the SDIO function and arrange for the tty layer to
6628 +        * give up on that port ASAP.
6629 +        * Beware: the lock ordering is critical.
6630 +        */
6631 +       mutex_lock(&port->open_lock);
6632 +       mutex_lock(&port->func_lock);
6633 +       func = port->func;
6634 +       sdio_claim_host(func);
6635 +       port->func = NULL;
6636 +       mutex_unlock(&port->func_lock);
6637 +       if (port->opened)
6638 +               tty_hangup(port->tty);
6639 +       mutex_unlock(&port->open_lock);
6640 +       sdio_release_irq(func);
6641 +       sdio_disable_func(func);
6642 +       sdio_release_host(func);
6643 +
6644 +       sdio_uart_port_put(port);
6645 +}
6646 +
6647 +static int sdio_uart_claim_func(struct sdio_uart_port *port)
6648 +{
6649 +       mutex_lock(&port->func_lock);
6650 +       if (unlikely(!port->func)) {
6651 +               mutex_unlock(&port->func_lock);
6652 +               return -ENODEV;
6653 +       }
6654 +       if (likely(port->in_sdio_uart_irq != current))
6655 +               sdio_claim_host(port->func);
6656 +       mutex_unlock(&port->func_lock);
6657 +       return 0;
6658 +}
6659 +
6660 +static inline void sdio_uart_release_func(struct sdio_uart_port *port)
6661 +{
6662 +       if (likely(port->in_sdio_uart_irq != current))
6663 +               sdio_release_host(port->func);
6664 +}
6665 +
6666 +static inline unsigned int sdio_in(struct sdio_uart_port *port, int offset)
6667 +{
6668 +       unsigned char c;
6669 +       c = sdio_readb(port->func, port->regs_offset + offset, NULL);
6670 +       return c;
6671 +}
6672 +
6673 +static inline void sdio_out(struct sdio_uart_port *port, int offset, int value)
6674 +{
6675 +       sdio_writeb(port->func, value, port->regs_offset + offset, NULL);
6676 +}
6677 +
6678 +static unsigned int sdio_uart_get_mctrl(struct sdio_uart_port *port)
6679 +{
6680 +       unsigned char status;
6681 +       unsigned int ret;
6682 +
6683 +       status = sdio_in(port, UART_MSR);
6684 +
6685 +       ret = 0;
6686 +       if (status & UART_MSR_DCD)
6687 +               ret |= TIOCM_CAR;
6688 +       if (status & UART_MSR_RI)
6689 +               ret |= TIOCM_RNG;
6690 +       if (status & UART_MSR_DSR)
6691 +               ret |= TIOCM_DSR;
6692 +       if (status & UART_MSR_CTS)
6693 +               ret |= TIOCM_CTS;
6694 +       return ret;
6695 +}
6696 +
6697 +static void sdio_uart_write_mctrl(struct sdio_uart_port *port, unsigned int mctrl)
6698 +{
6699 +       unsigned char mcr = 0;
6700 +
6701 +       if (mctrl & TIOCM_RTS)
6702 +               mcr |= UART_MCR_RTS;
6703 +       if (mctrl & TIOCM_DTR)
6704 +               mcr |= UART_MCR_DTR;
6705 +       if (mctrl & TIOCM_OUT1)
6706 +               mcr |= UART_MCR_OUT1;
6707 +       if (mctrl & TIOCM_OUT2)
6708 +               mcr |= UART_MCR_OUT2;
6709 +       if (mctrl & TIOCM_LOOP)
6710 +               mcr |= UART_MCR_LOOP;
6711 +
6712 +       sdio_out(port, UART_MCR, mcr);
6713 +}
6714 +
6715 +static inline void sdio_uart_update_mctrl(struct sdio_uart_port *port,
6716 +                                         unsigned int set, unsigned int clear)
6717 +{
6718 +       unsigned int old;
6719 +
6720 +       old = port->mctrl;
6721 +       port->mctrl = (old & ~clear) | set;
6722 +       if (old != port->mctrl)
6723 +               sdio_uart_write_mctrl(port, port->mctrl);
6724 +}
6725 +
6726 +#define sdio_uart_set_mctrl(port, x)   sdio_uart_update_mctrl(port, x, 0)
6727 +#define sdio_uart_clear_mctrl(port, x) sdio_uart_update_mctrl(port, 0, x)
6728 +
6729 +static void sdio_uart_change_speed(struct sdio_uart_port *port,
6730 +                                  struct ktermios *termios,
6731 +                                  struct ktermios *old)
6732 +{
6733 +       unsigned char cval, fcr = 0;
6734 +       unsigned int baud, quot;
6735 +
6736 +       switch (termios->c_cflag & CSIZE) {
6737 +       case CS5:
6738 +               cval = UART_LCR_WLEN5;
6739 +               break;
6740 +       case CS6:
6741 +               cval = UART_LCR_WLEN6;
6742 +               break;
6743 +       case CS7:
6744 +               cval = UART_LCR_WLEN7;
6745 +               break;
6746 +       default:
6747 +       case CS8:
6748 +               cval = UART_LCR_WLEN8;
6749 +               break;
6750 +       }
6751 +
6752 +       if (termios->c_cflag & CSTOPB)
6753 +               cval |= UART_LCR_STOP;
6754 +       if (termios->c_cflag & PARENB)
6755 +               cval |= UART_LCR_PARITY;
6756 +       if (!(termios->c_cflag & PARODD))
6757 +               cval |= UART_LCR_EPAR;
6758 +
6759 +       for (;;) {
6760 +               baud = tty_termios_baud_rate(termios);
6761 +               if (baud == 0)
6762 +                       baud = 9600;  /* Special case: B0 rate. */
6763 +               if (baud <= port->uartclk)
6764 +                       break;
6765 +               /*
6766 +                * Oops, the quotient was zero.  Try again with the old
6767 +                * baud rate if possible, otherwise default to 9600.
6768 +                */
6769 +               termios->c_cflag &= ~CBAUD;
6770 +               if (old) {
6771 +                       termios->c_cflag |= old->c_cflag & CBAUD;
6772 +                       old = NULL;
6773 +               } else
6774 +                       termios->c_cflag |= B9600;
6775 +       }
6776 +       quot = (2 * port->uartclk + baud) / (2 * baud);
6777 +
6778 +       if (baud < 2400)
6779 +               fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
6780 +       else
6781 +               fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10;
6782 +
6783 +       port->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
6784 +       if (termios->c_iflag & INPCK)
6785 +               port->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
6786 +       if (termios->c_iflag & (BRKINT | PARMRK))
6787 +               port->read_status_mask |= UART_LSR_BI;
6788 +
6789 +       /*
6790 +        * Characters to ignore
6791 +        */
6792 +       port->ignore_status_mask = 0;
6793 +       if (termios->c_iflag & IGNPAR)
6794 +               port->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
6795 +       if (termios->c_iflag & IGNBRK) {
6796 +               port->ignore_status_mask |= UART_LSR_BI;
6797 +               /*
6798 +                * If we're ignoring parity and break indicators,
6799 +                * ignore overruns too (for real raw support).
6800 +                */
6801 +               if (termios->c_iflag & IGNPAR)
6802 +                       port->ignore_status_mask |= UART_LSR_OE;
6803 +       }
6804 +
6805 +       /*
6806 +        * ignore all characters if CREAD is not set
6807 +        */
6808 +       if ((termios->c_cflag & CREAD) == 0)
6809 +               port->ignore_status_mask |= UART_LSR_DR;
6810 +
6811 +       /*
6812 +        * CTS flow control flag and modem status interrupts
6813 +        */
6814 +       port->ier &= ~UART_IER_MSI;
6815 +       if ((termios->c_cflag & CRTSCTS) || !(termios->c_cflag & CLOCAL))
6816 +               port->ier |= UART_IER_MSI;
6817 +
6818 +       port->lcr = cval;
6819 +
6820 +       sdio_out(port, UART_IER, port->ier);
6821 +       sdio_out(port, UART_LCR, cval | UART_LCR_DLAB);
6822 +       sdio_out(port, UART_DLL, quot & 0xff);
6823 +       sdio_out(port, UART_DLM, quot >> 8);
6824 +       sdio_out(port, UART_LCR, cval);
6825 +       sdio_out(port, UART_FCR, fcr);
6826 +
6827 +       sdio_uart_write_mctrl(port, port->mctrl);
6828 +}
6829 +
6830 +static void sdio_uart_start_tx(struct sdio_uart_port *port)
6831 +{
6832 +       if (!(port->ier & UART_IER_THRI)) {
6833 +               port->ier |= UART_IER_THRI;
6834 +               sdio_out(port, UART_IER, port->ier);
6835 +       }
6836 +}
6837 +
6838 +static void sdio_uart_stop_tx(struct sdio_uart_port *port)
6839 +{
6840 +       if (port->ier & UART_IER_THRI) {
6841 +               port->ier &= ~UART_IER_THRI;
6842 +               sdio_out(port, UART_IER, port->ier);
6843 +       }
6844 +}
6845 +
6846 +static void sdio_uart_stop_rx(struct sdio_uart_port *port)
6847 +{
6848 +       port->ier &= ~UART_IER_RLSI;
6849 +       port->read_status_mask &= ~UART_LSR_DR;
6850 +       sdio_out(port, UART_IER, port->ier);
6851 +}
6852 +
6853 +static void sdio_uart_receive_chars(struct sdio_uart_port *port, unsigned int *status)
6854 +{
6855 +       struct tty_struct *tty = port->tty;
6856 +       unsigned int ch, flag;
6857 +       int max_count = 256;
6858 +
6859 +       do {
6860 +               ch = sdio_in(port, UART_RX);
6861 +               flag = TTY_NORMAL;
6862 +               port->icount.rx++;
6863 +
6864 +               if (unlikely(*status & (UART_LSR_BI | UART_LSR_PE |
6865 +                                       UART_LSR_FE | UART_LSR_OE))) {
6866 +                       /*
6867 +                        * For statistics only
6868 +                        */
6869 +                       if (*status & UART_LSR_BI) {
6870 +                               *status &= ~(UART_LSR_FE | UART_LSR_PE);
6871 +                               port->icount.brk++;
6872 +                       } else if (*status & UART_LSR_PE)
6873 +                               port->icount.parity++;
6874 +                       else if (*status & UART_LSR_FE)
6875 +                               port->icount.frame++;
6876 +                       if (*status & UART_LSR_OE)
6877 +                               port->icount.overrun++;
6878 +
6879 +                       /*
6880 +                        * Mask off conditions which should be ignored.
6881 +                        */
6882 +                       *status &= port->read_status_mask;
6883 +                       if (*status & UART_LSR_BI) {
6884 +                               flag = TTY_BREAK;
6885 +                       } else if (*status & UART_LSR_PE)
6886 +                               flag = TTY_PARITY;
6887 +                       else if (*status & UART_LSR_FE)
6888 +                               flag = TTY_FRAME;
6889 +               }
6890 +
6891 +               if ((*status & port->ignore_status_mask & ~UART_LSR_OE) == 0)
6892 +                       tty_insert_flip_char(tty, ch, flag);
6893 +
6894 +               /*
6895 +                * Overrun is special.  Since it's reported immediately,
6896 +                * it doesn't affect the current character.
6897 +                */
6898 +               if (*status & ~port->ignore_status_mask & UART_LSR_OE)
6899 +                       tty_insert_flip_char(tty, 0, TTY_OVERRUN);
6900 +
6901 +               *status = sdio_in(port, UART_LSR);
6902 +       } while ((*status & UART_LSR_DR) && (max_count-- > 0));
6903 +       tty_flip_buffer_push(tty);
6904 +}
6905 +
6906 +static void sdio_uart_transmit_chars(struct sdio_uart_port *port)
6907 +{
6908 +       struct circ_buf *xmit = &port->xmit;
6909 +       int count;
6910 +
6911 +       if (port->x_char) {
6912 +               sdio_out(port, UART_TX, port->x_char);
6913 +               port->icount.tx++;
6914 +               port->x_char = 0;
6915 +               return;
6916 +       }
6917 +       if (circ_empty(xmit) || port->tty->stopped || port->tty->hw_stopped) {
6918 +               sdio_uart_stop_tx(port);
6919 +               return;
6920 +       }
6921 +
6922 +       count = 16;
6923 +       do {
6924 +               sdio_out(port, UART_TX, xmit->buf[xmit->tail]);
6925 +               xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
6926 +               port->icount.tx++;
6927 +               if (circ_empty(xmit))
6928 +                       break;
6929 +       } while (--count > 0);
6930 +
6931 +       if (circ_chars_pending(xmit) < WAKEUP_CHARS)
6932 +               tty_wakeup(port->tty);
6933 +
6934 +       if (circ_empty(xmit))
6935 +               sdio_uart_stop_tx(port);
6936 +}
6937 +
6938 +static void sdio_uart_check_modem_status(struct sdio_uart_port *port)
6939 +{
6940 +       int status;
6941 +
6942 +       status = sdio_in(port, UART_MSR);
6943 +
6944 +       if ((status & UART_MSR_ANY_DELTA) == 0)
6945 +               return;
6946 +
6947 +       if (status & UART_MSR_TERI)
6948 +               port->icount.rng++;
6949 +       if (status & UART_MSR_DDSR)
6950 +               port->icount.dsr++;
6951 +       if (status & UART_MSR_DDCD)
6952 +               port->icount.dcd++;
6953 +       if (status & UART_MSR_DCTS) {
6954 +               port->icount.cts++;
6955 +               if (port->tty->termios->c_cflag & CRTSCTS) {
6956 +                       int cts = (status & UART_MSR_CTS);
6957 +                       if (port->tty->hw_stopped) {
6958 +                               if (cts) {
6959 +                                       port->tty->hw_stopped = 0;
6960 +                                       sdio_uart_start_tx(port);
6961 +                                       tty_wakeup(port->tty);
6962 +                               }
6963 +                       } else {
6964 +                               if (!cts) {
6965 +                                       port->tty->hw_stopped = 1;
6966 +                                       sdio_uart_stop_tx(port);
6967 +                               }
6968 +                       }
6969 +               }
6970 +       }
6971 +}
6972 +
6973 +/*
6974 + * This handles the interrupt from one port.
6975 + */
6976 +static void sdio_uart_irq(struct sdio_func *func)
6977 +{
6978 +       struct sdio_uart_port *port = sdio_get_drvdata(func);
6979 +       unsigned int iir, lsr;
6980 +
6981 +       /*
6982 +        * In a few places sdio_uart_irq() is called directly instead of
6983 +        * waiting for the actual interrupt to be raised and the SDIO IRQ
6984 +        * thread scheduled in order to reduce latency.  However, some
6985 +        * interaction with the tty core may end up calling us back
6986 +        * (serial echo, flow control, etc.) through those same places
6987 +        * causing undesirable effects.  Let's stop the recursion here.
6988 +        */
6989 +       if (unlikely(port->in_sdio_uart_irq == current))
6990 +               return;
6991 +
6992 +       iir = sdio_in(port, UART_IIR);
6993 +       if (iir & UART_IIR_NO_INT)
6994 +               return;
6995 +
6996 +       port->in_sdio_uart_irq = current;
6997 +       lsr = sdio_in(port, UART_LSR);
6998 +       if (lsr & UART_LSR_DR)
6999 +               sdio_uart_receive_chars(port, &lsr);
7000 +       sdio_uart_check_modem_status(port);
7001 +       if (lsr & UART_LSR_THRE)
7002 +               sdio_uart_transmit_chars(port);
7003 +       port->in_sdio_uart_irq = NULL;
7004 +}
7005 +
7006 +static int sdio_uart_startup(struct sdio_uart_port *port)
7007 +{
7008 +       unsigned long page;
7009 +       int ret;
7010 +
7011 +       /*
7012 +        * Set the TTY IO error marker - we will only clear this
7013 +        * once we have successfully opened the port.
7014 +        */
7015 +       set_bit(TTY_IO_ERROR, &port->tty->flags);
7016 +
7017 +       /* Initialise and allocate the transmit buffer. */
7018 +       page = __get_free_page(GFP_KERNEL);
7019 +       if (!page)
7020 +               return -ENOMEM;
7021 +       port->xmit.buf = (unsigned char *)page;
7022 +       circ_clear(&port->xmit);
7023 +
7024 +       ret = sdio_uart_claim_func(port);
7025 +       if (ret)
7026 +               goto err1;
7027 +       ret = sdio_enable_func(port->func);
7028 +       if (ret)
7029 +               goto err2;
7030 +       ret = sdio_claim_irq(port->func, sdio_uart_irq);
7031 +       if (ret)
7032 +               goto err3;
7033 +
7034 +       /*
7035 +        * Clear the FIFO buffers and disable them.
7036 +        * (they will be reenabled in sdio_change_speed())
7037 +        */
7038 +       sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO);
7039 +       sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO |
7040 +                       UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
7041 +       sdio_out(port, UART_FCR, 0);
7042 +
7043 +       /*
7044 +        * Clear the interrupt registers.
7045 +        */
7046 +       (void) sdio_in(port, UART_LSR);
7047 +       (void) sdio_in(port, UART_RX);
7048 +       (void) sdio_in(port, UART_IIR);
7049 +       (void) sdio_in(port, UART_MSR);
7050 +
7051 +       /*
7052 +        * Now, initialize the UART
7053 +        */
7054 +       sdio_out(port, UART_LCR, UART_LCR_WLEN8);
7055 +
7056 +       port->ier = UART_IER_RLSI | UART_IER_RDI | UART_IER_RTOIE | UART_IER_UUE;
7057 +       port->mctrl = TIOCM_OUT2;
7058 +
7059 +       sdio_uart_change_speed(port, port->tty->termios, NULL);
7060 +
7061 +       if (port->tty->termios->c_cflag & CBAUD)
7062 +               sdio_uart_set_mctrl(port, TIOCM_RTS | TIOCM_DTR);
7063 +
7064 +       if (port->tty->termios->c_cflag & CRTSCTS)
7065 +               if (!(sdio_uart_get_mctrl(port) & TIOCM_CTS))
7066 +                       port->tty->hw_stopped = 1;
7067 +
7068 +       clear_bit(TTY_IO_ERROR, &port->tty->flags);
7069 +
7070 +       /* Kick the IRQ handler once while we're still holding the host lock */
7071 +       sdio_uart_irq(port->func);
7072 +
7073 +       sdio_uart_release_func(port);
7074 +       return 0;
7075 +
7076 +err3:
7077 +       sdio_disable_func(port->func);
7078 +err2:
7079 +       sdio_uart_release_func(port);
7080 +err1:
7081 +       free_page((unsigned long)port->xmit.buf);
7082 +       return ret;
7083 +}
7084 +
7085 +static void sdio_uart_shutdown(struct sdio_uart_port *port)
7086 +{
7087 +       int ret;
7088 +
7089 +       ret = sdio_uart_claim_func(port);
7090 +       if (ret)
7091 +               goto skip;
7092 +
7093 +       sdio_uart_stop_rx(port);
7094 +
7095 +       /* TODO: wait here for TX FIFO to drain */
7096 +
7097 +       /* Turn off DTR and RTS early. */
7098 +       if (port->tty->termios->c_cflag & HUPCL)
7099 +               sdio_uart_clear_mctrl(port, TIOCM_DTR | TIOCM_RTS);
7100 +
7101 +        /* Disable interrupts from this port */
7102 +       sdio_release_irq(port->func);
7103 +       port->ier = 0;
7104 +       sdio_out(port, UART_IER, 0);
7105 +
7106 +       sdio_uart_clear_mctrl(port, TIOCM_OUT2);
7107 +
7108 +       /* Disable break condition and FIFOs. */
7109 +       port->lcr &= ~UART_LCR_SBC;
7110 +       sdio_out(port, UART_LCR, port->lcr);
7111 +       sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO |
7112 +                                UART_FCR_CLEAR_RCVR |
7113 +                                UART_FCR_CLEAR_XMIT);
7114 +       sdio_out(port, UART_FCR, 0);
7115 +
7116 +       sdio_disable_func(port->func);
7117 +
7118 +       sdio_uart_release_func(port);
7119 +
7120 +skip:
7121 +       /* Free the transmit buffer page. */
7122 +       free_page((unsigned long)port->xmit.buf);
7123 +}
7124 +
7125 +static int sdio_uart_open (struct tty_struct *tty, struct file * filp)
7126 +{
7127 +       struct sdio_uart_port *port;
7128 +       int ret;
7129 +
7130 +       port = sdio_uart_port_get(tty->index);
7131 +       if (!port)
7132 +               return -ENODEV;
7133 +
7134 +       mutex_lock(&port->open_lock);
7135 +
7136 +       /*
7137 +        * Make sure not to mess up with a dead port
7138 +        * which has not been closed yet.
7139 +        */
7140 +       if (tty->driver_data && tty->driver_data != port) {
7141 +               mutex_unlock(&port->open_lock);
7142 +               sdio_uart_port_put(port);
7143 +               return -EBUSY;
7144 +       }
7145 +
7146 +       if (!port->opened) {
7147 +               tty->driver_data = port;
7148 +               port->tty = tty;
7149 +               ret = sdio_uart_startup(port);
7150 +               if (ret) {
7151 +                       tty->driver_data = NULL;
7152 +                       port->tty = NULL;
7153 +                       mutex_unlock(&port->open_lock);
7154 +                       sdio_uart_port_put(port);
7155 +                       return ret;
7156 +               }
7157 +       }
7158 +       port->opened++;
7159 +       mutex_unlock(&port->open_lock);
7160 +       return 0;
7161 +}
7162 +
7163 +static void sdio_uart_close(struct tty_struct *tty, struct file * filp)
7164 +{
7165 +       struct sdio_uart_port *port = tty->driver_data;
7166 +
7167 +       if (!port)
7168 +               return;
7169 +
7170 +       mutex_lock(&port->open_lock);
7171 +       BUG_ON(!port->opened);
7172 +
7173 +       /*
7174 +        * This is messy.  The tty layer calls us even when open()
7175 +        * returned an error.  Ignore this close request if tty->count
7176 +        * is larger than port->count.
7177 +        */
7178 +       if (tty->count > port->opened) {
7179 +               mutex_unlock(&port->open_lock);
7180 +               return;
7181 +       }
7182 +
7183 +       if (--port->opened == 0) {
7184 +               tty->closing = 1;
7185 +               sdio_uart_shutdown(port);
7186 +               tty_ldisc_flush(tty);
7187 +               port->tty = NULL;
7188 +               tty->driver_data = NULL;
7189 +               tty->closing = 0;
7190 +       }
7191 +       mutex_unlock(&port->open_lock);
7192 +       sdio_uart_port_put(port);
7193 +}
7194 +
7195 +static int sdio_uart_write(struct tty_struct * tty, const unsigned char *buf,
7196 +                          int count)
7197 +{
7198 +       struct sdio_uart_port *port = tty->driver_data;
7199 +       struct circ_buf *circ = &port->xmit;
7200 +       int c, ret = 0;
7201 +
7202 +       if (!port->func)
7203 +               return -ENODEV;
7204 +
7205 +       spin_lock(&port->write_lock);
7206 +       while (1) {
7207 +               c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
7208 +               if (count < c)
7209 +                       c = count;
7210 +               if (c <= 0)
7211 +                       break;
7212 +               memcpy(circ->buf + circ->head, buf, c);
7213 +               circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
7214 +               buf += c;
7215 +               count -= c;
7216 +               ret += c;
7217 +       }
7218 +       spin_unlock(&port->write_lock);
7219 +
7220 +       if ( !(port->ier & UART_IER_THRI)) {
7221 +               int err = sdio_uart_claim_func(port);
7222 +               if (!err) {
7223 +                       sdio_uart_start_tx(port);
7224 +                       sdio_uart_irq(port->func);
7225 +                       sdio_uart_release_func(port);
7226 +               } else
7227 +                       ret = err;
7228 +       }
7229 +
7230 +       return ret;
7231 +}
7232 +
7233 +static int sdio_uart_write_room(struct tty_struct *tty)
7234 +{
7235 +       struct sdio_uart_port *port = tty->driver_data;
7236 +       return port ? circ_chars_free(&port->xmit) : 0;
7237 +}
7238 +
7239 +static int sdio_uart_chars_in_buffer(struct tty_struct *tty)
7240 +{
7241 +       struct sdio_uart_port *port = tty->driver_data;
7242 +       return port ? circ_chars_pending(&port->xmit) : 0;
7243 +}
7244 +
7245 +static void sdio_uart_send_xchar(struct tty_struct *tty, char ch)
7246 +{
7247 +       struct sdio_uart_port *port = tty->driver_data;
7248 +
7249 +       port->x_char = ch;
7250 +       if (ch && !(port->ier & UART_IER_THRI)) {
7251 +               if (sdio_uart_claim_func(port) != 0)
7252 +                       return;
7253 +               sdio_uart_start_tx(port);
7254 +               sdio_uart_irq(port->func);
7255 +               sdio_uart_release_func(port);
7256 +       }
7257 +}
7258 +
7259 +static void sdio_uart_throttle(struct tty_struct *tty)
7260 +{
7261 +       struct sdio_uart_port *port = tty->driver_data;
7262 +
7263 +       if (!I_IXOFF(tty) && !(tty->termios->c_cflag & CRTSCTS))
7264 +               return;
7265 +
7266 +       if (sdio_uart_claim_func(port) != 0)
7267 +               return;
7268 +
7269 +       if (I_IXOFF(tty)) {
7270 +               port->x_char = STOP_CHAR(tty);
7271 +               sdio_uart_start_tx(port);
7272 +       }
7273 +
7274 +       if (tty->termios->c_cflag & CRTSCTS)
7275 +               sdio_uart_clear_mctrl(port, TIOCM_RTS);
7276 +
7277 +       sdio_uart_irq(port->func);
7278 +       sdio_uart_release_func(port);
7279 +}
7280 +
7281 +static void sdio_uart_unthrottle(struct tty_struct *tty)
7282 +{
7283 +       struct sdio_uart_port *port = tty->driver_data;
7284 +
7285 +       if (!I_IXOFF(tty) && !(tty->termios->c_cflag & CRTSCTS))
7286 +               return;
7287 +
7288 +       if (sdio_uart_claim_func(port) != 0)
7289 +               return;
7290 +
7291 +       if (I_IXOFF(tty)) {
7292 +               if (port->x_char) {
7293 +                       port->x_char = 0;
7294 +               } else {
7295 +                       port->x_char = START_CHAR(tty);
7296 +                       sdio_uart_start_tx(port);
7297 +               }
7298 +       }
7299 +
7300 +       if (tty->termios->c_cflag & CRTSCTS)
7301 +               sdio_uart_set_mctrl(port, TIOCM_RTS);
7302 +
7303 +       sdio_uart_irq(port->func);
7304 +       sdio_uart_release_func(port);
7305 +}
7306 +
7307 +static void sdio_uart_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
7308 +{
7309 +       struct sdio_uart_port *port = tty->driver_data;
7310 +       unsigned int cflag = tty->termios->c_cflag;
7311 +
7312 +#define RELEVANT_IFLAG(iflag)   ((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
7313 +
7314 +       if ((cflag ^ old_termios->c_cflag) == 0 &&
7315 +           RELEVANT_IFLAG(tty->termios->c_iflag ^ old_termios->c_iflag) == 0)
7316 +               return;
7317 +
7318 +       if (sdio_uart_claim_func(port) != 0)
7319 +               return;
7320 +
7321 +       sdio_uart_change_speed(port, tty->termios, old_termios);
7322 +
7323 +       /* Handle transition to B0 status */
7324 +       if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
7325 +               sdio_uart_clear_mctrl(port, TIOCM_RTS | TIOCM_DTR);
7326 +
7327 +       /* Handle transition away from B0 status */
7328 +       if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
7329 +               unsigned int mask = TIOCM_DTR;
7330 +               if (!(cflag & CRTSCTS) || !test_bit(TTY_THROTTLED, &tty->flags))
7331 +                       mask |= TIOCM_RTS;
7332 +               sdio_uart_set_mctrl(port, mask);
7333 +       }
7334 +
7335 +       /* Handle turning off CRTSCTS */
7336 +       if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) {
7337 +               tty->hw_stopped = 0;
7338 +               sdio_uart_start_tx(port);
7339 +       }
7340 +
7341 +       /* Handle turning on CRTSCTS */
7342 +       if (!(old_termios->c_cflag & CRTSCTS) && (cflag & CRTSCTS)) {
7343 +               if (!(sdio_uart_get_mctrl(port) & TIOCM_CTS)) {
7344 +                       tty->hw_stopped = 1;
7345 +                       sdio_uart_stop_tx(port);
7346 +               }
7347 +       }
7348 +
7349 +       sdio_uart_release_func(port);
7350 +}
7351 +
7352 +static void sdio_uart_break_ctl(struct tty_struct *tty, int break_state)
7353 +{
7354 +       struct sdio_uart_port *port = tty->driver_data;
7355 +
7356 +       if (sdio_uart_claim_func(port) != 0)
7357 +               return;
7358 +
7359 +       if (break_state == -1)
7360 +               port->lcr |= UART_LCR_SBC;
7361 +       else
7362 +               port->lcr &= ~UART_LCR_SBC;
7363 +       sdio_out(port, UART_LCR, port->lcr);
7364 +
7365 +       sdio_uart_release_func(port);
7366 +}
7367 +
7368 +static int sdio_uart_tiocmget(struct tty_struct *tty, struct file *file)
7369 +{
7370 +       struct sdio_uart_port *port = tty->driver_data;
7371 +       int result;
7372 +
7373 +       result = sdio_uart_claim_func(port);
7374 +       if (!result) {
7375 +               result = port->mctrl | sdio_uart_get_mctrl(port);
7376 +               sdio_uart_release_func(port);
7377 +       }
7378 +
7379 +       return result;
7380 +}
7381 +
7382 +static int sdio_uart_tiocmset(struct tty_struct *tty, struct file *file,
7383 +                             unsigned int set, unsigned int clear)
7384 +{
7385 +       struct sdio_uart_port *port = tty->driver_data;
7386 +       int result;
7387 +
7388 +       result =sdio_uart_claim_func(port);
7389 +       if(!result) {
7390 +               sdio_uart_update_mctrl(port, set, clear);
7391 +               sdio_uart_release_func(port);
7392 +       }
7393 +
7394 +       return result;
7395 +}
7396 +
7397 +static int sdio_uart_read_proc(char *page, char **start, off_t off,
7398 +                              int count, int *eof, void *data)
7399 +{
7400 +       int i, len = 0;
7401 +       off_t begin = 0;
7402 +
7403 +       len += sprintf(page, "serinfo:1.0 driver%s%s revision:%s\n",
7404 +                      "", "", "");
7405 +       for (i = 0; i < UART_NR && len < PAGE_SIZE - 96; i++) {
7406 +               struct sdio_uart_port *port = sdio_uart_port_get(i);
7407 +               if (port) {
7408 +                       len += sprintf(page+len, "%d: uart:SDIO", i);
7409 +                       if(capable(CAP_SYS_ADMIN)) {
7410 +                               len += sprintf(page + len, " tx:%d rx:%d",
7411 +                                              port->icount.tx, port->icount.rx);
7412 +                               if (port->icount.frame)
7413 +                                       len += sprintf(page + len, " fe:%d",
7414 +                                                      port->icount.frame);
7415 +                               if (port->icount.parity)
7416 +                                       len += sprintf(page + len, " pe:%d",
7417 +                                                      port->icount.parity);
7418 +                               if (port->icount.brk)
7419 +                                       len += sprintf(page + len, " brk:%d",
7420 +                                                      port->icount.brk);
7421 +                               if (port->icount.overrun)
7422 +                                       len += sprintf(page + len, " oe:%d",
7423 +                                                      port->icount.overrun);
7424 +                               if (port->icount.cts)
7425 +                                       len += sprintf(page + len, " cts:%d",
7426 +                                                      port->icount.cts);
7427 +                               if (port->icount.dsr)
7428 +                                       len += sprintf(page + len, " dsr:%d",
7429 +                                                      port->icount.dsr);
7430 +                               if (port->icount.rng)
7431 +                                       len += sprintf(page + len, " rng:%d",
7432 +                                                      port->icount.rng);
7433 +                               if (port->icount.dcd)
7434 +                                       len += sprintf(page + len, " dcd:%d",
7435 +                                                      port->icount.dcd);
7436 +                       }
7437 +                       strcat(page, "\n");
7438 +                       len++;
7439 +                       sdio_uart_port_put(port);
7440 +               }
7441 +
7442 +               if (len + begin > off + count)
7443 +                       goto done;
7444 +               if (len + begin < off) {
7445 +                       begin += len;
7446 +                       len = 0;
7447 +               }
7448 +       }
7449 +       *eof = 1;
7450 +
7451 +done:
7452 +       if (off >= len + begin)
7453 +               return 0;
7454 +       *start = page + (off - begin);
7455 +       return (count < begin + len - off) ? count : (begin + len - off);
7456 +}
7457 +
7458 +static const struct tty_operations sdio_uart_ops = {
7459 +       .open                   = sdio_uart_open,
7460 +       .close                  = sdio_uart_close,
7461 +       .write                  = sdio_uart_write,
7462 +       .write_room             = sdio_uart_write_room,
7463 +       .chars_in_buffer        = sdio_uart_chars_in_buffer,
7464 +       .send_xchar             = sdio_uart_send_xchar,
7465 +       .throttle               = sdio_uart_throttle,
7466 +       .unthrottle             = sdio_uart_unthrottle,
7467 +       .set_termios            = sdio_uart_set_termios,
7468 +       .break_ctl              = sdio_uart_break_ctl,
7469 +       .tiocmget               = sdio_uart_tiocmget,
7470 +       .tiocmset               = sdio_uart_tiocmset,
7471 +       .read_proc              = sdio_uart_read_proc,
7472 +};
7473 +
7474 +static struct tty_driver *sdio_uart_tty_driver;
7475 +
7476 +static int sdio_uart_probe(struct sdio_func *func,
7477 +                          const struct sdio_device_id *id)
7478 +{
7479 +       struct sdio_uart_port *port;
7480 +       int ret;
7481 +
7482 +       port = kzalloc(sizeof(struct sdio_uart_port), GFP_KERNEL);
7483 +       if (!port)
7484 +               return -ENOMEM;
7485 +
7486 +       if (func->class == SDIO_CLASS_UART) {
7487 +               printk(KERN_WARNING "%s: need info on UART class basic setup\n",
7488 +                      sdio_func_id(func));
7489 +               kfree(port);
7490 +               return -ENOSYS;
7491 +       } else if (func->class == SDIO_CLASS_GPS) {
7492 +               /*
7493 +                * We need tuple 0x91.  It contains SUBTPL_SIOREG
7494 +                * and SUBTPL_RCVCAPS.
7495 +                */
7496 +               struct sdio_func_tuple *tpl;
7497 +               for (tpl = func->tuples; tpl; tpl = tpl->next) {
7498 +                       if (tpl->code != 0x91)
7499 +                               continue;
7500 +                       if (tpl->size < 10)
7501 +                               continue;
7502 +                       if (tpl->data[1] == 0)  /* SUBTPL_SIOREG */
7503 +                               break;
7504 +               }
7505 +               if (!tpl) {
7506 +                       printk(KERN_WARNING
7507 +                              "%s: can't find tuple 0x91 subtuple 0 (SUBTPL_SIOREG) for GPS class\n",
7508 +                              sdio_func_id(func));
7509 +                       kfree(port);
7510 +                       return -EINVAL;
7511 +               }
7512 +               printk(KERN_DEBUG "%s: Register ID = 0x%02x, Exp ID = 0x%02x\n",
7513 +                      sdio_func_id(func), tpl->data[2], tpl->data[3]);
7514 +               port->regs_offset = (tpl->data[4] << 0) |
7515 +                                   (tpl->data[5] << 8) |
7516 +                                   (tpl->data[6] << 16);
7517 +               printk(KERN_DEBUG "%s: regs offset = 0x%x\n",
7518 +                      sdio_func_id(func), port->regs_offset);
7519 +               port->uartclk = tpl->data[7] * 115200;
7520 +               if (port->uartclk == 0)
7521 +                       port->uartclk = 115200;
7522 +               printk(KERN_DEBUG "%s: clk %d baudcode %u 4800-div %u\n",
7523 +                      sdio_func_id(func), port->uartclk,
7524 +                      tpl->data[7], tpl->data[8] | (tpl->data[9] << 8));
7525 +       } else {
7526 +               kfree(port);
7527 +               return -EINVAL;
7528 +       }
7529 +
7530 +       port->func = func;
7531 +       sdio_set_drvdata(func, port);
7532 +
7533 +       ret = sdio_uart_add_port(port);
7534 +       if (ret) {
7535 +               kfree(port);
7536 +       } else {
7537 +               struct device *dev;
7538 +               dev = tty_register_device(sdio_uart_tty_driver, port->index, &func->dev);
7539 +               if (IS_ERR(dev)) {
7540 +                       sdio_uart_port_remove(port);
7541 +                       ret = PTR_ERR(dev);
7542 +               }
7543 +       }
7544 +
7545 +       return ret;
7546 +}
7547 +
7548 +static void sdio_uart_remove(struct sdio_func *func)
7549 +{
7550 +       struct sdio_uart_port *port = sdio_get_drvdata(func);
7551 +
7552 +       tty_unregister_device(sdio_uart_tty_driver, port->index);
7553 +       sdio_uart_port_remove(port);
7554 +}
7555 +
7556 +static const struct sdio_device_id sdio_uart_ids[] = {
7557 +       { SDIO_DEVICE_CLASS(SDIO_CLASS_UART)            },
7558 +       { SDIO_DEVICE_CLASS(SDIO_CLASS_GPS)             },
7559 +       { /* end: all zeroes */                         },
7560 +};
7561 +
7562 +MODULE_DEVICE_TABLE(sdio, sdio_uart_ids);
7563 +
7564 +static struct sdio_driver sdio_uart_driver = {
7565 +       .probe          = sdio_uart_probe,
7566 +       .remove         = sdio_uart_remove,
7567 +       .name           = "sdio_uart",
7568 +       .id_table       = sdio_uart_ids,
7569 +};
7570 +
7571 +static int __init sdio_uart_init(void)
7572 +{
7573 +       int ret;
7574 +       struct tty_driver *tty_drv;
7575 +
7576 +       sdio_uart_tty_driver = tty_drv = alloc_tty_driver(UART_NR);
7577 +       if (!tty_drv)
7578 +               return -ENOMEM;
7579 +
7580 +       tty_drv->owner = THIS_MODULE;
7581 +       tty_drv->driver_name = "sdio_uart";
7582 +       tty_drv->name =   "ttySDIO";
7583 +       tty_drv->major = 0;  /* dynamically allocated */
7584 +       tty_drv->minor_start = 0;
7585 +       tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
7586 +       tty_drv->subtype = SERIAL_TYPE_NORMAL;
7587 +       tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
7588 +       tty_drv->init_termios = tty_std_termios;
7589 +       tty_drv->init_termios.c_cflag = B4800 | CS8 | CREAD | HUPCL | CLOCAL;
7590 +       tty_drv->init_termios.c_ispeed = 4800;
7591 +       tty_drv->init_termios.c_ospeed = 4800;
7592 +       tty_set_operations(tty_drv, &sdio_uart_ops);
7593 +
7594 +       ret = tty_register_driver(tty_drv);
7595 +       if (ret)
7596 +               goto err1;
7597 +
7598 +       ret = sdio_register_driver(&sdio_uart_driver);
7599 +       if (ret)
7600 +               goto err2;
7601 +
7602 +       return 0;
7603 +
7604 +err2:
7605 +       tty_unregister_driver(tty_drv);
7606 +err1:
7607 +       put_tty_driver(tty_drv);
7608 +       return ret;
7609 +}
7610 +
7611 +static void __exit sdio_uart_exit(void)
7612 +{
7613 +       sdio_unregister_driver(&sdio_uart_driver);
7614 +       tty_unregister_driver(sdio_uart_tty_driver);
7615 +       put_tty_driver(sdio_uart_tty_driver);
7616 +}
7617 +
7618 +module_init(sdio_uart_init);
7619 +module_exit(sdio_uart_exit);
7620 +
7621 +MODULE_AUTHOR("Nicolas Pitre");
7622 +MODULE_LICENSE("GPL");
7623 Index: linux-2.6.23.17/drivers/mmc/core/Makefile
7624 ===================================================================
7625 --- linux-2.6.23.17.orig/drivers/mmc/core/Makefile
7626 +++ linux-2.6.23.17/drivers/mmc/core/Makefile
7627 @@ -8,5 +8,7 @@ endif
7628  
7629  obj-$(CONFIG_MMC)              += mmc_core.o
7630  mmc_core-y                     := core.o sysfs.o bus.o host.o \
7631 -                                  mmc.o mmc_ops.o sd.o sd_ops.o
7632 +                                  mmc.o mmc_ops.o sd.o sd_ops.o \
7633 +                                  sdio.o sdio_ops.o sdio_bus.o \
7634 +                                  sdio_cis.o sdio_io.o sdio_irq.o
7635