summaryrefslogtreecommitdiff
path: root/drivers/bluetooth/sd8797/bt/bt_init.c
blob: 2de8c123842900b80dc93bbbdca0b0ccb6020f44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
/** @file bt_init.c
  *
  * @brief This file contains the init functions for BlueTooth
  * driver.
  *
  * Copyright (C) 2011-2012, Marvell International Ltd.
  *
  * This software file (the "File") is distributed by Marvell International
  * Ltd. under the terms of the GNU General Public License Version 2, June 1991
  * (the "License").  You may use, redistribute and/or modify this File in
  * accordance with the terms and conditions of the License, a copy of which
  * is available along with the File in the gpl.txt file or by writing to
  * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  * 02111-1307 or on the worldwide web at http://www.gnu.org/licenses/gpl.txt.
  *
  * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
  * ARE EXPRESSLY DISCLAIMED.  The License provides additional details about
  * this warranty disclaimer.
  *
  */

#include <linux/string.h>
#include <linux/firmware.h>

#include "bt_drv.h"

#define isxdigit(c)	(('0' <= (c) && (c) <= '9') \
			 || ('a' <= (c) && (c) <= 'f') \
			 || ('A' <= (c) && (c) <= 'F'))

#define isdigit(c)	(('0' <= (c) && (c) <= '9'))
#define isspace(c)  (c <= ' ' && (c == ' ' || (c <= 13 && c >=9)))
/**
 *  @brief Returns hex value of a give character
 *
 *  @param chr	Character to be converted
 *
 *  @return	The converted character if chr is a valid hex, else 0
 */
static int
bt_hexval(char chr)
{
	ENTER();

	if (chr >= '0' && chr <= '9')
		return chr - '0';
	if (chr >= 'A' && chr <= 'F')
		return chr - 'A' + 10;
	if (chr >= 'a' && chr <= 'f')
		return chr - 'a' + 10;

	LEAVE();
	return 0;
}

/**
 *  @brief Extension of strsep lib command. This function will also take care
 *	   escape character
 *
 *  @param s         A pointer to array of chars to process
 *  @param delim     The delimiter character to end the string
 *  @param esc       The escape character to ignore for delimiter
 *
 *  @return          Pointer to the separated string if delim found, else NULL
 */
static char *
bt_strsep(char **s, char delim, char esc)
{
	char *se = *s, *sb;

	ENTER();

	if (!(*s) || (*se == '\0')) {
		LEAVE();
		return NULL;
	}

	for (sb = *s; *sb != '\0'; ++sb) {
		if (*sb == esc && *(sb + 1) == esc) {
			/*
			 * We get a esc + esc seq then keep the one esc
			 * and chop off the other esc character
			 */
			memmove(sb, sb + 1, strlen(sb));
			continue;
		}
		if (*sb == esc && *(sb + 1) == delim) {
			/*
			 * We get a delim + esc seq then keep the delim
			 * and chop off the esc character
			 */
			memmove(sb, sb + 1, strlen(sb));
			continue;
		}
		if (*sb == delim)
			break;
	}

	if (*sb == '\0')
		sb = NULL;
	else
		*sb++ = '\0';

	*s = sb;

	LEAVE();
	return se;
}

/**
 *  @brief Returns hex value of a given ascii string
 *
 *  @param a	String to be converted
 *
 *  @return	hex value
 */
static int
bt_atox(char *a)
{
	int i = 0;
	ENTER();
	while (isxdigit(*a))
		i = i * 16 + bt_hexval(*a++);

	LEAVE();
	return i;
}

/**
 *  @brief Converts mac address from string to t_u8 buffer.
 *
 *  @param mac_addr The buffer to store the mac address in.
 *  @param buf      The source of mac address which is a string.
 *
 *  @return	N/A
 */
static void
bt_mac2u8(u8 * mac_addr, char *buf)
{
	char *begin = buf, *end;
	int i;

	ENTER();

	for (i = 0; i < ETH_ALEN; ++i) {
		end = bt_strsep(&begin, ':', '/');
		if (end)
			mac_addr[i] = bt_atox(end);
	}

	LEAVE();
}

/**
 *  @brief Returns integer value of a given ascii string
 *
 *  @param data    Converted data to be returned
 *  @param a       String to be converted
 *
 *  @return        BT_STATUS_SUCCESS or BT_STATUS_FAILURE
 */
static int
bt_atoi(int *data, char *a)
{
	int i, val = 0, len;

	ENTER();

	len = strlen(a);
	if (!strncmp(a, "0x", 2)) {
		a = a + 2;
		len -= 2;
		*data = bt_atox(a);
		return BT_STATUS_SUCCESS;
	}
	for (i = 0; i < len; i++) {
		if (isdigit(a[i])) {
			val = val * 10 + (a[i] - '0');
		} else {
			PRINTM(ERROR, "Invalid char %c in string %s\n", a[i],
			       a);
			return BT_STATUS_FAILURE;
		}
	}
	*data = val;

	LEAVE();
	return BT_STATUS_SUCCESS;
}

/**
 *  @brief parse cal-data
 *
 *  @param src      a pointer to cal-data string
 *  @param len      len of cal-data
 *  @param dst      a pointer to return cal-data
 *  @param dst_size size of dest buffer
 *
 *  @return        BT_STATUS_SUCCESS or BT_STATUS_FAILURE
 */
static u32
bt_parse_cal_cfg(u8 * src, u32 len, u8 * dst, u32 dst_size)
{
	u8 *ptr;
	u8 *dptr;

	ENTER();
	ptr = src;
	dptr = dst;

	while (ptr - src < len) {
		if (*ptr && (isspace(*ptr) || *ptr == '\t')) {
			ptr++;
			continue;
		}

		if (isxdigit(*ptr)) {
			*dptr++ = bt_atox(ptr);
			ptr += 2;
		} else {
			ptr++;
		}
		if ((dptr - dst) > dst_size) {
			PRINTM(ERROR, "cal_file size too big!!!\n");
			break;
		}
	}
	LEAVE();
	return (dptr - dst);
}

/**
 *    @brief BT get one line data from ASCII format data
 *
 *    @param data         Source data
 *    @param size         Source data length
 *    @param line_pos     Destination data
 *    @return             -1 or length of the line
 */
int
parse_cfg_get_line(u8 * data, u32 size, u8 * line_pos)
{
	static s32 pos = 0;
	u8 *src, *dest;

	if (pos >= size) {	/* reach the end */
		pos = 0;	/* Reset position for rfkill */
		return -1;
	}
	memset(line_pos, 0, MAX_LINE_LEN);
	src = data + pos;
	dest = line_pos;

	while (*src != '\x0A' && *src != '\0') {
		if (*src != ' ' && *src != '\t')	/* parse space */
			*dest++ = *src++;
		else
			src++;
		pos++;
	}
	*dest = '\0';
	/* parse new line */
	pos++;
	return strlen(line_pos);
}

/**
 *    @brief BT parse ASCII format data to MAC address
 *
 *    @param priv          BT private handle
 *    @param data          Source data
 *    @param size          data length
 *    @return              BT_STATUS_SUCCESS or BT_STATUS_FAILURE
 */
int
bt_process_init_cfg(bt_private * priv, u8 * data, u32 size)
{
	u8 *pos;
	u8 *intf_s, *intf_e;
	u8 s[MAX_LINE_LEN];	/* 1 line data */
	u32 line_len;
	char dev_name[MAX_PARAM_LEN];
	u8 buf[MAX_PARAM_LEN];
	u8 bt_addr[MAX_MAC_ADDR_LEN];
	u8 bt_mac[ETH_ALEN];
	int setting = 0;
	u8 type = 0;
	u16 value = 0;
	u32 offset = 0;
	int ret = BT_STATUS_FAILURE;

	memset(dev_name, 0, sizeof(dev_name));
	memset(bt_addr, 0, sizeof(bt_addr));
	memset(bt_mac, 0, sizeof(bt_mac));

	while ((line_len = parse_cfg_get_line(data, size, s)) != -1) {
		pos = s;
		while (*pos == ' ' || *pos == '\t')
			pos++;

		if (*pos == '#' || (*pos == '\r' && *(pos + 1) == '\n') ||
		    *pos == '\n' || *pos == '\0')
			continue;	/* Need n't process this line */

		/* Process MAC addr */
		if (strncmp(pos, "mac_addr", 8) == 0) {
			intf_s = strchr(pos, '=');
			if (intf_s != NULL)
				intf_e = strchr(intf_s, ':');
			else
				intf_e = NULL;
			if (intf_s != NULL && intf_e != NULL) {
				if ((intf_e - intf_s) > MAX_PARAM_LEN) {
					PRINTM(ERROR,
					       "BT: Too long interface name %d\n",
					       __LINE__);
					goto done;
				}
				strncpy(dev_name, intf_s + 1,
					intf_e - intf_s - 1);
				dev_name[intf_e - intf_s - 1] = '\0';
				if (strcmp
				    (dev_name,
				     priv->bt_dev.m_dev[BT_SEQ].name) == 0) {
					/* found hci device */
					strncpy(bt_addr, intf_e + 1,
						MAX_MAC_ADDR_LEN - 1);
					bt_addr[MAX_MAC_ADDR_LEN - 1] = '\0';
					/* Convert MAC format */
					bt_mac2u8(bt_mac, bt_addr);
					PRINTM(CMD,
					       "HCI: %s new BT Address %02x:%02x:%02x:%02x:%02x:%02x\n",
					       dev_name, bt_mac[0], bt_mac[1],
					       bt_mac[2], bt_mac[3], bt_mac[4],
					       bt_mac[5]);
					if (BT_STATUS_SUCCESS !=
					    bt_set_mac_address(priv, bt_mac)) {
						PRINTM(FATAL,
						       "BT: Fail to set mac address\n");
						goto done;
					}
				}
			} else {
				PRINTM(ERROR,
				       "BT: Wrong config file format %d\n",
				       __LINE__);
				goto done;
			}
		}
		/* Process REG value */
		else if (strncmp(pos, "bt_reg", 6) == 0) {
			intf_s = strchr(pos, '=');
			if (intf_s != NULL)
				intf_e = strchr(intf_s, ',');
			else
				intf_e = NULL;
			if (intf_s != NULL && intf_e != NULL) {
				/* Copy type */
				memset(buf, 0, sizeof(buf));
				strncpy(buf, intf_s + 1, 1);
				buf[1] = '\0';
				if (0 == bt_atoi(&setting, buf))
					type = (u8) setting;
				else {
					PRINTM(ERROR,
					       "BT: Fail to parse reg type\n");
					goto done;
				}
			} else {
				PRINTM(ERROR,
				       "BT: Wrong config file format %d\n",
				       __LINE__);
				goto done;
			}
			intf_s = intf_e + 1;
			if (intf_s != NULL)
				intf_e = strchr(intf_s, ',');
			else
				intf_e = NULL;
			if (intf_s != NULL && intf_e != NULL) {
				if ((intf_e - intf_s) >= MAX_PARAM_LEN) {
					PRINTM(ERROR,
					       "BT: Regsier offset is too long %d\n",
					       __LINE__);
					goto done;
				}
				/* Copy offset */
				memset(buf, 0, sizeof(buf));
				strncpy(buf, intf_s, intf_e - intf_s);
				buf[intf_e - intf_s] = '\0';
				if (0 == bt_atoi(&setting, buf))
					offset = (u32) setting;
				else {
					PRINTM(ERROR,
					       "BT: Fail to parse reg offset\n");
					goto done;
				}
			} else {
				PRINTM(ERROR,
				       "BT: Wrong config file format %d\n",
				       __LINE__);
				goto done;
			}
			intf_s = intf_e + 1;
			if (intf_s != NULL) {
				if ((strlen(intf_s) >= MAX_PARAM_LEN)) {
					PRINTM(ERROR,
					       "BT: Regsier value is too long %d\n",
					       __LINE__);
					goto done;
				}
				/* Copy value */
				memset(buf, 0, sizeof(buf));
				strncpy(buf, intf_s, sizeof(buf));
				if (0 == bt_atoi(&setting, buf))
					value = (u16) setting;
				else {
					PRINTM(ERROR,
					       "BT: Fail to parse reg value\n");
					goto done;
				}
			} else {
				PRINTM(ERROR,
				       "BT: Wrong config file format %d\n",
				       __LINE__);
				goto done;
			}

			PRINTM(CMD,
			       "BT: Write reg type: %d offset: 0x%x value: 0x%x\n",
			       type, offset, value);
			if (BT_STATUS_SUCCESS !=
			    bt_write_reg(priv, type, offset, value)) {
				PRINTM(FATAL,
				       "BT: Write reg failed. type: %d offset: 0x%x value: 0x%x\n",
				       type, offset, value);
				goto done;
			}
		}
	}
	ret = BT_STATUS_SUCCESS;

done:
	LEAVE();
	return ret;
}

/**
 *    @brief BT set user defined init data and param
 *
 *    @param priv     BT private handle
 *    @param cfg_file user cofig file
 *    @return         BT_STATUS_SUCCESS or BT_STATUS_FAILURE
 */
int
bt_init_config(bt_private * priv, char *cfg_file)
{
	const struct firmware *cfg = NULL;
	int ret = BT_STATUS_SUCCESS;

	ENTER();
	if ((request_firmware(&cfg, cfg_file, priv->hotplug_device)) < 0) {
		PRINTM(FATAL, "BT: request_firmware() %s failed\n", cfg_file);
		ret = BT_STATUS_FAILURE;
		goto done;
	}
	if (cfg)
		ret = bt_process_init_cfg(priv, (u8 *) cfg->data, cfg->size);
	else
		ret = BT_STATUS_FAILURE;
done:
	if (cfg)
		release_firmware(cfg);
	LEAVE();
	return ret;
}

/**
 *    @brief BT process calibration data
 *
 *    @param priv    a pointer to bt_private structure
 *    @param data    a pointer to cal data
 *    @param size    cal data size
 *    @param mac     mac address buf
 *    @return         BT_STATUS_SUCCESS or BT_STATUS_FAILURE
 */
int
bt_process_cal_cfg(bt_private * priv, u8 * data, u32 size, char *mac)
{
	u8 bt_mac[ETH_ALEN];
	u8 cal_data[32];
	int ret = BT_STATUS_FAILURE;

	memset(bt_mac, 0, sizeof(bt_mac));
	bt_parse_cal_cfg(data, size, cal_data, sizeof(cal_data));
	if (mac != NULL) {
		/* Convert MAC format */
		bt_mac2u8(bt_mac, mac);
		PRINTM(CMD,
		       "HCI: new BT Address %02x:%02x:%02x:%02x:%02x:%02x\n",
		       bt_mac[0], bt_mac[1], bt_mac[2], bt_mac[3], bt_mac[4],
		       bt_mac[5]);
		if (BT_STATUS_SUCCESS !=
		    bt_load_cal_data(priv, cal_data, bt_mac)) {
			PRINTM(FATAL, "BT: Fail to load calibrate data\n");
			goto done;
		}
	} else {
		if (BT_STATUS_SUCCESS != bt_load_cal_data(priv, cal_data, NULL)) {
			PRINTM(FATAL, "BT: Fail to load calibrate data\n");
			goto done;
		}
	}

	ret = BT_STATUS_SUCCESS;

done:
	LEAVE();
	return ret;
}

/**
 *    @brief BT process calibration file
 *
 *    @param priv    a pointer to bt_private structure
 *    @param cal_file calibration file name
 *    @param mac     mac address buf
 *    @return         BT_STATUS_SUCCESS or BT_STATUS_FAILURE
 */
int
bt_cal_config(bt_private * priv, char *cal_file, char *mac)
{
	const struct firmware *cfg = NULL;
	int ret = BT_STATUS_SUCCESS;

	ENTER();
	if ((request_firmware(&cfg, cal_file, priv->hotplug_device)) < 0) {
		PRINTM(FATAL, "BT: request_firmware() %s failed\n", cal_file);
		ret = BT_STATUS_FAILURE;
		goto done;
	}
	if (cfg)
		ret = bt_process_cal_cfg(priv, (u8 *) cfg->data, cfg->size,
					 mac);
	else
		ret = BT_STATUS_FAILURE;
done:
	if (cfg)
		release_firmware(cfg);
	LEAVE();
	return ret;
}