summaryrefslogtreecommitdiff
path: root/src/stlink-lib/sg.h
blob: f34b2e12202c95e49bc3d75f4af7c9c7643e2f4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
 * File:   sg.h
 * Author: karl
 */

#ifndef STLINK_SG_H
#define STLINK_SG_H

#include <stlink.h>
#include <libusb_settings.h>

#ifdef __cplusplus
extern "C" {
#endif

/* Device access */
#define RDWR        0
#define RO      1
#define SG_TIMEOUT_SEC  1 // actually 1 is about 2 sec
#define SG_TIMEOUT_MSEC 3 * 1000

// Each CDB can be a total of 6, 10, 12, or 16 bytes, later version of the SCSI standard
// also allow for variable-length CDBs (min. CDB is 6). The stlink needs max. 10 bytes.
#define CDB_6       6
#define CDB_10      10
#define CDB_12      12
#define CDB_16      16

#define CDB_SL      10

/* Query data flow direction */
#define Q_DATA_OUT  0
#define Q_DATA_IN   1

// The SCSI Request Sense command is used to obtain sense data (error information) from
// a target device. (http://en.wikipedia.org/wiki/SCSI_Request_Sense_Command)
#define SENSE_BUF_LEN       32

struct stlink_libsg {
    libusb_context* libusb_ctx;
    libusb_device_handle *usb_handle;
    unsigned ep_rep;
    unsigned ep_req;

    int sg_fd;
    int do_scsi_pt_err;

    unsigned char cdb_cmd_blk[CDB_SL];

    int q_data_dir; // Q_DATA_IN, Q_DATA_OUT
    // the start of the query data in the device memory space
    uint32_t q_addr;

    // Sense (error information) data
    // obsolete, this was fed to the scsi tools
    unsigned char sense_buf[SENSE_BUF_LEN];

    struct stlink_reg reg;
};

stlink_t* stlink_v1_open(const int verbose, int reset);

#ifdef __cplusplus
}
#endif

#endif // STLINK_SG_H