1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * UWB PAL support.
4 *
5 * Copyright (C) 2008 Cambridge Silicon Radio Ltd.
6 */
7 #include <linux/kernel.h>
8 #include <linux/debugfs.h>
9 #include <linux/export.h>
10
11 #include "uwb.h"
12 #include "uwb-internal.h"
13
14 /**
15 * uwb_pal_init - initialize a UWB PAL
16 * @pal: the PAL to initialize
17 */
uwb_pal_init(struct uwb_pal * pal)18 void uwb_pal_init(struct uwb_pal *pal)
19 {
20 INIT_LIST_HEAD(&pal->node);
21 }
22 EXPORT_SYMBOL_GPL(uwb_pal_init);
23
24 /**
25 * uwb_pal_register - register a UWB PAL
26 * @pal: the PAL
27 *
28 * The PAL must be initialized with uwb_pal_init().
29 */
uwb_pal_register(struct uwb_pal * pal)30 int uwb_pal_register(struct uwb_pal *pal)
31 {
32 struct uwb_rc *rc = pal->rc;
33 int ret;
34
35 if (pal->device) {
36 /* create a link to the uwb_rc in the PAL device's directory. */
37 ret = sysfs_create_link(&pal->device->kobj,
38 &rc->uwb_dev.dev.kobj, "uwb_rc");
39 if (ret < 0)
40 return ret;
41 /* create a link to the PAL in the UWB device's directory. */
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
50 pal->debugfs_dir = uwb_dbg_create_pal_dir(pal);
51
52 mutex_lock(&rc->uwb_dev.mutex);
53 list_add(&pal->node, &rc->pals);
54 mutex_unlock(&rc->uwb_dev.mutex);
55
56 return 0;
57 }
58 EXPORT_SYMBOL_GPL(uwb_pal_register);
59
find_rc(struct device * dev,const void * data)60 static 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 */
uwb_rc_class_device_exists(struct uwb_rc * target_rc)83 static 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
89 put_device(dev);
90
91 return (dev != NULL);
92 }
93
94 /**
95 * uwb_pal_unregister - unregister a UWB PAL
96 * @pal: the PAL
97 */
uwb_pal_unregister(struct uwb_pal * pal)98 void uwb_pal_unregister(struct uwb_pal *pal)
99 {
100 struct uwb_rc *rc = pal->rc;
101
102 uwb_radio_stop(pal);
103
104 mutex_lock(&rc->uwb_dev.mutex);
105 list_del(&pal->node);
106 mutex_unlock(&rc->uwb_dev.mutex);
107
108 debugfs_remove(pal->debugfs_dir);
109
110 if (pal->device) {
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. */
116 sysfs_remove_link(&pal->device->kobj, "uwb_rc");
117 }
118 }
119 EXPORT_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 */
uwb_rc_pal_init(struct uwb_rc * rc)125 void uwb_rc_pal_init(struct uwb_rc *rc)
126 {
127 INIT_LIST_HEAD(&rc->pals);
128 }
129