blob: 75f7a2d11a3ed015a0e6f8c98e976675fd69b28a [file] [log] [blame]
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001/*******************************************************************************
2
3 AudioScience HPI driver
Eliot Blennerhassett1c073b62011-07-22 15:52:46 +12004 Copyright (C) 1997-2011 AudioScience Inc. <support@audioscience.com>
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02005
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;
9
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.
14
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
18
19Common Linux HPI ioctl and module probe/remove functions
20*******************************************************************************/
21#define SOURCEFILE_NAME "hpioctl.c"
22
23#include "hpi_internal.h"
Eliot Blennerhassettf6baaec2011-12-22 13:38:31 +130024#include "hpi_version.h"
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020025#include "hpimsginit.h"
26#include "hpidebug.h"
27#include "hpimsgx.h"
28#include "hpioctl.h"
Eliot Blennerhassett6d0b8982011-04-05 20:55:47 +120029#include "hpicmn.h"
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020030
31#include <linux/fs.h>
32#include <linux/slab.h>
33#include <linux/moduleparam.h>
34#include <asm/uaccess.h>
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +130035#include <linux/pci.h>
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020036#include <linux/stringify.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040037#include <linux/module.h>
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020038
39#ifdef MODULE_FIRMWARE
40MODULE_FIRMWARE("asihpi/dsp5000.bin");
41MODULE_FIRMWARE("asihpi/dsp6200.bin");
42MODULE_FIRMWARE("asihpi/dsp6205.bin");
43MODULE_FIRMWARE("asihpi/dsp6400.bin");
44MODULE_FIRMWARE("asihpi/dsp6600.bin");
45MODULE_FIRMWARE("asihpi/dsp8700.bin");
46MODULE_FIRMWARE("asihpi/dsp8900.bin");
47#endif
48
49static int prealloc_stream_buf;
50module_param(prealloc_stream_buf, int, S_IRUGO);
51MODULE_PARM_DESC(prealloc_stream_buf,
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +130052 "Preallocate size for per-adapter stream buffer");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020053
54/* Allow the debug level to be changed after module load.
55 E.g. echo 2 > /sys/module/asihpi/parameters/hpiDebugLevel
56*/
57module_param(hpi_debug_level, int, S_IRUGO | S_IWUSR);
58MODULE_PARM_DESC(hpi_debug_level, "debug verbosity 0..5");
59
60/* List of adapters found */
61static struct hpi_adapter adapters[HPI_MAX_ADAPTERS];
62
63/* Wrapper function to HPI_Message to enable dumping of the
64 message and response types.
65*/
66static void hpi_send_recv_f(struct hpi_message *phm, struct hpi_response *phr,
67 struct file *file)
68{
69 int adapter = phm->adapter_index;
70
71 if ((adapter >= HPI_MAX_ADAPTERS || adapter < 0)
72 && (phm->object != HPI_OBJ_SUBSYSTEM))
73 phr->error = HPI_ERROR_INVALID_OBJ_INDEX;
74 else
75 hpi_send_recv_ex(phm, phr, file);
76}
77
78/* This is called from hpifunc.c functions, called by ALSA
79 * (or other kernel process) In this case there is no file descriptor
80 * available for the message cache code
81 */
82void hpi_send_recv(struct hpi_message *phm, struct hpi_response *phr)
83{
84 hpi_send_recv_f(phm, phr, HOWNER_KERNEL);
85}
86
87EXPORT_SYMBOL(hpi_send_recv);
88/* for radio-asihpi */
89
90int asihpi_hpi_release(struct file *file)
91{
92 struct hpi_message hm;
93 struct hpi_response hr;
94
95/* HPI_DEBUG_LOG(INFO,"hpi_release file %p, pid %d\n", file, current->pid); */
96 /* close the subsystem just in case the application forgot to. */
97 hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
98 HPI_SUBSYS_CLOSE);
99 hpi_send_recv_ex(&hm, &hr, file);
100 return 0;
101}
102
103long asihpi_hpi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
104{
105 struct hpi_ioctl_linux __user *phpi_ioctl_data;
106 void __user *puhm;
107 void __user *puhr;
108 union hpi_message_buffer_v1 *hm;
109 union hpi_response_buffer_v1 *hr;
110 u16 res_max_size;
111 u32 uncopied_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200112 int err = 0;
113
114 if (cmd != HPI_IOCTL_LINUX)
115 return -EINVAL;
116
117 hm = kmalloc(sizeof(*hm), GFP_KERNEL);
118 hr = kmalloc(sizeof(*hr), GFP_KERNEL);
119 if (!hm || !hr) {
120 err = -ENOMEM;
121 goto out;
122 }
123
124 phpi_ioctl_data = (struct hpi_ioctl_linux __user *)arg;
125
126 /* Read the message and response pointers from user space. */
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300127 if (get_user(puhm, &phpi_ioctl_data->phm)
128 || get_user(puhr, &phpi_ioctl_data->phr)) {
Kulikov Vasiliyec9d04b2010-07-28 20:41:56 +0400129 err = -EFAULT;
130 goto out;
131 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200132
133 /* Now read the message size and data from user space. */
Kulikov Vasiliyec9d04b2010-07-28 20:41:56 +0400134 if (get_user(hm->h.size, (u16 __user *)puhm)) {
135 err = -EFAULT;
136 goto out;
137 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200138 if (hm->h.size > sizeof(*hm))
139 hm->h.size = sizeof(*hm);
140
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300141 /* printk(KERN_INFO "message size %d\n", hm->h.wSize); */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200142
143 uncopied_bytes = copy_from_user(hm, puhm, hm->h.size);
144 if (uncopied_bytes) {
145 HPI_DEBUG_LOG(ERROR, "uncopied bytes %d\n", uncopied_bytes);
146 err = -EFAULT;
147 goto out;
148 }
149
Kulikov Vasiliyec9d04b2010-07-28 20:41:56 +0400150 if (get_user(res_max_size, (u16 __user *)puhr)) {
151 err = -EFAULT;
152 goto out;
153 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200154 /* printk(KERN_INFO "user response size %d\n", res_max_size); */
155 if (res_max_size < sizeof(struct hpi_response_header)) {
156 HPI_DEBUG_LOG(WARNING, "small res size %d\n", res_max_size);
157 err = -EFAULT;
158 goto out;
159 }
160
Eliot Blennerhassett6d0b8982011-04-05 20:55:47 +1200161 switch (hm->h.function) {
162 case HPI_SUBSYS_CREATE_ADAPTER:
163 case HPI_ADAPTER_DELETE:
164 /* Application must not use these functions! */
165 hr->h.size = sizeof(hr->h);
166 hr->h.error = HPI_ERROR_INVALID_OPERATION;
167 hr->h.function = hm->h.function;
168 uncopied_bytes = copy_to_user(puhr, hr, hr->h.size);
169 if (uncopied_bytes)
170 err = -EFAULT;
171 else
172 err = 0;
173 goto out;
174 }
175
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300176 hr->h.size = res_max_size;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200177 if (hm->h.object == HPI_OBJ_SUBSYSTEM) {
Eliot Blennerhassett6d0b8982011-04-05 20:55:47 +1200178 hpi_send_recv_f(&hm->m0, &hr->r0, file);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200179 } else {
180 u16 __user *ptr = NULL;
181 u32 size = 0;
Eliot Blennerhassett08f984c2011-08-02 09:44:24 +1200182 u32 adapter_present;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200183 /* -1=no data 0=read from user mem, 1=write to user mem */
184 int wrflag = -1;
Eliot Blennerhassett08f984c2011-08-02 09:44:24 +1200185 struct hpi_adapter *pa;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200186
Eliot Blennerhassett08f984c2011-08-02 09:44:24 +1200187 if (hm->h.adapter_index < HPI_MAX_ADAPTERS) {
188 pa = &adapters[hm->h.adapter_index];
189 adapter_present = pa->type;
190 } else {
191 adapter_present = 0;
192 }
193
194 if (!adapter_present) {
195 hpi_init_response(&hr->r0, hm->h.object,
196 hm->h.function, HPI_ERROR_BAD_ADAPTER_NUMBER);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200197
198 uncopied_bytes =
199 copy_to_user(puhr, hr, sizeof(hr->h));
200 if (uncopied_bytes)
201 err = -EFAULT;
202 else
203 err = 0;
204 goto out;
205 }
206
Eliot Blennerhassett767cd362011-07-27 20:03:51 +1200207 if (mutex_lock_interruptible(&pa->mutex)) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200208 err = -EINTR;
209 goto out;
210 }
211
212 /* Dig out any pointers embedded in the message. */
213 switch (hm->h.function) {
214 case HPI_OSTREAM_WRITE:
215 case HPI_ISTREAM_READ:{
216 /* Yes, sparse, this is correct. */
217 ptr = (u16 __user *)hm->m0.u.d.u.data.pb_data;
218 size = hm->m0.u.d.u.data.data_size;
219
220 /* Allocate buffer according to application request.
221 ?Is it better to alloc/free for the duration
222 of the transaction?
223 */
224 if (pa->buffer_size < size) {
225 HPI_DEBUG_LOG(DEBUG,
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300226 "Realloc adapter %d stream "
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200227 "buffer from %zd to %d\n",
228 hm->h.adapter_index,
229 pa->buffer_size, size);
230 if (pa->p_buffer) {
231 pa->buffer_size = 0;
232 vfree(pa->p_buffer);
233 }
234 pa->p_buffer = vmalloc(size);
235 if (pa->p_buffer)
236 pa->buffer_size = size;
237 else {
238 HPI_DEBUG_LOG(ERROR,
239 "HPI could not allocate "
240 "stream buffer size %d\n",
241 size);
242
Eliot Blennerhassett767cd362011-07-27 20:03:51 +1200243 mutex_unlock(&pa->mutex);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200244 err = -EINVAL;
245 goto out;
246 }
247 }
248
249 hm->m0.u.d.u.data.pb_data = pa->p_buffer;
250 if (hm->h.function == HPI_ISTREAM_READ)
251 /* from card, WRITE to user mem */
252 wrflag = 1;
253 else
254 wrflag = 0;
255 break;
256 }
257
258 default:
259 size = 0;
260 break;
261 }
262
263 if (size && (wrflag == 0)) {
264 uncopied_bytes =
265 copy_from_user(pa->p_buffer, ptr, size);
266 if (uncopied_bytes)
267 HPI_DEBUG_LOG(WARNING,
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300268 "Missed %d of %d "
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200269 "bytes from user\n", uncopied_bytes,
270 size);
271 }
272
273 hpi_send_recv_f(&hm->m0, &hr->r0, file);
274
275 if (size && (wrflag == 1)) {
276 uncopied_bytes =
277 copy_to_user(ptr, pa->p_buffer, size);
278 if (uncopied_bytes)
279 HPI_DEBUG_LOG(WARNING,
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300280 "Missed %d of %d " "bytes to user\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200281 uncopied_bytes, size);
282 }
283
Eliot Blennerhassett767cd362011-07-27 20:03:51 +1200284 mutex_unlock(&pa->mutex);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200285 }
286
287 /* on return response size must be set */
288 /*printk(KERN_INFO "response size %d\n", hr->h.wSize); */
289
290 if (!hr->h.size) {
291 HPI_DEBUG_LOG(ERROR, "response zero size\n");
292 err = -EFAULT;
293 goto out;
294 }
295
296 if (hr->h.size > res_max_size) {
297 HPI_DEBUG_LOG(ERROR, "response too big %d %d\n", hr->h.size,
298 res_max_size);
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300299 hr->h.error = HPI_ERROR_RESPONSE_BUFFER_TOO_SMALL;
300 hr->h.specific_error = hr->h.size;
301 hr->h.size = sizeof(hr->h);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200302 }
303
304 uncopied_bytes = copy_to_user(puhr, hr, hr->h.size);
305 if (uncopied_bytes) {
306 HPI_DEBUG_LOG(ERROR, "uncopied bytes %d\n", uncopied_bytes);
307 err = -EFAULT;
308 goto out;
309 }
310
311out:
312 kfree(hm);
313 kfree(hr);
314 return err;
315}
316
317int __devinit asihpi_adapter_probe(struct pci_dev *pci_dev,
318 const struct pci_device_id *pci_id)
319{
Eliot Blennerhassett42258da2011-04-05 20:55:48 +1200320 int idx, nm;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200321 unsigned int memlen;
322 struct hpi_message hm;
323 struct hpi_response hr;
324 struct hpi_adapter adapter;
325 struct hpi_pci pci;
326
327 memset(&adapter, 0, sizeof(adapter));
328
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300329 dev_printk(KERN_DEBUG, &pci_dev->dev,
330 "probe %04x:%04x,%04x:%04x,%04x\n", pci_dev->vendor,
331 pci_dev->device, pci_dev->subsystem_vendor,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200332 pci_dev->subsystem_device, pci_dev->devfn);
333
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300334 if (pci_enable_device(pci_dev) < 0) {
335 dev_printk(KERN_ERR, &pci_dev->dev,
336 "pci_enable_device failed, disabling device\n");
337 return -EIO;
338 }
339
340 pci_set_master(pci_dev); /* also sets latency timer if < 16 */
341
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200342 hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
343 HPI_SUBSYS_CREATE_ADAPTER);
344 hpi_init_response(&hr, HPI_OBJ_SUBSYSTEM, HPI_SUBSYS_CREATE_ADAPTER,
345 HPI_ERROR_PROCESSING_MESSAGE);
346
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300347 hm.adapter_index = HPI_ADAPTER_INDEX_INVALID;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200348
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200349 adapter.pci = pci_dev;
350
351 nm = HPI_MAX_ADAPTER_MEM_SPACES;
352
353 for (idx = 0; idx < nm; idx++) {
Eliot Blennerhassett42258da2011-04-05 20:55:48 +1200354 HPI_DEBUG_LOG(INFO, "resource %d %pR\n", idx,
355 &pci_dev->resource[idx]);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200356
357 if (pci_resource_flags(pci_dev, idx) & IORESOURCE_MEM) {
358 memlen = pci_resource_len(pci_dev, idx);
359 adapter.ap_remapped_mem_base[idx] =
360 ioremap(pci_resource_start(pci_dev, idx),
361 memlen);
362 if (!adapter.ap_remapped_mem_base[idx]) {
363 HPI_DEBUG_LOG(ERROR,
364 "ioremap failed, aborting\n");
365 /* unmap previously mapped pci mem space */
366 goto err;
367 }
368 }
369
370 pci.ap_mem_base[idx] = adapter.ap_remapped_mem_base[idx];
371 }
372
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300373 pci.pci_dev = pci_dev;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200374 hm.u.s.resource.bus_type = HPI_BUS_PCI;
375 hm.u.s.resource.r.pci = &pci;
376
377 /* call CreateAdapterObject on the relevant hpi module */
378 hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
379 if (hr.error)
380 goto err;
381
382 if (prealloc_stream_buf) {
383 adapter.p_buffer = vmalloc(prealloc_stream_buf);
384 if (!adapter.p_buffer) {
385 HPI_DEBUG_LOG(ERROR,
386 "HPI could not allocate "
387 "kernel buffer size %d\n",
388 prealloc_stream_buf);
389 goto err;
390 }
391 }
392
393 adapter.index = hr.u.s.adapter_index;
Eliot Blennerhassett2f918a62011-02-10 17:26:09 +1300394 adapter.type = hr.u.s.adapter_type;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200395
Eliot Blennerhassett6d0b8982011-04-05 20:55:47 +1200396 hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
397 HPI_ADAPTER_OPEN);
398 hm.adapter_index = adapter.index;
399 hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
400
401 if (hr.error)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200402 goto err;
403
404 adapter.snd_card_asihpi = NULL;
405 /* WARNING can't init mutex in 'adapter'
406 * and then copy it to adapters[] ?!?!
407 */
Eliot Blennerhassett6d0b8982011-04-05 20:55:47 +1200408 adapters[adapter.index] = adapter;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200409 mutex_init(&adapters[adapter.index].mutex);
410 pci_set_drvdata(pci_dev, &adapters[adapter.index]);
411
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300412 dev_printk(KERN_INFO, &pci_dev->dev,
413 "probe succeeded for ASI%04X HPI index %d\n", adapter.type,
414 adapter.index);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200415
416 return 0;
417
418err:
419 for (idx = 0; idx < HPI_MAX_ADAPTER_MEM_SPACES; idx++) {
420 if (adapter.ap_remapped_mem_base[idx]) {
421 iounmap(adapter.ap_remapped_mem_base[idx]);
422 adapter.ap_remapped_mem_base[idx] = NULL;
423 }
424 }
425
426 if (adapter.p_buffer) {
427 adapter.buffer_size = 0;
428 vfree(adapter.p_buffer);
429 }
430
431 HPI_DEBUG_LOG(ERROR, "adapter_probe failed\n");
432 return -ENODEV;
433}
434
435void __devexit asihpi_adapter_remove(struct pci_dev *pci_dev)
436{
437 int idx;
438 struct hpi_message hm;
439 struct hpi_response hr;
440 struct hpi_adapter *pa;
Joe Perches5dbea6b2010-11-15 12:14:02 -0800441 pa = pci_get_drvdata(pci_dev);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200442
Eliot Blennerhassett6d0b8982011-04-05 20:55:47 +1200443 hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
444 HPI_ADAPTER_DELETE);
445 hm.adapter_index = pa->index;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200446 hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
447
448 /* unmap PCI memory space, mapped during device init. */
449 for (idx = 0; idx < HPI_MAX_ADAPTER_MEM_SPACES; idx++) {
450 if (pa->ap_remapped_mem_base[idx]) {
451 iounmap(pa->ap_remapped_mem_base[idx]);
452 pa->ap_remapped_mem_base[idx] = NULL;
453 }
454 }
455
Eliot Blennerhassettc188dec2011-02-10 17:26:18 +1300456 if (pa->p_buffer)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200457 vfree(pa->p_buffer);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200458
459 pci_set_drvdata(pci_dev, NULL);
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300460 if (1)
461 dev_printk(KERN_INFO, &pci_dev->dev,
462 "remove %04x:%04x,%04x:%04x,%04x," " HPI index %d.\n",
463 pci_dev->vendor, pci_dev->device,
464 pci_dev->subsystem_vendor, pci_dev->subsystem_device,
465 pci_dev->devfn, pa->index);
Eliot Blennerhassettc188dec2011-02-10 17:26:18 +1300466
467 memset(pa, 0, sizeof(*pa));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200468}
469
470void __init asihpi_init(void)
471{
472 struct hpi_message hm;
473 struct hpi_response hr;
474
475 memset(adapters, 0, sizeof(adapters));
476
Eliot Blennerhassett1dd6aaa2010-07-06 08:37:07 +1200477 printk(KERN_INFO "ASIHPI driver " HPI_VER_STRING "\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200478
479 hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
480 HPI_SUBSYS_DRIVER_LOAD);
481 hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
482}
483
484void asihpi_exit(void)
485{
486 struct hpi_message hm;
487 struct hpi_response hr;
488
489 hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
490 HPI_SUBSYS_DRIVER_UNLOAD);
491 hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
492}