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>
36 #include <linux/module.h>
38 #ifdef MODULE_FIRMWARE
39 MODULE_FIRMWARE("asihpi/dsp5000.bin");
40 MODULE_FIRMWARE("asihpi/dsp6200.bin");
41 MODULE_FIRMWARE("asihpi/dsp6205.bin");
42 MODULE_FIRMWARE("asihpi/dsp6400.bin");
43 MODULE_FIRMWARE("asihpi/dsp6600.bin");
44 MODULE_FIRMWARE("asihpi/dsp8700.bin");
45 MODULE_FIRMWARE("asihpi/dsp8900.bin");
48 static int prealloc_stream_buf
;
49 module_param(prealloc_stream_buf
, int, S_IRUGO
);
50 MODULE_PARM_DESC(prealloc_stream_buf
,
51 "Preallocate size for per-adapter stream buffer");
53 /* Allow the debug level to be changed after module load.
54 E.g. echo 2 > /sys/module/asihpi/parameters/hpiDebugLevel
56 module_param(hpi_debug_level
, int, S_IRUGO
| S_IWUSR
);
57 MODULE_PARM_DESC(hpi_debug_level
, "debug verbosity 0..5");
59 /* List of adapters found */
60 static struct hpi_adapter adapters
[HPI_MAX_ADAPTERS
];
62 /* Wrapper function to HPI_Message to enable dumping of the
63 message and response types.
65 static void hpi_send_recv_f(struct hpi_message
*phm
, struct hpi_response
*phr
,
68 int adapter
= phm
->adapter_index
;
70 if ((adapter
>= HPI_MAX_ADAPTERS
|| adapter
< 0)
71 && (phm
->object
!= HPI_OBJ_SUBSYSTEM
))
72 phr
->error
= HPI_ERROR_INVALID_OBJ_INDEX
;
74 hpi_send_recv_ex(phm
, phr
, file
);
77 /* This is called from hpifunc.c functions, called by ALSA
78 * (or other kernel process) In this case there is no file descriptor
79 * available for the message cache code
81 void hpi_send_recv(struct hpi_message
*phm
, struct hpi_response
*phr
)
83 hpi_send_recv_f(phm
, phr
, HOWNER_KERNEL
);
86 EXPORT_SYMBOL(hpi_send_recv
);
87 /* for radio-asihpi */
89 int asihpi_hpi_release(struct file
*file
)
91 struct hpi_message hm
;
92 struct hpi_response hr
;
94 /* HPI_DEBUG_LOG(INFO,"hpi_release file %p, pid %d\n", file, current->pid); */
95 /* close the subsystem just in case the application forgot to. */
96 hpi_init_message_response(&hm
, &hr
, HPI_OBJ_SUBSYSTEM
,
98 hpi_send_recv_ex(&hm
, &hr
, file
);
102 long asihpi_hpi_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
104 struct hpi_ioctl_linux __user
*phpi_ioctl_data
;
107 union hpi_message_buffer_v1
*hm
;
108 union hpi_response_buffer_v1
*hr
;
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 struct hpi_adapter
*pa
;
186 if (hm
->h
.adapter_index
< HPI_MAX_ADAPTERS
) {
187 pa
= &adapters
[hm
->h
.adapter_index
];
188 adapter_present
= pa
->type
;
193 if (!adapter_present
) {
194 hpi_init_response(&hr
->r0
, hm
->h
.object
,
195 hm
->h
.function
, HPI_ERROR_BAD_ADAPTER_NUMBER
);
198 copy_to_user(puhr
, hr
, sizeof(hr
->h
));
206 if (mutex_lock_interruptible(&pa
->mutex
)) {
211 /* Dig out any pointers embedded in the message. */
212 switch (hm
->h
.function
) {
213 case HPI_OSTREAM_WRITE
:
214 case HPI_ISTREAM_READ
:{
215 /* Yes, sparse, this is correct. */
216 ptr
= (u16 __user
*)hm
->m0
.u
.d
.u
.data
.pb_data
;
217 size
= hm
->m0
.u
.d
.u
.data
.data_size
;
219 /* Allocate buffer according to application request.
220 ?Is it better to alloc/free for the duration
223 if (pa
->buffer_size
< size
) {
225 "Realloc adapter %d stream "
226 "buffer from %zd to %d\n",
228 pa
->buffer_size
, size
);
233 pa
->p_buffer
= vmalloc(size
);
235 pa
->buffer_size
= size
;
238 "HPI could not allocate "
239 "stream buffer size %d\n",
242 mutex_unlock(&pa
->mutex
);
248 hm
->m0
.u
.d
.u
.data
.pb_data
= pa
->p_buffer
;
249 if (hm
->h
.function
== HPI_ISTREAM_READ
)
250 /* from card, WRITE to user mem */
262 if (size
&& (wrflag
== 0)) {
264 copy_from_user(pa
->p_buffer
, ptr
, size
);
266 HPI_DEBUG_LOG(WARNING
,
268 "bytes from user\n", uncopied_bytes
,
272 hpi_send_recv_f(&hm
->m0
, &hr
->r0
, file
);
274 if (size
&& (wrflag
== 1)) {
276 copy_to_user(ptr
, pa
->p_buffer
, size
);
278 HPI_DEBUG_LOG(WARNING
,
279 "Missed %d of %d " "bytes to user\n",
280 uncopied_bytes
, size
);
283 mutex_unlock(&pa
->mutex
);
286 /* on return response size must be set */
287 /*printk(KERN_INFO "response size %d\n", hr->h.wSize); */
290 HPI_DEBUG_LOG(ERROR
, "response zero size\n");
295 if (hr
->h
.size
> res_max_size
) {
296 HPI_DEBUG_LOG(ERROR
, "response too big %d %d\n", hr
->h
.size
,
298 hr
->h
.error
= HPI_ERROR_RESPONSE_BUFFER_TOO_SMALL
;
299 hr
->h
.specific_error
= hr
->h
.size
;
300 hr
->h
.size
= sizeof(hr
->h
);
303 uncopied_bytes
= copy_to_user(puhr
, hr
, hr
->h
.size
);
304 if (uncopied_bytes
) {
305 HPI_DEBUG_LOG(ERROR
, "uncopied bytes %d\n", uncopied_bytes
);
316 int __devinit
asihpi_adapter_probe(struct pci_dev
*pci_dev
,
317 const struct pci_device_id
*pci_id
)
321 struct hpi_message hm
;
322 struct hpi_response hr
;
323 struct hpi_adapter adapter
;
326 memset(&adapter
, 0, sizeof(adapter
));
328 dev_printk(KERN_DEBUG
, &pci_dev
->dev
,
329 "probe %04x:%04x,%04x:%04x,%04x\n", pci_dev
->vendor
,
330 pci_dev
->device
, pci_dev
->subsystem_vendor
,
331 pci_dev
->subsystem_device
, pci_dev
->devfn
);
333 if (pci_enable_device(pci_dev
) < 0) {
334 dev_printk(KERN_ERR
, &pci_dev
->dev
,
335 "pci_enable_device failed, disabling device\n");
339 pci_set_master(pci_dev
); /* also sets latency timer if < 16 */
341 hpi_init_message_response(&hm
, &hr
, HPI_OBJ_SUBSYSTEM
,
342 HPI_SUBSYS_CREATE_ADAPTER
);
343 hpi_init_response(&hr
, HPI_OBJ_SUBSYSTEM
, HPI_SUBSYS_CREATE_ADAPTER
,
344 HPI_ERROR_PROCESSING_MESSAGE
);
346 hm
.adapter_index
= HPI_ADAPTER_INDEX_INVALID
;
348 adapter
.pci
= pci_dev
;
350 nm
= HPI_MAX_ADAPTER_MEM_SPACES
;
352 for (idx
= 0; idx
< nm
; idx
++) {
353 HPI_DEBUG_LOG(INFO
, "resource %d %pR\n", idx
,
354 &pci_dev
->resource
[idx
]);
356 if (pci_resource_flags(pci_dev
, idx
) & IORESOURCE_MEM
) {
357 memlen
= pci_resource_len(pci_dev
, idx
);
358 adapter
.ap_remapped_mem_base
[idx
] =
359 ioremap(pci_resource_start(pci_dev
, idx
),
361 if (!adapter
.ap_remapped_mem_base
[idx
]) {
363 "ioremap failed, aborting\n");
364 /* unmap previously mapped pci mem space */
369 pci
.ap_mem_base
[idx
] = adapter
.ap_remapped_mem_base
[idx
];
372 pci
.pci_dev
= pci_dev
;
373 hm
.u
.s
.resource
.bus_type
= HPI_BUS_PCI
;
374 hm
.u
.s
.resource
.r
.pci
= &pci
;
376 /* call CreateAdapterObject on the relevant hpi module */
377 hpi_send_recv_ex(&hm
, &hr
, HOWNER_KERNEL
);
381 if (prealloc_stream_buf
) {
382 adapter
.p_buffer
= vmalloc(prealloc_stream_buf
);
383 if (!adapter
.p_buffer
) {
385 "HPI could not allocate "
386 "kernel buffer size %d\n",
387 prealloc_stream_buf
);
392 adapter
.index
= hr
.u
.s
.adapter_index
;
393 adapter
.type
= hr
.u
.s
.adapter_type
;
395 hpi_init_message_response(&hm
, &hr
, HPI_OBJ_ADAPTER
,
397 hm
.adapter_index
= adapter
.index
;
398 hpi_send_recv_ex(&hm
, &hr
, HOWNER_KERNEL
);
403 adapter
.snd_card_asihpi
= NULL
;
404 /* WARNING can't init mutex in 'adapter'
405 * and then copy it to adapters[] ?!?!
407 adapters
[adapter
.index
] = adapter
;
408 mutex_init(&adapters
[adapter
.index
].mutex
);
409 pci_set_drvdata(pci_dev
, &adapters
[adapter
.index
]);
411 dev_printk(KERN_INFO
, &pci_dev
->dev
,
412 "probe succeeded for ASI%04X HPI index %d\n", adapter
.type
,
418 for (idx
= 0; idx
< HPI_MAX_ADAPTER_MEM_SPACES
; idx
++) {
419 if (adapter
.ap_remapped_mem_base
[idx
]) {
420 iounmap(adapter
.ap_remapped_mem_base
[idx
]);
421 adapter
.ap_remapped_mem_base
[idx
] = NULL
;
425 if (adapter
.p_buffer
) {
426 adapter
.buffer_size
= 0;
427 vfree(adapter
.p_buffer
);
430 HPI_DEBUG_LOG(ERROR
, "adapter_probe failed\n");
434 void __devexit
asihpi_adapter_remove(struct pci_dev
*pci_dev
)
437 struct hpi_message hm
;
438 struct hpi_response hr
;
439 struct hpi_adapter
*pa
;
440 pa
= pci_get_drvdata(pci_dev
);
442 hpi_init_message_response(&hm
, &hr
, HPI_OBJ_ADAPTER
,
444 hm
.adapter_index
= pa
->index
;
445 hpi_send_recv_ex(&hm
, &hr
, HOWNER_KERNEL
);
447 /* unmap PCI memory space, mapped during device init. */
448 for (idx
= 0; idx
< HPI_MAX_ADAPTER_MEM_SPACES
; idx
++) {
449 if (pa
->ap_remapped_mem_base
[idx
]) {
450 iounmap(pa
->ap_remapped_mem_base
[idx
]);
451 pa
->ap_remapped_mem_base
[idx
] = NULL
;
458 pci_set_drvdata(pci_dev
, NULL
);
460 dev_printk(KERN_INFO
, &pci_dev
->dev
,
461 "remove %04x:%04x,%04x:%04x,%04x," " HPI index %d.\n",
462 pci_dev
->vendor
, pci_dev
->device
,
463 pci_dev
->subsystem_vendor
, pci_dev
->subsystem_device
,
464 pci_dev
->devfn
, pa
->index
);
466 memset(pa
, 0, sizeof(*pa
));
469 void __init
asihpi_init(void)
471 struct hpi_message hm
;
472 struct hpi_response hr
;
474 memset(adapters
, 0, sizeof(adapters
));
476 printk(KERN_INFO
"ASIHPI driver " HPI_VER_STRING
"\n");
478 hpi_init_message_response(&hm
, &hr
, HPI_OBJ_SUBSYSTEM
,
479 HPI_SUBSYS_DRIVER_LOAD
);
480 hpi_send_recv_ex(&hm
, &hr
, HOWNER_KERNEL
);
483 void asihpi_exit(void)
485 struct hpi_message hm
;
486 struct hpi_response hr
;
488 hpi_init_message_response(&hm
, &hr
, HPI_OBJ_SUBSYSTEM
,
489 HPI_SUBSYS_DRIVER_UNLOAD
);
490 hpi_send_recv_ex(&hm
, &hr
, HOWNER_KERNEL
);