Commit | Line | Data |
---|---|---|
719f82d3 EB |
1 | /******************************************************************************* |
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 | ||
19 | Common 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> | |
3285ea10 | 33 | #include <linux/pci.h> |
719f82d3 EB |
34 | #include <linux/stringify.h> |
35 | ||
36 | #ifdef MODULE_FIRMWARE | |
37 | MODULE_FIRMWARE("asihpi/dsp5000.bin"); | |
38 | MODULE_FIRMWARE("asihpi/dsp6200.bin"); | |
39 | MODULE_FIRMWARE("asihpi/dsp6205.bin"); | |
40 | MODULE_FIRMWARE("asihpi/dsp6400.bin"); | |
41 | MODULE_FIRMWARE("asihpi/dsp6600.bin"); | |
42 | MODULE_FIRMWARE("asihpi/dsp8700.bin"); | |
43 | MODULE_FIRMWARE("asihpi/dsp8900.bin"); | |
44 | #endif | |
45 | ||
46 | static int prealloc_stream_buf; | |
47 | module_param(prealloc_stream_buf, int, S_IRUGO); | |
48 | MODULE_PARM_DESC(prealloc_stream_buf, | |
3285ea10 | 49 | "Preallocate size for per-adapter stream buffer"); |
719f82d3 EB |
50 | |
51 | /* Allow the debug level to be changed after module load. | |
52 | E.g. echo 2 > /sys/module/asihpi/parameters/hpiDebugLevel | |
53 | */ | |
54 | module_param(hpi_debug_level, int, S_IRUGO | S_IWUSR); | |
55 | MODULE_PARM_DESC(hpi_debug_level, "debug verbosity 0..5"); | |
56 | ||
57 | /* List of adapters found */ | |
58 | static 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 | */ | |
63 | static 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 | */ | |
79 | void hpi_send_recv(struct hpi_message *phm, struct hpi_response *phr) | |
80 | { | |
81 | hpi_send_recv_f(phm, phr, HOWNER_KERNEL); | |
82 | } | |
83 | ||
84 | EXPORT_SYMBOL(hpi_send_recv); | |
85 | /* for radio-asihpi */ | |
86 | ||
87 | int 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 | ||
100 | long 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. */ | |
3285ea10 EB |
125 | if (get_user(puhm, &phpi_ioctl_data->phm) |
126 | || get_user(puhr, &phpi_ioctl_data->phr)) { | |
ec9d04b2 KV |
127 | err = -EFAULT; |
128 | goto out; | |
129 | } | |
719f82d3 EB |
130 | |
131 | /* Now read the message size and data from user space. */ | |
ec9d04b2 KV |
132 | if (get_user(hm->h.size, (u16 __user *)puhm)) { |
133 | err = -EFAULT; | |
134 | goto out; | |
135 | } | |
719f82d3 EB |
136 | if (hm->h.size > sizeof(*hm)) |
137 | hm->h.size = sizeof(*hm); | |
138 | ||
3285ea10 | 139 | /* printk(KERN_INFO "message size %d\n", hm->h.wSize); */ |
719f82d3 EB |
140 | |
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 | ||
ec9d04b2 KV |
148 | if (get_user(res_max_size, (u16 __user *)puhr)) { |
149 | err = -EFAULT; | |
150 | goto out; | |
151 | } | |
719f82d3 EB |
152 | /* 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 | ||
4a122c10 DR |
159 | if (hm->h.adapter_index >= HPI_MAX_ADAPTERS) { |
160 | err = -EINVAL; | |
161 | goto out; | |
162 | } | |
163 | ||
719f82d3 | 164 | pa = &adapters[hm->h.adapter_index]; |
3285ea10 | 165 | hr->h.size = res_max_size; |
719f82d3 EB |
166 | if (hm->h.object == HPI_OBJ_SUBSYSTEM) { |
167 | switch (hm->h.function) { | |
168 | case HPI_SUBSYS_CREATE_ADAPTER: | |
169 | case HPI_SUBSYS_DELETE_ADAPTER: | |
170 | /* Application must not use these functions! */ | |
171 | hr->h.size = sizeof(hr->h); | |
172 | hr->h.error = HPI_ERROR_INVALID_OPERATION; | |
173 | hr->h.function = hm->h.function; | |
174 | uncopied_bytes = copy_to_user(puhr, hr, hr->h.size); | |
175 | if (uncopied_bytes) | |
176 | err = -EFAULT; | |
177 | else | |
178 | err = 0; | |
179 | goto out; | |
180 | ||
181 | default: | |
182 | hpi_send_recv_f(&hm->m0, &hr->r0, file); | |
183 | } | |
184 | } else { | |
185 | u16 __user *ptr = NULL; | |
186 | u32 size = 0; | |
187 | ||
188 | /* -1=no data 0=read from user mem, 1=write to user mem */ | |
189 | int wrflag = -1; | |
190 | u32 adapter = hm->h.adapter_index; | |
191 | ||
192 | if ((hm->h.adapter_index > HPI_MAX_ADAPTERS) || (!pa->type)) { | |
193 | hpi_init_response(&hr->r0, HPI_OBJ_ADAPTER, | |
194 | HPI_ADAPTER_OPEN, | |
195 | HPI_ERROR_BAD_ADAPTER_NUMBER); | |
196 | ||
197 | uncopied_bytes = | |
198 | copy_to_user(puhr, hr, sizeof(hr->h)); | |
199 | if (uncopied_bytes) | |
200 | err = -EFAULT; | |
201 | else | |
202 | err = 0; | |
203 | goto out; | |
204 | } | |
205 | ||
206 | if (mutex_lock_interruptible(&adapters[adapter].mutex)) { | |
207 | err = -EINTR; | |
208 | goto out; | |
209 | } | |
210 | ||
211 | /* Dig out any pointers embedded in the message. */ | |
212 | switch (hm->h.function) { | |
213 | case HPI_OSTREAM_WRITE: | |
214 | case HPI_ISTREAM_READ:{ | |
215 | /* Yes, sparse, this is correct. */ | |
216 | ptr = (u16 __user *)hm->m0.u.d.u.data.pb_data; | |
217 | size = hm->m0.u.d.u.data.data_size; | |
218 | ||
219 | /* Allocate buffer according to application request. | |
220 | ?Is it better to alloc/free for the duration | |
221 | of the transaction? | |
222 | */ | |
223 | if (pa->buffer_size < size) { | |
224 | HPI_DEBUG_LOG(DEBUG, | |
3285ea10 | 225 | "Realloc adapter %d stream " |
719f82d3 EB |
226 | "buffer from %zd to %d\n", |
227 | hm->h.adapter_index, | |
228 | pa->buffer_size, size); | |
229 | if (pa->p_buffer) { | |
230 | pa->buffer_size = 0; | |
231 | vfree(pa->p_buffer); | |
232 | } | |
233 | pa->p_buffer = vmalloc(size); | |
234 | if (pa->p_buffer) | |
235 | pa->buffer_size = size; | |
236 | else { | |
237 | HPI_DEBUG_LOG(ERROR, | |
238 | "HPI could not allocate " | |
239 | "stream buffer size %d\n", | |
240 | size); | |
241 | ||
242 | mutex_unlock(&adapters | |
243 | [adapter].mutex); | |
244 | 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, | |
3285ea10 | 268 | "Missed %d of %d " |
719f82d3 EB |
269 | "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, | |
3285ea10 | 280 | "Missed %d of %d " "bytes to user\n", |
719f82d3 EB |
281 | uncopied_bytes, size); |
282 | } | |
283 | ||
284 | mutex_unlock(&adapters[adapter].mutex); | |
285 | } | |
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); | |
3285ea10 EB |
299 | hr->h.error = HPI_ERROR_RESPONSE_BUFFER_TOO_SMALL; |
300 | hr->h.specific_error = hr->h.size; | |
301 | hr->h.size = sizeof(hr->h); | |
719f82d3 EB |
302 | } |
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 | ||
311 | out: | |
312 | kfree(hm); | |
313 | kfree(hr); | |
314 | return err; | |
315 | } | |
316 | ||
317 | int __devinit asihpi_adapter_probe(struct pci_dev *pci_dev, | |
318 | const struct pci_device_id *pci_id) | |
319 | { | |
320 | int err, idx, nm; | |
321 | 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 | ||
3285ea10 EB |
329 | 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, | |
719f82d3 EB |
332 | pci_dev->subsystem_device, pci_dev->devfn); |
333 | ||
3285ea10 EB |
334 | 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 | ||
719f82d3 EB |
342 | 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 | ||
3285ea10 | 347 | hm.adapter_index = HPI_ADAPTER_INDEX_INVALID; |
719f82d3 | 348 | |
719f82d3 EB |
349 | adapter.pci = pci_dev; |
350 | ||
351 | nm = HPI_MAX_ADAPTER_MEM_SPACES; | |
352 | ||
353 | for (idx = 0; idx < nm; idx++) { | |
354 | HPI_DEBUG_LOG(INFO, "resource %d %s %08llx-%08llx %04llx\n", | |
355 | idx, pci_dev->resource[idx].name, | |
356 | (unsigned long long)pci_resource_start(pci_dev, idx), | |
357 | (unsigned long long)pci_resource_end(pci_dev, idx), | |
358 | (unsigned long long)pci_resource_flags(pci_dev, idx)); | |
359 | ||
360 | if (pci_resource_flags(pci_dev, idx) & IORESOURCE_MEM) { | |
361 | memlen = pci_resource_len(pci_dev, idx); | |
362 | adapter.ap_remapped_mem_base[idx] = | |
363 | ioremap(pci_resource_start(pci_dev, idx), | |
364 | memlen); | |
365 | if (!adapter.ap_remapped_mem_base[idx]) { | |
366 | HPI_DEBUG_LOG(ERROR, | |
367 | "ioremap failed, aborting\n"); | |
368 | /* unmap previously mapped pci mem space */ | |
369 | goto err; | |
370 | } | |
371 | } | |
372 | ||
373 | pci.ap_mem_base[idx] = adapter.ap_remapped_mem_base[idx]; | |
374 | } | |
375 | ||
3285ea10 | 376 | pci.pci_dev = pci_dev; |
719f82d3 EB |
377 | hm.u.s.resource.bus_type = HPI_BUS_PCI; |
378 | hm.u.s.resource.r.pci = &pci; | |
379 | ||
380 | /* call CreateAdapterObject on the relevant hpi module */ | |
381 | hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL); | |
382 | if (hr.error) | |
383 | goto err; | |
384 | ||
385 | if (prealloc_stream_buf) { | |
386 | adapter.p_buffer = vmalloc(prealloc_stream_buf); | |
387 | if (!adapter.p_buffer) { | |
388 | HPI_DEBUG_LOG(ERROR, | |
389 | "HPI could not allocate " | |
390 | "kernel buffer size %d\n", | |
391 | prealloc_stream_buf); | |
392 | goto err; | |
393 | } | |
394 | } | |
395 | ||
396 | adapter.index = hr.u.s.adapter_index; | |
2f918a64 | 397 | adapter.type = hr.u.s.adapter_type; |
719f82d3 EB |
398 | hm.adapter_index = adapter.index; |
399 | ||
ba94455c | 400 | err = hpi_adapter_open(adapter.index); |
719f82d3 EB |
401 | if (err) |
402 | 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 | */ | |
408 | adapters[hr.u.s.adapter_index] = adapter; | |
409 | mutex_init(&adapters[adapter.index].mutex); | |
410 | pci_set_drvdata(pci_dev, &adapters[adapter.index]); | |
411 | ||
3285ea10 EB |
412 | dev_printk(KERN_INFO, &pci_dev->dev, |
413 | "probe succeeded for ASI%04X HPI index %d\n", adapter.type, | |
414 | adapter.index); | |
719f82d3 EB |
415 | |
416 | return 0; | |
417 | ||
418 | err: | |
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 | ||
435 | void __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; | |
5dbea6b1 | 441 | pa = pci_get_drvdata(pci_dev); |
719f82d3 EB |
442 | |
443 | hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM, | |
444 | HPI_SUBSYS_DELETE_ADAPTER); | |
3285ea10 EB |
445 | hm.obj_index = pa->index; |
446 | hm.adapter_index = HPI_ADAPTER_INDEX_INVALID; | |
719f82d3 EB |
447 | hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL); |
448 | ||
449 | /* unmap PCI memory space, mapped during device init. */ | |
450 | for (idx = 0; idx < HPI_MAX_ADAPTER_MEM_SPACES; idx++) { | |
451 | if (pa->ap_remapped_mem_base[idx]) { | |
452 | iounmap(pa->ap_remapped_mem_base[idx]); | |
453 | pa->ap_remapped_mem_base[idx] = NULL; | |
454 | } | |
455 | } | |
456 | ||
c188dec3 | 457 | if (pa->p_buffer) |
719f82d3 | 458 | vfree(pa->p_buffer); |
719f82d3 EB |
459 | |
460 | pci_set_drvdata(pci_dev, NULL); | |
3285ea10 EB |
461 | if (1) |
462 | dev_printk(KERN_INFO, &pci_dev->dev, | |
463 | "remove %04x:%04x,%04x:%04x,%04x," " HPI index %d.\n", | |
464 | pci_dev->vendor, pci_dev->device, | |
465 | pci_dev->subsystem_vendor, pci_dev->subsystem_device, | |
466 | pci_dev->devfn, pa->index); | |
c188dec3 EB |
467 | |
468 | memset(pa, 0, sizeof(*pa)); | |
719f82d3 EB |
469 | } |
470 | ||
471 | void __init asihpi_init(void) | |
472 | { | |
473 | struct hpi_message hm; | |
474 | struct hpi_response hr; | |
475 | ||
476 | memset(adapters, 0, sizeof(adapters)); | |
477 | ||
1dd6aaaa | 478 | printk(KERN_INFO "ASIHPI driver " HPI_VER_STRING "\n"); |
719f82d3 EB |
479 | |
480 | hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM, | |
481 | HPI_SUBSYS_DRIVER_LOAD); | |
482 | hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL); | |
483 | } | |
484 | ||
485 | void asihpi_exit(void) | |
486 | { | |
487 | struct hpi_message hm; | |
488 | struct hpi_response hr; | |
489 | ||
490 | hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM, | |
491 | HPI_SUBSYS_DRIVER_UNLOAD); | |
492 | hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL); | |
493 | } |