Deleted Added
full compact
altera_avgen.c (239674) altera_avgen.c (245376)
1/*-
1/*-
2 * Copyright (c) 2012 Robert N. M. Watson
2 * Copyright (c) 2012-2013 Robert N. M. Watson
3 * All rights reserved.
4 *
5 * This software was developed by SRI International and the University of
6 * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
7 * ("CTSRD"), as part of the DARPA CRASH research programme.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions

--- 13 unchanged lines hidden (view full) ---

24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <sys/cdefs.h>
3 * All rights reserved.
4 *
5 * This software was developed by SRI International and the University of
6 * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
7 * ("CTSRD"), as part of the DARPA CRASH research programme.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions

--- 13 unchanged lines hidden (view full) ---

24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <sys/cdefs.h>
32__FBSDID("$FreeBSD: head/sys/dev/altera/avgen/altera_avgen.c 239674 2012-08-25 11:07:43Z rwatson $");
32__FBSDID("$FreeBSD: head/sys/dev/altera/avgen/altera_avgen.c 245376 2013-01-13 16:43:59Z rwatson $");
33
34#include <sys/param.h>
35#include <sys/bus.h>
36#include <sys/condvar.h>
37#include <sys/conf.h>
38#include <sys/kernel.h>
39#include <sys/lock.h>
40#include <sys/malloc.h>

--- 180 unchanged lines hidden (view full) ---

221 rman_get_size(sc->avg_res) >= offset + PAGE_SIZE) {
222 *paddr = rman_get_start(sc->avg_res) + offset;
223 *memattr = VM_MEMATTR_UNCACHEABLE;
224 } else
225 return (ENODEV);
226 return (0);
227}
228
33
34#include <sys/param.h>
35#include <sys/bus.h>
36#include <sys/condvar.h>
37#include <sys/conf.h>
38#include <sys/kernel.h>
39#include <sys/lock.h>
40#include <sys/malloc.h>

--- 180 unchanged lines hidden (view full) ---

221 rman_get_size(sc->avg_res) >= offset + PAGE_SIZE) {
222 *paddr = rman_get_start(sc->avg_res) + offset;
223 *memattr = VM_MEMATTR_UNCACHEABLE;
224 } else
225 return (ENODEV);
226 return (0);
227}
228
229static int
230altera_avgen_nexus_probe(device_t dev)
231{
232
229
233 device_set_desc(dev, "Generic Altera Avalon device attachment");
234 return (BUS_PROBE_DEFAULT);
235}
236
237static int
238altera_avgen_process_options(struct altera_avgen_softc *sc,
239 const char *str_fileio, const char *str_mmapio, const char *str_devname,
240 int devunit)
241{
242 const char *cp;
243 device_t dev = sc->avg_dev;
244

--- 71 unchanged lines hidden (view full) ---

316 ALTERA_AVALON_STR_MMAPIO, *cp);
317 return (ENXIO);
318 }
319 }
320 }
321 return (0);
322}
323
230static int
231altera_avgen_process_options(struct altera_avgen_softc *sc,
232 const char *str_fileio, const char *str_mmapio, const char *str_devname,
233 int devunit)
234{
235 const char *cp;
236 device_t dev = sc->avg_dev;
237

--- 71 unchanged lines hidden (view full) ---

309 ALTERA_AVALON_STR_MMAPIO, *cp);
310 return (ENXIO);
311 }
312 }
313 }
314 return (0);
315}
316
324static int
325altera_avgen_nexus_attach(device_t dev)
317int
318altera_avgen_attach(struct altera_avgen_softc *sc, const char *str_fileio,
319 const char *str_mmapio, const char *str_devname, int devunit)
326{
320{
327 struct altera_avgen_softc *sc;
328 const char *str_fileio, *str_mmapio;
329 const char *str_devname;
321 device_t dev = sc->avg_dev;
330 char devname[SPECNAMELEN + 1];
322 char devname[SPECNAMELEN + 1];
331 int devunit, error;
323 int error;
332
324
333 sc = device_get_softc(dev);
334 sc->avg_dev = dev;
335 sc->avg_unit = device_get_unit(dev);
336
337 /*
338 * Query non-standard hints to find out what operations are permitted
339 * on the device, and whether it is cached.
340 */
341 str_fileio = NULL;
342 str_mmapio = NULL;
343 str_devname = NULL;
344 devunit = -1;
345 sc->avg_width = 1;
346 error = resource_int_value(device_get_name(dev), device_get_unit(dev),
347 ALTERA_AVALON_STR_WIDTH, &sc->avg_width);
348 if (error != 0 && error != ENOENT) {
349 device_printf(dev, "invalid %s\n", ALTERA_AVALON_STR_WIDTH);
350 return (error);
351 }
352 (void)resource_string_value(device_get_name(dev),
353 device_get_unit(dev), ALTERA_AVALON_STR_FILEIO, &str_fileio);
354 (void)resource_string_value(device_get_name(dev),
355 device_get_unit(dev), ALTERA_AVALON_STR_MMAPIO, &str_mmapio);
356 (void)resource_string_value(device_get_name(dev),
357 device_get_unit(dev), ALTERA_AVALON_STR_DEVNAME, &str_devname);
358 (void)resource_int_value(device_get_name(dev), device_get_unit(dev),
359 ALTERA_AVALON_STR_DEVUNIT, &devunit);
360 error = altera_avgen_process_options(sc, str_fileio, str_mmapio,
361 str_devname, devunit);
362 if (error)
363 return (error);
364
365 /* Select a device name. */
366 if (str_devname != NULL) {
367 if (devunit != -1)
368 (void)snprintf(devname, sizeof(devname), "%s%d",
369 str_devname, devunit);
370 else
371 (void)snprintf(devname, sizeof(devname), "%s",
372 str_devname);
373 } else
374 snprintf(devname, sizeof(devname), "%s%d", "avgen",
375 sc->avg_unit);
376
325 error = altera_avgen_process_options(sc, str_fileio, str_mmapio,
326 str_devname, devunit);
327 if (error)
328 return (error);
329
330 /* Select a device name. */
331 if (str_devname != NULL) {
332 if (devunit != -1)
333 (void)snprintf(devname, sizeof(devname), "%s%d",
334 str_devname, devunit);
335 else
336 (void)snprintf(devname, sizeof(devname), "%s",
337 str_devname);
338 } else
339 snprintf(devname, sizeof(devname), "%s%d", "avgen",
340 sc->avg_unit);
341
377 /* Memory allocation and checking. */
378 sc->avg_rid = 0;
379 sc->avg_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
380 &sc->avg_rid, RF_ACTIVE);
381 if (sc->avg_res == NULL) {
382 device_printf(dev, "couldn't map memory\n");
383 return (ENXIO);
384 }
385 if (rman_get_size(sc->avg_res) >= PAGE_SIZE || str_mmapio != NULL) {
386 if (rman_get_size(sc->avg_res) % PAGE_SIZE != 0) {
387 device_printf(dev,
388 "memory region not even multiple of page size\n");
342 if (rman_get_size(sc->avg_res) >= PAGE_SIZE || str_mmapio != NULL) {
343 if (rman_get_size(sc->avg_res) % PAGE_SIZE != 0) {
344 device_printf(dev,
345 "memory region not even multiple of page size\n");
389 error = ENXIO;
390 goto error;
346 return (ENXIO);
391 }
392 if (rman_get_start(sc->avg_res) % PAGE_SIZE != 0) {
393 device_printf(dev, "memory region not page-aligned\n");
347 }
348 if (rman_get_start(sc->avg_res) % PAGE_SIZE != 0) {
349 device_printf(dev, "memory region not page-aligned\n");
394 error = ENXIO;
395 goto error;
350 return (ENXIO);
396 }
397 }
398
399 /* Device node allocation. */
400 if (str_devname == NULL) {
401 str_devname = "altera_avgen%d";
402 devunit = sc->avg_unit;
403 }
404 if (devunit != -1)
405 sc->avg_cdev = make_dev(&avg_cdevsw, sc->avg_unit, UID_ROOT,
406 GID_WHEEL, S_IRUSR | S_IWUSR, str_devname, devunit);
407 else
408 sc->avg_cdev = make_dev(&avg_cdevsw, sc->avg_unit, UID_ROOT,
409 GID_WHEEL, S_IRUSR | S_IWUSR, str_devname);
410 if (sc->avg_cdev == NULL) {
411 device_printf(sc->avg_dev, "%s: make_dev failed\n", __func__);
351 }
352 }
353
354 /* Device node allocation. */
355 if (str_devname == NULL) {
356 str_devname = "altera_avgen%d";
357 devunit = sc->avg_unit;
358 }
359 if (devunit != -1)
360 sc->avg_cdev = make_dev(&avg_cdevsw, sc->avg_unit, UID_ROOT,
361 GID_WHEEL, S_IRUSR | S_IWUSR, str_devname, devunit);
362 else
363 sc->avg_cdev = make_dev(&avg_cdevsw, sc->avg_unit, UID_ROOT,
364 GID_WHEEL, S_IRUSR | S_IWUSR, str_devname);
365 if (sc->avg_cdev == NULL) {
366 device_printf(sc->avg_dev, "%s: make_dev failed\n", __func__);
412 error = ENXIO;
413 goto error;
367 return (ENXIO);
414 }
415 /* XXXRW: Slight race between make_dev(9) and here. */
416 sc->avg_cdev->si_drv1 = sc;
417 return (0);
368 }
369 /* XXXRW: Slight race between make_dev(9) and here. */
370 sc->avg_cdev->si_drv1 = sc;
371 return (0);
418
419error:
420 bus_release_resource(dev, SYS_RES_MEMORY, sc->avg_rid, sc->avg_res);
421 return (error);
422}
423
372}
373
424static int
425altera_avgen_nexus_detach(device_t dev)
374void
375altera_avgen_detach(struct altera_avgen_softc *sc)
426{
376{
427 struct altera_avgen_softc *sc;
428
377
429 sc = device_get_softc(dev);
430 destroy_dev(sc->avg_cdev);
378 destroy_dev(sc->avg_cdev);
431 bus_release_resource(dev, SYS_RES_MEMORY, sc->avg_rid, sc->avg_res);
432 return (0);
433}
379}
434
435static device_method_t altera_avgen_nexus_methods[] = {
436 DEVMETHOD(device_probe, altera_avgen_nexus_probe),
437 DEVMETHOD(device_attach, altera_avgen_nexus_attach),
438 DEVMETHOD(device_detach, altera_avgen_nexus_detach),
439 { 0, 0 }
440};
441
442static driver_t altera_avgen_nexus_driver = {
443 "altera_avgen",
444 altera_avgen_nexus_methods,
445 sizeof(struct altera_avgen_softc),
446};
447
448static devclass_t altera_avgen_devclass;
449
450DRIVER_MODULE(avgen, nexus, altera_avgen_nexus_driver, altera_avgen_devclass,
451 0, 0);