Annotation of margi2/margi.c, revision 1.3

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

LinuxTV legacy CVS <linuxtv.org/cvs>