ctimap.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /**
  2. * Copyright (C) 2008, Creative Technology Ltd. All Rights Reserved.
  3. *
  4. * This source file is released under GPL v2 license (no other versions).
  5. * See the COPYING file included in the main directory of this source
  6. * distribution for the license terms and conditions.
  7. *
  8. * @File ctimap.c
  9. *
  10. * @Brief
  11. * This file contains the implementation of generic input mapper operations
  12. * for input mapper management.
  13. *
  14. * @Author Liu Chun
  15. * @Date May 23 2008
  16. *
  17. */
  18. #include "ctimap.h"
  19. #include <linux/slab.h>
  20. int input_mapper_add(struct list_head *mappers, struct imapper *entry,
  21. int (*map_op)(void *, struct imapper *), void *data)
  22. {
  23. struct list_head *pos, *pre, *head;
  24. struct imapper *pre_ent, *pos_ent;
  25. head = mappers;
  26. if (list_empty(head)) {
  27. entry->next = entry->addr;
  28. map_op(data, entry);
  29. list_add(&entry->list, head);
  30. return 0;
  31. }
  32. list_for_each(pos, head) {
  33. pos_ent = list_entry(pos, struct imapper, list);
  34. if (pos_ent->slot > entry->slot) {
  35. /* found a position in list */
  36. break;
  37. }
  38. }
  39. if (pos != head) {
  40. pre = pos->prev;
  41. if (pre == head)
  42. pre = head->prev;
  43. __list_add(&entry->list, pos->prev, pos);
  44. } else {
  45. pre = head->prev;
  46. pos = head->next;
  47. list_add_tail(&entry->list, head);
  48. }
  49. pre_ent = list_entry(pre, struct imapper, list);
  50. pos_ent = list_entry(pos, struct imapper, list);
  51. entry->next = pos_ent->addr;
  52. map_op(data, entry);
  53. pre_ent->next = entry->addr;
  54. map_op(data, pre_ent);
  55. return 0;
  56. }
  57. int input_mapper_delete(struct list_head *mappers, struct imapper *entry,
  58. int (*map_op)(void *, struct imapper *), void *data)
  59. {
  60. struct list_head *next, *pre, *head;
  61. struct imapper *pre_ent, *next_ent;
  62. head = mappers;
  63. if (list_empty(head))
  64. return 0;
  65. pre = (entry->list.prev == head) ? head->prev : entry->list.prev;
  66. next = (entry->list.next == head) ? head->next : entry->list.next;
  67. if (pre == &entry->list) {
  68. /* entry is the only one node in mappers list */
  69. entry->next = entry->addr = entry->user = entry->slot = 0;
  70. map_op(data, entry);
  71. list_del(&entry->list);
  72. return 0;
  73. }
  74. pre_ent = list_entry(pre, struct imapper, list);
  75. next_ent = list_entry(next, struct imapper, list);
  76. pre_ent->next = next_ent->addr;
  77. map_op(data, pre_ent);
  78. list_del(&entry->list);
  79. return 0;
  80. }
  81. void free_input_mapper_list(struct list_head *head)
  82. {
  83. struct imapper *entry;
  84. struct list_head *pos;
  85. while (!list_empty(head)) {
  86. pos = head->next;
  87. list_del(pos);
  88. entry = list_entry(pos, struct imapper, list);
  89. kfree(entry);
  90. }
  91. }