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