Subject: [PATCH 1/5] Staging: ipack: use idr interface for numbering buses

Use idr interface to give the bus number. That way, we remove the
limitation of 64 buses.

The mutex is removed because the ida interface uses spinlocks inside, so it is
not needed an extra lock.

Signed-off-by: Samuel Iglesias Gonsalvez <[email protected]>
---
drivers/staging/ipack/ipack.c | 40 ++++++----------------------------------
1 file changed, 6 insertions(+), 34 deletions(-)

diff --git a/drivers/staging/ipack/ipack.c b/drivers/staging/ipack/ipack.c
index e6dc21a..e97be99 100644
--- a/drivers/staging/ipack/ipack.c
+++ b/drivers/staging/ipack/ipack.c
@@ -12,22 +12,14 @@
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

#include <linux/module.h>
-#include <linux/mutex.h>
#include <linux/slab.h>
+#include <linux/idr.h>
#include "ipack.h"

#define to_ipack_dev(device) container_of(device, struct ipack_device, dev)
#define to_ipack_driver(drv) container_of(drv, struct ipack_driver, driver)

-/* used when allocating bus numbers */
-#define IPACK_MAXBUS 64
-
-static DEFINE_MUTEX(ipack_mutex);
-
-struct ipack_busmap {
- unsigned long busmap[IPACK_MAXBUS / (8*sizeof(unsigned long))];
-};
-static struct ipack_busmap busmap;
+static DEFINE_IDA(ipack_ida);

static void ipack_device_release(struct device *dev)
{
@@ -79,26 +71,6 @@ static struct bus_type ipack_bus_type = {
.remove = ipack_bus_remove,
};

-static int ipack_assign_bus_number(void)
-{
- int busnum;
-
- mutex_lock(&ipack_mutex);
- busnum = find_next_zero_bit(busmap.busmap, IPACK_MAXBUS, 1);
-
- if (busnum >= IPACK_MAXBUS) {
- pr_err("too many buses\n");
- busnum = -1;
- goto error_find_busnum;
- }
-
- set_bit(busnum, busmap.busmap);
-
-error_find_busnum:
- mutex_unlock(&ipack_mutex);
- return busnum;
-}
-
struct ipack_bus_device *ipack_bus_register(struct device *parent, int slots,
struct ipack_bus_ops *ops)
{
@@ -109,7 +81,7 @@ struct ipack_bus_device *ipack_bus_register(struct device *parent, int slots,
if (!bus)
return NULL;

- bus_nr = ipack_assign_bus_number();
+ bus_nr = ida_simple_get(&ipack_ida, 0, 0, GFP_KERNEL);
if (bus_nr < 0) {
kfree(bus);
return NULL;
@@ -125,9 +97,7 @@ EXPORT_SYMBOL_GPL(ipack_bus_register);

int ipack_bus_unregister(struct ipack_bus_device *bus)
{
- mutex_lock(&ipack_mutex);
- clear_bit(bus->bus_nr, busmap.busmap);
- mutex_unlock(&ipack_mutex);
+ ida_simple_remove(&ipack_ida, bus->bus_nr);
kfree(bus);
return 0;
}
@@ -189,12 +159,14 @@ EXPORT_SYMBOL_GPL(ipack_device_unregister);

static int __init ipack_init(void)
{
+ ida_init(&ipack_ida);
return bus_register(&ipack_bus_type);
}

static void __exit ipack_exit(void)
{
bus_unregister(&ipack_bus_type);
+ ida_destroy(&ipack_ida);
}

module_init(ipack_init);
--
1.7.10


Subject: [PATCH 4/5] Staging: ipack/bridges/tpci200: avoid dereference of a freed tpci200->info

tpci200->info is used later when uninstalling the module. As there is another
kfree in the proper place, this patch removes the wrong one.

Signed-off-by: Samuel Iglesias Gonsalvez <[email protected]>
---
drivers/staging/ipack/bridges/tpci200.c | 2 --
1 file changed, 2 deletions(-)

diff --git a/drivers/staging/ipack/bridges/tpci200.c b/drivers/staging/ipack/bridges/tpci200.c
index 6751625..e04cb8a 100644
--- a/drivers/staging/ipack/bridges/tpci200.c
+++ b/drivers/staging/ipack/bridges/tpci200.c
@@ -290,8 +290,6 @@ static void tpci200_unregister(struct tpci200_board *tpci200)
pci_disable_device(tpci200->info->pdev);
pci_dev_put(tpci200->info->pdev);

- kfree(tpci200->info);
-
for (i = 0; i < TPCI200_NB_SLOT; i++) {
tpci200->slots[i].io_phys.address = NULL;
tpci200->slots[i].io_phys.size = 0;
--
1.7.10

Subject: [PATCH 5/5] Staging: ipack/bridges/tpci200: fix kernel oops when uninstalling a device

When uninstalling a device, the call to the ipack_bus_ops remove() frees
resources in the ipack device driver but without unregistering the device.

It generates a kernel oops when somebody wants to unregister the device.

Signed-off-by: Samuel Iglesias Gonsalvez <[email protected]>
---
drivers/staging/ipack/bridges/tpci200.c | 13 +------------
1 file changed, 1 insertion(+), 12 deletions(-)

diff --git a/drivers/staging/ipack/bridges/tpci200.c b/drivers/staging/ipack/bridges/tpci200.c
index e04cb8a..46127bc 100644
--- a/drivers/staging/ipack/bridges/tpci200.c
+++ b/drivers/staging/ipack/bridges/tpci200.c
@@ -751,23 +751,12 @@ out:
return res;
}

-static void tpci200_slot_remove(struct tpci200_slot *slot)
-{
- if ((slot->dev == NULL) ||
- (slot->dev->driver == NULL) ||
- (slot->dev->driver->ops == NULL) ||
- (slot->dev->driver->ops->remove == NULL))
- return;
-
- slot->dev->driver->ops->remove(slot->dev);
-}
-
static void tpci200_uninstall(struct tpci200_board *tpci200)
{
int i;

for (i = 0; i < TPCI200_NB_SLOT; i++)
- tpci200_slot_remove(&tpci200->slots[i]);
+ tpci200_slot_unregister(tpci200->slots[i].dev);

tpci200_unregister(tpci200);
kfree(tpci200->slots);
--
1.7.10

Subject: [PATCH 3/5] Staging: ipack/devices/ipoctal: avoid kernel oops when uninstalling

When uninstalling a device, there is a loop of calls that produces, at the end,
two calls to __ipoctal_remove() function with the same ipack_device argument.

The first time works fine, but the second will fail in tty_unregister_driver()

To avoid this situation, the call to __ipoctal_remove() it is done only from the
ipack bus driver and not from the ipack device driver.

Signed-off-by: Samuel Iglesias Gonsalvez <[email protected]>
---
drivers/staging/ipack/devices/ipoctal.c | 7 +------
1 file changed, 1 insertion(+), 6 deletions(-)

diff --git a/drivers/staging/ipack/devices/ipoctal.c b/drivers/staging/ipack/devices/ipoctal.c
index 29f6fa8..a6f424e 100644
--- a/drivers/staging/ipack/devices/ipoctal.c
+++ b/drivers/staging/ipack/devices/ipoctal.c
@@ -853,11 +853,6 @@ static void __ipoctal_remove(struct ipoctal *ipoctal)

tty_unregister_driver(ipoctal->tty_drv);
put_tty_driver(ipoctal->tty_drv);
-
- /* Tell the carrier board to free all the resources for this device */
- if (ipoctal->dev->bus->ops->remove_device != NULL)
- ipoctal->dev->bus->ops->remove_device(ipoctal->dev);
-
list_del(&ipoctal->list);
kfree(ipoctal);
}
@@ -889,7 +884,7 @@ static void __exit ipoctal_exit(void)
struct ipoctal *p, *next;

list_for_each_entry_safe(p, next, &ipoctal_list, list)
- __ipoctal_remove(p);
+ p->dev->bus->ops->remove_device(p->dev);

ipack_driver_unregister(&driver);
}
--
1.7.10

Subject: [PATCH 2/5] Staging: ipack: delete the call to remove() in ipack_driver_register

When a bus driver calls ipack_driver_register(), it should manages the returning
NULL value to undo all the operations it did before this call, and print the
corresponding trace.

It is not a task for the ipack driver to call the remove() function here.

Signed-off-by: Samuel Iglesias Gonsalvez <[email protected]>
---
drivers/staging/ipack/ipack.c | 2 --
1 file changed, 2 deletions(-)

diff --git a/drivers/staging/ipack/ipack.c b/drivers/staging/ipack/ipack.c
index e97be99..a1448e6 100644
--- a/drivers/staging/ipack/ipack.c
+++ b/drivers/staging/ipack/ipack.c
@@ -141,8 +141,6 @@ struct ipack_device *ipack_device_register(struct ipack_bus_device *bus,

ret = device_register(&dev->dev);
if (ret < 0) {
- pr_err("error registering the device.\n");
- dev->driver->ops->remove(dev);
kfree(dev);
return NULL;
}
--
1.7.10