RT-AC56 3.0.0.4.374.37 core
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / char / mwave / mwavedd.c
blob842c0e38a773aa345a26392b91698917bdd7d566
1 /*
3 * mwavedd.c -- mwave device driver
6 * Written By: Mike Sullivan IBM Corporation
8 * Copyright (C) 1999 IBM Corporation
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * NO WARRANTY
21 * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
22 * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
23 * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
24 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
25 * solely responsible for determining the appropriateness of using and
26 * distributing the Program and assumes all risks associated with its
27 * exercise of rights under this Agreement, including but not limited to
28 * the risks and costs of program errors, damage to or loss of data,
29 * programs or equipment, and unavailability or interruption of operations.
31 * DISCLAIMER OF LIABILITY
32 * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
33 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
38 * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
40 * You should have received a copy of the GNU General Public License
41 * along with this program; if not, write to the Free Software
42 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
45 * 10/23/2000 - Alpha Release
46 * First release to the public
49 #include <linux/module.h>
50 #include <linux/kernel.h>
51 #include <linux/fs.h>
52 #include <linux/init.h>
53 #include <linux/major.h>
54 #include <linux/miscdevice.h>
55 #include <linux/device.h>
56 #include <linux/serial.h>
57 #include <linux/sched.h>
58 #include <linux/spinlock.h>
59 #include <linux/smp_lock.h>
60 #include <linux/delay.h>
61 #include <linux/serial_8250.h>
62 #include "smapi.h"
63 #include "mwavedd.h"
64 #include "3780i.h"
65 #include "tp3780i.h"
67 MODULE_DESCRIPTION("3780i Advanced Communications Processor (Mwave) driver");
68 MODULE_AUTHOR("Mike Sullivan and Paul Schroeder");
69 MODULE_LICENSE("GPL");
72 * These parameters support the setting of MWave resources. Note that no
73 * checks are made against other devices (ie. superio) for conflicts.
74 * We'll depend on users using the tpctl utility to do that for now
76 int mwave_debug = 0;
77 int mwave_3780i_irq = 0;
78 int mwave_3780i_io = 0;
79 int mwave_uart_irq = 0;
80 int mwave_uart_io = 0;
81 module_param(mwave_debug, int, 0);
82 module_param(mwave_3780i_irq, int, 0);
83 module_param(mwave_3780i_io, int, 0);
84 module_param(mwave_uart_irq, int, 0);
85 module_param(mwave_uart_io, int, 0);
87 static int mwave_open(struct inode *inode, struct file *file);
88 static int mwave_close(struct inode *inode, struct file *file);
89 static long mwave_ioctl(struct file *filp, unsigned int iocmd,
90 unsigned long ioarg);
92 MWAVE_DEVICE_DATA mwave_s_mdd;
94 static int mwave_open(struct inode *inode, struct file *file)
96 unsigned int retval = 0;
98 PRINTK_3(TRACE_MWAVE,
99 "mwavedd::mwave_open, entry inode %p file %p\n",
100 inode, file);
101 PRINTK_2(TRACE_MWAVE,
102 "mwavedd::mwave_open, exit return retval %x\n", retval);
104 cycle_kernel_lock();
105 return retval;
108 static int mwave_close(struct inode *inode, struct file *file)
110 unsigned int retval = 0;
112 PRINTK_3(TRACE_MWAVE,
113 "mwavedd::mwave_close, entry inode %p file %p\n",
114 inode, file);
116 PRINTK_2(TRACE_MWAVE, "mwavedd::mwave_close, exit retval %x\n",
117 retval);
119 return retval;
122 static long mwave_ioctl(struct file *file, unsigned int iocmd,
123 unsigned long ioarg)
125 unsigned int retval = 0;
126 pMWAVE_DEVICE_DATA pDrvData = &mwave_s_mdd;
127 void __user *arg = (void __user *)ioarg;
129 PRINTK_4(TRACE_MWAVE,
130 "mwavedd::mwave_ioctl, entry file %p cmd %x arg %x\n",
131 file, iocmd, (int) ioarg);
133 switch (iocmd) {
135 case IOCTL_MW_RESET:
136 PRINTK_1(TRACE_MWAVE,
137 "mwavedd::mwave_ioctl, IOCTL_MW_RESET"
138 " calling tp3780I_ResetDSP\n");
139 lock_kernel();
140 retval = tp3780I_ResetDSP(&pDrvData->rBDData);
141 unlock_kernel();
142 PRINTK_2(TRACE_MWAVE,
143 "mwavedd::mwave_ioctl, IOCTL_MW_RESET"
144 " retval %x from tp3780I_ResetDSP\n",
145 retval);
146 break;
148 case IOCTL_MW_RUN:
149 PRINTK_1(TRACE_MWAVE,
150 "mwavedd::mwave_ioctl, IOCTL_MW_RUN"
151 " calling tp3780I_StartDSP\n");
152 lock_kernel();
153 retval = tp3780I_StartDSP(&pDrvData->rBDData);
154 unlock_kernel();
155 PRINTK_2(TRACE_MWAVE,
156 "mwavedd::mwave_ioctl, IOCTL_MW_RUN"
157 " retval %x from tp3780I_StartDSP\n",
158 retval);
159 break;
161 case IOCTL_MW_DSP_ABILITIES: {
162 MW_ABILITIES rAbilities;
164 PRINTK_1(TRACE_MWAVE,
165 "mwavedd::mwave_ioctl,"
166 " IOCTL_MW_DSP_ABILITIES calling"
167 " tp3780I_QueryAbilities\n");
168 lock_kernel();
169 retval = tp3780I_QueryAbilities(&pDrvData->rBDData,
170 &rAbilities);
171 unlock_kernel();
172 PRINTK_2(TRACE_MWAVE,
173 "mwavedd::mwave_ioctl, IOCTL_MW_DSP_ABILITIES"
174 " retval %x from tp3780I_QueryAbilities\n",
175 retval);
176 if (retval == 0) {
177 if( copy_to_user(arg, &rAbilities,
178 sizeof(MW_ABILITIES)) )
179 return -EFAULT;
181 PRINTK_2(TRACE_MWAVE,
182 "mwavedd::mwave_ioctl, IOCTL_MW_DSP_ABILITIES"
183 " exit retval %x\n",
184 retval);
186 break;
188 case IOCTL_MW_READ_DATA:
189 case IOCTL_MW_READCLEAR_DATA: {
190 MW_READWRITE rReadData;
191 unsigned short __user *pusBuffer = NULL;
193 if( copy_from_user(&rReadData, arg,
194 sizeof(MW_READWRITE)) )
195 return -EFAULT;
196 pusBuffer = (unsigned short __user *) (rReadData.pBuf);
198 PRINTK_4(TRACE_MWAVE,
199 "mwavedd::mwave_ioctl IOCTL_MW_READ_DATA,"
200 " size %lx, ioarg %lx pusBuffer %p\n",
201 rReadData.ulDataLength, ioarg, pusBuffer);
202 lock_kernel();
203 retval = tp3780I_ReadWriteDspDStore(&pDrvData->rBDData,
204 iocmd,
205 pusBuffer,
206 rReadData.ulDataLength,
207 rReadData.usDspAddress);
208 unlock_kernel();
210 break;
212 case IOCTL_MW_READ_INST: {
213 MW_READWRITE rReadData;
214 unsigned short __user *pusBuffer = NULL;
216 if( copy_from_user(&rReadData, arg,
217 sizeof(MW_READWRITE)) )
218 return -EFAULT;
219 pusBuffer = (unsigned short __user *) (rReadData.pBuf);
221 PRINTK_4(TRACE_MWAVE,
222 "mwavedd::mwave_ioctl IOCTL_MW_READ_INST,"
223 " size %lx, ioarg %lx pusBuffer %p\n",
224 rReadData.ulDataLength / 2, ioarg,
225 pusBuffer);
226 lock_kernel();
227 retval = tp3780I_ReadWriteDspDStore(&pDrvData->rBDData,
228 iocmd, pusBuffer,
229 rReadData.ulDataLength / 2,
230 rReadData.usDspAddress);
231 unlock_kernel();
233 break;
235 case IOCTL_MW_WRITE_DATA: {
236 MW_READWRITE rWriteData;
237 unsigned short __user *pusBuffer = NULL;
239 if( copy_from_user(&rWriteData, arg,
240 sizeof(MW_READWRITE)) )
241 return -EFAULT;
242 pusBuffer = (unsigned short __user *) (rWriteData.pBuf);
244 PRINTK_4(TRACE_MWAVE,
245 "mwavedd::mwave_ioctl IOCTL_MW_WRITE_DATA,"
246 " size %lx, ioarg %lx pusBuffer %p\n",
247 rWriteData.ulDataLength, ioarg,
248 pusBuffer);
249 lock_kernel();
250 retval = tp3780I_ReadWriteDspDStore(&pDrvData->rBDData,
251 iocmd, pusBuffer,
252 rWriteData.ulDataLength,
253 rWriteData.usDspAddress);
254 unlock_kernel();
256 break;
258 case IOCTL_MW_WRITE_INST: {
259 MW_READWRITE rWriteData;
260 unsigned short __user *pusBuffer = NULL;
262 if( copy_from_user(&rWriteData, arg,
263 sizeof(MW_READWRITE)) )
264 return -EFAULT;
265 pusBuffer = (unsigned short __user *)(rWriteData.pBuf);
267 PRINTK_4(TRACE_MWAVE,
268 "mwavedd::mwave_ioctl IOCTL_MW_WRITE_INST,"
269 " size %lx, ioarg %lx pusBuffer %p\n",
270 rWriteData.ulDataLength, ioarg,
271 pusBuffer);
272 lock_kernel();
273 retval = tp3780I_ReadWriteDspIStore(&pDrvData->rBDData,
274 iocmd, pusBuffer,
275 rWriteData.ulDataLength,
276 rWriteData.usDspAddress);
277 unlock_kernel();
279 break;
281 case IOCTL_MW_REGISTER_IPC: {
282 unsigned int ipcnum = (unsigned int) ioarg;
284 if (ipcnum >= ARRAY_SIZE(pDrvData->IPCs)) {
285 PRINTK_ERROR(KERN_ERR_MWAVE
286 "mwavedd::mwave_ioctl:"
287 " IOCTL_MW_REGISTER_IPC:"
288 " Error: Invalid ipcnum %x\n",
289 ipcnum);
290 return -EINVAL;
292 PRINTK_3(TRACE_MWAVE,
293 "mwavedd::mwave_ioctl IOCTL_MW_REGISTER_IPC"
294 " ipcnum %x entry usIntCount %x\n",
295 ipcnum,
296 pDrvData->IPCs[ipcnum].usIntCount);
298 lock_kernel();
299 pDrvData->IPCs[ipcnum].bIsHere = FALSE;
300 pDrvData->IPCs[ipcnum].bIsEnabled = TRUE;
301 unlock_kernel();
303 PRINTK_2(TRACE_MWAVE,
304 "mwavedd::mwave_ioctl IOCTL_MW_REGISTER_IPC"
305 " ipcnum %x exit\n",
306 ipcnum);
308 break;
310 case IOCTL_MW_GET_IPC: {
311 unsigned int ipcnum = (unsigned int) ioarg;
313 if (ipcnum >= ARRAY_SIZE(pDrvData->IPCs)) {
314 PRINTK_ERROR(KERN_ERR_MWAVE
315 "mwavedd::mwave_ioctl:"
316 " IOCTL_MW_GET_IPC: Error:"
317 " Invalid ipcnum %x\n", ipcnum);
318 return -EINVAL;
320 PRINTK_3(TRACE_MWAVE,
321 "mwavedd::mwave_ioctl IOCTL_MW_GET_IPC"
322 " ipcnum %x, usIntCount %x\n",
323 ipcnum,
324 pDrvData->IPCs[ipcnum].usIntCount);
326 lock_kernel();
327 if (pDrvData->IPCs[ipcnum].bIsEnabled == TRUE) {
328 DECLARE_WAITQUEUE(wait, current);
330 PRINTK_2(TRACE_MWAVE,
331 "mwavedd::mwave_ioctl, thread for"
332 " ipc %x going to sleep\n",
333 ipcnum);
334 add_wait_queue(&pDrvData->IPCs[ipcnum].ipc_wait_queue, &wait);
335 pDrvData->IPCs[ipcnum].bIsHere = TRUE;
336 set_current_state(TASK_INTERRUPTIBLE);
337 /* check whether an event was signalled by */
338 /* the interrupt handler while we were gone */
339 if (pDrvData->IPCs[ipcnum].usIntCount == 1) { /* first int has occurred (race condition) */
340 pDrvData->IPCs[ipcnum].usIntCount = 2; /* first int has been handled */
341 PRINTK_2(TRACE_MWAVE,
342 "mwavedd::mwave_ioctl"
343 " IOCTL_MW_GET_IPC ipcnum %x"
344 " handling first int\n",
345 ipcnum);
346 } else { /* either 1st int has not yet occurred, or we have already handled the first int */
347 schedule();
348 if (pDrvData->IPCs[ipcnum].usIntCount == 1) {
349 pDrvData->IPCs[ipcnum].usIntCount = 2;
351 PRINTK_2(TRACE_MWAVE,
352 "mwavedd::mwave_ioctl"
353 " IOCTL_MW_GET_IPC ipcnum %x"
354 " woke up and returning to"
355 " application\n",
356 ipcnum);
358 pDrvData->IPCs[ipcnum].bIsHere = FALSE;
359 remove_wait_queue(&pDrvData->IPCs[ipcnum].ipc_wait_queue, &wait);
360 set_current_state(TASK_RUNNING);
361 PRINTK_2(TRACE_MWAVE,
362 "mwavedd::mwave_ioctl IOCTL_MW_GET_IPC,"
363 " returning thread for ipc %x"
364 " processing\n",
365 ipcnum);
367 unlock_kernel();
369 break;
371 case IOCTL_MW_UNREGISTER_IPC: {
372 unsigned int ipcnum = (unsigned int) ioarg;
374 PRINTK_2(TRACE_MWAVE,
375 "mwavedd::mwave_ioctl IOCTL_MW_UNREGISTER_IPC"
376 " ipcnum %x\n",
377 ipcnum);
378 if (ipcnum >= ARRAY_SIZE(pDrvData->IPCs)) {
379 PRINTK_ERROR(KERN_ERR_MWAVE
380 "mwavedd::mwave_ioctl:"
381 " IOCTL_MW_UNREGISTER_IPC:"
382 " Error: Invalid ipcnum %x\n",
383 ipcnum);
384 return -EINVAL;
386 lock_kernel();
387 if (pDrvData->IPCs[ipcnum].bIsEnabled == TRUE) {
388 pDrvData->IPCs[ipcnum].bIsEnabled = FALSE;
389 if (pDrvData->IPCs[ipcnum].bIsHere == TRUE) {
390 wake_up_interruptible(&pDrvData->IPCs[ipcnum].ipc_wait_queue);
393 unlock_kernel();
395 break;
397 default:
398 return -ENOTTY;
399 break;
400 } /* switch */
402 PRINTK_2(TRACE_MWAVE, "mwavedd::mwave_ioctl, exit retval %x\n", retval);
404 return retval;
408 static ssize_t mwave_read(struct file *file, char __user *buf, size_t count,
409 loff_t * ppos)
411 PRINTK_5(TRACE_MWAVE,
412 "mwavedd::mwave_read entry file %p, buf %p, count %zx ppos %p\n",
413 file, buf, count, ppos);
415 return -EINVAL;
419 static ssize_t mwave_write(struct file *file, const char __user *buf,
420 size_t count, loff_t * ppos)
422 PRINTK_5(TRACE_MWAVE,
423 "mwavedd::mwave_write entry file %p, buf %p,"
424 " count %zx ppos %p\n",
425 file, buf, count, ppos);
427 return -EINVAL;
431 static int register_serial_portandirq(unsigned int port, int irq)
433 struct uart_port uart;
435 switch ( port ) {
436 case 0x3f8:
437 case 0x2f8:
438 case 0x3e8:
439 case 0x2e8:
440 /* OK */
441 break;
442 default:
443 PRINTK_ERROR(KERN_ERR_MWAVE
444 "mwavedd::register_serial_portandirq:"
445 " Error: Illegal port %x\n", port );
446 return -1;
447 } /* switch */
448 /* port is okay */
450 switch ( irq ) {
451 case 3:
452 case 4:
453 case 5:
454 case 7:
455 /* OK */
456 break;
457 default:
458 PRINTK_ERROR(KERN_ERR_MWAVE
459 "mwavedd::register_serial_portandirq:"
460 " Error: Illegal irq %x\n", irq );
461 return -1;
462 } /* switch */
463 /* irq is okay */
465 memset(&uart, 0, sizeof(struct uart_port));
467 uart.uartclk = 1843200;
468 uart.iobase = port;
469 uart.irq = irq;
470 uart.iotype = UPIO_PORT;
471 uart.flags = UPF_SHARE_IRQ;
472 return serial8250_register_port(&uart);
476 static const struct file_operations mwave_fops = {
477 .owner = THIS_MODULE,
478 .read = mwave_read,
479 .write = mwave_write,
480 .unlocked_ioctl = mwave_ioctl,
481 .open = mwave_open,
482 .release = mwave_close
486 static struct miscdevice mwave_misc_dev = { MWAVE_MINOR, "mwave", &mwave_fops };
490 * mwave_init is called on module load
492 * mwave_exit is called on module unload
493 * mwave_exit is also used to clean up after an aborted mwave_init
495 static void mwave_exit(void)
497 pMWAVE_DEVICE_DATA pDrvData = &mwave_s_mdd;
499 PRINTK_1(TRACE_MWAVE, "mwavedd::mwave_exit entry\n");
502 if ( pDrvData->sLine >= 0 ) {
503 serial8250_unregister_port(pDrvData->sLine);
505 if (pDrvData->bMwaveDevRegistered) {
506 misc_deregister(&mwave_misc_dev);
508 if (pDrvData->bDSPEnabled) {
509 tp3780I_DisableDSP(&pDrvData->rBDData);
511 if (pDrvData->bResourcesClaimed) {
512 tp3780I_ReleaseResources(&pDrvData->rBDData);
514 if (pDrvData->bBDInitialized) {
515 tp3780I_Cleanup(&pDrvData->rBDData);
518 PRINTK_1(TRACE_MWAVE, "mwavedd::mwave_exit exit\n");
521 module_exit(mwave_exit);
523 static int __init mwave_init(void)
525 int i;
526 int retval = 0;
527 pMWAVE_DEVICE_DATA pDrvData = &mwave_s_mdd;
529 PRINTK_1(TRACE_MWAVE, "mwavedd::mwave_init entry\n");
531 memset(&mwave_s_mdd, 0, sizeof(MWAVE_DEVICE_DATA));
533 pDrvData->bBDInitialized = FALSE;
534 pDrvData->bResourcesClaimed = FALSE;
535 pDrvData->bDSPEnabled = FALSE;
536 pDrvData->bDSPReset = FALSE;
537 pDrvData->bMwaveDevRegistered = FALSE;
538 pDrvData->sLine = -1;
540 for (i = 0; i < ARRAY_SIZE(pDrvData->IPCs); i++) {
541 pDrvData->IPCs[i].bIsEnabled = FALSE;
542 pDrvData->IPCs[i].bIsHere = FALSE;
543 pDrvData->IPCs[i].usIntCount = 0; /* no ints received yet */
544 init_waitqueue_head(&pDrvData->IPCs[i].ipc_wait_queue);
547 retval = tp3780I_InitializeBoardData(&pDrvData->rBDData);
548 PRINTK_2(TRACE_MWAVE,
549 "mwavedd::mwave_init, return from tp3780I_InitializeBoardData"
550 " retval %x\n",
551 retval);
552 if (retval) {
553 PRINTK_ERROR(KERN_ERR_MWAVE
554 "mwavedd::mwave_init: Error:"
555 " Failed to initialize board data\n");
556 goto cleanup_error;
558 pDrvData->bBDInitialized = TRUE;
560 retval = tp3780I_CalcResources(&pDrvData->rBDData);
561 PRINTK_2(TRACE_MWAVE,
562 "mwavedd::mwave_init, return from tp3780I_CalcResources"
563 " retval %x\n",
564 retval);
565 if (retval) {
566 PRINTK_ERROR(KERN_ERR_MWAVE
567 "mwavedd:mwave_init: Error:"
568 " Failed to calculate resources\n");
569 goto cleanup_error;
572 retval = tp3780I_ClaimResources(&pDrvData->rBDData);
573 PRINTK_2(TRACE_MWAVE,
574 "mwavedd::mwave_init, return from tp3780I_ClaimResources"
575 " retval %x\n",
576 retval);
577 if (retval) {
578 PRINTK_ERROR(KERN_ERR_MWAVE
579 "mwavedd:mwave_init: Error:"
580 " Failed to claim resources\n");
581 goto cleanup_error;
583 pDrvData->bResourcesClaimed = TRUE;
585 retval = tp3780I_EnableDSP(&pDrvData->rBDData);
586 PRINTK_2(TRACE_MWAVE,
587 "mwavedd::mwave_init, return from tp3780I_EnableDSP"
588 " retval %x\n",
589 retval);
590 if (retval) {
591 PRINTK_ERROR(KERN_ERR_MWAVE
592 "mwavedd:mwave_init: Error:"
593 " Failed to enable DSP\n");
594 goto cleanup_error;
596 pDrvData->bDSPEnabled = TRUE;
598 if (misc_register(&mwave_misc_dev) < 0) {
599 PRINTK_ERROR(KERN_ERR_MWAVE
600 "mwavedd:mwave_init: Error:"
601 " Failed to register misc device\n");
602 goto cleanup_error;
604 pDrvData->bMwaveDevRegistered = TRUE;
606 pDrvData->sLine = register_serial_portandirq(
607 pDrvData->rBDData.rDspSettings.usUartBaseIO,
608 pDrvData->rBDData.rDspSettings.usUartIrq
610 if (pDrvData->sLine < 0) {
611 PRINTK_ERROR(KERN_ERR_MWAVE
612 "mwavedd:mwave_init: Error:"
613 " Failed to register serial driver\n");
614 goto cleanup_error;
616 /* uart is registered */
619 /* SUCCESS! */
620 return 0;
622 cleanup_error:
623 PRINTK_ERROR(KERN_ERR_MWAVE
624 "mwavedd::mwave_init: Error:"
625 " Failed to initialize\n");
626 mwave_exit(); /* clean up */
628 return -EIO;
631 module_init(mwave_init);