Skip to contentSkip to conversation

staging: comedi: remove this_board macro in the s526 driver

PATCH In review H Hartley Sweeten 3 participants 10 messages May 23, 2012 – May 24, 2012 Staging

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 staging: comedi: remove this_board macro in the s526 driver

The 'thisboard' macro depends on having a local variable with a magic name. The CodingStyle document suggests not doing this to avoid confusion. Remove the macro and use the comedi_board() inline helper to get the dev->board_ptr information.

Cc Ian Abbott <[email protected]>
Cc Mori Hess <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>

---

drivers/staging/comedi/drivers/s526.c +5 -15
index 7a56434..3c8e979 100644
@@ -200,11 +200,6 @@ static const struct s526_board s526_boards[] = {
 #define ADDR_REG(reg) (dev->iobase + (reg))
 #define ADDR_CHAN_REG(reg, chan) (dev->iobase + (reg) + (chan) * 8)
 
-/*
- * Useful for shorthand access to the particular board structure
- */
-#define thisboard ((const struct s526_board *)dev->board_ptr)
-
 /* this structure is for data unique to this hardware driver.  If
    several hardware drivers keep similar information in this structure,
    feel free to suggest moving the variable to the struct comedi_device
@@ -744,6 +739,7 @@ static int s526_dio_insn_config(struct comedi_device *dev,
 
 static int s526_attach(struct comedi_device *dev, struct comedi_devconfig *it)
 {
+	const struct s526_board *board = comedi_board(dev);
 	struct comedi_subdevice *s;
 	int iobase;
 	int i, n;
@@ -754,7 +750,7 @@ static int s526_attach(struct comedi_device *dev, struct comedi_devconfig *it)
 	printk(KERN_INFO "comedi%d: s526: ", dev->minor);
 
 	iobase = it->options[0];
-	if (!iobase || !request_region(iobase, S526_IOSIZE, thisboard->name)) {
+	if (!iobase || !request_region(iobase, S526_IOSIZE, board->name)) {
 		comedi_error(dev, "I/O port conflict");
 		return -EIO;
 	}
@@ -769,13 +765,7 @@ static int s526_attach(struct comedi_device *dev, struct comedi_devconfig *it)
 	}
 	***/
 
-/*
- * Initialize dev->board_name.  Note that we can use the "thisboard"
- * macro now, since we just initialized it in the last line.
- */
-	dev->board_ptr = &s526_boards[0];
-
-	dev->board_name = thisboard->name;
+	dev->board_name = board->name;
 
 /*
  * Allocate the private structure area.  alloc_private() is a
@@ -797,7 +787,7 @@ static int s526_attach(struct comedi_device *dev, struct comedi_devconfig *it)
 	s->type = COMEDI_SUBD_COUNTER;
 	s->subdev_flags = SDF_READABLE | SDF_WRITABLE | SDF_LSAMPL;
 	/* KG: What does SDF_LSAMPL (see multiq3.c) mean? */
-	s->n_chan = thisboard->gpct_chans;
+	s->n_chan = board->gpct_chans;
 	s->maxdata = 0x00ffffff;	/* 24 bit counter */
 	s->insn_read = s526_gpct_rinsn;
 	s->insn_config = s526_gpct_insn_config;
@@ -838,7 +828,7 @@ static int s526_attach(struct comedi_device *dev, struct comedi_devconfig *it)
 
 	s = dev->subdevices + 3;
 	/* digital i/o subdevice */
-	if (thisboard->have_dio) {
+	if (board->have_dio) {
 		s->type = COMEDI_SUBD_DIO;
 		s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
 		s->n_chan = 8;
#1 lore raw
Quoted 12 lines · H Hartley Sweeten

The 'thisboard' macro depends on having a local variable with a magic name. The CodingStyle document suggests not doing this to avoid confusion. Remove the macro and use the comedi_board() inline helper to get the dev->board_ptr information.

Cc Ian Abbott <[email protected]>
Cc Mori Hess <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>

---

drivers/staging/comedi/drivers/s526.c static int s526_attach(struct comedi_device *dev, struct comedi_devconfig *it) · line 776

Quoted 8 lines
drivers/staging/comedi/drivers/s526.c +0 -5
@@ -769,13 +765,7 @@ static int s526_attach(struct comedi_device *dev, struct comedi_devconfig *it)
 	}
 	***/
 
-/*
- * Initialize dev->board_name.  Note that we can use the "thisboard"
- * macro now, since we just initialized it in the last line.
- */
-	dev->board_ptr = &s526_boards[0];

Was this intended? Most of the boards have auto probing so the ->board_ptr gets set automatically. We already called comedi_board() so I wonder if the autoprobed board is the same as the &s526_boards[0];? NULL pointer perhaps? I don't know.

drivers/staging/comedi/drivers/s526.c static int s526_attach(struct comedi_device *dev, struct comedi_devconfig *it) · line 771

Quoted 6 lines
drivers/staging/comedi/drivers/s526.c +1 -2
-
-	dev->board_name = thisboard->name;
+	dev->board_name = board->name;
 
 /*
  * Allocate the private structure area.  alloc_private() is a

regards,
dan carpenter

#2 lore raw
Quoted 16 lines · Dan Carpenter
Quoted 15 lines · H Hartley Sweeten

The 'thisboard' macro depends on having a local variable with a magic name. The CodingStyle document suggests not doing this to avoid confusion. Remove the macro and use the comedi_board() inline helper to get the dev->board_ptr information.

Cc Ian Abbott<[email protected]>
Cc Mori Hess<[email protected]>
Cc Greg Kroah-Hartman<[email protected]>

---

(quoted diff) +0 -0
@@ -769,13 +765,7 @@ static int s526_attach(struct comedi_device *dev, struct comedi_devconfig *it)
  	}
  	***/
 
Quoted 10 lines

drivers/staging/comedi/drivers/s526.c static int s526_attach(struct comedi_device *dev, struct comedi_devconfig *it)

Quoted 5 lines
drivers/staging/comedi/drivers/s526.c +0 -5
-/*
- * Initialize dev->board_name.  Note that we can use the "thisboard"
- * macro now, since we just initialized it in the last line.
- */
-	dev->board_ptr =&s526_boards[0];

Was this intended? Most of the boards have auto probing so the ->board_ptr gets set automatically. We already called comedi_board() so I wonder if the autoprobed board is the same as the&s526_boards[0];? NULL pointer perhaps? I don't know.

If .num_names in struct comedi_driver is non-zero, the core comedi module will set dev->board_ptr to something non-NULL and meaningful to the driver before it calls the driver's ->attach() method. (This is done by the comedi_recognize() function.) The driver can change dev->board_ptr to something else if it wants. In this case, dev->board_ptr will already be set to &s526_boards[0] so there is no point setting it again.

Signature · 2 lines
-=( Ian Abbott @ MEV Ltd.    E-mail: <[email protected]>        )=-
-=( Tel: +44 (0)161 477 1898   FAX: +44 (0)161 718 3587         )=-
#3 lore raw
Quoted 13 lines · Ian Abbott
Quoted 11 lines · Dan Carpenter

drivers/staging/comedi/drivers/s526.c static int s526_attach(struct comedi_device *dev, struct comedi_devconfig *it)

Quoted 5 lines · H Hartley Sweeten
drivers/staging/comedi/drivers/s526.c +0 -5
-/*
- * Initialize dev->board_name.  Note that we can use the "thisboard"
- * macro now, since we just initialized it in the last line.
- */
-	dev->board_ptr =&s526_boards[0];

Was this intended? Most of the boards have auto probing so the ->board_ptr gets set automatically. We already called comedi_board() so I wonder if the autoprobed board is the same as the&s526_boards[0];? NULL pointer perhaps? I don't know.

Quoted 7 lines

If .num_names in struct comedi_driver is non-zero, the core comedi module will set dev->board_ptr to something non-NULL and meaningful to the driver before it calls the driver's ->attach() method. (This is done by the comedi_recognize() function.) The driver can change dev->board_ptr to something else if it wants. In this case, dev->board_ptr will already be set to &s526_boards[0] so there is no point setting it again.

Ian,

Side-note on the boardinfo stuff.

I wonder if it makes sense to create a common boardinfo struct and modify the comedi_driver struct a bit. Something like:

struct comedi_boardinfo {
	const char *name;
	unsigned short	vendor;
	unsigned short device;
	void *private;
};
struct comedi_driver {
	struct comedi_driver *next;

	const char *driver_name;
	struct module *module;
	int (*attach) (struct comedi_device *, struct comedi_devconfig *);
	void (*detach) (struct comedi_device *);
	int (*attach_pci) (struct comedi_device *, struct pci_dev *);
	int (*attach_usb) (struct comedi_device *, struct usb_interface *);

	u32 num_boards;
	struct comedi_boardinfo *board;
};

This would allow comedi_recognize() to walk the boardinfo to find the match without all the ugly casts. It should also allow creating a common comedi_find_pcidev() for all the comedi pci drivers to use when probing for the board_ptr.

This would create a bit of churn but I think it would be a lot cleaner in the end.

What do you think?

Regards,
Hartley

#4 lore raw
Quoted 55 lines · H Hartley Sweeten
Quoted 13 lines · Ian Abbott
Quoted 11 lines · Dan Carpenter

drivers/staging/comedi/drivers/s526.c static int s526_attach(struct comedi_device *dev, struct comedi_devconfig *it)

Quoted 5 lines · H Hartley Sweeten
drivers/staging/comedi/drivers/s526.c +0 -5
-/*
- * Initialize dev->board_name.  Note that we can use the "thisboard"
- * macro now, since we just initialized it in the last line.
- */
-	dev->board_ptr =&s526_boards[0];

Was this intended? Most of the boards have auto probing so the ->board_ptr gets set automatically. We already called comedi_board() so I wonder if the autoprobed board is the same as the&s526_boards[0];? NULL pointer perhaps? I don't know.

Quoted 7 lines

If .num_names in struct comedi_driver is non-zero, the core comedi module will set dev->board_ptr to something non-NULL and meaningful to the driver before it calls the driver's ->attach() method. (This is done by the comedi_recognize() function.) The driver can change dev->board_ptr to something else if it wants. In this case, dev->board_ptr will already be set to&s526_boards[0] so there is no point setting it again.

Ian,

Side-note on the boardinfo stuff.

I wonder if it makes sense to create a common boardinfo struct and modify the comedi_driver struct a bit. Something like:

struct comedi_boardinfo {
	const char *name;
	unsigned short	vendor;
	unsigned short device;
	void *private;
};
struct comedi_driver {
	struct comedi_driver *next;

	const char *driver_name;
	struct module *module;
	int (*attach) (struct comedi_device *, struct comedi_devconfig *);
	void (*detach) (struct comedi_device *);
	int (*attach_pci) (struct comedi_device *, struct pci_dev *);
	int (*attach_usb) (struct comedi_device *, struct usb_interface *);

	u32 num_boards;
	struct comedi_boardinfo *board;
};

This would allow comedi_recognize() to walk the boardinfo to find the match without all the ugly casts. It should also allow creating a common comedi_find_pcidev() for all the comedi pci drivers to use when probing for the board_ptr.

Quoted 4 lines

This would create a bit of churn but I think it would be a lot cleaner in the end.

What do you think?

Please don't do this yet. We might want to implement auto configuration for device types that have no concept of vendor ID or product ID, such as PCMCIA, platform devices, SPI devices etc., either by adding extra attach_xxx hooks to struct comedi_driver for the more common bus types, or a more generic attach_generic hook for the less common bus types.

Also, it would waste space in the ISA-only drivers.

Signature · 2 lines
-=( Ian Abbott @ MEV Ltd.    E-mail: <[email protected]>        )=-
-=( Tel: +44 (0)161 477 1898   FAX: +44 (0)161 718 3587         )=-
#5 lore raw
Quoted 37 lines · Ian Abbott
Quoted 31 lines · H Hartley Sweeten

Side-note on the boardinfo stuff.

I wonder if it makes sense to create a common boardinfo struct and modify the comedi_driver struct a bit. Something like:

struct comedi_boardinfo {
	const char *name;
	unsigned short	vendor;
	unsigned short device;
	void *private;
};
struct comedi_driver {
	struct comedi_driver *next;

	const char *driver_name;
	struct module *module;
	int (*attach) (struct comedi_device *, struct comedi_devconfig *);
	void (*detach) (struct comedi_device *);
	int (*attach_pci) (struct comedi_device *, struct pci_dev *);
	int (*attach_usb) (struct comedi_device *, struct usb_interface *);

	u32 num_boards;
	struct comedi_boardinfo *board;
};

This would allow comedi_recognize() to walk the boardinfo to find the match without all the ugly casts. It should also allow creating a common comedi_find_pcidev() for all the comedi pci drivers to use when probing for the board_ptr.

Quoted 4 lines

This would create a bit of churn but I think it would be a lot cleaner in the end.

What do you think?

Quoted 7 lines

Please don't do this yet. We might want to implement auto configuration for device types that have no concept of vendor ID or product ID, such as PCMCIA, platform devices, SPI devices etc., either by adding extra attach_xxx hooks to struct comedi_driver for the more common bus types, or a more generic attach_generic hook for the less common bus types.

Also, it would waste space in the ISA-only drivers.

OK. I'll hold off on doing anything like this.

But, I think we would actually save space in all the drivers. I just need to think about it a bit more.

Regards,
Hartley

#6 lore raw
In reply to H Hartley Sweeten
Quoted 50 lines · H Hartley Sweeten
Quoted 13 lines · Ian Abbott
Quoted 11 lines · Dan Carpenter

drivers/staging/comedi/drivers/s526.c static int s526_attach(struct comedi_device *dev, struct comedi_devconfig *it)

Quoted 5 lines · H Hartley Sweeten
drivers/staging/comedi/drivers/s526.c +0 -5
-/*
- * Initialize dev->board_name.  Note that we can use the "thisboard"
- * macro now, since we just initialized it in the last line.
- */
-	dev->board_ptr =&s526_boards[0];

Was this intended? Most of the boards have auto probing so the ->board_ptr gets set automatically. We already called comedi_board() so I wonder if the autoprobed board is the same as the&s526_boards[0];? NULL pointer perhaps? I don't know.

Quoted 7 lines

If .num_names in struct comedi_driver is non-zero, the core comedi module will set dev->board_ptr to something non-NULL and meaningful to the driver before it calls the driver's ->attach() method. (This is done by the comedi_recognize() function.) The driver can change dev->board_ptr to something else if it wants. In this case, dev->board_ptr will already be set to &s526_boards[0] so there is no point setting it again.

Ian,

Side-note on the boardinfo stuff.

I wonder if it makes sense to create a common boardinfo struct and modify the comedi_driver struct a bit. Something like:

struct comedi_boardinfo {
	const char *name;
	unsigned short	vendor;
	unsigned short device;
	void *private;
};
struct comedi_driver {
	struct comedi_driver *next;

	const char *driver_name;
	struct module *module;
	int (*attach) (struct comedi_device *, struct comedi_devconfig *);
	void (*detach) (struct comedi_device *);
	int (*attach_pci) (struct comedi_device *, struct pci_dev *);
	int (*attach_usb) (struct comedi_device *, struct usb_interface *);

	u32 num_boards;
	struct comedi_boardinfo *board;
};
Quoted 2 lines

This would allow comedi_recognize() to walk the boardinfo to find the match without all the ugly casts.

That function wouldn't be so bad if we just removed all the consts. There is no point because we drop all of them anyway when we return the struct.

regards,
dan carpenter

#7 lore raw
Quoted 7 lines · Dan Carpenter
Quoted 2 lines · H Hartley Sweeten

This would allow comedi_recognize() to walk the boardinfo to find the match without all the ugly casts.

That function wouldn't be so bad if we just removed all the consts. There is no point because we drop all of them anyway when we return the struct.

Well maybe some of the consts. We could use const char **name_ptr instead of const char *const *name_ptr. *name_ptr is never modified, but as name_ptr is just a local variable, the compiler can figure that out for itself without the extra const.

Signature · 2 lines
-=( Ian Abbott @ MEV Ltd.    E-mail: <[email protected]>        )=-
-=( Tel: +44 (0)161 477 1898   FAX: +44 (0)161 718 3587         )=-
#8 lore raw
In reply to Dan Carpenter
Quoted 16 lines · Dan Carpenter
Quoted 15 lines · H Hartley Sweeten

The 'thisboard' macro depends on having a local variable with a magic name. The CodingStyle document suggests not doing this to avoid confusion. Remove the macro and use the comedi_board() inline helper to get the dev->board_ptr information.

Cc Ian Abbott <[email protected]>
Cc Mori Hess <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>

---

(quoted diff) +0 -0
@@ -769,13 +765,7 @@ static int s526_attach(struct comedi_device *dev, struct comedi_devconfig *it)
 	}
 	***/
 
Quoted 10 lines

drivers/staging/comedi/drivers/s526.c static int s526_attach(struct comedi_device *dev, struct comedi_devconfig *it) · line 776

Quoted 5 lines
drivers/staging/comedi/drivers/s526.c +0 -5
-/*
- * Initialize dev->board_name.  Note that we can use the "thisboard"
- * macro now, since we just initialized it in the last line.
- */
-	dev->board_ptr = &s526_boards[0];

Was this intended? Most of the boards have auto probing so the ->board_ptr gets set automatically. We already called comedi_board() so I wonder if the autoprobed board is the same as the &s526_boards[0];? NULL pointer perhaps? I don't know.

Yes, removing the line was intended. Sorry I didn't mention it in the commit message.

The dev->board_ptr will already be set by comedi_device_attach() before the drivers attach() method is called.

I think the author of this driver misunderstood the skel driver and thought this was needed in order to simulate a "probe". See this comment in the skel.c driver:

/*
* If you can probe the device to determine what device in a series
* it is, this is the place to do it. Otherwise, dev->board_ptr
* should already be initialized.
*/

	/* dev->board_ptr = skel_probe(dev, it); */

The only comedi drivers that modify the dev->board_ptr are the PCI ones. Hopefully I can figure out a way to clean them up... The comedi_board() inline does not change the dev->board_ptr, it's simply a helper to fetch it from the comedi_device.

Regards,
Hartley

#9 lore raw
Quoted 38 lines · H Hartley Sweeten
Quoted 16 lines · Dan Carpenter
Quoted 15 lines · H Hartley Sweeten

The 'thisboard' macro depends on having a local variable with a magic name. The CodingStyle document suggests not doing this to avoid confusion. Remove the macro and use the comedi_board() inline helper to get the dev->board_ptr information.

Cc Ian Abbott <[email protected]>
Cc Mori Hess <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>

---

(quoted diff) +0 -0
@@ -769,13 +765,7 @@ static int s526_attach(struct comedi_device *dev, struct comedi_devconfig *it)
 	}
 	***/
 
Quoted 10 lines

drivers/staging/comedi/drivers/s526.c static int s526_attach(struct comedi_device *dev, struct comedi_devconfig *it) · line 776

Quoted 5 lines
drivers/staging/comedi/drivers/s526.c +0 -5
-/*
- * Initialize dev->board_name.  Note that we can use the "thisboard"
- * macro now, since we just initialized it in the last line.
- */
-	dev->board_ptr = &s526_boards[0];

Was this intended? Most of the boards have auto probing so the ->board_ptr gets set automatically. We already called comedi_board() so I wonder if the autoprobed board is the same as the &s526_boards[0];? NULL pointer perhaps? I don't know.

Yes, removing the line was intended. Sorry I didn't mention it in the commit message.

The dev->board_ptr will already be set by comedi_device_attach() before the drivers attach() method is called.

I think the author of this driver misunderstood the skel driver and thought this was needed in order to simulate a "probe". See this comment in the skel.c driver:

Quoted 7 lines

/*
* If you can probe the device to determine what device in a series
* it is, this is the place to do it. Otherwise, dev->board_ptr
* should already be initialized.
*/

	/* dev->board_ptr = skel_probe(dev, it); */

Heh. Yeah. I saw you removed a bunch of these comments and this was the only one that wasn't commented so I wondered.

Ian already explained how the probing works. Looks good.

regards,
dan carpenter