blob: 0ea9aae8011b4573743108cff4158798a0e7907d [file] [log] [blame]
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001/*******************************************************************************
2
3 AudioScience HPI driver
4 Copyright (C) 1997-2010 AudioScience Inc. <support@audioscience.com>
5
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"
28
29#include <linux/fs.h>
30#include <linux/slab.h>
31#include <linux/moduleparam.h>
32#include <asm/uaccess.h>
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +130033#include <linux/pci.h>
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020034#include <linux/stringify.h>
35
36#ifdef MODULE_FIRMWARE
37MODULE_FIRMWARE("asihpi/dsp5000.bin");
38MODULE_FIRMWARE("asihpi/dsp6200.bin");
39MODULE_FIRMWARE("asihpi/dsp6205.bin");
40MODULE_FIRMWARE("asihpi/dsp6400.bin");
41MODULE_FIRMWARE("asihpi/dsp6600.bin");
42MODULE_FIRMWARE("asihpi/dsp8700.bin");
43MODULE_FIRMWARE("asihpi/dsp8900.bin");
44#endif
45
46static int prealloc_stream_buf;
47module_param(prealloc_stream_buf, int, S_IRUGO);
48MODULE_PARM_DESC(prealloc_stream_buf,
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +130049 "Preallocate size for per-adapter stream buffer");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020050
51/* Allow the debug level to be changed after module load.
52 E.g. echo 2 > /sys/module/asihpi/parameters/hpiDebugLevel
53*/
54module_param(hpi_debug_level, int, S_IRUGO | S_IWUSR);
55MODULE_PARM_DESC(hpi_debug_level, "debug verbosity 0..5");
56
57/* List of adapters found */
58static struct hpi_adapter adapters[HPI_MAX_ADAPTERS];
59
60/* Wrapper function to HPI_Message to enable dumping of the
61 message and response types.
62*/
63static void hpi_send_recv_f(struct hpi_message *phm, struct hpi_response *phr,
64 struct file *file)
65{
66 int adapter = phm->adapter_index;
67
68 if ((adapter >= HPI_MAX_ADAPTERS || adapter < 0)
69 && (phm->object != HPI_OBJ_SUBSYSTEM))
70 phr->error = HPI_ERROR_INVALID_OBJ_INDEX;
71 else
72 hpi_send_recv_ex(phm, phr, file);
73}
74
75/* This is called from hpifunc.c functions, called by ALSA
76 * (or other kernel process) In this case there is no file descriptor
77 * available for the message cache code
78 */
79void hpi_send_recv(struct hpi_message *phm, struct hpi_response *phr)
80{
81 hpi_send_recv_f(phm, phr, HOWNER_KERNEL);
82}
83
84EXPORT_SYMBOL(hpi_send_recv);
85/* for radio-asihpi */
86
87int asihpi_hpi_release(struct file *file)
88{
89 struct hpi_message hm;
90 struct hpi_response hr;
91
92/* HPI_DEBUG_LOG(INFO,"hpi_release file %p, pid %d\n", file, current->pid); */
93 /* close the subsystem just in case the application forgot to. */
94 hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
95 HPI_SUBSYS_CLOSE);
96 hpi_send_recv_ex(&hm, &hr, file);
97 return 0;
98}
99
100long asihpi_hpi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
101{
102 struct hpi_ioctl_linux __user *phpi_ioctl_data;
103 void __user *puhm;
104 void __user *puhr;
105 union hpi_message_buffer_v1 *hm;
106 union hpi_response_buffer_v1 *hr;
107 u16 res_max_size;
108 u32 uncopied_bytes;
109 struct hpi_adapter *pa = NULL;
110 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
159 pa = &adapters[hm->h.adapter_index];
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300160 hr->h.size = res_max_size;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200161 if (hm->h.object == HPI_OBJ_SUBSYSTEM) {
162 switch (hm->h.function) {
163 case HPI_SUBSYS_CREATE_ADAPTER:
164 case HPI_SUBSYS_DELETE_ADAPTER:
165 /* Application must not use these functions! */
166 hr->h.size = sizeof(hr->h);
167 hr->h.error = HPI_ERROR_INVALID_OPERATION;
168 hr->h.function = hm->h.function;
169 uncopied_bytes = copy_to_user(puhr, hr, hr->h.size);
170 if (uncopied_bytes)
171 err = -EFAULT;
172 else
173 err = 0;
174 goto out;
175
176 default:
177 hpi_send_recv_f(&hm->m0, &hr->r0, file);
178 }
179 } else {
180 u16 __user *ptr = NULL;
181 u32 size = 0;
182
183 /* -1=no data 0=read from user mem, 1=write to user mem */
184 int wrflag = -1;
185 u32 adapter = hm->h.adapter_index;
186
187 if ((hm->h.adapter_index > HPI_MAX_ADAPTERS) || (!pa->type)) {
188 hpi_init_response(&hr->r0, HPI_OBJ_ADAPTER,
189 HPI_ADAPTER_OPEN,
190 HPI_ERROR_BAD_ADAPTER_NUMBER);
191
192 uncopied_bytes =
193 copy_to_user(puhr, hr, sizeof(hr->h));
194 if (uncopied_bytes)
195 err = -EFAULT;
196 else
197 err = 0;
198 goto out;
199 }
200
201 if (mutex_lock_interruptible(&adapters[adapter].mutex)) {
202 err = -EINTR;
203 goto out;
204 }
205
206 /* Dig out any pointers embedded in the message. */
207 switch (hm->h.function) {
208 case HPI_OSTREAM_WRITE:
209 case HPI_ISTREAM_READ:{
210 /* Yes, sparse, this is correct. */
211 ptr = (u16 __user *)hm->m0.u.d.u.data.pb_data;
212 size = hm->m0.u.d.u.data.data_size;
213
214 /* Allocate buffer according to application request.
215 ?Is it better to alloc/free for the duration
216 of the transaction?
217 */
218 if (pa->buffer_size < size) {
219 HPI_DEBUG_LOG(DEBUG,
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300220 "Realloc adapter %d stream "
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200221 "buffer from %zd to %d\n",
222 hm->h.adapter_index,
223 pa->buffer_size, size);
224 if (pa->p_buffer) {
225 pa->buffer_size = 0;
226 vfree(pa->p_buffer);
227 }
228 pa->p_buffer = vmalloc(size);
229 if (pa->p_buffer)
230 pa->buffer_size = size;
231 else {
232 HPI_DEBUG_LOG(ERROR,
233 "HPI could not allocate "
234 "stream buffer size %d\n",
235 size);
236
237 mutex_unlock(&adapters
238 [adapter].mutex);
239 err = -EINVAL;
240 goto out;
241 }
242 }
243
244 hm->m0.u.d.u.data.pb_data = pa->p_buffer;
245 if (hm->h.function == HPI_ISTREAM_READ)
246 /* from card, WRITE to user mem */
247 wrflag = 1;
248 else
249 wrflag = 0;
250 break;
251 }
252
253 default:
254 size = 0;
255 break;
256 }
257
258 if (size && (wrflag == 0)) {
259 uncopied_bytes =
260 copy_from_user(pa->p_buffer, ptr, size);
261 if (uncopied_bytes)
262 HPI_DEBUG_LOG(WARNING,
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300263 "Missed %d of %d "
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200264 "bytes from user\n", uncopied_bytes,
265 size);
266 }
267
268 hpi_send_recv_f(&hm->m0, &hr->r0, file);
269
270 if (size && (wrflag == 1)) {
271 uncopied_bytes =
272 copy_to_user(ptr, pa->p_buffer, size);
273 if (uncopied_bytes)
274 HPI_DEBUG_LOG(WARNING,
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300275 "Missed %d of %d " "bytes to user\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200276 uncopied_bytes, size);
277 }
278
279 mutex_unlock(&adapters[adapter].mutex);
280 }
281
282 /* on return response size must be set */
283 /*printk(KERN_INFO "response size %d\n", hr->h.wSize); */
284
285 if (!hr->h.size) {
286 HPI_DEBUG_LOG(ERROR, "response zero size\n");
287 err = -EFAULT;
288 goto out;
289 }
290
291 if (hr->h.size > res_max_size) {
292 HPI_DEBUG_LOG(ERROR, "response too big %d %d\n", hr->h.size,
293 res_max_size);
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300294 hr->h.error = HPI_ERROR_RESPONSE_BUFFER_TOO_SMALL;
295 hr->h.specific_error = hr->h.size;
296 hr->h.size = sizeof(hr->h);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200297 }
298
299 uncopied_bytes = copy_to_user(puhr, hr, hr->h.size);
300 if (uncopied_bytes) {
301 HPI_DEBUG_LOG(ERROR, "uncopied bytes %d\n", uncopied_bytes);
302 err = -EFAULT;
303 goto out;
304 }
305
306out:
307 kfree(hm);
308 kfree(hr);
309 return err;
310}
311
312int __devinit asihpi_adapter_probe(struct pci_dev *pci_dev,
313 const struct pci_device_id *pci_id)
314{
315 int err, idx, nm;
316 unsigned int memlen;
317 struct hpi_message hm;
318 struct hpi_response hr;
319 struct hpi_adapter adapter;
320 struct hpi_pci pci;
321
322 memset(&adapter, 0, sizeof(adapter));
323
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300324 dev_printk(KERN_DEBUG, &pci_dev->dev,
325 "probe %04x:%04x,%04x:%04x,%04x\n", pci_dev->vendor,
326 pci_dev->device, pci_dev->subsystem_vendor,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200327 pci_dev->subsystem_device, pci_dev->devfn);
328
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300329 if (pci_enable_device(pci_dev) < 0) {
330 dev_printk(KERN_ERR, &pci_dev->dev,
331 "pci_enable_device failed, disabling device\n");
332 return -EIO;
333 }
334
335 pci_set_master(pci_dev); /* also sets latency timer if < 16 */
336
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200337 hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
338 HPI_SUBSYS_CREATE_ADAPTER);
339 hpi_init_response(&hr, HPI_OBJ_SUBSYSTEM, HPI_SUBSYS_CREATE_ADAPTER,
340 HPI_ERROR_PROCESSING_MESSAGE);
341
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300342 hm.adapter_index = HPI_ADAPTER_INDEX_INVALID;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200343
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200344 adapter.pci = pci_dev;
345
346 nm = HPI_MAX_ADAPTER_MEM_SPACES;
347
348 for (idx = 0; idx < nm; idx++) {
349 HPI_DEBUG_LOG(INFO, "resource %d %s %08llx-%08llx %04llx\n",
350 idx, pci_dev->resource[idx].name,
351 (unsigned long long)pci_resource_start(pci_dev, idx),
352 (unsigned long long)pci_resource_end(pci_dev, idx),
353 (unsigned long long)pci_resource_flags(pci_dev, idx));
354
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 hm.adapter_index = adapter.index;
394
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300395 err = hpi_adapter_open(adapter.index);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200396 if (err)
397 goto err;
398
399 adapter.snd_card_asihpi = NULL;
400 /* WARNING can't init mutex in 'adapter'
401 * and then copy it to adapters[] ?!?!
402 */
403 adapters[hr.u.s.adapter_index] = adapter;
404 mutex_init(&adapters[adapter.index].mutex);
405 pci_set_drvdata(pci_dev, &adapters[adapter.index]);
406
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300407 dev_printk(KERN_INFO, &pci_dev->dev,
408 "probe succeeded for ASI%04X HPI index %d\n", adapter.type,
409 adapter.index);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200410
411 return 0;
412
413err:
414 for (idx = 0; idx < HPI_MAX_ADAPTER_MEM_SPACES; idx++) {
415 if (adapter.ap_remapped_mem_base[idx]) {
416 iounmap(adapter.ap_remapped_mem_base[idx]);
417 adapter.ap_remapped_mem_base[idx] = NULL;
418 }
419 }
420
421 if (adapter.p_buffer) {
422 adapter.buffer_size = 0;
423 vfree(adapter.p_buffer);
424 }
425
426 HPI_DEBUG_LOG(ERROR, "adapter_probe failed\n");
427 return -ENODEV;
428}
429
430void __devexit asihpi_adapter_remove(struct pci_dev *pci_dev)
431{
432 int idx;
433 struct hpi_message hm;
434 struct hpi_response hr;
435 struct hpi_adapter *pa;
Joe Perches5dbea6b2010-11-15 12:14:02 -0800436 pa = pci_get_drvdata(pci_dev);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200437
438 hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
439 HPI_SUBSYS_DELETE_ADAPTER);
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300440 hm.obj_index = pa->index;
441 hm.adapter_index = HPI_ADAPTER_INDEX_INVALID;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200442 hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
443
444 /* unmap PCI memory space, mapped during device init. */
445 for (idx = 0; idx < HPI_MAX_ADAPTER_MEM_SPACES; idx++) {
446 if (pa->ap_remapped_mem_base[idx]) {
447 iounmap(pa->ap_remapped_mem_base[idx]);
448 pa->ap_remapped_mem_base[idx] = NULL;
449 }
450 }
451
452 if (pa->p_buffer) {
453 pa->buffer_size = 0;
454 vfree(pa->p_buffer);
455 }
456
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 Blennerhassett719f82d2010-04-21 18:17:39 +0200464}
465
466void __init asihpi_init(void)
467{
468 struct hpi_message hm;
469 struct hpi_response hr;
470
471 memset(adapters, 0, sizeof(adapters));
472
Eliot Blennerhassett1dd6aaa2010-07-06 08:37:07 +1200473 printk(KERN_INFO "ASIHPI driver " HPI_VER_STRING "\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200474
475 hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
476 HPI_SUBSYS_DRIVER_LOAD);
477 hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
478}
479
480void asihpi_exit(void)
481{
482 struct hpi_message hm;
483 struct hpi_response hr;
484
485 hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
486 HPI_SUBSYS_DRIVER_UNLOAD);
487 hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
488}