manage.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  1. /*
  2. * linux/kernel/irq/manage.c
  3. *
  4. * Copyright (C) 1992, 1998-2006 Linus Torvalds, Ingo Molnar
  5. * Copyright (C) 2005-2006 Thomas Gleixner
  6. *
  7. * This file contains driver APIs to the irq subsystem.
  8. */
  9. #include <linux/irq.h>
  10. #include <linux/module.h>
  11. #include <linux/random.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/slab.h>
  14. #include "internals.h"
  15. #if defined(CONFIG_SMP) && defined(CONFIG_GENERIC_HARDIRQS)
  16. cpumask_var_t irq_default_affinity;
  17. /**
  18. * synchronize_irq - wait for pending IRQ handlers (on other CPUs)
  19. * @irq: interrupt number to wait for
  20. *
  21. * This function waits for any pending IRQ handlers for this interrupt
  22. * to complete before returning. If you use this function while
  23. * holding a resource the IRQ handler may need you will deadlock.
  24. *
  25. * This function may be called - with care - from IRQ context.
  26. */
  27. void synchronize_irq(unsigned int irq)
  28. {
  29. struct irq_desc *desc = irq_to_desc(irq);
  30. unsigned int status;
  31. if (!desc)
  32. return;
  33. do {
  34. unsigned long flags;
  35. /*
  36. * Wait until we're out of the critical section. This might
  37. * give the wrong answer due to the lack of memory barriers.
  38. */
  39. while (desc->status & IRQ_INPROGRESS)
  40. cpu_relax();
  41. /* Ok, that indicated we're done: double-check carefully. */
  42. spin_lock_irqsave(&desc->lock, flags);
  43. status = desc->status;
  44. spin_unlock_irqrestore(&desc->lock, flags);
  45. /* Oops, that failed? */
  46. } while (status & IRQ_INPROGRESS);
  47. }
  48. EXPORT_SYMBOL(synchronize_irq);
  49. /**
  50. * irq_can_set_affinity - Check if the affinity of a given irq can be set
  51. * @irq: Interrupt to check
  52. *
  53. */
  54. int irq_can_set_affinity(unsigned int irq)
  55. {
  56. struct irq_desc *desc = irq_to_desc(irq);
  57. if (CHECK_IRQ_PER_CPU(desc->status) || !desc->chip ||
  58. !desc->chip->set_affinity)
  59. return 0;
  60. return 1;
  61. }
  62. /**
  63. * irq_set_affinity - Set the irq affinity of a given irq
  64. * @irq: Interrupt to set affinity
  65. * @cpumask: cpumask
  66. *
  67. */
  68. int irq_set_affinity(unsigned int irq, const struct cpumask *cpumask)
  69. {
  70. struct irq_desc *desc = irq_to_desc(irq);
  71. unsigned long flags;
  72. if (!desc->chip->set_affinity)
  73. return -EINVAL;
  74. spin_lock_irqsave(&desc->lock, flags);
  75. #ifdef CONFIG_GENERIC_PENDING_IRQ
  76. if (desc->status & IRQ_MOVE_PCNTXT || desc->status & IRQ_DISABLED) {
  77. cpumask_copy(desc->affinity, cpumask);
  78. desc->chip->set_affinity(irq, cpumask);
  79. } else {
  80. desc->status |= IRQ_MOVE_PENDING;
  81. cpumask_copy(desc->pending_mask, cpumask);
  82. }
  83. #else
  84. cpumask_copy(desc->affinity, cpumask);
  85. desc->chip->set_affinity(irq, cpumask);
  86. #endif
  87. desc->status |= IRQ_AFFINITY_SET;
  88. spin_unlock_irqrestore(&desc->lock, flags);
  89. return 0;
  90. }
  91. #ifndef CONFIG_AUTO_IRQ_AFFINITY
  92. /*
  93. * Generic version of the affinity autoselector.
  94. */
  95. static int setup_affinity(unsigned int irq, struct irq_desc *desc)
  96. {
  97. if (!irq_can_set_affinity(irq))
  98. return 0;
  99. /*
  100. * Preserve an userspace affinity setup, but make sure that
  101. * one of the targets is online.
  102. */
  103. if (desc->status & (IRQ_AFFINITY_SET | IRQ_NO_BALANCING)) {
  104. if (cpumask_any_and(desc->affinity, cpu_online_mask)
  105. < nr_cpu_ids)
  106. goto set_affinity;
  107. else
  108. desc->status &= ~IRQ_AFFINITY_SET;
  109. }
  110. cpumask_and(desc->affinity, cpu_online_mask, irq_default_affinity);
  111. set_affinity:
  112. desc->chip->set_affinity(irq, desc->affinity);
  113. return 0;
  114. }
  115. #else
  116. static inline int setup_affinity(unsigned int irq, struct irq_desc *d)
  117. {
  118. return irq_select_affinity(irq);
  119. }
  120. #endif
  121. /*
  122. * Called when affinity is set via /proc/irq
  123. */
  124. int irq_select_affinity_usr(unsigned int irq)
  125. {
  126. struct irq_desc *desc = irq_to_desc(irq);
  127. unsigned long flags;
  128. int ret;
  129. spin_lock_irqsave(&desc->lock, flags);
  130. ret = setup_affinity(irq, desc);
  131. spin_unlock_irqrestore(&desc->lock, flags);
  132. return ret;
  133. }
  134. #else
  135. static inline int setup_affinity(unsigned int irq, struct irq_desc *desc)
  136. {
  137. return 0;
  138. }
  139. #endif
  140. void __disable_irq(struct irq_desc *desc, unsigned int irq, bool suspend)
  141. {
  142. if (suspend) {
  143. if (!desc->action || (desc->action->flags & IRQF_TIMER))
  144. return;
  145. desc->status |= IRQ_SUSPENDED;
  146. }
  147. if (!desc->depth++) {
  148. desc->status |= IRQ_DISABLED;
  149. desc->chip->disable(irq);
  150. }
  151. }
  152. /**
  153. * disable_irq_nosync - disable an irq without waiting
  154. * @irq: Interrupt to disable
  155. *
  156. * Disable the selected interrupt line. Disables and Enables are
  157. * nested.
  158. * Unlike disable_irq(), this function does not ensure existing
  159. * instances of the IRQ handler have completed before returning.
  160. *
  161. * This function may be called from IRQ context.
  162. */
  163. void disable_irq_nosync(unsigned int irq)
  164. {
  165. struct irq_desc *desc = irq_to_desc(irq);
  166. unsigned long flags;
  167. if (!desc)
  168. return;
  169. spin_lock_irqsave(&desc->lock, flags);
  170. __disable_irq(desc, irq, false);
  171. spin_unlock_irqrestore(&desc->lock, flags);
  172. }
  173. EXPORT_SYMBOL(disable_irq_nosync);
  174. /**
  175. * disable_irq - disable an irq and wait for completion
  176. * @irq: Interrupt to disable
  177. *
  178. * Disable the selected interrupt line. Enables and Disables are
  179. * nested.
  180. * This function waits for any pending IRQ handlers for this interrupt
  181. * to complete before returning. If you use this function while
  182. * holding a resource the IRQ handler may need you will deadlock.
  183. *
  184. * This function may be called - with care - from IRQ context.
  185. */
  186. void disable_irq(unsigned int irq)
  187. {
  188. struct irq_desc *desc = irq_to_desc(irq);
  189. if (!desc)
  190. return;
  191. disable_irq_nosync(irq);
  192. if (desc->action)
  193. synchronize_irq(irq);
  194. }
  195. EXPORT_SYMBOL(disable_irq);
  196. void __enable_irq(struct irq_desc *desc, unsigned int irq, bool resume)
  197. {
  198. if (resume)
  199. desc->status &= ~IRQ_SUSPENDED;
  200. switch (desc->depth) {
  201. case 0:
  202. err_out:
  203. WARN(1, KERN_WARNING "Unbalanced enable for IRQ %d\n", irq);
  204. break;
  205. case 1: {
  206. unsigned int status = desc->status & ~IRQ_DISABLED;
  207. if (desc->status & IRQ_SUSPENDED)
  208. goto err_out;
  209. /* Prevent probing on this irq: */
  210. desc->status = status | IRQ_NOPROBE;
  211. check_irq_resend(desc, irq);
  212. /* fall-through */
  213. }
  214. default:
  215. desc->depth--;
  216. }
  217. }
  218. /**
  219. * enable_irq - enable handling of an irq
  220. * @irq: Interrupt to enable
  221. *
  222. * Undoes the effect of one call to disable_irq(). If this
  223. * matches the last disable, processing of interrupts on this
  224. * IRQ line is re-enabled.
  225. *
  226. * This function may be called from IRQ context.
  227. */
  228. void enable_irq(unsigned int irq)
  229. {
  230. struct irq_desc *desc = irq_to_desc(irq);
  231. unsigned long flags;
  232. if (!desc)
  233. return;
  234. spin_lock_irqsave(&desc->lock, flags);
  235. __enable_irq(desc, irq, false);
  236. spin_unlock_irqrestore(&desc->lock, flags);
  237. }
  238. EXPORT_SYMBOL(enable_irq);
  239. static int set_irq_wake_real(unsigned int irq, unsigned int on)
  240. {
  241. struct irq_desc *desc = irq_to_desc(irq);
  242. int ret = -ENXIO;
  243. if (desc->chip->set_wake)
  244. ret = desc->chip->set_wake(irq, on);
  245. return ret;
  246. }
  247. /**
  248. * set_irq_wake - control irq power management wakeup
  249. * @irq: interrupt to control
  250. * @on: enable/disable power management wakeup
  251. *
  252. * Enable/disable power management wakeup mode, which is
  253. * disabled by default. Enables and disables must match,
  254. * just as they match for non-wakeup mode support.
  255. *
  256. * Wakeup mode lets this IRQ wake the system from sleep
  257. * states like "suspend to RAM".
  258. */
  259. int set_irq_wake(unsigned int irq, unsigned int on)
  260. {
  261. struct irq_desc *desc = irq_to_desc(irq);
  262. unsigned long flags;
  263. int ret = 0;
  264. /* wakeup-capable irqs can be shared between drivers that
  265. * don't need to have the same sleep mode behaviors.
  266. */
  267. spin_lock_irqsave(&desc->lock, flags);
  268. if (on) {
  269. if (desc->wake_depth++ == 0) {
  270. ret = set_irq_wake_real(irq, on);
  271. if (ret)
  272. desc->wake_depth = 0;
  273. else
  274. desc->status |= IRQ_WAKEUP;
  275. }
  276. } else {
  277. if (desc->wake_depth == 0) {
  278. WARN(1, "Unbalanced IRQ %d wake disable\n", irq);
  279. } else if (--desc->wake_depth == 0) {
  280. ret = set_irq_wake_real(irq, on);
  281. if (ret)
  282. desc->wake_depth = 1;
  283. else
  284. desc->status &= ~IRQ_WAKEUP;
  285. }
  286. }
  287. spin_unlock_irqrestore(&desc->lock, flags);
  288. return ret;
  289. }
  290. EXPORT_SYMBOL(set_irq_wake);
  291. /*
  292. * Internal function that tells the architecture code whether a
  293. * particular irq has been exclusively allocated or is available
  294. * for driver use.
  295. */
  296. int can_request_irq(unsigned int irq, unsigned long irqflags)
  297. {
  298. struct irq_desc *desc = irq_to_desc(irq);
  299. struct irqaction *action;
  300. if (!desc)
  301. return 0;
  302. if (desc->status & IRQ_NOREQUEST)
  303. return 0;
  304. action = desc->action;
  305. if (action)
  306. if (irqflags & action->flags & IRQF_SHARED)
  307. action = NULL;
  308. return !action;
  309. }
  310. void compat_irq_chip_set_default_handler(struct irq_desc *desc)
  311. {
  312. /*
  313. * If the architecture still has not overriden
  314. * the flow handler then zap the default. This
  315. * should catch incorrect flow-type setting.
  316. */
  317. if (desc->handle_irq == &handle_bad_irq)
  318. desc->handle_irq = NULL;
  319. }
  320. int __irq_set_trigger(struct irq_desc *desc, unsigned int irq,
  321. unsigned long flags)
  322. {
  323. int ret;
  324. struct irq_chip *chip = desc->chip;
  325. if (!chip || !chip->set_type) {
  326. /*
  327. * IRQF_TRIGGER_* but the PIC does not support multiple
  328. * flow-types?
  329. */
  330. pr_debug("No set_type function for IRQ %d (%s)\n", irq,
  331. chip ? (chip->name ? : "unknown") : "unknown");
  332. return 0;
  333. }
  334. /* caller masked out all except trigger mode flags */
  335. ret = chip->set_type(irq, flags);
  336. if (ret)
  337. pr_err("setting trigger mode %d for irq %u failed (%pF)\n",
  338. (int)flags, irq, chip->set_type);
  339. else {
  340. if (flags & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
  341. flags |= IRQ_LEVEL;
  342. /* note that IRQF_TRIGGER_MASK == IRQ_TYPE_SENSE_MASK */
  343. desc->status &= ~(IRQ_LEVEL | IRQ_TYPE_SENSE_MASK);
  344. desc->status |= flags;
  345. }
  346. return ret;
  347. }
  348. /*
  349. * Internal function to register an irqaction - typically used to
  350. * allocate special interrupts that are part of the architecture.
  351. */
  352. static int
  353. __setup_irq(unsigned int irq, struct irq_desc *desc, struct irqaction *new)
  354. {
  355. struct irqaction *old, **old_ptr;
  356. const char *old_name = NULL;
  357. unsigned long flags;
  358. int shared = 0;
  359. int ret;
  360. if (!desc)
  361. return -EINVAL;
  362. if (desc->chip == &no_irq_chip)
  363. return -ENOSYS;
  364. /*
  365. * Some drivers like serial.c use request_irq() heavily,
  366. * so we have to be careful not to interfere with a
  367. * running system.
  368. */
  369. if (new->flags & IRQF_SAMPLE_RANDOM) {
  370. /*
  371. * This function might sleep, we want to call it first,
  372. * outside of the atomic block.
  373. * Yes, this might clear the entropy pool if the wrong
  374. * driver is attempted to be loaded, without actually
  375. * installing a new handler, but is this really a problem,
  376. * only the sysadmin is able to do this.
  377. */
  378. rand_initialize_irq(irq);
  379. }
  380. /*
  381. * The following block of code has to be executed atomically
  382. */
  383. spin_lock_irqsave(&desc->lock, flags);
  384. old_ptr = &desc->action;
  385. old = *old_ptr;
  386. if (old) {
  387. /*
  388. * Can't share interrupts unless both agree to and are
  389. * the same type (level, edge, polarity). So both flag
  390. * fields must have IRQF_SHARED set and the bits which
  391. * set the trigger type must match.
  392. */
  393. if (!((old->flags & new->flags) & IRQF_SHARED) ||
  394. ((old->flags ^ new->flags) & IRQF_TRIGGER_MASK)) {
  395. old_name = old->name;
  396. goto mismatch;
  397. }
  398. #if defined(CONFIG_IRQ_PER_CPU)
  399. /* All handlers must agree on per-cpuness */
  400. if ((old->flags & IRQF_PERCPU) !=
  401. (new->flags & IRQF_PERCPU))
  402. goto mismatch;
  403. #endif
  404. /* add new interrupt at end of irq queue */
  405. do {
  406. old_ptr = &old->next;
  407. old = *old_ptr;
  408. } while (old);
  409. shared = 1;
  410. }
  411. if (!shared) {
  412. irq_chip_set_defaults(desc->chip);
  413. /* Setup the type (level, edge polarity) if configured: */
  414. if (new->flags & IRQF_TRIGGER_MASK) {
  415. ret = __irq_set_trigger(desc, irq,
  416. new->flags & IRQF_TRIGGER_MASK);
  417. if (ret) {
  418. spin_unlock_irqrestore(&desc->lock, flags);
  419. return ret;
  420. }
  421. } else
  422. compat_irq_chip_set_default_handler(desc);
  423. #if defined(CONFIG_IRQ_PER_CPU)
  424. if (new->flags & IRQF_PERCPU)
  425. desc->status |= IRQ_PER_CPU;
  426. #endif
  427. desc->status &= ~(IRQ_AUTODETECT | IRQ_WAITING |
  428. IRQ_INPROGRESS | IRQ_SPURIOUS_DISABLED);
  429. if (!(desc->status & IRQ_NOAUTOEN)) {
  430. desc->depth = 0;
  431. desc->status &= ~IRQ_DISABLED;
  432. desc->chip->startup(irq);
  433. } else
  434. /* Undo nested disables: */
  435. desc->depth = 1;
  436. /* Exclude IRQ from balancing if requested */
  437. if (new->flags & IRQF_NOBALANCING)
  438. desc->status |= IRQ_NO_BALANCING;
  439. /* Set default affinity mask once everything is setup */
  440. setup_affinity(irq, desc);
  441. } else if ((new->flags & IRQF_TRIGGER_MASK)
  442. && (new->flags & IRQF_TRIGGER_MASK)
  443. != (desc->status & IRQ_TYPE_SENSE_MASK)) {
  444. /* hope the handler works with the actual trigger mode... */
  445. pr_warning("IRQ %d uses trigger mode %d; requested %d\n",
  446. irq, (int)(desc->status & IRQ_TYPE_SENSE_MASK),
  447. (int)(new->flags & IRQF_TRIGGER_MASK));
  448. }
  449. *old_ptr = new;
  450. /* Reset broken irq detection when installing new handler */
  451. desc->irq_count = 0;
  452. desc->irqs_unhandled = 0;
  453. /*
  454. * Check whether we disabled the irq via the spurious handler
  455. * before. Reenable it and give it another chance.
  456. */
  457. if (shared && (desc->status & IRQ_SPURIOUS_DISABLED)) {
  458. desc->status &= ~IRQ_SPURIOUS_DISABLED;
  459. __enable_irq(desc, irq, false);
  460. }
  461. spin_unlock_irqrestore(&desc->lock, flags);
  462. new->irq = irq;
  463. register_irq_proc(irq, desc);
  464. new->dir = NULL;
  465. register_handler_proc(irq, new);
  466. return 0;
  467. mismatch:
  468. #ifdef CONFIG_DEBUG_SHIRQ
  469. if (!(new->flags & IRQF_PROBE_SHARED)) {
  470. printk(KERN_ERR "IRQ handler type mismatch for IRQ %d\n", irq);
  471. if (old_name)
  472. printk(KERN_ERR "current handler: %s\n", old_name);
  473. dump_stack();
  474. }
  475. #endif
  476. spin_unlock_irqrestore(&desc->lock, flags);
  477. return -EBUSY;
  478. }
  479. /**
  480. * setup_irq - setup an interrupt
  481. * @irq: Interrupt line to setup
  482. * @act: irqaction for the interrupt
  483. *
  484. * Used to statically setup interrupts in the early boot process.
  485. */
  486. int setup_irq(unsigned int irq, struct irqaction *act)
  487. {
  488. struct irq_desc *desc = irq_to_desc(irq);
  489. return __setup_irq(irq, desc, act);
  490. }
  491. EXPORT_SYMBOL_GPL(setup_irq);
  492. /*
  493. * Internal function to unregister an irqaction - used to free
  494. * regular and special interrupts that are part of the architecture.
  495. */
  496. static struct irqaction *__free_irq(unsigned int irq, void *dev_id)
  497. {
  498. struct irq_desc *desc = irq_to_desc(irq);
  499. struct irqaction *action, **action_ptr;
  500. unsigned long flags;
  501. WARN(in_interrupt(), "Trying to free IRQ %d from IRQ context!\n", irq);
  502. if (!desc)
  503. return NULL;
  504. spin_lock_irqsave(&desc->lock, flags);
  505. /*
  506. * There can be multiple actions per IRQ descriptor, find the right
  507. * one based on the dev_id:
  508. */
  509. action_ptr = &desc->action;
  510. for (;;) {
  511. action = *action_ptr;
  512. if (!action) {
  513. WARN(1, "Trying to free already-free IRQ %d\n", irq);
  514. spin_unlock_irqrestore(&desc->lock, flags);
  515. return NULL;
  516. }
  517. if (action->dev_id == dev_id)
  518. break;
  519. action_ptr = &action->next;
  520. }
  521. /* Found it - now remove it from the list of entries: */
  522. *action_ptr = action->next;
  523. /* Currently used only by UML, might disappear one day: */
  524. #ifdef CONFIG_IRQ_RELEASE_METHOD
  525. if (desc->chip->release)
  526. desc->chip->release(irq, dev_id);
  527. #endif
  528. /* If this was the last handler, shut down the IRQ line: */
  529. if (!desc->action) {
  530. desc->status |= IRQ_DISABLED;
  531. if (desc->chip->shutdown)
  532. desc->chip->shutdown(irq);
  533. else
  534. desc->chip->disable(irq);
  535. }
  536. spin_unlock_irqrestore(&desc->lock, flags);
  537. unregister_handler_proc(irq, action);
  538. /* Make sure it's not being used on another CPU: */
  539. synchronize_irq(irq);
  540. #ifdef CONFIG_DEBUG_SHIRQ
  541. /*
  542. * It's a shared IRQ -- the driver ought to be prepared for an IRQ
  543. * event to happen even now it's being freed, so let's make sure that
  544. * is so by doing an extra call to the handler ....
  545. *
  546. * ( We do this after actually deregistering it, to make sure that a
  547. * 'real' IRQ doesn't run in * parallel with our fake. )
  548. */
  549. if (action->flags & IRQF_SHARED) {
  550. local_irq_save(flags);
  551. action->handler(irq, dev_id);
  552. local_irq_restore(flags);
  553. }
  554. #endif
  555. return action;
  556. }
  557. /**
  558. * remove_irq - free an interrupt
  559. * @irq: Interrupt line to free
  560. * @act: irqaction for the interrupt
  561. *
  562. * Used to remove interrupts statically setup by the early boot process.
  563. */
  564. void remove_irq(unsigned int irq, struct irqaction *act)
  565. {
  566. __free_irq(irq, act->dev_id);
  567. }
  568. EXPORT_SYMBOL_GPL(remove_irq);
  569. /**
  570. * free_irq - free an interrupt allocated with request_irq
  571. * @irq: Interrupt line to free
  572. * @dev_id: Device identity to free
  573. *
  574. * Remove an interrupt handler. The handler is removed and if the
  575. * interrupt line is no longer in use by any driver it is disabled.
  576. * On a shared IRQ the caller must ensure the interrupt is disabled
  577. * on the card it drives before calling this function. The function
  578. * does not return until any executing interrupts for this IRQ
  579. * have completed.
  580. *
  581. * This function must not be called from interrupt context.
  582. */
  583. void free_irq(unsigned int irq, void *dev_id)
  584. {
  585. kfree(__free_irq(irq, dev_id));
  586. }
  587. EXPORT_SYMBOL(free_irq);
  588. /**
  589. * request_irq - allocate an interrupt line
  590. * @irq: Interrupt line to allocate
  591. * @handler: Function to be called when the IRQ occurs
  592. * @irqflags: Interrupt type flags
  593. * @devname: An ascii name for the claiming device
  594. * @dev_id: A cookie passed back to the handler function
  595. *
  596. * This call allocates interrupt resources and enables the
  597. * interrupt line and IRQ handling. From the point this
  598. * call is made your handler function may be invoked. Since
  599. * your handler function must clear any interrupt the board
  600. * raises, you must take care both to initialise your hardware
  601. * and to set up the interrupt handler in the right order.
  602. *
  603. * Dev_id must be globally unique. Normally the address of the
  604. * device data structure is used as the cookie. Since the handler
  605. * receives this value it makes sense to use it.
  606. *
  607. * If your interrupt is shared you must pass a non NULL dev_id
  608. * as this is required when freeing the interrupt.
  609. *
  610. * Flags:
  611. *
  612. * IRQF_SHARED Interrupt is shared
  613. * IRQF_DISABLED Disable local interrupts while processing
  614. * IRQF_SAMPLE_RANDOM The interrupt can be used for entropy
  615. * IRQF_TRIGGER_* Specify active edge(s) or level
  616. *
  617. */
  618. int request_irq(unsigned int irq, irq_handler_t handler,
  619. unsigned long irqflags, const char *devname, void *dev_id)
  620. {
  621. struct irqaction *action;
  622. struct irq_desc *desc;
  623. int retval;
  624. /*
  625. * handle_IRQ_event() always ignores IRQF_DISABLED except for
  626. * the _first_ irqaction (sigh). That can cause oopsing, but
  627. * the behavior is classified as "will not fix" so we need to
  628. * start nudging drivers away from using that idiom.
  629. */
  630. if ((irqflags & (IRQF_SHARED|IRQF_DISABLED)) ==
  631. (IRQF_SHARED|IRQF_DISABLED)) {
  632. pr_warning(
  633. "IRQ %d/%s: IRQF_DISABLED is not guaranteed on shared IRQs\n",
  634. irq, devname);
  635. }
  636. #ifdef CONFIG_LOCKDEP
  637. /*
  638. * Lockdep wants atomic interrupt handlers:
  639. */
  640. irqflags |= IRQF_DISABLED;
  641. #endif
  642. /*
  643. * Sanity-check: shared interrupts must pass in a real dev-ID,
  644. * otherwise we'll have trouble later trying to figure out
  645. * which interrupt is which (messes up the interrupt freeing
  646. * logic etc).
  647. */
  648. if ((irqflags & IRQF_SHARED) && !dev_id)
  649. return -EINVAL;
  650. desc = irq_to_desc(irq);
  651. if (!desc)
  652. return -EINVAL;
  653. if (desc->status & IRQ_NOREQUEST)
  654. return -EINVAL;
  655. if (!handler)
  656. return -EINVAL;
  657. action = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
  658. if (!action)
  659. return -ENOMEM;
  660. action->handler = handler;
  661. action->flags = irqflags;
  662. action->name = devname;
  663. action->dev_id = dev_id;
  664. retval = __setup_irq(irq, desc, action);
  665. if (retval)
  666. kfree(action);
  667. #ifdef CONFIG_DEBUG_SHIRQ
  668. if (irqflags & IRQF_SHARED) {
  669. /*
  670. * It's a shared IRQ -- the driver ought to be prepared for it
  671. * to happen immediately, so let's make sure....
  672. * We disable the irq to make sure that a 'real' IRQ doesn't
  673. * run in parallel with our fake.
  674. */
  675. unsigned long flags;
  676. disable_irq(irq);
  677. local_irq_save(flags);
  678. handler(irq, dev_id);
  679. local_irq_restore(flags);
  680. enable_irq(irq);
  681. }
  682. #endif
  683. return retval;
  684. }
  685. EXPORT_SYMBOL(request_irq);