1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2016 Google, Inc
11 struct blk_driver *blk_driver_lookup_type(int uclass_id)
13 struct blk_driver *drv = ll_entry_start(struct blk_driver, blk_driver);
14 const int n_ents = ll_entry_count(struct blk_driver, blk_driver);
15 struct blk_driver *entry;
17 for (entry = drv; entry != drv + n_ents; entry++) {
18 if (uclass_id == entry->uclass_id)
26 static struct blk_driver *blk_driver_lookup_typename(const char *uclass_idname)
28 struct blk_driver *drv = ll_entry_start(struct blk_driver, blk_driver);
29 const int n_ents = ll_entry_count(struct blk_driver, blk_driver);
30 struct blk_driver *entry;
32 for (entry = drv; entry != drv + n_ents; entry++) {
33 if (!strcmp(uclass_idname, entry->uclass_idname))
41 const char *blk_get_uclass_name(enum uclass_id uclass_id)
43 struct blk_driver *drv = blk_driver_lookup_type(uclass_id);
45 return drv ? drv->uclass_idname : NULL;
49 * get_desc() - Get the block device descriptor for the given device number
51 * @drv: Legacy block driver
52 * @devnum: Device number (0 = first)
53 * @descp: Returns block device descriptor on success
54 * Return: 0 on success, -ENODEV if there is no such device, -ENOSYS if the
55 * driver does not provide a way to find a device, or other -ve on other
58 static int get_desc(struct blk_driver *drv, int devnum, struct blk_desc **descp)
61 if (devnum < 0 || devnum >= drv->max_devs)
63 *descp = &drv->desc[devnum];
69 return drv->get_dev(devnum, descp);
72 int blk_list_part(enum uclass_id uclass_id)
74 struct blk_driver *drv;
75 struct blk_desc *desc;
79 drv = blk_driver_lookup_type(uclass_id);
82 for (ok = 0, devnum = 0; devnum < drv->max_devs; ++devnum) {
83 if (get_desc(drv, devnum, &desc))
85 if (desc->part_type != PART_TYPE_UNKNOWN) {
99 int blk_print_part_devnum(enum uclass_id uclass_id, int devnum)
101 struct blk_driver *drv = blk_driver_lookup_type(uclass_id);
102 struct blk_desc *desc;
107 ret = get_desc(drv, devnum, &desc);
110 if (desc->type == DEV_TYPE_UNKNOWN)
117 void blk_list_devices(enum uclass_id uclass_id)
119 struct blk_driver *drv = blk_driver_lookup_type(uclass_id);
120 struct blk_desc *desc;
125 for (i = 0; i < drv->max_devs; ++i) {
126 if (get_desc(drv, i, &desc))
128 if (desc->type == DEV_TYPE_UNKNOWN)
129 continue; /* list only known devices */
130 printf("Device %d: ", i);
135 int blk_print_device_num(enum uclass_id uclass_id, int devnum)
137 struct blk_driver *drv = blk_driver_lookup_type(uclass_id);
138 struct blk_desc *desc;
143 ret = get_desc(drv, devnum, &desc);
146 printf("\n%s device %d: ", drv->uclass_idname, devnum);
152 int blk_show_device(enum uclass_id uclass_id, int devnum)
154 struct blk_driver *drv = blk_driver_lookup_type(uclass_id);
155 struct blk_desc *desc;
160 printf("\nDevice %d: ", devnum);
161 if (devnum >= drv->max_devs) {
162 puts("unknown device\n");
165 ret = get_desc(drv, devnum, &desc);
170 if (desc->type == DEV_TYPE_UNKNOWN)
176 struct blk_desc *blk_get_devnum_by_uclass_id(enum uclass_id uclass_id, int devnum)
178 struct blk_driver *drv = blk_driver_lookup_type(uclass_id);
179 struct blk_desc *desc;
184 if (get_desc(drv, devnum, &desc))
190 int blk_dselect_hwpart(struct blk_desc *desc, int hwpart)
192 struct blk_driver *drv = blk_driver_lookup_type(desc->uclass_id);
196 if (drv->select_hwpart)
197 return drv->select_hwpart(desc, hwpart);
202 struct blk_desc *blk_get_devnum_by_uclass_idname(const char *uclass_idname, int devnum)
204 struct blk_driver *drv = blk_driver_lookup_typename(uclass_idname);
205 struct blk_desc *desc;
210 if (get_desc(drv, devnum, &desc))
216 ulong blk_read_devnum(enum uclass_id uclass_id, int devnum, lbaint_t start,
217 lbaint_t blkcnt, void *buffer)
219 struct blk_driver *drv = blk_driver_lookup_type(uclass_id);
220 struct blk_desc *desc;
226 ret = get_desc(drv, devnum, &desc);
229 n = desc->block_read(desc, start, blkcnt, buffer);
236 ulong blk_write_devnum(enum uclass_id uclass_id, int devnum, lbaint_t start,
237 lbaint_t blkcnt, const void *buffer)
239 struct blk_driver *drv = blk_driver_lookup_type(uclass_id);
240 struct blk_desc *desc;
245 ret = get_desc(drv, devnum, &desc);
248 return desc->block_write(desc, start, blkcnt, buffer);
251 int blk_select_hwpart_devnum(enum uclass_id uclass_id, int devnum, int hwpart)
253 struct blk_driver *drv = blk_driver_lookup_type(uclass_id);
254 struct blk_desc *desc;
259 ret = get_desc(drv, devnum, &desc);
262 return drv->select_hwpart(desc, hwpart);