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.
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>
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>
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
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 int mwave_ioctl(struct inode *inode, struct file *filp,
90 unsigned int iocmd, 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;
99 "mwavedd::mwave_open, entry inode %p file %p\n",
101 PRINTK_2(TRACE_MWAVE,
102 "mwavedd::mwave_open, exit return retval %x\n", 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",
116 PRINTK_2(TRACE_MWAVE, "mwavedd::mwave_close, exit retval %x\n",
122 static int mwave_ioctl(struct inode *inode, struct file *file,
123 unsigned int iocmd, 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_5(TRACE_MWAVE,
130 "mwavedd::mwave_ioctl, entry inode %p file %p cmd %x arg %x\n",
131 inode, file, iocmd, (int) ioarg);
136 PRINTK_1(TRACE_MWAVE,
137 "mwavedd::mwave_ioctl, IOCTL_MW_RESET"
138 " calling tp3780I_ResetDSP\n");
139 retval = tp3780I_ResetDSP(&pDrvData->rBDData);
140 PRINTK_2(TRACE_MWAVE,
141 "mwavedd::mwave_ioctl, IOCTL_MW_RESET"
142 " retval %x from tp3780I_ResetDSP\n",
147 PRINTK_1(TRACE_MWAVE,
148 "mwavedd::mwave_ioctl, IOCTL_MW_RUN"
149 " calling tp3780I_StartDSP\n");
150 retval = tp3780I_StartDSP(&pDrvData->rBDData);
151 PRINTK_2(TRACE_MWAVE,
152 "mwavedd::mwave_ioctl, IOCTL_MW_RUN"
153 " retval %x from tp3780I_StartDSP\n",
157 case IOCTL_MW_DSP_ABILITIES: {
158 MW_ABILITIES rAbilities;
160 PRINTK_1(TRACE_MWAVE,
161 "mwavedd::mwave_ioctl,"
162 " IOCTL_MW_DSP_ABILITIES calling"
163 " tp3780I_QueryAbilities\n");
164 retval = tp3780I_QueryAbilities(&pDrvData->rBDData,
166 PRINTK_2(TRACE_MWAVE,
167 "mwavedd::mwave_ioctl, IOCTL_MW_DSP_ABILITIES"
168 " retval %x from tp3780I_QueryAbilities\n",
171 if( copy_to_user(arg, &rAbilities,
172 sizeof(MW_ABILITIES)) )
175 PRINTK_2(TRACE_MWAVE,
176 "mwavedd::mwave_ioctl, IOCTL_MW_DSP_ABILITIES"
182 case IOCTL_MW_READ_DATA:
183 case IOCTL_MW_READCLEAR_DATA: {
184 MW_READWRITE rReadData;
185 unsigned short __user *pusBuffer = NULL;
187 if( copy_from_user(&rReadData, arg,
188 sizeof(MW_READWRITE)) )
190 pusBuffer = (unsigned short __user *) (rReadData.pBuf);
192 PRINTK_4(TRACE_MWAVE,
193 "mwavedd::mwave_ioctl IOCTL_MW_READ_DATA,"
194 " size %lx, ioarg %lx pusBuffer %p\n",
195 rReadData.ulDataLength, ioarg, pusBuffer);
196 retval = tp3780I_ReadWriteDspDStore(&pDrvData->rBDData,
199 rReadData.ulDataLength,
200 rReadData.usDspAddress);
204 case IOCTL_MW_READ_INST: {
205 MW_READWRITE rReadData;
206 unsigned short __user *pusBuffer = NULL;
208 if( copy_from_user(&rReadData, arg,
209 sizeof(MW_READWRITE)) )
211 pusBuffer = (unsigned short __user *) (rReadData.pBuf);
213 PRINTK_4(TRACE_MWAVE,
214 "mwavedd::mwave_ioctl IOCTL_MW_READ_INST,"
215 " size %lx, ioarg %lx pusBuffer %p\n",
216 rReadData.ulDataLength / 2, ioarg,
218 retval = tp3780I_ReadWriteDspDStore(&pDrvData->rBDData,
220 rReadData.ulDataLength / 2,
221 rReadData.usDspAddress);
225 case IOCTL_MW_WRITE_DATA: {
226 MW_READWRITE rWriteData;
227 unsigned short __user *pusBuffer = NULL;
229 if( copy_from_user(&rWriteData, arg,
230 sizeof(MW_READWRITE)) )
232 pusBuffer = (unsigned short __user *) (rWriteData.pBuf);
234 PRINTK_4(TRACE_MWAVE,
235 "mwavedd::mwave_ioctl IOCTL_MW_WRITE_DATA,"
236 " size %lx, ioarg %lx pusBuffer %p\n",
237 rWriteData.ulDataLength, ioarg,
239 retval = tp3780I_ReadWriteDspDStore(&pDrvData->rBDData,
241 rWriteData.ulDataLength,
242 rWriteData.usDspAddress);
246 case IOCTL_MW_WRITE_INST: {
247 MW_READWRITE rWriteData;
248 unsigned short __user *pusBuffer = NULL;
250 if( copy_from_user(&rWriteData, arg,
251 sizeof(MW_READWRITE)) )
253 pusBuffer = (unsigned short __user *)(rWriteData.pBuf);
255 PRINTK_4(TRACE_MWAVE,
256 "mwavedd::mwave_ioctl IOCTL_MW_WRITE_INST,"
257 " size %lx, ioarg %lx pusBuffer %p\n",
258 rWriteData.ulDataLength, ioarg,
260 retval = tp3780I_ReadWriteDspIStore(&pDrvData->rBDData,
262 rWriteData.ulDataLength,
263 rWriteData.usDspAddress);
267 case IOCTL_MW_REGISTER_IPC: {
268 unsigned int ipcnum = (unsigned int) ioarg;
270 PRINTK_3(TRACE_MWAVE,
271 "mwavedd::mwave_ioctl IOCTL_MW_REGISTER_IPC"
272 " ipcnum %x entry usIntCount %x\n",
274 pDrvData->IPCs[ipcnum].usIntCount);
276 if (ipcnum >= ARRAY_SIZE(pDrvData->IPCs)) {
277 PRINTK_ERROR(KERN_ERR_MWAVE
278 "mwavedd::mwave_ioctl:"
279 " IOCTL_MW_REGISTER_IPC:"
280 " Error: Invalid ipcnum %x\n",
284 pDrvData->IPCs[ipcnum].bIsHere = FALSE;
285 pDrvData->IPCs[ipcnum].bIsEnabled = TRUE;
287 PRINTK_2(TRACE_MWAVE,
288 "mwavedd::mwave_ioctl IOCTL_MW_REGISTER_IPC"
294 case IOCTL_MW_GET_IPC: {
295 unsigned int ipcnum = (unsigned int) ioarg;
297 PRINTK_3(TRACE_MWAVE,
298 "mwavedd::mwave_ioctl IOCTL_MW_GET_IPC"
299 " ipcnum %x, usIntCount %x\n",
301 pDrvData->IPCs[ipcnum].usIntCount);
302 if (ipcnum >= ARRAY_SIZE(pDrvData->IPCs)) {
303 PRINTK_ERROR(KERN_ERR_MWAVE
304 "mwavedd::mwave_ioctl:"
305 " IOCTL_MW_GET_IPC: Error:"
306 " Invalid ipcnum %x\n", ipcnum);
310 if (pDrvData->IPCs[ipcnum].bIsEnabled == TRUE) {
311 DECLARE_WAITQUEUE(wait, current);
313 PRINTK_2(TRACE_MWAVE,
314 "mwavedd::mwave_ioctl, thread for"
315 " ipc %x going to sleep\n",
317 add_wait_queue(&pDrvData->IPCs[ipcnum].ipc_wait_queue, &wait);
318 pDrvData->IPCs[ipcnum].bIsHere = TRUE;
319 set_current_state(TASK_INTERRUPTIBLE);
320 /* check whether an event was signalled by */
321 /* the interrupt handler while we were gone */
322 if (pDrvData->IPCs[ipcnum].usIntCount == 1) { /* first int has occurred (race condition) */
323 pDrvData->IPCs[ipcnum].usIntCount = 2; /* first int has been handled */
324 PRINTK_2(TRACE_MWAVE,
325 "mwavedd::mwave_ioctl"
326 " IOCTL_MW_GET_IPC ipcnum %x"
327 " handling first int\n",
329 } else { /* either 1st int has not yet occurred, or we have already handled the first int */
331 if (pDrvData->IPCs[ipcnum].usIntCount == 1) {
332 pDrvData->IPCs[ipcnum].usIntCount = 2;
334 PRINTK_2(TRACE_MWAVE,
335 "mwavedd::mwave_ioctl"
336 " IOCTL_MW_GET_IPC ipcnum %x"
337 " woke up and returning to"
341 pDrvData->IPCs[ipcnum].bIsHere = FALSE;
342 remove_wait_queue(&pDrvData->IPCs[ipcnum].ipc_wait_queue, &wait);
343 set_current_state(TASK_RUNNING);
344 PRINTK_2(TRACE_MWAVE,
345 "mwavedd::mwave_ioctl IOCTL_MW_GET_IPC,"
346 " returning thread for ipc %x"
353 case IOCTL_MW_UNREGISTER_IPC: {
354 unsigned int ipcnum = (unsigned int) ioarg;
356 PRINTK_2(TRACE_MWAVE,
357 "mwavedd::mwave_ioctl IOCTL_MW_UNREGISTER_IPC"
360 if (ipcnum >= ARRAY_SIZE(pDrvData->IPCs)) {
361 PRINTK_ERROR(KERN_ERR_MWAVE
362 "mwavedd::mwave_ioctl:"
363 " IOCTL_MW_UNREGISTER_IPC:"
364 " Error: Invalid ipcnum %x\n",
368 if (pDrvData->IPCs[ipcnum].bIsEnabled == TRUE) {
369 pDrvData->IPCs[ipcnum].bIsEnabled = FALSE;
370 if (pDrvData->IPCs[ipcnum].bIsHere == TRUE) {
371 wake_up_interruptible(&pDrvData->IPCs[ipcnum].ipc_wait_queue);
378 PRINTK_ERROR(KERN_ERR_MWAVE "mwavedd::mwave_ioctl:"
379 " Error: Unrecognized iocmd %x\n",
385 PRINTK_2(TRACE_MWAVE, "mwavedd::mwave_ioctl, exit retval %x\n", retval);
391 static ssize_t mwave_read(struct file *file, char __user *buf, size_t count,
394 PRINTK_5(TRACE_MWAVE,
395 "mwavedd::mwave_read entry file %p, buf %p, count %zx ppos %p\n",
396 file, buf, count, ppos);
402 static ssize_t mwave_write(struct file *file, const char __user *buf,
403 size_t count, loff_t * ppos)
405 PRINTK_5(TRACE_MWAVE,
406 "mwavedd::mwave_write entry file %p, buf %p,"
407 " count %zx ppos %p\n",
408 file, buf, count, ppos);
414 static int register_serial_portandirq(unsigned int port, int irq)
416 struct uart_port uart;
426 PRINTK_ERROR(KERN_ERR_MWAVE
427 "mwavedd::register_serial_portandirq:"
428 " Error: Illegal port %x\n", port );
441 PRINTK_ERROR(KERN_ERR_MWAVE
442 "mwavedd::register_serial_portandirq:"
443 " Error: Illegal irq %x\n", irq );
448 memset(&uart, 0, sizeof(struct uart_port));
450 uart.uartclk = 1843200;
453 uart.iotype = UPIO_PORT;
454 uart.flags = UPF_SHARE_IRQ;
455 return serial8250_register_port(&uart);
459 static const struct file_operations mwave_fops = {
460 .owner = THIS_MODULE,
462 .write = mwave_write,
463 .ioctl = mwave_ioctl,
465 .release = mwave_close
469 static struct miscdevice mwave_misc_dev = { MWAVE_MINOR, "mwave", &mwave_fops };
471 #if 0 /* totally b0rked */
473 * sysfs support <paulsch@us.ibm.com>
476 struct device mwave_device;
478 /* Prevent code redundancy, create a macro for mwave_show_* functions. */
479 #define mwave_show_function(attr_name, format_string, field) \
480 static ssize_t mwave_show_##attr_name(struct device *dev, struct device_attribute *attr, char *buf) \
482 DSP_3780I_CONFIG_SETTINGS *pSettings = \
483 &mwave_s_mdd.rBDData.rDspSettings; \
484 return sprintf(buf, format_string, pSettings->field); \
487 /* All of our attributes are read attributes. */
488 #define mwave_dev_rd_attr(attr_name, format_string, field) \
489 mwave_show_function(attr_name, format_string, field) \
490 static DEVICE_ATTR(attr_name, S_IRUGO, mwave_show_##attr_name, NULL)
492 mwave_dev_rd_attr (3780i_dma, "%i\n", usDspDma);
493 mwave_dev_rd_attr (3780i_irq, "%i\n", usDspIrq);
494 mwave_dev_rd_attr (3780i_io, "%#.4x\n", usDspBaseIO);
495 mwave_dev_rd_attr (uart_irq, "%i\n", usUartIrq);
496 mwave_dev_rd_attr (uart_io, "%#.4x\n", usUartBaseIO);
498 static struct device_attribute * const mwave_dev_attrs[] = {
508 * mwave_init is called on module load
510 * mwave_exit is called on module unload
511 * mwave_exit is also used to clean up after an aborted mwave_init
513 static void mwave_exit(void)
515 pMWAVE_DEVICE_DATA pDrvData = &mwave_s_mdd;
517 PRINTK_1(TRACE_MWAVE, "mwavedd::mwave_exit entry\n");
520 for (i = 0; i < pDrvData->nr_registered_attrs; i++)
521 device_remove_file(&mwave_device, mwave_dev_attrs[i]);
522 pDrvData->nr_registered_attrs = 0;
524 if (pDrvData->device_registered) {
525 device_unregister(&mwave_device);
526 pDrvData->device_registered = FALSE;
530 if ( pDrvData->sLine >= 0 ) {
531 serial8250_unregister_port(pDrvData->sLine);
533 if (pDrvData->bMwaveDevRegistered) {
534 misc_deregister(&mwave_misc_dev);
536 if (pDrvData->bDSPEnabled) {
537 tp3780I_DisableDSP(&pDrvData->rBDData);
539 if (pDrvData->bResourcesClaimed) {
540 tp3780I_ReleaseResources(&pDrvData->rBDData);
542 if (pDrvData->bBDInitialized) {
543 tp3780I_Cleanup(&pDrvData->rBDData);
546 PRINTK_1(TRACE_MWAVE, "mwavedd::mwave_exit exit\n");
549 module_exit(mwave_exit);
551 static int __init mwave_init(void)
555 pMWAVE_DEVICE_DATA pDrvData = &mwave_s_mdd;
557 PRINTK_1(TRACE_MWAVE, "mwavedd::mwave_init entry\n");
559 memset(&mwave_s_mdd, 0, sizeof(MWAVE_DEVICE_DATA));
561 pDrvData->bBDInitialized = FALSE;
562 pDrvData->bResourcesClaimed = FALSE;
563 pDrvData->bDSPEnabled = FALSE;
564 pDrvData->bDSPReset = FALSE;
565 pDrvData->bMwaveDevRegistered = FALSE;
566 pDrvData->sLine = -1;
568 for (i = 0; i < ARRAY_SIZE(pDrvData->IPCs); i++) {
569 pDrvData->IPCs[i].bIsEnabled = FALSE;
570 pDrvData->IPCs[i].bIsHere = FALSE;
571 pDrvData->IPCs[i].usIntCount = 0; /* no ints received yet */
572 init_waitqueue_head(&pDrvData->IPCs[i].ipc_wait_queue);
575 retval = tp3780I_InitializeBoardData(&pDrvData->rBDData);
576 PRINTK_2(TRACE_MWAVE,
577 "mwavedd::mwave_init, return from tp3780I_InitializeBoardData"
581 PRINTK_ERROR(KERN_ERR_MWAVE
582 "mwavedd::mwave_init: Error:"
583 " Failed to initialize board data\n");
586 pDrvData->bBDInitialized = TRUE;
588 retval = tp3780I_CalcResources(&pDrvData->rBDData);
589 PRINTK_2(TRACE_MWAVE,
590 "mwavedd::mwave_init, return from tp3780I_CalcResources"
594 PRINTK_ERROR(KERN_ERR_MWAVE
595 "mwavedd:mwave_init: Error:"
596 " Failed to calculate resources\n");
600 retval = tp3780I_ClaimResources(&pDrvData->rBDData);
601 PRINTK_2(TRACE_MWAVE,
602 "mwavedd::mwave_init, return from tp3780I_ClaimResources"
606 PRINTK_ERROR(KERN_ERR_MWAVE
607 "mwavedd:mwave_init: Error:"
608 " Failed to claim resources\n");
611 pDrvData->bResourcesClaimed = TRUE;
613 retval = tp3780I_EnableDSP(&pDrvData->rBDData);
614 PRINTK_2(TRACE_MWAVE,
615 "mwavedd::mwave_init, return from tp3780I_EnableDSP"
619 PRINTK_ERROR(KERN_ERR_MWAVE
620 "mwavedd:mwave_init: Error:"
621 " Failed to enable DSP\n");
624 pDrvData->bDSPEnabled = TRUE;
626 if (misc_register(&mwave_misc_dev) < 0) {
627 PRINTK_ERROR(KERN_ERR_MWAVE
628 "mwavedd:mwave_init: Error:"
629 " Failed to register misc device\n");
632 pDrvData->bMwaveDevRegistered = TRUE;
634 pDrvData->sLine = register_serial_portandirq(
635 pDrvData->rBDData.rDspSettings.usUartBaseIO,
636 pDrvData->rBDData.rDspSettings.usUartIrq
638 if (pDrvData->sLine < 0) {
639 PRINTK_ERROR(KERN_ERR_MWAVE
640 "mwavedd:mwave_init: Error:"
641 " Failed to register serial driver\n");
644 /* uart is registered */
648 memset(&mwave_device, 0, sizeof (struct device));
649 snprintf(mwave_device.bus_id, BUS_ID_SIZE, "mwave");
651 if (device_register(&mwave_device))
653 pDrvData->device_registered = TRUE;
654 for (i = 0; i < ARRAY_SIZE(mwave_dev_attrs); i++) {
655 if(device_create_file(&mwave_device, mwave_dev_attrs[i])) {
656 PRINTK_ERROR(KERN_ERR_MWAVE
657 "mwavedd:mwave_init: Error:"
658 " Failed to create sysfs file %s\n",
659 mwave_dev_attrs[i]->attr.name);
662 pDrvData->nr_registered_attrs++;
670 PRINTK_ERROR(KERN_ERR_MWAVE
671 "mwavedd::mwave_init: Error:"
672 " Failed to initialize\n");
673 mwave_exit(); /* clean up */
678 module_init(mwave_init);