カスタムボードのLEDを制御しようとしています。デバイスドライバを作成し、正常にロードしました。/devディレクトリに正しいメジャー番号のデバイスファイルを作成しました。次に、ボード上のLEDを制御するプログラムをユーザープレーンで作成しましたが、ioctlがエラー番号2(そのようなファイルまたはディレクトリがない)で失敗し、返されるファイル記述子は3です。以下はユーザースペースのアプリケーションです。
int main(int argc, char **argv)
{
struct s_LEDControl sLedControlParam;
int ledNumber;
int command;
int fDDevice;
int ioctlReturn;
ledNumber = atoi(argv[1]);
command = atoi(argv[2]);
/* Prepare the message to be sent to Kernel Space */
sLedControlParam.led = ledNumber;
sLedControlParam.cmd = command;
/* Open the device */
fDDevice = open("/dev/newdevice_drv", O_RDWR);
if(fDDevice == -1)
{
printf("ERROR: Cannot open the device /dev/newdevice_drv\n");
exit(0);
}
ioctlReturn = ioctl(fDDevice, c_controlLED1, &sLedControlParam);
if(ioctlReturn != 0)
{
printf("ERROR: ioctl failed Reason:%s FD:%d\n", strerror(errno), fDDevice);
}
close(fDDevice);
return 0;
}
ファイル記述子に問題がないかどうかを確認するために、ダミーのファイル記述子をioctlに渡して、正しいエラー番号を取得しました。私はLinuxとデバイスドライバを初めて使用します。助けていただければ幸いです。
上記のCプログラムはPPC環境にクロスコンパイルされています。
以下はドライバーコードの一部です
MODULE_LICENSE("XXX");
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_SUPPORTED_DEVICE("XXXX");
/** Assign device functions to file_operation structure */
struct file_operations fops= {
.owner=THIS_MODULE,
.open=device_open,
.release=device_release,
.read=device_read,
.write=device_write,
.unlocked_ioctl=device_ioctl,
.llseek=NULL,
};
static int device_ioctl( struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)
{
enum e_FPGACommand e_cmd;
e_cmd = (enum e_FPGACommand)cmd;
printk("DEBUG: device_ioctl entered\n");
switch(e_cmd)
{
case c_controlLED1: controlLED1_ioctl(arg);
break;
default: printk("ERROR: Invalid command %d\n", e_cmd);
return INVALID_IOCTL;
break;
}
printk("DEBUG: device_ioctl exited\n");
return OK;
}
static int controlLED1_ioctl(unsigned int arg)
{
struct s_LEDControl *sLedControlParam;
int result;
sLedControlParam = (struct s_LEDControl *)kmalloc(sizeof(struct s_LEDControl), GFP_KERNEL);
if(sLedControlParam == GLO_NULL)
{
printk("ERROR: Memory allocation failed in controlLED1_ioctl\n");
return FAILURE_MEMORY_ALLOCATION;
}
result = copy_from_user(sLedControlParam, (struct s_LEDControl *)arg, sizeof(struct s_LEDControl));
if(result > 0)
{
printk("ERROR: copy_from_user in controlLED1_ioctl returned unfinished bytes\n");
kfree(sLedControlParam);
return FAILURE_COPY_FROM_USER;
}
if(sLedControlParam->cmd == On)
{
/* On - Do something */
}
return OK;
}
エラーメッセージが表示されません。デバイスドライバがロードされると、すべてのプリントが表示されます。アプリケーションを実行しようとすると、デバイスが開かれ、すぐに閉じられたことがわかりますが、ioctl関数からの出力は表示されません。