1 /*********************************************************************
3 * sir_dongle.c: manager for serial dongle protocol drivers
5 * Copyright (c) 2002 Martin Diehl
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of
10 * the License, or (at your option) any later version.
12 ********************************************************************/
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/init.h>
17 #include <linux/smp_lock.h>
18 #include <linux/kmod.h>
20 #include <net/irda/irda.h>
24 /**************************************************************************
26 * dongle registration and attachment
30 static LIST_HEAD(dongle_list
); /* list of registered dongle drivers */
31 static DECLARE_MUTEX(dongle_list_lock
); /* protects the list */
33 int irda_register_dongle(struct dongle_driver
*new)
35 struct list_head
*entry
;
36 struct dongle_driver
*drv
;
38 IRDA_DEBUG(0, "%s : registering dongle \"%s\" (%d).\n",
39 __FUNCTION__
, new->driver_name
, new->type
);
41 down(&dongle_list_lock
);
42 list_for_each(entry
, &dongle_list
) {
43 drv
= list_entry(entry
, struct dongle_driver
, dongle_list
);
44 if (new->type
== drv
->type
) {
45 up(&dongle_list_lock
);
49 list_add(&new->dongle_list
, &dongle_list
);
50 up(&dongle_list_lock
);
54 int irda_unregister_dongle(struct dongle_driver
*drv
)
56 down(&dongle_list_lock
);
57 list_del(&drv
->dongle_list
);
58 up(&dongle_list_lock
);
62 int sirdev_get_dongle(struct sir_dev
*dev
, IRDA_DONGLE type
)
64 struct list_head
*entry
;
65 const struct dongle_driver
*drv
= NULL
;
69 request_module("irda-dongle-%d", type
);
72 if (dev
->dongle_drv
!= NULL
)
75 /* serialize access to the list of registered dongles */
76 down(&dongle_list_lock
);
78 list_for_each(entry
, &dongle_list
) {
79 drv
= list_entry(entry
, struct dongle_driver
, dongle_list
);
80 if (drv
->type
== type
)
88 goto out_unlock
; /* no such dongle */
91 /* handling of SMP races with dongle module removal - three cases:
92 * 1) dongle driver was already unregistered - then we haven't found the
93 * requested dongle above and are already out here
94 * 2) the module is already marked deleted but the driver is still
95 * registered - then the try_module_get() below will fail
96 * 3) the try_module_get() below succeeds before the module is marked
97 * deleted - then sys_delete_module() fails and prevents the removal
98 * because the module is in use.
101 if (!try_module_get(drv
->owner
)) {
103 goto out_unlock
; /* rmmod already pending */
105 dev
->dongle_drv
= drv
;
107 if (!drv
->open
|| (err
=drv
->open(dev
))!=0)
108 goto out_reject
; /* failed to open driver */
110 up(&dongle_list_lock
);
114 dev
->dongle_drv
= NULL
;
115 module_put(drv
->owner
);
117 up(&dongle_list_lock
);
121 int sirdev_put_dongle(struct sir_dev
*dev
)
123 const struct dongle_driver
*drv
= dev
->dongle_drv
;
127 drv
->close(dev
); /* close this dongle instance */
129 dev
->dongle_drv
= NULL
; /* unlink the dongle driver */
130 module_put(drv
->owner
);/* decrement driver's module refcount */