Monday, February 14, 2011

Migration from Windows to Linux device control applications

From the device on and off by explaining, we need two parameters (file pathname, and device access mode) to open a Linux device.

According to the front of the original code, the first argument should be the/dev/hda, second O_RDONLY | O_NONBLOCK. The modified code is as follows: HANDLEdevHandle = open ("/dev/hda", O_RDONLY | O_NONBLOCK);. Corresponding to CloseHandle (devHandle); changes to close (devHandle);. The main part of the transplantations is how to use ioctl to access specific devices and obtain the required information. The original Windows code as shown in Listing 6: inventory of the source code on the DeviceIoControl 6.Windows typedefstruct_Bufer {UCHARreq [8];//DetailedcommandinformationothertancontrolcodeULONGDataBufferSize;//SizeofDataBuffer, hereis512UCHARDataBuffer [512];//DataBufer} Bufer; BufferregBufer; memcpy (regBuffer.req, cmdBuff, 7);//req [7] isreservedforfutureuse.Mustbezero.regBuffer.DataBufferSize = 512; unsignedintsize = 512 + 12;//SizeofregBufer//8forreq, 4forDataBufferSize, 512fordataDWORDbytesRet = 0;//Numberofbytesreturnedintretval;//Returnedvalueretval = DeviceIoControl (devHandle, IOCTL_IDE_PASS_THROUGH,//ControlcoderegBuffer,//InputBuffer, includingdetailedcommandsize, regBuffer,//OutputBuffer, usethesamebuffereresize, & bytesRet, NULL); if (! retval) cout <"deviceiocontrolfailed."> The device handle in the two platforms is the first argument, it from CreateFile and Linux open () returns. But Windows control codes and Linux on the definition of requests vary greatly, so that there is no fixed rule to identify these two parameters are mapped, as indicated above. IOCTL_IDE_PASS_THROUGH is defined in the header file ntddscsi.h as CTL_CODE (IOCTL_SCSI_BASE, 0x040a, METHOD_BUFFERED, FILE_READ_ACCESS | FILE_WRITE_ACCESS). Passed in the header file to find the definition,/usr/include/linux/hdreg.h can choose the corresponding Linux control code HDIO_DRIVE_CMD. In addition, the device to complete specific tasks require detailed command information. The command stored in the cache, and returns the data of the memory space in the exchange of data in the process. We use the same caching to send commands and gets the required log information. Linux cache size can be changed; not necessarily out of eight bytes. This example uses the order of four bytes. The corresponding Linux code (listing 7) looks simple, because its structure and function parameters than Windows is simple. List of source code 7.Linux function ioctl intretval; unsignedcharreq [4 + 512];//Enoughforreturneddataandthe4bytedetailedcommandinformationreq [0] = cmdBuff [6];//Considertherequirementinthissample, only4bytesareusedreq [1] = cmdBuf [2]; req [2] = cmdBuf [0]; req [3] = cmdBuf [1]; retval = ioctl (devHandle, HDIO_DRIVE_CMD, & req); if (ret) cout <"ioctlfailed."> The task now is to compile a Linux platform for the program and correct the remaining syntax errors. According to the Linux version and the build environment, you may need another modification.

No comments:

Post a Comment