Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 Google, Inc |
| 3 | * Written by Simon Glass <sjg@chromium.org> |
| 4 | * |
| 5 | * SPDX-License-Identifier: GPL-2.0+ |
| 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <blk.h> |
| 10 | #include <dm.h> |
| 11 | #include <dm/device-internal.h> |
| 12 | #include <dm/lists.h> |
| 13 | |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 14 | static const char *if_typename_str[IF_TYPE_COUNT] = { |
| 15 | [IF_TYPE_IDE] = "ide", |
| 16 | [IF_TYPE_SCSI] = "scsi", |
| 17 | [IF_TYPE_ATAPI] = "atapi", |
| 18 | [IF_TYPE_USB] = "usb", |
| 19 | [IF_TYPE_DOC] = "doc", |
| 20 | [IF_TYPE_MMC] = "mmc", |
| 21 | [IF_TYPE_SD] = "sd", |
| 22 | [IF_TYPE_SATA] = "sata", |
| 23 | [IF_TYPE_HOST] = "host", |
| 24 | [IF_TYPE_SYSTEMACE] = "ace", |
| 25 | }; |
| 26 | |
| 27 | static enum uclass_id if_type_uclass_id[IF_TYPE_COUNT] = { |
| 28 | [IF_TYPE_IDE] = UCLASS_INVALID, |
Michal Simek | c886f35 | 2016-09-08 15:06:45 +0200 | [diff] [blame] | 29 | [IF_TYPE_SCSI] = UCLASS_SCSI, |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 30 | [IF_TYPE_ATAPI] = UCLASS_INVALID, |
| 31 | [IF_TYPE_USB] = UCLASS_MASS_STORAGE, |
| 32 | [IF_TYPE_DOC] = UCLASS_INVALID, |
| 33 | [IF_TYPE_MMC] = UCLASS_MMC, |
| 34 | [IF_TYPE_SD] = UCLASS_INVALID, |
| 35 | [IF_TYPE_SATA] = UCLASS_AHCI, |
| 36 | [IF_TYPE_HOST] = UCLASS_ROOT, |
| 37 | [IF_TYPE_SYSTEMACE] = UCLASS_INVALID, |
| 38 | }; |
| 39 | |
| 40 | static enum if_type if_typename_to_iftype(const char *if_typename) |
| 41 | { |
| 42 | int i; |
| 43 | |
| 44 | for (i = 0; i < IF_TYPE_COUNT; i++) { |
| 45 | if (if_typename_str[i] && |
| 46 | !strcmp(if_typename, if_typename_str[i])) |
| 47 | return i; |
| 48 | } |
| 49 | |
| 50 | return IF_TYPE_UNKNOWN; |
| 51 | } |
| 52 | |
| 53 | static enum uclass_id if_type_to_uclass_id(enum if_type if_type) |
| 54 | { |
| 55 | return if_type_uclass_id[if_type]; |
| 56 | } |
| 57 | |
| 58 | struct blk_desc *blk_get_devnum_by_type(enum if_type if_type, int devnum) |
| 59 | { |
| 60 | struct blk_desc *desc; |
| 61 | struct udevice *dev; |
| 62 | int ret; |
| 63 | |
| 64 | ret = blk_get_device(if_type, devnum, &dev); |
| 65 | if (ret) |
| 66 | return NULL; |
| 67 | desc = dev_get_uclass_platdata(dev); |
| 68 | |
| 69 | return desc; |
| 70 | } |
| 71 | |
| 72 | /* |
| 73 | * This function is complicated with driver model. We look up the interface |
| 74 | * name in a local table. This gives us an interface type which we can match |
| 75 | * against the uclass of the block device's parent. |
| 76 | */ |
| 77 | struct blk_desc *blk_get_devnum_by_typename(const char *if_typename, int devnum) |
| 78 | { |
| 79 | enum uclass_id uclass_id; |
| 80 | enum if_type if_type; |
| 81 | struct udevice *dev; |
| 82 | struct uclass *uc; |
| 83 | int ret; |
| 84 | |
| 85 | if_type = if_typename_to_iftype(if_typename); |
| 86 | if (if_type == IF_TYPE_UNKNOWN) { |
| 87 | debug("%s: Unknown interface type '%s'\n", __func__, |
| 88 | if_typename); |
| 89 | return NULL; |
| 90 | } |
| 91 | uclass_id = if_type_to_uclass_id(if_type); |
| 92 | if (uclass_id == UCLASS_INVALID) { |
| 93 | debug("%s: Unknown uclass for interface type'\n", |
| 94 | if_typename_str[if_type]); |
| 95 | return NULL; |
| 96 | } |
| 97 | |
| 98 | ret = uclass_get(UCLASS_BLK, &uc); |
| 99 | if (ret) |
| 100 | return NULL; |
| 101 | uclass_foreach_dev(dev, uc) { |
| 102 | struct blk_desc *desc = dev_get_uclass_platdata(dev); |
| 103 | |
| 104 | debug("%s: if_type=%d, devnum=%d: %s, %d, %d\n", __func__, |
| 105 | if_type, devnum, dev->name, desc->if_type, desc->devnum); |
| 106 | if (desc->devnum != devnum) |
| 107 | continue; |
| 108 | |
| 109 | /* Find out the parent device uclass */ |
| 110 | if (device_get_uclass_id(dev->parent) != uclass_id) { |
| 111 | debug("%s: parent uclass %d, this dev %d\n", __func__, |
| 112 | device_get_uclass_id(dev->parent), uclass_id); |
| 113 | continue; |
| 114 | } |
| 115 | |
| 116 | if (device_probe(dev)) |
| 117 | return NULL; |
| 118 | |
| 119 | debug("%s: Device desc %p\n", __func__, desc); |
| 120 | return desc; |
| 121 | } |
| 122 | debug("%s: No device found\n", __func__); |
| 123 | |
| 124 | return NULL; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * get_desc() - Get the block device descriptor for the given device number |
| 129 | * |
| 130 | * @if_type: Interface type |
| 131 | * @devnum: Device number (0 = first) |
| 132 | * @descp: Returns block device descriptor on success |
| 133 | * @return 0 on success, -ENODEV if there is no such device and no device |
| 134 | * with a higher device number, -ENOENT if there is no such device but there |
| 135 | * is one with a higher number, or other -ve on other error. |
| 136 | */ |
| 137 | static int get_desc(enum if_type if_type, int devnum, struct blk_desc **descp) |
| 138 | { |
| 139 | bool found_more = false; |
| 140 | struct udevice *dev; |
| 141 | struct uclass *uc; |
| 142 | int ret; |
| 143 | |
| 144 | *descp = NULL; |
| 145 | ret = uclass_get(UCLASS_BLK, &uc); |
| 146 | if (ret) |
| 147 | return ret; |
| 148 | uclass_foreach_dev(dev, uc) { |
| 149 | struct blk_desc *desc = dev_get_uclass_platdata(dev); |
| 150 | |
| 151 | debug("%s: if_type=%d, devnum=%d: %s, %d, %d\n", __func__, |
| 152 | if_type, devnum, dev->name, desc->if_type, desc->devnum); |
| 153 | if (desc->if_type == if_type) { |
| 154 | if (desc->devnum == devnum) { |
| 155 | ret = device_probe(dev); |
| 156 | if (ret) |
| 157 | return ret; |
| 158 | |
Michal Simek | f438b6b | 2016-11-16 17:37:42 +0100 | [diff] [blame] | 159 | *descp = desc; |
| 160 | return 0; |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 161 | } else if (desc->devnum > devnum) { |
| 162 | found_more = true; |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | return found_more ? -ENOENT : -ENODEV; |
| 168 | } |
| 169 | |
Simon Glass | 13c2c29 | 2016-05-01 13:52:30 -0600 | [diff] [blame] | 170 | int blk_select_hwpart_devnum(enum if_type if_type, int devnum, int hwpart) |
| 171 | { |
| 172 | struct udevice *dev; |
| 173 | int ret; |
| 174 | |
| 175 | ret = blk_get_device(if_type, devnum, &dev); |
| 176 | if (ret) |
| 177 | return ret; |
| 178 | |
| 179 | return blk_select_hwpart(dev, hwpart); |
| 180 | } |
| 181 | |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 182 | int blk_list_part(enum if_type if_type) |
| 183 | { |
| 184 | struct blk_desc *desc; |
| 185 | int devnum, ok; |
| 186 | int ret; |
| 187 | |
| 188 | for (ok = 0, devnum = 0;; ++devnum) { |
| 189 | ret = get_desc(if_type, devnum, &desc); |
| 190 | if (ret == -ENODEV) |
| 191 | break; |
| 192 | else if (ret) |
| 193 | continue; |
| 194 | if (desc->part_type != PART_TYPE_UNKNOWN) { |
| 195 | ++ok; |
| 196 | if (devnum) |
| 197 | putc('\n'); |
| 198 | part_print(desc); |
| 199 | } |
| 200 | } |
| 201 | if (!ok) |
| 202 | return -ENODEV; |
| 203 | |
| 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | int blk_print_part_devnum(enum if_type if_type, int devnum) |
| 208 | { |
| 209 | struct blk_desc *desc; |
| 210 | int ret; |
| 211 | |
| 212 | ret = get_desc(if_type, devnum, &desc); |
| 213 | if (ret) |
| 214 | return ret; |
| 215 | if (desc->type == DEV_TYPE_UNKNOWN) |
| 216 | return -ENOENT; |
| 217 | part_print(desc); |
| 218 | |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | void blk_list_devices(enum if_type if_type) |
| 223 | { |
| 224 | struct blk_desc *desc; |
| 225 | int ret; |
| 226 | int i; |
| 227 | |
| 228 | for (i = 0;; ++i) { |
| 229 | ret = get_desc(if_type, i, &desc); |
| 230 | if (ret == -ENODEV) |
| 231 | break; |
| 232 | else if (ret) |
| 233 | continue; |
| 234 | if (desc->type == DEV_TYPE_UNKNOWN) |
| 235 | continue; /* list only known devices */ |
| 236 | printf("Device %d: ", i); |
| 237 | dev_print(desc); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | int blk_print_device_num(enum if_type if_type, int devnum) |
| 242 | { |
| 243 | struct blk_desc *desc; |
| 244 | int ret; |
| 245 | |
| 246 | ret = get_desc(if_type, devnum, &desc); |
| 247 | if (ret) |
| 248 | return ret; |
| 249 | printf("\nIDE device %d: ", devnum); |
| 250 | dev_print(desc); |
| 251 | |
| 252 | return 0; |
| 253 | } |
| 254 | |
| 255 | int blk_show_device(enum if_type if_type, int devnum) |
| 256 | { |
| 257 | struct blk_desc *desc; |
| 258 | int ret; |
| 259 | |
| 260 | printf("\nDevice %d: ", devnum); |
| 261 | ret = get_desc(if_type, devnum, &desc); |
| 262 | if (ret == -ENODEV || ret == -ENOENT) { |
| 263 | printf("unknown device\n"); |
| 264 | return -ENODEV; |
| 265 | } |
| 266 | if (ret) |
| 267 | return ret; |
| 268 | dev_print(desc); |
| 269 | |
| 270 | if (desc->type == DEV_TYPE_UNKNOWN) |
| 271 | return -ENOENT; |
| 272 | |
| 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | ulong blk_read_devnum(enum if_type if_type, int devnum, lbaint_t start, |
| 277 | lbaint_t blkcnt, void *buffer) |
| 278 | { |
| 279 | struct blk_desc *desc; |
| 280 | ulong n; |
| 281 | int ret; |
| 282 | |
| 283 | ret = get_desc(if_type, devnum, &desc); |
| 284 | if (ret) |
| 285 | return ret; |
| 286 | n = blk_dread(desc, start, blkcnt, buffer); |
| 287 | if (IS_ERR_VALUE(n)) |
| 288 | return n; |
| 289 | |
| 290 | /* flush cache after read */ |
| 291 | flush_cache((ulong)buffer, blkcnt * desc->blksz); |
| 292 | |
| 293 | return n; |
| 294 | } |
| 295 | |
| 296 | ulong blk_write_devnum(enum if_type if_type, int devnum, lbaint_t start, |
| 297 | lbaint_t blkcnt, const void *buffer) |
| 298 | { |
| 299 | struct blk_desc *desc; |
| 300 | int ret; |
| 301 | |
| 302 | ret = get_desc(if_type, devnum, &desc); |
| 303 | if (ret) |
| 304 | return ret; |
| 305 | return blk_dwrite(desc, start, blkcnt, buffer); |
| 306 | } |
| 307 | |
Simon Glass | 13c2c29 | 2016-05-01 13:52:30 -0600 | [diff] [blame] | 308 | int blk_select_hwpart(struct udevice *dev, int hwpart) |
| 309 | { |
| 310 | const struct blk_ops *ops = blk_get_ops(dev); |
| 311 | |
| 312 | if (!ops) |
| 313 | return -ENOSYS; |
| 314 | if (!ops->select_hwpart) |
| 315 | return 0; |
| 316 | |
| 317 | return ops->select_hwpart(dev, hwpart); |
| 318 | } |
| 319 | |
| 320 | int blk_dselect_hwpart(struct blk_desc *desc, int hwpart) |
| 321 | { |
| 322 | return blk_select_hwpart(desc->bdev, hwpart); |
| 323 | } |
| 324 | |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 325 | int blk_first_device(int if_type, struct udevice **devp) |
| 326 | { |
| 327 | struct blk_desc *desc; |
| 328 | int ret; |
| 329 | |
| 330 | ret = uclass_first_device(UCLASS_BLK, devp); |
| 331 | if (ret) |
| 332 | return ret; |
| 333 | if (!*devp) |
| 334 | return -ENODEV; |
| 335 | do { |
| 336 | desc = dev_get_uclass_platdata(*devp); |
| 337 | if (desc->if_type == if_type) |
| 338 | return 0; |
| 339 | ret = uclass_next_device(devp); |
| 340 | if (ret) |
| 341 | return ret; |
| 342 | } while (*devp); |
| 343 | |
| 344 | return -ENODEV; |
| 345 | } |
| 346 | |
| 347 | int blk_next_device(struct udevice **devp) |
| 348 | { |
| 349 | struct blk_desc *desc; |
| 350 | int ret, if_type; |
| 351 | |
| 352 | desc = dev_get_uclass_platdata(*devp); |
| 353 | if_type = desc->if_type; |
| 354 | do { |
| 355 | ret = uclass_next_device(devp); |
| 356 | if (ret) |
| 357 | return ret; |
| 358 | if (!*devp) |
| 359 | return -ENODEV; |
| 360 | desc = dev_get_uclass_platdata(*devp); |
| 361 | if (desc->if_type == if_type) |
| 362 | return 0; |
| 363 | } while (1); |
| 364 | } |
| 365 | |
Simon Glass | d5d4c10 | 2017-04-23 20:02:05 -0600 | [diff] [blame] | 366 | int blk_find_device(int if_type, int devnum, struct udevice **devp) |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 367 | { |
| 368 | struct uclass *uc; |
| 369 | struct udevice *dev; |
| 370 | int ret; |
| 371 | |
| 372 | ret = uclass_get(UCLASS_BLK, &uc); |
| 373 | if (ret) |
| 374 | return ret; |
| 375 | uclass_foreach_dev(dev, uc) { |
| 376 | struct blk_desc *desc = dev_get_uclass_platdata(dev); |
| 377 | |
| 378 | debug("%s: if_type=%d, devnum=%d: %s, %d, %d\n", __func__, |
| 379 | if_type, devnum, dev->name, desc->if_type, desc->devnum); |
| 380 | if (desc->if_type == if_type && desc->devnum == devnum) { |
| 381 | *devp = dev; |
Simon Glass | d5d4c10 | 2017-04-23 20:02:05 -0600 | [diff] [blame] | 382 | return 0; |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 383 | } |
| 384 | } |
| 385 | |
| 386 | return -ENODEV; |
| 387 | } |
| 388 | |
Simon Glass | d5d4c10 | 2017-04-23 20:02:05 -0600 | [diff] [blame] | 389 | int blk_get_device(int if_type, int devnum, struct udevice **devp) |
| 390 | { |
| 391 | int ret; |
| 392 | |
| 393 | ret = blk_find_device(if_type, devnum, devp); |
| 394 | if (ret) |
| 395 | return ret; |
| 396 | |
| 397 | return device_probe(*devp); |
| 398 | } |
| 399 | |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 400 | unsigned long blk_dread(struct blk_desc *block_dev, lbaint_t start, |
| 401 | lbaint_t blkcnt, void *buffer) |
| 402 | { |
| 403 | struct udevice *dev = block_dev->bdev; |
| 404 | const struct blk_ops *ops = blk_get_ops(dev); |
Eric Nelson | faf4f05 | 2016-03-28 10:05:44 -0700 | [diff] [blame] | 405 | ulong blks_read; |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 406 | |
| 407 | if (!ops->read) |
| 408 | return -ENOSYS; |
| 409 | |
Eric Nelson | faf4f05 | 2016-03-28 10:05:44 -0700 | [diff] [blame] | 410 | if (blkcache_read(block_dev->if_type, block_dev->devnum, |
| 411 | start, blkcnt, block_dev->blksz, buffer)) |
| 412 | return blkcnt; |
| 413 | blks_read = ops->read(dev, start, blkcnt, buffer); |
| 414 | if (blks_read == blkcnt) |
| 415 | blkcache_fill(block_dev->if_type, block_dev->devnum, |
| 416 | start, blkcnt, block_dev->blksz, buffer); |
| 417 | |
| 418 | return blks_read; |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | unsigned long blk_dwrite(struct blk_desc *block_dev, lbaint_t start, |
| 422 | lbaint_t blkcnt, const void *buffer) |
| 423 | { |
| 424 | struct udevice *dev = block_dev->bdev; |
| 425 | const struct blk_ops *ops = blk_get_ops(dev); |
| 426 | |
| 427 | if (!ops->write) |
| 428 | return -ENOSYS; |
| 429 | |
Eric Nelson | faf4f05 | 2016-03-28 10:05:44 -0700 | [diff] [blame] | 430 | blkcache_invalidate(block_dev->if_type, block_dev->devnum); |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 431 | return ops->write(dev, start, blkcnt, buffer); |
| 432 | } |
| 433 | |
| 434 | unsigned long blk_derase(struct blk_desc *block_dev, lbaint_t start, |
| 435 | lbaint_t blkcnt) |
| 436 | { |
| 437 | struct udevice *dev = block_dev->bdev; |
| 438 | const struct blk_ops *ops = blk_get_ops(dev); |
| 439 | |
| 440 | if (!ops->erase) |
| 441 | return -ENOSYS; |
| 442 | |
Eric Nelson | faf4f05 | 2016-03-28 10:05:44 -0700 | [diff] [blame] | 443 | blkcache_invalidate(block_dev->if_type, block_dev->devnum); |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 444 | return ops->erase(dev, start, blkcnt); |
| 445 | } |
| 446 | |
| 447 | int blk_prepare_device(struct udevice *dev) |
| 448 | { |
| 449 | struct blk_desc *desc = dev_get_uclass_platdata(dev); |
| 450 | |
| 451 | part_init(desc); |
| 452 | |
| 453 | return 0; |
| 454 | } |
| 455 | |
Simon Glass | d089ba3 | 2016-05-01 11:36:28 -0600 | [diff] [blame] | 456 | int blk_find_max_devnum(enum if_type if_type) |
| 457 | { |
| 458 | struct udevice *dev; |
| 459 | int max_devnum = -ENODEV; |
| 460 | struct uclass *uc; |
| 461 | int ret; |
| 462 | |
| 463 | ret = uclass_get(UCLASS_BLK, &uc); |
| 464 | if (ret) |
| 465 | return ret; |
| 466 | uclass_foreach_dev(dev, uc) { |
| 467 | struct blk_desc *desc = dev_get_uclass_platdata(dev); |
| 468 | |
| 469 | if (desc->if_type == if_type && desc->devnum > max_devnum) |
| 470 | max_devnum = desc->devnum; |
| 471 | } |
| 472 | |
| 473 | return max_devnum; |
| 474 | } |
| 475 | |
Simon Glass | dbc3861 | 2017-04-23 20:02:06 -0600 | [diff] [blame^] | 476 | static int blk_next_free_devnum(enum if_type if_type) |
| 477 | { |
| 478 | int ret; |
| 479 | |
| 480 | ret = blk_find_max_devnum(if_type); |
| 481 | if (ret == -ENODEV) |
| 482 | return 0; |
| 483 | if (ret < 0) |
| 484 | return ret; |
| 485 | |
| 486 | return ret + 1; |
| 487 | } |
| 488 | |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 489 | int blk_create_device(struct udevice *parent, const char *drv_name, |
| 490 | const char *name, int if_type, int devnum, int blksz, |
| 491 | lbaint_t size, struct udevice **devp) |
| 492 | { |
| 493 | struct blk_desc *desc; |
| 494 | struct udevice *dev; |
| 495 | int ret; |
| 496 | |
Simon Glass | d089ba3 | 2016-05-01 11:36:28 -0600 | [diff] [blame] | 497 | if (devnum == -1) { |
Simon Glass | dbc3861 | 2017-04-23 20:02:06 -0600 | [diff] [blame^] | 498 | ret = blk_next_free_devnum(if_type); |
| 499 | if (ret < 0) |
Simon Glass | d089ba3 | 2016-05-01 11:36:28 -0600 | [diff] [blame] | 500 | return ret; |
Simon Glass | dbc3861 | 2017-04-23 20:02:06 -0600 | [diff] [blame^] | 501 | devnum = ret; |
Simon Glass | d089ba3 | 2016-05-01 11:36:28 -0600 | [diff] [blame] | 502 | } |
Simon Glass | 77f7fb8 | 2016-05-01 13:52:22 -0600 | [diff] [blame] | 503 | ret = device_bind_driver(parent, drv_name, name, &dev); |
| 504 | if (ret) |
| 505 | return ret; |
| 506 | desc = dev_get_uclass_platdata(dev); |
| 507 | desc->if_type = if_type; |
| 508 | desc->blksz = blksz; |
| 509 | desc->lba = size / blksz; |
| 510 | desc->part_type = PART_TYPE_UNKNOWN; |
| 511 | desc->bdev = dev; |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 512 | desc->devnum = devnum; |
| 513 | *devp = dev; |
| 514 | |
| 515 | return 0; |
| 516 | } |
| 517 | |
Simon Glass | 966b695 | 2016-05-01 11:36:29 -0600 | [diff] [blame] | 518 | int blk_create_devicef(struct udevice *parent, const char *drv_name, |
| 519 | const char *name, int if_type, int devnum, int blksz, |
| 520 | lbaint_t size, struct udevice **devp) |
| 521 | { |
| 522 | char dev_name[30], *str; |
Simon Glass | 39e5492 | 2016-05-01 13:52:24 -0600 | [diff] [blame] | 523 | int ret; |
Simon Glass | 966b695 | 2016-05-01 11:36:29 -0600 | [diff] [blame] | 524 | |
| 525 | snprintf(dev_name, sizeof(dev_name), "%s.%s", parent->name, name); |
| 526 | str = strdup(dev_name); |
| 527 | if (!str) |
| 528 | return -ENOMEM; |
| 529 | |
Simon Glass | 39e5492 | 2016-05-01 13:52:24 -0600 | [diff] [blame] | 530 | ret = blk_create_device(parent, drv_name, str, if_type, devnum, |
| 531 | blksz, size, devp); |
| 532 | if (ret) { |
| 533 | free(str); |
| 534 | return ret; |
| 535 | } |
| 536 | device_set_name_alloced(*devp); |
| 537 | |
| 538 | return ret; |
Simon Glass | 966b695 | 2016-05-01 11:36:29 -0600 | [diff] [blame] | 539 | } |
| 540 | |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 541 | int blk_unbind_all(int if_type) |
| 542 | { |
| 543 | struct uclass *uc; |
| 544 | struct udevice *dev, *next; |
| 545 | int ret; |
| 546 | |
| 547 | ret = uclass_get(UCLASS_BLK, &uc); |
| 548 | if (ret) |
| 549 | return ret; |
| 550 | uclass_foreach_dev_safe(dev, next, uc) { |
| 551 | struct blk_desc *desc = dev_get_uclass_platdata(dev); |
| 552 | |
| 553 | if (desc->if_type == if_type) { |
Stefan Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 554 | ret = device_remove(dev, DM_REMOVE_NORMAL); |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 555 | if (ret) |
| 556 | return ret; |
| 557 | ret = device_unbind(dev); |
| 558 | if (ret) |
| 559 | return ret; |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | return 0; |
| 564 | } |
| 565 | |
| 566 | UCLASS_DRIVER(blk) = { |
| 567 | .id = UCLASS_BLK, |
| 568 | .name = "blk", |
| 569 | .per_device_platdata_auto_alloc_size = sizeof(struct blk_desc), |
| 570 | }; |