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/delay.h>
60 #include <linux/serial_8250.h>
66 MODULE_DESCRIPTION("3780i Advanced Communications Processor (Mwave) driver");
67 MODULE_AUTHOR("Mike Sullivan and Paul Schroeder");
68 MODULE_LICENSE("GPL");
71 * These parameters support the setting of MWave resources. Note that no
72 * checks are made against other devices (ie. superio) for conflicts.
73 * We'll depend on users using the tpctl utility to do that for now
76 int mwave_3780i_irq = 0;
77 int mwave_3780i_io = 0;
78 int mwave_uart_irq = 0;
79 int mwave_uart_io = 0;
80 module_param(mwave_debug, int, 0);
81 module_param(mwave_3780i_irq, int, 0);
82 module_param(mwave_3780i_io, int, 0);
83 module_param(mwave_uart_irq, int, 0);
84 module_param(mwave_uart_io, int, 0);
86 static int mwave_open(struct inode *inode, struct file *file);
87 static int mwave_close(struct inode *inode, struct file *file);
88 static int mwave_ioctl(struct inode *inode, struct file *filp,
89 unsigned int iocmd, unsigned long ioarg);
91 MWAVE_DEVICE_DATA mwave_s_mdd;
93 static int mwave_open(struct inode *inode, struct file *file)
95 unsigned int retval = 0;
98 "mwavedd::mwave_open, entry inode %p file %p\n",
100 PRINTK_2(TRACE_MWAVE,
101 "mwavedd::mwave_open, exit return retval %x\n", retval);
106 static int mwave_close(struct inode *inode, struct file *file)
108 unsigned int retval = 0;
110 PRINTK_3(TRACE_MWAVE,
111 "mwavedd::mwave_close, entry inode %p file %p\n",
114 PRINTK_2(TRACE_MWAVE, "mwavedd::mwave_close, exit retval %x\n",
120 static int mwave_ioctl(struct inode *inode, struct file *file,
121 unsigned int iocmd, unsigned long ioarg)
123 unsigned int retval = 0;
124 pMWAVE_DEVICE_DATA pDrvData = &mwave_s_mdd;
125 void __user *arg = (void __user *)ioarg;
127 PRINTK_5(TRACE_MWAVE,
128 "mwavedd::mwave_ioctl, entry inode %p file %p cmd %x arg %x\n",
129 inode, file, iocmd, (int) ioarg);
134 PRINTK_1(TRACE_MWAVE,
135 "mwavedd::mwave_ioctl, IOCTL_MW_RESET"
136 " calling tp3780I_ResetDSP\n");
137 retval = tp3780I_ResetDSP(&pDrvData->rBDData);
138 PRINTK_2(TRACE_MWAVE,
139 "mwavedd::mwave_ioctl, IOCTL_MW_RESET"
140 " retval %x from tp3780I_ResetDSP\n",
145 PRINTK_1(TRACE_MWAVE,
146 "mwavedd::mwave_ioctl, IOCTL_MW_RUN"
147 " calling tp3780I_StartDSP\n");
148 retval = tp3780I_StartDSP(&pDrvData->rBDData);
149 PRINTK_2(TRACE_MWAVE,
150 "mwavedd::mwave_ioctl, IOCTL_MW_RUN"
151 " retval %x from tp3780I_StartDSP\n",
155 case IOCTL_MW_DSP_ABILITIES: {
156 MW_ABILITIES rAbilities;
158 PRINTK_1(TRACE_MWAVE,
159 "mwavedd::mwave_ioctl,"
160 " IOCTL_MW_DSP_ABILITIES calling"
161 " tp3780I_QueryAbilities\n");
162 retval = tp3780I_QueryAbilities(&pDrvData->rBDData,
164 PRINTK_2(TRACE_MWAVE,
165 "mwavedd::mwave_ioctl, IOCTL_MW_DSP_ABILITIES"
166 " retval %x from tp3780I_QueryAbilities\n",
169 if( copy_to_user(arg, &rAbilities,
170 sizeof(MW_ABILITIES)) )
173 PRINTK_2(TRACE_MWAVE,
174 "mwavedd::mwave_ioctl, IOCTL_MW_DSP_ABILITIES"
180 case IOCTL_MW_READ_DATA:
181 case IOCTL_MW_READCLEAR_DATA: {
182 MW_READWRITE rReadData;
183 unsigned short __user *pusBuffer = NULL;
185 if( copy_from_user(&rReadData, arg,
186 sizeof(MW_READWRITE)) )
188 pusBuffer = (unsigned short __user *) (rReadData.pBuf);
190 PRINTK_4(TRACE_MWAVE,
191 "mwavedd::mwave_ioctl IOCTL_MW_READ_DATA,"
192 " size %lx, ioarg %lx pusBuffer %p\n",
193 rReadData.ulDataLength, ioarg, pusBuffer);
194 retval = tp3780I_ReadWriteDspDStore(&pDrvData->rBDData,
197 rReadData.ulDataLength,
198 rReadData.usDspAddress);
202 case IOCTL_MW_READ_INST: {
203 MW_READWRITE rReadData;
204 unsigned short __user *pusBuffer = NULL;
206 if( copy_from_user(&rReadData, arg,
207 sizeof(MW_READWRITE)) )
209 pusBuffer = (unsigned short __user *) (rReadData.pBuf);
211 PRINTK_4(TRACE_MWAVE,
212 "mwavedd::mwave_ioctl IOCTL_MW_READ_INST,"
213 " size %lx, ioarg %lx pusBuffer %p\n",
214 rReadData.ulDataLength / 2, ioarg,
216 retval = tp3780I_ReadWriteDspDStore(&pDrvData->rBDData,
218 rReadData.ulDataLength / 2,
219 rReadData.usDspAddress);
223 case IOCTL_MW_WRITE_DATA: {
224 MW_READWRITE rWriteData;
225 unsigned short __user *pusBuffer = NULL;
227 if( copy_from_user(&rWriteData, arg,
228 sizeof(MW_READWRITE)) )
230 pusBuffer = (unsigned short __user *) (rWriteData.pBuf);
232 PRINTK_4(TRACE_MWAVE,
233 "mwavedd::mwave_ioctl IOCTL_MW_WRITE_DATA,"
234 " size %lx, ioarg %lx pusBuffer %p\n",
235 rWriteData.ulDataLength, ioarg,
237 retval = tp3780I_ReadWriteDspDStore(&pDrvData->rBDData,
239 rWriteData.ulDataLength,
240 rWriteData.usDspAddress);
244 case IOCTL_MW_WRITE_INST: {
245 MW_READWRITE rWriteData;
246 unsigned short __user *pusBuffer = NULL;
248 if( copy_from_user(&rWriteData, arg,
249 sizeof(MW_READWRITE)) )
251 pusBuffer = (unsigned short __user *)(rWriteData.pBuf);
253 PRINTK_4(TRACE_MWAVE,
254 "mwavedd::mwave_ioctl IOCTL_MW_WRITE_INST,"
255 " size %lx, ioarg %lx pusBuffer %p\n",
256 rWriteData.ulDataLength, ioarg,
258 retval = tp3780I_ReadWriteDspIStore(&pDrvData->rBDData,
260 rWriteData.ulDataLength,
261 rWriteData.usDspAddress);
265 case IOCTL_MW_REGISTER_IPC: {
266 unsigned int ipcnum = (unsigned int) ioarg;
268 PRINTK_3(TRACE_MWAVE,
269 "mwavedd::mwave_ioctl IOCTL_MW_REGISTER_IPC"
270 " ipcnum %x entry usIntCount %x\n",
272 pDrvData->IPCs[ipcnum].usIntCount);
274 if (ipcnum > ARRAY_SIZE(pDrvData->IPCs)) {
275 PRINTK_ERROR(KERN_ERR_MWAVE
276 "mwavedd::mwave_ioctl:"
277 " IOCTL_MW_REGISTER_IPC:"
278 " Error: Invalid ipcnum %x\n",
282 pDrvData->IPCs[ipcnum].bIsHere = FALSE;
283 pDrvData->IPCs[ipcnum].bIsEnabled = TRUE;
285 PRINTK_2(TRACE_MWAVE,
286 "mwavedd::mwave_ioctl IOCTL_MW_REGISTER_IPC"
292 case IOCTL_MW_GET_IPC: {
293 unsigned int ipcnum = (unsigned int) ioarg;
295 PRINTK_3(TRACE_MWAVE,
296 "mwavedd::mwave_ioctl IOCTL_MW_GET_IPC"
297 " ipcnum %x, usIntCount %x\n",
299 pDrvData->IPCs[ipcnum].usIntCount);
300 if (ipcnum > ARRAY_SIZE(pDrvData->IPCs)) {
301 PRINTK_ERROR(KERN_ERR_MWAVE
302 "mwavedd::mwave_ioctl:"
303 " IOCTL_MW_GET_IPC: Error:"
304 " Invalid ipcnum %x\n", ipcnum);
308 if (pDrvData->IPCs[ipcnum].bIsEnabled == TRUE) {
309 DECLARE_WAITQUEUE(wait, current);
311 PRINTK_2(TRACE_MWAVE,
312 "mwavedd::mwave_ioctl, thread for"
313 " ipc %x going to sleep\n",
315 add_wait_queue(&pDrvData->IPCs[ipcnum].ipc_wait_queue, &wait);
316 pDrvData->IPCs[ipcnum].bIsHere = TRUE;
317 set_current_state(TASK_INTERRUPTIBLE);
318 /* check whether an event was signalled by */
319 /* the interrupt handler while we were gone */
320 if (pDrvData->IPCs[ipcnum].usIntCount == 1) { /* first int has occurred (race condition) */
321 pDrvData->IPCs[ipcnum].usIntCount = 2; /* first int has been handled */
322 PRINTK_2(TRACE_MWAVE,
323 "mwavedd::mwave_ioctl"
324 " IOCTL_MW_GET_IPC ipcnum %x"
325 " handling first int\n",
327 } else { /* either 1st int has not yet occurred, or we have already handled the first int */
329 if (pDrvData->IPCs[ipcnum].usIntCount == 1) {
330 pDrvData->IPCs[ipcnum].usIntCount = 2;
332 PRINTK_2(TRACE_MWAVE,
333 "mwavedd::mwave_ioctl"
334 " IOCTL_MW_GET_IPC ipcnum %x"
335 " woke up and returning to"
339 pDrvData->IPCs[ipcnum].bIsHere = FALSE;
340 remove_wait_queue(&pDrvData->IPCs[ipcnum].ipc_wait_queue, &wait);
341 set_current_state(TASK_RUNNING);
342 PRINTK_2(TRACE_MWAVE,
343 "mwavedd::mwave_ioctl IOCTL_MW_GET_IPC,"
344 " returning thread for ipc %x"
351 case IOCTL_MW_UNREGISTER_IPC: {
352 unsigned int ipcnum = (unsigned int) ioarg;
354 PRINTK_2(TRACE_MWAVE,
355 "mwavedd::mwave_ioctl IOCTL_MW_UNREGISTER_IPC"
358 if (ipcnum > ARRAY_SIZE(pDrvData->IPCs)) {
359 PRINTK_ERROR(KERN_ERR_MWAVE
360 "mwavedd::mwave_ioctl:"
361 " IOCTL_MW_UNREGISTER_IPC:"
362 " Error: Invalid ipcnum %x\n",
366 if (pDrvData->IPCs[ipcnum].bIsEnabled == TRUE) {
367 pDrvData->IPCs[ipcnum].bIsEnabled = FALSE;
368 if (pDrvData->IPCs[ipcnum].bIsHere == TRUE) {
369 wake_up_interruptible(&pDrvData->IPCs[ipcnum].ipc_wait_queue);
376 PRINTK_ERROR(KERN_ERR_MWAVE "mwavedd::mwave_ioctl:"
377 " Error: Unrecognized iocmd %x\n",
383 PRINTK_2(TRACE_MWAVE, "mwavedd::mwave_ioctl, exit retval %x\n", retval);
389 static ssize_t mwave_read(struct file *file, char __user *buf, size_t count,
392 PRINTK_5(TRACE_MWAVE,
393 "mwavedd::mwave_read entry file %p, buf %p, count %zx ppos %p\n",
394 file, buf, count, ppos);
400 static ssize_t mwave_write(struct file *file, const char __user *buf,
401 size_t count, loff_t * ppos)
403 PRINTK_5(TRACE_MWAVE,
404 "mwavedd::mwave_write entry file %p, buf %p,"
405 " count %zx ppos %p\n",
406 file, buf, count, ppos);
412 static int register_serial_portandirq(unsigned int port, int irq)
414 struct uart_port uart;
424 PRINTK_ERROR(KERN_ERR_MWAVE
425 "mwavedd::register_serial_portandirq:"
426 " Error: Illegal port %x\n", port );
439 PRINTK_ERROR(KERN_ERR_MWAVE
440 "mwavedd::register_serial_portandirq:"
441 " Error: Illegal irq %x\n", irq );
446 memset(&uart, 0, sizeof(struct uart_port));
448 uart.uartclk = 1843200;
451 uart.iotype = UPIO_PORT;
452 uart.flags = UPF_SHARE_IRQ;
453 return serial8250_register_port(&uart);
457 static struct file_operations mwave_fops = {
458 .owner = THIS_MODULE,
460 .write = mwave_write,
461 .ioctl = mwave_ioctl,
463 .release = mwave_close
467 static struct miscdevice mwave_misc_dev = { MWAVE_MINOR, "mwave", &mwave_fops };
469 #if 0 /* totally b0rked */
471 * sysfs support <paulsch@us.ibm.com>
474 struct device mwave_device;
476 /* Prevent code redundancy, create a macro for mwave_show_* functions. */
477 #define mwave_show_function(attr_name, format_string, field) \
478 static ssize_t mwave_show_##attr_name(struct device *dev, struct device_attribute *attr, char *buf) \
480 DSP_3780I_CONFIG_SETTINGS *pSettings = \
481 &mwave_s_mdd.rBDData.rDspSettings; \
482 return sprintf(buf, format_string, pSettings->field); \
485 /* All of our attributes are read attributes. */
486 #define mwave_dev_rd_attr(attr_name, format_string, field) \
487 mwave_show_function(attr_name, format_string, field) \
488 static DEVICE_ATTR(attr_name, S_IRUGO, mwave_show_##attr_name, NULL)
490 mwave_dev_rd_attr (3780i_dma, "%i\n", usDspDma);
491 mwave_dev_rd_attr (3780i_irq, "%i\n", usDspIrq);
492 mwave_dev_rd_attr (3780i_io, "%#.4x\n", usDspBaseIO);
493 mwave_dev_rd_attr (uart_irq, "%i\n", usUartIrq);
494 mwave_dev_rd_attr (uart_io, "%#.4x\n", usUartBaseIO);
496 static struct device_attribute * const mwave_dev_attrs[] = {
506 * mwave_init is called on module load
508 * mwave_exit is called on module unload
509 * mwave_exit is also used to clean up after an aborted mwave_init
511 static void mwave_exit(void)
513 pMWAVE_DEVICE_DATA pDrvData = &mwave_s_mdd;
515 PRINTK_1(TRACE_MWAVE, "mwavedd::mwave_exit entry\n");
518 for (i = 0; i < pDrvData->nr_registered_attrs; i++)
519 device_remove_file(&mwave_device, mwave_dev_attrs[i]);
520 pDrvData->nr_registered_attrs = 0;
522 if (pDrvData->device_registered) {
523 device_unregister(&mwave_device);
524 pDrvData->device_registered = FALSE;
528 if ( pDrvData->sLine >= 0 ) {
529 serial8250_unregister_port(pDrvData->sLine);
531 if (pDrvData->bMwaveDevRegistered) {
532 misc_deregister(&mwave_misc_dev);
534 if (pDrvData->bDSPEnabled) {
535 tp3780I_DisableDSP(&pDrvData->rBDData);
537 if (pDrvData->bResourcesClaimed) {
538 tp3780I_ReleaseResources(&pDrvData->rBDData);
540 if (pDrvData->bBDInitialized) {
541 tp3780I_Cleanup(&pDrvData->rBDData);
544 PRINTK_1(TRACE_MWAVE, "mwavedd::mwave_exit exit\n");
547 module_exit(mwave_exit);
549 static int __init mwave_init(void)
553 pMWAVE_DEVICE_DATA pDrvData = &mwave_s_mdd;
555 PRINTK_1(TRACE_MWAVE, "mwavedd::mwave_init entry\n");
557 memset(&mwave_s_mdd, 0, sizeof(MWAVE_DEVICE_DATA));
559 pDrvData->bBDInitialized = FALSE;
560 pDrvData->bResourcesClaimed = FALSE;
561 pDrvData->bDSPEnabled = FALSE;
562 pDrvData->bDSPReset = FALSE;
563 pDrvData->bMwaveDevRegistered = FALSE;
564 pDrvData->sLine = -1;
566 for (i = 0; i < ARRAY_SIZE(pDrvData->IPCs); i++) {
567 pDrvData->IPCs[i].bIsEnabled = FALSE;
568 pDrvData->IPCs[i].bIsHere = FALSE;
569 pDrvData->IPCs[i].usIntCount = 0; /* no ints received yet */
570 init_waitqueue_head(&pDrvData->IPCs[i].ipc_wait_queue);
573 retval = tp3780I_InitializeBoardData(&pDrvData->rBDData);
574 PRINTK_2(TRACE_MWAVE,
575 "mwavedd::mwave_init, return from tp3780I_InitializeBoardData"
579 PRINTK_ERROR(KERN_ERR_MWAVE
580 "mwavedd::mwave_init: Error:"
581 " Failed to initialize board data\n");
584 pDrvData->bBDInitialized = TRUE;
586 retval = tp3780I_CalcResources(&pDrvData->rBDData);
587 PRINTK_2(TRACE_MWAVE,
588 "mwavedd::mwave_init, return from tp3780I_CalcResources"
592 PRINTK_ERROR(KERN_ERR_MWAVE
593 "mwavedd:mwave_init: Error:"
594 " Failed to calculate resources\n");
598 retval = tp3780I_ClaimResources(&pDrvData->rBDData);
599 PRINTK_2(TRACE_MWAVE,
600 "mwavedd::mwave_init, return from tp3780I_ClaimResources"
604 PRINTK_ERROR(KERN_ERR_MWAVE
605 "mwavedd:mwave_init: Error:"
606 " Failed to claim resources\n");
609 pDrvData->bResourcesClaimed = TRUE;
611 retval = tp3780I_EnableDSP(&pDrvData->rBDData);
612 PRINTK_2(TRACE_MWAVE,
613 "mwavedd::mwave_init, return from tp3780I_EnableDSP"
617 PRINTK_ERROR(KERN_ERR_MWAVE
618 "mwavedd:mwave_init: Error:"
619 " Failed to enable DSP\n");
622 pDrvData->bDSPEnabled = TRUE;
624 if (misc_register(&mwave_misc_dev) < 0) {
625 PRINTK_ERROR(KERN_ERR_MWAVE
626 "mwavedd:mwave_init: Error:"
627 " Failed to register misc device\n");
630 pDrvData->bMwaveDevRegistered = TRUE;
632 pDrvData->sLine = register_serial_portandirq(
633 pDrvData->rBDData.rDspSettings.usUartBaseIO,
634 pDrvData->rBDData.rDspSettings.usUartIrq
636 if (pDrvData->sLine < 0) {
637 PRINTK_ERROR(KERN_ERR_MWAVE
638 "mwavedd:mwave_init: Error:"
639 " Failed to register serial driver\n");
642 /* uart is registered */
646 memset(&mwave_device, 0, sizeof (struct device));
647 snprintf(mwave_device.bus_id, BUS_ID_SIZE, "mwave");
649 if (device_register(&mwave_device))
651 pDrvData->device_registered = TRUE;
652 for (i = 0; i < ARRAY_SIZE(mwave_dev_attrs); i++) {
653 if(device_create_file(&mwave_device, mwave_dev_attrs[i])) {
654 PRINTK_ERROR(KERN_ERR_MWAVE
655 "mwavedd:mwave_init: Error:"
656 " Failed to create sysfs file %s\n",
657 mwave_dev_attrs[i]->attr.name);
660 pDrvData->nr_registered_attrs++;
668 PRINTK_ERROR(KERN_ERR_MWAVE
669 "mwavedd::mwave_init: Error:"
670 " Failed to initialize\n");
671 mwave_exit(); /* clean up */
676 module_init(mwave_init);