| 1 |
/* |
| 2 |
* Written by Oron Peled <[email protected]> |
| 3 |
* Copyright (C) 2007, Xorcom |
| 4 |
* |
| 5 |
* All rights reserved. |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation; either version 2 of the License, or |
| 10 |
* (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this program; if not, write to the Free Software |
| 19 |
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 20 |
* |
| 21 |
*/ |
| 22 |
#include <linux/version.h> |
| 23 |
#include <linux/kernel.h> |
| 24 |
#include <linux/module.h> |
| 25 |
#include <linux/parport.h> |
| 26 |
#include "parport_debug.h" |
| 27 |
|
| 28 |
static struct parport *debug_sync_parport; |
| 29 |
static int parport_toggles[8]; /* 8 bit flip-flop */ |
| 30 |
|
| 31 |
void flip_parport_bit(unsigned char bitnum) |
| 32 |
{ |
| 33 |
static unsigned char last_value; |
| 34 |
DEFINE_SPINLOCK(lock); |
| 35 |
unsigned long flags; |
| 36 |
unsigned char mask; |
| 37 |
unsigned char value; |
| 38 |
|
| 39 |
if (!debug_sync_parport) { |
| 40 |
if (printk_ratelimit()) { |
| 41 |
printk(KERN_NOTICE "%s: no debug parallel port\n", |
| 42 |
THIS_MODULE->name); |
| 43 |
} |
| 44 |
return; |
| 45 |
} |
| 46 |
BUG_ON(bitnum > 7); |
| 47 |
mask = 1 << bitnum; |
| 48 |
spin_lock_irqsave(&lock, flags); |
| 49 |
value = last_value & ~mask; |
| 50 |
if (parport_toggles[bitnum] % 2) /* square wave */ |
| 51 |
value |= mask; |
| 52 |
last_value = value; |
| 53 |
parport_toggles[bitnum]++; |
| 54 |
spin_unlock_irqrestore(&lock, flags); |
| 55 |
parport_write_data(debug_sync_parport, value); |
| 56 |
} |
| 57 |
EXPORT_SYMBOL(flip_parport_bit); |
| 58 |
|
| 59 |
static void parport_attach(struct parport *port) |
| 60 |
{ |
| 61 |
printk(KERN_INFO "%s: Using %s for debugging\n", THIS_MODULE->name, |
| 62 |
port->name); |
| 63 |
if (debug_sync_parport) { |
| 64 |
printk(KERN_ERR "%s: Using %s, ignore new attachment %s\n", |
| 65 |
THIS_MODULE->name, debug_sync_parport->name, port->name); |
| 66 |
return; |
| 67 |
} |
| 68 |
parport_get_port(port); |
| 69 |
debug_sync_parport = port; |
| 70 |
} |
| 71 |
|
| 72 |
static void parport_detach(struct parport *port) |
| 73 |
{ |
| 74 |
printk(KERN_INFO "%s: Releasing %s\n", THIS_MODULE->name, port->name); |
| 75 |
if (debug_sync_parport != port) { |
| 76 |
printk(KERN_ERR "%s: Using %s, ignore new detachment %s\n", |
| 77 |
THIS_MODULE->name, debug_sync_parport->name, port->name); |
| 78 |
return; |
| 79 |
} |
| 80 |
parport_put_port(debug_sync_parport); |
| 81 |
debug_sync_parport = NULL; |
| 82 |
} |
| 83 |
|
| 84 |
static struct parport_driver debug_parport_driver = { |
| 85 |
.name = "parport_debug", |
| 86 |
.attach = parport_attach, |
| 87 |
.detach = parport_detach, |
| 88 |
}; |
| 89 |
|
| 90 |
int __init parallel_dbg_init(void) |
| 91 |
{ |
| 92 |
int ret; |
| 93 |
|
| 94 |
ret = parport_register_driver(&debug_parport_driver); |
| 95 |
return ret; |
| 96 |
} |
| 97 |
|
| 98 |
void __exit parallel_dbg_cleanup(void) |
| 99 |
{ |
| 100 |
parport_unregister_driver(&debug_parport_driver); |
| 101 |
} |
| 102 |
|
| 103 |
MODULE_DESCRIPTION("Use parallel port to debug drivers"); |
| 104 |
MODULE_AUTHOR("Oron Peled <[email protected]>"); |
| 105 |
MODULE_LICENSE("GPL"); |
| 106 |
|
| 107 |
module_init(parallel_dbg_init); |
| 108 |
module_exit(parallel_dbg_cleanup); |