GNU Linux-libre 4.4.285-gnu1
[releases.git] / sound / pci / asihpi / hpioctl.c
1 /*******************************************************************************
2     AudioScience HPI driver
3     Common Linux HPI ioctl and module probe/remove functions
4
5     Copyright (C) 1997-2014  AudioScience Inc. <support@audioscience.com>
6
7     This program is free software; you can redistribute it and/or modify
8     it under the terms of version 2 of the GNU General Public License as
9     published by the Free Software Foundation;
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16 *******************************************************************************/
17 #define SOURCEFILE_NAME "hpioctl.c"
18
19 #include "hpi_internal.h"
20 #include "hpi_version.h"
21 #include "hpimsginit.h"
22 #include "hpidebug.h"
23 #include "hpimsgx.h"
24 #include "hpioctl.h"
25 #include "hpicmn.h"
26
27 #include <linux/fs.h>
28 #include <linux/interrupt.h>
29 #include <linux/slab.h>
30 #include <linux/moduleparam.h>
31 #include <linux/uaccess.h>
32 #include <linux/pci.h>
33 #include <linux/stringify.h>
34 #include <linux/module.h>
35 #include <linux/vmalloc.h>
36 #include <linux/nospec.h>
37
38 #ifdef MODULE_FIRMWARE
39 /*(DEBLOBBED)*/
40 #endif
41
42 static int prealloc_stream_buf;
43 module_param(prealloc_stream_buf, int, S_IRUGO);
44 MODULE_PARM_DESC(prealloc_stream_buf,
45         "Preallocate size for per-adapter stream buffer");
46
47 /* Allow the debug level to be changed after module load.
48  E.g.   echo 2 > /sys/module/asihpi/parameters/hpiDebugLevel
49 */
50 module_param(hpi_debug_level, int, S_IRUGO | S_IWUSR);
51 MODULE_PARM_DESC(hpi_debug_level, "debug verbosity 0..5");
52
53 /* List of adapters found */
54 static struct hpi_adapter adapters[HPI_MAX_ADAPTERS];
55
56 /* Wrapper function to HPI_Message to enable dumping of the
57    message and response types.
58 */
59 static void hpi_send_recv_f(struct hpi_message *phm, struct hpi_response *phr,
60         struct file *file)
61 {
62         if ((phm->adapter_index >= HPI_MAX_ADAPTERS)
63                 && (phm->object != HPI_OBJ_SUBSYSTEM))
64                 phr->error = HPI_ERROR_INVALID_OBJ_INDEX;
65         else
66                 hpi_send_recv_ex(phm, phr, file);
67 }
68
69 /* This is called from hpifunc.c functions, called by ALSA
70  * (or other kernel process) In this case there is no file descriptor
71  * available for the message cache code
72  */
73 void hpi_send_recv(struct hpi_message *phm, struct hpi_response *phr)
74 {
75         hpi_send_recv_f(phm, phr, HOWNER_KERNEL);
76 }
77
78 EXPORT_SYMBOL(hpi_send_recv);
79 /* for radio-asihpi */
80
81 int asihpi_hpi_release(struct file *file)
82 {
83         struct hpi_message hm;
84         struct hpi_response hr;
85
86 /* HPI_DEBUG_LOG(INFO,"hpi_release file %p, pid %d\n", file, current->pid); */
87         /* close the subsystem just in case the application forgot to. */
88         hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
89                 HPI_SUBSYS_CLOSE);
90         hpi_send_recv_ex(&hm, &hr, file);
91         return 0;
92 }
93
94 long asihpi_hpi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
95 {
96         struct hpi_ioctl_linux __user *phpi_ioctl_data;
97         void __user *puhm;
98         void __user *puhr;
99         union hpi_message_buffer_v1 *hm;
100         union hpi_response_buffer_v1 *hr;
101         u16 res_max_size;
102         u32 uncopied_bytes;
103         int err = 0;
104
105         if (cmd != HPI_IOCTL_LINUX)
106                 return -EINVAL;
107
108         hm = kmalloc(sizeof(*hm), GFP_KERNEL);
109         hr = kmalloc(sizeof(*hr), GFP_KERNEL);
110         if (!hm || !hr) {
111                 err = -ENOMEM;
112                 goto out;
113         }
114
115         phpi_ioctl_data = (struct hpi_ioctl_linux __user *)arg;
116
117         /* Read the message and response pointers from user space.  */
118         if (get_user(puhm, &phpi_ioctl_data->phm)
119                 || get_user(puhr, &phpi_ioctl_data->phr)) {
120                 err = -EFAULT;
121                 goto out;
122         }
123
124         /* Now read the message size and data from user space.  */
125         if (get_user(hm->h.size, (u16 __user *)puhm)) {
126                 err = -EFAULT;
127                 goto out;
128         }
129         if (hm->h.size > sizeof(*hm))
130                 hm->h.size = sizeof(*hm);
131
132         /* printk(KERN_INFO "message size %d\n", hm->h.wSize); */
133
134         uncopied_bytes = copy_from_user(hm, puhm, hm->h.size);
135         if (uncopied_bytes) {
136                 HPI_DEBUG_LOG(ERROR, "uncopied bytes %d\n", uncopied_bytes);
137                 err = -EFAULT;
138                 goto out;
139         }
140
141         if (get_user(res_max_size, (u16 __user *)puhr)) {
142                 err = -EFAULT;
143                 goto out;
144         }
145         /* printk(KERN_INFO "user response size %d\n", res_max_size); */
146         if (res_max_size < sizeof(struct hpi_response_header)) {
147                 HPI_DEBUG_LOG(WARNING, "small res size %d\n", res_max_size);
148                 err = -EFAULT;
149                 goto out;
150         }
151
152         res_max_size = min_t(size_t, res_max_size, sizeof(*hr));
153
154         switch (hm->h.function) {
155         case HPI_SUBSYS_CREATE_ADAPTER:
156         case HPI_ADAPTER_DELETE:
157                 /* Application must not use these functions! */
158                 hr->h.size = sizeof(hr->h);
159                 hr->h.error = HPI_ERROR_INVALID_OPERATION;
160                 hr->h.function = hm->h.function;
161                 uncopied_bytes = copy_to_user(puhr, hr, hr->h.size);
162                 if (uncopied_bytes)
163                         err = -EFAULT;
164                 else
165                         err = 0;
166                 goto out;
167         }
168
169         hr->h.size = res_max_size;
170         if (hm->h.object == HPI_OBJ_SUBSYSTEM) {
171                 hpi_send_recv_f(&hm->m0, &hr->r0, file);
172         } else {
173                 u16 __user *ptr = NULL;
174                 u32 size = 0;
175                 /* -1=no data 0=read from user mem, 1=write to user mem */
176                 int wrflag = -1;
177                 struct hpi_adapter *pa = NULL;
178
179                 if (hm->h.adapter_index < ARRAY_SIZE(adapters))
180                         pa = &adapters[array_index_nospec(hm->h.adapter_index,
181                                                           ARRAY_SIZE(adapters))];
182
183                 if (!pa || !pa->adapter || !pa->adapter->type) {
184                         hpi_init_response(&hr->r0, hm->h.object,
185                                 hm->h.function, HPI_ERROR_BAD_ADAPTER_NUMBER);
186
187                         uncopied_bytes =
188                                 copy_to_user(puhr, hr, sizeof(hr->h));
189                         if (uncopied_bytes)
190                                 err = -EFAULT;
191                         else
192                                 err = 0;
193                         goto out;
194                 }
195
196                 if (mutex_lock_interruptible(&pa->mutex)) {
197                         err = -EINTR;
198                         goto out;
199                 }
200
201                 /* Dig out any pointers embedded in the message.  */
202                 switch (hm->h.function) {
203                 case HPI_OSTREAM_WRITE:
204                 case HPI_ISTREAM_READ:{
205                                 /* Yes, sparse, this is correct. */
206                                 ptr = (u16 __user *)hm->m0.u.d.u.data.pb_data;
207                                 size = hm->m0.u.d.u.data.data_size;
208
209                                 /* Allocate buffer according to application request.
210                                    ?Is it better to alloc/free for the duration
211                                    of the transaction?
212                                  */
213                                 if (pa->buffer_size < size) {
214                                         HPI_DEBUG_LOG(DEBUG,
215                                                 "Realloc adapter %d stream "
216                                                 "buffer from %zd to %d\n",
217                                                 hm->h.adapter_index,
218                                                 pa->buffer_size, size);
219                                         if (pa->p_buffer) {
220                                                 pa->buffer_size = 0;
221                                                 vfree(pa->p_buffer);
222                                         }
223                                         pa->p_buffer = vmalloc(size);
224                                         if (pa->p_buffer)
225                                                 pa->buffer_size = size;
226                                         else {
227                                                 HPI_DEBUG_LOG(ERROR,
228                                                         "HPI could not allocate "
229                                                         "stream buffer size %d\n",
230                                                         size);
231
232                                                 mutex_unlock(&pa->mutex);
233                                                 err = -EINVAL;
234                                                 goto out;
235                                         }
236                                 }
237
238                                 hm->m0.u.d.u.data.pb_data = pa->p_buffer;
239                                 if (hm->h.function == HPI_ISTREAM_READ)
240                                         /* from card, WRITE to user mem */
241                                         wrflag = 1;
242                                 else
243                                         wrflag = 0;
244                                 break;
245                         }
246
247                 default:
248                         size = 0;
249                         break;
250                 }
251
252                 if (size && (wrflag == 0)) {
253                         uncopied_bytes =
254                                 copy_from_user(pa->p_buffer, ptr, size);
255                         if (uncopied_bytes)
256                                 HPI_DEBUG_LOG(WARNING,
257                                         "Missed %d of %d "
258                                         "bytes from user\n", uncopied_bytes,
259                                         size);
260                 }
261
262                 hpi_send_recv_f(&hm->m0, &hr->r0, file);
263
264                 if (size && (wrflag == 1)) {
265                         uncopied_bytes =
266                                 copy_to_user(ptr, pa->p_buffer, size);
267                         if (uncopied_bytes)
268                                 HPI_DEBUG_LOG(WARNING,
269                                         "Missed %d of %d " "bytes to user\n",
270                                         uncopied_bytes, size);
271                 }
272
273                 mutex_unlock(&pa->mutex);
274         }
275
276         /* on return response size must be set */
277         /*printk(KERN_INFO "response size %d\n", hr->h.wSize); */
278
279         if (!hr->h.size) {
280                 HPI_DEBUG_LOG(ERROR, "response zero size\n");
281                 err = -EFAULT;
282                 goto out;
283         }
284
285         if (hr->h.size > res_max_size) {
286                 HPI_DEBUG_LOG(ERROR, "response too big %d %d\n", hr->h.size,
287                         res_max_size);
288                 hr->h.error = HPI_ERROR_RESPONSE_BUFFER_TOO_SMALL;
289                 hr->h.specific_error = hr->h.size;
290                 hr->h.size = sizeof(hr->h);
291         }
292
293         uncopied_bytes = copy_to_user(puhr, hr, hr->h.size);
294         if (uncopied_bytes) {
295                 HPI_DEBUG_LOG(ERROR, "uncopied bytes %d\n", uncopied_bytes);
296                 err = -EFAULT;
297                 goto out;
298         }
299
300 out:
301         kfree(hm);
302         kfree(hr);
303         return err;
304 }
305
306 static int asihpi_irq_count;
307
308 static irqreturn_t asihpi_isr(int irq, void *dev_id)
309 {
310         struct hpi_adapter *a = dev_id;
311         int handled;
312
313         if (!a->adapter->irq_query_and_clear) {
314                 pr_err("asihpi_isr ASI%04X:%d no handler\n", a->adapter->type,
315                         a->adapter->index);
316                 return IRQ_NONE;
317         }
318
319         handled = a->adapter->irq_query_and_clear(a->adapter, 0);
320
321         if (!handled)
322                 return IRQ_NONE;
323
324         asihpi_irq_count++;
325         /* printk(KERN_INFO "asihpi_isr %d ASI%04X:%d irq handled\n",
326            asihpi_irq_count, a->adapter->type, a->adapter->index); */
327
328         if (a->interrupt_callback)
329                 a->interrupt_callback(a);
330
331         return IRQ_HANDLED;
332 }
333
334 int asihpi_adapter_probe(struct pci_dev *pci_dev,
335                          const struct pci_device_id *pci_id)
336 {
337         int idx, nm, low_latency_mode = 0, irq_supported = 0;
338         int adapter_index;
339         unsigned int memlen;
340         struct hpi_message hm;
341         struct hpi_response hr;
342         struct hpi_adapter adapter;
343         struct hpi_pci pci = { 0 };
344
345         memset(&adapter, 0, sizeof(adapter));
346
347         dev_printk(KERN_DEBUG, &pci_dev->dev,
348                 "probe %04x:%04x,%04x:%04x,%04x\n", pci_dev->vendor,
349                 pci_dev->device, pci_dev->subsystem_vendor,
350                 pci_dev->subsystem_device, pci_dev->devfn);
351
352         if (pci_enable_device(pci_dev) < 0) {
353                 dev_err(&pci_dev->dev,
354                         "pci_enable_device failed, disabling device\n");
355                 return -EIO;
356         }
357
358         pci_set_master(pci_dev);        /* also sets latency timer if < 16 */
359
360         hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
361                 HPI_SUBSYS_CREATE_ADAPTER);
362         hpi_init_response(&hr, HPI_OBJ_SUBSYSTEM, HPI_SUBSYS_CREATE_ADAPTER,
363                 HPI_ERROR_PROCESSING_MESSAGE);
364
365         hm.adapter_index = HPI_ADAPTER_INDEX_INVALID;
366
367         nm = HPI_MAX_ADAPTER_MEM_SPACES;
368
369         for (idx = 0; idx < nm; idx++) {
370                 HPI_DEBUG_LOG(INFO, "resource %d %pR\n", idx,
371                         &pci_dev->resource[idx]);
372
373                 if (pci_resource_flags(pci_dev, idx) & IORESOURCE_MEM) {
374                         memlen = pci_resource_len(pci_dev, idx);
375                         pci.ap_mem_base[idx] =
376                                 ioremap(pci_resource_start(pci_dev, idx),
377                                 memlen);
378                         if (!pci.ap_mem_base[idx]) {
379                                 HPI_DEBUG_LOG(ERROR,
380                                         "ioremap failed, aborting\n");
381                                 /* unmap previously mapped pci mem space */
382                                 goto err;
383                         }
384                 }
385         }
386
387         pci.pci_dev = pci_dev;
388         hm.u.s.resource.bus_type = HPI_BUS_PCI;
389         hm.u.s.resource.r.pci = &pci;
390
391         /* call CreateAdapterObject on the relevant hpi module */
392         hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
393         if (hr.error)
394                 goto err;
395
396         adapter_index = hr.u.s.adapter_index;
397         adapter.adapter = hpi_find_adapter(adapter_index);
398
399         if (prealloc_stream_buf) {
400                 adapter.p_buffer = vmalloc(prealloc_stream_buf);
401                 if (!adapter.p_buffer) {
402                         HPI_DEBUG_LOG(ERROR,
403                                 "HPI could not allocate "
404                                 "kernel buffer size %d\n",
405                                 prealloc_stream_buf);
406                         goto err;
407                 }
408         }
409
410         hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
411                 HPI_ADAPTER_OPEN);
412         hm.adapter_index = adapter.adapter->index;
413         hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
414
415         if (hr.error) {
416                 HPI_DEBUG_LOG(ERROR, "HPI_ADAPTER_OPEN failed, aborting\n");
417                 goto err;
418         }
419
420         /* Check if current mode == Low Latency mode */
421         hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
422                 HPI_ADAPTER_GET_MODE);
423         hm.adapter_index = adapter.adapter->index;
424         hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
425
426         if (!hr.error
427                 && hr.u.ax.mode.adapter_mode == HPI_ADAPTER_MODE_LOW_LATENCY)
428                 low_latency_mode = 1;
429         else
430                 dev_info(&pci_dev->dev,
431                         "Adapter at index %d is not in low latency mode\n",
432                         adapter.adapter->index);
433
434         /* Check if IRQs are supported */
435         hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
436                 HPI_ADAPTER_GET_PROPERTY);
437         hm.adapter_index = adapter.adapter->index;
438         hm.u.ax.property_set.property = HPI_ADAPTER_PROPERTY_SUPPORTS_IRQ;
439         hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
440         if (hr.error || !hr.u.ax.property_get.parameter1) {
441                 dev_info(&pci_dev->dev,
442                         "IRQs not supported by adapter at index %d\n",
443                         adapter.adapter->index);
444         } else {
445                 irq_supported = 1;
446         }
447
448         /* WARNING can't init mutex in 'adapter'
449          * and then copy it to adapters[] ?!?!
450          */
451         adapters[adapter_index] = adapter;
452         mutex_init(&adapters[adapter_index].mutex);
453         pci_set_drvdata(pci_dev, &adapters[adapter_index]);
454
455         if (low_latency_mode && irq_supported) {
456                 if (!adapter.adapter->irq_query_and_clear) {
457                         dev_err(&pci_dev->dev,
458                                 "no IRQ handler for adapter %d, aborting\n",
459                                 adapter.adapter->index);
460                         goto err;
461                 }
462
463                 /* Disable IRQ generation on DSP side by setting the rate to 0 */
464                 hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
465                         HPI_ADAPTER_SET_PROPERTY);
466                 hm.adapter_index = adapter.adapter->index;
467                 hm.u.ax.property_set.property = HPI_ADAPTER_PROPERTY_IRQ_RATE;
468                 hm.u.ax.property_set.parameter1 = 0;
469                 hm.u.ax.property_set.parameter2 = 0;
470                 hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
471                 if (hr.error) {
472                         HPI_DEBUG_LOG(ERROR,
473                                 "HPI_ADAPTER_GET_MODE failed, aborting\n");
474                         goto err;
475                 }
476
477                 /* Note: request_irq calls asihpi_isr here */
478                 if (request_irq(pci_dev->irq, asihpi_isr, IRQF_SHARED,
479                                 "asihpi", &adapters[adapter_index])) {
480                         dev_err(&pci_dev->dev, "request_irq(%d) failed\n",
481                                 pci_dev->irq);
482                         goto err;
483                 }
484
485                 adapters[adapter_index].interrupt_mode = 1;
486
487                 dev_info(&pci_dev->dev, "using irq %d\n", pci_dev->irq);
488                 adapters[adapter_index].irq = pci_dev->irq;
489         } else {
490                 dev_info(&pci_dev->dev, "using polled mode\n");
491         }
492
493         dev_info(&pci_dev->dev, "probe succeeded for ASI%04X HPI index %d\n",
494                  adapter.adapter->type, adapter_index);
495
496         return 0;
497
498 err:
499         while (--idx >= 0) {
500                 if (pci.ap_mem_base[idx]) {
501                         iounmap(pci.ap_mem_base[idx]);
502                         pci.ap_mem_base[idx] = NULL;
503                 }
504         }
505
506         if (adapter.p_buffer) {
507                 adapter.buffer_size = 0;
508                 vfree(adapter.p_buffer);
509         }
510
511         HPI_DEBUG_LOG(ERROR, "adapter_probe failed\n");
512         return -ENODEV;
513 }
514
515 void asihpi_adapter_remove(struct pci_dev *pci_dev)
516 {
517         int idx;
518         struct hpi_message hm;
519         struct hpi_response hr;
520         struct hpi_adapter *pa;
521         struct hpi_pci pci;
522
523         pa = pci_get_drvdata(pci_dev);
524         pci = pa->adapter->pci;
525
526         /* Disable IRQ generation on DSP side */
527         hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
528                 HPI_ADAPTER_SET_PROPERTY);
529         hm.adapter_index = pa->adapter->index;
530         hm.u.ax.property_set.property = HPI_ADAPTER_PROPERTY_IRQ_RATE;
531         hm.u.ax.property_set.parameter1 = 0;
532         hm.u.ax.property_set.parameter2 = 0;
533         hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
534
535         hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
536                 HPI_ADAPTER_DELETE);
537         hm.adapter_index = pa->adapter->index;
538         hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
539
540         /* unmap PCI memory space, mapped during device init. */
541         for (idx = 0; idx < HPI_MAX_ADAPTER_MEM_SPACES; ++idx)
542                 iounmap(pci.ap_mem_base[idx]);
543
544         if (pa->irq)
545                 free_irq(pa->irq, pa);
546
547         vfree(pa->p_buffer);
548
549         if (1)
550                 dev_info(&pci_dev->dev,
551                          "remove %04x:%04x,%04x:%04x,%04x, HPI index %d\n",
552                          pci_dev->vendor, pci_dev->device,
553                          pci_dev->subsystem_vendor, pci_dev->subsystem_device,
554                          pci_dev->devfn, pa->adapter->index);
555
556         memset(pa, 0, sizeof(*pa));
557 }
558
559 void __init asihpi_init(void)
560 {
561         struct hpi_message hm;
562         struct hpi_response hr;
563
564         memset(adapters, 0, sizeof(adapters));
565
566         printk(KERN_INFO "ASIHPI driver " HPI_VER_STRING "\n");
567
568         hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
569                 HPI_SUBSYS_DRIVER_LOAD);
570         hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
571 }
572
573 void asihpi_exit(void)
574 {
575         struct hpi_message hm;
576         struct hpi_response hr;
577
578         hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
579                 HPI_SUBSYS_DRIVER_UNLOAD);
580         hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
581 }