Debian Codegraph @

drivers/dahdi/voicebus/vpmoct.c

raw symbols SCIP
1 /*
2 * VPMOCT Driver.
3 *
4 * Written by Russ Meyerriecks <[email protected]>
5 *
6 * Copyright (C) 2010-2011 Digium, Inc.
7 *
8 * All rights reserved.
9 *
10 */
11
12 /*
13 * See http://www.asterisk.org for more information about
14 * the Asterisk project. Please do not directly contact
15 * any of the maintainers of this project for assistance;
16 * the project provides a web site, mailing lists and IRC
17 * channels for your use.
18 *
19 * This program is free software, distributed under the terms of
20 * the GNU General Public License Version 2 as published by the
21 * Free Software Foundation. See the LICENSE file included with
22 * this program for more details.
23 */
24
25 #include <linux/jiffies.h>
26 #include <linux/delay.h>
27 #include <linux/sched.h>
28 #include <linux/crc32.h>
29 #include <linux/slab.h>
30
31 #include "voicebus/vpmoct.h"
32 #include "linux/firmware.h"
33
34 struct vpmoct_header {
35 u8 header[6];
36 __le32 chksum;
37 u8 pad[20];
38 u8 major;
39 u8 minor;
40 } __packed;
41
42 static int _vpmoct_read(struct vpmoct *vpm, u8 address,
43 void *data, size_t size,
44 u8 *new_command, u8 *new_address)
45 {
46 struct vpmoct_cmd *cmd;
47 unsigned long flags;
48
49 if (unlikely(size >= ARRAY_SIZE(cmd->data))) {
50 memset(data, -1, size);
51 return -1;
52 }
53
54 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
55 if (!cmd) {
56 dev_info(vpm->dev, "Unable to allocate memory for vpmoct_cmd\n");
57 return 0;
58 }
59
60 init_completion(&cmd->complete);
61
62 cmd->command = 0x60 + size;
63 cmd->address = address;
64 cmd->chunksize = size;
65
66 spin_lock_irqsave(&vpm->list_lock, flags);
67 list_add_tail(&cmd->node, &vpm->pending_list);
68 spin_unlock_irqrestore(&vpm->list_lock, flags);
69
70 /* Wait for receiveprep to process our result */
71 if (!wait_for_completion_timeout(&cmd->complete, HZ/5)) {
72 spin_lock_irqsave(&vpm->list_lock, flags);
73 list_del(&cmd->node);
74 spin_unlock_irqrestore(&vpm->list_lock, flags);
75 kfree(cmd);
76 dev_err(vpm->dev, "vpmoct_read_byte cmd timed out\n");
77 return 0;
78 }
79
80 memcpy(data, &cmd->data[0], size);
81
82 if (new_command)
83 *new_command = cmd->command;
84 if (new_address)
85 *new_address = cmd->address;
86
87 kfree(cmd);
88 return 0;
89 }
90
91 static u8 vpmoct_read_byte(struct vpmoct *vpm, u8 address)
92 {
93 u8 val;
94 _vpmoct_read(vpm, address, &val, sizeof(val), NULL, NULL);
95 return val;
96 }
97
98 static u32 vpmoct_read_dword(struct vpmoct *vpm, u8 address)
99 {
100 __le32 val;
101 _vpmoct_read(vpm, address, &val, sizeof(val), NULL, NULL);
102 return le32_to_cpu(val);
103 }
104
105 static void vpmoct_write_byte(struct vpmoct *vpm, u8 address, u8 data)
106 {
107 struct vpmoct_cmd *cmd;
108 unsigned long flags;
109
110 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
111 if (!cmd) {
112 dev_info(vpm->dev, "Unable to allocate memory for vpmoct_cmd\n");
113 return;
114 }
115
116 cmd->command = 0x21;
117 cmd->address = address;
118 cmd->data[0] = data;
119 cmd->chunksize = 1;
120
121 spin_lock_irqsave(&vpm->list_lock, flags);
122 list_add_tail(&cmd->node, &vpm->pending_list);
123 spin_unlock_irqrestore(&vpm->list_lock, flags);
124 }
125
126 static void vpmoct_write_dword(struct vpmoct *vpm, u8 address, u32 data)
127 {
128 struct vpmoct_cmd *cmd;
129 unsigned long flags;
130
131 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
132 if (!cmd) {
133 dev_info(vpm->dev, "Unable to allocate memory for vpmoct_cmd\n");
134 return;
135 }
136
137 cmd->command = 0x20 + sizeof(data);
138 cmd->address = address;
139 *(__le32 *)(&cmd->data[0]) = cpu_to_le32(data);
140 cmd->chunksize = sizeof(data);
141
142 spin_lock_irqsave(&vpm->list_lock, flags);
143 list_add_tail(&cmd->node, &vpm->pending_list);
144 spin_unlock_irqrestore(&vpm->list_lock, flags);
145 }
146
147 static void vpmoct_write_chunk(struct vpmoct *vpm, u8 address,
148 const u8 *data, u8 chunksize)
149 {
150 struct vpmoct_cmd *cmd;
151 unsigned long flags;
152
153 if (unlikely(chunksize > ARRAY_SIZE(cmd->data)))
154 return;
155
156 cmd = kzalloc(sizeof(*cmd), GFP_ATOMIC);
157 if (unlikely(!cmd)) {
158 dev_info(vpm->dev, "Unable to allocate memory for vpmoct_cmd\n");
159 return;
160 }
161
162 cmd->command = 0x20 + chunksize;
163 cmd->address = address;
164 cmd->chunksize = chunksize;
165
166 memcpy(cmd->data, data, chunksize);
167
168 spin_lock_irqsave(&vpm->list_lock, flags);
169 list_add_tail(&cmd->node, &vpm->pending_list);
170 spin_unlock_irqrestore(&vpm->list_lock, flags);
171 }
172
173 static u8 vpmoct_resync(struct vpmoct *vpm)
174 {
175 unsigned long time;
176 u8 status = 0xff;
177 u8 address;
178 u8 command;
179
180 /* Poll the status register until it returns valid values
181 * This is because we have to wait on the bootloader to do
182 * its thing.
183 * Timeout after 3 seconds
184 */
185 time = jiffies + 3*HZ;
186 while (time_after(time, jiffies) && (0xff == status)) {
187 status = _vpmoct_read(vpm, VPMOCT_BOOT_STATUS, &status,
188 sizeof(status), &command, &address);
189
190 /* Throw out invalid statuses */
191 if ((0x55 != command) || (0xaa != address))
192 status = 0xff;
193 }
194
195 if ((status != 0xff) && status)
196 dev_info(vpm->dev, "Resync with status %x\n", status);
197
198 return status;
199 }
200
201 static inline short vpmoct_erase_flash(struct vpmoct *vpm)
202 {
203 short res;
204 vpmoct_write_byte(vpm, VPMOCT_BOOT_CMD, VPMOCT_BOOT_FLASH_ERASE);
205 res = vpmoct_resync(vpm);
206 if (res)
207 dev_info(vpm->dev, "Unable to erase flash\n");
208 return res;
209 }
210
211 static inline short
212 vpmoct_send_firmware_header(struct vpmoct *vpm, const struct firmware *fw)
213 {
214 unsigned short i;
215 short res;
216
217 /* Send the encrypted firmware header */
218 for (i = 0; i < VPMOCT_FIRM_HEADER_LEN; i++) {
219 vpmoct_write_byte(vpm, VPMOCT_BOOT_RAM+i,
220 fw->data[i + sizeof(struct vpmoct_header)]);
221 }
222 /* Decrypt header */
223 vpmoct_write_byte(vpm, VPMOCT_BOOT_CMD, VPMOCT_BOOT_DECRYPT);
224 res = vpmoct_resync(vpm);
225 if (res)
226 dev_info(vpm->dev, "Unable to send firmware header\n");
227 return res;
228 }
229
230 static inline short
231 vpmoct_send_firmware_body(struct vpmoct *vpm, const struct firmware *fw)
232 {
233 unsigned int i, ram_index, flash_index, flash_address;
234 const u8 *buf;
235 u8 chunksize;
236
237 /* Load the body of the firmware */
238 ram_index = 0;
239 flash_index = 0;
240 flash_address = 0;
241 for (i = VPMOCT_FIRM_HEADER_LEN*2; i < fw->size;) {
242 if (ram_index >= VPMOCT_BOOT_RAM_LEN) {
243 /* Tell bootloader to load ram buffer into buffer */
244 vpmoct_write_byte(vpm, VPMOCT_BOOT_CMD,
245 0x10 + flash_index);
246 /* Assuming the memory load doesn't take longer than 1
247 * eframe just insert a blank eframe before continuing
248 * the firmware load */
249 vpmoct_read_byte(vpm, VPMOCT_BOOT_STATUS);
250 ram_index = 0;
251 flash_index++;
252 }
253 if (flash_index >= VPMOCT_FLASH_BUF_SECTIONS) {
254 /* Tell the bootloader the memory address for load */
255 vpmoct_write_dword(vpm, VPMOCT_BOOT_ADDRESS1,
256 flash_address);
257 /* Tell the bootloader to load from flash buffer */
258 vpmoct_write_byte(vpm, VPMOCT_BOOT_CMD,
259 VPMOCT_BOOT_FLASH_COPY);
260 if (vpmoct_resync(vpm))
261 goto error;
262 flash_index = 0;
263 flash_address = i-VPMOCT_FIRM_HEADER_LEN*2;
264 }
265 /* Try to buffer for batch writes if possible */
266 chunksize = VPMOCT_BOOT_RAM_LEN - ram_index;
267 if (chunksize > VPMOCT_MAX_CHUNK)
268 chunksize = VPMOCT_MAX_CHUNK;
269
270 buf = &fw->data[i];
271 vpmoct_write_chunk(vpm, VPMOCT_BOOT_RAM+ram_index,
272 buf, chunksize);
273 ram_index += chunksize;
274 i += chunksize;
275 }
276
277 /* Flush remaining ram buffer to flash buffer */
278 vpmoct_write_byte(vpm, VPMOCT_BOOT_CMD,
279 VPMOCT_BOOT_FLASHLOAD + flash_index);
280 if (vpmoct_resync(vpm))
281 goto error;
282 /* Tell boot loader the memory address to flash load */
283 vpmoct_write_dword(vpm, VPMOCT_BOOT_ADDRESS1, flash_address);
284 /* Tell the bootloader to load flash from flash buffer */
285 vpmoct_write_byte(vpm, VPMOCT_BOOT_CMD, VPMOCT_BOOT_FLASH_COPY);
286 if (vpmoct_resync(vpm))
287 goto error;
288
289 return 0;
290
291 error:
292 dev_info(vpm->dev, "Unable to load firmware body\n");
293 return -1;
294 }
295
296 /**
297 * vpmoct_get_mode - Return the current operating mode of the VPMOCT032.
298 * @vpm: The vpm to query.
299 *
300 * Will be either BOOTLOADER, APPLICATION, or UNKNOWN.
301 *
302 */
303 static enum vpmoct_mode vpmoct_get_mode(struct vpmoct *vpm)
304 {
305 int i;
306 enum vpmoct_mode ret = UNKNOWN;
307 char identifier[11] = {0};
308
309 for (i = 0; i < ARRAY_SIZE(identifier) - 1; i++)
310 identifier[i] = vpmoct_read_byte(vpm, VPMOCT_IDENT+i);
311
312 if (!memcmp(identifier, "bootloader", sizeof(identifier) - 1))
313 ret = BOOTLOADER;
314 else if (!memcmp(identifier, "VPMOCT032\0", sizeof(identifier) - 1))
315 ret = APPLICATION;
316
317 dev_dbg(vpm->dev, "vpmoct identifier: %s\n", identifier);
318 return ret;
319 }
320
321
322 static inline short
323 vpmoct_check_firmware_crc(struct vpmoct *vpm, size_t size, u8 major, u8 minor)
324 {
325 short ret = 0;
326 u8 status;
327
328 /* Load firmware size */
329 vpmoct_write_dword(vpm, VPMOCT_BOOT_RAM, size);
330
331 /* Load firmware version */
332 vpmoct_write_byte(vpm, VPMOCT_BOOT_RAM+8, major);
333 vpmoct_write_byte(vpm, VPMOCT_BOOT_RAM+9, minor);
334
335 /* Validate the firmware load */
336 vpmoct_write_byte(vpm, VPMOCT_BOOT_CMD, VPMOCT_BOOT_IMAGE_VALIDATE);
337
338 status = vpmoct_resync(vpm);
339 if (status) {
340 dev_info(vpm->dev,
341 "vpmoct firmware CRC check failed: %x\n", status);
342 /* TODO: Try the load again */
343 ret = -1;
344 } else {
345
346 /* Switch to application code */
347 vpmoct_write_dword(vpm, VPMOCT_BOOT_ADDRESS2, 0xDEADBEEF);
348 vpmoct_write_byte(vpm, VPMOCT_BOOT_CMD, VPMOCT_BOOT_REBOOT);
349
350 msleep(250);
351 status = vpmoct_resync(vpm);
352
353 if (APPLICATION != vpmoct_get_mode(vpm)) {
354 dev_info(vpm->dev,
355 "vpmoct firmware failed to switch to "
356 "application. (%x)\n", status);
357 ret = -1;
358 } else {
359 vpm->mode = APPLICATION;
360 dev_info(vpm->dev,
361 "vpmoct firmware uploaded successfully\n");
362 }
363 }
364
365 return ret;
366 }
367
368 static inline short vpmoct_switch_to_boot(struct vpmoct *vpm)
369 {
370 vpmoct_write_dword(vpm, 0x74, 0x00009876);
371 vpmoct_write_byte(vpm, 0x71, 0x02);
372 if (vpmoct_resync(vpm)) {
373 dev_info(vpm->dev, "Failed to switch to bootloader\n");
374 return -1;
375 }
376 vpm->mode = BOOTLOADER;
377 return 0;
378 }
379
380 struct vpmoct_load_work {
381 struct vpmoct *vpm;
382 struct work_struct work;
383 struct workqueue_struct *wq;
384 load_complete_func_t load_complete;
385 bool operational;
386 };
387
388 /**
389 * vpmoct_load_complete_fn -
390 *
391 * This function should run in the context of one of the system workqueues so
392 * that it can destroy any workqueues that may have been created to setup a
393 * long running firmware load.
394 *
395 */
396 static void vpmoct_load_complete_fn(struct work_struct *data)
397 {
398 struct vpmoct_load_work *work =
399 container_of(data, struct vpmoct_load_work, work);
400 /* Do not touch work->vpm after calling load complete. It may have
401 * been freed in the function by the board driver. */
402 work->load_complete(work->vpm->dev, work->operational);
403 destroy_workqueue(work->wq);
404 kfree(work);
405 }
406
407 /**
408 * vpmoct_load_complete - Call the load_complete function in a system workqueue.
409 * @work:
410 * @operational: Whether the VPM is functioning or not.
411 *
412 */
413 static void
414 vpmoct_load_complete(struct vpmoct_load_work *work, bool operational)
415 {
416 work->operational = operational;
417
418 INIT_WORK(&work->work, vpmoct_load_complete_fn);
419 schedule_work(&work->work);
420 }
421
422 static bool is_valid_vpmoct_firmware(const struct firmware *fw)
423 {
424 const struct vpmoct_header *header =
425 (const struct vpmoct_header *)fw->data;
426 u32 crc = crc32(~0, &fw->data[10], fw->size - 10) ^ ~0;
427 return (!memcmp("DIGIUM", header->header, sizeof(header->header)) &&
428 (le32_to_cpu(header->chksum) == crc));
429 }
430
431 static void vpmoct_set_defaults(struct vpmoct *vpm)
432 {
433 vpmoct_write_dword(vpm, 0x40, 0);
434 vpmoct_write_dword(vpm, 0x30, 0);
435 }
436
437 static const char *const FIRMWARE_NAME = "dahdi-fw-vpmoct032.bin";
438 #if defined(HOTPLUG_FIRMWARE)
439 static int
440 vpmoct_request_firmware(const struct firmware **fw, struct device *dev)
441 {
442 return request_firmware(fw, FIRMWARE_NAME, dev);
443 }
444
445 static void vpmoct_release_firmware(const struct firmware *fw)
446 {
447 release_firmware(fw);
448 }
449 #else
450 static int
451 vpmoct_request_firmware(const struct firmware **fw_p, struct device *dev)
452 {
453 struct firmware *fw;
454 extern void _binary_dahdi_fw_vpmoct032_bin_size;
455 extern u8 _binary_dahdi_fw_vpmoct032_bin_start[];
456
457 *fw_p = fw = kzalloc(sizeof(*fw), GFP_KERNEL);
458 if (!fw)
459 return -ENOMEM;
460
461 fw->data = _binary_dahdi_fw_vpmoct032_bin_start;
462 /* Yes... this is weird. objcopy gives us a symbol containing
463 the size of the firmware, not a pointer a variable containing the
464 size. The only way we can get the value of the symbol is to take
465 its address, so we define it as a pointer and then cast that value
466 to the proper type. */
467 fw->size = (size_t) &_binary_dahdi_fw_vpmoct032_bin_size;
468
469 return 0;
470 }
471
472 static void vpmoct_release_firmware(const struct firmware *fw)
473 {
474 kfree(fw);
475 }
476 #endif
477
478 /**
479 * vpmoct_load_flash - Check the current flash version and possibly load.
480 * @vpm: The VPMOCT032 module to check / load.
481 *
482 */
483 static void vpmoct_load_flash(struct work_struct *data)
484 {
485 struct vpmoct_load_work *work =
486 container_of(data, struct vpmoct_load_work, work);
487 int res;
488 struct vpmoct *const vpm = work->vpm;
489 const struct firmware *fw;
490 const struct vpmoct_header *header;
491 char serial[VPMOCT_SERIAL_SIZE+1];
492 int i;
493
494 res = vpmoct_request_firmware(&fw, vpm->dev);
495 if (res) {
496 dev_warn(vpm->dev,
497 "vpmoct: Failed to load firmware from userspace! %d\n",
498 res);
499 header = NULL;
500 fw = NULL;
501 } else {
502 header = (const struct vpmoct_header *)fw->data;
503 }
504
505 if (vpm->mode == APPLICATION) {
506
507 /* Check the running application firmware
508 * for the proper version */
509 vpm->major = vpmoct_read_byte(vpm, VPMOCT_MAJOR);
510 vpm->minor = vpmoct_read_byte(vpm, VPMOCT_MINOR);
511 for (i = 0; i < VPMOCT_SERIAL_SIZE; i++)
512 serial[i] = vpmoct_read_byte(vpm, VPMOCT_SERIAL+i);
513 serial[VPMOCT_SERIAL_SIZE] = '\0';
514
515 dev_info(vpm->dev,
516 "vpmoct: Detected firmware v%d.%d Serial: %s\n",
517 vpm->major, vpm->minor,
518 (serial[0] != -1) ? serial : "(None)");
519
520 if (!fw) {
521 /* Again, we'll use the existing loaded firmware. */
522 vpmoct_set_defaults(vpm);
523 vpmoct_load_complete(work, true);
524 return;
525 }
526
527 if (!is_valid_vpmoct_firmware(fw)) {
528 dev_warn(vpm->dev,
529 "%s is invalid. Please reinstall.\n",
530 FIRMWARE_NAME);
531
532 /* Just use the old version of the fimware. */
533 vpmoct_release_firmware(fw);
534 vpmoct_set_defaults(vpm);
535 vpmoct_load_complete(work, true);
536 return;
537 }
538
539 if (vpm->minor == header->minor &&
540 vpm->major == header->major) {
541 /* Proper version is running */
542 vpmoct_release_firmware(fw);
543 vpmoct_set_defaults(vpm);
544 vpmoct_load_complete(work, true);
545 return;
546 } else {
547
548 /* Incorrect version of application code is
549 * loaded. Reset to bootloader mode */
550 if (vpmoct_switch_to_boot(vpm))
551 goto error;
552 }
553 }
554
555 if (!fw) {
556 vpmoct_load_complete(work, false);
557 return;
558 } else if (!is_valid_vpmoct_firmware(fw)) {
559 dev_warn(vpm->dev,
560 "%s is invalid. Please reinstall.\n", FIRMWARE_NAME);
561 goto error;
562 }
563
564 dev_info(vpm->dev, "vpmoct: Uploading firmware, v%d.%d. This can "\
565 "take up to 1 minute\n",
566 header->major, header->minor);
567 if (vpmoct_erase_flash(vpm))
568 goto error;
569 if (vpmoct_send_firmware_header(vpm, fw))
570 goto error;
571 if (vpmoct_send_firmware_body(vpm, fw))
572 goto error;
573 if (vpmoct_check_firmware_crc(vpm, fw->size-VPMOCT_FIRM_HEADER_LEN*2,
574 header->major, header->minor))
575 goto error;
576 vpmoct_release_firmware(fw);
577 vpmoct_set_defaults(vpm);
578 vpmoct_load_complete(work, true);
579 return;
580
581 error:
582 dev_info(vpm->dev, "Unable to load firmware\n");
583 vpmoct_release_firmware(fw);
584 /* TODO: Should we disable module if the firmware doesn't load? */
585 vpmoct_load_complete(work, false);
586 return;
587 }
588
589 struct vpmoct *vpmoct_alloc(void)
590 {
591 struct vpmoct *vpm;
592
593 vpm = kzalloc(sizeof(*vpm), GFP_KERNEL);
594 if (!vpm)
595 return NULL;
596
597 spin_lock_init(&vpm->list_lock);
598 INIT_LIST_HEAD(&vpm->pending_list);
599 INIT_LIST_HEAD(&vpm->active_list);
600 mutex_init(&vpm->mutex);
601 return vpm;
602 }
603 EXPORT_SYMBOL(vpmoct_alloc);
604
605 void vpmoct_free(struct vpmoct *vpm)
606 {
607 unsigned long flags;
608 struct vpmoct_cmd *cmd;
609 LIST_HEAD(list);
610
611 if (!vpm)
612 return;
613
614 spin_lock_irqsave(&vpm->list_lock, flags);
615 list_splice(&vpm->active_list, &list);
616 list_splice(&vpm->pending_list, &list);
617 spin_unlock_irqrestore(&vpm->list_lock, flags);
618
619 while (!list_empty(&list)) {
620 cmd = list_entry(list.next, struct vpmoct_cmd, node);
621 list_del(&cmd->node);
622 kfree(cmd);
623 }
624
625 kfree(vpm);
626 }
627 EXPORT_SYMBOL(vpmoct_free);
628
629 /**
630 * vpmoct_init - Check for / initialize VPMOCT032 module.
631 * @vpm: struct vpmoct allocated with vpmoct_alloc
632 * @load_complete_fn: Function to call when the load is complete.
633 *
634 * Check to see if there is a VPMOCT module installed. If there appears to be
635 * one return 0 and perform any necessary setup in the background. The
636 * load_complete function will be called in a system global workqueue when the
637 * initialization is complete.
638 *
639 * Must be called in process context.
640 */
641 int vpmoct_init(struct vpmoct *vpm, load_complete_func_t load_complete)
642 {
643 struct vpmoct_load_work *work;
644
645 if (!vpm || !vpm->dev || !load_complete)
646 return -EINVAL;
647
648 if (vpmoct_resync(vpm)) {
649 load_complete(vpm->dev, false);
650 return -ENODEV;
651 }
652
653 vpm->mode = vpmoct_get_mode(vpm);
654
655 if (UNKNOWN == vpm->mode) {
656 load_complete(vpm->dev, false);
657 return -ENODEV;
658 }
659
660 work = kzalloc(sizeof(*work), GFP_KERNEL);
661 if (!work) {
662 load_complete(vpm->dev, false);
663 return -ENOMEM;
664 }
665
666 work->wq = create_singlethread_workqueue("vpmoct");
667 if (!work->wq) {
668 kfree(work);
669 load_complete(vpm->dev, false);
670 return -ENOMEM;
671 }
672
673 INIT_WORK(&work->work, vpmoct_load_flash);
674
675 work->vpm = vpm;
676 work->load_complete = load_complete;
677 queue_work(work->wq, &work->work);
678 return 0;
679 }
680 EXPORT_SYMBOL(vpmoct_init);
681
682 static void
683 vpmoct_set_companding(struct vpmoct *vpm, int channo, int companding)
684 {
685 u32 new_companding;
686 bool do_update = false;
687
688 mutex_lock(&vpm->mutex);
689 new_companding = (DAHDI_LAW_MULAW == companding) ?
690 (vpm->companding & ~(1 << channo)) :
691 (vpm->companding | (1 << channo));
692 if (vpm->companding != new_companding) {
693 vpm->companding = new_companding;
694 if (!vpm->companding_update_active) {
695 do_update = true;
696 vpm->companding_update_active = 1;
697 }
698 }
699 mutex_unlock(&vpm->mutex);
700
701 while (do_update) {
702 u32 update;
703
704 vpmoct_write_dword(vpm, 0x40, new_companding);
705 update = vpmoct_read_dword(vpm, 0x40);
706
707 WARN_ON(new_companding != update);
708
709 mutex_lock(&vpm->mutex);
710 if (vpm->companding != new_companding) {
711 new_companding = vpm->companding;
712 } else {
713 vpm->companding_update_active = 0;
714 do_update = false;
715 }
716 mutex_unlock(&vpm->mutex);
717 }
718 }
719
720 /**
721 * vpmoct_echo_update - Enable / Disable the VPMOCT032 echocan state
722 * @vpm: The echocan to operate on.
723 * @channo: Which echocan timeslot to enable / disable.
724 * @echo_on: Whether we're turning the echocan on or off.
725 *
726 * When this function returns, the echocan is scheduled to be enabled or
727 * disabled at some point in the near future.
728 *
729 * Must be called in process context.
730 *
731 */
732 static void vpmoct_echo_update(struct vpmoct *vpm, int channo, bool echo_on)
733 {
734 u32 echo;
735 unsigned long timeout;
736 bool do_update = false;
737
738 mutex_lock(&vpm->mutex);
739 echo = (echo_on) ? (vpm->echo | (1 << channo)) :
740 (vpm->echo & ~(1 << channo));
741 if (vpm->echo != echo) {
742 vpm->echo = echo;
743 if (!vpm->echo_update_active) {
744 do_update = true;
745 vpm->echo_update_active = 1;
746 }
747 }
748 mutex_unlock(&vpm->mutex);
749
750 timeout = jiffies + 2*HZ;
751 while (do_update) {
752 u32 new;
753
754 vpmoct_write_dword(vpm, 0x30, echo);
755 new = vpmoct_read_dword(vpm, 0x10);
756
757 mutex_lock(&vpm->mutex);
758 if (((vpm->echo != echo) || (new != echo)) &&
759 time_before(jiffies, timeout)) {
760 echo = vpm->echo;
761 } else {
762 vpm->echo_update_active = 0;
763 do_update = false;
764 }
765 mutex_unlock(&vpm->mutex);
766 }
767
768 if (!time_before(jiffies, timeout))
769 dev_warn(vpm->dev, "vpmoct: Updating echo state timed out.\n");
770 }
771
772 int vpmoct_echocan_create(struct vpmoct *vpm, int channo, int companding)
773 {
774 vpmoct_set_companding(vpm, channo, companding);
775 vpmoct_echo_update(vpm, channo, true);
776 return 0;
777 }
778 EXPORT_SYMBOL(vpmoct_echocan_create);
779
780 void vpmoct_echocan_free(struct vpmoct *vpm, int channo)
781 {
782 vpmoct_echo_update(vpm, channo, false);
783 }
784 EXPORT_SYMBOL(vpmoct_echocan_free);
785
786 /* Enable a vpm debugging mode where the pre-echo-canceled audio
787 * stream is physically output on timeslot 24.
788 */
789 int vpmoct_preecho_enable(struct vpmoct *vpm, const int channo)
790 {
791 int ret;
792 mutex_lock(&vpm->mutex);
793 if (!vpm->preecho_enabled) {
794 vpm->preecho_enabled = 1;
795 vpm->preecho_timeslot = channo;
796
797 vpmoct_write_dword(vpm, 0x74, channo);
798
799 /* Begin pre-echo stream on timeslot 24 */
800 vpmoct_write_byte(vpm, 0x71, 0x0a);
801 ret = 0;
802 } else {
803 ret = -EBUSY;
804 }
805 mutex_unlock(&vpm->mutex);
806
807 return ret;
808 }
809 EXPORT_SYMBOL(vpmoct_preecho_enable);
810
811 int vpmoct_preecho_disable(struct vpmoct *vpm, const int channo)
812 {
813 int ret;
814
815 mutex_lock(&vpm->mutex);
816 if (!vpm->preecho_enabled) {
817 ret = 0;
818 } else if (channo == vpm->preecho_timeslot) {
819 vpm->preecho_enabled = 0;
820
821 /* Disable pre-echo stream by loading in a non-existing
822 * channel number */
823 vpmoct_write_byte(vpm, 0x74, 0xff);
824
825 /* Stop pre-echo stream on timeslot 24 */
826 vpmoct_write_byte(vpm, 0x71, 0x0a);
827 ret = 0;
828 } else {
829 ret = -EINVAL;
830 }
831 mutex_unlock(&vpm->mutex);
832
833 return ret;
834 }
835 EXPORT_SYMBOL(vpmoct_preecho_disable);