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