Commit 12a5dfc9 authored by Greg Kroah-Hartman's avatar Greg Kroah-Hartman

greybus: protocol: add a module owner to a protocol

Now that protocols can be in a module, we need to reference count them
to lock them into memory so they can't be removed while in use.  So add
a module owner structure, and have it automatically be assigned when
registering the protocol.
Signed-off-by: default avatarGreg Kroah-Hartman <greg@kroah.com>
Reviewed-by: default avatarAlex Elder <elder@linaro.org>
parent 1b6ea0db
...@@ -39,13 +39,15 @@ static struct gb_protocol *_gb_protocol_find(u8 id, u8 major, u8 minor) ...@@ -39,13 +39,15 @@ static struct gb_protocol *_gb_protocol_find(u8 id, u8 major, u8 minor)
return NULL; return NULL;
} }
int gb_protocol_register(struct gb_protocol *protocol) int __gb_protocol_register(struct gb_protocol *protocol, struct module *module)
{ {
struct gb_protocol *existing; struct gb_protocol *existing;
u8 id = protocol->id; u8 id = protocol->id;
u8 major = protocol->major; u8 major = protocol->major;
u8 minor = protocol->minor; u8 minor = protocol->minor;
protocol->owner = module;
/* /*
* The protocols list is sorted first by protocol id (low to * The protocols list is sorted first by protocol id (low to
* high), then by major version (high to low), and finally * high), then by major version (high to low), and finally
...@@ -92,7 +94,7 @@ int gb_protocol_register(struct gb_protocol *protocol) ...@@ -92,7 +94,7 @@ int gb_protocol_register(struct gb_protocol *protocol)
return 0; return 0;
} }
EXPORT_SYMBOL_GPL(gb_protocol_register); EXPORT_SYMBOL_GPL(__gb_protocol_register);
/* /*
* De-register a previously registered protocol. * De-register a previously registered protocol.
...@@ -135,9 +137,13 @@ struct gb_protocol *gb_protocol_get(u8 id, u8 major, u8 minor) ...@@ -135,9 +137,13 @@ struct gb_protocol *gb_protocol_get(u8 id, u8 major, u8 minor)
spin_lock_irq(&gb_protocols_lock); spin_lock_irq(&gb_protocols_lock);
protocol = _gb_protocol_find(id, major, minor); protocol = _gb_protocol_find(id, major, minor);
if (protocol) { if (protocol) {
protocol_count = protocol->count; if (!try_module_get(protocol->owner)) {
if (protocol_count != U8_MAX) protocol = NULL;
protocol->count++; } else {
protocol_count = protocol->count;
if (protocol_count != U8_MAX)
protocol->count++;
}
} }
spin_unlock_irq(&gb_protocols_lock); spin_unlock_irq(&gb_protocols_lock);
...@@ -163,6 +169,7 @@ void gb_protocol_put(struct gb_protocol *protocol) ...@@ -163,6 +169,7 @@ void gb_protocol_put(struct gb_protocol *protocol)
protocol_count = protocol->count; protocol_count = protocol->count;
if (protocol_count) if (protocol_count)
protocol->count--; protocol->count--;
module_put(protocol->owner);
} }
spin_unlock_irq(&gb_protocols_lock); spin_unlock_irq(&gb_protocols_lock);
if (protocol) if (protocol)
......
...@@ -34,11 +34,15 @@ struct gb_protocol { ...@@ -34,11 +34,15 @@ struct gb_protocol {
gb_connection_init_t connection_init; gb_connection_init_t connection_init;
gb_connection_exit_t connection_exit; gb_connection_exit_t connection_exit;
gb_request_recv_t request_recv; gb_request_recv_t request_recv;
struct module *owner;
}; };
int gb_protocol_register(struct gb_protocol *protocol); int __gb_protocol_register(struct gb_protocol *protocol, struct module *module);
int gb_protocol_deregister(struct gb_protocol *protocol); int gb_protocol_deregister(struct gb_protocol *protocol);
#define gb_protocol_register(protocol) \
__gb_protocol_register(protocol, THIS_MODULE)
struct gb_protocol *gb_protocol_get(u8 id, u8 major, u8 minor); struct gb_protocol *gb_protocol_get(u8 id, u8 major, u8 minor);
void gb_protocol_put(struct gb_protocol *protocol); void gb_protocol_put(struct gb_protocol *protocol);
......
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