Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * /dev/lcd driver for Apple Network Servers. |
| 3 | */ |
| 4 | |
| 5 | #include <linux/types.h> |
| 6 | #include <linux/errno.h> |
| 7 | #include <linux/kernel.h> |
| 8 | #include <linux/miscdevice.h> |
| 9 | #include <linux/fcntl.h> |
| 10 | #include <linux/init.h> |
| 11 | #include <linux/delay.h> |
| 12 | #include <linux/fs.h> |
| 13 | |
| 14 | #include <asm/uaccess.h> |
| 15 | #include <asm/sections.h> |
| 16 | #include <asm/prom.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <asm/io.h> |
| 18 | |
Kumar Gala | 33d71d2 | 2007-08-20 08:50:28 -0500 | [diff] [blame] | 19 | #include "ans-lcd.h" |
| 20 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #define ANSLCD_ADDR 0xf301c000 |
| 22 | #define ANSLCD_CTRL_IX 0x00 |
| 23 | #define ANSLCD_DATA_IX 0x10 |
| 24 | |
| 25 | static unsigned long anslcd_short_delay = 80; |
| 26 | static unsigned long anslcd_long_delay = 3280; |
| 27 | static volatile unsigned char __iomem *anslcd_ptr; |
Thomas Gleixner | 95fdac7 | 2009-10-10 13:38:57 +0200 | [diff] [blame] | 28 | static DEFINE_MUTEX(anslcd_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
| 30 | #undef DEBUG |
| 31 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 32 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | anslcd_write_byte_ctrl ( unsigned char c ) |
| 34 | { |
| 35 | #ifdef DEBUG |
| 36 | printk(KERN_DEBUG "LCD: CTRL byte: %02x\n",c); |
| 37 | #endif |
| 38 | out_8(anslcd_ptr + ANSLCD_CTRL_IX, c); |
| 39 | switch(c) { |
| 40 | case 1: |
| 41 | case 2: |
| 42 | case 3: |
| 43 | udelay(anslcd_long_delay); break; |
| 44 | default: udelay(anslcd_short_delay); |
| 45 | } |
| 46 | } |
| 47 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 48 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | anslcd_write_byte_data ( unsigned char c ) |
| 50 | { |
| 51 | out_8(anslcd_ptr + ANSLCD_DATA_IX, c); |
| 52 | udelay(anslcd_short_delay); |
| 53 | } |
| 54 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 55 | static ssize_t |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | anslcd_write( struct file * file, const char __user * buf, |
| 57 | size_t count, loff_t *ppos ) |
| 58 | { |
| 59 | const char __user *p = buf; |
| 60 | int i; |
| 61 | |
| 62 | #ifdef DEBUG |
| 63 | printk(KERN_DEBUG "LCD: write\n"); |
| 64 | #endif |
| 65 | |
| 66 | if (!access_ok(VERIFY_READ, buf, count)) |
| 67 | return -EFAULT; |
Thomas Gleixner | 95fdac7 | 2009-10-10 13:38:57 +0200 | [diff] [blame] | 68 | |
| 69 | mutex_lock(&anslcd_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | for ( i = *ppos; count > 0; ++i, ++p, --count ) |
| 71 | { |
| 72 | char c; |
| 73 | __get_user(c, p); |
| 74 | anslcd_write_byte_data( c ); |
| 75 | } |
Thomas Gleixner | 95fdac7 | 2009-10-10 13:38:57 +0200 | [diff] [blame] | 76 | mutex_unlock(&anslcd_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | *ppos = i; |
| 78 | return p - buf; |
| 79 | } |
| 80 | |
Thomas Gleixner | 95fdac7 | 2009-10-10 13:38:57 +0200 | [diff] [blame] | 81 | static long |
| 82 | anslcd_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | { |
| 84 | char ch, __user *temp; |
Thomas Gleixner | 95fdac7 | 2009-10-10 13:38:57 +0200 | [diff] [blame] | 85 | long ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | |
| 87 | #ifdef DEBUG |
| 88 | printk(KERN_DEBUG "LCD: ioctl(%d,%d)\n",cmd,arg); |
| 89 | #endif |
| 90 | |
Thomas Gleixner | 95fdac7 | 2009-10-10 13:38:57 +0200 | [diff] [blame] | 91 | mutex_lock(&anslcd_mutex); |
| 92 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | switch ( cmd ) |
| 94 | { |
| 95 | case ANSLCD_CLEAR: |
| 96 | anslcd_write_byte_ctrl ( 0x38 ); |
| 97 | anslcd_write_byte_ctrl ( 0x0f ); |
| 98 | anslcd_write_byte_ctrl ( 0x06 ); |
| 99 | anslcd_write_byte_ctrl ( 0x01 ); |
| 100 | anslcd_write_byte_ctrl ( 0x02 ); |
Thomas Gleixner | 95fdac7 | 2009-10-10 13:38:57 +0200 | [diff] [blame] | 101 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | case ANSLCD_SENDCTRL: |
| 103 | temp = (char __user *) arg; |
| 104 | __get_user(ch, temp); |
| 105 | for (; ch; temp++) { /* FIXME: This is ugly, but should work, as a \0 byte is not a valid command code */ |
| 106 | anslcd_write_byte_ctrl ( ch ); |
| 107 | __get_user(ch, temp); |
| 108 | } |
Thomas Gleixner | 95fdac7 | 2009-10-10 13:38:57 +0200 | [diff] [blame] | 109 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | case ANSLCD_SETSHORTDELAY: |
| 111 | if (!capable(CAP_SYS_ADMIN)) |
Thomas Gleixner | 95fdac7 | 2009-10-10 13:38:57 +0200 | [diff] [blame] | 112 | ret =-EACCES; |
| 113 | else |
| 114 | anslcd_short_delay=arg; |
| 115 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | case ANSLCD_SETLONGDELAY: |
| 117 | if (!capable(CAP_SYS_ADMIN)) |
Thomas Gleixner | 95fdac7 | 2009-10-10 13:38:57 +0200 | [diff] [blame] | 118 | ret = -EACCES; |
| 119 | else |
| 120 | anslcd_long_delay=arg; |
| 121 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | default: |
Thomas Gleixner | 95fdac7 | 2009-10-10 13:38:57 +0200 | [diff] [blame] | 123 | ret = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | } |
Thomas Gleixner | 95fdac7 | 2009-10-10 13:38:57 +0200 | [diff] [blame] | 125 | |
| 126 | mutex_unlock(&anslcd_mutex); |
| 127 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Jon Loeliger | aacaf9b | 2005-09-17 10:36:54 -0500 | [diff] [blame] | 130 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | anslcd_open( struct inode * inode, struct file * file ) |
| 132 | { |
| 133 | return 0; |
| 134 | } |
| 135 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 136 | const struct file_operations anslcd_fops = { |
Thomas Gleixner | 95fdac7 | 2009-10-10 13:38:57 +0200 | [diff] [blame] | 137 | .write = anslcd_write, |
| 138 | .unlocked_ioctl = anslcd_ioctl, |
| 139 | .open = anslcd_open, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | }; |
| 141 | |
| 142 | static struct miscdevice anslcd_dev = { |
| 143 | ANSLCD_MINOR, |
| 144 | "anslcd", |
| 145 | &anslcd_fops |
| 146 | }; |
| 147 | |
| 148 | const char anslcd_logo[] = "********************" /* Line #1 */ |
| 149 | "* LINUX! *" /* Line #3 */ |
| 150 | "* Welcome to *" /* Line #2 */ |
| 151 | "********************"; /* Line #4 */ |
| 152 | |
| 153 | static int __init |
| 154 | anslcd_init(void) |
| 155 | { |
| 156 | int a; |
| 157 | int retval; |
| 158 | struct device_node* node; |
| 159 | |
Stephen Rothwell | 30686ba | 2007-04-24 13:53:04 +1000 | [diff] [blame] | 160 | node = of_find_node_by_name(NULL, "lcd"); |
| 161 | if (!node || !node->parent || strcmp(node->parent->name, "gc")) { |
| 162 | of_node_put(node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | return -ENODEV; |
Stephen Rothwell | 30686ba | 2007-04-24 13:53:04 +1000 | [diff] [blame] | 164 | } |
| 165 | of_node_put(node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | |
| 167 | anslcd_ptr = ioremap(ANSLCD_ADDR, 0x20); |
| 168 | |
| 169 | retval = misc_register(&anslcd_dev); |
| 170 | if(retval < 0){ |
| 171 | printk(KERN_INFO "LCD: misc_register failed\n"); |
| 172 | iounmap(anslcd_ptr); |
| 173 | return retval; |
| 174 | } |
| 175 | |
| 176 | #ifdef DEBUG |
| 177 | printk(KERN_DEBUG "LCD: init\n"); |
| 178 | #endif |
| 179 | |
Thomas Gleixner | 95fdac7 | 2009-10-10 13:38:57 +0200 | [diff] [blame] | 180 | mutex_lock(&anslcd_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | anslcd_write_byte_ctrl ( 0x38 ); |
| 182 | anslcd_write_byte_ctrl ( 0x0c ); |
| 183 | anslcd_write_byte_ctrl ( 0x06 ); |
| 184 | anslcd_write_byte_ctrl ( 0x01 ); |
| 185 | anslcd_write_byte_ctrl ( 0x02 ); |
| 186 | for(a=0;a<80;a++) { |
| 187 | anslcd_write_byte_data(anslcd_logo[a]); |
| 188 | } |
Thomas Gleixner | 95fdac7 | 2009-10-10 13:38:57 +0200 | [diff] [blame] | 189 | mutex_unlock(&anslcd_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | static void __exit |
| 194 | anslcd_exit(void) |
| 195 | { |
| 196 | misc_deregister(&anslcd_dev); |
| 197 | iounmap(anslcd_ptr); |
| 198 | } |
| 199 | |
| 200 | module_init(anslcd_init); |
| 201 | module_exit(anslcd_exit); |