mars/brick.h

809 lines
27 KiB
C
Raw Normal View History

2010-07-23 11:55:18 +00:00
// (c) 2010 Thomas Schoebel-Theuer / 1&1 Internet AG
#ifndef BRICK_H
#define BRICK_H
2011-02-23 20:48:06 +00:00
#include <linux/list.h>
2010-07-30 05:46:22 +00:00
#include <linux/spinlock.h>
#include <linux/sched.h>
2011-02-23 20:48:06 +00:00
#include <linux/wait.h>
2010-07-30 05:46:22 +00:00
2010-07-23 11:55:18 +00:00
#ifdef _STRATEGY
#define _STRATEGY_CODE(X) X
#define _NORMAL_CODE(X) /**/
#else
#define _STRATEGY_CODE(X) /**/
#define _NORMAL_CODE(X) X
#endif
2011-02-23 20:48:06 +00:00
#define BRICK_ERROR "BRICK_ERROR "
#define BRICK_INFO "BRICK_INFO "
#define BRICK_DEBUG "BRICK_DEBUG "
//#define _BRICK_FMT(fmt) "[%s] " __BASE_FILE__ " %d %s(): " fmt, current->comm, __LINE__, __FUNCTION__
#define _BRICK_FMT(fmt) __BASE_FILE__ " %d %s(): " fmt, __LINE__, __FUNCTION__
2010-07-23 11:55:18 +00:00
2011-02-23 20:48:06 +00:00
#define BRICK_ERR(fmt, args...) printk(BRICK_ERROR _BRICK_FMT(fmt), ##args)
#define BRICK_INF(fmt, args...) printk(BRICK_INFO _BRICK_FMT(fmt), ##args)
2010-07-23 11:55:18 +00:00
#ifdef BRICK_DEBUGGING
2011-02-23 20:48:06 +00:00
#define BRICK_DBG(fmt, args...) printk(BRICK_DEBUG _BRICK_FMT(fmt), ##args)
2010-07-23 11:55:18 +00:00
#else
#define BRICK_DBG(args...) /**/
#endif
#define MAX_BRICK_TYPES 64
2011-03-20 17:38:08 +00:00
extern int brick_layout_generation;
2010-07-23 11:55:18 +00:00
/////////////////////////////////////////////////////////////////////////
// definitions for generic objects with aspects
#define MAX_DEFAULT_ASPECTS 8
struct generic_aspect;
2010-08-10 17:39:30 +00:00
#define GENERIC_ASPECT_TYPE(TYPE) \
2010-07-23 11:55:18 +00:00
char *aspect_type_name; \
2010-07-30 05:46:22 +00:00
const struct generic_object_type *object_type; \
2010-08-08 09:03:42 +00:00
int aspect_size; \
int (*init_fn)(struct generic_aspect *ini, void *data); \
void (*exit_fn)(struct generic_aspect *ini, void *data); \
2010-07-23 11:55:18 +00:00
struct generic_aspect_type {
GENERIC_ASPECT_TYPE(generic);
};
2010-08-10 17:39:30 +00:00
#define GENERIC_ASPECT_LAYOUT(TYPE) \
2010-07-23 11:55:18 +00:00
const struct generic_aspect_type *aspect_type; \
void *init_data; \
2010-07-30 05:46:22 +00:00
int aspect_offset; \
2010-07-23 11:55:18 +00:00
struct generic_aspect_layout {
GENERIC_ASPECT_LAYOUT(generic);
};
2010-08-10 17:39:30 +00:00
#define GENERIC_OBJECT_TYPE(TYPE) \
2010-07-23 11:55:18 +00:00
char *object_type_name; \
int default_size; \
int brick_obj_nr; \
2010-08-08 09:03:42 +00:00
int (*init_fn)(struct generic_aspect *ini, void *data); \
void (*exit_fn)(struct generic_aspect *ini, void *data); \
2010-07-23 11:55:18 +00:00
struct generic_object_type {
GENERIC_OBJECT_TYPE(generic);
};
2010-08-10 17:39:30 +00:00
#define GENERIC_OBJECT_LAYOUT(TYPE) \
2010-08-01 20:21:18 +00:00
struct generic_aspect_layout **aspect_layouts; \
2010-07-23 11:55:18 +00:00
const struct generic_object_type *object_type; \
2010-08-08 09:03:42 +00:00
void *init_data; \
2010-07-30 05:46:22 +00:00
int aspect_count; \
2010-08-01 20:21:18 +00:00
int aspect_max; \
int object_size; \
2011-03-20 17:38:08 +00:00
int layout_generation; \
2010-08-05 15:54:48 +00:00
int last_count; \
2010-11-26 13:45:10 +00:00
int last_jiffies; \
2010-08-05 15:54:48 +00:00
atomic_t alloc_count; \
2010-08-08 09:03:42 +00:00
atomic_t free_count; \
spinlock_t free_lock; \
struct generic_object *free_list; \
2010-08-05 15:54:48 +00:00
char *module_name; \
2010-07-23 11:55:18 +00:00
struct generic_object_layout {
GENERIC_OBJECT_LAYOUT(generic);
};
2010-08-10 17:39:30 +00:00
#define GENERIC_OBJECT(TYPE) \
struct TYPE##_object_layout *object_layout; \
2010-07-23 11:55:18 +00:00
int object_size; \
struct generic_object {
GENERIC_OBJECT(generic);
};
2010-08-10 17:39:30 +00:00
#define GENERIC_ASPECT(TYPE) \
struct TYPE##_object *object; \
2010-07-23 11:55:18 +00:00
struct generic_aspect {
GENERIC_ASPECT(generic);
};
2010-08-10 17:39:30 +00:00
#define GENERIC_CALLBACK(TYPE) \
void (*cb_fn)(struct TYPE##_callback *cb); \
2010-08-08 14:02:54 +00:00
void *cb_private; \
int cb_error; \
struct generic_callback *cb_prev; \
struct generic_callback {
GENERIC_CALLBACK(generic);
};
2010-07-23 11:55:18 +00:00
/////////////////////////////////////////////////////////////////////////
// definitions for generic bricks
struct generic_input;
struct generic_output;
struct generic_brick_ops;
struct generic_output_ops;
struct generic_brick_type;
2011-02-23 20:48:06 +00:00
struct generic_switch {
bool button;
bool led_on;
bool led_off;
2011-02-27 14:17:58 +00:00
bool force_off;
int percent_done;
2011-02-23 20:48:06 +00:00
wait_queue_head_t event;
};
2010-08-01 20:21:18 +00:00
#define GENERIC_BRICK(BRICK) \
2011-02-23 20:48:06 +00:00
const char *brick_name; \
2010-08-01 20:21:18 +00:00
const struct BRICK##_brick_type *type; \
struct BRICK##_brick_ops *ops; \
2010-07-23 11:55:18 +00:00
int nr_inputs; \
int nr_outputs; \
2010-08-01 20:21:18 +00:00
struct BRICK##_input **inputs; \
struct BRICK##_output **outputs; \
2011-02-23 20:48:06 +00:00
struct generic_switch power; \
2011-02-28 18:00:32 +00:00
int (*free)(struct BRICK##_brick *del); \
2010-07-23 11:55:18 +00:00
struct list_head tmp_head; \
struct generic_brick {
GENERIC_BRICK(generic);
};
2010-08-01 20:21:18 +00:00
#define GENERIC_INPUT(BRICK) \
2011-02-23 20:48:06 +00:00
const char *input_name; \
2010-08-01 20:21:18 +00:00
struct BRICK##_brick *brick; \
const struct BRICK##_input_type *type; \
2010-08-04 17:32:04 +00:00
struct BRICK##_output *connect; \
2010-07-30 05:46:22 +00:00
struct list_head input_head; \
2010-07-23 11:55:18 +00:00
struct generic_input {
GENERIC_INPUT(generic);
};
2010-08-01 20:21:18 +00:00
#define GENERIC_OUTPUT(BRICK) \
2011-02-23 20:48:06 +00:00
const char *output_name; \
2010-08-01 20:21:18 +00:00
struct BRICK##_brick *brick; \
2010-08-04 17:32:04 +00:00
const struct BRICK##_output_type *type; \
struct BRICK##_output_ops *ops; \
2010-07-30 05:46:22 +00:00
struct list_head output_head; \
2010-07-23 11:55:18 +00:00
int nr_connected; \
/* _must_ be the last member */ \
struct generic_aspect_layout aspect_layouts[BRICK_OBJ_NR]; \
struct generic_output {
GENERIC_OUTPUT(generic);
};
2010-07-30 05:46:22 +00:00
#define GENERIC_OUTPUT_CALL(OUTPUT,OP,ARGS...) \
( \
2010-08-01 20:21:18 +00:00
(void)LOCK_CHECK(OP), \
2010-07-30 05:46:22 +00:00
(OUTPUT) && (OUTPUT)->ops->OP ? \
(OUTPUT)->ops->OP(OUTPUT, ##ARGS) : \
-ENOSYS \
)
#define GENERIC_INPUT_CALL(INPUT,OP,ARGS...) \
( \
2010-08-01 20:21:18 +00:00
(void)LOCK_CHECK(OP), \
2010-07-30 05:46:22 +00:00
(INPUT) && (INPUT)->connect ? \
GENERIC_OUTPUT_CALL((INPUT)->connect, OP, ##ARGS) : \
2010-08-05 15:54:48 +00:00
-ECONNREFUSED \
2010-07-30 05:46:22 +00:00
)
2010-07-23 11:55:18 +00:00
2010-08-01 20:21:18 +00:00
#define GENERIC_BRICK_OPS(BRICK) \
2011-02-23 20:48:06 +00:00
int (*brick_switch)(struct BRICK##_brick *brick); \
2010-07-23 11:55:18 +00:00
struct generic_brick_ops {
GENERIC_BRICK_OPS(generic);
};
2010-08-01 20:21:18 +00:00
#define GENERIC_OUTPUT_OPS(BRICK) \
/*int (*output_start)(struct BRICK##_output *output);*/ \
/*int (*output_stop)(struct BRICK##_output *output);*/ \
int (*make_object_layout)(struct BRICK##_output *output, struct generic_object_layout *object_layout); \
2010-07-23 11:55:18 +00:00
struct generic_output_ops {
GENERIC_OUTPUT_OPS(generic)
};
// although possible, *_type should never be extended
2010-08-01 20:21:18 +00:00
#define GENERIC_BRICK_TYPE(BRICK) \
2011-02-23 20:48:06 +00:00
const char *type_name; \
2010-07-23 11:55:18 +00:00
int brick_size; \
int max_inputs; \
int max_outputs; \
2010-08-01 20:21:18 +00:00
const struct BRICK##_input_type **default_input_types; \
2011-02-23 20:48:06 +00:00
const char **default_input_names; \
2010-08-01 20:21:18 +00:00
const struct BRICK##_output_type **default_output_types; \
2011-02-23 20:48:06 +00:00
const char **default_output_names; \
2010-08-01 20:21:18 +00:00
struct BRICK##_brick_ops *master_ops; \
2010-08-04 22:38:48 +00:00
const struct BRICK##_input_types **default_type; \
2010-08-01 20:21:18 +00:00
int (*brick_construct)(struct BRICK##_brick *brick); \
int (*brick_destruct)(struct BRICK##_brick *brick); \
2010-07-23 11:55:18 +00:00
struct generic_brick_type {
GENERIC_BRICK_TYPE(generic);
};
2010-08-01 20:21:18 +00:00
#define GENERIC_INPUT_TYPE(BRICK) \
2010-07-23 11:55:18 +00:00
char *type_name; \
int input_size; \
2010-08-01 20:21:18 +00:00
int (*input_construct)(struct BRICK##_input *input); \
int (*input_destruct)(struct BRICK##_input *input); \
2010-07-23 11:55:18 +00:00
struct generic_input_type {
GENERIC_INPUT_TYPE(generic);
};
2010-08-01 20:21:18 +00:00
#define GENERIC_OUTPUT_TYPE(BRICK) \
2010-07-23 11:55:18 +00:00
char *type_name; \
int output_size; \
2010-08-01 20:21:18 +00:00
struct BRICK##_output_ops *master_ops; \
2010-08-04 22:38:48 +00:00
int (*output_construct)(struct BRICK##_output *output); \
2010-08-01 20:21:18 +00:00
int (*output_destruct)(struct BRICK##_output *output); \
2010-07-30 05:46:22 +00:00
const struct generic_aspect_type **aspect_types; \
const int layout_code[BRICK_OBJ_NR]; \
2010-07-23 11:55:18 +00:00
struct generic_output_type {
GENERIC_OUTPUT_TYPE(generic);
};
2010-07-30 05:46:22 +00:00
#define LAYOUT_NONE 0
#define LAYOUT_ALL -1
#define LAYOUT_1(X1) ((X1) | 255 << 8)
#define LAYOUT_2(X1,X2) ((X1) | (X2) << 8 | 255 << 16)
#define LAYOUT_3(X1,X2,X3) ((X1) | (X2) << 8 | (X3) << 16 | 255 << 24)
2010-07-23 11:55:18 +00:00
int generic_register_brick_type(const struct generic_brick_type *new_type);
int generic_unregister_brick_type(const struct generic_brick_type *old_type);
2011-02-23 20:48:06 +00:00
inline void _generic_output_init(struct generic_brick *brick, const struct generic_output_type *type, struct generic_output *output, const char *output_name)
2010-08-10 17:39:30 +00:00
{
output->output_name = output_name;
output->brick = brick;
output->type = type;
output->ops = type->master_ops;
output->nr_connected = 0;
2011-02-27 14:17:58 +00:00
INIT_LIST_HEAD(&output->output_head);
2010-08-10 17:39:30 +00:00
}
2010-07-23 11:55:18 +00:00
#ifdef _STRATEGY // call this only in strategy bricks, never in ordinary bricks
// you need this only if you circumvent generic_brick_init_full()
2011-02-23 20:48:06 +00:00
inline int generic_brick_init(const struct generic_brick_type *type, struct generic_brick *brick, const char *brick_name)
2010-07-23 11:55:18 +00:00
{
brick->brick_name = brick_name;
brick->type = type;
brick->ops = type->master_ops;
brick->nr_inputs = 0;
brick->nr_outputs = 0;
2011-02-23 20:48:06 +00:00
brick->power.led_off = true;
//brick->power.event = __WAIT_QUEUE_HEAD_INITIALIZER(brick->power.event);
init_waitqueue_head(&brick->power.event);
//INIT_LIST_HEAD(&brick->tmp_head);
2010-07-23 11:55:18 +00:00
brick->tmp_head.next = brick->tmp_head.prev = &brick->tmp_head;
return 0;
}
2011-02-23 20:48:06 +00:00
inline int generic_input_init(struct generic_brick *brick, int index, const struct generic_input_type *type, struct generic_input *input, const char *input_name)
2010-07-23 11:55:18 +00:00
{
if (index < 0 || index >= brick->type->max_inputs)
2011-03-01 18:00:14 +00:00
return -EINVAL;
2010-07-23 11:55:18 +00:00
if (brick->inputs[index])
return -EEXIST;
input->input_name = input_name;
input->brick = brick;
input->type = type;
input->connect = NULL;
2011-02-27 14:17:58 +00:00
INIT_LIST_HEAD(&input->input_head);
2010-07-23 11:55:18 +00:00
brick->inputs[index] = input;
brick->nr_inputs++;
return 0;
}
2011-02-23 20:48:06 +00:00
inline int generic_output_init(struct generic_brick *brick, int index, const struct generic_output_type *type, struct generic_output *output, const char *output_name)
2010-07-23 11:55:18 +00:00
{
if (index < 0 || index >= brick->type->max_outputs)
return -ENOMEM;
if (brick->outputs[index])
return -EEXIST;
2010-08-10 17:39:30 +00:00
_generic_output_init(brick, type, output, output_name);
2010-07-23 11:55:18 +00:00
brick->outputs[index] = output;
brick->nr_outputs++;
return 0;
}
2011-02-23 20:48:06 +00:00
inline int generic_size(const struct generic_brick_type *brick_type)
2010-07-23 11:55:18 +00:00
{
int size = brick_type->brick_size;
int i;
size += brick_type->max_inputs * sizeof(void*);
for (i = 0; i < brick_type->max_inputs; i++) {
size += brick_type->default_input_types[i]->input_size;
}
size += brick_type->max_outputs * sizeof(void*);
for (i = 0; i < brick_type->max_outputs; i++) {
size += brick_type->default_output_types[i]->output_size;
}
return size;
}
/* If possible, use this instead of generic_*_init().
* input_types and output_types may be NULL => use default_*_types
*/
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
int generic_brick_exit_full(
struct generic_brick *brick);
2011-02-23 20:48:06 +00:00
inline int generic_connect(struct generic_input *input, struct generic_output *output)
2010-07-23 11:55:18 +00:00
{
BRICK_DBG("generic_connect(input=%p, output=%p)\n", input, output);
2011-03-01 18:00:14 +00:00
if (unlikely(!input || !output))
2010-07-23 11:55:18 +00:00
return -EINVAL;
2011-03-01 18:00:14 +00:00
if (unlikely(input->connect))
2010-07-23 11:55:18 +00:00
return -EEXIST;
2011-03-01 18:00:14 +00:00
// helps only against the most common errors
if (unlikely(input->brick == output->brick))
return -EDEADLK;
2010-07-23 11:55:18 +00:00
input->connect = output;
2011-03-20 17:38:08 +00:00
output->nr_connected++;
/* no atomic_t necessary (_any_ change is sufficient)
*/
brick_layout_generation++;
2011-02-27 14:17:58 +00:00
list_add(&input->input_head, &output->output_head);
2010-07-23 11:55:18 +00:00
BRICK_DBG("now nr_connected=%d\n", output->nr_connected);
return 0;
}
2011-02-23 20:48:06 +00:00
inline int generic_disconnect(struct generic_input *input)
2010-07-23 11:55:18 +00:00
{
BRICK_DBG("generic_disconnect(input=%p)\n", input);
if (!input)
return -EINVAL;
if (input->connect) {
BRICK_DBG("now nr_connected=%d\n", input->connect->nr_connected);
input->connect = NULL;
2011-02-27 14:17:58 +00:00
list_del_init(&input->input_head);
2011-03-20 17:38:08 +00:00
input->connect->nr_connected--;
//brick_layout_generation++;
2010-07-23 11:55:18 +00:00
}
return 0;
}
#endif // _STRATEGY
// simple wrappers for type safety
2010-08-01 20:21:18 +00:00
#define GENERIC_MAKE_FUNCTIONS(BRICK) \
extern inline int BRICK##_register_brick_type(void) \
2010-07-23 11:55:18 +00:00
{ \
2010-08-01 20:21:18 +00:00
extern const struct BRICK##_brick_type BRICK##_brick_type; \
extern int BRICK##_brick_nr; \
if (BRICK##_brick_nr >= 0) { \
BRICK_ERR("brick type " #BRICK " is already registered.\n"); \
2010-07-23 11:55:18 +00:00
return -EEXIST; \
} \
2010-08-01 20:21:18 +00:00
BRICK##_brick_nr = generic_register_brick_type((const struct generic_brick_type*)&BRICK##_brick_type); \
return BRICK##_brick_nr < 0 ? BRICK##_brick_nr : 0; \
2010-07-23 11:55:18 +00:00
} \
\
2010-08-01 20:21:18 +00:00
extern inline int BRICK##_unregister_brick_type(void) \
2010-07-23 11:55:18 +00:00
{ \
2010-08-01 20:21:18 +00:00
extern const struct BRICK##_brick_type BRICK##_brick_type; \
return generic_unregister_brick_type((const struct generic_brick_type*)&BRICK##_brick_type); \
2010-07-23 11:55:18 +00:00
} \
\
2010-08-01 20:21:18 +00:00
extern int BRICK##_make_object_layout(struct BRICK##_output *output, struct generic_object_layout *object_layout) \
2010-07-30 05:46:22 +00:00
{ \
return default_make_object_layout((struct generic_output*)output, object_layout); \
} \
\
2010-08-01 20:21:18 +00:00
extern const struct BRICK##_brick_type BRICK##_brick_type; \
2010-08-10 17:39:30 +00:00
extern const struct BRICK##_input_type BRICK##_input_type; \
2010-08-01 20:21:18 +00:00
extern const struct BRICK##_output_type BRICK##_output_type; \
2010-07-23 11:55:18 +00:00
\
2010-08-10 17:39:30 +00:00
static inline void _##BRICK##_output_init(struct BRICK##_brick *brick, struct BRICK##_output *output, char *output_name) \
{ \
_generic_output_init( \
(struct generic_brick*)brick, \
(const struct generic_output_type*)&BRICK##_output_type, \
(struct generic_output*)output, \
output_name); \
} \
\
_STRATEGY_CODE( \
2010-08-01 20:21:18 +00:00
static inline int BRICK##_brick_init(struct BRICK##_brick *brick, char *brick_name) \
2010-07-23 11:55:18 +00:00
{ \
2010-08-01 20:21:18 +00:00
return generic_brick_init((const struct generic_brick_type*)&BRICK##_brick_type, (struct generic_brick*)brick, brick_name); \
2010-07-23 11:55:18 +00:00
} \
\
2010-08-01 20:21:18 +00:00
static inline int BRICK##_input_init(struct BRICK##_brick *brick, int index, struct BRICK##_input *input, char *input_name) \
2010-07-23 11:55:18 +00:00
{ \
return generic_input_init( \
(struct generic_brick*)brick, \
index, \
2010-08-01 20:21:18 +00:00
(struct generic_input_type*)&BRICK##_input_type, \
2010-07-23 11:55:18 +00:00
(struct generic_input*)input, \
input_name); \
} \
\
2010-08-10 17:39:30 +00:00
static inline int BRICK##_output_init(struct BRICK##_brick *brick, int index, struct BRICK##_output *output, char *output_name) \
2010-07-23 11:55:18 +00:00
{ \
return generic_output_init( \
(struct generic_brick*)brick, \
index, \
2010-08-01 20:21:18 +00:00
(const struct generic_output_type*)&BRICK##_output_type, \
2010-07-23 11:55:18 +00:00
(struct generic_output*)output, \
output_name); \
} \
\
)
/* Define a pair of connectable subtypes.
* For type safety, use this for all possible combinations.
* Yes, this may become quadratic in large type systems, but
* (a) thou shalt not define many types,
* (b) these macros generate only definitions, but no additional
* code at runtime.
*/
2010-08-10 17:39:30 +00:00
#define GENERIC_MAKE_CONNECT(INPUT_BRICK,OUTPUT_BRICK) \
2010-07-23 11:55:18 +00:00
\
_STRATEGY_CODE( \
\
2011-02-23 20:48:06 +00:00
inline int INPUT_BRICK##_##OUTPUT_BRICK##_connect( \
2010-08-01 20:21:18 +00:00
struct INPUT_BRICK##_input *input, \
struct OUTPUT_BRICK##_output *output) \
2010-07-23 11:55:18 +00:00
{ \
return generic_connect((struct generic_input*)input, (struct generic_output*)output); \
} \
\
2011-02-23 20:48:06 +00:00
inline int INPUT_BRICK##_##OUTPUT_BRICK####_disconnect( \
2010-08-01 20:21:18 +00:00
struct INPUT_BRICK##_input *input) \
2010-07-23 11:55:18 +00:00
{ \
return generic_disconnect((struct generic_input*)input); \
} \
)
2010-07-30 05:46:22 +00:00
///////////////////////////////////////////////////////////////////////
// default operations on objects / aspects
extern int default_make_object_layout(struct generic_output *output, struct generic_object_layout *object_layout);
2010-08-04 17:32:04 +00:00
extern int generic_add_aspect(struct generic_output *output, struct generic_object_layout *object_layout, const struct generic_aspect_type *aspect_type);
2010-07-30 05:46:22 +00:00
2010-08-05 15:54:48 +00:00
extern int default_init_object_layout(struct generic_output *output, struct generic_object_layout *object_layout, int aspect_max, const struct generic_object_type *object_type, char *module_name);
2010-08-01 20:21:18 +00:00
2010-08-04 17:32:04 +00:00
extern struct generic_object *alloc_generic(struct generic_object_layout *object_layout);
2010-08-01 20:21:18 +00:00
2010-08-02 16:31:10 +00:00
extern void free_generic(struct generic_object *object);
2010-08-01 20:21:18 +00:00
#define GENERIC_OBJECT_LAYOUT_FUNCTIONS(BRICK) \
2010-07-30 05:46:22 +00:00
\
2011-02-23 20:48:06 +00:00
inline int BRICK##_init_object_layout(struct BRICK##_output *output, struct generic_object_layout *object_layout, int aspect_max, const struct generic_object_type *object_type) \
2010-07-30 05:46:22 +00:00
{ \
2011-03-20 17:38:08 +00:00
if (likely(object_layout->aspect_layouts && object_layout->layout_generation == brick_layout_generation)) \
2010-08-01 20:21:18 +00:00
return 0; \
2010-08-05 15:54:48 +00:00
return default_init_object_layout((struct generic_output*)output, object_layout, aspect_max, object_type, #BRICK); \
2010-07-30 05:46:22 +00:00
} \
2010-08-10 17:39:30 +00:00
#define GENERIC_ASPECT_LAYOUT_FUNCTIONS(BRICK,TYPE) \
2010-07-30 05:46:22 +00:00
\
2011-02-23 20:48:06 +00:00
inline int BRICK##_##TYPE##_add_aspect(struct BRICK##_output *output, struct TYPE##_object_layout *object_layout, const struct generic_aspect_type *aspect_type) \
2010-07-30 05:46:22 +00:00
{ \
2010-08-01 20:21:18 +00:00
int res = generic_add_aspect((struct generic_output*)output, (struct generic_object_layout *)object_layout, aspect_type); \
2010-08-10 17:39:30 +00:00
BRICK_DBG(#BRICK " " #TYPE "added aspect_type %p (%s) to object_layout %p (type %s) on output %p (type %s), status=%d\n", aspect_type, aspect_type->aspect_type_name, object_layout, object_layout->object_type->object_type_name, output, output->type->type_name, res); \
2010-07-30 05:46:22 +00:00
return res; \
} \
2010-08-10 17:39:30 +00:00
#define GENERIC_OBJECT_FUNCTIONS(TYPE) \
2010-07-30 05:46:22 +00:00
\
2011-02-23 20:48:06 +00:00
inline struct TYPE##_object *TYPE##_construct(void *data, struct TYPE##_object_layout *object_layout) \
2010-07-30 05:46:22 +00:00
{ \
2010-08-10 17:39:30 +00:00
struct TYPE##_object *obj = data; \
2010-07-30 05:46:22 +00:00
int i; \
\
obj->object_layout = object_layout; \
2010-08-08 09:03:42 +00:00
if (object_layout->object_type->init_fn) { \
int status = object_layout->object_type->init_fn((void*)obj, object_layout->init_data); \
if (status < 0) { \
return NULL; \
} \
} \
2010-07-30 05:46:22 +00:00
for (i = 0; i < object_layout->aspect_count; i++) { \
struct generic_aspect_layout *aspect_layout; \
struct generic_aspect *aspect; \
aspect_layout = object_layout->aspect_layouts[i]; \
if (!aspect_layout->aspect_type) \
continue; \
aspect = data + aspect_layout->aspect_offset; \
aspect->object = (void*)obj; \
if (aspect_layout->aspect_type->init_fn) { \
int status = aspect_layout->aspect_type->init_fn((void*)aspect, aspect_layout->init_data); \
2010-08-08 09:03:42 +00:00
if (status < 0) { \
2010-07-30 05:46:22 +00:00
return NULL; \
} \
} \
} \
return obj; \
} \
2010-08-08 09:03:42 +00:00
\
2011-02-23 20:48:06 +00:00
inline void TYPE##_destruct(struct TYPE##_object *obj) \
2010-08-08 09:03:42 +00:00
{ \
2010-08-10 17:39:30 +00:00
struct TYPE##_object_layout *object_layout = obj->object_layout; \
2010-08-08 09:03:42 +00:00
int i; \
\
2010-08-23 05:06:06 +00:00
if (unlikely(!object_layout || !object_layout->object_type)) { \
BRICK_ERR("object %p/%p is corrupted\n", obj, object_layout); \
dump_stack(); \
return; \
} \
2010-08-08 09:03:42 +00:00
if (object_layout->object_type->exit_fn) { \
object_layout->object_type->exit_fn((void*)obj, object_layout->init_data); \
} \
for (i = 0; i < object_layout->aspect_count; i++) { \
struct generic_aspect_layout *aspect_layout; \
struct generic_aspect *aspect; \
aspect_layout = object_layout->aspect_layouts[i]; \
if (!aspect_layout->aspect_type) \
continue; \
aspect = ((void*)obj) + aspect_layout->aspect_offset; \
if (aspect_layout->aspect_type->exit_fn) { \
aspect_layout->aspect_type->exit_fn((void*)aspect, aspect_layout->init_data); \
} \
} \
} \
2010-07-30 05:46:22 +00:00
2010-08-10 17:39:30 +00:00
#define GENERIC_ASPECT_FUNCTIONS(BRICK,TYPE) \
2010-07-30 05:46:22 +00:00
\
2011-02-23 20:48:06 +00:00
inline struct BRICK##_##TYPE##_aspect *BRICK##_##TYPE##_get_aspect(struct BRICK##_output *output, struct TYPE##_object *obj) \
2010-07-30 05:46:22 +00:00
{ \
2010-08-04 17:32:04 +00:00
struct generic_object_layout *object_layout; \
2010-07-30 05:46:22 +00:00
struct generic_aspect_layout *aspect_layout; \
int nr; \
\
2010-08-04 17:32:04 +00:00
object_layout = (struct generic_object_layout*)obj->object_layout; \
2010-07-30 05:46:22 +00:00
nr = object_layout->object_type->brick_obj_nr; \
aspect_layout = &output->aspect_layouts[nr]; \
if (unlikely(!aspect_layout->aspect_type)) { \
2010-08-10 17:39:30 +00:00
BRICK_ERR("brick "#BRICK": bad aspect slot on " #TYPE " pointer %p\n", obj); \
2010-07-30 05:46:22 +00:00
return NULL; \
} \
return (void*)obj + aspect_layout->aspect_offset; \
} \
\
2011-02-23 20:48:06 +00:00
inline struct TYPE##_object *BRICK##_alloc_##TYPE(struct BRICK##_output *output, struct generic_object_layout *object_layout) \
2010-08-01 20:21:18 +00:00
{ \
2011-03-20 17:38:08 +00:00
if (unlikely(!object_layout->aspect_layouts || object_layout->layout_generation != brick_layout_generation)) { \
int status = default_init_object_layout((struct generic_output*)output, object_layout, 32, &TYPE##_type, #BRICK); \
if (status < 0) \
return NULL; \
} \
2010-08-10 17:39:30 +00:00
return (struct TYPE##_object*)alloc_generic(object_layout); \
2010-08-04 17:32:04 +00:00
} \
\
2011-02-23 20:48:06 +00:00
inline struct TYPE##_object *BRICK##_alloc_##TYPE##_pure(struct generic_object_layout *object_layout) \
2010-08-04 17:32:04 +00:00
{ \
2010-08-10 17:39:30 +00:00
return (struct TYPE##_object*)alloc_generic(object_layout); \
2010-08-01 20:21:18 +00:00
} \
2010-08-02 16:31:10 +00:00
\
2011-02-23 20:48:06 +00:00
inline void BRICK##_free_##TYPE(struct TYPE##_object *object) \
2010-08-02 16:31:10 +00:00
{ \
free_generic((struct generic_object*)object); \
} \
2010-08-01 20:21:18 +00:00
GENERIC_OBJECT_LAYOUT_FUNCTIONS(generic);
GENERIC_OBJECT_FUNCTIONS(generic);
2010-07-30 05:46:22 +00:00
///////////////////////////////////////////////////////////////////////
2011-02-23 20:48:06 +00:00
// some general helpers
extern void get_lamport(struct timespec *now);
extern void set_lamport(struct timespec *old);
2010-07-30 05:46:22 +00:00
#ifdef CONFIG_DEBUG_SPINLOCK
2010-07-30 11:50:20 +00:00
2010-08-07 15:02:16 +00:00
# define LOCK_CHECK(OP) \
({ \
if (atomic_read(&current->lock_count)) { \
2010-07-30 05:46:22 +00:00
BRICK_ERR("never call " #OP "() with a spinlock held.\n"); \
2010-08-07 15:02:16 +00:00
} \
2010-07-30 05:46:22 +00:00
})
2010-07-30 11:50:20 +00:00
2010-08-07 15:02:16 +00:00
# define traced_lock(spinlock,flags) \
do { \
if (atomic_read(&current->lock_count)) { \
BRICK_ERR("please do not nest spinlocks at line %d, reorganize your code.\n", __LINE__); \
} \
atomic_inc(&current->lock_count); \
(void)flags; \
2010-12-10 17:40:20 +00:00
spin_lock_irqsave(spinlock, flags); \
2010-08-07 15:02:16 +00:00
} while (0)
# define traced_unlock(spinlock,flags) \
do { \
2010-12-10 17:40:20 +00:00
spin_unlock_irqrestore(spinlock, flags); \
2010-08-07 15:02:16 +00:00
atomic_dec(&current->lock_count); \
} while (0)
# define traced_readlock(spinlock,flags) \
do { \
if (atomic_read(&current->lock_count)) { \
2010-08-02 16:31:10 +00:00
BRICK_ERR("please do not nest spinlocks at line %d, reorganize your code.\n", __LINE__); \
2010-08-07 15:02:16 +00:00
} \
atomic_inc(&current->lock_count); \
(void)flags; \
read_lock(spinlock); \
} while (0)
# define traced_readunlock(spinlock,flags) \
do { \
/*spin_unlock_irqrestore(spinlock,flags);*/ \
read_unlock(spinlock); \
atomic_dec(&current->lock_count); \
} while (0)
# define traced_writelock(spinlock,flags) \
do { \
if (atomic_read(&current->lock_count)) { \
BRICK_ERR("please do not nest spinlocks at line %d, reorganize your code.\n", __LINE__); \
} \
atomic_inc(&current->lock_count); \
(void)flags; \
2011-03-07 18:36:08 +00:00
write_lock(spinlock); \
2010-07-30 05:46:22 +00:00
} while (0)
2010-07-30 11:50:20 +00:00
2010-08-07 15:02:16 +00:00
# define traced_writeunlock(spinlock,flags) \
do { \
/*spin_unlock_irqrestore(spinlock,flags);*/ \
2011-03-07 18:36:08 +00:00
write_unlock(spinlock); \
2010-08-07 15:02:16 +00:00
atomic_dec(&current->lock_count); \
2010-07-30 05:46:22 +00:00
} while (0)
2010-07-30 11:50:20 +00:00
2010-07-30 05:46:22 +00:00
#else
2010-07-30 11:50:20 +00:00
# define LOCK_CHECK(OP) 0
2011-03-18 13:15:40 +00:00
#if 0
2010-07-30 05:46:22 +00:00
# define traced_lock(spinlock,flags) spin_lock_irqsave(spinlock,flags)
# define traced_unlock(spinlock,flags) spin_unlock_irqrestore(spinlock,flags)
2010-08-07 15:02:16 +00:00
# define traced_readlock(spinlock,flags) read_lock_irqsave(spinlock,flags)
# define traced_readunlock(spinlock,flags) read_unlock_irqrestore(spinlock,flags)
# define traced_writelock(spinlock,flags) write_lock_irqsave(spinlock,flags)
# define traced_writeunlock(spinlock,flags) write_unlock_irqrestore(spinlock,flags)
2011-03-18 13:15:40 +00:00
#else
# define traced_lock(spinlock,flags) do { (void)flags; spin_lock(spinlock); } while (0)
# define traced_unlock(spinlock,flags) do { (void)flags; spin_unlock(spinlock); } while (0)
# define traced_readlock(spinlock,flags) do { (void)flags; read_lock(spinlock); } while (0)
# define traced_readunlock(spinlock,flags) do { (void)flags; read_unlock(spinlock); } while (0)
# define traced_writelock(spinlock,flags) do { (void)flags; write_lock(spinlock); } while (0)
# define traced_writeunlock(spinlock,flags) do { (void)flags; write_unlock(spinlock); } while (0)
#endif
2010-07-23 11:55:18 +00:00
#endif
2011-02-23 20:48:06 +00:00
2011-02-28 18:00:32 +00:00
/* Generic interface to simple brick status changes.
2011-02-27 14:17:58 +00:00
*/
extern void set_button(struct generic_switch *sw, bool val, bool force);
2011-02-23 20:48:06 +00:00
extern void set_led_on(struct generic_switch *sw, bool val);
extern void set_led_off(struct generic_switch *sw, bool val);
2011-03-01 09:34:36 +00:00
/*
* "Forced switch off" means that it cannot be switched on again.
*/
extern void set_button_wait(struct generic_brick *brick, bool val, bool force, int timeout);
2011-02-23 20:48:06 +00:00
2011-02-27 14:17:58 +00:00
/* Operations on networks of bricks (wiring graphs).
*
* Switch on => first switch on all predecessors in the wiring graph
* Switch off => first switch off all successors in the wiring graph
*
* Operations on brick networks by multiple threads in parallel are dangerous,
* because the buttons may start flipping.
* There is one exception: when @force is set, only the direction to
* "off" remains possible. This is useful for emergency shutdowns.
*/
2011-02-28 18:00:32 +00:00
typedef enum {
// only one brick instance
BR_ON_ONE, // switch on one brick instance
BR_OFF_ONE, // just switch off (may be switched on again)
BR_KILL_ONE, // forced switch off => may be never switched on again
BR_FREE_ONE, // forced switch off + deallocation (when possible)
// dito, but operating on the whole graph
BR_ON_ALL,
BR_OFF_ALL,
BR_KILL_ALL,
BR_FREE_ALL,
} brick_switch_t;
extern int set_recursive_button(struct generic_brick *brick, brick_switch_t mode, int timeout);
2011-02-27 14:17:58 +00:00
2011-02-23 20:48:06 +00:00
/////////////////////////////////////////////////////////////////////////
// metadata descriptions
/* The idea is to describe your C structures in such a way that
* transfers to disk or over a network become self-describing.
*
* In essence, this is a kind of version-independent marshalling.
*
* Advantage:
* When you extend your original C struct (and of course update the
* corresponding meta structure), old data on disk (or network peers
* running an old version of your program) will remain valid.
* Upon read, newly added fields missing in the old version will be simply
* not filled in and therefore remain zeroed (if you don't forget to
* initially clear your structures via memset() / initializers / etc).
* Note that this works only if you never rename or remove existing
* fields; you should only add new ones.
* [TODO: add macros for description of ignored / renamed fields to
* overcome this limitation]
* You may increase the size of integers, for example from 32bit to 64bit
* or even higher; sign extension will be automatically carried out
* when necessary.
* [TODO; NYI]
* Also, you may change the order of fields, because the metadata interpreter
* will check each field individually; field offsets are automatically
* maintained.
*
* Disadvantage: this adds some (small) overhead.
*/
#define MAX_FIELD_LEN 24
enum field_type {
FIELD_DONE,
FIELD_REF,
FIELD_SUB,
FIELD_STRING,
FIELD_RAW,
FIELD_INT,
FIELD_UINT,
};
struct meta {
char field_name[MAX_FIELD_LEN];
int field_type;
int field_size;
int field_offset;
const struct meta *field_ref;
};
#define _META_INI(NAME,STRUCT,TYPE) \
.field_name = #NAME, \
.field_type = TYPE, \
.field_size = sizeof(((STRUCT*)NULL)->NAME), \
.field_offset = offsetof(STRUCT, NAME) \
#define META_INI(NAME,STRUCT,TYPE) { _META_INI(NAME,STRUCT,TYPE) }
#define _META_INI_REF(NAME,STRUCT,REF) \
.field_name = #NAME, \
.field_type = FIELD_REF, \
.field_size = sizeof(*(((STRUCT*)NULL)->NAME)), \
.field_offset = offsetof(STRUCT, NAME), \
.field_ref = REF
#define META_INI_REF(NAME,STRUCT,REF) { _META_INI_REF(NAME,STRUCT,REF) }
#define _META_INI_SUB(NAME,STRUCT,SUB) \
.field_name = #NAME, \
.field_type = FIELD_SUB, \
.field_size = sizeof(((STRUCT*)NULL)->NAME), \
.field_offset = offsetof(STRUCT, NAME), \
.field_ref = SUB
#define META_INI_SUB(NAME,STRUCT,SUB) { _META_INI_SUB(NAME,STRUCT,SUB) }
extern const struct meta *find_meta(const struct meta *meta, const char *field_name);
extern void free_meta(void *data, const struct meta *meta);
2010-07-30 11:50:20 +00:00
#endif