]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/net/irda/sir_dongle.c
[NET]: dev_put/dev_hold cleanup
[mirror_ubuntu-artful-kernel.git] / drivers / net / irda / sir_dongle.c
CommitLineData
1da177e4
LT
1/*********************************************************************
2 *
3 * sir_dongle.c: manager for serial dongle protocol drivers
4 *
5 * Copyright (c) 2002 Martin Diehl
6 *
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.
11 *
12 ********************************************************************/
13
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>
19
20#include <net/irda/irda.h>
21
22#include "sir-dev.h"
23
24/**************************************************************************
25 *
26 * dongle registration and attachment
27 *
28 */
29
30static LIST_HEAD(dongle_list); /* list of registered dongle drivers */
31static DECLARE_MUTEX(dongle_list_lock); /* protects the list */
32
33int irda_register_dongle(struct dongle_driver *new)
34{
35 struct list_head *entry;
36 struct dongle_driver *drv;
37
38 IRDA_DEBUG(0, "%s : registering dongle \"%s\" (%d).\n",
39 __FUNCTION__, new->driver_name, new->type);
40
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);
46 return -EEXIST;
47 }
48 }
49 list_add(&new->dongle_list, &dongle_list);
50 up(&dongle_list_lock);
51 return 0;
52}
214ad784 53EXPORT_SYMBOL(irda_register_dongle);
1da177e4
LT
54
55int irda_unregister_dongle(struct dongle_driver *drv)
56{
57 down(&dongle_list_lock);
58 list_del(&drv->dongle_list);
59 up(&dongle_list_lock);
60 return 0;
61}
214ad784 62EXPORT_SYMBOL(irda_unregister_dongle);
1da177e4
LT
63
64int sirdev_get_dongle(struct sir_dev *dev, IRDA_DONGLE type)
65{
66 struct list_head *entry;
67 const struct dongle_driver *drv = NULL;
68 int err = -EINVAL;
69
70#ifdef CONFIG_KMOD
71 request_module("irda-dongle-%d", type);
72#endif
73
74 if (dev->dongle_drv != NULL)
75 return -EBUSY;
76
77 /* serialize access to the list of registered dongles */
78 down(&dongle_list_lock);
79
80 list_for_each(entry, &dongle_list) {
81 drv = list_entry(entry, struct dongle_driver, dongle_list);
82 if (drv->type == type)
83 break;
84 else
85 drv = NULL;
86 }
87
88 if (!drv) {
89 err = -ENODEV;
90 goto out_unlock; /* no such dongle */
91 }
92
93 /* handling of SMP races with dongle module removal - three cases:
94 * 1) dongle driver was already unregistered - then we haven't found the
95 * requested dongle above and are already out here
96 * 2) the module is already marked deleted but the driver is still
97 * registered - then the try_module_get() below will fail
98 * 3) the try_module_get() below succeeds before the module is marked
99 * deleted - then sys_delete_module() fails and prevents the removal
100 * because the module is in use.
101 */
102
103 if (!try_module_get(drv->owner)) {
104 err = -ESTALE;
105 goto out_unlock; /* rmmod already pending */
106 }
107 dev->dongle_drv = drv;
108
109 if (!drv->open || (err=drv->open(dev))!=0)
110 goto out_reject; /* failed to open driver */
111
112 up(&dongle_list_lock);
113 return 0;
114
115out_reject:
116 dev->dongle_drv = NULL;
117 module_put(drv->owner);
118out_unlock:
119 up(&dongle_list_lock);
120 return err;
121}
122
123int sirdev_put_dongle(struct sir_dev *dev)
124{
125 const struct dongle_driver *drv = dev->dongle_drv;
126
127 if (drv) {
128 if (drv->close)
129 drv->close(dev); /* close this dongle instance */
130
131 dev->dongle_drv = NULL; /* unlink the dongle driver */
132 module_put(drv->owner);/* decrement driver's module refcount */
133 }
134
135 return 0;
136}