Skip to contentSkip to conversation

parport: mfc3: Convert to a modern zorro driver

PATCH Superseded Daniel Palmer 2 participants 2 messages Feb 26 Parallel port

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 parport: mfc3: Convert to a modern zorro driver

Currently this driver causes my Amiga 4000 to crash if it is not a module because it tries to iterate over the zorro devices before that has been setup.

Convert the driver over to the modern zorro driver API so it probes like the other zorro drivers and doesn't crash my machine.

Claude was able to do the manual work of moving the original code but I had to fix up some mistakes, clean up the allocations to use devm versions and so on.

Only build, boot, and "checked the driver is registered correctly" tested as I don't have the card and it seems to be one of the few things winuae doesn't emulate. Using claude to fix it up seemed better than just deleting it considering all of the effort Joerg put in to documenting the hardware etc.

Assisted-by Claude:claude-4.6-sonnet
---
 drivers/parport/parport_mfc3.c | 157 ++++++++++++++++++---------------
 1 file changed, 87 insertions(+), 70 deletions(-)
drivers/parport/parport_mfc3.c +87 -70
index bb1817218d7b..c77282ba78d3 100644
@@ -66,17 +66,19 @@
 #include <asm/irq.h>
 #include <asm/amigaints.h>
 
-/* Maximum Number of Cards supported */
-#define MAX_MFC 5
-
 #undef DEBUG
 
-static struct parport *this_port[MAX_MFC] = {NULL, };
 static volatile int dummy; /* for trigger readds */
 
 #define pia(dev) ((struct pia *)(dev->base))
 static struct parport_operations pp_mfc3_ops;
 
+struct mfc3_card {
+	struct parport *port;
+	unsigned long piabase;
+	bool has_irq;
+};
+
 static void mfc3_write_data(struct parport *p, unsigned char data)
 {
 	pr_debug("write_data %c\n", data);
@@ -173,14 +175,18 @@ static int use_cnt;
 
 static irqreturn_t mfc3_interrupt(int irq, void *dev_id)
 {
-	int i;
+	struct zorro_dev *z = NULL;
 
-	for( i = 0; i < MAX_MFC; i++)
-		if (this_port[i] != NULL)
-			if (pia(this_port[i])->crb & 128) { /* Board caused interrupt */
-				dummy = pia(this_port[i])->pprb; /* clear irq bit */
-				parport_generic_irq(this_port[i]);
-			}
+	while ((z = zorro_find_device(ZORRO_PROD_BSC_MULTIFACE_III, z))) {
+		struct mfc3_card *card = zorro_get_drvdata(z);
+
+		if (!card || !card->port)
+			continue;
+		if (pia(card->port)->crb & 128) { /* Board caused interrupt */
+			dummy = pia(card->port)->pprb; /* clear irq bit */
+			parport_generic_irq(card->port);
+		}
+	}
 	return IRQ_HANDLED;
 }
 
@@ -280,85 +286,96 @@ static struct parport_operations pp_mfc3_ops = {
 
 /* ----------- Initialisation code --------------------------------- */
 
-static int __init parport_mfc3_init(void)
+static int mfc3_probe(struct zorro_dev *z, const struct zorro_device_id *ent)
 {
+	unsigned long piabase = z->resource.start + PIABASE;
+	struct device *dev = &z->dev;
+	struct mfc3_card *card;
 	struct parport *p;
-	int pias = 0;
 	struct pia *pp;
-	struct zorro_dev *z = NULL;
 
-	if (!MACH_IS_AMIGA)
+	card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
+	if (!card)
+		return -ENOMEM;
+
+	if (!devm_request_mem_region(dev, piabase, sizeof(struct pia), "PIA"))
+		return -EBUSY;
+
+	pp = ZTWO_VADDR(piabase);
+	pp->crb = 0;
+	pp->pddrb = 255; /* all data pins output */
+	pp->crb = PIA_DDR | 32 | 8;
+	dummy = pp->pddrb; /* reading clears interrupt */
+	pp->cra = 0;
+	pp->pddra = 0xe0; /* /RESET, /DIR, /AUTO-FEED output */
+	pp->cra = PIA_DDR;
+	pp->ppra = 0; /* reset printer */
+	udelay(10);
+	pp->ppra = 128;
+
+	p = parport_register_port((unsigned long)pp, IRQ_AMIGA_PORTS,
+				  PARPORT_DMA_NONE, &pp_mfc3_ops);
+	if (!p)
 		return -ENODEV;
 
-	while ((z = zorro_find_device(ZORRO_PROD_BSC_MULTIFACE_III, z))) {
-		unsigned long piabase = z->resource.start+PIABASE;
-		if (!request_mem_region(piabase, sizeof(struct pia), "PIA"))
-			continue;
+	card->port = p;
+	card->piabase = piabase;
 
-		pp = ZTWO_VADDR(piabase);
-		pp->crb = 0;
-		pp->pddrb = 255; /* all data pins output */
-		pp->crb = PIA_DDR|32|8;
-		dummy = pp->pddrb; /* reading clears interrupt */
-		pp->cra = 0;
-		pp->pddra = 0xe0; /* /RESET,  /DIR ,/AUTO-FEED output */
-		pp->cra = PIA_DDR;
-		pp->ppra = 0; /* reset printer */
-		udelay(10);
-		pp->ppra = 128;
-		p = parport_register_port((unsigned long)pp, IRQ_AMIGA_PORTS,
-					  PARPORT_DMA_NONE, &pp_mfc3_ops);
-		if (!p)
-			goto out_port;
-
-		if (p->irq != PARPORT_IRQ_NONE) {
-			if (use_cnt++ == 0)
-				if (request_irq(IRQ_AMIGA_PORTS, mfc3_interrupt, IRQF_SHARED, p->name, &pp_mfc3_ops))
-					goto out_irq;
+	if (p->irq != PARPORT_IRQ_NONE) {
+		if (use_cnt++ == 0) {
+			if (request_irq(IRQ_AMIGA_PORTS, mfc3_interrupt,
+					IRQF_SHARED, p->name, &pp_mfc3_ops)) {
+				use_cnt--;
+				goto err_put_port;
+			}
 		}
-		p->dev = &z->dev;
+		card->has_irq = true;
+	}
 
-		this_port[pias++] = p;
-		pr_info("%s: Multiface III port using irq\n", p->name);
-		/* XXX: set operating mode */
+	p->dev = dev;
+	p->private_data = (void *)piabase;
 
-		p->private_data = (void *)piabase;
-		parport_announce_port (p);
+	zorro_set_drvdata(z, card);
 
-		if (pias >= MAX_MFC)
-			break;
-		continue;
+	pr_info("%s: Multiface III port using irq\n", p->name);
+	/* XXX: set operating mode */
+	parport_announce_port(p);
 
-	out_irq:
-		parport_put_port(p);
-	out_port:
-		release_mem_region(piabase, sizeof(struct pia));
-	}
+	return 0;
 
-	return pias ? 0 : -ENODEV;
+err_put_port:
+	parport_put_port(p);
+
+	return -ENODEV;
 }
 
-static void __exit parport_mfc3_exit(void)
+static void mfc3_remove(struct zorro_dev *z)
 {
-	int i;
+	struct mfc3_card *card = zorro_get_drvdata(z);
 
-	for (i = 0; i < MAX_MFC; i++) {
-		if (!this_port[i])
-			continue;
-		parport_remove_port(this_port[i]);
-		if (this_port[i]->irq != PARPORT_IRQ_NONE) {
-			if (--use_cnt == 0) 
-				free_irq(IRQ_AMIGA_PORTS, &pp_mfc3_ops);
-		}
-		release_mem_region(ZTWO_PADDR(this_port[i]->private_data), sizeof(struct pia));
-		parport_put_port(this_port[i]);
-	}
+	parport_remove_port(card->port);
+
+	if (card->has_irq && (--use_cnt == 0))
+		free_irq(IRQ_AMIGA_PORTS, &pp_mfc3_ops);
+
+	parport_put_port(card->port);
 }
 
+static const struct zorro_device_id mfc3_zorro_tbl[] = {
+	{ ZORRO_PROD_BSC_MULTIFACE_III },
+	{ 0 }
+};
+MODULE_DEVICE_TABLE(zorro, mfc3_zorro_tbl);
+
+static struct zorro_driver mfc3_driver = {
+	.name = "parport_mfc3",
+	.id_table = mfc3_zorro_tbl,
+	.probe = mfc3_probe,
+	.remove = mfc3_remove,
+};
+
+module_driver(mfc3_driver, zorro_register_driver, zorro_unregister_driver);
 
 MODULE_AUTHOR("Joerg Dorchain <[email protected]>");
 MODULE_DESCRIPTION("Parport Driver for Multiface 3 expansion cards Parallel Port");
 MODULE_LICENSE("GPL");
-
-module_init(parport_mfc3_init)
-module_exit(parport_mfc3_exit)
Signature · 1 line
2.51.0
#1 lore raw

Hi Daniel,

Thanks for your patch!

Quoted 3 lines · Daniel Palmer

Currently this driver causes my Amiga 4000 to crash if it is not a module because it tries to iterate over the zorro devices before that has been setup.

Interesting, that is not supposed to happen.

Quoted 3 lines

Convert the driver over to the modern zorro driver API so it probes like the other zorro drivers and doesn't crash my machine.

Thanks, doing the conversion is definitely worthwhile!

Quoted 43 lines

Claude was able to do the manual work of moving the original code but I had to fix up some mistakes, clean up the allocations to use devm versions and so on.

Only build, boot, and "checked the driver is registered correctly" tested as I don't have the card and it seems to be one of the few things winuae doesn't emulate. Using claude to fix it up seemed better than just deleting it considering all of the effort Joerg put in to documenting the hardware etc.

Assisted-by Claude:claude-4.6-sonnet
drivers/parport/parport_mfc3.c +6 -4
@@ -66,17 +66,19 @@
 #include <asm/irq.h>
 #include <asm/amigaints.h>
 
-/* Maximum Number of Cards supported */
-#define MAX_MFC 5
-
 #undef DEBUG
 
-static struct parport *this_port[MAX_MFC] = {NULL, };
 static volatile int dummy; /* for trigger readds */
 
 #define pia(dev) ((struct pia *)(dev->base))
 static struct parport_operations pp_mfc3_ops;
 
+struct mfc3_card {
+       struct parport *port;
+       unsigned long piabase;
+       bool has_irq;
+};
+
 static void mfc3_write_data(struct parport *p, unsigned char data)
 {
        pr_debug("write_data %c\n", data);
@@ -173,14 +175,18 @@ static int use_cnt;
 
 static irqreturn_t mfc3_interrupt(int irq, void *dev_id)
 {

drivers/parport/parport_mfc3.c static int use_cnt; · line 180

Quoted 10 lines
drivers/parport/parport_mfc3.c +2 -7
-       int i;
+       struct zorro_dev *z = NULL;
 
-       for( i = 0; i < MAX_MFC; i++)
-               if (this_port[i] != NULL)
-                       if (pia(this_port[i])->crb & 128) { /* Board caused interrupt */
-                               dummy = pia(this_port[i])->pprb; /* clear irq bit */
-                               parport_generic_irq(this_port[i]);
-                       }
+       while ((z = zorro_find_device(ZORRO_PROD_BSC_MULTIFACE_III, z))) {

Please no such while loops in an interrupt handler.
I guess you can just register multiple interrupt handlers (IRQF_SHARED, fwiw), one per instance, and pass the mfc3_card structure as dev_id instead? That would let you get rid of has_irq and use_cnt, too.

drivers/parport/parport_mfc3.c static int use_cnt; · line 190

Quoted 10 lines
drivers/parport/parport_mfc3.c +9 -0
+               struct mfc3_card *card = zorro_get_drvdata(z);
+
+               if (!card || !card->port)
+                       continue;
+               if (pia(card->port)->crb & 128) { /* Board caused interrupt */
+                       dummy = pia(card->port)->pprb; /* clear irq bit */
+                       parport_generic_irq(card->port);
+               }
+       }
        return IRQ_HANDLED;

Pre-existing: the handler should return IRQ_NONE if parport_generic_irq() was not called.

drivers/parport/parport_mfc3.c static int use_cnt; · line 191

Quoted 1 line
drivers/parport/parport_mfc3.c +0 -0
 }

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds