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>
65 MODULE_DESCRIPTION("3780i Advanced Communications Processor (Mwave) driver");
66 MODULE_AUTHOR("Mike Sullivan and Paul Schroeder");
67 MODULE_LICENSE("GPL");
70 * These parameters support the setting of MWave resources. Note that no
71 * checks are made against other devices (ie. superio) for conflicts.
72 * We'll depend on users using the tpctl utility to do that for now
75 int mwave_3780i_irq = 0;
76 int mwave_3780i_io = 0;
77 int mwave_uart_irq = 0;
78 int mwave_uart_io = 0;
79 module_param(mwave_debug, int, 0);
80 module_param(mwave_3780i_irq, int, 0);
81 module_param(mwave_3780i_io, int, 0);
82 module_param(mwave_uart_irq, int, 0);
83 module_param(mwave_uart_io, int, 0);
85 static int mwave_open(struct inode *inode, struct file *file);
86 static int mwave_close(struct inode *inode, struct file *file);
87 static int mwave_ioctl(struct inode *inode, struct file *filp,
88 unsigned int iocmd, unsigned long ioarg);
90 MWAVE_DEVICE_DATA mwave_s_mdd;
92 static int mwave_open(struct inode *inode, struct file *file)
94 unsigned int retval = 0;
97 "mwavedd::mwave_open, entry inode %p file %p\n",
100 "mwavedd::mwave_open, exit return retval %x\n", retval);
105 static int mwave_close(struct inode *inode, struct file *file)
107 unsigned int retval = 0;
109 PRINTK_3(TRACE_MWAVE,
110 "mwavedd::mwave_close, entry inode %p file %p\n",
113 PRINTK_2(TRACE_MWAVE, "mwavedd::mwave_close, exit retval %x\n",
119 static int mwave_ioctl(struct inode *inode, struct file *file,
120 unsigned int iocmd, unsigned long ioarg)
122 unsigned int retval = 0;
123 pMWAVE_DEVICE_DATA pDrvData = &mwave_s_mdd;
124 void __user *arg = (void __user *)ioarg;
126 PRINTK_5(TRACE_MWAVE,
127 "mwavedd::mwave_ioctl, entry inode %p file %p cmd %x arg %x\n",
128 inode, file, iocmd, (int) ioarg);
133 PRINTK_1(TRACE_MWAVE,
134 "mwavedd::mwave_ioctl, IOCTL_MW_RESET"
135 " calling tp3780I_ResetDSP\n");
136 retval = tp3780I_ResetDSP(&pDrvData->rBDData);
137 PRINTK_2(TRACE_MWAVE,
138 "mwavedd::mwave_ioctl, IOCTL_MW_RESET"
139 " retval %x from tp3780I_ResetDSP\n",
144 PRINTK_1(TRACE_MWAVE,
145 "mwavedd::mwave_ioctl, IOCTL_MW_RUN"
146 " calling tp3780I_StartDSP\n");
147 retval = tp3780I_StartDSP(&pDrvData->rBDData);
148 PRINTK_2(TRACE_MWAVE,
149 "mwavedd::mwave_ioctl, IOCTL_MW_RUN"
150 " retval %x from tp3780I_StartDSP\n",
154 case IOCTL_MW_DSP_ABILITIES: {
155 MW_ABILITIES rAbilities;
157 PRINTK_1(TRACE_MWAVE,
158 "mwavedd::mwave_ioctl,"
159 " IOCTL_MW_DSP_ABILITIES calling"
160 " tp3780I_QueryAbilities\n");
161 retval = tp3780I_QueryAbilities(&pDrvData->rBDData,
163 PRINTK_2(TRACE_MWAVE,
164 "mwavedd::mwave_ioctl, IOCTL_MW_DSP_ABILITIES"
165 " retval %x from tp3780I_QueryAbilities\n",
168 if( copy_to_user(arg, &rAbilities,
169 sizeof(MW_ABILITIES)) )
172 PRINTK_2(TRACE_MWAVE,
173 "mwavedd::mwave_ioctl, IOCTL_MW_DSP_ABILITIES"
179 case IOCTL_MW_READ_DATA:
180 case IOCTL_MW_READCLEAR_DATA: {
181 MW_READWRITE rReadData;
182 unsigned short __user *pusBuffer = NULL;
184 if( copy_from_user(&rReadData, arg,
185 sizeof(MW_READWRITE)) )
187 pusBuffer = (unsigned short __user *) (rReadData.pBuf);
189 PRINTK_4(TRACE_MWAVE,
190 "mwavedd::mwave_ioctl IOCTL_MW_READ_DATA,"
191 " size %lx, ioarg %lx pusBuffer %p\n",
192 rReadData.ulDataLength, ioarg, pusBuffer);
193 retval = tp3780I_ReadWriteDspDStore(&pDrvData->rBDData,
196 rReadData.ulDataLength,
197 rReadData.usDspAddress);
201 case IOCTL_MW_READ_INST: {
202 MW_READWRITE rReadData;
203 unsigned short __user *pusBuffer = NULL;
205 if( copy_from_user(&rReadData, arg,
206 sizeof(MW_READWRITE)) )
208 pusBuffer = (unsigned short __user *) (rReadData.pBuf);
210 PRINTK_4(TRACE_MWAVE,
211 "mwavedd::mwave_ioctl IOCTL_MW_READ_INST,"
212 " size %lx, ioarg %lx pusBuffer %p\n",
213 rReadData.ulDataLength / 2, ioarg,
215 retval = tp3780I_ReadWriteDspDStore(&pDrvData->rBDData,
217 rReadData.ulDataLength / 2,
218 rReadData.usDspAddress);
222 case IOCTL_MW_WRITE_DATA: {
223 MW_READWRITE rWriteData;
224 unsigned short __user *pusBuffer = NULL;
226 if( copy_from_user(&rWriteData, arg,
227 sizeof(MW_READWRITE)) )
229 pusBuffer = (unsigned short __user *) (rWriteData.pBuf);
231 PRINTK_4(TRACE_MWAVE,
232 "mwavedd::mwave_ioctl IOCTL_MW_WRITE_DATA,"
233 " size %lx, ioarg %lx pusBuffer %p\n",
234 rWriteData.ulDataLength, ioarg,
236 retval = tp3780I_ReadWriteDspDStore(&pDrvData->rBDData,
238 rWriteData.ulDataLength,
239 rWriteData.usDspAddress);
243 case IOCTL_MW_WRITE_INST: {
244 MW_READWRITE rWriteData;
245 unsigned short __user *pusBuffer = NULL;
247 if( copy_from_user(&rWriteData, arg,
248 sizeof(MW_READWRITE)) )
250 pusBuffer = (unsigned short __user *)(rWriteData.pBuf);
252 PRINTK_4(TRACE_MWAVE,
253 "mwavedd::mwave_ioctl IOCTL_MW_WRITE_INST,"
254 " size %lx, ioarg %lx pusBuffer %p\n",
255 rWriteData.ulDataLength, ioarg,
257 retval = tp3780I_ReadWriteDspIStore(&pDrvData->rBDData,
259 rWriteData.ulDataLength,
260 rWriteData.usDspAddress);
264 case IOCTL_MW_REGISTER_IPC: {
265 unsigned int ipcnum = (unsigned int) ioarg;
267 PRINTK_3(TRACE_MWAVE,
268 "mwavedd::mwave_ioctl IOCTL_MW_REGISTER_IPC"
269 " ipcnum %x entry usIntCount %x\n",
271 pDrvData->IPCs[ipcnum].usIntCount);
273 if (ipcnum > ARRAY_SIZE(pDrvData->IPCs)) {
274 PRINTK_ERROR(KERN_ERR_MWAVE
275 "mwavedd::mwave_ioctl:"
276 " IOCTL_MW_REGISTER_IPC:"
277 " Error: Invalid ipcnum %x\n",
281 pDrvData->IPCs[ipcnum].bIsHere = FALSE;
282 pDrvData->IPCs[ipcnum].bIsEnabled = TRUE;
284 PRINTK_2(TRACE_MWAVE,
285 "mwavedd::mwave_ioctl IOCTL_MW_REGISTER_IPC"
291 case IOCTL_MW_GET_IPC: {
292 unsigned int ipcnum = (unsigned int) ioarg;
294 PRINTK_3(TRACE_MWAVE,
295 "mwavedd::mwave_ioctl IOCTL_MW_GET_IPC"
296 " ipcnum %x, usIntCount %x\n",
298 pDrvData->IPCs[ipcnum].usIntCount);
299 if (ipcnum > ARRAY_SIZE(pDrvData->IPCs)) {
300 PRINTK_ERROR(KERN_ERR_MWAVE
301 "mwavedd::mwave_ioctl:"
302 " IOCTL_MW_GET_IPC: Error:"
303 " Invalid ipcnum %x\n", ipcnum);
307 if (pDrvData->IPCs[ipcnum].bIsEnabled == TRUE) {
308 DECLARE_WAITQUEUE(wait, current);
310 PRINTK_2(TRACE_MWAVE,
311 "mwavedd::mwave_ioctl, thread for"
312 " ipc %x going to sleep\n",
314 add_wait_queue(&pDrvData->IPCs[ipcnum].ipc_wait_queue, &wait);
315 pDrvData->IPCs[ipcnum].bIsHere = TRUE;
316 set_current_state(TASK_INTERRUPTIBLE);
317 /* check whether an event was signalled by */
318 /* the interrupt handler while we were gone */
319 if (pDrvData->IPCs[ipcnum].usIntCount == 1) { /* first int has occurred (race condition) */
320 pDrvData->IPCs[ipcnum].usIntCount = 2; /* first int has been handled */
321 PRINTK_2(TRACE_MWAVE,
322 "mwavedd::mwave_ioctl"
323 " IOCTL_MW_GET_IPC ipcnum %x"
324 " handling first int\n",
326 } else { /* either 1st int has not yet occurred, or we have already handled the first int */
328 if (pDrvData->IPCs[ipcnum].usIntCount == 1) {
329 pDrvData->IPCs[ipcnum].usIntCount = 2;
331 PRINTK_2(TRACE_MWAVE,
332 "mwavedd::mwave_ioctl"
333 " IOCTL_MW_GET_IPC ipcnum %x"
334 " woke up and returning to"
338 pDrvData->IPCs[ipcnum].bIsHere = FALSE;
339 remove_wait_queue(&pDrvData->IPCs[ipcnum].ipc_wait_queue, &wait);
340 set_current_state(TASK_RUNNING);
341 PRINTK_2(TRACE_MWAVE,
342 "mwavedd::mwave_ioctl IOCTL_MW_GET_IPC,"
343 " returning thread for ipc %x"
350 case IOCTL_MW_UNREGISTER_IPC: {
351 unsigned int ipcnum = (unsigned int) ioarg;
353 PRINTK_2(TRACE_MWAVE,
354 "mwavedd::mwave_ioctl IOCTL_MW_UNREGISTER_IPC"
357 if (ipcnum > ARRAY_SIZE(pDrvData->IPCs)) {
358 PRINTK_ERROR(KERN_ERR_MWAVE
359 "mwavedd::mwave_ioctl:"
360 " IOCTL_MW_UNREGISTER_IPC:"
361 " Error: Invalid ipcnum %x\n",
365 if (pDrvData->IPCs[ipcnum].bIsEnabled == TRUE) {
366 pDrvData->IPCs[ipcnum].bIsEnabled = FALSE;
367 if (pDrvData->IPCs[ipcnum].bIsHere == TRUE) {
368 wake_up_interruptible(&pDrvData->IPCs[ipcnum].ipc_wait_queue);
375 PRINTK_ERROR(KERN_ERR_MWAVE "mwavedd::mwave_ioctl:"
376 " Error: Unrecognized iocmd %x\n",
382 PRINTK_2(TRACE_MWAVE, "mwavedd::mwave_ioctl, exit retval %x\n", retval);
388 static ssize_t mwave_read(struct file *file, char __user *buf, size_t count,
391 PRINTK_5(TRACE_MWAVE,
392 "mwavedd::mwave_read entry file %p, buf %p, count %zx ppos %p\n",
393 file, buf, count, ppos);
399 static ssize_t mwave_write(struct file *file, const char __user *buf,
400 size_t count, loff_t * ppos)
402 PRINTK_5(TRACE_MWAVE,
403 "mwavedd::mwave_write entry file %p, buf %p,"
404 " count %zx ppos %p\n",
405 file, buf, count, ppos);
411 static int register_serial_portandirq(unsigned int port, int irq)
413 struct serial_struct serial;
423 PRINTK_ERROR(KERN_ERR_MWAVE
424 "mwavedd::register_serial_portandirq:"
425 " Error: Illegal port %x\n", port );
438 PRINTK_ERROR(KERN_ERR_MWAVE
439 "mwavedd::register_serial_portandirq:"
440 " Error: Illegal irq %x\n", irq );
445 memset(&serial, 0, sizeof(serial));
448 serial.flags = ASYNC_SHARE_IRQ;
450 return register_serial(&serial);
454 static struct file_operations mwave_fops = {
455 .owner = THIS_MODULE,
457 .write = mwave_write,
458 .ioctl = mwave_ioctl,
460 .release = mwave_close
464 static struct miscdevice mwave_misc_dev = { MWAVE_MINOR, "mwave", &mwave_fops };
466 #if 0 /* totally b0rked */
468 * sysfs support <paulsch@us.ibm.com>
471 struct device mwave_device;
473 /* Prevent code redundancy, create a macro for mwave_show_* functions. */
474 #define mwave_show_function(attr_name, format_string, field) \
475 static ssize_t mwave_show_##attr_name(struct device *dev, struct device_attribute *attr, char *buf) \
477 DSP_3780I_CONFIG_SETTINGS *pSettings = \
478 &mwave_s_mdd.rBDData.rDspSettings; \
479 return sprintf(buf, format_string, pSettings->field); \
482 /* All of our attributes are read attributes. */
483 #define mwave_dev_rd_attr(attr_name, format_string, field) \
484 mwave_show_function(attr_name, format_string, field) \
485 static DEVICE_ATTR(attr_name, S_IRUGO, mwave_show_##attr_name, NULL)
487 mwave_dev_rd_attr (3780i_dma, "%i\n", usDspDma);
488 mwave_dev_rd_attr (3780i_irq, "%i\n", usDspIrq);
489 mwave_dev_rd_attr (3780i_io, "%#.4x\n", usDspBaseIO);
490 mwave_dev_rd_attr (uart_irq, "%i\n", usUartIrq);
491 mwave_dev_rd_attr (uart_io, "%#.4x\n", usUartBaseIO);
493 static struct device_attribute * const mwave_dev_attrs[] = {
503 * mwave_init is called on module load
505 * mwave_exit is called on module unload
506 * mwave_exit is also used to clean up after an aborted mwave_init
508 static void mwave_exit(void)
510 pMWAVE_DEVICE_DATA pDrvData = &mwave_s_mdd;
512 PRINTK_1(TRACE_MWAVE, "mwavedd::mwave_exit entry\n");
515 for (i = 0; i < pDrvData->nr_registered_attrs; i++)
516 device_remove_file(&mwave_device, mwave_dev_attrs[i]);
517 pDrvData->nr_registered_attrs = 0;
519 if (pDrvData->device_registered) {
520 device_unregister(&mwave_device);
521 pDrvData->device_registered = FALSE;
525 if ( pDrvData->sLine >= 0 ) {
526 unregister_serial(pDrvData->sLine);
528 if (pDrvData->bMwaveDevRegistered) {
529 misc_deregister(&mwave_misc_dev);
531 if (pDrvData->bDSPEnabled) {
532 tp3780I_DisableDSP(&pDrvData->rBDData);
534 if (pDrvData->bResourcesClaimed) {
535 tp3780I_ReleaseResources(&pDrvData->rBDData);
537 if (pDrvData->bBDInitialized) {
538 tp3780I_Cleanup(&pDrvData->rBDData);
541 PRINTK_1(TRACE_MWAVE, "mwavedd::mwave_exit exit\n");
544 module_exit(mwave_exit);
546 static int __init mwave_init(void)
550 pMWAVE_DEVICE_DATA pDrvData = &mwave_s_mdd;
552 PRINTK_1(TRACE_MWAVE, "mwavedd::mwave_init entry\n");
554 memset(&mwave_s_mdd, 0, sizeof(MWAVE_DEVICE_DATA));
556 pDrvData->bBDInitialized = FALSE;
557 pDrvData->bResourcesClaimed = FALSE;
558 pDrvData->bDSPEnabled = FALSE;
559 pDrvData->bDSPReset = FALSE;
560 pDrvData->bMwaveDevRegistered = FALSE;
561 pDrvData->sLine = -1;
563 for (i = 0; i < ARRAY_SIZE(pDrvData->IPCs); i++) {
564 pDrvData->IPCs[i].bIsEnabled = FALSE;
565 pDrvData->IPCs[i].bIsHere = FALSE;
566 pDrvData->IPCs[i].usIntCount = 0; /* no ints received yet */
567 init_waitqueue_head(&pDrvData->IPCs[i].ipc_wait_queue);
570 retval = tp3780I_InitializeBoardData(&pDrvData->rBDData);
571 PRINTK_2(TRACE_MWAVE,
572 "mwavedd::mwave_init, return from tp3780I_InitializeBoardData"
576 PRINTK_ERROR(KERN_ERR_MWAVE
577 "mwavedd::mwave_init: Error:"
578 " Failed to initialize board data\n");
581 pDrvData->bBDInitialized = TRUE;
583 retval = tp3780I_CalcResources(&pDrvData->rBDData);
584 PRINTK_2(TRACE_MWAVE,
585 "mwavedd::mwave_init, return from tp3780I_CalcResources"
589 PRINTK_ERROR(KERN_ERR_MWAVE
590 "mwavedd:mwave_init: Error:"
591 " Failed to calculate resources\n");
595 retval = tp3780I_ClaimResources(&pDrvData->rBDData);
596 PRINTK_2(TRACE_MWAVE,
597 "mwavedd::mwave_init, return from tp3780I_ClaimResources"
601 PRINTK_ERROR(KERN_ERR_MWAVE
602 "mwavedd:mwave_init: Error:"
603 " Failed to claim resources\n");
606 pDrvData->bResourcesClaimed = TRUE;
608 retval = tp3780I_EnableDSP(&pDrvData->rBDData);
609 PRINTK_2(TRACE_MWAVE,
610 "mwavedd::mwave_init, return from tp3780I_EnableDSP"
614 PRINTK_ERROR(KERN_ERR_MWAVE
615 "mwavedd:mwave_init: Error:"
616 " Failed to enable DSP\n");
619 pDrvData->bDSPEnabled = TRUE;
621 if (misc_register(&mwave_misc_dev) < 0) {
622 PRINTK_ERROR(KERN_ERR_MWAVE
623 "mwavedd:mwave_init: Error:"
624 " Failed to register misc device\n");
627 pDrvData->bMwaveDevRegistered = TRUE;
629 pDrvData->sLine = register_serial_portandirq(
630 pDrvData->rBDData.rDspSettings.usUartBaseIO,
631 pDrvData->rBDData.rDspSettings.usUartIrq
633 if (pDrvData->sLine < 0) {
634 PRINTK_ERROR(KERN_ERR_MWAVE
635 "mwavedd:mwave_init: Error:"
636 " Failed to register serial driver\n");
639 /* uart is registered */
643 memset(&mwave_device, 0, sizeof (struct device));
644 snprintf(mwave_device.bus_id, BUS_ID_SIZE, "mwave");
646 if (device_register(&mwave_device))
648 pDrvData->device_registered = TRUE;
649 for (i = 0; i < ARRAY_SIZE(mwave_dev_attrs); i++) {
650 if(device_create_file(&mwave_device, mwave_dev_attrs[i])) {
651 PRINTK_ERROR(KERN_ERR_MWAVE
652 "mwavedd:mwave_init: Error:"
653 " Failed to create sysfs file %s\n",
654 mwave_dev_attrs[i]->attr.name);
657 pDrvData->nr_registered_attrs++;
665 PRINTK_ERROR(KERN_ERR_MWAVE
666 "mwavedd::mwave_init: Error:"
667 " Failed to initialize\n");
668 mwave_exit(); /* clean up */
673 module_init(mwave_init);