2014-11-21 10:51:34 +00:00
|
|
|
/*
|
|
|
|
* MARS Long Distance Replication Software
|
|
|
|
*
|
|
|
|
* This file is part of MARS project: http://schoebel.github.io/mars/
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010-2014 Thomas Schoebel-Theuer
|
|
|
|
* Copyright (C) 2011-2014 1&1 Internet AG
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2010-07-23 11:55:18 +00:00
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/string.h>
|
|
|
|
|
2010-07-30 05:46:22 +00:00
|
|
|
//#define BRICK_DEBUGGING
|
2011-02-23 20:48:06 +00:00
|
|
|
|
2010-07-23 11:55:18 +00:00
|
|
|
#define _STRATEGY
|
2010-07-30 05:46:22 +00:00
|
|
|
|
2010-07-23 11:55:18 +00:00
|
|
|
#include "brick.h"
|
2011-08-12 11:09:48 +00:00
|
|
|
#include "brick_mem.h"
|
2011-03-24 16:05:46 +00:00
|
|
|
|
2012-09-17 10:11:25 +00:00
|
|
|
int _brick_msleep(int msecs, bool shorten)
|
2011-11-08 10:17:41 +00:00
|
|
|
{
|
|
|
|
unsigned long timeout;
|
2021-07-05 11:12:19 +00:00
|
|
|
|
2017-05-22 09:23:39 +00:00
|
|
|
flush_signals(current);
|
2011-11-08 10:17:41 +00:00
|
|
|
if (msecs <= 0) {
|
2020-06-12 07:46:58 +00:00
|
|
|
brick_yield();
|
2011-11-08 10:17:41 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
timeout = msecs_to_jiffies(msecs) + 1;
|
2011-11-08 11:31:19 +00:00
|
|
|
|
|
|
|
timeout = schedule_timeout_interruptible(timeout);
|
|
|
|
|
2012-12-20 15:29:26 +00:00
|
|
|
if (!shorten) {
|
|
|
|
while ((long)timeout > 0) {
|
2011-11-08 11:31:19 +00:00
|
|
|
timeout = schedule_timeout_uninterruptible(timeout);
|
2012-12-20 15:29:26 +00:00
|
|
|
}
|
|
|
|
}
|
2011-11-08 11:31:19 +00:00
|
|
|
|
2011-11-08 10:17:41 +00:00
|
|
|
return jiffies_to_msecs(timeout);
|
|
|
|
}
|
2012-09-17 10:11:25 +00:00
|
|
|
EXPORT_SYMBOL_GPL(_brick_msleep);
|
2011-11-08 10:17:41 +00:00
|
|
|
|
2011-03-24 16:05:46 +00:00
|
|
|
//////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// number management
|
|
|
|
|
|
|
|
static char *nr_table = NULL;
|
2011-07-20 13:11:44 +00:00
|
|
|
int nr_max = 256;
|
|
|
|
EXPORT_SYMBOL_GPL(nr_max);
|
2011-03-24 16:05:46 +00:00
|
|
|
|
|
|
|
int get_nr(void)
|
|
|
|
{
|
|
|
|
char *new;
|
|
|
|
int nr;
|
|
|
|
|
|
|
|
if (unlikely(!nr_table)) {
|
2011-08-12 11:09:48 +00:00
|
|
|
nr_table = brick_zmem_alloc(nr_max);
|
2011-03-24 16:05:46 +00:00
|
|
|
if (!nr_table) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
for (nr = 1; nr < nr_max; nr++) {
|
|
|
|
if (!nr_table[nr]) {
|
|
|
|
nr_table[nr] = 1;
|
|
|
|
return nr;
|
|
|
|
}
|
|
|
|
}
|
2011-08-12 11:09:48 +00:00
|
|
|
new = brick_zmem_alloc(nr_max << 1);
|
2011-03-24 16:05:46 +00:00
|
|
|
if (!new)
|
|
|
|
return 0;
|
|
|
|
memcpy(new, nr_table, nr_max);
|
2011-08-12 11:09:48 +00:00
|
|
|
brick_mem_free(nr_table);
|
2011-03-24 16:05:46 +00:00
|
|
|
nr_table = new;
|
|
|
|
nr_max <<= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(get_nr);
|
|
|
|
|
|
|
|
void put_nr(int nr)
|
|
|
|
{
|
|
|
|
if (likely(nr_table && nr > 0 && nr < nr_max)) {
|
|
|
|
nr_table[nr] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(put_nr);
|
|
|
|
|
2010-07-23 11:55:18 +00:00
|
|
|
//////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// object stuff
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// brick stuff
|
|
|
|
|
|
|
|
static int nr_brick_types = 0;
|
|
|
|
static const struct generic_brick_type *brick_types[MAX_BRICK_TYPES] = {};
|
|
|
|
|
|
|
|
int generic_register_brick_type(const struct generic_brick_type *new_type)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int found = -1;
|
2021-07-05 11:12:19 +00:00
|
|
|
|
2010-07-30 05:46:22 +00:00
|
|
|
BRICK_DBG("generic_register_brick_type() name=%s\n", new_type->type_name);
|
2010-07-23 11:55:18 +00:00
|
|
|
for (i = 0; i < nr_brick_types; i++) {
|
|
|
|
if (!brick_types[i]) {
|
|
|
|
found = i;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!strcmp(brick_types[i]->type_name, new_type->type_name)) {
|
2011-02-23 20:48:06 +00:00
|
|
|
BRICK_DBG("bricktype %s is already registered.\n", new_type->type_name);
|
|
|
|
return 0;
|
2010-07-23 11:55:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (found < 0) {
|
|
|
|
if (nr_brick_types >= MAX_BRICK_TYPES) {
|
2010-07-30 05:46:22 +00:00
|
|
|
BRICK_ERR("sorry, cannot register bricktype %s.\n", new_type->type_name);
|
2022-07-27 08:01:21 +00:00
|
|
|
return -EINVAL;
|
2010-07-23 11:55:18 +00:00
|
|
|
}
|
|
|
|
found = nr_brick_types++;
|
|
|
|
}
|
|
|
|
brick_types[found] = new_type;
|
|
|
|
BRICK_DBG("generic_register_brick_type() done.\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(generic_register_brick_type);
|
|
|
|
|
|
|
|
int generic_unregister_brick_type(const struct generic_brick_type *old_type)
|
|
|
|
{
|
|
|
|
BRICK_DBG("generic_unregister_brick_type()\n");
|
|
|
|
return -1; // NYI
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(generic_unregister_brick_type);
|
|
|
|
|
|
|
|
int generic_brick_init_full(
|
|
|
|
void *data,
|
|
|
|
int size,
|
|
|
|
const struct generic_brick_type *brick_type,
|
|
|
|
const struct generic_input_type **input_types,
|
|
|
|
const struct generic_output_type **output_types,
|
2011-02-23 20:48:06 +00:00
|
|
|
const char **names)
|
2010-07-23 11:55:18 +00:00
|
|
|
{
|
|
|
|
struct generic_brick *brick = data;
|
|
|
|
int status;
|
|
|
|
int i;
|
|
|
|
|
2011-02-23 20:48:06 +00:00
|
|
|
BRICK_DBG("brick_type = %s\n", brick_type->type_name);
|
|
|
|
if (unlikely(!data)) {
|
|
|
|
BRICK_ERR("invalid memory\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// call the generic constructors
|
2010-07-23 11:55:18 +00:00
|
|
|
|
2011-02-23 20:48:06 +00:00
|
|
|
status = generic_brick_init(brick_type, brick, names ? *names++ : NULL);
|
2010-07-23 11:55:18 +00:00
|
|
|
if (status)
|
|
|
|
return status;
|
|
|
|
data += brick_type->brick_size;
|
|
|
|
size -= brick_type->brick_size;
|
2010-07-30 05:46:22 +00:00
|
|
|
if (size < 0) {
|
|
|
|
BRICK_ERR("Not enough MEMORY\n");
|
2010-07-23 11:55:18 +00:00
|
|
|
return -ENOMEM;
|
2010-07-30 05:46:22 +00:00
|
|
|
}
|
2010-07-23 11:55:18 +00:00
|
|
|
if (!input_types) {
|
|
|
|
BRICK_DBG("generic_brick_init_full: switch to default input_types\n");
|
|
|
|
input_types = brick_type->default_input_types;
|
|
|
|
names = brick_type->default_input_names;
|
2011-03-23 17:58:02 +00:00
|
|
|
if (unlikely(!input_types)) {
|
|
|
|
BRICK_ERR("no input types specified\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2010-07-23 11:55:18 +00:00
|
|
|
}
|
2011-03-23 17:58:02 +00:00
|
|
|
BRICK_DBG("generic_brick_init_full: input_types\n");
|
|
|
|
brick->inputs = data;
|
|
|
|
data += sizeof(void*) * brick_type->max_inputs;
|
|
|
|
size -= sizeof(void*) * brick_type->max_inputs;
|
|
|
|
if (size < 0) {
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
for (i = 0; i < brick_type->max_inputs; i++) {
|
|
|
|
struct generic_input *input = data;
|
|
|
|
const struct generic_input_type *type = *input_types++;
|
2021-07-05 11:12:19 +00:00
|
|
|
|
2011-03-23 17:58:02 +00:00
|
|
|
if (!type || type->input_size <= 0) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
BRICK_DBG("generic_brick_init_full: calling generic_input_init()\n");
|
|
|
|
status = generic_input_init(brick, i, type, input, (names && *names) ? *names++ : type->type_name);
|
|
|
|
if (status < 0)
|
|
|
|
return status;
|
|
|
|
data += type->input_size;
|
|
|
|
size -= type->input_size;
|
|
|
|
if (size < 0)
|
2010-07-30 05:46:22 +00:00
|
|
|
return -ENOMEM;
|
2010-07-23 11:55:18 +00:00
|
|
|
}
|
|
|
|
if (!output_types) {
|
|
|
|
BRICK_DBG("generic_brick_init_full: switch to default output_types\n");
|
|
|
|
output_types = brick_type->default_output_types;
|
|
|
|
names = brick_type->default_output_names;
|
2011-03-23 17:58:02 +00:00
|
|
|
if (unlikely(!output_types)) {
|
|
|
|
BRICK_ERR("no output types specified\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2010-07-23 11:55:18 +00:00
|
|
|
}
|
2011-03-23 17:58:02 +00:00
|
|
|
BRICK_DBG("generic_brick_init_full: output_types\n");
|
|
|
|
brick->outputs = data;
|
|
|
|
data += sizeof(void*) * brick_type->max_outputs;
|
|
|
|
size -= sizeof(void*) * brick_type->max_outputs;
|
|
|
|
if (size < 0)
|
|
|
|
return -ENOMEM;
|
|
|
|
for (i = 0; i < brick_type->max_outputs; i++) {
|
|
|
|
struct generic_output *output = data;
|
|
|
|
const struct generic_output_type *type = *output_types++;
|
2021-07-05 11:12:19 +00:00
|
|
|
|
2011-03-23 17:58:02 +00:00
|
|
|
if (!type || type->output_size <= 0) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
BRICK_DBG("generic_brick_init_full: calling generic_output_init()\n");
|
|
|
|
generic_output_init(brick, i, type, output, (names && *names) ? *names++ : type->type_name);
|
|
|
|
if (status < 0)
|
|
|
|
return status;
|
|
|
|
data += type->output_size;
|
|
|
|
size -= type->output_size;
|
2010-07-23 11:55:18 +00:00
|
|
|
if (size < 0)
|
2010-08-05 08:36:36 +00:00
|
|
|
return -ENOMEM;
|
2010-07-23 11:55:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// call the specific constructors
|
|
|
|
BRICK_DBG("generic_brick_init_full: call specific contructors.\n");
|
|
|
|
if (brick_type->brick_construct) {
|
|
|
|
BRICK_DBG("generic_brick_init_full: calling brick_construct()\n");
|
|
|
|
status = brick_type->brick_construct(brick);
|
2011-03-23 17:58:02 +00:00
|
|
|
if (status < 0)
|
2010-07-23 11:55:18 +00:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
for (i = 0; i < brick_type->max_inputs; i++) {
|
|
|
|
struct generic_input *input = brick->inputs[i];
|
2021-07-05 11:12:19 +00:00
|
|
|
|
2010-07-23 11:55:18 +00:00
|
|
|
if (!input)
|
|
|
|
continue;
|
|
|
|
if (!input->type) {
|
|
|
|
BRICK_ERR("input has no associated type!\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (input->type->input_construct) {
|
|
|
|
BRICK_DBG("generic_brick_init_full: calling input_construct()\n");
|
|
|
|
status = input->type->input_construct(input);
|
2011-03-23 17:58:02 +00:00
|
|
|
if (status < 0)
|
2010-07-23 11:55:18 +00:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (i = 0; i < brick_type->max_outputs; i++) {
|
|
|
|
struct generic_output *output = brick->outputs[i];
|
2021-07-05 11:12:19 +00:00
|
|
|
|
2010-07-23 11:55:18 +00:00
|
|
|
if (!output)
|
|
|
|
continue;
|
|
|
|
if (!output->type) {
|
|
|
|
BRICK_ERR("output has no associated type!\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (output->type->output_construct) {
|
|
|
|
BRICK_DBG("generic_brick_init_full: calling output_construct()\n");
|
|
|
|
status = output->type->output_construct(output);
|
2011-03-23 17:58:02 +00:00
|
|
|
if (status < 0)
|
2010-07-23 11:55:18 +00:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(generic_brick_init_full);
|
|
|
|
|
|
|
|
int generic_brick_exit_full(struct generic_brick *brick)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int status;
|
2021-07-05 11:12:19 +00:00
|
|
|
|
2010-07-23 11:55:18 +00:00
|
|
|
// first, check all outputs
|
2011-03-23 17:58:02 +00:00
|
|
|
for (i = 0; i < brick->type->max_outputs; i++) {
|
2010-07-23 11:55:18 +00:00
|
|
|
struct generic_output *output = brick->outputs[i];
|
|
|
|
if (!output)
|
|
|
|
continue;
|
|
|
|
if (!output->type) {
|
|
|
|
BRICK_ERR("output has no associated type!\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (output->nr_connected) {
|
2011-08-25 10:16:32 +00:00
|
|
|
BRICK_ERR("output is connected!\n");
|
2010-07-23 11:55:18 +00:00
|
|
|
return -EPERM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// ok, test succeeded. start destruction...
|
|
|
|
for (i = 0; i < brick->type->max_outputs; i++) {
|
|
|
|
struct generic_output *output = brick->outputs[i];
|
2021-07-05 11:12:19 +00:00
|
|
|
|
2010-07-23 11:55:18 +00:00
|
|
|
if (!output)
|
|
|
|
continue;
|
|
|
|
if (!output->type) {
|
|
|
|
BRICK_ERR("output has no associated type!\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (output->type->output_destruct) {
|
|
|
|
BRICK_DBG("generic_brick_exit_full: calling output_destruct()\n");
|
|
|
|
status = output->type->output_destruct(output);
|
2011-08-25 10:16:32 +00:00
|
|
|
if (status < 0)
|
2010-07-23 11:55:18 +00:00
|
|
|
return status;
|
2011-03-24 16:05:46 +00:00
|
|
|
_generic_output_exit(output);
|
2010-07-23 11:55:18 +00:00
|
|
|
brick->outputs[i] = NULL; // others may remain leftover
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (i = 0; i < brick->type->max_inputs; i++) {
|
|
|
|
struct generic_input *input = brick->inputs[i];
|
2021-07-05 11:12:19 +00:00
|
|
|
|
2010-07-23 11:55:18 +00:00
|
|
|
if (!input)
|
|
|
|
continue;
|
|
|
|
if (!input->type) {
|
|
|
|
BRICK_ERR("input has no associated type!\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (input->type->input_destruct) {
|
2011-08-25 10:16:32 +00:00
|
|
|
status = generic_disconnect(input);
|
|
|
|
if (status < 0)
|
|
|
|
return status;
|
2010-07-23 11:55:18 +00:00
|
|
|
BRICK_DBG("generic_brick_exit_full: calling input_destruct()\n");
|
|
|
|
status = input->type->input_destruct(input);
|
2011-08-25 10:16:32 +00:00
|
|
|
if (status < 0)
|
2010-07-23 11:55:18 +00:00
|
|
|
return status;
|
|
|
|
brick->inputs[i] = NULL; // others may remain leftover
|
2011-03-24 16:05:46 +00:00
|
|
|
generic_input_exit(input);
|
2010-07-23 11:55:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (brick->type->brick_destruct) {
|
|
|
|
BRICK_DBG("generic_brick_exit_full: calling brick_destruct()\n");
|
|
|
|
status = brick->type->brick_destruct(brick);
|
2011-08-25 10:16:32 +00:00
|
|
|
if (status < 0)
|
2010-07-23 11:55:18 +00:00
|
|
|
return status;
|
|
|
|
}
|
2011-03-24 16:05:46 +00:00
|
|
|
generic_brick_exit(brick);
|
2010-07-23 11:55:18 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(generic_brick_exit_full);
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// default implementations
|
|
|
|
|
2011-10-17 12:31:48 +00:00
|
|
|
struct generic_object *generic_alloc(struct generic_brick *brick, struct generic_object_layout *object_layout, const struct generic_object_type *object_type)
|
2011-09-02 12:17:40 +00:00
|
|
|
{
|
2011-10-17 12:31:48 +00:00
|
|
|
struct generic_object *object;
|
|
|
|
void *data;
|
|
|
|
int object_size;
|
|
|
|
int aspect_nr_max;
|
|
|
|
int total_size;
|
|
|
|
int hint_size;
|
|
|
|
|
|
|
|
CHECK_PTR_NULL(object_type, err);
|
|
|
|
CHECK_PTR(object_layout, err);
|
|
|
|
|
|
|
|
object_size = object_type->default_size;
|
|
|
|
aspect_nr_max = nr_max;
|
|
|
|
total_size = object_size + aspect_nr_max * sizeof(void*);
|
|
|
|
hint_size = object_layout->size_hint;
|
2012-11-06 10:29:34 +00:00
|
|
|
if (likely(total_size <= hint_size)) {
|
2011-10-17 12:31:48 +00:00
|
|
|
total_size = hint_size;
|
2012-11-06 10:29:34 +00:00
|
|
|
} else { // usually happens only at the first time
|
|
|
|
object_layout->size_hint = total_size;
|
|
|
|
}
|
2011-10-17 12:31:48 +00:00
|
|
|
|
|
|
|
data = brick_zmem_alloc(total_size);
|
|
|
|
if (!data)
|
|
|
|
goto err;
|
2011-09-02 12:17:40 +00:00
|
|
|
|
2011-10-17 12:31:48 +00:00
|
|
|
atomic_inc(&object_layout->alloc_count);
|
2012-02-02 15:25:43 +00:00
|
|
|
atomic_inc(&object_layout->total_alloc_count);
|
2011-09-02 12:17:40 +00:00
|
|
|
|
2011-10-17 12:31:48 +00:00
|
|
|
object = data;
|
|
|
|
object->object_type = object_type;
|
|
|
|
object->object_layout = object_layout;
|
|
|
|
object->aspects = data + object_size;
|
|
|
|
object->aspect_nr_max = aspect_nr_max;
|
|
|
|
object->free_offset = object_size + aspect_nr_max * sizeof(void*);
|
|
|
|
object->max_offset = total_size;
|
|
|
|
|
|
|
|
if (object_type->init_fn) {
|
|
|
|
int status = object_type->init_fn(object);
|
2021-07-05 11:12:19 +00:00
|
|
|
|
2011-10-17 12:31:48 +00:00
|
|
|
if (status < 0) {
|
|
|
|
goto err_free;
|
|
|
|
}
|
2011-10-05 13:04:43 +00:00
|
|
|
}
|
2011-09-02 12:17:40 +00:00
|
|
|
|
2011-10-17 12:31:48 +00:00
|
|
|
return object;
|
|
|
|
|
|
|
|
err_free:
|
|
|
|
brick_mem_free(data);
|
|
|
|
err:
|
|
|
|
return NULL;
|
2011-09-02 12:17:40 +00:00
|
|
|
}
|
2011-10-17 12:31:48 +00:00
|
|
|
EXPORT_SYMBOL_GPL(generic_alloc);
|
2011-09-02 12:17:40 +00:00
|
|
|
|
2011-10-17 12:31:48 +00:00
|
|
|
void generic_free(struct generic_object *object)
|
2010-08-01 20:21:18 +00:00
|
|
|
{
|
2011-10-17 12:31:48 +00:00
|
|
|
const struct generic_object_type *object_type;
|
|
|
|
struct generic_object_layout *object_layout;
|
2021-07-05 09:36:07 +00:00
|
|
|
struct generic_aspect **all_aspects;
|
2021-07-05 10:04:57 +00:00
|
|
|
int aspect_nr_max;
|
2011-10-17 12:31:48 +00:00
|
|
|
int i;
|
2010-08-05 08:36:36 +00:00
|
|
|
|
2011-10-17 12:31:48 +00:00
|
|
|
CHECK_PTR(object, done);
|
2021-07-05 09:36:07 +00:00
|
|
|
object_type = READ_ONCE(object->object_type);
|
|
|
|
WRITE_ONCE(object->object_type, NULL);
|
2011-10-17 12:31:48 +00:00
|
|
|
CHECK_PTR_NULL(object_type, done);
|
|
|
|
object_layout = object->object_layout;
|
|
|
|
CHECK_PTR(object_layout, done);
|
|
|
|
atomic_dec(&object_layout->alloc_count);
|
2021-07-05 09:36:07 +00:00
|
|
|
|
|
|
|
all_aspects = READ_ONCE(object->aspects);
|
|
|
|
WRITE_ONCE(object->aspects, NULL);
|
|
|
|
if (!all_aspects)
|
|
|
|
goto free;
|
|
|
|
|
2021-07-05 10:04:57 +00:00
|
|
|
aspect_nr_max = READ_ONCE(object->aspect_nr_max);
|
|
|
|
WRITE_ONCE(object->aspect_nr_max, 0);
|
|
|
|
|
|
|
|
for (i = 0; i < aspect_nr_max; i++) {
|
2011-10-17 12:31:48 +00:00
|
|
|
const struct generic_aspect_type *aspect_type;
|
2021-07-05 09:36:07 +00:00
|
|
|
struct generic_aspect *aspect = READ_ONCE(all_aspects[i]);
|
|
|
|
|
2011-10-17 12:31:48 +00:00
|
|
|
if (!aspect)
|
|
|
|
continue;
|
2021-07-05 09:36:07 +00:00
|
|
|
|
|
|
|
WRITE_ONCE(all_aspects[i], NULL);
|
2011-10-17 12:31:48 +00:00
|
|
|
aspect_type = aspect->aspect_type;
|
|
|
|
CHECK_PTR_NULL(aspect_type, done);
|
2021-07-05 11:18:05 +00:00
|
|
|
if (aspect_type->exit_fn)
|
2011-10-17 12:31:48 +00:00
|
|
|
aspect_type->exit_fn(aspect);
|
|
|
|
if (aspect->shortcut)
|
|
|
|
continue;
|
|
|
|
brick_mem_free(aspect);
|
2012-12-03 13:21:12 +00:00
|
|
|
atomic_dec(&object_layout->aspect_count);
|
2011-03-24 16:05:46 +00:00
|
|
|
}
|
2021-07-05 09:36:07 +00:00
|
|
|
free:
|
2021-07-05 11:18:05 +00:00
|
|
|
if (object_type->exit_fn)
|
2011-10-17 12:31:48 +00:00
|
|
|
object_type->exit_fn(object);
|
|
|
|
brick_mem_free(object);
|
|
|
|
done: ;
|
2010-08-01 20:21:18 +00:00
|
|
|
}
|
2011-10-17 12:31:48 +00:00
|
|
|
EXPORT_SYMBOL_GPL(generic_free);
|
2010-08-04 17:32:04 +00:00
|
|
|
|
2011-10-17 12:31:48 +00:00
|
|
|
static
|
2021-07-05 10:31:43 +00:00
|
|
|
struct generic_aspect *_new_aspect(struct generic_brick *brick,
|
|
|
|
struct generic_object *obj,
|
|
|
|
int nr)
|
2010-08-01 20:21:18 +00:00
|
|
|
{
|
2011-10-17 12:31:48 +00:00
|
|
|
struct generic_aspect *res = NULL;
|
2021-07-05 11:05:51 +00:00
|
|
|
struct generic_aspect *old_aspect;
|
2021-07-05 10:47:30 +00:00
|
|
|
struct generic_aspect **all_aspects;
|
2011-10-17 12:31:48 +00:00
|
|
|
const struct generic_brick_type *brick_type = brick->type;
|
|
|
|
const struct generic_object_type *object_type;
|
|
|
|
const struct generic_aspect_type *aspect_type;
|
|
|
|
int object_type_nr;
|
|
|
|
int size;
|
|
|
|
int rest;
|
|
|
|
|
|
|
|
object_type = obj->object_type;
|
|
|
|
CHECK_PTR_NULL(object_type, done);
|
|
|
|
object_type_nr = object_type->object_type_nr;
|
|
|
|
aspect_type = brick_type->aspect_types[object_type_nr];
|
|
|
|
CHECK_PTR_NULL(aspect_type, done);
|
|
|
|
|
2021-07-05 10:47:30 +00:00
|
|
|
all_aspects = READ_ONCE(obj->aspects);
|
|
|
|
if (unlikely(!all_aspects)) {
|
|
|
|
BRICK_WRN("object %p: no aspect %d possible\n",
|
|
|
|
obj, nr);
|
|
|
|
goto done;
|
|
|
|
}
|
2021-07-05 11:05:51 +00:00
|
|
|
old_aspect = READ_ONCE(all_aspects[nr]);
|
|
|
|
if (unlikely(old_aspect)) {
|
|
|
|
BRICK_WRN("object %p: aspect index %d already at %p\n",
|
|
|
|
obj, nr, old_aspect);
|
|
|
|
return old_aspect;
|
|
|
|
}
|
2021-07-05 10:47:30 +00:00
|
|
|
|
2011-10-17 12:31:48 +00:00
|
|
|
size = aspect_type->aspect_size;
|
|
|
|
rest = obj->max_offset - obj->free_offset;
|
|
|
|
if (likely(size <= rest)) {
|
2012-11-06 10:29:34 +00:00
|
|
|
/* Optimisation: re-use single memory allocation for both
|
|
|
|
* the object and the new aspect.
|
|
|
|
*/
|
2011-10-17 12:31:48 +00:00
|
|
|
res = ((void*)obj) + obj->free_offset;
|
|
|
|
obj->free_offset += size;
|
|
|
|
res->shortcut = true;
|
|
|
|
} else {
|
2012-12-03 13:21:12 +00:00
|
|
|
struct generic_object_layout *object_layout = obj->object_layout;
|
2021-07-05 11:12:19 +00:00
|
|
|
|
2012-12-03 13:21:12 +00:00
|
|
|
CHECK_PTR(object_layout, done);
|
2012-11-06 10:29:34 +00:00
|
|
|
/* Maintain the size hint.
|
|
|
|
* In future, only small aspects should be integrated into
|
|
|
|
* the same memory block, and the hint should not grow larger
|
|
|
|
* than PAGE_SIZE if it was smaller before.
|
2011-10-17 12:31:48 +00:00
|
|
|
*/
|
2012-11-06 10:29:34 +00:00
|
|
|
if (size < PAGE_SIZE / 2) {
|
|
|
|
int max;
|
2021-07-05 11:12:19 +00:00
|
|
|
|
2012-11-06 10:29:34 +00:00
|
|
|
max = obj->free_offset + size;
|
|
|
|
/* This is racy, but races won't do any harm because
|
|
|
|
* it is just a hint, not essential.
|
|
|
|
*/
|
|
|
|
if ((max < PAGE_SIZE || object_layout->size_hint > PAGE_SIZE) &&
|
|
|
|
object_layout->size_hint < max)
|
|
|
|
object_layout->size_hint = max;
|
|
|
|
}
|
2011-03-24 16:05:46 +00:00
|
|
|
|
2011-10-17 12:31:48 +00:00
|
|
|
res = brick_zmem_alloc(size);
|
|
|
|
if (unlikely(!res)) {
|
|
|
|
goto done;
|
2010-08-08 09:03:42 +00:00
|
|
|
}
|
2012-12-03 13:21:12 +00:00
|
|
|
atomic_inc(&object_layout->aspect_count);
|
|
|
|
atomic_inc(&object_layout->total_aspect_count);
|
2010-08-08 09:03:42 +00:00
|
|
|
}
|
2011-10-17 12:31:48 +00:00
|
|
|
res->object = obj;
|
|
|
|
res->aspect_type = aspect_type;
|
2010-08-01 20:21:18 +00:00
|
|
|
|
2011-10-17 12:31:48 +00:00
|
|
|
if (aspect_type->init_fn) {
|
|
|
|
int status = aspect_type->init_fn(res);
|
2021-07-05 11:12:19 +00:00
|
|
|
|
2011-10-17 12:31:48 +00:00
|
|
|
if (unlikely(status < 0)) {
|
|
|
|
BRICK_ERR("aspect init %p %p %p status = %d\n", brick, obj, res, status);
|
|
|
|
goto done;
|
2010-08-05 15:54:48 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-05 10:47:30 +00:00
|
|
|
WRITE_ONCE(all_aspects[nr], res);
|
2010-08-01 20:21:18 +00:00
|
|
|
|
2011-10-17 12:31:48 +00:00
|
|
|
done:
|
|
|
|
return res;
|
2010-08-01 20:21:18 +00:00
|
|
|
}
|
2010-08-02 16:31:10 +00:00
|
|
|
|
2011-10-17 12:31:48 +00:00
|
|
|
struct generic_aspect *generic_get_aspect(struct generic_brick *brick, struct generic_object *obj)
|
2010-08-02 16:31:10 +00:00
|
|
|
{
|
2011-10-17 12:31:48 +00:00
|
|
|
struct generic_aspect *res = NULL;
|
2021-07-05 10:31:43 +00:00
|
|
|
struct generic_aspect **all_aspects;
|
2011-10-17 12:31:48 +00:00
|
|
|
int nr;
|
2010-08-08 09:03:42 +00:00
|
|
|
|
2011-10-17 12:31:48 +00:00
|
|
|
CHECK_PTR(brick, done);
|
|
|
|
CHECK_PTR(obj, done);
|
2010-08-08 09:03:42 +00:00
|
|
|
|
2011-10-17 12:31:48 +00:00
|
|
|
nr = brick->brick_index;
|
|
|
|
if (unlikely(nr <= 0 || nr >= obj->aspect_nr_max)) {
|
|
|
|
BRICK_ERR("bad nr = %d\n", nr);
|
|
|
|
goto done;
|
|
|
|
}
|
2021-07-05 10:31:43 +00:00
|
|
|
all_aspects = READ_ONCE(obj->aspects);
|
|
|
|
if (unlikely(!all_aspects)) {
|
|
|
|
BRICK_ERR("get_aspect on deleting object %p\n",
|
|
|
|
obj);
|
|
|
|
goto done;
|
2010-08-08 09:03:42 +00:00
|
|
|
}
|
2021-07-05 10:31:43 +00:00
|
|
|
res = all_aspects[nr];
|
|
|
|
if (!res)
|
|
|
|
res = _new_aspect(brick, obj, nr);
|
2011-10-17 12:31:48 +00:00
|
|
|
CHECK_PTR(res, done);
|
|
|
|
CHECK_PTR(res->object, done);
|
|
|
|
_CHECK(res->object == obj, done);
|
2010-08-05 15:54:48 +00:00
|
|
|
|
2011-10-17 12:31:48 +00:00
|
|
|
done:
|
|
|
|
return res;
|
2010-08-02 16:31:10 +00:00
|
|
|
}
|
2011-10-17 12:31:48 +00:00
|
|
|
EXPORT_SYMBOL_GPL(generic_get_aspect);
|
2010-08-06 11:29:06 +00:00
|
|
|
|
2011-02-23 20:48:06 +00:00
|
|
|
/////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// helper stuff
|
|
|
|
|
2011-02-27 14:17:58 +00:00
|
|
|
void set_button(struct generic_switch *sw, bool val, bool force)
|
2011-02-23 20:48:06 +00:00
|
|
|
{
|
|
|
|
bool oldval = sw->button;
|
2021-07-05 11:12:19 +00:00
|
|
|
|
2011-02-27 14:17:58 +00:00
|
|
|
if ((sw->force_off |= force))
|
|
|
|
val = false;
|
2011-02-23 20:48:06 +00:00
|
|
|
if (val != oldval) {
|
|
|
|
sw->button = val;
|
2011-02-27 14:17:58 +00:00
|
|
|
//sw->trigger = true;
|
2011-02-23 20:48:06 +00:00
|
|
|
wake_up_interruptible(&sw->event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(set_button);
|
|
|
|
|
|
|
|
void set_led_on(struct generic_switch *sw, bool val)
|
|
|
|
{
|
|
|
|
bool oldval = sw->led_on;
|
2021-07-05 11:12:19 +00:00
|
|
|
|
2011-02-23 20:48:06 +00:00
|
|
|
if (val != oldval) {
|
|
|
|
sw->led_on = val;
|
2011-02-27 14:17:58 +00:00
|
|
|
//sw->trigger = true;
|
2011-02-23 20:48:06 +00:00
|
|
|
wake_up_interruptible(&sw->event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(set_led_on);
|
|
|
|
|
|
|
|
void set_led_off(struct generic_switch *sw, bool val)
|
|
|
|
{
|
|
|
|
bool oldval = sw->led_off;
|
2021-07-05 11:12:19 +00:00
|
|
|
|
2011-02-23 20:48:06 +00:00
|
|
|
if (val != oldval) {
|
|
|
|
sw->led_off = val;
|
2011-02-27 14:17:58 +00:00
|
|
|
//sw->trigger = true;
|
2011-02-23 20:48:06 +00:00
|
|
|
wake_up_interruptible(&sw->event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(set_led_off);
|
|
|
|
|
2011-03-01 09:34:36 +00:00
|
|
|
void set_button_wait(struct generic_brick *brick, bool val, bool force, int timeout)
|
2011-02-27 14:17:58 +00:00
|
|
|
{
|
2011-03-01 09:34:36 +00:00
|
|
|
set_button(&brick->power, val, force);
|
|
|
|
if (brick->ops)
|
|
|
|
(void)brick->ops->brick_switch(brick);
|
2011-02-27 14:17:58 +00:00
|
|
|
if (val) {
|
2011-03-01 09:34:36 +00:00
|
|
|
wait_event_interruptible_timeout(brick->power.event, brick->power.led_on, timeout);
|
2011-02-27 14:17:58 +00:00
|
|
|
} else {
|
2011-03-01 09:34:36 +00:00
|
|
|
wait_event_interruptible_timeout(brick->power.event, brick->power.led_off, timeout);
|
2011-02-27 14:17:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(set_button_wait);
|
|
|
|
|
2011-02-23 20:48:06 +00:00
|
|
|
/////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// meta stuff
|
|
|
|
|
|
|
|
const struct meta *find_meta(const struct meta *meta, const char *field_name)
|
|
|
|
{
|
|
|
|
const struct meta *tmp;
|
2021-07-05 11:12:19 +00:00
|
|
|
|
2011-07-20 13:11:44 +00:00
|
|
|
for (tmp = meta; tmp->field_name; tmp++) {
|
|
|
|
if (!strcmp(field_name, tmp->field_name)) {
|
2011-02-23 20:48:06 +00:00
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(find_meta);
|
|
|
|
|
2011-07-20 13:11:44 +00:00
|
|
|
#if 0 // currently not needed, but this may change
|
2011-02-23 20:48:06 +00:00
|
|
|
void free_meta(void *data, const struct meta *meta)
|
|
|
|
{
|
|
|
|
for (; meta->field_name[0]; meta++) {
|
|
|
|
void *item;
|
|
|
|
switch (meta->field_type) {
|
|
|
|
case FIELD_SUB:
|
|
|
|
if (meta->field_ref) {
|
|
|
|
item = data + meta->field_offset;
|
|
|
|
free_meta(item, meta->field_ref);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case FIELD_REF:
|
|
|
|
case FIELD_STRING:
|
|
|
|
item = data + meta->field_offset;
|
|
|
|
item = *(void**)item;
|
|
|
|
if (meta->field_ref)
|
|
|
|
free_meta(item, meta->field_ref);
|
2011-08-12 11:09:48 +00:00
|
|
|
brick_mem_free(item);
|
2011-02-23 20:48:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(free_meta);
|
2011-07-20 13:11:44 +00:00
|
|
|
#endif
|
2011-02-23 20:48:06 +00:00
|
|
|
|
2011-03-24 16:05:46 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// module init stuff
|
|
|
|
|
2011-08-25 10:16:32 +00:00
|
|
|
int __init init_brick(void)
|
2011-03-24 16:05:46 +00:00
|
|
|
{
|
2011-08-12 11:09:48 +00:00
|
|
|
nr_table = brick_zmem_alloc(nr_max);
|
2011-03-24 16:05:46 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-04-23 11:16:26 +00:00
|
|
|
void exit_brick(void)
|
2011-03-24 16:05:46 +00:00
|
|
|
{
|
2020-07-08 18:55:34 +00:00
|
|
|
brick_mem_free(nr_table);
|
2011-03-24 16:05:46 +00:00
|
|
|
}
|