blob: 9454932fc9c0a45d2bf0bc875f7244e57fb70a52 [file] [log] [blame]
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001/*******************************************************************************
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002 AudioScience HPI driver
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +13003 Common Linux HPI ioctl and module probe/remove functions
4
5 Copyright (C) 1997-2014 AudioScience Inc. <support@audioscience.com>
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02006
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 Blennerhassett719f82d2010-04-21 18:17:39 +020016*******************************************************************************/
17#define SOURCEFILE_NAME "hpioctl.c"
18
19#include "hpi_internal.h"
Eliot Blennerhassettf6baaec2011-12-22 13:38:31 +130020#include "hpi_version.h"
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020021#include "hpimsginit.h"
22#include "hpidebug.h"
23#include "hpimsgx.h"
24#include "hpioctl.h"
Eliot Blennerhassett6d0b8982011-04-05 20:55:47 +120025#include "hpicmn.h"
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020026
27#include <linux/fs.h>
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +130028#include <linux/interrupt.h>
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020029#include <linux/slab.h>
30#include <linux/moduleparam.h>
31#include <asm/uaccess.h>
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +130032#include <linux/pci.h>
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020033#include <linux/stringify.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040034#include <linux/module.h>
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020035
36#ifdef MODULE_FIRMWARE
37MODULE_FIRMWARE("asihpi/dsp5000.bin");
38MODULE_FIRMWARE("asihpi/dsp6200.bin");
39MODULE_FIRMWARE("asihpi/dsp6205.bin");
40MODULE_FIRMWARE("asihpi/dsp6400.bin");
41MODULE_FIRMWARE("asihpi/dsp6600.bin");
42MODULE_FIRMWARE("asihpi/dsp8700.bin");
43MODULE_FIRMWARE("asihpi/dsp8900.bin");
44#endif
45
46static int prealloc_stream_buf;
47module_param(prealloc_stream_buf, int, S_IRUGO);
48MODULE_PARM_DESC(prealloc_stream_buf,
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +130049 "Preallocate size for per-adapter stream buffer");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020050
51/* Allow the debug level to be changed after module load.
52 E.g. echo 2 > /sys/module/asihpi/parameters/hpiDebugLevel
53*/
54module_param(hpi_debug_level, int, S_IRUGO | S_IWUSR);
55MODULE_PARM_DESC(hpi_debug_level, "debug verbosity 0..5");
56
57/* List of adapters found */
58static 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*/
63static void hpi_send_recv_f(struct hpi_message *phm, struct hpi_response *phr,
64 struct file *file)
65{
Eliot Blennerhassett7036b922011-12-22 13:38:43 +130066 if ((phm->adapter_index >= HPI_MAX_ADAPTERS)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020067 && (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 */
77void hpi_send_recv(struct hpi_message *phm, struct hpi_response *phr)
78{
79 hpi_send_recv_f(phm, phr, HOWNER_KERNEL);
80}
81
82EXPORT_SYMBOL(hpi_send_recv);
83/* for radio-asihpi */
84
85int 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
98long 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 Blennerhassett719f82d2010-04-21 18:17:39 +0200107 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 Blennerhassett3285ea12011-02-10 17:25:58 +1300122 if (get_user(puhm, &phpi_ioctl_data->phm)
123 || get_user(puhr, &phpi_ioctl_data->phr)) {
Kulikov Vasiliyec9d04b2010-07-28 20:41:56 +0400124 err = -EFAULT;
125 goto out;
126 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200127
128 /* Now read the message size and data from user space. */
Kulikov Vasiliyec9d04b2010-07-28 20:41:56 +0400129 if (get_user(hm->h.size, (u16 __user *)puhm)) {
130 err = -EFAULT;
131 goto out;
132 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200133 if (hm->h.size > sizeof(*hm))
134 hm->h.size = sizeof(*hm);
135
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300136 /* printk(KERN_INFO "message size %d\n", hm->h.wSize); */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200137
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 Vasiliyec9d04b2010-07-28 20:41:56 +0400145 if (get_user(res_max_size, (u16 __user *)puhr)) {
146 err = -EFAULT;
147 goto out;
148 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200149 /* 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 Blennerhassett6d0b8982011-04-05 20:55:47 +1200156 switch (hm->h.function) {
157 case HPI_SUBSYS_CREATE_ADAPTER:
158 case HPI_ADAPTER_DELETE:
159 /* Application must not use these functions! */
160 hr->h.size = sizeof(hr->h);
161 hr->h.error = HPI_ERROR_INVALID_OPERATION;
162 hr->h.function = hm->h.function;
163 uncopied_bytes = copy_to_user(puhr, hr, hr->h.size);
164 if (uncopied_bytes)
165 err = -EFAULT;
166 else
167 err = 0;
168 goto out;
169 }
170
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300171 hr->h.size = res_max_size;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200172 if (hm->h.object == HPI_OBJ_SUBSYSTEM) {
Eliot Blennerhassett6d0b8982011-04-05 20:55:47 +1200173 hpi_send_recv_f(&hm->m0, &hr->r0, file);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200174 } else {
175 u16 __user *ptr = NULL;
176 u32 size = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200177 /* -1=no data 0=read from user mem, 1=write to user mem */
178 int wrflag = -1;
Eliot Blennerhassett7036b922011-12-22 13:38:43 +1300179 struct hpi_adapter *pa = NULL;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200180
Eliot Blennerhassett7036b922011-12-22 13:38:43 +1300181 if (hm->h.adapter_index < ARRAY_SIZE(adapters))
Eliot Blennerhassett08f984c2011-08-02 09:44:24 +1200182 pa = &adapters[hm->h.adapter_index];
Eliot Blennerhassett08f984c2011-08-02 09:44:24 +1200183
Eliot Blennerhassett7036b922011-12-22 13:38:43 +1300184 if (!pa || !pa->adapter || !pa->adapter->type) {
Eliot Blennerhassett08f984c2011-08-02 09:44:24 +1200185 hpi_init_response(&hr->r0, hm->h.object,
186 hm->h.function, HPI_ERROR_BAD_ADAPTER_NUMBER);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200187
188 uncopied_bytes =
189 copy_to_user(puhr, hr, sizeof(hr->h));
190 if (uncopied_bytes)
191 err = -EFAULT;
192 else
193 err = 0;
194 goto out;
195 }
196
Eliot Blennerhassett767cd362011-07-27 20:03:51 +1200197 if (mutex_lock_interruptible(&pa->mutex)) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200198 err = -EINTR;
199 goto out;
200 }
201
202 /* Dig out any pointers embedded in the message. */
203 switch (hm->h.function) {
204 case HPI_OSTREAM_WRITE:
205 case HPI_ISTREAM_READ:{
206 /* Yes, sparse, this is correct. */
207 ptr = (u16 __user *)hm->m0.u.d.u.data.pb_data;
208 size = hm->m0.u.d.u.data.data_size;
209
210 /* Allocate buffer according to application request.
211 ?Is it better to alloc/free for the duration
212 of the transaction?
213 */
214 if (pa->buffer_size < size) {
215 HPI_DEBUG_LOG(DEBUG,
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300216 "Realloc adapter %d stream "
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200217 "buffer from %zd to %d\n",
218 hm->h.adapter_index,
219 pa->buffer_size, size);
220 if (pa->p_buffer) {
221 pa->buffer_size = 0;
222 vfree(pa->p_buffer);
223 }
224 pa->p_buffer = vmalloc(size);
225 if (pa->p_buffer)
226 pa->buffer_size = size;
227 else {
228 HPI_DEBUG_LOG(ERROR,
229 "HPI could not allocate "
230 "stream buffer size %d\n",
231 size);
232
Eliot Blennerhassett767cd362011-07-27 20:03:51 +1200233 mutex_unlock(&pa->mutex);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200234 err = -EINVAL;
235 goto out;
236 }
237 }
238
239 hm->m0.u.d.u.data.pb_data = pa->p_buffer;
240 if (hm->h.function == HPI_ISTREAM_READ)
241 /* from card, WRITE to user mem */
242 wrflag = 1;
243 else
244 wrflag = 0;
245 break;
246 }
247
248 default:
249 size = 0;
250 break;
251 }
252
253 if (size && (wrflag == 0)) {
254 uncopied_bytes =
255 copy_from_user(pa->p_buffer, ptr, size);
256 if (uncopied_bytes)
257 HPI_DEBUG_LOG(WARNING,
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300258 "Missed %d of %d "
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200259 "bytes from user\n", uncopied_bytes,
260 size);
261 }
262
263 hpi_send_recv_f(&hm->m0, &hr->r0, file);
264
265 if (size && (wrflag == 1)) {
266 uncopied_bytes =
267 copy_to_user(ptr, pa->p_buffer, size);
268 if (uncopied_bytes)
269 HPI_DEBUG_LOG(WARNING,
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300270 "Missed %d of %d " "bytes to user\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200271 uncopied_bytes, size);
272 }
273
Eliot Blennerhassett767cd362011-07-27 20:03:51 +1200274 mutex_unlock(&pa->mutex);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200275 }
276
277 /* on return response size must be set */
278 /*printk(KERN_INFO "response size %d\n", hr->h.wSize); */
279
280 if (!hr->h.size) {
281 HPI_DEBUG_LOG(ERROR, "response zero size\n");
282 err = -EFAULT;
283 goto out;
284 }
285
286 if (hr->h.size > res_max_size) {
287 HPI_DEBUG_LOG(ERROR, "response too big %d %d\n", hr->h.size,
288 res_max_size);
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300289 hr->h.error = HPI_ERROR_RESPONSE_BUFFER_TOO_SMALL;
290 hr->h.specific_error = hr->h.size;
291 hr->h.size = sizeof(hr->h);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200292 }
293
294 uncopied_bytes = copy_to_user(puhr, hr, hr->h.size);
295 if (uncopied_bytes) {
296 HPI_DEBUG_LOG(ERROR, "uncopied bytes %d\n", uncopied_bytes);
297 err = -EFAULT;
298 goto out;
299 }
300
301out:
302 kfree(hm);
303 kfree(hr);
304 return err;
305}
306
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300307static int asihpi_irq_count;
308
309static irqreturn_t asihpi_isr(int irq, void *dev_id)
310{
311 struct hpi_adapter *a = dev_id;
312 int handled;
313
314 if (!a->adapter->irq_query_and_clear) {
315 pr_err("asihpi_isr ASI%04X:%d no handler\n", a->adapter->type,
316 a->adapter->index);
317 return IRQ_NONE;
318 }
319
320 handled = a->adapter->irq_query_and_clear(a->adapter, 0);
321
322 if (!handled)
323 return IRQ_NONE;
324
325 asihpi_irq_count++;
326 /* printk(KERN_INFO "asihpi_isr %d ASI%04X:%d irq handled\n",
327 asihpi_irq_count, a->adapter->type, a->adapter->index); */
328
329 if (a->interrupt_callback)
330 a->interrupt_callback(a);
331
332 return IRQ_HANDLED;
333}
334
Bill Pembertone23e7a12012-12-06 12:35:10 -0500335int asihpi_adapter_probe(struct pci_dev *pci_dev,
336 const struct pci_device_id *pci_id)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200337{
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300338 int idx, nm, low_latency_mode = 0, irq_supported = 0;
Eliot Blennerhassett7036b922011-12-22 13:38:43 +1300339 int adapter_index;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200340 unsigned int memlen;
341 struct hpi_message hm;
342 struct hpi_response hr;
343 struct hpi_adapter adapter;
344 struct hpi_pci pci;
345
346 memset(&adapter, 0, sizeof(adapter));
347
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300348 dev_printk(KERN_DEBUG, &pci_dev->dev,
349 "probe %04x:%04x,%04x:%04x,%04x\n", pci_dev->vendor,
350 pci_dev->device, pci_dev->subsystem_vendor,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200351 pci_dev->subsystem_device, pci_dev->devfn);
352
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300353 if (pci_enable_device(pci_dev) < 0) {
Joe Perches4ee3bff2012-10-28 01:05:54 -0700354 dev_err(&pci_dev->dev,
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300355 "pci_enable_device failed, disabling device\n");
356 return -EIO;
357 }
358
359 pci_set_master(pci_dev); /* also sets latency timer if < 16 */
360
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200361 hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
362 HPI_SUBSYS_CREATE_ADAPTER);
363 hpi_init_response(&hr, HPI_OBJ_SUBSYSTEM, HPI_SUBSYS_CREATE_ADAPTER,
364 HPI_ERROR_PROCESSING_MESSAGE);
365
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300366 hm.adapter_index = HPI_ADAPTER_INDEX_INVALID;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200367
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200368 nm = HPI_MAX_ADAPTER_MEM_SPACES;
369
370 for (idx = 0; idx < nm; idx++) {
Eliot Blennerhassett42258da2011-04-05 20:55:48 +1200371 HPI_DEBUG_LOG(INFO, "resource %d %pR\n", idx,
372 &pci_dev->resource[idx]);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200373
374 if (pci_resource_flags(pci_dev, idx) & IORESOURCE_MEM) {
375 memlen = pci_resource_len(pci_dev, idx);
Eliot Blennerhassett7036b922011-12-22 13:38:43 +1300376 pci.ap_mem_base[idx] =
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200377 ioremap(pci_resource_start(pci_dev, idx),
378 memlen);
Eliot Blennerhassett7036b922011-12-22 13:38:43 +1300379 if (!pci.ap_mem_base[idx]) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200380 HPI_DEBUG_LOG(ERROR,
381 "ioremap failed, aborting\n");
382 /* unmap previously mapped pci mem space */
383 goto err;
384 }
385 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200386 }
387
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300388 pci.pci_dev = pci_dev;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200389 hm.u.s.resource.bus_type = HPI_BUS_PCI;
390 hm.u.s.resource.r.pci = &pci;
391
392 /* call CreateAdapterObject on the relevant hpi module */
393 hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
394 if (hr.error)
395 goto err;
396
Eliot Blennerhassett7036b922011-12-22 13:38:43 +1300397 adapter_index = hr.u.s.adapter_index;
398 adapter.adapter = hpi_find_adapter(adapter_index);
399
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200400 if (prealloc_stream_buf) {
401 adapter.p_buffer = vmalloc(prealloc_stream_buf);
402 if (!adapter.p_buffer) {
403 HPI_DEBUG_LOG(ERROR,
404 "HPI could not allocate "
405 "kernel buffer size %d\n",
406 prealloc_stream_buf);
407 goto err;
408 }
409 }
410
Eliot Blennerhassett6d0b8982011-04-05 20:55:47 +1200411 hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
412 HPI_ADAPTER_OPEN);
Eliot Blennerhassett7036b922011-12-22 13:38:43 +1300413 hm.adapter_index = adapter.adapter->index;
Eliot Blennerhassett6d0b8982011-04-05 20:55:47 +1200414 hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
415
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300416 if (hr.error) {
417 HPI_DEBUG_LOG(ERROR, "HPI_ADAPTER_OPEN failed, aborting\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200418 goto err;
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300419 }
420
421 /* Check if current mode == Low Latency mode */
422 hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
423 HPI_ADAPTER_GET_MODE);
424 hm.adapter_index = adapter.adapter->index;
425 hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
426
427 if (hr.error) {
428 HPI_DEBUG_LOG(ERROR,
429 "HPI_ADAPTER_GET_MODE failed, aborting\n");
430 goto err;
431 }
432
433 if (hr.u.ax.mode.adapter_mode == HPI_ADAPTER_MODE_LOW_LATENCY)
434 low_latency_mode = 1;
435
436 /* Check if IRQs are supported */
437 hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
438 HPI_ADAPTER_GET_PROPERTY);
439 hm.adapter_index = adapter.adapter->index;
440 hm.u.ax.property_set.property = HPI_ADAPTER_PROPERTY_SUPPORTS_IRQ;
441 hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
442 if (hr.error || !hr.u.ax.property_get.parameter1) {
443 dev_info(&pci_dev->dev,
444 "IRQs not supported by adapter at index %d\n",
445 adapter.adapter->index);
446 } else {
447 irq_supported = 1;
448 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200449
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200450 /* WARNING can't init mutex in 'adapter'
451 * and then copy it to adapters[] ?!?!
452 */
Eliot Blennerhassett7036b922011-12-22 13:38:43 +1300453 adapters[adapter_index] = adapter;
454 mutex_init(&adapters[adapter_index].mutex);
455 pci_set_drvdata(pci_dev, &adapters[adapter_index]);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200456
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300457 if (low_latency_mode && irq_supported) {
458 if (!adapter.adapter->irq_query_and_clear) {
459 dev_err(&pci_dev->dev,
460 "no IRQ handler for adapter %d, aborting\n",
461 adapter.adapter->index);
462 goto err;
463 }
464
465 /* Disable IRQ generation on DSP side by setting the rate to 0 */
466 hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
467 HPI_ADAPTER_SET_PROPERTY);
468 hm.adapter_index = adapter.adapter->index;
469 hm.u.ax.property_set.property = HPI_ADAPTER_PROPERTY_IRQ_RATE;
470 hm.u.ax.property_set.parameter1 = 0;
471 hm.u.ax.property_set.parameter2 = 0;
472 hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
473 if (hr.error) {
474 HPI_DEBUG_LOG(ERROR,
475 "HPI_ADAPTER_GET_MODE failed, aborting\n");
476 goto err;
477 }
478
479 /* Note: request_irq calls asihpi_isr here */
480 if (request_irq(pci_dev->irq, asihpi_isr, IRQF_SHARED,
481 "asihpi", &adapters[adapter_index])) {
482 dev_err(&pci_dev->dev, "request_irq(%d) failed\n",
483 pci_dev->irq);
484 goto err;
485 }
486
487 adapters[adapter_index].interrupt_mode = 1;
488
489 dev_info(&pci_dev->dev, "using irq %d\n", pci_dev->irq);
490 adapters[adapter_index].irq = pci_dev->irq;
491 } else {
492 dev_info(&pci_dev->dev, "using polled mode\n");
493 }
494
Joe Perches4ee3bff2012-10-28 01:05:54 -0700495 dev_info(&pci_dev->dev, "probe succeeded for ASI%04X HPI index %d\n",
496 adapter.adapter->type, adapter_index);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200497
498 return 0;
499
500err:
501 for (idx = 0; idx < HPI_MAX_ADAPTER_MEM_SPACES; idx++) {
Eliot Blennerhassett7036b922011-12-22 13:38:43 +1300502 if (pci.ap_mem_base[idx]) {
503 iounmap(pci.ap_mem_base[idx]);
504 pci.ap_mem_base[idx] = NULL;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200505 }
506 }
507
508 if (adapter.p_buffer) {
509 adapter.buffer_size = 0;
510 vfree(adapter.p_buffer);
511 }
512
513 HPI_DEBUG_LOG(ERROR, "adapter_probe failed\n");
514 return -ENODEV;
515}
516
Bill Pembertone23e7a12012-12-06 12:35:10 -0500517void asihpi_adapter_remove(struct pci_dev *pci_dev)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200518{
519 int idx;
520 struct hpi_message hm;
521 struct hpi_response hr;
522 struct hpi_adapter *pa;
Eliot Blennerhassett7036b922011-12-22 13:38:43 +1300523 struct hpi_pci pci;
524
Joe Perches5dbea6b2010-11-15 12:14:02 -0800525 pa = pci_get_drvdata(pci_dev);
Eliot Blennerhassett7036b922011-12-22 13:38:43 +1300526 pci = pa->adapter->pci;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200527
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300528 /* Disable IRQ generation on DSP side */
529 hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
530 HPI_ADAPTER_SET_PROPERTY);
531 hm.adapter_index = pa->adapter->index;
532 hm.u.ax.property_set.property = HPI_ADAPTER_PROPERTY_IRQ_RATE;
533 hm.u.ax.property_set.parameter1 = 0;
534 hm.u.ax.property_set.parameter2 = 0;
535 hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
536
Eliot Blennerhassett6d0b8982011-04-05 20:55:47 +1200537 hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
538 HPI_ADAPTER_DELETE);
Eliot Blennerhassett7036b922011-12-22 13:38:43 +1300539 hm.adapter_index = pa->adapter->index;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200540 hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
541
542 /* unmap PCI memory space, mapped during device init. */
543 for (idx = 0; idx < HPI_MAX_ADAPTER_MEM_SPACES; idx++) {
Eliot Blennerhassett7036b922011-12-22 13:38:43 +1300544 if (pci.ap_mem_base[idx])
545 iounmap(pci.ap_mem_base[idx]);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200546 }
547
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300548 if (pa->irq)
549 free_irq(pa->irq, pa);
550
Eliot Blennerhassettc188dec2011-02-10 17:26:18 +1300551 if (pa->p_buffer)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200552 vfree(pa->p_buffer);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200553
Eliot Blennerhassett3285ea12011-02-10 17:25:58 +1300554 if (1)
Joe Perches4ee3bff2012-10-28 01:05:54 -0700555 dev_info(&pci_dev->dev,
556 "remove %04x:%04x,%04x:%04x,%04x, HPI index %d\n",
557 pci_dev->vendor, pci_dev->device,
558 pci_dev->subsystem_vendor, pci_dev->subsystem_device,
559 pci_dev->devfn, pa->adapter->index);
Eliot Blennerhassettc188dec2011-02-10 17:26:18 +1300560
561 memset(pa, 0, sizeof(*pa));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200562}
563
564void __init asihpi_init(void)
565{
566 struct hpi_message hm;
567 struct hpi_response hr;
568
569 memset(adapters, 0, sizeof(adapters));
570
Eliot Blennerhassett1dd6aaa2010-07-06 08:37:07 +1200571 printk(KERN_INFO "ASIHPI driver " HPI_VER_STRING "\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200572
573 hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
574 HPI_SUBSYS_DRIVER_LOAD);
575 hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
576}
577
578void asihpi_exit(void)
579{
580 struct hpi_message hm;
581 struct hpi_response hr;
582
583 hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
584 HPI_SUBSYS_DRIVER_UNLOAD);
585 hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
586}