blob: a32502e796de937946b1e1b2c9b9d3318441049f [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"
24#include "hpimsginit.h"
25#include "hpidebug.h"
26#include "hpimsgx.h"
27#include "hpioctl.h"
Eliot Blennerhassett6d0b8982011-04-05 20:55:47 +120028#include "hpicmn.h"
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020029
30#include <linux/fs.h>
31#include <linux/slab.h>
32#include <linux/moduleparam.h>
33#include <asm/uaccess.h>
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +130034#include <linux/pci.h>
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020035#include <linux/stringify.h>
36
37#ifdef MODULE_FIRMWARE
38MODULE_FIRMWARE("asihpi/dsp5000.bin");
39MODULE_FIRMWARE("asihpi/dsp6200.bin");
40MODULE_FIRMWARE("asihpi/dsp6205.bin");
41MODULE_FIRMWARE("asihpi/dsp6400.bin");
42MODULE_FIRMWARE("asihpi/dsp6600.bin");
43MODULE_FIRMWARE("asihpi/dsp8700.bin");
44MODULE_FIRMWARE("asihpi/dsp8900.bin");
45#endif
46
47static int prealloc_stream_buf;
48module_param(prealloc_stream_buf, int, S_IRUGO);
49MODULE_PARM_DESC(prealloc_stream_buf,
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +130050 "Preallocate size for per-adapter stream buffer");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020051
52/* Allow the debug level to be changed after module load.
53 E.g. echo 2 > /sys/module/asihpi/parameters/hpiDebugLevel
54*/
55module_param(hpi_debug_level, int, S_IRUGO | S_IWUSR);
56MODULE_PARM_DESC(hpi_debug_level, "debug verbosity 0..5");
57
58/* List of adapters found */
59static struct hpi_adapter adapters[HPI_MAX_ADAPTERS];
60
61/* Wrapper function to HPI_Message to enable dumping of the
62 message and response types.
63*/
64static void hpi_send_recv_f(struct hpi_message *phm, struct hpi_response *phr,
65 struct file *file)
66{
67 int adapter = phm->adapter_index;
68
69 if ((adapter >= HPI_MAX_ADAPTERS || adapter < 0)
70 && (phm->object != HPI_OBJ_SUBSYSTEM))
71 phr->error = HPI_ERROR_INVALID_OBJ_INDEX;
72 else
73 hpi_send_recv_ex(phm, phr, file);
74}
75
76/* This is called from hpifunc.c functions, called by ALSA
77 * (or other kernel process) In this case there is no file descriptor
78 * available for the message cache code
79 */
80void hpi_send_recv(struct hpi_message *phm, struct hpi_response *phr)
81{
82 hpi_send_recv_f(phm, phr, HOWNER_KERNEL);
83}
84
85EXPORT_SYMBOL(hpi_send_recv);
86/* for radio-asihpi */
87
88int asihpi_hpi_release(struct file *file)
89{
90 struct hpi_message hm;
91 struct hpi_response hr;
92
93/* HPI_DEBUG_LOG(INFO,"hpi_release file %p, pid %d\n", file, current->pid); */
94 /* close the subsystem just in case the application forgot to. */
95 hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
96 HPI_SUBSYS_CLOSE);
97 hpi_send_recv_ex(&hm, &hr, file);
98 return 0;
99}
100
101long asihpi_hpi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
102{
103 struct hpi_ioctl_linux __user *phpi_ioctl_data;
104 void __user *puhm;
105 void __user *puhr;
106 union hpi_message_buffer_v1 *hm;
107 union hpi_response_buffer_v1 *hr;
108 u16 res_max_size;
109 u32 uncopied_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200110 int err = 0;
111
112 if (cmd != HPI_IOCTL_LINUX)
113 return -EINVAL;
114
115 hm = kmalloc(sizeof(*hm), GFP_KERNEL);
116 hr = kmalloc(sizeof(*hr), GFP_KERNEL);
117 if (!hm || !hr) {
118 err = -ENOMEM;
119 goto out;
120 }
121
122 phpi_ioctl_data = (struct hpi_ioctl_linux __user *)arg;
123
124 /* Read the message and response pointers from user space. */
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300125 if (get_user(puhm, &phpi_ioctl_data->phm)
126 || get_user(puhr, &phpi_ioctl_data->phr)) {
Kulikov Vasiliyec9d04b2010-07-28 20:41:56 +0400127 err = -EFAULT;
128 goto out;
129 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200130
131 /* Now read the message size and data from user space. */
Kulikov Vasiliyec9d04b2010-07-28 20:41:56 +0400132 if (get_user(hm->h.size, (u16 __user *)puhm)) {
133 err = -EFAULT;
134 goto out;
135 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200136 if (hm->h.size > sizeof(*hm))
137 hm->h.size = sizeof(*hm);
138
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300139 /* printk(KERN_INFO "message size %d\n", hm->h.wSize); */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200140
141 uncopied_bytes = copy_from_user(hm, puhm, hm->h.size);
142 if (uncopied_bytes) {
143 HPI_DEBUG_LOG(ERROR, "uncopied bytes %d\n", uncopied_bytes);
144 err = -EFAULT;
145 goto out;
146 }
147
Kulikov Vasiliyec9d04b2010-07-28 20:41:56 +0400148 if (get_user(res_max_size, (u16 __user *)puhr)) {
149 err = -EFAULT;
150 goto out;
151 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200152 /* printk(KERN_INFO "user response size %d\n", res_max_size); */
153 if (res_max_size < sizeof(struct hpi_response_header)) {
154 HPI_DEBUG_LOG(WARNING, "small res size %d\n", res_max_size);
155 err = -EFAULT;
156 goto out;
157 }
158
Eliot Blennerhassett6d0b8982011-04-05 20:55:47 +1200159 switch (hm->h.function) {
160 case HPI_SUBSYS_CREATE_ADAPTER:
161 case HPI_ADAPTER_DELETE:
162 /* Application must not use these functions! */
163 hr->h.size = sizeof(hr->h);
164 hr->h.error = HPI_ERROR_INVALID_OPERATION;
165 hr->h.function = hm->h.function;
166 uncopied_bytes = copy_to_user(puhr, hr, hr->h.size);
167 if (uncopied_bytes)
168 err = -EFAULT;
169 else
170 err = 0;
171 goto out;
172 }
173
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300174 hr->h.size = res_max_size;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200175 if (hm->h.object == HPI_OBJ_SUBSYSTEM) {
Eliot Blennerhassett6d0b8982011-04-05 20:55:47 +1200176 hpi_send_recv_f(&hm->m0, &hr->r0, file);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200177 } else {
178 u16 __user *ptr = NULL;
179 u32 size = 0;
Eliot Blennerhassett08f984c2011-08-02 09:44:24 +1200180 u32 adapter_present;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200181 /* -1=no data 0=read from user mem, 1=write to user mem */
182 int wrflag = -1;
Eliot Blennerhassett08f984c2011-08-02 09:44:24 +1200183 struct hpi_adapter *pa;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200184
Eliot Blennerhassett08f984c2011-08-02 09:44:24 +1200185 if (hm->h.adapter_index < HPI_MAX_ADAPTERS) {
186 pa = &adapters[hm->h.adapter_index];
187 adapter_present = pa->type;
188 } else {
189 adapter_present = 0;
190 }
191
192 if (!adapter_present) {
193 hpi_init_response(&hr->r0, hm->h.object,
194 hm->h.function, HPI_ERROR_BAD_ADAPTER_NUMBER);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200195
196 uncopied_bytes =
197 copy_to_user(puhr, hr, sizeof(hr->h));
198 if (uncopied_bytes)
199 err = -EFAULT;
200 else
201 err = 0;
202 goto out;
203 }
204
Eliot Blennerhassett767cd362011-07-27 20:03:51 +1200205 if (mutex_lock_interruptible(&pa->mutex)) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200206 err = -EINTR;
207 goto out;
208 }
209
210 /* Dig out any pointers embedded in the message. */
211 switch (hm->h.function) {
212 case HPI_OSTREAM_WRITE:
213 case HPI_ISTREAM_READ:{
214 /* Yes, sparse, this is correct. */
215 ptr = (u16 __user *)hm->m0.u.d.u.data.pb_data;
216 size = hm->m0.u.d.u.data.data_size;
217
218 /* Allocate buffer according to application request.
219 ?Is it better to alloc/free for the duration
220 of the transaction?
221 */
222 if (pa->buffer_size < size) {
223 HPI_DEBUG_LOG(DEBUG,
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300224 "Realloc adapter %d stream "
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200225 "buffer from %zd to %d\n",
226 hm->h.adapter_index,
227 pa->buffer_size, size);
228 if (pa->p_buffer) {
229 pa->buffer_size = 0;
230 vfree(pa->p_buffer);
231 }
232 pa->p_buffer = vmalloc(size);
233 if (pa->p_buffer)
234 pa->buffer_size = size;
235 else {
236 HPI_DEBUG_LOG(ERROR,
237 "HPI could not allocate "
238 "stream buffer size %d\n",
239 size);
240
Eliot Blennerhassett767cd362011-07-27 20:03:51 +1200241 mutex_unlock(&pa->mutex);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200242 err = -EINVAL;
243 goto out;
244 }
245 }
246
247 hm->m0.u.d.u.data.pb_data = pa->p_buffer;
248 if (hm->h.function == HPI_ISTREAM_READ)
249 /* from card, WRITE to user mem */
250 wrflag = 1;
251 else
252 wrflag = 0;
253 break;
254 }
255
256 default:
257 size = 0;
258 break;
259 }
260
261 if (size && (wrflag == 0)) {
262 uncopied_bytes =
263 copy_from_user(pa->p_buffer, ptr, size);
264 if (uncopied_bytes)
265 HPI_DEBUG_LOG(WARNING,
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300266 "Missed %d of %d "
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200267 "bytes from user\n", uncopied_bytes,
268 size);
269 }
270
271 hpi_send_recv_f(&hm->m0, &hr->r0, file);
272
273 if (size && (wrflag == 1)) {
274 uncopied_bytes =
275 copy_to_user(ptr, pa->p_buffer, size);
276 if (uncopied_bytes)
277 HPI_DEBUG_LOG(WARNING,
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300278 "Missed %d of %d " "bytes to user\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200279 uncopied_bytes, size);
280 }
281
Eliot Blennerhassett767cd362011-07-27 20:03:51 +1200282 mutex_unlock(&pa->mutex);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200283 }
284
285 /* on return response size must be set */
286 /*printk(KERN_INFO "response size %d\n", hr->h.wSize); */
287
288 if (!hr->h.size) {
289 HPI_DEBUG_LOG(ERROR, "response zero size\n");
290 err = -EFAULT;
291 goto out;
292 }
293
294 if (hr->h.size > res_max_size) {
295 HPI_DEBUG_LOG(ERROR, "response too big %d %d\n", hr->h.size,
296 res_max_size);
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300297 hr->h.error = HPI_ERROR_RESPONSE_BUFFER_TOO_SMALL;
298 hr->h.specific_error = hr->h.size;
299 hr->h.size = sizeof(hr->h);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200300 }
301
302 uncopied_bytes = copy_to_user(puhr, hr, hr->h.size);
303 if (uncopied_bytes) {
304 HPI_DEBUG_LOG(ERROR, "uncopied bytes %d\n", uncopied_bytes);
305 err = -EFAULT;
306 goto out;
307 }
308
309out:
310 kfree(hm);
311 kfree(hr);
312 return err;
313}
314
315int __devinit asihpi_adapter_probe(struct pci_dev *pci_dev,
316 const struct pci_device_id *pci_id)
317{
Eliot Blennerhassett42258da2011-04-05 20:55:48 +1200318 int idx, nm;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200319 unsigned int memlen;
320 struct hpi_message hm;
321 struct hpi_response hr;
322 struct hpi_adapter adapter;
323 struct hpi_pci pci;
324
325 memset(&adapter, 0, sizeof(adapter));
326
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300327 dev_printk(KERN_DEBUG, &pci_dev->dev,
328 "probe %04x:%04x,%04x:%04x,%04x\n", pci_dev->vendor,
329 pci_dev->device, pci_dev->subsystem_vendor,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200330 pci_dev->subsystem_device, pci_dev->devfn);
331
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300332 if (pci_enable_device(pci_dev) < 0) {
333 dev_printk(KERN_ERR, &pci_dev->dev,
334 "pci_enable_device failed, disabling device\n");
335 return -EIO;
336 }
337
338 pci_set_master(pci_dev); /* also sets latency timer if < 16 */
339
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200340 hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
341 HPI_SUBSYS_CREATE_ADAPTER);
342 hpi_init_response(&hr, HPI_OBJ_SUBSYSTEM, HPI_SUBSYS_CREATE_ADAPTER,
343 HPI_ERROR_PROCESSING_MESSAGE);
344
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300345 hm.adapter_index = HPI_ADAPTER_INDEX_INVALID;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200346
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200347 adapter.pci = pci_dev;
348
349 nm = HPI_MAX_ADAPTER_MEM_SPACES;
350
351 for (idx = 0; idx < nm; idx++) {
Eliot Blennerhassett42258da2011-04-05 20:55:48 +1200352 HPI_DEBUG_LOG(INFO, "resource %d %pR\n", idx,
353 &pci_dev->resource[idx]);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200354
355 if (pci_resource_flags(pci_dev, idx) & IORESOURCE_MEM) {
356 memlen = pci_resource_len(pci_dev, idx);
357 adapter.ap_remapped_mem_base[idx] =
358 ioremap(pci_resource_start(pci_dev, idx),
359 memlen);
360 if (!adapter.ap_remapped_mem_base[idx]) {
361 HPI_DEBUG_LOG(ERROR,
362 "ioremap failed, aborting\n");
363 /* unmap previously mapped pci mem space */
364 goto err;
365 }
366 }
367
368 pci.ap_mem_base[idx] = adapter.ap_remapped_mem_base[idx];
369 }
370
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300371 pci.pci_dev = pci_dev;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200372 hm.u.s.resource.bus_type = HPI_BUS_PCI;
373 hm.u.s.resource.r.pci = &pci;
374
375 /* call CreateAdapterObject on the relevant hpi module */
376 hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
377 if (hr.error)
378 goto err;
379
380 if (prealloc_stream_buf) {
381 adapter.p_buffer = vmalloc(prealloc_stream_buf);
382 if (!adapter.p_buffer) {
383 HPI_DEBUG_LOG(ERROR,
384 "HPI could not allocate "
385 "kernel buffer size %d\n",
386 prealloc_stream_buf);
387 goto err;
388 }
389 }
390
391 adapter.index = hr.u.s.adapter_index;
Eliot Blennerhassett2f918a62011-02-10 17:26:09 +1300392 adapter.type = hr.u.s.adapter_type;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200393
Eliot Blennerhassett6d0b8982011-04-05 20:55:47 +1200394 hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
395 HPI_ADAPTER_OPEN);
396 hm.adapter_index = adapter.index;
397 hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
398
399 if (hr.error)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200400 goto err;
401
402 adapter.snd_card_asihpi = NULL;
403 /* WARNING can't init mutex in 'adapter'
404 * and then copy it to adapters[] ?!?!
405 */
Eliot Blennerhassett6d0b8982011-04-05 20:55:47 +1200406 adapters[adapter.index] = adapter;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200407 mutex_init(&adapters[adapter.index].mutex);
408 pci_set_drvdata(pci_dev, &adapters[adapter.index]);
409
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300410 dev_printk(KERN_INFO, &pci_dev->dev,
411 "probe succeeded for ASI%04X HPI index %d\n", adapter.type,
412 adapter.index);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200413
414 return 0;
415
416err:
417 for (idx = 0; idx < HPI_MAX_ADAPTER_MEM_SPACES; idx++) {
418 if (adapter.ap_remapped_mem_base[idx]) {
419 iounmap(adapter.ap_remapped_mem_base[idx]);
420 adapter.ap_remapped_mem_base[idx] = NULL;
421 }
422 }
423
424 if (adapter.p_buffer) {
425 adapter.buffer_size = 0;
426 vfree(adapter.p_buffer);
427 }
428
429 HPI_DEBUG_LOG(ERROR, "adapter_probe failed\n");
430 return -ENODEV;
431}
432
433void __devexit asihpi_adapter_remove(struct pci_dev *pci_dev)
434{
435 int idx;
436 struct hpi_message hm;
437 struct hpi_response hr;
438 struct hpi_adapter *pa;
Joe Perches5dbea6b2010-11-15 12:14:02 -0800439 pa = pci_get_drvdata(pci_dev);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200440
Eliot Blennerhassett6d0b8982011-04-05 20:55:47 +1200441 hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
442 HPI_ADAPTER_DELETE);
443 hm.adapter_index = pa->index;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200444 hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
445
446 /* unmap PCI memory space, mapped during device init. */
447 for (idx = 0; idx < HPI_MAX_ADAPTER_MEM_SPACES; idx++) {
448 if (pa->ap_remapped_mem_base[idx]) {
449 iounmap(pa->ap_remapped_mem_base[idx]);
450 pa->ap_remapped_mem_base[idx] = NULL;
451 }
452 }
453
Eliot Blennerhassettc188dec2011-02-10 17:26:18 +1300454 if (pa->p_buffer)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200455 vfree(pa->p_buffer);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200456
457 pci_set_drvdata(pci_dev, NULL);
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300458 if (1)
459 dev_printk(KERN_INFO, &pci_dev->dev,
460 "remove %04x:%04x,%04x:%04x,%04x," " HPI index %d.\n",
461 pci_dev->vendor, pci_dev->device,
462 pci_dev->subsystem_vendor, pci_dev->subsystem_device,
463 pci_dev->devfn, pa->index);
Eliot Blennerhassettc188dec2011-02-10 17:26:18 +1300464
465 memset(pa, 0, sizeof(*pa));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200466}
467
468void __init asihpi_init(void)
469{
470 struct hpi_message hm;
471 struct hpi_response hr;
472
473 memset(adapters, 0, sizeof(adapters));
474
Eliot Blennerhassett1dd6aaa2010-07-06 08:37:07 +1200475 printk(KERN_INFO "ASIHPI driver " HPI_VER_STRING "\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200476
477 hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
478 HPI_SUBSYS_DRIVER_LOAD);
479 hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
480}
481
482void asihpi_exit(void)
483{
484 struct hpi_message hm;
485 struct hpi_response hr;
486
487 hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
488 HPI_SUBSYS_DRIVER_UNLOAD);
489 hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
490}