4 * Home page of code is: http://smartmontools.sourceforge.net
6 * Copyright (C) 2008-9 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 3015 2009-12-30 16:25:59Z chrfranke $\n"
28 #if !defined(__GNUC__) && !defined(__attribute__)
29 #define __attribute__(x) /**/
32 #ifdef _MSC_VER // Disable MSVC warning
33 #pragma warning(disable:4250) // 'class1' : inherits 'class2::member' via dominance
36 /////////////////////////////////////////////////////////////////////////////
37 // Common functionality for all device types
39 // Forward declarations
40 class smart_interface
;
44 /// Base class for all devices
49 /// Device info strings
53 device_info(const char * d_name
, const char * d_type
, const char * r_type
)
54 : dev_name(d_name
), info_name(d_name
),
55 dev_type(d_type
), req_type(r_type
)
58 std::string dev_name
; ///< Device (path)name
59 std::string info_name
; ///< Informal name
60 std::string dev_type
; ///< Actual device type
61 std::string req_type
; ///< Device type requested by user, empty if none
64 /// Error (number,message) pair
66 explicit error_info(int n
= 0)
68 error_info(int n
, const char * m
)
71 { no
= 0; msg
.erase(); }
73 int no
; ///< Error number
74 std::string msg
; ///< Error message
79 /// Constructor to init interface and device info.
80 /// Must be called in implementation classes.
81 smart_device(smart_interface
* intf
, const char * dev_name
,
82 const char * dev_type
, const char * req_type
);
84 /// Dummy enum for dummy constructor.
85 enum do_not_use_in_implementation_classes
{ never_called
};
86 /// Dummy constructor for abstract classes.
87 /// Must never be called in implementation classes.
88 smart_device(do_not_use_in_implementation_classes
);
91 virtual ~smart_device() throw();
95 ///////////////////////////////////////////////
96 // Dynamic downcasts to actual device flavor
98 /// Return true if ATA device
100 { return !!m_ata_ptr
; }
101 /// Return true if SCSI device
103 { return !!m_scsi_ptr
; }
105 /// Downcast to ATA device.
106 ata_device
* to_ata()
107 { return m_ata_ptr
; }
108 /// Downcast to ATA device (const).
109 const ata_device
* to_ata() const
110 { return m_ata_ptr
; }
111 /// Downcast to SCSI device.
112 scsi_device
* to_scsi()
113 { return m_scsi_ptr
; }
114 /// Downcast to ATA device (const).
115 const scsi_device
* to_scsi() const
116 { return m_scsi_ptr
; }
118 ///////////////////////////////////////////////
119 // Device information
121 /// Get device info struct.
122 const device_info
& get_info() const
125 /// Get device (path)name.
126 const char * get_dev_name() const
127 { return m_info
.dev_name
.c_str(); }
128 /// Get informal name.
129 const char * get_info_name() const
130 { return m_info
.info_name
.c_str(); }
132 const char * get_dev_type() const
133 { return m_info
.dev_type
.c_str(); }
134 /// Get type requested by user, empty if none.
135 const char * get_req_type() const
136 { return m_info
.req_type
.c_str(); }
139 /// R/W access to device info struct.
140 device_info
& set_info()
144 ///////////////////////////////////////////////
145 // Last error information
147 /// Get last error info struct.
148 const error_info
& get_err() const
150 /// Get last error number.
151 int get_errno() const
153 /// Get last error message.
154 const char * get_errmsg() const
155 { return m_err
.msg
.c_str(); }
157 /// Set last error number and message.
158 /// Printf()-like formatting is supported.
159 /// Returns false always to allow use as a return expression.
160 bool set_err(int no
, const char * msg
, ...)
161 __attribute__ ((format (printf
, 3, 4)));
163 /// Set last error info struct.
164 bool set_err(const error_info
& err
)
165 { m_err
= err
; return false; }
167 /// Clear last error info.
171 /// Set last error number and default message.
172 /// Message is retrieved from interface's get_msg_for_errno(no).
173 bool set_err(int no
);
177 ///////////////////////////////////////////////
179 // Must be implemented in derived class
181 /// Return true if device is open.
182 virtual bool is_open() const = 0;
184 /// Open device, return false on error.
185 virtual bool open() = 0;
187 /// Close device, return false on error.
188 virtual bool close() = 0;
190 /// Open device with autodetection support.
191 /// May return another device for further access.
192 /// In this case, the original pointer is no longer valid.
193 /// Default Implementation calls 'open()' and returns 'this'.
194 virtual smart_device
* autodetect_open();
196 ///////////////////////////////////////////////
197 // Support for tunnelled devices
199 /// Return true if other device is owned by this device.
200 /// Default implementation returns false.
201 virtual bool owns(const smart_device
* dev
) const;
203 /// Release ownership of other device.
204 /// Default implementation does nothing.
205 virtual void release(const smart_device
* dev
);
208 /// Set dynamic downcast for ATA
209 void this_is_ata(ata_device
* ata
);
212 /// Set dynamic downcast for SCSI
213 void this_is_scsi(scsi_device
* scsi
);
216 /// Get interface which produced this object.
217 smart_interface
* smi()
219 /// Get interface which produced this object (const).
220 const smart_interface
* smi() const
225 smart_interface
* m_intf
;
227 ata_device
* m_ata_ptr
;
228 scsi_device
* m_scsi_ptr
;
231 // Prevent copy/assigment
232 smart_device(const smart_device
&);
233 void operator=(const smart_device
&);
237 /////////////////////////////////////////////////////////////////////////////
238 // ATA specific interface
240 /// ATA register value and info whether is has been ever set
241 // (Automatically set by first assignment)
246 : m_val(0x00), m_is_set(false) { }
248 ata_register
& operator=(unsigned char val
)
249 { m_val
= val
; m_is_set
= true; return * this; }
251 unsigned char val() const
253 operator unsigned char() const
260 unsigned char m_val
; ///< Register value
261 bool m_is_set
; ///< true if set
264 /// ATA Input registers (for 28-bit commands)
267 // ATA-6/7 register names // ATA-3/4/5 // ATA-8
268 ata_register features
; // features // features
269 ata_register sector_count
; // sector count // count
270 ata_register lba_low
; // sector number // ]
271 ata_register lba_mid
; // cylinder low // ] lba
272 ata_register lba_high
; // cylinder high // ]
273 ata_register device
; // device/head // device
274 ata_register command
; // command // command
276 /// Return true if any register is set
278 { return (features
.is_set() || sector_count
.is_set()
279 || lba_low
.is_set() || lba_mid
.is_set() || lba_high
.is_set()
280 || device
.is_set() || command
.is_set()); }
283 /// ATA Output registers (for 28-bit commands)
287 ata_register sector_count
;
288 ata_register lba_low
;
289 ata_register lba_mid
;
290 ata_register lba_high
;
294 /// Return true if any register is set
296 { return (error
.is_set() || sector_count
.is_set()
297 || lba_low
.is_set() || lba_mid
.is_set() || lba_high
.is_set()
298 || device
.is_set() || status
.is_set()); }
302 /// 16-bit alias to a 8-bit ATA register pair.
303 class ata_reg_alias_16
306 ata_reg_alias_16(ata_register
& lo
, ata_register
& hi
)
307 : m_lo(lo
), m_hi(hi
) { }
309 ata_reg_alias_16
& operator=(unsigned short val
)
310 { m_lo
= (unsigned char) val
;
311 m_hi
= (unsigned char)(val
>> 8);
314 unsigned short val() const
315 { return m_lo
| (m_hi
<< 8); }
316 operator unsigned short() const
317 { return m_lo
| (m_hi
<< 8); }
320 ata_register
& m_lo
, & m_hi
;
322 // References must not be copied.
323 ata_reg_alias_16(const ata_reg_alias_16
&);
324 void operator=(const ata_reg_alias_16
&);
328 /// 48-bit alias to six 8-bit ATA registers (for LBA).
329 class ata_reg_alias_48
332 ata_reg_alias_48(ata_register
& ll
, ata_register
& lm
, ata_register
& lh
,
333 ata_register
& hl
, ata_register
& hm
, ata_register
& hh
)
334 : m_ll(ll
), m_lm(lm
), m_lh(lh
),
335 m_hl(hl
), m_hm(hm
), m_hh(hh
)
338 ata_reg_alias_48
& operator=(uint64_t val
)
340 m_ll
= (unsigned char) val
;
341 m_lm
= (unsigned char)(val
>> 8);
342 m_lh
= (unsigned char)(val
>> 16);
343 m_hl
= (unsigned char)(val
>> 24);
344 m_hm
= (unsigned char)(val
>> 32);
345 m_hh
= (unsigned char)(val
>> 40);
351 return ( (unsigned)m_ll
352 | ((unsigned)m_lm
<< 8)
353 | ((unsigned)m_lh
<< 16)
354 | ((unsigned)m_hl
<< 24)
355 | ((uint64_t)m_hm
<< 32)
356 | ((uint64_t)m_hh
<< 40));
359 operator uint64_t() const
363 ata_register
& m_ll
, & m_lm
, & m_lh
,
364 & m_hl
, & m_hm
, & m_hh
;
366 // References must not be copied.
367 ata_reg_alias_48(const ata_reg_alias_48
&);
368 void operator=(const ata_reg_alias_48
&);
372 /// ATA Input registers for 48-bit commands
373 // See section 4.14 of T13/1532D Volume 1 Revision 4b
375 // Uses ATA-6/7 method to specify 16-bit registers as
376 // recent (low byte) and previous (high byte) content of
379 // (ATA-8 ACS does not longer follow this scheme, it uses
380 // abstract registers with sufficient size and leaves the
381 // actual mapping to the transport layer.)
383 struct ata_in_regs_48bit
384 : public ata_in_regs
// "most recently written" registers
386 ata_in_regs prev
; ///< "previous content"
388 // 16-bit aliases for above pair.
389 ata_reg_alias_16 features_16
;
390 ata_reg_alias_16 sector_count_16
;
391 ata_reg_alias_16 lba_low_16
;
392 ata_reg_alias_16 lba_mid_16
;
393 ata_reg_alias_16 lba_high_16
;
395 // 48-bit alias to all 8-bit LBA registers.
396 ata_reg_alias_48 lba_48
;
398 /// Return true if 48-bit command
399 bool is_48bit_cmd() const
400 { return prev
.is_set(); }
402 /// Return true if 48-bit command with any nonzero high byte
403 bool is_real_48bit_cmd() const
404 { return ( prev
.features
|| prev
.sector_count
405 || prev
.lba_low
|| prev
.lba_mid
|| prev
.lba_high
); }
411 /// ATA Output registers for 48-bit commands
412 struct ata_out_regs_48bit
413 : public ata_out_regs
// read with HOB=0
415 ata_out_regs prev
; ///< read with HOB=1
417 // 16-bit aliases for above pair.
418 ata_reg_alias_16 sector_count_16
;
419 ata_reg_alias_16 lba_low_16
;
420 ata_reg_alias_16 lba_mid_16
;
421 ata_reg_alias_16 lba_high_16
;
423 // 48-bit alias to all 8-bit LBA registers.
424 ata_reg_alias_48 lba_48
;
426 ata_out_regs_48bit();
430 /// Flags for each ATA output register
431 struct ata_out_regs_flags
433 bool error
, sector_count
, lba_low
, lba_mid
, lba_high
, device
, status
;
435 /// Return true if any flag is set.
437 { return ( error
|| sector_count
|| lba_low
438 || lba_mid
|| lba_high
|| device
|| status
); }
440 /// Default constructor clears all flags.
442 : error(false), sector_count(false), lba_low(false), lba_mid(false),
443 lba_high(false), device(false), status(false) { }
447 /// ATA pass through input parameters
450 ata_in_regs_48bit in_regs
; ///< Input registers
451 ata_out_regs_flags out_needed
; ///< True if output register value needed
452 enum { no_data
= 0, data_in
, data_out
} direction
; ///< I/O direction
453 void * buffer
; ///< Pointer to data buffer
454 unsigned size
; ///< Size of buffer
456 /// Prepare for 28-bit DATA IN command
457 void set_data_in(void * buf
, unsigned nsectors
)
460 in_regs
.sector_count
= nsectors
;
462 size
= nsectors
* 512;
465 /// Prepare for 28-bit DATA OUT command
466 void set_data_out(const void * buf
, unsigned nsectors
)
468 buffer
= const_cast<void *>(buf
);
469 in_regs
.sector_count
= nsectors
;
470 direction
= data_out
;
471 size
= nsectors
* 512;
474 /// Prepare for 48-bit DATA IN command
475 void set_data_in_48bit(void * buf
, unsigned nsectors
)
478 // Note: This also sets 'in_regs.is_48bit_cmd()'
479 in_regs
.sector_count_16
= nsectors
;
481 size
= nsectors
* 512;
487 /// ATA pass through output parameters
490 ata_out_regs_48bit out_regs
; ///< Output registers
495 /// ATA device access
497 : virtual public /*extends*/ smart_device
500 /// ATA pass through.
501 /// Return false on error.
502 /// Must be implemented in derived class.
503 virtual bool ata_pass_through(const ata_cmd_in
& in
, ata_cmd_out
& out
) = 0;
505 /// ATA pass through without output registers.
506 /// Return false on error.
507 /// Calls ata_pass_through(in, dummy), cannot be reimplemented.
508 bool ata_pass_through(const ata_cmd_in
& in
);
510 /// Return true if OS caches ATA identify sector.
511 /// Default implementation returns false.
512 virtual bool ata_identify_is_cached() const;
515 /// Check command input parameters.
516 /// Calls set_err(...) accordingly.
517 bool ata_cmd_is_ok(const ata_cmd_in
& in
,
518 bool data_out_support
= false,
519 bool multi_sector_support
= false,
520 bool ata_48bit_support
= false);
522 /// Default constructor, registers device as ATA.
524 : smart_device(never_called
)
525 { this_is_ata(this); }
529 /////////////////////////////////////////////////////////////////////////////
530 // SCSI specific interface
534 /// SCSI device access
536 : virtual public /*extends*/ smart_device
539 /// SCSI pass through.
540 /// Returns false on error.
541 virtual bool scsi_pass_through(scsi_cmnd_io
* iop
) = 0;
544 /// Default constructor, registers device as SCSI.
546 : smart_device(never_called
)
547 { this_is_scsi(this); }
551 /////////////////////////////////////////////////////////////////////////////
553 // Set dynamic downcasts
554 // Note that due to virtual inheritance,
555 // (ata == this) does not imply ((void*)ata == (void*)this))
557 inline void smart_device::this_is_ata(ata_device
* ata
)
559 m_ata_ptr
= (ata
== this ? ata
: 0);
562 inline void smart_device::this_is_scsi(scsi_device
* scsi
)
564 m_scsi_ptr
= (scsi
== this ? scsi
: 0);
568 /////////////////////////////////////////////////////////////////////////////
569 /// Smart pointer class for device pointers
572 class any_device_auto_ptr
575 typedef Dev device_type
;
577 /// Construct from optional pointer to device
578 /// and optional pointer to base device.
579 explicit any_device_auto_ptr(device_type
* dev
= 0,
580 smart_device
* base_dev
= 0)
581 : m_dev(dev
), m_base_dev(base_dev
) { }
583 /// Destructor deletes device object.
584 ~any_device_auto_ptr() throw()
587 /// Assign a new pointer.
588 /// Throws if a pointer is already assigned.
589 void operator=(device_type
* dev
)
596 /// Delete device object and clear the pointer.
600 if (m_base_dev
&& m_dev
->owns(m_base_dev
))
601 m_dev
->release(m_base_dev
);
607 /// Return the pointer and release ownership.
608 device_type
* release()
610 device_type
* dev
= m_dev
;
615 /// Replace the pointer.
616 /// Used to call dev->autodetect_open().
617 void replace(device_type
* dev
)
620 /// Return the pointer.
621 device_type
* get() const
624 /// Pointer dereferencing.
625 device_type
& operator*() const
628 /// Pointer dereferencing.
629 device_type
* operator->() const
632 /// For (ptr != 0) check.
633 operator bool() const
636 /// For (ptr == 0) check.
637 bool operator !() const
642 smart_device
* m_base_dev
;
645 { throw std::logic_error("any_device_auto_ptr: wrong usage"); }
647 // Prevent copy/assignment
648 any_device_auto_ptr(const any_device_auto_ptr
<Dev
> &);
649 void operator=(const any_device_auto_ptr
<Dev
> &);
652 typedef any_device_auto_ptr
<smart_device
> smart_device_auto_ptr
;
653 typedef any_device_auto_ptr
<ata_device
> ata_device_auto_ptr
;
654 typedef any_device_auto_ptr
<scsi_device
> scsi_device_auto_ptr
;
657 /////////////////////////////////////////////////////////////////////////////
660 /// List of devices for DEVICESCAN
661 class smart_device_list
668 ~smart_device_list() throw()
670 for (unsigned i
= 0; i
< m_list
.size(); i
++)
675 unsigned size() const
676 { return m_list
.size(); }
681 for (unsigned i
= 0; i
< m_list
.size(); i
++)
687 void push_back(smart_device
* dev
)
688 { m_list
.push_back(dev
); }
690 void push_back(smart_device_auto_ptr
& dev
)
692 m_list
.push_back(dev
.get());
696 smart_device
* at(unsigned i
)
697 { return m_list
.at(i
); }
699 const smart_device
* at(unsigned i
) const
700 { return m_list
.at(i
); }
702 smart_device
* release(unsigned i
)
704 smart_device
* dev
= m_list
.at(i
);
711 std::vector
<smart_device
*> m_list
;
713 // Prevent copy/assigment
714 smart_device_list(const smart_device_list
&);
715 void operator=(const smart_device_list
&);
719 /////////////////////////////////////////////////////////////////////////////
722 /// The platform interface abstraction
723 class smart_interface
726 /// Initialize platform interface and register with smi().
727 /// Must be implemented by platform module and register interface with set()
733 virtual ~smart_interface() throw()
736 /// Return info string about build host and/or OS version.
737 /// Default implementation returns SMARTMONTOOLS_BUILD_HOST.
738 virtual std::string
get_os_version_str();
740 /// Return valid args for device type option/directive.
741 /// Default implementation returns "ata, scsi, sat, usb*..."
742 /// concatenated with result from get_valid_custom_dev_types_str().
743 virtual std::string
get_valid_dev_types_str();
745 /// Return example string for program 'appname'.
746 /// Default implementation returns empty string.
747 /// For the migration of print_smartctl_examples(),
748 /// function is allowed to print examples to stdout.
749 /// TODO: Remove this hack.
750 virtual std::string
get_app_examples(const char * appname
);
752 ///////////////////////////////////////////////
753 // Last error information
755 /// Get last error info struct.
756 const smart_device::error_info
& get_err() const
758 /// Get last error number.
759 int get_errno() const
761 /// Get last error message.
762 const char * get_errmsg() const
763 { return m_err
.msg
.c_str(); }
765 /// Set last error number and message.
766 /// Printf()-like formatting is supported.
767 void set_err(int no
, const char * msg
, ...)
768 __attribute__ ((format (printf
, 3, 4)));
770 /// Set last error info struct.
771 void set_err(const smart_device::error_info
& err
)
774 /// Clear last error info.
778 /// Set last error number and default message.
779 /// Message is retrieved from get_msg_for_errno(no).
780 void set_err(int no
);
782 /// Set last error number and default message to any error_info.
783 /// Used by set_err(no).
784 void set_err_var(smart_device::error_info
* err
, int no
);
786 /// Convert error number into message, used by set_err(no).
787 /// Default implementation returns strerror(no).
788 virtual const char * get_msg_for_errno(int no
);
790 ///////////////////////////////////////////////////////////////////////////
793 /// Return device object for device 'name' with some 'type'.
794 /// 'type' is 0 if not specified by user.
795 /// Return 0 on error.
796 /// Default implementation selects between ata, scsi and custom device.
797 virtual smart_device
* get_smart_device(const char * name
, const char * type
);
799 /// Fill 'devlist' with devices of some 'type' with devices names.
800 /// specified by some optional 'pattern'.
801 /// Return false on error.
802 virtual bool scan_smart_devices(smart_device_list
& devlist
, const char * type
,
803 const char * pattern
= 0) = 0;
806 /// Return standard ATA device.
807 virtual ata_device
* get_ata_device(const char * name
, const char * type
) = 0;
809 /// Return standard SCSI device.
810 virtual scsi_device
* get_scsi_device(const char * name
, const char * type
) = 0;
812 /// Autodetect device if no device type specified.
813 virtual smart_device
* autodetect_smart_device(const char * name
) = 0;
815 /// Return device for platform specific 'type'.
816 /// Default implementation returns 0.
817 virtual smart_device
* get_custom_smart_device(const char * name
, const char * type
);
819 /// Return valid 'type' args accepted by above.
820 /// This is called in get_valid_dev_types_str().
821 /// Default implementation returns empty string.
822 virtual std::string
get_valid_custom_dev_types_str();
824 /// Return ATA->SCSI filter for SAT or USB.
825 /// Override only if platform needs special handling.
826 virtual ata_device
* get_sat_device(const char * type
, scsi_device
* scsidev
);
827 //{ implemented in scsiata.cpp }
830 /// Try to detect a SAT device behind a SCSI interface.
831 /// Inquiry data can be passed if available.
832 /// Return appropriate device if yes, otherwise 0.
833 /// Override only if platform needs special handling.
834 virtual ata_device
* autodetect_sat_device(scsi_device
* scsidev
,
835 const unsigned char * inqdata
, unsigned inqsize
);
836 //{ implemented in scsiata.cpp }
838 /// Get type name for USB device with known VENDOR:PRODUCT ID.
839 /// Return name if device known and supported, otherwise 0.
840 virtual const char * get_usb_dev_type_by_id(int vendor_id
, int product_id
,
842 //{ implemented in scsiata.cpp }
845 /// Set interface to use, must be called from init().
846 static void set(smart_interface
* intf
)
847 { s_instance
= intf
; }
851 smart_device::error_info m_err
;
853 friend smart_interface
* smi(); // below
854 static smart_interface
* s_instance
; ///< Pointer to the interface object.
856 // Prevent copy/assigment
857 smart_interface(const smart_interface
&);
858 void operator=(const smart_interface
&);
862 /////////////////////////////////////////////////////////////////////////////
865 /// Global access to the (usually singleton) smart_interface
866 inline smart_interface
* smi()
867 { return smart_interface::s_instance
; }
869 /////////////////////////////////////////////////////////////////////////////
871 #endif // DEV_INTERFACE_H