blob: 65fcf4770731d1e0fa5606112f3c741f38dab7b0 [file] [log] [blame]
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001/*******************************************************************************
2
3 AudioScience HPI driver
Eliot Blennerhassett1c073b62011-07-22 15:52:46 +12004 Copyright (C) 1997-2011 AudioScience Inc. <support@audioscience.com>
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02005
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of version 2 of the GNU General Public License as
8 published by the Free Software Foundation;
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19Common Linux HPI ioctl and module probe/remove functions
20*******************************************************************************/
21#define SOURCEFILE_NAME "hpioctl.c"
22
23#include "hpi_internal.h"
24#include "hpimsginit.h"
25#include "hpidebug.h"
26#include "hpimsgx.h"
27#include "hpioctl.h"
Eliot Blennerhassett6d0b8982011-04-05 20:55:47 +120028#include "hpicmn.h"
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020029
30#include <linux/fs.h>
31#include <linux/slab.h>
32#include <linux/moduleparam.h>
33#include <asm/uaccess.h>
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +130034#include <linux/pci.h>
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020035#include <linux/stringify.h>
36
37#ifdef MODULE_FIRMWARE
38MODULE_FIRMWARE("asihpi/dsp5000.bin");
39MODULE_FIRMWARE("asihpi/dsp6200.bin");
40MODULE_FIRMWARE("asihpi/dsp6205.bin");
41MODULE_FIRMWARE("asihpi/dsp6400.bin");
42MODULE_FIRMWARE("asihpi/dsp6600.bin");
43MODULE_FIRMWARE("asihpi/dsp8700.bin");
44MODULE_FIRMWARE("asihpi/dsp8900.bin");
45#endif
46
47static int prealloc_stream_buf;
48module_param(prealloc_stream_buf, int, S_IRUGO);
49MODULE_PARM_DESC(prealloc_stream_buf,
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +130050 "Preallocate size for per-adapter stream buffer");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020051
52/* Allow the debug level to be changed after module load.
53 E.g. echo 2 > /sys/module/asihpi/parameters/hpiDebugLevel
54*/
55module_param(hpi_debug_level, int, S_IRUGO | S_IWUSR);
56MODULE_PARM_DESC(hpi_debug_level, "debug verbosity 0..5");
57
58/* List of adapters found */
59static struct hpi_adapter adapters[HPI_MAX_ADAPTERS];
60
61/* Wrapper function to HPI_Message to enable dumping of the
62 message and response types.
63*/
64static void hpi_send_recv_f(struct hpi_message *phm, struct hpi_response *phr,
65 struct file *file)
66{
67 int adapter = phm->adapter_index;
68
69 if ((adapter >= HPI_MAX_ADAPTERS || adapter < 0)
70 && (phm->object != HPI_OBJ_SUBSYSTEM))
71 phr->error = HPI_ERROR_INVALID_OBJ_INDEX;
72 else
73 hpi_send_recv_ex(phm, phr, file);
74}
75
76/* This is called from hpifunc.c functions, called by ALSA
77 * (or other kernel process) In this case there is no file descriptor
78 * available for the message cache code
79 */
80void hpi_send_recv(struct hpi_message *phm, struct hpi_response *phr)
81{
82 hpi_send_recv_f(phm, phr, HOWNER_KERNEL);
83}
84
85EXPORT_SYMBOL(hpi_send_recv);
86/* for radio-asihpi */
87
88int asihpi_hpi_release(struct file *file)
89{
90 struct hpi_message hm;
91 struct hpi_response hr;
92
93/* HPI_DEBUG_LOG(INFO,"hpi_release file %p, pid %d\n", file, current->pid); */
94 /* close the subsystem just in case the application forgot to. */
95 hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
96 HPI_SUBSYS_CLOSE);
97 hpi_send_recv_ex(&hm, &hr, file);
98 return 0;
99}
100
101long asihpi_hpi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
102{
103 struct hpi_ioctl_linux __user *phpi_ioctl_data;
104 void __user *puhm;
105 void __user *puhr;
106 union hpi_message_buffer_v1 *hm;
107 union hpi_response_buffer_v1 *hr;
108 u16 res_max_size;
109 u32 uncopied_bytes;
110 struct hpi_adapter *pa = NULL;
111 int err = 0;
112
113 if (cmd != HPI_IOCTL_LINUX)
114 return -EINVAL;
115
116 hm = kmalloc(sizeof(*hm), GFP_KERNEL);
117 hr = kmalloc(sizeof(*hr), GFP_KERNEL);
118 if (!hm || !hr) {
119 err = -ENOMEM;
120 goto out;
121 }
122
123 phpi_ioctl_data = (struct hpi_ioctl_linux __user *)arg;
124
125 /* Read the message and response pointers from user space. */
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300126 if (get_user(puhm, &phpi_ioctl_data->phm)
127 || get_user(puhr, &phpi_ioctl_data->phr)) {
Kulikov Vasiliyec9d04b2010-07-28 20:41:56 +0400128 err = -EFAULT;
129 goto out;
130 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200131
132 /* Now read the message size and data from user space. */
Kulikov Vasiliyec9d04b2010-07-28 20:41:56 +0400133 if (get_user(hm->h.size, (u16 __user *)puhm)) {
134 err = -EFAULT;
135 goto out;
136 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200137 if (hm->h.size > sizeof(*hm))
138 hm->h.size = sizeof(*hm);
139
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300140 /* printk(KERN_INFO "message size %d\n", hm->h.wSize); */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200141
142 uncopied_bytes = copy_from_user(hm, puhm, hm->h.size);
143 if (uncopied_bytes) {
144 HPI_DEBUG_LOG(ERROR, "uncopied bytes %d\n", uncopied_bytes);
145 err = -EFAULT;
146 goto out;
147 }
148
Kulikov Vasiliyec9d04b2010-07-28 20:41:56 +0400149 if (get_user(res_max_size, (u16 __user *)puhr)) {
150 err = -EFAULT;
151 goto out;
152 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200153 /* printk(KERN_INFO "user response size %d\n", res_max_size); */
154 if (res_max_size < sizeof(struct hpi_response_header)) {
155 HPI_DEBUG_LOG(WARNING, "small res size %d\n", res_max_size);
156 err = -EFAULT;
157 goto out;
158 }
159
Eliot Blennerhassett6d0b8982011-04-05 20:55:47 +1200160 switch (hm->h.function) {
161 case HPI_SUBSYS_CREATE_ADAPTER:
162 case HPI_ADAPTER_DELETE:
163 /* Application must not use these functions! */
164 hr->h.size = sizeof(hr->h);
165 hr->h.error = HPI_ERROR_INVALID_OPERATION;
166 hr->h.function = hm->h.function;
167 uncopied_bytes = copy_to_user(puhr, hr, hr->h.size);
168 if (uncopied_bytes)
169 err = -EFAULT;
170 else
171 err = 0;
172 goto out;
173 }
174
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300175 hr->h.size = res_max_size;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200176 if (hm->h.object == HPI_OBJ_SUBSYSTEM) {
Eliot Blennerhassett6d0b8982011-04-05 20:55:47 +1200177 hpi_send_recv_f(&hm->m0, &hr->r0, file);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200178 } else {
179 u16 __user *ptr = NULL;
180 u32 size = 0;
181
182 /* -1=no data 0=read from user mem, 1=write to user mem */
183 int wrflag = -1;
184 u32 adapter = hm->h.adapter_index;
185
Eliot Blennerhassett6d0b8982011-04-05 20:55:47 +1200186 if ((adapter > HPI_MAX_ADAPTERS) || (!pa->type)) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200187 hpi_init_response(&hr->r0, HPI_OBJ_ADAPTER,
188 HPI_ADAPTER_OPEN,
189 HPI_ERROR_BAD_ADAPTER_NUMBER);
190
191 uncopied_bytes =
192 copy_to_user(puhr, hr, sizeof(hr->h));
193 if (uncopied_bytes)
194 err = -EFAULT;
195 else
196 err = 0;
197 goto out;
198 }
199
Eliot Blennerhassett1c073b62011-07-22 15:52:46 +1200200 pa = &adapters[adapter];
201
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200202 if (mutex_lock_interruptible(&adapters[adapter].mutex)) {
203 err = -EINTR;
204 goto out;
205 }
206
207 /* Dig out any pointers embedded in the message. */
208 switch (hm->h.function) {
209 case HPI_OSTREAM_WRITE:
210 case HPI_ISTREAM_READ:{
211 /* Yes, sparse, this is correct. */
212 ptr = (u16 __user *)hm->m0.u.d.u.data.pb_data;
213 size = hm->m0.u.d.u.data.data_size;
214
215 /* Allocate buffer according to application request.
216 ?Is it better to alloc/free for the duration
217 of the transaction?
218 */
219 if (pa->buffer_size < size) {
220 HPI_DEBUG_LOG(DEBUG,
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300221 "Realloc adapter %d stream "
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200222 "buffer from %zd to %d\n",
223 hm->h.adapter_index,
224 pa->buffer_size, size);
225 if (pa->p_buffer) {
226 pa->buffer_size = 0;
227 vfree(pa->p_buffer);
228 }
229 pa->p_buffer = vmalloc(size);
230 if (pa->p_buffer)
231 pa->buffer_size = size;
232 else {
233 HPI_DEBUG_LOG(ERROR,
234 "HPI could not allocate "
235 "stream buffer size %d\n",
236 size);
237
238 mutex_unlock(&adapters
239 [adapter].mutex);
240 err = -EINVAL;
241 goto out;
242 }
243 }
244
245 hm->m0.u.d.u.data.pb_data = pa->p_buffer;
246 if (hm->h.function == HPI_ISTREAM_READ)
247 /* from card, WRITE to user mem */
248 wrflag = 1;
249 else
250 wrflag = 0;
251 break;
252 }
253
254 default:
255 size = 0;
256 break;
257 }
258
259 if (size && (wrflag == 0)) {
260 uncopied_bytes =
261 copy_from_user(pa->p_buffer, ptr, size);
262 if (uncopied_bytes)
263 HPI_DEBUG_LOG(WARNING,
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300264 "Missed %d of %d "
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200265 "bytes from user\n", uncopied_bytes,
266 size);
267 }
268
269 hpi_send_recv_f(&hm->m0, &hr->r0, file);
270
271 if (size && (wrflag == 1)) {
272 uncopied_bytes =
273 copy_to_user(ptr, pa->p_buffer, size);
274 if (uncopied_bytes)
275 HPI_DEBUG_LOG(WARNING,
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300276 "Missed %d of %d " "bytes to user\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200277 uncopied_bytes, size);
278 }
279
280 mutex_unlock(&adapters[adapter].mutex);
281 }
282
283 /* on return response size must be set */
284 /*printk(KERN_INFO "response size %d\n", hr->h.wSize); */
285
286 if (!hr->h.size) {
287 HPI_DEBUG_LOG(ERROR, "response zero size\n");
288 err = -EFAULT;
289 goto out;
290 }
291
292 if (hr->h.size > res_max_size) {
293 HPI_DEBUG_LOG(ERROR, "response too big %d %d\n", hr->h.size,
294 res_max_size);
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300295 hr->h.error = HPI_ERROR_RESPONSE_BUFFER_TOO_SMALL;
296 hr->h.specific_error = hr->h.size;
297 hr->h.size = sizeof(hr->h);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200298 }
299
300 uncopied_bytes = copy_to_user(puhr, hr, hr->h.size);
301 if (uncopied_bytes) {
302 HPI_DEBUG_LOG(ERROR, "uncopied bytes %d\n", uncopied_bytes);
303 err = -EFAULT;
304 goto out;
305 }
306
307out:
308 kfree(hm);
309 kfree(hr);
310 return err;
311}
312
313int __devinit asihpi_adapter_probe(struct pci_dev *pci_dev,
314 const struct pci_device_id *pci_id)
315{
Eliot Blennerhassett42258da2011-04-05 20:55:48 +1200316 int idx, nm;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200317 unsigned int memlen;
318 struct hpi_message hm;
319 struct hpi_response hr;
320 struct hpi_adapter adapter;
321 struct hpi_pci pci;
322
323 memset(&adapter, 0, sizeof(adapter));
324
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300325 dev_printk(KERN_DEBUG, &pci_dev->dev,
326 "probe %04x:%04x,%04x:%04x,%04x\n", pci_dev->vendor,
327 pci_dev->device, pci_dev->subsystem_vendor,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200328 pci_dev->subsystem_device, pci_dev->devfn);
329
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300330 if (pci_enable_device(pci_dev) < 0) {
331 dev_printk(KERN_ERR, &pci_dev->dev,
332 "pci_enable_device failed, disabling device\n");
333 return -EIO;
334 }
335
336 pci_set_master(pci_dev); /* also sets latency timer if < 16 */
337
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200338 hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
339 HPI_SUBSYS_CREATE_ADAPTER);
340 hpi_init_response(&hr, HPI_OBJ_SUBSYSTEM, HPI_SUBSYS_CREATE_ADAPTER,
341 HPI_ERROR_PROCESSING_MESSAGE);
342
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300343 hm.adapter_index = HPI_ADAPTER_INDEX_INVALID;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200344
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200345 adapter.pci = pci_dev;
346
347 nm = HPI_MAX_ADAPTER_MEM_SPACES;
348
349 for (idx = 0; idx < nm; idx++) {
Eliot Blennerhassett42258da2011-04-05 20:55:48 +1200350 HPI_DEBUG_LOG(INFO, "resource %d %pR\n", idx,
351 &pci_dev->resource[idx]);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200352
353 if (pci_resource_flags(pci_dev, idx) & IORESOURCE_MEM) {
354 memlen = pci_resource_len(pci_dev, idx);
355 adapter.ap_remapped_mem_base[idx] =
356 ioremap(pci_resource_start(pci_dev, idx),
357 memlen);
358 if (!adapter.ap_remapped_mem_base[idx]) {
359 HPI_DEBUG_LOG(ERROR,
360 "ioremap failed, aborting\n");
361 /* unmap previously mapped pci mem space */
362 goto err;
363 }
364 }
365
366 pci.ap_mem_base[idx] = adapter.ap_remapped_mem_base[idx];
367 }
368
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300369 pci.pci_dev = pci_dev;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200370 hm.u.s.resource.bus_type = HPI_BUS_PCI;
371 hm.u.s.resource.r.pci = &pci;
372
373 /* call CreateAdapterObject on the relevant hpi module */
374 hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
375 if (hr.error)
376 goto err;
377
378 if (prealloc_stream_buf) {
379 adapter.p_buffer = vmalloc(prealloc_stream_buf);
380 if (!adapter.p_buffer) {
381 HPI_DEBUG_LOG(ERROR,
382 "HPI could not allocate "
383 "kernel buffer size %d\n",
384 prealloc_stream_buf);
385 goto err;
386 }
387 }
388
389 adapter.index = hr.u.s.adapter_index;
Eliot Blennerhassett2f918a62011-02-10 17:26:09 +1300390 adapter.type = hr.u.s.adapter_type;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200391
Eliot Blennerhassett6d0b8982011-04-05 20:55:47 +1200392 hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
393 HPI_ADAPTER_OPEN);
394 hm.adapter_index = adapter.index;
395 hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
396
397 if (hr.error)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200398 goto err;
399
400 adapter.snd_card_asihpi = NULL;
401 /* WARNING can't init mutex in 'adapter'
402 * and then copy it to adapters[] ?!?!
403 */
Eliot Blennerhassett6d0b8982011-04-05 20:55:47 +1200404 adapters[adapter.index] = adapter;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200405 mutex_init(&adapters[adapter.index].mutex);
406 pci_set_drvdata(pci_dev, &adapters[adapter.index]);
407
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300408 dev_printk(KERN_INFO, &pci_dev->dev,
409 "probe succeeded for ASI%04X HPI index %d\n", adapter.type,
410 adapter.index);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200411
412 return 0;
413
414err:
415 for (idx = 0; idx < HPI_MAX_ADAPTER_MEM_SPACES; idx++) {
416 if (adapter.ap_remapped_mem_base[idx]) {
417 iounmap(adapter.ap_remapped_mem_base[idx]);
418 adapter.ap_remapped_mem_base[idx] = NULL;
419 }
420 }
421
422 if (adapter.p_buffer) {
423 adapter.buffer_size = 0;
424 vfree(adapter.p_buffer);
425 }
426
427 HPI_DEBUG_LOG(ERROR, "adapter_probe failed\n");
428 return -ENODEV;
429}
430
431void __devexit asihpi_adapter_remove(struct pci_dev *pci_dev)
432{
433 int idx;
434 struct hpi_message hm;
435 struct hpi_response hr;
436 struct hpi_adapter *pa;
Joe Perches5dbea6b2010-11-15 12:14:02 -0800437 pa = pci_get_drvdata(pci_dev);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200438
Eliot Blennerhassett6d0b8982011-04-05 20:55:47 +1200439 hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
440 HPI_ADAPTER_DELETE);
441 hm.adapter_index = pa->index;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200442 hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
443
444 /* unmap PCI memory space, mapped during device init. */
445 for (idx = 0; idx < HPI_MAX_ADAPTER_MEM_SPACES; idx++) {
446 if (pa->ap_remapped_mem_base[idx]) {
447 iounmap(pa->ap_remapped_mem_base[idx]);
448 pa->ap_remapped_mem_base[idx] = NULL;
449 }
450 }
451
Eliot Blennerhassettc188dec2011-02-10 17:26:18 +1300452 if (pa->p_buffer)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200453 vfree(pa->p_buffer);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200454
455 pci_set_drvdata(pci_dev, NULL);
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300456 if (1)
457 dev_printk(KERN_INFO, &pci_dev->dev,
458 "remove %04x:%04x,%04x:%04x,%04x," " HPI index %d.\n",
459 pci_dev->vendor, pci_dev->device,
460 pci_dev->subsystem_vendor, pci_dev->subsystem_device,
461 pci_dev->devfn, pa->index);
Eliot Blennerhassettc188dec2011-02-10 17:26:18 +1300462
463 memset(pa, 0, sizeof(*pa));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200464}
465
466void __init asihpi_init(void)
467{
468 struct hpi_message hm;
469 struct hpi_response hr;
470
471 memset(adapters, 0, sizeof(adapters));
472
Eliot Blennerhassett1dd6aaa2010-07-06 08:37:07 +1200473 printk(KERN_INFO "ASIHPI driver " HPI_VER_STRING "\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200474
475 hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
476 HPI_SUBSYS_DRIVER_LOAD);
477 hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
478}
479
480void asihpi_exit(void)
481{
482 struct hpi_message hm;
483 struct hpi_response hr;
484
485 hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
486 HPI_SUBSYS_DRIVER_UNLOAD);
487 hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
488}