HelenOS sources
This source file includes following definitions.
- bd_srv_usbmast
- usbmast_device_gone
- usbmast_device_remove
- usbmast_device_add
- usbmast_fun_create
- usbmast_bd_connection
- usbmast_bd_open
- usbmast_bd_close
- usbmast_bd_read_blocks
- usbmast_bd_sync_cache
- usbmast_bd_write_blocks
- usbmast_bd_get_block_size
- usbmast_bd_get_num_blocks
- main
#include <as.h>
#include <async.h>
#include <bd_srv.h>
#include <macros.h>
#include <usb/dev/driver.h>
#include <usb/debug.h>
#include <usb/classes/classes.h>
#include <usb/classes/massstor.h>
#include <errno.h>
#include <io/logctl.h>
#include <str_error.h>
#include "cmdw.h"
#include "bo_trans.h"
#include "scsi_ms.h"
#include "usbmast.h"
#define NAME "usbmast"
static const usb_endpoint_description_t bulk_in_ep = {
.transfer_type = USB_TRANSFER_BULK,
.direction = USB_DIRECTION_IN,
.interface_class = USB_CLASS_MASS_STORAGE,
.interface_subclass = USB_MASSSTOR_SUBCLASS_SCSI,
.interface_protocol = USB_MASSSTOR_PROTOCOL_BBB,
.flags = 0
};
static const usb_endpoint_description_t bulk_out_ep = {
.transfer_type = USB_TRANSFER_BULK,
.direction = USB_DIRECTION_OUT,
.interface_class = USB_CLASS_MASS_STORAGE,
.interface_subclass = USB_MASSSTOR_SUBCLASS_SCSI,
.interface_protocol = USB_MASSSTOR_PROTOCOL_BBB,
.flags = 0
};
static const usb_endpoint_description_t *mast_endpoints[] = {
&bulk_in_ep,
&bulk_out_ep,
NULL
};
static errno_t usbmast_fun_create(usbmast_dev_t *mdev, unsigned lun);
static void usbmast_bd_connection(ipc_call_t *icall, void *arg);
static errno_t usbmast_bd_open(bd_srvs_t *, bd_srv_t *);
static errno_t usbmast_bd_close(bd_srv_t *);
static errno_t usbmast_bd_read_blocks(bd_srv_t *, aoff64_t, size_t, void *, size_t);
static errno_t usbmast_bd_sync_cache(bd_srv_t *, aoff64_t, size_t);
static errno_t usbmast_bd_write_blocks(bd_srv_t *, aoff64_t, size_t, const void *, size_t);
static errno_t usbmast_bd_get_block_size(bd_srv_t *, size_t *);
static errno_t usbmast_bd_get_num_blocks(bd_srv_t *, aoff64_t *);
static bd_ops_t usbmast_bd_ops = {
.open = usbmast_bd_open,
.close = usbmast_bd_close,
.read_blocks = usbmast_bd_read_blocks,
.sync_cache = usbmast_bd_sync_cache,
.write_blocks = usbmast_bd_write_blocks,
.get_block_size = usbmast_bd_get_block_size,
.get_num_blocks = usbmast_bd_get_num_blocks
};
static usbmast_fun_t *bd_srv_usbmast(bd_srv_t *bd)
{
return (usbmast_fun_t *) bd->srvs->sarg;
}
static errno_t usbmast_device_gone(usb_device_t *dev)
{
usbmast_dev_t *mdev = usb_device_data_get(dev);
assert(mdev);
for (size_t i = 0; i < mdev->lun_count; ++i) {
const errno_t rc = ddf_fun_unbind(mdev->luns[i]);
if (rc != EOK) {
usb_log_error("Failed to unbind LUN function %zu: "
"%s\n", i, str_error(rc));
return rc;
}
ddf_fun_destroy(mdev->luns[i]);
mdev->luns[i] = NULL;
}
free(mdev->luns);
return EOK;
}
static errno_t usbmast_device_remove(usb_device_t *dev)
{
return ENOTSUP;
}
static errno_t usbmast_device_add(usb_device_t *dev)
{
errno_t rc;
usbmast_dev_t *mdev = NULL;
unsigned i;
usb_endpoint_mapping_t *epm_in =
usb_device_get_mapped_ep_desc(dev, &bulk_in_ep);
usb_endpoint_mapping_t *epm_out =
usb_device_get_mapped_ep_desc(dev, &bulk_out_ep);
if (!epm_in || !epm_out || !epm_in->present || !epm_out->present) {
usb_log_error("Required EPs were not mapped.");
return ENOENT;
}
mdev = usb_device_data_alloc(dev, sizeof(usbmast_dev_t));
if (mdev == NULL) {
usb_log_error("Failed allocating softstate.");
return ENOMEM;
}
mdev->usb_dev = dev;
usb_log_info("Initializing mass storage `%s'.",
usb_device_get_name(dev));
usb_log_debug("Bulk in endpoint: %d [%zuB].",
epm_in->pipe.desc.endpoint_no, epm_in->pipe.desc.max_transfer_size);
usb_log_debug("Bulk out endpoint: %d [%zuB].",
epm_out->pipe.desc.endpoint_no, epm_out->pipe.desc.max_transfer_size);
usb_log_debug("Get LUN count...");
mdev->lun_count = usb_masstor_get_lun_count(mdev);
mdev->luns = calloc(mdev->lun_count, sizeof(ddf_fun_t *));
if (mdev->luns == NULL) {
usb_log_error("Failed allocating luns table.");
return ENOMEM;
}
mdev->bulk_in_pipe = &epm_in->pipe;
mdev->bulk_out_pipe = &epm_out->pipe;
for (i = 0; i < mdev->lun_count; i++) {
rc = usbmast_fun_create(mdev, i);
if (rc != EOK)
goto error;
}
return EOK;
error:
for (size_t i = 0; i < mdev->lun_count; ++i) {
if (mdev->luns[i] == NULL)
continue;
const errno_t rc = ddf_fun_unbind(mdev->luns[i]);
if (rc != EOK) {
usb_log_warning("Failed to unbind LUN function %zu: "
"%s.\n", i, str_error(rc));
}
ddf_fun_destroy(mdev->luns[i]);
}
free(mdev->luns);
return rc;
}
static errno_t usbmast_fun_create(usbmast_dev_t *mdev, unsigned lun)
{
errno_t rc;
char *fun_name = NULL;
ddf_fun_t *fun = NULL;
usbmast_fun_t *mfun = NULL;
bool bound = false;
if (asprintf(&fun_name, "l%u", lun) < 0) {
usb_log_error("Out of memory.");
rc = ENOMEM;
goto error;
}
fun = usb_device_ddf_fun_create(mdev->usb_dev, fun_exposed, fun_name);
if (fun == NULL) {
usb_log_error("Failed to create DDF function %s.", fun_name);
rc = ENOMEM;
goto error;
}
mfun = ddf_fun_data_alloc(fun, sizeof(usbmast_fun_t));
if (mfun == NULL) {
usb_log_error("Failed allocating softstate.");
rc = ENOMEM;
goto error;
}
mfun->ddf_fun = fun;
mfun->mdev = mdev;
mfun->lun = lun;
bd_srvs_init(&mfun->bds);
mfun->bds.ops = &usbmast_bd_ops;
mfun->bds.sarg = mfun;
ddf_fun_set_conn_handler(fun, usbmast_bd_connection);
usb_log_debug("Inquire...");
usbmast_inquiry_data_t inquiry;
rc = usbmast_inquiry(mfun, &inquiry);
if (rc != EOK) {
usb_log_warning("Failed to inquire device `%s': %s.",
usb_device_get_name(mdev->usb_dev), str_error(rc));
rc = EIO;
goto error;
}
usb_log_info("Mass storage `%s' LUN %u: "
"%s by %s rev. %s is %s (%s).\n",
usb_device_get_name(mdev->usb_dev),
lun,
inquiry.product,
inquiry.vendor,
inquiry.revision,
usbmast_scsi_dev_type_str(inquiry.device_type),
inquiry.removable ? "removable" : "non-removable");
uint32_t nblocks, block_size;
rc = usbmast_read_capacity(mfun, &nblocks, &block_size);
if (rc != EOK) {
usb_log_warning("Failed to read capacity, device `%s': %s.",
usb_device_get_name(mdev->usb_dev), str_error(rc));
rc = EIO;
goto error;
}
usb_log_info("Read Capacity: nblocks=%" PRIu32 ", "
"block_size=%" PRIu32 "\n", nblocks, block_size);
mfun->nblocks = nblocks;
mfun->block_size = block_size;
rc = ddf_fun_bind(fun);
if (rc != EOK) {
usb_log_error("Failed to bind DDF function %s: %s.",
fun_name, str_error(rc));
goto error;
}
bound = true;
rc = ddf_fun_add_to_category(fun, "disk");
if (rc != EOK) {
usb_log_error("Failed to add function %s to category 'disk': %s.",
fun_name, str_error(rc));
goto error;
}
free(fun_name);
mdev->luns[lun] = fun;
return EOK;
error:
if (bound)
ddf_fun_unbind(fun);
if (fun != NULL)
ddf_fun_destroy(fun);
if (fun_name != NULL)
free(fun_name);
return rc;
}
static void usbmast_bd_connection(ipc_call_t *icall, void *arg)
{
usbmast_fun_t *mfun;
mfun = (usbmast_fun_t *) ddf_fun_data_get((ddf_fun_t *)arg);
bd_conn(icall, &mfun->bds);
}
static errno_t usbmast_bd_open(bd_srvs_t *bds, bd_srv_t *bd)
{
return EOK;
}
static errno_t usbmast_bd_close(bd_srv_t *bd)
{
return EOK;
}
static errno_t usbmast_bd_read_blocks(bd_srv_t *bd, uint64_t ba, size_t cnt, void *buf,
size_t size)
{
usbmast_fun_t *mfun = bd_srv_usbmast(bd);
if (size < cnt * mfun->block_size)
return EINVAL;
return usbmast_read(mfun, ba, cnt, buf);
}
static errno_t usbmast_bd_sync_cache(bd_srv_t *bd, uint64_t ba, size_t cnt)
{
usbmast_fun_t *mfun = bd_srv_usbmast(bd);
return usbmast_sync_cache(mfun, ba, cnt);
}
static errno_t usbmast_bd_write_blocks(bd_srv_t *bd, uint64_t ba, size_t cnt,
const void *buf, size_t size)
{
usbmast_fun_t *mfun = bd_srv_usbmast(bd);
if (size < cnt * mfun->block_size)
return EINVAL;
return usbmast_write(mfun, ba, cnt, buf);
}
static errno_t usbmast_bd_get_block_size(bd_srv_t *bd, size_t *rsize)
{
usbmast_fun_t *mfun = bd_srv_usbmast(bd);
*rsize = mfun->block_size;
return EOK;
}
static errno_t usbmast_bd_get_num_blocks(bd_srv_t *bd, aoff64_t *rnb)
{
usbmast_fun_t *mfun = bd_srv_usbmast(bd);
*rnb = mfun->nblocks;
return EOK;
}
static const usb_driver_ops_t usbmast_driver_ops = {
.device_add = usbmast_device_add,
.device_remove = usbmast_device_remove,
.device_gone = usbmast_device_gone,
};
static const usb_driver_t usbmast_driver = {
.name = NAME,
.ops = &usbmast_driver_ops,
.endpoints = mast_endpoints
};
int main(int argc, char *argv[])
{
log_init(NAME);
logctl_set_log_level(NAME, LVL_NOTE);
return usb_driver_main(&usbmast_driver);
}
HelenOS homepage, sources at GitHub