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> 33 #include <linux/stringify.h> 34 35 #ifdef MODULE_FIRMWARE 36 MODULE_FIRMWARE("asihpi/dsp5000.bin"); 37 MODULE_FIRMWARE("asihpi/dsp6200.bin"); 38 MODULE_FIRMWARE("asihpi/dsp6205.bin"); 39 MODULE_FIRMWARE("asihpi/dsp6400.bin"); 40 MODULE_FIRMWARE("asihpi/dsp6600.bin"); 41 MODULE_FIRMWARE("asihpi/dsp8700.bin"); 42 MODULE_FIRMWARE("asihpi/dsp8900.bin"); 43 #endif 44 45 static int prealloc_stream_buf; 46 module_param(prealloc_stream_buf, int, S_IRUGO); 47 MODULE_PARM_DESC(prealloc_stream_buf, 48 "preallocate size for per-adapter stream buffer"); 49 50 /* Allow the debug level to be changed after module load. 51 E.g. echo 2 > /sys/module/asihpi/parameters/hpiDebugLevel 52 */ 53 module_param(hpi_debug_level, int, S_IRUGO | S_IWUSR); 54 MODULE_PARM_DESC(hpi_debug_level, "debug verbosity 0..5"); 55 56 /* List of adapters found */ 57 static struct hpi_adapter adapters[HPI_MAX_ADAPTERS]; 58 59 /* Wrapper function to HPI_Message to enable dumping of the 60 message and response types. 61 */ 62 static void hpi_send_recv_f(struct hpi_message *phm, struct hpi_response *phr, 63 struct file *file) 64 { 65 int adapter = phm->adapter_index; 66 67 if ((adapter >= HPI_MAX_ADAPTERS || adapter < 0) 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 struct hpi_adapter *pa = NULL; 109 int err = 0; 110 111 if (cmd != HPI_IOCTL_LINUX) 112 return -EINVAL; 113 114 hm = kmalloc(sizeof(*hm), GFP_KERNEL); 115 hr = kmalloc(sizeof(*hr), GFP_KERNEL); 116 if (!hm || !hr) { 117 err = -ENOMEM; 118 goto out; 119 } 120 121 phpi_ioctl_data = (struct hpi_ioctl_linux __user *)arg; 122 123 /* Read the message and response pointers from user space. */ 124 get_user(puhm, &phpi_ioctl_data->phm); 125 get_user(puhr, &phpi_ioctl_data->phr); 126 127 /* Now read the message size and data from user space. */ 128 get_user(hm->h.size, (u16 __user *)puhm); 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 get_user(res_max_size, (u16 __user *)puhr); 142 /* printk(KERN_INFO "user response size %d\n", res_max_size); */ 143 if (res_max_size < sizeof(struct hpi_response_header)) { 144 HPI_DEBUG_LOG(WARNING, "small res size %d\n", res_max_size); 145 err = -EFAULT; 146 goto out; 147 } 148 149 pa = &adapters[hm->h.adapter_index]; 150 hr->h.size = 0; 151 if (hm->h.object == HPI_OBJ_SUBSYSTEM) { 152 switch (hm->h.function) { 153 case HPI_SUBSYS_CREATE_ADAPTER: 154 case HPI_SUBSYS_DELETE_ADAPTER: 155 /* Application must not use these functions! */ 156 hr->h.size = sizeof(hr->h); 157 hr->h.error = HPI_ERROR_INVALID_OPERATION; 158 hr->h.function = hm->h.function; 159 uncopied_bytes = copy_to_user(puhr, hr, hr->h.size); 160 if (uncopied_bytes) 161 err = -EFAULT; 162 else 163 err = 0; 164 goto out; 165 166 default: 167 hpi_send_recv_f(&hm->m0, &hr->r0, file); 168 } 169 } else { 170 u16 __user *ptr = NULL; 171 u32 size = 0; 172 173 /* -1=no data 0=read from user mem, 1=write to user mem */ 174 int wrflag = -1; 175 u32 adapter = hm->h.adapter_index; 176 177 if ((hm->h.adapter_index > HPI_MAX_ADAPTERS) || (!pa->type)) { 178 hpi_init_response(&hr->r0, HPI_OBJ_ADAPTER, 179 HPI_ADAPTER_OPEN, 180 HPI_ERROR_BAD_ADAPTER_NUMBER); 181 182 uncopied_bytes = 183 copy_to_user(puhr, hr, sizeof(hr->h)); 184 if (uncopied_bytes) 185 err = -EFAULT; 186 else 187 err = 0; 188 goto out; 189 } 190 191 if (mutex_lock_interruptible(&adapters[adapter].mutex)) { 192 err = -EINTR; 193 goto out; 194 } 195 196 /* Dig out any pointers embedded in the message. */ 197 switch (hm->h.function) { 198 case HPI_OSTREAM_WRITE: 199 case HPI_ISTREAM_READ:{ 200 /* Yes, sparse, this is correct. */ 201 ptr = (u16 __user *)hm->m0.u.d.u.data.pb_data; 202 size = hm->m0.u.d.u.data.data_size; 203 204 /* Allocate buffer according to application request. 205 ?Is it better to alloc/free for the duration 206 of the transaction? 207 */ 208 if (pa->buffer_size < size) { 209 HPI_DEBUG_LOG(DEBUG, 210 "realloc adapter %d stream " 211 "buffer from %zd to %d\n", 212 hm->h.adapter_index, 213 pa->buffer_size, size); 214 if (pa->p_buffer) { 215 pa->buffer_size = 0; 216 vfree(pa->p_buffer); 217 } 218 pa->p_buffer = vmalloc(size); 219 if (pa->p_buffer) 220 pa->buffer_size = size; 221 else { 222 HPI_DEBUG_LOG(ERROR, 223 "HPI could not allocate " 224 "stream buffer size %d\n", 225 size); 226 227 mutex_unlock(&adapters 228 [adapter].mutex); 229 err = -EINVAL; 230 goto out; 231 } 232 } 233 234 hm->m0.u.d.u.data.pb_data = pa->p_buffer; 235 if (hm->h.function == HPI_ISTREAM_READ) 236 /* from card, WRITE to user mem */ 237 wrflag = 1; 238 else 239 wrflag = 0; 240 break; 241 } 242 243 default: 244 size = 0; 245 break; 246 } 247 248 if (size && (wrflag == 0)) { 249 uncopied_bytes = 250 copy_from_user(pa->p_buffer, ptr, size); 251 if (uncopied_bytes) 252 HPI_DEBUG_LOG(WARNING, 253 "missed %d of %d " 254 "bytes from user\n", uncopied_bytes, 255 size); 256 } 257 258 hpi_send_recv_f(&hm->m0, &hr->r0, file); 259 260 if (size && (wrflag == 1)) { 261 uncopied_bytes = 262 copy_to_user(ptr, pa->p_buffer, size); 263 if (uncopied_bytes) 264 HPI_DEBUG_LOG(WARNING, 265 "missed %d of %d " "bytes to user\n", 266 uncopied_bytes, size); 267 } 268 269 mutex_unlock(&adapters[adapter].mutex); 270 } 271 272 /* on return response size must be set */ 273 /*printk(KERN_INFO "response size %d\n", hr->h.wSize); */ 274 275 if (!hr->h.size) { 276 HPI_DEBUG_LOG(ERROR, "response zero size\n"); 277 err = -EFAULT; 278 goto out; 279 } 280 281 if (hr->h.size > res_max_size) { 282 HPI_DEBUG_LOG(ERROR, "response too big %d %d\n", hr->h.size, 283 res_max_size); 284 /*HPI_DEBUG_MESSAGE(ERROR, hm); */ 285 err = -EFAULT; 286 goto out; 287 } 288 289 uncopied_bytes = copy_to_user(puhr, hr, hr->h.size); 290 if (uncopied_bytes) { 291 HPI_DEBUG_LOG(ERROR, "uncopied bytes %d\n", uncopied_bytes); 292 err = -EFAULT; 293 goto out; 294 } 295 296 out: 297 kfree(hm); 298 kfree(hr); 299 return err; 300 } 301 302 int __devinit asihpi_adapter_probe(struct pci_dev *pci_dev, 303 const struct pci_device_id *pci_id) 304 { 305 int err, idx, nm; 306 unsigned int memlen; 307 struct hpi_message hm; 308 struct hpi_response hr; 309 struct hpi_adapter adapter; 310 struct hpi_pci pci; 311 312 memset(&adapter, 0, sizeof(adapter)); 313 314 printk(KERN_DEBUG "probe PCI device (%04x:%04x,%04x:%04x,%04x)\n", 315 pci_dev->vendor, pci_dev->device, pci_dev->subsystem_vendor, 316 pci_dev->subsystem_device, pci_dev->devfn); 317 318 hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM, 319 HPI_SUBSYS_CREATE_ADAPTER); 320 hpi_init_response(&hr, HPI_OBJ_SUBSYSTEM, HPI_SUBSYS_CREATE_ADAPTER, 321 HPI_ERROR_PROCESSING_MESSAGE); 322 323 hm.adapter_index = -1; /* an invalid index */ 324 325 /* fill in HPI_PCI information from kernel provided information */ 326 adapter.pci = pci_dev; 327 328 nm = HPI_MAX_ADAPTER_MEM_SPACES; 329 330 for (idx = 0; idx < nm; idx++) { 331 HPI_DEBUG_LOG(INFO, "resource %d %s %08llx-%08llx %04llx\n", 332 idx, pci_dev->resource[idx].name, 333 (unsigned long long)pci_resource_start(pci_dev, idx), 334 (unsigned long long)pci_resource_end(pci_dev, idx), 335 (unsigned long long)pci_resource_flags(pci_dev, idx)); 336 337 if (pci_resource_flags(pci_dev, idx) & IORESOURCE_MEM) { 338 memlen = pci_resource_len(pci_dev, idx); 339 adapter.ap_remapped_mem_base[idx] = 340 ioremap(pci_resource_start(pci_dev, idx), 341 memlen); 342 if (!adapter.ap_remapped_mem_base[idx]) { 343 HPI_DEBUG_LOG(ERROR, 344 "ioremap failed, aborting\n"); 345 /* unmap previously mapped pci mem space */ 346 goto err; 347 } 348 } 349 350 pci.ap_mem_base[idx] = adapter.ap_remapped_mem_base[idx]; 351 } 352 353 /* could replace Pci with direct pointer to pci_dev for linux 354 Instead wrap accessor functions for IDs etc. 355 Would it work for windows? 356 */ 357 pci.bus_number = pci_dev->bus->number; 358 pci.vendor_id = (u16)pci_dev->vendor; 359 pci.device_id = (u16)pci_dev->device; 360 pci.subsys_vendor_id = (u16)(pci_dev->subsystem_vendor & 0xffff); 361 pci.subsys_device_id = (u16)(pci_dev->subsystem_device & 0xffff); 362 pci.device_number = pci_dev->devfn; 363 pci.interrupt = pci_dev->irq; 364 pci.p_os_data = pci_dev; 365 366 hm.u.s.resource.bus_type = HPI_BUS_PCI; 367 hm.u.s.resource.r.pci = &pci; 368 369 /* call CreateAdapterObject on the relevant hpi module */ 370 hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL); 371 if (hr.error) 372 goto err; 373 374 if (prealloc_stream_buf) { 375 adapter.p_buffer = vmalloc(prealloc_stream_buf); 376 if (!adapter.p_buffer) { 377 HPI_DEBUG_LOG(ERROR, 378 "HPI could not allocate " 379 "kernel buffer size %d\n", 380 prealloc_stream_buf); 381 goto err; 382 } 383 } 384 385 adapter.index = hr.u.s.adapter_index; 386 adapter.type = hr.u.s.aw_adapter_list[adapter.index]; 387 hm.adapter_index = adapter.index; 388 389 err = hpi_adapter_open(NULL, adapter.index); 390 if (err) 391 goto err; 392 393 adapter.snd_card_asihpi = NULL; 394 /* WARNING can't init mutex in 'adapter' 395 * and then copy it to adapters[] ?!?! 396 */ 397 adapters[hr.u.s.adapter_index] = adapter; 398 mutex_init(&adapters[adapter.index].mutex); 399 pci_set_drvdata(pci_dev, &adapters[adapter.index]); 400 401 printk(KERN_INFO "probe found adapter ASI%04X HPI index #%d.\n", 402 adapter.type, adapter.index); 403 404 return 0; 405 406 err: 407 for (idx = 0; idx < HPI_MAX_ADAPTER_MEM_SPACES; idx++) { 408 if (adapter.ap_remapped_mem_base[idx]) { 409 iounmap(adapter.ap_remapped_mem_base[idx]); 410 adapter.ap_remapped_mem_base[idx] = NULL; 411 } 412 } 413 414 if (adapter.p_buffer) { 415 adapter.buffer_size = 0; 416 vfree(adapter.p_buffer); 417 } 418 419 HPI_DEBUG_LOG(ERROR, "adapter_probe failed\n"); 420 return -ENODEV; 421 } 422 423 void __devexit asihpi_adapter_remove(struct pci_dev *pci_dev) 424 { 425 int idx; 426 struct hpi_message hm; 427 struct hpi_response hr; 428 struct hpi_adapter *pa; 429 pa = (struct hpi_adapter *)pci_get_drvdata(pci_dev); 430 431 hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM, 432 HPI_SUBSYS_DELETE_ADAPTER); 433 hm.adapter_index = pa->index; 434 hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL); 435 436 /* unmap PCI memory space, mapped during device init. */ 437 for (idx = 0; idx < HPI_MAX_ADAPTER_MEM_SPACES; idx++) { 438 if (pa->ap_remapped_mem_base[idx]) { 439 iounmap(pa->ap_remapped_mem_base[idx]); 440 pa->ap_remapped_mem_base[idx] = NULL; 441 } 442 } 443 444 if (pa->p_buffer) { 445 pa->buffer_size = 0; 446 vfree(pa->p_buffer); 447 } 448 449 pci_set_drvdata(pci_dev, NULL); 450 /* 451 printk(KERN_INFO "PCI device (%04x:%04x,%04x:%04x,%04x)," 452 " HPI index # %d, removed.\n", 453 pci_dev->vendor, pci_dev->device, 454 pci_dev->subsystem_vendor, 455 pci_dev->subsystem_device, pci_dev->devfn, 456 pa->index); 457 */ 458 } 459 460 void __init asihpi_init(void) 461 { 462 struct hpi_message hm; 463 struct hpi_response hr; 464 465 memset(adapters, 0, sizeof(adapters)); 466 467 printk(KERN_INFO "ASIHPI driver %d.%02d.%02d\n", 468 HPI_VER_MAJOR(HPI_VER), HPI_VER_MINOR(HPI_VER), 469 HPI_VER_RELEASE(HPI_VER)); 470 471 hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM, 472 HPI_SUBSYS_DRIVER_LOAD); 473 hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL); 474 } 475 476 void asihpi_exit(void) 477 { 478 struct hpi_message hm; 479 struct hpi_response hr; 480 481 hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM, 482 HPI_SUBSYS_DRIVER_UNLOAD); 483 hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL); 484 } 485