Merge from vendor branch BINUTILS:
[dragonfly.git] / sys / bus / pccard / pccard_beep.c
1 /*-
2  * pccard noise interface.
3  * Nate Williams, October 1997.
4  * This file is in the public domain.
5  */
6 /* $FreeBSD: src/sys/pccard/pccard_beep.c,v 1.3.2.3 2001/06/05 19:11:34 imp Exp $ */
7 /* $DragonFly: src/sys/bus/pccard/Attic/pccard_beep.c,v 1.3 2003/08/07 21:16:46 dillon Exp $ */
8
9 #include <sys/param.h>
10 #include <sys/kernel.h>
11 #include <sys/systm.h>
12
13 #include <machine/clock.h>
14
15 #include "driver.h"
16
17 static enum beepstate allow_beep = BEEP_OFF;
18 static int melody_type = 0;
19
20 #define MAX_TONE_MODE   3
21 #define MAX_STATE       4 
22
23 struct tone {
24         int pitch;
25         int duration;
26 };
27
28 static struct tone silent_beep[] = {
29         {0, 0}
30 };
31
32 static struct tone success_beep[] = {
33         {1200,   40}, {0, 0}
34 };
35 static struct tone failure_beep[] = {
36         {3200,   40}, {0, 0}
37 };
38 static struct tone insert_remove_beep[] = {
39         {1600,   20}, {0, 0}
40 };
41
42 static struct tone success_melody_beep[] = {
43         {1200,    7}, {1000,    7}, { 800,   15}, {0, 0}
44 };
45 static struct tone failure_melody_beep[] = {
46         {2000,    7}, {2400,    7}, {2800,   15}, {0, 0}
47 };
48 static struct tone insert_melody_beep[] = {
49         {1600,   10}, {1200,    5}, {0, 0}
50 };
51 static struct tone remove_melody_beep[] = {
52         {1200,   10}, {1600,    5}, {0, 0}
53 };
54
55 static struct tone *melody_table[MAX_TONE_MODE][MAX_STATE] = {
56         { /* silent mode */
57                 silent_beep, silent_beep, silent_beep, silent_beep,
58         },
59         { /* simple beep mode */
60                 success_beep, failure_beep,
61                 insert_remove_beep, insert_remove_beep,
62         },
63         { /* melody beep mode */
64                 success_melody_beep, failure_melody_beep,
65                 insert_melody_beep, remove_melody_beep,
66         },
67 };
68
69
70 static void
71 pccard_beep_sub(void *arg)
72 {
73         struct tone *melody;
74         melody = (struct tone *)arg;
75
76         if (melody->pitch != 0) {
77                 sysbeep(melody->pitch, melody->duration);
78                 timeout(pccard_beep_sub, melody + 1, melody->duration);
79         } else 
80                 allow_beep = BEEP_ON;
81 }
82
83 static void
84 pccard_beep_start(void *arg)
85 {
86         struct tone *melody;
87         melody = (struct tone *)arg;
88
89         if (allow_beep == BEEP_ON && melody->pitch != 0) {
90                 allow_beep = BEEP_OFF;
91                 sysbeep(melody->pitch, melody->duration);
92                 timeout(pccard_beep_sub, melody + 1, melody->duration);
93         }
94 }
95
96 void
97 pccard_success_beep(void)
98 {
99         pccard_beep_start(melody_table[melody_type][0]);
100 }
101
102 void
103 pccard_failure_beep(void)
104 {
105         pccard_beep_start(melody_table[melody_type][1]);
106 }
107
108 void
109 pccard_insert_beep(void)
110 {
111         pccard_beep_start(melody_table[melody_type][2]);
112 }
113
114 void
115 pccard_remove_beep(void)
116 {
117         pccard_beep_start(melody_table[melody_type][3]);
118 }
119
120 int
121 pccard_beep_select(int type)
122 {
123         int errcode = 0;
124
125         if (type == 0)  {
126                 allow_beep = BEEP_OFF;
127                 melody_type = 0;
128         } else if (type < 0 || MAX_TONE_MODE - 1 < type) {
129                 errcode = 1;
130         } else {
131                 allow_beep = BEEP_ON;
132                 melody_type = type;
133         }
134         return (errcode);
135 }