Load USB Gadget Mass Storage module from C - c

Development platform: TI davinci processor with Linux OS
I am developing C program which receives user command over serial port and does the corresponding task. Basic task is to start and stop the video recording. And for retrieving the video to host pc from the embedded device, usb interface is used.
The partition used for storing the video file is loaded as USB gadget mass storage device using following kernel module for the user to save the video on host pc.
modprobe g_file_storage file=/root/usbstorage
But i want to load(or some otherway) this kernel module from the c function whenever user send a command to connect the embedded device to host. And disconnect whenever disconnect command is received.
As of now I'm able to load this module only from the shell.
Could any suggest any possible way of doing this from within the c-program.

Related

How to switch between the g_printer and g_serial USB driver without rebooting the whole Linux system?

I'm developing a project on an embedded Linux device in C, which uses those two USB drivers on the same USB cable:
g_printer(Linux USB Printer Gadget Driver)
g_serial(Linux Gadget Serial Driver)
When the user selects USB Virtual COM Mode, the program will:
Check whether the device file /dev/g_printer0 is opened, if yes, close the opened file descriptor.
Check whether the g_printer is installed by using system("lsmod | grep g_printer > /dev/null");, if yes, use system("rmmod g_printer); to uninstall it.
Check whether the usb_f_printer is installed by using system("lsmod | grep usb_f_printer > /dev/null");, if yes, use system("rmmod usb_f_printer); to uninstall it.
Install the g_serial by using system(modprobe g_serial);
Open device file /dev/ttyGS0
When the user selects USB Printer Mode, the program will do almost the same steps as above, but uninstall g_serial first and then install g_printer(also the closed and opened device files are different).
But the whole Linux system crashed at step 3 when it switches from USB Printer Mode to USB Virtual COM Mode, the log shows:
rmmod: can't unload module 'usb_f_printer': Resource temporarily unavailable
In addition, this Linux kernel is customized, after the system boots, the g_printer is automatically installed by the kernel, but the /dev/g_printer0 is not opened yet, so at this time, if I switch to USB Virtual COM Mode by using the steps above, it works with no errors(and the system won't crash of course), so it means I have another weird way to let the user switch between those two modes, that is when user wants to switch the mode, the system won't switch it immediately but save this selection in the configuration file, then it reboots the system, after the reboot, the program will check the configuration file to decide whether switches to USB Virtual COM Mode or just open /dev/g_printer0 to be ready for use. But this is too weird :(.
I can't kill this process because this is the only process running in this device, and also I can't exec another process to do this USB communication stuff because it's kind of wasting memory and computing resources.
So I have two questions:
Is there any way for the user to switch those two modes without rebooting the Linux system?
Does it means, once a process opens the /dev/g_printer0 device file, this driver can't be uninstalled but only reboot the system or kill the process? (even if I have already closed the opened device file)

Writing a task in VxWorks

I am trying to understand how can I write tasks for VxWorks. I have VxWorks on a board that is mounted on a development board.
I can access the board through Putty and a serial terminal connected to the UART of the system. How can I write other tasks (maybe to communicate with other UARTs or other Serial Interfaces) ?
And how do I know where to find the documentation describing how I can access those serial interfaces? (for example write/read methods, which file to include etc)*
Thank you
VxWorks is cross development environment, so you need a licensed development system on Windows or Linux desktop to create applications.
Then you load the compiled code on the target reference board as process/RTP or LKM/DKM. How you get it there varies by board: FTP, TFTP, removable USB or SD card....
At the C shell on booted system you can start a task in the kernel context with any public symbol. But if your working with deployed system, it shouldn't have the C shell, that's a serious security risk :)
Try..
-> sp printf, "\nHello VxWorks World!\n"

USB host hotplug detection in embedded usb gadget device

We are developing an embedded device running Linux, which needs to act as a usb serial gadget and connect to android phones for different types of data transfer. In order to initiate the communication with the host usb (android phone), I need to detect whenever the USB host is connected to the device. I came to know about writing udev rules in order to run an executable on usb host insertion/removal, but We already have a main C program that manages all of the device's main functionality. I would be running a thread in this program which would be responsible for this communication. Is there any way by which I can receive the USB hotplug event in this thread. I am really trying to avoid using loops in this thread so that it doesn't get CPU intensive. So is there any way by which I can receive the USB hotplug event?

Autoconfiguration on programmable Xbee modules

Non-programmable Xbee modules should be configured through a PC (with XCTU) or other devices like Arduino... but can the programmable xbee modules (like xbee-pro zb s2b) autoconfigure themselves, without being connected to another device like a PC or Arduino, by running code stored in their memory?
I mean, can they run orders like the ones you run through XCTU but programming them in the internal memory code? Like scan energy of every channel, select a channel, set a PAN ID, configure the different parameters of the device...
Thank you
Yes, the development kit includes an API for sending AT commands from the co-processor to the radio on those boards.
There's also a passthrough mode that relays the host computer serial port through to the radio processor, which can help with initial setup/configuration of the modules like you might do during manufacturing.
To answer your question:
I mean, can they run orders like the ones you run through XCTU but programming them in the internal memory code?
No. You can not program a sequence of orders/commands into the internal memory of the device. To do anything meaningful the device needs to be "driven" from a host PC or MCU that can send the AT Commands.
If you want 1 device solution that does not require a Host MCU then you will need to use a ZigBee SoC (System on Chip), such as the CC2538 - http://www.ti.com/product/cc2538 running a ZgBee SDK (Software Development Kit) - http://www.ti.com/tool/z-stack (ZStack-Home). However this will require you to develop the ZigBee application SW.
Regards,
TC.

How to write a simple usb driver for a hardware copyright protection dongle?

I want to create a USB driver, so my own C application to be able to get into my flash drive and take information from the imported flash drive.
OS: Windows
I start my app contains my USB driver and I plug-in my flash and its shows in Explorer that this drive is plugged and it is accessible, while that's happening, my app is checking that file data.txt EXIST if that file exist, program run next function, if not program, my program runs down/exit/!
If you are looking for a programmable USB dongle, I suggest inspecting this SO thread: Programmable USB dongles
The Windows Driver Kit should simplify your work or atleast help you at the start.

Resources