Linux-libre 3.10.54-gnu
[librecmc/linux-libre.git] / sound / pci / asihpi / hpioctl.c
1 /*******************************************************************************
2
3     AudioScience HPI driver
4     Copyright (C) 1997-2011  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 "hpi_version.h"
25 #include "hpimsginit.h"
26 #include "hpidebug.h"
27 #include "hpimsgx.h"
28 #include "hpioctl.h"
29 #include "hpicmn.h"
30
31 #include <linux/fs.h>
32 #include <linux/slab.h>
33 #include <linux/moduleparam.h>
34 #include <asm/uaccess.h>
35 #include <linux/pci.h>
36 #include <linux/stringify.h>
37 #include <linux/module.h>
38
39 #ifdef MODULE_FIRMWARE
40 /*(DEBLOBBED)*/
41 #endif
42
43 static int prealloc_stream_buf;
44 module_param(prealloc_stream_buf, int, S_IRUGO);
45 MODULE_PARM_DESC(prealloc_stream_buf,
46         "Preallocate size for per-adapter stream buffer");
47
48 /* Allow the debug level to be changed after module load.
49  E.g.   echo 2 > /sys/module/asihpi/parameters/hpiDebugLevel
50 */
51 module_param(hpi_debug_level, int, S_IRUGO | S_IWUSR);
52 MODULE_PARM_DESC(hpi_debug_level, "debug verbosity 0..5");
53
54 /* List of adapters found */
55 static struct hpi_adapter adapters[HPI_MAX_ADAPTERS];
56
57 /* Wrapper function to HPI_Message to enable dumping of the
58    message and response types.
59 */
60 static void hpi_send_recv_f(struct hpi_message *phm, struct hpi_response *phr,
61         struct file *file)
62 {
63         if ((phm->adapter_index >= HPI_MAX_ADAPTERS)
64                 && (phm->object != HPI_OBJ_SUBSYSTEM))
65                 phr->error = HPI_ERROR_INVALID_OBJ_INDEX;
66         else
67                 hpi_send_recv_ex(phm, phr, file);
68 }
69
70 /* This is called from hpifunc.c functions, called by ALSA
71  * (or other kernel process) In this case there is no file descriptor
72  * available for the message cache code
73  */
74 void hpi_send_recv(struct hpi_message *phm, struct hpi_response *phr)
75 {
76         hpi_send_recv_f(phm, phr, HOWNER_KERNEL);
77 }
78
79 EXPORT_SYMBOL(hpi_send_recv);
80 /* for radio-asihpi */
81
82 int asihpi_hpi_release(struct file *file)
83 {
84         struct hpi_message hm;
85         struct hpi_response hr;
86
87 /* HPI_DEBUG_LOG(INFO,"hpi_release file %p, pid %d\n", file, current->pid); */
88         /* close the subsystem just in case the application forgot to. */
89         hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
90                 HPI_SUBSYS_CLOSE);
91         hpi_send_recv_ex(&hm, &hr, file);
92         return 0;
93 }
94
95 long asihpi_hpi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
96 {
97         struct hpi_ioctl_linux __user *phpi_ioctl_data;
98         void __user *puhm;
99         void __user *puhr;
100         union hpi_message_buffer_v1 *hm;
101         union hpi_response_buffer_v1 *hr;
102         u16 res_max_size;
103         u32 uncopied_bytes;
104         int err = 0;
105
106         if (cmd != HPI_IOCTL_LINUX)
107                 return -EINVAL;
108
109         hm = kmalloc(sizeof(*hm), GFP_KERNEL);
110         hr = kmalloc(sizeof(*hr), GFP_KERNEL);
111         if (!hm || !hr) {
112                 err = -ENOMEM;
113                 goto out;
114         }
115
116         phpi_ioctl_data = (struct hpi_ioctl_linux __user *)arg;
117
118         /* Read the message and response pointers from user space.  */
119         if (get_user(puhm, &phpi_ioctl_data->phm)
120                 || get_user(puhr, &phpi_ioctl_data->phr)) {
121                 err = -EFAULT;
122                 goto out;
123         }
124
125         /* Now read the message size and data from user space.  */
126         if (get_user(hm->h.size, (u16 __user *)puhm)) {
127                 err = -EFAULT;
128                 goto out;
129         }
130         if (hm->h.size > sizeof(*hm))
131                 hm->h.size = sizeof(*hm);
132
133         /* printk(KERN_INFO "message size %d\n", hm->h.wSize); */
134
135         uncopied_bytes = copy_from_user(hm, puhm, hm->h.size);
136         if (uncopied_bytes) {
137                 HPI_DEBUG_LOG(ERROR, "uncopied bytes %d\n", uncopied_bytes);
138                 err = -EFAULT;
139                 goto out;
140         }
141
142         if (get_user(res_max_size, (u16 __user *)puhr)) {
143                 err = -EFAULT;
144                 goto out;
145         }
146         /* printk(KERN_INFO "user response size %d\n", res_max_size); */
147         if (res_max_size < sizeof(struct hpi_response_header)) {
148                 HPI_DEBUG_LOG(WARNING, "small res size %d\n", res_max_size);
149                 err = -EFAULT;
150                 goto out;
151         }
152
153         switch (hm->h.function) {
154         case HPI_SUBSYS_CREATE_ADAPTER:
155         case HPI_ADAPTER_DELETE:
156                 /* Application must not use these functions! */
157                 hr->h.size = sizeof(hr->h);
158                 hr->h.error = HPI_ERROR_INVALID_OPERATION;
159                 hr->h.function = hm->h.function;
160                 uncopied_bytes = copy_to_user(puhr, hr, hr->h.size);
161                 if (uncopied_bytes)
162                         err = -EFAULT;
163                 else
164                         err = 0;
165                 goto out;
166         }
167
168         hr->h.size = res_max_size;
169         if (hm->h.object == HPI_OBJ_SUBSYSTEM) {
170                 hpi_send_recv_f(&hm->m0, &hr->r0, file);
171         } else {
172                 u16 __user *ptr = NULL;
173                 u32 size = 0;
174                 /* -1=no data 0=read from user mem, 1=write to user mem */
175                 int wrflag = -1;
176                 struct hpi_adapter *pa = NULL;
177
178                 if (hm->h.adapter_index < ARRAY_SIZE(adapters))
179                         pa = &adapters[hm->h.adapter_index];
180
181                 if (!pa || !pa->adapter || !pa->adapter->type) {
182                         hpi_init_response(&hr->r0, hm->h.object,
183                                 hm->h.function, HPI_ERROR_BAD_ADAPTER_NUMBER);
184
185                         uncopied_bytes =
186                                 copy_to_user(puhr, hr, sizeof(hr->h));
187                         if (uncopied_bytes)
188                                 err = -EFAULT;
189                         else
190                                 err = 0;
191                         goto out;
192                 }
193
194                 if (mutex_lock_interruptible(&pa->mutex)) {
195                         err = -EINTR;
196                         goto out;
197                 }
198
199                 /* Dig out any pointers embedded in the message.  */
200                 switch (hm->h.function) {
201                 case HPI_OSTREAM_WRITE:
202                 case HPI_ISTREAM_READ:{
203                                 /* Yes, sparse, this is correct. */
204                                 ptr = (u16 __user *)hm->m0.u.d.u.data.pb_data;
205                                 size = hm->m0.u.d.u.data.data_size;
206
207                                 /* Allocate buffer according to application request.
208                                    ?Is it better to alloc/free for the duration
209                                    of the transaction?
210                                  */
211                                 if (pa->buffer_size < size) {
212                                         HPI_DEBUG_LOG(DEBUG,
213                                                 "Realloc adapter %d stream "
214                                                 "buffer from %zd to %d\n",
215                                                 hm->h.adapter_index,
216                                                 pa->buffer_size, size);
217                                         if (pa->p_buffer) {
218                                                 pa->buffer_size = 0;
219                                                 vfree(pa->p_buffer);
220                                         }
221                                         pa->p_buffer = vmalloc(size);
222                                         if (pa->p_buffer)
223                                                 pa->buffer_size = size;
224                                         else {
225                                                 HPI_DEBUG_LOG(ERROR,
226                                                         "HPI could not allocate "
227                                                         "stream buffer size %d\n",
228                                                         size);
229
230                                                 mutex_unlock(&pa->mutex);
231                                                 err = -EINVAL;
232                                                 goto out;
233                                         }
234                                 }
235
236                                 hm->m0.u.d.u.data.pb_data = pa->p_buffer;
237                                 if (hm->h.function == HPI_ISTREAM_READ)
238                                         /* from card, WRITE to user mem */
239                                         wrflag = 1;
240                                 else
241                                         wrflag = 0;
242                                 break;
243                         }
244
245                 default:
246                         size = 0;
247                         break;
248                 }
249
250                 if (size && (wrflag == 0)) {
251                         uncopied_bytes =
252                                 copy_from_user(pa->p_buffer, ptr, size);
253                         if (uncopied_bytes)
254                                 HPI_DEBUG_LOG(WARNING,
255                                         "Missed %d of %d "
256                                         "bytes from user\n", uncopied_bytes,
257                                         size);
258                 }
259
260                 hpi_send_recv_f(&hm->m0, &hr->r0, file);
261
262                 if (size && (wrflag == 1)) {
263                         uncopied_bytes =
264                                 copy_to_user(ptr, pa->p_buffer, size);
265                         if (uncopied_bytes)
266                                 HPI_DEBUG_LOG(WARNING,
267                                         "Missed %d of %d " "bytes to user\n",
268                                         uncopied_bytes, size);
269                 }
270
271                 mutex_unlock(&pa->mutex);
272         }
273
274         /* on return response size must be set */
275         /*printk(KERN_INFO "response size %d\n", hr->h.wSize); */
276
277         if (!hr->h.size) {
278                 HPI_DEBUG_LOG(ERROR, "response zero size\n");
279                 err = -EFAULT;
280                 goto out;
281         }
282
283         if (hr->h.size > res_max_size) {
284                 HPI_DEBUG_LOG(ERROR, "response too big %d %d\n", hr->h.size,
285                         res_max_size);
286                 hr->h.error = HPI_ERROR_RESPONSE_BUFFER_TOO_SMALL;
287                 hr->h.specific_error = hr->h.size;
288                 hr->h.size = sizeof(hr->h);
289         }
290
291         uncopied_bytes = copy_to_user(puhr, hr, hr->h.size);
292         if (uncopied_bytes) {
293                 HPI_DEBUG_LOG(ERROR, "uncopied bytes %d\n", uncopied_bytes);
294                 err = -EFAULT;
295                 goto out;
296         }
297
298 out:
299         kfree(hm);
300         kfree(hr);
301         return err;
302 }
303
304 int asihpi_adapter_probe(struct pci_dev *pci_dev,
305                          const struct pci_device_id *pci_id)
306 {
307         int idx, nm;
308         int adapter_index;
309         unsigned int memlen;
310         struct hpi_message hm;
311         struct hpi_response hr;
312         struct hpi_adapter adapter;
313         struct hpi_pci pci;
314
315         memset(&adapter, 0, sizeof(adapter));
316
317         dev_printk(KERN_DEBUG, &pci_dev->dev,
318                 "probe %04x:%04x,%04x:%04x,%04x\n", pci_dev->vendor,
319                 pci_dev->device, pci_dev->subsystem_vendor,
320                 pci_dev->subsystem_device, pci_dev->devfn);
321
322         if (pci_enable_device(pci_dev) < 0) {
323                 dev_err(&pci_dev->dev,
324                         "pci_enable_device failed, disabling device\n");
325                 return -EIO;
326         }
327
328         pci_set_master(pci_dev);        /* also sets latency timer if < 16 */
329
330         hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
331                 HPI_SUBSYS_CREATE_ADAPTER);
332         hpi_init_response(&hr, HPI_OBJ_SUBSYSTEM, HPI_SUBSYS_CREATE_ADAPTER,
333                 HPI_ERROR_PROCESSING_MESSAGE);
334
335         hm.adapter_index = HPI_ADAPTER_INDEX_INVALID;
336
337         nm = HPI_MAX_ADAPTER_MEM_SPACES;
338
339         for (idx = 0; idx < nm; idx++) {
340                 HPI_DEBUG_LOG(INFO, "resource %d %pR\n", idx,
341                         &pci_dev->resource[idx]);
342
343                 if (pci_resource_flags(pci_dev, idx) & IORESOURCE_MEM) {
344                         memlen = pci_resource_len(pci_dev, idx);
345                         pci.ap_mem_base[idx] =
346                                 ioremap(pci_resource_start(pci_dev, idx),
347                                 memlen);
348                         if (!pci.ap_mem_base[idx]) {
349                                 HPI_DEBUG_LOG(ERROR,
350                                         "ioremap failed, aborting\n");
351                                 /* unmap previously mapped pci mem space */
352                                 goto err;
353                         }
354                 }
355         }
356
357         pci.pci_dev = pci_dev;
358         hm.u.s.resource.bus_type = HPI_BUS_PCI;
359         hm.u.s.resource.r.pci = &pci;
360
361         /* call CreateAdapterObject on the relevant hpi module */
362         hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
363         if (hr.error)
364                 goto err;
365
366         adapter_index = hr.u.s.adapter_index;
367         adapter.adapter = hpi_find_adapter(adapter_index);
368
369         if (prealloc_stream_buf) {
370                 adapter.p_buffer = vmalloc(prealloc_stream_buf);
371                 if (!adapter.p_buffer) {
372                         HPI_DEBUG_LOG(ERROR,
373                                 "HPI could not allocate "
374                                 "kernel buffer size %d\n",
375                                 prealloc_stream_buf);
376                         goto err;
377                 }
378         }
379
380         hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
381                 HPI_ADAPTER_OPEN);
382         hm.adapter_index = adapter.adapter->index;
383         hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
384
385         if (hr.error)
386                 goto err;
387
388         /* WARNING can't init mutex in 'adapter'
389          * and then copy it to adapters[] ?!?!
390          */
391         adapters[adapter_index] = adapter;
392         mutex_init(&adapters[adapter_index].mutex);
393         pci_set_drvdata(pci_dev, &adapters[adapter_index]);
394
395         dev_info(&pci_dev->dev, "probe succeeded for ASI%04X HPI index %d\n",
396                  adapter.adapter->type, adapter_index);
397
398         return 0;
399
400 err:
401         for (idx = 0; idx < HPI_MAX_ADAPTER_MEM_SPACES; idx++) {
402                 if (pci.ap_mem_base[idx]) {
403                         iounmap(pci.ap_mem_base[idx]);
404                         pci.ap_mem_base[idx] = NULL;
405                 }
406         }
407
408         if (adapter.p_buffer) {
409                 adapter.buffer_size = 0;
410                 vfree(adapter.p_buffer);
411         }
412
413         HPI_DEBUG_LOG(ERROR, "adapter_probe failed\n");
414         return -ENODEV;
415 }
416
417 void asihpi_adapter_remove(struct pci_dev *pci_dev)
418 {
419         int idx;
420         struct hpi_message hm;
421         struct hpi_response hr;
422         struct hpi_adapter *pa;
423         struct hpi_pci pci;
424
425         pa = pci_get_drvdata(pci_dev);
426         pci = pa->adapter->pci;
427
428         hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
429                 HPI_ADAPTER_DELETE);
430         hm.adapter_index = pa->adapter->index;
431         hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
432
433         /* unmap PCI memory space, mapped during device init. */
434         for (idx = 0; idx < HPI_MAX_ADAPTER_MEM_SPACES; idx++) {
435                 if (pci.ap_mem_base[idx])
436                         iounmap(pci.ap_mem_base[idx]);
437         }
438
439         if (pa->p_buffer)
440                 vfree(pa->p_buffer);
441
442         pci_set_drvdata(pci_dev, NULL);
443         if (1)
444                 dev_info(&pci_dev->dev,
445                          "remove %04x:%04x,%04x:%04x,%04x, HPI index %d\n",
446                          pci_dev->vendor, pci_dev->device,
447                          pci_dev->subsystem_vendor, pci_dev->subsystem_device,
448                          pci_dev->devfn, pa->adapter->index);
449
450         memset(pa, 0, sizeof(*pa));
451 }
452
453 void __init asihpi_init(void)
454 {
455         struct hpi_message hm;
456         struct hpi_response hr;
457
458         memset(adapters, 0, sizeof(adapters));
459
460         printk(KERN_INFO "ASIHPI driver " HPI_VER_STRING "\n");
461
462         hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
463                 HPI_SUBSYS_DRIVER_LOAD);
464         hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
465 }
466
467 void asihpi_exit(void)
468 {
469         struct hpi_message hm;
470         struct hpi_response hr;
471
472         hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
473                 HPI_SUBSYS_DRIVER_UNLOAD);
474         hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
475 }