cfsrvl.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * Copyright (C) ST-Ericsson AB 2010
  3. * Author: Sjur Brendeland/sjur.brandeland@stericsson.com
  4. * License terms: GNU General Public License (GPL) version 2
  5. */
  6. #define pr_fmt(fmt) KBUILD_MODNAME ":%s(): " fmt, __func__
  7. #include <linux/kernel.h>
  8. #include <linux/types.h>
  9. #include <linux/errno.h>
  10. #include <linux/slab.h>
  11. #include <linux/module.h>
  12. #include <net/caif/caif_layer.h>
  13. #include <net/caif/cfsrvl.h>
  14. #include <net/caif/cfpkt.h>
  15. #define SRVL_CTRL_PKT_SIZE 1
  16. #define SRVL_FLOW_OFF 0x81
  17. #define SRVL_FLOW_ON 0x80
  18. #define SRVL_SET_PIN 0x82
  19. #define SRVL_CTRL_PKT_SIZE 1
  20. #define container_obj(layr) container_of(layr, struct cfsrvl, layer)
  21. static void cfservl_ctrlcmd(struct cflayer *layr, enum caif_ctrlcmd ctrl,
  22. int phyid)
  23. {
  24. struct cfsrvl *service = container_obj(layr);
  25. if (layr->up == NULL || layr->up->ctrlcmd == NULL)
  26. return;
  27. switch (ctrl) {
  28. case CAIF_CTRLCMD_INIT_RSP:
  29. service->open = true;
  30. layr->up->ctrlcmd(layr->up, ctrl, phyid);
  31. break;
  32. case CAIF_CTRLCMD_DEINIT_RSP:
  33. case CAIF_CTRLCMD_INIT_FAIL_RSP:
  34. service->open = false;
  35. layr->up->ctrlcmd(layr->up, ctrl, phyid);
  36. break;
  37. case _CAIF_CTRLCMD_PHYIF_FLOW_OFF_IND:
  38. if (phyid != service->dev_info.id)
  39. break;
  40. if (service->modem_flow_on)
  41. layr->up->ctrlcmd(layr->up,
  42. CAIF_CTRLCMD_FLOW_OFF_IND, phyid);
  43. service->phy_flow_on = false;
  44. break;
  45. case _CAIF_CTRLCMD_PHYIF_FLOW_ON_IND:
  46. if (phyid != service->dev_info.id)
  47. return;
  48. if (service->modem_flow_on) {
  49. layr->up->ctrlcmd(layr->up,
  50. CAIF_CTRLCMD_FLOW_ON_IND,
  51. phyid);
  52. }
  53. service->phy_flow_on = true;
  54. break;
  55. case CAIF_CTRLCMD_FLOW_OFF_IND:
  56. if (service->phy_flow_on) {
  57. layr->up->ctrlcmd(layr->up,
  58. CAIF_CTRLCMD_FLOW_OFF_IND, phyid);
  59. }
  60. service->modem_flow_on = false;
  61. break;
  62. case CAIF_CTRLCMD_FLOW_ON_IND:
  63. if (service->phy_flow_on) {
  64. layr->up->ctrlcmd(layr->up,
  65. CAIF_CTRLCMD_FLOW_ON_IND, phyid);
  66. }
  67. service->modem_flow_on = true;
  68. break;
  69. case _CAIF_CTRLCMD_PHYIF_DOWN_IND:
  70. /* In case interface is down, let's fake a remove shutdown */
  71. layr->up->ctrlcmd(layr->up,
  72. CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND, phyid);
  73. break;
  74. case CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND:
  75. layr->up->ctrlcmd(layr->up, ctrl, phyid);
  76. break;
  77. default:
  78. pr_warn("Unexpected ctrl in cfsrvl (%d)\n", ctrl);
  79. /* We have both modem and phy flow on, send flow on */
  80. layr->up->ctrlcmd(layr->up, ctrl, phyid);
  81. service->phy_flow_on = true;
  82. break;
  83. }
  84. }
  85. static int cfservl_modemcmd(struct cflayer *layr, enum caif_modemcmd ctrl)
  86. {
  87. struct cfsrvl *service = container_obj(layr);
  88. caif_assert(layr != NULL);
  89. caif_assert(layr->dn != NULL);
  90. caif_assert(layr->dn->transmit != NULL);
  91. if (!service->supports_flowctrl)
  92. return 0;
  93. switch (ctrl) {
  94. case CAIF_MODEMCMD_FLOW_ON_REQ:
  95. {
  96. struct cfpkt *pkt;
  97. struct caif_payload_info *info;
  98. u8 flow_on = SRVL_FLOW_ON;
  99. pkt = cfpkt_create(SRVL_CTRL_PKT_SIZE);
  100. if (!pkt)
  101. return -ENOMEM;
  102. if (cfpkt_add_head(pkt, &flow_on, 1) < 0) {
  103. pr_err("Packet is erroneous!\n");
  104. cfpkt_destroy(pkt);
  105. return -EPROTO;
  106. }
  107. info = cfpkt_info(pkt);
  108. info->channel_id = service->layer.id;
  109. info->hdr_len = 1;
  110. info->dev_info = &service->dev_info;
  111. return layr->dn->transmit(layr->dn, pkt);
  112. }
  113. case CAIF_MODEMCMD_FLOW_OFF_REQ:
  114. {
  115. struct cfpkt *pkt;
  116. struct caif_payload_info *info;
  117. u8 flow_off = SRVL_FLOW_OFF;
  118. pkt = cfpkt_create(SRVL_CTRL_PKT_SIZE);
  119. if (!pkt)
  120. return -ENOMEM;
  121. if (cfpkt_add_head(pkt, &flow_off, 1) < 0) {
  122. pr_err("Packet is erroneous!\n");
  123. cfpkt_destroy(pkt);
  124. return -EPROTO;
  125. }
  126. info = cfpkt_info(pkt);
  127. info->channel_id = service->layer.id;
  128. info->hdr_len = 1;
  129. info->dev_info = &service->dev_info;
  130. return layr->dn->transmit(layr->dn, pkt);
  131. }
  132. default:
  133. break;
  134. }
  135. return -EINVAL;
  136. }
  137. static void cfsrvl_release(struct cflayer *layer)
  138. {
  139. struct cfsrvl *service = container_of(layer, struct cfsrvl, layer);
  140. kfree(service);
  141. }
  142. void cfsrvl_init(struct cfsrvl *service,
  143. u8 channel_id,
  144. struct dev_info *dev_info,
  145. bool supports_flowctrl
  146. )
  147. {
  148. caif_assert(offsetof(struct cfsrvl, layer) == 0);
  149. service->open = false;
  150. service->modem_flow_on = true;
  151. service->phy_flow_on = true;
  152. service->layer.id = channel_id;
  153. service->layer.ctrlcmd = cfservl_ctrlcmd;
  154. service->layer.modemcmd = cfservl_modemcmd;
  155. service->dev_info = *dev_info;
  156. service->supports_flowctrl = supports_flowctrl;
  157. service->release = cfsrvl_release;
  158. }
  159. bool cfsrvl_ready(struct cfsrvl *service, int *err)
  160. {
  161. if (!service->open) {
  162. *err = -ENOTCONN;
  163. return false;
  164. }
  165. return true;
  166. }
  167. u8 cfsrvl_getphyid(struct cflayer *layer)
  168. {
  169. struct cfsrvl *servl = container_obj(layer);
  170. return servl->dev_info.id;
  171. }
  172. bool cfsrvl_phyid_match(struct cflayer *layer, int phyid)
  173. {
  174. struct cfsrvl *servl = container_obj(layer);
  175. return servl->dev_info.id == phyid;
  176. }
  177. void caif_free_client(struct cflayer *adap_layer)
  178. {
  179. struct cfsrvl *servl;
  180. if (adap_layer == NULL || adap_layer->dn == NULL)
  181. return;
  182. servl = container_obj(adap_layer->dn);
  183. servl->release(&servl->layer);
  184. }
  185. EXPORT_SYMBOL(caif_free_client);
  186. void caif_client_register_refcnt(struct cflayer *adapt_layer,
  187. void (*hold)(struct cflayer *lyr),
  188. void (*put)(struct cflayer *lyr))
  189. {
  190. struct cfsrvl *service;
  191. service = container_of(adapt_layer->dn, struct cfsrvl, layer);
  192. WARN_ON(adapt_layer == NULL || adapt_layer->dn == NULL);
  193. service->hold = hold;
  194. service->put = put;
  195. }
  196. EXPORT_SYMBOL(caif_client_register_refcnt);