ircomm_lmp.c 10 KB

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