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