Skip to content

Commit 648b50d

Browse files
Heikki Krogerusjmberg-intel
authored andcommitted
net: rfkill: add rfkill_find_type function
Helper for finding the type based on name. Useful if the type needs to be determined based on device property. Signed-off-by: Heikki Krogerus <[email protected]> [modify rfkill_types array and BUILD_BUG_ON to not cause errors] Signed-off-by: Johannes Berg <[email protected]>
1 parent 7837a77 commit 648b50d

File tree

2 files changed

+45
-28
lines changed

2 files changed

+45
-28
lines changed

include/linux/rfkill.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,15 @@ void rfkill_set_states(struct rfkill *rfkill, bool sw, bool hw);
213213
* @rfkill: rfkill struct to query
214214
*/
215215
bool rfkill_blocked(struct rfkill *rfkill);
216+
217+
/**
218+
* rfkill_find_type - Helpper for finding rfkill type by name
219+
* @name: the name of the type
220+
*
221+
* Returns enum rfkill_type that conrresponds the name.
222+
*/
223+
enum rfkill_type rfkill_find_type(const char *name);
224+
216225
#else /* !RFKILL */
217226
static inline struct rfkill * __must_check
218227
rfkill_alloc(const char *name,
@@ -269,6 +278,12 @@ static inline bool rfkill_blocked(struct rfkill *rfkill)
269278
{
270279
return false;
271280
}
281+
282+
static inline enum rfkill_type rfkill_find_type(const char *name)
283+
{
284+
return RFKILL_TYPE_ALL;
285+
}
286+
272287
#endif /* RFKILL || RFKILL_MODULE */
273288

274289

net/rfkill/core.c

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,34 @@ void rfkill_set_states(struct rfkill *rfkill, bool sw, bool hw)
572572
}
573573
EXPORT_SYMBOL(rfkill_set_states);
574574

575+
static const char * const rfkill_types[] = {
576+
NULL, /* RFKILL_TYPE_ALL */
577+
"wlan",
578+
"bluetooth",
579+
"ultrawideband",
580+
"wimax",
581+
"wwan",
582+
"gps",
583+
"fm",
584+
"nfc",
585+
};
586+
587+
enum rfkill_type rfkill_find_type(const char *name)
588+
{
589+
int i;
590+
591+
BUILD_BUG_ON(ARRAY_SIZE(rfkill_types) != NUM_RFKILL_TYPES);
592+
593+
if (!name)
594+
return RFKILL_TYPE_ALL;
595+
596+
for (i = 1; i < NUM_RFKILL_TYPES; i++)
597+
if (!strcmp(name, rfkill_types[i]))
598+
return i;
599+
return RFKILL_TYPE_ALL;
600+
}
601+
EXPORT_SYMBOL(rfkill_find_type);
602+
575603
static ssize_t name_show(struct device *dev, struct device_attribute *attr,
576604
char *buf)
577605
{
@@ -581,38 +609,12 @@ static ssize_t name_show(struct device *dev, struct device_attribute *attr,
581609
}
582610
static DEVICE_ATTR_RO(name);
583611

584-
static const char *rfkill_get_type_str(enum rfkill_type type)
585-
{
586-
BUILD_BUG_ON(NUM_RFKILL_TYPES != RFKILL_TYPE_NFC + 1);
587-
588-
switch (type) {
589-
case RFKILL_TYPE_WLAN:
590-
return "wlan";
591-
case RFKILL_TYPE_BLUETOOTH:
592-
return "bluetooth";
593-
case RFKILL_TYPE_UWB:
594-
return "ultrawideband";
595-
case RFKILL_TYPE_WIMAX:
596-
return "wimax";
597-
case RFKILL_TYPE_WWAN:
598-
return "wwan";
599-
case RFKILL_TYPE_GPS:
600-
return "gps";
601-
case RFKILL_TYPE_FM:
602-
return "fm";
603-
case RFKILL_TYPE_NFC:
604-
return "nfc";
605-
default:
606-
BUG();
607-
}
608-
}
609-
610612
static ssize_t type_show(struct device *dev, struct device_attribute *attr,
611613
char *buf)
612614
{
613615
struct rfkill *rfkill = to_rfkill(dev);
614616

615-
return sprintf(buf, "%s\n", rfkill_get_type_str(rfkill->type));
617+
return sprintf(buf, "%s\n", rfkill_types[rfkill->type]);
616618
}
617619
static DEVICE_ATTR_RO(type);
618620

@@ -750,7 +752,7 @@ static int rfkill_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
750752
if (error)
751753
return error;
752754
error = add_uevent_var(env, "RFKILL_TYPE=%s",
753-
rfkill_get_type_str(rfkill->type));
755+
rfkill_types[rfkill->type]);
754756
if (error)
755757
return error;
756758
spin_lock_irqsave(&rfkill->lock, flags);

0 commit comments

Comments
 (0)