Linux 5.1.15
[linux/fpc-iii.git] / drivers / uwb / pal.c
blob678e93741ae156bf5d2c41b7a52f7b215262210d
1 /*
2 * UWB PAL support.
4 * Copyright (C) 2008 Cambridge Silicon Radio Ltd.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version
8 * 2 as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include <linux/kernel.h>
19 #include <linux/debugfs.h>
20 #include <linux/uwb.h>
21 #include <linux/export.h>
23 #include "uwb-internal.h"
25 /**
26 * uwb_pal_init - initialize a UWB PAL
27 * @pal: the PAL to initialize
29 void uwb_pal_init(struct uwb_pal *pal)
31 INIT_LIST_HEAD(&pal->node);
33 EXPORT_SYMBOL_GPL(uwb_pal_init);
35 /**
36 * uwb_pal_register - register a UWB PAL
37 * @pal: the PAL
39 * The PAL must be initialized with uwb_pal_init().
41 int uwb_pal_register(struct uwb_pal *pal)
43 struct uwb_rc *rc = pal->rc;
44 int ret;
46 if (pal->device) {
47 /* create a link to the uwb_rc in the PAL device's directory. */
48 ret = sysfs_create_link(&pal->device->kobj,
49 &rc->uwb_dev.dev.kobj, "uwb_rc");
50 if (ret < 0)
51 return ret;
52 /* create a link to the PAL in the UWB device's directory. */
53 ret = sysfs_create_link(&rc->uwb_dev.dev.kobj,
54 &pal->device->kobj, pal->name);
55 if (ret < 0) {
56 sysfs_remove_link(&pal->device->kobj, "uwb_rc");
57 return ret;
61 pal->debugfs_dir = uwb_dbg_create_pal_dir(pal);
63 mutex_lock(&rc->uwb_dev.mutex);
64 list_add(&pal->node, &rc->pals);
65 mutex_unlock(&rc->uwb_dev.mutex);
67 return 0;
69 EXPORT_SYMBOL_GPL(uwb_pal_register);
71 static int find_rc(struct device *dev, const void *data)
73 const struct uwb_rc *target_rc = data;
74 struct uwb_rc *rc = dev_get_drvdata(dev);
76 if (rc == NULL) {
77 WARN_ON(1);
78 return 0;
80 if (rc == target_rc) {
81 if (rc->ready == 0)
82 return 0;
83 else
84 return 1;
86 return 0;
89 /**
90 * Given a radio controller descriptor see if it is registered.
92 * @returns false if the rc does not exist or is quiescing; true otherwise.
94 static bool uwb_rc_class_device_exists(struct uwb_rc *target_rc)
96 struct device *dev;
98 dev = class_find_device(&uwb_rc_class, NULL, target_rc, find_rc);
100 put_device(dev);
102 return (dev != NULL);
106 * uwb_pal_unregister - unregister a UWB PAL
107 * @pal: the PAL
109 void uwb_pal_unregister(struct uwb_pal *pal)
111 struct uwb_rc *rc = pal->rc;
113 uwb_radio_stop(pal);
115 mutex_lock(&rc->uwb_dev.mutex);
116 list_del(&pal->node);
117 mutex_unlock(&rc->uwb_dev.mutex);
119 debugfs_remove(pal->debugfs_dir);
121 if (pal->device) {
122 /* remove link to the PAL in the UWB device's directory. */
123 if (uwb_rc_class_device_exists(rc))
124 sysfs_remove_link(&rc->uwb_dev.dev.kobj, pal->name);
126 /* remove link to uwb_rc in the PAL device's directory. */
127 sysfs_remove_link(&pal->device->kobj, "uwb_rc");
130 EXPORT_SYMBOL_GPL(uwb_pal_unregister);
133 * uwb_rc_pal_init - initialize the PAL related parts of a radio controller
134 * @rc: the radio controller
136 void uwb_rc_pal_init(struct uwb_rc *rc)
138 INIT_LIST_HEAD(&rc->pals);