How to write device drivers in Javascript?
Asked Answered
P

8

20

Is it possible to write hardware drivers in Javascript? What would be the steps required for such a task?

Also, I was unsure where to post this, so any suggestions regarding this are also welcome. I hope this is the accurate location for the question.

Peroneus answered 22/8, 2012 at 17:10 Comment(3)
JS was originally implemented as part of web browsers so that client-side scripts ...Rome
You should check this title by O'ReillyFidole
This should be the accepted answer @AnttiHaapalaRufusrug
P
29

No. Not really. I mean, you sort of could by writing something that compiles Javascript into C, but that would be pretty crazy. Bit like trying to use a spoon as a chainsaw.

Learn C. That is the right tool for the job.

Perfectionist answered 22/8, 2012 at 17:12 Comment(4)
What about the many user-mode driver frameworks that exist on various platforms? :-)Saied
@JasonMalinowski by far the easiest way to do itHandal
I'm now tempted to write a filesystem driver on top of FUSE just because I can! Atwood's Law FTW!Saied
You still wouldn't use Javascript with UMDF as far as I'm aware. Personally, I like JS, but this is a case where it's better to find the right tool that to try and use a different tool in it's place.Perfectionist
H
5

Oh, gosh. Writing a driver in js? Why? I mean, you could write a javascript wrapper for something in C or C++, maybe, but why would you want to do that? Device drivers communicate with the machine at a fairly low level (hardware level). Javascript doesn't. Javascript is a web language (well, mostly).

As Rich Bradshaw said, it's like using a spoon as a chainsaw. Though to me it'd be more like trying to use a canoe as a tank.

Handal answered 22/8, 2012 at 17:17 Comment(1)
but we can make a program that turns javascript into driver codes right?Clari
G
3

Wow, this idea is non-sense, IMHO you pick a programming language to solve a problem or task and not the other way around. I work with device drivers and OS kernel related stuff, but just because I can program in C I don't use C to do other task such as Linux administration for my embedded device; instead, I use something high level such as Bash, Perl or Python (depending on my mood :)).

Why are you interested in js? Actually, you should understand the internals of the programming language to know what are you trying to achieve and also you need to know how you program will interact with you OS to communicate with device registers and interrupts among other things.

Grapheme answered 24/8, 2012 at 5:30 Comment(0)
K
3

As silly as it sounds, this is now being done for various IOT devices. But in all cases i have seen, the device itself includes a modified version of V8 JS engine. The Mozilla phone exposes a HW access interface, but again it's not really a "real" device driver, but rather a skeleton API being exposed to JS.

I would urge you to learn Object Pascal or C/C++ since they are the only true "real" languages suitable for this type of work. Traditionally C is the most used language, but C and Pascal is essentially the same thing with different syntax. C++ builder and Object Pascal even share the same codegen, with different parser/lexer on top.

Having said that, there is no real reason why some custom drivers can be coded in NodeJS. Under Linux a lot of HW middleware is first written in Python, only to be finalized in C. So everything is possible, as long as someone has adapted the runtime regarding access to the hardware. FreePascal and Python makes GPIO access on the Raspberry PI 1-2 a snap. But there can be no doubt that real languages, like C/C++ and Object Pascal has the upper hand.

With "real" meaning compiled to machine code for the platform, and unreal refering to script engines like python and javascript.

Khmer answered 28/2, 2016 at 1:22 Comment(7)
I would like to know where this notion that stuff for Linux is first written in Python and finalized in C comes from. I have done some kernel development myself and know many people who do it on daily basis for a living. I think they would have hard time stopping laughing if presented with this idea.Anyhow
Same goes for Pascal and C++Anyhow
And where do i write that "kernel stuff" as you put it igor, is written in python? Perhaps when you are finished acting like a child you can read the text rather than make stuff up? If you are targeting the GPIO pins on the PI which is used to control more complex hardware, its faster to experiment with python and get the IO right. Once that is done you want more speed than python can master (depending on payload and device), and use freepascal or C to write the final piece. You clearly know little about the code quality of FPC if your response is as childish as you write above.Khmer
And I hate to tell you but, one of the most popular embedded kernels is written in object pascal. You seriously dont think that linux is the only game in town? Right now, i have a ton of friends working in the embedded field that would have a hard time stopping laughing at your limited view of low-level work. Its typical, people only see their own little world. But on stack-overflow you should at least be prepared to learn something newKhmer
Uhmmm, you do know that it's ok to attack ideas, but one should not extend that to people, right? Well, now you do. I hope you will keep that in mind and please try to not get so emotional. It doesn't make you look particularly good. You seem to confuse "using" a GPIO through an existing kernel API with writing a real device driver. Yes, you can play with some simple API, but in the end you will be subject to unnecessary context switch, have to deal with preemption and many other issues coming from this bright idea.Anyhow
WRT FPC, if you think you have a point, pleas bring objective data. "one of the most popular embedded kernels" Since when popularity is a measure of quality? Widows for POS is probably far more popular, if we go by that criteria.Anyhow
Igor, popularity is not the measure of quality, but rather a measure of flexibility and usability. As for personal level insults, read over your initial reply once more and see if you can spot where the seed of that came from. My point is this: there are many ways to solve a problem, and in some cases it might be using a tool you are not acustomed to. That goes for me, you and everyone. I have no problems using python as a solution when it is clearly the better option, nor nodeJs for that matter. What i reacted to was the lack of open-ness towards other languages etc.Khmer
P
0

Any language can be used to write device driver, provided a few condition have to be satisfied:

  1. Direct memory access. Look at this source code:

https://patchwork.kernel.org/patch/8163061/

As device driver you may be accessing the virtual memory or physical memory directly (in the case of DMA), and thus bypassing the virtual memory setup by the MMU is needed. Direct accessing the virtual memory means you know the virtual address, and want to read the address directly.

Java or Javascript does not have any language construct to read memory via known address directly.

  1. Assembly language sensitive tasks: Access the hardware very often need special assembly instruction, like disabling the interrupt, or switching from one CPU to another, or broadcasting inter-CPU messages etc. There is no Java construct to do all these, perhaps not even C language. So which is why combining C + assembly is often needed. But there is no way to combine Java and assembly.

  2. Native vs interpreted language: All intepreted language will have to go through a intepreter to execute the language. In Javascript or Java, you need JVM to execute the Java. So if you need Java in the kernel, then you will need a JVM intepreter in the kernel. This is not impossible - recent Linux kernel have a BPF intepreter running in the kernel, so you have a BPF VM running in the kernel:

https://events.linuxfoundation.org/sites/events/files/slides/bpf_collabsummit_2015feb20.pdf

https://lwn.net/Articles/599755/

The idea of Java as a device driver HAS been implemented before, as a research paper/project (for Sun Solaris OS):

http://dl.acm.org/citation.cfm?id=1215998

http://www.c0t0d0s0.org/archives/2587-Device-driver-in-Java.html

But I am not sure how the problem of directly memory access is solved.

Still, it is always possible to design a system whereby part of the tasks can be done by a low level module, which is C/Assembly dependent, and other component which can be written in non-C language, as shown in this recent paper (Usenix 2009):

https://www.usenix.org/legacy/event/usenix09/tech/full_papers/renzelmann/renzelmann_html/

See the diagram below:

Click to see picture

Putty answered 3/8, 2017 at 7:31 Comment(0)
P
0

We faced a similar problem, we needed to access hardware throught our online platform and show it live so our solution was to buy an adapter which gives ip to the hardware port so we are able to speak with it using node.js maybe you can find a similar solution

Puerperium answered 3/8, 2017 at 7:34 Comment(0)
M
0

I support you dude. You should be free to write that device driver in JavaScript. You can then use a tool like https://github.com/andrei-markeev/ts2c to convert it to C.

Mariner answered 26/8, 2021 at 20:25 Comment(0)
W
0

You could probably do something like that by implementing the WebUSB API. It's used to understand usb devices without the required driver. Fully implementable in Chrome

Winny answered 2/3, 2023 at 17:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.