4 * Home page of code is: http://smartmontools.sourceforge.net
6 * Copyright (C) 2008-12 Christian Franke <smartmontools-support@lists.sourceforge.net>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2, or (at your option)
13 * You should have received a copy of the GNU General Public License
14 * (for example COPYING); If not, see <http://www.gnu.org/licenses/>.
18 #ifndef DEV_INTERFACE_H
19 #define DEV_INTERFACE_H
21 #define DEV_INTERFACE_H_CVSID "$Id: dev_interface.h 3524 2012-03-21 22:19:31Z chrfranke $\n"
29 /////////////////////////////////////////////////////////////////////////////
30 // Common functionality for all device types
32 // Forward declarations
33 class smart_interface
;
37 /// Base class for all devices
42 /// Device info strings
46 device_info(const char * d_name
, const char * d_type
, const char * r_type
)
47 : dev_name(d_name
), info_name(d_name
),
48 dev_type(d_type
), req_type(r_type
)
51 std::string dev_name
; ///< Device (path)name
52 std::string info_name
; ///< Informal name
53 std::string dev_type
; ///< Actual device type
54 std::string req_type
; ///< Device type requested by user, empty if none
57 /// Error (number,message) pair
59 explicit error_info(int n
= 0)
61 error_info(int n
, const char * m
)
64 { no
= 0; msg
.erase(); }
66 int no
; ///< Error number
67 std::string msg
; ///< Error message
72 /// Constructor to init interface and device info.
73 /// Must be called in implementation classes.
74 smart_device(smart_interface
* intf
, const char * dev_name
,
75 const char * dev_type
, const char * req_type
);
77 /// Dummy enum for dummy constructor.
78 enum do_not_use_in_implementation_classes
{ never_called
};
79 /// Dummy constructor for abstract classes.
80 /// Must never be called in implementation classes.
81 smart_device(do_not_use_in_implementation_classes
);
84 virtual ~smart_device() throw();
88 ///////////////////////////////////////////////
89 // Dynamic downcasts to actual device flavor
91 /// Return true if ATA device
93 { return !!m_ata_ptr
; }
94 /// Return true if SCSI device
96 { return !!m_scsi_ptr
; }
98 /// Downcast to ATA device.
100 { return m_ata_ptr
; }
101 /// Downcast to ATA device (const).
102 const ata_device
* to_ata() const
103 { return m_ata_ptr
; }
104 /// Downcast to SCSI device.
105 scsi_device
* to_scsi()
106 { return m_scsi_ptr
; }
107 /// Downcast to SCSI device (const).
108 const scsi_device
* to_scsi() const
109 { return m_scsi_ptr
; }
111 ///////////////////////////////////////////////
112 // Device information
114 /// Get device info struct.
115 const device_info
& get_info() const
118 /// Get device (path)name.
119 const char * get_dev_name() const
120 { return m_info
.dev_name
.c_str(); }
121 /// Get informal name.
122 const char * get_info_name() const
123 { return m_info
.info_name
.c_str(); }
125 const char * get_dev_type() const
126 { return m_info
.dev_type
.c_str(); }
127 /// Get type requested by user, empty if none.
128 const char * get_req_type() const
129 { return m_info
.req_type
.c_str(); }
132 /// R/W access to device info struct.
133 device_info
& set_info()
137 ///////////////////////////////////////////////
138 // Last error information
140 /// Get last error info struct.
141 const error_info
& get_err() const
143 /// Get last error number.
144 int get_errno() const
146 /// Get last error message.
147 const char * get_errmsg() const
148 { return m_err
.msg
.c_str(); }
150 /// Set last error number and message.
151 /// Printf()-like formatting is supported.
152 /// Returns false always to allow use as a return expression.
153 bool set_err(int no
, const char * msg
, ...)
154 __attribute_format_printf(3, 4);
156 /// Set last error info struct.
157 bool set_err(const error_info
& err
)
158 { m_err
= err
; return false; }
160 /// Clear last error info.
164 /// Set last error number and default message.
165 /// Message is retrieved from interface's get_msg_for_errno(no).
166 bool set_err(int no
);
170 ///////////////////////////////////////////////
172 // Must be implemented in derived class
174 /// Return true if device is open.
175 virtual bool is_open() const = 0;
177 /// Open device, return false on error.
178 virtual bool open() = 0;
180 /// Close device, return false on error.
181 virtual bool close() = 0;
183 /// Open device with autodetection support.
184 /// May return another device for further access.
185 /// In this case, the original pointer is no longer valid.
186 /// Default Implementation calls 'open()' and returns 'this'.
187 virtual smart_device
* autodetect_open();
189 ///////////////////////////////////////////////
190 // Support for tunnelled devices
192 /// Return true if other device is owned by this device.
193 /// Default implementation returns false.
194 virtual bool owns(const smart_device
* dev
) const;
196 /// Release ownership of other device.
197 /// Default implementation does nothing.
198 virtual void release(const smart_device
* dev
);
201 /// Get interface which produced this object.
202 smart_interface
* smi()
204 /// Get interface which produced this object (const).
205 const smart_interface
* smi() const
210 smart_interface
* m_intf
;
214 // Pointers for to_ata(), to_scsi(),
215 // set by ATA/SCSI interface classes.
216 friend class ata_device
;
217 ata_device
* m_ata_ptr
;
218 friend class scsi_device
;
219 scsi_device
* m_scsi_ptr
;
221 // Prevent copy/assigment
222 smart_device(const smart_device
&);
223 void operator=(const smart_device
&);
227 /////////////////////////////////////////////////////////////////////////////
228 // ATA specific interface
230 /// ATA register value and info whether it has ever been set
231 // (Automatically set by first assignment)
236 : m_val(0x00), m_is_set(false) { }
238 ata_register
& operator=(unsigned char x
)
239 { m_val
= x
; m_is_set
= true; return * this; }
241 unsigned char val() const
243 operator unsigned char() const
250 unsigned char m_val
; ///< Register value
251 bool m_is_set
; ///< true if set
254 /// ATA Input registers (for 28-bit commands)
257 // ATA-6/7 register names // ATA-3/4/5 // ATA-8
258 ata_register features
; // features // features
259 ata_register sector_count
; // sector count // count
260 ata_register lba_low
; // sector number // ]
261 ata_register lba_mid
; // cylinder low // ] lba
262 ata_register lba_high
; // cylinder high // ]
263 ata_register device
; // device/head // device
264 ata_register command
; // command // command
266 /// Return true if any register is set
268 { return (features
.is_set() || sector_count
.is_set()
269 || lba_low
.is_set() || lba_mid
.is_set() || lba_high
.is_set()
270 || device
.is_set() || command
.is_set()); }
273 /// ATA Output registers (for 28-bit commands)
277 ata_register sector_count
;
278 ata_register lba_low
;
279 ata_register lba_mid
;
280 ata_register lba_high
;
284 /// Return true if any register is set
286 { return (error
.is_set() || sector_count
.is_set()
287 || lba_low
.is_set() || lba_mid
.is_set() || lba_high
.is_set()
288 || device
.is_set() || status
.is_set()); }
292 /// 16-bit alias to a 8-bit ATA register pair.
293 class ata_reg_alias_16
296 ata_reg_alias_16(ata_register
& lo
, ata_register
& hi
)
297 : m_lo(lo
), m_hi(hi
) { }
299 ata_reg_alias_16
& operator=(unsigned short x
)
300 { m_lo
= (unsigned char) x
;
301 m_hi
= (unsigned char)(x
>> 8);
304 unsigned short val() const
305 { return m_lo
| (m_hi
<< 8); }
306 operator unsigned short() const
307 { return m_lo
| (m_hi
<< 8); }
310 ata_register
& m_lo
, & m_hi
;
312 // References must not be copied.
313 ata_reg_alias_16(const ata_reg_alias_16
&);
314 void operator=(const ata_reg_alias_16
&);
318 /// 48-bit alias to six 8-bit ATA registers (for LBA).
319 class ata_reg_alias_48
322 ata_reg_alias_48(ata_register
& ll
, ata_register
& lm
, ata_register
& lh
,
323 ata_register
& hl
, ata_register
& hm
, ata_register
& hh
)
324 : m_ll(ll
), m_lm(lm
), m_lh(lh
),
325 m_hl(hl
), m_hm(hm
), m_hh(hh
)
328 ata_reg_alias_48
& operator=(uint64_t x
)
330 m_ll
= (unsigned char) x
;
331 m_lm
= (unsigned char)(x
>> 8);
332 m_lh
= (unsigned char)(x
>> 16);
333 m_hl
= (unsigned char)(x
>> 24);
334 m_hm
= (unsigned char)(x
>> 32);
335 m_hh
= (unsigned char)(x
>> 40);
341 return ( (unsigned)m_ll
342 | ((unsigned)m_lm
<< 8)
343 | ((unsigned)m_lh
<< 16)
344 | ((unsigned)m_hl
<< 24)
345 | ((uint64_t)m_hm
<< 32)
346 | ((uint64_t)m_hh
<< 40));
349 operator uint64_t() const
353 ata_register
& m_ll
, & m_lm
, & m_lh
,
354 & m_hl
, & m_hm
, & m_hh
;
356 // References must not be copied.
357 ata_reg_alias_48(const ata_reg_alias_48
&);
358 void operator=(const ata_reg_alias_48
&);
362 /// ATA Input registers for 48-bit commands
363 // See section 4.14 of T13/1532D Volume 1 Revision 4b
365 // Uses ATA-6/7 method to specify 16-bit registers as
366 // recent (low byte) and previous (high byte) content of
369 // (ATA-8 ACS does not longer follow this scheme, it uses
370 // abstract registers with sufficient size and leaves the
371 // actual mapping to the transport layer.)
373 struct ata_in_regs_48bit
374 : public ata_in_regs
// "most recently written" registers
376 ata_in_regs prev
; ///< "previous content"
378 // 16-bit aliases for above pair.
379 ata_reg_alias_16 features_16
;
380 ata_reg_alias_16 sector_count_16
;
381 ata_reg_alias_16 lba_low_16
;
382 ata_reg_alias_16 lba_mid_16
;
383 ata_reg_alias_16 lba_high_16
;
385 // 48-bit alias to all 8-bit LBA registers.
386 ata_reg_alias_48 lba_48
;
388 /// Return true if 48-bit command
389 bool is_48bit_cmd() const
390 { return prev
.is_set(); }
392 /// Return true if 48-bit command with any nonzero high byte
393 bool is_real_48bit_cmd() const
394 { return ( prev
.features
|| prev
.sector_count
395 || prev
.lba_low
|| prev
.lba_mid
|| prev
.lba_high
); }
401 /// ATA Output registers for 48-bit commands
402 struct ata_out_regs_48bit
403 : public ata_out_regs
// read with HOB=0
405 ata_out_regs prev
; ///< read with HOB=1
407 // 16-bit aliases for above pair.
408 ata_reg_alias_16 sector_count_16
;
409 ata_reg_alias_16 lba_low_16
;
410 ata_reg_alias_16 lba_mid_16
;
411 ata_reg_alias_16 lba_high_16
;
413 // 48-bit alias to all 8-bit LBA registers.
414 ata_reg_alias_48 lba_48
;
416 ata_out_regs_48bit();
420 /// Flags for each ATA output register
421 struct ata_out_regs_flags
423 bool error
, sector_count
, lba_low
, lba_mid
, lba_high
, device
, status
;
425 /// Return true if any flag is set.
427 { return ( error
|| sector_count
|| lba_low
428 || lba_mid
|| lba_high
|| device
|| status
); }
430 /// Default constructor clears all flags.
432 : error(false), sector_count(false), lba_low(false), lba_mid(false),
433 lba_high(false), device(false), status(false) { }
437 /// ATA pass through input parameters
440 ata_in_regs_48bit in_regs
; ///< Input registers
441 ata_out_regs_flags out_needed
; ///< True if output register value needed
442 enum { no_data
= 0, data_in
, data_out
} direction
; ///< I/O direction
443 void * buffer
; ///< Pointer to data buffer
444 unsigned size
; ///< Size of buffer
446 /// Prepare for 28-bit DATA IN command
447 void set_data_in(void * buf
, unsigned nsectors
)
450 in_regs
.sector_count
= nsectors
;
452 size
= nsectors
* 512;
455 /// Prepare for 28-bit DATA OUT command
456 void set_data_out(const void * buf
, unsigned nsectors
)
458 buffer
= const_cast<void *>(buf
);
459 in_regs
.sector_count
= nsectors
;
460 direction
= data_out
;
461 size
= nsectors
* 512;
464 /// Prepare for 48-bit DATA IN command
465 void set_data_in_48bit(void * buf
, unsigned nsectors
)
468 // Note: This also sets 'in_regs.is_48bit_cmd()'
469 in_regs
.sector_count_16
= nsectors
;
471 size
= nsectors
* 512;
477 /// ATA pass through output parameters
480 ata_out_regs_48bit out_regs
; ///< Output registers
485 /// ATA device access
487 : virtual public /*extends*/ smart_device
490 /// ATA pass through.
491 /// Return false on error.
492 /// Must be implemented in derived class.
493 virtual bool ata_pass_through(const ata_cmd_in
& in
, ata_cmd_out
& out
) = 0;
495 /// ATA pass through without output registers.
496 /// Return false on error.
497 /// Calls ata_pass_through(in, dummy), cannot be reimplemented.
498 bool ata_pass_through(const ata_cmd_in
& in
);
500 /// Return true if OS caches ATA identify sector.
501 /// Default implementation returns false.
502 virtual bool ata_identify_is_cached() const;
505 /// Check command input parameters.
506 /// Calls set_err(...) accordingly.
507 bool ata_cmd_is_ok(const ata_cmd_in
& in
,
508 bool data_out_support
= false,
509 bool multi_sector_support
= false,
510 bool ata_48bit_support
= false);
512 /// Hide/unhide ATA interface.
513 void hide_ata(bool hide
= true)
514 { m_ata_ptr
= (!hide
? this : 0); }
516 /// Default constructor, registers device as ATA.
518 : smart_device(never_called
)
523 /////////////////////////////////////////////////////////////////////////////
524 // SCSI specific interface
528 /// SCSI device access
530 : virtual public /*extends*/ smart_device
533 /// SCSI pass through.
534 /// Returns false on error.
535 virtual bool scsi_pass_through(scsi_cmnd_io
* iop
) = 0;
538 /// Hide/unhide SCSI interface.
539 void hide_scsi(bool hide
= true)
540 { m_scsi_ptr
= (!hide
? this : 0); }
542 /// Default constructor, registers device as SCSI.
544 : smart_device(never_called
)
545 { hide_scsi(false); }
549 /////////////////////////////////////////////////////////////////////////////
550 /// Smart pointer class for device pointers
553 class any_device_auto_ptr
556 typedef Dev device_type
;
558 /// Construct from optional pointer to device
559 /// and optional pointer to base device.
560 explicit any_device_auto_ptr(device_type
* dev
= 0,
561 smart_device
* base_dev
= 0)
562 : m_dev(dev
), m_base_dev(base_dev
) { }
564 /// Destructor deletes device object.
565 ~any_device_auto_ptr() throw()
568 /// Assign a new pointer.
569 /// Throws if a pointer is already assigned.
570 void operator=(device_type
* dev
)
577 /// Delete device object and clear the pointer.
581 if (m_base_dev
&& m_dev
->owns(m_base_dev
))
582 m_dev
->release(m_base_dev
);
588 /// Return the pointer and release ownership.
589 device_type
* release()
591 device_type
* dev
= m_dev
;
596 /// Replace the pointer.
597 /// Used to call dev->autodetect_open().
598 void replace(device_type
* dev
)
601 /// Return the pointer.
602 device_type
* get() const
605 /// Pointer dereferencing.
606 device_type
& operator*() const
609 /// Pointer dereferencing.
610 device_type
* operator->() const
613 /// For (ptr != 0) check.
614 operator bool() const
617 /// For (ptr == 0) check.
618 bool operator !() const
623 smart_device
* m_base_dev
;
626 { throw std::logic_error("any_device_auto_ptr: wrong usage"); }
628 // Prevent copy/assignment
629 any_device_auto_ptr(const any_device_auto_ptr
<Dev
> &);
630 void operator=(const any_device_auto_ptr
<Dev
> &);
633 typedef any_device_auto_ptr
<smart_device
> smart_device_auto_ptr
;
634 typedef any_device_auto_ptr
<ata_device
> ata_device_auto_ptr
;
635 typedef any_device_auto_ptr
<scsi_device
> scsi_device_auto_ptr
;
638 /////////////////////////////////////////////////////////////////////////////
641 /// List of devices for DEVICESCAN
642 class smart_device_list
649 ~smart_device_list() throw()
651 for (unsigned i
= 0; i
< m_list
.size(); i
++)
656 unsigned size() const
657 { return m_list
.size(); }
662 for (unsigned i
= 0; i
< m_list
.size(); i
++)
668 void push_back(smart_device
* dev
)
669 { m_list
.push_back(dev
); }
671 void push_back(smart_device_auto_ptr
& dev
)
673 m_list
.push_back(dev
.get());
677 smart_device
* at(unsigned i
)
678 { return m_list
.at(i
); }
680 const smart_device
* at(unsigned i
) const
681 { return m_list
.at(i
); }
683 smart_device
* release(unsigned i
)
685 smart_device
* dev
= m_list
.at(i
);
692 std::vector
<smart_device
*> m_list
;
694 // Prevent copy/assigment
695 smart_device_list(const smart_device_list
&);
696 void operator=(const smart_device_list
&);
700 /////////////////////////////////////////////////////////////////////////////
703 /// The platform interface abstraction
704 class smart_interface
707 /// Initialize platform interface and register with smi().
708 /// Must be implemented by platform module and register interface with set()
714 virtual ~smart_interface() throw()
717 /// Return info string about build host and/or OS version.
718 /// Default implementation returns SMARTMONTOOLS_BUILD_HOST.
719 virtual std::string
get_os_version_str();
721 /// Return valid args for device type option/directive.
722 /// Default implementation returns "ata, scsi, sat, usb*..."
723 /// concatenated with result from get_valid_custom_dev_types_str().
724 virtual std::string
get_valid_dev_types_str();
726 /// Return example string for program 'appname'.
727 /// Default implementation returns empty string.
728 /// For the migration of print_smartctl_examples(),
729 /// function is allowed to print examples to stdout.
730 /// TODO: Remove this hack.
731 virtual std::string
get_app_examples(const char * appname
);
733 /// Get microseconds since some unspecified starting point.
734 /// Used only for command duration measurements in debug outputs.
735 /// Returns -1 if unsupported.
736 /// Default implementation uses clock_gettime(), gettimeofday() or ftime().
737 virtual int64_t get_timer_usec();
739 /// Disable/Enable system auto standby/sleep mode.
740 /// Return false if unsupported or if system is running
742 /// Default implementation returns false.
743 virtual bool disable_system_auto_standby(bool disable
);
746 ///////////////////////////////////////////////
747 // Last error information
749 /// Get last error info struct.
750 const smart_device::error_info
& get_err() const
752 /// Get last error number.
753 int get_errno() const
755 /// Get last error message.
756 const char * get_errmsg() const
757 { return m_err
.msg
.c_str(); }
759 /// Set last error number and message.
760 /// Printf()-like formatting is supported.
761 /// Returns false always to allow use as a return expression.
762 bool set_err(int no
, const char * msg
, ...)
763 __attribute_format_printf(3, 4);
765 /// Set last error info struct.
766 bool set_err(const smart_device::error_info
& err
)
767 { m_err
= err
; return false; }
769 /// Clear last error info.
773 /// Set last error number and default message.
774 /// Message is retrieved from get_msg_for_errno(no).
775 bool set_err(int no
);
777 /// Set last error number and default message to any error_info.
778 /// Used by set_err(no).
779 bool set_err_var(smart_device::error_info
* err
, int no
);
781 /// Convert error number into message, used by set_err(no).
782 /// Default implementation returns strerror(no).
783 virtual const char * get_msg_for_errno(int no
);
785 ///////////////////////////////////////////////////////////////////////////
788 /// Return device object for device 'name' with some 'type'.
789 /// 'type' is 0 if not specified by user.
790 /// Return 0 on error.
791 /// Default implementation selects between ata, scsi and custom device.
792 virtual smart_device
* get_smart_device(const char * name
, const char * type
);
794 /// Fill 'devlist' with devices of some 'type' with device names
795 /// specified by some optional 'pattern'.
796 /// Return false on error.
797 virtual bool scan_smart_devices(smart_device_list
& devlist
, const char * type
,
798 const char * pattern
= 0) = 0;
801 /// Return standard ATA device.
802 virtual ata_device
* get_ata_device(const char * name
, const char * type
) = 0;
804 /// Return standard SCSI device.
805 virtual scsi_device
* get_scsi_device(const char * name
, const char * type
) = 0;
807 /// Autodetect device if no device type specified.
808 virtual smart_device
* autodetect_smart_device(const char * name
) = 0;
810 /// Return device for platform specific 'type'.
811 /// Default implementation returns 0.
812 virtual smart_device
* get_custom_smart_device(const char * name
, const char * type
);
814 /// Return valid 'type' args accepted by above.
815 /// This is called in get_valid_dev_types_str().
816 /// Default implementation returns empty string.
817 virtual std::string
get_valid_custom_dev_types_str();
819 /// Return ATA->SCSI filter for SAT or USB.
820 /// Override only if platform needs special handling.
821 virtual ata_device
* get_sat_device(const char * type
, scsi_device
* scsidev
);
822 //{ implemented in scsiata.cpp }
825 /// Try to detect a SAT device behind a SCSI interface.
826 /// Inquiry data can be passed if available.
827 /// Return appropriate device if yes, otherwise 0.
828 /// Override only if platform needs special handling.
829 virtual ata_device
* autodetect_sat_device(scsi_device
* scsidev
,
830 const unsigned char * inqdata
, unsigned inqsize
);
831 //{ implemented in scsiata.cpp }
833 /// Get type name for USB device with known VENDOR:PRODUCT ID.
834 /// Return name if device known and supported, otherwise 0.
835 virtual const char * get_usb_dev_type_by_id(int vendor_id
, int product_id
,
837 //{ implemented in scsiata.cpp }
840 /// Set interface to use, must be called from init().
841 static void set(smart_interface
* intf
)
842 { s_instance
= intf
; }
846 smart_device::error_info m_err
;
848 friend smart_interface
* smi(); // below
849 static smart_interface
* s_instance
; ///< Pointer to the interface object.
851 // Prevent copy/assigment
852 smart_interface(const smart_interface
&);
853 void operator=(const smart_interface
&);
857 /////////////////////////////////////////////////////////////////////////////
860 /// Global access to the (usually singleton) smart_interface
861 inline smart_interface
* smi()
862 { return smart_interface::s_instance
; }
864 /////////////////////////////////////////////////////////////////////////////
866 #endif // DEV_INTERFACE_H