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 "hpi_version.h"
25 #include "hpimsginit.h"
32 #include <linux/slab.h>
33 #include <linux/moduleparam.h>
34 #include <asm/uaccess.h>
35 #include <linux/pci.h>
36 #include <linux/stringify.h>
37 #include <linux/module.h>
39 #ifdef MODULE_FIRMWARE
43 static int prealloc_stream_buf;
44 module_param(prealloc_stream_buf, int, S_IRUGO);
45 MODULE_PARM_DESC(prealloc_stream_buf,
46 "Preallocate size for per-adapter stream buffer");
48 /* Allow the debug level to be changed after module load.
49 E.g. echo 2 > /sys/module/asihpi/parameters/hpiDebugLevel
51 module_param(hpi_debug_level, int, S_IRUGO | S_IWUSR);
52 MODULE_PARM_DESC(hpi_debug_level, "debug verbosity 0..5");
54 /* List of adapters found */
55 static struct hpi_adapter adapters[HPI_MAX_ADAPTERS];
57 /* Wrapper function to HPI_Message to enable dumping of the
58 message and response types.
60 static void hpi_send_recv_f(struct hpi_message *phm, struct hpi_response *phr,
63 if ((phm->adapter_index >= HPI_MAX_ADAPTERS)
64 && (phm->object != HPI_OBJ_SUBSYSTEM))
65 phr->error = HPI_ERROR_INVALID_OBJ_INDEX;
67 hpi_send_recv_ex(phm, phr, file);
70 /* This is called from hpifunc.c functions, called by ALSA
71 * (or other kernel process) In this case there is no file descriptor
72 * available for the message cache code
74 void hpi_send_recv(struct hpi_message *phm, struct hpi_response *phr)
76 hpi_send_recv_f(phm, phr, HOWNER_KERNEL);
79 EXPORT_SYMBOL(hpi_send_recv);
80 /* for radio-asihpi */
82 int asihpi_hpi_release(struct file *file)
84 struct hpi_message hm;
85 struct hpi_response hr;
87 /* HPI_DEBUG_LOG(INFO,"hpi_release file %p, pid %d\n", file, current->pid); */
88 /* close the subsystem just in case the application forgot to. */
89 hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
91 hpi_send_recv_ex(&hm, &hr, file);
95 long asihpi_hpi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
97 struct hpi_ioctl_linux __user *phpi_ioctl_data;
100 union hpi_message_buffer_v1 *hm;
101 union hpi_response_buffer_v1 *hr;
106 if (cmd != HPI_IOCTL_LINUX)
109 hm = kmalloc(sizeof(*hm), GFP_KERNEL);
110 hr = kmalloc(sizeof(*hr), GFP_KERNEL);
116 phpi_ioctl_data = (struct hpi_ioctl_linux __user *)arg;
118 /* Read the message and response pointers from user space. */
119 if (get_user(puhm, &phpi_ioctl_data->phm)
120 || get_user(puhr, &phpi_ioctl_data->phr)) {
125 /* Now read the message size and data from user space. */
126 if (get_user(hm->h.size, (u16 __user *)puhm)) {
130 if (hm->h.size > sizeof(*hm))
131 hm->h.size = sizeof(*hm);
133 /* printk(KERN_INFO "message size %d\n", hm->h.wSize); */
135 uncopied_bytes = copy_from_user(hm, puhm, hm->h.size);
136 if (uncopied_bytes) {
137 HPI_DEBUG_LOG(ERROR, "uncopied bytes %d\n", uncopied_bytes);
142 if (get_user(res_max_size, (u16 __user *)puhr)) {
146 /* printk(KERN_INFO "user response size %d\n", res_max_size); */
147 if (res_max_size < sizeof(struct hpi_response_header)) {
148 HPI_DEBUG_LOG(WARNING, "small res size %d\n", res_max_size);
153 switch (hm->h.function) {
154 case HPI_SUBSYS_CREATE_ADAPTER:
155 case HPI_ADAPTER_DELETE:
156 /* Application must not use these functions! */
157 hr->h.size = sizeof(hr->h);
158 hr->h.error = HPI_ERROR_INVALID_OPERATION;
159 hr->h.function = hm->h.function;
160 uncopied_bytes = copy_to_user(puhr, hr, hr->h.size);
168 hr->h.size = res_max_size;
169 if (hm->h.object == HPI_OBJ_SUBSYSTEM) {
170 hpi_send_recv_f(&hm->m0, &hr->r0, file);
172 u16 __user *ptr = NULL;
174 /* -1=no data 0=read from user mem, 1=write to user mem */
176 struct hpi_adapter *pa = NULL;
178 if (hm->h.adapter_index < ARRAY_SIZE(adapters))
179 pa = &adapters[hm->h.adapter_index];
181 if (!pa || !pa->adapter || !pa->adapter->type) {
182 hpi_init_response(&hr->r0, hm->h.object,
183 hm->h.function, HPI_ERROR_BAD_ADAPTER_NUMBER);
186 copy_to_user(puhr, hr, sizeof(hr->h));
194 if (mutex_lock_interruptible(&pa->mutex)) {
199 /* Dig out any pointers embedded in the message. */
200 switch (hm->h.function) {
201 case HPI_OSTREAM_WRITE:
202 case HPI_ISTREAM_READ:{
203 /* Yes, sparse, this is correct. */
204 ptr = (u16 __user *)hm->m0.u.d.u.data.pb_data;
205 size = hm->m0.u.d.u.data.data_size;
207 /* Allocate buffer according to application request.
208 ?Is it better to alloc/free for the duration
211 if (pa->buffer_size < size) {
213 "Realloc adapter %d stream "
214 "buffer from %zd to %d\n",
216 pa->buffer_size, size);
221 pa->p_buffer = vmalloc(size);
223 pa->buffer_size = size;
226 "HPI could not allocate "
227 "stream buffer size %d\n",
230 mutex_unlock(&pa->mutex);
236 hm->m0.u.d.u.data.pb_data = pa->p_buffer;
237 if (hm->h.function == HPI_ISTREAM_READ)
238 /* from card, WRITE to user mem */
250 if (size && (wrflag == 0)) {
252 copy_from_user(pa->p_buffer, ptr, size);
254 HPI_DEBUG_LOG(WARNING,
256 "bytes from user\n", uncopied_bytes,
260 hpi_send_recv_f(&hm->m0, &hr->r0, file);
262 if (size && (wrflag == 1)) {
264 copy_to_user(ptr, pa->p_buffer, size);
266 HPI_DEBUG_LOG(WARNING,
267 "Missed %d of %d " "bytes to user\n",
268 uncopied_bytes, size);
271 mutex_unlock(&pa->mutex);
274 /* on return response size must be set */
275 /*printk(KERN_INFO "response size %d\n", hr->h.wSize); */
278 HPI_DEBUG_LOG(ERROR, "response zero size\n");
283 if (hr->h.size > res_max_size) {
284 HPI_DEBUG_LOG(ERROR, "response too big %d %d\n", hr->h.size,
286 hr->h.error = HPI_ERROR_RESPONSE_BUFFER_TOO_SMALL;
287 hr->h.specific_error = hr->h.size;
288 hr->h.size = sizeof(hr->h);
291 uncopied_bytes = copy_to_user(puhr, hr, hr->h.size);
292 if (uncopied_bytes) {
293 HPI_DEBUG_LOG(ERROR, "uncopied bytes %d\n", uncopied_bytes);
304 int asihpi_adapter_probe(struct pci_dev *pci_dev,
305 const struct pci_device_id *pci_id)
310 struct hpi_message hm;
311 struct hpi_response hr;
312 struct hpi_adapter adapter;
315 memset(&adapter, 0, sizeof(adapter));
317 dev_printk(KERN_DEBUG, &pci_dev->dev,
318 "probe %04x:%04x,%04x:%04x,%04x\n", pci_dev->vendor,
319 pci_dev->device, pci_dev->subsystem_vendor,
320 pci_dev->subsystem_device, pci_dev->devfn);
322 if (pci_enable_device(pci_dev) < 0) {
323 dev_err(&pci_dev->dev,
324 "pci_enable_device failed, disabling device\n");
328 pci_set_master(pci_dev); /* also sets latency timer if < 16 */
330 hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
331 HPI_SUBSYS_CREATE_ADAPTER);
332 hpi_init_response(&hr, HPI_OBJ_SUBSYSTEM, HPI_SUBSYS_CREATE_ADAPTER,
333 HPI_ERROR_PROCESSING_MESSAGE);
335 hm.adapter_index = HPI_ADAPTER_INDEX_INVALID;
337 nm = HPI_MAX_ADAPTER_MEM_SPACES;
339 for (idx = 0; idx < nm; idx++) {
340 HPI_DEBUG_LOG(INFO, "resource %d %pR\n", idx,
341 &pci_dev->resource[idx]);
343 if (pci_resource_flags(pci_dev, idx) & IORESOURCE_MEM) {
344 memlen = pci_resource_len(pci_dev, idx);
345 pci.ap_mem_base[idx] =
346 ioremap(pci_resource_start(pci_dev, idx),
348 if (!pci.ap_mem_base[idx]) {
350 "ioremap failed, aborting\n");
351 /* unmap previously mapped pci mem space */
357 pci.pci_dev = pci_dev;
358 hm.u.s.resource.bus_type = HPI_BUS_PCI;
359 hm.u.s.resource.r.pci = &pci;
361 /* call CreateAdapterObject on the relevant hpi module */
362 hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
366 adapter_index = hr.u.s.adapter_index;
367 adapter.adapter = hpi_find_adapter(adapter_index);
369 if (prealloc_stream_buf) {
370 adapter.p_buffer = vmalloc(prealloc_stream_buf);
371 if (!adapter.p_buffer) {
373 "HPI could not allocate "
374 "kernel buffer size %d\n",
375 prealloc_stream_buf);
380 hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
382 hm.adapter_index = adapter.adapter->index;
383 hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
388 /* WARNING can't init mutex in 'adapter'
389 * and then copy it to adapters[] ?!?!
391 adapters[adapter_index] = adapter;
392 mutex_init(&adapters[adapter_index].mutex);
393 pci_set_drvdata(pci_dev, &adapters[adapter_index]);
395 dev_info(&pci_dev->dev, "probe succeeded for ASI%04X HPI index %d\n",
396 adapter.adapter->type, adapter_index);
401 for (idx = 0; idx < HPI_MAX_ADAPTER_MEM_SPACES; idx++) {
402 if (pci.ap_mem_base[idx]) {
403 iounmap(pci.ap_mem_base[idx]);
404 pci.ap_mem_base[idx] = NULL;
408 if (adapter.p_buffer) {
409 adapter.buffer_size = 0;
410 vfree(adapter.p_buffer);
413 HPI_DEBUG_LOG(ERROR, "adapter_probe failed\n");
417 void asihpi_adapter_remove(struct pci_dev *pci_dev)
420 struct hpi_message hm;
421 struct hpi_response hr;
422 struct hpi_adapter *pa;
425 pa = pci_get_drvdata(pci_dev);
426 pci = pa->adapter->pci;
428 hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
430 hm.adapter_index = pa->adapter->index;
431 hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
433 /* unmap PCI memory space, mapped during device init. */
434 for (idx = 0; idx < HPI_MAX_ADAPTER_MEM_SPACES; idx++) {
435 if (pci.ap_mem_base[idx])
436 iounmap(pci.ap_mem_base[idx]);
442 pci_set_drvdata(pci_dev, NULL);
444 dev_info(&pci_dev->dev,
445 "remove %04x:%04x,%04x:%04x,%04x, HPI index %d\n",
446 pci_dev->vendor, pci_dev->device,
447 pci_dev->subsystem_vendor, pci_dev->subsystem_device,
448 pci_dev->devfn, pa->adapter->index);
450 memset(pa, 0, sizeof(*pa));
453 void __init asihpi_init(void)
455 struct hpi_message hm;
456 struct hpi_response hr;
458 memset(adapters, 0, sizeof(adapters));
460 printk(KERN_INFO "ASIHPI driver " HPI_VER_STRING "\n");
462 hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
463 HPI_SUBSYS_DRIVER_LOAD);
464 hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
467 void asihpi_exit(void)
469 struct hpi_message hm;
470 struct hpi_response hr;
472 hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
473 HPI_SUBSYS_DRIVER_UNLOAD);
474 hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);