by hermvale
Published: November 20, 2022 (6 days ago)

Linux Device Drivers 3rd Edition Mobi

Download ☆☆☆☆☆ DOWNLOAD


Linux Device Drivers 3rd Edition Mobi

the second thing access_ok() checks is if the buffer has been initialised with no ptr_err set; if it’s not, it sets one. even if we can dereference the pointer, we should still throw in a ptr_err, just in case the code was hoping for something else but got a segfault instead. for example, by checking that the pointer is non-null, we can satisfy the compiler and the kernel that we may have control over that pointer.

the final check is another simple bit; if the pointer has been initialised but not zeroed, it sets the ptr_err flag. this is because the kernel expects a pointer to be zeroed in order to check its validity. if a pointer isn’t zeroed, then we’re hosed, so we should signal this with a ptr_err and print a nice message to say that we had a bad pointer.

in this driver, we’ll deal with the miscclass; this will be interesting, since the miscclass doesn’t usually appear on character devices. in fact, it’s the fdentry of theopenmethod that determines that we are dealing with a characterdevice. the system call is invoked using ioctl(), a system call that actually bridges between kernel code and the unixdomain of programming (don’t panic, we’ll cover that in the unix domainsection in the next chapter).

unlike character devices, the miscclass driver is very, very special. there’s no concept of ‘device drivers’ (that is, user-space processes and the kernel, through device nodes, interface directly with devices via the filesystem); rather, a driver, for the miscclass, is an abstract concept. the bulk of the driver logic is placed inside a special, kernel-space symbol called the struct file_operations data structure. this data structure has a set of methods associated with it. these methods are called upon by the kernel when it needs to ‘handle’ a device (a dev, on a character device).

note: note that this is just the hid class driver! i don’t know how to explain this – but, even better, you can go to the for the complete kernel coverage of the device drivers ; this is a professional-level, comprehensive site!
so, what we are doing is that we first check if the user-mode process’s buffer (pointed to the struct inode of the device in question) is zero-filled. if this is the case, our driver sets the device’s (we only support one device so far) flags argument as beingif_no_buffer – meaning that it isn’t interested in the buffer and can provide the data; effectively, this bit tells the user-mode to not copy any data into its buffer. this is very crucial as without it, its buffer may be corrupted. at this stage, we needn’t bother doing much more than setting the dev member of our driver structure. so, we let the kernel worry about it! (for now!)
the read(), write(), and ioctl() operation are used to perform various vfs operations on the device. as we are merely performing the minimum vfs layer functionality on our device (and otherwise not doing much), we invoke the read() or write() vfs methods with our f_op pointer set to null. this will trigger the nonseekable_open() api described earlier.
you can call file_mmap() in the vfs layer to map your device, if you wish to do so. (it’s available via the snd_pcm_new and libc apis, respectively). note that this api performs an access_ok check; if this call fails, you can also code yourfile_mmap api to print an error message, which you can do via printk (again, thanks to the print_ctx macro).