ese_hal.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright (C) 2020 Samsung Electronics. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program;
  16. *
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/slab.h>
  20. #include "ese_i2c.h"
  21. #include "ese_spi.h"
  22. #include "ese_hal.h"
  23. #undef ENABLE_HAL_LOG
  24. struct ese_hal_s {
  25. void *client;
  26. int (*send)(void *ctx, unsigned char *buf, unsigned int size);
  27. int (*receive)(void *ctx, unsigned char *buf, unsigned int size);
  28. };
  29. int ese_hal_send(void *ctx, unsigned char *buf, unsigned int size)
  30. {
  31. struct ese_hal_s *hal = (struct ese_hal_s *)ctx;
  32. if (hal == NULL || hal->client == NULL || hal->send == NULL
  33. || buf == NULL || size == 0) {
  34. return -1;
  35. }
  36. #ifdef ENABLE_HAL_LOG
  37. print_hex_dump(KERN_DEBUG, "[star-hal] send : ", DUMP_PREFIX_NONE, 16, 1, buf, size, 0);
  38. #endif
  39. if (hal->send(hal->client, buf, size) < 0) {
  40. return -1;
  41. }
  42. return (int)size;
  43. }
  44. int ese_hal_receive(void *ctx, unsigned char *buf, unsigned int size)
  45. {
  46. struct ese_hal_s *hal = (struct ese_hal_s *)ctx;
  47. if (hal == NULL || hal->client == NULL || hal->receive == NULL
  48. || buf == NULL || size == 0) {
  49. return -1;
  50. }
  51. if (hal->receive(hal->client, buf, size) < 0) {
  52. return -1;
  53. }
  54. #ifdef ENABLE_HAL_LOG
  55. print_hex_dump(KERN_DEBUG, "[star-hal] recv : ", DUMP_PREFIX_NONE, 16, 1, buf, size, 0);
  56. #endif
  57. return (int)size;
  58. }
  59. void *ese_hal_init(enum hal_type_e type, void *client)
  60. {
  61. struct ese_hal_s *hal = NULL;
  62. if (client == NULL) {
  63. return NULL;
  64. }
  65. hal = kzalloc(sizeof(struct ese_hal_s), GFP_KERNEL);
  66. if (hal == NULL) {
  67. return NULL;
  68. }
  69. switch(type) {
  70. case ESE_HAL_I2C:
  71. hal->client = client;
  72. hal->send = ese_i2c_send;
  73. hal->receive = ese_i2c_receive;
  74. break;
  75. case ESE_HAL_SPI:
  76. hal->client = client;
  77. hal->send = ese_spi_send;
  78. hal->receive = ese_spi_receive;
  79. break;
  80. default:
  81. kfree(hal);
  82. return NULL;
  83. }
  84. return hal;
  85. }
  86. void ese_hal_release(void *ctx)
  87. {
  88. if (ctx != NULL) {
  89. kfree(ctx);
  90. }
  91. }