From c459901f263b19d13466c47fcb8e2dce828ab59c Mon Sep 17 00:00:00 2001 From: px4dev Date: Thu, 11 Jul 2013 23:52:36 -0700 Subject: [PATCH] Let's have some direct-access I/O methods as well. --- src/drivers/device/cdev.cpp | 35 +++++++++++++++++++++------ src/drivers/device/device.h | 47 +++++++++++++++++++++++++++++++++---- 2 files changed, 70 insertions(+), 12 deletions(-) diff --git a/src/drivers/device/cdev.cpp b/src/drivers/device/cdev.cpp index 422321850a..dcbac25e30 100644 --- a/src/drivers/device/cdev.cpp +++ b/src/drivers/device/cdev.cpp @@ -111,21 +111,21 @@ CDev::~CDev() int CDev::init() { - int ret = OK; - // base class init first - ret = Device::init(); + int ret = Device::init(); if (ret != OK) goto out; // now register the driver - ret = register_driver(_devname, &fops, 0666, (void *)this); + if (_devname != nullptr) { + ret = register_driver(_devname, &fops, 0666, (void *)this); - if (ret != OK) - goto out; + if (ret != OK) + goto out; - _registered = true; + _registered = true; + } out: return ret; @@ -395,4 +395,25 @@ cdev_poll(struct file *filp, struct pollfd *fds, bool setup) return cdev->poll(filp, fds, setup); } +int +CDev::read(unsigned offset, void *data, unsigned count) +{ + errno = ENODEV; + return -1; +} + +int +CDev::write(unsigned offset, void *data, unsigned count) +{ + errno = ENODEV; + return -1; +} + +int +CDev::ioctl(unsigned operation, unsigned &arg) +{ + errno = ENODEV; + return -1; +} + } // namespace device \ No newline at end of file diff --git a/src/drivers/device/device.h b/src/drivers/device/device.h index 7d375aab91..2cac86636d 100644 --- a/src/drivers/device/device.h +++ b/src/drivers/device/device.h @@ -85,7 +85,7 @@ protected: */ Device(const char *name, int irq = 0); - ~Device(); + virtual ~Device(); /** * Initialise the driver and make it ready for use. @@ -189,7 +189,7 @@ public: /** * Destructor */ - ~CDev(); + virtual ~CDev(); virtual int init(); @@ -287,6 +287,43 @@ public: */ bool is_open() { return _open_count > 0; } + /* + * Direct access methods. + */ + + /** + * Read directly from the device. + * + * The actual size of each unit quantity is device-specific. + * + * @param offset The device offset at which to start reading + * @param data The buffer into which the read values should be placed. + * @param count The number of items to read, defaults to 1. + * @return count on success, < 0 on error. + */ + virtual int read(unsigned offset, void *data, unsigned count = 1); + + /** + * Write directly to the device. + * + * The actual size of each unit quantity is device-specific. + * + * @param address The device address at which to start writing. + * @param data The buffer from which values should be read. + * @param count The number of registers to write, defaults to 1. + * @return count on success, < 0 on error. + */ + virtual int write(unsigned address, void *data, unsigned count = 1); + + /** + * Perform a device-specific operation. + * + * @param operation The operation to perform + * @param arg An argument to the operation. + * @return < 0 on error + */ + virtual int ioctl(unsigned operation, unsigned &arg); + protected: /** * Pointer to the default cdev file operations table; useful for @@ -396,9 +433,9 @@ public: const char *devname, uint32_t base, int irq = 0); - ~PIO(); + virtual ~PIO(); - int init(); + virtual int init(); protected: @@ -407,7 +444,7 @@ protected: * * @param offset Register offset in bytes from the base address. */ - uint32_t reg(uint32_t offset) { + uint32_t reg(uint32_t offset) { return *(volatile uint32_t *)(_base + offset); }