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