9609444181bf71815eea6aad89a745122c3e676e
[openwrt.git] / package / fonera-mp3-drv / src / mp3_drv.c
1 /*
2 * a.lp_mp3 - VS1011B driver for Fonera 
3 * Copyright (c) 2007 phrozen.org - John Crispin <john@phrozen.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307USA
18 *
19 * Feedback, Bugs...  john@phrozen.org 
20 *
21 */
22
23
24 #include <linux/module.h>
25 #include <linux/errno.h>
26 #include <linux/ioport.h>
27 #include <linux/init.h>
28 #include <asm/uaccess.h>
29 #include <asm/io.h>
30 #include <linux/timer.h>
31 #include <linux/init.h>
32 #include <linux/genhd.h>
33 #include <linux/device.h> 
34
35 // do we want debuging info ?
36 #if 0
37 #define DBG(x) x
38 #else
39 #define DBG(x) 
40 #endif
41
42 #define MP3_CHUNK_SIZE                  4096
43 #define MP3_BUFFERING                   0
44 #define MP3_PLAYING                     1
45 #define MP3_BUFFER_FINISHED             2
46 #define MP3_PLAY_FINISHED               3
47 typedef struct _MP3_DATA{
48         unsigned char   mp3[MP3_CHUNK_SIZE];
49         unsigned char state;
50 } MP3_DATA;
51
52 #define IOCTL_MP3_INIT                  0x01
53 #define IOCTL_MP3_RESET                 0x02
54 #define IOCTL_MP3_SETVOLUME             0x03
55 #define IOCTL_MP3_GETVOLUME             0x04
56
57 typedef struct _AUDIO_DATA{
58         unsigned int bitrate;
59         unsigned int sample_rate;
60         unsigned char is_stereo;
61 }AUDIO_DATA;
62 #define IOCTL_MP3_GETAUDIODATA          0x05
63 #define IOCTL_MP3_CLEARBUFFER           0x06
64 #define IOCTL_MP3_PLAY                  0x07
65
66 typedef struct _MP3_BEEP{
67         unsigned char   freq;
68         unsigned int    ms;
69 } MP3_BEEP;
70 #define IOCTL_MP3_BEEP                  0x08                    
71 #define IOCTL_MP3_END_REACHED           0x09
72 #define IOCTL_MP3_BASS                  0x10
73
74 #define CRYSTAL12288    0x9800
75 #define CRYSTAL24576    0x0
76
77 #define DEV_NAME                        "mp3"
78 #define DEV_MAJOR                       196
79 #define MAX_MP3_COUNT   1
80
81 typedef struct _mp3_inf{
82         unsigned char is_open;
83 } mp3_inf;
84 static mp3_inf mp3_info[MAX_MP3_COUNT];
85
86 #define MP3_BUFFER_SIZE         (128 * 1024)
87 unsigned char mp3_buffer[MP3_BUFFER_SIZE];
88
89 static unsigned long int mp3_buffer_offset_write = 0;
90 static unsigned long int mp3_buffer_offset_read = 0;
91 static unsigned char mp3_buffering_status = MP3_BUFFERING;
92 static unsigned long int mp3_data_in_buffer = 0;
93 static int mp3_thread = 0;
94 unsigned int crystal_freq;
95
96 #include "vs10xx.c"
97
98 static wait_queue_head_t wq;
99 static DECLARE_COMPLETION(mp3_exit);
100
101 static int mp3_playback_thread(void *data){
102         int j;
103         unsigned long timeout;
104         printk("started kthread\n");
105         daemonize("kmp3");
106         while(mp3_buffering_status != MP3_PLAY_FINISHED){
107                 if((mp3_buffering_status == MP3_PLAYING) || (mp3_buffering_status == MP3_BUFFER_FINISHED)){
108                         while(VS1011_NEEDS_DATA){
109                                 if(mp3_buffer_offset_read == MP3_BUFFER_SIZE){
110                                         mp3_buffer_offset_read = 0;
111                                 }
112                                 
113                                 if(mp3_data_in_buffer == 0){
114                                         if(mp3_buffering_status == MP3_BUFFER_FINISHED){
115                                                 printk("mp3_drv.ko : finished playing\n");
116                                                 mp3_buffering_status = MP3_PLAY_FINISHED;
117                                         } else {
118                                                 printk("mp3_drv.ko : buffer empty ?\n");
119                                                 if(mp3_buffering_status != MP3_PLAY_FINISHED){
120                                                 }
121                                         }
122                                 } else {
123                                         for(j = 0; j < 32; j++){
124                                                 VS1011_send_SDI(mp3_buffer[mp3_buffer_offset_read + j]);
125                                         }
126                                         mp3_buffer_offset_read += 32;
127                                         mp3_data_in_buffer -= 32;
128                                 }
129                         }
130                 }
131                 timeout = 1;    
132                 timeout = wait_event_interruptible_timeout(wq, (timeout==0), timeout);  
133         }
134         complete_and_exit(&mp3_exit, 0); 
135 }
136
137 static ssize_t module_write(struct file * file, const char * buffer, size_t count, loff_t *offset){
138         MP3_DATA mp3_data;
139         
140         copy_from_user((char*) &mp3_data, buffer, sizeof(MP3_DATA));
141         
142         if(mp3_data.state == MP3_BUFFER_FINISHED){
143                 mp3_buffering_status = MP3_BUFFER_FINISHED;
144                 DBG(printk("mp3_drv.ko : file end reached\n"));
145                 return 1;
146         }
147         
148         if(mp3_data.state == MP3_PLAY_FINISHED){
149                 mp3_buffering_status = MP3_PLAY_FINISHED;
150                 mp3_data_in_buffer = 0;
151                 DBG(printk("mp3_drv.ko : stop playing\n"));
152                 return 1;
153         }
154         
155         if(mp3_data_in_buffer + MP3_CHUNK_SIZE >= MP3_BUFFER_SIZE){
156                 DBG(printk("mp3_drv.ko : buffer is full? %ld\n", mp3_data_in_buffer);)
157                 return 0;
158         }
159         
160         if(mp3_buffer_offset_write == MP3_BUFFER_SIZE){
161                 mp3_buffer_offset_write = 0;
162         }
163         
164         memcpy(&mp3_buffer[mp3_buffer_offset_write], mp3_data.mp3, MP3_CHUNK_SIZE);
165         mp3_buffer_offset_write += MP3_CHUNK_SIZE;
166         mp3_buffering_status = mp3_data.state;
167         mp3_data_in_buffer += MP3_CHUNK_SIZE;
168         return 1;
169 }
170
171 static int module_ioctl(struct inode * inode, struct file * file, unsigned int cmd, unsigned long arg){
172         unsigned int    retval = 0;
173         AUDIO_DATA      audio_data;
174         MP3_BEEP        mp3_beep;
175         DBG(printk("mp3_drv.ko : Ioctl Called (cmd=%d)\n", cmd );)
176         switch (cmd) {
177                 case IOCTL_MP3_INIT:
178                         crystal_freq = arg;
179                         VS1011_init(crystal_freq, 1);
180                         VS1011_print_registers();
181                         break;
182
183                 case IOCTL_MP3_RESET:
184                         DBG(printk("mp3_drv.ko : doing a sw reset\n");)
185                         VS1011_init(crystal_freq, 0);
186                         VS1011_print_registers();
187                         VS1011_send_zeros(0x20);
188                         break;
189
190                 case IOCTL_MP3_SETVOLUME:
191                         DBG(printk("mp3_drv.ko : setting volume to : %lu\n", arg&0xffff);)
192                         VS1011_set_volume(arg);
193                         break;
194
195                 case IOCTL_MP3_GETVOLUME:
196                         retval = VS1011_get_volume();
197                         DBG(printk("mp3_drv.ko : read volume : %d\n", retval);)
198                         break;
199
200                 case IOCTL_MP3_GETAUDIODATA:
201                         DBG(printk("mp3_drv.ko : read audio data\n");)
202                         VS1011_get_audio_data(&audio_data);
203                         copy_to_user((char*)arg, (char*)&audio_data, sizeof(AUDIO_DATA));
204                         break;
205
206                 case IOCTL_MP3_CLEARBUFFER:
207                         DBG(printk("mp3_drv.ko : clearing buffer\n");)
208                         mp3_buffer_offset_read = 0;
209                         mp3_buffer_offset_write = 0;
210                         mp3_buffering_status = MP3_PLAY_FINISHED;
211                         mp3_data_in_buffer = 0;
212                         break;
213
214                 case IOCTL_MP3_PLAY:
215                         mp3_thread = kernel_thread(mp3_playback_thread, NULL, CLONE_KERNEL);
216                         break;
217
218                 case IOCTL_MP3_BEEP:
219                         copy_from_user((char*)&mp3_beep, (char*)arg, sizeof(MP3_BEEP));
220                         VS1011_sine(1,mp3_beep.freq);
221                         msDelay(mp3_beep.ms);
222                         VS1011_sine(0,0);
223                         break;
224
225                 case IOCTL_MP3_END_REACHED:
226                         if(mp3_buffering_status == MP3_PLAY_FINISHED){
227                                 retval = 1;
228                         }
229                         break;
230
231                 case IOCTL_MP3_BASS:
232                         VS1011_set_bass(arg);
233                         break;
234
235                 default:
236                         printk("mp3_drv.ko : unknown ioctl\n");
237                         break;
238
239         }
240         return retval;
241 }
242
243 static int module_open(struct inode *inode, struct file *file){
244         unsigned int dev_minor = MINOR(inode->i_rdev);
245         if(dev_minor !=  0){
246                 printk("mp3_drv.ko : trying to access unknown minor device -> %d\n", dev_minor);
247                 return -ENODEV;
248         }
249         if(mp3_info[dev_minor].is_open) {
250                 printk("mp3_drv.ko : Device with minor ID %d already in use\n", dev_minor);
251                 return -EBUSY;
252         }
253         mp3_info[dev_minor].is_open = 1;
254         
255         mp3_buffering_status = MP3_PLAY_FINISHED;
256         printk("mp3_drv.ko : Minor %d has been opened\n", dev_minor);
257         return 0;
258 }
259
260 static int module_close(struct inode * inode, struct file * file){
261         unsigned int dev_minor = MINOR(inode->i_rdev);
262         mp3_info[dev_minor].is_open = 0;
263         printk("mp3_drv.ko : Minor %d has been closed\n", dev_minor);
264         mp3_buffering_status = MP3_PLAY_FINISHED;
265         return 0;
266 }
267
268 struct file_operations modulemp3_fops = {
269         write:         module_write,
270         ioctl:         module_ioctl,
271         open:          module_open,
272         release:       module_close
273 };
274
275 static struct class *mp3_class; 
276
277 static int __init mod_init(void){
278         printk("mp3_drv.ko : VS1011b Driver\n");
279         printk("mp3_drv.ko : Made by John '2B|!2B' Crispin (john@phrozen.org)\n");
280         printk("mp3_drv.ko : Starting ...\n");
281         
282         if(register_chrdev(DEV_MAJOR, DEV_NAME, &modulemp3_fops)) {
283                 printk( "mp3_drv.ko : Error whilst opening %s (%d)\n", DEV_NAME, DEV_MAJOR);
284                 return( -ENODEV );
285         }
286
287         printk("mp3_drv.ko : using sysfs to create device nodes\n");
288         mp3_class = class_create(THIS_MODULE, DEV_NAME); 
289         class_device_create(mp3_class, NULL, 
290                 MKDEV(DEV_MAJOR, 0), 
291                 NULL, DEV_NAME); 
292
293         mp3_info[0].is_open = 0;
294         printk("mp3_drv.ko : Device %s registered for major ID %d\n", DEV_NAME, DEV_MAJOR);
295         crystal_freq = CRYSTAL12288;
296         VS1011_init(crystal_freq, 1);
297         VS1011_print_registers();
298         printk("end of init\n");
299         init_waitqueue_head(&wq);
300         printk("wait queue started\n");
301         return 0;
302 }
303
304 static void __exit mod_exit(void){
305         printk( "mp3_drv.ko : Cleanup\n" );
306         unregister_chrdev(DEV_MAJOR, DEV_NAME);
307 }
308
309 module_init (mod_init);
310 module_exit (mod_exit);
311
312 MODULE_LICENSE("GPL");
313 MODULE_AUTHOR("K. John '2B|!2B' Crispin");
314 MODULE_DESCRIPTION("vs1011 Driver for Fox Board");
315
316
317