xref: /freebsd/sys/dev/mmc/mmcsd.c (revision fcb560670601b2a4d87bb31d7531c8dcc37ee71b)
1 /*-
2  * Copyright (c) 2006 Bernd Walter.  All rights reserved.
3  * Copyright (c) 2006 M. Warner Losh.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  * Portions of this software may have been developed with reference to
26  * the SD Simplified Specification.  The following disclaimer may apply:
27  *
28  * The following conditions apply to the release of the simplified
29  * specification ("Simplified Specification") by the SD Card Association and
30  * the SD Group. The Simplified Specification is a subset of the complete SD
31  * Specification which is owned by the SD Card Association and the SD
32  * Group. This Simplified Specification is provided on a non-confidential
33  * basis subject to the disclaimers below. Any implementation of the
34  * Simplified Specification may require a license from the SD Card
35  * Association, SD Group, SD-3C LLC or other third parties.
36  *
37  * Disclaimers:
38  *
39  * The information contained in the Simplified Specification is presented only
40  * as a standard specification for SD Cards and SD Host/Ancillary products and
41  * is provided "AS-IS" without any representations or warranties of any
42  * kind. No responsibility is assumed by the SD Group, SD-3C LLC or the SD
43  * Card Association for any damages, any infringements of patents or other
44  * right of the SD Group, SD-3C LLC, the SD Card Association or any third
45  * parties, which may result from its use. No license is granted by
46  * implication, estoppel or otherwise under any patent or other rights of the
47  * SD Group, SD-3C LLC, the SD Card Association or any third party. Nothing
48  * herein shall be construed as an obligation by the SD Group, the SD-3C LLC
49  * or the SD Card Association to disclose or distribute any technical
50  * information, know-how or other confidential information to any third party.
51  */
52 
53 #include <sys/cdefs.h>
54 __FBSDID("$FreeBSD$");
55 
56 #include <sys/param.h>
57 #include <sys/systm.h>
58 #include <sys/bio.h>
59 #include <sys/bus.h>
60 #include <sys/conf.h>
61 #include <sys/kernel.h>
62 #include <sys/kthread.h>
63 #include <sys/lock.h>
64 #include <sys/malloc.h>
65 #include <sys/module.h>
66 #include <sys/mutex.h>
67 #include <sys/time.h>
68 #include <geom/geom_disk.h>
69 
70 #include <dev/mmc/mmcbrvar.h>
71 #include <dev/mmc/mmcreg.h>
72 #include <dev/mmc/mmcvar.h>
73 
74 #include "mmcbus_if.h"
75 
76 #if __FreeBSD_version < 800002
77 #define	kproc_create	kthread_create
78 #define	kproc_exit	kthread_exit
79 #endif
80 
81 struct mmcsd_softc {
82 	device_t dev;
83 	struct mtx sc_mtx;
84 	struct disk *disk;
85 	struct proc *p;
86 	struct bio_queue_head bio_queue;
87 	daddr_t eblock, eend;	/* Range remaining after the last erase. */
88 	int running;
89 	int suspend;
90 	int log_count;
91 	struct timeval log_time;
92 };
93 
94 static const char *errmsg[] =
95 {
96 	"None",
97 	"Timeout",
98 	"Bad CRC",
99 	"Fifo",
100 	"Failed",
101 	"Invalid",
102 	"NO MEMORY"
103 };
104 
105 #define	LOG_PPS		5 /* Log no more than 5 errors per second. */
106 
107 /* bus entry points */
108 static int mmcsd_attach(device_t dev);
109 static int mmcsd_detach(device_t dev);
110 static int mmcsd_probe(device_t dev);
111 
112 /* disk routines */
113 static int mmcsd_close(struct disk *dp);
114 static int mmcsd_dump(void *arg, void *virtual, vm_offset_t physical,
115 	off_t offset, size_t length);
116 static int mmcsd_open(struct disk *dp);
117 static void mmcsd_strategy(struct bio *bp);
118 static void mmcsd_task(void *arg);
119 
120 static int mmcsd_bus_bit_width(device_t dev);
121 static daddr_t mmcsd_delete(struct mmcsd_softc *sc, struct bio *bp);
122 static daddr_t mmcsd_rw(struct mmcsd_softc *sc, struct bio *bp);
123 
124 #define MMCSD_LOCK(_sc)		mtx_lock(&(_sc)->sc_mtx)
125 #define	MMCSD_UNLOCK(_sc)	mtx_unlock(&(_sc)->sc_mtx)
126 #define MMCSD_LOCK_INIT(_sc) \
127 	mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->dev), \
128 	    "mmcsd", MTX_DEF)
129 #define MMCSD_LOCK_DESTROY(_sc)	mtx_destroy(&_sc->sc_mtx);
130 #define MMCSD_ASSERT_LOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_OWNED);
131 #define MMCSD_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED);
132 
133 static int
134 mmcsd_probe(device_t dev)
135 {
136 
137 	device_quiet(dev);
138 	device_set_desc(dev, "MMC/SD Memory Card");
139 	return (0);
140 }
141 
142 static int
143 mmcsd_attach(device_t dev)
144 {
145 	struct mmcsd_softc *sc;
146 	struct disk *d;
147 	intmax_t mb;
148 	uint32_t speed;
149 	uint32_t maxblocks;
150 	char unit;
151 
152 	sc = device_get_softc(dev);
153 	sc->dev = dev;
154 	MMCSD_LOCK_INIT(sc);
155 
156 	d = sc->disk = disk_alloc();
157 	d->d_open = mmcsd_open;
158 	d->d_close = mmcsd_close;
159 	d->d_strategy = mmcsd_strategy;
160 	d->d_dump = mmcsd_dump;
161 	d->d_name = "mmcsd";
162 	d->d_drv1 = sc;
163 	d->d_sectorsize = mmc_get_sector_size(dev);
164 	d->d_maxsize = mmc_get_max_data(dev) * d->d_sectorsize;
165 	d->d_mediasize = (off_t)mmc_get_media_size(dev) * d->d_sectorsize;
166 	d->d_stripeoffset = 0;
167 	d->d_stripesize = mmc_get_erase_sector(dev) * d->d_sectorsize;
168 	d->d_unit = device_get_unit(dev);
169 	d->d_flags = DISKFLAG_CANDELETE;
170 	d->d_delmaxsize = mmc_get_erase_sector(dev) * d->d_sectorsize * 1; /* conservative */
171 	/*
172 	 * The d_fw* values are fake. However, layout is aided by making the
173 	 * number of fwsectors equal to the erase sectors from the drive since
174 	 * we set the stripe size equal to that. We set fwheads such that there
175 	 * are ~20 cylinder groups since all values are somewhat arbitrary here
176 	 * and this gives good behavior with ffs without wasting too much
177 	 * space.  Sadly, geom_part wants to round partitions to these
178 	 * values. While not bad, in and of itself, the values we present here
179 	 * will almost certainly be different then the values that USB SD
180 	 * adapters use and there's too much variation between brands to just
181 	 * use those values here.  Also SD to ATA adapters favor traditional
182 	 * ata sizes, which are different again from the USB adapters (which
183 	 * favor SCSI values). This rounding leads to a loss of up to 5% of the
184 	 * usable space (usually much less, but that's why 20 was selected: to
185 	 * limit this effect at a few percent). gpart needs a way to override
186 	 * this behavior for situations like this, but doesn't provide
187 	 * one. Perhaps this behavior should be tunable as well, but maybe that
188 	 * belongs in the disk layer.  These values will be much better than
189 	 * the default ones.
190 	 */
191 	d->d_fwsectors = mmc_get_erase_sector(dev);
192 	d->d_fwheads = mmc_get_media_size(dev) / (d->d_fwsectors * 20);
193 	strlcpy(d->d_ident, mmc_get_card_sn_string(dev), sizeof(d->d_ident));
194 	strlcpy(d->d_descr, mmc_get_card_id_string(dev), sizeof(d->d_descr));
195 
196 	/*
197 	 * Display in most natural units.  There's no cards < 1MB.  The SD
198 	 * standard goes to 2GiB due to its reliance on FAT, but the data
199 	 * format supports up to 4GiB and some card makers push it up to this
200 	 * limit.  The SDHC standard only goes to 32GiB due to FAT32, but the
201 	 * data format supports up to 2TiB however. 2048GB isn't too ugly, so
202 	 * we note it in passing here and don't add the code to print
203 	 * TB). Since these cards are sold in terms of MB and GB not MiB and
204 	 * GiB, report them like that. We also round to the nearest unit, since
205 	 * many cards are a few percent short, even of the power of 10 size.
206 	 */
207 	mb = (d->d_mediasize + 1000000 / 2 - 1) / 1000000;
208 	unit = 'M';
209 	if (mb >= 1000) {
210 		unit = 'G';
211 		mb = (mb + 1000 / 2 - 1) / 1000;
212 	}
213 	/*
214 	 * Report the clock speed of the underlying hardware, which might be
215 	 * different than what the card reports due to hardware limitations.
216 	 * Report how many blocks the hardware transfers at once.
217 	 */
218 	speed = mmcbr_get_clock(device_get_parent(dev));
219 	maxblocks = mmc_get_max_data(dev);
220 	device_printf(dev, "%ju%cB <%s>%s at %s %d.%01dMHz/%dbit/%d-block\n",
221 	    mb, unit, d->d_descr,
222 	    mmc_get_read_only(dev) ? " (read-only)" : "",
223 	    device_get_nameunit(device_get_parent(dev)),
224 	    speed / 1000000, (speed / 100000) % 10,
225 	    mmcsd_bus_bit_width(dev), maxblocks);
226 	disk_create(d, DISK_VERSION);
227 	bioq_init(&sc->bio_queue);
228 
229 	sc->running = 1;
230 	sc->suspend = 0;
231 	sc->eblock = sc->eend = 0;
232 	kproc_create(&mmcsd_task, sc, &sc->p, 0, 0, "%s: mmc/sd card",
233 	    device_get_nameunit(dev));
234 
235 	return (0);
236 }
237 
238 static int
239 mmcsd_detach(device_t dev)
240 {
241 	struct mmcsd_softc *sc = device_get_softc(dev);
242 
243 	MMCSD_LOCK(sc);
244 	sc->suspend = 0;
245 	if (sc->running > 0) {
246 		/* kill thread */
247 		sc->running = 0;
248 		wakeup(sc);
249 		/* wait for thread to finish. */
250 		while (sc->running != -1)
251 			msleep(sc, &sc->sc_mtx, 0, "detach", 0);
252 	}
253 	MMCSD_UNLOCK(sc);
254 
255 	/* Flush the request queue. */
256 	bioq_flush(&sc->bio_queue, NULL, ENXIO);
257 	/* kill disk */
258 	disk_destroy(sc->disk);
259 
260 	MMCSD_LOCK_DESTROY(sc);
261 
262 	return (0);
263 }
264 
265 static int
266 mmcsd_suspend(device_t dev)
267 {
268 	struct mmcsd_softc *sc = device_get_softc(dev);
269 
270 	MMCSD_LOCK(sc);
271 	sc->suspend = 1;
272 	if (sc->running > 0) {
273 		/* kill thread */
274 		sc->running = 0;
275 		wakeup(sc);
276 		/* wait for thread to finish. */
277 		while (sc->running != -1)
278 			msleep(sc, &sc->sc_mtx, 0, "detach", 0);
279 	}
280 	MMCSD_UNLOCK(sc);
281 	return (0);
282 }
283 
284 static int
285 mmcsd_resume(device_t dev)
286 {
287 	struct mmcsd_softc *sc = device_get_softc(dev);
288 
289 	MMCSD_LOCK(sc);
290 	sc->suspend = 0;
291 	if (sc->running <= 0) {
292 		sc->running = 1;
293 		MMCSD_UNLOCK(sc);
294 		kproc_create(&mmcsd_task, sc, &sc->p, 0, 0, "%s: mmc/sd card",
295 		    device_get_nameunit(dev));
296 	} else
297 		MMCSD_UNLOCK(sc);
298 	return (0);
299 }
300 
301 static int
302 mmcsd_open(struct disk *dp)
303 {
304 
305 	return (0);
306 }
307 
308 static int
309 mmcsd_close(struct disk *dp)
310 {
311 
312 	return (0);
313 }
314 
315 static void
316 mmcsd_strategy(struct bio *bp)
317 {
318 	struct mmcsd_softc *sc;
319 
320 	sc = (struct mmcsd_softc *)bp->bio_disk->d_drv1;
321 	MMCSD_LOCK(sc);
322 	if (sc->running > 0 || sc->suspend > 0) {
323 		bioq_disksort(&sc->bio_queue, bp);
324 		MMCSD_UNLOCK(sc);
325 		wakeup(sc);
326 	} else {
327 		MMCSD_UNLOCK(sc);
328 		biofinish(bp, NULL, ENXIO);
329 	}
330 }
331 
332 static const char *
333 mmcsd_errmsg(int e)
334 {
335 	if (e < 0 || e > MMC_ERR_MAX)
336 		return "Bad error code";
337 	return errmsg[e];
338 }
339 
340 static daddr_t
341 mmcsd_rw(struct mmcsd_softc *sc, struct bio *bp)
342 {
343 	daddr_t block, end;
344 	struct mmc_command cmd;
345 	struct mmc_command stop;
346 	struct mmc_request req;
347 	struct mmc_data data;
348 	device_t dev = sc->dev;
349 	int sz = sc->disk->d_sectorsize;
350 	device_t mmcbr = device_get_parent(dev);
351 
352 	block = bp->bio_pblkno;
353 	end = bp->bio_pblkno + (bp->bio_bcount / sz);
354 	while (block < end) {
355 		char *vaddr = bp->bio_data +
356 		    (block - bp->bio_pblkno) * sz;
357 		int numblocks = min(end - block, mmc_get_max_data(dev));
358 		memset(&req, 0, sizeof(req));
359     		memset(&cmd, 0, sizeof(cmd));
360 		memset(&stop, 0, sizeof(stop));
361 		memset(&data, 0, sizeof(data));
362 		cmd.mrq = &req;
363 		req.cmd = &cmd;
364 		cmd.data = &data;
365 		if (bp->bio_cmd == BIO_READ) {
366 			if (numblocks > 1)
367 				cmd.opcode = MMC_READ_MULTIPLE_BLOCK;
368 			else
369 				cmd.opcode = MMC_READ_SINGLE_BLOCK;
370 		} else {
371 			if (numblocks > 1)
372 				cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
373 			else
374 				cmd.opcode = MMC_WRITE_BLOCK;
375 		}
376 		cmd.arg = block;
377 		if (!mmc_get_high_cap(dev))
378 			cmd.arg <<= 9;
379 		cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
380 		data.data = vaddr;
381 		data.mrq = &req;
382 		if (bp->bio_cmd == BIO_READ)
383 			data.flags = MMC_DATA_READ;
384 		else
385 			data.flags = MMC_DATA_WRITE;
386 		data.len = numblocks * sz;
387 		if (numblocks > 1) {
388 			data.flags |= MMC_DATA_MULTI;
389 			stop.opcode = MMC_STOP_TRANSMISSION;
390 			stop.arg = 0;
391 			stop.flags = MMC_RSP_R1B | MMC_CMD_AC;
392 			stop.mrq = &req;
393 			req.stop = &stop;
394 		}
395 		MMCBUS_WAIT_FOR_REQUEST(mmcbr, dev, &req);
396 		if (req.cmd->error != MMC_ERR_NONE) {
397 			if (ppsratecheck(&sc->log_time, &sc->log_count, LOG_PPS)) {
398 				device_printf(dev, "Error indicated: %d %s\n",
399 				    req.cmd->error, mmcsd_errmsg(req.cmd->error));
400 			}
401 			break;
402 		}
403 		block += numblocks;
404 	}
405 	return (block);
406 }
407 
408 static daddr_t
409 mmcsd_delete(struct mmcsd_softc *sc, struct bio *bp)
410 {
411 	daddr_t block, end, start, stop;
412 	struct mmc_command cmd;
413 	struct mmc_request req;
414 	device_t dev = sc->dev;
415 	int sz = sc->disk->d_sectorsize;
416 	int erase_sector;
417 	device_t mmcbr = device_get_parent(dev);
418 
419 	block = bp->bio_pblkno;
420 	end = bp->bio_pblkno + (bp->bio_bcount / sz);
421 	/* Coalesce with part remaining from previous request. */
422 	if (block > sc->eblock && block <= sc->eend)
423 		block = sc->eblock;
424 	if (end >= sc->eblock && end < sc->eend)
425 		end = sc->eend;
426 	/* Safe round to the erase sector boundaries. */
427 	erase_sector = mmc_get_erase_sector(dev);
428 	start = block + erase_sector - 1;	 /* Round up. */
429 	start -= start % erase_sector;
430 	stop = end;				/* Round down. */
431 	stop -= end % erase_sector;
432 	/* We can't erase area smaller then sector, store it for later. */
433 	if (start >= stop) {
434 		sc->eblock = block;
435 		sc->eend = end;
436 		return (end);
437 	}
438 
439 	/* Set erase start position. */
440 	memset(&req, 0, sizeof(req));
441 	memset(&cmd, 0, sizeof(cmd));
442 	cmd.mrq = &req;
443 	req.cmd = &cmd;
444 	if (mmc_get_card_type(dev) == mode_sd)
445 		cmd.opcode = SD_ERASE_WR_BLK_START;
446 	else
447 		cmd.opcode = MMC_ERASE_GROUP_START;
448 	cmd.arg = start;
449 	if (!mmc_get_high_cap(dev))
450 		cmd.arg <<= 9;
451 	cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
452 	MMCBUS_WAIT_FOR_REQUEST(mmcbr, dev, &req);
453 	if (req.cmd->error != MMC_ERR_NONE) {
454 	    printf("erase err1: %d\n", req.cmd->error);
455 	    return (block);
456 	}
457 	/* Set erase stop position. */
458 	memset(&req, 0, sizeof(req));
459 	memset(&cmd, 0, sizeof(cmd));
460 	req.cmd = &cmd;
461 	if (mmc_get_card_type(dev) == mode_sd)
462 		cmd.opcode = SD_ERASE_WR_BLK_END;
463 	else
464 		cmd.opcode = MMC_ERASE_GROUP_END;
465 	cmd.arg = stop;
466 	if (!mmc_get_high_cap(dev))
467 		cmd.arg <<= 9;
468 	cmd.arg--;
469 	cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
470 	MMCBUS_WAIT_FOR_REQUEST(mmcbr, dev, &req);
471 	if (req.cmd->error != MMC_ERR_NONE) {
472 	    printf("erase err2: %d\n", req.cmd->error);
473 	    return (block);
474 	}
475 	/* Erase range. */
476 	memset(&req, 0, sizeof(req));
477 	memset(&cmd, 0, sizeof(cmd));
478 	req.cmd = &cmd;
479 	cmd.opcode = MMC_ERASE;
480 	cmd.arg = 0;
481 	cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
482 	MMCBUS_WAIT_FOR_REQUEST(mmcbr, dev, &req);
483 	if (req.cmd->error != MMC_ERR_NONE) {
484 	    printf("erase err3 %d\n", req.cmd->error);
485 	    return (block);
486 	}
487 	/* Store one of remaining parts for the next call. */
488 	if (bp->bio_pblkno >= sc->eblock || block == start) {
489 		sc->eblock = stop;	/* Predict next forward. */
490 		sc->eend = end;
491 	} else {
492 		sc->eblock = block;	/* Predict next backward. */
493 		sc->eend = start;
494 	}
495 	return (end);
496 }
497 
498 static int
499 mmcsd_dump(void *arg, void *virtual, vm_offset_t physical,
500 	off_t offset, size_t length)
501 {
502 	struct disk *disk = arg;
503 	struct mmcsd_softc *sc = (struct mmcsd_softc *)disk->d_drv1;
504 	device_t dev = sc->dev;
505 	struct bio bp;
506 	daddr_t block, end;
507 	device_t mmcbr = device_get_parent(dev);
508 
509 	/* length zero is special and really means flush buffers to media */
510 	if (!length)
511 		return (0);
512 
513 	bzero(&bp, sizeof(struct bio));
514 	bp.bio_disk = disk;
515 	bp.bio_pblkno = offset / disk->d_sectorsize;
516 	bp.bio_bcount = length;
517 	bp.bio_data = virtual;
518 	bp.bio_cmd = BIO_WRITE;
519 	end = bp.bio_pblkno + bp.bio_bcount / sc->disk->d_sectorsize;
520 	MMCBUS_ACQUIRE_BUS(mmcbr, dev);
521 	block = mmcsd_rw(sc, &bp);
522 	MMCBUS_RELEASE_BUS(mmcbr, dev);
523 	return ((end < block) ? EIO : 0);
524 }
525 
526 static void
527 mmcsd_task(void *arg)
528 {
529 	struct mmcsd_softc *sc = (struct mmcsd_softc*)arg;
530 	struct bio *bp;
531 	int sz;
532 	daddr_t block, end;
533 	device_t dev = sc->dev;
534 	device_t mmcbr = device_get_parent(sc->dev);
535 
536 	while (1) {
537 		MMCSD_LOCK(sc);
538 		do {
539 			if (sc->running == 0)
540 				goto out;
541 			bp = bioq_takefirst(&sc->bio_queue);
542 			if (bp == NULL)
543 				msleep(sc, &sc->sc_mtx, PRIBIO, "jobqueue", 0);
544 		} while (bp == NULL);
545 		MMCSD_UNLOCK(sc);
546 		if (bp->bio_cmd != BIO_READ && mmc_get_read_only(dev)) {
547 			bp->bio_error = EROFS;
548 			bp->bio_resid = bp->bio_bcount;
549 			bp->bio_flags |= BIO_ERROR;
550 			biodone(bp);
551 			continue;
552 		}
553 		MMCBUS_ACQUIRE_BUS(mmcbr, dev);
554 		sz = sc->disk->d_sectorsize;
555 		block = bp->bio_pblkno;
556 		end = bp->bio_pblkno + (bp->bio_bcount / sz);
557 		if (bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE) {
558 			/* Access to the remaining erase block obsoletes it. */
559 			if (block < sc->eend && end > sc->eblock)
560 				sc->eblock = sc->eend = 0;
561 			block = mmcsd_rw(sc, bp);
562 		} else if (bp->bio_cmd == BIO_DELETE) {
563 			block = mmcsd_delete(sc, bp);
564 		}
565 		MMCBUS_RELEASE_BUS(mmcbr, dev);
566 		if (block < end) {
567 			bp->bio_error = EIO;
568 			bp->bio_resid = (end - block) * sz;
569 			bp->bio_flags |= BIO_ERROR;
570 		}
571 		biodone(bp);
572 	}
573 out:
574 	/* tell parent we're done */
575 	sc->running = -1;
576 	MMCSD_UNLOCK(sc);
577 	wakeup(sc);
578 
579 	kproc_exit(0);
580 }
581 
582 static int
583 mmcsd_bus_bit_width(device_t dev)
584 {
585 
586 	if (mmc_get_bus_width(dev) == bus_width_1)
587 		return (1);
588 	if (mmc_get_bus_width(dev) == bus_width_4)
589 		return (4);
590 	return (8);
591 }
592 
593 static device_method_t mmcsd_methods[] = {
594 	DEVMETHOD(device_probe, mmcsd_probe),
595 	DEVMETHOD(device_attach, mmcsd_attach),
596 	DEVMETHOD(device_detach, mmcsd_detach),
597 	DEVMETHOD(device_suspend, mmcsd_suspend),
598 	DEVMETHOD(device_resume, mmcsd_resume),
599 	DEVMETHOD_END
600 };
601 
602 static driver_t mmcsd_driver = {
603 	"mmcsd",
604 	mmcsd_methods,
605 	sizeof(struct mmcsd_softc),
606 };
607 static devclass_t mmcsd_devclass;
608 
609 DRIVER_MODULE(mmcsd, mmc, mmcsd_driver, mmcsd_devclass, NULL, NULL);
610