ir-core: Remove magic numbers at the sysfs logic
A message that is not in the archive (sent off-list or not yet received): [email protected]
PATCH 1/4 ir-core: Remove magic numbers at the sysfs logic
Instead of using "magic" sizes for protocol names, replace them by an array, and use strlen().
No functional changes.
drivers/media/IR/ir-sysfs.c +32 -51
@@ -33,6 +33,18 @@ static struct class ir_input_class = { .devnode = ir_devnode, }; +static struct { +u64 type; +char *name; +} proto_names[] = { + {IR_TYPE_UNKNOWN ,"unknown" }, + {IR_TYPE_RC5 ,"rc5" }, + {IR_TYPE_NEC ,"nec" }, + {IR_TYPE_RC6 ,"rc6" }, + {IR_TYPE_JVC ,"jvc" }, + {IR_TYPE_SONY ,"sony" }, +}; +/** * show_protocols() - shows the current IR protocol(s) * @d: the device descriptor
@@ -50,6 +62,7 @@ static ssize_t show_protocols(struct device *d,struct ir_input_dev *ir_dev =dev_get_drvdata (d);u64 allowed, enabled;char *tmp = buf; +int i;if (ir_dev->props->driver_type ==RC_DRIVER_SCANCODE ) { enabled = ir_dev->rc_tab.ir_type;
@@ -63,35 +76,12 @@ static ssize_t show_protocols(struct device *d, (long long )allowed, (long long)enabled); -if (allowed & enabled &IR_TYPE_UNKNOWN ) - tmp +=sprintf (tmp,"[unknown] " ); -else if (allowed &IR_TYPE_UNKNOWN ) - tmp +=sprintf (tmp,"unknown " ); - -if (allowed & enabled &IR_TYPE_RC5 ) - tmp +=sprintf (tmp,"[rc5] " ); -else if (allowed &IR_TYPE_RC5 ) - tmp +=sprintf (tmp,"rc5 " ); - -if (allowed & enabled &IR_TYPE_NEC ) - tmp +=sprintf (tmp,"[nec] " ); -else if (allowed &IR_TYPE_NEC ) - tmp +=sprintf (tmp,"nec " ); - -if (allowed & enabled &IR_TYPE_RC6 ) - tmp +=sprintf (tmp,"[rc6] " ); -else if (allowed &IR_TYPE_RC6 ) - tmp +=sprintf (tmp,"rc6 " ); - -if (allowed & enabled &IR_TYPE_JVC ) - tmp +=sprintf (tmp,"[jvc] " ); -else if (allowed &IR_TYPE_JVC ) - tmp +=sprintf (tmp,"jvc " ); - -if (allowed & enabled &IR_TYPE_SONY ) - tmp +=sprintf (tmp,"[sony] " ); -else if (allowed &IR_TYPE_SONY ) - tmp +=sprintf (tmp,"sony " ); +for (i =0 ; i <ARRAY_SIZE (proto_names); i++) { +if (allowed & enabled & proto_names[i].type) + tmp +=sprintf (tmp,"[%s] " , proto_names[i].name); +else if (allowed & proto_names[i].type) + tmp +=sprintf (tmp,"%s " , proto_names[i].name); + }if (tmp != buf) tmp--;
@@ -124,12 +114,14 @@ static ssize_t store_protocols(struct device *d,const char *tmp;u64 type;u64 mask; -int rc; +int rc, i;unsigned long flags; tmp =skip_spaces (data); - -if (*tmp =='+' ) { +if (*tmp =='\0' ) { +IR_dprintk (1 ,"Protocol not specified\n" ); +return -EINVAL ; + }else if (*tmp =='+' ) { enable = true; disable = false; tmp++;
@@ -142,25 +134,14 @@ static ssize_t store_protocols(struct device *d, disable = false; } -if (!strncasecmp (tmp,"unknown" ,7 )) { - tmp +=7 ; - mask =IR_TYPE_UNKNOWN ; - }else if (!strncasecmp (tmp,"rc5" ,3 )) { - tmp +=3 ; - mask =IR_TYPE_RC5 ; - }else if (!strncasecmp (tmp,"nec" ,3 )) { - tmp +=3 ; - mask =IR_TYPE_NEC ; - }else if (!strncasecmp (tmp,"rc6" ,3 )) { - tmp +=3 ; - mask =IR_TYPE_RC6 ; - }else if (!strncasecmp (tmp,"jvc" ,3 )) { - tmp +=3 ; - mask =IR_TYPE_JVC ; - }else if (!strncasecmp (tmp,"sony" ,4 )) { - tmp +=4 ; - mask =IR_TYPE_SONY ; - }else { +for (i =0 ; i <ARRAY_SIZE (proto_names); i++) { +if (!strncasecmp (tmp, proto_names[i].name,strlen (proto_names[i].name))) { + tmp +=strlen (proto_names[i].name); + mask = proto_names[i].type; +break ; + } + } +if (i ==ARRAY_SIZE (proto_names)) {IR_dprintk (1 ,"Unknown protocol\n" );return -EINVAL ; }
Signature · 1 line
1.7.1
PATCH 2/4 ir-core: Rename sysfs protocols nomenclature to rc-5 and rc-6
In reply to a message not in the archiveWhile rc-5 and rc-6 protocols are generally abreviated as "rc5" and "rc6", previous sysfs nodes uses rc-5 and rc-6 for the Philips protocols. This is consistent with the protocol nomenclature given by the original Philips
spec: "Remote control system RC-5" (doc. Nr. 9398 706 23011). Also, rc5 is the name of a widely known cryptography protocol.
So, the better is to keep referring to those protocols as "rc-5" and "rc-6".
drivers/media/IR/ir-sysfs.c +2 -2
@@ -38,9 +38,9 @@ static struct {char *name; } proto_names[] = { { IR_TYPE_UNKNOWN, "unknown" }, - { IR_TYPE_RC5, "rc5" }, + { IR_TYPE_RC5, "rc-5" }, { IR_TYPE_NEC, "nec" }, - { IR_TYPE_RC6, "rc6" }, + { IR_TYPE_RC6, "rc-6" }, { IR_TYPE_JVC, "jvc" }, { IR_TYPE_SONY, "sony" }, };
Signature · 1 line
1.7.1
PATCH 3/4 ir-core: Add support for disabling all protocols
In reply to a message not in the archiveWriting "none" to /dev/class/rc/rc*/protocols will disable all protocols. This allows an easier setup, from userspace, as userspace applications don't need to disable protocol per protocol, before enabling a different set of protocols.
drivers/media/IR/ir-sysfs.c +18 -9
@@ -45,6 +45,8 @@ static struct { { IR_TYPE_SONY, "sony" }, }; +#define PROTO_NONE "none" +/** * show_protocols() - shows the current IR protocol(s) * @d: the device descriptor
@@ -101,6 +103,7 @@ static ssize_t show_protocols(struct device *d,* Writing "+proto" will add a protocol to the list of enabled protocols. * Writing "-proto" will remove a protocol from the list of enabled protocols. * Writing "proto" will enable only "proto". +* Writing "none" will disable all protocols. * Returns -EINVAL if an invalid protocol combination or unknown protocol name * is used, otherwise @len. */
@@ -134,16 +137,22 @@ static ssize_t store_protocols(struct device *d, disable = false; } -for (i =0 ; i <ARRAY_SIZE (proto_names); i++) { -if (!strncasecmp (tmp, proto_names[i].name,strlen (proto_names[i].name))) { - tmp +=strlen (proto_names[i].name); - mask = proto_names[i].type; -break ; + +if (!enable && !disable && !strncasecmp (tmp,PROTO_NONE ,sizeof (PROTO_NONE ))) { + mask =0 ; + tmp +=sizeof (PROTO_NONE ); + }else { +for (i =0 ; i <ARRAY_SIZE (proto_names); i++) { +if (!strncasecmp (tmp, proto_names[i].name,strlen (proto_names[i].name))) { + tmp +=strlen (proto_names[i].name); + mask = proto_names[i].type; +break ; + } + } +if (i ==ARRAY_SIZE (proto_names)) { +IR_dprintk (1 ,"Unknown protocol\n" ); +return -EINVAL ; } - } -if (i ==ARRAY_SIZE (proto_names)) { -IR_dprintk (1 ,"Unknown protocol\n" ); -return -EINVAL ; } tmp =skip_spaces (tmp);
Signature · 1 line
1.7.1
PATCH 4/4 ir-core: allow specifying multiple protocols at one open/write
In reply to a message not in the archiveWith this change, it is now possible to do something like:
su -c 'echo "none +rc-5 +nec" > /sys/class/rc/rc1/protocols'
This prevents the need of multiple opens, one for each protocol change, and makes userspace application easier.
drivers/media/IR/ir-sysfs.c +49 -49
@@ -117,61 +117,62 @@ static ssize_t store_protocols(struct device *d,const char *tmp;u64 type;u64 mask; -int rc, i; +int rc, i, count =0 ;unsigned long flags; - tmp =skip_spaces (data); -if (*tmp =='\0' ) { -IR_dprintk (1 ,"Protocol not specified\n" ); -return -EINVAL ; - }else if (*tmp =='+' ) { - enable = true; - disable = false; - tmp++; - }else if (*tmp =='-' ) { - enable = false; - disable = true; - tmp++; - }else { - enable = false; - disable = false; - } - - -if (!enable && !disable && !strncasecmp (tmp,PROTO_NONE ,sizeof (PROTO_NONE ))) { - mask =0 ; - tmp +=sizeof (PROTO_NONE ); - }else { -for (i =0 ; i <ARRAY_SIZE (proto_names); i++) { -if (!strncasecmp (tmp, proto_names[i].name,strlen (proto_names[i].name))) { - tmp +=strlen (proto_names[i].name); - mask = proto_names[i].type; -break ; - } - } -if (i ==ARRAY_SIZE (proto_names)) { -IR_dprintk (1 ,"Unknown protocol\n" ); -return -EINVAL ; - } - } - - tmp =skip_spaces (tmp); -if (*tmp !='\0' ) { -IR_dprintk (1 ,"Invalid trailing characters\n" ); -return -EINVAL ; - } -if (ir_dev->props->driver_type ==RC_DRIVER_SCANCODE ) type = ir_dev->rc_tab.ir_type;else type = ir_dev->raw->enabled_protocols; -if (enable) - type |= mask; -else if (disable) - type &= ~mask; -else - type = mask; +while ((tmp =strsep ((char **) &data," \n" )) !=NULL ) { +if (!*tmp) +break ; + +if (*tmp =='+' ) { + enable = true; + disable = false; + tmp++; + }else if (*tmp =='-' ) { + enable = false; + disable = true; + tmp++; + }else { + enable = false; + disable = false; + } + +if (!enable && !disable && !strncasecmp (tmp,PROTO_NONE ,sizeof (PROTO_NONE ))) { + tmp +=sizeof (PROTO_NONE ); + mask =0 ; + count++; + }else { +for (i =0 ; i <ARRAY_SIZE (proto_names); i++) { +if (!strncasecmp (tmp, proto_names[i].name,strlen (proto_names[i].name))) { + tmp +=strlen (proto_names[i].name); + mask = proto_names[i].type; +break ; + } + } +if (i ==ARRAY_SIZE (proto_names)) { +IR_dprintk (1 ,"Unknown protocol: '%s'\n" , tmp); +return -EINVAL ; + } + count++; + } + +if (enable) + type |= mask; +else if (disable) + type &= ~mask; +else + type = mask; + } + +if (!count) { +IR_dprintk (1 ,"Protocol not specified\n" ); +return -EINVAL ; + }if (ir_dev->props && ir_dev->props->change_protocol) { rc = ir_dev->props->change_protocol(ir_dev->props->priv,
@@ -191,7 +192,6 @@ static ssize_t store_protocols(struct device *d, ir_dev->raw->enabled_protocols = type; } -IR_dprintk (1 ,"Current protocol(s): 0x%llx\n" , (long long )type);
Signature · 1 line
1.7.1