summaryrefslogtreecommitdiff
path: root/drivers/dahdi/vpmadt032_loader/dahdi_vpmadt032_loader.c
blob: cdba79061f5b4a4f1817e058d9c123c8867df027 (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
/*
 * DAHDI Telephony Interface to VPMADT032 Firmware Loader
 *
 * Copyright (C) 2008-2010 Digium, Inc. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/ctype.h>
#include <linux/moduleparam.h>
#include <linux/pci.h>

#include <dahdi/kernel.h>

static int debug;

#include "voicebus/voicebus.h"
#include "voicebus/vpmadtreg.h"
#include "vpmadt032_loader.h"

vpmlinkage static int __attribute__((format (printf, 1, 2)))
logger(const char *format, ...)
{
	int res;
	va_list args;

#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 9)
	va_start(args, format);
	res = vprintk(format, args);
	va_end(args);
#else
	char buf[256];

	va_start(args, format);
	res = vsnprintf(buf, sizeof(buf), format, args);
	va_end(args);
	printk(KERN_INFO "%s" buf);
#endif

	return res;
}

vpmlinkage static void *memalloc(size_t len)
{
	return kmalloc(len, GFP_KERNEL);
}

vpmlinkage static void memfree(void *ptr)
{
	kfree(ptr);
}

struct private_context {
	struct voicebus *vb;
	void *pvt;
	struct completion done;
};

static void init_private_context(struct private_context *ctx)
{
	init_completion(&ctx->done);
}

static void handle_receive(struct voicebus *vb, struct list_head *buffers)
{
	struct private_context *ctx = pci_get_drvdata(vb->pdev);
	struct vbb *vbb;
	list_for_each_entry(vbb, buffers, entry) {
		__vpmadt032_receive(ctx->pvt, vbb->data);
		if (__vpmadt032_done(ctx->pvt))
			complete(&ctx->done);
	}
}

static void handle_transmit(struct voicebus *vb, struct list_head *buffers)
{
	struct vbb *vbb;
	struct private_context *ctx = pci_get_drvdata(vb->pdev);
	list_for_each_entry(vbb, buffers, entry)
		__vpmadt032_transmit(ctx->pvt, vbb->data);
}

static const struct voicebus_operations loader_operations = {
	.handle_receive = handle_receive,
	.handle_transmit = handle_transmit,
};

static int vpmadt032_load_firmware(struct voicebus *vb)
{
	int ret = 0;
	struct private_context *ctx;
	const struct voicebus_operations *old;
	void *old_drvdata;
	int id;
	might_sleep();
	ctx = kzalloc(sizeof(struct private_context), GFP_KERNEL);
	if (!ctx)
		return -ENOMEM;
	init_private_context(ctx);
	ctx->vb = vb;

	if (0x8007 == vb->pdev->device || 0x8008 == vb->pdev->device)
		id = vb->pdev->vendor << 16 | 0x2400;
	else
		id = vb->pdev->vendor << 16 | vb->pdev->device;

	ret = __vpmadt032_start_load(0, id, &ctx->pvt);
	if (ret)
		goto error_exit;
	old_drvdata = pci_get_drvdata(vb->pdev);
	pci_set_drvdata(vb->pdev, ctx);
	old = vb->ops;
	vb->ops = &loader_operations;
	if (!wait_for_completion_timeout(&ctx->done, HZ*20))
		ret = -EIO;
	vb->ops = old;
	pci_set_drvdata(vb->pdev, old_drvdata);
	__vpmadt032_cleanup(ctx->pvt);
error_exit:
	kfree(ctx);
	return ret;
}

static struct vpmadt_loader loader = {
	.owner = THIS_MODULE,
	.load = vpmadt032_load_firmware,
};

static int __init vpmadt032_loader_init(void)
{
	__vpmadt032_init(logger, debug, memalloc, memfree);
	vpmadtreg_register(&loader);
	return 0;
}

static void __exit vpmadt032_loader_exit(void)
{
	vpmadtreg_unregister(&loader);
	return;
}

module_param(debug, int, S_IRUGO | S_IWUSR);
MODULE_DESCRIPTION("DAHDI VPMADT032 (Hardware Echo Canceller) Firmware Loader");
MODULE_AUTHOR("Digium Incorporated <support@digium.com>");
MODULE_LICENSE("Digium Commercial");

module_init(vpmadt032_loader_init);
module_exit(vpmadt032_loader_exit);