The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/adlink/adlink.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 2003-2004 Poul-Henning Kamp
    3  * 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  * 3. The names of the authors may not be used to endorse or promote
   14  *    products derived from this software without specific prior written
   15  *    permission.
   16  *
   17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   27  * SUCH DAMAGE.
   28  */
   29 
   30 #include <sys/cdefs.h>
   31 __FBSDID("$FreeBSD$");
   32 
   33 #ifdef _KERNEL
   34 #include <sys/param.h>
   35 #include <sys/systm.h>
   36 #include <sys/malloc.h>
   37 #include <sys/kernel.h>
   38 #include <sys/module.h>
   39 #include <sys/kthread.h>
   40 #include <sys/conf.h>
   41 #include <sys/bus.h>
   42 #include <machine/bus.h>
   43 #include <machine/resource.h>
   44 #include <sys/rman.h>
   45 #include <dev/pci/pcireg.h>
   46 #include <dev/pci/pcivar.h>
   47 #include <pci_if.h>
   48 #include <vm/vm.h>
   49 #include <vm/pmap.h>
   50 
   51 #endif /* _KERNEL */
   52 
   53 #include <sys/ioccom.h>
   54 
   55 struct wave {
   56         int                     index;
   57         int                     period;
   58         int                     offset;
   59         int                     length;
   60         int                     avg;
   61         off_t                   mapvir;
   62         int                     flags;
   63 
   64         int                     npages;
   65         void                    **virtual;
   66 };
   67 
   68 #define ADLINK_SETWAVE          _IOWR('A', 232, struct wave)
   69 #define ADLINK_GETWAVE          _IOWR('A', 233, struct wave)
   70 
   71 #ifdef _KERNEL
   72 
   73 #define INTPERPAGE (PAGE_SIZE / sizeof(int))
   74 #define I16PERPAGE (PAGE_SIZE / sizeof(int16_t))
   75 
   76 /*
   77  * Sample rate
   78  */
   79 #define SPS     1250000
   80 
   81 /*
   82  * We sample one channel (= 16 bits) at 1.25 msps giving 2.5Mbyte/sec,
   83  * 100 pages will give us about 1/6 second buffering.
   84  */
   85 #define NRING   100
   86 
   87 /*
   88  * How many waves are we willing to entertain
   89  */
   90 #define NWAVE   25
   91 
   92 struct info {
   93         int                     nring;
   94         off_t                   o_ring;
   95         
   96         int                     ngri;
   97         int                     ppgri;
   98         off_t                   o_gri;
   99 };
  100 
  101 struct softc {
  102         device_t                device;
  103         void                    *intrhand;
  104         struct resource         *r0, *r1, *ri;
  105         bus_space_tag_t         t0, t1;
  106         bus_space_handle_t      h0, h1;
  107         struct cdev *dev;
  108         off_t                   mapvir;
  109 
  110         struct proc             *procp;
  111 
  112         struct info             *info;
  113 
  114         struct wave             *wave[NWAVE];
  115 
  116         int                     idx;
  117         void                    *ring[NRING];
  118         vm_paddr_t              pring[NRING];
  119         int                     stat[NRING];
  120 
  121         uint64_t                cnt;
  122 
  123         u_char                  flags[I16PERPAGE];
  124 };
  125 
  126 static void
  127 adlink_wave(struct softc *sc, struct wave *wp, int16_t *sp)
  128 {
  129         int f, i, k, m, *ip;
  130 
  131         f = 0;
  132         for (i = 0; i < I16PERPAGE; ) {
  133                 k = (sc->cnt - wp->offset + i) % wp->period;
  134                 if (k >= wp->length) {
  135                         i += wp->period - k;
  136                         sp += wp->period - k;
  137                         continue;
  138                 }
  139                 m = k % INTPERPAGE;
  140                 ip = (int *)(wp->virtual[k / INTPERPAGE]) + m;
  141                 while (m < INTPERPAGE && i < I16PERPAGE && k < wp->length) {
  142                         if (sc->flags[i] >= wp->index)
  143                                 *ip += (*sp * 8 - *ip) >> wp->avg;
  144                         if (wp->flags & 1)
  145                                 sc->flags[i] = wp->index;
  146                         sp++;
  147                         ip++;
  148                         m++;
  149                         i++;
  150                         k++;
  151                 }
  152         }
  153 }
  154 
  155 static void
  156 adlink_tickle(struct softc *sc)
  157 {
  158 
  159         wakeup(sc);
  160         tsleep(&sc->ring, PUSER | PCATCH, "tickle", 1);
  161 }
  162 
  163 static int
  164 adlink_new_wave(struct softc *sc, int index, int period, int offset, int length, int avg, int flags)
  165 {
  166         struct wave *wp;
  167         int l, i;
  168         void **oldvir, **newvir;
  169 
  170         if (index < 0 || index >= NWAVE)
  171                 return (EINVAL);
  172         wp = sc->wave[index];
  173         if (wp == NULL) {
  174                 adlink_tickle(sc);
  175                 wp = malloc(sizeof *wp, M_DEVBUF, M_WAITOK | M_ZERO);
  176         }
  177         l = howmany(length, INTPERPAGE);
  178         /* Setting a high average here to neuter the realtime bits */
  179         wp->avg = 31;
  180         if (wp->npages < l) {
  181                 oldvir = wp->virtual;
  182                 adlink_tickle(sc);
  183                 newvir = malloc(sizeof(void *) * l, M_DEVBUF, M_WAITOK | M_ZERO);
  184                 if (wp->npages > 0) {
  185                         adlink_tickle(sc);
  186                         bcopy(oldvir, newvir, wp->npages * sizeof(void *));
  187                 }
  188                 for (i = wp->npages; i < l; i++) {
  189                         adlink_tickle(sc);
  190                         newvir[i] = malloc(PAGE_SIZE, M_DEVBUF, M_WAITOK);
  191                 }
  192                 wp->virtual = newvir;
  193                 wp->npages = l;
  194                 wp->mapvir = sc->mapvir;
  195                 sc->mapvir += l * PAGE_SIZE;
  196         } else {
  197                 oldvir = NULL;
  198         }
  199         wp->index = index;
  200         wp->period = period;
  201         wp->offset = offset;
  202         wp->length = length;
  203         wp->flags = flags;
  204         
  205         for (i = 0; i < l; i++) {
  206                 adlink_tickle(sc);
  207                 bzero(wp->virtual[i], PAGE_SIZE);
  208         }
  209         wp->avg = avg;
  210         sc->wave[index] = wp;
  211         printf("Wave[%d] {period %d, offset %d, length %d, avg %d, flags %x}\n",
  212             wp->index, wp->period, wp->offset, wp->length, wp->avg, wp->flags);
  213         free(oldvir, M_DEVBUF);
  214         return (0);
  215 }
  216 
  217 static void
  218 adlink_loran(void *arg)
  219 {
  220         struct softc *sc;
  221         int idx, i;
  222 
  223         sc = arg;
  224         idx = 0;
  225         mtx_lock(&Giant);
  226         for (;;) {
  227                 while (sc->stat[idx] == 0)
  228                         msleep(sc, NULL, PRIBIO, "loran", 1);
  229                 memset(sc->flags, NWAVE, sizeof sc->flags);
  230                 for (i = 0; i < NWAVE; i++) {
  231                         if (sc->wave[i] != NULL)
  232                                 adlink_wave(sc, sc->wave[i], sc->ring[idx]);
  233                 }
  234                 sc->cnt += I16PERPAGE;
  235                 sc->stat[idx] = 0;
  236                 idx++;
  237                 idx %= NRING;
  238         }
  239         mtx_unlock(&Giant);
  240         kthread_exit(0);
  241 }
  242 
  243 static int
  244 adlink_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
  245 {
  246         static int once;
  247         struct softc *sc;
  248         int i, error;
  249         uint32_t u;
  250 
  251         if (once)
  252                 return (0);
  253         once = 1;
  254 
  255         sc = dev->si_drv1;
  256         sc->info = malloc(PAGE_SIZE, M_DEVBUF, M_ZERO | M_WAITOK);
  257         sc->info->nring = NRING;
  258 
  259         sc->info->o_ring = PAGE_SIZE;
  260         for (i = 0; i < NRING; i++) {
  261                 sc->ring[i] = malloc(PAGE_SIZE, M_DEVBUF, M_ZERO | M_WAITOK);
  262                 sc->pring[i] = vtophys(sc->ring[i]);
  263         }
  264 
  265         error = adlink_new_wave(sc, NWAVE - 1, SPS, 0, SPS, 7, 0);
  266         if (error)
  267                 return (error);
  268 
  269         error = kthread_create(adlink_loran, sc, &sc->procp,
  270             0, 0, "adlink%d", device_get_unit(sc->device));
  271         if (error)
  272                 return (error);
  273 
  274         /* Enable interrupts on write complete */
  275         bus_space_write_4(sc->t0, sc->h0, 0x38, 0x00004000);
  276 
  277         /* Sample CH0 only */
  278         bus_space_write_4(sc->t1, sc->h1, 0x00, 1);
  279 
  280         /* Divide clock by four */
  281         bus_space_write_4(sc->t1, sc->h1, 0x04, 4);
  282 
  283         /* Software trigger mode: software */
  284         bus_space_write_4(sc->t1, sc->h1, 0x08, 0);
  285 
  286         /* Trigger level zero */
  287         bus_space_write_4(sc->t1, sc->h1, 0x0c, 0);
  288 
  289         /* Trigger source CH0 (not used) */
  290         bus_space_write_4(sc->t1, sc->h1, 0x10, 0);
  291 
  292         /* Fifo control/status: flush */
  293         bus_space_write_4(sc->t1, sc->h1, 0x18, 3);
  294 
  295         /* Clock source: external sine */
  296         bus_space_write_4(sc->t1, sc->h1, 0x20, 2);
  297 
  298         /* Set up Write DMA */
  299         bus_space_write_4(sc->t0, sc->h0, 0x24, sc->pring[i]);
  300         bus_space_write_4(sc->t0, sc->h0, 0x28, PAGE_SIZE);
  301         u = bus_space_read_4(sc->t0, sc->h0, 0x3c);
  302         bus_space_write_4(sc->t0, sc->h0, 0x3c, u | 0x00000600);
  303 
  304         /* Acquisition Enable Register: go! */
  305         bus_space_write_4(sc->t1, sc->h1, 0x1c, 1);
  306         return (0);
  307 }
  308 
  309 static int
  310 adlink_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
  311 {
  312         struct softc *sc;
  313         struct wave *wp;
  314         int i, error;
  315         
  316         sc = dev->si_drv1;
  317         wp = (struct wave *)data;
  318         i = wp->index;
  319         if (i < 0 || i >= NWAVE)
  320                 return (EINVAL);
  321         if (cmd == ADLINK_GETWAVE) {
  322                 if (sc->wave[i] == NULL)
  323                         return (ENOENT);
  324                 bcopy(sc->wave[i], wp, sizeof(*wp));
  325                 return (0);
  326         }
  327         if (cmd == ADLINK_SETWAVE) {
  328                 error = adlink_new_wave(sc,
  329                         i,
  330                         wp->period,
  331                         wp->offset,
  332                         wp->length,
  333                         wp->avg,
  334                         wp->flags);
  335                 if (error)
  336                         return (error);
  337                 bcopy(sc->wave[i], wp, sizeof(*wp));
  338                 return (0);
  339         }
  340         return (ENOIOCTL);
  341 }
  342 
  343 static int
  344 adlink_mmap(struct cdev *dev, vm_offset_t offset, vm_paddr_t *paddr, int nprot)
  345 {
  346         struct softc *sc;
  347         struct wave *wp;
  348         int i, j;
  349 
  350         sc = dev->si_drv1;
  351         if (nprot != VM_PROT_READ)
  352                 return (-1);
  353         for (i = 0; i < NWAVE; i++) {
  354                 if (sc->wave[i] == NULL)
  355                         continue;
  356                 wp = sc->wave[i];
  357                 if (offset < wp->mapvir)
  358                         continue;
  359                 j = (offset - wp->mapvir) / PAGE_SIZE;
  360                 if (j >= wp->npages)
  361                         continue;
  362                 *paddr = vtophys(wp->virtual[j]);
  363                 return (0);
  364         }
  365         return (-1);
  366 }
  367 
  368 static void
  369 adlink_intr(void *arg)
  370 {
  371         struct softc *sc;
  372         uint32_t u;
  373         int i, j;
  374 
  375         sc = arg;
  376         u = bus_space_read_4(sc->t0, sc->h0, 0x38);
  377         if (!(u & 0x00800000))
  378                 return;
  379         bus_space_write_4(sc->t0, sc->h0, 0x38, u | 0x003f4000);
  380 
  381         j = sc->idx;
  382         sc->stat[j] = 1;
  383         i = (j + 1) % NRING;
  384         sc->idx = i;
  385         u = bus_space_read_4(sc->t1, sc->h1, 0x18);
  386         if (u & 1) {
  387                 printf("adlink FIFO overrun\n");
  388                 return;
  389         }
  390         bus_space_write_4(sc->t0, sc->h0, 0x24, sc->pring[i]);
  391         bus_space_write_4(sc->t0, sc->h0, 0x28, PAGE_SIZE);
  392         wakeup(sc);
  393         if (sc->stat[i]) {
  394                 printf("adlink page busy\n");
  395         }
  396 }
  397 
  398 static struct cdevsw adlink_cdevsw = {
  399         .d_version =    D_VERSION,
  400         .d_flags =      D_NEEDGIANT,
  401         .d_open =       adlink_open,
  402         .d_ioctl =      adlink_ioctl,
  403         .d_mmap =       adlink_mmap,
  404         .d_name =       "adlink",
  405 };
  406 
  407 static devclass_t adlink_devclass;
  408 
  409 static int
  410 adlink_probe(device_t self)
  411 {
  412 
  413         if (pci_get_devid(self) != 0x80da10e8)
  414                 return (ENXIO);
  415         device_set_desc(self, "Adlink PCI-9812 4 ch 12 bit 20 msps");
  416         return (0);
  417 }
  418 
  419 static int
  420 adlink_attach(device_t self)
  421 {
  422         struct softc *sc;
  423         int rid, i;
  424 
  425         sc = device_get_softc(self);
  426         bzero(sc, sizeof *sc);
  427         sc->device = self;
  428 
  429         /*
  430          * This is the PCI mapped registers of the AMCC 9535 "matchmaker"
  431          * chip.
  432          */
  433         rid = 0x10;
  434         sc->r0 = bus_alloc_resource_any(self, SYS_RES_IOPORT, &rid, RF_ACTIVE);
  435         if (sc->r0 == NULL)
  436                 return(ENODEV);
  437         sc->t0 = rman_get_bustag(sc->r0);
  438         sc->h0 = rman_get_bushandle(sc->r0);
  439         printf("Res0 %x %x\n", sc->t0, sc->h0);
  440 
  441         /*
  442          * This is the PCI mapped registers of the ADC hardware, they
  443          * are described in the manual which comes with the card.
  444          */
  445         rid = 0x14;
  446         sc->r1 =  bus_alloc_resource_any(self, SYS_RES_IOPORT, &rid, RF_ACTIVE);
  447         if (sc->r1 == NULL)
  448                 return(ENODEV);
  449         sc->t1 = rman_get_bustag(sc->r1);
  450         sc->h1 = rman_get_bushandle(sc->r1);
  451         printf("Res1 %x %x\n", sc->t1, sc->h1);
  452 
  453         rid = 0x0;
  454         sc->ri =  bus_alloc_resource_any(self, SYS_RES_IRQ, &rid, 
  455             RF_ACTIVE | RF_SHAREABLE);
  456         if (sc->ri == NULL)
  457                 return (ENODEV);
  458 
  459         i = bus_setup_intr(self, sc->ri, INTR_MPSAFE | INTR_TYPE_MISC | INTR_FAST,
  460             adlink_intr, sc, &sc->intrhand);
  461         if (i) {
  462                 printf("adlink: Couldn't get FAST intr\n");
  463                 i = bus_setup_intr(self, sc->ri, INTR_TYPE_MISC,
  464                     adlink_intr, sc, &sc->intrhand);
  465         }
  466 
  467         if (i)
  468                 return (ENODEV);
  469 
  470         sc->dev = make_dev(&adlink_cdevsw, device_get_unit(self),
  471             UID_ROOT, GID_WHEEL, 0444, "adlink%d", device_get_unit(self));
  472         sc->dev->si_drv1 = sc;
  473 
  474         return (0);
  475 }
  476 
  477 static device_method_t adlink_methods[] = {
  478         /* Device interface */
  479         DEVMETHOD(device_probe,         adlink_probe),
  480         DEVMETHOD(device_attach,        adlink_attach),
  481         DEVMETHOD(device_suspend,       bus_generic_suspend),
  482         DEVMETHOD(device_resume,        bus_generic_resume),
  483         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
  484         {0, 0}
  485 };
  486  
  487 static driver_t adlink_driver = {
  488         "adlink",
  489         adlink_methods,
  490         sizeof(struct softc)
  491 };
  492 
  493 DRIVER_MODULE(adlink, pci, adlink_driver, adlink_devclass, 0, 0);
  494 #endif /* _KERNEL */

Cache object: 37e1ddba7601d9b5f3ddafe782982ec6


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.