ircomm_lmp.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*********************************************************************
  2. *
  3. * Filename: ircomm_lmp.c
  4. * Version: 1.0
  5. * Description: Interface between IrCOMM and IrLMP
  6. * Status: Stable
  7. * Author: Dag Brattli <dagb@cs.uit.no>
  8. * Created at: Sun Jun 6 20:48:27 1999
  9. * Modified at: Sun Dec 12 13:44:17 1999
  10. * Modified by: Dag Brattli <dagb@cs.uit.no>
  11. * Sources: Previous IrLPT work by Thomas Davis
  12. *
  13. * Copyright (c) 1999 Dag Brattli, All Rights Reserved.
  14. * Copyright (c) 2000-2003 Jean Tourrilhes <jt@hpl.hp.com>
  15. *
  16. * This program is free software; you can redistribute it and/or
  17. * modify it under the terms of the GNU General Public License as
  18. * published by the Free Software Foundation; either version 2 of
  19. * the License, or (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  28. *
  29. ********************************************************************/
  30. #include <linux/init.h>
  31. #include <linux/gfp.h>
  32. #include <net/irda/irda.h>
  33. #include <net/irda/irlmp.h>
  34. #include <net/irda/iriap.h>
  35. #include <net/irda/irda_device.h> /* struct irda_skb_cb */
  36. #include <net/irda/ircomm_event.h>
  37. #include <net/irda/ircomm_lmp.h>
  38. /*
  39. * Function ircomm_lmp_connect_request (self, userdata)
  40. *
  41. *
  42. *
  43. */
  44. static int ircomm_lmp_connect_request(struct ircomm_cb *self,
  45. struct sk_buff *userdata,
  46. struct ircomm_info *info)
  47. {
  48. int ret = 0;
  49. /* Don't forget to refcount it - should be NULL anyway */
  50. if(userdata)
  51. skb_get(userdata);
  52. ret = irlmp_connect_request(self->lsap, info->dlsap_sel,
  53. info->saddr, info->daddr, NULL, userdata);
  54. return ret;
  55. }
  56. /*
  57. * Function ircomm_lmp_connect_response (self, skb)
  58. *
  59. *
  60. *
  61. */
  62. static int ircomm_lmp_connect_response(struct ircomm_cb *self,
  63. struct sk_buff *userdata)
  64. {
  65. struct sk_buff *tx_skb;
  66. /* Any userdata supplied? */
  67. if (userdata == NULL) {
  68. tx_skb = alloc_skb(LMP_MAX_HEADER, GFP_ATOMIC);
  69. if (!tx_skb)
  70. return -ENOMEM;
  71. /* Reserve space for MUX and LAP header */
  72. skb_reserve(tx_skb, LMP_MAX_HEADER);
  73. } else {
  74. /*
  75. * Check that the client has reserved enough space for
  76. * headers
  77. */
  78. IRDA_ASSERT(skb_headroom(userdata) >= LMP_MAX_HEADER,
  79. return -1;);
  80. /* Don't forget to refcount it - should be NULL anyway */
  81. skb_get(userdata);
  82. tx_skb = userdata;
  83. }
  84. return irlmp_connect_response(self->lsap, tx_skb);
  85. }
  86. static int ircomm_lmp_disconnect_request(struct ircomm_cb *self,
  87. struct sk_buff *userdata,
  88. struct ircomm_info *info)
  89. {
  90. struct sk_buff *tx_skb;
  91. int ret;
  92. if (!userdata) {
  93. tx_skb = alloc_skb(LMP_MAX_HEADER, GFP_ATOMIC);
  94. if (!tx_skb)
  95. return -ENOMEM;
  96. /* Reserve space for MUX and LAP header */
  97. skb_reserve(tx_skb, LMP_MAX_HEADER);
  98. userdata = tx_skb;
  99. } else {
  100. /* Don't forget to refcount it - should be NULL anyway */
  101. skb_get(userdata);
  102. }
  103. ret = irlmp_disconnect_request(self->lsap, userdata);
  104. return ret;
  105. }
  106. /*
  107. * Function ircomm_lmp_flow_control (skb)
  108. *
  109. * This function is called when a data frame we have sent to IrLAP has
  110. * been deallocated. We do this to make sure we don't flood IrLAP with
  111. * frames, since we are not using the IrTTP flow control mechanism
  112. */
  113. static void ircomm_lmp_flow_control(struct sk_buff *skb)
  114. {
  115. struct irda_skb_cb *cb;
  116. struct ircomm_cb *self;
  117. int line;
  118. IRDA_ASSERT(skb != NULL, return;);
  119. cb = (struct irda_skb_cb *) skb->cb;
  120. line = cb->line;
  121. self = (struct ircomm_cb *) hashbin_lock_find(ircomm, line, NULL);
  122. if (!self) {
  123. pr_debug("%s(), didn't find myself\n", __func__);
  124. return;
  125. }
  126. IRDA_ASSERT(self != NULL, return;);
  127. IRDA_ASSERT(self->magic == IRCOMM_MAGIC, return;);
  128. self->pkt_count--;
  129. if ((self->pkt_count < 2) && (self->flow_status == FLOW_STOP)) {
  130. pr_debug("%s(), asking TTY to start again!\n", __func__);
  131. self->flow_status = FLOW_START;
  132. if (self->notify.flow_indication)
  133. self->notify.flow_indication(self->notify.instance,
  134. self, FLOW_START);
  135. }
  136. }
  137. /*
  138. * Function ircomm_lmp_data_request (self, userdata)
  139. *
  140. * Send data frame to peer device
  141. *
  142. */
  143. static int ircomm_lmp_data_request(struct ircomm_cb *self,
  144. struct sk_buff *skb,
  145. int not_used)
  146. {
  147. struct irda_skb_cb *cb;
  148. int ret;
  149. IRDA_ASSERT(skb != NULL, return -1;);
  150. cb = (struct irda_skb_cb *) skb->cb;
  151. cb->line = self->line;
  152. pr_debug("%s(), sending frame\n", __func__);
  153. /* Don't forget to refcount it - see ircomm_tty_do_softint() */
  154. skb_get(skb);
  155. skb_orphan(skb);
  156. skb->destructor = ircomm_lmp_flow_control;
  157. if ((self->pkt_count++ > 7) && (self->flow_status == FLOW_START)) {
  158. pr_debug("%s(), asking TTY to slow down!\n", __func__);
  159. self->flow_status = FLOW_STOP;
  160. if (self->notify.flow_indication)
  161. self->notify.flow_indication(self->notify.instance,
  162. self, FLOW_STOP);
  163. }
  164. ret = irlmp_data_request(self->lsap, skb);
  165. if (ret) {
  166. net_err_ratelimited("%s(), failed\n", __func__);
  167. /* irlmp_data_request already free the packet */
  168. }
  169. return ret;
  170. }
  171. /*
  172. * Function ircomm_lmp_data_indication (instance, sap, skb)
  173. *
  174. * Incoming data which we must deliver to the state machine, to check
  175. * we are still connected.
  176. */
  177. static int ircomm_lmp_data_indication(void *instance, void *sap,
  178. struct sk_buff *skb)
  179. {
  180. struct ircomm_cb *self = (struct ircomm_cb *) instance;
  181. IRDA_ASSERT(self != NULL, return -1;);
  182. IRDA_ASSERT(self->magic == IRCOMM_MAGIC, return -1;);
  183. IRDA_ASSERT(skb != NULL, return -1;);
  184. ircomm_do_event(self, IRCOMM_LMP_DATA_INDICATION, skb, NULL);
  185. /* Drop reference count - see ircomm_tty_data_indication(). */
  186. dev_kfree_skb(skb);
  187. return 0;
  188. }
  189. /*
  190. * Function ircomm_lmp_connect_confirm (instance, sap, qos, max_sdu_size,
  191. * max_header_size, skb)
  192. *
  193. * Connection has been confirmed by peer device
  194. *
  195. */
  196. static void ircomm_lmp_connect_confirm(void *instance, void *sap,
  197. struct qos_info *qos,
  198. __u32 max_seg_size,
  199. __u8 max_header_size,
  200. struct sk_buff *skb)
  201. {
  202. struct ircomm_cb *self = (struct ircomm_cb *) instance;
  203. struct ircomm_info info;
  204. IRDA_ASSERT(self != NULL, return;);
  205. IRDA_ASSERT(self->magic == IRCOMM_MAGIC, return;);
  206. IRDA_ASSERT(skb != NULL, return;);
  207. IRDA_ASSERT(qos != NULL, return;);
  208. info.max_data_size = max_seg_size;
  209. info.max_header_size = max_header_size;
  210. info.qos = qos;
  211. ircomm_do_event(self, IRCOMM_LMP_CONNECT_CONFIRM, skb, &info);
  212. /* Drop reference count - see ircomm_tty_connect_confirm(). */
  213. dev_kfree_skb(skb);
  214. }
  215. /*
  216. * Function ircomm_lmp_connect_indication (instance, sap, qos, max_sdu_size,
  217. * max_header_size, skb)
  218. *
  219. * Peer device wants to make a connection with us
  220. *
  221. */
  222. static void ircomm_lmp_connect_indication(void *instance, void *sap,
  223. struct qos_info *qos,
  224. __u32 max_seg_size,
  225. __u8 max_header_size,
  226. struct sk_buff *skb)
  227. {
  228. struct ircomm_cb *self = (struct ircomm_cb *)instance;
  229. struct ircomm_info info;
  230. IRDA_ASSERT(self != NULL, return;);
  231. IRDA_ASSERT(self->magic == IRCOMM_MAGIC, return;);
  232. IRDA_ASSERT(skb != NULL, return;);
  233. IRDA_ASSERT(qos != NULL, return;);
  234. info.max_data_size = max_seg_size;
  235. info.max_header_size = max_header_size;
  236. info.qos = qos;
  237. ircomm_do_event(self, IRCOMM_LMP_CONNECT_INDICATION, skb, &info);
  238. /* Drop reference count - see ircomm_tty_connect_indication(). */
  239. dev_kfree_skb(skb);
  240. }
  241. /*
  242. * Function ircomm_lmp_disconnect_indication (instance, sap, reason, skb)
  243. *
  244. * Peer device has closed the connection, or the link went down for some
  245. * other reason
  246. */
  247. static void ircomm_lmp_disconnect_indication(void *instance, void *sap,
  248. LM_REASON reason,
  249. struct sk_buff *skb)
  250. {
  251. struct ircomm_cb *self = (struct ircomm_cb *) instance;
  252. struct ircomm_info info;
  253. IRDA_ASSERT(self != NULL, return;);
  254. IRDA_ASSERT(self->magic == IRCOMM_MAGIC, return;);
  255. info.reason = reason;
  256. ircomm_do_event(self, IRCOMM_LMP_DISCONNECT_INDICATION, skb, &info);
  257. /* Drop reference count - see ircomm_tty_disconnect_indication(). */
  258. if(skb)
  259. dev_kfree_skb(skb);
  260. }
  261. /*
  262. * Function ircomm_open_lsap (self)
  263. *
  264. * Open LSAP. This function will only be used when using "raw" services
  265. *
  266. */
  267. int ircomm_open_lsap(struct ircomm_cb *self)
  268. {
  269. notify_t notify;
  270. /* Register callbacks */
  271. irda_notify_init(&notify);
  272. notify.data_indication = ircomm_lmp_data_indication;
  273. notify.connect_confirm = ircomm_lmp_connect_confirm;
  274. notify.connect_indication = ircomm_lmp_connect_indication;
  275. notify.disconnect_indication = ircomm_lmp_disconnect_indication;
  276. notify.instance = self;
  277. strlcpy(notify.name, "IrCOMM", sizeof(notify.name));
  278. self->lsap = irlmp_open_lsap(LSAP_ANY, &notify, 0);
  279. if (!self->lsap) {
  280. pr_debug("%sfailed to allocate tsap\n", __func__);
  281. return -1;
  282. }
  283. self->slsap_sel = self->lsap->slsap_sel;
  284. /*
  285. * Initialize the call-table for issuing commands
  286. */
  287. self->issue.data_request = ircomm_lmp_data_request;
  288. self->issue.connect_request = ircomm_lmp_connect_request;
  289. self->issue.connect_response = ircomm_lmp_connect_response;
  290. self->issue.disconnect_request = ircomm_lmp_disconnect_request;
  291. return 0;
  292. }