Книга: Embedded Linux Primer: A Practical, Real-World Approach

8.4. Bringing It All Together

8.4. Bringing It All Together

Now that we have a skeletal device driver, we can load it and exercise it. Listing 8-11 is a simple user space application that exercises our device driver. We've already seen how to load the driver. Simply compile it and issue the make modules_install command to place it on your file system, as previously described.

Listing 8-11. Exercising Our Device Driver

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, char **argv) {
 /* Our file descriptor */
 int fd;
 int rc = 0;
 char *rd_buf[16];
 printf("%s: enteredn", argv[0]);
 /* Open the device */
 fd = open("/dev/hello1", O_RDWR);
 if (fd == -1) {
  perror("open failed");
  rc = fd;
  exit(-1);
 }
 printf("%s: open: successfuln", argv[0]);
 /* Issue a read */
 rc = read(fd, rd_buf, 0);
 if (rc == -1) {
  perror("read failed");
  close(fd);
  exit(-1);
 }
 printf("%s: read: returning %d bytes!n", argv[0], rc);
 close(fd);
 return 0;

This simple file, compiled on an ARM XScale system, demonstrates the binding of application to device driver, through the device node. Like the device driver, it doesn't do any useful work, but it does demonstrate the concepts as it exercises some of the methods we introduced in the device driver of Listing 8-10.

First we issue an open() system call[67] on our device node created earlier. If the open succeeds, we indicate that with a message to the console. Next we issue a read() command and again print a message to the console on success. Notice that a read of 0 bytes is perfectly acceptable as far as the kernel is concerned and, in actual practice, indicates an end-of-file or out-of-data condition. Your device driver defines that special condition. When complete, we simply close the file and exit. Listing 8-12 captures the output of running this example application on an ARM XScale target:

Listing 8-12. Using the Example Driver

$ modprobe hello1
Hello Example Init - debug mode is disabled
Hello: registered module successfully!
$ ./use-hello
./use-hello: entered
./use-hello: open: successful
./use-hello: read: returning zero bytes!
$
 

Оглавление книги


Генерация: 2.429. Запросов К БД/Cache: 3 / 1
поделиться
Вверх Вниз