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 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
397 static void vpmoct_load_complete_fn(void *data)
398 {
399 struct vpmoct_load_work *work = data;
400 #else
401 static void vpmoct_load_complete_fn(struct work_struct *data)
402 {
403 struct vpmoct_load_work *work =
404 container_of(data, struct vpmoct_load_work, work);
405 #endif
406 /* Do not touch work->vpm after calling load complete. It may have
407 * been freed in the function by the board driver. */
408 work->load_complete(work->vpm->dev, work->operational);
409 destroy_workqueue(work->wq);
410 kfree(work);
411 }
412
413 /**
414 * vpmoct_load_complete - Call the load_complete function in a system workqueue.
415 * @work:
416 * @operational: Whether the VPM is functioning or not.
417 *
418 */
419 static void
420 vpmoct_load_complete(struct vpmoct_load_work *work, bool operational)
421 {
422 work->operational = operational;
423
424 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
425 INIT_WORK(&work->work, vpmoct_load_complete_fn, work);
426 #else
427 INIT_WORK(&work->work, vpmoct_load_complete_fn);
428 #endif
429 schedule_work(&work->work);
430 }
431
432 static bool is_valid_vpmoct_firmware(const struct firmware *fw)
433 {
434 const struct vpmoct_header *header =
435 (const struct vpmoct_header *)fw->data;
436 u32 crc = crc32(~0, &fw->data[10], fw->size - 10) ^ ~0;
437 return (!memcmp("DIGIUM", header->header, sizeof(header->header)) &&
438 (le32_to_cpu(header->chksum) == crc));
439 }
440
441 static void vpmoct_set_defaults(struct vpmoct *vpm)
442 {
443 vpmoct_write_dword(vpm, 0x40, 0);
444 vpmoct_write_dword(vpm, 0x30, 0);
445 }
446
447 static const char *const FIRMWARE_NAME = "dahdi-fw-vpmoct032.bin";
448 #if defined(HOTPLUG_FIRMWARE)
449 static int
450 vpmoct_request_firmware(const struct firmware **fw, struct device *dev)
451 {
452 return request_firmware(fw, FIRMWARE_NAME, dev);
453 }
454
455 static void vpmoct_release_firmware(const struct firmware *fw)
456 {
457 release_firmware(fw);
458 }
459 #else
460 static int
461 vpmoct_request_firmware(const struct firmware **fw_p, struct device *dev)
462 {
463 struct firmware *fw;
464 extern void _binary_dahdi_fw_vpmoct032_bin_size;
465 extern u8 _binary_dahdi_fw_vpmoct032_bin_start[];
466
467 *fw_p = fw = kzalloc(sizeof(*fw), GFP_KERNEL);
468 if (!fw)
469 return -ENOMEM;
470
471 fw->data = _binary_dahdi_fw_vpmoct032_bin_start;
472 /* Yes... this is weird. objcopy gives us a symbol containing
473 the size of the firmware, not a pointer a variable containing the
474 size. The only way we can get the value of the symbol is to take
475 its address, so we define it as a pointer and then cast that value
476 to the proper type. */
477 fw->size = (size_t) &_binary_dahdi_fw_vpmoct032_bin_size;
478
479 return 0;
480 }
481
482 static void vpmoct_release_firmware(const struct firmware *fw)
483 {
484 kfree(fw);
485 }
486 #endif
487
488 /**
489 * vpmoct_load_flash - Check the current flash version and possibly load.
490 * @vpm: The VPMOCT032 module to check / load.
491 *
492 */
493 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
494 static void vpmoct_load_flash(void *data)
495 {
496 struct vpmoct_load_work *work = data;
497 #else
498 static void vpmoct_load_flash(struct work_struct *data)
499 {
500 struct vpmoct_load_work *work =
501 container_of(data, struct vpmoct_load_work, work);
502 #endif
503 int res;
504 struct vpmoct *const vpm = work->vpm;
505 const struct firmware *fw;
506 const struct vpmoct_header *header;
507 char serial[VPMOCT_SERIAL_SIZE+1];
508 int i;
509
510 res = vpmoct_request_firmware(&fw, vpm->dev);
511 if (res) {
512 dev_warn(vpm->dev,
513 "vpmoct: Failed to load firmware from userspace! %d\n",
514 res);
515 header = NULL;
516 fw = NULL;
517 } else {
518 header = (const struct vpmoct_header *)fw->data;
519 }
520
521 if (vpm->mode == APPLICATION) {
522
523 /* Check the running application firmware
524 * for the proper version */
525 vpm->major = vpmoct_read_byte(vpm, VPMOCT_MAJOR);
526 vpm->minor = vpmoct_read_byte(vpm, VPMOCT_MINOR);
527 for (i = 0; i < VPMOCT_SERIAL_SIZE; i++)
528 serial[i] = vpmoct_read_byte(vpm, VPMOCT_SERIAL+i);
529 serial[VPMOCT_SERIAL_SIZE] = '\0';
530
531 dev_info(vpm->dev,
532 "vpmoct: Detected firmware v%d.%d Serial: %s\n",
533 vpm->major, vpm->minor,
534 (serial[0] != -1) ? serial : "(None)");
535
536 if (!fw) {
537 /* Again, we'll use the existing loaded firmware. */
538 vpmoct_set_defaults(vpm);
539 vpmoct_load_complete(work, true);
540 return;
541 }
542
543 if (!is_valid_vpmoct_firmware(fw)) {
544 dev_warn(vpm->dev,
545 "%s is invalid. Please reinstall.\n",
546 FIRMWARE_NAME);
547
548 /* Just use the old version of the fimware. */
549 vpmoct_release_firmware(fw);
550 vpmoct_set_defaults(vpm);
551 vpmoct_load_complete(work, true);
552 return;
553 }
554
555 if (vpm->minor == header->minor &&
556 vpm->major == header->major) {
557 /* Proper version is running */
558 vpmoct_release_firmware(fw);
559 vpmoct_set_defaults(vpm);
560 vpmoct_load_complete(work, true);
561 return;
562 } else {
563
564 /* Incorrect version of application code is
565 * loaded. Reset to bootloader mode */
566 if (vpmoct_switch_to_boot(vpm))
567 goto error;
568 }
569 }
570
571 if (!fw) {
572 vpmoct_load_complete(work, false);
573 return;
574 } else if (!is_valid_vpmoct_firmware(fw)) {
575 dev_warn(vpm->dev,
576 "%s is invalid. Please reinstall.\n", FIRMWARE_NAME);
577 goto error;
578 }
579
580 dev_info(vpm->dev, "vpmoct: Uploading firmware, v%d.%d. This can "\
581 "take up to 1 minute\n",
582 header->major, header->minor);
583 if (vpmoct_erase_flash(vpm))
584 goto error;
585 if (vpmoct_send_firmware_header(vpm, fw))
586 goto error;
587 if (vpmoct_send_firmware_body(vpm, fw))
588 goto error;
589 if (vpmoct_check_firmware_crc(vpm, fw->size-VPMOCT_FIRM_HEADER_LEN*2,
590 header->major, header->minor))
591 goto error;
592 vpmoct_release_firmware(fw);
593 vpmoct_set_defaults(vpm);
594 vpmoct_load_complete(work, true);
595 return;
596
597 error:
598 dev_info(vpm->dev, "Unable to load firmware\n");
599 vpmoct_release_firmware(fw);
600 /* TODO: Should we disable module if the firmware doesn't load? */
601 vpmoct_load_complete(work, false);
602 return;
603 }
604
605 struct vpmoct *vpmoct_alloc(void)
606 {
607 struct vpmoct *vpm;
608
609 vpm = kzalloc(sizeof(*vpm), GFP_KERNEL);
610 if (!vpm)
611 return NULL;
612
613 spin_lock_init(&vpm->list_lock);
614 INIT_LIST_HEAD(&vpm->pending_list);
615 INIT_LIST_HEAD(&vpm->active_list);
616 mutex_init(&vpm->mutex);
617 return vpm;
618 }
619 EXPORT_SYMBOL(vpmoct_alloc);
620
621 void vpmoct_free(struct vpmoct *vpm)
622 {
623 unsigned long flags;
624 struct vpmoct_cmd *cmd;
625 LIST_HEAD(list);
626
627 if (!vpm)
628 return;
629
630 spin_lock_irqsave(&vpm->list_lock, flags);
631 list_splice(&vpm->active_list, &list);
632 list_splice(&vpm->pending_list, &list);
633 spin_unlock_irqrestore(&vpm->list_lock, flags);
634
635 while (!list_empty(&list)) {
636 cmd = list_entry(list.next, struct vpmoct_cmd, node);
637 list_del(&cmd->node);
638 kfree(cmd);
639 }
640
641 kfree(vpm);
642 }
643 EXPORT_SYMBOL(vpmoct_free);
644
645 /**
646 * vpmoct_init - Check for / initialize VPMOCT032 module.
647 * @vpm: struct vpmoct allocated with vpmoct_alloc
648 * @load_complete_fn: Function to call when the load is complete.
649 *
650 * Check to see if there is a VPMOCT module installed. If there appears to be
651 * one return 0 and perform any necessary setup in the background. The
652 * load_complete function will be called in a system global workqueue when the
653 * initialization is complete.
654 *
655 * Must be called in process context.
656 */
657 int vpmoct_init(struct vpmoct *vpm, load_complete_func_t load_complete)
658 {
659 struct vpmoct_load_work *work;
660
661 if (!vpm || !vpm->dev || !load_complete)
662 return -EINVAL;
663
664 if (vpmoct_resync(vpm)) {
665 load_complete(vpm->dev, false);
666 return -ENODEV;
667 }
668
669 vpm->mode = vpmoct_get_mode(vpm);
670
671 if (UNKNOWN == vpm->mode) {
672 load_complete(vpm->dev, false);
673 return -ENODEV;
674 }
675
676 work = kzalloc(sizeof(*work), GFP_KERNEL);
677 if (!work) {
678 load_complete(vpm->dev, false);
679 return -ENOMEM;
680 }
681
682 work->wq = create_singlethread_workqueue("vpmoct");
683 if (!work->wq) {
684 kfree(work);
685 load_complete(vpm->dev, false);
686 return -ENOMEM;
687 }
688
689 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
690 INIT_WORK(&work->work, vpmoct_load_flash, work);
691 #else
692 INIT_WORK(&work->work, vpmoct_load_flash);
693 #endif
694
695 work->vpm = vpm;
696 work->load_complete = load_complete;
697 queue_work(work->wq, &work->work);
698 return 0;
699 }
700 EXPORT_SYMBOL(vpmoct_init);
701
702 static void
703 vpmoct_set_companding(struct vpmoct *vpm, int channo, int companding)
704 {
705 u32 new_companding;
706 bool do_update = false;
707
708 mutex_lock(&vpm->mutex);
709 new_companding = (DAHDI_LAW_MULAW == companding) ?
710 (vpm->companding & ~(1 << channo)) :
711 (vpm->companding | (1 << channo));
712 if (vpm->companding != new_companding) {
713 vpm->companding = new_companding;
714 if (!vpm->companding_update_active) {
715 do_update = true;
716 vpm->companding_update_active = 1;
717 }
718 }
719 mutex_unlock(&vpm->mutex);
720
721 while (do_update) {
722 u32 update;
723
724 vpmoct_write_dword(vpm, 0x40, new_companding);
725 update = vpmoct_read_dword(vpm, 0x40);
726
727 WARN_ON(new_companding != update);
728
729 mutex_lock(&vpm->mutex);
730 if (vpm->companding != new_companding) {
731 new_companding = vpm->companding;
732 } else {
733 vpm->companding_update_active = 0;
734 do_update = false;
735 }
736 mutex_unlock(&vpm->mutex);
737 }
738 }
739
740 /**
741 * vpmoct_echo_update - Enable / Disable the VPMOCT032 echocan state
742 * @vpm: The echocan to operate on.
743 * @channo: Which echocan timeslot to enable / disable.
744 * @echo_on: Whether we're turning the echocan on or off.
745 *
746 * When this function returns, the echocan is scheduled to be enabled or
747 * disabled at some point in the near future.
748 *
749 * Must be called in process context.
750 *
751 */
752 static void vpmoct_echo_update(struct vpmoct *vpm, int channo, bool echo_on)
753 {
754 u32 echo;
755 unsigned long timeout;
756 bool do_update = false;
757
758 mutex_lock(&vpm->mutex);
759 echo = (echo_on) ? (vpm->echo | (1 << channo)) :
760 (vpm->echo & ~(1 << channo));
761 if (vpm->echo != echo) {
762 vpm->echo = echo;
763 if (!vpm->echo_update_active) {
764 do_update = true;
765 vpm->echo_update_active = 1;
766 }
767 }
768 mutex_unlock(&vpm->mutex);
769
770 timeout = jiffies + 2*HZ;
771 while (do_update) {
772 u32 new;
773
774 vpmoct_write_dword(vpm, 0x30, echo);
775 new = vpmoct_read_dword(vpm, 0x10);
776
777 mutex_lock(&vpm->mutex);
778 if (((vpm->echo != echo) || (new != echo)) &&
779 time_before(jiffies, timeout)) {
780 echo = vpm->echo;
781 } else {
782 vpm->echo_update_active = 0;
783 do_update = false;
784 }
785 mutex_unlock(&vpm->mutex);
786 }
787
788 if (!time_before(jiffies, timeout))
789 dev_warn(vpm->dev, "vpmoct: Updating echo state timed out.\n");
790 }
791
792 int vpmoct_echocan_create(struct vpmoct *vpm, int channo, int companding)
793 {
794 vpmoct_set_companding(vpm, channo, companding);
795 vpmoct_echo_update(vpm, channo, true);
796 return 0;
797 }
798 EXPORT_SYMBOL(vpmoct_echocan_create);
799
800 void vpmoct_echocan_free(struct vpmoct *vpm, int channo)
801 {
802 vpmoct_echo_update(vpm, channo, false);
803 }
804 EXPORT_SYMBOL(vpmoct_echocan_free);
805
806 /* Enable a vpm debugging mode where the pre-echo-canceled audio
807 * stream is physically output on timeslot 24.
808 */
809 int vpmoct_preecho_enable(struct vpmoct *vpm, const int channo)
810 {
811 int ret;
812 mutex_lock(&vpm->mutex);
813 if (!vpm->preecho_enabled) {
814 vpm->preecho_enabled = 1;
815 vpm->preecho_timeslot = channo;
816
817 vpmoct_write_dword(vpm, 0x74, channo);
818
819 /* Begin pre-echo stream on timeslot 24 */
820 vpmoct_write_byte(vpm, 0x71, 0x0a);
821 ret = 0;
822 } else {
823 ret = -EBUSY;
824 }
825 mutex_unlock(&vpm->mutex);
826
827 return ret;
828 }
829 EXPORT_SYMBOL(vpmoct_preecho_enable);
830
831 int vpmoct_preecho_disable(struct vpmoct *vpm, const int channo)
832 {
833 int ret;
834
835 mutex_lock(&vpm->mutex);
836 if (!vpm->preecho_enabled) {
837 ret = 0;
838 } else if (channo == vpm->preecho_timeslot) {
839 vpm->preecho_enabled = 0;
840
841 /* Disable pre-echo stream by loading in a non-existing
842 * channel number */
843 vpmoct_write_byte(vpm, 0x74, 0xff);
844
845 /* Stop pre-echo stream on timeslot 24 */
846 vpmoct_write_byte(vpm, 0x71, 0x0a);
847 ret = 0;
848 } else {
849 ret = -EINVAL;
850 }
851 mutex_unlock(&vpm->mutex);
852
853 return ret;
854 }
855 EXPORT_SYMBOL(vpmoct_preecho_disable);