From 26eb0b9d724654bd87edd9191e72a726f5ce240b Mon Sep 17 00:00:00 2001 From: px4dev Date: Sat, 13 Jul 2013 21:00:03 -0700 Subject: [PATCH] =?UTF-8?q?Move=20the=20direct-access=20methods=20from=20C?= =?UTF-8?q?Dev=20to=20Device=E2=80=A6=20makes=20more=20sense=20that=20way.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/drivers/device/cdev.cpp | 21 -------- src/drivers/device/device.cpp | 26 ++++++++++ src/drivers/device/device.h | 90 ++++++++++++++++++++--------------- src/drivers/device/i2c.h | 12 ++--- 4 files changed, 83 insertions(+), 66 deletions(-) diff --git a/src/drivers/device/cdev.cpp b/src/drivers/device/cdev.cpp index dcbac25e30..1972d2efcb 100644 --- a/src/drivers/device/cdev.cpp +++ b/src/drivers/device/cdev.cpp @@ -395,25 +395,4 @@ 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.cpp b/src/drivers/device/device.cpp index 04a5222c36..dd8074460e 100644 --- a/src/drivers/device/device.cpp +++ b/src/drivers/device/device.cpp @@ -223,5 +223,31 @@ interrupt(int irq, void *context) return OK; } +int +Device::probe() +{ + return -1; +} + +int +Device::read(unsigned offset, void *data, unsigned count) +{ + errno = ENODEV; + return -1; +} + +int +Device::write(unsigned offset, void *data, unsigned count) +{ + errno = ENODEV; + return -1; +} + +int +Device::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 2cac86636d..d69d1b2d6b 100644 --- a/src/drivers/device/device.h +++ b/src/drivers/device/device.h @@ -68,11 +68,62 @@ namespace device __EXPORT class __EXPORT Device { public: + /** + * Destructor. + * + * Public so that anonymous devices can be destroyed. + */ + virtual ~Device(); + /** * Interrupt handler. */ virtual void interrupt(void *ctx); /**< interrupt handler */ + /* + * Direct access methods. + */ + + /** + * Probe to test whether the device is present. + * + * @return Zero if present, < 0 (error) otherwise. + */ + virtual int probe(); + + /** + * Read directly from the device. + * + * The actual size of each unit quantity is device-specific. + * + * @param offset The device address 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 address, 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: const char *_name; /**< driver name */ bool _debug_enabled; /**< if true, debug messages are printed */ @@ -85,7 +136,6 @@ protected: */ Device(const char *name, int irq = 0); - virtual ~Device(); /** * Initialise the driver and make it ready for use. @@ -282,48 +332,12 @@ public: * Test whether the device is currently open. * * This can be used to avoid tearing down a device that is still active. + * Note - not virtual, cannot be overridden by a subclass. * * @return True if the device is currently open. */ 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 diff --git a/src/drivers/device/i2c.h b/src/drivers/device/i2c.h index b4a9cdd536..8c4fab4c3e 100644 --- a/src/drivers/device/i2c.h +++ b/src/drivers/device/i2c.h @@ -55,13 +55,11 @@ class __EXPORT I2C : public CDev public: - /** - * Get the address - */ - uint16_t get_address() { - return _address; - } - + /** + * Get the address + */ + int16_t get_address() { return _address; } + protected: /** * The number of times a read or write operation will be retried on