1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2016 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
10 #include <linux/err.h>
12 struct blk_driver *blk_driver_lookup_type(int uclass_id)
14 struct blk_driver *drv = ll_entry_start(struct blk_driver, blk_driver);
15 const int n_ents = ll_entry_count(struct blk_driver, blk_driver);
16 struct blk_driver *entry;
18 for (entry = drv; entry != drv + n_ents; entry++) {
19 if (uclass_id == entry->uclass_id)
27 static struct blk_driver *blk_driver_lookup_typename(const char *uclass_idname)
29 struct blk_driver *drv = ll_entry_start(struct blk_driver, blk_driver);
30 const int n_ents = ll_entry_count(struct blk_driver, blk_driver);
31 struct blk_driver *entry;
33 for (entry = drv; entry != drv + n_ents; entry++) {
34 if (!strcmp(uclass_idname, entry->uclass_idname))
42 const char *blk_get_uclass_name(enum uclass_id uclass_id)
44 struct blk_driver *drv = blk_driver_lookup_type(uclass_id);
46 return drv ? drv->uclass_idname : NULL;
50 * get_desc() - Get the block device descriptor for the given device number
52 * @drv: Legacy block driver
53 * @devnum: Device number (0 = first)
54 * @descp: Returns block device descriptor on success
55 * Return: 0 on success, -ENODEV if there is no such device, -ENOSYS if the
56 * driver does not provide a way to find a device, or other -ve on other
59 static int get_desc(struct blk_driver *drv, int devnum, struct blk_desc **descp)
62 if (devnum < 0 || devnum >= drv->max_devs)
64 *descp = &drv->desc[devnum];
70 return drv->get_dev(devnum, descp);
73 int blk_list_part(enum uclass_id uclass_id)
75 struct blk_driver *drv;
76 struct blk_desc *desc;
80 drv = blk_driver_lookup_type(uclass_id);
83 for (ok = 0, devnum = 0; devnum < drv->max_devs; ++devnum) {
84 if (get_desc(drv, devnum, &desc))
86 if (desc->part_type != PART_TYPE_UNKNOWN) {
100 int blk_print_part_devnum(enum uclass_id uclass_id, int devnum)
102 struct blk_driver *drv = blk_driver_lookup_type(uclass_id);
103 struct blk_desc *desc;
108 ret = get_desc(drv, devnum, &desc);
111 if (desc->type == DEV_TYPE_UNKNOWN)
118 void blk_list_devices(enum uclass_id uclass_id)
120 struct blk_driver *drv = blk_driver_lookup_type(uclass_id);
121 struct blk_desc *desc;
126 for (i = 0; i < drv->max_devs; ++i) {
127 if (get_desc(drv, i, &desc))
129 if (desc->type == DEV_TYPE_UNKNOWN)
130 continue; /* list only known devices */
131 printf("Device %d: ", i);
136 int blk_print_device_num(enum uclass_id uclass_id, int devnum)
138 struct blk_driver *drv = blk_driver_lookup_type(uclass_id);
139 struct blk_desc *desc;
144 ret = get_desc(drv, devnum, &desc);
147 printf("\n%s device %d: ", drv->uclass_idname, devnum);
153 int blk_show_device(enum uclass_id uclass_id, int devnum)
155 struct blk_driver *drv = blk_driver_lookup_type(uclass_id);
156 struct blk_desc *desc;
161 printf("\nDevice %d: ", devnum);
162 if (devnum >= drv->max_devs) {
163 puts("unknown device\n");
166 ret = get_desc(drv, devnum, &desc);
171 if (desc->type == DEV_TYPE_UNKNOWN)
177 struct blk_desc *blk_get_devnum_by_uclass_id(enum uclass_id uclass_id, int devnum)
179 struct blk_driver *drv = blk_driver_lookup_type(uclass_id);
180 struct blk_desc *desc;
185 if (get_desc(drv, devnum, &desc))
191 int blk_dselect_hwpart(struct blk_desc *desc, int hwpart)
193 struct blk_driver *drv = blk_driver_lookup_type(desc->uclass_id);
197 if (drv->select_hwpart)
198 return drv->select_hwpart(desc, hwpart);
203 struct blk_desc *blk_get_devnum_by_uclass_idname(const char *uclass_idname, int devnum)
205 struct blk_driver *drv = blk_driver_lookup_typename(uclass_idname);
206 struct blk_desc *desc;
211 if (get_desc(drv, devnum, &desc))
217 ulong blk_read_devnum(enum uclass_id uclass_id, int devnum, lbaint_t start,
218 lbaint_t blkcnt, void *buffer)
220 struct blk_driver *drv = blk_driver_lookup_type(uclass_id);
221 struct blk_desc *desc;
227 ret = get_desc(drv, devnum, &desc);
230 n = desc->block_read(desc, start, blkcnt, buffer);
237 ulong blk_write_devnum(enum uclass_id uclass_id, int devnum, lbaint_t start,
238 lbaint_t blkcnt, const void *buffer)
240 struct blk_driver *drv = blk_driver_lookup_type(uclass_id);
241 struct blk_desc *desc;
246 ret = get_desc(drv, devnum, &desc);
249 return desc->block_write(desc, start, blkcnt, buffer);
252 int blk_select_hwpart_devnum(enum uclass_id uclass_id, int devnum, int hwpart)
254 struct blk_driver *drv = blk_driver_lookup_type(uclass_id);
255 struct blk_desc *desc;
260 ret = get_desc(drv, devnum, &desc);
263 return drv->select_hwpart(desc, hwpart);