summaryrefslogtreecommitdiff
path: root/sound/pci/asihpi/hpimsgx.c
blob: 360028b9abf59ea3564735a6ba29992e80a426a9 (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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
/******************************************************************************

    AudioScience HPI driver
    Copyright (C) 1997-2010  AudioScience Inc. <support@audioscience.com>

    This program is free software; you can redistribute it and/or modify
    it under the terms of version 2 of the GNU General Public License as
    published by the Free Software Foundation;

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

Extended Message Function With Response Cacheing

(C) Copyright AudioScience Inc. 2002
*****************************************************************************/
#define SOURCEFILE_NAME "hpimsgx.c"
#include "hpi_internal.h"
#include "hpimsginit.h"
#include "hpicmn.h"
#include "hpimsgx.h"
#include "hpidebug.h"

static struct pci_device_id asihpi_pci_tbl[] = {
#include "hpipcida.h"
};

static struct hpios_spinlock msgx_lock;

static hpi_handler_func *hpi_entry_points[HPI_MAX_ADAPTERS];

static hpi_handler_func *hpi_lookup_entry_point_function(const struct hpi_pci
	*pci_info)
{

	int i;

	for (i = 0; asihpi_pci_tbl[i].vendor != 0; i++) {
		if (asihpi_pci_tbl[i].vendor != PCI_ANY_ID
			&& asihpi_pci_tbl[i].vendor !=
			pci_info->pci_dev->vendor)
			continue;
		if (asihpi_pci_tbl[i].device != PCI_ANY_ID
			&& asihpi_pci_tbl[i].device !=
			pci_info->pci_dev->device)
			continue;
		if (asihpi_pci_tbl[i].subvendor != PCI_ANY_ID
			&& asihpi_pci_tbl[i].subvendor !=
			pci_info->pci_dev->subsystem_vendor)
			continue;
		if (asihpi_pci_tbl[i].subdevice != PCI_ANY_ID
			&& asihpi_pci_tbl[i].subdevice !=
			pci_info->pci_dev->subsystem_device)
			continue;

		/* HPI_DEBUG_LOG(DEBUG, " %x,%lx\n", i,
		   asihpi_pci_tbl[i].driver_data); */
		return (hpi_handler_func *) asihpi_pci_tbl[i].driver_data;
	}

	return NULL;
}

static inline void hw_entry_point(struct hpi_message *phm,
	struct hpi_response *phr)
{
	if ((phm->adapter_index < HPI_MAX_ADAPTERS)
		&& hpi_entry_points[phm->adapter_index])
		hpi_entry_points[phm->adapter_index] (phm, phr);
	else
		hpi_init_response(phr, phm->object, phm->function,
			HPI_ERROR_PROCESSING_MESSAGE);
}

static void adapter_open(struct hpi_message *phm, struct hpi_response *phr);
static void adapter_close(struct hpi_message *phm, struct hpi_response *phr);

static void mixer_open(struct hpi_message *phm, struct hpi_response *phr);
static void mixer_close(struct hpi_message *phm, struct hpi_response *phr);

static void outstream_open(struct hpi_message *phm, struct hpi_response *phr,
	void *h_owner);
static void outstream_close(struct hpi_message *phm, struct hpi_response *phr,
	void *h_owner);
static void instream_open(struct hpi_message *phm, struct hpi_response *phr,
	void *h_owner);
static void instream_close(struct hpi_message *phm, struct hpi_response *phr,
	void *h_owner);

static void HPIMSGX__reset(u16 adapter_index);

static u16 HPIMSGX__init(struct hpi_message *phm, struct hpi_response *phr);
static void HPIMSGX__cleanup(u16 adapter_index, void *h_owner);

#ifndef DISABLE_PRAGMA_PACK1
#pragma pack(push, 1)
#endif

struct hpi_subsys_response {
	struct hpi_response_header h;
	struct hpi_subsys_res s;
};

struct hpi_adapter_response {
	struct hpi_response_header h;
	struct hpi_adapter_res a;
};

struct hpi_mixer_response {
	struct hpi_response_header h;
	struct hpi_mixer_res m;
};

struct hpi_stream_response {
	struct hpi_response_header h;
	struct hpi_stream_res d;
};

struct adapter_info {
	u16 type;
	u16 num_instreams;
	u16 num_outstreams;
};

struct asi_open_state {
	int open_flag;
	void *h_owner;
};

#ifndef DISABLE_PRAGMA_PACK1
#pragma pack(pop)
#endif

/* Globals */
static struct hpi_adapter_response rESP_HPI_ADAPTER_OPEN[HPI_MAX_ADAPTERS];

static struct hpi_stream_response
	rESP_HPI_OSTREAM_OPEN[HPI_MAX_ADAPTERS][HPI_MAX_STREAMS];

static struct hpi_stream_response
	rESP_HPI_ISTREAM_OPEN[HPI_MAX_ADAPTERS][HPI_MAX_STREAMS];

static struct hpi_mixer_response rESP_HPI_MIXER_OPEN[HPI_MAX_ADAPTERS];

static struct adapter_info aDAPTER_INFO[HPI_MAX_ADAPTERS];

/* use these to keep track of opens from user mode apps/DLLs */
static struct asi_open_state
	outstream_user_open[HPI_MAX_ADAPTERS][HPI_MAX_STREAMS];

static struct asi_open_state
	instream_user_open[HPI_MAX_ADAPTERS][HPI_MAX_STREAMS];

static void subsys_message(struct hpi_message *phm, struct hpi_response *phr,
	void *h_owner)
{
	if (phm->adapter_index != HPI_ADAPTER_INDEX_INVALID)
		HPI_DEBUG_LOG(WARNING,
			"suspicious adapter index %d in subsys message 0x%x.\n",
			phm->adapter_index, phm->function);

	switch (phm->function) {
	case HPI_SUBSYS_GET_VERSION:
		hpi_init_response(phr, HPI_OBJ_SUBSYSTEM,
			HPI_SUBSYS_GET_VERSION, 0);
		phr->u.s.version = HPI_VER >> 8;	/* return major.minor */
		phr->u.s.data = HPI_VER;	/* return major.minor.release */
		break;
	case HPI_SUBSYS_OPEN:
		/*do not propagate the message down the chain */
		hpi_init_response(phr, HPI_OBJ_SUBSYSTEM, HPI_SUBSYS_OPEN, 0);
		break;
	case HPI_SUBSYS_CLOSE:
		/*do not propagate the message down the chain */
		hpi_init_response(phr, HPI_OBJ_SUBSYSTEM, HPI_SUBSYS_CLOSE,
			0);
		HPIMSGX__cleanup(HPIMSGX_ALLADAPTERS, h_owner);
		break;
	case HPI_SUBSYS_DRIVER_LOAD:
		/* Initialize this module's internal state */
		hpios_msgxlock_init(&msgx_lock);
		memset(&hpi_entry_points, 0, sizeof(hpi_entry_points));
		hpios_locked_mem_init();
		/* Init subsys_findadapters response to no-adapters */
		HPIMSGX__reset(HPIMSGX_ALLADAPTERS);
		hpi_init_response(phr, HPI_OBJ_SUBSYSTEM,
			HPI_SUBSYS_DRIVER_LOAD, 0);
		/* individual HPIs dont implement driver load */
		HPI_COMMON(phm, phr);
		break;
	case HPI_SUBSYS_DRIVER_UNLOAD:
		HPI_COMMON(phm, phr);
		HPIMSGX__cleanup(HPIMSGX_ALLADAPTERS, h_owner);
		hpios_locked_mem_free_all();
		hpi_init_response(phr, HPI_OBJ_SUBSYSTEM,
			HPI_SUBSYS_DRIVER_UNLOAD, 0);
		return;

	case HPI_SUBSYS_GET_NUM_ADAPTERS:
	case HPI_SUBSYS_GET_ADAPTER:
		HPI_COMMON(phm, phr);
		break;

	case HPI_SUBSYS_CREATE_ADAPTER:
		HPIMSGX__init(phm, phr);
		break;

	case HPI_SUBSYS_DELETE_ADAPTER:
		HPIMSGX__cleanup(phm->obj_index, h_owner);
		{
			struct hpi_message hm;
			struct hpi_response hr;
			hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
				HPI_ADAPTER_CLOSE);
			hm.adapter_index = phm->obj_index;
			hw_entry_point(&hm, &hr);
		}
		if ((phm->obj_index < HPI_MAX_ADAPTERS)
			&& hpi_entry_points[phm->obj_index]) {
			hpi_entry_points[phm->obj_index] (phm, phr);
			hpi_entry_points[phm->obj_index] = NULL;
		} else
			phr->error = HPI_ERROR_INVALID_OBJ_INDEX;

		break;
	default:
		/* Must explicitly handle every subsys message in this switch */
		hpi_init_response(phr, HPI_OBJ_SUBSYSTEM, phm->function,
			HPI_ERROR_INVALID_FUNC);
		break;
	}
}

static void adapter_message(struct hpi_message *phm, struct hpi_response *phr,
	void *h_owner)
{
	switch (phm->function) {
	case HPI_ADAPTER_OPEN:
		adapter_open(phm, phr);
		break;
	case HPI_ADAPTER_CLOSE:
		adapter_close(phm, phr);
		break;
	default:
		hw_entry_point(phm, phr);
		break;
	}
}

static void mixer_message(struct hpi_message *phm, struct hpi_response *phr)
{
	switch (phm->function) {
	case HPI_MIXER_OPEN:
		mixer_open(phm, phr);
		break;
	case HPI_MIXER_CLOSE:
		mixer_close(phm, phr);
		break;
	default:
		hw_entry_point(phm, phr);
		break;
	}
}

static void outstream_message(struct hpi_message *phm,
	struct hpi_response *phr, void *h_owner)
{
	if (phm->obj_index >= aDAPTER_INFO[phm->adapter_index].num_outstreams) {
		hpi_init_response(phr, HPI_OBJ_OSTREAM, phm->function,
			HPI_ERROR_INVALID_OBJ_INDEX);
		return;
	}

	switch (phm->function) {
	case HPI_OSTREAM_OPEN:
		outstream_open(phm, phr, h_owner);
		break;
	case HPI_OSTREAM_CLOSE:
		outstream_close(phm, phr, h_owner);
		break;
	default:
		hw_entry_point(phm, phr);
		break;
	}
}

static void instream_message(struct hpi_message *phm,
	struct hpi_response *phr, void *h_owner)
{
	if (phm->obj_index >= aDAPTER_INFO[phm->adapter_index].num_instreams) {
		hpi_init_response(phr, HPI_OBJ_ISTREAM, phm->function,
			HPI_ERROR_INVALID_OBJ_INDEX);
		return;
	}

	switch (phm->function) {
	case HPI_ISTREAM_OPEN:
		instream_open(phm, phr, h_owner);
		break;
	case HPI_ISTREAM_CLOSE:
		instream_close(phm, phr, h_owner);
		break;
	default:
		hw_entry_point(phm, phr);
		break;
	}
}

/* NOTE: HPI_Message() must be defined in the driver as a wrapper for
 * HPI_MessageEx so that functions in hpifunc.c compile.
 */
void hpi_send_recv_ex(struct hpi_message *phm, struct hpi_response *phr,
	void *h_owner)
{
	HPI_DEBUG_MESSAGE(DEBUG, phm);

	if (phm->type != HPI_TYPE_MESSAGE) {
		hpi_init_response(phr, phm->object, phm->function,
			HPI_ERROR_INVALID_TYPE);
		return;
	}

	if (phm->adapter_index >= HPI_MAX_ADAPTERS
		&& phm->adapter_index != HPIMSGX_ALLADAPTERS) {
		hpi_init_response(phr, phm->object, phm->function,
			HPI_ERROR_BAD_ADAPTER_NUMBER);
		return;
	}

	switch (phm->object) {
	case HPI_OBJ_SUBSYSTEM:
		subsys_message(phm, phr, h_owner);
		break;

	case HPI_OBJ_ADAPTER:
		adapter_message(phm, phr, h_owner);
		break;

	case HPI_OBJ_MIXER:
		mixer_message(phm, phr);
		break;

	case HPI_OBJ_OSTREAM:
		outstream_message(phm, phr, h_owner);
		break;

	case HPI_OBJ_ISTREAM:
		instream_message(phm, phr, h_owner);
		break;

	default:
		hw_entry_point(phm, phr);
		break;
	}
	HPI_DEBUG_RESPONSE(phr);

}

static void adapter_open(struct hpi_message *phm, struct hpi_response *phr)
{
	HPI_DEBUG_LOG(VERBOSE, "adapter_open\n");
	memcpy(phr, &rESP_HPI_ADAPTER_OPEN[phm->adapter_index],
		sizeof(rESP_HPI_ADAPTER_OPEN[0]));
}

static void adapter_close(struct hpi_message *phm, struct hpi_response *phr)
{
	HPI_DEBUG_LOG(VERBOSE, "adapter_close\n");
	hpi_init_response(phr, HPI_OBJ_ADAPTER, HPI_ADAPTER_CLOSE, 0);
}

static void mixer_open(struct hpi_message *phm, struct hpi_response *phr)
{
	memcpy(phr, &rESP_HPI_MIXER_OPEN[phm->adapter_index],
		sizeof(rESP_HPI_MIXER_OPEN[0]));
}

static void mixer_close(struct hpi_message *phm, struct hpi_response *phr)
{
	hpi_init_response(phr, HPI_OBJ_MIXER, HPI_MIXER_CLOSE, 0);
}

static void instream_open(struct hpi_message *phm, struct hpi_response *phr,
	void *h_owner)
{

	struct hpi_message hm;
	struct hpi_response hr;

	hpi_init_response(phr, HPI_OBJ_ISTREAM, HPI_ISTREAM_OPEN, 0);

	hpios_msgxlock_lock(&msgx_lock);

	if (instream_user_open[phm->adapter_index][phm->obj_index].open_flag)
		phr->error = HPI_ERROR_OBJ_ALREADY_OPEN;
	else if (rESP_HPI_ISTREAM_OPEN[phm->adapter_index]
		[phm->obj_index].h.error)
		memcpy(phr,
			&rESP_HPI_ISTREAM_OPEN[phm->adapter_index][phm->
				obj_index],
			sizeof(rESP_HPI_ISTREAM_OPEN[0][0]));
	else {
		instream_user_open[phm->adapter_index][phm->
			obj_index].open_flag = 1;
		hpios_msgxlock_unlock(&msgx_lock);

		/* issue a reset */
		hpi_init_message_response(&hm, &hr, HPI_OBJ_ISTREAM,
			HPI_ISTREAM_RESET);
		hm.adapter_index = phm->adapter_index;
		hm.obj_index = phm->obj_index;
		hw_entry_point(&hm, &hr);

		hpios_msgxlock_lock(&msgx_lock);
		if (hr.error) {
			instream_user_open[phm->adapter_index][phm->
				obj_index].open_flag = 0;
			phr->error = hr.error;
		} else {
			instream_user_open[phm->adapter_index][phm->
				obj_index].open_flag = 1;
			instream_user_open[phm->adapter_index][phm->
				obj_index].h_owner = h_owner;
			memcpy(phr,
				&rESP_HPI_ISTREAM_OPEN[phm->adapter_index]
				[phm->obj_index],
				sizeof(rESP_HPI_ISTREAM_OPEN[0][0]));
		}
	}
	hpios_msgxlock_unlock(&msgx_lock);
}

static void instream_close(struct hpi_message *phm, struct hpi_response *phr,
	void *h_owner)
{

	struct hpi_message hm;
	struct hpi_response hr;

	hpi_init_response(phr, HPI_OBJ_ISTREAM, HPI_ISTREAM_CLOSE, 0);

	hpios_msgxlock_lock(&msgx_lock);
	if (h_owner ==
		instream_user_open[phm->adapter_index][phm->
			obj_index].h_owner) {
		/* HPI_DEBUG_LOG(INFO,"closing adapter %d "
		   "instream %d owned by %p\n",
		   phm->wAdapterIndex, phm->wObjIndex, hOwner); */
		instream_user_open[phm->adapter_index][phm->
			obj_index].h_owner = NULL;
		hpios_msgxlock_unlock(&msgx_lock);
		/* issue a reset */
		hpi_init_message_response(&hm, &hr, HPI_OBJ_ISTREAM,
			HPI_ISTREAM_RESET);
		hm.adapter_index = phm->adapter_index;
		hm.obj_index = phm->obj_index;
		hw_entry_point(&hm, &hr);
		hpios_msgxlock_lock(&msgx_lock);
		if (hr.error) {
			instream_user_open[phm->adapter_index][phm->
				obj_index].h_owner = h_owner;
			phr->error = hr.error;
		} else {
			instream_user_open[phm->adapter_index][phm->
				obj_index].open_flag = 0;
			instream_user_open[phm->adapter_index][phm->
				obj_index].h_owner = NULL;
		}
	} else {
		HPI_DEBUG_LOG(WARNING,
			"%p trying to close %d instream %d owned by %p\n",
			h_owner, phm->adapter_index, phm->obj_index,
			instream_user_open[phm->adapter_index][phm->
				obj_index].h_owner);
		phr->error = HPI_ERROR_OBJ_NOT_OPEN;
	}
	hpios_msgxlock_unlock(&msgx_lock);
}

static void outstream_open(struct hpi_message *phm, struct hpi_response *phr,
	void *h_owner)
{

	struct hpi_message hm;
	struct hpi_response hr;

	hpi_init_response(phr, HPI_OBJ_OSTREAM, HPI_OSTREAM_OPEN, 0);

	hpios_msgxlock_lock(&msgx_lock);

	if (outstream_user_open[phm->adapter_index][phm->obj_index].open_flag)
		phr->error = HPI_ERROR_OBJ_ALREADY_OPEN;
	else if (rESP_HPI_OSTREAM_OPEN[phm->adapter_index]
		[phm->obj_index].h.error)
		memcpy(phr,
			&rESP_HPI_OSTREAM_OPEN[phm->adapter_index][phm->
				obj_index],
			sizeof(rESP_HPI_OSTREAM_OPEN[0][0]));
	else {
		outstream_user_open[phm->adapter_index][phm->
			obj_index].open_flag = 1;
		hpios_msgxlock_unlock(&msgx_lock);

		/* issue a reset */
		hpi_init_message_response(&hm, &hr, HPI_OBJ_OSTREAM,
			HPI_OSTREAM_RESET);
		hm.adapter_index = phm->adapter_index;
		hm.obj_index = phm->obj_index;
		hw_entry_point(&hm, &hr);

		hpios_msgxlock_lock(&msgx_lock);
		if (hr.error) {
			outstream_user_open[phm->adapter_index][phm->
				obj_index].open_flag = 0;
			phr->error = hr.error;
		} else {
			outstream_user_open[phm->adapter_index][phm->
				obj_index].open_flag = 1;
			outstream_user_open[phm->adapter_index][phm->
				obj_index].h_owner = h_owner;
			memcpy(phr,
				&rESP_HPI_OSTREAM_OPEN[phm->adapter_index]
				[phm->obj_index],
				sizeof(rESP_HPI_OSTREAM_OPEN[0][0]));
		}
	}
	hpios_msgxlock_unlock(&msgx_lock);
}

static void outstream_close(struct hpi_message *phm, struct hpi_response *phr,
	void *h_owner)
{

	struct hpi_message hm;
	struct hpi_response hr;

	hpi_init_response(phr, HPI_OBJ_OSTREAM, HPI_OSTREAM_CLOSE, 0);

	hpios_msgxlock_lock(&msgx_lock);

	if (h_owner ==
		outstream_user_open[phm->adapter_index][phm->
			obj_index].h_owner) {
		/* HPI_DEBUG_LOG(INFO,"closing adapter %d "
		   "outstream %d owned by %p\n",
		   phm->wAdapterIndex, phm->wObjIndex, hOwner); */
		outstream_user_open[phm->adapter_index][phm->
			obj_index].h_owner = NULL;
		hpios_msgxlock_unlock(&msgx_lock);
		/* issue a reset */
		hpi_init_message_response(&hm, &hr, HPI_OBJ_OSTREAM,
			HPI_OSTREAM_RESET);
		hm.adapter_index = phm->adapter_index;
		hm.obj_index = phm->obj_index;
		hw_entry_point(&hm, &hr);
		hpios_msgxlock_lock(&msgx_lock);
		if (hr.error) {
			outstream_user_open[phm->adapter_index][phm->
				obj_index].h_owner = h_owner;
			phr->error = hr.error;
		} else {
			outstream_user_open[phm->adapter_index][phm->
				obj_index].open_flag = 0;
			outstream_user_open[phm->adapter_index][phm->
				obj_index].h_owner = NULL;
		}
	} else {
		HPI_DEBUG_LOG(WARNING,
			"%p trying to close %d outstream %d owned by %p\n",
			h_owner, phm->adapter_index, phm->obj_index,
			outstream_user_open[phm->adapter_index][phm->
				obj_index].h_owner);
		phr->error = HPI_ERROR_OBJ_NOT_OPEN;
	}
	hpios_msgxlock_unlock(&msgx_lock);
}

static u16 adapter_prepare(u16 adapter)
{
	struct hpi_message hm;
	struct hpi_response hr;

	/* Open the adapter and streams */
	u16 i;

	/* call to HPI_ADAPTER_OPEN */
	hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
		HPI_ADAPTER_OPEN);
	hm.adapter_index = adapter;
	hw_entry_point(&hm, &hr);
	memcpy(&rESP_HPI_ADAPTER_OPEN[adapter], &hr,
		sizeof(rESP_HPI_ADAPTER_OPEN[0]));
	if (hr.error)
		return hr.error;

	/* call to HPI_ADAPTER_GET_INFO */
	hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
		HPI_ADAPTER_GET_INFO);
	hm.adapter_index = adapter;
	hw_entry_point(&hm, &hr);
	if (hr.error)
		return hr.error;

	aDAPTER_INFO[adapter].num_outstreams = hr.u.ax.info.num_outstreams;
	aDAPTER_INFO[adapter].num_instreams = hr.u.ax.info.num_instreams;
	aDAPTER_INFO[adapter].type = hr.u.ax.info.adapter_type;

	/* call to HPI_OSTREAM_OPEN */
	for (i = 0; i < aDAPTER_INFO[adapter].num_outstreams; i++) {
		hpi_init_message_response(&hm, &hr, HPI_OBJ_OSTREAM,
			HPI_OSTREAM_OPEN);
		hm.adapter_index = adapter;
		hm.obj_index = i;
		hw_entry_point(&hm, &hr);
		memcpy(&rESP_HPI_OSTREAM_OPEN[adapter][i], &hr,
			sizeof(rESP_HPI_OSTREAM_OPEN[0][0]));
		outstream_user_open[adapter][i].open_flag = 0;
		outstream_user_open[adapter][i].h_owner = NULL;
	}

	/* call to HPI_ISTREAM_OPEN */
	for (i = 0; i < aDAPTER_INFO[adapter].num_instreams; i++) {
		hpi_init_message_response(&hm, &hr, HPI_OBJ_ISTREAM,
			HPI_ISTREAM_OPEN);
		hm.adapter_index = adapter;
		hm.obj_index = i;
		hw_entry_point(&hm, &hr);
		memcpy(&rESP_HPI_ISTREAM_OPEN[adapter][i], &hr,
			sizeof(rESP_HPI_ISTREAM_OPEN[0][0]));
		instream_user_open[adapter][i].open_flag = 0;
		instream_user_open[adapter][i].h_owner = NULL;
	}

	/* call to HPI_MIXER_OPEN */
	hpi_init_message_response(&hm, &hr, HPI_OBJ_MIXER, HPI_MIXER_OPEN);
	hm.adapter_index = adapter;
	hw_entry_point(&hm, &hr);
	memcpy(&rESP_HPI_MIXER_OPEN[adapter], &hr,
		sizeof(rESP_HPI_MIXER_OPEN[0]));

	return 0;
}

static void HPIMSGX__reset(u16 adapter_index)
{
	int i;
	u16 adapter;
	struct hpi_response hr;

	if (adapter_index == HPIMSGX_ALLADAPTERS) {
		for (adapter = 0; adapter < HPI_MAX_ADAPTERS; adapter++) {

			hpi_init_response(&hr, HPI_OBJ_ADAPTER,
				HPI_ADAPTER_OPEN, HPI_ERROR_BAD_ADAPTER);
			memcpy(&rESP_HPI_ADAPTER_OPEN[adapter], &hr,
				sizeof(rESP_HPI_ADAPTER_OPEN[adapter]));

			hpi_init_response(&hr, HPI_OBJ_MIXER, HPI_MIXER_OPEN,
				HPI_ERROR_INVALID_OBJ);
			memcpy(&rESP_HPI_MIXER_OPEN[adapter], &hr,
				sizeof(rESP_HPI_MIXER_OPEN[adapter]));

			for (i = 0; i < HPI_MAX_STREAMS; i++) {
				hpi_init_response(&hr, HPI_OBJ_OSTREAM,
					HPI_OSTREAM_OPEN,
					HPI_ERROR_INVALID_OBJ);
				memcpy(&rESP_HPI_OSTREAM_OPEN[adapter][i],
					&hr,
					sizeof(rESP_HPI_OSTREAM_OPEN[adapter]
						[i]));
				hpi_init_response(&hr, HPI_OBJ_ISTREAM,
					HPI_ISTREAM_OPEN,
					HPI_ERROR_INVALID_OBJ);
				memcpy(&rESP_HPI_ISTREAM_OPEN[adapter][i],
					&hr,
					sizeof(rESP_HPI_ISTREAM_OPEN[adapter]
						[i]));
			}
		}
	} else if (adapter_index < HPI_MAX_ADAPTERS) {
		rESP_HPI_ADAPTER_OPEN[adapter_index].h.error =
			HPI_ERROR_BAD_ADAPTER;
		rESP_HPI_MIXER_OPEN[adapter_index].h.error =
			HPI_ERROR_INVALID_OBJ;
		for (i = 0; i < HPI_MAX_STREAMS; i++) {
			rESP_HPI_OSTREAM_OPEN[adapter_index][i].h.error =
				HPI_ERROR_INVALID_OBJ;
			rESP_HPI_ISTREAM_OPEN[adapter_index][i].h.error =
				HPI_ERROR_INVALID_OBJ;
		}
	}
}

static u16 HPIMSGX__init(struct hpi_message *phm,
	/* HPI_SUBSYS_CREATE_ADAPTER structure with */
	/* resource list or NULL=find all */
	struct hpi_response *phr
	/* response from HPI_ADAPTER_GET_INFO */
	)
{
	hpi_handler_func *entry_point_func;
	struct hpi_response hr;

	/* Init response here so we can pass in previous adapter list */
	hpi_init_response(&hr, phm->object, phm->function,
		HPI_ERROR_INVALID_OBJ);

	entry_point_func =
		hpi_lookup_entry_point_function(phm->u.s.resource.r.pci);

	if (entry_point_func) {
		HPI_DEBUG_MESSAGE(DEBUG, phm);
		entry_point_func(phm, &hr);
	} else {
		phr->error = HPI_ERROR_PROCESSING_MESSAGE;
		return phr->error;
	}
	if (hr.error == 0) {
		/* the adapter was created successfully
		   save the mapping for future use */
		hpi_entry_points[hr.u.s.adapter_index] = entry_point_func;
		/* prepare adapter (pre-open streams etc.) */
		HPI_DEBUG_LOG(DEBUG,
			"HPI_SUBSYS_CREATE_ADAPTER successful,"
			" preparing adapter\n");
		adapter_prepare(hr.u.s.adapter_index);
	}
	memcpy(phr, &hr, hr.size);
	return phr->error;
}

static void HPIMSGX__cleanup(u16 adapter_index, void *h_owner)
{
	int i, adapter, adapter_limit;

	if (!h_owner)
		return;

	if (adapter_index == HPIMSGX_ALLADAPTERS) {
		adapter = 0;
		adapter_limit = HPI_MAX_ADAPTERS;
	} else {
		adapter = adapter_index;
		adapter_limit = adapter + 1;
	}

	for (; adapter < adapter_limit; adapter++) {
		/*      printk(KERN_INFO "Cleanup adapter #%d\n",wAdapter); */
		for (i = 0; i < HPI_MAX_STREAMS; i++) {
			if (h_owner ==
				outstream_user_open[adapter][i].h_owner) {
				struct hpi_message hm;
				struct hpi_response hr;

				HPI_DEBUG_LOG(DEBUG,
					"Close adapter %d ostream %d\n",
					adapter, i);

				hpi_init_message_response(&hm, &hr,
					HPI_OBJ_OSTREAM, HPI_OSTREAM_RESET);
				hm.adapter_index = (u16)adapter;
				hm.obj_index = (u16)i;
				hw_entry_point(&hm, &hr);

				hm.function = HPI_OSTREAM_HOSTBUFFER_FREE;
				hw_entry_point(&hm, &hr);

				hm.function = HPI_OSTREAM_GROUP_RESET;
				hw_entry_point(&hm, &hr);

				outstream_user_open[adapter][i].open_flag = 0;
				outstream_user_open[adapter][i].h_owner =
					NULL;
			}
			if (h_owner == instream_user_open[adapter][i].h_owner) {
				struct hpi_message hm;
				struct hpi_response hr;

				HPI_DEBUG_LOG(DEBUG,
					"Close adapter %d istream %d\n",
					adapter, i);

				hpi_init_message_response(&hm, &hr,
					HPI_OBJ_ISTREAM, HPI_ISTREAM_RESET);
				hm.adapter_index = (u16)adapter;
				hm.obj_index = (u16)i;
				hw_entry_point(&hm, &hr);

				hm.function = HPI_ISTREAM_HOSTBUFFER_FREE;
				hw_entry_point(&hm, &hr);

				hm.function = HPI_ISTREAM_GROUP_RESET;
				hw_entry_point(&hm, &hr);

				instream_user_open[adapter][i].open_flag = 0;
				instream_user_open[adapter][i].h_owner = NULL;
			}
		}
	}
}