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