Skip to contentSkip to conversation

PCI driver

DISCUSSION Manu Abraham 7 participants 40 messages Sep 14, 2005 – Oct 10, 2005 5 messages not in the archive

lore
Share & tools
lore
#0 lore raw

Hi,

I have been in the process of trying to write a new PCI driver. The hardware is Memory Mapped IO device similar to the Fusion 878, but not that complicated, but simpler

Now that i have been trying to implement the driver using the new PCI API, i feel a bit lost at the different changes gone into the PCI API. So if someone could give me a brief idea how a minimal PCI probe routine should consist of, that would be quite helpful.

Thanks,
Manu

#1 lore raw

Manu Abraham napsal(a):

Quoted 1 line

Now that i have been trying to implement the driver using the new PCI API, i feel a bit lost at the different changes gone into the PCI API. So if someone could give me a brief idea how a minimal PCI probe routine should consist of, that would be quite helpful.

Maybe, you want to read http://lwn.net/Kernel/LDD3/, chapter 12, pages 311+.

regards,

Signature · 3 lines
Jiri Slaby         www.fi.muni.cz/~xslaby
~\-/~      [email protected]      ~\-/~
241B347EC88228DE51EE A49C4A73A25004CB2A10
#2 lore raw
Quoted 7 lines · Jiri Slaby

Manu Abraham napsal(a):

Quoted 1 line

Now that i have been trying to implement the driver using the new PCI API, i feel a bit lost at the different changes gone into the PCI API. So if someone could give me a brief idea how a minimal PCI probe routine should consist of, that would be quite helpful.

Maybe, you want to read http://lwn.net/Kernel/LDD3/, chapter 12, pages 311+.

I have been updating myself from LDD2 to LDD3. What i was wondering was in what order should i be calling the functions.

Thanks,
Manu

#3 lore raw

Manu Abraham napsal(a):

Quoted 4 lines
Quoted 2 lines · Jiri Slaby

Manu Abraham napsal(a):

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

Now that i have been trying to implement the driver using the new PCI API, i feel a bit lost at the different changes gone into the PCI API. So if someone could give me a brief idea how a minimal PCI probe routine should consist of, that would be quite helpful.

Maybe, you want to read http://lwn.net/Kernel/LDD3/, chapter 12, pages 311+.

I have been updating myself from LDD2 to LDD3. What i was wondering was in what order should i be calling the functions.

You won't call anything, kernel does. You only register driver. struct pcitbl {venids, devids}

struct driver ... = {probe=a, remove=b, tbl=pcitbl};
a() { if device from pcitbl is in the system (or has been added before some little time) this function is called}
b() {if the device was removed from system: modules and hotplug: never called; modules: so if modules unload; if both: if the device was removed on the fly, or module unload}

module_init() { register_driver(driver)}
module_exit() { unregister_driver(driver); }

Signature · 3 lines
Jiri Slaby         www.fi.muni.cz/~xslaby
~\-/~      [email protected]      ~\-/~
241B347EC88228DE51EE A49C4A73A25004CB2A10
#4 lore raw
Quoted 20 lines · Jiri Slaby

Manu Abraham napsal(a):

Quoted 9 lines
Quoted 7 lines · Jiri Slaby

Manu Abraham napsal(a):

Quoted 1 line

Now that i have been trying to implement the driver using the new PCI API, i feel a bit lost at the different changes gone into the PCI API. So if someone could give me a brief idea how a minimal PCI probe routine should consist of, that would be quite helpful.

Quoted 4 lines
Quoted 2 lines

Maybe, you want to read http://lwn.net/Kernel/LDD3/, chapter 12, pages 311+.

I have been updating myself from LDD2 to LDD3. What i was wondering was in what order should i be calling the functions.

You won't call anything, kernel does. You only register driver. struct pcitbl {venids, devids}

Quoted 7 lines

struct driver ... = {probe=a, remove=b, tbl=pcitbl};
a() { if device from pcitbl is in the system (or has been added before some little time) this function is called}
b() {if the device was removed from system: modules and hotplug: never called; modules: so if modules unload; if both: if the device was removed on the fly, or module unload}

module_init() { register_driver(driver)}
module_exit() { unregister_driver(driver); }

I was wondering whether pci_enable_device() should come first or pci_dev_put() in the probe routine.

Thanks,
Manu

#5 lore raw

Manu Abraham napsal(a):

Quoted 28 lines
Quoted 26 lines · Jiri Slaby

Manu Abraham napsal(a):

Quoted 10 lines
Quoted 8 lines · Jiri Slaby

Manu Abraham napsal(a):

Quoted 1 line

Now that i have been trying to implement the driver using the new PCI API, i feel a bit lost at the different changes gone into the PCI API. So if someone could give me a brief idea how a minimal PCI probe routine should consist of, that would be quite helpful.

Quoted 4 lines
Quoted 2 lines

Maybe, you want to read http://lwn.net/Kernel/LDD3/, chapter 12, pages 311+.

I have been updating myself from LDD2 to LDD3. What i was wondering was in what order should i be calling the functions.

You won't call anything, kernel does. You only register driver. struct pcitbl {venids, devids}

struct driver ... = {probe=a, remove=b, tbl=pcitbl};
a() { if device from pcitbl is in the system (or has been added before some little time) this function is called}
b() {if the device was removed from system: modules and hotplug: never called; modules: so if modules unload; if both: if the device was removed on the fly, or module unload}

Quoted 5 lines
Quoted 3 lines

module_init() { register_driver(driver)}
module_exit() { unregister_driver(driver); }

I was wondering whether pci_enable_device() should come first or pci_dev_put() in the probe routine.

pci_dev_put? No, it counts down reference count, so you would loose the structure. You do NOT do pci_dev_put anymore with pci probing (but some very very specific cases).

Signature · 3 lines
Jiri Slaby         www.fi.muni.cz/~xslaby
~\-/~      [email protected]      ~\-/~
241B347EC88228DE51EE A49C4A73A25004CB2A10
#6 lore raw
Quoted 32 lines · Jiri Slaby

Manu Abraham napsal(a):

Quoted 30 lines
Quoted 28 lines · Jiri Slaby

Manu Abraham napsal(a):

Quoted 11 lines
Quoted 9 lines · Jiri Slaby

Manu Abraham napsal(a):

Quoted 1 line

Now that i have been trying to implement the driver using the new PCI API, i feel a bit lost at the different changes gone into the PCI API. So if someone could give me a brief idea how a minimal PCI probe routine should consist of, that would be quite helpful.

Quoted 4 lines
Quoted 2 lines

Maybe, you want to read http://lwn.net/Kernel/LDD3/, chapter 12, pages 311+.

I have been updating myself from LDD2 to LDD3. What i was wondering was in what order should i be calling the functions.

You won't call anything, kernel does. You only register driver. struct pcitbl {venids, devids}

struct driver ... = {probe=a, remove=b, tbl=pcitbl};
a() { if device from pcitbl is in the system (or has been added before some little time) this function is called}
b() {if the device was removed from system: modules and hotplug: never called; modules: so if modules unload; if both: if the device was removed on the fly, or module unload}

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

module_init() { register_driver(driver)}
module_exit() { unregister_driver(driver); }

I was wondering whether pci_enable_device() should come first or pci_dev_put() in the probe routine.

pci_dev_put? No, it counts down reference count, so you would loose the structure. You do NOT do pci_dev_put anymore with pci probing (but some very very specific cases).

Oh, i thought after a pci_dev_get() one does a pci_dev_put()
I wrote something like this, i was now confused with what to do with pci_get_drvdata() and pci_set_drvdata()
I am not very sure whether i am doing it right in the first place, since my mapped memory seems to be wrong, but first i have to clear up about pci_get/set_drvdata().

Manu

static int mantis_pci_probe(*struct* pci_dev *pdev, const *struct* pci_device_id *mantis_pci_table)

{
	*struct* mantis_pci *mantis;
	u8 revision, latency;

	pdev = pci_get_device(PCI_VENDOR_ID_MANTIS, PCI_DEVICE_ID_MANTIS_R11, NULL);
	*if* (pdev) {
		dprintk(verbose, MANTIS_DEBUG, 1, "Found a mantis chip");
		*if* ((mantis = (*struct* mantis_pci *) kmalloc(*sizeof* (*struct* mantis_pci), GFP_KERNEL)) == NULL) {
			dprintk(verbose, MANTIS_ERROR, 1, "Out of memory");
			*return* -ENOMEM;
		}
		*if* (pci_enable_device(pdev) < 0) {
			dprintk(verbose, MANTIS_ERROR, 1, "Could not enable device");
			kfree(mantis);
			*return* -ENODEV;
		}
		mantis->pdev = pdev;
		mantis->mantis_addr = pci_resource_start(pdev, 0);
		*if* (!request_mem_region(pci_resource_start(pdev, 0),
			pci_resource_len(pdev, 0), DRIVER_NAME)) {
			kfree(mantis);
			*return* -EBUSY;
		}
		pci_read_config_byte(mantis->pdev, PCI_CLASS_REVISION, &revision);
		pci_read_config_byte(mantis->pdev, PCI_LATENCY_TIMER, &latency);
		mantis->mantis_mmio = ioremap(mantis->mantis_addr, 0x1000);
		mmwrite(0, MANTIS_INT_STAT); //*		Clear interrupts	*//
		*if* (request_irq(mantis->pdev->irq, (void *) mantis_pci_irq, SA_SHIRQ | 
						SA_INTERRUPT, DRIVER_NAME, (void *) mantis) < 0) {
			dprintk(verbose, MANTIS_ERROR, 1, "Mantis IRQ registration failed");
			release_mem_region(pci_resource_start(mantis->pdev, 0), 
					pci_resource_len(mantis->pdev, 0));
			pci_disable_device(pdev);
			kfree(mantis);
		}
		pci_dev_put(pdev);


	}

	
	*return* 0;
}
#7 lore raw
In reply to Jiri Slaby
Quoted 32 lines · Jiri Slaby

Manu Abraham napsal(a):

Quoted 30 lines
Quoted 28 lines · Jiri Slaby

Manu Abraham napsal(a):

Quoted 11 lines
Quoted 9 lines · Jiri Slaby

Manu Abraham napsal(a):

Quoted 1 line

Now that i have been trying to implement the driver using the new PCI API, i feel a bit lost at the different changes gone into the PCI API. So if someone could give me a brief idea how a minimal PCI probe routine should consist of, that would be quite helpful.

Quoted 4 lines
Quoted 2 lines

Maybe, you want to read http://lwn.net/Kernel/LDD3/, chapter 12, pages 311+.

I have been updating myself from LDD2 to LDD3. What i was wondering was in what order should i be calling the functions.

You won't call anything, kernel does. You only register driver. struct pcitbl {venids, devids}

struct driver ... = {probe=a, remove=b, tbl=pcitbl};
a() { if device from pcitbl is in the system (or has been added before some little time) this function is called}
b() {if the device was removed from system: modules and hotplug: never called; modules: so if modules unload; if both: if the device was removed on the fly, or module unload}

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

module_init() { register_driver(driver)}
module_exit() { unregister_driver(driver); }

I was wondering whether pci_enable_device() should come first or pci_dev_put() in the probe routine.

pci_dev_put? No, it counts down reference count, so you would loose the structure. You do NOT do pci_dev_put anymore with pci probing (but some very very specific cases).

Oh, i thought after a pci_dev_get() one does a pci_dev_put() I wrote something like this, i was now confused with what to do with pci_get_drvdata() and pci_set_drvdata()
I am not very sure whether i am doing it right in the first place, since my mapped memory seems to be wrong, but first i have to clear up about pci_get/set_drvdata().

Manu

static int mantis_pci_probe(*struct* pci_dev *pdev, const *struct* pci_device_id *mantis_pci_table)

{
	*struct* mantis_pci *mantis;
	u8 revision, latency;

	pdev = pci_get_device(PCI_VENDOR_ID_MANTIS, PCI_DEVICE_ID_MANTIS_R11, 
NULL);
	*if* (pdev) {
		dprintk(verbose, MANTIS_DEBUG, 1, "Found a mantis chip");
		*if* ((mantis = (*struct* mantis_pci *) kmalloc(*sizeof* (*struct* 
mantis_pci), GFP_KERNEL)) == NULL) {
			dprintk(verbose, MANTIS_ERROR, 1, "Out of memory");
			*return* -ENOMEM;
		}
		*if* (pci_enable_device(pdev) < 0) {
			dprintk(verbose, MANTIS_ERROR, 1, "Could not enable device");
			kfree(mantis);
			*return* -ENODEV;
		}
		mantis->pdev = pdev;
		mantis->mantis_addr = pci_resource_start(pdev, 0);
		*if* (!request_mem_region(pci_resource_start(pdev, 0),
			pci_resource_len(pdev, 0), DRIVER_NAME)) {
			kfree(mantis);
			*return* -EBUSY;
		}
		pci_read_config_byte(mantis->pdev, PCI_CLASS_REVISION, &revision);
		pci_read_config_byte(mantis->pdev, PCI_LATENCY_TIMER, &latency);
		mantis->mantis_mmio = ioremap(mantis->mantis_addr, 0x1000);
		mmwrite(0, MANTIS_INT_STAT); //*		Clear interrupts	*//
		*if* (request_irq(mantis->pdev->irq, (void *) mantis_pci_irq, SA_SHIRQ |
						SA_INTERRUPT, DRIVER_NAME, (void *) mantis) < 0) {
			dprintk(verbose, MANTIS_ERROR, 1, "Mantis IRQ registration failed");
			release_mem_region(pci_resource_start(mantis->pdev, 0),
					pci_resource_len(mantis->pdev, 0));
			pci_disable_device(pdev);
			kfree(mantis);
		}
		pci_dev_put(pdev);


	}

	
	*return* 0;
}
#8 lore raw

Manu Abraham napsal(a):

Quoted 57 lines
Quoted 34 lines · Jiri Slaby

Manu Abraham napsal(a):

Quoted 32 lines
Quoted 30 lines · Jiri Slaby

Manu Abraham napsal(a):

Quoted 12 lines
Quoted 10 lines · Jiri Slaby

Manu Abraham napsal(a):

Quoted 1 line

Now that i have been trying to implement the driver using the new PCI API, i feel a bit lost at the different changes gone into the PCI API. So if someone could give me a brief idea how a minimal PCI probe routine should consist of, that would be quite helpful.

Quoted 4 lines
Quoted 2 lines

Maybe, you want to read http://lwn.net/Kernel/LDD3/, chapter 12, pages 311+.

I have been updating myself from LDD2 to LDD3. What i was wondering was in what order should i be calling the functions.

You won't call anything, kernel does. You only register driver. struct pcitbl {venids, devids}

struct driver ... = {probe=a, remove=b, tbl=pcitbl};
a() { if device from pcitbl is in the system (or has been added before some little time) this function is called}
b() {if the device was removed from system: modules and hotplug: never called; modules: so if modules unload; if both: if the device was removed on the fly, or module unload}

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

module_init() { register_driver(driver)}
module_exit() { unregister_driver(driver); }

I was wondering whether pci_enable_device() should come first or pci_dev_put() in the probe routine.

pci_dev_put? No, it counts down reference count, so you would loose the structure. You do NOT do pci_dev_put anymore with pci probing (but some very very specific cases).

Oh, i thought after a pci_dev_get() one does a pci_dev_put() I wrote something like this, i was now confused with what to do with pci_get_drvdata() and pci_set_drvdata()
I am not very sure whether i am doing it right in the first place, since my mapped memory seems to be wrong, but first i have to clear up about pci_get/set_drvdata().

Manu

Quoted 7 lines

static int mantis_pci_probe(*struct* pci_dev *pdev, const *struct* pci_device_id *mantis_pci_table)

{
    *struct* mantis_pci *mantis;
    u8 revision, latency;

    pdev = pci_get_device(PCI_VENDOR_ID_MANTIS, 

PCI_DEVICE_ID_MANTIS_R11, NULL);

you do NOT do this at all, because you have pdev already (the param of the probe function)

Quoted 8 lines
    *if* (pdev) {
        dprintk(verbose, MANTIS_DEBUG, 1, "Found a mantis chip");
        *if* ((mantis = (*struct* mantis_pci *) kmalloc(*sizeof* 
(*struct* mantis_pci), GFP_KERNEL)) == NULL) {
            dprintk(verbose, MANTIS_ERROR, 1, "Out of memory");
            *return* -ENOMEM;
        }
        *if* (pci_enable_device(pdev) < 0) {

you do only this

Quoted 3 lines
            dprintk(verbose, MANTIS_ERROR, 1, "Could not enable device");
            kfree(mantis);
            *return* -ENODEV;

gotos and return on one place

Quoted 2 lines
        }
        mantis->pdev = pdev;

you don't need this, need you? you put mantis with pci_setdrvdata and whenever you want it, you get it from it

Quoted 16 lines
        mantis->mantis_addr = pci_resource_start(pdev, 0);
        *if* (!request_mem_region(pci_resource_start(pdev, 0),
            pci_resource_len(pdev, 0), DRIVER_NAME)) {
            kfree(mantis);
            *return* -EBUSY;
        }
        pci_read_config_byte(mantis->pdev, PCI_CLASS_REVISION, 
&revision);
        pci_read_config_byte(mantis->pdev, PCI_LATENCY_TIMER, &latency);
        mantis->mantis_mmio = ioremap(mantis->mantis_addr, 0x1000);
        mmwrite(0, MANTIS_INT_STAT); //*        Clear interrupts    *//
        *if* (request_irq(mantis->pdev->irq, (void *) mantis_pci_irq, 

SA_SHIRQ |
SA_INTERRUPT, DRIVER_NAME, (void *) mantis) <
0) {
dprintk(verbose, MANTIS_ERROR, 1, "Mantis IRQ registration

Quoted 7 lines
failed");
            release_mem_region(pci_resource_start(mantis->pdev, 0),
                    pci_resource_len(mantis->pdev, 0));
            pci_disable_device(pdev);
            kfree(mantis);
        }
        pci_dev_put(pdev);

don't do that

Quoted 9 lines

    }

    
    *return* 0;
}
#9 lore raw
Quoted 2 lines · Jiri Slaby

you do NOT do this at all, because you have pdev already (the param of the probe function)

I rewrote the entire thing like this including the pci_remove function too, but now it so seems that in the remove function, pci_get_drvdata(pdev) returns NULL, and hence i get an Oops at module removal.

Manu

struct mantis_pci {
	/*	PCI stuff	*/
	__u16 id;
	__u16 vendor_id;
	__u16 device_id;
	__u16 sub_vendor_id;
	__u16 sub_device_id;
	__u8  latency;
	
	/*	Linux PCI	*/	
	struct pci_dev *pdev;

	__u32 mantis_addr;
	volatile __u8 __iomem *mantis_mmio;

	__u8  irq;
	__u8  revision;
	
	__u16 mantis_card_num;

	/*	RISC Core	*/
	__u32 block_count;
	__u32 block_bytes;
	__u32 line_bytes;
	__u32 line_count;
	
	__u32 risc_pos;
	
	__u32 buf_size;
	__u8  *buf_cpu;
	dma_addr_t buf_dma;
	
	__u32 risc_size;
	__u32 *risc_cpu;
	dma_addr_t risc_dma;
};

static int mantis_pci_probe(struct pci_dev *pdev, const struct pci_device_id *mantis_pci_table)

{
	struct mantis_pci *mantis;
	struct mantis_eeprom eeprom;
	u8 revision, latency;
	u8 data[2];	

	if (pci_enable_device(pdev)) {		
		dprintk(verbose, MANTIS_DEBUG, 1, "Found a mantis chip");
		if ((mantis = (struct mantis_pci *) kmalloc(sizeof (struct 
mantis_pci), GFP_KERNEL)) == NULL) {
			dprintk(verbose, MANTIS_ERROR, 1, "Out of memory");
			return -ENOMEM;
		}
		pci_set_master(pdev);
		mantis->mantis_addr = pci_resource_start(pdev, 0);
		if (!request_mem_region(pci_resource_start(pdev, 0),
			pci_resource_len(pdev, 0), DRIVER_NAME)) {
			kfree(mantis);
			return -EBUSY;
		}
		pci_read_config_byte(pdev, PCI_CLASS_REVISION, &revision);
		pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &latency);
		mantis->mantis_mmio = ioremap(mantis->mantis_addr, 0x1000);
		pci_set_drvdata(pdev, mantis);		

		if (request_irq(pdev->irq, (void *) mantis_pci_irq, SA_SHIRQ |
						SA_INTERRUPT, DRIVER_NAME, (void *) mantis) < 0) {
			dprintk(verbose, MANTIS_ERROR, 1, "Mantis IRQ registration failed");
			release_mem_region(pci_resource_start(pdev, 0),
					pci_resource_len(pdev, 0));
			pci_disable_device(pdev);
			kfree(mantis);
			return -ENODEV;
		}


		mantis_reg_dump(mantis);	
	}

	
	return 0;
}

static void mantis_pci_remove(struct pci_dev *pdev)

{
	struct mantis_pci *mantis;
	dprintk(verbose, MANTIS_DEBUG, 1, "Removing Mantis device");			
	mantis = pci_get_drvdata(pdev);
	if (mantis == NULL)
		dprintk(verbose, MANTIS_DEBUG, 1, "Aeio, mantis");
		
	dprintk(verbose, MANTIS_ERROR, 1, "Mantis irq: %d, latency: %d\nmemory: 
0x%04x, mmio: 0x%p", pdev->irq, mantis->latency,
		mantis->mantis_addr, mantis->mantis_mmio);
	free_irq(pdev->irq, mantis);
	dprintk(verbose, MANTIS_ERROR, 1, "Mantis @ 0x%p", mantis->mantis_mmio);
	if (mantis->mantis_mmio)
		iounmap((u8 *) mantis->mantis_mmio);
	release_mem_region(pci_resource_start(pdev, 0),
			pci_resource_len(pdev, 0));
	pci_disable_device(pdev);
	pci_set_drvdata(pdev, NULL);
	kfree(mantis);
}
#10 lore raw

Manu Abraham napsal(a):

Quoted 6 lines
Quoted 2 lines · Jiri Slaby

you do NOT do this at all, because you have pdev already (the param of the probe function)

I rewrote the entire thing like this including the pci_remove function too, but now it so seems that in the remove function, pci_get_drvdata(pdev) returns NULL, and hence i get an Oops at module removal.

Maybe because this is badly written driver.

Quoted 17 lines

static int mantis_pci_probe(struct pci_dev *pdev, const struct pci_device_id *mantis_pci_table)

{
    struct mantis_pci *mantis;
    struct mantis_eeprom eeprom;
    u8 revision, latency;
    u8 data[2];   

    if (pci_enable_device(pdev)) {       
        dprintk(verbose, MANTIS_DEBUG, 1, "Found a mantis chip");
        if ((mantis = (struct mantis_pci *) kmalloc(sizeof (struct 
mantis_pci), GFP_KERNEL)) == NULL) {
            dprintk(verbose, MANTIS_ERROR, 1, "Out of memory");
            return -ENOMEM;
        }
        pci_set_master(pdev);
        mantis->mantis_addr = pci_resource_start(pdev, 0);
        if (!request_mem_region(pci_resource_start(pdev, 0),
Quoted 8 lines
            pci_resource_len(pdev, 0), DRIVER_NAME)) {
            kfree(mantis);
            return -EBUSY;
        }
        pci_read_config_byte(pdev, PCI_CLASS_REVISION, &revision);
        pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &latency);
        mantis->mantis_mmio = ioremap(mantis->mantis_addr, 0x1000);
        pci_set_drvdata(pdev, mantis);       

if pci_enable_device fails, you set this?? Maybe you haven't read the doc enough.

Signature · 3 lines
Jiri Slaby         www.fi.muni.cz/~xslaby
~\-/~      [email protected]      ~\-/~
241B347EC88228DE51EE A49C4A73A25004CB2A10
#11 lore raw
Quoted 10 lines · Jiri Slaby

Manu Abraham napsal(a):

Quoted 6 lines
Quoted 2 lines · Jiri Slaby

you do NOT do this at all, because you have pdev already (the param of the probe function)

I rewrote the entire thing like this including the pci_remove function too, but now it so seems that in the remove function, pci_get_drvdata(pdev) returns NULL, and hence i get an Oops at module removal.

Maybe because this is badly written driver.

I have not written the driver, but this is my first go at it ..

Quoted 26 lines
Quoted 17 lines

static int mantis_pci_probe(struct pci_dev *pdev, const struct pci_device_id *mantis_pci_table)

{
    struct mantis_pci *mantis;
    struct mantis_eeprom eeprom;
    u8 revision, latency;
    u8 data[2];  
    if (pci_enable_device(pdev)) {               dprintk(verbose, 
MANTIS_DEBUG, 1, "Found a mantis chip");
        if ((mantis = (struct mantis_pci *) kmalloc(sizeof (struct 
mantis_pci), GFP_KERNEL)) == NULL) {
            dprintk(verbose, MANTIS_ERROR, 1, "Out of memory");
            return -ENOMEM;
        }
        pci_set_master(pdev);
        mantis->mantis_addr = pci_resource_start(pdev, 0);
        if (!request_mem_region(pci_resource_start(pdev, 0),
Quoted 8 lines
            pci_resource_len(pdev, 0), DRIVER_NAME)) {
            kfree(mantis);
            return -EBUSY;
        }
        pci_read_config_byte(pdev, PCI_CLASS_REVISION, &revision);
        pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &latency);
        mantis->mantis_mmio = ioremap(mantis->mantis_addr, 0x1000);
        pci_set_drvdata(pdev, mantis);       
Quoted 1 line

if pci_enable_device fails, you set this?? Maybe you haven't read the doc enough.

I just found that, pci_enable_device() fails. So what's the way to go ahead ?

Manu

#12 lore raw

Manu Abraham napsal(a):

Quoted 43 lines
Quoted 11 lines · Jiri Slaby

Manu Abraham napsal(a):

Quoted 6 lines
Quoted 2 lines · Jiri Slaby

you do NOT do this at all, because you have pdev already (the param of the probe function)

I rewrote the entire thing like this including the pci_remove function too, but now it so seems that in the remove function, pci_get_drvdata(pdev) returns NULL, and hence i get an Oops at module removal.

Maybe because this is badly written driver.

I have not written the driver, but this is my first go at it ..

Quoted 26 lines
Quoted 24 lines

static int mantis_pci_probe(struct pci_dev *pdev, const struct pci_device_id *mantis_pci_table)

{
    struct mantis_pci *mantis;
    struct mantis_eeprom eeprom;
    u8 revision, latency;
    u8 data[2];      if (pci_enable_device(pdev)) {               
dprintk(verbose, MANTIS_DEBUG, 1, "Found a mantis chip");
        if ((mantis = (struct mantis_pci *) kmalloc(sizeof (struct 
mantis_pci), GFP_KERNEL)) == NULL) {
            dprintk(verbose, MANTIS_ERROR, 1, "Out of memory");
            return -ENOMEM;
        }
        pci_set_master(pdev);
        mantis->mantis_addr = pci_resource_start(pdev, 0);
        if (!request_mem_region(pci_resource_start(pdev, 0),
            pci_resource_len(pdev, 0), DRIVER_NAME)) {
            kfree(mantis);
            return -EBUSY;
        }
        pci_read_config_byte(pdev, PCI_CLASS_REVISION, &revision);
        pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &latency);
        mantis->mantis_mmio = ioremap(mantis->mantis_addr, 0x1000);
        pci_set_drvdata(pdev, mantis);       
Quoted 5 lines
Quoted 1 line

if pci_enable_device fails, you set this?? Maybe you haven't read the doc enough.

I just found that, pci_enable_device() fails. So what's the way to go ahead ?

JESUS.
int retval = 0;

if ((retval = pci_enable_device()))

	goto end;

...
pci_set_drvdata(pdev, mantis);
...

end:

	return retval;

not
if (pci_enable_device())
do something

Signature · 3 lines
Jiri Slaby         www.fi.muni.cz/~xslaby
~\-/~      [email protected]      ~\-/~
241B347EC88228DE51EE A49C4A73A25004CB2A10
#13 lore raw
Quoted 48 lines · Jiri Slaby

Manu Abraham napsal(a):

Quoted 46 lines
Quoted 4 lines · Jiri Slaby

Manu Abraham napsal(a):

Quoted 2 lines

Jiri Slaby wrote:

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

you do NOT do this at all, because you have pdev already (the param of the probe function)

I rewrote the entire thing like this including the pci_remove function too, but now it so seems that in the remove function, pci_get_drvdata(pdev) returns NULL, and hence i get an Oops at module removal.

Maybe because this is badly written driver.

I have not written the driver, but this is my first go at it ..

Quoted 27 lines
Quoted 24 lines

static int mantis_pci_probe(struct pci_dev *pdev, const struct pci_device_id *mantis_pci_table)

{
    struct mantis_pci *mantis;
    struct mantis_eeprom eeprom;
    u8 revision, latency;
    u8 data[2];      if (pci_enable_device(pdev)) {               
dprintk(verbose, MANTIS_DEBUG, 1, "Found a mantis chip");
        if ((mantis = (struct mantis_pci *) kmalloc(sizeof (struct 
mantis_pci), GFP_KERNEL)) == NULL) {
            dprintk(verbose, MANTIS_ERROR, 1, "Out of memory");
            return -ENOMEM;
        }
        pci_set_master(pdev);
        mantis->mantis_addr = pci_resource_start(pdev, 0);
        if (!request_mem_region(pci_resource_start(pdev, 0),
            pci_resource_len(pdev, 0), DRIVER_NAME)) {
            kfree(mantis);
            return -EBUSY;
        }
        pci_read_config_byte(pdev, PCI_CLASS_REVISION, &revision);
        pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &latency);
        mantis->mantis_mmio = ioremap(mantis->mantis_addr, 0x1000);
        pci_set_drvdata(pdev, mantis);       
Quoted 8 lines
Quoted 6 lines
Quoted 1 line

if pci_enable_device fails, you set this?? Maybe you haven't read the doc enough.

I just found that, pci_enable_device() fails. So what's the way to go ahead ?

JESUS.

What i meant is i do have to enable the device, not just exit.
I understood that i have to exit if pci_enable_device() fails, but what i am looking for is why i can't enable it in the first place.

Quoted 9 lines

int retval = 0;

if ((retval = pci_enable_device()))

    goto end;

...
pci_set_drvdata(pdev, mantis);
...

Quoted 6 lines

end:

    return retval;

not
if (pci_enable_device())
do something

Regards,
Manu

#14 lore raw
In reply to Jiri Slaby
Quoted 8 lines · Jiri Slaby

Manu Abraham napsal(a):

Quoted 6 lines
Quoted 4 lines · Jiri Slaby

Manu Abraham napsal(a):

Quoted 2 lines

Jiri Slaby wrote:

Quoted 8 lines
Quoted 6 lines
Quoted 5 lines
Quoted 4 lines
Quoted 2 lines

you do NOT do this at all, because you have pdev already (the param of the probe function)

I rewrote the entire thing like this including the pci_remove function too, but now it so seems that in the remove function, pci_get_drvdata(pdev) returns NULL, and hence i get an Oops at module removal.

I just found that, pci_enable_device() fails. So what's the way to go ahead ?

JESUS.

Hmm.. i finally got it to work. It seems pci_get_device() is necessary, i can't seem to enable the device or request for an IRQ the way you suggested. It looks some quirks are there though ..

If only i could explain why it works this way and not the other way ..

Thanks for the help,
Regards,
Manu

[   81.269655] mantis_pci_probe: Got a device
[   81.269825] mantis_pci_probe: We got an IRQ
[   81.269987] mantis_pci_probe: We finally enabled the device
[   81.270191] Mantis Rev 1, irq: 23, latency: 32
[   81.270289] memory: 0xefeff000, mmio: f9218000
[   81.270519] Trying to free free IRQ23
[   90.485885] mantis_pci_remove: Removing -->Mantis irq: 23, latency: 32
[   90.485887] memory: 0xefeff000, mmio: 0xf9218000
[   90.486293] Trying to free free IRQ23
[   90.486429] Trying to free nonexistent resource <efeff000-efefffff>

static int __devinit mantis_pci_probe(struct pci_dev *pdev, const struct pci_device_id *mantis_pci_table)

{
	u8 revision, latency;
	u8 data[2];	
	struct mantis_pci *mantis;
	mantis = (struct mantis_pci *) kmalloc(sizeof (struct mantis_pci), 
GFP_KERNEL);
	if (mantis == NULL) {
		dprintk(verbose, MANTIS_ERROR, 1, "Out of memory");
		return -ENOMEM;
	}
	
	pdev = pci_get_device(PCI_VENDOR_ID_MANTIS, PCI_DEVICE_ID_MANTIS_R11, 
NULL);
	if (pdev) {
		dprintk(verbose, MANTIS_ERROR, 1, "Got a device");
		mantis->mantis_addr = pci_resource_start(pdev, 0);
		if (!request_mem_region(pci_resource_start(pdev, 0),
			pci_resource_len(pdev, 0), DRIVER_NAME)) {
			dprintk(verbose, MANTIS_ERROR, 1, "Request for memory region failed");
			goto err0;
		}
		if ((mantis->mantis_mmio = ioremap(mantis->mantis_addr, 0x1000)) == 
NULL) {
			dprintk(verbose, MANTIS_ERROR, 1, "IO remap failed");
			goto err1;
		}
		if (request_irq(pdev->irq, (void *) mantis_pci_irq, SA_SHIRQ |
						SA_INTERRUPT, DRIVER_NAME, (void *) mantis) < 0) {
			dprintk(verbose, MANTIS_ERROR, 1, "Mantis IRQ registration failed");
			goto err2;
		}
		dprintk(verbose, MANTIS_DEBUG, 1, "We got an IRQ");
		if (pci_enable_device(pdev)) {
			dprintk(verbose, MANTIS_ERROR, 1, "Mantis PCI device enable failed");
			goto err3;		
		}
		dprintk(verbose, MANTIS_DEBUG, 1, "We finally enabled the device");
		pci_set_master(pdev);
		pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &latency);
		pci_read_config_byte(pdev, PCI_CLASS_REVISION, &revision);
		mantis->latency = latency;
		mantis->revision = revision;
		if (!latency) {
			pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 32);
		}
		pci_set_drvdata(pdev, mantis);	
		dprintk(verbose, MANTIS_ERROR, 0, "Mantis Rev %d, ", mantis->revision);
		dprintk(verbose, MANTIS_ERROR, 0, "irq: %d, latency: %d\nmemory: 
0x%04x, mmio: %p\n", pdev->irq, mantis->latency,
			mantis->mantis_addr, mantis->mantis_mmio);

		pci_dev_put(pdev);		

	} else {
		dprintk(verbose, MANTIS_ERROR, 1, "No device found");
		return -ENODEV;
	}

err3:

	free_irq(pdev->irq, pdev);	
err2:
	if (mantis->mantis_mmio)
		iounmap(mantis->mantis_mmio);
err1:
	release_mem_region(pci_resource_start(pdev, 0),
				pci_resource_len(pdev, 0));
err0:
	kfree(mantis);
	
	return 0;
}

static void __devexit mantis_pci_remove(struct pci_dev *pdev)

{
	struct mantis_pci *mantis = pci_get_drvdata(pdev);
	if (mantis == NULL) {
		dprintk(verbose, MANTIS_ERROR, 1, "Aeio, MAntis NULL ptr");
	}
	dprintk(verbose, MANTIS_ERROR, 1, "Removing -->Mantis irq: %d, latency: 
%d\nmemory: 0x%04x, mmio: 0x%p", pdev->irq, mantis->latency,
			mantis->mantis_addr, mantis->mantis_mmio);

	free_irq(pdev->irq, pdev);
	
	release_mem_region(pci_resource_start(pdev, 0),
		pci_resource_len(pdev, 0));
	pci_disable_device(pdev);
	pci_set_drvdata(pdev, NULL);
	kfree(mantis);
}
#15 lore raw
Quoted 16 lines · Manu Abraham
Quoted 15 lines · Jiri Slaby

Manu Abraham napsal(a):

Quoted 13 lines
Quoted 9 lines · Jiri Slaby

Manu Abraham napsal(a):

Quoted 8 lines
Quoted 2 lines · Jiri Slaby

you do NOT do this at all, because you have pdev already (the param of the probe function)

I rewrote the entire thing like this including the pci_remove function too, but now it so seems that in the remove function, pci_get_drvdata(pdev) returns NULL, and hence i get an Oops at module removal.

I just found that, pci_enable_device() fails. So what's the way to go ahead ?

Quoted 7 lines
Quoted 1 line

JESUS.

Hmm.. i finally got it to work. It seems pci_get_device() is necessary, i can't seem to enable the device or request for an IRQ the way you suggested. It looks some quirks are there though ..

If only i could explain why it works this way and not the other way ..

Because pci_enable_device() works like most other kernel (and also libc)
functions: it returns 0 if everything went fine.

Quoted 10 lines
[   81.269655] mantis_pci_probe: Got a device
[   81.269825] mantis_pci_probe: We got an IRQ
[   81.269987] mantis_pci_probe: We finally enabled the device
[   81.270191] Mantis Rev 1, irq: 23, latency: 32
[   81.270289] memory: 0xefeff000, mmio: f9218000
[   81.270519] Trying to free free IRQ23
[   90.485885] mantis_pci_remove: Removing -->Mantis irq: 23, latency: 32
[   90.485887] memory: 0xefeff000, mmio: 0xf9218000
[   90.486293] Trying to free free IRQ23
[   90.486429] Trying to free nonexistent resource <efeff000-efefffff>

You should introduce a table of PCI devices here that your driver feels responsible for. Then put this into a struct pci_driver which you pass to pci_module_init. Take a look on a random other PCI driver, drivers/net/8139too.c, drivers/scsi/aic7xxx/aic7xxx_osm_pci.c, whatever.

Quoted 14 lines

static int __devinit mantis_pci_probe(struct pci_dev *pdev, const struct pci_device_id *mantis_pci_table)

{
	u8 revision, latency;
	u8 data[2];
	struct mantis_pci *mantis;
	mantis = (struct mantis_pci *) kmalloc(sizeof (struct mantis_pci),
GFP_KERNEL);
	if (mantis == NULL) {
		dprintk(verbose, MANTIS_ERROR, 1, "Out of memory");
		return -ENOMEM;
	}

	pdev = pci_get_device(PCI_VENDOR_ID_MANTIS, PCI_DEVICE_ID_MANTIS_R11,
Quoted 1 line

NULL);

This is not needed anymore then. Your probe function will get called with for any pci dev your driver can handle.

Quoted 6 lines
	if (pdev) {
		dprintk(verbose, MANTIS_ERROR, 1, "Got a device");
		mantis->mantis_addr = pci_resource_start(pdev, 0);
		if (!request_mem_region(pci_resource_start(pdev, 0),
			pci_resource_len(pdev, 0), DRIVER_NAME)) {
			dprintk(verbose, MANTIS_ERROR, 1, "Request for memory region failed");

Line length is maximum 80 characters. See Documentation/CodingStyle

Quoted 8 lines
			goto err0;
		}
		if ((mantis->mantis_mmio = ioremap(mantis->mantis_addr, 0x1000)) == NULL) {
			dprintk(verbose, MANTIS_ERROR, 1, "IO remap failed");
			goto err1;
		}
		if (request_irq(pdev->irq, (void *) mantis_pci_irq, SA_SHIRQ |
						SA_INTERRUPT, DRIVER_NAME, (void *) mantis) < 0) {

You don't need to cast a pointer to void* or vice versa.

Quoted 9 lines
			dprintk(verbose, MANTIS_ERROR, 1, "Mantis IRQ registration failed");
			goto err2;
		}
		dprintk(verbose, MANTIS_DEBUG, 1, "We got an IRQ");
		if (pci_enable_device(pdev)) {
			dprintk(verbose, MANTIS_ERROR, 1, "Mantis PCI device enable failed");
			goto err3;
		}
		dprintk(verbose, MANTIS_DEBUG, 1, "We finally enabled the device");
Quoted 8 lines
		pci_set_master(pdev);
		pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &latency);
		pci_read_config_byte(pdev, PCI_CLASS_REVISION, &revision);
		mantis->latency = latency;
		mantis->revision = revision;
		if (!latency) {
			pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 32);
		}

The value in mantis->latency and the one in the card's address space now differ.

Quoted 7 lines
		pci_set_drvdata(pdev, mantis);
		dprintk(verbose, MANTIS_ERROR, 0, "Mantis Rev %d, ", mantis->revision);
		dprintk(verbose, MANTIS_ERROR, 0, "irq: %d, latency: %d\nmemory:
0x%04x, mmio: %p\n", pdev->irq, mantis->latency,
			mantis->mantis_addr, mantis->mantis_mmio);

		pci_dev_put(pdev);

No, DON'T DO THAT! This will drop the a reference count from the struct pci_dev, which means it can get freed while your driver still wants to work with it.

Quoted 20 lines
	} else {
		dprintk(verbose, MANTIS_ERROR, 1, "No device found");
		return -ENODEV;
	}

err3:

	free_irq(pdev->irq, pdev);
err2:
	if (mantis->mantis_mmio)
		iounmap(mantis->mantis_mmio);
err1:
	release_mem_region(pci_resource_start(pdev, 0),
				pci_resource_len(pdev, 0));
err0:
	kfree(mantis);

	return 0;
}
Quoted 5 lines

static void __devexit mantis_pci_remove(struct pci_dev *pdev)

{
	struct mantis_pci *mantis = pci_get_drvdata(pdev);
	if (mantis == NULL) {
		dprintk(verbose, MANTIS_ERROR, 1, "Aeio, MAntis NULL ptr");

a) this should really never happen. If it happens, it's a kernel bug.
b) if you catch this error while debugging, you should return here so you do not dereference this NULL pointer.

Quoted 5 lines
	}
	dprintk(verbose, MANTIS_ERROR, 1, "Removing -->Mantis irq: %d, latency:
%d\nmemory: 0x%04x, mmio: 0x%p", pdev->irq, mantis->latency,
			mantis->mantis_addr, mantis->mantis_mmio);
Quoted 8 lines
	free_irq(pdev->irq, pdev);

	release_mem_region(pci_resource_start(pdev, 0),
		pci_resource_len(pdev, 0));
	pci_disable_device(pdev);
	pci_set_drvdata(pdev, NULL);
	kfree(mantis);
}

Eike

#16 lore raw
Quoted 61 lines · Rolf Eike Beer
Quoted 29 lines · Manu Abraham
Quoted 26 lines · Jiri Slaby

Manu Abraham napsal(a):

Quoted 23 lines
Quoted 16 lines · Jiri Slaby

Manu Abraham napsal(a):

Quoted 13 lines
Quoted 4 lines · Jiri Slaby

you do NOT do this at all, because you have pdev already (the param of the probe function)

I rewrote the entire thing like this including the pci_remove function too, but now it so seems that in the remove function, pci_get_drvdata(pdev) returns NULL, and hence i get an Oops at module removal.

I just found that, pci_enable_device() fails. So what's the way to go ahead ?

Quoted 10 lines
Quoted 3 lines

JESUS.

Hmm.. i finally got it to work. It seems pci_get_device() is necessary, i can't seem to enable the device or request for an IRQ the way you suggested. It looks some quirks are there though ..

If only i could explain why it works this way and not the other way ..

Because pci_enable_device() works like most other kernel (and also libc) functions: it returns 0 if everything went fine.

Quoted 12 lines
[   81.269655] mantis_pci_probe: Got a device
[   81.269825] mantis_pci_probe: We got an IRQ
[   81.269987] mantis_pci_probe: We finally enabled the device
[   81.270191] Mantis Rev 1, irq: 23, latency: 32
[   81.270289] memory: 0xefeff000, mmio: f9218000
[   81.270519] Trying to free free IRQ23
[   90.485885] mantis_pci_remove: Removing -->Mantis irq: 23, latency: 32
[   90.485887] memory: 0xefeff000, mmio: 0xf9218000
[   90.486293] Trying to free free IRQ23
[   90.486429] Trying to free nonexistent resource <efeff000-efefffff>
Quoted 3 lines

You should introduce a table of PCI devices here that your driver feels responsible for. Then put this into a struct pci_driver which you pass to

I am in fact doing that ..

static *struct* pci_device_id mantis_pci_table[] = {
	{ PCI_DEVICE(PCI_VENDOR_ID_MANTIS, PCI_DEVICE_ID_MANTIS_R11) },
	{ 0 },
};

MODULE_DEVICE_TABLE(pci, mantis_pci_table);

static *struct* pci_driver mantis_pci_driver = {
.name = "Mantis PCI combo driver",
.id_table = mantis_pci_table,
.probe = mantis_pci_probe,
.remove = mantis_pci_remove,
};

static int __devinit mantis_pci_init(void)

{ 
	*return* pci_register_driver(&mantis_pci_driver);
}

static void __devexit mantis_pci_exit(void)

{
	pci_unregister_driver(&mantis_pci_driver);
}

module_init(mantis_pci_init);
module_exit(mantis_pci_exit);

MODULE_DESCRIPTION("Mantis PCI DTV bridge driver"); MODULE_AUTHOR("Manu Abraham"); MODULE_LICENSE("GPL");

Quoted 17 lines

pci_module_init. Take a look on a random other PCI driver, drivers/net/8139too.c, drivers/scsi/aic7xxx/aic7xxx_osm_pci.c, whatever.

Quoted 13 lines

static int __devinit mantis_pci_probe(struct pci_dev *pdev, const struct pci_device_id *mantis_pci_table)

{
	u8 revision, latency;
	u8 data[2];
	struct mantis_pci *mantis;
	mantis = (struct mantis_pci *) kmalloc(sizeof (struct mantis_pci),
GFP_KERNEL);
	if (mantis == NULL) {
		dprintk(verbose, MANTIS_ERROR, 1, "Out of memory");
		return -ENOMEM;
	}
Quoted 9 lines
Quoted 4 lines

pdev = pci_get_device(PCI_VENDOR_ID_MANTIS, PCI_DEVICE_ID_MANTIS_R11,
NULL);

This is not needed anymore then. Your probe function will get called with for any pci dev your driver can handle.

I will just check it up again to see what went wrong ..

Quoted 9 lines
Quoted 8 lines
	if (pdev) {
		dprintk(verbose, MANTIS_ERROR, 1, "Got a device");
		mantis->mantis_addr = pci_resource_start(pdev, 0);
		if (!request_mem_region(pci_resource_start(pdev, 0),
			pci_resource_len(pdev, 0), DRIVER_NAME)) {
			dprintk(verbose, MANTIS_ERROR, 1, "Request for memory region failed");
Quoted 3 lines

Line length is maximum 80 characters. See Documentation/CodingStyle

That was not meant to go into the kernel straight away, as it needs *lot* of work, the PCI part is only something extremely small.

Quoted 13 lines
Quoted 12 lines
 

			goto err0;
		}
		if ((mantis->mantis_mmio = ioremap(mantis->mantis_addr, 0x1000)) == NULL) {
			dprintk(verbose, MANTIS_ERROR, 1, "IO remap failed");
			goto err1;
		}
		if (request_irq(pdev->irq, (void *) mantis_pci_irq, SA_SHIRQ |
						SA_INTERRUPT, DRIVER_NAME, (void *) mantis) < 0) {
Quoted 4 lines

You don't need to cast a pointer to void* or vice versa.

Ah, thanks ...

Quoted 20 lines
Quoted 9 lines
			dprintk(verbose, MANTIS_ERROR, 1, "Mantis IRQ registration failed");
			goto err2;
		}
		dprintk(verbose, MANTIS_DEBUG, 1, "We got an IRQ");
		if (pci_enable_device(pdev)) {
			dprintk(verbose, MANTIS_ERROR, 1, "Mantis PCI device enable failed");
			goto err3;
		}
		dprintk(verbose, MANTIS_DEBUG, 1, "We finally enabled the device");
Quoted 10 lines
		pci_set_master(pdev);
		pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &latency);
		pci_read_config_byte(pdev, PCI_CLASS_REVISION, &revision);
		mantis->latency = latency;
		mantis->revision = revision;
		if (!latency) {
			pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 32);
		}
Quoted 3 lines

The value in mantis->latency and the one in the card's address space now differ.

Yes, i set it to the default latency as specified by vendor.. But temporarily to test the card ..

Quoted 8 lines
Quoted 8 lines
 

		pci_set_drvdata(pdev, mantis);
		dprintk(verbose, MANTIS_ERROR, 0, "Mantis Rev %d, ", mantis->revision);
		dprintk(verbose, MANTIS_ERROR, 0, "irq: %d, latency: %d\nmemory:
0x%04x, mmio: %p\n", pdev->irq, mantis->latency,
			mantis->mantis_addr, mantis->mantis_mmio);
Quoted 8 lines
Quoted 3 lines
		pci_dev_put(pdev);

No, DON'T DO THAT! This will drop the a reference count from the struct pci_dev, which means it can get freed while your driver still wants to work with it.

Hmm.. I thought after i make a call to pci_get_device(), i have to do a pci_dev_put() after the usage .. I was a bit lost when to use pci_dev_put() in this case.

Quoted 28 lines
Quoted 20 lines
	} else {
		dprintk(verbose, MANTIS_ERROR, 1, "No device found");
		return -ENODEV;
	}

err3:

	free_irq(pdev->irq, pdev);
err2:
	if (mantis->mantis_mmio)
		iounmap(mantis->mantis_mmio);
err1:
	release_mem_region(pci_resource_start(pdev, 0),
				pci_resource_len(pdev, 0));
err0:
	kfree(mantis);

	return 0;
}
Quoted 7 lines

static void __devexit mantis_pci_remove(struct pci_dev *pdev)

{
	struct mantis_pci *mantis = pci_get_drvdata(pdev);
	if (mantis == NULL) {
		dprintk(verbose, MANTIS_ERROR, 1, "Aeio, MAntis NULL ptr");
Quoted 6 lines

a) this should really never happen. If it happens, it's a kernel bug.
b) if you catch this error while debugging, you should return here so you do not dereference this NULL pointer.

Ack.

Quoted 7 lines
Quoted 7 lines
	}
	dprintk(verbose, MANTIS_ERROR, 1, "Removing -->Mantis irq: %d, latency:
%d\nmemory: 0x%04x, mmio: 0x%p", pdev->irq, mantis->latency,
			mantis->mantis_addr, mantis->mantis_mmio);

	free_irq(pdev->irq, pdev);
Quoted 8 lines
Quoted 8 lines
	release_mem_region(pci_resource_start(pdev, 0),
		pci_resource_len(pdev, 0));
	pci_disable_device(pdev);
	pci_set_drvdata(pdev, NULL);
	kfree(mantis);
}

Thanks ..

Regards,
Manu

#17 lore raw
Quoted 30 lines · Manu Abraham
Quoted 15 lines · Rolf Eike Beer
Quoted 13 lines · Manu Abraham

static int __devinit mantis_pci_probe(struct pci_dev *pdev, const struct pci_device_id *mantis_pci_table)

{
	u8 revision, latency;
	u8 data[2];
	struct mantis_pci *mantis;
	mantis = (struct mantis_pci *) kmalloc(sizeof (struct mantis_pci),
GFP_KERNEL);
	if (mantis == NULL) {
		dprintk(verbose, MANTIS_ERROR, 1, "Out of memory");
		return -ENOMEM;
	}
Quoted 5 lines
Quoted 2 lines

pdev = pci_get_device(PCI_VENDOR_ID_MANTIS, PCI_DEVICE_ID_MANTIS_R11,
NULL);

This is not needed anymore then. Your probe function will get called with for any pci dev your driver can handle.

I will just check it up again to see what went wrong ..

Quoted 6 lines
Quoted 6 lines
	if (pdev) {
		dprintk(verbose, MANTIS_ERROR, 1, "Got a device");
		mantis->mantis_addr = pci_resource_start(pdev, 0);
		if (!request_mem_region(pci_resource_start(pdev, 0),
			pci_resource_len(pdev, 0), DRIVER_NAME)) {
			dprintk(verbose, MANTIS_ERROR, 1, "Request for memory region failed");

[...]

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
		pci_set_drvdata(pdev, mantis);
		dprintk(verbose, MANTIS_ERROR, 0, "Mantis Rev %d, ", mantis->revision);
		dprintk(verbose, MANTIS_ERROR, 0, "irq: %d, latency: %d\nmemory:
0x%04x, mmio: %p\n", pdev->irq, mantis->latency,
			mantis->mantis_addr, mantis->mantis_mmio);

		pci_dev_put(pdev);
Quoted 7 lines
Quoted 3 lines

No, DON'T DO THAT! This will drop the a reference count from the struct pci_dev, which means it can get freed while your driver still wants to work with it.

Hmm.. I thought after i make a call to pci_get_device(), i have to do a pci_dev_put() after the usage ..
I was a bit lost when to use pci_dev_put() in this case.

That is true, but you should not call pci_get_device() in this function at all.

Eike

#18 lore raw
Quoted 4 lines · Rolf Eike Beer

That is true, but you should not call pci_get_device() in this function at all.

I reworked the whole thing over .. but i am feeling that something's a bit wrong somewhere ..

The log i get on a load - unload ..

[  102.261264] mantis_pci_probe: Got a device
[  102.262852] mantis_pci_probe: We got an IRQ
[  102.264392] mantis_pci_probe: We finally enabled the device
[  102.266020] Mantis Rev 1, irq: 23, latency: 32
[  102.266118]          memory: 0xefeff000, mmio: f9218000
[  102.269162] Trying to free free IRQ23
[  110.297341] mantis_pci_remove: Removing -->Mantis irq: 23,         latency: 32
[  110.297344]  memory: 0xefeff000, mmio: 0xf9218000
[  110.301326] Trying to free free IRQ23
[  110.303445] Trying to free nonexistent resource <efeff000-efefffff>

#include <asm/io.h>
#include <asm/pgtable.h>
#include <asm/page.h>
#include <linux/interrupt.h>
#include <linux/kmod.h>
#include <linux/vmalloc.h>
#include <linux/init.h>
#include <linux/sched.h>
#include <linux/device.h>
#include "mantis_common.h"
#include "mantis_dma.h"
#include "mantis_i2c.h"
#include "mantis_eeprom.h"

#define DRIVER_NAME "Mantis"

static struct pci_device_id mantis_pci_table[] = {
{ PCI_DEVICE(PCI_VENDOR_ID_MANTIS, PCI_DEVICE_ID_MANTIS_R11) }, { 0 },
};

MODULE_DEVICE_TABLE(pci, mantis_pci_table);

static irqreturn_t mantis_pci_irq(int irq, void *dev_id, struct pt_regs *regs) {
struct mantis_pci *mantis;

   dprintk(verbose, MANTIS_DEBUG, 1, "Mantis PCI IRQ");
   mantis = (struct mantis_pci *) dev_id;
   if (mantis == NULL)
       dprintk(verbose, MANTIS_DEBUG, 1, "Aeio, mantis ISR");
   /*    Events
    *    (1) PCMCIA insert
    *    (2) PCMCIA extract
    *    (3) I2C complete
    */

return IRQ_HANDLED;
}

static int mantis_i2c_setup(struct mantis_pci *mantis) {
u32 config = 0;

// mmwrite(0x80, MANTIS_DMA_CTL); // MCU i2c read
config = mmread(MANTIS_DMA_CTL);
dprintk(verbose, MANTIS_DEBUG, 1, "Mantis Ctl reg=0x%04x", config);

return 0;
}

static int mantis_reg_dump(struct mantis_pci *mantis) {
u32 ctlreg, intstat, intmask, i2cdata;

   ctlreg = mmread(MANTIS_DMA_CTL);
   intstat = mmread(MANTIS_INT_STAT);
   intmask = mmread(MANTIS_INT_MASK);
   i2cdata = mmread(MANTIS_I2C_DATA);
   dprintk(verbose, MANTIS_DEBUG, 1, "CTL_REG=0x%04x, INT_STAT=0x%04x, \
       INT_MASK=0x%04x, I2C_DATA=0x%04x", ctlreg, intstat,        \
       intmask, i2cdata);

return 0;
}

static int __devinit mantis_pci_probe(struct pci_dev *pdev,
const struct pci_device_id *mantis_pci_table)
{
u8 revision, latency;
// u8 data[2];
struct mantis_pci *mantis;
mantis = (struct mantis_pci *)

               kmalloc(sizeof (struct mantis_pci), GFP_KERNEL);
   if (mantis == NULL) {
       dprintk(verbose, MANTIS_ERROR, 1, "Out of memory");
       return -ENOMEM;
   }
   dprintk(verbose, MANTIS_ERROR, 1, "Got a device");
   mantis->mantis_addr = pci_resource_start(pdev, 0);
   if (!request_mem_region(pci_resource_start(pdev, 0),
       pci_resource_len(pdev, 0), DRIVER_NAME)) {
       dprintk(verbose, MANTIS_ERROR, 1, "Request mem region failed");
       goto err0;
   }
   if ((mantis->mantis_mmio =
               ioremap(mantis->mantis_addr, 0x1000)) == NULL) {
       dprintk(verbose, MANTIS_ERROR, 1, "IO remap failed");
       goto err1;
   }
   if (request_irq(pdev->irq, mantis_pci_irq, SA_SHIRQ |
               SA_INTERRUPT, DRIVER_NAME, mantis) < 0) {
       dprintk(verbose, MANTIS_ERROR, 1, "Mantis IRQ reg failed");
       goto err2;
   }
   dprintk(verbose, MANTIS_DEBUG, 1, "We got an IRQ");
   if (pci_enable_device(pdev)) {
       dprintk(verbose, MANTIS_ERROR, 1, "Mantis PCI enable failed");
       goto err3;       
   }
   dprintk(verbose, MANTIS_DEBUG, 1, "We finally enabled the device");
   pci_set_master(pdev);
   pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &latency);
   pci_read_config_byte(pdev, PCI_CLASS_REVISION, &revision);
   mantis->latency = latency;
   mantis->revision = revision;
   if (!latency) {
       pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 32);
   }
   pci_set_drvdata(pdev, mantis);   
   dprintk(verbose, MANTIS_ERROR, 0, "Mantis Rev %d, ", mantis->revision);
   dprintk(verbose, MANTIS_ERROR, 0, "irq: %d, latency: %d\n \
       memory: 0x%04x, mmio: %p\n", pdev->irq, mantis->latency,    \
       mantis->mantis_addr, mantis->mantis_mmio);   

err3:
free_irq(pdev->irq, pdev);
err2:
if (mantis->mantis_mmio)

       iounmap(mantis->mantis_mmio);
err1:
   release_mem_region(pci_resource_start(pdev, 0),
               pci_resource_len(pdev, 0));

err0:
kfree(mantis);

return 0;
}

static void __devexit mantis_pci_remove(struct pci_dev *pdev) {
struct mantis_pci *mantis = pci_get_drvdata(pdev);

   if (mantis == NULL) {
       dprintk(verbose, MANTIS_ERROR, 1, "Aeio, MAntis NULL ptr");
       return;
   }
   dprintk(verbose, MANTIS_ERROR, 1, "Removing -->Mantis irq: %d, \
       latency: %d\n memory: 0x%04x, mmio: 0x%p",
       pdev->irq, mantis->latency, mantis->mantis_addr,
       mantis->mantis_mmio);
   free_irq(pdev->irq, pdev);

release_mem_region(pci_resource_start(pdev, 0),

       pci_resource_len(pdev, 0));

pci_set_drvdata(pdev, NULL);
pci_disable_device(pdev);
kfree(mantis);
}

static struct pci_driver mantis_pci_driver = {
.name = "Mantis PCI combo driver",
.id_table = mantis_pci_table,
.probe = mantis_pci_probe,
.remove = mantis_pci_remove,
};

static int __devinit mantis_pci_init(void) {
return pci_register_driver(&mantis_pci_driver);
}

static void __devexit mantis_pci_exit(void)
{
pci_unregister_driver(&mantis_pci_driver);
}

module_init(mantis_pci_init);
module_exit(mantis_pci_exit);

MODULE_DESCRIPTION("Mantis PCI DTV bridge driver"); MODULE_AUTHOR("Manu Abraham"); MODULE_LICENSE("GPL");

#19 lore raw
Quoted 47 lines · Manu Abraham
Quoted 2 lines · Rolf Eike Beer

That is true, but you should not call pci_get_device() in this function at all.

I reworked the whole thing over .. but i am feeling that something's a bit wrong somewhere ..

The log i get on a load - unload ..

[  102.261264] mantis_pci_probe: Got a device
[  102.262852] mantis_pci_probe: We got an IRQ
[  102.264392] mantis_pci_probe: We finally enabled the device
[  102.266020] Mantis Rev 1, irq: 23, latency: 32
[  102.266118]          memory: 0xefeff000, mmio: f9218000
[  102.269162] Trying to free free IRQ23
[  110.297341] mantis_pci_remove: Removing -->Mantis irq: 23,
latency: 32
[  110.297344]  memory: 0xefeff000, mmio: 0xf9218000
[  110.301326] Trying to free free IRQ23
[  110.303445] Trying to free nonexistent resource <efeff000-efefffff>

#include <asm/io.h>
#include <asm/pgtable.h>
#include <asm/page.h>
#include <linux/interrupt.h>
#include <linux/kmod.h>
#include <linux/vmalloc.h>
#include <linux/init.h>
#include <linux/sched.h>
#include <linux/device.h>
#include "mantis_common.h"
#include "mantis_dma.h"
#include "mantis_i2c.h"
#include "mantis_eeprom.h"

#define DRIVER_NAME "Mantis"

static struct pci_device_id mantis_pci_table[] = {
   { PCI_DEVICE(PCI_VENDOR_ID_MANTIS, PCI_DEVICE_ID_MANTIS_R11) },
   { 0 },
};

MODULE_DEVICE_TABLE(pci, mantis_pci_table);

static irqreturn_t mantis_pci_irq(int irq, void *dev_id, struct pt_regs *regs)

Quoted 16 lines
{
   struct mantis_pci *mantis;

   dprintk(verbose, MANTIS_DEBUG, 1, "Mantis PCI IRQ");
   mantis = (struct mantis_pci *) dev_id;
   if (mantis == NULL)
       dprintk(verbose, MANTIS_DEBUG, 1, "Aeio, mantis ISR");

   /*    Events
    *    (1) PCMCIA insert
    *    (2) PCMCIA extract
    *    (3) I2C complete
    */

   return IRQ_HANDLED;
}

You must check here if this interrupt was really from your device. If not, you must return IRQ_NONE. You have requested your interrupt as a shared one so this may happen at any time.

Quoted 3 lines

static int mantis_i2c_setup(struct mantis_pci *mantis)

{
   u32 config = 0;

You don't need to set this here, you will overwrite it anyway before looking at it.

Quoted 7 lines

// mmwrite(0x80, MANTIS_DMA_CTL); // MCU i2c read

   config = mmread(MANTIS_DMA_CTL);
   dprintk(verbose, MANTIS_DEBUG, 1, "Mantis Ctl reg=0x%04x", config);

   return 0;
}
Quoted 6 lines

static int __devinit mantis_pci_probe(struct pci_dev *pdev,
const struct pci_device_id *mantis_pci_table)

{
   u8 revision, latency;
//    u8 data[2];
   struct mantis_pci *mantis;

Please insert a blank line here, this improves readability.

Quoted 4 lines
   mantis = (struct mantis_pci *)
               kmalloc(sizeof (struct mantis_pci), GFP_KERNEL);
   if (mantis == NULL) {
       dprintk(verbose, MANTIS_ERROR, 1, "Out of memory");
Quoted 8 lines
       return -ENOMEM;
   }
   dprintk(verbose, MANTIS_ERROR, 1, "Got a device");
   mantis->mantis_addr = pci_resource_start(pdev, 0);
   if (!request_mem_region(pci_resource_start(pdev, 0),
       pci_resource_len(pdev, 0), DRIVER_NAME)) {
       dprintk(verbose, MANTIS_ERROR, 1, "Request mem region failed");
       goto err0;

I would prefer to use more descriptive names for the labels, like err_memreg, err_iomem, err_irq or something like that.

Also you will get wrong assignements here. You must call pci_enable_device() _first_, it will set up the BARs of that device. Also I think pci_request_regions() might be a better way to get this assignements.

Quoted 45 lines
   }
   if ((mantis->mantis_mmio =
               ioremap(mantis->mantis_addr, 0x1000)) == NULL) {
       dprintk(verbose, MANTIS_ERROR, 1, "IO remap failed");
       goto err1;
   }
   if (request_irq(pdev->irq, mantis_pci_irq, SA_SHIRQ |
               SA_INTERRUPT, DRIVER_NAME, mantis) < 0) {
       dprintk(verbose, MANTIS_ERROR, 1, "Mantis IRQ reg failed");
       goto err2;
   }
   dprintk(verbose, MANTIS_DEBUG, 1, "We got an IRQ");
   if (pci_enable_device(pdev)) {
       dprintk(verbose, MANTIS_ERROR, 1, "Mantis PCI enable failed");
       goto err3;
   }
   dprintk(verbose, MANTIS_DEBUG, 1, "We finally enabled the device");
   pci_set_master(pdev);
   pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &latency);
   pci_read_config_byte(pdev, PCI_CLASS_REVISION, &revision);
   mantis->latency = latency;
   mantis->revision = revision;
   if (!latency) {
       pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 32);
   }
   pci_set_drvdata(pdev, mantis);
   dprintk(verbose, MANTIS_ERROR, 0, "Mantis Rev %d, ", mantis->revision);
   dprintk(verbose, MANTIS_ERROR, 0, "irq: %d, latency: %d\n \
       memory: 0x%04x, mmio: %p\n", pdev->irq, mantis->latency,    \
       mantis->mantis_addr, mantis->mantis_mmio);
err3:
   free_irq(pdev->irq, pdev);
err2:
   if (mantis->mantis_mmio)
       iounmap(mantis->mantis_mmio);
err1:
   release_mem_region(pci_resource_start(pdev, 0),
               pci_resource_len(pdev, 0));
err0:
   kfree(mantis);

   return 0;
}

static void __devexit mantis_pci_remove(struct pci_dev *pdev)

Quoted 15 lines
{
   struct mantis_pci *mantis = pci_get_drvdata(pdev);
   if (mantis == NULL) {
       dprintk(verbose, MANTIS_ERROR, 1, "Aeio, MAntis NULL ptr");
       return;
   }
   dprintk(verbose, MANTIS_ERROR, 1, "Removing -->Mantis irq: %d, \
       latency: %d\n memory: 0x%04x, mmio: 0x%p",
       pdev->irq, mantis->latency, mantis->mantis_addr,
       mantis->mantis_mmio);

   free_irq(pdev->irq, pdev);

   release_mem_region(pci_resource_start(pdev, 0),
       pci_resource_len(pdev, 0));

pci_release_regions(pdev);

Quoted 4 lines
   pci_set_drvdata(pdev, NULL);
   pci_disable_device(pdev);
   kfree(mantis);
}

HTH

Eike

#20 lore raw
Quoted 5 lines · Rolf Eike Beer

You must check here if this interrupt was really from your device. If not, you must return IRQ_NONE. You have requested your interrupt as a shared one so this may happen at any time.

Fixed this one, temporarily ..

Quoted 10 lines
Quoted 5 lines

static int mantis_i2c_setup(struct mantis_pci *mantis) {
u32 config = 0;

You don't need to set this here, you will overwrite it anyway before looking at it.

Oh, that was the remnants of some i2c tests that i was trying to do .. The other i2c parts i removed for testing ..

Quoted 5 lines

Also you will get wrong assignements here. You must call pci_enable_device() _first_, it will set up the BARs of that device. Also I think pci_request_regions() might be a better way to get this assignements.

Fixed that one too ..

Quoted 7 lines
Quoted 2 lines

pci_release_regions(pdev);

Fixed ..

So it now looks like this but i have another problem now, after consecutive, load/unload, i get an oops ..

-----------------------------------------------------------------

[  330.625715] mantis_pci_probe: <1:>IRQ=23
[  330.631130] mantis_pci_probe: <2:>IRQ=23
[  330.636138] mantis_pci_probe: Got a device
[  330.641635] mantis_pci_probe: We got an IRQ
[  330.647189] mantis_pci_probe: We finally enabled the device
[  330.652468] Mantis Rev 1, irq: 23, latency: 32
[  330.652577]  		memory: 0xefeff000, mmio: f9218000
[  332.615009] mantis_pci_remove: Removing -->Mantis irq: 23, 		latency: 32
[  332.615011]  memory: 0xefeff000, mmio: 0xf9218000
[  332.627714] Trying to free free IRQ23
[  334.131573] mantis_pci_probe: <1:>IRQ=23
[  334.138602] mantis_pci_probe: <2:>IRQ=23
[  334.145178] mantis_pci_probe: Got a device
[  334.152287] mantis_pci_probe: We got an IRQ
[  334.159038] mantis_pci_probe: We finally enabled the device
[  334.166326] Mantis Rev 1, irq: 23, latency: 32
[  334.166437]  		memory: 0xefeff000, mmio: f92a4000
[  335.716624] mantis_pci_remove: Removing -->Mantis irq: 23, 		latency: 32
[  335.716626]  memory: 0xefeff000, mmio: 0xf92a4000
[  335.732424] Trying to free free IRQ23
[  337.110714] mantis_pci_probe: <1:>IRQ=23
[  337.118943] mantis_pci_probe: <2:>IRQ=23
[  337.127573] mantis_pci_probe: Got a device
[  337.135847] Unable to handle kernel paging request at virtual address f92bd772
[  337.144907]  printing eip:
[  337.153542] c013e87f
[  337.162663] *pde = 37ecf067
[  337.171417] *pte = 00000000
[  337.180682] Oops: 0000 [#1]
[  337.189583] SMP [  337.198762] Modules linked in: mantis i2c_core mb86a15 dvb_core 3c59x piix sd_mod
[  337.208187] CPU:    0
[  337.208188] EIP:    0060:[<c013e87f>]    Not tainted VLI
[  337.208189] EFLAGS: 00010286   (2.6.13) [  337.235401] EIP is at name_unique+0x2f/0x60
[  337.244234] eax: 00000b4d   ebx: f6594ee0   ecx: 00000001   edx: f678e520
[  337.252586] esi: f92a7773   edi: f92bd772   ebp: f6594ee0   esp: f4a3fdb0
[  337.261383] ds: 007b   es: 007b   ss: 0068
[  337.269415] Process modprobe (pid: 2554, threadinfo=f4a3f000 task=f63e7a20)
[  337.269680] Stack: 00000000 00000017 00000017 c013e92f 00000017 f6594ee0 00000046 000030ec [  337.278569]        000030bb c0103a04 000030ec 00000000 000030ec 000030bb 00000296 00000008 [  337.287187]        00000001 f92bd000 00000000 c0110cca c0429250 00000008 c011cbfd 000030bb [  337.296352] Call Trace:
[  337.314072]  [<c013e92f>] register_handler_proc+0x7f/0xe0
[  337.323270]  [<c0103a04>] apic_timer_interrupt+0x1c/0x24
[  337.332356]  [<c0110cca>] smp_call_function+0x11a/0x170
[  337.341707]  [<c011cbfd>] release_console_sem+0x7d/0xc0
[  337.350561]  [<c011ca54>] vprintk+0x194/0x240
[  337.359944]  [<c0110b53>] flush_tlb_all+0x33/0x40
[  337.368978]  [<c013dfc9>] setup_irq+0xd9/0x120
[  337.378465]  [<f92a6000>] mantis_pci_irq+0x0/0x60 [mantis]
[  337.387679]  [<c013e1a5>] request_irq+0x85/0xa0
[  337.397350]  [<f92a6238>] mantis_pci_probe+0x138/0x3e0 [mantis]
[  337.406839]  [<f92a6000>] mantis_pci_irq+0x0/0x60 [mantis]
[  337.416374]  [<c020f38f>] __pci_device_probe+0x5f/0x70
[  337.426156]  [<c020f3cf>] pci_device_probe+0x2f/0x50
[  337.435457]  [<c0247c38>] driver_probe_device+0x38/0xb0
[  337.445227]  [<c0247d30>] __driver_attach+0x0/0x60
[  337.454521]  [<c0247d80>] __driver_attach+0x50/0x60
[  337.464090]  [<c0247219>] bus_for_each_dev+0x69/0x80
[  337.473215]  [<c0247db5>] driver_attach+0x25/0x30
[  337.482410]  [<c0247d30>] __driver_attach+0x0/0x60
[  337.492096]  [<c024776d>] bus_add_driver+0x8d/0xe0
[  337.501394]  [<c020f69b>] pci_register_driver+0x7b/0xa0
[  337.511118]  [<f92a65cf>] mantis_pci_init+0xf/0x20 [mantis]
[  337.520399]  [<c0139032>] sys_init_module+0x162/0x200
[  337.530161]  [<c0102f5f>] sysenter_past_esp+0x54/0x75
[  337.539433] Code: 44 24 10 8b 5c 24 14 c1 e0 07 8b 90 88 18 42 c0 85 d2 74 33 90 8d b4 26 00 00 00 00 39 da 74 20 8b 7a 0c 85 ff 74 19 8b 73 0c ac <ae> 75 08 84 c0 75 f8 31 c0 eb 04 19 c0 0c 01 31 c9 85 c0 74 0c [  337.560185]  

-----------------------------------------------------------------
#include <asm/io.h>
#include <asm/pgtable.h>
#include <asm/page.h>
#include <linux/interrupt.h>
#include <linux/kmod.h>
#include <linux/vmalloc.h>
#include <linux/init.h>
#include <linux/sched.h>
#include <linux/device.h>
#include "mantis_common.h"
#include "mantis_dma.h"
#include "mantis_i2c.h"
#include "mantis_eeprom.h"

unsigned int verbose = 1;
module_param(verbose, int, 0644);
MODULE_PARM_DESC(verbose, "verbose startup messages, default is 1 (yes)");

#define PCI_VENDOR_ID_MANTIS 0x1822
#define PCI_DEVICE_ID_MANTIS_R11 0x4e35
#define DRIVER_NAME "Mantis"

static struct pci_device_id mantis_pci_table[] = {
{ PCI_DEVICE(PCI_VENDOR_ID_MANTIS, PCI_DEVICE_ID_MANTIS_R11) }, { 0 },
};

MODULE_DEVICE_TABLE(pci, mantis_pci_table);

static irqreturn_t mantis_pci_irq(int irq, void *dev_id, struct pt_regs *regs) {
struct mantis_pci *mantis;

   dprintk(verbose, MANTIS_DEBUG, 1, "Mantis PCI IRQ");
   mantis = (struct mantis_pci *) dev_id;
   if (mantis == NULL)
       dprintk(verbose, MANTIS_DEBUG, 1, "Aeio, mantis ISR");
   /*    Events
    *    (1) PCMCIA insert
    *    (2) PCMCIA extract
    *    (3) I2C complete
    */

/*
return IRQ_HANDLED;
*/
return IRQ_NONE; // temporary, for now
}

static int mantis_i2c_setup(struct mantis_pci *mantis) {
u32 config = 0;

// mmwrite(0x80, MANTIS_DMA_CTL); // MCU i2c read
config = mmread(MANTIS_DMA_CTL);
dprintk(verbose, MANTIS_DEBUG, 1, "Mantis Ctl reg=0x%04x", config);

return 0;
}

static int mantis_reg_dump(struct mantis_pci *mantis) {
u32 ctlreg, intstat, intmask, i2cdata;

   ctlreg = mmread(MANTIS_DMA_CTL);
   intstat = mmread(MANTIS_INT_STAT);
   intmask = mmread(MANTIS_INT_MASK);
   i2cdata = mmread(MANTIS_I2C_DATA);
   dprintk(verbose, MANTIS_DEBUG, 1, "CTL_REG=0x%04x, INT_STAT=0x%04x, \
       INT_MASK=0x%04x, I2C_DATA=0x%04x", ctlreg, intstat,        \
       intmask, i2cdata);

return 0;
}

static int __devinit mantis_pci_probe(struct pci_dev *pdev,
const struct pci_device_id *mantis_pci_table)
{
u8 revision, latency;
// u8 data[2];
struct mantis_pci *mantis;

   dprintk(verbose, MANTIS_ERROR, 1, "<1:>IRQ=%d", pdev->irq);
   if (pci_enable_device(pdev)) {
       dprintk(verbose, MANTIS_ERROR, 1, "Mantis PCI enable failed");
       goto err;       
   }
   dprintk(verbose, MANTIS_ERROR, 1, "<2:>IRQ=%d", pdev->irq);

   mantis = (struct mantis_pci *)
               kmalloc(sizeof (struct mantis_pci), GFP_KERNEL);
   if (mantis == NULL) {
       dprintk(verbose, MANTIS_ERROR, 1, "Out of memory");
       return -ENOMEM;
   }
   dprintk(verbose, MANTIS_ERROR, 1, "Got a device");
   mantis->mantis_addr = pci_resource_start(pdev, 0);
   if (!request_mem_region(pci_resource_start(pdev, 0),
       pci_resource_len(pdev, 0), DRIVER_NAME)) {
       dprintk(verbose, MANTIS_ERROR, 1, "Request mem region failed");
       goto err0;
   }
   if ((mantis->mantis_mmio =
               ioremap(mantis->mantis_addr, 0x1000)) == NULL) {
       dprintk(verbose, MANTIS_ERROR, 1, "IO remap failed");
       goto err1;
   }
   mmwrite(0, MANTIS_INT_STAT);
   if (request_irq(pdev->irq, mantis_pci_irq, SA_SHIRQ |
               SA_INTERRUPT, DRIVER_NAME, mantis) < 0) {
       dprintk(verbose, MANTIS_ERROR, 1, "Mantis IRQ reg failed");
       goto err2;
   }
   dprintk(verbose, MANTIS_DEBUG, 1, "We got an IRQ");
   dprintk(verbose, MANTIS_DEBUG, 1, "We finally enabled the device");
   pci_set_master(pdev);
   pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &latency);
   pci_read_config_byte(pdev, PCI_CLASS_REVISION, &revision);
   mantis->latency = latency;
   mantis->revision = revision;
   if (!latency) {
       pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 32);
   }
   pci_set_drvdata(pdev, mantis);   
   dprintk(verbose, MANTIS_ERROR, 0, "Mantis Rev %d, ", mantis->revision);
   dprintk(verbose, MANTIS_ERROR, 0, "irq: %d, latency: %d\n \
       memory: 0x%04x, mmio: %p\n", pdev->irq, mantis->latency,    \
       mantis->mantis_addr, mantis->mantis_mmio);

return 0;
err2:
if (mantis->mantis_mmio)

       iounmap(mantis->mantis_mmio);
err1:
   release_mem_region(pci_resource_start(pdev, 0),
               pci_resource_len(pdev, 0));

err0:
kfree(mantis);
err:
return -ENODEV;
}

static void __devexit mantis_pci_remove(struct pci_dev *pdev) {
struct mantis_pci *mantis = pci_get_drvdata(pdev);

   if (mantis == NULL) {
       dprintk(verbose, MANTIS_ERROR, 1, "Aeio, MAntis NULL ptr");
       return;
   }
   dprintk(verbose, MANTIS_ERROR, 1, "Removing -->Mantis irq: %d, \
       latency: %d\n memory: 0x%04x, mmio: 0x%p",
       pdev->irq, mantis->latency, mantis->mantis_addr,
       mantis->mantis_mmio);

free_irq(pdev->irq, pdev);
// release_mem_region(pci_resource_start(pdev, 0),
// pci_resource_len(pdev, 0));
pci_release_regions(pdev);
pci_set_drvdata(pdev, NULL);
pci_disable_device(pdev);
kfree(mantis);
}

static struct pci_driver mantis_pci_driver = {
.name = "Mantis PCI combo driver",
.id_table = mantis_pci_table,
.probe = mantis_pci_probe,
.remove = mantis_pci_remove,
};

static int __devinit mantis_pci_init(void) {
return pci_register_driver(&mantis_pci_driver);
}

static void __devexit mantis_pci_exit(void)
{
pci_unregister_driver(&mantis_pci_driver);
}

module_init(mantis_pci_init);
module_exit(mantis_pci_exit);

MODULE_DESCRIPTION("Mantis PCI DTV bridge driver"); MODULE_AUTHOR("Manu Abraham"); MODULE_LICENSE("GPL");

Manu

#21 lore raw
Quoted 2 lines · Manu Abraham

So it now looks like this but i have another problem now, after consecutive, load/unload, i get an oops ..

No idea, sorry.

Quoted 16 lines

static int __devinit mantis_pci_probe(struct pci_dev *pdev,
const struct pci_device_id *mantis_pci_table)

{
   u8 revision, latency;
//    u8 data[2];
   struct mantis_pci *mantis;

   dprintk(verbose, MANTIS_ERROR, 1, "<1:>IRQ=%d", pdev->irq);
   if (pci_enable_device(pdev)) {
       dprintk(verbose, MANTIS_ERROR, 1, "Mantis PCI enable failed");
       goto err;
   }
   dprintk(verbose, MANTIS_ERROR, 1, "<2:>IRQ=%d", pdev->irq);

   mantis = (struct mantis_pci *)
               kmalloc(sizeof (struct mantis_pci), GFP_KERNEL);

mantis = kmalloc(sizeof(*mantis), GFP_KERNEL);

You don't have to cast a void* to any other pointer and this way you will always get the correct size of memory allocated, even if mantis will become another pointer type.

Eike

#22 lore raw
Quoted 27 lines · Rolf Eike Beer
Quoted 4 lines · Manu Abraham

So it now looks like this but i have another problem now, after consecutive, load/unload, i get an oops ..

No idea, sorry.

Quoted 14 lines

static int __devinit mantis_pci_probe(struct pci_dev *pdev,
const struct pci_device_id *mantis_pci_table)
{
u8 revision, latency;
// u8 data[2];
struct mantis_pci *mantis;

  dprintk(verbose, MANTIS_ERROR, 1, "<1:>IRQ=%d", pdev->irq);
  if (pci_enable_device(pdev)) {
      dprintk(verbose, MANTIS_ERROR, 1, "Mantis PCI enable failed");
      goto err;
  }
  dprintk(verbose, MANTIS_ERROR, 1, "<2:>IRQ=%d", pdev->irq);
Quoted 11 lines
Quoted 4 lines
  mantis = (struct mantis_pci *)
              kmalloc(sizeof (struct mantis_pci), GFP_KERNEL);

mantis = kmalloc(sizeof(*mantis), GFP_KERNEL);

You don't have to cast a void* to any other pointer and this way you will always get the correct size of memory allocated, even if mantis will become another pointer type.

Thanks i will keep it in mind, while i get on with the rest. Stuck on with the Oops at the moment. FIrst load unload seems to be okay, second one also, but sometimes it is the 3rd load, or sometimes it is the unload ..

Sounds a bit weird also ..

Thanks,
Manu

#23 lore raw
In reply to Rolf Eike Beer
Quoted 8 lines · Rolf Eike Beer
Quoted 4 lines · Manu Abraham

So it now looks like this but i have another problem now, after consecutive, load/unload, i get an oops ..

Ah, one more thing i noticed, in the probe function, whatever i do to generate interrupts, the interrupt handler does not receive any interrupts, which makes me to think whether request_irq() really yielded me an IRQ.

One more thing after a module load unload cycle, i did a cat /proc/interrupts, which led to another oops.. Which makes me to think that request_irq did not work out as expected or something like that. Any clues .. ?

Regards,
Manu

[ 4001.876841] mantis_pci_probe: <1:>IRQ=23
[ 4001.877014] mantis_pci_probe: <2:>IRQ=23
[ 4001.877176] mantis_pci_probe: Got a device
[ 4001.877356] mantis_pci_probe: We got an IRQ
[ 4001.877514] mantis_pci_probe: We finally enabled the device
[ 4001.877730] Mantis Rev 1, irq: 23, latency: 32   [ 4001.877836] memory: 0xefeff000, mmio: f9218000
[ 4009.156947] mantis_pci_remove: Removing -->Mantis irq: 23,         latency: 32
[ 4009.156949]  memory: 0xefeff000, mmio: 0xf9218000
[ 4009.157391] Trying to free free IRQ23
[ 4516.358401] Unable to handle kernel paging request at virtual address f92bd7e2
[ 4516.360543]  printing eip:
[ 4516.362599] c02086fe
[ 4516.364574] *pde = 37ecf067
[ 4516.366591] *pte = 00000000
[ 4516.368554] Oops: 0000 [#1]
[ 4516.370304] SMP
[ 4516.372372] Modules linked in: i2c_core mb86a15 dvb_core 3c59x piix sd_mod
[ 4516.374903] CPU:    0
[ 4516.374904] EIP:    0060:[<c02086fe>]    Not tainted VLI
[ 4516.374906] EFLAGS: 00010097   (2.6.13)
[ 4516.382874] EIP is at vsnprintf+0x35e/0x4f0
[ 4516.385638] eax: f92bd7e2   ebx: 0000000a   ecx: f92bd7e2   edx: fffffffe
[ 4516.388683] esi: f4322122   edi: 00000000   ebp: f4322fff   esp: f2b0fec0
[ 4516.391978] ds: 007b   es: 007b   ss: 0068
[ 4516.395387] Process cat (pid: 3375, threadinfo=f2b0f000 task=f3c61020)
[ 4516.395641] Stack: f2b0ff04 f4322fff 00000000 00000000 0000000a 0000000a 00000000 00000000
[ 4516.399463]        ffffffff ffffffff f2e328c0 f5f5dfa0 00000017 f2e328c0 c01801d7 f4322120
[ 4516.403230]        00000ee0 c0369c81 f2b0ff20 00000008 c0105a4c f2e328c0 c0369c7e f92bd7e2
[ 4516.407497] Call Trace:
[ 4516.415981]  [<c01801d7>] seq_printf+0x37/0x60
[ 4516.420212]  [<c0105a4c>] show_interrupts+0x2bc/0x3b0
[ 4516.424905]  [<c017fcd6>] seq_read+0x1d6/0x2d0
[ 4516.429671]  [<c015e186>] vfs_read+0xb6/0x180
[ 4516.434507]  [<c015e531>] sys_read+0x51/0x80
[ 4516.439056]  [<c0102f5f>] sysenter_past_esp+0x54/0x75
[ 4516.444082] Code: 00 83 cf 01 89 44 24 24 eb bb 8b 44 24 48 8b 54 24 20 83 44 24 48 04 8b 08 b8 3c 95 36 c0 81 f9 ff 0f 00 00 0f 46 c8 89 c8 eb 06 <80> 38 00 74 07 40 4a 83 fa ff 75 f4 29 c8 83 e7 10 89 c3 75 20
[ 4516.456263] 
#24 lore raw
In reply to Manu Abraham

Hi Manu,

Quoted 11 lines · Manu Abraham
[  102.261264] mantis_pci_probe: Got a device
[  102.262852] mantis_pci_probe: We got an IRQ
[  102.264392] mantis_pci_probe: We finally enabled the device
[  102.266020] Mantis Rev 1, irq: 23, latency: 32
[  102.266118]          memory: 0xefeff000, mmio: f9218000
[  102.269162] Trying to free free IRQ23
[  110.297341] mantis_pci_remove: Removing -->Mantis irq: 23,         
latency: 32
[  110.297344]  memory: 0xefeff000, mmio: 0xf9218000
[  110.301326] Trying to free free IRQ23
[  110.303445] Trying to free nonexistent resource <efeff000-efefffff>

I think you should call pci_enable_device() before request_irq, etc. AFAIK, the pci_enable_device() can change resources like IRQ. That's probably what causes these errors. Just print out the irq number before and after pci_enable_device() to check if that's the problem.

Ralph

#25 lore raw
Quoted 4 lines · Ralph Metzler

Hi Manu,

Hello Ralph,

It's been a long time since heard your voice. I thought you had been damn busy. Nice to hear from you.

Quoted 13 lines
Quoted 10 lines · Manu Abraham
[  102.261264] mantis_pci_probe: Got a device
[  102.262852] mantis_pci_probe: We got an IRQ
[  102.264392] mantis_pci_probe: We finally enabled the device
[  102.266020] Mantis Rev 1, irq: 23, latency: 32
[  102.266118]          memory: 0xefeff000, mmio: f9218000
[  102.269162] Trying to free free IRQ23
[  110.297341] mantis_pci_remove: Removing -->Mantis irq: 23,         latency: 32
[  110.297344]  memory: 0xefeff000, mmio: 0xf9218000
[  110.301326] Trying to free free IRQ23
[  110.303445] Trying to free nonexistent resource <efeff000-efefffff>
Quoted 3 lines

I think you should call pci_enable_device() before request_irq, etc.

Sure i will try that out..

Quoted 5 lines

AFAIK, the pci_enable_device() can change resources like IRQ.
That's probably what causes these errors. Just print out the irq number before and after pci_enable_device() to check if that's the problem.

I will check this out. I will come back on this soon.

Regards,
Manu

#26 lore raw
In reply to Ralph Metzler
Quoted 15 lines · Ralph Metzler

Hi Manu,

Quoted 10 lines · Manu Abraham
[  102.261264] mantis_pci_probe: Got a device
[  102.262852] mantis_pci_probe: We got an IRQ
[  102.264392] mantis_pci_probe: We finally enabled the device
[  102.266020] Mantis Rev 1, irq: 23, latency: 32
[  102.266118]          memory: 0xefeff000, mmio: f9218000
[  102.269162] Trying to free free IRQ23
[  110.297341] mantis_pci_remove: Removing -->Mantis irq: 23,         latency: 32
[  110.297344]  memory: 0xefeff000, mmio: 0xf9218000
[  110.301326] Trying to free free IRQ23
[  110.303445] Trying to free nonexistent resource <efeff000-efefffff>
Quoted 7 lines

I think you should call pci_enable_device() before request_irq, etc. AFAIK, the pci_enable_device() can change resources like IRQ.

That's probably what causes these errors. Just print out the irq number before and after pci_enable_device() to check if that's the problem.

Hmm.. not much of a change i can say ..

Manu

[  631.211320] mantis_pci_probe: <1:>IRQ=23
[  631.211495] mantis_pci_probe: <2:>IRQ=23
[  631.211664] mantis_pci_probe: Got a device
[  631.211850] mantis_pci_probe: We got an IRQ
[  631.212013] mantis_pci_probe: We finally enabled the device
[  631.212236] Mantis Rev 1, irq: 23, latency: 32
[  631.212322]          memory: 0xefeff000, mmio: f9218000
[  639.259136] mantis_pci_remove: Removing -->Mantis irq: 23,         latency: 32
[  639.259138]  memory: 0xefeff000, mmio: 0xf9218000
[  639.259504] Trying to free free IRQ23
[  639.259673] Trying to free nonexistent resource <efeff000-efefffff>

static int __devinit mantis_pci_probe(struct pci_dev *pdev,
const struct pci_device_id *mantis_pci_table)
{
u8 revision, latency;
// u8 data[2];
struct mantis_pci *mantis;

   dprintk(verbose, MANTIS_ERROR, 1, "<1:>IRQ=%d", pdev->irq);
   if (pci_enable_device(pdev)) {
       dprintk(verbose, MANTIS_ERROR, 1, "Mantis PCI enable failed");
       goto err;       
   }
   dprintk(verbose, MANTIS_ERROR, 1, "<2:>IRQ=%d", pdev->irq);

mantis = (struct mantis_pci *)

               kmalloc(sizeof (struct mantis_pci), GFP_KERNEL);
   if (mantis == NULL) {
       dprintk(verbose, MANTIS_ERROR, 1, "Out of memory");
       return -ENOMEM;
   }
   dprintk(verbose, MANTIS_ERROR, 1, "Got a device");
   mantis->mantis_addr = pci_resource_start(pdev, 0);
   if (!request_mem_region(pci_resource_start(pdev, 0),
       pci_resource_len(pdev, 0), DRIVER_NAME)) {
       dprintk(verbose, MANTIS_ERROR, 1, "Request mem region failed");
       goto err0;
   }
   if ((mantis->mantis_mmio =
               ioremap(mantis->mantis_addr, 0x1000)) == NULL) {
       dprintk(verbose, MANTIS_ERROR, 1, "IO remap failed");
       goto err1;
   }
   if (request_irq(pdev->irq, mantis_pci_irq, SA_SHIRQ |
               SA_INTERRUPT, DRIVER_NAME, mantis) < 0) {
       dprintk(verbose, MANTIS_ERROR, 1, "Mantis IRQ reg failed");
       goto err2;
   }
   dprintk(verbose, MANTIS_DEBUG, 1, "We got an IRQ");
   dprintk(verbose, MANTIS_DEBUG, 1, "We finally enabled the device");
   pci_set_master(pdev);
   pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &latency);
   pci_read_config_byte(pdev, PCI_CLASS_REVISION, &revision);
   mantis->latency = latency;
   mantis->revision = revision;
   if (!latency) {
       pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 32);
   }
   pci_set_drvdata(pdev, mantis);   
   dprintk(verbose, MANTIS_ERROR, 0, "Mantis Rev %d, ", mantis->revision);
   dprintk(verbose, MANTIS_ERROR, 0, "irq: %d, latency: %d\n \
       memory: 0x%04x, mmio: %p\n", pdev->irq, mantis->latency,    \
       mantis->mantis_addr, mantis->mantis_mmio);   
err2:
   if (mantis->mantis_mmio)
       iounmap(mantis->mantis_mmio);
err1:
   release_mem_region(pci_resource_start(pdev, 0),
               pci_resource_len(pdev, 0));

err0:
kfree(mantis);
err:
return 0;
}

#27 lore raw

Am Donnerstag, 15. September 2005 13:42 schrieb Manu Abraham:

Quoted 26 lines
Quoted 15 lines · Ralph Metzler

Hi Manu,

Quoted 11 lines · Manu Abraham
[  102.261264] mantis_pci_probe: Got a device
[  102.262852] mantis_pci_probe: We got an IRQ
[  102.264392] mantis_pci_probe: We finally enabled the device
[  102.266020] Mantis Rev 1, irq: 23, latency: 32
[  102.266118]          memory: 0xefeff000, mmio: f9218000
[  102.269162] Trying to free free IRQ23
[  110.297341] mantis_pci_remove: Removing -->Mantis irq: 23,
latency: 32
[  110.297344]  memory: 0xefeff000, mmio: 0xf9218000
[  110.301326] Trying to free free IRQ23
[  110.303445] Trying to free nonexistent resource <efeff000-efefffff>
Quoted 7 lines

I think you should call pci_enable_device() before request_irq, etc. AFAIK, the pci_enable_device() can change resources like IRQ.

That's probably what causes these errors. Just print out the irq number before and after pci_enable_device() to check if that's the problem.

Hmm.. not much of a change i can say ..

Quoted 12 lines
[  631.211320] mantis_pci_probe: <1:>IRQ=23
[  631.211495] mantis_pci_probe: <2:>IRQ=23
[  631.211664] mantis_pci_probe: Got a device
[  631.211850] mantis_pci_probe: We got an IRQ
[  631.212013] mantis_pci_probe: We finally enabled the device
[  631.212236] Mantis Rev 1, irq: 23, latency: 32
[  631.212322]          memory: 0xefeff000, mmio: f9218000
[  639.259136] mantis_pci_remove: Removing -->Mantis irq: 23,
latency: 32
[  639.259138]  memory: 0xefeff000, mmio: 0xf9218000
[  639.259504] Trying to free free IRQ23
[  639.259673] Trying to free nonexistent resource <efeff000-efefffff>

Oh yes, of course. Now I see what's wrong. See changes below.

Quoted 44 lines

static int __devinit mantis_pci_probe(struct pci_dev *pdev,
const struct pci_device_id *mantis_pci_table)

{
   u8 revision, latency;
//    u8 data[2];
   struct mantis_pci *mantis;

   dprintk(verbose, MANTIS_ERROR, 1, "<1:>IRQ=%d", pdev->irq);
   if (pci_enable_device(pdev)) {
       dprintk(verbose, MANTIS_ERROR, 1, "Mantis PCI enable failed");
       goto err;
   }
   dprintk(verbose, MANTIS_ERROR, 1, "<2:>IRQ=%d", pdev->irq);

   mantis = (struct mantis_pci *)
               kmalloc(sizeof (struct mantis_pci), GFP_KERNEL);
   if (mantis == NULL) {
       dprintk(verbose, MANTIS_ERROR, 1, "Out of memory");
       return -ENOMEM;
   }
   dprintk(verbose, MANTIS_ERROR, 1, "Got a device");
   mantis->mantis_addr = pci_resource_start(pdev, 0);
   if (!request_mem_region(pci_resource_start(pdev, 0),
       pci_resource_len(pdev, 0), DRIVER_NAME)) {
       dprintk(verbose, MANTIS_ERROR, 1, "Request mem region failed");
       goto err0;
   }
   if ((mantis->mantis_mmio =
               ioremap(mantis->mantis_addr, 0x1000)) == NULL) {
       dprintk(verbose, MANTIS_ERROR, 1, "IO remap failed");
       goto err1;
   }
   if (request_irq(pdev->irq, mantis_pci_irq, SA_SHIRQ |
               SA_INTERRUPT, DRIVER_NAME, mantis) < 0) {
       dprintk(verbose, MANTIS_ERROR, 1, "Mantis IRQ reg failed");
       goto err2;
   }
   dprintk(verbose, MANTIS_DEBUG, 1, "We got an IRQ");
   dprintk(verbose, MANTIS_DEBUG, 1, "We finally enabled the device");
   pci_set_master(pdev);
   pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &latency);
   pci_read_config_byte(pdev, PCI_CLASS_REVISION, &revision);
   mantis->latency = latency;
   mantis->revision = revision;
Quoted 8 lines
   if (!latency) {
       pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 32);
   }
   pci_set_drvdata(pdev, mantis);
   dprintk(verbose, MANTIS_ERROR, 0, "Mantis Rev %d, ", mantis->revision);
   dprintk(verbose, MANTIS_ERROR, 0, "irq: %d, latency: %d\n \
       memory: 0x%04x, mmio: %p\n", pdev->irq, mantis->latency,    \
       mantis->mantis_addr, mantis->mantis_mmio);

return 0;

Quoted 9 lines

err2:

   if (mantis->mantis_mmio)
       iounmap(mantis->mantis_mmio);
err1:
   release_mem_region(pci_resource_start(pdev, 0),
               pci_resource_len(pdev, 0));
err0:
   kfree(mantis);

err:

return -ENODEV.

Quoted 2 lines
   return 0;
}

You release the regions directly after you've claimed them. Freeing them again in release function is impossible because you don't own them any longer.

Eike

#28 lore raw
In reply to Manu Abraham
Quoted 14 lines · Manu Abraham

Ralph Metzler wrote:

               SA_INTERRUPT, DRIVER_NAME, mantis) < 0) {
       dprintk(verbose, MANTIS_ERROR, 1, "Mantis IRQ reg failed");
       goto err2;
   }
   dprintk(verbose, MANTIS_DEBUG, 1, "We got an IRQ");
   dprintk(verbose, MANTIS_DEBUG, 1, "We finally enabled the device");
   pci_set_master(pdev);
   pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &latency);
   pci_read_config_byte(pdev, PCI_CLASS_REVISION, &revision);
   mantis->latency = latency;
   mantis->revision = revision;
Quoted 8 lines
   if (!latency) {
       pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 32);
   }
   pci_set_drvdata(pdev, mantis);      dprintk(verbose, MANTIS_ERROR, 0,
"Mantis Rev %d, ", mantis->revision);
   dprintk(verbose, MANTIS_ERROR, 0, "irq: %d, latency: %d\n \
       memory: 0x%04x, mmio: %p\n", pdev->irq, mantis->latency,    \
       mantis->mantis_addr, mantis->mantis_mmio);

Success! So don't enter the failure path, return 0 here.

Quoted 10 lines

err2:

   if (mantis->mantis_mmio)
       iounmap(mantis->mantis_mmio);
err1:
   release_mem_region(pci_resource_start(pdev, 0),
               pci_resource_len(pdev, 0));
err0:
   kfree(mantis);
err:
   return 0;

This is your failure path, return nonzero here, preferable describing the error condition.

Tony

#29 lore raw
Quoted 37 lines · Antonino A. Daplas
Quoted 14 lines · Manu Abraham

Ralph Metzler wrote:

               SA_INTERRUPT, DRIVER_NAME, mantis) < 0) {
       dprintk(verbose, MANTIS_ERROR, 1, "Mantis IRQ reg failed");
       goto err2;
   }
   dprintk(verbose, MANTIS_DEBUG, 1, "We got an IRQ");
   dprintk(verbose, MANTIS_DEBUG, 1, "We finally enabled the device");
   pci_set_master(pdev);
   pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &latency);
   pci_read_config_byte(pdev, PCI_CLASS_REVISION, &revision);
   mantis->latency = latency;
   mantis->revision = revision;
Quoted 8 lines
   if (!latency) {
       pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 32);
   }
   pci_set_drvdata(pdev, mantis);      dprintk(verbose, MANTIS_ERROR, 0,
"Mantis Rev %d, ", mantis->revision);
   dprintk(verbose, MANTIS_ERROR, 0, "irq: %d, latency: %d\n \
       memory: 0x%04x, mmio: %p\n", pdev->irq, mantis->latency,    \
       mantis->mantis_addr, mantis->mantis_mmio);

Success! So don't enter the failure path, return 0 here.

Quoted 10 lines

err2:

   if (mantis->mantis_mmio)
       iounmap(mantis->mantis_mmio);
err1:
   release_mem_region(pci_resource_start(pdev, 0),
               pci_resource_len(pdev, 0));
err0:
   kfree(mantis);
err:
   return 0;
Quoted 2 lines

This is your failure path, return nonzero here, preferable describing the error condition.

Hey Tony, you get a penalty for not using PGP. If you had used it my mail would not have been sent 7 seconds after yours :))

Eike

#30 lore raw
In reply to Antonino A. Daplas
Quoted 4 lines · Antonino A. Daplas

This is your failure path, return nonzero here, preferable describing the error condition.

Thanks a lot guys, I mean all of you. Otherwise i would have pulled out my hair for some more time !

The IRQ issue still i don't feel a too comfortable though.

Thanks,
Manu

[ 3689.479339] mantis_pci_probe: <1:>IRQ=23
[ 3689.479518] mantis_pci_probe: <2:>IRQ=23
[ 3689.479685] mantis_pci_probe: Got a device
[ 3689.479869] mantis_pci_probe: We got an IRQ
[ 3689.480036] mantis_pci_probe: We finally enabled the device
[ 3689.480290] Mantis Rev 1, irq: 23, latency: 32
[ 3689.480403]          memory: 0xefeff000, mmio: f92a4000
[ 3695.984470] mantis_pci_remove: Removing -->Mantis irq: 23,         latency: 32
[ 3695.984473]  memory: 0xefeff000, mmio: 0xf92a4000
[ 3695.984934] Trying to free free IRQ23
#31 lore raw
In reply to Manu Abraham

Am Donnerstag, 15. September 2005 00:27 schrieben Sie:

Quoted 44 lines
Quoted 12 lines · Jiri Slaby

Manu Abraham napsal(a):

Quoted 11 lines
Quoted 9 lines · Jiri Slaby

Manu Abraham napsal(a):

Quoted 8 lines
Quoted 2 lines · Jiri Slaby

you do NOT do this at all, because you have pdev already (the param of the probe function)

I rewrote the entire thing like this including the pci_remove function too, but now it so seems that in the remove function, pci_get_drvdata(pdev) returns NULL, and hence i get an Oops at module removal.

Quoted 4 lines
Quoted 2 lines

I just found that, pci_enable_device() fails. So what's the way to go ahead ?

JESUS.

Hmm.. i finally got it to work. It seems pci_get_device() is necessary, i can't seem to enable the device or request for an IRQ the way you suggested. It looks some quirks are there though ..

If only i could explain why it works this way and not the other way ..

Thanks for the help,
Regards,
Manu

[   81.269655] mantis_pci_probe: Got a device
[   81.269825] mantis_pci_probe: We got an IRQ
[   81.269987] mantis_pci_probe: We finally enabled the device
[   81.270191] Mantis Rev 1, irq: 23, latency: 32
[   81.270289] memory: 0xefeff000, mmio: f9218000
[   81.270519] Trying to free free IRQ23
[   90.485885] mantis_pci_remove: Removing -->Mantis irq: 23, latency: 32
[   90.485887] memory: 0xefeff000, mmio: 0xf9218000
[   90.486293] Trying to free free IRQ23
[   90.486429] Trying to free nonexistent resource <efeff000-efefffff>
Quoted 8 lines

static int __devinit mantis_pci_probe(struct pci_dev *pdev, const struct pci_device_id *mantis_pci_table)

{
	u8 revision, latency;
	u8 data[2];
	struct mantis_pci *mantis;
	mantis = (struct mantis_pci *) kmalloc(sizeof (struct mantis_pci),

GFP_KERNEL);

Cast is unneeded.

Quoted 7 lines
	if (mantis == NULL) {
		dprintk(verbose, MANTIS_ERROR, 1, "Out of memory");
		return -ENOMEM;
	}

	pdev = pci_get_device(PCI_VENDOR_ID_MANTIS, PCI_DEVICE_ID_MANTIS_R11,

NULL);

Hm, this is wrong. pdev contains the struct pci_dev of the found device. If you do it your way the first device is always used (and the found one is ignored). When you have two mantis cards in your system it will happily trash the reference counting. The driver needs to work if you comment out this line.

Quoted 1 line
	if (pdev) {

This if is not needed either, the probe function is only called if pdev is set.

Quoted 14 lines
		dprintk(verbose, MANTIS_ERROR, 1, "Got a device");
		mantis->mantis_addr = pci_resource_start(pdev, 0);
		if (!request_mem_region(pci_resource_start(pdev, 0),
			pci_resource_len(pdev, 0), DRIVER_NAME)) {
			dprintk(verbose, MANTIS_ERROR, 1, "Request for memory region failed");
			goto err0;
		}
		if ((mantis->mantis_mmio = ioremap(mantis->mantis_addr, 0x1000)) ==
NULL) {
			dprintk(verbose, MANTIS_ERROR, 1, "IO remap failed");
			goto err1;
		}
		if (request_irq(pdev->irq, (void *) mantis_pci_irq, SA_SHIRQ |
						SA_INTERRUPT, DRIVER_NAME, (void *) mantis) < 0) {
Quoted 8 lines
			dprintk(verbose, MANTIS_ERROR, 1, "Mantis IRQ registration failed");
			goto err2;
		}
		dprintk(verbose, MANTIS_DEBUG, 1, "We got an IRQ");
		if (pci_enable_device(pdev)) {
			dprintk(verbose, MANTIS_ERROR, 1, "Mantis PCI device enable failed");
			goto err3;
		}

IIRC the call to pci_enable_device() must be the first thing you do. This will do the things like assigning memory regions to the device and so on.

Quoted 13 lines
		dprintk(verbose, MANTIS_DEBUG, 1, "We finally enabled the device");
		pci_set_master(pdev);
		pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &latency);
		pci_read_config_byte(pdev, PCI_CLASS_REVISION, &revision);
		mantis->latency = latency;
		mantis->revision = revision;
		if (!latency) {
			pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 32);
		}
		pci_set_drvdata(pdev, mantis);
		dprintk(verbose, MANTIS_ERROR, 0, "Mantis Rev %d, ", mantis->revision);
		dprintk(verbose, MANTIS_ERROR, 0, "irq: %d, latency: %d\nmemory:

0x%04x, mmio: %p\n", pdev->irq, mantis->latency,

Quoted 8 lines
			mantis->mantis_addr, mantis->mantis_mmio);

		pci_dev_put(pdev);

	} else {
		dprintk(verbose, MANTIS_ERROR, 1, "No device found");
		return -ENODEV;
	}

return 0;

Quoted 13 lines

err3:

	free_irq(pdev->irq, pdev);
err2:
	if (mantis->mantis_mmio)
		iounmap(mantis->mantis_mmio);
err1:
	release_mem_region(pci_resource_start(pdev, 0),
				pci_resource_len(pdev, 0));
err0:
	kfree(mantis);

	return 0;
}

Returning 0 in error cases is just wrong. And you free the assignments even in case of success AFAICS. Try the return I introduced above and see what happens.

Eike

#32 lore raw
Quoted 4 lines · Rolf Eike Beer

IIRC the call to pci_enable_device() must be the first thing you do. This will do the things like assigning memory regions to the device and so on.

I fixed this one

Quoted 4 lines

Returning 0 in error cases is just wrong. And you free the assignments even in case of success AFAICS. Try the return I introduced above and see what happens.

I fixed this one too ..

I have fixed most of the stuff, it is partly working, not ready yet as there are some more things to be added to .. I have attached what i was working on.

Thanks,
Manu

/*
Mantis PCI bridge driver

Copyright (C) 2005 Manu Abraham ([email protected])

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#ifndef _MANTIS_COMMON_H_
#define _MANTIS_COMMON_H_

#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kernel.h>
#include <linux/pci.h>

#include "dvbdev.h"
#include "dvb_demux.h"
#include "dmxdev.h"
#include "dvb_frontend.h"
#include "dvb_net.h"
#include <linux/i2c.h>
#include "mantis_reg.h"

#define MANTIS_ERROR 0
#define MANTIS_NOTICE 1
#define MANTIS_INFO 2
#define MANTIS_DEBUG 3

#define dprintk(x, y, z, format, arg...) do {						\
	if (z) {									\
		if	((x > MANTIS_ERROR) && (x > y))					\
			printk(KERN_ERR "%s: " format "\n" , __FUNCTION__ , ##arg);	\
		else if	((x > MANTIS_NOTICE) && (x > y))				\
			printk(KERN_NOTICE "%s: " format "\n" , __FUNCTION__ , ##arg);	\
		else if ((x > MANTIS_INFO) && (x > y))					\
			printk(KERN_INFO "%s: " format "\n" , __FUNCTION__ , ##arg);	\
		else if ((x > MANTIS_DEBUG) && (x > y))					\
			printk(KERN_DEBUG "%s: " format "\n" , __FUNCTION__ , ##arg);	\
	} else {									\
		if (x > y)								\
			printk(format , ##arg);						\
	}										\
} while(0)

#define mwrite(dat, addr) writel((dat), addr)
#define mread(addr) readl(addr)

#define mmwrite(dat, addr) mwrite((dat), (mantis->mantis_mmio + (addr)))
#define mmread(addr) mread(mantis->mantis_mmio + (addr))
#define mmand(dat, addr) mmwrite((dat) & mmread(addr), addr)
#define mmor(dat, addr) mmwrite((dat) | mmread(addr), addr)
#define mmaor(dat, addr) mmwrite((dat) | ((mask) & mmread(addr)), addr)

struct mantis_pci {
	/*	PCI stuff	*/
	__u16 vendor_id;
	__u16 device_id;
	__u8  latency;

	/*	Linux PCI	*/
	struct pci_dev *pdev;

	unsigned long mantis_addr;
	volatile void __iomem *mantis_mmio;

	__u8  irq;
	__u8  revision;

	__u16 mantis_card_num;
	__u16 ts_size;

	/*	RISC Core	*/
	volatile __u32 finished_block;
	volatile __u32 last_block;

	__u32 block_count;
	__u32 block_bytes;
	__u32 line_bytes;
	__u32 line_count;

	__u32 risc_pos;

	__u32 buf_size;
	__u8  *buf_cpu;
	dma_addr_t buf_dma;

	__u32 risc_size;
	__u32 *risc_cpu;
	dma_addr_t risc_dma;

	struct	tasklet_struct tasklet;

	struct	i2c_adapter adapter;
	int	i2c_rc;

	/*	DVB stuff	*/
	struct  dvb_adapter dvb_adapter;
	struct  dvb_frontend *fe;
	struct  dvb_demux demux;
	struct  dmxdev dmxdev;
	struct  dmx_frontend fe_hw;
	struct  dmx_frontend fe_mem;
	struct  dvb_net dvbnet;

	__u8	feeds;

	struct  mantis_config *config;

	__u32 mantis_int_stat;
	__u32 mantis_int_mask;

	/*	board specific		*/
	__u8  mac_address[8];
	__u32 sub_vendor_id;
	__u32 sub_device_id;
};

extern unsigned int verbose;
extern int mantis_dvb_init(struct mantis_pci *mantis); extern int mantis_frontend_init(struct mantis_pci *mantis); extern int mantis_dvb_exit(struct mantis_pci *mantis); extern void mantis_dma_xfer(unsigned long data);

#endif

/*
Mantis PCI bridge driver

Copyright (C) 2005 Manu Abraham ([email protected])

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "mantis_common.h"
#include "mantis_core.h"

static int read_eeprom_byte(struct mantis_pci *mantis, u8 *data, u8 length)

{
	int err;
	struct i2c_msg msg = {
		.addr = 0x50,
		.flags = I2C_M_RD,
		.buf = data,
		.len = length
	};
	if ((err = i2c_transfer(&mantis->adapter, &msg, 1)) < 0) {
		dprintk(verbose, MANTIS_ERROR, 1, "ERROR: i2c read: < err=%i d0=0x%02x d1=0x%02x >", err, data[0], data[1]);
		return err;
	}
	msleep(2);

	return 0;
}

static int write_eeprom_byte(struct mantis_pci *mantis, u8 *data, u8 length)

{
	int err;
	struct i2c_msg msg = {
		.addr = 0x50,
		.flags = 0,
		.buf = data,
		.len = length
	};
	if ((err = i2c_transfer(&mantis->adapter, &msg, 1)) < 0) {
		dprintk(verbose, MANTIS_ERROR, 1, "ERROR: i2c write: < err=%i length=0x%02x d0=0x%02x, d1=0x%02x >", err, length, data[0], data[1]);
		return err;
	}

	return 0;
}

static int get_subdevice_id(struct mantis_pci *mantis)

{
	int err;
	static u8 sub_device_id[2];

	mantis->sub_device_id = 0;
	sub_device_id[0] = 0xfc;
	if ((err = read_eeprom_byte(mantis, &sub_device_id[0], 2)) < 0) {
		dprintk(verbose, MANTIS_ERROR, 1, "Mantis EEPROM read error");
		return err;
	}
	mantis->sub_device_id = (sub_device_id[0] << 8) | sub_device_id[1];
	dprintk(verbose, MANTIS_ERROR, 1, "Sub Device ID=[0x%04x]", mantis->sub_device_id);

	return 0;
}

static int get_subvendor_id(struct mantis_pci *mantis)

{
	int err;
	static u8 sub_vendor_id[2];

	mantis->sub_vendor_id = 0;
	sub_vendor_id[0] = 0xfe;
	if ((err = read_eeprom_byte(mantis, &sub_vendor_id[0], 2)) < 0) {
		dprintk(verbose, MANTIS_ERROR, 1, "Mantis EEPROM read error");
		return err;
	}
	mantis->sub_vendor_id = (sub_vendor_id[0] << 8) | sub_vendor_id[1];
	dprintk(verbose, MANTIS_ERROR, 1, "Sub Vendor ID=[0x%04x]", mantis->sub_vendor_id);

	return 0;
}

static int get_mac_address(struct mantis_pci *mantis)

{
	int err;

	mantis->mac_address[0] = 0x08;
	if ((err = read_eeprom_byte(mantis, &mantis->mac_address[0], 6)) < 0) {
		dprintk(verbose, MANTIS_ERROR, 1, "Mantis EEPROM read error");
		return err;
	}
	dprintk(verbose, MANTIS_ERROR, 1, "MAC Address=[%02x:%02x:%02x:%02x:%02x:%02x]",
		mantis->mac_address[0], mantis->mac_address[1],	mantis->mac_address[2],
		mantis->mac_address[3], mantis->mac_address[4], mantis->mac_address[5]);

	return 0;
}

struct vendorname vendorlist[] = {

	{
		.sub_vendor_name	= "Twinhan",
		.sub_vendor_id		= 0x1822,
	},

	{ }
};

struct devicetype devicelist[] = {

	{
		.sub_device_name	= "VP-1033",
		.sub_device_id		= 0x0016,
		.device_type		= FE_TYPE_SAT,
		.type_flags		= FE_TYPE_TS204,
	},

	{ }
};

int mantis_core_init(struct mantis_pci *mantis)

{
	int err;

	if ((err = mantis_i2c_init(mantis)) < 0) {
		dprintk(verbose, MANTIS_ERROR, 1, "Mantis I2C init failed");
		goto err;
	}
	if ((err = get_mac_address(mantis)) < 0) {
		dprintk(verbose, MANTIS_ERROR, 1, "get MAC address failed");
		goto err;
	}
	if ((err = get_subvendor_id(mantis)) < 0) {
		dprintk(verbose, MANTIS_ERROR, 1, "get Sub vendor ID failed");
		goto err;
	}
	if ((err = get_subdevice_id(mantis)) < 0) {
		dprintk(verbose, MANTIS_ERROR, 1, "get Sub device ID failed");
		goto err;
	}
	if ((err = mantis_dvb_init(mantis)) < 0) {
		dprintk(verbose, MANTIS_DEBUG, 1, "Mantis DVB init failed");
		goto err;
	}

	return 0;

err:

	return err;
}

int mantis_core_exit(struct mantis_pci *mantis)

{
	if (mantis_dvb_exit(mantis) < 0)
		dprintk(verbose, MANTIS_ERROR, 1, "DVB exit failed");

	if (mantis_i2c_exit(mantis) < 0)
		dprintk(verbose, MANTIS_ERROR, 1, "I2C adapter delete.. failed");

	return 0;
}

static void gpio_set_bits(struct mantis_pci *mantis, u32 bitpos, u8 value)

{
	u32 reg;

	if (value)
		reg = 0x0000;
	else
		reg = 0xffff;

	reg = (value << bitpos);

	mmwrite(mmread(MANTIS_GPIF_HIFADDR) | reg, MANTIS_GPIF_HIFADDR);
	mmwrite(0x00, MANTIS_GPIF_HIFDOUT);
	udelay(100);
	mmwrite(mmread(MANTIS_GPIF_HIFADDR) | reg, MANTIS_GPIF_HIFADDR);
	mmwrite(0x00, MANTIS_GPIF_HIFDOUT);
}

/*
* Tuner power supply control
* Note: this is different from the LNB power control
*
*/
void mantis_fe_powerup(struct mantis_pci *mantis)

{
	gpio_set_bits(mantis, 0x0c, 1);
	mdelay(100);
	gpio_set_bits(mantis, 0x0c, 1);
}

void mantis_fe_powerdown(struct mantis_pci *mantis)

{
	gpio_set_bits(mantis, 0x0c, 0);
}

/*
Mantis PCI bridge driver

Copyright (C) 2005 Manu Abraham ([email protected])

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#ifndef _MANTIS_CORE_H_
#define _MANTIS_CORE_H_

#include "mantis_common.h"

#define FE_TYPE_SAT 0
#define FE_TYPE_CAB 1
#define FE_TYPE_TER 2

#define FE_TYPE_TS204 0
#define FE_TYPE_TS188 1

struct vendorname {
	__u8  *sub_vendor_name;
	__u32 sub_vendor_id;
};
struct devicetype {
	__u8  *sub_device_name;
	__u32 sub_device_id;
	__u8  device_type;
	__u32 type_flags;
};

extern int mantis_dma_init(struct mantis_pci *mantis); extern int mantis_dma_exit(struct mantis_pci *mantis); extern void mantis_dma_start(struct mantis_pci *mantis); extern void mantis_dma_stop(struct mantis_pci *mantis); extern int mantis_i2c_init(struct mantis_pci *mantis); extern int mantis_i2c_exit(struct mantis_pci *mantis); extern int mantis_core_init(struct mantis_pci *mantis); extern int mantis_core_exit(struct mantis_pci *mantis); extern void mantis_fe_powerup(struct mantis_pci *mantis); extern void mantis_fe_powerdown(struct mantis_pci *mantis);

#endif

/*
Mantis PCI bridge driver

Copyright (C) 2005 Manu Abraham ([email protected])

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include <asm/page.h>
#include <linux/vmalloc.h>
#include "mantis_common.h"

#define RISC_WRITE (0x01 << 28)
#define RISC_JUMP (0x07 << 28)
#define RISC_SYNC (0x08 << 28)

#define RISC_WR_SOL (1 << 27)
#define RISC_WR_EOL (1 << 26)
#define RISC_IRQ (1 << 24)

#define RISC_STATUS(status) ((((~status) & 0x0F) << 20) | ((status & 0x0F) << 16))

#define RISC_FLUSH() mantis->risc_pos = 0
#define RISC_INSTR(opcode) mantis->risc_cpu[mantis->risc_pos++] = cpu_to_le32(opcode)

static void mantis_free(struct mantis_pci *mantis)

{
	if (mantis->buf_cpu) {
		dprintk(verbose, MANTIS_ERROR, 1, "DMA=%lx", (unsigned long) mantis->buf_dma);
		pci_free_consistent(mantis->pdev, mantis->buf_size, mantis->buf_cpu, mantis->buf_dma);
		mantis->buf_cpu = NULL;
	}
	if (mantis->risc_cpu) {
		dprintk(verbose, MANTIS_ERROR, 1, "RISC=%lx", (unsigned long) mantis->risc_dma);
		pci_free_consistent(mantis->pdev, mantis->risc_size, mantis->risc_cpu, mantis->risc_dma);
		mantis->risc_cpu = NULL;
	}
}

static int mantis_alloc_buffers(struct mantis_pci *mantis)

{
	if (!mantis->buf_cpu) {
		mantis->buf_size = 128 * 1024;
		mantis->buf_cpu = pci_alloc_consistent(mantis->pdev, mantis->buf_size, &mantis->buf_dma);
		if (!mantis->buf_cpu) {
			dprintk(verbose, MANTIS_ERROR, 1, "DMA buffer allocation failed");
			goto err;
		}
		dprintk(verbose, MANTIS_ERROR, 1, "DMA=0x%lx cpu=0x%p size=%d",
				(unsigned long) mantis->buf_dma, mantis->buf_cpu, mantis->buf_size);
	}
	if (!mantis->risc_cpu) {
		mantis->risc_size = PAGE_SIZE;
		mantis->risc_cpu = pci_alloc_consistent(mantis->pdev, mantis->risc_size, &mantis->risc_dma);
		if (!mantis->risc_cpu) {
			dprintk(verbose, MANTIS_ERROR, 1, "RISC program allocation failed");
			mantis_free(mantis);
			goto err;
		}
		dprintk(verbose, MANTIS_ERROR, 1, "RISC=0x%lx cpu=0x%p size=%d",
				(unsigned long) mantis->risc_dma, mantis->risc_cpu, mantis->risc_size);
	}

	return 0;
err:
	dprintk(verbose, MANTIS_ERROR, 1, "Out of memory (?) .....");
	return -ENOMEM;
}

static int mantis_calc_lines(struct mantis_pci *mantis)

{
	mantis->block_bytes = mantis->buf_size >> 4;
	mantis->block_count = 1 << 4;
	mantis->line_bytes = mantis->block_bytes;
	mantis->line_count = mantis->block_count;

	while (mantis->line_bytes > 4095) {
		mantis->line_bytes >>= 1;
		mantis->line_count <<= 1;
	}
	dprintk(verbose, MANTIS_DEBUG, 1, "Mantis RISC block bytes=[%d], line bytes=[%d], line count=[%d]",
			mantis->block_bytes, mantis->line_bytes, mantis->line_count);

	if (mantis->line_count > 255) {
		dprintk(verbose, MANTIS_ERROR, 1, "Buffer size error");
		return -EINVAL;
	}

	return 0;
}

static void mantis_risc_program(struct mantis_pci *mantis)

{
	u32 buf_pos = 0;
	u32 line;

	dprintk(verbose, MANTIS_DEBUG, 1, "Mantis create RISC program");
	RISC_FLUSH();
	RISC_INSTR(RISC_SYNC);
	RISC_INSTR(0);

	dprintk(verbose, MANTIS_DEBUG, 1, "risc len lines %u, bytes per line %u", mantis->line_count, mantis->line_bytes);
	for (line = 0; line < mantis->line_count; line++) {
		dprintk(verbose, MANTIS_DEBUG, 1, "RISC PROG line=[%d]", line);
		if (!(buf_pos % mantis->block_bytes))
			RISC_INSTR(RISC_WRITE | RISC_WR_SOL | RISC_WR_EOL | RISC_IRQ |
						RISC_STATUS(((buf_pos / mantis->block_bytes) +
						(mantis->block_count - 1)) % mantis->block_count) | mantis->line_bytes);
		else
			RISC_INSTR(RISC_WRITE | RISC_WR_SOL | RISC_WR_EOL | mantis->line_bytes);
		RISC_INSTR(mantis->buf_dma + buf_pos);
		buf_pos += mantis->line_bytes;
	}
	RISC_INSTR(RISC_SYNC);
	RISC_INSTR(0);
	RISC_INSTR(RISC_JUMP);
	RISC_INSTR(mantis->risc_dma);
}

void mantis_dma_start(struct mantis_pci *mantis)

{
	dprintk(verbose, MANTIS_DEBUG, 1, "Mantis Start DMA engine");
	/*
	 *	Create RISC program to be loaded to the RISC core
	 */
	mantis_risc_program(mantis);

	/*
	 *	Load the RISC program from Host to RISC
	 */
	mmwrite(cpu_to_le32(mantis->risc_dma), MANTIS_RISC_START);

	/*
	 *	Enable INT's, start DMA engine
	 *	DMA engine: Enable FIFO, Enable Data Capture
	 *	Enable RISC engine
	 */
	mmwrite(mmread(MANTIS_INT_MASK) | MANTIS_FIFO_EN | MANTIS_DCAP_EN | MANTIS_RISC_EN, MANTIS_INT_MASK);
	mmwrite(mmread(MANTIS_DMA_CTL) | MANTIS_FIFO_EN | MANTIS_DCAP_EN | MANTIS_RISC_EN, MANTIS_DMA_CTL);

}

void mantis_dma_stop(struct mantis_pci *mantis)

{
	dprintk(verbose, MANTIS_DEBUG, 1, "Mantis Stop DMA engine");

	/*
	 *	Stop only the DMA engine, do not clear
	 *	the other interrupts
	 */
	mmwrite((mmread(MANTIS_DMA_CTL) & ~(MANTIS_FIFO_EN | MANTIS_DCAP_EN | MANTIS_RISC_EN)), MANTIS_DMA_CTL);
}

int mantis_dma_init(struct mantis_pci *mantis)

{
	int err = 0;

	dprintk(verbose, MANTIS_DEBUG, 1, "Mantis DMA init");
	if (mantis_alloc_buffers(mantis) < 0) {
		dprintk(verbose, MANTIS_ERROR, 1, "Error allocating DMA buffer");

		/*
		 *	Disable all interrupts during initialization
		 *	to avoid spurious interrupts creating hell.
		 *	Clear all INT's, disable FIFO RISC.
		 */
		mmwrite(0x00, MANTIS_INT_MASK);
		mmwrite(0x00, MANTIS_DMA_CTL);

		goto err;
	}
	if ((err = mantis_calc_lines(mantis)) < 0) {
		dprintk(verbose, MANTIS_ERROR, 1, "Mantis calc lines failed");
		goto err;
	}

	return 0;
err:
	return err;
}

void mantis_dma_xfer(unsigned long data)

{
	struct mantis_pci *mantis = (struct mantis_pci *) data;

	while (mantis->last_block != mantis->finished_block) {
		(mantis->ts_size ? dvb_dmx_swfilter_204: dvb_dmx_swfilter)
		(&mantis->demux, &mantis->buf_cpu[mantis->last_block * mantis->block_bytes], mantis->block_bytes);
		mantis->last_block = (mantis->last_block + 1) % mantis->block_count;
	}
}

int mantis_dma_exit(struct mantis_pci *mantis)

{
	if (mantis->buf_cpu) {
		dprintk(verbose, MANTIS_ERROR, 1, "DMA=0x%lx cpu=0x%p size=%d",
				(unsigned long) mantis->buf_dma, mantis->buf_cpu, mantis->buf_size);
		pci_free_consistent(mantis->pdev, mantis->buf_size, mantis->buf_cpu, mantis->buf_dma);
		mantis->buf_cpu = NULL;
	}
	if (mantis->risc_cpu) {
		dprintk(verbose, MANTIS_ERROR, 1, "RISC=0x%lx cpu=0x%p size=%d",
				(unsigned long) mantis->risc_dma, mantis->risc_cpu, mantis->risc_size);
		pci_free_consistent(mantis->pdev, mantis->risc_size, mantis->risc_cpu, mantis->risc_dma);
		mantis->risc_cpu = NULL;
	}

	return 0;
}

/*
Mantis PCI bridge driver

Copyright (C) 2005 Manu Abraham ([email protected])

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <asm/io.h>
#include <linux/ioport.h>
#include <asm/pgtable.h>
#include <asm/page.h>
#include "mantis_common.h"

#define I2C_HW_B_MANTIS 0x1c

/*
* Wait till we receive INT I2CRACK. I2CDONE will follow suit,
* since we save the states, we would have both I2CRACK and I2CDONE set
* we don't wait till eternity though.
*/
static int mantis_ack_wait(struct mantis_pci *mantis)

{
	u8 timeout = 0;

	/*
	 *	I2CRACK will be reset when I2CDONE is set
	 *
	 *
	 */
	while (!(mantis->mantis_int_stat & MANTIS_INT_I2CRACK)) {
		udelay(1000);
		timeout++;
		dprintk(verbose, MANTIS_DEBUG, 0, ".");
		if (timeout > 50)
			return -1;
	}

	/*
	 *	Reset STATUS flags, before we leave.
	 *	since we save states.
	 */
	dprintk(verbose, MANTIS_DEBUG, 1, "Status  ... MANTIS_INT_STAT=[0x%08x]", mantis->mantis_int_stat);
	mantis->mantis_int_stat &= (~MANTIS_INT_I2CRACK | ~MANTIS_INT_I2CDONE);

	return 0;
}

static inline void mantis_i2cint_set(struct mantis_pci *mantis)

{
	/*
	 *	We don't have a mask for I2CRACK
	 *	I2CRACK is cleared on setting I2CDONE (?)
	 */
//	mmwrite(mmread(MANTIS_INT_STAT) & MANTIS_INT_I2CRACK, MANTIS_INT_STAT);
	mmwrite(mmread(MANTIS_INT_MASK) | MANTIS_INT_I2CDONE, MANTIS_INT_MASK);
	udelay(1000);
}

static int mantis_i2c_pagewrite(struct mantis_pci *mantis, struct i2c_msg *msg)

{
	u8  i;
	u32 txd = 0;

	dprintk(verbose, MANTIS_DEBUG, 1, "Writing to [0x%02x]", msg->addr);

	for (i = 0; i < msg->len; i++) {
		dprintk(verbose, MANTIS_DEBUG, 1, "Data<W[%d]>=[0x%02x]", i, msg->buf[i]);
		txd |= (msg->addr << 25) | (msg->buf[i] << 8) | MANTIS_I2C_RATE_3 | MANTIS_I2C_STOP | MANTIS_I2C_PGMODE;
		if (i == 3)
			txd &= ~MANTIS_I2C_STOP;

		mantis_i2cint_set(mantis);
		mmwrite(txd, MANTIS_I2CDATA_CTL);

		if (mantis_ack_wait(mantis) < 0) {
			dprintk(verbose, MANTIS_DEBUG, 1, "ACK failed");
			return -1;
		}
		udelay(10);
	}

	return 0;
}

/* FIXME! needs to be fixed, we need to put things back */
static int mantis_i2c_pageread(struct mantis_pci *mantis, struct i2c_msg *msg)

{
	u8 i;
	u32 rxd = 0;

	for (i = 0; i < 2; i++) {
		rxd |= ((msg->addr << 25) | (1 << 24)) | MANTIS_I2C_RATE_3 | MANTIS_I2C_STOP | MANTIS_I2C_PGMODE;
		if (i == 1)
			rxd &= ~MANTIS_I2C_STOP;
		mmwrite(mmread(MANTIS_INT_STAT) | MANTIS_INT_I2CDONE, MANTIS_INT_STAT);
		mmwrite(mmread(MANTIS_INT_MASK) | MANTIS_INT_I2CDONE, MANTIS_INT_MASK);
		udelay(1000);
		if (mantis_ack_wait(mantis) < 0)
			return -EIO;
		rxd = mmread(MANTIS_I2CDATA_CTL);
		udelay(500);
	}

	return 0;
}

///* FIXME ! Right now writes only a byte */
static int mantis_i2c_writebyte(struct mantis_pci *mantis, struct i2c_msg *msg)

{
	u32 txd = 0;
	mantis->mantis_int_stat = 0;

	txd = (msg->addr << 25) | (msg->buf[0] << 16) | (msg->buf[1] << 8) | MANTIS_I2C_RATE_3;
	dprintk(verbose, MANTIS_DEBUG, 1, "Writing to [0x%02x],  Data<W>=[%02x]", msg->addr, msg->buf[1]);
	mmwrite(txd, MANTIS_I2CDATA_CTL);

	if (mantis_ack_wait(mantis) < 0) {
		dprintk(verbose, MANTIS_DEBUG, 1, "Slave did not ACK !");
		return -EIO;
	}

	return 0;
}

static int mantis_i2c_readbyte(struct mantis_pci *mantis, struct i2c_msg *msg)

{
	u32 rxd;
	u16 i;
	u8  subaddr;

	/*
	 *	Clear saved previous states
	 */
	mantis->mantis_int_stat &= (~MANTIS_INT_I2CDONE | ~MANTIS_INT_I2CRACK);
	subaddr = msg->buf[0];

	for (i = 0; i < msg->len; i++) {
		/*	Rate settings		*/
		rxd = ((msg->addr << 25) | (1 << 24)) | (subaddr << 16) | MANTIS_I2C_RATE_3;

		mmwrite(rxd, MANTIS_I2CDATA_CTL);

		/*
		 *	Enable INT_I2CDONE
		 */
		mmwrite(mmread(MANTIS_INT_MASK) | MANTIS_INT_I2CDONE, MANTIS_INT_MASK);
		udelay(1000);

		/*	Slave ACK		*/
		if (mantis_ack_wait(mantis) < 0)
			return -EIO;

		rxd = mmread(MANTIS_I2CDATA_CTL);
		udelay(500);
		msg->buf[i] = (rxd >> 8) & 0xff;
		dprintk(verbose, MANTIS_DEBUG, 1, "Reading from [0x%02x], Data<R[%d]>=[0x%02x]", msg->addr, i, msg->buf[i]);
		subaddr++;
	}

	return 0;
}

static int mantis_i2c_readbytes(struct mantis_pci *mantis, struct i2c_msg *msg)

{
	u32 rxd;
	u16 i;

	mantis->mantis_int_stat = 0;

	for (i = 0; i < msg[1].len; i++) {
		/*	Rate settings		*/
		rxd = ((msg[0].addr  << 25) | (1 << 24)) | (msg[0].buf[0] << 16) | MANTIS_I2C_RATE_3;

		mmwrite(rxd, MANTIS_I2CDATA_CTL);

		/*
		 *	Need to wait till I2CDONE, rather than slave I2CRACK
		 */
		mmwrite(mmread(MANTIS_INT_MASK) | MANTIS_INT_I2CDONE, MANTIS_INT_MASK);
		udelay(1000);
		if (mantis_ack_wait(mantis) < 0)
			return -EIO;

		rxd = mmread(MANTIS_I2CDATA_CTL);
		udelay(500);
		msg[1].buf[i] = (rxd >> 8) & 0xff;
		dprintk(verbose, MANTIS_DEBUG, 1, "Reading from [0x%02x], Data<R[%d]>=[0x%02x]", msg[0].addr, i, msg[1].buf[i]);
		msg[0].buf[0]++;
	}

	return 0;
}

/* FIXME ! Right now writes only a byte */
static int mantis_i2c_writebytes(struct mantis_pci *mantis, struct i2c_msg *msg)

{
	u32 txd = 0;
	mantis->mantis_int_stat = 0;

	txd = (msg->addr << 25) | (msg->buf[0] << 16) | (msg->buf[1] << 8) | MANTIS_I2C_RATE_3;
	dprintk(verbose, MANTIS_DEBUG, 1, "Writing to [0x%02x], Data<W>=[%02x]", msg->addr, msg->buf[1]);
	mmwrite(txd, MANTIS_I2CDATA_CTL);

	if (mantis_ack_wait(mantis) < 0)
		return -EIO;

	return 0;
}

static int mantis_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msg, int num)

{
	int retval = 0;
	struct mantis_pci *mantis;

	mantis = i2c_get_adapdata(adapter);

	if (num == 4) {
		if (msg[0].flags & I2C_M_RD) {
			if ((retval = mantis_i2c_pageread(mantis, msg)) < 0)
				return retval;
		} else {
			if ((retval = mantis_i2c_pagewrite(mantis, msg)) < 0)
				return retval;
		}
	/*	 STV0299 operation (???)	*/
	} else if (num == 2) {
		mantis_i2cint_set(mantis);
		/*	Sanity check	*/
		if (msg[0].buf != NULL && msg[1].buf != NULL) {
			/*	I2C operation type	*/
			if (msg[0].flags == 0 && msg[1].flags == I2C_M_RD) {
				/*	Read	*/
				if ((retval = mantis_i2c_readbytes(mantis, msg)) < 0)
					return retval;
			} else {
				/*	Write	*/
				if ((retval = mantis_i2c_writebytes(mantis, msg)) < 0)
					return retval;
			}
		}
	/*	Normal case operation	*/
	} else {
		mantis_i2cint_set(mantis);
		if (msg[0].flags & I2C_M_RD) {
			if ((retval = mantis_i2c_readbyte(mantis, msg)) < 0)
				return retval;
		} else {
			if ((retval = mantis_i2c_writebyte(mantis, msg)) < 0)
				return retval;
		}
	}

	return num;
}

static __u32 mantis_i2c_func(struct i2c_adapter *adapter)

{
	return I2C_FUNC_SMBUS_EMUL;
}

static int mantis_attach_inform(struct i2c_client *client)

{
	struct mantis_pci *mantis;

	mantis = i2c_get_adapdata(client->adapter);
	dprintk(verbose, MANTIS_ERROR, 1, "mantis: %s i2c attach [addr = 0x%x, client=%s]",
		client->driver->name, client->addr, i2c_clientname(client));

	return 0;
}
static struct i2c_algorithm mantis_algo = {
	.name			= "Mantis I2C",
	.id			= I2C_HW_B_MANTIS,
	.master_xfer		= mantis_i2c_xfer,
	.functionality		= mantis_i2c_func,
};
static struct i2c_adapter mantis_i2c_adapter = {
	.owner			= THIS_MODULE,
	.class			= I2C_CLASS_TV_DIGITAL,
	.algo			= &mantis_algo,
	.client_register	= mantis_attach_inform,
};

int __devinit mantis_i2c_init(struct mantis_pci *mantis)

{
	memcpy(&mantis->adapter, &mantis_i2c_adapter, sizeof (mantis_i2c_adapter));
	i2c_set_adapdata(&mantis->adapter, mantis);
	mantis->i2c_rc = i2c_add_adapter(&mantis->adapter);
	if (mantis->i2c_rc < 0)
		return mantis->i2c_rc;

	dprintk(verbose, MANTIS_DEBUG, 1, "Initializing I2C ..");

	/*
	 *	I2CRACK will be reset when I2CDONE is set
	 *	Clear and setup I2C int mask
	 */
	mmwrite(mmread(MANTIS_INT_STAT) & ( ~MANTIS_INT_I2CDONE | ~MANTIS_INT_I2CRACK), MANTIS_INT_STAT);
	mmwrite(mmread(MANTIS_INT_MASK) | MANTIS_INT_I2CDONE | MANTIS_INT_I2CRACK, MANTIS_INT_MASK);
	dprintk(verbose, MANTIS_DEBUG, 1, "INT_STAT=[0x%08x], INT_MASK=[0x%08x]", mmread(MANTIS_INT_STAT), mmread(MANTIS_INT_MASK));

	return 0;
}

int __devexit mantis_i2c_exit(struct mantis_pci *mantis)

{
	dprintk(verbose, MANTIS_DEBUG, 1, "Removing I2C adapter");
	return i2c_del_adapter(&mantis->adapter);
}

/*
Mantis PCI bridge driver

Copyright (C) 2005 Manu Abraham ([email protected])

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include <asm/io.h>
#include <asm/pgtable.h>
#include <asm/page.h>
#include <linux/kmod.h>
#include <linux/vmalloc.h>
#include <linux/init.h>
#include <linux/device.h>
#include "mantis_common.h"
#include "mantis_core.h"

#include <asm/irq.h>
#include <linux/signal.h>
#include <linux/sched.h>
#include <linux/interrupt.h>

unsigned int verbose = 1;
module_param(verbose, int, 0644);
MODULE_PARM_DESC(verbose, "verbose startup messages, default is 1 (yes)");

#define PCI_VENDOR_ID_MANTIS 0x1822
#define PCI_DEVICE_ID_MANTIS_R11 0x4e35
#define DRIVER_NAME "Mantis"

static struct pci_device_id mantis_pci_table[] = {
	{ PCI_DEVICE(PCI_VENDOR_ID_MANTIS, PCI_DEVICE_ID_MANTIS_R11) },
	{ 0 },
};

MODULE_DEVICE_TABLE(pci, mantis_pci_table);

static irqreturn_t mantis_pci_irq(int irq, void *dev_id, struct pt_regs *regs)

{
	int count = 0, interrupts = 0;
	u32 stat = 0, mask = 0, temp;

	struct mantis_pci *mantis;

	mantis = (struct mantis_pci *) dev_id;
	if (mantis == NULL)
		dprintk(verbose, MANTIS_DEBUG, 1, "Aeio, mantis ISR");

	/*
	 *	Don't read each and everytime, but read in one shot
	 *	We need to save the states for later too ..
	 *
	 */

	stat = mmread(MANTIS_INT_STAT);
	mask = mmread(MANTIS_INT_MASK);

	/*
	 *	To speed up things, do a basic check whether it is our
	 *	interrupt.
	 *	Also, we don't have a mask for I2CRACK
	 */
	if (!(stat & (mask | MANTIS_INT_I2CRACK)) ) {
		dprintk(verbose, MANTIS_ERROR, 1, "Not ours !");
		return IRQ_NONE;
	}

	/*
	 *	The int is for us, clear int condition
	 */
	mmwrite(stat, MANTIS_INT_STAT);

	/*
	 *	Check how many 1's are in the INT_STAT, Count will reflect
	 *	no. of interrupts, we should loop count times.
	 *
	 */
	 temp = stat;
	 while (temp) {
		if (temp & 0x01) {
			interrupts++;
			dprintk(verbose, MANTIS_DEBUG, 1, "Interrupt @ %d", interrupts);
		}
		temp >>= 1;
		count++;

		/*
		 *	We should never loop more than reg. width
		 *	else, Bail out
		 */
		if (count > 32) {
			dprintk(verbose, MANTIS_ERROR, 1, "Bailing out !!");
			break;
		}
	}
	count = 0;
	while (count < interrupts) {
		if ((stat & mask) & MANTIS_INT_RISCEN) {
			dprintk(verbose, MANTIS_DEBUG, 1, "**** DMA enabl ****");
			mantis->mantis_int_stat |= MANTIS_INT_RISCEN;
			stat &= ~MANTIS_INT_RISCEN;

		/*
		 *	It's a shame that there is no mask for I2CRACK !
		 */
		} else if (stat & MANTIS_INT_I2CRACK) {
			dprintk(verbose, MANTIS_DEBUG, 1, "**** I2C R-ACK ****");
			mantis->mantis_int_stat |= MANTIS_INT_I2CRACK;
			stat &= ~MANTIS_INT_I2CRACK;

		} else if ((stat & mask) & MANTIS_INT_IRQ0) {
			dprintk(verbose, MANTIS_DEBUG, 1, "**** INT IRQ-0 ****");
			mantis->mantis_int_stat |= MANTIS_INT_IRQ0;
			stat &= ~MANTIS_INT_IRQ0;

		} else if ((stat & mask) & MANTIS_INT_IRQ1) {
			dprintk(verbose, MANTIS_DEBUG, 1, "**** INT IRQ-1 ****");
			mantis->mantis_int_stat |= MANTIS_INT_IRQ1;
			stat &= ~MANTIS_INT_IRQ1;

		} else if ((stat & mask) & MANTIS_INT_OCERR) {
			dprintk(verbose, MANTIS_DEBUG, 1, "**** INT OCERR ****");
			mantis->mantis_int_stat |= MANTIS_INT_OCERR;
			stat &= ~MANTIS_INT_OCERR;

		} else if ((stat & mask) & MANTIS_INT_PABORT) {
			dprintk(verbose, MANTIS_DEBUG, 1, "**** INT PABRT ****");
			mantis->mantis_int_stat |= MANTIS_INT_PABORT;
			stat &= ~MANTIS_INT_PABORT;

		} else if ((stat & mask) & MANTIS_INT_RIPERR) {
			dprintk(verbose, MANTIS_DEBUG, 1, "**** INT RIPRR ****");
			mantis->mantis_int_stat |= MANTIS_INT_RIPERR;
			stat &= ~MANTIS_INT_RIPERR;

		} else if ((stat & mask) & MANTIS_INT_PPERR) {
			dprintk(verbose, MANTIS_DEBUG, 1, "**** INT PPERR ****");
			mantis->mantis_int_stat |= MANTIS_INT_PPERR;
			stat &= ~MANTIS_INT_PPERR;

		} else if ((stat & mask) & MANTIS_INT_FTRGT) {
			dprintk(verbose, MANTIS_DEBUG, 1, "**** INT FTRGT ****");
			mantis->mantis_int_stat |= MANTIS_INT_FTRGT;
			stat &= ~MANTIS_INT_FTRGT;

		} else if ((stat & mask) & MANTIS_INT_RISCI) {
			mantis->finished_block = (mantis->mantis_int_stat & MANTIS_INT_RISCSTAT) >> 28;
			tasklet_schedule(&mantis->tasklet);
			stat &= ~MANTIS_INT_RISCI;

		} else if ((stat & mask) & MANTIS_INT_I2CDONE) {
			dprintk(verbose, MANTIS_DEBUG, 1, "**** I2C DONE  ****");
			mantis->mantis_int_stat |= MANTIS_INT_I2CDONE;
			stat &= ~MANTIS_INT_I2CDONE;

		} else {
			dprintk(verbose, MANTIS_DEBUG, 1, "Unknown INT ???");
		}

		count++;

		/*
		 *	Being paranoid, check whether we are too loopy !
		 */
		if (count > 32) {
			dprintk(verbose, MANTIS_ERROR, 1, "IRQ Lockup, clearing INT mask");
			dprintk(verbose, MANTIS_ERROR, 1, "Count=%d, Interrupts=%d, stat=[0x%08x]", count, interrupts, stat);
			mmwrite(0, MANTIS_INT_MASK);
			stat = 0, interrupts = 0;

			break;
		}
	}

	return IRQ_HANDLED;
}

static int __devinit mantis_pci_probe(struct pci_dev *pdev,
const struct pci_device_id *mantis_pci_table)

{
	u8 revision, latency;
	struct mantis_pci *mantis;

	if (pci_enable_device(pdev)) {
		dprintk(verbose, MANTIS_ERROR, 1, "Mantis PCI enable failed");
		goto err;
	}
	mantis = kmalloc(sizeof (struct mantis_pci), GFP_KERNEL);
	if (mantis == NULL) {
		dprintk(verbose, MANTIS_ERROR, 1, "Out of memory");
		return -ENOMEM;
	}
	memset(mantis, 0, sizeof (struct mantis_pci));

	mantis->mantis_addr = pci_resource_start(pdev, 0);
	if (!request_mem_region(pci_resource_start(pdev, 0),
			pci_resource_len(pdev, 0), DRIVER_NAME)) {
		goto err0;
	}

	if ((mantis->mantis_mmio =
				ioremap(mantis->mantis_addr, 0x1000)) == NULL) {
		dprintk(verbose, MANTIS_ERROR, 1, "IO remap failed");
		goto err1;
	}
	/*
	 *	Clear and disable all interrupts at startup
	 *	to avoid lockup situations
	 */
	mmwrite(0x00, MANTIS_INT_STAT);
	mmwrite(0x00, MANTIS_INT_MASK);
	if (request_irq(pdev->irq, mantis_pci_irq, SA_SHIRQ | SA_INTERRUPT,
						DRIVER_NAME, mantis) < 0) {
		dprintk(verbose, MANTIS_ERROR, 1, "Mantis IRQ reg failed");
		goto err2;
	}
	pci_set_master(pdev);
	pci_set_drvdata(pdev, mantis);
	pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &latency);
	pci_read_config_byte(pdev, PCI_CLASS_REVISION, &revision);
	mantis->latency = latency;
	mantis->revision = revision;
	mantis->pdev = pdev;

	/*
	 *	Setup default latency 32 if none specified
	 */
	if (!latency)
		pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 32);
	dprintk(verbose, MANTIS_ERROR, 0, "Mantis Rev %d, ", mantis->revision);
	dprintk(verbose, MANTIS_ERROR, 0, "irq: %d, latency: %d			\
	\nmemory: 0x%lx, mmio: 0x%p\n", pdev->irq, mantis->latency,		\
		mantis->mantis_addr, mantis->mantis_mmio);
	if ((mantis_dma_init(mantis)) < 0) {
		dprintk(verbose, MANTIS_ERROR, 1, "Mantis DMA init failed");
		goto err0;
	}

	/*
	 *	No more PCI specific stuff !
	 */	
	if (mantis_core_init(mantis) < 0) {
		dprintk(verbose, MANTIS_ERROR, 1, "Mantis core init failed");
		goto err2;
	}

	return 0;
	
	/*
	 *	Error conditions ..
	 */

err2:

	if (mantis_dma_exit(mantis) < 0)
		dprintk(verbose, MANTIS_ERROR, 1, "Mantis DMA exit failed");

	dprintk(verbose, MANTIS_DEBUG, 1, "Err: IO Unmap");
	if (mantis->mantis_mmio)
		iounmap(mantis->mantis_mmio);
err1:
	dprintk(verbose, MANTIS_DEBUG, 1, "Err: Release regions");
	release_mem_region(pci_resource_start(pdev, 0),
				pci_resource_len(pdev, 0));
	pci_disable_device(pdev);
err0:
	dprintk(verbose, MANTIS_DEBUG, 1, "Err: Free");
	kfree(mantis);
err:
	dprintk(verbose, MANTIS_DEBUG, 1, "Err:");
	return -ENODEV;
}

static void __devexit mantis_pci_remove(struct pci_dev *pdev)

{
	struct mantis_pci *mantis = pci_get_drvdata(pdev);
	if (mantis == NULL) {
		dprintk(verbose, MANTIS_ERROR, 1, "Aeio, Mantis NULL ptr");
		return;
	}
	mantis_core_exit(mantis);
	dprintk(verbose, MANTIS_ERROR, 1, "Removing -->Mantis irq: %d,		\
		latency: %d\n memory: 0x%lx, mmio: 0x%p",
		pdev->irq, mantis->latency, mantis->mantis_addr,
		mantis->mantis_mmio);

	free_irq(pdev->irq, mantis);
	pci_release_regions(pdev);
	if (mantis_dma_exit(mantis) < 0)
		dprintk(verbose, MANTIS_ERROR, 1, "DMA exit failed");

	pci_set_drvdata(pdev, NULL);
	pci_disable_device(pdev);
	kfree(mantis);
}

static struct pci_driver mantis_pci_driver = {
.name = DRIVER_NAME,
.id_table = mantis_pci_table,
.probe = mantis_pci_probe,
.remove = mantis_pci_remove,
};

static int __devinit mantis_pci_init(void)

{
	return pci_register_driver(&mantis_pci_driver);
}

static void __devexit mantis_pci_exit(void)

{
	pci_unregister_driver(&mantis_pci_driver);
}

module_init(mantis_pci_init);
module_exit(mantis_pci_exit);

MODULE_DESCRIPTION("Mantis PCI DTV bridge driver"); MODULE_AUTHOR("Manu Abraham"); MODULE_LICENSE("GPL");

/*
Mantis PCI bridge driver

Copyright (C) 2005 Manu Abraham ([email protected])

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#ifndef _MANTIS_REG_H_
#define _MANTIS_REG_H_

/* Interrupts */
#define MANTIS_INT_STAT 0x00
#define MANTIS_INT_MASK 0x04
#define MANTIS_INT_RISCSTAT (0xf << 28)
#define MANTIS_INT_RISCEN (1 << 27)
#define MANTIS_INT_I2CRACK (1 << 26)
#define MANTIS_INT_IRQ0 (1 << 11)
#define MANTIS_INT_IRQ1 (1 << 10)
#define MANTIS_INT_OCERR (1 << 8)
#define MANTIS_INT_PABORT (1 << 7)
#define MANTIS_INT_RIPERR (1 << 6)
#define MANTIS_INT_PPERR (1 << 5)
#define MANTIS_INT_FTRGT (1 << 3)
#define MANTIS_INT_RISCI (1 << 1)
#define MANTIS_INT_I2CDONE (1 << 0)

/* DMA */
#define MANTIS_DMA_CTL 0x08
#define MANTIS_I2C_RD (1 << 7)
#define MANTIS_I2C_WR (1 << 6)
#define MANTIS_DCAP_MODE (1 << 5)
#define MANTIS_FIFO_TP_4 (0 << 3)
#define MANTIS_FIFO_TP_8 (1 << 3)
#define MANTIS_FIFO_TP_16 (2 << 3)
#define MANTIS_FIFO_EN (1 << 2)
#define MANTIS_DCAP_EN (1 << 1)
#define MANTIS_RISC_EN (1 << 0)

#define MANTIS_RISC_START 0x10
#define MANTIS_RISC_PC 0x14

/* I2C */
#define MANTIS_I2CDATA_CTL 0x18
#define MANTIS_I2C_RATE_1 (0 << 6)
#define MANTIS_I2C_RATE_2 (1 << 6)
#define MANTIS_I2C_RATE_3 (2 << 6)
#define MANTIS_I2C_RATE_4 (3 << 6)
#define MANTIS_I2C_STOP (1 << 5)
#define MANTIS_I2C_PGMODE (1 << 3)

#define MANTIS_GPIF_HIFADDR 0xb0
#define MANTIS_GPIF_HIFDOUT 0xb4

#endif

#33 lore raw

Am Montag, 10. Oktober 2005 14:48 schrieben Sie:

Quoted 10 lines
Quoted 2 lines · Rolf Eike Beer

IIRC the call to pci_enable_device() must be the first thing you do. This will do the things like assigning memory regions to the device and so on.

I fixed this one

Quoted 3 lines

Returning 0 in error cases is just wrong. And you free the assignments even in case of success AFAICS. Try the return I introduced above and see what happens.

Quoted 6 lines

I fixed this one too ..

I have fixed most of the stuff, it is partly working, not ready yet as there are some more things to be added to ..
I have attached what i was working on.

If the kmalloc() fails in mantis_pci_probe() you don't call pci_disable_device(). And you should kzalloc() instead of kmalloc() and memset().

It looks like you never use "__u16 vendor_id;" and "__u16 device_id;" in struct mantis_pci.

Eike

#34 lore raw
Quoted 22 lines · Rolf Eike Beer

Am Montag, 10. Oktober 2005 14:48 schrieben Sie:

Quoted 19 lines
Quoted 4 lines · Rolf Eike Beer

IIRC the call to pci_enable_device() must be the first thing you do. This will do the things like assigning memory regions to the device and so on.

I fixed this one

Quoted 5 lines

Returning 0 in error cases is just wrong. And you free the assignments even in case of success AFAICS. Try the return I introduced above and see what happens.

I fixed this one too ..

Quoted 10 lines
Quoted 5 lines

I have fixed most of the stuff, it is partly working, not ready yet as there are some more things to be added to ..
I have attached what i was working on.

If the kmalloc() fails in mantis_pci_probe() you don't call pci_disable_device(). And you should kzalloc() instead of kmalloc() and memset().

Yep, thanks for pointing it out ..

Quoted 4 lines

It looks like you never use "__u16 vendor_id;" and "__u16 device_id;" in struct mantis_pci.

I was working on that part, not yet finished on that ..

Thanks,
Manu

A message that is not in the archive (sent off-list or not yet received): [email protected]

#36 lore raw
Quoted 13 lines · Jiri Slaby
Quoted 5 lines · Manu Abraham

I have fixed most of the stuff, it is partly working, not ready yet as there are some more things to be added to ..
I have attached what i was working on.

I'm not so bad, I help people (but not 10 times with the same point), fuck it and forget.

presumption is not always good, and it is very clear. Most of the flamewars blaze up because of that. Anyway let's not hover on that ..

Quoted 6 lines

Some points to the new driver:
indentation isn't so good, the code is not readable well (80 chars on a line mainly).

I do agree that indentation is not so good, but how would you suggest the code be wrapped ?

The problem with wrapping is that readability goes down horribly, but while debugging a driver, this is too painful. Considering that it has a long way to go still..

Quoted 5 lines
Quoted 5 lines

static irqreturn_t mantis_pci_irq(int irq, void *dev_id, struct pt_regs *regs)

{
      int count = 0, interrupts = 0;
      u32 stat = 0, mask = 0, temp;
Quoted 10 lines
Quoted 5 lines
      struct mantis_pci *mantis;

      mantis = (struct mantis_pci *) dev_id;

you don't need to cast from void *
struct mantis_pci *mantis = dev_id; is enough

Thanks, Ack'd.

Quoted 9 lines
Quoted 3 lines
      mantis->pdev = pdev;

If you work with this out from pci functions, you should call pci_get_dev and in exit function pci_dev_put, otherwise you don't need it at all.

Well i am using it in mantis_dma.c, pci_alloc/free

You mean rather than saving off the pointer, i do a pci_get_dev() and later on in the exit routine, i do a pci_dev_put() .. ?

Quoted 5 lines

mantis_dma_init and others could be __devinit too, or not? Try to use it as much as possible.

Any thoughts as to why ? I am not saying it is not needed, but i would like to know what was the idea.

Quoted 10 lines
Quoted 7 lines

static struct pci_driver mantis_pci_driver = {
.name = DRIVER_NAME,
.id_table = mantis_pci_table,
.probe = mantis_pci_probe,
.remove = mantis_pci_remove,

here should be = __devexit_p(...),

Ack'd, Thanks ..

Quoted 6 lines
Quoted 3 lines

};

Quoted 7 lines
Quoted 3 lines

err0:

you should change naming of labels such as errfree, where do you do kfree etc.

Ah , okay.

Thanks,
Manu

A message that is not in the archive (sent off-list or not yet received): [email protected]

#38 lore raw
Quoted 11 lines · Jiri Slaby
Quoted 8 lines · Manu Abraham
Quoted 5 lines · Jiri Slaby

Some points to the new driver:
indentation isn't so good, the code is not readable well (80 chars on a line mainly).

Quoted 10 lines
Quoted 4 lines

I do agree that indentation is not so good, but how would you suggest the code be wrapped ?

Divide strings not "text blabalba \
continue"
but with better "text blablabalasjdl"
"continue"

The dprintk() macro (in mantis_common.h ) was looking very badly with wrap, which Andrew also commented on (about the col's) (the macro being the same, eventhough i was using it elsewhere) it being more than 80 cols, but wrapping the macro made it look like hell.

Quoted 5 lines

wrap line, if it is longer than 80 columns, near last comma, |, & and so on You seem to use some editor with tab to be less than 8 columns and some headers are indented bad because of it.

My editor uses 8 columns only as a tab, Thunderbird does really handle things a bit different though.

Quoted 25 lines
Quoted 11 lines

The problem with wrapping is that readability goes down horribly, but while debugging a driver, this is too painful.
Considering that it has a long way to go still..

Quoted 5 lines
Quoted 5 lines
     mantis->pdev = pdev;
Quoted 8 lines
Quoted 5 lines

If you work with this out from pci functions, you should call pci_get_dev and in exit function pci_dev_put, otherwise you don't need it at all.

Well i am using it in mantis_dma.c, pci_alloc/free

And it is called only from places, where pdev is known (i.e. in parameter of function, e.g. mantis_pci_probe). So you don't need it to store in mantis, but only call mantis_dma_init(mantis, pdev). Read below.

Quoted 10 lines
Quoted 4 lines

You mean rather than saving off the pointer, i do a pci_get_dev() and later on in the exit routine, i do a pci_dev_put() .. ?

But if you really want it, call pci_get_dev() and store it into mantis struct. In the _device_ exit routine call the latter. But I think, that not to store is better, or the best is to call mantis_dma_init(pdev) and do pci_get_drvdata inside.

i think will pass (pdev) it as a function argument. Looks a bit more cleaner
But what i fail to understand is , if you can pass it as an argument, why can't you save the pointer in the struct ?

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

mantis_dma_init and others could be __devinit too, or not? Try to use it as much as possible.

Quoted 8 lines
Quoted 4 lines

Any thoughts as to why ? I am not saying it is not needed, but i would like to know what was the idea.

Kernel frees up the sections that won't be needed anymore. You put the function in some section by this.

Ok,

Thanks.
Manu

A message that is not in the archive (sent off-list or not yet received): [email protected]

#40 lore raw
Quoted 10 lines · Jiri Slaby
Quoted 7 lines · Manu Abraham

Jiri Slaby wrote:
The dprintk() macro (in mantis_common.h ) was looking very badly with wrap, which Andrew also commented on (about the col's) (the macro being the same, eventhough i was using it elsewhere) it being more than 80 cols, but wrapping the macro made it look like hell.

Quoted 5 lines

Did you consider using of (inline) function. It would be better in this case. [Consider using __attribute__ ((format(...)))]

Ok, that sounds better .. I will take a look at how it looks ..

Quoted 46 lines
Quoted 41 lines
Quoted 33 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
    mantis->pdev = pdev;
Quoted 12 lines
Quoted 7 lines

If you work with this out from pci functions, you should call pci_get_dev and in exit function pci_dev_put, otherwise you don't need it at all.

Well i am using it in mantis_dma.c, pci_alloc/free

And it is called only from places, where pdev is known (i.e. in parameter of function, e.g. mantis_pci_probe). So you don't need it to store in mantis, but only call mantis_dma_init(mantis, pdev). Read below.

Quoted 6 lines

You mean rather than saving off the pointer, i do a pci_get_dev() and later on in the exit routine, i do a pci_dev_put() .. ?

Quoted 8 lines

But if you really want it, call pci_get_dev() and store it into mantis struct. In the _device_ exit routine call the latter. But I think, that not to store is better, or the best is to call mantis_dma_init(pdev) and do pci_get_drvdata inside.

Quoted 5 lines

i think will pass (pdev) it as a function argument. Looks a bit more cleaner But what i fail to understand is , if you can pass it as an argument, why can't you save the pointer in the struct ?

Quoted 8 lines

You can do that. I don't know, how do you handle the mantis structure. But if you need pdev in it, you should do (in most cases) pci_dev_get to increase ref count = you use it in one more place and telling kernel by that: do not free it, because I need it, until I call pci_dev_put.

I would need to do that even if i pass it as an argument to the function , right ?

what i mean is if i do a

dma_init(pdev)

and in

int dma_init(struct pci_dev *pdev)
{
struct x *a = pci_get_drvdata(pdev);

   pci_alloc_consistent(pdev, .. );

}

alone would be sufficient ? I went this way as you can see ..

Regards,
Manu

/*
Mantis PCI bridge driver

Copyright (C) 2005 Manu Abraham ([email protected])

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#ifndef _MANTIS_COMMON_H_
#define _MANTIS_COMMON_H_

#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kernel.h>
#include <linux/pci.h>

#include "dvbdev.h"
#include "dvb_demux.h"
#include "dmxdev.h"
#include "dvb_frontend.h"
#include "dvb_net.h"
#include <linux/i2c.h>
#include "mantis_reg.h"

#define MANTIS_ERROR 0
#define MANTIS_NOTICE 1
#define MANTIS_INFO 2
#define MANTIS_DEBUG 3

#define dprintk(x, y, z, format, arg...) do {						\
	if (z) {									\
		if	((x > MANTIS_ERROR) && (x > y))					\
			printk(KERN_ERR "%s: " format "\n" , __FUNCTION__ , ##arg);	\
		else if	((x > MANTIS_NOTICE) && (x > y))				\
			printk(KERN_NOTICE "%s: " format "\n" , __FUNCTION__ , ##arg);	\
		else if ((x > MANTIS_INFO) && (x > y))					\
			printk(KERN_INFO "%s: " format "\n" , __FUNCTION__ , ##arg);	\
		else if ((x > MANTIS_DEBUG) && (x > y))					\
			printk(KERN_DEBUG "%s: " format "\n" , __FUNCTION__ , ##arg);	\
	} else {									\
		if (x > y)								\
			printk(format , ##arg);						\
	}										\
} while(0)

#define mwrite(dat, addr) writel((dat), addr)
#define mread(addr) readl(addr)

#define mmwrite(dat, addr) mwrite((dat), (mantis->mantis_mmio + (addr)))
#define mmread(addr) mread(mantis->mantis_mmio + (addr))
#define mmand(dat, addr) mmwrite((dat) & mmread(addr), addr)
#define mmor(dat, addr) mmwrite((dat) | mmread(addr), addr)
#define mmaor(dat, addr) mmwrite((dat) | ((mask) & mmread(addr)), addr)

struct mantis_pci {
	/*	PCI stuff	*/
	__u16 vendor_id;
	__u16 device_id;
	__u8  latency;

	unsigned long mantis_addr;
	volatile void __iomem *mantis_mmio;

	__u8  irq;
	__u8  revision;

	__u16 mantis_card_num;
	__u16 ts_size;

	/*	RISC Core	*/
	volatile __u32 finished_block;
	volatile __u32 last_block;

	__u32 block_count;
	__u32 block_bytes;
	__u32 line_bytes;
	__u32 line_count;

	__u32 risc_pos;

	__u32 buf_size;
	__u8  *buf_cpu;
	dma_addr_t buf_dma;

	__u32 risc_size;
	__u32 *risc_cpu;
	dma_addr_t risc_dma;

	struct	tasklet_struct tasklet;

	struct	i2c_adapter adapter;
	int	i2c_rc;

	/*	DVB stuff	*/
	struct  dvb_adapter dvb_adapter;
	struct  dvb_frontend *fe;
	struct  dvb_demux demux;
	struct  dmxdev dmxdev;
	struct  dmx_frontend fe_hw;
	struct  dmx_frontend fe_mem;
	struct  dvb_net dvbnet;

	__u8	feeds;

	struct  mantis_config *config;

	__u32 mantis_int_stat;
	__u32 mantis_int_mask;

	/*	board specific		*/
	__u8  mac_address[8];
	__u32 sub_vendor_id;
	__u32 sub_device_id;
};

extern unsigned int verbose;
extern int mantis_dvb_init(struct mantis_pci *mantis); extern int mantis_frontend_init(struct mantis_pci *mantis); extern int mantis_dvb_exit(struct mantis_pci *mantis); extern void mantis_dma_xfer(unsigned long data);

#endif

/*
Mantis PCI bridge driver

Copyright (C) 2005 Manu Abraham ([email protected])

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "mantis_common.h"
#include "mantis_core.h"

static int read_eeprom_byte(struct mantis_pci *mantis, u8 *data, u8 length)

{
	int err;
	struct i2c_msg msg = {
		.addr = 0x50,
		.flags = I2C_M_RD,
		.buf = data,
		.len = length
	};
	if ((err = i2c_transfer(&mantis->adapter, &msg, 1)) < 0) {
		dprintk(verbose, MANTIS_ERROR, 1, "ERROR: i2c read: < err=%i d0=0x%02x d1=0x%02x >", err, data[0], data[1]);
		return err;
	}
	msleep(2);

	return 0;
}

static int write_eeprom_byte(struct mantis_pci *mantis, u8 *data, u8 length)

{
	int err;
	struct i2c_msg msg = {
		.addr = 0x50,
		.flags = 0,
		.buf = data,
		.len = length
	};
	if ((err = i2c_transfer(&mantis->adapter, &msg, 1)) < 0) {
		dprintk(verbose, MANTIS_ERROR, 1, "ERROR: i2c write: < err=%i length=0x%02x d0=0x%02x, d1=0x%02x >", err, length, data[0], data[1]);
		return err;
	}

	return 0;
}

static int get_subdevice_id(struct mantis_pci *mantis)

{
	int err;
	static u8 sub_device_id[2];

	mantis->sub_device_id = 0;
	sub_device_id[0] = 0xfc;
	if ((err = read_eeprom_byte(mantis, &sub_device_id[0], 2)) < 0) {
		dprintk(verbose, MANTIS_ERROR, 1, "Mantis EEPROM read error");
		return err;
	}
	mantis->sub_device_id = (sub_device_id[0] << 8) | sub_device_id[1];
	dprintk(verbose, MANTIS_ERROR, 1, "Sub Device ID=[0x%04x]", mantis->sub_device_id);

	return 0;
}

static int get_subvendor_id(struct mantis_pci *mantis)

{
	int err;
	static u8 sub_vendor_id[2];

	mantis->sub_vendor_id = 0;
	sub_vendor_id[0] = 0xfe;
	if ((err = read_eeprom_byte(mantis, &sub_vendor_id[0], 2)) < 0) {
		dprintk(verbose, MANTIS_ERROR, 1, "Mantis EEPROM read error");
		return err;
	}
	mantis->sub_vendor_id = (sub_vendor_id[0] << 8) | sub_vendor_id[1];
	dprintk(verbose, MANTIS_ERROR, 1, "Sub Vendor ID=[0x%04x]", mantis->sub_vendor_id);

	return 0;
}

static int get_mac_address(struct mantis_pci *mantis)

{
	int err;

	mantis->mac_address[0] = 0x08;
	if ((err = read_eeprom_byte(mantis, &mantis->mac_address[0], 6)) < 0) {
		dprintk(verbose, MANTIS_ERROR, 1, "Mantis EEPROM read error");
		return err;
	}
	dprintk(verbose, MANTIS_ERROR, 1, "MAC Address=[%02x:%02x:%02x:%02x:%02x:%02x]",
		mantis->mac_address[0], mantis->mac_address[1],	mantis->mac_address[2],
		mantis->mac_address[3], mantis->mac_address[4], mantis->mac_address[5]);

	return 0;
}

struct vendorname vendorlist[] = {

	{
		.sub_vendor_name	= "Twinhan",
		.sub_vendor_id		= 0x1822,
	},

	{ }
};

struct devicetype devicelist[] = {

	{
		.sub_device_name	= "VP-1033",
		.sub_device_id		= 0x0016,
		.device_type		= FE_TYPE_SAT,
		.type_flags		= FE_TYPE_TS204,
	},

	{ }
};

int __devinit mantis_core_init(struct mantis_pci *mantis)

{
	int err;

	if ((err = mantis_i2c_init(mantis)) < 0) {
		dprintk(verbose, MANTIS_ERROR, 1, "Mantis I2C init failed");
		goto err;
	}
	if ((err = get_mac_address(mantis)) < 0) {
		dprintk(verbose, MANTIS_ERROR, 1, "get MAC address failed");
		goto err;
	}
	if ((err = get_subvendor_id(mantis)) < 0) {
		dprintk(verbose, MANTIS_ERROR, 1, "get Sub vendor ID failed");
		goto err;
	}
	if ((err = get_subdevice_id(mantis)) < 0) {
		dprintk(verbose, MANTIS_ERROR, 1, "get Sub device ID failed");
		goto err;
	}
	if ((err = mantis_dvb_init(mantis)) < 0) {
		dprintk(verbose, MANTIS_DEBUG, 1, "Mantis DVB init failed");
		goto err;
	}

	return 0;

err:

	return err;
}

int __devexit mantis_core_exit(struct mantis_pci *mantis)

{
	if (mantis_dvb_exit(mantis) < 0)
		dprintk(verbose, MANTIS_ERROR, 1, "DVB exit failed");

	if (mantis_i2c_exit(mantis) < 0)
		dprintk(verbose, MANTIS_ERROR, 1, "I2C adapter delete.. failed");

	return 0;
}

static void gpio_set_bits(struct mantis_pci *mantis, u32 bitpos, u8 value)

{
	u32 reg;

	if (value)
		reg = 0x0000;
	else
		reg = 0xffff;

	reg = (value << bitpos);

	mmwrite(mmread(MANTIS_GPIF_HIFADDR) | reg, MANTIS_GPIF_HIFADDR);
	mmwrite(0x00, MANTIS_GPIF_HIFDOUT);
	udelay(100);
	mmwrite(mmread(MANTIS_GPIF_HIFADDR) | reg, MANTIS_GPIF_HIFADDR);
	mmwrite(0x00, MANTIS_GPIF_HIFDOUT);
}

/*
* Tuner power supply control
* Note: this is different from the LNB power control
*
*/
void mantis_fe_powerup(struct mantis_pci *mantis)

{
	gpio_set_bits(mantis, 0x0c, 1);
	mdelay(100);
	gpio_set_bits(mantis, 0x0c, 1);
}

void mantis_fe_powerdown(struct mantis_pci *mantis)

{
	gpio_set_bits(mantis, 0x0c, 0);
}

/*
Mantis PCI bridge driver

Copyright (C) 2005 Manu Abraham ([email protected])

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#ifndef _MANTIS_CORE_H_
#define _MANTIS_CORE_H_

#include "mantis_common.h"

#define FE_TYPE_SAT 0
#define FE_TYPE_CAB 1
#define FE_TYPE_TER 2

#define FE_TYPE_TS204 0
#define FE_TYPE_TS188 1

struct vendorname {
	__u8  *sub_vendor_name;
	__u32 sub_vendor_id;
};
struct devicetype {
	__u8  *sub_device_name;
	__u32 sub_device_id;
	__u8  device_type;
	__u32 type_flags;
};

extern int mantis_dma_init(struct pci_dev *pdev); extern int mantis_dma_exit(struct pci_dev *pdev); extern void mantis_dma_start(struct mantis_pci *mantis); extern void mantis_dma_stop(struct mantis_pci *mantis); extern int mantis_i2c_init(struct mantis_pci *mantis); extern int mantis_i2c_exit(struct mantis_pci *mantis); extern int mantis_core_init(struct mantis_pci *mantis); extern int mantis_core_exit(struct mantis_pci *mantis); extern void mantis_fe_powerup(struct mantis_pci *mantis); extern void mantis_fe_powerdown(struct mantis_pci *mantis);

#endif

/*
Mantis PCI bridge driver

Copyright (C) 2005 Manu Abraham ([email protected])

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include <asm/page.h>
#include <linux/vmalloc.h>
#include "mantis_common.h"

#define RISC_WRITE (0x01 << 28)
#define RISC_JUMP (0x07 << 28)
#define RISC_SYNC (0x08 << 28)

#define RISC_WR_SOL (1 << 27)
#define RISC_WR_EOL (1 << 26)
#define RISC_IRQ (1 << 24)

#define RISC_STATUS(status) ((((~status) & 0x0F) << 20) | ((status & 0x0F) << 16))

#define RISC_FLUSH() mantis->risc_pos = 0
#define RISC_INSTR(opcode) mantis->risc_cpu[mantis->risc_pos++] = cpu_to_le32(opcode)

static void mantis_free(struct pci_dev *pdev)

{
	struct mantis_pci *mantis = pci_get_drvdata(pdev);
	
	if (mantis->buf_cpu) {
		dprintk(verbose, MANTIS_ERROR, 1, "DMA=%lx", (unsigned long) mantis->buf_dma);
		pci_free_consistent(pdev, mantis->buf_size, mantis->buf_cpu, mantis->buf_dma);
		mantis->buf_cpu = NULL;
	}
	if (mantis->risc_cpu) {
		dprintk(verbose, MANTIS_ERROR, 1, "RISC=%lx", (unsigned long) mantis->risc_dma);
		pci_free_consistent(pdev, mantis->risc_size, mantis->risc_cpu, mantis->risc_dma);
		mantis->risc_cpu = NULL;
	}
}

static int mantis_alloc_buffers(struct pci_dev *pdev)

{	
	struct mantis_pci *mantis = pci_get_drvdata(pdev);
	
	if (!mantis->buf_cpu) {
		mantis->buf_size = 128 * 1024;
		mantis->buf_cpu = pci_alloc_consistent(pdev, mantis->buf_size, &mantis->buf_dma);
		if (!mantis->buf_cpu) {
			dprintk(verbose, MANTIS_ERROR, 1, "DMA buffer allocation failed");
			goto err;
		}
		dprintk(verbose, MANTIS_ERROR, 1, "DMA=0x%lx cpu=0x%p size=%d",
				(unsigned long) mantis->buf_dma, mantis->buf_cpu, mantis->buf_size);
	}
	if (!mantis->risc_cpu) {
		mantis->risc_size = PAGE_SIZE;
		mantis->risc_cpu = pci_alloc_consistent(pdev, mantis->risc_size, &mantis->risc_dma);
		if (!mantis->risc_cpu) {
			dprintk(verbose, MANTIS_ERROR, 1, "RISC program allocation failed");
			mantis_free(pdev);
			goto err;
		}
		dprintk(verbose, MANTIS_ERROR, 1, "RISC=0x%lx cpu=0x%p size=%d",
				(unsigned long) mantis->risc_dma, mantis->risc_cpu, mantis->risc_size);
	}

	return 0;
err:
	dprintk(verbose, MANTIS_ERROR, 1, "Out of memory (?) .....");
	return -ENOMEM;
}

static int mantis_calc_lines(struct mantis_pci *mantis)

{
	mantis->block_bytes = mantis->buf_size >> 4;
	mantis->block_count = 1 << 4;
	mantis->line_bytes = mantis->block_bytes;
	mantis->line_count = mantis->block_count;

	while (mantis->line_bytes > 4095) {
		mantis->line_bytes >>= 1;
		mantis->line_count <<= 1;
	}
	dprintk(verbose, MANTIS_DEBUG, 1, "Mantis RISC block bytes=[%d], line bytes=[%d], line count=[%d]",
			mantis->block_bytes, mantis->line_bytes, mantis->line_count);

	if (mantis->line_count > 255) {
		dprintk(verbose, MANTIS_ERROR, 1, "Buffer size error");
		return -EINVAL;
	}

	return 0;
}

static void mantis_risc_program(struct mantis_pci *mantis)

{
	u32 buf_pos = 0;
	u32 line;

	dprintk(verbose, MANTIS_DEBUG, 1, "Mantis create RISC program");
	RISC_FLUSH();
	RISC_INSTR(RISC_SYNC);
	RISC_INSTR(0);

	dprintk(verbose, MANTIS_DEBUG, 1, "risc len lines %u, bytes per line %u", mantis->line_count, mantis->line_bytes);
	for (line = 0; line < mantis->line_count; line++) {
		dprintk(verbose, MANTIS_DEBUG, 1, "RISC PROG line=[%d]", line);
		if (!(buf_pos % mantis->block_bytes))
			RISC_INSTR(RISC_WRITE | RISC_WR_SOL | RISC_WR_EOL | RISC_IRQ |
						RISC_STATUS(((buf_pos / mantis->block_bytes) +
						(mantis->block_count - 1)) % mantis->block_count) | mantis->line_bytes);
		else
			RISC_INSTR(RISC_WRITE | RISC_WR_SOL | RISC_WR_EOL | mantis->line_bytes);
		RISC_INSTR(mantis->buf_dma + buf_pos);
		buf_pos += mantis->line_bytes;
	}
	RISC_INSTR(RISC_SYNC);
	RISC_INSTR(0);
	RISC_INSTR(RISC_JUMP);
	RISC_INSTR(mantis->risc_dma);
}

void mantis_dma_start(struct mantis_pci *mantis)

{
	dprintk(verbose, MANTIS_DEBUG, 1, "Mantis Start DMA engine");
	/*
	 *	Create RISC program to be loaded to the RISC core
	 */
	mantis_risc_program(mantis);

	/*
	 *	Load the RISC program from Host to RISC
	 */
	mmwrite(cpu_to_le32(mantis->risc_dma), MANTIS_RISC_START);

	/*
	 *	Enable INT's, start DMA engine
	 *	DMA engine: Enable FIFO, Enable Data Capture
	 *	Enable RISC engine
	 */
	mmwrite(mmread(MANTIS_INT_MASK) | MANTIS_FIFO_EN | MANTIS_DCAP_EN | MANTIS_RISC_EN, MANTIS_INT_MASK);
	mmwrite(mmread(MANTIS_DMA_CTL) | MANTIS_FIFO_EN | MANTIS_DCAP_EN | MANTIS_RISC_EN, MANTIS_DMA_CTL);

}

void mantis_dma_stop(struct mantis_pci *mantis)

{
	dprintk(verbose, MANTIS_DEBUG, 1, "Mantis Stop DMA engine");

	/*
	 *	Stop only the DMA engine, do not clear
	 *	the other interrupts
	 */
	mmwrite((mmread(MANTIS_DMA_CTL) & ~(MANTIS_FIFO_EN | MANTIS_DCAP_EN | MANTIS_RISC_EN)), MANTIS_DMA_CTL);
}

int __devinit mantis_dma_init(struct pci_dev *pdev)

{
	int err = 0;
	struct mantis_pci *mantis = pci_get_drvdata(pdev);
	
	dprintk(verbose, MANTIS_DEBUG, 1, "Mantis DMA init");
	if (mantis_alloc_buffers(pdev) < 0) {
		dprintk(verbose, MANTIS_ERROR, 1, "Error allocating DMA buffer");

		/*
		 *	Disable all interrupts during initialization
		 *	to avoid spurious interrupts creating hell.
		 *	Clear all INT's, disable FIFO RISC.
		 */
		mmwrite(0x00, MANTIS_INT_MASK);
		mmwrite(0x00, MANTIS_DMA_CTL);

		goto err;
	}
	if ((err = mantis_calc_lines(mantis)) < 0) {
		dprintk(verbose, MANTIS_ERROR, 1, "Mantis calc lines failed");
		goto err;
	}

	return 0;
err:
	return err;
}

void mantis_dma_xfer(unsigned long data)

{
	struct mantis_pci *mantis = (struct mantis_pci *) data;

	while (mantis->last_block != mantis->finished_block) {
		(mantis->ts_size ? dvb_dmx_swfilter_204: dvb_dmx_swfilter)
		(&mantis->demux, &mantis->buf_cpu[mantis->last_block * mantis->block_bytes], mantis->block_bytes);
		mantis->last_block = (mantis->last_block + 1) % mantis->block_count;
	}
}

int __devexit mantis_dma_exit(struct pci_dev *pdev)

{
	struct mantis_pci *mantis = pci_get_drvdata(pdev);
	
	if (mantis->buf_cpu) {
		dprintk(verbose, MANTIS_ERROR, 1, "DMA=0x%lx cpu=0x%p size=%d",
				(unsigned long) mantis->buf_dma, mantis->buf_cpu, mantis->buf_size);
		pci_free_consistent(pdev, mantis->buf_size, mantis->buf_cpu, mantis->buf_dma);
		mantis->buf_cpu = NULL;
	}
	if (mantis->risc_cpu) {
		dprintk(verbose, MANTIS_ERROR, 1, "RISC=0x%lx cpu=0x%p size=%d",
				(unsigned long) mantis->risc_dma, mantis->risc_cpu, mantis->risc_size);
		pci_free_consistent(pdev, mantis->risc_size, mantis->risc_cpu, mantis->risc_dma);
		mantis->risc_cpu = NULL;
	}

	return 0;
}

/*
Mantis PCI bridge driver

Copyright (C) 2005 Manu Abraham ([email protected])

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include <linux/bitops.h>
#include "mantis_common.h"
#include "mantis_core.h"

#include "dmxdev.h"
#include "dvbdev.h"
#include "dvb_demux.h"
#include "dvb_frontend.h"
#include "stv0299.h"
/*

	     0x01, 0x15,
	     0x02, 0x00,	// 0x30
	     0x03, 0x00,
	     0x04, 0x2a,	// 0x7d
	     0x05, 0x85,	// 0x35
	     0x06, 0x02,
	     0x07, 0x00,
	     0x08, 0x00,	// 0xc3
	     0x0C, 0x01,	// 0x00
	     0x0D, 0x81,
	     0x0E, 0x23,	// 0x23
	     0x0F, 0x12,	// 0x12
	     0x10, 0x7e,	// 0x7e
	     0x11, 0x84,
	     0x12, 0xB9,
	     0x13, 0x88,	// 0x88
	     0x14, 0x89,	// 0x89
	     0x15, 0xC9,
	     0x16, 0x00,	// 0x00
	     0x17, 0x5c,	// 0x5c
	     0x18, 0x00,	// 0x00
	     0x19, 0x00,	// 0x00
	     0x1A, 0x00,
	     0x1C, 0x00,
	     0x1D, 0x00,
	     0x1E, 0x00,
	     0x1F, 0x3A,
	     0x20, 0x2E,
	     0x21, 0x80,
	     0x22, 0xFF,
	     0x23, 0xC1,
	     0x28, 0x00,
	     0x29, 0x1E,
	     0x2A, 0x14,
	     0x2B, 0x0F,
	     0x2C, 0x09,
	     0x2D, 0x05,
	     0x31, 0x1F,
	     0x32, 0x19,
	     0x33, 0xFE,
	     0x34, 0x93,
	     0xff, 0xff,

*/

static u8 lgtdqcs001f_inittab[] = {
	0x01, 0x15,
	0x02, 0x00,
	0x03, 0x00,
	0x04, 0x2a,
	0x05, 0x85,
	0x06, 0x02,
	0x07, 0x00,
	0x08, 0x00,
	0x0c, 0x01,
	0x0d, 0x81,
	0x0e, 0x44,
//	0x0f, 0x14,
	0x0f, 0x94,
	0x10, 0x3c,
	0x11, 0x84,
	0x12, 0xb9,
	0x13, 0xb5,
	0x14, 0x4f,
	0x15, 0xc9,
	0x16, 0x80,
	0x17, 0x36,
	0x18, 0xfb,
	0x19, 0xcf,
	0x1a, 0xbc,
	0x1c, 0x2b,
	0x1d, 0x27,
	0x1e, 0x00,
	0x1f, 0x0b,
	0x20, 0xa1,
	0x21, 0x60,
	0x22, 0x00,
	0x23, 0x00,
	0x28, 0x00,
	0x29, 0x28,
	0x2a, 0x14,
	0x2b, 0x0f,
	0x2c, 0x09,
	0x2d, 0x05,
	0x31, 0x1f,
	0x32, 0x19,
	0x33, 0xfc,
	0x34, 0x13,
	0xff, 0xff,
};

static int lgtdqcs001f_pll_set(struct dvb_frontend* fe, struct i2c_adapter *i2c, struct dvb_frontend_parameters* params)

{
	u8 buf[4];
	u32 div;
	struct i2c_msg msg = { .addr = 0x61, .flags = 0, .buf = buf, .len = sizeof(buf) };

// struct i2c_msg msg_r = { .addr = 0x61, .flags = I2C_M_RD, .buf = buf, .len = 1 };

	div = params->frequency / 250;

	buf[0] = (div >> 8) & 0x7f;
	buf[1] =  div & 0xff;
	buf[2] =  0x83;
	buf[3] =  0xc0;

	if (params->frequency < 1531000)
		buf[3] |= 0x04;
	else
		buf[3] &= ~0x04;

	if (i2c_transfer(i2c, &msg, 4) < 0) {
		dprintk(verbose, MANTIS_ERROR, 1, "Write: I2C Transfer failed");
		return -EIO;
	}
	msleep(100);
/*
	if (i2c_transfer(i2c, &msg_r, 1) < 0) {
		dprintk(verbose, MANTIS_ERROR, 1, "Read: I2C Transfer failed");
		return -EIO;
	}
	dprintk(verbose, MANTIS_ERROR, 1, "TSA5059 read=[0x%02x]", buf[0]);

*/

	return 0;
}

static int lgtdqcs001f_set_symbol_rate(struct dvb_frontend* fe, u32 srate, u32 ratio)

{
	u8 aclk = 0;
	u8 bclk = 0;

	if (srate < 1500000) { aclk = 0xb7; bclk = 0x47; }
	else if (srate < 3000000) { aclk = 0xb7; bclk = 0x4b; }
	else if (srate < 7000000) { aclk = 0xb7; bclk = 0x4f; }
	else if (srate < 14000000) { aclk = 0xb7; bclk = 0x53; }
	else if (srate < 30000000) { aclk = 0xb6; bclk = 0x53; }
	else if (srate < 45000000) { aclk = 0xb4; bclk = 0x51; }

	stv0299_writereg (fe, 0x13, aclk);
	stv0299_writereg (fe, 0x14, bclk);

	stv0299_writereg (fe, 0x1f, (ratio >> 16) & 0xff);
	stv0299_writereg (fe, 0x20, (ratio >>  8) & 0xff);
	stv0299_writereg (fe, 0x21, (ratio      ) & 0xf0);

	return 0;
}

static struct stv0299_config lgtdqcs001f_config = {

	.demod_address = 0x68,
	.inittab = lgtdqcs001f_inittab,
	.mclk = 88000000UL,
//	.invert = 0,
	.invert = 1,
//	.enhanced_tuning = 0,
	.skip_reinit = 0,
	.lock_output = STV0229_LOCKOUTPUT_0,
	.volt13_op0_op1 = STV0299_VOLT13_OP0,
	.min_delay_ms = 100,
	.set_symbol_rate = lgtdqcs001f_set_symbol_rate,
	.pll_set = lgtdqcs001f_pll_set,
};

static int mantis_dvb_start_feed(struct dvb_demux_feed *dvbdmxfeed)

{
	struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
	struct mantis_pci *mantis = dvbdmx->priv;

	dprintk(verbose, MANTIS_DEBUG, 1, "Mantis DVB Start feed");
	if (!dvbdmx->dmx.frontend) {
		dprintk(verbose, MANTIS_DEBUG, 1, "no frontend ?");
		return -EINVAL;
	}
	mantis->feeds++;
	dprintk(verbose, MANTIS_DEBUG, 1, "mantis start feed, feeds=%d", mantis->feeds);
	if (mantis->feeds == 1)	 {
		dprintk(verbose, MANTIS_DEBUG, 1, "mantis start feed & dma");
		mantis_dma_start(mantis);
	}

	return mantis->feeds;
}

static int mantis_dvb_stop_feed(struct dvb_demux_feed *dvbdmxfeed)

{
	struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
	struct mantis_pci *mantis = dvbdmx->priv;

	dprintk(verbose, MANTIS_DEBUG, 1, "Mantis DVB Stop feed");
	if (!dvbdmx->dmx.frontend) {
		dprintk(verbose, MANTIS_DEBUG, 1, "no frontend ?");
		return -EINVAL;
	}
	mantis->feeds--;
	if (mantis->feeds == 0) {
		dprintk(verbose, MANTIS_DEBUG, 1, "mantis stop feed and dma");
		mantis_dma_stop(mantis);
	}

	return 0;
}

int __devinit mantis_dvb_init(struct mantis_pci *mantis)

{
	int result;
	dprintk(verbose, MANTIS_DEBUG, 1, "dvb_register_adapter");
	if (dvb_register_adapter(&mantis->dvb_adapter, "Mantis dvb adapter", THIS_MODULE) < 0) {
		dprintk(verbose, MANTIS_ERROR, 1, "Error registering adapter");
		return -ENODEV;
	}
	mantis->dvb_adapter.priv = mantis;
	mantis->demux.dmx.capabilities = DMX_TS_FILTERING | DMX_SECTION_FILTERING | DMX_MEMORY_BASED_FILTERING;
	mantis->demux.priv = mantis;
	mantis->demux.filternum = 256;
	mantis->demux.feednum = 256;
	mantis->demux.start_feed = mantis_dvb_start_feed;
	mantis->demux.stop_feed = mantis_dvb_stop_feed;
	mantis->demux.write_to_decoder = NULL;

	mantis->ts_size = 1;
	dprintk(verbose, MANTIS_DEBUG, 1, "dvb_dmx_init");
	if ((result = dvb_dmx_init(&mantis->demux)) < 0) {
		dprintk(verbose, MANTIS_ERROR, 1, "dvb_dmx_init failed, ERROR=%d", result);
		goto err0;
	}
	mantis->dmxdev.filternum = 256;
	mantis->dmxdev.demux = &mantis->demux.dmx;
	mantis->dmxdev.capabilities = 0;
	dprintk(verbose, MANTIS_DEBUG, 1, "dvb_dmxdev_init");
	if ((result = dvb_dmxdev_init(&mantis->dmxdev, &mantis->dvb_adapter)) < 0) {
		dprintk(verbose, MANTIS_ERROR, 1, "dvb_dmxdev_init failed, ERROR=%d", result);
		goto err1;
	}
	mantis->fe_hw.source = DMX_FRONTEND_0;
	if ((result = mantis->demux.dmx.add_frontend(&mantis->demux.dmx, &mantis->fe_hw)) < 0) {
		dprintk(verbose, MANTIS_ERROR, 1, "dvb_dmx_init failed, ERROR=%d", result);
		goto err2;
	}
	mantis->fe_mem.source = DMX_MEMORY_FE;
	if ((result = mantis->demux.dmx.add_frontend(&mantis->demux.dmx, &mantis->fe_mem)) < 0) {
		dprintk(verbose, MANTIS_ERROR, 1, "dvb_dmx_init failed, ERROR=%d", result);
		goto err3;
	}
	if ((result = mantis->demux.dmx.connect_frontend(&mantis->demux.dmx, &mantis->fe_hw)) < 0) {
		dprintk(verbose, MANTIS_ERROR, 1, "dvb_dmx_init failed, ERROR=%d", result);
		goto err4;
	}
	dvb_net_init(&mantis->dvb_adapter, &mantis->dvbnet, &mantis->demux.dmx);
	tasklet_init(&mantis->tasklet, mantis_dma_xfer, (unsigned long) mantis);
	mantis_frontend_init(mantis);

	return 0;
	
	/*
	 *	Error conditions ..
	 */

err4:

	mantis->demux.dmx.remove_frontend(&mantis->demux.dmx, &mantis->fe_mem);
err3:
	mantis->demux.dmx.remove_frontend(&mantis->demux.dmx, &mantis->fe_hw);
err2:
	dvb_dmxdev_release(&mantis->dmxdev);
err1:
	dvb_dmx_release(&mantis->demux);
err0:
	dvb_unregister_adapter(&mantis->dvb_adapter);

	return result;
}

int __devinit mantis_frontend_init(struct mantis_pci *mantis)

{
	dprintk(verbose, MANTIS_DEBUG, 1, "Mantis frontend Init");
	mantis_fe_powerup(mantis);

	switch (mantis->sub_device_id) {
	case 0x0016:	/*	VP-1033		*/
		dprintk(verbose, MANTIS_DEBUG, 1, "Searching for a STV0299 (DVB-S) frontend");
		mantis->fe = stv0299_attach(&lgtdqcs001f_config, &mantis->adapter);
		if (mantis->fe) {
			dprintk(verbose, MANTIS_DEBUG, 1, "Mantis DVB-S STV0299 frontend attach success");
			dprintk(verbose, MANTIS_ERROR, 1, "found STV0299 DVB-S frontend @ 0x%02x", lgtdqcs001f_config.demod_address);
		}
		break;
	default:
		dprintk(verbose, MANTIS_DEBUG, 1, "Unknown frontend type:[0x%02x]", mantis->sub_device_id);
		return -ENODEV;
	}
	if (mantis->fe == NULL)
		dprintk(verbose, MANTIS_ERROR, 1, "A frontend was not found for the device");
	else {
		if (dvb_register_frontend(&mantis->dvb_adapter, mantis->fe)) {
			dprintk(verbose, MANTIS_ERROR, 1, "ERROR: Frontend registration failed");
			if (mantis->fe->ops->release)
				mantis->fe->ops->release(mantis->fe);
			mantis->fe = NULL;
			return -ENODEV;
		}
	}

	return 0;
}

int __devexit mantis_dvb_exit(struct mantis_pci *mantis)

{
	tasklet_kill(&mantis->tasklet);
	dvb_net_release(&mantis->dvbnet);
	mantis->demux.dmx.remove_frontend(&mantis->demux.dmx, &mantis->fe_mem);
	mantis->demux.dmx.remove_frontend(&mantis->demux.dmx, &mantis->fe_hw);
	dvb_dmxdev_release(&mantis->dmxdev);
	dvb_dmx_release(&mantis->demux);
	if (mantis->fe)
		dvb_unregister_frontend(mantis->fe);
	dprintk(verbose, MANTIS_DEBUG, 1, "dvb_unregister_adapter");
	dvb_unregister_adapter(&mantis->dvb_adapter);

	return 0;
}

/*
Mantis PCI bridge driver

Copyright (C) 2005 Manu Abraham ([email protected])

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <asm/io.h>
#include <linux/ioport.h>
#include <asm/pgtable.h>
#include <asm/page.h>
#include "mantis_common.h"

#define I2C_HW_B_MANTIS 0x1c

/*
* Wait till we receive INT I2CRACK. I2CDONE will follow suit,
* since we save the states, we would have both I2CRACK and I2CDONE set
* we don't wait till eternity though.
*/
static int mantis_ack_wait(struct mantis_pci *mantis)

{
	u8 timeout = 0;

	/*
	 *	I2CRACK will be reset when I2CDONE is set
	 *
	 *
	 */
	while (!(mantis->mantis_int_stat & MANTIS_INT_I2CRACK)) {
		udelay(1000);
		timeout++;
		dprintk(verbose, MANTIS_DEBUG, 0, ".");
		if (timeout > 50)
			return -1;
	}

	/*
	 *	Reset STATUS flags, before we leave.
	 *	since we save states.
	 */
	dprintk(verbose, MANTIS_DEBUG, 1, "Status  ... MANTIS_INT_STAT=[0x%08x]", mantis->mantis_int_stat);
	mantis->mantis_int_stat &= (~MANTIS_INT_I2CRACK | ~MANTIS_INT_I2CDONE);

	return 0;
}

static inline void mantis_i2cint_set(struct mantis_pci *mantis)

{
	/*
	 *	We don't have a mask for I2CRACK
	 *	I2CRACK is cleared on setting I2CDONE (?)
	 */
//	mmwrite(mmread(MANTIS_INT_STAT) & MANTIS_INT_I2CRACK, MANTIS_INT_STAT);
	mmwrite(mmread(MANTIS_INT_MASK) | MANTIS_INT_I2CDONE, MANTIS_INT_MASK);
	udelay(1000);
}

static int mantis_i2c_pagewrite(struct mantis_pci *mantis, struct i2c_msg *msg)

{
	u8  i;
	u32 txd = 0;

	dprintk(verbose, MANTIS_DEBUG, 1, "Writing to [0x%02x]", msg->addr);

	for (i = 0; i < msg->len; i++) {
		dprintk(verbose, MANTIS_DEBUG, 1, "Data<W[%d]>=[0x%02x]", i, msg->buf[i]);
		txd |= (msg->addr << 25) | (msg->buf[i] << 8) | MANTIS_I2C_RATE_3 | MANTIS_I2C_STOP | MANTIS_I2C_PGMODE;
		if (i == 3)
			txd &= ~MANTIS_I2C_STOP;

		mantis_i2cint_set(mantis);
		mmwrite(txd, MANTIS_I2CDATA_CTL);

		if (mantis_ack_wait(mantis) < 0) {
			dprintk(verbose, MANTIS_DEBUG, 1, "ACK failed");
			return -1;
		}
		udelay(10);
	}

	return 0;
}

/* FIXME! needs to be fixed, we need to put things back */
static int mantis_i2c_pageread(struct mantis_pci *mantis, struct i2c_msg *msg)

{
	u8 i;
	u32 rxd = 0;

	for (i = 0; i < 2; i++) {
		rxd |= ((msg->addr << 25) | (1 << 24)) | MANTIS_I2C_RATE_3 | MANTIS_I2C_STOP | MANTIS_I2C_PGMODE;
		if (i == 1)
			rxd &= ~MANTIS_I2C_STOP;
		mmwrite(mmread(MANTIS_INT_STAT) | MANTIS_INT_I2CDONE, MANTIS_INT_STAT);
		mmwrite(mmread(MANTIS_INT_MASK) | MANTIS_INT_I2CDONE, MANTIS_INT_MASK);
		udelay(1000);
		if (mantis_ack_wait(mantis) < 0)
			return -EIO;
		rxd = mmread(MANTIS_I2CDATA_CTL);
		udelay(500);
	}

	return 0;
}

///* FIXME ! Right now writes only a byte */
static int mantis_i2c_writebyte(struct mantis_pci *mantis, struct i2c_msg *msg)

{
	u32 txd = 0;
	mantis->mantis_int_stat = 0;

	txd = (msg->addr << 25) | (msg->buf[0] << 16) | (msg->buf[1] << 8) | MANTIS_I2C_RATE_3;
	dprintk(verbose, MANTIS_DEBUG, 1, "Writing to [0x%02x],  Data<W>=[%02x]", msg->addr, msg->buf[1]);
	mmwrite(txd, MANTIS_I2CDATA_CTL);

	if (mantis_ack_wait(mantis) < 0) {
		dprintk(verbose, MANTIS_DEBUG, 1, "Slave did not ACK !");
		return -EIO;
	}

	return 0;
}

static int mantis_i2c_readbyte(struct mantis_pci *mantis, struct i2c_msg *msg)

{
	u32 rxd;
	u16 i;
	u8  subaddr;

	/*
	 *	Clear saved previous states
	 */
	mantis->mantis_int_stat &= (~MANTIS_INT_I2CDONE | ~MANTIS_INT_I2CRACK);
	subaddr = msg->buf[0];

	for (i = 0; i < msg->len; i++) {
		/*	Rate settings		*/
		rxd = ((msg->addr << 25) | (1 << 24)) | (subaddr << 16) | MANTIS_I2C_RATE_3;

		mmwrite(rxd, MANTIS_I2CDATA_CTL);

		/*
		 *	Enable INT_I2CDONE
		 */
		mmwrite(mmread(MANTIS_INT_MASK) | MANTIS_INT_I2CDONE, MANTIS_INT_MASK);
		udelay(1000);

		/*	Slave ACK		*/
		if (mantis_ack_wait(mantis) < 0)
			return -EIO;

		rxd = mmread(MANTIS_I2CDATA_CTL);
		udelay(500);
		msg->buf[i] = (rxd >> 8) & 0xff;
		dprintk(verbose, MANTIS_DEBUG, 1, "Reading from [0x%02x], Data<R[%d]>=[0x%02x]", msg->addr, i, msg->buf[i]);
		subaddr++;
	}

	return 0;
}

static int mantis_i2c_readbytes(struct mantis_pci *mantis, struct i2c_msg *msg)

{
	u32 rxd;
	u16 i;

	mantis->mantis_int_stat = 0;

	for (i = 0; i < msg[1].len; i++) {
		/*	Rate settings		*/
		rxd = ((msg[0].addr  << 25) | (1 << 24)) | (msg[0].buf[0] << 16) | MANTIS_I2C_RATE_3;

		mmwrite(rxd, MANTIS_I2CDATA_CTL);

		/*
		 *	Need to wait till I2CDONE, rather than slave I2CRACK
		 */
		mmwrite(mmread(MANTIS_INT_MASK) | MANTIS_INT_I2CDONE, MANTIS_INT_MASK);
		udelay(1000);
		if (mantis_ack_wait(mantis) < 0)
			return -EIO;

		rxd = mmread(MANTIS_I2CDATA_CTL);
		udelay(500);
		msg[1].buf[i] = (rxd >> 8) & 0xff;
		dprintk(verbose, MANTIS_DEBUG, 1, "Reading from [0x%02x], Data<R[%d]>=[0x%02x]", msg[0].addr, i, msg[1].buf[i]);
		msg[0].buf[0]++;
	}

	return 0;
}

/* FIXME ! Right now writes only a byte */
static int mantis_i2c_writebytes(struct mantis_pci *mantis, struct i2c_msg *msg)

{
	u32 txd = 0;
	mantis->mantis_int_stat = 0;

	txd = (msg->addr << 25) | (msg->buf[0] << 16) | (msg->buf[1] << 8) | MANTIS_I2C_RATE_3;
	dprintk(verbose, MANTIS_DEBUG, 1, "Writing to [0x%02x], Data<W>=[%02x]", msg->addr, msg->buf[1]);
	mmwrite(txd, MANTIS_I2CDATA_CTL);

	if (mantis_ack_wait(mantis) < 0)
		return -EIO;

	return 0;
}

static int mantis_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msg, int num)

{
	int retval = 0;
	struct mantis_pci *mantis;

	mantis = i2c_get_adapdata(adapter);

	if (num == 4) {
		if (msg[0].flags & I2C_M_RD) {
			if ((retval = mantis_i2c_pageread(mantis, msg)) < 0)
				return retval;
		} else {
			if ((retval = mantis_i2c_pagewrite(mantis, msg)) < 0)
				return retval;
		}
	/*	 STV0299 operation (???)	*/
	} else if (num == 2) {
		mantis_i2cint_set(mantis);
		/*	Sanity check	*/
		if (msg[0].buf != NULL && msg[1].buf != NULL) {
			/*	I2C operation type	*/
			if (msg[0].flags == 0 && msg[1].flags == I2C_M_RD) {
				/*	Read	*/
				if ((retval = mantis_i2c_readbytes(mantis, msg)) < 0)
					return retval;
			} else {
				/*	Write	*/
				if ((retval = mantis_i2c_writebytes(mantis, msg)) < 0)
					return retval;
			}
		}
	/*	Normal case operation	*/
	} else {
		mantis_i2cint_set(mantis);
		if (msg[0].flags & I2C_M_RD) {
			if ((retval = mantis_i2c_readbyte(mantis, msg)) < 0)
				return retval;
		} else {
			if ((retval = mantis_i2c_writebyte(mantis, msg)) < 0)
				return retval;
		}
	}

	return num;
}

static __u32 mantis_i2c_func(struct i2c_adapter *adapter)

{
	return I2C_FUNC_SMBUS_EMUL;
}

static int mantis_attach_inform(struct i2c_client *client)

{
	struct mantis_pci *mantis;

	mantis = i2c_get_adapdata(client->adapter);
	dprintk(verbose, MANTIS_ERROR, 1, "mantis: %s i2c attach [addr = 0x%x, client=%s]",
		client->driver->name, client->addr, i2c_clientname(client));

	return 0;
}
static struct i2c_algorithm mantis_algo = {
	.name			= "Mantis I2C",
	.id			= I2C_HW_B_MANTIS,
	.master_xfer		= mantis_i2c_xfer,
	.functionality		= mantis_i2c_func,
};
static struct i2c_adapter mantis_i2c_adapter = {
	.owner			= THIS_MODULE,
	.class			= I2C_CLASS_TV_DIGITAL,
	.algo			= &mantis_algo,
	.client_register	= mantis_attach_inform,
};

int __devinit mantis_i2c_init(struct mantis_pci *mantis)

{
	memcpy(&mantis->adapter, &mantis_i2c_adapter, sizeof (mantis_i2c_adapter));
	i2c_set_adapdata(&mantis->adapter, mantis);
	mantis->i2c_rc = i2c_add_adapter(&mantis->adapter);
	if (mantis->i2c_rc < 0)
		return mantis->i2c_rc;

	dprintk(verbose, MANTIS_DEBUG, 1, "Initializing I2C ..");

	/*
	 *	I2CRACK will be reset when I2CDONE is set
	 *	Clear and setup I2C int mask
	 */
	mmwrite(mmread(MANTIS_INT_STAT) & ( ~MANTIS_INT_I2CDONE | ~MANTIS_INT_I2CRACK), MANTIS_INT_STAT);
	mmwrite(mmread(MANTIS_INT_MASK) | MANTIS_INT_I2CDONE | MANTIS_INT_I2CRACK, MANTIS_INT_MASK);
	dprintk(verbose, MANTIS_DEBUG, 1, "INT_STAT=[0x%08x], INT_MASK=[0x%08x]", mmread(MANTIS_INT_STAT), mmread(MANTIS_INT_MASK));

	return 0;
}

int __devexit mantis_i2c_exit(struct mantis_pci *mantis)

{
	dprintk(verbose, MANTIS_DEBUG, 1, "Removing I2C adapter");
	return i2c_del_adapter(&mantis->adapter);
}

/*
Mantis PCI bridge driver

Copyright (C) 2005 Manu Abraham ([email protected])

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include <asm/io.h>
#include <asm/pgtable.h>
#include <asm/page.h>
#include <linux/kmod.h>
#include <linux/vmalloc.h>
#include <linux/init.h>
#include <linux/device.h>
#include "mantis_common.h"
#include "mantis_core.h"

#include <asm/irq.h>
#include <linux/signal.h>
#include <linux/sched.h>
#include <linux/interrupt.h>

unsigned int verbose = 1;
module_param(verbose, int, 0644);
MODULE_PARM_DESC(verbose, "verbose startup messages, default is 1 (yes)");

#define PCI_VENDOR_ID_MANTIS 0x1822
#define PCI_DEVICE_ID_MANTIS_R11 0x4e35
#define DRIVER_NAME "Mantis"

static struct pci_device_id mantis_pci_table[] = {
	{ PCI_DEVICE(PCI_VENDOR_ID_MANTIS, PCI_DEVICE_ID_MANTIS_R11) },
	{ 0 },
};

MODULE_DEVICE_TABLE(pci, mantis_pci_table);

static irqreturn_t mantis_pci_irq(int irq, void *dev_id, struct pt_regs *regs)

{
	int count = 0, interrupts = 0;
	u32 stat = 0, mask = 0, temp;

	struct mantis_pci *mantis = dev_id;

	if (mantis == NULL)
		dprintk(verbose, MANTIS_DEBUG, 1, "Aeio, mantis ISR");

	/*
	 *	Don't read each and everytime, but read in one shot
	 *	We need to save the states for later too ..
	 *
	 */

	stat = mmread(MANTIS_INT_STAT);
	mask = mmread(MANTIS_INT_MASK);

	/*
	 *	To speed up things, do a basic check whether it is our
	 *	interrupt.
	 *	Also, we don't have a mask for I2CRACK
	 */
	if (!(stat & (mask | MANTIS_INT_I2CRACK)) ) {
		dprintk(verbose, MANTIS_ERROR, 1, "Not ours !");
		return IRQ_NONE;
	}

	/*
	 *	The int is for us, clear int condition
	 */
	mmwrite(stat, MANTIS_INT_STAT);

	/*
	 *	Check how many 1's are in the INT_STAT, Count will reflect
	 *	no. of interrupts, we should loop count times.
	 *
	 */
	 temp = stat;
	 while (temp) {
		if (temp & 0x01) {
			interrupts++;
			dprintk(verbose, MANTIS_DEBUG, 1, "Interrupt @ %d", interrupts);
		}
		temp >>= 1;
		count++;

		/*
		 *	We should never loop more than reg. width
		 *	else, Bail out
		 */
		if (count > 32) {
			dprintk(verbose, MANTIS_ERROR, 1, "Bailing out !!");
			break;
		}
	}
	count = 0;
	while (count < interrupts) {
		if ((stat & mask) & MANTIS_INT_RISCEN) {
			dprintk(verbose, MANTIS_DEBUG, 1, "**** DMA enabl ****");
			mantis->mantis_int_stat |= MANTIS_INT_RISCEN;
			stat &= ~MANTIS_INT_RISCEN;

		/*
		 *	It's a shame that there is no mask for I2CRACK !
		 */
		} else if (stat & MANTIS_INT_I2CRACK) {
			dprintk(verbose, MANTIS_DEBUG, 1, "**** I2C R-ACK ****");
			mantis->mantis_int_stat |= MANTIS_INT_I2CRACK;
			stat &= ~MANTIS_INT_I2CRACK;

		} else if ((stat & mask) & MANTIS_INT_IRQ0) {
			dprintk(verbose, MANTIS_DEBUG, 1, "**** INT IRQ-0 ****");
			mantis->mantis_int_stat |= MANTIS_INT_IRQ0;
			stat &= ~MANTIS_INT_IRQ0;

		} else if ((stat & mask) & MANTIS_INT_IRQ1) {
			dprintk(verbose, MANTIS_DEBUG, 1, "**** INT IRQ-1 ****");
			mantis->mantis_int_stat |= MANTIS_INT_IRQ1;
			stat &= ~MANTIS_INT_IRQ1;

		} else if ((stat & mask) & MANTIS_INT_OCERR) {
			dprintk(verbose, MANTIS_DEBUG, 1, "**** INT OCERR ****");
			mantis->mantis_int_stat |= MANTIS_INT_OCERR;
			stat &= ~MANTIS_INT_OCERR;

		} else if ((stat & mask) & MANTIS_INT_PABORT) {
			dprintk(verbose, MANTIS_DEBUG, 1, "**** INT PABRT ****");
			mantis->mantis_int_stat |= MANTIS_INT_PABORT;
			stat &= ~MANTIS_INT_PABORT;

		} else if ((stat & mask) & MANTIS_INT_RIPERR) {
			dprintk(verbose, MANTIS_DEBUG, 1, "**** INT RIPRR ****");
			mantis->mantis_int_stat |= MANTIS_INT_RIPERR;
			stat &= ~MANTIS_INT_RIPERR;

		} else if ((stat & mask) & MANTIS_INT_PPERR) {
			dprintk(verbose, MANTIS_DEBUG, 1, "**** INT PPERR ****");
			mantis->mantis_int_stat |= MANTIS_INT_PPERR;
			stat &= ~MANTIS_INT_PPERR;

		} else if ((stat & mask) & MANTIS_INT_FTRGT) {
			dprintk(verbose, MANTIS_DEBUG, 1, "**** INT FTRGT ****");
			mantis->mantis_int_stat |= MANTIS_INT_FTRGT;
			stat &= ~MANTIS_INT_FTRGT;

		} else if ((stat & mask) & MANTIS_INT_RISCI) {
			mantis->finished_block = (mantis->mantis_int_stat & MANTIS_INT_RISCSTAT) >> 28;
			tasklet_schedule(&mantis->tasklet);
			stat &= ~MANTIS_INT_RISCI;

		} else if ((stat & mask) & MANTIS_INT_I2CDONE) {
			dprintk(verbose, MANTIS_DEBUG, 1, "**** I2C DONE  ****");
			mantis->mantis_int_stat |= MANTIS_INT_I2CDONE;
			stat &= ~MANTIS_INT_I2CDONE;

		} else {
			dprintk(verbose, MANTIS_DEBUG, 1, "Unknown INT ???");
		}

		count++;

		/*
		 *	Being paranoid, check whether we are too loopy !
		 */
		if (count > 32) {
			dprintk(verbose, MANTIS_ERROR, 1, "IRQ Lockup, clearing INT mask");
			dprintk(verbose, MANTIS_ERROR, 1, "Count=%d, Interrupts=%d, stat=[0x%08x]", count, interrupts, stat);
			mmwrite(0, MANTIS_INT_MASK);
			stat = 0, interrupts = 0;

			break;
		}
	}

	return IRQ_HANDLED;
}

static int __devinit mantis_pci_probe(struct pci_dev *pdev,
const struct pci_device_id *mantis_pci_table)

{
	u8 revision, latency;
	struct mantis_pci *mantis;

	if (pci_enable_device(pdev)) {
		dprintk(verbose, MANTIS_ERROR, 1, "Mantis PCI enable failed");
		goto exit;
	}

	/*
	 *	We need to change this to kzalloc on newer kernels
	 *	kzalloc() = kmalloc() + memset()
	 *	Code simplification for 2.6.14 and upwards
	 */	 
	mantis = kmalloc(sizeof (struct mantis_pci), GFP_KERNEL);
//	mantis = kzalloc(sizeof (struct mantis_pci), GFP_KERNEL);
	if (mantis == NULL) {
		dprintk(verbose, MANTIS_ERROR, 1, "Out of memory");
		goto disable;
	}
	memset(mantis, 0, sizeof (struct mantis_pci));

	mantis->mantis_addr = pci_resource_start(pdev, 0);
	if (!request_mem_region(pci_resource_start(pdev, 0),
			pci_resource_len(pdev, 0), DRIVER_NAME)) {
		goto free;
	}

	if ((mantis->mantis_mmio =
				ioremap(mantis->mantis_addr, 0x1000)) == NULL) {
		dprintk(verbose, MANTIS_ERROR, 1, "IO remap failed");
		goto release;
	}
	
	/*
	 *	Clear and disable all interrupts at startup
	 *	to avoid lockup situations
	 */
	mmwrite(0x00, MANTIS_INT_STAT);
	mmwrite(0x00, MANTIS_INT_MASK);
	if (request_irq(pdev->irq, mantis_pci_irq, SA_SHIRQ | SA_INTERRUPT,
						DRIVER_NAME, mantis) < 0) {
		dprintk(verbose, MANTIS_ERROR, 1, "Mantis IRQ reg failed");
		goto unmap;
	}
	pci_set_master(pdev);
	pci_set_drvdata(pdev, mantis);
	pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &latency);
	pci_read_config_byte(pdev, PCI_CLASS_REVISION, &revision);
	mantis->latency = latency;
	mantis->revision = revision;

	/*
	 *	Setup default latency 32 if none specified
	 */
	if (!latency)
		pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 32);

	printk("%s: Mantis Rev %d, ", __func__, mantis->revision);
	printk("irq: %d, latency: %d\nmemory: 0x%lx, mmio: 0x%p\n", 
			pdev->irq, mantis->latency, mantis->mantis_addr, 
				mantis->mantis_mmio);

	if ((mantis_dma_init(pdev)) < 0) {
		dprintk(verbose, MANTIS_ERROR, 1, "Mantis DMA init failed");
		goto disable;
	}

	/*
	 *	No more PCI specific stuff !
	 */	
	if (mantis_core_init(mantis) < 0) {
		dprintk(verbose, MANTIS_ERROR, 1, "Mantis core init failed");
		goto dma_exit;
	}

	return 0;
	
	/*
	 *	Error conditions ..
	 */

dma_exit:

	if (mantis_dma_exit(pdev) < 0)
		dprintk(verbose, MANTIS_ERROR, 1, "Mantis DMA exit failed");

	dprintk(verbose, MANTIS_DEBUG, 1, "Err: IO Unmap");
unmap:
	if (mantis->mantis_mmio)
		iounmap(mantis->mantis_mmio);
release:
	dprintk(verbose, MANTIS_DEBUG, 1, "Err: Release regions");
	release_mem_region(pci_resource_start(pdev, 0),
				pci_resource_len(pdev, 0));
free:
	kfree(mantis);

disable:

	pci_disable_device(pdev);
	dprintk(verbose, MANTIS_DEBUG, 1, "Err: Disabling device");
exit:
	dprintk(verbose, MANTIS_DEBUG, 1, "Err:");
	return -ENODEV;
}

static void __devexit mantis_pci_remove(struct pci_dev *pdev)

{
	struct mantis_pci *mantis = pci_get_drvdata(pdev);
	if (mantis == NULL) {
		dprintk(verbose, MANTIS_ERROR, 1, "Aeio, Mantis NULL ptr");
		return;
	}
	mantis_core_exit(mantis);

	printk("%s: Mantis irq: %d,latency: %d\n memory: 0x%lx, mmio: 0x%p",
		__func__, pdev->irq, mantis->latency, mantis->mantis_addr,
			mantis->mantis_mmio);

	free_irq(pdev->irq, mantis);
	pci_release_regions(pdev);
	if (mantis_dma_exit(pdev) < 0)
		dprintk(verbose, MANTIS_ERROR, 1, "DMA exit failed");

	pci_set_drvdata(pdev, NULL);
	pci_disable_device(pdev);
	kfree(mantis);
}

static struct pci_driver mantis_pci_driver = {
.name = DRIVER_NAME,
.id_table = mantis_pci_table,
.probe = mantis_pci_probe,
.remove = __devexit_p(mantis_pci_remove),
};

static int __init mantis_pci_init(void)

{
	return pci_register_driver(&mantis_pci_driver);
}

static void __exit mantis_pci_exit(void)

{
	pci_unregister_driver(&mantis_pci_driver);
}

module_init(mantis_pci_init);
module_exit(mantis_pci_exit);

MODULE_DESCRIPTION("Mantis PCI DTV bridge driver"); MODULE_AUTHOR("Manu Abraham"); MODULE_LICENSE("GPL");

A message that is not in the archive (sent off-list or not yet received): [email protected]

#42 lore raw
Quoted 17 lines · Jiri Slaby
Quoted 5 lines · Manu Abraham

I have fixed most of the stuff, it is partly working, not ready yet as there are some more things to be added to ..
I have attached what i was working on.

One more thing.

Quoted 5 lines

static int __devinit mantis_pci_init(void)

{
       return pci_register_driver(&mantis_pci_driver);
}
Quoted 10 lines
Quoted 6 lines

static void __devexit mantis_pci_exit(void)

{
       pci_unregister_driver(&mantis_pci_driver);
}

These should be __init and __exit, which could be freed in more cases (only few bytes, but...).

Ack'd..

Thanks,
Manu

A message that is not in the archive (sent off-list or not yet received): [email protected]

#44 lore raw
Quoted 2 lines · Jiri Slaby

Oh no, Manu's nightmare is back :d.

If you don't help people, why do you make a big noise ?
You could atleast keep quiet, to reduce the spam as you termed it. Right ?

Manu