blob: c1304b8d498530124c7ca30319c12cc823e84d6e [file] [log] [blame]
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +01001/*
2 * UWB PAL support.
3 *
4 * Copyright (C) 2008 Cambridge Silicon Radio Ltd.
5 *
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.
9 *
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.
14 *
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/>.
17 */
18#include <linux/kernel.h>
David Vrabeldcc74612008-11-26 13:36:59 +000019#include <linux/debugfs.h>
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +010020#include <linux/uwb.h>
Paul Gortmaker475c0a62011-07-10 13:18:02 -040021#include <linux/export.h>
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +010022
23#include "uwb-internal.h"
24
25/**
26 * uwb_pal_init - initialize a UWB PAL
27 * @pal: the PAL to initialize
28 */
29void uwb_pal_init(struct uwb_pal *pal)
30{
31 INIT_LIST_HEAD(&pal->node);
32}
33EXPORT_SYMBOL_GPL(uwb_pal_init);
34
35/**
36 * uwb_pal_register - register a UWB PAL
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +010037 * @pal: the PAL
38 *
39 * The PAL must be initialized with uwb_pal_init().
40 */
David Vrabel6fae35f2008-11-17 15:53:42 +000041int uwb_pal_register(struct uwb_pal *pal)
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +010042{
David Vrabel6fae35f2008-11-17 15:53:42 +000043 struct uwb_rc *rc = pal->rc;
David Vrabelb60066c2008-09-17 16:34:40 +010044 int ret;
45
46 if (pal->device) {
Thomas Pugliesea8995752013-06-24 14:26:35 -050047 /* create a link to the uwb_rc in the PAL device's directory. */
David Vrabelb60066c2008-09-17 16:34:40 +010048 ret = sysfs_create_link(&pal->device->kobj,
49 &rc->uwb_dev.dev.kobj, "uwb_rc");
50 if (ret < 0)
51 return ret;
Thomas Pugliesea8995752013-06-24 14:26:35 -050052 /* create a link to the PAL in the UWB device's directory. */
David Vrabelb60066c2008-09-17 16:34:40 +010053 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;
58 }
59 }
60
David Vrabeldcc74612008-11-26 13:36:59 +000061 pal->debugfs_dir = uwb_dbg_create_pal_dir(pal);
62
David Vrabel6fae35f2008-11-17 15:53:42 +000063 mutex_lock(&rc->uwb_dev.mutex);
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +010064 list_add(&pal->node, &rc->pals);
David Vrabel6fae35f2008-11-17 15:53:42 +000065 mutex_unlock(&rc->uwb_dev.mutex);
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +010066
67 return 0;
68}
69EXPORT_SYMBOL_GPL(uwb_pal_register);
70
Thomas Pugliesefbbde0742013-08-08 09:38:23 -050071static int find_rc(struct device *dev, const void *data)
72{
73 const struct uwb_rc *target_rc = data;
74 struct uwb_rc *rc = dev_get_drvdata(dev);
75
76 if (rc == NULL) {
77 WARN_ON(1);
78 return 0;
79 }
80 if (rc == target_rc) {
81 if (rc->ready == 0)
82 return 0;
83 else
84 return 1;
85 }
86 return 0;
87}
88
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +010089/**
Thomas Pugliesefbbde0742013-08-08 09:38:23 -050090 * Given a radio controller descriptor see if it is registered.
91 *
92 * @returns false if the rc does not exist or is quiescing; true otherwise.
93 */
94static bool uwb_rc_class_device_exists(struct uwb_rc *target_rc)
95{
96 struct device *dev;
97
98 dev = class_find_device(&uwb_rc_class, NULL, target_rc, find_rc);
99
100 return (dev != NULL);
101}
102
103/**
104 * uwb_pal_unregister - unregister a UWB PAL
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +0100105 * @pal: the PAL
106 */
David Vrabel6fae35f2008-11-17 15:53:42 +0000107void uwb_pal_unregister(struct uwb_pal *pal)
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +0100108{
David Vrabel6fae35f2008-11-17 15:53:42 +0000109 struct uwb_rc *rc = pal->rc;
110
111 uwb_radio_stop(pal);
112
113 mutex_lock(&rc->uwb_dev.mutex);
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +0100114 list_del(&pal->node);
David Vrabel6fae35f2008-11-17 15:53:42 +0000115 mutex_unlock(&rc->uwb_dev.mutex);
David Vrabelb60066c2008-09-17 16:34:40 +0100116
David Vrabeldcc74612008-11-26 13:36:59 +0000117 debugfs_remove(pal->debugfs_dir);
118
David Vrabelb60066c2008-09-17 16:34:40 +0100119 if (pal->device) {
Thomas Pugliesefbbde0742013-08-08 09:38:23 -0500120 /* remove link to the PAL in the UWB device's directory. */
121 if (uwb_rc_class_device_exists(rc))
122 sysfs_remove_link(&rc->uwb_dev.dev.kobj, pal->name);
123
124 /* remove link to uwb_rc in the PAL device's directory. */
David Vrabelb60066c2008-09-17 16:34:40 +0100125 sysfs_remove_link(&pal->device->kobj, "uwb_rc");
126 }
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +0100127}
128EXPORT_SYMBOL_GPL(uwb_pal_unregister);
129
130/**
131 * uwb_rc_pal_init - initialize the PAL related parts of a radio controller
132 * @rc: the radio controller
133 */
134void uwb_rc_pal_init(struct uwb_rc *rc)
135{
Inaky Perez-Gonzalez183b9b52008-09-17 16:34:06 +0100136 INIT_LIST_HEAD(&rc->pals);
137}