libnvdimm/altmap: Track namespace boundaries in altmap
[linux-2.6-block.git] / drivers / uwb / pal.c
CommitLineData
caab277b 1// SPDX-License-Identifier: GPL-2.0-only
183b9b59
IPG
2/*
3 * UWB PAL support.
4 *
5 * Copyright (C) 2008 Cambridge Silicon Radio Ltd.
183b9b59
IPG
6 */
7#include <linux/kernel.h>
dcc7461e 8#include <linux/debugfs.h>
183b9b59 9#include <linux/uwb.h>
475c0a6b 10#include <linux/export.h>
183b9b59
IPG
11
12#include "uwb-internal.h"
13
14/**
15 * uwb_pal_init - initialize a UWB PAL
16 * @pal: the PAL to initialize
17 */
18void uwb_pal_init(struct uwb_pal *pal)
19{
20 INIT_LIST_HEAD(&pal->node);
21}
22EXPORT_SYMBOL_GPL(uwb_pal_init);
23
24/**
25 * uwb_pal_register - register a UWB PAL
183b9b59
IPG
26 * @pal: the PAL
27 *
28 * The PAL must be initialized with uwb_pal_init().
29 */
6fae35f9 30int uwb_pal_register(struct uwb_pal *pal)
183b9b59 31{
6fae35f9 32 struct uwb_rc *rc = pal->rc;
b60066c1
DV
33 int ret;
34
35 if (pal->device) {
a8995751 36 /* create a link to the uwb_rc in the PAL device's directory. */
b60066c1
DV
37 ret = sysfs_create_link(&pal->device->kobj,
38 &rc->uwb_dev.dev.kobj, "uwb_rc");
39 if (ret < 0)
40 return ret;
a8995751 41 /* create a link to the PAL in the UWB device's directory. */
b60066c1
DV
42 ret = sysfs_create_link(&rc->uwb_dev.dev.kobj,
43 &pal->device->kobj, pal->name);
44 if (ret < 0) {
45 sysfs_remove_link(&pal->device->kobj, "uwb_rc");
46 return ret;
47 }
48 }
49
dcc7461e
DV
50 pal->debugfs_dir = uwb_dbg_create_pal_dir(pal);
51
6fae35f9 52 mutex_lock(&rc->uwb_dev.mutex);
183b9b59 53 list_add(&pal->node, &rc->pals);
6fae35f9 54 mutex_unlock(&rc->uwb_dev.mutex);
183b9b59
IPG
55
56 return 0;
57}
58EXPORT_SYMBOL_GPL(uwb_pal_register);
59
fbbde074
TP
60static int find_rc(struct device *dev, const void *data)
61{
62 const struct uwb_rc *target_rc = data;
63 struct uwb_rc *rc = dev_get_drvdata(dev);
64
65 if (rc == NULL) {
66 WARN_ON(1);
67 return 0;
68 }
69 if (rc == target_rc) {
70 if (rc->ready == 0)
71 return 0;
72 else
73 return 1;
74 }
75 return 0;
76}
77
78/**
79 * Given a radio controller descriptor see if it is registered.
80 *
81 * @returns false if the rc does not exist or is quiescing; true otherwise.
82 */
83static bool uwb_rc_class_device_exists(struct uwb_rc *target_rc)
84{
85 struct device *dev;
86
87 dev = class_find_device(&uwb_rc_class, NULL, target_rc, find_rc);
88
d6124b40
JH
89 put_device(dev);
90
fbbde074
TP
91 return (dev != NULL);
92}
93
183b9b59 94/**
fbbde074 95 * uwb_pal_unregister - unregister a UWB PAL
183b9b59
IPG
96 * @pal: the PAL
97 */
6fae35f9 98void uwb_pal_unregister(struct uwb_pal *pal)
183b9b59 99{
6fae35f9
DV
100 struct uwb_rc *rc = pal->rc;
101
102 uwb_radio_stop(pal);
103
104 mutex_lock(&rc->uwb_dev.mutex);
183b9b59 105 list_del(&pal->node);
6fae35f9 106 mutex_unlock(&rc->uwb_dev.mutex);
b60066c1 107
dcc7461e
DV
108 debugfs_remove(pal->debugfs_dir);
109
b60066c1 110 if (pal->device) {
fbbde074
TP
111 /* remove link to the PAL in the UWB device's directory. */
112 if (uwb_rc_class_device_exists(rc))
113 sysfs_remove_link(&rc->uwb_dev.dev.kobj, pal->name);
114
115 /* remove link to uwb_rc in the PAL device's directory. */
b60066c1
DV
116 sysfs_remove_link(&pal->device->kobj, "uwb_rc");
117 }
183b9b59
IPG
118}
119EXPORT_SYMBOL_GPL(uwb_pal_unregister);
120
121/**
122 * uwb_rc_pal_init - initialize the PAL related parts of a radio controller
123 * @rc: the radio controller
124 */
125void uwb_rc_pal_init(struct uwb_rc *rc)
126{
183b9b59
IPG
127 INIT_LIST_HEAD(&rc->pals);
128}