Skip to contentSkip to conversation

staging: comedi: Add helper macro for comedi pci driver boilerplate

PATCH In review H Hartley Sweeten 3 participants 13 messages May 11, 2012 – May 12, 2012 Staging

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 staging: comedi: Add helper macro for comedi pci driver boilerplate

Introduce the module_comedi_pci_driver macro, and the associated register/unregister functions, which is a convenience macro for comedi pci driver modules similar to module_platform_driver. It is intended to be used by drivers where the init/exit section does nothing but register/unregister the comedi driver and associated pci driver. By using this macro it is possible to eliminate a few lines of boilerplate code per comedi pci driver.

Also, when registering the pci driver check for failure and unregister the comedi driver.

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

---

drivers/staging/comedi/comedidev.h +19 -0
index 8828609..92a1199 100644
@@ -310,6 +310,25 @@ int comedi_driver_unregister(struct comedi_driver *);
 	module_driver(__comedi_driver, comedi_driver_register, \
 			comedi_driver_unregister)
 
+struct pci_driver;
+
+int comedi_pci_driver_register(struct comedi_driver *, struct pci_driver *);
+void comedi_pci_driver_unregister(struct comedi_driver *, struct pci_driver *);
+
+/**
+ * module_comedi_pci_driver() - Helper macro for registering a comedi PCI driver
+ * @__comedi_driver: comedi_driver struct
+ * @__pci_driver: pci_driver struct
+ *
+ * Helper macro for comedi PCI drivers which do not do anything special
+ * in module init/exit. This eliminates a lot of boilerplate. Each
+ * module may only use this macro once, and calling it replaces
+ * module_init() and module_exit()
+ */
+#define module_comedi_pci_driver(__comedi_driver, __pci_driver) \
+	module_driver(__comedi_driver, comedi_pci_driver_register, \
+			comedi_pci_driver_unregister, &(__pci_driver))
+
 void init_polling(void);
 void cleanup_polling(void);
 void start_polling(struct comedi_device *);
drivers/staging/comedi/drivers.c +28 -0
index 49681a1..7071109 100644
@@ -950,6 +950,34 @@ void comedi_pci_auto_unconfig(struct pci_dev *pcidev)
 }
 EXPORT_SYMBOL_GPL(comedi_pci_auto_unconfig);
 
+int comedi_pci_driver_register(struct comedi_driver *comedi_driver,
+		struct pci_driver *pci_driver)
+{
+	int ret;
+
+	ret = comedi_driver_register(comedi_driver);
+	if (ret < 0)
+		return ret;
+
+	pci_driver->name = comedi_driver->driver_name;
+	ret = pci_register_driver(pci_driver);
+	if (ret < 0) {
+		comedi_driver_unregister(comedi_driver);
+		return ret;
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL(comedi_pci_driver_register);
+
+void comedi_pci_driver_unregister(struct comedi_driver *comedi_driver,
+		struct pci_driver *pci_driver)
+{
+	pci_unregister_driver(pci_driver);
+	comedi_driver_unregister(comedi_driver);
+}
+EXPORT_SYMBOL(comedi_pci_driver_unregister);
+
 static int comedi_old_usb_auto_config(struct usb_interface *intf,
 				      struct comedi_driver *driver)
 {
#1 lore raw

drivers/staging/comedi/drivers.c void comedi_pci_auto_unconfig(struct pci_dev *pcidev) · line 962

Quoted 10 lines · H Hartley Sweeten
drivers/staging/comedi/drivers.c +10 -0
+int comedi_pci_driver_register(struct comedi_driver *comedi_driver,
+		struct pci_driver *pci_driver)
+{
+	int ret;
+
+	ret = comedi_driver_register(comedi_driver);
+	if (ret<  0)
+		return ret;
+
+	pci_driver->name = comedi_driver->driver_name;

I think it would be better if the driver modules that call this function already had pci_driver->name initialized statically.

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

drivers/staging/comedi/drivers.c void comedi_pci_auto_unconfig(struct pci_dev *pcidev) · line 962

Quoted 10 lines · H Hartley Sweeten
drivers/staging/comedi/drivers.c +10 -0
+int comedi_pci_driver_register(struct comedi_driver *comedi_driver,
+		struct pci_driver *pci_driver)
+{
+	int ret;
+
+	ret = comedi_driver_register(comedi_driver);
+	if (ret<  0)
+		return ret;
+
+	pci_driver->name = comedi_driver->driver_name;

I think it would be better if the driver modules that call this function already had pci_driver->name initialized statically.

I agree, but this is how all the pci drivers do it now.

To change this will require auditing all the comedi pci drivers and adding the static name.

Regards,
Hartley

���{.n�+�������+%�����ݶ��w��{.n�+����{��G�����{ay�ʇڙ�,j��f���h��������z_�(�階�ݢj"���m������G������?����&���~��iO��z��v�^�m���� ������?�I�

#3 lore raw
Quoted 16 lines · H Hartley Sweeten
Quoted 14 lines · Ian Abbott

drivers/staging/comedi/drivers.c void comedi_pci_auto_unconfig(struct pci_dev *pcidev) · line 962

Quoted 10 lines · H Hartley Sweeten
drivers/staging/comedi/drivers.c +10 -0
+int comedi_pci_driver_register(struct comedi_driver *comedi_driver,
+		struct pci_driver *pci_driver)
+{
+	int ret;
+
+	ret = comedi_driver_register(comedi_driver);
+	if (ret<   0)
+		return ret;
+
+	pci_driver->name = comedi_driver->driver_name;

I think it would be better if the driver modules that call this function already had pci_driver->name initialized statically.

Quoted 4 lines

I agree, but this is how all the pci drivers do it now.

To change this will require auditing all the comedi pci drivers and adding the static name.

But if you're going to be calling this function from those drivers anyway, you might as well add the static names at the same time! Saves another round of patches.

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

drivers/staging/comedi/drivers.c void comedi_pci_auto_unconfig(struct pci_dev *pcidev) · line 962

Quoted 10 lines · H Hartley Sweeten
drivers/staging/comedi/drivers.c +10 -0
+int comedi_pci_driver_register(struct comedi_driver *comedi_driver,
+		struct pci_driver *pci_driver)
+{
+	int ret;
+
+	ret = comedi_driver_register(comedi_driver);
+	if (ret<   0)
+		return ret;
+
+	pci_driver->name = comedi_driver->driver_name;

I think it would be better if the driver modules that call this function already had pci_driver->name initialized statically.

Quoted 8 lines
Quoted 4 lines

I agree, but this is how all the pci drivers do it now.

To change this will require auditing all the comedi pci drivers and adding the static name.

But if you're going to be calling this function from those drivers anyway, you might as well add the static names at the same time! Saves another round of patches.

True, but many of the pci drivers still need quite a bit of cleanup. After this patch was accepted I was going to just change the ones that have already had the init/exit stuff cleaned up. Then as I hit all the others I would change them.

I guess I could do it this way:

1) add the "name" to all the pci drivers and remove the "pci_driver->name = comedi_driver->name" from all the module_init functions.

2) add the module_comedi_pci_driver stuff, without the "pci_driver->name = comedi_driver->name"

3) Update all the refactored comedi pci drivers to use the module_comedi_pci_driver macro.

4) As the other comedi pci drivers are refactored, use the module_comedi_pci_driver macro.

How does that sound?

Regards,
Hartley

���{.n�+�������+%�����ݶ��w��{.n�+����{��G�����{ay�ʇڙ�,j��f���h��������z_�(�階�ݢj"���m������G������?����&���~��iO��z��v�^�m���� ������?�I�

#5 lore raw
Quoted 41 lines · H Hartley Sweeten
Quoted 17 lines · Ian Abbott
Quoted 16 lines · H Hartley Sweeten
Quoted 14 lines · Ian Abbott

drivers/staging/comedi/drivers.c void comedi_pci_auto_unconfig(struct pci_dev *pcidev) · line 962

Quoted 10 lines · H Hartley Sweeten
drivers/staging/comedi/drivers.c +10 -0
+int comedi_pci_driver_register(struct comedi_driver *comedi_driver,
+		struct pci_driver *pci_driver)
+{
+	int ret;
+
+	ret = comedi_driver_register(comedi_driver);
+	if (ret<    0)
+		return ret;
+
+	pci_driver->name = comedi_driver->driver_name;

I think it would be better if the driver modules that call this function already had pci_driver->name initialized statically.

Quoted 8 lines
Quoted 4 lines

I agree, but this is how all the pci drivers do it now.

To change this will require auditing all the comedi pci drivers and adding the static name.

But if you're going to be calling this function from those drivers anyway, you might as well add the static names at the same time! Saves another round of patches.

True, but many of the pci drivers still need quite a bit of cleanup. After this patch was accepted I was going to just change the ones that have already had the init/exit stuff cleaned up. Then as I hit all the others I would change them.

I guess I could do it this way:

1) add the "name" to all the pci drivers and remove the "pci_driver->name = comedi_driver->name" from all the module_init functions.

2) add the module_comedi_pci_driver stuff, without the "pci_driver->name = comedi_driver->name"

Quoted 7 lines

3) Update all the refactored comedi pci drivers to use the module_comedi_pci_driver macro.

4) As the other comedi pci drivers are refactored, use the module_comedi_pci_driver macro.

How does that sound?

Okay, but I don't think it's worth doing three patches for each driver where one would do. Adding a one-line .name = "foo", to the single patch for each driver you were planning to do anyway seems like a trivial addition that could be slotted into the patch without too much argument from the "one change per patch" folks, especially if you mention the purpose of that line in the commit message. (I'd defer to the better judgement of Greg though.)

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

Okay, but I don't think it's worth doing three patches for each driver where one would do. Adding a one-line .name = "foo", to the single patch for each driver you were planning to do anyway seems like a trivial addition that could be slotted into the patch without too much argument from the "one change per patch" folks, especially if you mention the purpose of that line in the commit message. (I'd defer to the better judgement of Greg though.)

With this patch my main goal was to not "break" any of the drivers.

As a follow-up to this patch I was going to use the macro in all the pci drivers that have been refactored. Similar to what I did with the module_comedi_driver macro. When I do that I can add the static name field to those drivers.

How about doing this in the register function:

	If (!pci_driver->name)
		pci_driver->name = comedi_driver->driver_name;

This way all the drivers that still need to be refactored will still work.

After all the drivers have been updated, those two lines can be removed.

So we have this patch and one big patch updating all the refactored drivers. Some number of patches refactoring the remaining drivers and using the module_comedi_pci_driver macro. Then one last patch removing the check above.

How does that sound?

Regards,
Hartley

���{.n�+�������+%�����ݶ��w��{.n�+����{��G�����{ay�ʇڙ�,j��f���h��������z_�(�階�ݢj"���m������G������?����&���~��iO��z��v�^�m���� ������?�I�

#7 lore raw
Quoted 26 lines · H Hartley Sweeten
Quoted 7 lines · Ian Abbott

Okay, but I don't think it's worth doing three patches for each driver where one would do. Adding a one-line .name = "foo", to the single patch for each driver you were planning to do anyway seems like a trivial addition that could be slotted into the patch without too much argument from the "one change per patch" folks, especially if you mention the purpose of that line in the commit message. (I'd defer to the better judgement of Greg though.)

With this patch my main goal was to not "break" any of the drivers.

As a follow-up to this patch I was going to use the macro in all the pci drivers that have been refactored. Similar to what I did with the module_comedi_driver macro. When I do that I can add the static name field to those drivers.

How about doing this in the register function:

	If (!pci_driver->name)
		pci_driver->name = comedi_driver->driver_name;

This way all the drivers that still need to be refactored will still work.

After all the drivers have been updated, those two lines can be removed.

Quoted 6 lines

So we have this patch and one big patch updating all the refactored drivers. Some number of patches refactoring the remaining drivers and using the module_comedi_pci_driver macro. Then one last patch removing the check above.

How does that sound?

That sounds reasonable to me.

greg k-h

#8 lore raw
Quoted 11 lines · On 11/05/2012 22:59, [email protected] wrote:
Quoted 10 lines · H Hartley Sweeten

How about doing this in the register function:

	If (!pci_driver->name)
		pci_driver->name = comedi_driver->driver_name;

This way all the drivers that still need to be refactored will still work.

After all the drivers have been updated, those two lines can be removed.

Quoted 8 lines
Quoted 6 lines

So we have this patch and one big patch updating all the refactored drivers. Some number of patches refactoring the remaining drivers and using the module_comedi_pci_driver macro. Then one last patch removing the check above.

How does that sound?

That sounds reasonable to me.

Sounds reasonable to me too.

Signature · 2 lines
-=( Ian Abbott @ MEV Ltd.    E-mail: <[email protected]>        )=-
-=( Tel: +44 (0)161 477 1898   FAX: +44 (0)161 718 3587         )=-
maintainer
#9 lore raw
In reply to H Hartley Sweeten
Quoted 23 lines · H Hartley Sweeten
drivers/staging/comedi/drivers.c +17 -0
@@ -950,6 +950,34 @@ void comedi_pci_auto_unconfig(struct pci_dev *pcidev)
 }
 EXPORT_SYMBOL_GPL(comedi_pci_auto_unconfig);
 
+int comedi_pci_driver_register(struct comedi_driver *comedi_driver,
+		struct pci_driver *pci_driver)
+{
+	int ret;
+
+	ret = comedi_driver_register(comedi_driver);
+	if (ret < 0)
+		return ret;
+
+	pci_driver->name = comedi_driver->driver_name;
+	ret = pci_register_driver(pci_driver);
+	if (ret < 0) {
+		comedi_driver_unregister(comedi_driver);
+		return ret;
+	}
+
+	return 0;

drivers/staging/comedi/drivers.c void comedi_pci_auto_unconfig(struct pci_dev *pcidev) · line 979

Quoted 10 lines
drivers/staging/comedi/drivers.c +10 -0
+}
+EXPORT_SYMBOL(comedi_pci_driver_register);
+
+void comedi_pci_driver_unregister(struct comedi_driver *comedi_driver,
+		struct pci_driver *pci_driver)
+{
+	pci_unregister_driver(pci_driver);
+	comedi_driver_unregister(comedi_driver);
+}
+EXPORT_SYMBOL(comedi_pci_driver_unregister);

EXPORT_SYMBOL_GPL() for these two functions?

thanks,

greg k-h

#10 lore raw
Quoted 3 lines · Greg KH
Quoted 2 lines · H Hartley Sweeten
drivers/staging/comedi/drivers.c +0 -0

<snip>

Quoted 1 line
Quoted 1 line

+EXPORT_SYMBOL(comedi_pci_driver_register);

<snip>

Quoted 3 lines
Quoted 1 line

+EXPORT_SYMBOL(comedi_pci_driver_unregister);

EXPORT_SYMBOL_GPL() for these two functions?

I wasn't sure about that...

Question.. when "should" EXPORT_SYMBOL be used and when should it be EXPORT_SYMBOL_GPL?

Also, do you have any comments on Ian Abbott's comment about the:

drivers/staging/comedi/drivers.c void comedi_pci_auto_unconfig(struct pci_dev *pcidev) · line 962

Quoted 1 line
drivers/staging/comedi/drivers.c +1 -0
+	pci_driver->name = comedi_driver->driver_name;

In the register function?

Regards,
Hartley

#11 lore raw
Quoted 10 lines · H Hartley Sweeten
Quoted 4 lines · Greg KH
Quoted 1 line · H Hartley Sweeten

+EXPORT_SYMBOL(comedi_pci_driver_unregister);

EXPORT_SYMBOL_GPL() for these two functions?

I wasn't sure about that...

Question.. when "should" EXPORT_SYMBOL be used and when should it be EXPORT_SYMBOL_GPL?

Adding EXPORT_SYMBOL_GPL shouldn't remove previous functionality. It doesn't in this case, so that's okay!

Signature · 2 lines
-=( Ian Abbott @ MEV Ltd.    E-mail: <[email protected]>        )=-
-=( Tel: +44 (0)161 477 1898   FAX: +44 (0)161 718 3587         )=-
#12 lore raw
Quoted 7 lines · Ian Abbott
Quoted 6 lines · H Hartley Sweeten
Quoted 4 lines · Greg KH
Quoted 1 line · H Hartley Sweeten

+EXPORT_SYMBOL(comedi_pci_driver_unregister);

EXPORT_SYMBOL_GPL() for these two functions?

Quoted 7 lines
Quoted 4 lines

I wasn't sure about that...

Question.. when "should" EXPORT_SYMBOL be used and when should it be EXPORT_SYMBOL_GPL?

Adding EXPORT_SYMBOL_GPL shouldn't remove previous functionality. It doesn't in this case, so that's okay!

OK. I'll change it.

Regards,
Hartley