Re-enable generic device driver infrastructure
[connman] / src / device.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2008  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <errno.h>
27
28 #include "connman.h"
29
30 static GSList *driver_list = NULL;
31
32 static gboolean match_driver(struct connman_device *device,
33                                         struct connman_device_driver *driver)
34 {
35         if (device->element->subtype == driver->type ||
36                         driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
37                 return TRUE;
38
39         return FALSE;
40 }
41
42 static int device_probe(struct connman_element *element)
43 {
44         struct connman_device *device;
45         GSList *list;
46
47         DBG("element %p name %s", element, element->name);
48
49         device = g_try_new0(struct connman_device, 1);
50         if (device == NULL)
51                 return -ENOMEM;
52
53         device->element = element;
54
55         for (list = driver_list; list; list = list->next) {
56                 struct connman_device_driver *driver = list->data;
57
58                 if (match_driver(device, driver) == FALSE)
59                         continue;
60
61                 DBG("driver %p name %s", driver, driver->name);
62
63                 if (driver->probe(device) == 0) {
64                         device->driver = driver;
65                         connman_element_set_data(element, device);
66                         return 0;
67                 }
68         }
69
70         g_free(device);
71
72         return -ENODEV;
73 }
74
75 static void device_remove(struct connman_element *element)
76 {
77         struct connman_device *device = connman_element_get_data(element);
78
79         DBG("element %p name %s", element, element->name);
80
81         if (device->driver && device->driver->remove)
82                 device->driver->remove(device);
83
84         connman_element_set_data(element, NULL);
85
86         g_free(device);
87 }
88
89 static struct connman_driver device_driver = {
90         .name           = "device",
91         .type           = CONNMAN_ELEMENT_TYPE_DEVICE,
92         .priority       = CONNMAN_DRIVER_PRIORITY_LOW,
93         .probe          = device_probe,
94         .remove         = device_remove,
95 };
96
97 int __connman_device_init(void)
98 {
99         DBG("");
100
101         return connman_driver_register(&device_driver);
102 }
103
104 void __connman_device_cleanup(void)
105 {
106         DBG("");
107
108         connman_driver_unregister(&device_driver);
109 }
110
111 static gint compare_priority(gconstpointer a, gconstpointer b)
112 {
113         const struct connman_device_driver *driver1 = a;
114         const struct connman_device_driver *driver2 = b;
115
116         return driver2->priority - driver1->priority;
117 }
118
119 /**
120  * connman_device_driver_register:
121  * @driver: device driver definition
122  *
123  * Register a new device driver
124  *
125  * Returns: %0 on success
126  */
127 int connman_device_driver_register(struct connman_device_driver *driver)
128 {
129         DBG("driver %p name %s", driver, driver->name);
130
131         driver_list = g_slist_insert_sorted(driver_list, driver,
132                                                         compare_priority);
133
134         //__connman_driver_rescan(&device_driver);
135
136         return 0;
137 }
138
139 /**
140  * connman_device_driver_unregister:
141  * @driver: device driver definition
142  *
143  * Remove a previously registered device driver
144  */
145 void connman_device_driver_unregister(struct connman_device_driver *driver)
146 {
147         DBG("driver %p name %s", driver, driver->name);
148
149         driver_list = g_slist_remove(driver_list, driver);
150 }