5

I am developing a Linux device driver where I have to pass a string of characters to it using sysfs interface. Can the sysfs attributes accept the data in a string form (something like echo "somedata" > sysfs_interface )?

I have implemented it above, and it seems to be working fine, but I would like to make certain that this is valid (acceptable in the kernel community).

xenteros
  • 15,586
  • 12
  • 56
  • 91
Raulp
  • 7,758
  • 20
  • 93
  • 155
  • Please provide [mcve] of what you have implemented if you want [so] to decide if your solution is valid or not. [ask] – xenteros Oct 17 '16 at 05:08

1 Answers1

7

Can the sysfs attributes accepts the data in a string form ...

Yes.
Actually that is what sysfs accepts when you use echo. When you use echo 0 the output is two bytes, 0x30 (the ASCII code for digit zero) and 0x0A (a newline).

For example the GPIO LED interface uses keywords to report and select the trigger.

# cat /sys/class/leds/d8/trigger
none nand-disk mmc0 timer [heartbeat] gpio

(The bracketed keyword indicates the current selection, the heartbeat timer.)

# echo none > /sys/class/leds/d8/trigger
# cat /sys/class/leds/d8/trigger
[none] nand-disk mmc0 timer heartbeat gpio

... (something like echo "somedata" > sysfs_interface )

You don't even need to use the quote marks.
See the above example of setting the LED trigger to none.


ADDENDUM

these are the custom interfaces ...

No, this is in mainline .

... but what about the one provided by the subsystem?

The authoritative answer is from Linux Documentation/filesystems/sysfs.txt:

Attributes should be ASCII text files, preferably with only one value
per file.
sawdust
  • 16,103
  • 3
  • 40
  • 50
  • these are the custom interfaces , but what about the one provided by the subsystem?Like led susbsystem's "brightness" attribute? – Raulp Sep 05 '16 at 18:29
  • @Raulp -- See addendum. – sawdust Sep 05 '16 at 20:01
  • I agree and accept your answer ! any idea how can the array of system defined attribute be made so that it appears under each of the similar types of attribute.say for example there are 4 channels and I want brightness to be appeared in every channel.If you want I cant put it as a new Question.!! – Raulp Sep 06 '16 at 03:48
  • That's a new question that should be separate from this one. – sawdust Sep 06 '16 at 07:01
  • does /sys/devices/platform//attribute aslso comes under the syfs for the same driver? – Raulp Sep 13 '16 at 08:04
  • Study Documentation/driver-model/binding.txt in your Linux kernel source. – sawdust Sep 13 '16 at 19:42