Annotation of margi2/margi.c, revision 1.13

1.1       cvs         1: /* 
                      2:     margi.c
                      3: 
                      4:     Copyright (C) Marcus Metzler for convergence integrated media.
                      5: 
                      6:     This program is free software; you can redistribute it and/or modify
                      7:     it under the terms of the GNU General Public License as published by
                      8:     the Free Software Foundation; either version 2 of the License, or
                      9:     (at your option) any later version.
                     10: 
                     11:     This program is distributed in the hope that it will be useful,
                     12:     but WITHOUT ANY WARRANTY; without even the implied warranty of
                     13:     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     14:     GNU General Public License for more details.
                     15: 
                     16:     You should have received a copy of the GNU General Public License
                     17:     along with this program; if not, write to the Free Software
                     18:     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
                     19: */
                     20: 
                     21: #include "margi.h"
                     22: 
                     23: #include <pcmcia/version.h>
                     24: #include <pcmcia/cs_types.h>
                     25: #include <pcmcia/cs.h>
                     26: #include <pcmcia/cistpl.h>
                     27: #include <pcmcia/cisreg.h>
                     28: #include <pcmcia/bus_ops.h>
                     29: #include <pcmcia/ds.h>
                     30: 
                     31: 
                     32: 
                     33: #include "l64014.h"
                     34: #include "l64021.h"
                     35: #include "i2c.h"
                     36: #include "decoder.h"
                     37: #include "dram.h"
                     38: #include "video.h"
                     39: #include "cvdv.h"
                     40: 
1.13    ! mocm       41: 
        !            42: static char *version = "margi_cs.c 0.5 11/1/2000 (Marcus Metzler)";
        !            43: 
1.1       cvs        44: //#define USE_BH 1
                     45: #ifdef USE_BH
                     46: #define MARGI_BH 31
                     47: // shouldn't be a number, but then MARGI_BH must be entered into interrupt.h
                     48: #endif
                     49: 
                     50: MODULE_AUTHOR(AUTHOR);
                     51: MODULE_DESCRIPTION(MEDDEVNAME " Driver V." DVERSION);
                     52: 
                     53: #define MAX_DEV 4
                     54: #define DEVICE_NR(minor)       ((minor)>>4)
                     55: 
                     56: /*====================================================================*/
                     57: 
                     58: /* Parameters that can be set with 'insmod' */
                     59: 
                     60: /* Release IO ports after configuration? */
                     61: static int free_ports = 0;
                     62: 
                     63: /* The old way: bit map of interrupts to choose from */
                     64: /* This means pick from 15, 14, 12, 11, 10, 9, 7, 5, 4, and 3 */
                     65: static u_int irq_mask = 0xdeb8;
                     66: /* Newer, simpler way of listing specific interrupts */
                     67: static int irq_list[4] = { -1 };
                     68: 
                     69: MODULE_PARM(free_ports, "i");
                     70: MODULE_PARM(irq_mask, "i");
                     71: MODULE_PARM(irq_list, "1-4i");
                     72: 
                     73: extern unsigned int major_device_number;
                     74: extern struct file_operations cvdv_fileops;
                     75: 
                     76: typedef struct margi_info_t {
                     77:        dev_link_t link;
                     78:        dev_node_t node;
                     79:        struct cvdv_cards card;
                     80:        int stop;
                     81: } margi_info_t;
                     82: 
                     83: 
                     84: 
                     85: /*
                     86:    The event() function is this driver's Card Services event handler.
                     87:    It will be called by Card Services when an appropriate card status
                     88:    event is received.  The config() and release() entry points are
                     89:    used to configure or release a socket, in response to card
                     90:    insertion and ejection events.  They are invoked from the margi
                     91:    event handler. 
                     92: */
                     93: 
                     94: static void margi_config(dev_link_t * link);
                     95: static void margi_release(u_long arg);
                     96: static int margi_event(event_t event, int priority,
                     97:                       event_callback_args_t * args);
                     98: /*
                     99:    The attach() and detach() entry points are used to create and destroy
                    100:    "instances" of the driver, where each instance represents everything
                    101:    needed to manage one actual PCMCIA card.
                    102: */
                    103: 
                    104: static dev_link_t *margi_attach(void);
                    105: static void margi_detach(dev_link_t *);
                    106: static u_char read_lsi_status(struct cvdv_cards *card);
                    107: 
                    108: /*
                    109:    You'll also need to prototype all the functions that will actually
                    110:    be used to talk to your device.  See 'memory_cs' for a good example
                    111:    of a fully self-sufficient driver; the other drivers rely more or
                    112:    less on other parts of the kernel.
                    113: */
                    114: 
                    115: /*
                    116:    The dev_info variable is the "key" that is used to match up this
                    117:    device driver with appropriate cards, through the card configuration
                    118:    database.
                    119: */
                    120: 
                    121: static dev_link_t *dev_table[MAX_DEV] = { NULL, /* ... */  };
                    122: 
                    123: static dev_info_t dev_info = "margi_cs";
                    124: 
                    125: /*
                    126:    A linked list of "instances" of the margi device.  Each actual
                    127:    PCMCIA card corresponds to one device instance, and is described
                    128:    by one dev_link_t structure (defined in ds.h).
                    129: 
                    130:    You may not want to use a linked list for this -- for example, the
                    131:    memory card driver uses an array of dev_link_t pointers, where minor
                    132:    device numbers are used to derive the corresponding array index.
                    133: */
                    134: 
                    135: static dev_link_t *dev_list = NULL;
                    136: 
                    137: /*
                    138:    A dev_link_t structure has fields for most things that are needed
                    139:    to keep track of a socket, but there will usually be some device
                    140:    specific information that also needs to be kept track of.  The
                    141:    'priv' pointer in a dev_link_t structure can be used to point to
                    142:    a device-specific private data structure, like this.
                    143: 
                    144:    To simplify the data structure handling, we actually include the
                    145:    dev_link_t structure in the device's private data structure.
                    146: 
                    147:    A driver needs to provide a dev_node_t structure for each device
                    148:    on a card.  In some cases, there is only one device per card (for
                    149:    example, ethernet cards, modems).  In other cases, there may be
                    150:    many actual or logical devices (SCSI adapters, memory cards with
                    151:    multiple partitions).  The dev_node_t structures need to be kept
                    152:    in a linked list starting at the 'dev' field of a dev_link_t
                    153:    structure.  We allocate them in the card's private data structure,
                    154:    because they generally shouldn't be allocated dynamically.
                    155: 
                    156:    In this case, we also provide a flag to indicate if a device is
                    157:    "stopped" due to a power management event, or card ejection.  The
                    158:    device IO routines can use a flag like this to throttle IO to a
                    159:    card that is not ready to accept it.
                    160: 
                    161:    The bus_operations pointer is used on platforms for which we need
                    162:    to use special socket-specific versions of normal IO primitives
                    163:    (inb, outb, readb, writeb, etc) for card IO.
                    164: */
                    165: 
                    166: void DACSetFrequency(struct cvdv_cards *card, int khz, int multiple) {
1.11      mocm      167:        uint8_t b =     read_indexed_register(card, IIO_OSC_AUD);
1.1       cvs       168: 
                    169:        b &= 0xf8;
                    170: 
                    171:        switch (khz){
1.2       rjkm      172:        case 32:
                    173:                b |= 0x04;
                    174:                break;
1.1       cvs       175:        case 48:
                    176:                b |= 0x00;
                    177:                break;
                    178:        case 44:
                    179:                b |= 0x01;
                    180:                break;
                    181:        case 96:
                    182:                b |= 0x02;
                    183:                break;
                    184:        default:
                    185:                b |= 0x00;
                    186:                break;
                    187:        }
                    188:        write_indexed_register(card, IIO_OSC_AUD, b);
                    189: 
                    190: }
                    191: 
                    192: int MargiFreeBuffers(struct cvdv_cards *card)
                    193: {
1.13    ! mocm      194:        MDEBUG(1, ": -- MargiFreeBuffers\n");
1.1       cvs       195:        
1.13    ! mocm      196:        ring_destroy(&(card->rbufA));
        !           197:        card->use_ringA = 0;
        !           198:        ring_destroy(&(card->rbufB));
        !           199:        card->use_ringB = 0;
1.1       cvs       200:        return 0;
                    201: }
                    202: 
                    203: 
1.13    ! mocm      204: int MargiSetABuffers(struct cvdv_cards *card, uint32_t size)
1.1       cvs       205: {
                    206:        MargiFreeBuffers(card);
1.13    ! mocm      207:        MDEBUG(1, ": -- MargiSetABuffers(%d)\n",
1.1       cvs       208:               size);
                    209: 
1.13    ! mocm      210:        ring_init(&(card->rbufA),size);
        !           211:        card->use_ringA = 1;
1.1       cvs       212:        return 0;
1.13    ! mocm      213: }
1.1       cvs       214: 
1.13    ! mocm      215: int MargiSetBBuffers(struct cvdv_cards *card, uint32_t size)
        !           216: {
        !           217:        MargiFreeBuffers(card);
        !           218:        MDEBUG(1, ": -- MargiSetBBuffers(%d)\n",
        !           219:               size);
        !           220: 
        !           221:        ring_init(&(card->rbufB),size);
        !           222:        card->use_ringB = 1;
        !           223:        return 0;
1.1       cvs       224: }
                    225: 
                    226: int MargiFlush (struct cvdv_cards *card)
                    227: {
                    228:        int co = 0;
                    229:        int i;
1.13    ! mocm      230:        for (i=0;i<100;i++){
        !           231:                MargiPushA(card, 32, FlushPacket);
        !           232:                MargiPushB(card, 32, FlushPacket);
        !           233:        }
        !           234:        while ( (ring_write_rest(&(card->rbufA))|| ring_write_rest(&(card->rbufB)))  && co<100) 
        !           235:                co++;
1.1       cvs       236:        VideoSetBackground(card, 1, 0, 0, 0);   // black
                    237: 
1.13    ! mocm      238:        if (card->use_ringA) ring_flush(&(card->rbufA));
        !           239:        if (card->use_ringB) ring_flush(&(card->rbufB));
1.1       cvs       240:        card->DMAABusy = 0;
1.13    ! mocm      241:        card->DMABBusy = 0;
1.1       cvs       242: 
                    243: 
                    244:        DecoderStopChannel(card);
                    245:        DecoderStreamReset(card);
                    246:        DecoderSetupReset(card);
                    247:        card->channelrun = 0;
                    248: 
1.13    ! mocm      249:        MDEBUG(1, ": Margi Flush \n");
1.1       cvs       250:        return 0;
                    251: }
                    252: 
                    253: 
1.13    ! mocm      254: int MargiPushA(struct cvdv_cards *card, int count, const char *data)
1.1       cvs       255: {
1.11      mocm      256:        int fill;
                    257:   
1.13    ! mocm      258:        fill =  ring_read_rest(&(card->rbufA));
1.11      mocm      259: 
1.13    ! mocm      260:        if (!card->use_ringA)
1.1       cvs       261:                return 0;
                    262: 
1.13    ! mocm      263:        if (fill > 3*card->rbufA.size/4 && !card->channelrun){
1.1       cvs       264:                DecoderStartChannel(card);
1.11      mocm      265:                card->DMAABusy = 1;
1.1       cvs       266:        }
                    267: 
1.13    ! mocm      268:        count = ring_write(&(card->rbufA),data,count);
        !           269:        
        !           270:        return count;
        !           271: }
        !           272: 
        !           273: int MargiPushB(struct cvdv_cards *card, int count, const char *data)
        !           274: {
        !           275:        int fill;
        !           276:   
        !           277:        fill =  ring_read_rest(&(card->rbufB));
        !           278: 
        !           279:        if (!card->use_ringB)
        !           280:                return 0;
        !           281: 
        !           282:        if (fill > 3*card->rbufB.size/4 && !card->channelrun){
        !           283:                DecoderStartChannel(card);
        !           284:                card->DMABBusy = 1;
        !           285:        }
        !           286: 
        !           287:        count = ring_write(&(card->rbufB),data,count);
1.11      mocm      288:        
1.1       cvs       289:        return count;
                    290: }
                    291: 
                    292: int DecoderStartChannel(struct cvdv_cards *card)
                    293: {
                    294:        DecoderMaskByte(card, 0x007, 0xC3, 0xC3);       // channel start
1.11      mocm      295: 
1.1       cvs       296: #ifdef BYPASS 
                    297:        DecoderMaskByte(card,0x005,0x0F,0x08);
                    298: #else
                    299:        DecoderMaskByte(card,0x005,0x0F,0x01);
                    300: #endif
                    301:        card->channelrun = 1;
                    302:        return 0;
                    303: }
                    304: 
                    305: int DecoderStopChannel(struct cvdv_cards *card)
                    306: {
                    307:        DecoderMaskByte(card, 0x007, 0xC3, 0xC2);       // channel reset
                    308:        DecoderSetByte(card, 0x005, 0x04);      // channel pause
                    309:        card->channelrun = 0;
                    310:        return 0;
                    311: }
                    312: 
1.11      mocm      313: uint32_t DecoderGetAudioBufferSpace(struct cvdv_cards *card)
1.1       cvs       314: {
                    315: 
1.11      mocm      316:        uint32_t MaxSize, Size;
1.1       cvs       317: 
                    318:        MaxSize = card->AudioESSize;
                    319:        Size = DecoderGetAudioESLevel(card);
                    320: 
                    321:        if (Size>MaxSize)
                    322:          return 0;
                    323:        return (MaxSize - Size);
                    324: 
                    325: }
                    326: 
1.11      mocm      327: uint32_t DecoderGetVideoBufferSpace(struct cvdv_cards *card)
1.1       cvs       328: {
                    329: 
1.11      mocm      330:        uint32_t MaxSize, Size;
1.1       cvs       331: 
                    332:        MaxSize = card->VideoESSize;
                    333:        Size = DecoderGetVideoESLevel(card);
                    334: 
                    335:        if (Size>MaxSize)
                    336:          return 0;
                    337:        return (MaxSize - Size);
                    338: 
                    339: }
                    340: 
1.11      mocm      341: uint32_t DecoderGetBufferSpace(struct cvdv_cards *card)
1.1       cvs       342: {
1.11      mocm      343:        uint32_t audio,video;
1.1       cvs       344:        
                    345:        audio = DecoderGetAudioBufferSpace(card);
                    346:        video = DecoderGetVideoBufferSpace(card);
                    347: 
1.10      mocm      348:        if (audio > 2048) audio -= 2048;
1.11      mocm      349:        if (video > 2048) video -= 2048;
1.1       cvs       350: 
                    351:        if (audio < video) return audio;
                    352:        return video;
                    353: }
                    354: 
1.13    ! mocm      355: 
        !           356: static int ringDMA_PES (struct cvdv_cards *card){
        !           357:        
1.11      mocm      358:        uint32_t size = 0;
1.7       mocm      359:        u_char stat;
                    360:        dev_link_t *link = &(((margi_info_t *) card->margi)->link);
1.13    ! mocm      361:        uint32_t count=0;
1.11      mocm      362:        uint8_t data;
1.7       mocm      363:        
1.13    ! mocm      364:        return 0;
        !           365: }
1.7       mocm      366: 
                    367: 
                    368: 
1.1       cvs       369: static int ringDMA (struct cvdv_cards *card){
                    370:        
1.11      mocm      371:        uint32_t size = 0;
1.1       cvs       372:        u_char stat;
                    373:        dev_link_t *link = &(((margi_info_t *) card->margi)->link);
1.11      mocm      374:        uint32_t count=0;
                    375:        uint8_t data;
1.10      mocm      376: 
1.13    ! mocm      377:        count = ring_read_rest(&(card->rbufA));
1.4       rjkm      378:        if (count < 2048) {
1.1       cvs       379:                wake_up_interruptible(&(card->wqA));
                    380:                return 0;
1.4       rjkm      381:        }
1.10      mocm      382:        
1.1       cvs       383:        stat = read_lsi_status(card);
1.10      mocm      384:        
1.13    ! mocm      385:        MDEBUG( 3, ": -- stat: %d  readpos: %d writepos: %d \n",
        !           386:               stat,(int) card->rbufA.read_pos,(int) card->rbufA.write_pos);
1.1       cvs       387:        if (stat & LSI_ARQ) {
                    388:                stat = read_lsi_status(card);
                    389:        }
                    390: 
                    391:        if (stat & LSI_READY){
                    392:                data = read_indexed_register(card, IIO_LSI_CONTROL);
                    393:                data |= RR;
                    394:                write_indexed_register(card, IIO_LSI_CONTROL, data);
                    395:                return 0;
                    396:        }
                    397: 
                    398:        if ((stat & LSI_ARQ) == 0) {
                    399:                size = DecoderGetBufferSpace(card);
                    400:                if (count > size) count = size & 0xfffffffc;
                    401:                if (count>=2048) count &=0xfffff800;
                    402:                count &=0xfffffffc;
1.5       mocm      403:               
1.4       rjkm      404:                if (!size &&
1.5       mocm      405:                    !card->DecoderOpen  ){
1.11      mocm      406:                        printk(KERN_ERR LOGNAME 
1.4       rjkm      407:                               ": -- PREPARE IT ALREADY\n");
                    408:                        Prepare(card);
1.5       mocm      409:                        card->startingV = 1;
                    410:                        card->startingA = 1;
1.4       rjkm      411:                }
1.11      mocm      412:                
1.5       mocm      413:                if (count > size) count = size & 0xfffffffc;
1.13    ! mocm      414:                MDEBUG(3,": -- stat: %d  length: %d size: %d startV: %d startA: %d\n",
1.4       rjkm      415:                       stat,count,size, card->startingV, card->startingA);
1.11      mocm      416: 
1.1       cvs       417:                if (count) {
1.13    ! mocm      418:                        ring_read_direct(&(card->rbufA),
1.1       cvs       419:                                         link->io.BasePort1+DIO_LSI_STATUS, 
                    420:                                         count);
                    421:                    }
                    422:        } else {
                    423:                count = 0;
                    424:        }
                    425: 
                    426:        return count;
                    427: }
                    428: 
                    429: 
                    430: u_char read_indexed_register(struct cvdv_cards * card, int addr)
                    431: {
                    432:        dev_link_t *link = &(((margi_info_t *) card->margi)->link);
1.10      mocm      433:        u_char data;
                    434: #ifdef NOINT
                    435:        spin_lock(&card->timelock);
                    436: #endif
1.1       cvs       437:        outb(addr, link->io.BasePort1 + DIO_CONTROL_INDEX);
1.10      mocm      438:        data = (inb(link->io.BasePort1 + DIO_CONTROL_DATA));
                    439: #ifdef NOINT
                    440:        spin_unlock(&card->timelock);
                    441: #endif 
                    442:        return data;
1.1       cvs       443: }
                    444: 
                    445: 
                    446: void write_indexed_register(struct cvdv_cards *card, int addr, u_char data)
                    447: {
                    448:        dev_link_t *link = &(((margi_info_t *) card->margi)->link);
1.10      mocm      449: #ifdef NOINT
                    450:        spin_lock(&card->timelock);
                    451: #endif
1.1       cvs       452:        outb(addr, link->io.BasePort1 + DIO_CONTROL_INDEX);
                    453:        outb(data, link->io.BasePort1 + DIO_CONTROL_DATA);
1.10      mocm      454: 
                    455: #ifdef NOINT
                    456:        spin_unlock(&card->timelock);
                    457: #endif
1.1       cvs       458: }
                    459: 
                    460: void WriteByte(struct cvdv_cards *card, int addr, u_char data)
                    461: {
                    462:        dev_link_t *link = &(((margi_info_t *) card->margi)->link);
                    463: 
1.10      mocm      464: #ifdef NOINT
                    465:        spin_lock(&card->timelock);
                    466: #endif
1.1       cvs       467:        outb((u_char) (addr & 255),
                    468:             link->io.BasePort1 + DIO_LSI_INDEX_LOW);
                    469:        outb(((addr & 256) ? 1 : 0),
                    470:             link->io.BasePort1 + DIO_LSI_INDEX_HIGH);
                    471:        outb(data, link->io.BasePort1 + DIO_LSI_DATA);
1.10      mocm      472: #ifdef NOINT
                    473:        spin_unlock(&card->timelock);
                    474: #endif
1.1       cvs       475: }
                    476: 
                    477: u_char ReadByte(struct cvdv_cards *card, int addr)
                    478: {
                    479:        dev_link_t *link = &(((margi_info_t *) card->margi)->link);
1.10      mocm      480:        u_char data;
1.1       cvs       481: 
1.10      mocm      482: #ifdef NOINT
                    483:        spin_lock(&card->timelock);
                    484: #endif
1.1       cvs       485:        outb((u_char) (addr & 255),
                    486:             link->io.BasePort1 + DIO_LSI_INDEX_LOW);
                    487:        outb(((addr & 256) ? 1 : 0),
                    488:             link->io.BasePort1 + DIO_LSI_INDEX_HIGH);
1.10      mocm      489:        data = inb(link->io.BasePort1 + DIO_LSI_DATA);
                    490: #ifdef NOINT
                    491:        spin_unlock(&card->timelock);
                    492: #endif
                    493:        return data;
1.1       cvs       494: }
                    495: 
                    496: void MaskByte(struct cvdv_cards *card, int addr, u_char mask, u_char bits)
                    497: {
                    498:        WriteByte(card, addr, (ReadByte(card, addr) & ~(mask)) | (bits));
                    499: }
                    500: 
                    501: 
                    502: 
1.11      mocm      503: #define MAXWRITE CHANNELBUFFERSIZE
                    504: #define MAX_COUNT 10
1.1       cvs       505: 
                    506: #ifdef USE_BH
                    507: struct cvdv_cards *bh_card;
                    508: 
                    509: static void do_margi_bh(void)
                    510: {
                    511:        struct cvdv_cards *card = bh_card;
                    512: #else
                    513: 
                    514: static void do_margi(struct cvdv_cards *card)
                    515: {
                    516: 
                    517: #endif
                    518:        int countA, countB;
                    519:        int try;
1.13    ! mocm      520:        int stype = card->setup.streamtype;
1.1       cvs       521: 
                    522:        countA = 0;
                    523:        countB = 0;
                    524: 
                    525:        card->currentType = 0;
                    526:        for ( try = 0; try < MAX_COUNT ;try++)
                    527:                if (countA < MAXWRITE){
                    528:                        int count = 0;
1.13    ! mocm      529:                        switch (stype){
        !           530:                        case stream_PES:
        !           531:                        case stream_ES:
        !           532: //                             count = ringDMA_PES(card);
        !           533:                                count = ringDMA(card);
        !           534:                                countA += count;
        !           535:                                if (!count) 
        !           536:                                        try=MAX_COUNT;                  
        !           537:                                break;
        !           538:                        case stream_PS:
        !           539:                        case stream_DVD:
        !           540:                                count = ringDMA(card);
        !           541:                                countA += count;
        !           542:                                if (!count) 
        !           543:                                        try=MAX_COUNT;
        !           544:                                break;
        !           545:                        }
1.1       cvs       546:                } else break;
                    547: 
                    548: }
                    549: 
1.7       mocm      550: 
                    551: 
                    552: 
                    553: void L64014Intr_function(struct cvdv_cards *card)
1.1       cvs       554: {
1.11      mocm      555:        uint8_t control,mask,stat;
1.1       cvs       556:        int try;
                    557: 
1.13    ! mocm      558: 
1.1       cvs       559:        control= read_indexed_register(card, IIO_IRQ_CONTROL);
                    560:        if (control & IRQ_EN){
                    561:                mask = 0;
                    562:                if ( control & DEC_EN ) mask |= DEC_INT;
                    563:                if ( control & VSYNC_EN ) mask |= VSYNC_INT;
                    564:                stat = read_indexed_register(card, IIO_IRQ_STATUS);
                    565:                try = 0;
                    566:                while ( (try++ < 100) && (stat & mask) ){                     
1.11      mocm      567:                
                    568:                  if (stat & VSYNC_INT) {
                    569:        
1.1       cvs       570:                                write_indexed_register(card,IIO_IRQ_CONTROL,
                    571:                                                       control & (~VSYNC_EN));
                    572:                                write_indexed_register(card,IIO_IRQ_CONTROL,
                    573:                                                       control);
1.10      mocm      574: 
                    575: 
1.13    ! mocm      576:                                if (card->DMAABusy || card->DMABBusy){
1.1       cvs       577: 
                    578: #ifdef USE_BH
                    579:                                        bh_card = card;
                    580:                                        mark_bh(MARGI_BH);
                    581: #else 
                    582:                                        do_margi(card);
                    583: #endif
1.13    ! mocm      584:                                        if(card->use_ringA || card->use_ringB){
1.11      mocm      585:                                          L64021Intr(card);
                    586:                                        }
1.13    ! mocm      587:                                } else  {
        !           588:                                        wake_up_interruptible(&(card->wqA));
        !           589:                                        wake_up_interruptible(&(card->wqB));
1.11      mocm      590:                                }
                    591:                        }
                    592: 
                    593:                        if (stat & DEC_INT) {
                    594:                                write_indexed_register(card,IIO_IRQ_CONTROL,
                    595:                                                       control & (~DEC_EN));
                    596:                                write_indexed_register(card,IIO_IRQ_CONTROL,
                    597:                                                       control);
                    598:                                
1.13    ! mocm      599:                                if(card->use_ringA || card->use_ringB){
1.11      mocm      600:                                        L64021Intr(card);
                    601:                                }
1.1       cvs       602:                        }
                    603: 
                    604:                        stat = read_indexed_register(card, IIO_IRQ_STATUS);
                    605:                }
                    606:        }
                    607: 
1.7       mocm      608: }
                    609: 
                    610: 
                    611: #ifdef NOINT
                    612: void Timerfunction(unsigned long data)
                    613: {
                    614:        struct cvdv_cards *card = (struct cvdv_cards *) data;
                    615: 
1.10      mocm      616: 
1.7       mocm      617:        L64014Intr_function(card);
                    618: 
                    619:        card->timer.function = Timerfunction;
                    620:        card->timer.data=(unsigned long) card;
                    621:        card->timer.expires=jiffies+1;
1.13    ! mocm      622:        if ( card->open)
1.10      mocm      623:                add_timer(&card->timer);
1.7       mocm      624: 
                    625: }
                    626: #endif
                    627: 
                    628: 
                    629: void L64014Intr(int irq, void *dev_id, struct pt_regs *regs)
                    630: {
                    631:        margi_info_t *margi = dev_id;
                    632:        struct cvdv_cards *card = &(margi->card);
                    633:        u_char dio_index, lsi_index_low, lsi_index_high;
                    634: 
1.10      mocm      635: #ifdef NOINT
                    636:        spin_lock(&card->timelock);
                    637: #endif
1.7       mocm      638:        //save registers
                    639:        dio_index = inb(margi->link.io.BasePort1 + DIO_CONTROL_INDEX);
                    640:        lsi_index_low = inb(margi->link.io.BasePort1 + DIO_LSI_INDEX_LOW);
1.10      mocm      641:        lsi_index_high = inb(margi->link.io.BasePort1 + DIO_LSI_INDEX_HIGH);
1.7       mocm      642:        
                    643: 
                    644:        L64014Intr_function(card);
                    645: 
1.1       cvs       646:        //load registers
                    647:        outb(dio_index, margi->link.io.BasePort1 + DIO_CONTROL_INDEX);
                    648:        outb(lsi_index_low, margi->link.io.BasePort1 + DIO_LSI_INDEX_LOW);
                    649:        outb(lsi_index_high,margi->link.io.BasePort1 + DIO_LSI_INDEX_HIGH);
1.10      mocm      650: #ifdef NOINT
                    651:        spin_unlock(&card->timelock);
                    652: #endif
1.1       cvs       653: }
                    654: 
                    655: int L64014RemoveIntr(struct cvdv_cards *card)
                    656: {
1.13    ! mocm      657:        MDEBUG(1, ": -- L64014RemoveIntr\n");
1.1       cvs       658:        // Disable the IRQ's
                    659:        write_indexed_register(card, IIO_IRQ_CONTROL, 0x00);
                    660:        if (!card->IntInstalled)
                    661:                return 1;
                    662:        L64021RemoveIntr(card);
                    663:        return 0;
                    664: }
                    665: 
                    666: void l64020Reset(struct cvdv_cards *card){
1.11      mocm      667:        uint8_t data;
1.1       cvs       668:        
                    669:        
                    670:        data = read_indexed_register(card, IIO_LSI_CONTROL);
                    671:        data &= ~(RR | DR);
                    672:        write_indexed_register(card, IIO_LSI_CONTROL, data);
                    673:        mdelay(100);
                    674:        data = read_indexed_register(card, IIO_LSI_CONTROL);
                    675:        data |= DR;
                    676:        write_indexed_register(card, IIO_LSI_CONTROL, data);
                    677: 
                    678:        data = read_indexed_register(card,IIO_GPIO_PINS);
                    679:        data &= ~0x01;
                    680:        write_indexed_register(card,IIO_GPIO_PINS,data);
                    681:        data |= 0x01;
                    682:        write_indexed_register(card,IIO_GPIO_PINS,data);
                    683:        
                    684:        //write_indexed_register(card, IIO_LSI_CONTROL, DR);
                    685: }
                    686: 
1.7       mocm      687: void ZV_init(struct cvdv_cards *card)
                    688: {
1.11      mocm      689:        uint32_t delay, activel;
                    690:        uint8_t reg;
1.7       mocm      691:        delay = 235;
                    692:        activel = delay + 1448;
                    693:        
                    694:        // init delay and active lines
                    695:        write_indexed_register(card, IIO_VIDEO_HOR_DELAY, 
1.11      mocm      696:                               (uint8_t)(delay & 0x00FF));
1.7       mocm      697:        write_indexed_register(card, IIO_VIDEO_HOR_ACTIVE, 
1.11      mocm      698:                               (uint8_t)(activel & 0x00FF));      
                    699:        reg = ((uint8_t)((activel >> 4) & 0x0070))|((uint8_t)((delay >> 8) & 0x0007));
1.7       mocm      700:        write_indexed_register(card, IIO_VIDEO_HOR_HIGH, reg);
                    701: 
                    702:        //init video
                    703:        reg = read_indexed_register(card, IIO_VIDEO_CONTROL0);
                    704:        reg |= (ZVCLK13 | ZV16BIT | ZVCLKINV);
                    705:        write_indexed_register(card, IIO_VIDEO_CONTROL0, reg);
                    706:        reg = read_indexed_register(card, IIO_VIDEO_CONTROL1);
                    707:        reg |= (ZV_OVERRIDE | ZV_ENABLE);
                    708:        write_indexed_register(card, IIO_VIDEO_CONTROL1, reg);
                    709: }
                    710: 
1.1       cvs       711: int L64014Init(struct cvdv_cards *card)
                    712: {
1.11      mocm      713:        uint16_t testram[16];
1.1       cvs       714:        int i, err;
                    715: 
1.13    ! mocm      716:        MDEBUG(1, ": -- L64014Init\n");
1.1       cvs       717:        card->videomode = VIDEO_MODE;
                    718: 
                    719:        /* Reset 64020 */
                    720:        write_indexed_register(card, IIO_GPIO_CONTROL, 0x01);
                    721:        l64020Reset(card);
                    722:        /* init GPIO */
                    723:        write_indexed_register(card, IIO_GPIO_CONTROL, 0x01);
                    724:        write_indexed_register(card, IIO_GPIO_PINS, 0xff);
                    725: 
                    726:        /* Set to PAL */
                    727:        write_indexed_register(card, IIO_VIDEO_CONTROL0, 0);
                    728:        write_indexed_register(card, IIO_VIDEO_CONTROL1, VMS_PAL);
                    729: 
                    730:        /* Set Audio freq */
                    731:        write_indexed_register(card, IIO_OSC_AUD, 0x12);
                    732: 
                    733:        write_indexed_register(card, CSS_COMMAND, 0x01);
                    734: 
                    735: 
1.13    ! mocm      736:        MDEBUG(0, "CSID: %02x\n", I2CRead(card, 0, 0x3d));
1.1       cvs       737:        card->i2c_addr = I2CRead(card, 0, 0x0f);
1.13    ! mocm      738:        MDEBUG(0, "I2CADDR: %02x\n", card->i2c_addr);
1.1       cvs       739: 
                    740:        I2CWrite(card, card->i2c_addr, CS_CONTROL0, 0x4a);
                    741:        I2CWrite(card, card->i2c_addr, CS_CONTROL1, 0x04);
                    742:        I2CWrite(card, card->i2c_addr, CS_SC_AMP, 0x15);
                    743:        I2CWrite(card, card->i2c_addr, CS_SC_SYNTH0, 0x96);
                    744:        I2CWrite(card, card->i2c_addr, CS_SC_SYNTH1, 0x15);
                    745:        I2CWrite(card, card->i2c_addr, CS_SC_SYNTH2, 0x13);
                    746:        I2CWrite(card, card->i2c_addr, CS_SC_SYNTH3, 0x54);
                    747: 
                    748:        I2CWrite(card, card->i2c_addr, CS_DAC, 0x87);
                    749:        I2CWrite(card, card->i2c_addr, CS_BKG_COL, 0x03);
                    750: 
1.13    ! mocm      751:        MDEBUG(0,"Decoder Status: %d\n", read_lsi_status(card));
        !           752:        MDEBUG(0,"lsi stat %d\n", DecoderReadByte(card, 0x005));
1.1       cvs       753: 
1.7       mocm      754: #ifdef USE_ZV
                    755:        ZV_init(card);
                    756: #endif
1.1       cvs       757:        L64021Init(card);
                    758: 
                    759:        // Find out how much DRAM we have
1.5       mocm      760:        card->DRAMSize = 0x00100000;    // maximum size
1.1       cvs       761:        do {
1.13    ! mocm      762:                MDEBUG(0,
1.1       cvs       763:                       ": Probing DRAM Size: 0x%08X (%d kByte) ... ",
                    764:                       card->DRAMSize, card->DRAMSize / 512);
                    765:                for (i = 0; i < 8; i++)
                    766:                        testram[i] = rnd(0x100) | (rnd(0x100) << 8);
                    767:                if (DRAMWriteWord(card, 0, 4, &testram[0], 0))
1.13    ! mocm      768:                        MDEBUG(0, ": DRAM Write error.\n");
1.1       cvs       769:                if (DRAMWriteWord
                    770:                    (card, card->DRAMSize - 4, 4, &testram[4],
1.13    ! mocm      771:                     0)) MDEBUG(0,
1.1       cvs       772:                                ": DRAM Write error.\n");
                    773:                if (DRAMReadWord(card, 0, 4, &testram[8], 0))
1.13    ! mocm      774:                        MDEBUG(0, ": DRAM Read error.\n");
1.1       cvs       775:                if (DRAMReadWord
                    776:                    (card, card->DRAMSize - 4, 4, &testram[12],
1.13    ! mocm      777:                     0)) MDEBUG(0, ": DRAM Read error.\n");
1.1       cvs       778:                err = 0;
                    779:                for (i = 0; (!err) && (i < 8); i++)
                    780:                        if (testram[i] != testram[i + 8])
                    781:                                err = i + 1;
1.13    ! mocm      782:                if (err) {
        !           783:                        MDEBUG(0," failed\n");
        !           784:                } else {
        !           785:                        MDEBUG(0," ok\n");
        !           786:                }
1.1       cvs       787:                if (err)
1.13    ! mocm      788:                        MDEBUG(2,": DRAM compare error at cell %d: 0x%04X %04X %04X %04X->0x%04X %04X %04X %04X / 0x%04X %04X %04X %04X->0x%04X %04X %04X %04X\n",
1.1       cvs       789:                               err, testram[0], testram[1], testram[2],
                    790:                               testram[3], testram[8], testram[9],
                    791:                               testram[10], testram[11], testram[4],
                    792:                               testram[5], testram[6], testram[7],
                    793:                               testram[12], testram[13], testram[14],
                    794:                               testram[15]);
                    795:                if (err)
                    796:                        card->DRAMSize >>= 1;
                    797:        } while (err && (card->DRAMSize >= 0x00100000));
                    798:        printk(KERN_INFO LOGNAME ": DRAM Size: 0x%08X (%d kByte)\n",
                    799:               card->DRAMSize, card->DRAMSize / 512);
                    800:        if (card->DRAMSize < 0x00100000) {      // minimum size
                    801:                printk(KERN_INFO LOGNAME
                    802:                       ": DRAM ERROR: Not enough memory on card!\n");
                    803:                return 1;
                    804:        }
                    805:        return 0;
                    806: }
                    807: 
                    808: 
                    809: void CardDeInit(struct cvdv_cards *card)
                    810: {
                    811:        CloseCard(card);
                    812:        MargiFlush(card);
                    813:        MargiFreeBuffers(card);
1.7       mocm      814: 
1.1       cvs       815:        L64014RemoveIntr(card);
1.4       rjkm      816:        card_init(card, 0);
1.1       cvs       817: }
                    818: 
                    819: 
                    820: static u_char read_lsi_status(struct cvdv_cards *card)
                    821: {
                    822:        margi_info_t *margi = (margi_info_t *) card->margi;
                    823:        return (inb(margi->link.io.BasePort1 + DIO_LSI_STATUS) & 15);
                    824: 
                    825: }
                    826: 
                    827: /*====================================================================*/
                    828: 
                    829: static void cs_error(client_handle_t handle, int func, int ret)
                    830: {
                    831:        error_info_t err = { func, ret };
                    832:        CardServices(ReportError, handle, &err);
                    833: }
                    834: 
                    835: /*======================================================================
                    836: 
                    837:     margi_attach() creates an "instance" of the driver, allocating
                    838:     local data structures for one device.  The device is registered
                    839:     with Card Services.
                    840: 
                    841:     The dev_link structure is initialized, but we don't actually
                    842:     configure the card at this point -- we wait until we receive a
                    843:     card insertion event.
                    844:     
                    845: ======================================================================*/
                    846: 
                    847: static dev_link_t *margi_attach(void)
                    848: {
                    849:        margi_info_t *local;
                    850:        dev_link_t *link;
                    851:        client_reg_t client_reg;
                    852:        int ret, i;
                    853: 
1.13    ! mocm      854:        MDEBUG(0, "margi_attach()\n");
1.1       cvs       855: 
                    856:        for (i = 0; i < MAX_DEV; i++)
                    857:                if (dev_table[i] == NULL)
                    858:                        break;
                    859:        if (i == MAX_DEV) {
                    860:                printk(KERN_NOTICE "margi_cs: no devices available\n");
                    861:                return NULL;
                    862:        }
                    863: 
                    864:        /* Allocate space for private device-specific data */
                    865:        local = kmalloc(sizeof(margi_info_t), GFP_KERNEL);
                    866:        if (!local)
                    867:                return NULL;
                    868:        memset(local, 0, sizeof(margi_info_t));
                    869:        link = &local->link;
                    870:        link->priv = local;
                    871:        local->card.margi = (void *) local;
                    872:        dev_table[i] = link;
                    873: 
                    874:        /* Initialize the dev_link_t structure */
                    875:        link->release.function = &margi_release;
                    876:        link->release.data = (u_long) link;
                    877: 
                    878:        /* Interrupt setup */
                    879:        link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
                    880:        link->irq.IRQInfo1 = IRQ_INFO2_VALID | IRQ_LEVEL_ID;
                    881:        if (irq_list[0] == -1)
                    882:                link->irq.IRQInfo2 = irq_mask;
                    883:        else
                    884:                for (i = 0; i < 4; i++)
                    885:                        link->irq.IRQInfo2 |= 1 << irq_list[i];
                    886:        link->irq.Handler = NULL;
                    887: 
                    888:        /*
                    889:           General socket configuration defaults can go here.  In this
                    890:           client, we assume very little, and rely on the CIS for almost
                    891:           everything.  In most clients, many details (i.e., number, sizes,
                    892:           and attributes of IO windows) are fixed by the nature of the
                    893:           device, and can be hard-wired here.
                    894:         */
                    895:        link->conf.Attributes = 0;
                    896:        link->conf.Vcc = 50;
1.7       mocm      897:        
                    898: #ifndef USE_ZV
1.1       cvs       899:        link->conf.IntType = INT_MEMORY_AND_IO;
1.7       mocm      900: #else
                    901:        link->conf.IntType = INT_ZOOMED_VIDEO;
                    902: #endif
1.1       cvs       903: 
                    904:        /* Register with Card Services */
                    905:        link->next = dev_list;
                    906:        dev_list = link;
                    907:        client_reg.dev_info = &dev_info;
                    908:        client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
                    909:        client_reg.EventMask =
                    910:            CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
                    911:            CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
                    912:            CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
                    913:        client_reg.event_handler = &margi_event;
                    914:        client_reg.Version = 0x0210;
                    915:        client_reg.event_callback_args.client_data = link;
                    916:        ret = CardServices(RegisterClient, &link->handle, &client_reg);
                    917:        if (ret != CS_SUCCESS) {
                    918:                cs_error(link->handle, RegisterClient, ret);
                    919:                margi_detach(link);
                    920:                return NULL;
                    921:        }
                    922: 
                    923:        return link;
                    924: }                              /* margi_attach */
                    925: 
                    926: /*======================================================================
                    927: 
                    928:     This deletes a driver "instance".  The device is de-registered
                    929:     with Card Services.  If it has been released, all local data
                    930:     structures are freed.  Otherwise, the structures will be freed
                    931:     when the device is released.
                    932: 
                    933: ======================================================================*/
                    934: 
                    935: static void margi_detach(dev_link_t * link)
                    936: {
                    937:        dev_link_t **linkp;
                    938: 
                    939:        int nd;
                    940: 
1.13    ! mocm      941:        MDEBUG(0, "margi_detach(0x%p)\n", link);
1.1       cvs       942: 
                    943:        for (nd = 0; nd < MAX_DEV; nd++)
                    944:                if (dev_table[nd] == link)
                    945:                        break;
                    946:        if (nd == MAX_DEV)
                    947:                return;
                    948: 
                    949:        /* Locate device structure */
                    950:        for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
                    951:                if (*linkp == link)
                    952:                        break;
                    953:        if (*linkp == NULL)
                    954:                return;
                    955: 
                    956:        /*
                    957:           If the device is currently configured and active, we won't
                    958:           actually delete it yet.  Instead, it is marked so that when
                    959:           the release() function is called, that will trigger a proper
                    960:           detach().
                    961:         */
                    962:        if (link->state & DEV_CONFIG) {
1.13    ! mocm      963:                MDEBUG(2, "margi_cs: detach postponed, '%s' "
1.1       cvs       964:                       "still locked\n", link->dev->dev_name);
                    965:                link->state |= DEV_STALE_LINK;
                    966:                return;
                    967:        }
                    968: 
                    969:        /* Break the link with Card Services */
                    970:        if (link->handle)
                    971:                CardServices(DeregisterClient, link->handle);
                    972: 
                    973:        /* Unlink device structure, and free it */
                    974:        *linkp = link->next;
                    975:        /* This points to the parent struct cvdv_cards struct */
                    976:        dev_table[nd] = NULL;
                    977: 
                    978:        kfree(link->priv);
                    979: 
                    980: }                              /* margi_detach */
                    981: 
                    982: /*======================================================================
                    983: 
                    984:     margi_config() is scheduled to run after a CARD_INSERTION event
                    985:     is received, to configure the PCMCIA socket, and to make the
                    986:     device available to the system.
                    987:     
                    988: ======================================================================*/
                    989: 
                    990: #define CS_CHECK(fn, args...) \
                    991: while ((last_ret=CardServices(last_fn=(fn),args))!=0) goto cs_failed
                    992: 
                    993: #define CFG_CHECK(fn, args...) \
                    994: if (CardServices(fn, args) != 0) goto next_entry
                    995: 
                    996: static void margi_config(dev_link_t * link)
                    997: {
                    998:        client_handle_t handle = link->handle;
                    999:        margi_info_t *dev = link->priv;
                   1000:        struct cvdv_cards *card = &(dev->card);
                   1001:        tuple_t tuple;
                   1002:        cisparse_t parse;
                   1003:        int last_fn, last_ret, i;
                   1004:        u_char buf[64];
                   1005:        config_info_t conf;
                   1006:        win_req_t req;
                   1007:        memreq_t map;
                   1008:        int minor = 0;
                   1009: 
1.13    ! mocm     1010:        MDEBUG(0, "margi_config(0x%p)\n", link);
1.1       cvs      1011: 
                   1012:        /*
                   1013:           This reads the card's CONFIG tuple to find its configuration
                   1014:           registers.
                   1015:         */
                   1016:        tuple.DesiredTuple = CISTPL_CONFIG;
                   1017:        tuple.Attributes = 0;
                   1018:        tuple.TupleData = buf;
                   1019:        tuple.TupleDataMax = sizeof(buf);
                   1020:        tuple.TupleOffset = 0;
                   1021:        CS_CHECK(GetFirstTuple, handle, &tuple);
                   1022:        CS_CHECK(GetTupleData, handle, &tuple);
                   1023:        CS_CHECK(ParseTuple, handle, &tuple, &parse);
                   1024:        link->conf.ConfigBase = parse.config.base;
                   1025:        link->conf.Present = parse.config.rmask[0];
                   1026: 
                   1027:        /* Configure card */
                   1028:        link->state |= DEV_CONFIG;
                   1029: 
                   1030:        /* Look up the current Vcc */
                   1031:        CS_CHECK(GetConfigurationInfo, handle, &conf);
                   1032:        link->conf.Vcc = conf.Vcc;
                   1033: 
                   1034:        /*
                   1035:           In this loop, we scan the CIS for configuration table entries,
                   1036:           each of which describes a valid card configuration, including
                   1037:           voltage, IO window, memory window, and interrupt settings.
                   1038: 
                   1039:           We make no assumptions about the card to be configured: we use
                   1040:           just the information available in the CIS.  In an ideal world,
                   1041:           this would work for any PCMCIA card, but it requires a complete
                   1042:           and accurate CIS.  In practice, a driver usually "knows" most of
                   1043:           these things without consulting the CIS, and most client drivers
                   1044:           will only use the CIS to fill in implementation-defined details.
                   1045:         */
                   1046:        tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
                   1047:        CS_CHECK(GetFirstTuple, handle, &tuple);
                   1048:        while (1) {
                   1049:                cistpl_cftable_entry_t dflt = { 0 };
                   1050:                cistpl_cftable_entry_t *cfg = &(parse.cftable_entry);
                   1051:                CFG_CHECK(GetTupleData, handle, &tuple);
                   1052:                CFG_CHECK(ParseTuple, handle, &tuple, &parse);
                   1053: 
                   1054:                if (cfg->flags & CISTPL_CFTABLE_DEFAULT)
                   1055:                        dflt = *cfg;
                   1056:                if (cfg->index == 0)
                   1057:                        goto next_entry;
                   1058:                link->conf.ConfigIndex = cfg->index;
                   1059: 
                   1060:                /* Does this card need audio output? */
                   1061:                if (cfg->flags & CISTPL_CFTABLE_AUDIO) {
                   1062:                        link->conf.Attributes |= CONF_ENABLE_SPKR;
                   1063:                        link->conf.Status = CCSR_AUDIO_ENA;
                   1064:                }
                   1065: 
                   1066:                /* Use power settings for Vcc and Vpp if present */
                   1067:                /*  Note that the CIS values need to be rescaled */
                   1068:                if (cfg->vcc.present & (1 << CISTPL_POWER_VNOM)) {
                   1069:                        if (conf.Vcc !=
                   1070:                            cfg->vcc.param[CISTPL_POWER_VNOM] /
                   1071:                            10000) goto next_entry;
                   1072:                } else if (dflt.vcc.present & (1 << CISTPL_POWER_VNOM)) {
                   1073:                        if (conf.Vcc !=
                   1074:                            dflt.vcc.param[CISTPL_POWER_VNOM] /
                   1075:                            10000) goto next_entry;
                   1076:                }
                   1077: 
                   1078:                if (cfg->vpp1.present & (1 << CISTPL_POWER_VNOM))
                   1079:                        link->conf.Vpp1 = link->conf.Vpp2 =
                   1080:                            cfg->vpp1.param[CISTPL_POWER_VNOM] / 10000;
                   1081:                else if (dflt.vpp1.present & (1 << CISTPL_POWER_VNOM))
                   1082:                        link->conf.Vpp1 = link->conf.Vpp2 =
                   1083:                            dflt.vpp1.param[CISTPL_POWER_VNOM] / 10000;
                   1084: 
                   1085:                /*
                   1086:                   Allocate an interrupt line.  Note that this does not assign a
                   1087:                   handler to the interrupt, unless the 'Handler' member of the
                   1088:                   irq structure is initialized.
                   1089:                 */
1.7       mocm     1090: #ifndef NOINT
1.1       cvs      1091:                link->irq.Attributes =
                   1092:                  IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
                   1093:                link->irq.Handler = &L64014Intr;
                   1094:                link->irq.Instance = link;
1.7       mocm     1095:                link->conf.Attributes |= CONF_ENABLE_IRQ;               
1.1       cvs      1096: #ifdef USE_BH
                   1097:                init_bh(MARGI_BH, do_margi_bh);
                   1098: #endif
                   1099:                if (link->conf.Attributes & CONF_ENABLE_IRQ)
                   1100:                        CS_CHECK(RequestIRQ, link->handle, &link->irq);
1.7       mocm     1101: #endif
1.1       cvs      1102: 
                   1103:                /* IO window settings */
                   1104:                link->io.NumPorts1 = link->io.NumPorts2 = 0;
                   1105:                if ((cfg->io.nwin > 0) || (dflt.io.nwin > 0)) {
                   1106:                        cistpl_io_t *io =
                   1107:                            (cfg->io.nwin) ? &cfg->io : &dflt.io;
                   1108:                        link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
                   1109:                        if (!(io->flags & CISTPL_IO_8BIT))
                   1110:                                link->io.Attributes1 =
                   1111:                                    IO_DATA_PATH_WIDTH_16;
                   1112:                        if (!(io->flags & CISTPL_IO_16BIT))
                   1113:                                link->io.Attributes1 =
                   1114:                                    IO_DATA_PATH_WIDTH_8;
                   1115:                        link->io.IOAddrLines =
                   1116:                            io->flags & CISTPL_IO_LINES_MASK;
                   1117:                        link->io.BasePort1 = io->win[0].base;
                   1118:                        link->io.NumPorts1 = io->win[0].len;
                   1119:                        if (io->nwin > 1) {
                   1120:                                link->io.Attributes2 =
                   1121:                                    link->io.Attributes1;
                   1122:                                link->io.BasePort2 = io->win[1].base;
                   1123:                                link->io.NumPorts2 = io->win[1].len;
                   1124:                        }
                   1125:                }
                   1126: 
                   1127:                /* This reserves IO space but doesn't actually enable it */
                   1128:                CFG_CHECK(RequestIO, link->handle, &link->io);
                   1129: 
                   1130:                /*
                   1131:                   Now set up a common memory window, if needed.  There is room
                   1132:                   in the dev_link_t structure for one memory window handle,
                   1133:                   but if the base addresses need to be saved, or if multiple
                   1134:                   windows are needed, the info should go in the private data
                   1135:                   structure for this device.
                   1136: 
                   1137:                   Note that the memory window base is a physical address, and
                   1138:                   needs to be mapped to virtual space with ioremap() before it
                   1139:                   is used.
                   1140:                 */
                   1141:                if ((cfg->mem.nwin > 0) || (dflt.mem.nwin > 0)) {
                   1142:                        cistpl_mem_t *mem =
                   1143:                            (cfg->mem.nwin) ? &cfg->mem : &dflt.mem;
                   1144:                        req.Attributes =
                   1145:                            WIN_DATA_WIDTH_16 | WIN_MEMORY_TYPE_CM;
                   1146:                        req.Attributes |= WIN_ENABLE;
                   1147:                        req.Base = mem->win[0].host_addr;
                   1148:                        req.Size = mem->win[0].len;
                   1149:                        req.AccessSpeed = 0;
                   1150:                        link->win = (window_handle_t) link->handle;
                   1151:                        CFG_CHECK(RequestWindow, &link->win, &req);
                   1152:                        map.Page = 0;
                   1153:                        map.CardOffset = mem->win[0].card_addr;
                   1154:                        CFG_CHECK(MapMemPage, link->win, &map);
                   1155:                }
                   1156:                /* If we got this far, we're cool! */
                   1157:                break;
                   1158:                
                   1159:        next_entry:
                   1160:                CS_CHECK(GetNextTuple, handle, &tuple);
                   1161:        }
                   1162: 
                   1163:        /*
                   1164:           This actually configures the PCMCIA socket -- setting up
                   1165:           the I/O windows and the interrupt mapping, and putting the
                   1166:           card and host interface into "Memory and IO" mode.
                   1167:         */
                   1168:        CS_CHECK(RequestConfiguration, link->handle, &link->conf);
                   1169: 
                   1170:        /*
                   1171:           We can release the IO port allocations here, if some other
                   1172:           driver for the card is going to loaded, and will expect the
                   1173:           ports to be available.
                   1174:         */
                   1175:        if (free_ports) {
                   1176:                if (link->io.BasePort1)
                   1177:                        release_region(link->io.BasePort1,
                   1178:                                       link->io.NumPorts1);
                   1179:                if (link->io.BasePort2)
                   1180:                        release_region(link->io.BasePort2,
                   1181:                                       link->io.NumPorts2);
                   1182:        }
                   1183: 
                   1184:        /*
                   1185:           At this point, the dev_node_t structure(s) need to be
                   1186:           initialized and arranged in a linked list at link->dev.
                   1187:         */
                   1188: 
                   1189:        first_card = card;
                   1190:        minor=0;
                   1191:        card->next = NULL;
                   1192:        card_init(card, minor);
                   1193:        if ((i = register_chrdev(CVDV_MAJOR, CVDV_PROCNAME, &cvdv_fileops))
                   1194:            >= 0) {
                   1195:                major_device_number = ((i) ? i : CVDV_MAJOR);
                   1196:                printk(KERN_INFO LOGNAME
                   1197:                       ": Char-device with major number %d installed\n",
                   1198:                       major_device_number);
                   1199:        } else {
                   1200:                printk(KERN_ERR LOGNAME
                   1201:                       ": ERROR: Failed to install Char-device %d, error %d\n",
                   1202:                       CVDV_MAJOR, i);
                   1203:        }
1.7       mocm     1204: 
                   1205: 
1.1       cvs      1206:        sprintf(dev->node.dev_name, "margi");
                   1207:        dev->node.major = major_device_number;
                   1208:        dev->node.minor = minor;
                   1209:        link->dev = &dev->node;
1.7       mocm     1210: #ifdef DVB
                   1211:        dvb_register(card);
                   1212: #endif
1.1       cvs      1213:        /* Finally, report what we've done */
                   1214:        printk(KERN_INFO "%s: index 0x%02x: Vcc %d.%d",
                   1215:               dev->node.dev_name, link->conf.ConfigIndex,
                   1216:               link->conf.Vcc / 10, link->conf.Vcc % 10);
                   1217:        if (link->conf.Vpp1)
                   1218:                printk(", Vpp %d.%d", link->conf.Vpp1 / 10,
                   1219:                       link->conf.Vpp1 % 10);
                   1220:        if (link->conf.Attributes & CONF_ENABLE_IRQ)
                   1221:                printk(", irq %d", link->irq.AssignedIRQ);
                   1222:        if (link->io.NumPorts1)
                   1223:                printk(", io 0x%04x-0x%04x", link->io.BasePort1,
                   1224:                       link->io.BasePort1 + link->io.NumPorts1 - 1);
                   1225:        if (link->io.NumPorts2)
                   1226:                printk(" & 0x%04x-0x%04x", link->io.BasePort2,
                   1227:                       link->io.BasePort2 + link->io.NumPorts2 - 1);
                   1228:        if (link->win)
                   1229:                printk(", mem 0x%06lx-0x%06lx", req.Base,
                   1230:                       req.Base + req.Size - 1);
                   1231:        printk("\n");
                   1232: 
                   1233:        link->state &= ~DEV_CONFIG_PENDING;
                   1234:        if (0xdd == read_indexed_register(card, IIO_ID)) {
                   1235:                printk("L64014 Version %d in mode %d detected\n",
                   1236:                       (read_indexed_register(card, IIO_MODE) & 248) >> 3,
                   1237:                       read_indexed_register(card, IIO_MODE) & 7);
                   1238:                write_indexed_register(card, IIO_GPIO_CONTROL, 0x07);
                   1239: 
                   1240:                L64014Init(card);
1.7       mocm     1241:                
1.1       cvs      1242:                // default: color bars
                   1243:                VideoSetBackground(card, 1, 0, 0, 0);   // black
                   1244:                SetVideoSystem(card);
                   1245:                minorlist[minor] = card;        // fast access for the char driver
                   1246: 
                   1247: 
                   1248:                /*enable L64014 IRQ */
                   1249:                write_indexed_register(card, IIO_IRQ_CONTROL,
                   1250:                                       IRQ_POL | IRQ_EN | VSYNC_EN);
                   1251: //             write_indexed_register(card, IIO_IRQ_CONTROL, 0x24);
1.7       mocm     1252: 
1.6       mocm     1253:                OSDOpen(card, 50, 50, 150, 150, 2, 1);
                   1254:                OSDTest(card);
1.1       cvs      1255:        }
                   1256:        return;
                   1257: 
                   1258:       cs_failed:
                   1259:        cs_error(link->handle, last_fn, last_ret);
                   1260:        margi_release((u_long) link);
                   1261: 
                   1262: }                              /* margi_config */
                   1263: 
                   1264: /*======================================================================
                   1265: 
                   1266:     After a card is removed, margi_release() will unregister the
                   1267:     device, and release the PCMCIA configuration.  If the device is
                   1268:     still open, this will be postponed until it is closed.
                   1269:     
                   1270: ======================================================================*/
                   1271: 
                   1272: static void margi_release(u_long arg)
                   1273: {
                   1274:        dev_link_t *link = (dev_link_t *) arg;
                   1275:        margi_info_t *dev = link->priv;
                   1276:        struct cvdv_cards *card = &(dev->card);
                   1277: 
1.13    ! mocm     1278:        MDEBUG(0, "margi_release(0x%p)\n", link);
1.1       cvs      1279:        /*
                   1280:           If the device is currently in use, we won't release until it
                   1281:           is actually closed, because until then, we can't be sure that
                   1282:           no one will try to access the device or its data structures.
                   1283:         */
                   1284:        if (link->open) {
1.13    ! mocm     1285:                MDEBUG(1, "margi_cs: release postponed, '%s' still open\n",
1.1       cvs      1286:                      link->dev->dev_name);
                   1287:                link->state |= DEV_STALE_CONFIG;
                   1288:                return;
                   1289:        }
                   1290: 
                   1291:        /* Unlink the device chain */
                   1292:        link->dev = NULL;
                   1293: 
                   1294:        /*
                   1295:           In a normal driver, additional code may be needed to release
                   1296:           other kernel data structures associated with this device. 
                   1297:         */
                   1298: 
1.13    ! mocm     1299:        MDEBUG(1,": Unloading device driver\n");
1.1       cvs      1300:        if (major_device_number)
                   1301:                unregister_chrdev(major_device_number, CVDV_PROCNAME);
                   1302:        CardDeInit(card);
                   1303: 
1.7       mocm     1304: #ifndef NOINT
1.1       cvs      1305: #ifdef USE_BH
                   1306:        remove_bh(MARGI_BH);
                   1307: #endif
                   1308:        mdelay(100);
1.7       mocm     1309: #endif
                   1310:        CloseCard(card);
                   1311: #ifdef DVB
                   1312:        dvb_unregister(card);
                   1313: #endif
1.1       cvs      1314:        /* Don't bother checking to see if these succeed or not */
                   1315:        if (link->win)
                   1316:          CardServices(ReleaseWindow, link->win);
                   1317:        CardServices(ReleaseConfiguration, link->handle);
                   1318:        if (link->io.NumPorts1)
                   1319:          CardServices(ReleaseIO, link->handle, &link->io);
1.7       mocm     1320: #ifndef NOINT
1.1       cvs      1321:        if (link->irq.AssignedIRQ)
                   1322:          CardServices(ReleaseIRQ, link->handle, &link->irq);
1.7       mocm     1323: #endif
1.1       cvs      1324:        link->state &= ~DEV_CONFIG;
                   1325: 
                   1326:        if (link->state & DEV_STALE_LINK)
                   1327:                margi_detach(link);
                   1328: 
                   1329: }                              /* margi_release */
                   1330: 
                   1331: /*======================================================================
                   1332: 
                   1333:     The card status event handler.  Mostly, this schedules other
                   1334:     stuff to run after an event is received.
                   1335: 
                   1336:     When a CARD_REMOVAL event is received, we immediately set a
                   1337:     private flag to block future accesses to this device.  All the
                   1338:     functions that actually access the device should check this flag
                   1339:     to make sure the card is still present.
                   1340:     
                   1341: ======================================================================*/
                   1342: 
                   1343: static int margi_event(event_t event, int priority,
                   1344:                       event_callback_args_t * args)
                   1345: {
                   1346:        dev_link_t *link = args->client_data;
                   1347:        margi_info_t *dev = link->priv;
                   1348: 
1.13    ! mocm     1349:        MDEBUG(1, "margi_event(0x%06x)\n", event);
1.1       cvs      1350: 
                   1351:        switch (event) {
                   1352:        case CS_EVENT_CARD_REMOVAL:
                   1353:                link->state &= ~DEV_PRESENT;
                   1354:                if (link->state & DEV_CONFIG) {
                   1355:                        ((margi_info_t *) link->priv)->stop = 1;
                   1356:                        link->release.expires = jiffies + HZ / 20;
                   1357:                        add_timer(&link->release);
                   1358:                }
                   1359:                break;
                   1360:        case CS_EVENT_CARD_INSERTION:
                   1361:                link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
                   1362:                dev->card.bus = args->bus;
                   1363:                margi_config(link);
                   1364:                break;
                   1365:        case CS_EVENT_PM_SUSPEND:
                   1366:                link->state |= DEV_SUSPEND;
                   1367:                /* Fall through... */
                   1368:        case CS_EVENT_RESET_PHYSICAL:
                   1369:                /* Mark the device as stopped, to block IO until later */
                   1370:                dev->stop = 1;
                   1371:                if (link->state & DEV_CONFIG)
                   1372:                        CardServices(ReleaseConfiguration, link->handle);
                   1373:                break;
                   1374:        case CS_EVENT_PM_RESUME:
                   1375:                link->state &= ~DEV_SUSPEND;
                   1376:                /* Fall through... */
                   1377:        case CS_EVENT_CARD_RESET:
                   1378:                if (link->state & DEV_CONFIG)
                   1379:                        CardServices(RequestConfiguration, link->handle,
                   1380:                                     &link->conf);
                   1381:                dev->stop = 0;
                   1382:                /*
                   1383:                   In a normal driver, additional code may go here to restore
                   1384:                   the device state and restart IO. 
                   1385:                 */
                   1386:                break;
                   1387:        }
                   1388:        return 0;
                   1389: }                              /* margi_event */
                   1390: 
                   1391: /*====================================================================*/
                   1392: 
                   1393: static int __init init_margi_cs(void)
                   1394: {
                   1395:        servinfo_t serv;
1.13    ! mocm     1396:        MDEBUG(0, "%s\n", version);
1.1       cvs      1397:        CardServices(GetCardServicesInfo, &serv);
                   1398:        if (serv.Revision != CS_RELEASE_CODE) {
                   1399:                printk(KERN_NOTICE "margi_cs: Card Services release "
                   1400:                       "does not match!\n");
                   1401:                return -1;
                   1402:        }
                   1403:        register_pccard_driver(&dev_info, &margi_attach, &margi_detach);
                   1404:        return 0;
                   1405: }
                   1406: 
                   1407: static void __exit exit_margi_cs(void)
                   1408: {
1.13    ! mocm     1409:        MDEBUG(0, "margi_cs: unloading\n");
1.1       cvs      1410:        unregister_pccard_driver(&dev_info);
                   1411:        while (dev_list != NULL) {
                   1412:                if (dev_list->state & DEV_CONFIG)
                   1413:                        margi_release((u_long) dev_list);
                   1414:                margi_detach(dev_list);
                   1415:        }
                   1416: }
                   1417: 
                   1418: module_init(init_margi_cs);
                   1419: module_exit(exit_margi_cs);

LinuxTV legacy CVS <linuxtv.org/cvs>