Commit 459f7923 authored by Stefan Richter's avatar Stefan Richter

firewire: clean up fw_card reference counting

This is a functionally equivalent replacement of the current reference
counting of struct fw_card instances.  It only converts it to common
idioms as suggested by Kristian Høgsberg:
  - struct kref replaces atomic_t as the counter.
  - wait_for_completion is used to wait for all card users to complete.

BTW, it may make sense to count card->flush_timer and card->work as
card users too.
Signed-off-by: default avatarStefan Richter <stefanr@s5r6.in-berlin.de>
parent 2147ef20
...@@ -16,12 +16,15 @@ ...@@ -16,12 +16,15 @@
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/ */
#include <linux/module.h> #include <linux/completion.h>
#include <linux/errno.h> #include <linux/crc-itu-t.h>
#include <linux/delay.h> #include <linux/delay.h>
#include <linux/device.h> #include <linux/device.h>
#include <linux/errno.h>
#include <linux/kref.h>
#include <linux/module.h>
#include <linux/mutex.h> #include <linux/mutex.h>
#include <linux/crc-itu-t.h>
#include "fw-transaction.h" #include "fw-transaction.h"
#include "fw-topology.h" #include "fw-topology.h"
#include "fw-device.h" #include "fw-device.h"
...@@ -396,7 +399,6 @@ fw_card_initialize(struct fw_card *card, const struct fw_card_driver *driver, ...@@ -396,7 +399,6 @@ fw_card_initialize(struct fw_card *card, const struct fw_card_driver *driver,
{ {
static atomic_t index = ATOMIC_INIT(-1); static atomic_t index = ATOMIC_INIT(-1);
atomic_set(&card->device_count, 0);
card->index = atomic_inc_return(&index); card->index = atomic_inc_return(&index);
card->driver = driver; card->driver = driver;
card->device = device; card->device = device;
...@@ -405,6 +407,8 @@ fw_card_initialize(struct fw_card *card, const struct fw_card_driver *driver, ...@@ -405,6 +407,8 @@ fw_card_initialize(struct fw_card *card, const struct fw_card_driver *driver,
card->color = 0; card->color = 0;
card->broadcast_channel = BROADCAST_CHANNEL_INITIAL; card->broadcast_channel = BROADCAST_CHANNEL_INITIAL;
kref_init(&card->kref);
init_completion(&card->done);
INIT_LIST_HEAD(&card->transaction_list); INIT_LIST_HEAD(&card->transaction_list);
spin_lock_init(&card->lock); spin_lock_init(&card->lock);
setup_timer(&card->flush_timer, setup_timer(&card->flush_timer,
...@@ -506,6 +510,14 @@ static struct fw_card_driver dummy_driver = { ...@@ -506,6 +510,14 @@ static struct fw_card_driver dummy_driver = {
.enable_phys_dma = dummy_enable_phys_dma, .enable_phys_dma = dummy_enable_phys_dma,
}; };
void
fw_card_release(struct kref *kref)
{
struct fw_card *card = container_of(kref, struct fw_card, kref);
complete(&card->done);
}
void void
fw_core_remove_card(struct fw_card *card) fw_core_remove_card(struct fw_card *card)
{ {
...@@ -521,12 +533,10 @@ fw_core_remove_card(struct fw_card *card) ...@@ -521,12 +533,10 @@ fw_core_remove_card(struct fw_card *card)
card->driver = &dummy_driver; card->driver = &dummy_driver;
fw_destroy_nodes(card); fw_destroy_nodes(card);
/*
* Wait for all device workqueue jobs to finish. Otherwise the /* Wait for all users, especially device workqueue jobs, to finish. */
* firewire-core module could be unloaded before the jobs ran. fw_card_put(card);
*/ wait_for_completion(&card->done);
while (atomic_read(&card->device_count) > 0)
msleep(100);
cancel_delayed_work_sync(&card->work); cancel_delayed_work_sync(&card->work);
fw_flush_transactions(card); fw_flush_transactions(card);
......
...@@ -168,7 +168,7 @@ static void fw_device_release(struct device *dev) ...@@ -168,7 +168,7 @@ static void fw_device_release(struct device *dev)
fw_node_put(device->node); fw_node_put(device->node);
kfree(device->config_rom); kfree(device->config_rom);
kfree(device); kfree(device);
atomic_dec(&card->device_count); fw_card_put(card);
} }
int fw_device_enable_phys_dma(struct fw_device *device) int fw_device_enable_phys_dma(struct fw_device *device)
...@@ -946,8 +946,7 @@ void fw_node_event(struct fw_card *card, struct fw_node *node, int event) ...@@ -946,8 +946,7 @@ void fw_node_event(struct fw_card *card, struct fw_node *node, int event)
*/ */
device_initialize(&device->device); device_initialize(&device->device);
atomic_set(&device->state, FW_DEVICE_INITIALIZING); atomic_set(&device->state, FW_DEVICE_INITIALIZING);
atomic_inc(&card->device_count); device->card = fw_card_get(card);
device->card = card;
device->node = fw_node_get(node); device->node = fw_node_get(node);
device->node_id = node->node_id; device->node_id = node->node_id;
device->generation = card->generation; device->generation = card->generation;
......
...@@ -19,14 +19,15 @@ ...@@ -19,14 +19,15 @@
#ifndef __fw_transaction_h #ifndef __fw_transaction_h
#define __fw_transaction_h #define __fw_transaction_h
#include <linux/completion.h>
#include <linux/device.h> #include <linux/device.h>
#include <linux/dma-mapping.h> #include <linux/dma-mapping.h>
#include <linux/firewire-constants.h> #include <linux/firewire-constants.h>
#include <linux/kref.h>
#include <linux/list.h> #include <linux/list.h>
#include <linux/spinlock_types.h> #include <linux/spinlock_types.h>
#include <linux/timer.h> #include <linux/timer.h>
#include <linux/workqueue.h> #include <linux/workqueue.h>
#include <asm/atomic.h>
#define TCODE_IS_READ_REQUEST(tcode) (((tcode) & ~1) == 4) #define TCODE_IS_READ_REQUEST(tcode) (((tcode) & ~1) == 4)
#define TCODE_IS_BLOCK_PACKET(tcode) (((tcode) & 1) != 0) #define TCODE_IS_BLOCK_PACKET(tcode) (((tcode) & 1) != 0)
...@@ -219,7 +220,8 @@ extern struct bus_type fw_bus_type; ...@@ -219,7 +220,8 @@ extern struct bus_type fw_bus_type;
struct fw_card { struct fw_card {
const struct fw_card_driver *driver; const struct fw_card_driver *driver;
struct device *device; struct device *device;
atomic_t device_count; struct kref kref;
struct completion done;
int node_id; int node_id;
int generation; int generation;
...@@ -260,6 +262,20 @@ struct fw_card { ...@@ -260,6 +262,20 @@ struct fw_card {
int bm_generation; int bm_generation;
}; };
static inline struct fw_card *fw_card_get(struct fw_card *card)
{
kref_get(&card->kref);
return card;
}
void fw_card_release(struct kref *kref);
static inline void fw_card_put(struct fw_card *card)
{
kref_put(&card->kref, fw_card_release);
}
/* /*
* The iso packet format allows for an immediate header/payload part * The iso packet format allows for an immediate header/payload part
* stored in 'header' immediately after the packet info plus an * stored in 'header' immediately after the packet info plus an
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment