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
42 static int prealloc_stream_buf;
43 module_param(prealloc_stream_buf, int, S_IRUGO);
44 MODULE_PARM_DESC(prealloc_stream_buf,
45 "Preallocate size for per-adapter stream buffer");
47 /* Allow the debug level to be changed after module load.
48 E.g. echo 2 > /sys/module/asihpi/parameters/hpiDebugLevel
50 module_param(hpi_debug_level, int, S_IRUGO | S_IWUSR);
51 MODULE_PARM_DESC(hpi_debug_level, "debug verbosity 0..5");
53 /* List of adapters found */
54 static struct hpi_adapter adapters[HPI_MAX_ADAPTERS];
56 /* Wrapper function to HPI_Message to enable dumping of the
57 message and response types.
59 static void hpi_send_recv_f(struct hpi_message *phm, struct hpi_response *phr,
62 int adapter = phm->adapter_index;
64 if ((adapter >= HPI_MAX_ADAPTERS || adapter < 0)
65 && (phm->object != HPI_OBJ_SUBSYSTEM))
66 phr->error = HPI_ERROR_INVALID_OBJ_INDEX;
68 hpi_send_recv_ex(phm, phr, file);
71 /* This is called from hpifunc.c functions, called by ALSA
72 * (or other kernel process) In this case there is no file descriptor
73 * available for the message cache code
75 void hpi_send_recv(struct hpi_message *phm, struct hpi_response *phr)
77 hpi_send_recv_f(phm, phr, HOWNER_KERNEL);
80 EXPORT_SYMBOL(hpi_send_recv);
81 /* for radio-asihpi */
83 int asihpi_hpi_release(struct file *file)
85 struct hpi_message hm;
86 struct hpi_response hr;
88 /* HPI_DEBUG_LOG(INFO,"hpi_release file %p, pid %d\n", file, current->pid); */
89 /* close the subsystem just in case the application forgot to. */
90 hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
92 hpi_send_recv_ex(&hm, &hr, file);
96 long asihpi_hpi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
98 struct hpi_ioctl_linux __user *phpi_ioctl_data;
101 union hpi_message_buffer_v1 *hm;
102 union hpi_response_buffer_v1 *hr;
107 if (cmd != HPI_IOCTL_LINUX)
110 hm = kmalloc(sizeof(*hm), GFP_KERNEL);
111 hr = kmalloc(sizeof(*hr), GFP_KERNEL);
117 phpi_ioctl_data = (struct hpi_ioctl_linux __user *)arg;
119 /* Read the message and response pointers from user space. */
120 if (get_user(puhm, &phpi_ioctl_data->phm)
121 || get_user(puhr, &phpi_ioctl_data->phr)) {
126 /* Now read the message size and data from user space. */
127 if (get_user(hm->h.size, (u16 __user *)puhm)) {
131 if (hm->h.size > sizeof(*hm))
132 hm->h.size = sizeof(*hm);
134 /* printk(KERN_INFO "message size %d\n", hm->h.wSize); */
136 uncopied_bytes = copy_from_user(hm, puhm, hm->h.size);
137 if (uncopied_bytes) {
138 HPI_DEBUG_LOG(ERROR, "uncopied bytes %d\n", uncopied_bytes);
143 if (get_user(res_max_size, (u16 __user *)puhr)) {
147 /* printk(KERN_INFO "user response size %d\n", res_max_size); */
148 if (res_max_size < sizeof(struct hpi_response_header)) {
149 HPI_DEBUG_LOG(WARNING, "small res size %d\n", res_max_size);
154 switch (hm->h.function) {
155 case HPI_SUBSYS_CREATE_ADAPTER:
156 case HPI_ADAPTER_DELETE:
157 /* Application must not use these functions! */
158 hr->h.size = sizeof(hr->h);
159 hr->h.error = HPI_ERROR_INVALID_OPERATION;
160 hr->h.function = hm->h.function;
161 uncopied_bytes = copy_to_user(puhr, hr, hr->h.size);
169 hr->h.size = res_max_size;
170 if (hm->h.object == HPI_OBJ_SUBSYSTEM) {
171 hpi_send_recv_f(&hm->m0, &hr->r0, file);
173 u16 __user *ptr = NULL;
176 /* -1=no data 0=read from user mem, 1=write to user mem */
178 struct hpi_adapter *pa;
180 if (hm->h.adapter_index < HPI_MAX_ADAPTERS) {
181 pa = &adapters[hm->h.adapter_index];
182 adapter_present = pa->type;
187 if (!adapter_present) {
188 hpi_init_response(&hr->r0, hm->h.object,
189 hm->h.function, HPI_ERROR_BAD_ADAPTER_NUMBER);
192 copy_to_user(puhr, hr, sizeof(hr->h));
200 if (mutex_lock_interruptible(&pa->mutex)) {
205 /* Dig out any pointers embedded in the message. */
206 switch (hm->h.function) {
207 case HPI_OSTREAM_WRITE:
208 case HPI_ISTREAM_READ:{
209 /* Yes, sparse, this is correct. */
210 ptr = (u16 __user *)hm->m0.u.d.u.data.pb_data;
211 size = hm->m0.u.d.u.data.data_size;
213 /* Allocate buffer according to application request.
214 ?Is it better to alloc/free for the duration
217 if (pa->buffer_size < size) {
219 "Realloc adapter %d stream "
220 "buffer from %zd to %d\n",
222 pa->buffer_size, size);
227 pa->p_buffer = vmalloc(size);
229 pa->buffer_size = size;
232 "HPI could not allocate "
233 "stream buffer size %d\n",
236 mutex_unlock(&pa->mutex);
242 hm->m0.u.d.u.data.pb_data = pa->p_buffer;
243 if (hm->h.function == HPI_ISTREAM_READ)
244 /* from card, WRITE to user mem */
256 if (size && (wrflag == 0)) {
258 copy_from_user(pa->p_buffer, ptr, size);
260 HPI_DEBUG_LOG(WARNING,
262 "bytes from user\n", uncopied_bytes,
266 hpi_send_recv_f(&hm->m0, &hr->r0, file);
268 if (size && (wrflag == 1)) {
270 copy_to_user(ptr, pa->p_buffer, size);
272 HPI_DEBUG_LOG(WARNING,
273 "Missed %d of %d " "bytes to user\n",
274 uncopied_bytes, size);
277 mutex_unlock(&pa->mutex);
280 /* on return response size must be set */
281 /*printk(KERN_INFO "response size %d\n", hr->h.wSize); */
284 HPI_DEBUG_LOG(ERROR, "response zero size\n");
289 if (hr->h.size > res_max_size) {
290 HPI_DEBUG_LOG(ERROR, "response too big %d %d\n", hr->h.size,
292 hr->h.error = HPI_ERROR_RESPONSE_BUFFER_TOO_SMALL;
293 hr->h.specific_error = hr->h.size;
294 hr->h.size = sizeof(hr->h);
297 uncopied_bytes = copy_to_user(puhr, hr, hr->h.size);
298 if (uncopied_bytes) {
299 HPI_DEBUG_LOG(ERROR, "uncopied bytes %d\n", uncopied_bytes);
310 int __devinit asihpi_adapter_probe(struct pci_dev *pci_dev,
311 const struct pci_device_id *pci_id)
315 struct hpi_message hm;
316 struct hpi_response hr;
317 struct hpi_adapter adapter;
320 memset(&adapter, 0, sizeof(adapter));
322 dev_printk(KERN_DEBUG, &pci_dev->dev,
323 "probe %04x:%04x,%04x:%04x,%04x\n", pci_dev->vendor,
324 pci_dev->device, pci_dev->subsystem_vendor,
325 pci_dev->subsystem_device, pci_dev->devfn);
327 if (pci_enable_device(pci_dev) < 0) {
328 dev_printk(KERN_ERR, &pci_dev->dev,
329 "pci_enable_device failed, disabling device\n");
333 pci_set_master(pci_dev); /* also sets latency timer if < 16 */
335 hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
336 HPI_SUBSYS_CREATE_ADAPTER);
337 hpi_init_response(&hr, HPI_OBJ_SUBSYSTEM, HPI_SUBSYS_CREATE_ADAPTER,
338 HPI_ERROR_PROCESSING_MESSAGE);
340 hm.adapter_index = HPI_ADAPTER_INDEX_INVALID;
342 adapter.pci = pci_dev;
344 nm = HPI_MAX_ADAPTER_MEM_SPACES;
346 for (idx = 0; idx < nm; idx++) {
347 HPI_DEBUG_LOG(INFO, "resource %d %pR\n", idx,
348 &pci_dev->resource[idx]);
350 if (pci_resource_flags(pci_dev, idx) & IORESOURCE_MEM) {
351 memlen = pci_resource_len(pci_dev, idx);
352 adapter.ap_remapped_mem_base[idx] =
353 ioremap(pci_resource_start(pci_dev, idx),
355 if (!adapter.ap_remapped_mem_base[idx]) {
357 "ioremap failed, aborting\n");
358 /* unmap previously mapped pci mem space */
363 pci.ap_mem_base[idx] = adapter.ap_remapped_mem_base[idx];
366 pci.pci_dev = pci_dev;
367 hm.u.s.resource.bus_type = HPI_BUS_PCI;
368 hm.u.s.resource.r.pci = &pci;
370 /* call CreateAdapterObject on the relevant hpi module */
371 hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
375 if (prealloc_stream_buf) {
376 adapter.p_buffer = vmalloc(prealloc_stream_buf);
377 if (!adapter.p_buffer) {
379 "HPI could not allocate "
380 "kernel buffer size %d\n",
381 prealloc_stream_buf);
386 adapter.index = hr.u.s.adapter_index;
387 adapter.type = hr.u.s.adapter_type;
389 hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
391 hm.adapter_index = adapter.index;
392 hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
397 adapter.snd_card_asihpi = NULL;
398 /* WARNING can't init mutex in 'adapter'
399 * and then copy it to adapters[] ?!?!
401 adapters[adapter.index] = adapter;
402 mutex_init(&adapters[adapter.index].mutex);
403 pci_set_drvdata(pci_dev, &adapters[adapter.index]);
405 dev_printk(KERN_INFO, &pci_dev->dev,
406 "probe succeeded for ASI%04X HPI index %d\n", adapter.type,
412 for (idx = 0; idx < HPI_MAX_ADAPTER_MEM_SPACES; idx++) {
413 if (adapter.ap_remapped_mem_base[idx]) {
414 iounmap(adapter.ap_remapped_mem_base[idx]);
415 adapter.ap_remapped_mem_base[idx] = NULL;
419 if (adapter.p_buffer) {
420 adapter.buffer_size = 0;
421 vfree(adapter.p_buffer);
424 HPI_DEBUG_LOG(ERROR, "adapter_probe failed\n");
428 void __devexit asihpi_adapter_remove(struct pci_dev *pci_dev)
431 struct hpi_message hm;
432 struct hpi_response hr;
433 struct hpi_adapter *pa;
434 pa = pci_get_drvdata(pci_dev);
436 hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
438 hm.adapter_index = pa->index;
439 hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
441 /* unmap PCI memory space, mapped during device init. */
442 for (idx = 0; idx < HPI_MAX_ADAPTER_MEM_SPACES; idx++) {
443 if (pa->ap_remapped_mem_base[idx]) {
444 iounmap(pa->ap_remapped_mem_base[idx]);
445 pa->ap_remapped_mem_base[idx] = NULL;
452 pci_set_drvdata(pci_dev, NULL);
454 dev_printk(KERN_INFO, &pci_dev->dev,
455 "remove %04x:%04x,%04x:%04x,%04x," " HPI index %d.\n",
456 pci_dev->vendor, pci_dev->device,
457 pci_dev->subsystem_vendor, pci_dev->subsystem_device,
458 pci_dev->devfn, pa->index);
460 memset(pa, 0, sizeof(*pa));
463 void __init asihpi_init(void)
465 struct hpi_message hm;
466 struct hpi_response hr;
468 memset(adapters, 0, sizeof(adapters));
470 printk(KERN_INFO "ASIHPI driver " HPI_VER_STRING "\n");
472 hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
473 HPI_SUBSYS_DRIVER_LOAD);
474 hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
477 void asihpi_exit(void)
479 struct hpi_message hm;
480 struct hpi_response hr;
482 hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
483 HPI_SUBSYS_DRIVER_UNLOAD);
484 hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);