Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions include/unicore-mx/usb/byteorder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#ifndef _USB_BYTEORDER_H
#define _USB_BYTEORDER_H

#ifdef __cplusplus
extern "C" {
#endif

#ifndef __LITTLE_ENDIAN_BITFIELD
#define __LITTLE_ENDIAN_BITFIELD
#endif

#ifndef HTONS

#include <stdint.h>
static inline
uint32_t usb_ntohl(uint32_t x){
return ((x)>> 24 & 0xff)
| ((x)>>8 & 0xff00)
| ((x)<<8 & 0xff0000L)
| ((x)<<24 & 0xff000000L)
;
}

static inline
uint16_t usb_ntohs(uint16_t x){
return ((x)>>8 & 0xff) | ((x)<<8 & 0xff00);
}

static inline
uint32_t usb_htonl(uint32_t x) { return usb_ntohl(x); };

static inline
uint16_t usb_htons(uint16_t x) {return usb_ntohs(x); };

#define HTONS(x) usb_htons(x)
#define HTONL(x) usb_htonl(x)
#define NTOHS(x) usb_ntohs(x)
#define NTOHL(x) usb_ntohl(x)

#endif


#ifdef __cplusplus
}
#endif

#endif
41 changes: 12 additions & 29 deletions include/unicore-mx/usb/class/msc.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,35 +86,12 @@ LGPL License Terms @ref lgpl_license
#define USB_MSC_CSW_STATUS_FAILED 1
#define USB_MSC_CSW_STATUS_PHASE_ERROR 2

/* Implemented SCSI Commands */
#define USB_MSC_SCSI_TEST_UNIT_READY 0x00
#define USB_MSC_SCSI_REQUEST_SENSE 0x03
#define USB_MSC_SCSI_FORMAT_UNIT 0x04
#define USB_MSC_SCSI_READ_6 0x08
#define USB_MSC_SCSI_WRITE_6 0x0A
#define USB_MSC_SCSI_INQUIRY 0x12
#define USB_MSC_SCSI_MODE_SENSE_6 0x1A
#define USB_MSC_SCSI_SEND_DIAGNOSTIC 0x1D
#define USB_MSC_SCSI_READ_CAPACITY 0x25
#define USB_MSC_SCSI_READ_10 0x28


/* Required SCSI Commands */

/* Optional SCSI Commands */
#define USB_MSC_SCSI_REPORT_LUNS 0xA0
#define USB_MSC_SCSI_PREVENT_ALLOW_MEDIUM_REMOVAL 0x1E
#define USB_MSC_SCSI_MODE_SELECT_6 0x15
#define USB_MSC_SCSI_MODE_SELECT_10 0x55
#define USB_MSC_SCSI_MODE_SENSE_10 0x5A
#define USB_MSC_SCSI_READ_12 0xA8
#define USB_MSC_SCSI_READ_FORMAT_CAPACITIES 0x23
#define USB_MSC_SCSI_READ_TOC_PMA_ATIP 0x43
#define USB_MSC_SCSI_START_STOP_UNIT 0x1B
#define USB_MSC_SCSI_SYNCHRONIZE_CACHE 0x35
#define USB_MSC_SCSI_VERIFY 0x2F
#define USB_MSC_SCSI_WRITE_10 0x2A
#define USB_MSC_SCSI_WRITE_12 0xAA
#include <unicore-mx/usb/class/msc_scsi.h>

#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif

struct usb_msc_cbw {
uint32_t dCBWSignature;
Expand All @@ -133,6 +110,12 @@ struct usb_msc_csw {
uint8_t bCSWStatus;
} __attribute__((packed));



#ifdef __cplusplus
}
#endif

#endif

/**@}*/
164 changes: 164 additions & 0 deletions include/unicore-mx/usb/class/msc_scsi.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
/** @defgroup usb_msc_scsi_defines USB MSC SCSI API Definitions

@brief <b>Defined Constants and Types for the USB MSC SCSI API Definitions</b>

@ingroup USB_defines

@version 1.0.0

@author @htmlonly &copy; @endhtmlonly 2013
alexrayne <[email protected]>
@date 30 ecember 2016

LGPL License Terms @ref lgpl_license
*/
/*
* Copyright (C) 2016 alexrayne <[email protected]>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor note: INAL, as of my understanding of copyright.
copyright name/year/email for Weston Schmidt and/or Pavol Rusnak need to be added because they authored the file from which some of the content you moved.

*
* This library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef UNICORE_USB_CLASS_MSC_SCSI_H
#define UNICORE_USB_CLASS_MSC_SCSI_H

#include <stdint.h>

/* Implemented SCSI Commands */
#define USB_MSC_SCSI_TEST_UNIT_READY 0x00
#define USB_MSC_SCSI_REQUEST_SENSE 0x03
#define USB_MSC_SCSI_FORMAT_UNIT 0x04
#define USB_MSC_SCSI_READ_6 0x08
#define USB_MSC_SCSI_WRITE_6 0x0A
#define USB_MSC_SCSI_INQUIRY 0x12
#define USB_MSC_SCSI_MODE_SENSE_6 0x1A
#define USB_MSC_SCSI_SEND_DIAGNOSTIC 0x1D
#define USB_MSC_SCSI_READ_FORMAT_CAPACITIES 0x23
#define USB_MSC_SCSI_READ_CAPACITY 0x25
#define USB_MSC_SCSI_READ_10 0x28
/* Required SCSI Commands */

/* Optional SCSI Commands */
#define USB_MSC_SCSI_REPORT_LUNS 0xA0
#define USB_MSC_SCSI_PREVENT_ALLOW_MEDIUM_REMOVAL 0x1E
#define USB_MSC_SCSI_MODE_SELECT_6 0x15
#define USB_MSC_SCSI_MODE_SELECT_10 0x55
#define USB_MSC_SCSI_MODE_SENSE_10 0x5A
#define USB_MSC_SCSI_READ_12 0xA8
#define USB_MSC_SCSI_READ_TOC_PMA_ATIP 0x43
#define USB_MSC_SCSI_START_STOP_UNIT 0x1B
#define USB_MSC_SCSI_SYNCHRONIZE_CACHE 0x35
#define USB_MSC_SCSI_VERIFY 0x2F
#define USB_MSC_SCSI_WRITE_10 0x2A
#define USB_MSC_SCSI_WRITE_12 0xAA

#ifdef __cplusplus
extern "C" {
#endif

//* structires uses net order (big_endian)
typedef uint32_t net_u32_t;
typedef uint16_t net_u16_t;
typedef uint8_t net_u24_t[3];

static inline
void u24_assign(net_u24_t dst, uint32_t x){
dst[2] = x & 0xff;
x >>= 8;
dst[1] = x & 0xff;
x >>= 8;
dst[0] = x & 0xff;
};

static inline
uint32_t u24_asul(net_u24_t x)
{
uint32_t res = (x[0] << 8) | x[1];
res = (res << 8) | x[2];
return res;
};

typedef enum{
scsi_vpd_Supported = 0 //* < Supported Vital Product Data pages
, scsi_vpd_Serial = 0x80 //* < Unit Serial Number page
, scsi_vpd_DevIdentification = 0x83 //* < Device Identification
} scsi_VPD_page;

//* SCSI - INQUIRY structures
typedef struct {
uint8_t op_code;
uint8_t evpd;
uint8_t page_code; //* \see scsi_VPD_page
net_u16_t alloc_len;
} __attribute__((packed)) usb_inquiry_cmd ;


typedef enum {
/** A peripheral device having the specified peripheral device type is connected to this logical unit. If the
device server is unable to determine whether or not a peripheral device is connected, it also shall use
this peripheral qualifier. This peripheral qualifier does not mean that the peripheral device connected
to the logical unit is ready for access.*/
scsi_pqAvail = 0
/** A peripheral device having the specified peripheral device type is not connected to this logical unit.
However, the device server is capable of supporting the specified peripheral device type on this logi-
cal unit. */
, scsi_pqRemoved = 1
/** The device server is not capable of supporting a peripheral device on this logical unit. For this periph-
eral qualifier the peripheral device type shall be set to 1Fh. All other peripheral device type values are
reserved for this peripheral qualifier */
, scsi_pqNoSupport = 3
} scsi_periferial_qualify;

typedef enum {
scsi_dtDirectBlock = 0 //* < Direct access block device (e.g., magnetic disk)
, scsi_dtSeqBlock = 1 //* < Sequential-access device (e.g., magnetic tape)
, scsi_dtWriteOnce = 4 //* < Write-once device (e.g., some optical disks)
, scsi_dtCDDVD = 5 //* < CD/DVD device
, scsi_dtOptical = 7 //* <Optical memory device (e.g., some optical disks)
, scsi_dtRAID = 0xc //* <Storage array controller device (e.g., RAID)
, scsi_dtSimleDirect= 0xe //* <Simplified direct-access device (e.g., magnetic disk)
} scsi_periferial_devtype;

#define SCSI_DEVICE_CODE(type, qual) ( (((qual) & 0x7)<<5) | ((type) & 0x1f) )

typedef struct {
uint8_t device_code; //* \see SCSI_DEVICE_CODE
uint8_t page_code; //* \see scsi_VPD_page
uint8_t page_len;
} __attribute__((packed)) usb_inquiry_ack ;



//* SCSI - READ FORMAT CAPACITIES structures
typedef struct {
net_u24_t dummy;
uint8_t list_len;
} __attribute__((packed)) usb_msc_rfc_capacity_list_header ;

typedef enum {
rfc_dc_Unfomatted = 1 //* < Unformatted Media - Maximum formattable capacity for this cartridge
, rfc_dc_Fomatted = 2 //* < Formatted Media - Current media capacity
, rfc_dc_NoMedia = 3 //* < No Cartridge in Drive - Maximum formattable capacity for any cartridge
} usb_msc_rfc_capacity_descriptor_code;

typedef struct {
net_u32_t blocks_count;
uint8_t code; //* < \see usb_msc_rfc_capacity_descriptor_code
net_u24_t block_size;
} __attribute__((packed)) usb_msc_rfc_capacity_descriptor;

#ifdef __cplusplus
}
#endif

#endif//UNICORE_USB_CLASS_MSC_SCSI_H
50 changes: 47 additions & 3 deletions include/unicore-mx/usbd/class/msc.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@
#include <unicore-mx/usbd/usbd.h>
#include <unicore-mx/usb/class/msc.h>

#ifdef __cplusplus
extern "C" {
#endif

typedef struct usbd_msc usbd_msc;
typedef struct usbd_msc_backend usbd_msc_backend;

Expand All @@ -64,18 +68,28 @@ typedef struct usbd_msc_backend usbd_msc_backend;
* @param lock Lock. Optional - can be NULL
* @param unlock Unlock. Optional - can be NULL
*/
typedef uint32_t msc_lba_t;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Look ok to me but,

Tab between "uint32_t" "msc_lba_t;"
(Not nitpicking) but a space is sufficient.
In other part of the code (was a typedef), you use a space instead.
Please read more code of the project and maintain the same pattern/code style.

typedef unsigned msc_lun_t;

struct usbd_msc_backend {
const char *vendor_id;
const char *product_id;
const char *product_rev;
uint32_t block_count;
msc_lba_t block_count;
int (*read_block)(const usbd_msc_backend *backend,
uint32_t lba, void *copy_to);
msc_lba_t lba, void *copy_to);
int (*write_block)(const usbd_msc_backend *backend,
uint32_t lba, const void *copy_from);
msc_lba_t lba, const void *copy_from);
int (*format_unit)(const usbd_msc_backend *backend);
int (*lock)(void);
int (*unlock)(void);
msc_lba_t (*unit_blocks_count)(const usbd_msc_backend *self);
int (*inquiry_page)(const usbd_msc_backend *self
//, usb_inquiry_cmd* cmd
, int vpd_page //* < \value -1 request standart iquiry page (EVPD=0)
, unsigned alloc_limit //* < TODO limited by sector size now, due use MCS sector buffer as buf
, void* buf //* < buffer for inquiry data fill to
);
};

usbd_msc *usbd_msc_init(usbd_device *dev,
Expand All @@ -91,6 +105,36 @@ void usbd_msc_set_config(usbd_msc *ms,

void usbd_msc_start(usbd_msc *ms);

static inline
msc_lba_t usbd_msc_blocks(const usbd_msc_backend *u)
{
if (u->unit_blocks_count != NULL)
return (u->unit_blocks_count)(u);
return u->block_count;
}



//* INQUIRY support routines.
//* this resources linked as weak, and can be ovverriden in user code.
int usbd_scsi_inquiry_page(const usbd_msc_backend *self
//, usb_inquiry_cmd* cmd
, int vpd_page //* < \value -1 request standart iquiry page (EVPD=0)
, unsigned alloc_limit //* < TODO limited by sector size now, due use MCS sector buffer as buf
, void* buf //* < buffer for inquiry data fill to
);
int usbd_scsi_inquiry_standart_page(const usbd_msc_backend *self, unsigned alloc_limit, void* buf);
int usbd_scsi_inquiry_evpd_supports(const usbd_msc_backend *self, unsigned alloc_limit, void* buf);
int usbd_scsi_inquiry_evpd_serial(const usbd_msc_backend *self, unsigned alloc_limit, void* buf);

extern const uint8_t usbd_scsi_inquiry_evpd_supports_Data[];
extern const uint8_t usbd_scsi_inquiry_evpd_serial_Data[];


#ifdef __cplusplus
}
#endif

#endif

/**@}*/
Loading