Initial work for the mmc_disk driver

No read and write support for now. But we implement getting SD card
capacity. SDHC is not supported yet (it uses a different layout for the
CSD register which will be rejected by this version of the code)

Change-Id: Ife844a62f3846c0a780259e9a3a08195e2fd965e
Reviewed-on: https://review.haiku-os.org/c/haiku/+/1068
Reviewed-by: Jérôme Duval <jerome.duval@gmail.com>
This commit is contained in:
Adrien Destugues 2019-02-19 20:12:08 +01:00 committed by Adrien Destugues
parent b66b01c283
commit e46898932c
8 changed files with 344 additions and 28 deletions

Binary file not shown.

View File

@ -1,5 +1,5 @@
/*
* Copyright 2019, Haiku, Inc. All Rights Reserved.
* Copyright 2019-2020, Haiku, Inc. All Rights Reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
@ -25,7 +25,8 @@ enum {
};
// Interface between mmc_bus and underlying implementation
// Interface between mmc_bus and underlying implementation (sdhci_pci or any
// other thing that can execute mmc commands)
typedef struct mmc_bus_interface {
driver_module_info info;
@ -44,4 +45,9 @@ typedef struct mmc_device_interface {
} mmc_device_interface;
// Device attribute paths for the MMC device
static const char* kMmcRcaAttribute = "mmc/rca";
static const char* kMmcTypeAttribute = "mmc/type";
#endif /* _MMC_H */

View File

@ -185,8 +185,8 @@ MMCBus::WorkerThread(void* cookie)
{ "mmc/revision", B_UINT16_TYPE, {ui16: revision}},
{ "mmc/month", B_UINT8_TYPE, {ui8: month}},
{ "mmc/year", B_UINT16_TYPE, {ui16: year}},
{ "mmc/rca", B_UINT16_TYPE, {ui16: rca}},
{ "mmc/type", B_UINT8_TYPE, {ui8: cardType}},
{ kMmcRcaAttribute, B_UINT16_TYPE, {ui16: rca}},
{ kMmcTypeAttribute, B_UINT8_TYPE, {ui8: cardType}},
{}
};

View File

@ -17,7 +17,6 @@
#define SDHCI_PCI_SLOTS(x) ((( x >> 4) & 7))
#define SDHCI_PCI_SLOT_INFO_FIRST_BASE_INDEX(x) (( x ) & 7)
#define SDHCI_DEVICE_TYPE_ITEM "sdhci/type"
#define SDHCI_BUS_TYPE_NAME "bus/sdhci/v1"

View File

@ -1,6 +1,7 @@
SubDir HAIKU_TOP src add-ons kernel drivers disk mmc ;
UsePrivateKernelHeaders ;
UsePrivateHeaders drivers ;
SubDirHdrs $(HAIKU_TOP) src system kernel device_manager ;
KernelAddon mmc_disk :

View File

@ -1,15 +1,22 @@
/*
* Copyright 2018 Haiku, Inc. All rights reserved.
* Copyright 2018-2020 Haiku, Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* B Krishnan Iyer, krishnaniyer97@gmail.com
* Adrien Destugues, pulkomandy@pulkomandy.tk
*/
#include <new>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include "mmc_disk.h"
#include "mmc_icon.h"
#include "mmc.h"
#include <drivers/device_manager.h>
#include <drivers/KernelExport.h>
@ -20,11 +27,11 @@
#define TRACE_MMC_DISK
#ifdef TRACE_MMC_DISK
# define TRACE(x...) dprintf("mmc_disk: " x)
# define TRACE(x...) dprintf("\33[33mmmc_disk:\33[0m " x)
#else
# define TRACE(x...) ;
#endif
#define ERROR(x...) dprintf("\33[33mmc_disk:\33[0m " x)
#define ERROR(x...) dprintf("\33[33mmmc_disk:\33[0m " x)
#define CALLED() TRACE("CALLED %s\n", __PRETTY_FUNCTION__)
#define MMC_DISK_DRIVER_MODULE_NAME "drivers/disk/mmc/mmc_disk/driver_v1"
@ -34,33 +41,48 @@
static device_manager_info* sDeviceManager;
struct mmc_disk_csd {
uint64 bits[2];
uint8 structure_version() { return bits[1] >> 60; }
uint8 read_bl_len() { return (bits[1] >> 8) & 0xF; }
uint16 c_size()
{
return ((bits[0] >> 54) & 0x3FF) | ((bits[1] & 0x3) << 10);
}
uint8 c_size_mult() { return (bits[0] >> 39) & 0x7; }
};
static float
mmc_disk_supports_device(device_node* parent)
{
CALLED();
// Filter all devices that are not on an MMC bus
const char* bus;
// uint16 deviceType;
if (sDeviceManager->get_attr_string(parent, B_DEVICE_BUS, &bus,
true) != B_OK)
return -1;
if (strcmp(bus, "mmc") != 0) {
TRACE("bus value %s, parent: %p\n",bus, parent);
if (strcmp(bus, "mmc") != 0)
return 0.0;
}
// Used to check the device type but later we have attached an
// attribute(mmc) to the bus. Not a compulsion to use this condition
#if 0
if (sDeviceManager->get_attr_uint16(parent, SDHCI_DEVICE_TYPE_ITEM,
CALLED();
// Filter all devices that are not of the known types
uint8_t deviceType;
if (sDeviceManager->get_attr_uint8(parent, kMmcTypeAttribute,
&deviceType, true) != B_OK)
{
TRACE(" Inavlid device type, bus found: %s and attr val %d\n",
bus, deviceType);
return 0.0;
ERROR("Could not get device type\n");
return -1;
}
#endif
TRACE("sdhci device found, parent: %p\n", parent);
if (deviceType == CARD_TYPE_SD)
TRACE("SD card found, parent: %p\n", parent);
else if (deviceType == CARD_TYPE_SDHC)
TRACE("SDHC card found, parent: %p\n", parent);
else
return 0.0;
return 0.8;
}
@ -72,6 +94,7 @@ mmc_disk_register_device(device_node* node)
CALLED();
device_attr attrs[] = {
{ B_DEVICE_PRETTY_NAME, B_STRING_TYPE, { string: "SD Card" }},
{ NULL }
};
@ -92,7 +115,22 @@ mmc_disk_init_driver(device_node* node, void** cookie)
memset(info, 0, sizeof(*info));
void* unused;
info->node = node;
info->parent = sDeviceManager->get_parent_node(info->node);
sDeviceManager->get_driver(info->parent, (driver_module_info **)&info->mmc,
&unused);
TRACE("MMC bus handle: %p %s\n", info->mmc, info->mmc->info.info.name);
if (sDeviceManager->get_attr_uint16(node, kMmcRcaAttribute, &info->rca,
true) != B_OK) {
TRACE("MMC card node has no RCA attribute\n");
free(info);
return B_BAD_DATA;
}
TRACE("MMC card device initialized for RCA %x\n", info->rca);
*cookie = info;
return B_OK;
@ -104,6 +142,7 @@ mmc_disk_uninit_driver(void* _cookie)
{
CALLED();
mmc_disk_driver_info* info = (mmc_disk_driver_info*)_cookie;
sDeviceManager->put_node(info->parent);
free(info);
}
@ -129,12 +168,207 @@ mmc_disk_register_child_devices(void* _cookie)
}
// #pragma mark - device module API
static status_t
mmc_block_init_device(void* _info, void** _cookie)
{
CALLED();
// No additional context, so just reuse the same data as the disk device
mmc_disk_driver_info* info = (mmc_disk_driver_info*)_info;
*_cookie = info;
return B_OK;
}
static void
mmc_block_uninit_device(void* _cookie)
{
CALLED();
//mmc_disk_driver_info* info = (mmc_disk_driver_info*)_cookie;
// TODO cleanup whatever is relevant
}
static status_t
mmc_block_open(void* _info, const char* path, int openMode, void** _cookie)
{
CALLED();
mmc_disk_driver_info* info = (mmc_disk_driver_info*)_info;
// TODO allocate cookie
mmc_disk_handle* handle = new(std::nothrow) mmc_disk_handle;
*_cookie = handle;
if (handle == NULL) {
return B_NO_MEMORY;
}
handle->info = info;
return B_OK;
}
static status_t
mmc_block_close(void* cookie)
{
mmc_disk_handle* handle = (mmc_disk_handle*)cookie;
CALLED();
return B_OK;
}
static status_t
mmc_block_free(void* cookie)
{
CALLED();
mmc_disk_handle* handle = (mmc_disk_handle*)cookie;
delete handle;
return B_OK;
}
static status_t
mmc_block_get_geometry(mmc_disk_handle* handle, device_geometry* geometry)
{
struct mmc_disk_csd csd;
TRACE("Ready to execute %p\n", handle->info->mmc->execute_command);
handle->info->mmc->execute_command(handle->info->parent, 9,
handle->info->rca << 16, (uint32_t*)&csd);
TRACE("CSD: %lx %lx\n", csd.bits[0], csd.bits[1]);
if (csd.structure_version() == 0) {
geometry->bytes_per_sector = 1 << csd.read_bl_len();
geometry->sectors_per_track = csd.c_size() + 1;
geometry->cylinder_count = 1 << (csd.c_size_mult() + 2);
geometry->head_count = 1;
geometry->device_type = B_DISK;
geometry->removable = true; // TODO detect eMMC which isn't
geometry->read_only = true; // TODO add write support
geometry->write_once = false;
return B_OK;
}
TRACE("unknown CSD version %d\n", csd.structure_version());
return B_NOT_SUPPORTED;
}
static status_t
mmc_block_ioctl(void* cookie, uint32 op, void* buffer, size_t length)
{
CALLED();
mmc_disk_handle* handle = (mmc_disk_handle*)cookie;
mmc_disk_driver_info* info = handle->info;
TRACE("ioctl(op = %ld)\n", op);
switch (op) {
case B_GET_MEDIA_STATUS:
{
if (buffer == NULL || length < sizeof(status_t))
return B_BAD_VALUE;
*(status_t *)buffer = B_OK;
TRACE("B_GET_MEDIA_STATUS: 0x%08lx\n", *(status_t *)buffer);
return B_OK;
break;
}
case B_GET_DEVICE_SIZE:
{
//size_t size = info->capacity * info->block_size;
//return user_memcpy(buffer, &size, sizeof(size_t));
return B_NOT_SUPPORTED;
}
case B_GET_GEOMETRY:
{
if (buffer == NULL || length < sizeof(device_geometry))
return B_BAD_VALUE;
device_geometry geometry;
status_t status = mmc_block_get_geometry(handle, &geometry);
if (status != B_OK)
return status;
return user_memcpy(buffer, &geometry, sizeof(device_geometry));
}
case B_GET_ICON_NAME:
return user_strlcpy((char*)buffer, "devices/drive-harddisk",
B_FILE_NAME_LENGTH);
case B_GET_VECTOR_ICON:
{
// TODO: take device type into account!
device_icon iconData;
if (length != sizeof(device_icon))
return B_BAD_VALUE;
if (user_memcpy(&iconData, buffer, sizeof(device_icon)) != B_OK)
return B_BAD_ADDRESS;
if (iconData.icon_size >= (int32)sizeof(kDriveIcon)) {
if (user_memcpy(iconData.icon_data, kDriveIcon,
sizeof(kDriveIcon)) != B_OK)
return B_BAD_ADDRESS;
}
iconData.icon_size = sizeof(kDriveIcon);
return user_memcpy(buffer, &iconData, sizeof(device_icon));
}
/*case B_FLUSH_DRIVE_CACHE:
return synchronize_cache(info);*/
}
return B_DEV_INVALID_IOCTL;
}
module_dependency module_dependencies[] = {
{B_DEVICE_MANAGER_MODULE_NAME, (module_info**)&sDeviceManager},
{}
};
// The "block device" associated with the device file. It can be open()
// multiple times, eash allocating an mmc_disk_handle. It does not interact
// with the hardware directly, instead it forwards all IO requests to the
// disk driver through the IO scheduler.
struct device_module_info sMMCBlockDevice = {
{
MMC_DISK_DEVICE_MODULE_NAME,
0,
NULL
},
mmc_block_init_device,
mmc_block_uninit_device,
NULL, // remove,
mmc_block_open,
mmc_block_close,
mmc_block_free,
NULL, //mmc_block_read,
NULL, //mmc_block_write,
NULL, //mmc_block_io,
mmc_block_ioctl,
NULL, // select
NULL, // deselect
};
// Driver for the disk devices itself. This is paired with an
// mmc_disk_driver_info instanciated once per device. Handles the actual disk
// I/O operations
struct driver_module_info sMMCDiskDriver = {
{
MMC_DISK_DRIVER_MODULE_NAME,
@ -153,5 +387,6 @@ struct driver_module_info sMMCDiskDriver = {
module_info* modules[] = {
(module_info*)&sMMCDiskDriver,
(module_info*)&sMMCBlockDevice,
NULL
};

View File

@ -1,9 +1,10 @@
/*
* Copyright 2018 Haiku, Inc. All rights reserved.
* Copyright 2018-2020 Haiku, Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* B Krishnan Iyer, krishnaniyer97@gmail.com
* Adrien Destugues, pulkomandy@pulkomandy.tk
*/
#ifndef _MMC_DISK_H
#define _MMC_DISK_H
@ -12,13 +13,24 @@
#include <device_manager.h>
#include <KernelExport.h>
#include <stdint.h>
#define SDHCI_DEVICE_TYPE_ITEM "sdhci/type"
#include <mmc.h>
typedef struct {
device_node* node;
status_t media_status;
device_node* node;
device_node* parent;
mmc_device_interface* mmc;
uint16_t rca;
size_t block_size;
uint64_t capacity;
} mmc_disk_driver_info;
#endif /*_MMC_DISK_H*/
typedef struct {
mmc_disk_driver_info* info;
} mmc_disk_handle;
#endif /*_MMC_DISK_H*/

View File

@ -0,0 +1,63 @@
const unsigned char kDriveIcon[] = {
0x6e, 0x63, 0x69, 0x66, 0x07, 0x05, 0x00, 0x04, 0x00, 0x63, 0x02, 0x00,
0x06, 0x02, 0x3a, 0x9c, 0xfb, 0x3b, 0x07, 0x44, 0xbd, 0x68, 0x2d, 0x3c,
0xf0, 0x9a, 0x49, 0x8c, 0xd7, 0x4a, 0x02, 0xf0, 0x00, 0xa4, 0xa4, 0xa4,
0xff, 0x5a, 0x5b, 0x65, 0x02, 0x00, 0x16, 0x02, 0x39, 0xd8, 0xc8, 0x38,
0x57, 0x6f, 0xbb, 0x1e, 0xa3, 0x3c, 0x90, 0x06, 0x4b, 0x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