5 * Copyright (C) 2007-2008 Intel Corporation. All rights reserved.
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.
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.
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
30 static GSList *driver_list = NULL;
32 static gboolean match_driver(struct connman_device *device,
33 struct connman_device_driver *driver)
35 if (device->element->subtype == driver->type ||
36 driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
42 static int device_probe(struct connman_element *element)
44 struct connman_device *device;
47 DBG("element %p name %s", element, element->name);
49 device = g_try_new0(struct connman_device, 1);
53 device->element = element;
55 for (list = driver_list; list; list = list->next) {
56 struct connman_device_driver *driver = list->data;
58 if (match_driver(device, driver) == FALSE)
61 DBG("driver %p name %s", driver, driver->name);
63 if (driver->probe(device) == 0) {
64 device->driver = driver;
65 connman_element_set_data(element, device);
75 static void device_remove(struct connman_element *element)
77 struct connman_device *device = connman_element_get_data(element);
79 DBG("element %p name %s", element, element->name);
81 if (device->driver && device->driver->remove)
82 device->driver->remove(device);
84 connman_element_set_data(element, NULL);
89 static struct connman_driver device_driver = {
91 .type = CONNMAN_ELEMENT_TYPE_DEVICE,
92 .priority = CONNMAN_DRIVER_PRIORITY_LOW,
93 .probe = device_probe,
94 .remove = device_remove,
97 int __connman_device_init(void)
101 return connman_driver_register(&device_driver);
104 void __connman_device_cleanup(void)
108 connman_driver_unregister(&device_driver);
111 static gint compare_priority(gconstpointer a, gconstpointer b)
113 const struct connman_device_driver *driver1 = a;
114 const struct connman_device_driver *driver2 = b;
116 return driver2->priority - driver1->priority;
120 * connman_device_driver_register:
121 * @driver: device driver definition
123 * Register a new device driver
125 * Returns: %0 on success
127 int connman_device_driver_register(struct connman_device_driver *driver)
129 DBG("driver %p name %s", driver, driver->name);
131 driver_list = g_slist_insert_sorted(driver_list, driver,
134 __connman_driver_rescan(&device_driver);
140 * connman_device_driver_unregister:
141 * @driver: device driver definition
143 * Remove a previously registered device driver
145 void connman_device_driver_unregister(struct connman_device_driver *driver)
147 DBG("driver %p name %s", driver, driver->name);
149 driver_list = g_slist_remove(driver_list, driver);