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