const struct resource_attribute *attrs;
};
+struct resource_device {
+ char *name;
+ int type;
+
+ /*
+ * Never initialize it by user of add_resource_device().
+ * It will be initialized by add_resource_device function automatically.
+ */
+ int index;
+};
+
struct resource {
char *name;
int type;
+ int index;
void *user_data;
int num_attrs;
remove_resource_driver(resource_driver); \
}
+/* Add/remove resource driver and device */
void add_resource_driver(const struct resource_driver *resource_driver);
void remove_resource_driver(const struct resource_driver *resource_driver);
-struct resource *create_resource(int resource_type, const char *resource_name, void *user_data);
+int get_resource_device_count_all(void);
+int get_resource_device_count(int resource_type);
+const struct resource_device *find_resource_device(int resource_type, int resource_index);
+int add_resource_device(struct resource_device *resource_device);
+void remove_resource_device(struct resource_device *resource_device);
+
+/* Create/delete resource instance */
+struct resource *create_resource(int resource_type, int resource_index, void *user_data);
void delete_resource(struct resource *resource);
+/* Handle resource attribute */
int update_resource_attr(struct resource *resource, u_int64_t attr_id);
int update_resource_attrs(struct resource *resource);
int update_resource_attrs_interest(struct resource *resource, u_int64_t attr_interest_mask);
#define RESOURCE_ATTR_INDEX(id) (63 - __builtin_clzll(id))
static GList *g_resource_driver_head;
+static GList *g_resource_device_head;
static gint __compare_resource_type(gconstpointer data, gconstpointer input)
{
g_list_remove(g_resource_driver_head, (gpointer)driver);
}
-const struct resource_driver *get_resource_driver(int resource_type)
+int get_resource_device_count_all(void)
+{
+ return g_list_length(g_resource_device_head);
+}
+
+int get_resource_device_count(int resource_type)
+{
+ GList *node;
+ struct resource_device *device;
+ int count = 0;
+
+ for (node = g_resource_device_head; node != NULL; node = node->next) {
+ device = node->data;
+ if (device->type == resource_type)
+ count++;
+ }
+
+ return count;
+}
+
+const struct resource_device *find_resource_device(int resource_type,
+ int resource_index)
+{
+ GList *node;
+ const struct resource_device *device;
+
+ for (node = g_resource_device_head; node != NULL; node = node->next) {
+ device = node->data;
+ if (device->type == resource_type &&
+ device->index == resource_index)
+ return device;
+ }
+
+ return NULL;
+}
+
+int add_resource_device(struct resource_device *device)
+{
+ int count;
+
+ if (!device)
+ return -EINVAL;
+
+ count = get_resource_device_count(device->type);
+ device->index = count;
+
+ g_resource_device_head =
+ g_list_append(g_resource_device_head, (gpointer)device);
+
+ return 0;
+}
+
+void remove_resource_device(struct resource_device *device)
+{
+ if (!device)
+ return;
+
+ g_resource_device_head =
+ g_list_remove(g_resource_device_head, (gpointer)device);
+}
+
+static const struct resource_driver *find_resource_driver(int resource_type)
{
GList *node;
free(resource);
}
-struct resource *create_resource(int resource_type, const char *resource_name, void *user_data)
+struct resource *create_resource(int resource_type, int resource_index,
+ void *user_data)
{
+ const struct resource_device *device = NULL;
const struct resource_driver *driver = NULL;
struct resource *resource = NULL;
int i;
- driver = get_resource_driver(resource_type);
+ device = find_resource_device(resource_type, resource_index);
+ if (!device)
+ return NULL;
+
+ driver = find_resource_driver(resource_type);
if (!driver)
return NULL;
if (!resource)
return NULL;
- resource->type = resource_type;
- resource->name = g_strdup(resource_name);
+ resource->type = device->type;
+ resource->name = g_strdup(device->name);
+ resource->index = device->index;
resource->user_data = user_data;
resource->num_attrs = driver->num_attrs;
resource->attrs = driver->attrs;