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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
|
#include <linux/kernel.h> #include <linux/module.h> #include <linux/init.h> #include <linux/gpio.h> #include <linux/interrupt.h>
#include <asm/siginfo.h> #include <linux/rcupdate.h> #include <linux/sched.h> #include <linux/debugfs.h> #include <linux/uaccess.h>
#ifdef DEBUG_OUT #define debug(fmt,args...) printk (KERN_INFO fmt ,##args) #else #define debug(fmt,args...) #endif
#define SIG_GPIO_IRQ 42
#define DRV_NAME "GPIO IRQ handler" #define FILE_NAME "gpio-irq"
#define MAX_PROCESSES 10
typedef struct { int gpio; int irq; int last_value;
pid_t processes[MAX_PROCESSES]; } _gpio_handler;
#define TOTAL_GPIO 50 static _gpio_handler all_handlers[TOTAL_GPIO]; static struct dentry* in_file;
static int is_space(char symbol) { return (symbol == ' ') || (symbol == '\t'); }
static int is_digit(char symbol) { return (symbol >= '0') && (symbol <= '9'); }
static int is_eol(char symbol) { return (symbol == '\n') || (symbol == '\r'); }
static irqreturn_t gpio_edge_interrupt(int irq, void* dev_id) { _gpio_handler* handler=(_gpio_handler*)dev_id;
if(handler && (handler->irq == irq)) { int val=0;
debug("Got _handler!\n");
val = gpio_get_value(handler->gpio)!=0;
if(val != handler->last_value) { struct siginfo info; struct task_struct* ts=NULL;
int i=0;
handler->last_value=val; debug("IRQ %d event (GPIO %d) - new value is %d!\n", irq, handler->gpio, val);
memset(&info, 0, sizeof(struct siginfo)); info.si_signo = SIG_GPIO_IRQ; info.si_code = SI_QUEUE;
for(i=0; i < MAX_PROCESSES; ++i) { pid_t pid=handler->processes[i]; if(pid == 0) break; info.si_int=(handler->gpio << 24) | (val & 1); rcu_read_lock(); ts=pid_task(find_vpid(pid), PIDTYPE_PID); rcu_read_unlock(); if(ts == NULL) { debug("PID %u is not found, remove it please.\n",pid); } else { debug("Sending signal to PID %u.\n",pid); send_sig_info(SIG_GPIO_IRQ, &info, ts); } } } } else { debug("IRQ %d event - no handlers found!\n",irq); }
return(IRQ_HANDLED); }
static int add_irq(int gpio,void* data) { if(gpio_request(gpio, DRV_NAME) >= 0) { int irq_number=gpio_to_irq(gpio);
if(irq_number >= 0) { int err = request_irq(irq_number, gpio_edge_interrupt, IRQ_TYPE_EDGE_BOTH, "gpio_irq_handler", data);
if(!err) { debug("Got IRQ %d for GPIO %d\n", irq_number, gpio); return irq_number; } else { debug("GPIO IRQ handler: trouble requesting IRQ %d error %d\n",irq_number, err); } } else { debug("Can't map GPIO %d to IRQ : error %d\n",gpio, irq_number); } } else { debug("Can't get GPIO %d\n", gpio); }
return -1; }
static void free_handler(int gpio) { if((gpio >= 0) && (gpio < TOTAL_GPIO)) { _gpio_handler* handler=&all_handlers[gpio];
if(handler->gpio == gpio) { int i=0;
if(handler->irq >= 0) { free_irq(handler->irq, (void*)handler); handler->irq=-1; }
gpio_free(gpio); handler->gpio=-1;
for(; i < MAX_PROCESSES; ++i) { handler->processes[i]=0; } } } }
static void remove_handler(int gpio,pid_t pid) { if((gpio >= 0) && (gpio < TOTAL_GPIO)) { _gpio_handler* handler=&all_handlers[gpio];
if(handler->gpio == gpio) { int i=0; for(i=0; i < MAX_PROCESSES; ++i) { if(handler->processes[i] == pid) { for(++i; i < MAX_PROCESSES; ++i) { handler->processes[i-1]=handler->processes[i]; } handler->processes[i-1]=0; return; } }
debug("Handler for GPIO %d to PID %u is not found.\n", gpio, pid); } } }
static int add_handler(int gpio, pid_t pid) { if((gpio >= 0) && (gpio < TOTAL_GPIO)) { _gpio_handler* handler=&all_handlers[gpio]; int p=0;
if(handler->gpio != gpio) { int irq=add_irq(gpio, handler);
if(irq < 0) { free_handler(gpio); return -1; }
handler->gpio=gpio; handler->irq=irq; handler->last_value=-1; }
while((handler->processes[p] > 0) && (handler->processes[p] != pid)) ++p;
if(p < MAX_PROCESSES) { handler->processes[p]=pid; return handler->irq; } else { debug("Can't add handler: %d processes already handle GPIO %d.\n", p, gpio); } }
return -1; }
static ssize_t run_command(struct file *file, const char __user *buf, size_t count, loff_t *ppos) { char buffer[512]; char line[20]; char* in_pos=NULL; char* end=NULL; char* out_pos=NULL;
int add=0; int gpio=-1; int pid=0;
if(count > 512) return -EINVAL;
copy_from_user(buffer, buf, count); buffer[count]=0;
debug("Command is found (%u bytes length):\n%s\n",count,buffer);
in_pos=buffer; end=in_pos+count-1;
while(in_pos < end) { add=0; gpio=-1; pid=0;
while((in_pos < end) && is_space(*in_pos)) ++in_pos; if(in_pos >= end) break;
if(*in_pos == '+') { add=1; } else if(*in_pos == '-') { add=0; } else if(*in_pos == '?') { int i,j;
for(i=0; i < TOTAL_GPIO; ++i) { if(all_handlers[i].gpio != -1) { printk(KERN_INFO "GPIO %d (IRQ %d): ",all_handlers[i].gpio,all_handlers[i].irq);
for(j=0; j < MAX_PROCESSES; ++j) { if(all_handlers[i].processes[j] != 0) { printk("%u ",all_handlers[i].processes[j]); } else { break; } }
printk("\n"); } }
return count; } else { printk(KERN_INFO "Wrong command '%c'.\n", *in_pos); break; } ++in_pos; while((in_pos < end) && is_space(*in_pos)) ++in_pos;
out_pos=line; while((in_pos < end) && is_digit(*in_pos)) *out_pos++=*in_pos++; *out_pos=0;
if(is_digit(line[0])) { sscanf(line, "%d", &gpio); } else { printk(KERN_INFO "Can't read GPIO number.\n"); break; }
while((in_pos < end) && is_space(*in_pos)) ++in_pos;
out_pos=line; while((in_pos < end) && is_digit(*in_pos)) *out_pos++=*in_pos++; *out_pos=0;
if(is_digit(line[0])) { sscanf(line, "%u", &pid); } else { if(add) { printk(KERN_INFO "Can't read PID.\n"); break; } }
if(add) { debug("Trying to add handler for GPIO %d to PID %u.\n",gpio,pid); add_handler(gpio,pid); } else { if(pid) { _gpio_handler* handler=&all_handlers[gpio];
debug("Trying to remove handler for GPIO %d to PID %u.\n",gpio,pid); remove_handler(gpio,pid);
if(handler->processes[0] == 0) { debug("It was the last handler. Let's free IRQ %d.\n",handler->irq); free_handler(gpio); } } else { debug("Trying to remove all handlers for GPIO %d.\n",gpio); free_handler(gpio); } }
while((in_pos < end) && (is_space(*in_pos) || is_eol(*in_pos))) ++in_pos; }
return count; }
static const struct file_operations irq_fops = {
.write = run_command, };
static int __init mymodule_init(void) { int i=0,j=0;
for(i=0; i < TOTAL_GPIO; ++i) { all_handlers[i].gpio=-1; all_handlers[i].irq=-1; all_handlers[i].gpio=-1;
for(j=0; j < MAX_PROCESSES; ++j) { all_handlers[i].processes[j]=0; } } in_file=debugfs_create_file(FILE_NAME, 0666, NULL, NULL, &irq_fops); debug("Waiting for commands in file /sys/kernel/debug/" FILE_NAME ".\n"); return 0; }
static void __exit mymodule_exit(void) { int i=0;
for(; i < TOTAL_GPIO; ++i) { free_handler(i); }
debugfs_remove(in_file);
return; }
module_init(mymodule_init); module_exit(mymodule_exit);
MODULE_LICENSE("GPL"); MODULE_AUTHOR("Dmitriy Zherebkov (Black Swift team)");
|