FreeBSD kernel usb device Code
cfumass.c File Reference
#include <sys/cdefs.h>
#include <sys/param.h>
#include <sys/bus.h>
#include <sys/kernel.h>
#include <sys/lock.h>
#include <sys/module.h>
#include <sys/mutex.h>
#include <sys/refcount.h>
#include <sys/stdint.h>
#include <sys/sysctl.h>
#include <sys/systm.h>
#include <dev/usb/usb.h>
#include <dev/usb/usbdi.h>
#include "usbdevs.h"
#include "usb_if.h"
#include <cam/scsi/scsi_all.h>
#include <cam/scsi/scsi_da.h>
#include <cam/ctl/ctl_io.h>
#include <cam/ctl/ctl.h>
#include <cam/ctl/ctl_backend.h>
#include <cam/ctl/ctl_error.h>
#include <cam/ctl/ctl_frontend.h>
#include <cam/ctl/ctl_debug.h>
#include <cam/ctl/ctl_ha.h>
#include <cam/ctl/ctl_ioctl.h>
#include <cam/ctl/ctl_private.h>
Include dependency graph for cfumass.c:

Go to the source code of this file.

Data Structures

struct  cfumass_cbw_t
 
struct  cfumass_csw_t
 
struct  cfumass_softc
 

Macros

#define CFUMASS_BULK_SIZE   (1U << 17) /* bytes */
 
#define CFUMASS_T_COMMAND   0
 
#define CFUMASS_T_DATA_OUT   1
 
#define CFUMASS_T_DATA_IN   2
 
#define CFUMASS_T_STATUS   3
 
#define CFUMASS_T_MAX   4
 
#define UR_RESET   0xff /* Bulk-Only Mass Storage Reset */
 
#define UR_GET_MAX_LUN   0xfe /* Get Max LUN */
 
#define CBWSIGNATURE   0x43425355 /* "USBC" */
 
#define CBWFLAGS_OUT   0x00
 
#define CBWFLAGS_IN   0x80
 
#define CBWCBLENGTH   16
 
#define CFUMASS_CBW_SIZE   31
 
#define CSWSIGNATURE   0x53425355 /* "USBS" */
 
#define CSWSTATUS_GOOD   0x0
 
#define CSWSTATUS_FAILED   0x1
 
#define CSWSTATUS_PHASE   0x2
 
#define CFUMASS_CSW_SIZE   13
 
#define CFUMASS_DEBUG(S, X, ...)
 
#define CFUMASS_WARN(S, X, ...)
 
#define CFUMASS_LOCK(X)   mtx_lock(&X->sc_mtx)
 
#define CFUMASS_UNLOCK(X)   mtx_unlock(&X->sc_mtx)
 

Functions

 __FBSDID ("$FreeBSD$")
 
 SYSCTL_NODE (_hw_usb, OID_AUTO, cfumass, CTLFLAG_RW|CTLFLAG_MPSAFE, 0, "CAM Target Layer USB Mass Storage Frontend")
 
 SYSCTL_INT (_hw_usb_cfumass, OID_AUTO, debug, CTLFLAG_RWTUN, &debug, 1, "Enable debug messages")
 
 SYSCTL_INT (_hw_usb_cfumass, OID_AUTO, max_lun, CTLFLAG_RWTUN, &max_lun, 1, "Maximum advertised LUN number")
 
 SYSCTL_INT (_hw_usb_cfumass, OID_AUTO, ignore_stop, CTLFLAG_RWTUN, &ignore_stop, 1, "Ignore START STOP UNIT with START and LOEJ bits cleared")
 
 CTASSERT (sizeof(struct cfumass_cbw_t)==CFUMASS_CBW_SIZE)
 
 CTASSERT (sizeof(struct cfumass_csw_t)==CFUMASS_CSW_SIZE)
 
 DRIVER_MODULE (cfumass, uhub, cfumass_driver, cfumass_devclass, NULL, 0)
 
 MODULE_VERSION (cfumass, 0)
 
 MODULE_DEPEND (cfumass, usb, 1, 1, 1)
 
 MODULE_DEPEND (cfumass, usb_template, 1, 1, 1)
 
static int cfumass_init (void)
 
static int cfumass_shutdown (void)
 
static void cfumass_online (void *arg)
 
static void cfumass_offline (void *arg)
 
static void cfumass_datamove (union ctl_io *io)
 
static void cfumass_done (union ctl_io *io)
 
 CTL_FRONTEND_DECLARE (ctlcfumass, cfumass_frontend)
 
static void cfumass_transfer_start (struct cfumass_softc *sc, uint8_t xfer_index)
 
static void cfumass_terminate (struct cfumass_softc *sc)
 
static int cfumass_probe (device_t dev)
 
static int cfumass_attach (device_t dev)
 
static int cfumass_detach (device_t dev)
 
static int cfumass_suspend (device_t dev)
 
static int cfumass_resume (device_t dev)
 
static void cfumass_transfer_stop_and_drain (struct cfumass_softc *sc, uint8_t xfer_index)
 
static int cfumass_handle_request (device_t dev, const void *preq, void **pptr, uint16_t *plen, uint16_t offset, uint8_t *pstate)
 
static int cfumass_quirk (struct cfumass_softc *sc, unsigned char *cdb, int cdb_len)
 
static void cfumass_t_command_callback (struct usb_xfer *xfer, usb_error_t usb_error)
 
static void cfumass_t_data_callback (struct usb_xfer *xfer, usb_error_t usb_error)
 
static void cfumass_t_status_callback (struct usb_xfer *xfer, usb_error_t usb_error)
 
static void cfumass_online (void *arg __unused)
 
static void cfumass_offline (void *arg __unused)
 

Variables

static int debug = 1
 
static int max_lun = 0
 
static int ignore_stop = 1
 
struct ctl_port cfumass_port
 
bool cfumass_port_online
 
volatile u_int cfumass_refcount
 
struct cfumass_cbw_t __packed
 
static device_probe_t cfumass_probe
 
static device_attach_t cfumass_attach
 
static device_detach_t cfumass_detach
 
static device_suspend_t cfumass_suspend
 
static device_resume_t cfumass_resume
 
static usb_handle_request_t cfumass_handle_request
 
static usb_callback_t cfumass_t_command_callback
 
static usb_callback_t cfumass_t_data_callback
 
static usb_callback_t cfumass_t_status_callback
 
static device_method_t cfumass_methods []
 
static driver_t cfumass_driver
 
static devclass_t cfumass_devclass
 
static struct usb_config cfumass_config [CFUMASS_T_MAX]
 
static struct ctl_frontend cfumass_frontend
 

Macro Definition Documentation

◆ CBWCBLENGTH

#define CBWCBLENGTH   16

Definition at line 125 of file cfumass.c.

◆ CBWFLAGS_IN

#define CBWFLAGS_IN   0x80

Definition at line 122 of file cfumass.c.

◆ CBWFLAGS_OUT

#define CBWFLAGS_OUT   0x00

Definition at line 121 of file cfumass.c.

◆ CBWSIGNATURE

#define CBWSIGNATURE   0x43425355 /* "USBC" */

Definition at line 117 of file cfumass.c.

◆ CFUMASS_BULK_SIZE

#define CFUMASS_BULK_SIZE   (1U << 17) /* bytes */

Definition at line 94 of file cfumass.c.

◆ CFUMASS_CBW_SIZE

#define CFUMASS_CBW_SIZE   31

Definition at line 129 of file cfumass.c.

◆ CFUMASS_CSW_SIZE

#define CFUMASS_CSW_SIZE   13

Definition at line 146 of file cfumass.c.

◆ CFUMASS_DEBUG

#define CFUMASS_DEBUG (   S,
  X,
  ... 
)
Value:
do { \
if (debug > 1) { \
device_printf(S->sc_dev, "%s: " X "\n", \
__func__, ## __VA_ARGS__); \
} \
} while (0)
@ X
Definition: atp.c:588
static int debug
Definition: cfumass.c:73

Definition at line 294 of file cfumass.c.

◆ CFUMASS_LOCK

#define CFUMASS_LOCK (   X)    mtx_lock(&X->sc_mtx)

Definition at line 310 of file cfumass.c.

◆ CFUMASS_T_COMMAND

#define CFUMASS_T_COMMAND   0

Definition at line 100 of file cfumass.c.

◆ CFUMASS_T_DATA_IN

#define CFUMASS_T_DATA_IN   2

Definition at line 102 of file cfumass.c.

◆ CFUMASS_T_DATA_OUT

#define CFUMASS_T_DATA_OUT   1

Definition at line 101 of file cfumass.c.

◆ CFUMASS_T_MAX

#define CFUMASS_T_MAX   4

Definition at line 104 of file cfumass.c.

◆ CFUMASS_T_STATUS

#define CFUMASS_T_STATUS   3

Definition at line 103 of file cfumass.c.

◆ CFUMASS_UNLOCK

#define CFUMASS_UNLOCK (   X)    mtx_unlock(&X->sc_mtx)

Definition at line 311 of file cfumass.c.

◆ CFUMASS_WARN

#define CFUMASS_WARN (   S,
  X,
  ... 
)
Value:
do { \
if (debug > 0) { \
device_printf(S->sc_dev, "WARNING: %s: " X "\n",\
__func__, ## __VA_ARGS__); \
} \
} while (0)

Definition at line 302 of file cfumass.c.

◆ CSWSIGNATURE

#define CSWSIGNATURE   0x53425355 /* "USBS" */

Definition at line 137 of file cfumass.c.

◆ CSWSTATUS_FAILED

#define CSWSTATUS_FAILED   0x1

Definition at line 142 of file cfumass.c.

◆ CSWSTATUS_GOOD

#define CSWSTATUS_GOOD   0x0

Definition at line 141 of file cfumass.c.

◆ CSWSTATUS_PHASE

#define CSWSTATUS_PHASE   0x2

Definition at line 143 of file cfumass.c.

◆ UR_GET_MAX_LUN

#define UR_GET_MAX_LUN   0xfe /* Get Max LUN */

Definition at line 110 of file cfumass.c.

◆ UR_RESET

#define UR_RESET   0xff /* Bulk-Only Mass Storage Reset */

Definition at line 109 of file cfumass.c.

Function Documentation

◆ __FBSDID()

__FBSDID ( "$FreeBSD$"  )

◆ cfumass_attach()

◆ cfumass_datamove()

static void cfumass_datamove ( union ctl_io *  io)
static

Definition at line 849 of file cfumass.c.

References CBWFLAGS_IN, CBWFLAGS_OUT, CFUMASS_DEBUG, CFUMASS_LOCK, CFUMASS_T_DATA_IN, CFUMASS_T_DATA_OUT, cfumass_transfer_start(), CFUMASS_UNLOCK, CFUMASS_WARN, fail, cfumass_softc::sc_ctl_io, and cfumass_softc::sc_current_flags.

Referenced by cfumass_init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ cfumass_detach()

static int cfumass_detach ( device_t  dev)
static

◆ cfumass_done()

static void cfumass_done ( union ctl_io *  io)
static

Definition at line 895 of file cfumass.c.

References CFUMASS_DEBUG, CFUMASS_LOCK, CFUMASS_T_STATUS, cfumass_transfer_start(), CFUMASS_UNLOCK, cfumass_softc::sc_ctl_io, cfumass_softc::sc_current_status, and cfumass_softc::sc_queued.

Referenced by cfumass_init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ cfumass_handle_request()

static int cfumass_handle_request ( device_t  dev,
const void *  preq,
void **  pptr,
uint16_t *  plen,
uint16_t  offset,
uint8_t *  pstate 
)
static

◆ cfumass_init()

int cfumass_init ( void  )
static

Definition at line 948 of file cfumass.c.

References cfumass_datamove(), cfumass_done(), cfumass_frontend, cfumass_offline(), cfumass_online(), cfumass_port, cfumass_port_online, cfumass_refcount, and error.

Here is the call graph for this function:

◆ cfumass_offline() [1/2]

static void cfumass_offline ( void *arg  __unused)
static

Definition at line 842 of file cfumass.c.

References cfumass_port_online.

◆ cfumass_offline() [2/2]

static void cfumass_offline ( void *  arg)
static

Referenced by cfumass_init().

Here is the caller graph for this function:

◆ cfumass_online() [1/2]

static void cfumass_online ( void *arg  __unused)
static

Definition at line 835 of file cfumass.c.

References cfumass_port_online.

◆ cfumass_online() [2/2]

static void cfumass_online ( void *  arg)
static

Referenced by cfumass_init().

Here is the caller graph for this function:

◆ cfumass_probe()

static int cfumass_probe ( device_t  dev)
static

Definition at line 318 of file cfumass.c.

References dev, id, usb_attach_arg::iface, UICLASS_MASS, UIPROTO_MASS_BBB, UISUBCLASS_SCSI, usb_attach_arg::usb_mode, USB_MODE_DEVICE, and usbd_get_interface_descriptor().

Here is the call graph for this function:

◆ cfumass_quirk()

static int cfumass_quirk ( struct cfumass_softc sc,
unsigned char *  cdb,
int  cdb_len 
)
static

Definition at line 559 of file cfumass.c.

References CFUMASS_DEBUG, CFUMASS_T_STATUS, cfumass_transfer_start(), CFUMASS_WARN, ignore_stop, and cfumass_softc::sc_current_status.

Referenced by cfumass_t_command_callback().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ cfumass_resume()

static int cfumass_resume ( device_t  dev)
static

Definition at line 438 of file cfumass.c.

References CFUMASS_DEBUG, and dev.

◆ cfumass_shutdown()

int cfumass_shutdown ( void  )
static

Definition at line 978 of file cfumass.c.

References cfumass_port, cfumass_refcount, debug, and error.

◆ cfumass_suspend()

static int cfumass_suspend ( device_t  dev)
static

Definition at line 427 of file cfumass.c.

References CFUMASS_DEBUG, and dev.

◆ cfumass_t_command_callback()

◆ cfumass_t_data_callback()

◆ cfumass_t_status_callback()

◆ cfumass_terminate()

static void cfumass_terminate ( struct cfumass_softc sc)
static

Definition at line 466 of file cfumass.c.

References CFUMASS_DEBUG, CFUMASS_T_COMMAND, CFUMASS_T_DATA_IN, CFUMASS_T_DATA_OUT, CFUMASS_T_STATUS, cfumass_transfer_stop_and_drain(), cfumass_softc::sc_ctl_io, cfumass_softc::sc_mtx, and cfumass_softc::sc_queued.

Referenced by cfumass_detach(), and cfumass_handle_request().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ cfumass_transfer_start()

static void cfumass_transfer_start ( struct cfumass_softc sc,
uint8_t  xfer_index 
)
static

Definition at line 449 of file cfumass.c.

References cfumass_softc::sc_xfer, and usbd_transfer_start().

Referenced by cfumass_attach(), cfumass_datamove(), cfumass_done(), cfumass_handle_request(), cfumass_quirk(), and cfumass_t_status_callback().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ cfumass_transfer_stop_and_drain()

static void cfumass_transfer_stop_and_drain ( struct cfumass_softc sc,
uint8_t  xfer_index 
)
static

Definition at line 456 of file cfumass.c.

References CFUMASS_LOCK, CFUMASS_UNLOCK, cfumass_softc::sc_xfer, usbd_transfer_drain(), and usbd_transfer_stop().

Referenced by cfumass_terminate().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ CTASSERT() [1/2]

CTASSERT ( sizeof(struct cfumass_cbw_t = =CFUMASS_CBW_SIZE)

◆ CTASSERT() [2/2]

CTASSERT ( sizeof(struct cfumass_csw_t = =CFUMASS_CSW_SIZE)

◆ CTL_FRONTEND_DECLARE()

CTL_FRONTEND_DECLARE ( ctlcfumass  ,
cfumass_frontend   
)

◆ DRIVER_MODULE()

DRIVER_MODULE ( cfumass  ,
uhub  ,
cfumass_driver  ,
cfumass_devclass  ,
NULL  ,
 
)

◆ MODULE_DEPEND() [1/2]

MODULE_DEPEND ( cfumass  ,
usb  ,
,
,
 
)

◆ MODULE_DEPEND() [2/2]

MODULE_DEPEND ( cfumass  ,
usb_template  ,
,
,
 
)

◆ MODULE_VERSION()

MODULE_VERSION ( cfumass  ,
 
)

◆ SYSCTL_INT() [1/3]

SYSCTL_INT ( _hw_usb_cfumass  ,
OID_AUTO  ,
debug  ,
CTLFLAG_RWTUN  ,
debug,
,
"Enable debug messages"   
)

◆ SYSCTL_INT() [2/3]

SYSCTL_INT ( _hw_usb_cfumass  ,
OID_AUTO  ,
ignore_stop  ,
CTLFLAG_RWTUN  ,
ignore_stop,
,
"Ignore START STOP UNIT with START and LOEJ bits cleared"   
)

◆ SYSCTL_INT() [3/3]

SYSCTL_INT ( _hw_usb_cfumass  ,
OID_AUTO  ,
max_lun  ,
CTLFLAG_RWTUN  ,
max_lun,
,
"Maximum advertised LUN number"   
)

◆ SYSCTL_NODE()

SYSCTL_NODE ( _hw_usb  ,
OID_AUTO  ,
cfumass  ,
CTLFLAG_RW|  CTLFLAG_MPSAFE,
,
"CAM Target Layer USB Mass Storage Frontend"   
)

Variable Documentation

◆ __packed

◆ cfumass_attach

device_attach_t cfumass_attach
static

Definition at line 197 of file cfumass.c.

◆ cfumass_config

struct usb_config cfumass_config[CFUMASS_T_MAX]
static

Definition at line 234 of file cfumass.c.

Referenced by cfumass_attach().

◆ cfumass_detach

device_detach_t cfumass_detach
static

Definition at line 198 of file cfumass.c.

◆ cfumass_devclass

devclass_t cfumass_devclass
static

Definition at line 227 of file cfumass.c.

◆ cfumass_driver

driver_t cfumass_driver
static
Initial value:
= {
.name = "cfumass",
.methods = cfumass_methods,
.size = sizeof(struct cfumass_softc),
}
static device_method_t cfumass_methods[]
Definition: cfumass.c:207

Definition at line 221 of file cfumass.c.

◆ cfumass_frontend

struct ctl_frontend cfumass_frontend
static
Initial value:
= {
.name = "umass",
.init = cfumass_init,
.shutdown = cfumass_shutdown,
}
static int cfumass_init(void)
Definition: cfumass.c:948
static int cfumass_shutdown(void)
Definition: cfumass.c:978

Definition at line 287 of file cfumass.c.

Referenced by cfumass_init().

◆ cfumass_handle_request

usb_handle_request_t cfumass_handle_request
static

Definition at line 201 of file cfumass.c.

◆ cfumass_methods

device_method_t cfumass_methods[]
static
Initial value:
= {
DEVMETHOD(device_probe, cfumass_probe),
DEVMETHOD(device_attach, cfumass_attach),
DEVMETHOD(device_detach, cfumass_detach),
DEVMETHOD(device_suspend, cfumass_suspend),
DEVMETHOD(device_resume, cfumass_resume),
DEVMETHOD_END
}
static device_resume_t cfumass_resume
Definition: cfumass.c:200
static device_probe_t cfumass_probe
Definition: cfumass.c:196
static usb_handle_request_t cfumass_handle_request
Definition: cfumass.c:201
static device_detach_t cfumass_detach
Definition: cfumass.c:198
static device_suspend_t cfumass_suspend
Definition: cfumass.c:199
static device_attach_t cfumass_attach
Definition: cfumass.c:197
static usb_error_t usb_handle_request(struct usb_xfer *)

Definition at line 207 of file cfumass.c.

◆ cfumass_port

struct ctl_port cfumass_port

◆ cfumass_port_online

bool cfumass_port_online

◆ cfumass_probe

device_probe_t cfumass_probe
static

Definition at line 196 of file cfumass.c.

◆ cfumass_refcount

volatile u_int cfumass_refcount

Definition at line 91 of file cfumass.c.

Referenced by cfumass_attach(), cfumass_detach(), cfumass_init(), and cfumass_shutdown().

◆ cfumass_resume

device_resume_t cfumass_resume
static

Definition at line 200 of file cfumass.c.

◆ cfumass_suspend

device_suspend_t cfumass_suspend
static

Definition at line 199 of file cfumass.c.

◆ cfumass_t_command_callback

usb_callback_t cfumass_t_command_callback
static

Definition at line 203 of file cfumass.c.

◆ cfumass_t_data_callback

usb_callback_t cfumass_t_data_callback
static

Definition at line 204 of file cfumass.c.

◆ cfumass_t_status_callback

usb_callback_t cfumass_t_status_callback
static

Definition at line 205 of file cfumass.c.

◆ debug

int debug = 1
static

Definition at line 73 of file cfumass.c.

Referenced by cfumass_shutdown().

◆ ignore_stop

int ignore_stop = 1
static

Definition at line 79 of file cfumass.c.

Referenced by cfumass_quirk().

◆ max_lun

int max_lun = 0
static

Definition at line 76 of file cfumass.c.

Referenced by cfumass_handle_request().