ircomm_lmp.c 9.7 KB

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