Debian Codegraph @

drivers/dahdi/voicebus/GpakCust.c

raw symbols SCIP
1 /*
2 * Copyright (c) 2005, Adaptive Digital Technologies, Inc.
3 * Copyright (c) 2005-2010, Digium Incorporated
4 *
5 * File Name: GpakCust.c
6 *
7 * Description:
8 * This file contains host system dependent functions to support generic
9 * G.PAK API functions. The file is integrated into the host processor
10 * connected to C55x G.PAK DSPs via a Host Port Interface.
11 *
12 * Note: This file is supplied by Adaptive Digital Technologies and
13 * modified by Digium in order to support the VPMADT032 modules.
14 *
15 * This program has been released under the terms of the GPL version 2 by
16 * permission of Adaptive Digital Technologies, Inc.
17 *
18 */
19
20 /*
21 * See http://www.asterisk.org for more information about
22 * the Asterisk project. Please do not directly contact
23 * any of the maintainers of this project for assistance;
24 * the project provides a web site, mailing lists and IRC
25 * channels for your use.
26 *
27 * This program is free software, distributed under the terms of
28 * the GNU General Public License Version 2 as published by the
29 * Free Software Foundation. See the LICENSE file included with
30 * this program for more details.
31 */
32
33 #include <linux/version.h>
34 #include <linux/types.h>
35 #include <linux/pci.h>
36 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
37 #include <linux/semaphore.h>
38 #else
39 #include <asm/semaphore.h>
40 #endif
41 #include <linux/slab.h>
42
43 #include <dahdi/kernel.h>
44 #include <dahdi/user.h>
45
46 #include "GpakCust.h"
47 #include "GpakApi.h"
48
49 #include "adt_lec.h"
50 #include "voicebus.h"
51 #include "vpmadtreg.h"
52
53 static DEFINE_SPINLOCK(ifacelock);
54 static struct vpmadt032 *ifaces[MAX_DSP_CORES];
55
56 #define vpm_info(vpm, format, arg...) \
57 dev_info(&vpm->vb->pdev->dev , format , ## arg)
58
59 static inline struct vpmadt032 *find_iface(const unsigned short dspid)
60 {
61 struct vpmadt032 *ret;
62
63 spin_lock(&ifacelock);
64 if (ifaces[dspid]) {
65 ret = ifaces[dspid];
66 } else {
67 ret = NULL;
68 }
69 spin_unlock(&ifacelock);
70 return ret;
71 }
72
73 static struct vpmadt032_cmd *vpmadt032_get_free_cmd(struct vpmadt032 *vpm)
74 {
75 struct vpmadt032_cmd *cmd;
76 might_sleep();
77 cmd = kmalloc(sizeof(struct vpmadt032_cmd), GFP_KERNEL);
78 if (unlikely(!cmd))
79 return NULL;
80 memset(cmd, 0, sizeof(*cmd));
81 init_completion(&cmd->complete);
82 return cmd;
83 }
84
85 /* Wait for any outstanding commands to the VPMADT032 to complete */
86 static inline int vpmadt032_io_wait(struct vpmadt032 *vpm)
87 {
88 unsigned long flags;
89 int empty;
90 while (1) {
91 spin_lock_irqsave(&vpm->list_lock, flags);
92 empty = list_empty(&vpm->pending_cmds) && list_empty(&vpm->active_cmds);
93 spin_unlock_irqrestore(&vpm->list_lock, flags);
94 if (empty) {
95 break;
96 } else {
97 msleep(1);
98 }
99 }
100 return 0;
101 }
102
103 /* Issue a read command to a register on the VPMADT032. We'll get the results
104 * later. */
105 static struct vpmadt032_cmd *vpmadt032_getreg_full_async(struct vpmadt032 *vpm, int pagechange,
106 unsigned short addr)
107 {
108 unsigned long flags;
109 struct vpmadt032_cmd *cmd;
110 cmd = vpmadt032_get_free_cmd(vpm);
111 if (!cmd)
112 return NULL;
113 cmd->desc = (pagechange) ? __VPM150M_RWPAGE | __VPM150M_RD : __VPM150M_RD;
114 cmd->address = addr;
115 cmd->data = 0;
116 spin_lock_irqsave(&vpm->list_lock, flags);
117 list_add_tail(&cmd->node, &vpm->pending_cmds);
118 spin_unlock_irqrestore(&vpm->list_lock, flags);
119 return cmd;
120 }
121
122 /* Get the results from a previous call to vpmadt032_getreg_full_async. */
123 static int vpmadt032_getreg_full_return(struct vpmadt032 *vpm, int pagechange,
124 u16 addr, u16 *outbuf, struct vpmadt032_cmd *cmd)
125 {
126 unsigned long flags;
127 unsigned long ret;
128 BUG_ON(!cmd);
129
130 /* We'll wait for 2s */
131 ret = wait_for_completion_timeout(&cmd->complete, HZ*2);
132 if (unlikely(!ret)) {
133 spin_lock_irqsave(&vpm->list_lock, flags);
134 list_del(&cmd->node);
135 spin_unlock_irqrestore(&vpm->list_lock, flags);
136 kfree(cmd);
137 return -EIO;
138 }
139
140 if (cmd->desc & __VPM150M_FIN) {
141 *outbuf = cmd->data;
142 cmd->desc = 0;
143 ret = 0;
144 }
145
146 list_del(&cmd->node);
147 kfree(cmd);
148 return 0;
149 }
150
151 /* Read one of the registers on the VPMADT032 */
152 static int vpmadt032_getreg_full(struct vpmadt032 *vpm, int pagechange, u16 addr, u16 *outbuf)
153 {
154 struct vpmadt032_cmd *cmd;
155 cmd = vpmadt032_getreg_full_async(vpm, pagechange, addr);
156 if (unlikely(!cmd)) {
157 return -ENOMEM;
158 }
159 return vpmadt032_getreg_full_return(vpm, pagechange, addr, outbuf, cmd);
160 }
161
162 static int vpmadt032_setreg_full(struct vpmadt032 *vpm, int pagechange, unsigned int addr,
163 u16 data)
164 {
165 unsigned long flags;
166 struct vpmadt032_cmd *cmd;
167 cmd = vpmadt032_get_free_cmd(vpm);
168 if (!cmd)
169 return -ENOMEM;
170 cmd->desc = cpu_to_le16((pagechange) ? (__VPM150M_WR|__VPM150M_RWPAGE) : __VPM150M_WR);
171 cmd->address = cpu_to_le16(addr);
172 cmd->data = cpu_to_le16(data);
173 spin_lock_irqsave(&vpm->list_lock, flags);
174 list_add_tail(&cmd->node, &vpm->pending_cmds);
175 spin_unlock_irqrestore(&vpm->list_lock, flags);
176 return 0;
177 }
178
179
180 static int vpmadt032_setpage(struct vpmadt032 *vpm, u16 addr)
181 {
182 addr &= 0xf;
183 /* We do not need to set the page if we're already on the page we're
184 * interested in. */
185 if (vpm->curpage == addr)
186 return 0;
187 else
188 vpm->curpage = addr;
189
190 return vpmadt032_setreg_full(vpm, 1, 0, addr);
191 }
192
193 static unsigned char vpmadt032_getpage(struct vpmadt032 *vpm)
194 {
195 unsigned short res;
196 const int pagechange = 1;
197 vpmadt032_getreg_full(vpm, pagechange, 0, &res);
198 return res;
199 }
200
201 static int vpmadt032_getreg(struct vpmadt032 *vpm, unsigned int addr, u16 *data)
202 {
203 unsigned short res;
204 vpmadt032_setpage(vpm, addr >> 16);
205 res = vpmadt032_getreg_full(vpm, 0, addr & 0xffff, data);
206 return res;
207 }
208
209 static int vpmadt032_setreg(struct vpmadt032 *vpm, unsigned int addr, u16 data)
210 {
211 int res;
212 vpmadt032_setpage(vpm, addr >> 16);
213 res = vpmadt032_setreg_full(vpm, 0, addr & 0xffff, data);
214 return res;
215 }
216
217 struct change_order {
218 struct list_head node;
219 unsigned int channel;
220 struct adt_lec_params params;
221 };
222
223 static struct change_order *alloc_change_order(void)
224 {
225 return kzalloc(sizeof(struct change_order), GFP_ATOMIC);
226 }
227
228 static void free_change_order(struct change_order *order)
229 {
230 kfree(order);
231 }
232
233 static int vpmadt032_control(struct vpmadt032 *vpm, unsigned short int channel,
234 GpakAlgCtrl_t control_code,
235 GPAK_AlgControlStat_t *pstatus)
236 {
237 gpakAlgControlStat_t stat;
238 int retry = 4;
239 while (retry--) {
240 stat = gpakAlgControl(vpm->dspid, channel,
241 control_code, pstatus);
242
243 if (AcDspCommFailure == stat)
244 msleep(5);
245 else
246 break;
247 }
248
249 return stat;
250 }
251
252 static int vpmadt032_enable_ec(struct vpmadt032 *vpm, int channel,
253 enum adt_companding companding)
254 {
255 int res;
256 GPAK_AlgControlStat_t pstatus;
257 GpakAlgCtrl_t control;
258
259 control = (ADT_COMP_ALAW == companding) ? EnableALawSwCompanding :
260 EnableMuLawSwCompanding;
261
262 if (vpm->options.debug & DEBUG_VPMADT032_ECHOCAN) {
263 const char *law;
264 law = (control == EnableMuLawSwCompanding) ? "MuLaw" : "ALaw";
265 vpm_info(vpm, "Enabling ecan on channel: %d (%s)\n",
266 channel, law);
267 }
268 res = vpmadt032_control(vpm, channel, control, &pstatus);
269 if (res) {
270 vpm_info(vpm, "Unable to set SW Companding on "
271 "channel %d (reason %d)\n", channel, res);
272 }
273 res = vpmadt032_control(vpm, channel, EnableEcanA, &pstatus);
274 return res;
275 }
276
277 static int vpmadt032_disable_ec(struct vpmadt032 *vpm, int channel)
278 {
279 int res;
280 GPAK_AlgControlStat_t pstatus;
281
282 if (vpm->options.debug & DEBUG_VPMADT032_ECHOCAN)
283 vpm_info(vpm, "Disabling ecan on channel: %d\n", channel);
284
285 res = vpmadt032_control(vpm, channel, BypassSwCompanding, &pstatus);
286 if (res) {
287 vpm_info(vpm, "Unable to disable sw companding on "
288 "echo cancellation channel %d (reason %d)\n",
289 channel, res);
290 }
291 res = vpmadt032_control(vpm, channel, BypassEcanA, &pstatus);
292 return res;
293 }
294
295 static struct change_order *get_next_order(struct vpmadt032 *vpm)
296 {
297 struct change_order *order;
298
299 spin_lock(&vpm->change_list_lock);
300 if (!list_empty(&vpm->change_list)) {
301 order = list_entry(vpm->change_list.next,
302 struct change_order, node);
303 list_del(&order->node);
304 } else {
305 order = NULL;
306 }
307 spin_unlock(&vpm->change_list_lock);
308
309 return order;
310 }
311
312 static int nlp_settings_changed(const struct adt_lec_params *a,
313 const struct adt_lec_params *b)
314 {
315 return ((a->nlp_type != b->nlp_type) ||
316 (a->nlp_threshold != b->nlp_threshold) ||
317 (a->nlp_max_suppress != b->nlp_max_suppress));
318 }
319
320 static int echocan_on(const struct adt_lec_params *new,
321 const struct adt_lec_params *old)
322 {
323 return ((new->tap_length != old->tap_length) &&
324 (new->tap_length > 0));
325 }
326
327 static int echocan_off(const struct adt_lec_params *new,
328 const struct adt_lec_params *old)
329 {
330 return ((new->tap_length != old->tap_length) &&
331 (0 == new->tap_length));
332 }
333
334 static void update_channel_config(struct vpmadt032 *vpm, unsigned int channel,
335 struct adt_lec_params *desired)
336 {
337 int res;
338 GPAK_AlgControlStat_t pstatus;
339 GPAK_ChannelConfigStat_t cstatus;
340 GPAK_TearDownChanStat_t tstatus;
341 GpakChannelConfig_t chanconfig;
342
343 if (vpm->options.debug & DEBUG_VPMADT032_ECHOCAN) {
344 vpm_info(vpm, "Reconfiguring chan %d for nlp %d, "
345 "nlp_thresh %d, and max_supp %d\n", channel + 1,
346 desired->nlp_type, desired->nlp_threshold,
347 desired->nlp_max_suppress);
348 }
349
350 vpm->setchanconfig_from_state(vpm, channel, &chanconfig);
351
352 res = gpakTearDownChannel(vpm->dspid, channel, &tstatus);
353 if (res)
354 return;
355
356 res = gpakConfigureChannel(vpm->dspid, channel, tdmToTdm,
357 &chanconfig, &cstatus);
358 if (res)
359 return;
360
361 if (!desired->tap_length) {
362 res = vpmadt032_control(vpm, channel,
363 BypassSwCompanding, &pstatus);
364 if (res) {
365 vpm_info(vpm, "Unable to disable sw companding on "
366 "echo cancellation channel %d (reason %d)\n",
367 channel, res);
368 }
369 vpmadt032_control(vpm, channel, BypassEcanA, &pstatus);
370 }
371
372 return;
373 }
374
375 /**
376 * vpmadt032_bh - Changes the echocan parameters on the vpmadt032 module.
377 *
378 * This function is typically scheduled to run in the workqueue by the
379 * vpmadt032_echocan_with_params function. This is because communicating with
380 * the hardware can take some time while messages are sent to the VPMADT032
381 * module and the driver waits for the responses.
382 */
383 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
384 static void vpmadt032_bh(void *data)
385 {
386 struct vpmadt032 *vpm = data;
387 #else
388 static void vpmadt032_bh(struct work_struct *data)
389 {
390 struct vpmadt032 *vpm = container_of(data, struct vpmadt032, work);
391 #endif
392 struct change_order *order;
393
394 while ((order = get_next_order(vpm))) {
395
396 struct adt_lec_params *old;
397 struct adt_lec_params *new;
398 unsigned int channel;
399
400 channel = order->channel;
401 BUG_ON(channel >= ARRAY_SIZE(vpm->curecstate));
402 old = &vpm->curecstate[channel];
403 new = &order->params;
404
405 if (nlp_settings_changed(new, old))
406 update_channel_config(vpm, channel, new);
407 else if (echocan_on(new, old))
408 vpmadt032_enable_ec(vpm, channel, new->companding);
409 else if (echocan_off(new, old))
410 vpmadt032_disable_ec(vpm, channel);
411
412 *old = order->params;
413 free_change_order(order);
414 }
415 }
416
417 #include "adt_lec.c"
418 static void vpmadt032_check_and_schedule_update(struct vpmadt032 *vpm,
419 struct change_order *order)
420 {
421 struct change_order *cur;
422 struct change_order *n;
423
424 INIT_LIST_HEAD(&order->node);
425 spin_lock(&vpm->change_list_lock);
426 list_for_each_entry_safe(cur, n, &vpm->change_list, node) {
427 if (cur->channel == order->channel) {
428 list_replace(&cur->node, &order->node);
429 free_change_order(cur);
430 break;
431 }
432 }
433 if (list_empty(&order->node))
434 list_add_tail(&order->node, &vpm->change_list);
435 spin_unlock(&vpm->change_list_lock);
436
437 queue_work(vpm->wq, &vpm->work);
438 }
439 int vpmadt032_echocan_create(struct vpmadt032 *vpm, int channo,
440 enum adt_companding companding,
441 struct dahdi_echocanparams *ecp,
442 struct dahdi_echocanparam *p)
443 {
444 unsigned int ret;
445 struct change_order *order = alloc_change_order();
446 if (!order)
447 return -ENOMEM;
448
449 memcpy(&order->params, &vpm->curecstate[channo], sizeof(order->params));
450 ret = adt_lec_parse_params(&order->params, ecp, p);
451 if (ret) {
452 free_change_order(order);
453 return ret;
454 }
455
456 if (vpm->options.debug & DEBUG_VPMADT032_ECHOCAN) {
457 vpm_info(vpm, "Channel is %d length %d\n",
458 channo, ecp->tap_length);
459 }
460
461 /* The driver cannot control the number of taps on the VPMADT032
462 * module. Instead, it uses tap_length to enable or disable the echo
463 * cancellation. */
464 order->params.tap_length = (ecp->tap_length) ? 1 : 0;
465 order->params.companding = companding;
466 order->channel = channo;
467
468 vpmadt032_check_and_schedule_update(vpm, order);
469 return 0;
470 }
471 EXPORT_SYMBOL(vpmadt032_echocan_create);
472
473 void vpmadt032_echocan_free(struct vpmadt032 *vpm, int channo,
474 struct dahdi_echocan_state *ec)
475 {
476 struct change_order *order;
477 order = alloc_change_order();
478 WARN_ON(!order);
479 if (!order)
480 return;
481
482 adt_lec_init_defaults(&order->params, 0);
483 order->params.nlp_type = vpm->options.vpmnlptype;
484 order->params.nlp_threshold = vpm->options.vpmnlpthresh;
485 order->params.nlp_max_suppress = vpm->options.vpmnlpmaxsupp;
486 order->channel = channo;
487
488 if (vpm->options.debug & DEBUG_VPMADT032_ECHOCAN)
489 vpm_info(vpm, "Channel is %d length 0\n", channo);
490
491 vpmadt032_check_and_schedule_update(vpm, order);
492 }
493 EXPORT_SYMBOL(vpmadt032_echocan_free);
494
495 struct vpmadt032 *
496 vpmadt032_alloc(struct vpmadt032_options *options)
497 {
498 struct vpmadt032 *vpm;
499 int i;
500
501 might_sleep();
502
503 vpm = kzalloc(sizeof(*vpm), GFP_KERNEL);
504 if (!vpm)
505 return NULL;
506
507 /* Init our vpmadt032 struct */
508 memcpy(&vpm->options, options, sizeof(*options));
509 spin_lock_init(&vpm->list_lock);
510 spin_lock_init(&vpm->change_list_lock);
511 INIT_LIST_HEAD(&vpm->change_list);
512 INIT_LIST_HEAD(&vpm->pending_cmds);
513 INIT_LIST_HEAD(&vpm->active_cmds);
514 sema_init(&vpm->sem, 1);
515 vpm->curpage = 0x80;
516 vpm->dspid = -1;
517 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
518 INIT_WORK(&vpm->work, vpmadt032_bh, vpm);
519 #else
520 INIT_WORK(&vpm->work, vpmadt032_bh);
521 #endif
522
523 /* Do not use the global workqueue for processing these events. Some of
524 * the operations can take 100s of ms, most of that time spent sleeping.
525 * On single CPU systems, this unduly serializes operations accross
526 * multiple vpmadt032 instances. */
527 vpm->wq = create_singlethread_workqueue("vpmadt032");
528 if (!vpm->wq) {
529 kfree(vpm);
530 return NULL;
531 }
532
533 /* Place this structure in the ifaces array so that the DspId from the
534 * Gpak Library can be used to locate it. */
535 spin_lock(&ifacelock);
536 for (i=0; i<MAX_DSP_CORES; ++i) {
537 if (NULL == ifaces[i]) {
538 ifaces[i] = vpm;
539 vpm->dspid = i;
540 break;
541 }
542 }
543 spin_unlock(&ifacelock);
544
545 if (-1 == vpm->dspid) {
546 kfree(vpm);
547 dev_notice(&vpm->vb->pdev->dev, "Unable to initialize another vpmadt032 modules\n");
548 vpm = NULL;
549 }
550
551 return vpm;
552 }
553 EXPORT_SYMBOL(vpmadt032_alloc);
554
555 int vpmadt032_reset(struct vpmadt032 *vpm)
556 {
557 int res;
558 gpakPingDspStat_t pingstatus;
559 u16 version;
560 unsigned long stoptime;
561 struct device *const dev = &vpm->vb->pdev->dev;
562
563 might_sleep();
564
565 set_bit(VPM150M_HPIRESET, &vpm->control);
566 msleep(2000);
567 /* It should never take longer than 5 seconds. */
568 stoptime = jiffies + 3*HZ;
569 while (test_bit(VPM150M_HPIRESET, &vpm->control) &&
570 time_before(jiffies, stoptime))
571 msleep(1);
572
573 if (time_after(jiffies, stoptime)) {
574 dev_dbg(dev, "Detected failure to clear HPIRESET.\n");
575 return -EIO;
576 }
577
578 /* Set us up to page 0 */
579 vpmadt032_setpage(vpm, 0);
580
581 res = vpmadtreg_loadfirmware(vpm->vb);
582 if (res) {
583 dev_err(dev, "Failed to load VPMADT032 firmware.\n");
584 return res;
585 }
586 vpm->curpage = -1;
587
588 stoptime = jiffies + 3*HZ;
589 set_bit(VPM150M_SWRESET, &vpm->control);
590 while (test_bit(VPM150M_SWRESET, &vpm->control) &&
591 time_before(jiffies, stoptime))
592 msleep(1);
593
594 if (time_after(jiffies, stoptime)) {
595 dev_dbg(dev, "Detected failure to clear SWRESET.\n");
596 return -EIO;
597 }
598
599
600 /* Set us up to page 0 */
601 pingstatus = gpakPingDsp(vpm->dspid, &version);
602 if (!pingstatus) {
603 vpm_info(vpm, "VPM present and operational "
604 "(Firmware version %x)\n", version);
605 vpm->version = version;
606 res = 0;
607 } else {
608 res = -EIO;
609 }
610 return res;
611 }
612 EXPORT_SYMBOL(vpmadt032_reset);
613
614 /**
615 * vpmadt032_test - Check if there is a VPMADT032 present on voicebus device.
616 * @vpm: Allocated with vpmadt032_alloc previously.
617 * @vb: Voicebus structure to test on.
618 *
619 * Returns 0 if there is a device, otherwise -ENODEV.
620 *
621 */
622 int vpmadt032_test(struct vpmadt032 *vpm, struct voicebus *vb)
623 {
624 u8 reg;
625 int i, x;
626 struct device *dev = &vb->pdev->dev;
627
628 vpm->vb = vb;
629
630 if (vpm->options.debug & DEBUG_VPMADT032_ECHOCAN)
631 dev_info(dev, "VPMADT032 Testing page access: ");
632
633 for (i = 0; i < 0xf; i++) {
634 for (x = 0; x < 3; x++) {
635 vpmadt032_setpage(vpm, i);
636 reg = vpmadt032_getpage(vpm);
637 if (reg != i) {
638 if (vpm->options.debug &
639 DEBUG_VPMADT032_ECHOCAN) {
640 dev_err(dev, "Failed: Sent %x != %x " \
641 "VPMADT032 Failed HI page " \
642 "test\n", i, reg);
643 }
644 return -ENODEV;
645 }
646 }
647 }
648
649 if (vpm->options.debug & DEBUG_VPMADT032_ECHOCAN)
650 dev_info(dev, "Passed\n");
651
652 return 0;
653 }
654 EXPORT_SYMBOL(vpmadt032_test);
655
656 /**
657 * vpmadt032_init - Initialize and load VPMADT032 firmware.
658 * @vpm: Allocated with vpmadt032_alloc previously.
659 *
660 * Returns 0 on success. This must be called after vpmadt032_test already
661 * checked if there appears to be a VPMADT032 installed on the board.
662 *
663 */
664 int vpmadt032_init(struct vpmadt032 *vpm)
665 {
666 int i;
667 u16 reg;
668 int res = -EFAULT;
669 unsigned long stoptime;
670 struct device *dev;
671 gpakPingDspStat_t pingstatus;
672
673 BUG_ON(!vpm->setchanconfig_from_state);
674 BUG_ON(!vpm->wq);
675 BUG_ON(!vpm->vb);
676
677 might_sleep();
678
679 dev = &vpm->vb->pdev->dev;
680
681 stoptime = jiffies + 3*HZ;
682 set_bit(VPM150M_HPIRESET, &vpm->control);
683 while (test_bit(VPM150M_HPIRESET, &vpm->control) &&
684 time_before(jiffies, stoptime))
685 msleep(1);
686
687 if (time_after(jiffies, stoptime)) {
688 dev_dbg(dev, "Detected failure to clear HPIRESET.\n");
689 res = -EIO;
690 goto failed_exit;
691 }
692 msleep(250);
693
694 /* Set us up to page 0 */
695 vpmadt032_setpage(vpm, 0);
696 if (vpm->options.debug & DEBUG_VPMADT032_ECHOCAN)
697 dev_info(&vpm->vb->pdev->dev, "VPMADT032 now doing address test: ");
698
699 for (i = 0; i < 16; i++) {
700 int x;
701 for (x = 0; x < 2; x++) {
702 vpmadt032_setreg(vpm, 0x1000, i);
703 vpmadt032_getreg(vpm, 0x1000, &reg);
704 if (reg != i) {
705 printk(KERN_CONT "VPMADT032 Failed address test\n");
706 res = -EIO;
707 }
708 }
709 }
710
711 if (vpm->options.debug & DEBUG_VPMADT032_ECHOCAN)
712 printk(KERN_CONT "Passed\n");
713
714 stoptime = jiffies + 3*HZ;
715 set_bit(VPM150M_HPIRESET, &vpm->control);
716 while (test_bit(VPM150M_HPIRESET, &vpm->control) &&
717 time_before(jiffies, stoptime))
718 msleep(1);
719
720 if (time_after(jiffies, stoptime)) {
721 dev_dbg(dev, "Detected failure to clear SWRESET.\n");
722 res = -EIO;
723 goto failed_exit;
724 }
725
726 res = vpmadtreg_loadfirmware(vpm->vb);
727 if (res) {
728 dev_info(&vpm->vb->pdev->dev, "Failed to load the firmware.\n");
729 return res;
730 }
731 vpm->curpage = -1;
732
733 dev_info(&vpm->vb->pdev->dev, "Booting VPMADT032\n");
734
735 stoptime = jiffies + 3*HZ;
736 set_bit(VPM150M_SWRESET, &vpm->control);
737 while (test_bit(VPM150M_SWRESET, &vpm->control) &&
738 time_before(jiffies, stoptime))
739 msleep(1);
740
741 if (time_after(jiffies, stoptime)) {
742 dev_dbg(dev, "Detected failure to clear SWRESET.\n");
743 res = -EIO;
744 goto failed_exit;
745 }
746
747 pingstatus = gpakPingDsp(vpm->dspid, &vpm->version);
748
749 if (!pingstatus) {
750 dev_info(&vpm->vb->pdev->dev, "VPM present and operational "
751 "(Firmware version %x)\n", vpm->version);
752 } else {
753 dev_notice(&vpm->vb->pdev->dev, "VPMADT032 Failed! Unable to ping the DSP (%d)!\n", pingstatus);
754 res = -EIO;
755 goto failed_exit;
756 }
757
758 return 0;
759
760 failed_exit:
761 return res;
762 }
763 EXPORT_SYMBOL(vpmadt032_init);
764
765
766 void vpmadt032_get_default_parameters(struct GpakEcanParms *p)
767 {
768 memset(p, 0, sizeof(*p));
769
770 p->EcanTapLength = 1024;
771 p->EcanNlpType = DEFAULT_NLPTYPE;
772 p->EcanAdaptEnable = 1;
773
774 #ifdef CONFIG_DAHDI_NO_ECHOCAN_DISABLE
775 p->EcanG165DetEnable = 0;
776 #else
777 p->EcanG165DetEnable = 1;
778 #endif
779
780 p->EcanDblTalkThresh = 6;
781 p->EcanMaxDoubleTalkThres = 40;
782 p->EcanNlpThreshold = DEFAULT_NLPTHRESH;
783 p->EcanNlpConv = 18;
784 p->EcanNlpUnConv = 12;
785 p->EcanNlpMaxSuppress = DEFAULT_NLPMAXSUPP;
786 p->EcanCngThreshold = 43;
787 p->EcanAdaptLimit = 25;
788 p->EcanCrossCorrLimit = 15;
789 p->EcanNumFirSegments = 3;
790 p->EcanFirSegmentLen = 48;
791 p->EcanReconvergenceCheckEnable = 1;
792 p->EcanTandemOperationEnable = 0;
793 p->EcanMixedFourWireMode = 0;
794 p->EcanSaturationLevel = 3;
795 p->EcanNLPSaturationThreshold = 6;
796 }
797 EXPORT_SYMBOL(vpmadt032_get_default_parameters);
798
799 void vpmadt032_free(struct vpmadt032 *vpm)
800 {
801 unsigned long flags;
802 struct vpmadt032_cmd *cmd;
803 struct change_order *order;
804 LIST_HEAD(local_list);
805
806 if (!vpm)
807 return;
808
809 BUG_ON(!vpm->wq);
810
811 destroy_workqueue(vpm->wq);
812
813 /* Move all the commands onto the local list protected by the locks */
814 spin_lock_irqsave(&vpm->list_lock, flags);
815 list_splice(&vpm->pending_cmds, &local_list);
816 list_splice(&vpm->active_cmds, &local_list);
817 spin_unlock_irqrestore(&vpm->list_lock, flags);
818
819 while (!list_empty(&local_list)) {
820 cmd = list_entry(local_list.next, struct vpmadt032_cmd, node);
821 list_del(&cmd->node);
822 kfree(cmd);
823 }
824
825 spin_lock(&vpm->change_list_lock);
826 list_splice(&vpm->change_list, &local_list);
827 spin_unlock(&vpm->change_list_lock);
828
829 while (!list_empty(&local_list)) {
830 order = list_entry(local_list.next, struct change_order, node);
831 list_del(&order->node);
832 kfree(order);
833 }
834
835 BUG_ON(ifaces[vpm->dspid] != vpm);
836 spin_lock(&ifacelock);
837 ifaces[vpm->dspid] = NULL;
838 spin_unlock(&ifacelock);
839 kfree(vpm);
840 }
841 EXPORT_SYMBOL(vpmadt032_free);
842
843 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
844 * gpakReadDspMemory - Read DSP memory.
845 *
846 * FUNCTION
847 * This function reads a contiguous block of words from DSP memory starting at
848 * the specified address.
849 *
850 * RETURNS
851 * nothing
852 *
853 */
854 void gpakReadDspMemory(
855 unsigned short int DspId, /* DSP Identifier (0 to MAX_DSP_CORES-1) */
856 DSP_ADDRESS DspAddress, /* DSP's memory address of first word */
857 unsigned int NumWords, /* number of contiguous words to read */
858 DSP_WORD *pWordValues /* pointer to array of word values variable */
859 )
860 {
861 struct vpmadt032 *vpm = find_iface(DspId);
862 int i;
863 int ret;
864
865 vpmadt032_io_wait(vpm);
866 if ( NumWords < VPM150M_MAX_COMMANDS ) {
867 struct vpmadt032_cmd *cmds[VPM150M_MAX_COMMANDS];
868 memset(cmds, 0, sizeof(cmds));
869 vpmadt032_setpage(vpm, DspAddress >> 16);
870 DspAddress &= 0xffff;
871 for (i=0; i < NumWords; ++i) {
872 if (!(cmds[i] = vpmadt032_getreg_full_async(vpm,0,DspAddress+i))) {
873 return;
874 }
875 }
876 for (i=NumWords-1; i >=0; --i) {
877 ret = vpmadt032_getreg_full_return(vpm,0,DspAddress+i,&pWordValues[i],
878 cmds[i]);
879 if (0 != ret) {
880 return;
881 }
882 }
883 }
884 else {
885 for (i=0; i<NumWords; ++i) {
886 vpmadt032_getreg(vpm, DspAddress + i, &pWordValues[i]);
887 }
888 }
889 return;
890 }
891
892 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
893 * gpakWriteDspMemory - Write DSP memory.
894 *
895 * FUNCTION
896 * This function writes a contiguous block of words to DSP memory starting at
897 * the specified address.
898 *
899 * RETURNS
900 * nothing
901 *
902 */
903 void gpakWriteDspMemory(
904 unsigned short int DspId, /* DSP Identifier (0 to MAX_DSP_CORES-1) */
905 DSP_ADDRESS DspAddress, /* DSP's memory address of first word */
906 unsigned int NumWords, /* number of contiguous words to write */
907 DSP_WORD *pWordValues /* pointer to array of word values to write */
908 )
909 {
910
911 struct vpmadt032 *vpm = find_iface(DspId);
912 int i;
913
914 //dev_info(&vpm->vb->pdev->dev, "Writing %d words to memory\n", NumWords);
915 if (vpm) {
916 for (i = 0; i < NumWords; ++i) {
917 vpmadt032_setreg(vpm, DspAddress + i, pWordValues[i]);
918 }
919 #if 0
920 for (i = 0; i < NumWords; i++) {
921 if (wctdm_vpmadt032_getreg(wc, DspAddress + i) != pWordValues[i]) {
922 dev_notice(&vpm->vb->pdev->dev, "Error in write. Address %x is not %x\n", DspAddress + i, pWordValues[i]);
923 }
924 }
925 #endif
926 }
927 return;
928
929 }
930
931
932 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
933 * gpakHostDelay - Delay for a fixed time interval.
934 *
935 * FUNCTION
936 * This function delays for a fixed time interval before returning. The time
937 * interval is the Host Port Interface sampling period when polling a DSP for
938 * replies to command messages.
939 *
940 * RETURNS
941 * nothing
942 *
943 */
944 void gpakHostDelay(void)
945 {
946 }
947
948
949 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
950 * gpakLockAccess - Lock access to the specified DSP.
951 *
952 * FUNCTION
953 * This function aquires exclusive access to the specified DSP.
954 *
955 * RETURNS
956 * nothing
957 *
958 */
959 void gpakLockAccess(unsigned short DspId)
960 {
961 struct vpmadt032 *vpm;
962
963 vpm = find_iface(DspId);
964
965 if (!vpm)
966 return;
967
968 down(&vpm->sem);
969 return;
970 }
971
972
973 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
974 * gpakUnlockAccess - Unlock access to the specified DSP.
975 *
976 * FUNCTION
977 * This function releases exclusive access to the specified DSP.
978 *
979 * RETURNS
980 * nothing
981 *
982 */
983 void gpakUnlockAccess(unsigned short DspId)
984 {
985 struct vpmadt032 *vpm;
986
987 vpm = find_iface(DspId);
988
989 if (vpm) {
990 up(&vpm->sem);
991 }
992 }
993
994
995 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
996 * gpakReadFile - Read a block of bytes from a G.PAK Download file.
997 *
998 * FUNCTION
999 * This function reads a contiguous block of bytes from a G.PAK Download file
1000 * starting at the current file position.
1001 *
1002 * RETURNS
1003 * The number of bytes read from the file.
1004 * -1 indicates an error occurred.
1005 * 0 indicates all bytes have been read (end of file)
1006 *
1007 */
1008 int gpakReadFile(
1009 GPAK_FILE_ID FileId, /* G.PAK Download File Identifier */
1010 unsigned char *pBuffer, /* pointer to buffer for storing bytes */
1011 unsigned int NumBytes /* number of bytes to read */
1012 )
1013 {
1014 /* The firmware is loaded into the part by a closed-source firmware
1015 * loader, and therefore this function should never be called. */
1016 WARN_ON(1);
1017 return -1;
1018 }