nandemulator.c revision 1.1 1 /* $NetBSD: nandemulator.c,v 1.1 2011/02/26 18:07:31 ahoka Exp $ */
2
3 /*-
4 * Copyright (c) 2011 Department of Software Engineering,
5 * University of Szeged, Hungary
6 * Copyright (c) 2011 Adam Hoka <ahoka (at) NetBSD.org>
7 * All rights reserved.
8 *
9 * This code is derived from software contributed to The NetBSD Foundation
10 * by the Department of Software Engineering, University of Szeged, Hungary
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: nandemulator.c,v 1.1 2011/02/26 18:07:31 ahoka Exp $");
36
37 #include <sys/param.h>
38 #include <sys/device.h>
39 #include <sys/conf.h>
40 #include <sys/kmem.h>
41 #include <sys/kernel.h>
42
43 #include "nandemulator.h"
44
45 #include <dev/nand/nand.h>
46 #include <dev/nand/onfi.h>
47 #include <dev/nand/nand_crc.h>
48
49 extern struct cfdriver nandemulator_cd;
50 void nandemulatorattach(int n);
51
52 static int nandemulator_match(device_t, cfdata_t, void *);
53 static void nandemulator_attach(device_t, device_t, void *);
54 static int nandemulator_detach(device_t, int);
55
56 static void nandemulator_device_reset(device_t);
57 static void nandemulator_command(device_t, uint8_t);
58 static void nandemulator_address(device_t, uint8_t);
59 static void nandemulator_busy(device_t);
60 static void nandemulator_read_byte(device_t, uint8_t *);
61 static void nandemulator_write_byte(device_t, uint8_t);
62 static void nandemulator_read_word(device_t, uint16_t *);
63 static void nandemulator_write_word(device_t, uint16_t);
64 static void nandemulator_read_buf_byte(device_t, void *, size_t);
65 static void nandemulator_read_buf_word(device_t, void *, size_t);
66 static void nandemulator_write_buf_byte(device_t, const void *, size_t);
67 static void nandemulator_write_buf_word(device_t, const void *, size_t);
68
69 static size_t nandemulator_address_to_page(device_t);
70 static size_t nandemulator_page_to_backend_offset(device_t, size_t);
71 static size_t nandemulator_column_address_to_subpage(device_t);
72 /*
73 #define NANDEMULATOR_DEBUG 1
74
75 #ifdef NANDEMULATOR_DEBUG
76 #warning debug enabled
77 #define DPRINTF(x) if (nandemulatordebug) printf x
78 #define DPRINTFN(n,x) if (nandemulatordebug>(n)) printf x
79 #else
80 #error no debug
81 #define DPRINTF(x)
82 #define DPRINTFN(n,x)
83 #endif
84
85 #ifdef NANDEMULATOR_DEBUG
86 int nandemulatordebug = NANDEMULATOR_DEBUG;
87 #endif
88 */
89
90 extern int nanddebug;
91
92 enum {
93 NANDEMULATOR_8BIT,
94 NANDEMULATOR_16BIT
95 };
96
97 struct nandemulator_softc {
98 device_t sc_dev;
99 device_t sc_nanddev;
100
101 int sc_buswidth;
102
103 struct nand_interface sc_nand_if;
104
105 uint8_t sc_command;
106 size_t sc_io_len;
107 uint8_t *sc_io_pointer;
108 uint64_t sc_address;
109
110 uint8_t *sc_backend;
111 size_t sc_backend_size;
112 size_t sc_device_size;
113 bool sc_register_writable;
114
115 uint8_t sc_status_register;
116 uint8_t sc_ids[2];
117 uint8_t sc_onfi[4];
118
119 size_t sc_page_size;
120 size_t sc_block_size;
121 size_t sc_spare_size;
122 size_t sc_lun_size;
123 uint8_t sc_row_cycles;
124 uint8_t sc_column_cycles;
125 uint64_t sc_row_mask;
126
127 int sc_address_counter;
128
129 struct onfi_parameter_page *sc_parameter_page;
130 };
131
132 CFATTACH_DECL_NEW(nandemulator, sizeof(struct nandemulator_softc),
133 nandemulator_match, nandemulator_attach, nandemulator_detach, NULL);
134
135 void
136 nandemulatorattach(int n)
137 {
138 int i, err;
139 cfdata_t cf;
140
141 aprint_debug("nandemulator: requested %d units\n", n);
142
143 err = config_cfattach_attach(nandemulator_cd.cd_name,
144 &nandemulator_ca);
145 if (err) {
146 aprint_error("%s: couldn't register cfattach: %d\n",
147 nandemulator_cd.cd_name, err);
148 config_cfdriver_detach(&nandemulator_cd);
149 return;
150 }
151 for (i = 0; i < n; i++) {
152 cf = kmem_alloc(sizeof(struct cfdata), KM_NOSLEEP);
153 if (cf == NULL) {
154 aprint_error("%s: couldn't allocate cfdata\n",
155 nandemulator_cd.cd_name);
156 continue;
157 }
158 cf->cf_name = nandemulator_cd.cd_name;
159 cf->cf_atname = nandemulator_cd.cd_name;
160 cf->cf_unit = i;
161 cf->cf_fstate = FSTATE_STAR;
162
163 (void)config_attach_pseudo(cf);
164 }
165 }
166
167 /* ARGSUSED */
168 static int
169 nandemulator_match(device_t parent, cfdata_t match, void *aux)
170 {
171 /* pseudo device, always attaches */
172 return 1;
173 }
174
175 static void
176 nandemulator_attach(device_t parent, device_t self, void *aux)
177 {
178 struct nandemulator_softc *sc = device_private(self);
179 int i;
180
181 aprint_normal_dev(self, "NAND emulator\n");
182
183 sc->sc_dev = self;
184
185 sc->sc_nand_if.select = &nand_default_select;
186 sc->sc_nand_if.command = &nandemulator_command;
187 sc->sc_nand_if.address = &nandemulator_address;
188 sc->sc_nand_if.read_buf_byte = &nandemulator_read_buf_byte;
189 sc->sc_nand_if.read_buf_word = &nandemulator_read_buf_word;
190 sc->sc_nand_if.read_byte = &nandemulator_read_byte;
191 sc->sc_nand_if.read_word = &nandemulator_read_word;
192 sc->sc_nand_if.write_buf_byte = &nandemulator_write_buf_byte;
193 sc->sc_nand_if.write_buf_word = &nandemulator_write_buf_word;
194 sc->sc_nand_if.write_byte = &nandemulator_write_byte;
195 sc->sc_nand_if.write_word = &nandemulator_write_word;
196 sc->sc_nand_if.busy = &nandemulator_busy;
197
198 sc->sc_nand_if.ecc_compute = &nand_default_ecc_compute;
199 sc->sc_nand_if.ecc_correct = &nand_default_ecc_correct;
200 sc->sc_nand_if.ecc_prepare = NULL;
201 sc->sc_nand_if.ecc.necc_code_size = 3;
202 sc->sc_nand_if.ecc.necc_block_size = 256;
203 sc->sc_nand_if.ecc.necc_type = NAND_ECC_TYPE_SW;
204
205 if (!pmf_device_register1(sc->sc_dev, NULL, NULL, NULL))
206 aprint_error_dev(sc->sc_dev,
207 "couldn't establish power handler\n");
208
209 sc->sc_buswidth = NANDEMULATOR_16BIT; /* 16bit for now */
210
211 /* hardcode these now, make it configurable later */
212 sc->sc_device_size = 32 * 1024 * 1024; /* 32MB */
213 sc->sc_page_size = 2048;
214 sc->sc_block_size = 64;
215 sc->sc_lun_size =
216 sc->sc_device_size / (sc->sc_page_size * sc->sc_block_size);
217 KASSERT(sc->sc_device_size %
218 (sc->sc_page_size * sc->sc_block_size) == 0);
219 sc->sc_spare_size = 64;
220
221 sc->sc_column_cycles = 2;
222 sc->sc_row_cycles = 3;
223
224 /* init the emulator data structures */
225 sc->sc_backend_size =
226 sc->sc_device_size +
227 sc->sc_device_size / sc->sc_page_size * sc->sc_spare_size;
228
229 sc->sc_backend = kmem_alloc(sc->sc_backend_size, KM_SLEEP);
230 memset(sc->sc_backend, 0xff, sc->sc_backend_size);
231
232 sc->sc_parameter_page =
233 kmem_zalloc(sizeof(struct onfi_parameter_page) * 4, KM_SLEEP);
234
235 struct onfi_parameter_page *opp;
236 uint8_t sig[4] = { 'O', 'N', 'F', 'I' };
237
238 for (i = 0; i < 4; i++) {
239 opp = &sc->sc_parameter_page[i];
240
241 opp->param_signature = *(uint32_t *)sig;
242 opp->param_pagesize = sc->sc_page_size;
243 opp->param_blocksize = sc->sc_block_size;
244 opp->param_sparesize = sc->sc_spare_size;
245 opp->param_lunsize = sc->sc_lun_size;
246 opp->param_numluns = 1;
247
248 opp->param_manufacturer_id = 0x00;
249 memcpy(opp->param_manufacturer,
250 "NETBSD", strlen("NETBSD"));
251 memcpy(opp->param_model,
252 "NANDEMULATOR", strlen("NANDEMULATOR"));
253
254 opp->param_features = ONFI_FEATURE_16BIT;
255
256 /* the lower 4 bits contain the row address cycles
257 * the upper 4 bits contain the column address cycles
258 */
259 opp->param_addr_cycles = sc->sc_row_cycles;
260 opp->param_addr_cycles |= (sc->sc_column_cycles << 4);
261
262 opp->param_integrity_crc = nand_crc16((uint8_t *)opp, 254);
263 }
264
265 sc->sc_ids[0] = 0x00;
266 sc->sc_ids[1] = 0x00;
267
268 sc->sc_onfi[0] = 'O';
269 sc->sc_onfi[1] = 'N';
270 sc->sc_onfi[2] = 'F';
271 sc->sc_onfi[3] = 'I';
272
273 sc->sc_row_mask = 0x00;
274 for (i = 0; i < sc->sc_row_cycles; i++) {
275 sc->sc_row_mask <<= 8;
276 sc->sc_row_mask |= 0xff;
277 }
278
279 nandemulator_device_reset(self);
280
281 sc->sc_nanddev = nand_attach_mi(&sc->sc_nand_if, sc->sc_dev);
282 }
283
284 static int
285 nandemulator_detach(device_t self, int flags)
286 {
287 struct nandemulator_softc *sc = device_private(self);
288 int ret = 0;
289
290 aprint_normal_dev(sc->sc_dev, "detaching emulator\n");
291
292 pmf_device_deregister(sc->sc_dev);
293
294 if (sc->sc_nanddev != NULL)
295 ret = config_detach(sc->sc_nanddev, flags);
296
297 kmem_free(sc->sc_backend, sc->sc_backend_size);
298 kmem_free(sc->sc_parameter_page,
299 sizeof(struct onfi_parameter_page) * 4);
300
301 return ret;
302 }
303
304 /**
305 * bring the emulated device to a known state
306 */
307 static void
308 nandemulator_device_reset(device_t self)
309 {
310 struct nandemulator_softc *sc = device_private(self);
311
312 sc->sc_command = 0;
313 sc->sc_register_writable = false;
314 sc->sc_io_len = 0;
315 sc->sc_io_pointer = NULL;
316 sc->sc_address = 0;
317 sc->sc_address_counter = 0;
318
319 sc->sc_status_register = ONFI_STATUS_RDY | ONFI_STATUS_WP;
320 }
321
322 static void
323 nandemulator_address_chip(device_t self)
324 {
325 struct nandemulator_softc *sc = device_private(self);
326 size_t page, offset;
327
328 if (sc->sc_address_counter !=
329 sc->sc_column_cycles + sc->sc_row_cycles) {
330 aprint_error_dev(self, "incorrect number of address cycles\n");
331 aprint_error_dev(self, "cc: %d, rc: %d, ac: %d\n",
332 sc->sc_column_cycles, sc->sc_row_cycles,
333 sc->sc_address_counter);
334 }
335
336 page = nandemulator_address_to_page(self);
337 offset = sc->sc_page_size * page;
338
339 DPRINTF(("READ/PROGRAM; page: 0x%jx (row addr: 0x%jx)\n",
340 (uintmax_t )page,
341 (uintmax_t )offset));
342
343 if (offset >= sc->sc_device_size) {
344 aprint_error_dev(self, "address > device size!\n");
345 sc->sc_io_len = 0;
346 } else {
347 size_t addr =
348 nandemulator_page_to_backend_offset(self, page);
349 size_t pageoff =
350 nandemulator_column_address_to_subpage(self);
351
352 DPRINTF(("subpage: 0x%jx\n", (uintmax_t )pageoff));
353
354 KASSERT(pageoff <
355 sc->sc_page_size + sc->sc_spare_size);
356 KASSERT(addr < sc->sc_backend_size);
357
358 sc->sc_io_pointer = sc->sc_backend + addr + pageoff;
359 sc->sc_io_len =
360 sc->sc_page_size + sc->sc_spare_size - pageoff;
361 }
362 }
363
364 static void
365 nandemulator_command(device_t self, uint8_t command)
366 {
367 struct nandemulator_softc *sc = device_private(self);
368 size_t offset, page;
369
370 sc->sc_command = command;
371 sc->sc_register_writable = false;
372
373 DPRINTF(("nandemulator command: 0x%hhx\n", command));
374
375 switch (command) {
376 case ONFI_READ_STATUS:
377 sc->sc_io_pointer = &sc->sc_status_register;
378 sc->sc_io_len = 1;
379 break;
380 case ONFI_RESET:
381 nandemulator_device_reset(self);
382 break;
383 case ONFI_PAGE_PROGRAM:
384 sc->sc_register_writable = true;
385 case ONFI_READ:
386 case ONFI_BLOCK_ERASE:
387 sc->sc_address_counter = 0;
388 case ONFI_READ_ID:
389 case ONFI_READ_PARAMETER_PAGE:
390 sc->sc_io_len = 0;
391 sc->sc_address = 0;
392 break;
393 case ONFI_PAGE_PROGRAM_START:
394 /* XXX the program should only happen here */
395 break;
396 case ONFI_READ_START:
397 nandemulator_address_chip(self);
398 break;
399 case ONFI_BLOCK_ERASE_START:
400 page = nandemulator_address_to_page(self);
401 offset = sc->sc_page_size * page;
402
403 KASSERT(offset %
404 (sc->sc_block_size * sc->sc_page_size) == 0);
405
406 if (offset >= sc->sc_device_size) {
407 aprint_error_dev(self, "address > device size!\n");
408 } else {
409 size_t addr =
410 nandemulator_page_to_backend_offset(self, page);
411
412 size_t blocklen =
413 sc->sc_block_size *
414 (sc->sc_page_size + sc->sc_spare_size);
415
416 KASSERT(addr < sc->sc_backend_size);
417 uint8_t *block = sc->sc_backend + addr;
418
419 DPRINTF(("erasing block at 0x%jx\n",
420 (uintmax_t )offset));
421
422 memset(block, 0xff, blocklen);
423 }
424 sc->sc_io_len = 0;
425 break;
426 default:
427 aprint_error_dev(self,
428 "invalid nand command (0x%hhx)\n", command);
429 sc->sc_io_len = 0;
430 }
431 };
432
433 static void
434 nandemulator_address(device_t self, uint8_t address)
435 {
436 struct nandemulator_softc *sc = device_private(self);
437
438 /**
439 * we have to handle read id/parameter page here,
440 * as we can read right after giving the address.
441 */
442 switch (sc->sc_command) {
443 case ONFI_READ_ID:
444 if (address == 0x00) {
445 sc->sc_io_len = 2;
446 sc->sc_io_pointer = sc->sc_ids;
447 } else if (address == 0x20) {
448 sc->sc_io_len = 4;
449 sc->sc_io_pointer = sc->sc_onfi;
450 } else {
451 sc->sc_io_len = 0;
452 }
453 break;
454 case ONFI_READ_PARAMETER_PAGE:
455 if (address == 0x00) {
456 sc->sc_io_len = sizeof(struct onfi_parameter_page) * 4;
457 sc->sc_io_pointer = (uint8_t *)sc->sc_parameter_page;
458 } else {
459 sc->sc_io_len = 0;
460 }
461 break;
462 case ONFI_PAGE_PROGRAM:
463 sc->sc_address <<= 8;
464 sc->sc_address |= address;
465 sc->sc_address_counter++;
466
467 if (sc->sc_address_counter ==
468 sc->sc_column_cycles + sc->sc_row_cycles) {
469 nandemulator_address_chip(self);
470 }
471 break;
472 default:
473 sc->sc_address <<= 8;
474 sc->sc_address |= address;
475 sc->sc_address_counter++;
476 }
477 };
478
479 static void
480 nandemulator_busy(device_t self)
481 {
482 #ifdef NANDEMULATOR_DELAYS
483 struct nandemulator_softc *sc = device_private(self);
484
485 /* do some delay depending on command */
486 switch (sc->sc_command) {
487 case ONFI_PAGE_PROGRAM_START:
488 case ONFI_BLOCK_ERASE_START:
489 DELAY(10);
490 break;
491 case ONFI_READ_START:
492 default:
493 DELAY(1);
494 }
495 #endif
496 }
497
498 static void
499 nandemulator_read_byte(device_t self, uint8_t *data)
500 {
501 struct nandemulator_softc *sc = device_private(self);
502
503 if (sc->sc_io_len > 0) {
504 *data = *sc->sc_io_pointer;
505
506 sc->sc_io_pointer++;
507 sc->sc_io_len--;
508 } else {
509 aprint_error_dev(self, "reading byte from invalid location\n");
510 *data = 0xff;
511 }
512 }
513
514 static void
515 nandemulator_write_byte(device_t self, uint8_t data)
516 {
517 struct nandemulator_softc *sc = device_private(self);
518
519 if (!sc->sc_register_writable) {
520 aprint_error_dev(self,
521 "trying to write read only location without effect\n");
522 return;
523 }
524
525 if (sc->sc_io_len > 0) {
526 *sc->sc_io_pointer = data;
527
528 sc->sc_io_pointer++;
529 sc->sc_io_len--;
530 } else {
531 aprint_error_dev(self, "write to invalid location\n");
532 }
533 }
534
535 static void
536 nandemulator_read_word(device_t self, uint16_t *data)
537 {
538 struct nandemulator_softc *sc = device_private(self);
539
540 if (sc->sc_buswidth != NANDEMULATOR_16BIT) {
541 aprint_error_dev(self,
542 "trying to read a word on an 8bit chip\n");
543 return;
544 }
545
546 if (sc->sc_io_len > 1) {
547 *data = *(uint16_t *)sc->sc_io_pointer;
548
549 sc->sc_io_pointer += 2;
550 sc->sc_io_len -= 2;
551 } else {
552 aprint_error_dev(self, "reading word from invalid location\n");
553 *data = 0xffff;
554 }
555 }
556
557 static void
558 nandemulator_write_word(device_t self, uint16_t data)
559 {
560 struct nandemulator_softc *sc = device_private(self);
561
562 if (!sc->sc_register_writable) {
563 aprint_error_dev(self,
564 "trying to write read only location without effect\n");
565 return;
566 }
567
568 if (sc->sc_buswidth != NANDEMULATOR_16BIT) {
569 aprint_error_dev(self,
570 "trying to write a word to an 8bit chip");
571 return;
572 }
573
574 if (sc->sc_io_len > 1) {
575 *(uint16_t *)sc->sc_io_pointer = data;
576
577 sc->sc_io_pointer += 2;
578 sc->sc_io_len -= 2;
579 } else {
580 aprint_error_dev(self, "writing to invalid location");
581 }
582 }
583
584 static void
585 nandemulator_read_buf_byte(device_t self, void *buf, size_t len)
586 {
587 uint8_t *addr;
588
589 KASSERT(buf != NULL);
590 KASSERT(len >= 1);
591
592 addr = buf;
593 while (len > 0) {
594 nandemulator_read_byte(self, addr);
595 addr++, len--;
596 }
597 }
598
599 static void
600 nandemulator_read_buf_word(device_t self, void *buf, size_t len)
601 {
602 uint16_t *addr;
603
604 KASSERT(buf != NULL);
605 KASSERT(len >= 2);
606 KASSERT(!(len & 0x01));
607
608 addr = buf;
609 len /= 2;
610 while (len > 0) {
611 nandemulator_read_word(self, addr);
612 addr++, len--;
613 }
614 }
615
616 static void
617 nandemulator_write_buf_byte(device_t self, const void *buf, size_t len)
618 {
619 const uint8_t *addr;
620
621 KASSERT(buf != NULL);
622 KASSERT(len >= 1);
623
624 addr = buf;
625 while (len > 0) {
626 nandemulator_write_byte(self, *addr);
627 addr++, len--;
628 }
629 }
630
631 static void
632 nandemulator_write_buf_word(device_t self, const void *buf, size_t len)
633 {
634 const uint16_t *addr;
635
636 KASSERT(buf != NULL);
637 KASSERT(len >= 2);
638 KASSERT(!(len & 0x01));
639
640 addr = buf;
641 len /= 2;
642 while (len > 0) {
643 nandemulator_write_word(self, *addr);
644 addr++, len--;
645 }
646 }
647
648 static size_t
649 nandemulator_address_to_page(device_t self)
650 {
651 struct nandemulator_softc *sc = device_private(self);
652 uint64_t address, offset;
653 int i;
654
655 address = htole64(sc->sc_address);
656 address &= sc->sc_row_mask;
657
658 offset = 0;
659 for (i = 0; i < sc->sc_row_cycles; i++) {
660 offset <<= 8;
661 offset |= (address & 0xff);
662 address >>= 8;
663 }
664
665 return le64toh(offset);
666 }
667
668 static size_t
669 nandemulator_column_address_to_subpage(device_t self)
670 {
671 struct nandemulator_softc *sc = device_private(self);
672 uint64_t address, offset;
673 int i;
674
675 address = htole64(sc->sc_address);
676 address >>= (8 * sc->sc_row_cycles);
677
678 offset = 0;
679 for (i = 0; i < sc->sc_column_cycles; i++) {
680 offset <<= 8;
681 offset |= (address & 0xff);
682 address >>= 8;
683 }
684
685 if (sc->sc_buswidth == NANDEMULATOR_16BIT)
686 return (size_t )le64toh(offset << 1);
687 else
688 return (size_t )le64toh(offset);
689 }
690
691 static size_t
692 nandemulator_page_to_backend_offset(device_t self, size_t page)
693 {
694 struct nandemulator_softc *sc = device_private(self);
695
696 return (sc->sc_page_size + sc->sc_spare_size) * page;
697 }
698
699 #ifdef _MODULE
700
701 MODULE(MODULE_CLASS_DRIVER, nandemulator, "nand");
702
703 static const struct cfiattrdata nandbuscf_iattrdata = {
704 "nandbus", 0, { { NULL, NULL, 0 }, }
705 };
706 static const struct cfiattrdata * const nandemulator_attrs[] = {
707 &nandbuscf_iattrdata, NULL
708 };
709
710 CFDRIVER_DECL(nandemulator, DV_DULL, nandemulator_attrs);
711 extern struct cfattach nandemulator_ca;
712 static int nandemulatorloc[] = { -1, -1 };
713
714 static struct cfdata nandemulator_cfdata[] = {
715 {
716 .cf_name = "nandemulator",
717 .cf_atname = "nandemulator",
718 .cf_unit = 0,
719 .cf_fstate = FSTATE_STAR,
720 .cf_loc = nandemulatorloc,
721 .cf_flags = 0,
722 .cf_pspec = NULL,
723 },
724 { NULL, NULL, 0, 0, NULL, 0, NULL }
725 };
726
727 static int
728 nandemulator_modcmd(modcmd_t cmd, void *arg)
729 {
730 int error;
731
732 switch (cmd) {
733 case MODULE_CMD_INIT:
734 error = config_cfdriver_attach(&nandemulator_cd);
735 if (error) {
736 return error;
737 }
738
739 error = config_cfattach_attach(nandemulator_cd.cd_name,
740 &nandemulator_ca);
741 if (error) {
742 config_cfdriver_detach(&nandemulator_cd);
743 aprint_error("%s: unable to register cfattach\n",
744 nandemulator_cd.cd_name);
745
746 return error;
747 }
748
749 error = config_cfdata_attach(nandemulator_cfdata, 1);
750 if (error) {
751 config_cfattach_detach(nandemulator_cd.cd_name,
752 &nandemulator_ca);
753 config_cfdriver_detach(&nandemulator_cd);
754 aprint_error("%s: unable to register cfdata\n",
755 nandemulator_cd.cd_name);
756
757 return error;
758 }
759
760 (void)config_attach_pseudo(nandemulator_cfdata);
761
762 return 0;
763 case MODULE_CMD_FINI:
764 error = config_cfdata_detach(nandemulator_cfdata);
765 if (error) {
766 return error;
767 }
768
769 config_cfattach_detach(nandemulator_cd.cd_name,
770 &nandemulator_ca);
771 config_cfdriver_detach(&nandemulator_cd);
772
773 return 0;
774 default:
775 return ENOTTY;
776 }
777 }
778
779 #endif
780