ircomm_lmp.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. IRDA_DEBUG(0, "%s()\n", __func__ );
  50. /* Don't forget to refcount it - should be NULL anyway */
  51. if(userdata)
  52. skb_get(userdata);
  53. ret = irlmp_connect_request(self->lsap, info->dlsap_sel,
  54. info->saddr, info->daddr, NULL, userdata);
  55. return ret;
  56. }
  57. /*
  58. * Function ircomm_lmp_connect_response (self, skb)
  59. *
  60. *
  61. *
  62. */
  63. static int ircomm_lmp_connect_response(struct ircomm_cb *self,
  64. struct sk_buff *userdata)
  65. {
  66. struct sk_buff *tx_skb;
  67. IRDA_DEBUG(0, "%s()\n", __func__ );
  68. /* Any userdata supplied? */
  69. if (userdata == NULL) {
  70. tx_skb = alloc_skb(LMP_MAX_HEADER, GFP_ATOMIC);
  71. if (!tx_skb)
  72. return -ENOMEM;
  73. /* Reserve space for MUX and LAP header */
  74. skb_reserve(tx_skb, LMP_MAX_HEADER);
  75. } else {
  76. /*
  77. * Check that the client has reserved enough space for
  78. * headers
  79. */
  80. IRDA_ASSERT(skb_headroom(userdata) >= LMP_MAX_HEADER,
  81. return -1;);
  82. /* Don't forget to refcount it - should be NULL anyway */
  83. skb_get(userdata);
  84. tx_skb = userdata;
  85. }
  86. return irlmp_connect_response(self->lsap, tx_skb);
  87. }
  88. static int ircomm_lmp_disconnect_request(struct ircomm_cb *self,
  89. struct sk_buff *userdata,
  90. struct ircomm_info *info)
  91. {
  92. struct sk_buff *tx_skb;
  93. int ret;
  94. IRDA_DEBUG(0, "%s()\n", __func__ );
  95. if (!userdata) {
  96. tx_skb = alloc_skb(LMP_MAX_HEADER, GFP_ATOMIC);
  97. if (!tx_skb)
  98. return -ENOMEM;
  99. /* Reserve space for MUX and LAP header */
  100. skb_reserve(tx_skb, LMP_MAX_HEADER);
  101. userdata = tx_skb;
  102. } else {
  103. /* Don't forget to refcount it - should be NULL anyway */
  104. skb_get(userdata);
  105. }
  106. ret = irlmp_disconnect_request(self->lsap, userdata);
  107. return ret;
  108. }
  109. /*
  110. * Function ircomm_lmp_flow_control (skb)
  111. *
  112. * This function is called when a data frame we have sent to IrLAP has
  113. * been deallocated. We do this to make sure we don't flood IrLAP with
  114. * frames, since we are not using the IrTTP flow control mechanism
  115. */
  116. static void ircomm_lmp_flow_control(struct sk_buff *skb)
  117. {
  118. struct irda_skb_cb *cb;
  119. struct ircomm_cb *self;
  120. int line;
  121. IRDA_ASSERT(skb != NULL, return;);
  122. cb = (struct irda_skb_cb *) skb->cb;
  123. IRDA_DEBUG(2, "%s()\n", __func__ );
  124. line = cb->line;
  125. self = (struct ircomm_cb *) hashbin_lock_find(ircomm, line, NULL);
  126. if (!self) {
  127. IRDA_DEBUG(2, "%s(), didn't find myself\n", __func__ );
  128. return;
  129. }
  130. IRDA_ASSERT(self != NULL, return;);
  131. IRDA_ASSERT(self->magic == IRCOMM_MAGIC, return;);
  132. self->pkt_count--;
  133. if ((self->pkt_count < 2) && (self->flow_status == FLOW_STOP)) {
  134. IRDA_DEBUG(2, "%s(), asking TTY to start again!\n", __func__ );
  135. self->flow_status = FLOW_START;
  136. if (self->notify.flow_indication)
  137. self->notify.flow_indication(self->notify.instance,
  138. self, FLOW_START);
  139. }
  140. }
  141. /*
  142. * Function ircomm_lmp_data_request (self, userdata)
  143. *
  144. * Send data frame to peer device
  145. *
  146. */
  147. static int ircomm_lmp_data_request(struct ircomm_cb *self,
  148. struct sk_buff *skb,
  149. int not_used)
  150. {
  151. struct irda_skb_cb *cb;
  152. int ret;
  153. IRDA_ASSERT(skb != NULL, return -1;);
  154. cb = (struct irda_skb_cb *) skb->cb;
  155. cb->line = self->line;
  156. IRDA_DEBUG(4, "%s(), sending frame\n", __func__ );
  157. /* Don't forget to refcount it - see ircomm_tty_do_softint() */
  158. skb_get(skb);
  159. skb_orphan(skb);
  160. skb->destructor = ircomm_lmp_flow_control;
  161. if ((self->pkt_count++ > 7) && (self->flow_status == FLOW_START)) {
  162. IRDA_DEBUG(2, "%s(), asking TTY to slow down!\n", __func__ );
  163. self->flow_status = FLOW_STOP;
  164. if (self->notify.flow_indication)
  165. self->notify.flow_indication(self->notify.instance,
  166. self, FLOW_STOP);
  167. }
  168. ret = irlmp_data_request(self->lsap, skb);
  169. if (ret) {
  170. IRDA_ERROR("%s(), failed\n", __func__);
  171. /* irlmp_data_request already free the packet */
  172. }
  173. return ret;
  174. }
  175. /*
  176. * Function ircomm_lmp_data_indication (instance, sap, skb)
  177. *
  178. * Incoming data which we must deliver to the state machine, to check
  179. * we are still connected.
  180. */
  181. static int ircomm_lmp_data_indication(void *instance, void *sap,
  182. struct sk_buff *skb)
  183. {
  184. struct ircomm_cb *self = (struct ircomm_cb *) instance;
  185. IRDA_DEBUG(4, "%s()\n", __func__ );
  186. IRDA_ASSERT(self != NULL, return -1;);
  187. IRDA_ASSERT(self->magic == IRCOMM_MAGIC, return -1;);
  188. IRDA_ASSERT(skb != NULL, return -1;);
  189. ircomm_do_event(self, IRCOMM_LMP_DATA_INDICATION, skb, NULL);
  190. /* Drop reference count - see ircomm_tty_data_indication(). */
  191. dev_kfree_skb(skb);
  192. return 0;
  193. }
  194. /*
  195. * Function ircomm_lmp_connect_confirm (instance, sap, qos, max_sdu_size,
  196. * max_header_size, skb)
  197. *
  198. * Connection has been confirmed by peer device
  199. *
  200. */
  201. static void ircomm_lmp_connect_confirm(void *instance, void *sap,
  202. struct qos_info *qos,
  203. __u32 max_seg_size,
  204. __u8 max_header_size,
  205. struct sk_buff *skb)
  206. {
  207. struct ircomm_cb *self = (struct ircomm_cb *) instance;
  208. struct ircomm_info info;
  209. IRDA_DEBUG(0, "%s()\n", __func__ );
  210. IRDA_ASSERT(self != NULL, return;);
  211. IRDA_ASSERT(self->magic == IRCOMM_MAGIC, return;);
  212. IRDA_ASSERT(skb != NULL, return;);
  213. IRDA_ASSERT(qos != NULL, return;);
  214. info.max_data_size = max_seg_size;
  215. info.max_header_size = max_header_size;
  216. info.qos = qos;
  217. ircomm_do_event(self, IRCOMM_LMP_CONNECT_CONFIRM, skb, &info);
  218. /* Drop reference count - see ircomm_tty_connect_confirm(). */
  219. dev_kfree_skb(skb);
  220. }
  221. /*
  222. * Function ircomm_lmp_connect_indication (instance, sap, qos, max_sdu_size,
  223. * max_header_size, skb)
  224. *
  225. * Peer device wants to make a connection with us
  226. *
  227. */
  228. static void ircomm_lmp_connect_indication(void *instance, void *sap,
  229. struct qos_info *qos,
  230. __u32 max_seg_size,
  231. __u8 max_header_size,
  232. struct sk_buff *skb)
  233. {
  234. struct ircomm_cb *self = (struct ircomm_cb *)instance;
  235. struct ircomm_info info;
  236. IRDA_DEBUG(0, "%s()\n", __func__ );
  237. IRDA_ASSERT(self != NULL, return;);
  238. IRDA_ASSERT(self->magic == IRCOMM_MAGIC, return;);
  239. IRDA_ASSERT(skb != NULL, return;);
  240. IRDA_ASSERT(qos != NULL, return;);
  241. info.max_data_size = max_seg_size;
  242. info.max_header_size = max_header_size;
  243. info.qos = qos;
  244. ircomm_do_event(self, IRCOMM_LMP_CONNECT_INDICATION, skb, &info);
  245. /* Drop reference count - see ircomm_tty_connect_indication(). */
  246. dev_kfree_skb(skb);
  247. }
  248. /*
  249. * Function ircomm_lmp_disconnect_indication (instance, sap, reason, skb)
  250. *
  251. * Peer device has closed the connection, or the link went down for some
  252. * other reason
  253. */
  254. static void ircomm_lmp_disconnect_indication(void *instance, void *sap,
  255. LM_REASON reason,
  256. struct sk_buff *skb)
  257. {
  258. struct ircomm_cb *self = (struct ircomm_cb *) instance;
  259. struct ircomm_info info;
  260. IRDA_DEBUG(0, "%s()\n", __func__ );
  261. IRDA_ASSERT(self != NULL, return;);
  262. IRDA_ASSERT(self->magic == IRCOMM_MAGIC, return;);
  263. info.reason = reason;
  264. ircomm_do_event(self, IRCOMM_LMP_DISCONNECT_INDICATION, skb, &info);
  265. /* Drop reference count - see ircomm_tty_disconnect_indication(). */
  266. if(skb)
  267. dev_kfree_skb(skb);
  268. }
  269. /*
  270. * Function ircomm_open_lsap (self)
  271. *
  272. * Open LSAP. This function will only be used when using "raw" services
  273. *
  274. */
  275. int ircomm_open_lsap(struct ircomm_cb *self)
  276. {
  277. notify_t notify;
  278. IRDA_DEBUG(0, "%s()\n", __func__ );
  279. /* Register callbacks */
  280. irda_notify_init(&notify);
  281. notify.data_indication = ircomm_lmp_data_indication;
  282. notify.connect_confirm = ircomm_lmp_connect_confirm;
  283. notify.connect_indication = ircomm_lmp_connect_indication;
  284. notify.disconnect_indication = ircomm_lmp_disconnect_indication;
  285. notify.instance = self;
  286. strlcpy(notify.name, "IrCOMM", sizeof(notify.name));
  287. self->lsap = irlmp_open_lsap(LSAP_ANY, &notify, 0);
  288. if (!self->lsap) {
  289. IRDA_DEBUG(0,"%sfailed to allocate tsap\n", __func__ );
  290. return -1;
  291. }
  292. self->slsap_sel = self->lsap->slsap_sel;
  293. /*
  294. * Initialize the call-table for issuing commands
  295. */
  296. self->issue.data_request = ircomm_lmp_data_request;
  297. self->issue.connect_request = ircomm_lmp_connect_request;
  298. self->issue.connect_response = ircomm_lmp_connect_response;
  299. self->issue.disconnect_request = ircomm_lmp_disconnect_request;
  300. return 0;
  301. }