register sysfs device for lp devices
Subsystems (3)
PATCH 1/1 register sysfs device for lp devices
Give class_device_create a device pointer parport drivers can set via a new parport_set_dev.
Also patch parport_gsci and parport_pc as example.
---
Compile and run-tested with linux-2.6.16-rc3-git4.
drivers/char/lp.c +1 -1
@@ -805,7 +805,7 @@if (reset)lp_reset (nr); -class_device_create (lp_class,NULL ,MKDEV (LP_MAJOR , nr),NULL , +class_device_create (lp_class,NULL ,MKDEV (LP_MAJOR , nr), port->dev,"lp%d" , nr);devfs_mk_cdev (MKDEV (LP_MAJOR , nr),S_IFCHR |S_IRUGO |S_IWUGO ,"printers/%d" , nr);
drivers/parport/parport_gsc.c +3 -0
@@ -340,6 +340,9 @@parport_gsc_write_data (p,0 );parport_gsc_data_forward (p); +/* Tell sysfs which device is behind this parport */ +parport_set_dev (p, &dev->dev); +/* Now that we've told the sharing engine about the port, and found out its characteristics, let the high-level drivers know about it. */
drivers/parport/parport_pc.c +1 -0
@@ -2340,6 +2340,7 @@spin_lock (&ports_lock);list_add (&priv->list, &ports_list);spin_unlock (&ports_lock); +parport_set_dev (p, &dev->dev);parport_announce_port (p);return p;
drivers/parport/share.c +5 -0
@@ -341,6 +341,11 @@ tmp->waithead = tmp->waittail =NULL ; +/* +* no sysfs device known unless the announcer sets it before +*/ + tmp->dev =NULL ; +return tmp; }
include/linux/parport.h +7 -0
@@ -258,6 +258,7 @@struct semaphore irq; }; +struct device ;/* A parallel port */ struct parport {unsigned long base;/* base address */
@@ -313,6 +314,8 @@struct list_head full_list;struct parport *slaves[3 ]; + +struct device *dev;/* for the sysfs device symlink */ };#define DEFAULT_SPIN_TIME 500/* us */
@@ -331,6 +334,10 @@struct parport *parport_register_port (unsigned long base,int irq,int dma,struct parport_operations *ops); +/* parport_set_dev set the generic device behind this port, so drivers +* can display it in their sysfs nodes */ +#define parport_set_dev (port,devptr) ((port)->dev = (devptr)) +/* Once a registered port is ready for high-level drivers to use, the low-level driver that registered it should announce it. This will call the high-level drivers' attach() functions (after things like
O
drivers/parport/parport_pc.c line 2344
Quoted 2 lines
drivers/parport/parport_pc.c +1
-0+parport_set_dev (p, &dev->dev);parport_announce_port (p);
Why does the parallel port code use a different whitespace style than the rest of the kernel. It is incorrect and potentially confusing to put a blank between the function name and the arguments. It is like reading, english, with commas in the wrong spot.
Quoted 8 lines · Stephen Hemminger
O
drivers/parport/parport_pc.c line 2344
Quoted 2 lines
drivers/parport/parport_pc.c +1
-0+parport_set_dev (p, &dev->dev);parport_announce_port (p);Why does the parallel port code use a different whitespace style than the rest of the kernel. It is incorrect and potentially confusing to put a blank between the function name and the arguments. It is like reading, english, with commas in the wrong spot.
What is the policy for patches of such code? Should patches adhere to the style of the specific file or that of the whole kernel?
Hochachtungsvoll,
Bernhard R. Link
Quoted 54 lines · Bernhard R. Link
Give
class_device_createa device pointer parport drivers can set via a newparport_set_dev.
Also patchparport_gsciandparport_pcas example.Signed-of-by Bernhard R Link <[email protected]>---
Compile and run-tested with linux-2.6.16-rc3-git4.
Index:
mlinux-2.6.15-git12/drivers/char/lp.c
===================================================================drivers/char/lp.c +1
-1@@ -805,7 +805,7 @@if (reset)lp_reset (nr); -class_device_create (lp_class,NULL ,MKDEV (LP_MAJOR , nr),NULL , +class_device_create (lp_class,NULL ,MKDEV (LP_MAJOR , nr), port->dev,"lp%d" , nr);devfs_mk_cdev (MKDEV (LP_MAJOR , nr),S_IFCHR |S_IRUGO |S_IWUGO ,"printers/%d" , nr);Index:
mlinux-2.6.15-git12/drivers/parport/parport_gsc.c
===================================================================drivers/parport/parport_gsc.c +3
-0@@ -340,6 +340,9 @@parport_gsc_write_data (p,0 );parport_gsc_data_forward (p); +/* Tell sysfs which device is behind this parport */ +parport_set_dev (p, &dev->dev); +/* Now that we've told the sharing engine about the port, and found out its characteristics, let the high-level drivers know about it. */ Index:
mlinux-2.6.15-git12/drivers/parport/parport_pc.c
===================================================================drivers/parport/parport_pc.c +1
-0@@ -2340,6 +2340,7 @@spin_lock (&ports_lock);list_add (&priv->list, &ports_list);spin_unlock (&ports_lock); +parport_set_dev (p, &dev->dev);parport_announce_port (p);return p;Index:
mlinux-2.6.15-git12/drivers/parport/share.c
===================================================================
drivers/parport/share.c line 348
Quoted 8 lines
drivers/parport/share.c +5
-0@@ -341,6 +341,11 @@ tmp->waithead = tmp->waittail =NULL ; +/* +* no sysfs device known unless the announcer sets it before +*/ + tmp->dev =NULL ; +
This is not needed, as tmp is zeroed out earlier in the function.
Quoted 25 lines
drivers/parport/share.c +0
-0return tmp; }Index:
mlinux-2.6.15-git12/include/linux/parport.h
===================================================================include/linux/parport.h +3
-0@@ -258,6 +258,7 @@struct semaphore irq; }; +struct device ;/* A parallel port */ struct parport {unsigned long base;/* base address */ @@ -313,6 +314,8 @@struct list_head full_list;struct parport *slaves[3 ]; + +struct device *dev;/* for the sysfs device symlink */ };#define DEFAULT_SPIN_TIME 500/* us */
include/linux/parport.h line 339
Quoted 6 lines
include/linux/parport.h +3
-0@@ -331,6 +334,10 @@struct parport *parport_register_port (unsigned long base,int irq,int dma,struct parport_operations *ops); +/* parport_set_dev set the generic device behind this port, so drivers +* can display it in their sysfs nodes */ +#define parport_set_dev (port,devptr) ((port)->dev = (devptr))
If you are going to save off a pointer to a structure, you need to increment it's reference count. You aren't doing that here, and bad things might happen if it gets removed from under you :(
thanks,
greg k-h
Quoted 6 lines
Quoted 2 lines
+ * can display it in their sysfs nodes */
+#defineparport_set_dev(port,devptr)((port)->dev = (devptr))If you are going to save off a pointer to a structure, you need to increment it's reference count. You aren't doing that here, and bad things might happen if it gets removed from under you :(
Guess I should think a bit more and copy other code a bit less. The two examples I looked at were sound and network devices. While they differ by calling class_device_create more directly, this does not call a get_device either. (Or where have I missed it?)
I thought that parport would only exist while the device exists and is bound to the driver, when that stops it is removed anyway. But lp.c does not implement lp_detach, so it still keeps this data and it is really exposed even when the device vanished.
Some unrelated strage behaviour from this: When I manualy unbind and bind an parport supplier, via
echo -n "id" > /sys/bus/.../drivers/.../unbind
and then echo -n "id" > /sys/bus/.../drivers/.../bind I get a new lp device every time without the old one vanishing.
Hochachtungsvoll,
Bernhard R. Link