ldd3 驅動--測試scull

來源:互聯網
上載者:User

scull驅動類比裝置。編寫讀寫裝置的控制檔案testscull.c向scull發送和接收資料。

1 安裝scull.ko

  在ldd3驅動源碼檔案夾中,進入scull檔案夾,make,產生scull.ko,運行其中的shell檔案:scull_load,完成scull.ko的安裝。

  # cd /sys/module/scull/parameters/

  #cat scull_major

  254

  #cat scull_minor

  0

  可知scull.ko的主次裝置號為254 0

  進入/dev 建立裝置檔案:

  #cd /dev

  #mknod sculldev c 254 0

2 編寫測試檔案testscull.c

  代碼:

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<error.h>
#include<fcntl.h>
#include<sys/types.h>
int main()
{
  int fd,len;
  char inbuf[20];
  char outbuf[20]="scull dev test!";
  fd=open("/dev/sculldev",O_WRONLY);
  if(fd<0)
  {printf("Error openning the device of sculldev for writing!/n");
  exit(1);
  }
  len=write(fd,outbuf,strlen(outbuf)+1);
  if(len<0)
    { printf("Error writing to the device!/n");
      close(fd);
      exit(1);
   
    }
  printf("writing %d bytes to the device!/n",len);
  close(fd);
  fd=open("/dev/sculldev",O_RDONLY);
  if(fd<0)
  {
    printf("Error openning the device of sculldev for reading!/n");
    exit(1);
  }
  len=read(fd,inbuf,len);
  if(len<0)
    {printf("Error reading from the device!/n ");
     close(fd);
     exit(1);
    }
  printf("reading %d bytes from the device!/n",len);
  printf("%s/n",inbuf);

}

3 測試

#gcc testscull.c -o testscull

#./testscull

 

 

經過對scull_load的分析,其中包含了裝置檔案建立的代碼,所以不用自己建立裝置檔案

#!/bin/sh
module="scull"
device="scull"
mode="664"
# invoke insmod with all arguments we got
# and use a pathname, as newer modutils don't look in . by default
/sbin/insmod ./$module.ko $* || exit 1
# remove stale nodes
rm -f /dev/${device}[0-3]
major=$(awk "//$2= =/"$module/" {print //$1}" /proc/devices)
mknod /dev/${device}0 c $major 0
mknod /dev/${device}1 c $major 1
mknod /dev/${device}2 c $major 2
mknod /dev/${device}3 c $major 3
# give appropriate group/permissions, and change the group.
# Not all distributions have staff, some have "wheel" instead.
group="staff"
grep -q '^staff:' /etc/group || group="wheel"
chgrp $group /dev/${device}[0-3]
chmod $mode  /dev/${device}[0-3]

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.