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_PARM(mwave_debug, "i");
80 MODULE_PARM(mwave_3780i_irq, "i");
81 MODULE_PARM(mwave_3780i_io, "i");
82 MODULE_PARM(mwave_uart_irq, "i");
83 MODULE_PARM(mwave_uart_io, "i");
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 %x file %x\n",
98 (int) inode, (int) file);
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 %x file %x\n",
111 (int) inode, (int) file);
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;
125 PRINTK_5(TRACE_MWAVE,
126 "mwavedd::mwave_ioctl, entry inode %x file %x cmd %x arg %x\n",
127 (int) inode, (int) file, iocmd, (int) ioarg);
132 PRINTK_1(TRACE_MWAVE,
133 "mwavedd::mwave_ioctl, IOCTL_MW_RESET"
134 " calling tp3780I_ResetDSP\n");
135 retval = tp3780I_ResetDSP(&pDrvData->rBDData);
136 PRINTK_2(TRACE_MWAVE,
137 "mwavedd::mwave_ioctl, IOCTL_MW_RESET"
138 " retval %x from tp3780I_ResetDSP\n",
143 PRINTK_1(TRACE_MWAVE,
144 "mwavedd::mwave_ioctl, IOCTL_MW_RUN"
145 " calling tp3780I_StartDSP\n");
146 retval = tp3780I_StartDSP(&pDrvData->rBDData);
147 PRINTK_2(TRACE_MWAVE,
148 "mwavedd::mwave_ioctl, IOCTL_MW_RUN"
149 " retval %x from tp3780I_StartDSP\n",
153 case IOCTL_MW_DSP_ABILITIES: {
154 MW_ABILITIES rAbilities;
156 PRINTK_1(TRACE_MWAVE,
157 "mwavedd::mwave_ioctl,"
158 " IOCTL_MW_DSP_ABILITIES calling"
159 " tp3780I_QueryAbilities\n");
160 retval = tp3780I_QueryAbilities(&pDrvData->rBDData,
162 PRINTK_2(TRACE_MWAVE,
163 "mwavedd::mwave_ioctl, IOCTL_MW_DSP_ABILITIES"
164 " retval %x from tp3780I_QueryAbilities\n",
167 if( copy_to_user((char *) ioarg,
168 (char *) &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 *pusBuffer = 0;
184 if( copy_from_user((char *) &rReadData,
186 sizeof(MW_READWRITE)) )
188 pusBuffer = (unsigned short *) (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 *pusBuffer = 0;
206 if( copy_from_user((char *) &rReadData, (char *) ioarg,
207 sizeof(MW_READWRITE)) )
209 pusBuffer = (unsigned short *) (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 *pusBuffer = 0;
227 if( copy_from_user((char *) &rWriteData,
229 sizeof(MW_READWRITE)) )
231 pusBuffer = (unsigned short *) (rWriteData.pBuf);
233 PRINTK_4(TRACE_MWAVE,
234 "mwavedd::mwave_ioctl IOCTL_MW_WRITE_DATA,"
235 " size %lx, ioarg %lx pusBuffer %p\n",
236 rWriteData.ulDataLength, ioarg,
238 retval = tp3780I_ReadWriteDspDStore(&pDrvData->rBDData,
240 rWriteData.ulDataLength,
241 rWriteData.usDspAddress);
245 case IOCTL_MW_WRITE_INST: {
246 MW_READWRITE rWriteData;
247 unsigned short *pusBuffer = 0;
249 if( copy_from_user((char *) &rWriteData,
251 sizeof(MW_READWRITE)) )
253 pusBuffer = (unsigned short *) (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 *buf, size_t count,
394 PRINTK_5(TRACE_MWAVE,
395 "mwavedd::mwave_read entry file %p, buf %p, count %x ppos %p\n",
396 file, buf, count, ppos);
402 static ssize_t mwave_write(struct file *file, const char *buf,
403 size_t count, loff_t * ppos)
405 PRINTK_5(TRACE_MWAVE,
406 "mwavedd::mwave_write entry file %p, buf %p,"
407 " count %x ppos %p\n",
408 file, buf, count, ppos);
414 static int register_serial_portandirq(unsigned int port, int irq)
416 struct serial_struct serial;
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(&serial, 0, sizeof(serial));
451 serial.flags = ASYNC_SHARE_IRQ;
453 return register_serial(&serial);
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 };
470 * sysfs support <paulsch@us.ibm.com>
473 struct device mwave_device;
475 /* Prevent code redundancy, create a macro for mwave_show_* functions. */
476 #define mwave_show_function(attr_name, format_string, field) \
477 static ssize_t mwave_show_##attr_name(struct device *dev, char *buf) \
479 DSP_3780I_CONFIG_SETTINGS *pSettings = \
480 &mwave_s_mdd.rBDData.rDspSettings; \
481 return sprintf(buf, format_string, pSettings->field); \
484 /* All of our attributes are read attributes. */
485 #define mwave_dev_rd_attr(attr_name, format_string, field) \
486 mwave_show_function(attr_name, format_string, field) \
487 static DEVICE_ATTR(attr_name, S_IRUGO, mwave_show_##attr_name, NULL)
489 mwave_dev_rd_attr (3780i_dma, "%i\n", usDspDma);
490 mwave_dev_rd_attr (3780i_irq, "%i\n", usDspIrq);
491 mwave_dev_rd_attr (3780i_io, "%#.4x\n", usDspBaseIO);
492 mwave_dev_rd_attr (uart_irq, "%i\n", usUartIrq);
493 mwave_dev_rd_attr (uart_io, "%#.4x\n", usUartBaseIO);
495 static struct device_attribute * const mwave_dev_attrs[] = {
504 * mwave_init is called on module load
506 * mwave_exit is called on module unload
507 * mwave_exit is also used to clean up after an aborted mwave_init
509 static void mwave_exit(void)
512 pMWAVE_DEVICE_DATA pDrvData = &mwave_s_mdd;
514 PRINTK_1(TRACE_MWAVE, "mwavedd::mwave_exit entry\n");
516 for (i = 0; i < pDrvData->nr_registered_attrs; i++)
517 device_remove_file(&mwave_device, mwave_dev_attrs[i]);
518 pDrvData->nr_registered_attrs = 0;
520 if (pDrvData->device_registered) {
521 device_unregister(&mwave_device);
522 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 */
642 memset(&mwave_device, 0, sizeof (struct device));
643 snprintf(mwave_device.bus_id, BUS_ID_SIZE, "mwave");
645 if (device_register(&mwave_device))
647 pDrvData->device_registered = TRUE;
648 for (i = 0; i < ARRAY_SIZE(mwave_dev_attrs); i++) {
649 if(device_create_file(&mwave_device, mwave_dev_attrs[i])) {
650 PRINTK_ERROR(KERN_ERR_MWAVE
651 "mwavedd:mwave_init: Error:"
652 " Failed to create sysfs file %s\n",
653 mwave_dev_attrs[i]->attr.name);
656 pDrvData->nr_registered_attrs++;
663 PRINTK_ERROR(KERN_ERR_MWAVE
664 "mwavedd::mwave_init: Error:"
665 " Failed to initialize\n");
666 mwave_exit(); /* clean up */
671 module_init(mwave_init);