Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | /* * Device driver for the /dev/adb device on macintoshes. * * Copyright (C) 1996 Paul Mackerras. */ #include <linux/types.h> #include <linux/errno.h> #include <linux/kernel.h> #include <linux/malloc.h> #include <linux/fs.h> #include <linux/mm.h> #include <asm/prom.h> #include <asm/cuda.h> #include <asm/uaccess.h> #define ADB_MAJOR 56 /* major number for /dev/adb */ extern void adbdev_init(void); struct adbdev_state { struct cuda_request req; }; static struct wait_queue *adb_wait; static int adb_wait_reply(struct adbdev_state *state, struct file *file) { int ret = 0; struct wait_queue wait = { current, NULL }; add_wait_queue(&adb_wait, &wait); current->state = TASK_INTERRUPTIBLE; while (!state->req.got_reply) { if (file->f_flags & O_NONBLOCK) { ret = -EAGAIN; break; } if (signal_pending(current)) { ret = -ERESTARTSYS; break; } schedule(); } current->state = TASK_RUNNING; remove_wait_queue(&adb_wait, &wait); return ret; } static void adb_write_done(struct cuda_request *req) { if (!req->got_reply) { req->reply_len = 0; req->got_reply = 1; } wake_up_interruptible(&adb_wait); } static int adb_open(struct inode *inode, struct file *file) { struct adbdev_state *state; if (MINOR(inode->i_rdev) > 0) return -ENXIO; state = kmalloc(sizeof(struct adbdev_state), GFP_KERNEL); if (state == 0) return -ENOMEM; file->private_data = state; state->req.reply_expected = 0; return 0; } static int adb_release(struct inode *inode, struct file *file) { struct adbdev_state *state = file->private_data; if (state) { file->private_data = NULL; if (state->req.reply_expected && !state->req.got_reply) if (adb_wait_reply(state, file)) return 0; kfree(state); } return 0; } static long long adb_lseek(struct file *file, long long offset, int origin) { return -ESPIPE; } static long adb_read(struct inode *inode, struct file *file, char *buf, unsigned long count) { int ret; struct adbdev_state *state = file->private_data; if (count < 2) return -EINVAL; if (count > sizeof(state->req.reply)) count = sizeof(state->req.reply); ret = verify_area(VERIFY_WRITE, buf, count); if (ret) return ret; if (!state->req.reply_expected) return 0; ret = adb_wait_reply(state, file); if (ret) return ret; ret = state->req.reply_len; copy_to_user(buf, state->req.reply, ret); state->req.reply_expected = 0; return ret; } static long adb_write(struct inode *inode, struct file *file, const char *buf, unsigned long count) { int ret; struct adbdev_state *state = file->private_data; if (count < 2 || count > sizeof(state->req.data)) return -EINVAL; ret = verify_area(VERIFY_READ, buf, count); if (ret) return ret; if (state->req.reply_expected && !state->req.got_reply) { /* A previous request is still being processed. Wait for it to finish. */ ret = adb_wait_reply(state, file); if (ret) return ret; } state->req.nbytes = count; state->req.done = adb_write_done; copy_from_user(state->req.data, buf, count); state->req.reply_expected = 1; state->req.got_reply = 0; cuda_send_request(&state->req); return count; } static struct file_operations adb_fops = { adb_lseek, adb_read, adb_write, NULL, /* no readdir */ NULL, /* no poll yet */ NULL, /* no ioctl yet */ NULL, /* no mmap */ adb_open, adb_release }; void adbdev_init() { if (register_chrdev(ADB_MAJOR, "adb", &adb_fops)) printk(KERN_ERR "adb: unable to get major %d\n", ADB_MAJOR); } |