From 4101f6879256720b30df712089a3df18565f9203 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Mon, 29 Feb 2016 15:25:34 -0700 Subject: dm: Drop the block_dev_desc_t typedef Use 'struct' instead of a typdef. Also since 'struct block_dev_desc' is long and causes 80-column violations, rename it to struct blk_desc. Signed-off-by: Simon Glass Reviewed-by: Bin Meng Tested-by: Stephen Warren --- common/env_fat.c | 4 ++-- common/fb_mmc.c | 12 ++++++------ common/spl/spl_ext.c | 6 +++--- common/spl/spl_fat.c | 8 ++++---- common/spl/spl_sata.c | 2 +- common/spl/spl_usb.c | 2 +- common/usb_storage.c | 22 +++++++++++----------- 7 files changed, 28 insertions(+), 28 deletions(-) (limited to 'common') diff --git a/common/env_fat.c b/common/env_fat.c index d79d864..e88279e 100644 --- a/common/env_fat.c +++ b/common/env_fat.c @@ -38,7 +38,7 @@ int env_init(void) int saveenv(void) { env_t env_new; - block_dev_desc_t *dev_desc = NULL; + struct blk_desc *dev_desc = NULL; disk_partition_t info; int dev, part; int err; @@ -77,7 +77,7 @@ int saveenv(void) void env_relocate_spec(void) { ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE); - block_dev_desc_t *dev_desc = NULL; + struct blk_desc *dev_desc = NULL; disk_partition_t info; int dev, part; int err; diff --git a/common/fb_mmc.c b/common/fb_mmc.c index 6e742da..fd43085 100644 --- a/common/fb_mmc.c +++ b/common/fb_mmc.c @@ -22,10 +22,10 @@ static char *response_str; struct fb_mmc_sparse { - block_dev_desc_t *dev_desc; + struct blk_desc *dev_desc; }; -static int get_partition_info_efi_by_name_or_alias(block_dev_desc_t *dev_desc, +static int get_partition_info_efi_by_name_or_alias(struct blk_desc *dev_desc, const char *name, disk_partition_t *info) { int ret; @@ -55,7 +55,7 @@ static int fb_mmc_sparse_write(struct sparse_storage *storage, char *data) { struct fb_mmc_sparse *sparse = priv; - block_dev_desc_t *dev_desc = sparse->dev_desc; + struct blk_desc *dev_desc = sparse->dev_desc; int ret; ret = dev_desc->block_write(dev_desc, offset, size, data); @@ -65,7 +65,7 @@ static int fb_mmc_sparse_write(struct sparse_storage *storage, return ret; } -static void write_raw_image(block_dev_desc_t *dev_desc, disk_partition_t *info, +static void write_raw_image(struct blk_desc *dev_desc, disk_partition_t *info, const char *part_name, void *buffer, unsigned int download_bytes) { @@ -100,7 +100,7 @@ void fb_mmc_flash_write(const char *cmd, unsigned int session_id, void *download_buffer, unsigned int download_bytes, char *response) { - block_dev_desc_t *dev_desc; + struct blk_desc *dev_desc; disk_partition_t info; /* initialize the response buffer */ @@ -165,7 +165,7 @@ void fb_mmc_flash_write(const char *cmd, unsigned int session_id, void fb_mmc_erase(const char *cmd, char *response) { int ret; - block_dev_desc_t *dev_desc; + struct blk_desc *dev_desc; disk_partition_t info; lbaint_t blks, blks_start, blks_size, grp_size; struct mmc *mmc = find_mmc_device(CONFIG_FASTBOOT_FLASH_MMC_DEV); diff --git a/common/spl/spl_ext.c b/common/spl/spl_ext.c index a42fbd0..d29d229 100644 --- a/common/spl/spl_ext.c +++ b/common/spl/spl_ext.c @@ -10,7 +10,7 @@ #include #ifdef CONFIG_SPL_EXT_SUPPORT -int spl_load_image_ext(block_dev_desc_t *block_dev, +int spl_load_image_ext(struct blk_desc *block_dev, int partition, const char *filename) { @@ -64,7 +64,7 @@ end: } #ifdef CONFIG_SPL_OS_BOOT -int spl_load_image_ext_os(block_dev_desc_t *block_dev, int partition) +int spl_load_image_ext_os(struct blk_desc *block_dev, int partition) { int err; __maybe_unused loff_t filelen, actlen; @@ -137,7 +137,7 @@ defaults: CONFIG_SPL_FS_LOAD_KERNEL_NAME); } #else -int spl_load_image_ext_os(block_dev_desc_t *block_dev, int partition) +int spl_load_image_ext_os(struct blk_desc *block_dev, int partition) { return -ENOSYS; } diff --git a/common/spl/spl_fat.c b/common/spl/spl_fat.c index 0daadbe..d761b26 100644 --- a/common/spl/spl_fat.c +++ b/common/spl/spl_fat.c @@ -19,7 +19,7 @@ static int fat_registered; #ifdef CONFIG_SPL_FAT_SUPPORT -static int spl_register_fat_device(block_dev_desc_t *block_dev, int partition) +static int spl_register_fat_device(struct blk_desc *block_dev, int partition) { int err = 0; @@ -39,7 +39,7 @@ static int spl_register_fat_device(block_dev_desc_t *block_dev, int partition) return err; } -int spl_load_image_fat(block_dev_desc_t *block_dev, +int spl_load_image_fat(struct blk_desc *block_dev, int partition, const char *filename) { @@ -72,7 +72,7 @@ end: } #ifdef CONFIG_SPL_OS_BOOT -int spl_load_image_fat_os(block_dev_desc_t *block_dev, int partition) +int spl_load_image_fat_os(struct blk_desc *block_dev, int partition) { int err; __maybe_unused char *file; @@ -121,7 +121,7 @@ defaults: CONFIG_SPL_FS_LOAD_KERNEL_NAME); } #else -int spl_load_image_fat_os(block_dev_desc_t *block_dev, int partition) +int spl_load_image_fat_os(struct blk_desc *block_dev, int partition) { return -ENOSYS; } diff --git a/common/spl/spl_sata.c b/common/spl/spl_sata.c index 3ba4c24..1719946 100644 --- a/common/spl/spl_sata.c +++ b/common/spl/spl_sata.c @@ -23,7 +23,7 @@ DECLARE_GLOBAL_DATA_PTR; int spl_sata_load_image(void) { int err; - block_dev_desc_t *stor_dev; + struct blk_desc *stor_dev; err = init_sata(CONFIG_SPL_SATA_BOOT_DEVICE); if (err) { diff --git a/common/spl/spl_usb.c b/common/spl/spl_usb.c index 588b85c..c42848e 100644 --- a/common/spl/spl_usb.c +++ b/common/spl/spl_usb.c @@ -25,7 +25,7 @@ static int usb_stor_curr_dev = -1; /* current device */ int spl_usb_load_image(void) { int err; - block_dev_desc_t *stor_dev; + struct blk_desc *stor_dev; usb_stop(); err = usb_init(); diff --git a/common/usb_storage.c b/common/usb_storage.c index 8737cf7..ca5aeea 100644 --- a/common/usb_storage.c +++ b/common/usb_storage.c @@ -67,7 +67,7 @@ static __u32 CBWTag; static int usb_max_devs; /* number of highest available usb device */ -static block_dev_desc_t usb_dev_desc[USB_MAX_STOR_DEV]; +static struct blk_desc usb_dev_desc[USB_MAX_STOR_DEV]; struct us_data; typedef int (*trans_cmnd)(ccb *cb, struct us_data *data); @@ -115,17 +115,17 @@ static struct us_data usb_stor[USB_MAX_STOR_DEV]; #define USB_STOR_TRANSPORT_ERROR -2 int usb_stor_get_info(struct usb_device *dev, struct us_data *us, - block_dev_desc_t *dev_desc); + struct blk_desc *dev_desc); int usb_storage_probe(struct usb_device *dev, unsigned int ifnum, struct us_data *ss); -static unsigned long usb_stor_read(block_dev_desc_t *block_dev, lbaint_t blknr, +static unsigned long usb_stor_read(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt, void *buffer); -static unsigned long usb_stor_write(block_dev_desc_t *block_dev, lbaint_t blknr, +static unsigned long usb_stor_write(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt, const void *buffer); void uhci_show_temp_int_td(void); #ifdef CONFIG_PARTITIONS -block_dev_desc_t *usb_stor_get_dev(int index) +struct blk_desc *usb_stor_get_dev(int index) { return (index < usb_max_devs) ? &usb_dev_desc[index] : NULL; } @@ -187,10 +187,10 @@ static int usb_stor_probe_device(struct usb_device *dev) for (lun = 0; lun <= max_lun && usb_max_devs < USB_MAX_STOR_DEV; lun++) { - struct block_dev_desc *blkdev; + struct blk_desc *blkdev; blkdev = &usb_dev_desc[usb_max_devs]; - memset(blkdev, '\0', sizeof(block_dev_desc_t)); + memset(blkdev, '\0', sizeof(struct blk_desc)); blkdev->if_type = IF_TYPE_USB; blkdev->dev = usb_max_devs; blkdev->part_type = PART_TYPE_UNKNOWN; @@ -1011,7 +1011,7 @@ static int usb_write_10(ccb *srb, struct us_data *ss, unsigned long start, * device with proper values (as reported by 'usb info'). * * Vendor and product length limits are taken from the definition of - * block_dev_desc_t in include/part.h. + * struct blk_desc in include/part.h. */ static void usb_bin_fixup(struct usb_device_descriptor descriptor, unsigned char vendor[], @@ -1026,7 +1026,7 @@ static void usb_bin_fixup(struct usb_device_descriptor descriptor, } #endif /* CONFIG_USB_BIN_FIXUP */ -static unsigned long usb_stor_read(block_dev_desc_t *block_dev, lbaint_t blknr, +static unsigned long usb_stor_read(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt, void *buffer) { int device = block_dev->dev; @@ -1097,7 +1097,7 @@ retry_it: return blkcnt; } -static unsigned long usb_stor_write(block_dev_desc_t *block_dev, lbaint_t blknr, +static unsigned long usb_stor_write(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt, const void *buffer) { int device = block_dev->dev; @@ -1289,7 +1289,7 @@ int usb_storage_probe(struct usb_device *dev, unsigned int ifnum, } int usb_stor_get_info(struct usb_device *dev, struct us_data *ss, - block_dev_desc_t *dev_desc) + struct blk_desc *dev_desc) { unsigned char perq, modi; ALLOC_CACHE_ALIGN_BUFFER(u32, cap, 2); -- cgit v1.1 From db1d9e78e6f0ea51a698f18abe4cebc5ff39b691 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Mon, 29 Feb 2016 15:25:42 -0700 Subject: dm: blk: Rename get_dev() to blk_get_dev() The current name is too generic. Add a 'blk_' prefix to aid searching and make its purpose clearer. Signed-off-by: Simon Glass Reviewed-by: Bin Meng Tested-by: Stephen Warren --- common/fb_mmc.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'common') diff --git a/common/fb_mmc.c b/common/fb_mmc.c index fd43085..4324f58 100644 --- a/common/fb_mmc.c +++ b/common/fb_mmc.c @@ -106,7 +106,7 @@ void fb_mmc_flash_write(const char *cmd, unsigned int session_id, /* initialize the response buffer */ response_str = response; - dev_desc = get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV); + dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV); if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) { error("invalid mmc device\n"); fastboot_fail(response_str, "invalid mmc device"); @@ -179,7 +179,7 @@ void fb_mmc_erase(const char *cmd, char *response) /* initialize the response buffer */ response_str = response; - dev_desc = get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV); + dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV); if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) { error("invalid mmc device"); fastboot_fail(response_str, "invalid mmc device"); -- cgit v1.1 From e35929e4a1ffe861f2f45e107986568c56c4f077 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Mon, 29 Feb 2016 15:25:44 -0700 Subject: dm: blk: Rename get_device_and_partition() Rename this function to blk_get_device_part_str(). This is a better name because it makes it clear that the function returns a block device and parses a string. Signed-off-by: Simon Glass Reviewed-by: Bin Meng Tested-by: Stephen Warren --- common/env_fat.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'common') diff --git a/common/env_fat.c b/common/env_fat.c index e88279e..2f22710 100644 --- a/common/env_fat.c +++ b/common/env_fat.c @@ -48,7 +48,7 @@ int saveenv(void) if (err) return err; - part = get_device_and_partition(FAT_ENV_INTERFACE, + part = blk_get_device_part_str(FAT_ENV_INTERFACE, FAT_ENV_DEVICE_AND_PART, &dev_desc, &info, 1); if (part < 0) @@ -82,7 +82,7 @@ void env_relocate_spec(void) int dev, part; int err; - part = get_device_and_partition(FAT_ENV_INTERFACE, + part = blk_get_device_part_str(FAT_ENV_INTERFACE, FAT_ENV_DEVICE_AND_PART, &dev_desc, &info, 1); if (part < 0) -- cgit v1.1 From 3e8bd469504f5d5a8800a2ea46d664dde701105b Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Mon, 29 Feb 2016 15:25:48 -0700 Subject: dm: part: Rename some partition functions Rename three partition functions so that they start with part_. This makes it clear what they relate to. Signed-off-by: Simon Glass Tested-by: Stephen Warren --- common/fb_mmc.c | 10 +++++----- common/spl/spl_ext.c | 6 ++---- common/spl/spl_mmc.c | 2 +- common/usb_storage.c | 2 +- 4 files changed, 9 insertions(+), 11 deletions(-) (limited to 'common') diff --git a/common/fb_mmc.c b/common/fb_mmc.c index 4324f58..d5c9135 100644 --- a/common/fb_mmc.c +++ b/common/fb_mmc.c @@ -25,12 +25,12 @@ struct fb_mmc_sparse { struct blk_desc *dev_desc; }; -static int get_partition_info_efi_by_name_or_alias(struct blk_desc *dev_desc, +static int part_get_info_efi_by_name_or_alias(struct blk_desc *dev_desc, const char *name, disk_partition_t *info) { int ret; - ret = get_partition_info_efi_by_name(dev_desc, name, info); + ret = part_get_info_efi_by_name(dev_desc, name, info); if (ret) { /* strlen("fastboot_partition_alias_") + 32(part_name) + 1 */ char env_alias_name[25 + 32 + 1]; @@ -41,7 +41,7 @@ static int get_partition_info_efi_by_name_or_alias(struct blk_desc *dev_desc, strncat(env_alias_name, name, 32); aliased_part_name = getenv(env_alias_name); if (aliased_part_name != NULL) - ret = get_partition_info_efi_by_name(dev_desc, + ret = part_get_info_efi_by_name(dev_desc, aliased_part_name, info); } return ret; @@ -131,7 +131,7 @@ void fb_mmc_flash_write(const char *cmd, unsigned int session_id, printf("........ success\n"); fastboot_okay(response_str, ""); return; - } else if (get_partition_info_efi_by_name_or_alias(dev_desc, cmd, &info)) { + } else if (part_get_info_efi_by_name_or_alias(dev_desc, cmd, &info)) { error("cannot find partition: '%s'\n", cmd); fastboot_fail(response_str, "cannot find partition"); return; @@ -186,7 +186,7 @@ void fb_mmc_erase(const char *cmd, char *response) return; } - ret = get_partition_info_efi_by_name_or_alias(dev_desc, cmd, &info); + ret = part_get_info_efi_by_name_or_alias(dev_desc, cmd, &info); if (ret) { error("cannot find partition: '%s'", cmd); fastboot_fail(response_str, "cannot find partition"); diff --git a/common/spl/spl_ext.c b/common/spl/spl_ext.c index d29d229..b77dbf4 100644 --- a/common/spl/spl_ext.c +++ b/common/spl/spl_ext.c @@ -22,8 +22,7 @@ int spl_load_image_ext(struct blk_desc *block_dev, header = (struct image_header *)(CONFIG_SYS_TEXT_BASE - sizeof(struct image_header)); - if (get_partition_info(block_dev, - partition, &part_info)) { + if (part_get_info(block_dev, partition, &part_info)) { printf("spl: no partition table found\n"); return -1; } @@ -71,8 +70,7 @@ int spl_load_image_ext_os(struct blk_desc *block_dev, int partition) disk_partition_t part_info = {}; __maybe_unused char *file; - if (get_partition_info(block_dev, - partition, &part_info)) { + if (part_get_info(block_dev, partition, &part_info)) { printf("spl: no partition table found\n"); return -1; } diff --git a/common/spl/spl_mmc.c b/common/spl/spl_mmc.c index c27a250..5204f52 100644 --- a/common/spl/spl_mmc.c +++ b/common/spl/spl_mmc.c @@ -122,7 +122,7 @@ static int mmc_load_image_raw_partition(struct mmc *mmc, int partition) disk_partition_t info; int err; - err = get_partition_info(&mmc->block_dev, partition, &info); + err = part_get_info(&mmc->block_dev, partition, &info); if (err) { #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT puts("spl: partition error\n"); diff --git a/common/usb_storage.c b/common/usb_storage.c index ca5aeea..e105a95 100644 --- a/common/usb_storage.c +++ b/common/usb_storage.c @@ -1374,7 +1374,7 @@ int usb_stor_get_info(struct usb_device *dev, struct us_data *ss, debug(" address %d\n", dev_desc->target); debug("partype: %d\n", dev_desc->part_type); - init_part(dev_desc); + part_init(dev_desc); debug("partype: %d\n", dev_desc->part_type); return 1; -- cgit v1.1 From bcce53d048de7f41078d25e39aa2f26d752d3658 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Mon, 29 Feb 2016 15:25:51 -0700 Subject: dm: block: Rename device number member dev to devnum This is a device number, and we want to use 'dev' to mean a driver model device. Rename the member. Signed-off-by: Simon Glass Reviewed-by: Bin Meng Tested-by: Stephen Warren --- common/env_fat.c | 4 ++-- common/fb_mmc.c | 4 ++-- common/usb_storage.c | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) (limited to 'common') diff --git a/common/env_fat.c b/common/env_fat.c index 2f22710..75616d4 100644 --- a/common/env_fat.c +++ b/common/env_fat.c @@ -54,7 +54,7 @@ int saveenv(void) if (part < 0) return 1; - dev = dev_desc->dev; + dev = dev_desc->devnum; if (fat_set_blk_dev(dev_desc, &info) != 0) { printf("\n** Unable to use %s %d:%d for saveenv **\n", FAT_ENV_INTERFACE, dev, part); @@ -88,7 +88,7 @@ void env_relocate_spec(void) if (part < 0) goto err_env_relocate; - dev = dev_desc->dev; + dev = dev_desc->devnum; if (fat_set_blk_dev(dev_desc, &info) != 0) { printf("\n** Unable to use %s %d:%d for loading the env **\n", FAT_ENV_INTERFACE, dev, part); diff --git a/common/fb_mmc.c b/common/fb_mmc.c index d5c9135..da7949f 100644 --- a/common/fb_mmc.c +++ b/common/fb_mmc.c @@ -86,7 +86,7 @@ static void write_raw_image(struct blk_desc *dev_desc, disk_partition_t *info, blks = dev_desc->block_write(dev_desc, info->start, blkcnt, buffer); if (blks != blkcnt) { - error("failed writing to device %d\n", dev_desc->dev); + error("failed writing to device %d\n", dev_desc->devnum); fastboot_fail(response_str, "failed writing to device"); return; } @@ -207,7 +207,7 @@ void fb_mmc_erase(const char *cmd, char *response) blks = dev_desc->block_erase(dev_desc, blks_start, blks_size); if (blks != blks_size) { - error("failed erasing from device %d", dev_desc->dev); + error("failed erasing from device %d", dev_desc->devnum); fastboot_fail(response_str, "failed erasing from device"); return; } diff --git a/common/usb_storage.c b/common/usb_storage.c index e105a95..60531e2 100644 --- a/common/usb_storage.c +++ b/common/usb_storage.c @@ -192,7 +192,7 @@ static int usb_stor_probe_device(struct usb_device *dev) blkdev = &usb_dev_desc[usb_max_devs]; memset(blkdev, '\0', sizeof(struct blk_desc)); blkdev->if_type = IF_TYPE_USB; - blkdev->dev = usb_max_devs; + blkdev->devnum = usb_max_devs; blkdev->part_type = PART_TYPE_UNKNOWN; blkdev->target = 0xff; blkdev->type = DEV_TYPE_UNKNOWN; @@ -1029,7 +1029,7 @@ static void usb_bin_fixup(struct usb_device_descriptor descriptor, static unsigned long usb_stor_read(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt, void *buffer) { - int device = block_dev->dev; + int device = block_dev->devnum; lbaint_t start, blks; uintptr_t buf_addr; unsigned short smallblks; @@ -1100,7 +1100,7 @@ retry_it: static unsigned long usb_stor_write(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt, const void *buffer) { - int device = block_dev->dev; + int device = block_dev->devnum; lbaint_t start, blks; uintptr_t buf_addr; unsigned short smallblks; -- cgit v1.1 From 2a981dc2c62c500110aad297fa70503aec36e689 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Mon, 29 Feb 2016 15:25:52 -0700 Subject: dm: block: Adjust device calls to go through helpers function To ease conversion to driver model, add helper functions which deal with calling each block device method. With driver model we can reimplement these functions with the same arguments. Use inline functions to avoid increasing code size on some boards. Signed-off-by: Simon Glass Reviewed-by: Bin Meng Tested-by: Stephen Warren --- common/fb_mmc.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'common') diff --git a/common/fb_mmc.c b/common/fb_mmc.c index da7949f..e3abcc8 100644 --- a/common/fb_mmc.c +++ b/common/fb_mmc.c @@ -6,6 +6,7 @@ #include #include +#include #include #include #include @@ -58,7 +59,7 @@ static int fb_mmc_sparse_write(struct sparse_storage *storage, struct blk_desc *dev_desc = sparse->dev_desc; int ret; - ret = dev_desc->block_write(dev_desc, offset, size, data); + ret = blk_dwrite(dev_desc, offset, size, data); if (!ret) return -EIO; @@ -84,7 +85,7 @@ static void write_raw_image(struct blk_desc *dev_desc, disk_partition_t *info, puts("Flashing Raw Image\n"); - blks = dev_desc->block_write(dev_desc, info->start, blkcnt, buffer); + blks = blk_dwrite(dev_desc, info->start, blkcnt, buffer); if (blks != blkcnt) { error("failed writing to device %d\n", dev_desc->devnum); fastboot_fail(response_str, "failed writing to device"); -- cgit v1.1 From c89e79d4199cf76f3a018c230d5705df7d2225c6 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Mon, 29 Feb 2016 15:25:53 -0700 Subject: dm: usb: Avoid exceeding available array size for storage devices The limit on storage devices is USB_MAX_STOR_DEV but we use one extra element while probing to see if a device is a storage device. Avoid this, since it causes memory corruption. Signed-off-by: Simon Glass Reviewed-by: Marek Vasut Tested-by: Stephen Warren --- common/usb_storage.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'common') diff --git a/common/usb_storage.c b/common/usb_storage.c index 60531e2..f2d2ad9 100644 --- a/common/usb_storage.c +++ b/common/usb_storage.c @@ -176,6 +176,13 @@ static int usb_stor_probe_device(struct usb_device *dev) if (dev == NULL) return -ENOENT; /* no more devices available */ + /* We don't have space to even probe if we hit the maximum */ + if (usb_max_devs == USB_MAX_STOR_DEV) { + printf("max USB Storage Device reached: %d stopping\n", + usb_max_devs); + return -ENOSPC; + } + debug("\n\nProbing for storage\n"); if (usb_storage_probe(dev, 0, &usb_stor[usb_max_devs])) { /* OK, it's a storage device. Iterate over its LUNs @@ -210,13 +217,6 @@ static int usb_stor_probe_device(struct usb_device *dev) } } - /* if storage device */ - if (usb_max_devs == USB_MAX_STOR_DEV) { - printf("max USB Storage Device reached: %d stopping\n", - usb_max_devs); - return -ENOSPC; - } - return 0; } -- cgit v1.1 From 9807c3b78a4a2f1f33daf9432ef222d5ce0bbafc Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Mon, 29 Feb 2016 15:25:54 -0700 Subject: dm: usb: Tidy up storage code ready for driver model conversion Adjust a few things so that the addition of driver-models support involved adding code rather than also changing it. This makes the patches easier to review. Signed-off-by: Simon Glass Reviewed-by: Marek Vasut Tested-by: Stephen Warren --- common/usb_storage.c | 117 +++++++++++++++++++++++++++------------------------ 1 file changed, 61 insertions(+), 56 deletions(-) (limited to 'common') diff --git a/common/usb_storage.c b/common/usb_storage.c index f2d2ad9..0475123 100644 --- a/common/usb_storage.c +++ b/common/usb_storage.c @@ -142,6 +142,7 @@ static void usb_show_progress(void) */ int usb_stor_info(void) { + int count = 0; int i; if (usb_max_devs > 0) { @@ -152,7 +153,11 @@ int usb_stor_info(void) return 0; } - printf("No storage devices, perhaps not 'usb start'ed..?\n"); + if (!count) { + printf("No storage devices, perhaps not 'usb start'ed..?\n"); + return 1; + } + return 1; } @@ -171,9 +176,12 @@ static unsigned int usb_get_max_lun(struct us_data *us) return (len > 0) ? *result : 0; } -static int usb_stor_probe_device(struct usb_device *dev) +static int usb_stor_probe_device(struct usb_device *udev) { - if (dev == NULL) + int lun, max_lun; + int start; + + if (udev == NULL) return -ENOENT; /* no more devices available */ /* We don't have space to even probe if we hit the maximum */ @@ -184,36 +192,36 @@ static int usb_stor_probe_device(struct usb_device *dev) } debug("\n\nProbing for storage\n"); - if (usb_storage_probe(dev, 0, &usb_stor[usb_max_devs])) { - /* OK, it's a storage device. Iterate over its LUNs - * and populate `usb_dev_desc'. - */ - int lun, max_lun, start = usb_max_devs; - - max_lun = usb_get_max_lun(&usb_stor[usb_max_devs]); - for (lun = 0; - lun <= max_lun && usb_max_devs < USB_MAX_STOR_DEV; - lun++) { - struct blk_desc *blkdev; - - blkdev = &usb_dev_desc[usb_max_devs]; - memset(blkdev, '\0', sizeof(struct blk_desc)); - blkdev->if_type = IF_TYPE_USB; - blkdev->devnum = usb_max_devs; - blkdev->part_type = PART_TYPE_UNKNOWN; - blkdev->target = 0xff; - blkdev->type = DEV_TYPE_UNKNOWN; - blkdev->block_read = usb_stor_read; - blkdev->block_write = usb_stor_write; - blkdev->lun = lun; - blkdev->priv = dev; - - if (usb_stor_get_info(dev, &usb_stor[start], - &usb_dev_desc[usb_max_devs]) == - 1) { - usb_max_devs++; - debug("%s: Found device %p\n", __func__, dev); - } + if (!usb_storage_probe(udev, 0, &usb_stor[usb_max_devs])) + return 0; + + /* + * OK, it's a storage device. Iterate over its LUNs and populate + * usb_dev_desc' + */ + start = usb_max_devs; + + max_lun = usb_get_max_lun(&usb_stor[usb_max_devs]); + for (lun = 0; lun <= max_lun && usb_max_devs < USB_MAX_STOR_DEV; + lun++) { + struct blk_desc *blkdev; + + blkdev = &usb_dev_desc[usb_max_devs]; + memset(blkdev, '\0', sizeof(struct blk_desc)); + blkdev->if_type = IF_TYPE_USB; + blkdev->devnum = usb_max_devs; + blkdev->part_type = PART_TYPE_UNKNOWN; + blkdev->target = 0xff; + blkdev->type = DEV_TYPE_UNKNOWN; + blkdev->block_read = usb_stor_read; + blkdev->block_write = usb_stor_write; + blkdev->lun = lun; + blkdev->priv = udev; + + if (usb_stor_get_info(udev, &usb_stor[start], + &usb_dev_desc[usb_max_devs]) == 1) { + usb_max_devs++; + debug("%s: Found device %p\n", __func__, udev); } } @@ -1029,36 +1037,33 @@ static void usb_bin_fixup(struct usb_device_descriptor descriptor, static unsigned long usb_stor_read(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt, void *buffer) { - int device = block_dev->devnum; lbaint_t start, blks; uintptr_t buf_addr; unsigned short smallblks; - struct usb_device *dev; + struct usb_device *udev; struct us_data *ss; int retry; ccb *srb = &usb_ccb; if (blkcnt == 0) return 0; - - device &= 0xff; /* Setup device */ - debug("\nusb_read: dev %d\n", device); - dev = usb_dev_desc[device].priv; - if (!dev) { + debug("\nusb_read: udev %d\n", block_dev->devnum); + udev = usb_dev_desc[block_dev->devnum].priv; + if (!udev) { debug("%s: No device\n", __func__); return 0; } - ss = (struct us_data *)dev->privptr; + ss = (struct us_data *)udev->privptr; usb_disable_asynch(1); /* asynch transfer not allowed */ - srb->lun = usb_dev_desc[device].lun; + srb->lun = block_dev->lun; buf_addr = (uintptr_t)buffer; start = blknr; blks = blkcnt; - debug("\nusb_read: dev %d startblk " LBAF ", blccnt " LBAF - " buffer %" PRIxPTR "\n", device, start, blks, buf_addr); + debug("\nusb_read: dev %d startblk " LBAF ", blccnt " LBAF " buffer %" + PRIxPTR "\n", block_dev->devnum, start, blks, buf_addr); do { /* XXX need some comment here */ @@ -1071,7 +1076,7 @@ static unsigned long usb_stor_read(struct blk_desc *block_dev, lbaint_t blknr, retry_it: if (smallblks == USB_MAX_XFER_BLK) usb_show_progress(); - srb->datalen = usb_dev_desc[device].blksz * smallblks; + srb->datalen = block_dev->blksz * smallblks; srb->pdata = (unsigned char *)buf_addr; if (usb_read_10(srb, ss, start, smallblks)) { debug("Read ERROR\n"); @@ -1100,11 +1105,10 @@ retry_it: static unsigned long usb_stor_write(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt, const void *buffer) { - int device = block_dev->devnum; lbaint_t start, blks; uintptr_t buf_addr; unsigned short smallblks; - struct usb_device *dev; + struct usb_device *udev; struct us_data *ss; int retry; ccb *srb = &usb_ccb; @@ -1112,23 +1116,24 @@ static unsigned long usb_stor_write(struct blk_desc *block_dev, lbaint_t blknr, if (blkcnt == 0) return 0; - device &= 0xff; /* Setup device */ - debug("\nusb_write: dev %d\n", device); - dev = usb_dev_desc[device].priv; - if (!dev) + debug("\nusb_read: udev %d\n", block_dev->devnum); + udev = usb_dev_desc[block_dev->devnum].priv; + if (!udev) { + debug("%s: No device\n", __func__); return 0; - ss = (struct us_data *)dev->privptr; + } + ss = (struct us_data *)udev->privptr; usb_disable_asynch(1); /* asynch transfer not allowed */ - srb->lun = usb_dev_desc[device].lun; + srb->lun = block_dev->lun; buf_addr = (uintptr_t)buffer; start = blknr; blks = blkcnt; - debug("\nusb_write: dev %d startblk " LBAF ", blccnt " LBAF - " buffer %" PRIxPTR "\n", device, start, blks, buf_addr); + debug("\nusb_write: dev %d startblk " LBAF ", blccnt " LBAF " buffer %" + PRIxPTR "\n", block_dev->devnum, start, blks, buf_addr); do { /* If write fails retry for max retry count else @@ -1143,7 +1148,7 @@ static unsigned long usb_stor_write(struct blk_desc *block_dev, lbaint_t blknr, retry_it: if (smallblks == USB_MAX_XFER_BLK) usb_show_progress(); - srb->datalen = usb_dev_desc[device].blksz * smallblks; + srb->datalen = block_dev->blksz * smallblks; srb->pdata = (unsigned char *)buf_addr; if (usb_write_10(srb, ss, start, smallblks)) { debug("Write ERROR\n"); -- cgit v1.1 From 07b2b78ce4bc8ae25e066c65245eaf58c0d9a67c Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Mon, 29 Feb 2016 15:25:58 -0700 Subject: dm: usb: Convert USB storage to use driver-model for block devs Update this code to support CONFIG_BLK. Each USB storage device can have one or more block devices as children, each one representing a LUN (logical unit) of the USB device. Signed-off-by: Simon Glass Reviewed-by: Marek Vasut Tested-by: Stephen Warren --- common/usb_storage.c | 141 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 135 insertions(+), 6 deletions(-) (limited to 'common') diff --git a/common/usb_storage.c b/common/usb_storage.c index 0475123..1472824 100644 --- a/common/usb_storage.c +++ b/common/usb_storage.c @@ -43,6 +43,7 @@ #include #include #include +#include #include #include @@ -67,7 +68,9 @@ static __u32 CBWTag; static int usb_max_devs; /* number of highest available usb device */ +#ifndef CONFIG_BLK static struct blk_desc usb_dev_desc[USB_MAX_STOR_DEV]; +#endif struct us_data; typedef int (*trans_cmnd)(ccb *cb, struct us_data *data); @@ -108,7 +111,9 @@ struct us_data { #define USB_MAX_XFER_BLK 20 #endif +#ifndef CONFIG_BLK static struct us_data usb_stor[USB_MAX_STOR_DEV]; +#endif #define USB_STOR_TRANSPORT_GOOD 0 #define USB_STOR_TRANSPORT_FAILED -1 @@ -118,16 +123,33 @@ int usb_stor_get_info(struct usb_device *dev, struct us_data *us, struct blk_desc *dev_desc); int usb_storage_probe(struct usb_device *dev, unsigned int ifnum, struct us_data *ss); +#ifdef CONFIG_BLK +static unsigned long usb_stor_read(struct udevice *dev, lbaint_t blknr, + lbaint_t blkcnt, void *buffer); +static unsigned long usb_stor_write(struct udevice *dev, lbaint_t blknr, + lbaint_t blkcnt, const void *buffer); +#else static unsigned long usb_stor_read(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt, void *buffer); static unsigned long usb_stor_write(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt, const void *buffer); +#endif void uhci_show_temp_int_td(void); #ifdef CONFIG_PARTITIONS struct blk_desc *usb_stor_get_dev(int index) { +#ifdef CONFIG_BLK + struct udevice *dev; + int ret; + + ret = blk_get_device(IF_TYPE_USB, index, &dev); + if (ret) + return NULL; + return dev_get_uclass_platdata(dev); +#else return (index < usb_max_devs) ? &usb_dev_desc[index] : NULL; +#endif } #endif @@ -143,6 +165,19 @@ static void usb_show_progress(void) int usb_stor_info(void) { int count = 0; +#ifdef CONFIG_BLK + struct udevice *dev; + + for (blk_first_device(IF_TYPE_USB, &dev); + dev; + blk_next_device(&dev)) { + struct blk_desc *desc = dev_get_uclass_platdata(dev); + + printf(" Device %d: ", desc->devnum); + dev_print(desc); + count++; + } +#else int i; if (usb_max_devs > 0) { @@ -152,7 +187,7 @@ int usb_stor_info(void) } return 0; } - +#endif if (!count) { printf("No storage devices, perhaps not 'usb start'ed..?\n"); return 1; @@ -179,11 +214,63 @@ static unsigned int usb_get_max_lun(struct us_data *us) static int usb_stor_probe_device(struct usb_device *udev) { int lun, max_lun; + +#ifdef CONFIG_BLK + struct us_data *data; + char dev_name[30], *str; + int ret; +#else int start; if (udev == NULL) return -ENOENT; /* no more devices available */ +#endif + + debug("\n\nProbing for storage\n"); +#ifdef CONFIG_BLK + /* + * We store the us_data in the mass storage device's platdata. It + * is shared by all LUNs (block devices) attached to this mass storage + * device. + */ + data = dev_get_platdata(udev->dev); + if (!usb_storage_probe(udev, 0, data)) + return 0; + max_lun = usb_get_max_lun(data); + for (lun = 0; lun <= max_lun; lun++) { + struct blk_desc *blkdev; + struct udevice *dev; + + snprintf(dev_name, sizeof(dev_name), "%s.lun%d", + udev->dev->name, lun); + str = strdup(dev_name); + if (!str) + return -ENOMEM; + ret = blk_create_device(udev->dev, "usb_storage_blk", str, + IF_TYPE_USB, usb_max_devs, 512, 0, &dev); + if (ret) { + debug("Cannot bind driver\n"); + return ret; + } + + blkdev = dev_get_uclass_platdata(dev); + blkdev->target = 0xff; + blkdev->lun = lun; + ret = usb_stor_get_info(udev, data, blkdev); + if (ret == 1) + ret = blk_prepare_device(dev); + if (!ret) { + usb_max_devs++; + debug("%s: Found device %p\n", __func__, udev); + } else { + debug("usb_stor_get_info: Invalid device\n"); + ret = device_unbind(dev); + if (ret) + return ret; + } + } +#else /* We don't have space to even probe if we hit the maximum */ if (usb_max_devs == USB_MAX_STOR_DEV) { printf("max USB Storage Device reached: %d stopping\n", @@ -191,7 +278,6 @@ static int usb_stor_probe_device(struct usb_device *udev) return -ENOSPC; } - debug("\n\nProbing for storage\n"); if (!usb_storage_probe(udev, 0, &usb_stor[usb_max_devs])) return 0; @@ -220,10 +306,14 @@ static int usb_stor_probe_device(struct usb_device *udev) if (usb_stor_get_info(udev, &usb_stor[start], &usb_dev_desc[usb_max_devs]) == 1) { + debug("partype: %d\n", blkdev->part_type); + part_init(blkdev); + debug("partype: %d\n", blkdev->part_type); usb_max_devs++; debug("%s: Found device %p\n", __func__, udev); } } +#endif return 0; } @@ -1034,8 +1124,13 @@ static void usb_bin_fixup(struct usb_device_descriptor descriptor, } #endif /* CONFIG_USB_BIN_FIXUP */ +#ifdef CONFIG_BLK +static unsigned long usb_stor_read(struct udevice *dev, lbaint_t blknr, + lbaint_t blkcnt, void *buffer) +#else static unsigned long usb_stor_read(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt, void *buffer) +#endif { lbaint_t start, blks; uintptr_t buf_addr; @@ -1044,16 +1139,25 @@ static unsigned long usb_stor_read(struct blk_desc *block_dev, lbaint_t blknr, struct us_data *ss; int retry; ccb *srb = &usb_ccb; +#ifdef CONFIG_BLK + struct blk_desc *block_dev; +#endif if (blkcnt == 0) return 0; /* Setup device */ +#ifdef CONFIG_BLK + block_dev = dev_get_uclass_platdata(dev); + udev = dev_get_parent_priv(dev_get_parent(dev)); + debug("\nusb_read: udev %d\n", block_dev->devnum); +#else debug("\nusb_read: udev %d\n", block_dev->devnum); udev = usb_dev_desc[block_dev->devnum].priv; if (!udev) { debug("%s: No device\n", __func__); return 0; } +#endif ss = (struct us_data *)udev->privptr; usb_disable_asynch(1); /* asynch transfer not allowed */ @@ -1102,8 +1206,13 @@ retry_it: return blkcnt; } +#ifdef CONFIG_BLK +static unsigned long usb_stor_write(struct udevice *dev, lbaint_t blknr, + lbaint_t blkcnt, const void *buffer) +#else static unsigned long usb_stor_write(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt, const void *buffer) +#endif { lbaint_t start, blks; uintptr_t buf_addr; @@ -1112,17 +1221,26 @@ static unsigned long usb_stor_write(struct blk_desc *block_dev, lbaint_t blknr, struct us_data *ss; int retry; ccb *srb = &usb_ccb; +#ifdef CONFIG_BLK + struct blk_desc *block_dev; +#endif if (blkcnt == 0) return 0; /* Setup device */ +#ifdef CONFIG_BLK + block_dev = dev_get_uclass_platdata(dev); + udev = dev_get_parent_priv(dev_get_parent(dev)); + debug("\nusb_read: udev %d\n", block_dev->devnum); +#else debug("\nusb_read: udev %d\n", block_dev->devnum); udev = usb_dev_desc[block_dev->devnum].priv; if (!udev) { debug("%s: No device\n", __func__); return 0; } +#endif ss = (struct us_data *)udev->privptr; usb_disable_asynch(1); /* asynch transfer not allowed */ @@ -1377,11 +1495,7 @@ int usb_stor_get_info(struct usb_device *dev, struct us_data *ss, dev_desc->log2blksz = LOG2(dev_desc->blksz); dev_desc->type = perq; debug(" address %d\n", dev_desc->target); - debug("partype: %d\n", dev_desc->part_type); - - part_init(dev_desc); - debug("partype: %d\n", dev_desc->part_type); return 1; } @@ -1409,6 +1523,9 @@ U_BOOT_DRIVER(usb_mass_storage) = { .id = UCLASS_MASS_STORAGE, .of_match = usb_mass_storage_ids, .probe = usb_mass_storage_probe, +#ifdef CONFIG_BLK + .platdata_auto_alloc_size = sizeof(struct us_data), +#endif }; UCLASS_DRIVER(usb_mass_storage) = { @@ -1425,5 +1542,17 @@ static const struct usb_device_id mass_storage_id_table[] = { }; U_BOOT_USB_DEVICE(usb_mass_storage, mass_storage_id_table); +#endif +#ifdef CONFIG_BLK +static const struct blk_ops usb_storage_ops = { + .read = usb_stor_read, + .write = usb_stor_write, +}; + +U_BOOT_DRIVER(usb_storage_blk) = { + .name = "usb_storage_blk", + .id = UCLASS_BLK, + .ops = &usb_storage_ops, +}; #endif -- cgit v1.1