This is a patch to implement dynamic minor number allocation below the 171 major allocated for ieee1394. I thought that on today's systems one doesn't need to have fixed minors any more, but it'd still be useful to use the ieee1394 major number for any firewire related devices (like the mem1394 I'm implementing at the moment). Thus this patch. Signed-off-by: Johannes Berg diff --git a/drivers/ieee1394/ieee1394_core.c b/drivers/ieee1394/ieee1394_core.c index 25ef5a8..33634ed 100644 --- a/drivers/ieee1394/ieee1394_core.c +++ b/drivers/ieee1394/ieee1394_core.c @@ -29,10 +29,12 @@ #include #include #include +#include #include #include #include #include +#include #include #include @@ -1053,9 +1055,14 @@ static int hpsbpkt_thread(void *__hi) complete_and_exit(&khpsbpkt_complete, 0); } +/* used further below, but needs to be here for initialisation */ +static spinlock_t used_minors_lock; + static int __init ieee1394_init(void) { int i, ret; + + spin_lock_init(&used_minors_lock); skb_queue_head_init(&hpsbpkt_queue); @@ -1197,6 +1204,45 @@ static void __exit ieee1394_cleanup(void module_init(ieee1394_init); module_exit(ieee1394_cleanup); +/* dynamic minor allocation functions */ +static DECLARE_BITMAP(used_minors, IEEE1394_MINOR_DYNAMIC_COUNT); + +int hpsb_cdev_add(struct cdev *chardev) +{ + int minor, ret; + + spin_lock(&used_minors_lock); + minor = find_first_zero_bit(used_minors, IEEE1394_MINOR_DYNAMIC_COUNT); + if (minor >= IEEE1394_MINOR_DYNAMIC_COUNT) { + spin_unlock(&used_minors_lock); + return -ENODEV; + } + set_bit(minor, used_minors); + spin_unlock(&used_minors_lock); + + minor += IEEE1394_MINOR_DYNAMIC_FIRST; + ret = cdev_add(chardev, MKDEV(IEEE1394_MAJOR, minor), 1); + if (unlikely(ret)) { + spin_lock(&used_minors_lock); + clear_bit(minor-IEEE1394_MINOR_DYNAMIC_FIRST, used_minors); + spin_unlock(&used_minors_lock); + } + return ret; +} + +void hpsb_cdev_del(struct cdev *chardev) +{ + dev_t dev; + + BUG_ON(MAJOR(chardev->dev) != IEEE1394_MAJOR); + dev = chardev->dev; + cdev_del(chardev); + + spin_lock(&used_minors_lock); + clear_bit(MINOR(dev) - IEEE1394_MINOR_DYNAMIC_FIRST, used_minors); + spin_unlock(&used_minors_lock); +} + /* Exported symbols */ /** hosts.c **/ @@ -1219,6 +1265,8 @@ EXPORT_SYMBOL(hpsb_selfid_complete); EXPORT_SYMBOL(hpsb_packet_sent); EXPORT_SYMBOL(hpsb_packet_received); EXPORT_SYMBOL_GPL(hpsb_disable_irm); +EXPORT_SYMBOL_GPL(hpsb_cdev_add); +EXPORT_SYMBOL_GPL(hpsb_cdev_del); #ifdef CONFIG_IEEE1394_EXPORT_FULL_API EXPORT_SYMBOL(hpsb_send_phy_config); EXPORT_SYMBOL(hpsb_send_packet_and_wait); diff --git a/drivers/ieee1394/ieee1394_core.h b/drivers/ieee1394/ieee1394_core.h index 3f4bfd5..a00533c 100644 --- a/drivers/ieee1394/ieee1394_core.h +++ b/drivers/ieee1394/ieee1394_core.h @@ -186,19 +186,38 @@ void hpsb_packet_received(struct hpsb_ho * 171:0-255, the various drivers must then cdev_add() their cdev * objects to handle their respective sub-regions. * + * Alternatively, drivers may use a dynamic minor number character + * device by using the functions hpsb_cdev_add and hpsb_cdev_del. + * hpsb_cdev_add requires an initialised struct cdev and will add + * it with cdev_add() automatically, reserving a new minor number + * for the new device (unless cdev_add() fails). It returns the + * status of cdev_add(), or -ENODEV if no minor could be allocated. + * + * Currently 64 minor numbers are reserved for that, if necessary + * this number can be increased by simply adjusting the constant + * IEEE1394_MINOR_DYNAMIC_FIRST. + * * Minor device number block allocations: * * Block 0 ( 0- 15) raw1394 * Block 1 ( 16- 31) video1394 * Block 2 ( 32- 47) dv1394 * - * Blocks 3-14 free for future allocation + * Blocks 3-10 free for future allocation * + * Block 11 (176-191) dynamic allocation region + * Block 12 (192-207) dynamic allocation region + * Block 13 (208-223) dynamic allocation region + * Block 14 (224-239) dynamic allocation region * Block 15 (240-255) reserved for drivers under development, etc. */ #define IEEE1394_MAJOR 171 +#define IEEE1394_MINOR_DYNAMIC_FIRST 176 +#define IEEE1394_MINOR_DYNAMIC_LAST 239 +#define IEEE1394_MINOR_DYNAMIC_COUNT (IEEE1394_MINOR_DYNAMIC_LAST-IEEE1394_MINOR_DYNAMIC_FIRST+1) + #define IEEE1394_MINOR_BLOCK_RAW1394 0 #define IEEE1394_MINOR_BLOCK_VIDEO1394 1 #define IEEE1394_MINOR_BLOCK_DV1394 2 @@ -216,6 +235,11 @@ static inline unsigned char ieee1394_fil return file->f_dentry->d_inode->i_cindex; } +/* add a dynamic ieee1394 device */ +int hpsb_cdev_add(struct cdev *chardev); +/* remove a dynamic ieee1394 device */ +void hpsb_cdev_del(struct cdev *chardev); + extern int hpsb_disable_irm; /* Our sysfs bus entry */