summaryrefslogtreecommitdiff
path: root/drivers/usb/host/dwc_common_port/dwc_cc.c
blob: 5ec2ae28698c15d1c441d3104706b04e9f1dde27 (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
/* =========================================================================
 * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_cc.c $
 * $Revision: #4 $
 * $Date: 2010/11/04 $
 * $Change: 1621692 $
 *
 * Synopsys Portability Library Software and documentation
 * (hereinafter, "Software") is an Unsupported proprietary work of
 * Synopsys, Inc. unless otherwise expressly agreed to in writing
 * between Synopsys and you.
 *
 * The Software IS NOT an item of Licensed Software or Licensed Product
 * under any End User Software License Agreement or Agreement for
 * Licensed Product with Synopsys or any supplement thereto. You are
 * permitted to use and redistribute this Software in source and binary
 * forms, with or without modification, provided that redistributions
 * of source code must retain this notice. You may not view, use,
 * disclose, copy or distribute this file or any information contained
 * herein except pursuant to this license grant from Synopsys. If you
 * do not agree with this notice, including the disclaimer below, then
 * you are not authorized to use the Software.
 *
 * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
 * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
 * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 * ========================================================================= */
#ifdef DWC_CCLIB

#include "dwc_cc.h"

typedef struct dwc_cc
{
	uint32_t uid;
	uint8_t chid[16];
	uint8_t cdid[16];
	uint8_t ck[16];
	uint8_t *name;
	uint8_t length;
        DWC_CIRCLEQ_ENTRY(dwc_cc) list_entry;
} dwc_cc_t;

DWC_CIRCLEQ_HEAD(context_list, dwc_cc);

/** The main structure for CC management.  */
struct dwc_cc_if
{
	dwc_mutex_t *mutex;
	char *filename;

	unsigned is_host:1;

	dwc_notifier_t *notifier;

	struct context_list list;
};

#ifdef DEBUG
static inline void dump_bytes(char *name, uint8_t *bytes, int len)
{
	int i;
	DWC_PRINTF("%s: ", name);
	for (i=0; i<len; i++) {
		DWC_PRINTF("%02x ", bytes[i]);
	}
	DWC_PRINTF("\n");
}
#else
#define dump_bytes(x...)
#endif

static dwc_cc_t *alloc_cc(void *mem_ctx, uint8_t *name, uint32_t length)
{
	dwc_cc_t *cc = dwc_alloc(mem_ctx, sizeof(dwc_cc_t));
	if (!cc) {
		return NULL;
	}
	DWC_MEMSET(cc, 0, sizeof(dwc_cc_t));

	if (name) {
		cc->length = length;
		cc->name = dwc_alloc(mem_ctx, length);
		if (!cc->name) {
			dwc_free(mem_ctx, cc);
			return NULL;
		}

		DWC_MEMCPY(cc->name, name, length);
	}

	return cc;
}

static void free_cc(void *mem_ctx, dwc_cc_t *cc)
{
	if (cc->name) {
		dwc_free(mem_ctx, cc->name);
	}
	dwc_free(mem_ctx, cc);
}

static uint32_t next_uid(dwc_cc_if_t *cc_if)
{
	uint32_t uid = 0;
	dwc_cc_t *cc;
	DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
		if (cc->uid > uid) {
			uid = cc->uid;
		}
	}

	if (uid == 0) {
		uid = 255;
	}

	return uid + 1;
}

static dwc_cc_t *cc_find(dwc_cc_if_t *cc_if, uint32_t uid)
{
	dwc_cc_t *cc;
	DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
		if (cc->uid == uid) {
			return cc;
		}
	}
	return NULL;
}

static unsigned int cc_data_size(dwc_cc_if_t *cc_if)
{
	unsigned int size = 0;
	dwc_cc_t *cc;
	DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
		size += (48 + 1);
		if (cc->name) {
			size += cc->length;
		}
	}
	return size;
}

static uint32_t cc_match_chid(dwc_cc_if_t *cc_if, uint8_t *chid)
{
	uint32_t uid = 0;
	dwc_cc_t *cc;

	DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
		if (DWC_MEMCMP(cc->chid, chid, 16) == 0) {
			uid = cc->uid;
			break;
		}
	}
	return uid;
}
static uint32_t cc_match_cdid(dwc_cc_if_t *cc_if, uint8_t *cdid)
{
	uint32_t uid = 0;
	dwc_cc_t *cc;

	DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
		if (DWC_MEMCMP(cc->cdid, cdid, 16) == 0) {
			uid = cc->uid;
			break;
		}
	}
	return uid;
}

/* Internal cc_add */
static int32_t cc_add(void *mem_ctx, dwc_cc_if_t *cc_if, uint8_t *chid,
		      uint8_t *cdid, uint8_t *ck, uint8_t *name, uint8_t length)
{
	dwc_cc_t *cc;
	uint32_t uid;

	if (cc_if->is_host) {
		uid = cc_match_cdid(cc_if, cdid);
	}
	else {
		uid = cc_match_chid(cc_if, chid);
	}

	if (uid) {
		DWC_DEBUGC("Replacing previous connection context id=%d name=%p name_len=%d", uid, name, length);
		cc = cc_find(cc_if, uid);
	}
	else {
		cc = alloc_cc(mem_ctx, name, length);
		cc->uid = next_uid(cc_if);
		DWC_CIRCLEQ_INSERT_TAIL(&cc_if->list, cc, list_entry);
	}

	DWC_MEMCPY(&(cc->chid[0]), chid, 16);
	DWC_MEMCPY(&(cc->cdid[0]), cdid, 16);
	DWC_MEMCPY(&(cc->ck[0]), ck, 16);

	DWC_DEBUGC("Added connection context id=%d name=%p name_len=%d", cc->uid, name, length);
	dump_bytes("CHID", cc->chid, 16);
	dump_bytes("CDID", cc->cdid, 16);
	dump_bytes("CK", cc->ck, 16);
	return cc->uid;
}

/* Internal cc_clear */
static void cc_clear(void *mem_ctx, dwc_cc_if_t *cc_if)
{
	while (!DWC_CIRCLEQ_EMPTY(&cc_if->list)) {
		dwc_cc_t *cc = DWC_CIRCLEQ_FIRST(&cc_if->list);
		DWC_CIRCLEQ_REMOVE_INIT(&cc_if->list, cc, list_entry);
		free_cc(mem_ctx, cc);
	}
}

dwc_cc_if_t *dwc_cc_if_alloc(void *mem_ctx, void *mtx_ctx,
			     dwc_notifier_t *notifier, unsigned is_host)
{
	dwc_cc_if_t *cc_if = NULL;

	/* Allocate a common_cc_if structure */
	cc_if = dwc_alloc(mem_ctx, sizeof(dwc_cc_if_t));

	if (!cc_if)
		return NULL;

#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
	DWC_MUTEX_ALLOC_LINUX_DEBUG(cc_if->mutex);
#else
	cc_if->mutex = dwc_mutex_alloc(mtx_ctx);
#endif
	if (!cc_if->mutex) {
		dwc_free(mem_ctx, cc_if);
		return NULL;
	}

	DWC_CIRCLEQ_INIT(&cc_if->list);
	cc_if->is_host = is_host;
	cc_if->notifier = notifier;
	return cc_if;
}

void dwc_cc_if_free(void *mem_ctx, void *mtx_ctx, dwc_cc_if_t *cc_if)
{
#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
	DWC_MUTEX_FREE(cc_if->mutex);
#else
	dwc_mutex_free(mtx_ctx, cc_if->mutex);
#endif
	cc_clear(mem_ctx, cc_if);
	dwc_free(mem_ctx, cc_if);
}

static void cc_changed(dwc_cc_if_t *cc_if)
{
	if (cc_if->notifier) {
		dwc_notify(cc_if->notifier, DWC_CC_LIST_CHANGED_NOTIFICATION, cc_if);
	}
}

void dwc_cc_clear(void *mem_ctx, dwc_cc_if_t *cc_if)
{
	DWC_MUTEX_LOCK(cc_if->mutex);
	cc_clear(mem_ctx, cc_if);
	DWC_MUTEX_UNLOCK(cc_if->mutex);
	cc_changed(cc_if);
}

int32_t dwc_cc_add(void *mem_ctx, dwc_cc_if_t *cc_if, uint8_t *chid,
		   uint8_t *cdid, uint8_t *ck, uint8_t *name, uint8_t length)
{
	uint32_t uid;

	DWC_MUTEX_LOCK(cc_if->mutex);
	uid = cc_add(mem_ctx, cc_if, chid, cdid, ck, name, length);
	DWC_MUTEX_UNLOCK(cc_if->mutex);
	cc_changed(cc_if);

	return uid;
}

void dwc_cc_change(void *mem_ctx, dwc_cc_if_t *cc_if, int32_t id, uint8_t *chid,
		   uint8_t *cdid, uint8_t *ck, uint8_t *name, uint8_t length)
{
	dwc_cc_t* cc;

	DWC_DEBUGC("Change connection context %d", id);

	DWC_MUTEX_LOCK(cc_if->mutex);
	cc = cc_find(cc_if, id);
	if (!cc) {
		DWC_ERROR("Uid %d not found in cc list\n", id);
		DWC_MUTEX_UNLOCK(cc_if->mutex);
		return;
	}

	if (chid) {
		DWC_MEMCPY(&(cc->chid[0]), chid, 16);
	}
	if (cdid) {
		DWC_MEMCPY(&(cc->cdid[0]), cdid, 16);
	}
	if (ck) {
		DWC_MEMCPY(&(cc->ck[0]), ck, 16);
	}

	if (name) {
		if (cc->name) {
			dwc_free(mem_ctx, cc->name);
		}
		cc->name = dwc_alloc(mem_ctx, length);
		if (!cc->name) {
			DWC_ERROR("Out of memory in dwc_cc_change()\n");
			DWC_MUTEX_UNLOCK(cc_if->mutex);
			return;
		}
		cc->length = length;
		DWC_MEMCPY(cc->name, name, length);
	}

	DWC_MUTEX_UNLOCK(cc_if->mutex);

	cc_changed(cc_if);

	DWC_DEBUGC("Changed connection context id=%d\n", id);
	dump_bytes("New CHID", cc->chid, 16);
	dump_bytes("New CDID", cc->cdid, 16);
	dump_bytes("New CK", cc->ck, 16);
}

void dwc_cc_remove(void *mem_ctx, dwc_cc_if_t *cc_if, int32_t id)
{
	dwc_cc_t *cc;

	DWC_DEBUGC("Removing connection context %d", id);

	DWC_MUTEX_LOCK(cc_if->mutex);
	cc = cc_find(cc_if, id);
	if (!cc) {
		DWC_ERROR("Uid %d not found in cc list\n", id);
		DWC_MUTEX_UNLOCK(cc_if->mutex);
		return;
	}

	DWC_CIRCLEQ_REMOVE_INIT(&cc_if->list, cc, list_entry);
	DWC_MUTEX_UNLOCK(cc_if->mutex);
	free_cc(mem_ctx, cc);

	cc_changed(cc_if);
}

uint8_t *dwc_cc_data_for_save(void *mem_ctx, dwc_cc_if_t *cc_if, unsigned int *length)
{
	uint8_t *buf, *x;
	uint8_t zero = 0;
	dwc_cc_t *cc;

	DWC_MUTEX_LOCK(cc_if->mutex);
	*length = cc_data_size(cc_if);
	if (!(*length)) {
		DWC_MUTEX_UNLOCK(cc_if->mutex);
		return NULL;
	}

	DWC_DEBUGC("Creating data for saving (length=%d)", *length);

	buf = dwc_alloc(mem_ctx, *length);
	if (!buf) {
		*length = 0;
		DWC_MUTEX_UNLOCK(cc_if->mutex);
		return NULL;
	}

	x = buf;
	DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
		DWC_MEMCPY(x, cc->chid, 16);
		x += 16;
		DWC_MEMCPY(x, cc->cdid, 16);
		x += 16;
		DWC_MEMCPY(x, cc->ck, 16);
		x += 16;
		if (cc->name) {
			DWC_MEMCPY(x, &cc->length, 1);
			x += 1;
			DWC_MEMCPY(x, cc->name, cc->length);
			x += cc->length;
		}
		else {
			DWC_MEMCPY(x, &zero, 1);
			x += 1;
		}
	}
	DWC_MUTEX_UNLOCK(cc_if->mutex);

	return buf;
}

void dwc_cc_restore_from_data(void *mem_ctx, dwc_cc_if_t *cc_if, uint8_t *data, uint32_t length)
{
	uint8_t name_length;
	uint8_t *name;
	uint8_t *chid;
	uint8_t *cdid;
	uint8_t *ck;
	uint32_t i = 0;

	DWC_MUTEX_LOCK(cc_if->mutex);
	cc_clear(mem_ctx, cc_if);

	while (i < length) {
		chid = &data[i];
		i += 16;
		cdid = &data[i];
		i += 16;
		ck = &data[i];
		i += 16;

		name_length = data[i];
		i ++;

		if (name_length) {
			name = &data[i];
			i += name_length;
		}
		else {
			name = NULL;
		}

		/* check to see if we haven't overflown the buffer */
		if (i > length) {
			DWC_ERROR("Data format error while attempting to load CCs "
				  "(nlen=%d, iter=%d, buflen=%d).\n", name_length, i, length);
			break;
		}

		cc_add(mem_ctx, cc_if, chid, cdid, ck, name, name_length);
	}
	DWC_MUTEX_UNLOCK(cc_if->mutex);

	cc_changed(cc_if);
}

uint32_t dwc_cc_match_chid(dwc_cc_if_t *cc_if, uint8_t *chid)
{
	uint32_t uid = 0;

	DWC_MUTEX_LOCK(cc_if->mutex);
	uid = cc_match_chid(cc_if, chid);
	DWC_MUTEX_UNLOCK(cc_if->mutex);
	return uid;
}
uint32_t dwc_cc_match_cdid(dwc_cc_if_t *cc_if, uint8_t *cdid)
{
	uint32_t uid = 0;

	DWC_MUTEX_LOCK(cc_if->mutex);
	uid = cc_match_cdid(cc_if, cdid);
	DWC_MUTEX_UNLOCK(cc_if->mutex);
	return uid;
}

uint8_t *dwc_cc_ck(dwc_cc_if_t *cc_if, int32_t id)
{
	uint8_t *ck = NULL;
	dwc_cc_t *cc;

	DWC_MUTEX_LOCK(cc_if->mutex);
	cc = cc_find(cc_if, id);
	if (cc) {
		ck = cc->ck;
	}
	DWC_MUTEX_UNLOCK(cc_if->mutex);

	return ck;

}

uint8_t *dwc_cc_chid(dwc_cc_if_t *cc_if, int32_t id)
{
	uint8_t *retval = NULL;
	dwc_cc_t *cc;

	DWC_MUTEX_LOCK(cc_if->mutex);
	cc = cc_find(cc_if, id);
	if (cc) {
		retval = cc->chid;
	}
	DWC_MUTEX_UNLOCK(cc_if->mutex);

	return retval;
}

uint8_t *dwc_cc_cdid(dwc_cc_if_t *cc_if, int32_t id)
{
	uint8_t *retval = NULL;
	dwc_cc_t *cc;

	DWC_MUTEX_LOCK(cc_if->mutex);
	cc = cc_find(cc_if, id);
	if (cc) {
		retval = cc->cdid;
	}
	DWC_MUTEX_UNLOCK(cc_if->mutex);

	return retval;
}

uint8_t *dwc_cc_name(dwc_cc_if_t *cc_if, int32_t id, uint8_t *length)
{
	uint8_t *retval = NULL;
	dwc_cc_t *cc;

	DWC_MUTEX_LOCK(cc_if->mutex);
	*length = 0;
	cc = cc_find(cc_if, id);
	if (cc) {
		*length = cc->length;
		retval = cc->name;
	}
	DWC_MUTEX_UNLOCK(cc_if->mutex);

	return retval;
}

#endif	/* DWC_CCLIB */