Successfully locate and open stlinkv1 by usb
[fw/stlink] / src / stlink-sg.h
1 /* 
2  * File:   stlink-sg.h
3  * Author: karl
4  *
5  * Created on October 1, 2011, 11:29 PM
6  */
7
8 #ifndef STLINK_SG_H
9 #define STLINK_SG_H
10
11 #ifdef  __cplusplus
12 extern "C" {
13 #endif
14     
15 #include <libusb-1.0/libusb.h>
16 #include "stlink-common.h"
17     
18         // device access
19 #define RDWR            0
20 #define RO              1
21 #define SG_TIMEOUT_SEC  1 // actually 1 is about 2 sec
22     // Each CDB can be a total of 6, 10, 12, or 16 bytes, later version
23     // of the SCSI standard also allow for variable-length CDBs (min. CDB is 6).
24     // the stlink needs max. 10 bytes.
25 #define CDB_6           6
26 #define CDB_10          10
27 #define CDB_12          12
28 #define CDB_16          16
29
30 #define CDB_SL          10
31
32     // Query data flow direction.
33 #define Q_DATA_OUT      0
34 #define Q_DATA_IN       1
35
36     // The SCSI Request Sense command is used to obtain sense data
37     // (error information) from a target device.
38     // http://en.wikipedia.org/wiki/SCSI_Request_Sense_Command
39 #define SENSE_BUF_LEN           32
40
41
42
43     struct stlink_libsg {
44         libusb_context* libusb_ctx;
45         libusb_device_handle *handle;
46         
47         int sg_fd;
48         int do_scsi_pt_err;
49
50         unsigned char cdb_cmd_blk[CDB_SL];
51
52         int q_data_dir; // Q_DATA_IN, Q_DATA_OUT
53         // the start of the query data in the device memory space
54         uint32_t q_addr;
55
56         // Sense (error information) data
57         unsigned char sense_buf[SENSE_BUF_LEN];
58
59         reg reg;
60     };
61
62     stlink_t* stlink_v1_open(const char *dev_name, const int verbose);
63
64 #ifdef  __cplusplus
65 }
66 #endif
67
68 #endif  /* STLINK_SG_H */
69