1 /*******************************************************************************
3 AudioScience HPI driver
4 Copyright (C) 1997-2011 AudioScience Inc. <support@audioscience.com>
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;
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.
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
19 Common Linux HPI ioctl and module probe/remove functions
20 *******************************************************************************/
21 #define SOURCEFILE_NAME "hpioctl.c"
23 #include "hpi_internal.h"
24 #include "hpimsginit.h"
31 #include <linux/slab.h>
32 #include <linux/moduleparam.h>
33 #include <asm/uaccess.h>
34 #include <linux/pci.h>
35 #include <linux/stringify.h>
37 #ifdef MODULE_FIRMWARE
38 MODULE_FIRMWARE("asihpi/dsp5000.bin");
39 MODULE_FIRMWARE("asihpi/dsp6200.bin");
40 MODULE_FIRMWARE("asihpi/dsp6205.bin");
41 MODULE_FIRMWARE("asihpi/dsp6400.bin");
42 MODULE_FIRMWARE("asihpi/dsp6600.bin");
43 MODULE_FIRMWARE("asihpi/dsp8700.bin");
44 MODULE_FIRMWARE("asihpi/dsp8900.bin");
47 static int prealloc_stream_buf;
48 module_param(prealloc_stream_buf, int, S_IRUGO);
49 MODULE_PARM_DESC(prealloc_stream_buf,
50 "Preallocate size for per-adapter stream buffer");
52 /* Allow the debug level to be changed after module load.
53 E.g. echo 2 > /sys/module/asihpi/parameters/hpiDebugLevel
55 module_param(hpi_debug_level, int, S_IRUGO | S_IWUSR);
56 MODULE_PARM_DESC(hpi_debug_level, "debug verbosity 0..5");
58 /* List of adapters found */
59 static struct hpi_adapter adapters[HPI_MAX_ADAPTERS];
61 /* Wrapper function to HPI_Message to enable dumping of the
62 message and response types.
64 static void hpi_send_recv_f(struct hpi_message *phm, struct hpi_response *phr,
67 int adapter = phm->adapter_index;
69 if ((adapter >= HPI_MAX_ADAPTERS || adapter < 0)
70 && (phm->object != HPI_OBJ_SUBSYSTEM))
71 phr->error = HPI_ERROR_INVALID_OBJ_INDEX;
73 hpi_send_recv_ex(phm, phr, file);
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
80 void hpi_send_recv(struct hpi_message *phm, struct hpi_response *phr)
82 hpi_send_recv_f(phm, phr, HOWNER_KERNEL);
85 EXPORT_SYMBOL(hpi_send_recv);
86 /* for radio-asihpi */
88 int asihpi_hpi_release(struct file *file)
90 struct hpi_message hm;
91 struct hpi_response hr;
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,
97 hpi_send_recv_ex(&hm, &hr, file);
101 long asihpi_hpi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
103 struct hpi_ioctl_linux __user *phpi_ioctl_data;
106 union hpi_message_buffer_v1 *hm;
107 union hpi_response_buffer_v1 *hr;
110 struct hpi_adapter *pa = NULL;
113 if (cmd != HPI_IOCTL_LINUX)
116 hm = kmalloc(sizeof(*hm), GFP_KERNEL);
117 hr = kmalloc(sizeof(*hr), GFP_KERNEL);
123 phpi_ioctl_data = (struct hpi_ioctl_linux __user *)arg;
125 /* Read the message and response pointers from user space. */
126 if (get_user(puhm, &phpi_ioctl_data->phm)
127 || get_user(puhr, &phpi_ioctl_data->phr)) {
132 /* Now read the message size and data from user space. */
133 if (get_user(hm->h.size, (u16 __user *)puhm)) {
137 if (hm->h.size > sizeof(*hm))
138 hm->h.size = sizeof(*hm);
140 /* printk(KERN_INFO "message size %d\n", hm->h.wSize); */
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);
149 if (get_user(res_max_size, (u16 __user *)puhr)) {
153 /* 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);
160 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);
175 hr->h.size = res_max_size;
176 if (hm->h.object == HPI_OBJ_SUBSYSTEM) {
177 hpi_send_recv_f(&hm->m0, &hr->r0, file);
179 u16 __user *ptr = NULL;
182 /* -1=no data 0=read from user mem, 1=write to user mem */
184 u32 adapter = hm->h.adapter_index;
186 if ((adapter > HPI_MAX_ADAPTERS) || (!pa->type)) {
187 hpi_init_response(&hr->r0, HPI_OBJ_ADAPTER,
189 HPI_ERROR_BAD_ADAPTER_NUMBER);
192 copy_to_user(puhr, hr, sizeof(hr->h));
200 pa = &adapters[adapter];
202 if (mutex_lock_interruptible(&adapters[adapter].mutex)) {
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;
215 /* Allocate buffer according to application request.
216 ?Is it better to alloc/free for the duration
219 if (pa->buffer_size < size) {
221 "Realloc adapter %d stream "
222 "buffer from %zd to %d\n",
224 pa->buffer_size, size);
229 pa->p_buffer = vmalloc(size);
231 pa->buffer_size = size;
234 "HPI could not allocate "
235 "stream buffer size %d\n",
238 mutex_unlock(&adapters
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 */
259 if (size && (wrflag == 0)) {
261 copy_from_user(pa->p_buffer, ptr, size);
263 HPI_DEBUG_LOG(WARNING,
265 "bytes from user\n", uncopied_bytes,
269 hpi_send_recv_f(&hm->m0, &hr->r0, file);
271 if (size && (wrflag == 1)) {
273 copy_to_user(ptr, pa->p_buffer, size);
275 HPI_DEBUG_LOG(WARNING,
276 "Missed %d of %d " "bytes to user\n",
277 uncopied_bytes, size);
280 mutex_unlock(&adapters[adapter].mutex);
283 /* on return response size must be set */
284 /*printk(KERN_INFO "response size %d\n", hr->h.wSize); */
287 HPI_DEBUG_LOG(ERROR, "response zero size\n");
292 if (hr->h.size > res_max_size) {
293 HPI_DEBUG_LOG(ERROR, "response too big %d %d\n", hr->h.size,
295 hr->h.error = HPI_ERROR_RESPONSE_BUFFER_TOO_SMALL;
296 hr->h.specific_error = hr->h.size;
297 hr->h.size = sizeof(hr->h);
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);
313 int __devinit asihpi_adapter_probe(struct pci_dev *pci_dev,
314 const struct pci_device_id *pci_id)
318 struct hpi_message hm;
319 struct hpi_response hr;
320 struct hpi_adapter adapter;
323 memset(&adapter, 0, sizeof(adapter));
325 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,
328 pci_dev->subsystem_device, pci_dev->devfn);
330 if (pci_enable_device(pci_dev) < 0) {
331 dev_printk(KERN_ERR, &pci_dev->dev,
332 "pci_enable_device failed, disabling device\n");
336 pci_set_master(pci_dev); /* also sets latency timer if < 16 */
338 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);
343 hm.adapter_index = HPI_ADAPTER_INDEX_INVALID;
345 adapter.pci = pci_dev;
347 nm = HPI_MAX_ADAPTER_MEM_SPACES;
349 for (idx = 0; idx < nm; idx++) {
350 HPI_DEBUG_LOG(INFO, "resource %d %pR\n", idx,
351 &pci_dev->resource[idx]);
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),
358 if (!adapter.ap_remapped_mem_base[idx]) {
360 "ioremap failed, aborting\n");
361 /* unmap previously mapped pci mem space */
366 pci.ap_mem_base[idx] = adapter.ap_remapped_mem_base[idx];
369 pci.pci_dev = pci_dev;
370 hm.u.s.resource.bus_type = HPI_BUS_PCI;
371 hm.u.s.resource.r.pci = &pci;
373 /* call CreateAdapterObject on the relevant hpi module */
374 hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
378 if (prealloc_stream_buf) {
379 adapter.p_buffer = vmalloc(prealloc_stream_buf);
380 if (!adapter.p_buffer) {
382 "HPI could not allocate "
383 "kernel buffer size %d\n",
384 prealloc_stream_buf);
389 adapter.index = hr.u.s.adapter_index;
390 adapter.type = hr.u.s.adapter_type;
392 hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
394 hm.adapter_index = adapter.index;
395 hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
400 adapter.snd_card_asihpi = NULL;
401 /* WARNING can't init mutex in 'adapter'
402 * and then copy it to adapters[] ?!?!
404 adapters[adapter.index] = adapter;
405 mutex_init(&adapters[adapter.index].mutex);
406 pci_set_drvdata(pci_dev, &adapters[adapter.index]);
408 dev_printk(KERN_INFO, &pci_dev->dev,
409 "probe succeeded for ASI%04X HPI index %d\n", adapter.type,
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;
422 if (adapter.p_buffer) {
423 adapter.buffer_size = 0;
424 vfree(adapter.p_buffer);
427 HPI_DEBUG_LOG(ERROR, "adapter_probe failed\n");
431 void __devexit asihpi_adapter_remove(struct pci_dev *pci_dev)
434 struct hpi_message hm;
435 struct hpi_response hr;
436 struct hpi_adapter *pa;
437 pa = pci_get_drvdata(pci_dev);
439 hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
441 hm.adapter_index = pa->index;
442 hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
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;
455 pci_set_drvdata(pci_dev, NULL);
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);
463 memset(pa, 0, sizeof(*pa));
466 void __init asihpi_init(void)
468 struct hpi_message hm;
469 struct hpi_response hr;
471 memset(adapters, 0, sizeof(adapters));
473 printk(KERN_INFO "ASIHPI driver " HPI_VER_STRING "\n");
475 hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
476 HPI_SUBSYS_DRIVER_LOAD);
477 hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
480 void asihpi_exit(void)
482 struct hpi_message hm;
483 struct hpi_response hr;
485 hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
486 HPI_SUBSYS_DRIVER_UNLOAD);
487 hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);