]> git.proxmox.com Git - mirror_smartmontools-debian.git/blame - dev_interface.h
import smartmontools 7.0
[mirror_smartmontools-debian.git] / dev_interface.h
CommitLineData
2127e193
GI
1/*
2 * dev_interface.h
3 *
a86ec89e 4 * Home page of code is: http://www.smartmontools.org
2127e193 5 *
ff28b140 6 * Copyright (C) 2008-18 Christian Franke
2127e193 7 *
ff28b140 8 * SPDX-License-Identifier: GPL-2.0-or-later
2127e193
GI
9 */
10
11#ifndef DEV_INTERFACE_H
12#define DEV_INTERFACE_H
13
ff28b140 14#define DEV_INTERFACE_H_CVSID "$Id: dev_interface.h 4848 2018-12-05 18:30:46Z chrfranke $\n"
d008864d
GI
15
16#include "utility.h"
2127e193 17
bed94269 18#include <stdexcept>
2127e193
GI
19#include <string>
20#include <vector>
21
2127e193
GI
22/////////////////////////////////////////////////////////////////////////////
23// Common functionality for all device types
24
25// Forward declarations
26class smart_interface;
27class ata_device;
28class scsi_device;
a86ec89e 29class nvme_device;
2127e193
GI
30
31/// Base class for all devices
32class smart_device
33{
34// Types
35public:
36 /// Device info strings
37 struct device_info {
38 device_info()
39 { }
40 device_info(const char * d_name, const char * d_type, const char * r_type)
41 : dev_name(d_name), info_name(d_name),
42 dev_type(d_type), req_type(r_type)
43 { }
44
45 std::string dev_name; ///< Device (path)name
46 std::string info_name; ///< Informal name
47 std::string dev_type; ///< Actual device type
48 std::string req_type; ///< Device type requested by user, empty if none
49 };
50
51 /// Error (number,message) pair
52 struct error_info {
53 explicit error_info(int n = 0)
54 : no(n) { }
55 error_info(int n, const char * m)
56 : no(n), msg(m) { }
57 void clear()
58 { no = 0; msg.erase(); }
59
60 int no; ///< Error number
61 std::string msg; ///< Error message
62 };
63
64// Construction
65protected:
66 /// Constructor to init interface and device info.
67 /// Must be called in implementation classes.
68 smart_device(smart_interface * intf, const char * dev_name,
69 const char * dev_type, const char * req_type);
70
71 /// Dummy enum for dummy constructor.
72 enum do_not_use_in_implementation_classes { never_called };
73 /// Dummy constructor for abstract classes.
74 /// Must never be called in implementation classes.
a86ec89e 75 explicit smart_device(do_not_use_in_implementation_classes);
2127e193
GI
76
77public:
78 virtual ~smart_device() throw();
79
80// Attributes
81public:
82 ///////////////////////////////////////////////
83 // Dynamic downcasts to actual device flavor
84
85 /// Return true if ATA device
86 bool is_ata() const
87 { return !!m_ata_ptr; }
88 /// Return true if SCSI device
89 bool is_scsi() const
90 { return !!m_scsi_ptr; }
a86ec89e
GI
91 /// Return true if NVMe device
92 bool is_nvme() const
93 { return !!m_nvme_ptr; }
2127e193
GI
94
95 /// Downcast to ATA device.
96 ata_device * to_ata()
97 { return m_ata_ptr; }
98 /// Downcast to ATA device (const).
99 const ata_device * to_ata() const
100 { return m_ata_ptr; }
101 /// Downcast to SCSI device.
102 scsi_device * to_scsi()
103 { return m_scsi_ptr; }
cfbba5b9 104 /// Downcast to SCSI device (const).
2127e193
GI
105 const scsi_device * to_scsi() const
106 { return m_scsi_ptr; }
a86ec89e
GI
107 /// Downcast to NVMe device.
108 nvme_device * to_nvme()
109 { return m_nvme_ptr; }
110 /// Downcast to NVMe device (const).
111 const nvme_device * to_nvme() const
112 { return m_nvme_ptr; }
2127e193
GI
113
114 ///////////////////////////////////////////////
115 // Device information
116
117 /// Get device info struct.
118 const device_info & get_info() const
119 { return m_info; }
120
121 /// Get device (path)name.
122 const char * get_dev_name() const
123 { return m_info.dev_name.c_str(); }
124 /// Get informal name.
125 const char * get_info_name() const
126 { return m_info.info_name.c_str(); }
127 /// Get device type.
128 const char * get_dev_type() const
129 { return m_info.dev_type.c_str(); }
130 /// Get type requested by user, empty if none.
131 const char * get_req_type() const
132 { return m_info.req_type.c_str(); }
133
134protected:
135 /// R/W access to device info struct.
136 device_info & set_info()
137 { return m_info; }
138
139public:
140 ///////////////////////////////////////////////
141 // Last error information
142
143 /// Get last error info struct.
144 const error_info & get_err() const
145 { return m_err; }
146 /// Get last error number.
147 int get_errno() const
148 { return m_err.no; }
149 /// Get last error message.
150 const char * get_errmsg() const
151 { return m_err.msg.c_str(); }
152
f4e463df
GI
153 /// Return true if last error indicates an unsupported system call.
154 /// Default implementation returns true on ENOSYS and ENOTSUP.
155 virtual bool is_syscall_unsup() const;
156
2127e193
GI
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, ...)
d008864d 161 __attribute_format_printf(3, 4);
2127e193
GI
162
163 /// Set last error info struct.
164 bool set_err(const error_info & err)
165 { m_err = err; return false; }
166
167 /// Clear last error info.
168 void clear_err()
169 { m_err.clear(); }
170
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);
174
a86ec89e
GI
175 /// Get current number of allocated 'smart_device' objects.
176 static int get_num_objects()
177 { return s_num_objects; }
178
2127e193
GI
179// Operations
180public:
181 ///////////////////////////////////////////////
182 // Device open/close
183 // Must be implemented in derived class
184
185 /// Return true if device is open.
186 virtual bool is_open() const = 0;
187
188 /// Open device, return false on error.
189 virtual bool open() = 0;
190
191 /// Close device, return false on error.
192 virtual bool close() = 0;
193
194 /// Open device with autodetection support.
195 /// May return another device for further access.
196 /// In this case, the original pointer is no longer valid.
f4e463df 197 /// Default implementation calls 'open()' and returns 'this'.
2127e193
GI
198 virtual smart_device * autodetect_open();
199
a86ec89e
GI
200 ///////////////////////////////////////////////
201 // Support for checking power mode reported by operating system
202
203 /// Early test if device is powered up or down.
204 /// Can be used without calling 'open()' first!
205 /// Return true when device is powered down, false when
206 /// powered up. If this function is not implemented or
207 /// the mode cannot be determined, return false.
208 /// Default implementation returns false.
209 virtual bool is_powered_down();
210
2127e193
GI
211 ///////////////////////////////////////////////
212 // Support for tunnelled devices
213
214 /// Return true if other device is owned by this device.
215 /// Default implementation returns false.
216 virtual bool owns(const smart_device * dev) const;
217
218 /// Release ownership of other device.
219 /// Default implementation does nothing.
220 virtual void release(const smart_device * dev);
221
222protected:
2127e193
GI
223 /// Get interface which produced this object.
224 smart_interface * smi()
225 { return m_intf; }
226 /// Get interface which produced this object (const).
227 const smart_interface * smi() const
228 { return m_intf; }
229
230// Implementation
231private:
232 smart_interface * m_intf;
233 device_info m_info;
d008864d
GI
234 error_info m_err;
235
a86ec89e
GI
236 // Pointers for to_ata(), to_scsi(), to_nvme()
237 // set by ATA/SCSI/NVMe interface classes.
d008864d 238 friend class ata_device;
2127e193 239 ata_device * m_ata_ptr;
d008864d 240 friend class scsi_device;
2127e193 241 scsi_device * m_scsi_ptr;
a86ec89e
GI
242 friend class nvme_device;
243 nvme_device * m_nvme_ptr;
244
245 // Number of objects.
246 static int s_num_objects;
2127e193 247
ff28b140 248 // Prevent copy/assignment
2127e193
GI
249 smart_device(const smart_device &);
250 void operator=(const smart_device &);
251};
252
253
254/////////////////////////////////////////////////////////////////////////////
255// ATA specific interface
256
cfbba5b9 257/// ATA register value and info whether it has ever been set
2127e193
GI
258// (Automatically set by first assignment)
259class ata_register
260{
261public:
262 ata_register()
263 : m_val(0x00), m_is_set(false) { }
264
cfbba5b9
GI
265 ata_register & operator=(unsigned char x)
266 { m_val = x; m_is_set = true; return * this; }
2127e193
GI
267
268 unsigned char val() const
269 { return m_val; }
270 operator unsigned char() const
271 { return m_val; }
272
273 bool is_set() const
274 { return m_is_set; }
275
276private:
277 unsigned char m_val; ///< Register value
278 bool m_is_set; ///< true if set
279};
280
281/// ATA Input registers (for 28-bit commands)
282struct ata_in_regs
283{
284 // ATA-6/7 register names // ATA-3/4/5 // ATA-8
285 ata_register features; // features // features
286 ata_register sector_count; // sector count // count
287 ata_register lba_low; // sector number // ]
288 ata_register lba_mid; // cylinder low // ] lba
289 ata_register lba_high; // cylinder high // ]
290 ata_register device; // device/head // device
291 ata_register command; // command // command
292
293 /// Return true if any register is set
294 bool is_set() const
295 { return (features.is_set() || sector_count.is_set()
296 || lba_low.is_set() || lba_mid.is_set() || lba_high.is_set()
297 || device.is_set() || command.is_set()); }
298};
299
300/// ATA Output registers (for 28-bit commands)
301struct ata_out_regs
302{
303 ata_register error;
304 ata_register sector_count;
305 ata_register lba_low;
306 ata_register lba_mid;
307 ata_register lba_high;
308 ata_register device;
309 ata_register status;
310
311 /// Return true if any register is set
312 bool is_set() const
313 { return (error.is_set() || sector_count.is_set()
314 || lba_low.is_set() || lba_mid.is_set() || lba_high.is_set()
315 || device.is_set() || status.is_set()); }
316};
317
318
319/// 16-bit alias to a 8-bit ATA register pair.
320class ata_reg_alias_16
321{
322public:
323 ata_reg_alias_16(ata_register & lo, ata_register & hi)
324 : m_lo(lo), m_hi(hi) { }
325
cfbba5b9
GI
326 ata_reg_alias_16 & operator=(unsigned short x)
327 { m_lo = (unsigned char) x;
328 m_hi = (unsigned char)(x >> 8);
2127e193
GI
329 return * this; }
330
331 unsigned short val() const
332 { return m_lo | (m_hi << 8); }
333 operator unsigned short() const
334 { return m_lo | (m_hi << 8); }
335
336private:
337 ata_register & m_lo, & m_hi;
338
339 // References must not be copied.
340 ata_reg_alias_16(const ata_reg_alias_16 &);
341 void operator=(const ata_reg_alias_16 &);
342};
343
344
a23d5117
GI
345/// 48-bit alias to six 8-bit ATA registers (for LBA).
346class ata_reg_alias_48
347{
348public:
349 ata_reg_alias_48(ata_register & ll, ata_register & lm, ata_register & lh,
350 ata_register & hl, ata_register & hm, ata_register & hh)
351 : m_ll(ll), m_lm(lm), m_lh(lh),
352 m_hl(hl), m_hm(hm), m_hh(hh)
353 { }
354
cfbba5b9 355 ata_reg_alias_48 & operator=(uint64_t x)
a23d5117 356 {
cfbba5b9
GI
357 m_ll = (unsigned char) x;
358 m_lm = (unsigned char)(x >> 8);
359 m_lh = (unsigned char)(x >> 16);
360 m_hl = (unsigned char)(x >> 24);
361 m_hm = (unsigned char)(x >> 32);
362 m_hh = (unsigned char)(x >> 40);
a23d5117
GI
363 return * this;
364 }
365
366 uint64_t val() const
367 {
368 return ( (unsigned)m_ll
369 | ((unsigned)m_lm << 8)
370 | ((unsigned)m_lh << 16)
371 | ((unsigned)m_hl << 24)
372 | ((uint64_t)m_hm << 32)
373 | ((uint64_t)m_hh << 40));
374 }
375
376 operator uint64_t() const
377 { return val(); }
378
379private:
380 ata_register & m_ll, & m_lm, & m_lh,
381 & m_hl, & m_hm, & m_hh;
382
383 // References must not be copied.
384 ata_reg_alias_48(const ata_reg_alias_48 &);
385 void operator=(const ata_reg_alias_48 &);
386};
387
388
2127e193
GI
389/// ATA Input registers for 48-bit commands
390// See section 4.14 of T13/1532D Volume 1 Revision 4b
391//
392// Uses ATA-6/7 method to specify 16-bit registers as
393// recent (low byte) and previous (high byte) content of
394// 8-bit registers.
395//
396// (ATA-8 ACS does not longer follow this scheme, it uses
397// abstract registers with sufficient size and leaves the
398// actual mapping to the transport layer.)
399//
400struct ata_in_regs_48bit
401: public ata_in_regs // "most recently written" registers
402{
403 ata_in_regs prev; ///< "previous content"
404
405 // 16-bit aliases for above pair.
406 ata_reg_alias_16 features_16;
407 ata_reg_alias_16 sector_count_16;
408 ata_reg_alias_16 lba_low_16;
409 ata_reg_alias_16 lba_mid_16;
410 ata_reg_alias_16 lba_high_16;
411
a23d5117
GI
412 // 48-bit alias to all 8-bit LBA registers.
413 ata_reg_alias_48 lba_48;
414
2127e193
GI
415 /// Return true if 48-bit command
416 bool is_48bit_cmd() const
417 { return prev.is_set(); }
418
419 /// Return true if 48-bit command with any nonzero high byte
420 bool is_real_48bit_cmd() const
421 { return ( prev.features || prev.sector_count
422 || prev.lba_low || prev.lba_mid || prev.lba_high); }
423
424 ata_in_regs_48bit();
425};
426
427
428/// ATA Output registers for 48-bit commands
429struct ata_out_regs_48bit
430: public ata_out_regs // read with HOB=0
431{
432 ata_out_regs prev; ///< read with HOB=1
433
434 // 16-bit aliases for above pair.
435 ata_reg_alias_16 sector_count_16;
436 ata_reg_alias_16 lba_low_16;
437 ata_reg_alias_16 lba_mid_16;
438 ata_reg_alias_16 lba_high_16;
439
a23d5117
GI
440 // 48-bit alias to all 8-bit LBA registers.
441 ata_reg_alias_48 lba_48;
442
2127e193
GI
443 ata_out_regs_48bit();
444};
445
446
447/// Flags for each ATA output register
448struct ata_out_regs_flags
449{
450 bool error, sector_count, lba_low, lba_mid, lba_high, device, status;
451
452 /// Return true if any flag is set.
453 bool is_set() const
454 { return ( error || sector_count || lba_low
455 || lba_mid || lba_high || device || status); }
456
457 /// Default constructor clears all flags.
458 ata_out_regs_flags()
459 : error(false), sector_count(false), lba_low(false), lba_mid(false),
460 lba_high(false), device(false), status(false) { }
461};
462
463
464/// ATA pass through input parameters
465struct ata_cmd_in
466{
467 ata_in_regs_48bit in_regs; ///< Input registers
468 ata_out_regs_flags out_needed; ///< True if output register value needed
469 enum { no_data = 0, data_in, data_out } direction; ///< I/O direction
470 void * buffer; ///< Pointer to data buffer
471 unsigned size; ///< Size of buffer
472
473 /// Prepare for 28-bit DATA IN command
474 void set_data_in(void * buf, unsigned nsectors)
475 {
476 buffer = buf;
477 in_regs.sector_count = nsectors;
478 direction = data_in;
479 size = nsectors * 512;
480 }
481
482 /// Prepare for 28-bit DATA OUT command
483 void set_data_out(const void * buf, unsigned nsectors)
484 {
485 buffer = const_cast<void *>(buf);
486 in_regs.sector_count = nsectors;
487 direction = data_out;
488 size = nsectors * 512;
489 }
490
491 /// Prepare for 48-bit DATA IN command
492 void set_data_in_48bit(void * buf, unsigned nsectors)
493 {
494 buffer = buf;
495 // Note: This also sets 'in_regs.is_48bit_cmd()'
496 in_regs.sector_count_16 = nsectors;
497 direction = data_in;
498 size = nsectors * 512;
499 }
500
501 ata_cmd_in();
502};
503
504/// ATA pass through output parameters
505struct ata_cmd_out
506{
507 ata_out_regs_48bit out_regs; ///< Output registers
508
509 ata_cmd_out();
510};
511
512/// ATA device access
513class ata_device
514: virtual public /*extends*/ smart_device
515{
516public:
517 /// ATA pass through.
518 /// Return false on error.
519 /// Must be implemented in derived class.
520 virtual bool ata_pass_through(const ata_cmd_in & in, ata_cmd_out & out) = 0;
521
522 /// ATA pass through without output registers.
523 /// Return false on error.
524 /// Calls ata_pass_through(in, dummy), cannot be reimplemented.
525 bool ata_pass_through(const ata_cmd_in & in);
526
527 /// Return true if OS caches ATA identify sector.
528 /// Default implementation returns false.
529 virtual bool ata_identify_is_cached() const;
530
531protected:
ee38a438
GI
532 /// Flags for ata_cmd_is_supported().
533 enum {
534 supports_data_out = 0x01, // PIO DATA OUT
535 supports_smart_status = 0x02, // read output registers for SMART STATUS only
536 supports_output_regs = 0x04, // read output registers for all commands
537 supports_multi_sector = 0x08, // more than one sector (1 DRQ/sector variant)
538 supports_48bit_hi_null = 0x10, // 48-bit commands with null high bytes only
539 supports_48bit = 0x20, // all 48-bit commands
540 };
541
2127e193 542 /// Check command input parameters.
ee38a438 543 /// Return false if required features are not implemented.
2127e193 544 /// Calls set_err(...) accordingly.
ee38a438
GI
545 bool ata_cmd_is_supported(const ata_cmd_in & in, unsigned flags,
546 const char * type = 0);
547
548 /// Check command input parameters (old version).
549 // TODO: Remove if no longer used.
2127e193
GI
550 bool ata_cmd_is_ok(const ata_cmd_in & in,
551 bool data_out_support = false,
552 bool multi_sector_support = false,
ee38a438
GI
553 bool ata_48bit_support = false)
554 {
555 return ata_cmd_is_supported(in,
556 (data_out_support ? supports_data_out : 0) |
557 supports_output_regs |
558 (multi_sector_support ? supports_multi_sector : 0) |
559 (ata_48bit_support ? supports_48bit : 0));
560 }
2127e193 561
d008864d
GI
562 /// Hide/unhide ATA interface.
563 void hide_ata(bool hide = true)
564 { m_ata_ptr = (!hide ? this : 0); }
565
2127e193
GI
566 /// Default constructor, registers device as ATA.
567 ata_device()
568 : smart_device(never_called)
d008864d 569 { hide_ata(false); }
2127e193
GI
570};
571
572
573/////////////////////////////////////////////////////////////////////////////
574// SCSI specific interface
575
576struct scsi_cmnd_io;
577
578/// SCSI device access
579class scsi_device
580: virtual public /*extends*/ smart_device
581{
582public:
583 /// SCSI pass through.
584 /// Returns false on error.
585 virtual bool scsi_pass_through(scsi_cmnd_io * iop) = 0;
586
ff28b140
TL
587 // Call scsi_pass_through and check sense.
588 bool scsi_pass_through_and_check(scsi_cmnd_io * iop,
589 const char * msg = "");
590
591 /// Always try READ CAPACITY(10) (rcap10) first but once we know
592 /// rcap16 is needed, use it instead.
593 void set_rcap16_first()
594 { rcap16_first = true; }
595
596 bool use_rcap16() const
597 { return rcap16_first; }
598
2127e193 599protected:
d008864d
GI
600 /// Hide/unhide SCSI interface.
601 void hide_scsi(bool hide = true)
602 { m_scsi_ptr = (!hide ? this : 0); }
603
2127e193
GI
604 /// Default constructor, registers device as SCSI.
605 scsi_device()
ff28b140
TL
606 : smart_device(never_called),
607 rcap16_first(false)
d008864d 608 { hide_scsi(false); }
ff28b140
TL
609
610private:
611 bool rcap16_first;
2127e193
GI
612};
613
614
a86ec89e
GI
615/////////////////////////////////////////////////////////////////////////////
616// NVMe specific interface
617
618/// NVMe pass through input parameters
619struct nvme_cmd_in
620{
621 unsigned char opcode; ///< Opcode (CDW0 07:00)
622 unsigned nsid; ///< Namespace ID
623 unsigned cdw10, cdw11, cdw12, cdw13, cdw14, cdw15; ///< Cmd specific
624
625 void * buffer; ///< Pointer to data buffer
626 unsigned size; ///< Size of buffer
627
628 enum {
629 no_data = 0x0, data_out = 0x1, data_in = 0x2, data_io = 0x3
630 };
631
632 /// Get I/O direction from opcode
633 unsigned char direction() const
634 { return (opcode & 0x3); }
635
636 // Prepare for DATA IN command
637 void set_data_in(unsigned char op, void * buf, unsigned sz)
638 {
639 opcode = op;
640 if (direction() != data_in)
641 throw std::logic_error("invalid opcode for DATA IN");
642 buffer = buf;
643 size = sz;
644 }
645
646 nvme_cmd_in()
647 : opcode(0), nsid(0),
648 cdw10(0), cdw11(0), cdw12(0), cdw13(0), cdw14(0), cdw15(0),
649 buffer(0), size(0)
650 { }
651};
652
653/// NVMe pass through output parameters
654struct nvme_cmd_out
655{
656 unsigned result; ///< Command specific result (DW0)
657 unsigned short status; ///< Status Field (DW3 31:17)
658 bool status_valid; ///< true if status is valid
659
660 nvme_cmd_out()
661 : result(0), status(0), status_valid(false)
662 { }
663};
664
665/// NVMe device access
666class nvme_device
667: virtual public /*extends*/ smart_device
668{
669public:
670 /// NVMe pass through.
671 /// Return false on error.
672 virtual bool nvme_pass_through(const nvme_cmd_in & in, nvme_cmd_out & out) = 0;
673
674 /// Get namespace id.
675 unsigned get_nsid() const
676 { return m_nsid; }
677
678protected:
679 /// Hide/unhide NVMe interface.
680 void hide_nvme(bool hide = true)
681 { m_nvme_ptr = (!hide ? this : 0); }
682
683 /// Constructor requires namespace ID, registers device as NVMe.
684 explicit nvme_device(unsigned nsid)
685 : smart_device(never_called),
686 m_nsid(nsid)
687 { hide_nvme(false); }
688
689 /// Set namespace id.
690 /// Should be called in open() function if get_nsid() returns 0.
691 void set_nsid(unsigned nsid)
692 { m_nsid = nsid; }
693
694 /// Set last error number and message if pass-through returns NVMe error status.
695 /// Returns false always to allow use as a return expression.
696 bool set_nvme_err(nvme_cmd_out & out, unsigned status, const char * msg = 0);
697
698private:
699 unsigned m_nsid;
700};
701
702
bed94269
GI
703/////////////////////////////////////////////////////////////////////////////
704/// Smart pointer class for device pointers
705
706template <class Dev>
707class any_device_auto_ptr
708{
709public:
710 typedef Dev device_type;
711
712 /// Construct from optional pointer to device
713 /// and optional pointer to base device.
714 explicit any_device_auto_ptr(device_type * dev = 0,
715 smart_device * base_dev = 0)
716 : m_dev(dev), m_base_dev(base_dev) { }
717
718 /// Destructor deletes device object.
719 ~any_device_auto_ptr() throw()
720 { reset(); }
721
722 /// Assign a new pointer.
723 /// Throws if a pointer is already assigned.
724 void operator=(device_type * dev)
725 {
726 if (m_dev)
727 fail();
728 m_dev = dev;
729 }
730
731 /// Delete device object and clear the pointer.
732 void reset()
733 {
734 if (m_dev) {
735 if (m_base_dev && m_dev->owns(m_base_dev))
736 m_dev->release(m_base_dev);
737 delete m_dev;
cfbba5b9 738 m_dev = 0;
bed94269 739 }
bed94269
GI
740 }
741
742 /// Return the pointer and release ownership.
743 device_type * release()
744 {
745 device_type * dev = m_dev;
746 m_dev = 0;
747 return dev;
748 }
749
750 /// Replace the pointer.
751 /// Used to call dev->autodetect_open().
752 void replace(device_type * dev)
753 { m_dev = dev; }
754
755 /// Return the pointer.
756 device_type * get() const
757 { return m_dev; }
758
759 /// Pointer dereferencing.
760 device_type & operator*() const
761 { return *m_dev; }
762
763 /// Pointer dereferencing.
764 device_type * operator->() const
765 { return m_dev; }
766
767 /// For (ptr != 0) check.
768 operator bool() const
769 { return !!m_dev; }
770
771 /// For (ptr == 0) check.
772 bool operator !() const
773 { return !m_dev; }
774
775private:
776 device_type * m_dev;
777 smart_device * m_base_dev;
778
779 void fail() const
780 { throw std::logic_error("any_device_auto_ptr: wrong usage"); }
781
782 // Prevent copy/assignment
783 any_device_auto_ptr(const any_device_auto_ptr<Dev> &);
784 void operator=(const any_device_auto_ptr<Dev> &);
785};
786
787typedef any_device_auto_ptr<smart_device> smart_device_auto_ptr;
788typedef any_device_auto_ptr<ata_device> ata_device_auto_ptr;
789typedef any_device_auto_ptr<scsi_device> scsi_device_auto_ptr;
a86ec89e 790typedef any_device_auto_ptr<nvme_device> nvme_device_auto_ptr;
bed94269
GI
791
792
2127e193
GI
793/////////////////////////////////////////////////////////////////////////////
794// smart_device_list
795
796/// List of devices for DEVICESCAN
797class smart_device_list
798{
799// Construction
800public:
801 smart_device_list()
802 { }
803
804 ~smart_device_list() throw()
805 {
806 for (unsigned i = 0; i < m_list.size(); i++)
807 delete m_list[i];
808 }
809
810// Attributes
811 unsigned size() const
812 { return m_list.size(); }
813
814// Operations
815 void clear()
816 {
817 for (unsigned i = 0; i < m_list.size(); i++)
818 delete m_list[i];
819 m_list.clear();
820 }
821
822
2127e193
GI
823 void push_back(smart_device * dev)
824 { m_list.push_back(dev); }
825
bed94269
GI
826 void push_back(smart_device_auto_ptr & dev)
827 {
828 m_list.push_back(dev.get());
829 dev.release();
830 }
831
2127e193
GI
832 smart_device * at(unsigned i)
833 { return m_list.at(i); }
834
835 const smart_device * at(unsigned i) const
836 { return m_list.at(i); }
837
838 smart_device * release(unsigned i)
839 {
840 smart_device * dev = m_list.at(i);
841 m_list[i] = 0;
842 return dev;
843 }
844
a86ec89e
GI
845 void append(smart_device_list & devlist)
846 {
847 for (unsigned i = 0; i < devlist.size(); i++) {
848 smart_device * dev = devlist.at(i);
849 if (!dev)
850 continue;
851 push_back(dev);
852 devlist.m_list.at(i) = 0;
853 }
854 }
855
2127e193
GI
856// Implementation
857private:
858 std::vector<smart_device *> m_list;
859
ff28b140 860 // Prevent copy/assignment
2127e193
GI
861 smart_device_list(const smart_device_list &);
862 void operator=(const smart_device_list &);
863};
864
865
a86ec89e
GI
866/// List of types for DEVICESCAN
867typedef std::vector<std::string> smart_devtype_list;
868
869
2127e193
GI
870/////////////////////////////////////////////////////////////////////////////
871// smart_interface
872
873/// The platform interface abstraction
874class smart_interface
875{
876public:
877 /// Initialize platform interface and register with smi().
878 /// Must be implemented by platform module and register interface with set()
879 static void init();
880
881 smart_interface()
882 { }
883
884 virtual ~smart_interface() throw()
885 { }
886
54965743
GI
887 /// Return info string about build host and/or OS version.
888 /// Default implementation returns SMARTMONTOOLS_BUILD_HOST.
889 virtual std::string get_os_version_str();
2127e193
GI
890
891 /// Return valid args for device type option/directive.
54965743
GI
892 /// Default implementation returns "ata, scsi, sat, usb*..."
893 /// concatenated with result from get_valid_custom_dev_types_str().
894 virtual std::string get_valid_dev_types_str();
2127e193
GI
895
896 /// Return example string for program 'appname'.
54965743 897 /// Default implementation returns empty string.
2127e193
GI
898 /// For the migration of print_smartctl_examples(),
899 /// function is allowed to print examples to stdout.
900 /// TODO: Remove this hack.
54965743 901 virtual std::string get_app_examples(const char * appname);
2127e193 902
e165493d
GI
903 /// Get microseconds since some unspecified starting point.
904 /// Used only for command duration measurements in debug outputs.
905 /// Returns -1 if unsupported.
906 /// Default implementation uses clock_gettime(), gettimeofday() or ftime().
907 virtual int64_t get_timer_usec();
908
d008864d
GI
909 /// Disable/Enable system auto standby/sleep mode.
910 /// Return false if unsupported or if system is running
911 /// on battery.
912 /// Default implementation returns false.
913 virtual bool disable_system_auto_standby(bool disable);
914
915
2127e193
GI
916 ///////////////////////////////////////////////
917 // Last error information
918
919 /// Get last error info struct.
920 const smart_device::error_info & get_err() const
921 { return m_err; }
922 /// Get last error number.
923 int get_errno() const
924 { return m_err.no; }
925 /// Get last error message.
926 const char * get_errmsg() const
927 { return m_err.msg.c_str(); }
928
929 /// Set last error number and message.
930 /// Printf()-like formatting is supported.
d008864d
GI
931 /// Returns false always to allow use as a return expression.
932 bool set_err(int no, const char * msg, ...)
933 __attribute_format_printf(3, 4);
2127e193
GI
934
935 /// Set last error info struct.
d008864d
GI
936 bool set_err(const smart_device::error_info & err)
937 { m_err = err; return false; }
2127e193
GI
938
939 /// Clear last error info.
940 void clear_err()
941 { m_err.clear(); }
942
943 /// Set last error number and default message.
944 /// Message is retrieved from get_msg_for_errno(no).
d008864d 945 bool set_err(int no);
2127e193
GI
946
947 /// Set last error number and default message to any error_info.
948 /// Used by set_err(no).
d008864d 949 bool set_err_var(smart_device::error_info * err, int no);
2127e193
GI
950
951 /// Convert error number into message, used by set_err(no).
952 /// Default implementation returns strerror(no).
953 virtual const char * get_msg_for_errno(int no);
954
955 ///////////////////////////////////////////////////////////////////////////
956 // Device factory:
957
958 /// Return device object for device 'name' with some 'type'.
959 /// 'type' is 0 if not specified by user.
960 /// Return 0 on error.
961 /// Default implementation selects between ata, scsi and custom device.
962 virtual smart_device * get_smart_device(const char * name, const char * type);
963
cfbba5b9 964 /// Fill 'devlist' with devices of some 'type' with device names
2127e193 965 /// specified by some optional 'pattern'.
a86ec89e 966 /// Use platform specific default if 'type' is empty or 0.
2127e193 967 /// Return false on error.
ff28b140 968 /// Default implementation returns false;
2127e193 969 virtual bool scan_smart_devices(smart_device_list & devlist, const char * type,
ff28b140 970 const char * pattern = 0);
2127e193 971
a86ec89e
GI
972 /// Fill 'devlist' with devices of all 'types' with device names
973 /// specified by some optional 'pattern'.
974 /// Use platform specific default if 'types' is empty.
975 /// Return false on error.
976 /// Default implementation calls above function for all types
977 /// and concatenates the results.
978 virtual bool scan_smart_devices(smart_device_list & devlist,
979 const smart_devtype_list & types, const char * pattern = 0);
980
2127e193
GI
981protected:
982 /// Return standard ATA device.
983 virtual ata_device * get_ata_device(const char * name, const char * type) = 0;
984
985 /// Return standard SCSI device.
986 virtual scsi_device * get_scsi_device(const char * name, const char * type) = 0;
987
a86ec89e
GI
988 /// Return standard NVMe device.
989 /// Default implementation returns 0.
990 virtual nvme_device * get_nvme_device(const char * name, const char * type,
991 unsigned nsid);
992
2127e193
GI
993 /// Autodetect device if no device type specified.
994 virtual smart_device * autodetect_smart_device(const char * name) = 0;
995
996 /// Return device for platform specific 'type'.
997 /// Default implementation returns 0.
998 virtual smart_device * get_custom_smart_device(const char * name, const char * type);
999
1000 /// Return valid 'type' args accepted by above.
1001 /// This is called in get_valid_dev_types_str().
54965743
GI
1002 /// Default implementation returns empty string.
1003 virtual std::string get_valid_custom_dev_types_str();
2127e193 1004
ff28b140
TL
1005 /// Return ATA->SCSI of NVMe->SCSI filter for a SAT, SNT or USB 'type'.
1006 /// Uses get_sat_device and get_snt_device.
1007 /// Return 0 and delete 'scsidev' on error.
1008 virtual smart_device * get_scsi_passthrough_device(const char * type, scsi_device * scsidev);
1009
a86ec89e
GI
1010 /// Return ATA->SCSI filter for a SAT or USB 'type'.
1011 /// Device 'scsidev' is used for SCSI access.
1012 /// Return 0 and delete 'scsidev' on error.
2127e193
GI
1013 /// Override only if platform needs special handling.
1014 virtual ata_device * get_sat_device(const char * type, scsi_device * scsidev);
1015 //{ implemented in scsiata.cpp }
1016
ff28b140
TL
1017 /// Return NVMe->SCSI filter for a SNT or USB 'type'.
1018 /// Device 'scsidev' is used for SCSI access.
1019 /// Return 0 and delete 'scsidev' on error.
1020 /// Override only if platform needs special handling.
1021 virtual nvme_device * get_snt_device(const char * type, scsi_device * scsidev);
1022 //{ implemented in scsinvme.cpp }
1023
2127e193
GI
1024public:
1025 /// Try to detect a SAT device behind a SCSI interface.
1026 /// Inquiry data can be passed if available.
1027 /// Return appropriate device if yes, otherwise 0.
1028 /// Override only if platform needs special handling.
1029 virtual ata_device * autodetect_sat_device(scsi_device * scsidev,
1030 const unsigned char * inqdata, unsigned inqsize);
1031 //{ implemented in scsiata.cpp }
1032
1033 /// Get type name for USB device with known VENDOR:PRODUCT ID.
1034 /// Return name if device known and supported, otherwise 0.
1035 virtual const char * get_usb_dev_type_by_id(int vendor_id, int product_id,
1036 int version = -1);
1037 //{ implemented in scsiata.cpp }
1038
1039protected:
1040 /// Set interface to use, must be called from init().
1041 static void set(smart_interface * intf)
1042 { s_instance = intf; }
1043
1044// Implementation
1045private:
1046 smart_device::error_info m_err;
1047
1048 friend smart_interface * smi(); // below
1049 static smart_interface * s_instance; ///< Pointer to the interface object.
1050
ff28b140 1051 // Prevent copy/assignment
2127e193
GI
1052 smart_interface(const smart_interface &);
1053 void operator=(const smart_interface &);
1054};
1055
1056
1057/////////////////////////////////////////////////////////////////////////////
1058// smi()
1059
1060/// Global access to the (usually singleton) smart_interface
1061inline smart_interface * smi()
1062 { return smart_interface::s_instance; }
1063
1064/////////////////////////////////////////////////////////////////////////////
1065
1066#endif // DEV_INTERFACE_H