• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * linux/drivers/mmc/core/sdio_cis.c
3  *
4  * Author:	Nicolas Pitre
5  * Created:	June 11, 2007
6  * Copyright:	MontaVista Software Inc.
7  *
8  * Copyright 2007 Pierre Ossman
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or (at
13  * your option) any later version.
14  */
15 
16 #include <linux/kernel.h>
17 #include <linux/slab.h>
18 
19 #include <linux/mmc/host.h>
20 #include <linux/mmc/card.h>
21 #include <linux/mmc/sdio.h>
22 #include <linux/mmc/sdio_func.h>
23 
24 #include "sdio_cis.h"
25 #include "sdio_ops.h"
26 
27 #define SDIO_READ_CIS_TIMEOUT_MS  (10 * 1000) /* 10s */
28 
cistpl_vers_1(struct mmc_card * card,struct sdio_func * func,const unsigned char * buf,unsigned size)29 static int cistpl_vers_1(struct mmc_card *card, struct sdio_func *func,
30 			 const unsigned char *buf, unsigned size)
31 {
32 	unsigned i, nr_strings;
33 	char **buffer, *string;
34 
35 	if (size < 2)
36 		return 0;
37 
38 	/* Find all null-terminated (including zero length) strings in
39 	   the TPLLV1_INFO field. Trailing garbage is ignored. */
40 	buf += 2;
41 	size -= 2;
42 
43 	nr_strings = 0;
44 	for (i = 0; i < size; i++) {
45 		if (buf[i] == 0xff)
46 			break;
47 		if (buf[i] == 0)
48 			nr_strings++;
49 	}
50 	if (nr_strings == 0)
51 		return 0;
52 
53 	size = i;
54 
55 	buffer = kzalloc(sizeof(char*) * nr_strings + size, GFP_KERNEL);
56 	if (!buffer)
57 		return -ENOMEM;
58 
59 	string = (char*)(buffer + nr_strings);
60 
61 	for (i = 0; i < nr_strings; i++) {
62 		buffer[i] = string;
63 		strcpy(string, buf);
64 		string += strlen(string) + 1;
65 		buf += strlen(buf) + 1;
66 	}
67 
68 	if (func) {
69 		func->num_info = nr_strings;
70 		func->info = (const char**)buffer;
71 	} else {
72 		card->num_info = nr_strings;
73 		card->info = (const char**)buffer;
74 	}
75 
76 	return 0;
77 }
78 
cistpl_manfid(struct mmc_card * card,struct sdio_func * func,const unsigned char * buf,unsigned size)79 static int cistpl_manfid(struct mmc_card *card, struct sdio_func *func,
80 			 const unsigned char *buf, unsigned size)
81 {
82 	unsigned int vendor, device;
83 
84 	/* TPLMID_MANF */
85 	vendor = buf[0] | (buf[1] << 8);
86 
87 	/* TPLMID_CARD */
88 	device = buf[2] | (buf[3] << 8);
89 
90 	if (func) {
91 		func->vendor = vendor;
92 		func->device = device;
93 	} else {
94 		card->cis.vendor = vendor;
95 		card->cis.device = device;
96 	}
97 
98 	return 0;
99 }
100 
101 static const unsigned char speed_val[16] =
102 	{ 0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80 };
103 static const unsigned int speed_unit[8] =
104 	{ 10000, 100000, 1000000, 10000000, 0, 0, 0, 0 };
105 
106 
107 typedef int (tpl_parse_t)(struct mmc_card *, struct sdio_func *,
108 			   const unsigned char *, unsigned);
109 
110 struct cis_tpl {
111 	unsigned char code;
112 	unsigned char min_size;
113 	tpl_parse_t *parse;
114 };
115 
cis_tpl_parse(struct mmc_card * card,struct sdio_func * func,const char * tpl_descr,const struct cis_tpl * tpl,int tpl_count,unsigned char code,const unsigned char * buf,unsigned size)116 static int cis_tpl_parse(struct mmc_card *card, struct sdio_func *func,
117 			 const char *tpl_descr,
118 			 const struct cis_tpl *tpl, int tpl_count,
119 			 unsigned char code,
120 			 const unsigned char *buf, unsigned size)
121 {
122 	int i, ret;
123 
124 	/* look for a matching code in the table */
125 	for (i = 0; i < tpl_count; i++, tpl++) {
126 		if (tpl->code == code)
127 			break;
128 	}
129 	if (i < tpl_count) {
130 		if (size >= tpl->min_size) {
131 			if (tpl->parse)
132 				ret = tpl->parse(card, func, buf, size);
133 			else
134 				ret = -EILSEQ;	/* known tuple, not parsed */
135 		} else {
136 			/* invalid tuple */
137 			ret = -EINVAL;
138 		}
139 		if (ret && ret != -EILSEQ && ret != -ENOENT) {
140 			pr_err("%s: bad %s tuple 0x%02x (%u bytes)\n",
141 			       mmc_hostname(card->host), tpl_descr, code, size);
142 		}
143 	} else {
144 		/* unknown tuple */
145 		ret = -ENOENT;
146 	}
147 
148 	return ret;
149 }
150 
cistpl_funce_common(struct mmc_card * card,struct sdio_func * func,const unsigned char * buf,unsigned size)151 static int cistpl_funce_common(struct mmc_card *card, struct sdio_func *func,
152 			       const unsigned char *buf, unsigned size)
153 {
154 	/* Only valid for the common CIS (function 0) */
155 	if (func)
156 		return -EINVAL;
157 
158 	/* TPLFE_FN0_BLK_SIZE */
159 	card->cis.blksize = buf[1] | (buf[2] << 8);
160 
161 	/* TPLFE_MAX_TRAN_SPEED */
162 	card->cis.max_dtr = speed_val[(buf[3] >> 3) & 15] *
163 			    speed_unit[buf[3] & 7];
164 
165 	return 0;
166 }
167 
cistpl_funce_func(struct mmc_card * card,struct sdio_func * func,const unsigned char * buf,unsigned size)168 static int cistpl_funce_func(struct mmc_card *card, struct sdio_func *func,
169 			     const unsigned char *buf, unsigned size)
170 {
171 	unsigned vsn;
172 	unsigned min_size;
173 
174 	/* Only valid for the individual function's CIS (1-7) */
175 	if (!func)
176 		return -EINVAL;
177 
178 	/*
179 	 * This tuple has a different length depending on the SDIO spec
180 	 * version.
181 	 */
182 	vsn = func->card->cccr.sdio_vsn;
183 	min_size = (vsn == SDIO_SDIO_REV_1_00) ? 28 : 42;
184 
185 	if (size < min_size)
186 		return -EINVAL;
187 
188 	/* TPLFE_MAX_BLK_SIZE */
189 	func->max_blksize = buf[12] | (buf[13] << 8);
190 
191 	/* TPLFE_ENABLE_TIMEOUT_VAL, present in ver 1.1 and above */
192 	if (vsn > SDIO_SDIO_REV_1_00)
193 		func->enable_timeout = (buf[28] | (buf[29] << 8)) * 10;
194 	else
195 		func->enable_timeout = jiffies_to_msecs(HZ);
196 
197 	return 0;
198 }
199 
200 /*
201  * Known TPLFE_TYPEs table for CISTPL_FUNCE tuples.
202  *
203  * Note that, unlike PCMCIA, CISTPL_FUNCE tuples are not parsed depending
204  * on the TPLFID_FUNCTION value of the previous CISTPL_FUNCID as on SDIO
205  * TPLFID_FUNCTION is always hardcoded to 0x0C.
206  */
207 static const struct cis_tpl cis_tpl_funce_list[] = {
208 	{	0x00,	4,	cistpl_funce_common		},
209 	{	0x01,	0,	cistpl_funce_func		},
210 	{	0x04,	1+1+6,	/* CISTPL_FUNCE_LAN_NODE_ID */	},
211 };
212 
cistpl_funce(struct mmc_card * card,struct sdio_func * func,const unsigned char * buf,unsigned size)213 static int cistpl_funce(struct mmc_card *card, struct sdio_func *func,
214 			const unsigned char *buf, unsigned size)
215 {
216 	if (size < 1)
217 		return -EINVAL;
218 
219 	return cis_tpl_parse(card, func, "CISTPL_FUNCE",
220 			     cis_tpl_funce_list,
221 			     ARRAY_SIZE(cis_tpl_funce_list),
222 			     buf[0], buf, size);
223 }
224 
225 /* Known TPL_CODEs table for CIS tuples */
226 static const struct cis_tpl cis_tpl_list[] = {
227 	{	0x15,	3,	cistpl_vers_1		},
228 	{	0x20,	4,	cistpl_manfid		},
229 	{	0x21,	2,	/* cistpl_funcid */	},
230 	{	0x22,	0,	cistpl_funce		},
231 };
232 
sdio_read_cis(struct mmc_card * card,struct sdio_func * func)233 static int sdio_read_cis(struct mmc_card *card, struct sdio_func *func)
234 {
235 	int ret;
236 	struct sdio_func_tuple *this, **prev;
237 	unsigned i, ptr = 0;
238 
239 	/*
240 	 * Note that this works for the common CIS (function number 0) as
241 	 * well as a function's CIS * since SDIO_CCCR_CIS and SDIO_FBR_CIS
242 	 * have the same offset.
243 	 */
244 	for (i = 0; i < 3; i++) {
245 		unsigned char x, fn;
246 
247 		if (func)
248 			fn = func->num;
249 		else
250 			fn = 0;
251 
252 		ret = mmc_io_rw_direct(card, 0, 0,
253 			SDIO_FBR_BASE(fn) + SDIO_FBR_CIS + i, 0, &x);
254 		if (ret)
255 			return ret;
256 		ptr |= x << (i * 8);
257 	}
258 
259 	if (func)
260 		prev = &func->tuples;
261 	else
262 		prev = &card->tuples;
263 
264 	BUG_ON(*prev);
265 
266 	do {
267 		unsigned char tpl_code, tpl_link;
268 		unsigned long timeout = jiffies +
269 			msecs_to_jiffies(SDIO_READ_CIS_TIMEOUT_MS);
270 
271 		ret = mmc_io_rw_direct(card, 0, 0, ptr++, 0, &tpl_code);
272 		if (ret)
273 			break;
274 
275 		/* 0xff means we're done */
276 		if (tpl_code == 0xff)
277 			break;
278 
279 		/* null entries have no link field or data */
280 		if (tpl_code == 0x00)
281 			continue;
282 
283 		ret = mmc_io_rw_direct(card, 0, 0, ptr++, 0, &tpl_link);
284 		if (ret)
285 			break;
286 
287 		/* a size of 0xff also means we're done */
288 		if (tpl_link == 0xff)
289 			break;
290 
291 		this = kmalloc(sizeof(*this) + tpl_link, GFP_KERNEL);
292 		if (!this)
293 			return -ENOMEM;
294 
295 		for (i = 0; i < tpl_link; i++) {
296 			ret = mmc_io_rw_direct(card, 0, 0,
297 					       ptr + i, 0, &this->data[i]);
298 			if (ret)
299 				break;
300 		}
301 		if (ret) {
302 			kfree(this);
303 			break;
304 		}
305 
306 		/* Try to parse the CIS tuple */
307 		ret = cis_tpl_parse(card, func, "CIS",
308 				    cis_tpl_list, ARRAY_SIZE(cis_tpl_list),
309 				    tpl_code, this->data, tpl_link);
310 		if (ret == -EILSEQ || ret == -ENOENT) {
311 			/*
312 			 * The tuple is unknown or known but not parsed.
313 			 * Queue the tuple for the function driver.
314 			 */
315 			this->next = NULL;
316 			this->code = tpl_code;
317 			this->size = tpl_link;
318 			*prev = this;
319 			prev = &this->next;
320 
321 			if (ret == -ENOENT) {
322 				if (time_after(jiffies, timeout))
323 					break;
324 				/* warn about unknown tuples */
325 				pr_warn_ratelimited("%s: queuing unknown"
326 				       " CIS tuple 0x%02x (%u bytes)\n",
327 				       mmc_hostname(card->host),
328 				       tpl_code, tpl_link);
329 			}
330 
331 			/* keep on analyzing tuples */
332 			ret = 0;
333 		} else {
334 			/*
335 			 * We don't need the tuple anymore if it was
336 			 * successfully parsed by the SDIO core or if it is
337 			 * not going to be queued for a driver.
338 			 */
339 			kfree(this);
340 		}
341 
342 		ptr += tpl_link;
343 	} while (!ret);
344 
345 	/*
346 	 * Link in all unknown tuples found in the common CIS so that
347 	 * drivers don't have to go digging in two places.
348 	 */
349 	if (func)
350 		*prev = card->tuples;
351 
352 	return ret;
353 }
354 
sdio_read_common_cis(struct mmc_card * card)355 int sdio_read_common_cis(struct mmc_card *card)
356 {
357 	return sdio_read_cis(card, NULL);
358 }
359 
sdio_free_common_cis(struct mmc_card * card)360 void sdio_free_common_cis(struct mmc_card *card)
361 {
362 	struct sdio_func_tuple *tuple, *victim;
363 
364 	tuple = card->tuples;
365 
366 	while (tuple) {
367 		victim = tuple;
368 		tuple = tuple->next;
369 		kfree(victim);
370 	}
371 
372 	card->tuples = NULL;
373 }
374 
sdio_read_func_cis(struct sdio_func * func)375 int sdio_read_func_cis(struct sdio_func *func)
376 {
377 	int ret;
378 
379 	ret = sdio_read_cis(func->card, func);
380 	if (ret)
381 		return ret;
382 
383 	/*
384 	 * Since we've linked to tuples in the card structure,
385 	 * we must make sure we have a reference to it.
386 	 */
387 	get_device(&func->card->dev);
388 
389 	/*
390 	 * Vendor/device id is optional for function CIS, so
391 	 * copy it from the card structure as needed.
392 	 */
393 	if (func->vendor == 0) {
394 		func->vendor = func->card->cis.vendor;
395 		func->device = func->card->cis.device;
396 	}
397 
398 	return 0;
399 }
400 
sdio_free_func_cis(struct sdio_func * func)401 void sdio_free_func_cis(struct sdio_func *func)
402 {
403 	struct sdio_func_tuple *tuple, *victim;
404 
405 	tuple = func->tuples;
406 
407 	while (tuple && tuple != func->card->tuples) {
408 		victim = tuple;
409 		tuple = tuple->next;
410 		kfree(victim);
411 	}
412 
413 	func->tuples = NULL;
414 
415 	/*
416 	 * We have now removed the link to the tuples in the
417 	 * card structure, so remove the reference.
418 	 */
419 	put_device(&func->card->dev);
420 }
421 
422