1 /*******************************************************************************
3 AudioScience HPI driver
4 Copyright (C) 1997-2010 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"
30 #include <linux/slab.h>
31 #include <linux/moduleparam.h>
32 #include <asm/uaccess.h>
33 #include <linux/stringify.h>
35 #ifdef MODULE_FIRMWARE
36 MODULE_FIRMWARE("asihpi/dsp5000.bin");
37 MODULE_FIRMWARE("asihpi/dsp6200.bin");
38 MODULE_FIRMWARE("asihpi/dsp6205.bin");
39 MODULE_FIRMWARE("asihpi/dsp6400.bin");
40 MODULE_FIRMWARE("asihpi/dsp6600.bin");
41 MODULE_FIRMWARE("asihpi/dsp8700.bin");
42 MODULE_FIRMWARE("asihpi/dsp8900.bin");
45 static int prealloc_stream_buf
;
46 module_param(prealloc_stream_buf
, int, S_IRUGO
);
47 MODULE_PARM_DESC(prealloc_stream_buf
,
48 "preallocate size for per-adapter stream buffer");
50 /* Allow the debug level to be changed after module load.
51 E.g. echo 2 > /sys/module/asihpi/parameters/hpiDebugLevel
53 module_param(hpi_debug_level
, int, S_IRUGO
| S_IWUSR
);
54 MODULE_PARM_DESC(hpi_debug_level
, "debug verbosity 0..5");
56 /* List of adapters found */
57 static struct hpi_adapter adapters
[HPI_MAX_ADAPTERS
];
59 /* Wrapper function to HPI_Message to enable dumping of the
60 message and response types.
62 static void hpi_send_recv_f(struct hpi_message
*phm
, struct hpi_response
*phr
,
65 int adapter
= phm
->adapter_index
;
67 if ((adapter
>= HPI_MAX_ADAPTERS
|| adapter
< 0)
68 && (phm
->object
!= HPI_OBJ_SUBSYSTEM
))
69 phr
->error
= HPI_ERROR_INVALID_OBJ_INDEX
;
71 hpi_send_recv_ex(phm
, phr
, file
);
74 /* This is called from hpifunc.c functions, called by ALSA
75 * (or other kernel process) In this case there is no file descriptor
76 * available for the message cache code
78 void hpi_send_recv(struct hpi_message
*phm
, struct hpi_response
*phr
)
80 hpi_send_recv_f(phm
, phr
, HOWNER_KERNEL
);
83 EXPORT_SYMBOL(hpi_send_recv
);
84 /* for radio-asihpi */
86 int asihpi_hpi_release(struct file
*file
)
88 struct hpi_message hm
;
89 struct hpi_response hr
;
91 /* HPI_DEBUG_LOG(INFO,"hpi_release file %p, pid %d\n", file, current->pid); */
92 /* close the subsystem just in case the application forgot to. */
93 hpi_init_message_response(&hm
, &hr
, HPI_OBJ_SUBSYSTEM
,
95 hpi_send_recv_ex(&hm
, &hr
, file
);
99 long asihpi_hpi_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
101 struct hpi_ioctl_linux __user
*phpi_ioctl_data
;
104 union hpi_message_buffer_v1
*hm
;
105 union hpi_response_buffer_v1
*hr
;
108 struct hpi_adapter
*pa
= NULL
;
111 if (cmd
!= HPI_IOCTL_LINUX
)
114 hm
= kmalloc(sizeof(*hm
), GFP_KERNEL
);
115 hr
= kmalloc(sizeof(*hr
), GFP_KERNEL
);
121 phpi_ioctl_data
= (struct hpi_ioctl_linux __user
*)arg
;
123 /* Read the message and response pointers from user space. */
124 get_user(puhm
, &phpi_ioctl_data
->phm
);
125 get_user(puhr
, &phpi_ioctl_data
->phr
);
127 /* Now read the message size and data from user space. */
128 get_user(hm
->h
.size
, (u16 __user
*)puhm
);
129 if (hm
->h
.size
> sizeof(*hm
))
130 hm
->h
.size
= sizeof(*hm
);
132 /*printk(KERN_INFO "message size %d\n", hm->h.wSize); */
134 uncopied_bytes
= copy_from_user(hm
, puhm
, hm
->h
.size
);
135 if (uncopied_bytes
) {
136 HPI_DEBUG_LOG(ERROR
, "uncopied bytes %d\n", uncopied_bytes
);
141 get_user(res_max_size
, (u16 __user
*)puhr
);
142 /* printk(KERN_INFO "user response size %d\n", res_max_size); */
143 if (res_max_size
< sizeof(struct hpi_response_header
)) {
144 HPI_DEBUG_LOG(WARNING
, "small res size %d\n", res_max_size
);
149 pa
= &adapters
[hm
->h
.adapter_index
];
151 if (hm
->h
.object
== HPI_OBJ_SUBSYSTEM
) {
152 switch (hm
->h
.function
) {
153 case HPI_SUBSYS_CREATE_ADAPTER
:
154 case HPI_SUBSYS_DELETE_ADAPTER
:
155 /* Application must not use these functions! */
156 hr
->h
.size
= sizeof(hr
->h
);
157 hr
->h
.error
= HPI_ERROR_INVALID_OPERATION
;
158 hr
->h
.function
= hm
->h
.function
;
159 uncopied_bytes
= copy_to_user(puhr
, hr
, hr
->h
.size
);
167 hpi_send_recv_f(&hm
->m0
, &hr
->r0
, file
);
170 u16 __user
*ptr
= NULL
;
173 /* -1=no data 0=read from user mem, 1=write to user mem */
175 u32 adapter
= hm
->h
.adapter_index
;
177 if ((hm
->h
.adapter_index
> HPI_MAX_ADAPTERS
) || (!pa
->type
)) {
178 hpi_init_response(&hr
->r0
, HPI_OBJ_ADAPTER
,
180 HPI_ERROR_BAD_ADAPTER_NUMBER
);
183 copy_to_user(puhr
, hr
, sizeof(hr
->h
));
191 if (mutex_lock_interruptible(&adapters
[adapter
].mutex
)) {
196 /* Dig out any pointers embedded in the message. */
197 switch (hm
->h
.function
) {
198 case HPI_OSTREAM_WRITE
:
199 case HPI_ISTREAM_READ
:{
200 /* Yes, sparse, this is correct. */
201 ptr
= (u16 __user
*)hm
->m0
.u
.d
.u
.data
.pb_data
;
202 size
= hm
->m0
.u
.d
.u
.data
.data_size
;
204 /* Allocate buffer according to application request.
205 ?Is it better to alloc/free for the duration
208 if (pa
->buffer_size
< size
) {
210 "realloc adapter %d stream "
211 "buffer from %zd to %d\n",
213 pa
->buffer_size
, size
);
218 pa
->p_buffer
= vmalloc(size
);
220 pa
->buffer_size
= size
;
223 "HPI could not allocate "
224 "stream buffer size %d\n",
227 mutex_unlock(&adapters
234 hm
->m0
.u
.d
.u
.data
.pb_data
= pa
->p_buffer
;
235 if (hm
->h
.function
== HPI_ISTREAM_READ
)
236 /* from card, WRITE to user mem */
248 if (size
&& (wrflag
== 0)) {
250 copy_from_user(pa
->p_buffer
, ptr
, size
);
252 HPI_DEBUG_LOG(WARNING
,
254 "bytes from user\n", uncopied_bytes
,
258 hpi_send_recv_f(&hm
->m0
, &hr
->r0
, file
);
260 if (size
&& (wrflag
== 1)) {
262 copy_to_user(ptr
, pa
->p_buffer
, size
);
264 HPI_DEBUG_LOG(WARNING
,
265 "missed %d of %d " "bytes to user\n",
266 uncopied_bytes
, size
);
269 mutex_unlock(&adapters
[adapter
].mutex
);
272 /* on return response size must be set */
273 /*printk(KERN_INFO "response size %d\n", hr->h.wSize); */
276 HPI_DEBUG_LOG(ERROR
, "response zero size\n");
281 if (hr
->h
.size
> res_max_size
) {
282 HPI_DEBUG_LOG(ERROR
, "response too big %d %d\n", hr
->h
.size
,
284 /*HPI_DEBUG_MESSAGE(ERROR, hm); */
289 uncopied_bytes
= copy_to_user(puhr
, hr
, hr
->h
.size
);
290 if (uncopied_bytes
) {
291 HPI_DEBUG_LOG(ERROR
, "uncopied bytes %d\n", uncopied_bytes
);
302 int __devinit
asihpi_adapter_probe(struct pci_dev
*pci_dev
,
303 const struct pci_device_id
*pci_id
)
307 struct hpi_message hm
;
308 struct hpi_response hr
;
309 struct hpi_adapter adapter
;
312 memset(&adapter
, 0, sizeof(adapter
));
314 printk(KERN_DEBUG
"probe PCI device (%04x:%04x,%04x:%04x,%04x)\n",
315 pci_dev
->vendor
, pci_dev
->device
, pci_dev
->subsystem_vendor
,
316 pci_dev
->subsystem_device
, pci_dev
->devfn
);
318 hpi_init_message_response(&hm
, &hr
, HPI_OBJ_SUBSYSTEM
,
319 HPI_SUBSYS_CREATE_ADAPTER
);
320 hpi_init_response(&hr
, HPI_OBJ_SUBSYSTEM
, HPI_SUBSYS_CREATE_ADAPTER
,
321 HPI_ERROR_PROCESSING_MESSAGE
);
323 hm
.adapter_index
= -1; /* an invalid index */
325 /* fill in HPI_PCI information from kernel provided information */
326 adapter
.pci
= pci_dev
;
328 nm
= HPI_MAX_ADAPTER_MEM_SPACES
;
330 for (idx
= 0; idx
< nm
; idx
++) {
331 HPI_DEBUG_LOG(INFO
, "resource %d %s %08llx-%08llx %04llx\n",
332 idx
, pci_dev
->resource
[idx
].name
,
333 (unsigned long long)pci_resource_start(pci_dev
, idx
),
334 (unsigned long long)pci_resource_end(pci_dev
, idx
),
335 (unsigned long long)pci_resource_flags(pci_dev
, idx
));
337 if (pci_resource_flags(pci_dev
, idx
) & IORESOURCE_MEM
) {
338 memlen
= pci_resource_len(pci_dev
, idx
);
339 adapter
.ap_remapped_mem_base
[idx
] =
340 ioremap(pci_resource_start(pci_dev
, idx
),
342 if (!adapter
.ap_remapped_mem_base
[idx
]) {
344 "ioremap failed, aborting\n");
345 /* unmap previously mapped pci mem space */
350 pci
.ap_mem_base
[idx
] = adapter
.ap_remapped_mem_base
[idx
];
353 /* could replace Pci with direct pointer to pci_dev for linux
354 Instead wrap accessor functions for IDs etc.
355 Would it work for windows?
357 pci
.bus_number
= pci_dev
->bus
->number
;
358 pci
.vendor_id
= (u16
)pci_dev
->vendor
;
359 pci
.device_id
= (u16
)pci_dev
->device
;
360 pci
.subsys_vendor_id
= (u16
)(pci_dev
->subsystem_vendor
& 0xffff);
361 pci
.subsys_device_id
= (u16
)(pci_dev
->subsystem_device
& 0xffff);
362 pci
.device_number
= pci_dev
->devfn
;
363 pci
.interrupt
= pci_dev
->irq
;
364 pci
.p_os_data
= pci_dev
;
366 hm
.u
.s
.resource
.bus_type
= HPI_BUS_PCI
;
367 hm
.u
.s
.resource
.r
.pci
= &pci
;
369 /* call CreateAdapterObject on the relevant hpi module */
370 hpi_send_recv_ex(&hm
, &hr
, HOWNER_KERNEL
);
374 if (prealloc_stream_buf
) {
375 adapter
.p_buffer
= vmalloc(prealloc_stream_buf
);
376 if (!adapter
.p_buffer
) {
378 "HPI could not allocate "
379 "kernel buffer size %d\n",
380 prealloc_stream_buf
);
385 adapter
.index
= hr
.u
.s
.adapter_index
;
386 adapter
.type
= hr
.u
.s
.aw_adapter_list
[adapter
.index
];
387 hm
.adapter_index
= adapter
.index
;
389 err
= hpi_adapter_open(NULL
, adapter
.index
);
393 adapter
.snd_card_asihpi
= NULL
;
394 /* WARNING can't init mutex in 'adapter'
395 * and then copy it to adapters[] ?!?!
397 adapters
[hr
.u
.s
.adapter_index
] = adapter
;
398 mutex_init(&adapters
[adapter
.index
].mutex
);
399 pci_set_drvdata(pci_dev
, &adapters
[adapter
.index
]);
401 printk(KERN_INFO
"probe found adapter ASI%04X HPI index #%d.\n",
402 adapter
.type
, adapter
.index
);
407 for (idx
= 0; idx
< HPI_MAX_ADAPTER_MEM_SPACES
; idx
++) {
408 if (adapter
.ap_remapped_mem_base
[idx
]) {
409 iounmap(adapter
.ap_remapped_mem_base
[idx
]);
410 adapter
.ap_remapped_mem_base
[idx
] = NULL
;
414 if (adapter
.p_buffer
) {
415 adapter
.buffer_size
= 0;
416 vfree(adapter
.p_buffer
);
419 HPI_DEBUG_LOG(ERROR
, "adapter_probe failed\n");
423 void __devexit
asihpi_adapter_remove(struct pci_dev
*pci_dev
)
426 struct hpi_message hm
;
427 struct hpi_response hr
;
428 struct hpi_adapter
*pa
;
429 pa
= (struct hpi_adapter
*)pci_get_drvdata(pci_dev
);
431 hpi_init_message_response(&hm
, &hr
, HPI_OBJ_SUBSYSTEM
,
432 HPI_SUBSYS_DELETE_ADAPTER
);
433 hm
.adapter_index
= pa
->index
;
434 hpi_send_recv_ex(&hm
, &hr
, HOWNER_KERNEL
);
436 /* unmap PCI memory space, mapped during device init. */
437 for (idx
= 0; idx
< HPI_MAX_ADAPTER_MEM_SPACES
; idx
++) {
438 if (pa
->ap_remapped_mem_base
[idx
]) {
439 iounmap(pa
->ap_remapped_mem_base
[idx
]);
440 pa
->ap_remapped_mem_base
[idx
] = NULL
;
449 pci_set_drvdata(pci_dev
, NULL
);
451 printk(KERN_INFO "PCI device (%04x:%04x,%04x:%04x,%04x),"
452 " HPI index # %d, removed.\n",
453 pci_dev->vendor, pci_dev->device,
454 pci_dev->subsystem_vendor,
455 pci_dev->subsystem_device, pci_dev->devfn,
460 void __init
asihpi_init(void)
462 struct hpi_message hm
;
463 struct hpi_response hr
;
465 memset(adapters
, 0, sizeof(adapters
));
467 printk(KERN_INFO
"ASIHPI driver %d.%02d.%02d\n",
468 HPI_VER_MAJOR(HPI_VER
), HPI_VER_MINOR(HPI_VER
),
469 HPI_VER_RELEASE(HPI_VER
));
471 hpi_init_message_response(&hm
, &hr
, HPI_OBJ_SUBSYSTEM
,
472 HPI_SUBSYS_DRIVER_LOAD
);
473 hpi_send_recv_ex(&hm
, &hr
, HOWNER_KERNEL
);
476 void asihpi_exit(void)
478 struct hpi_message hm
;
479 struct hpi_response hr
;
481 hpi_init_message_response(&hm
, &hr
, HPI_OBJ_SUBSYSTEM
,
482 HPI_SUBSYS_DRIVER_UNLOAD
);
483 hpi_send_recv_ex(&hm
, &hr
, HOWNER_KERNEL
);