Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / mmc / core / sdio_cis.c
blob44bea5e4aeda19b05c8e5c8149f2276f7e4e4ae9
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * linux/drivers/mmc/core/sdio_cis.c
5 * Author: Nicolas Pitre
6 * Created: June 11, 2007
7 * Copyright: MontaVista Software Inc.
9 * Copyright 2007 Pierre Ossman
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
15 #include <linux/mmc/host.h>
16 #include <linux/mmc/card.h>
17 #include <linux/mmc/sdio.h>
18 #include <linux/mmc/sdio_func.h>
20 #include "sdio_cis.h"
21 #include "sdio_ops.h"
23 static int cistpl_vers_1(struct mmc_card *card, struct sdio_func *func,
24 const unsigned char *buf, unsigned size)
26 u8 major_rev, minor_rev;
27 unsigned i, nr_strings;
28 char **buffer, *string;
30 if (size < 2)
31 return 0;
33 major_rev = buf[0];
34 minor_rev = buf[1];
36 /* Find all null-terminated (including zero length) strings in
37 the TPLLV1_INFO field. Trailing garbage is ignored. */
38 buf += 2;
39 size -= 2;
41 nr_strings = 0;
42 for (i = 0; i < size; i++) {
43 if (buf[i] == 0xff)
44 break;
45 if (buf[i] == 0)
46 nr_strings++;
48 if (nr_strings == 0)
49 return 0;
51 size = i;
53 buffer = kzalloc(sizeof(char*) * nr_strings + size, GFP_KERNEL);
54 if (!buffer)
55 return -ENOMEM;
57 string = (char*)(buffer + nr_strings);
59 for (i = 0; i < nr_strings; i++) {
60 buffer[i] = string;
61 strcpy(string, buf);
62 string += strlen(string) + 1;
63 buf += strlen(buf) + 1;
66 if (func) {
67 func->major_rev = major_rev;
68 func->minor_rev = minor_rev;
69 func->num_info = nr_strings;
70 func->info = (const char**)buffer;
71 } else {
72 card->major_rev = major_rev;
73 card->minor_rev = minor_rev;
74 card->num_info = nr_strings;
75 card->info = (const char**)buffer;
78 return 0;
81 static int cistpl_manfid(struct mmc_card *card, struct sdio_func *func,
82 const unsigned char *buf, unsigned size)
84 unsigned int vendor, device;
86 /* TPLMID_MANF */
87 vendor = buf[0] | (buf[1] << 8);
89 /* TPLMID_CARD */
90 device = buf[2] | (buf[3] << 8);
92 if (func) {
93 func->vendor = vendor;
94 func->device = device;
95 } else {
96 card->cis.vendor = vendor;
97 card->cis.device = device;
100 return 0;
103 static const unsigned char speed_val[16] =
104 { 0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80 };
105 static const unsigned int speed_unit[8] =
106 { 10000, 100000, 1000000, 10000000, 0, 0, 0, 0 };
109 typedef int (tpl_parse_t)(struct mmc_card *, struct sdio_func *,
110 const unsigned char *, unsigned);
112 struct cis_tpl {
113 unsigned char code;
114 unsigned char min_size;
115 tpl_parse_t *parse;
118 static int cis_tpl_parse(struct mmc_card *card, struct sdio_func *func,
119 const char *tpl_descr,
120 const struct cis_tpl *tpl, int tpl_count,
121 unsigned char code,
122 const unsigned char *buf, unsigned size)
124 int i, ret;
126 /* look for a matching code in the table */
127 for (i = 0; i < tpl_count; i++, tpl++) {
128 if (tpl->code == code)
129 break;
131 if (i < tpl_count) {
132 if (size >= tpl->min_size) {
133 if (tpl->parse)
134 ret = tpl->parse(card, func, buf, size);
135 else
136 ret = -EILSEQ; /* known tuple, not parsed */
137 } else {
138 /* invalid tuple */
139 ret = -EINVAL;
141 if (ret && ret != -EILSEQ && ret != -ENOENT) {
142 pr_err("%s: bad %s tuple 0x%02x (%u bytes)\n",
143 mmc_hostname(card->host), tpl_descr, code, size);
145 } else {
146 /* unknown tuple */
147 ret = -ENOENT;
150 return ret;
153 static int cistpl_funce_common(struct mmc_card *card, struct sdio_func *func,
154 const unsigned char *buf, unsigned size)
156 /* Only valid for the common CIS (function 0) */
157 if (func)
158 return -EINVAL;
160 /* TPLFE_FN0_BLK_SIZE */
161 card->cis.blksize = buf[1] | (buf[2] << 8);
163 /* TPLFE_MAX_TRAN_SPEED */
164 card->cis.max_dtr = speed_val[(buf[3] >> 3) & 15] *
165 speed_unit[buf[3] & 7];
167 return 0;
170 static int cistpl_funce_func(struct mmc_card *card, struct sdio_func *func,
171 const unsigned char *buf, unsigned size)
173 unsigned vsn;
174 unsigned min_size;
176 /* Only valid for the individual function's CIS (1-7) */
177 if (!func)
178 return -EINVAL;
181 * This tuple has a different length depending on the SDIO spec
182 * version.
184 vsn = func->card->cccr.sdio_vsn;
185 min_size = (vsn == SDIO_SDIO_REV_1_00) ? 28 : 42;
187 if (size == 28 && vsn == SDIO_SDIO_REV_1_10) {
188 pr_warn("%s: card has broken SDIO 1.1 CIS, forcing SDIO 1.0\n",
189 mmc_hostname(card->host));
190 vsn = SDIO_SDIO_REV_1_00;
191 } else if (size < min_size) {
192 return -EINVAL;
195 /* TPLFE_MAX_BLK_SIZE */
196 func->max_blksize = buf[12] | (buf[13] << 8);
198 /* TPLFE_ENABLE_TIMEOUT_VAL, present in ver 1.1 and above */
199 if (vsn > SDIO_SDIO_REV_1_00)
200 func->enable_timeout = (buf[28] | (buf[29] << 8)) * 10;
201 else
202 func->enable_timeout = jiffies_to_msecs(HZ);
204 return 0;
208 * Known TPLFE_TYPEs table for CISTPL_FUNCE tuples.
210 * Note that, unlike PCMCIA, CISTPL_FUNCE tuples are not parsed depending
211 * on the TPLFID_FUNCTION value of the previous CISTPL_FUNCID as on SDIO
212 * TPLFID_FUNCTION is always hardcoded to 0x0C.
214 static const struct cis_tpl cis_tpl_funce_list[] = {
215 { 0x00, 4, cistpl_funce_common },
216 { 0x01, 0, cistpl_funce_func },
217 { 0x04, 1+1+6, /* CISTPL_FUNCE_LAN_NODE_ID */ },
220 static int cistpl_funce(struct mmc_card *card, struct sdio_func *func,
221 const unsigned char *buf, unsigned size)
223 if (size < 1)
224 return -EINVAL;
226 return cis_tpl_parse(card, func, "CISTPL_FUNCE",
227 cis_tpl_funce_list,
228 ARRAY_SIZE(cis_tpl_funce_list),
229 buf[0], buf, size);
232 /* Known TPL_CODEs table for CIS tuples */
233 static const struct cis_tpl cis_tpl_list[] = {
234 { 0x15, 3, cistpl_vers_1 },
235 { 0x20, 4, cistpl_manfid },
236 { 0x21, 2, /* cistpl_funcid */ },
237 { 0x22, 0, cistpl_funce },
238 { 0x91, 2, /* cistpl_sdio_std */ },
241 static int sdio_read_cis(struct mmc_card *card, struct sdio_func *func)
243 int ret;
244 struct sdio_func_tuple *this, **prev;
245 unsigned i, ptr = 0;
248 * Note that this works for the common CIS (function number 0) as
249 * well as a function's CIS * since SDIO_CCCR_CIS and SDIO_FBR_CIS
250 * have the same offset.
252 for (i = 0; i < 3; i++) {
253 unsigned char x, fn;
255 if (func)
256 fn = func->num;
257 else
258 fn = 0;
260 ret = mmc_io_rw_direct(card, 0, 0,
261 SDIO_FBR_BASE(fn) + SDIO_FBR_CIS + i, 0, &x);
262 if (ret)
263 return ret;
264 ptr |= x << (i * 8);
267 if (func)
268 prev = &func->tuples;
269 else
270 prev = &card->tuples;
272 if (*prev)
273 return -EINVAL;
275 do {
276 unsigned char tpl_code, tpl_link;
278 ret = mmc_io_rw_direct(card, 0, 0, ptr++, 0, &tpl_code);
279 if (ret)
280 break;
282 /* 0xff means we're done */
283 if (tpl_code == 0xff)
284 break;
286 /* null entries have no link field or data */
287 if (tpl_code == 0x00)
288 continue;
290 ret = mmc_io_rw_direct(card, 0, 0, ptr++, 0, &tpl_link);
291 if (ret)
292 break;
294 /* a size of 0xff also means we're done */
295 if (tpl_link == 0xff)
296 break;
298 this = kmalloc(sizeof(*this) + tpl_link, GFP_KERNEL);
299 if (!this)
300 return -ENOMEM;
302 for (i = 0; i < tpl_link; i++) {
303 ret = mmc_io_rw_direct(card, 0, 0,
304 ptr + i, 0, &this->data[i]);
305 if (ret)
306 break;
308 if (ret) {
309 kfree(this);
310 break;
313 /* Try to parse the CIS tuple */
314 ret = cis_tpl_parse(card, func, "CIS",
315 cis_tpl_list, ARRAY_SIZE(cis_tpl_list),
316 tpl_code, this->data, tpl_link);
317 if (ret == -EILSEQ || ret == -ENOENT) {
319 * The tuple is unknown or known but not parsed.
320 * Queue the tuple for the function driver.
322 this->next = NULL;
323 this->code = tpl_code;
324 this->size = tpl_link;
325 *prev = this;
326 prev = &this->next;
328 if (ret == -ENOENT) {
329 /* warn about unknown tuples */
330 pr_warn_ratelimited("%s: queuing unknown"
331 " CIS tuple 0x%02x (%u bytes)\n",
332 mmc_hostname(card->host),
333 tpl_code, tpl_link);
336 /* keep on analyzing tuples */
337 ret = 0;
338 } else {
340 * We don't need the tuple anymore if it was
341 * successfully parsed by the SDIO core or if it is
342 * not going to be queued for a driver.
344 kfree(this);
347 ptr += tpl_link;
348 } while (!ret);
351 * Link in all unknown tuples found in the common CIS so that
352 * drivers don't have to go digging in two places.
354 if (func)
355 *prev = card->tuples;
357 return ret;
360 int sdio_read_common_cis(struct mmc_card *card)
362 return sdio_read_cis(card, NULL);
365 void sdio_free_common_cis(struct mmc_card *card)
367 struct sdio_func_tuple *tuple, *victim;
369 tuple = card->tuples;
371 while (tuple) {
372 victim = tuple;
373 tuple = tuple->next;
374 kfree(victim);
377 card->tuples = NULL;
380 int sdio_read_func_cis(struct sdio_func *func)
382 int ret;
384 ret = sdio_read_cis(func->card, func);
385 if (ret)
386 return ret;
389 * Since we've linked to tuples in the card structure,
390 * we must make sure we have a reference to it.
392 get_device(&func->card->dev);
395 * Vendor/device id is optional for function CIS, so
396 * copy it from the card structure as needed.
398 if (func->vendor == 0) {
399 func->vendor = func->card->cis.vendor;
400 func->device = func->card->cis.device;
403 return 0;
406 void sdio_free_func_cis(struct sdio_func *func)
408 struct sdio_func_tuple *tuple, *victim;
410 tuple = func->tuples;
412 while (tuple && tuple != func->card->tuples) {
413 victim = tuple;
414 tuple = tuple->next;
415 kfree(victim);
418 func->tuples = NULL;
421 * We have now removed the link to the tuples in the
422 * card structure, so remove the reference.
424 put_device(&func->card->dev);