1
2
3
4
5
6
7
8
9
10
11
12
13
14#include <linux/module.h>
15#include <linux/kernel.h>
16#include <linux/init.h>
17#include <linux/kmod.h>
18#include <linux/mutex.h>
19
20#include <net/irda/irda.h>
21
22#include "sir-dev.h"
23
24
25
26
27
28
29
30static LIST_HEAD(dongle_list);
31static DEFINE_MUTEX(dongle_list_lock);
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 __func__, new->driver_name, new->type);
40
41 mutex_lock(&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 mutex_unlock(&dongle_list_lock);
46 return -EEXIST;
47 }
48 }
49 list_add(&new->dongle_list, &dongle_list);
50 mutex_unlock(&dongle_list_lock);
51 return 0;
52}
53EXPORT_SYMBOL(irda_register_dongle);
54
55int irda_unregister_dongle(struct dongle_driver *drv)
56{
57 mutex_lock(&dongle_list_lock);
58 list_del(&drv->dongle_list);
59 mutex_unlock(&dongle_list_lock);
60 return 0;
61}
62EXPORT_SYMBOL(irda_unregister_dongle);
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 request_module("irda-dongle-%d", type);
71
72 if (dev->dongle_drv != NULL)
73 return -EBUSY;
74
75
76 mutex_lock(&dongle_list_lock);
77
78 list_for_each(entry, &dongle_list) {
79 drv = list_entry(entry, struct dongle_driver, dongle_list);
80 if (drv->type == type)
81 break;
82 else
83 drv = NULL;
84 }
85
86 if (!drv) {
87 err = -ENODEV;
88 goto out_unlock;
89 }
90
91
92
93
94
95
96
97
98
99
100
101 if (!try_module_get(drv->owner)) {
102 err = -ESTALE;
103 goto out_unlock;
104 }
105 dev->dongle_drv = drv;
106
107 if (!drv->open || (err=drv->open(dev))!=0)
108 goto out_reject;
109
110 mutex_unlock(&dongle_list_lock);
111 return 0;
112
113out_reject:
114 dev->dongle_drv = NULL;
115 module_put(drv->owner);
116out_unlock:
117 mutex_unlock(&dongle_list_lock);
118 return err;
119}
120
121int sirdev_put_dongle(struct sir_dev *dev)
122{
123 const struct dongle_driver *drv = dev->dongle_drv;
124
125 if (drv) {
126 if (drv->close)
127 drv->close(dev);
128
129 dev->dongle_drv = NULL;
130 module_put(drv->owner);
131 }
132
133 return 0;
134}
135