ircomm_core.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. /*********************************************************************
  2. *
  3. * Filename: ircomm_core.c
  4. * Version: 1.0
  5. * Description: IrCOMM service interface
  6. * Status: Experimental.
  7. * Author: Dag Brattli <dagb@cs.uit.no>
  8. * Created at: Sun Jun 6 20:37:34 1999
  9. * Modified at: Tue Dec 21 13:26:41 1999
  10. * Modified by: Dag Brattli <dagb@cs.uit.no>
  11. *
  12. * Copyright (c) 1999 Dag Brattli, All Rights Reserved.
  13. * Copyright (c) 2000-2003 Jean Tourrilhes <jt@hpl.hp.com>
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License as
  17. * published by the Free Software Foundation; either version 2 of
  18. * the License, or (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  28. * MA 02111-1307 USA
  29. *
  30. ********************************************************************/
  31. #include <linux/module.h>
  32. #include <linux/proc_fs.h>
  33. #include <linux/seq_file.h>
  34. #include <linux/init.h>
  35. #include <net/irda/irda.h>
  36. #include <net/irda/irmod.h>
  37. #include <net/irda/irlmp.h>
  38. #include <net/irda/iriap.h>
  39. #include <net/irda/irttp.h>
  40. #include <net/irda/irias_object.h>
  41. #include <net/irda/ircomm_event.h>
  42. #include <net/irda/ircomm_lmp.h>
  43. #include <net/irda/ircomm_ttp.h>
  44. #include <net/irda/ircomm_param.h>
  45. #include <net/irda/ircomm_core.h>
  46. static int __ircomm_close(struct ircomm_cb *self);
  47. static void ircomm_control_indication(struct ircomm_cb *self,
  48. struct sk_buff *skb, int clen);
  49. #ifdef CONFIG_PROC_FS
  50. extern struct proc_dir_entry *proc_irda;
  51. static int ircomm_seq_open(struct inode *, struct file *);
  52. static const struct file_operations ircomm_proc_fops = {
  53. .owner = THIS_MODULE,
  54. .open = ircomm_seq_open,
  55. .read = seq_read,
  56. .llseek = seq_lseek,
  57. .release = seq_release,
  58. };
  59. #endif /* CONFIG_PROC_FS */
  60. hashbin_t *ircomm = NULL;
  61. static int __init ircomm_init(void)
  62. {
  63. ircomm = hashbin_new(HB_LOCK);
  64. if (ircomm == NULL) {
  65. IRDA_ERROR("%s(), can't allocate hashbin!\n", __func__);
  66. return -ENOMEM;
  67. }
  68. #ifdef CONFIG_PROC_FS
  69. { struct proc_dir_entry *ent;
  70. ent = proc_create("ircomm", 0, proc_irda, &ircomm_proc_fops);
  71. if (!ent) {
  72. printk(KERN_ERR "ircomm_init: can't create /proc entry!\n");
  73. return -ENODEV;
  74. }
  75. }
  76. #endif /* CONFIG_PROC_FS */
  77. IRDA_MESSAGE("IrCOMM protocol (Dag Brattli)\n");
  78. return 0;
  79. }
  80. static void __exit ircomm_cleanup(void)
  81. {
  82. IRDA_DEBUG(2, "%s()\n", __func__ );
  83. hashbin_delete(ircomm, (FREE_FUNC) __ircomm_close);
  84. #ifdef CONFIG_PROC_FS
  85. remove_proc_entry("ircomm", proc_irda);
  86. #endif /* CONFIG_PROC_FS */
  87. }
  88. /*
  89. * Function ircomm_open (client_notify)
  90. *
  91. * Start a new IrCOMM instance
  92. *
  93. */
  94. struct ircomm_cb *ircomm_open(notify_t *notify, __u8 service_type, int line)
  95. {
  96. struct ircomm_cb *self = NULL;
  97. int ret;
  98. IRDA_DEBUG(2, "%s(), service_type=0x%02x\n", __func__ ,
  99. service_type);
  100. IRDA_ASSERT(ircomm != NULL, return NULL;);
  101. self = kzalloc(sizeof(struct ircomm_cb), GFP_ATOMIC);
  102. if (self == NULL)
  103. return NULL;
  104. self->notify = *notify;
  105. self->magic = IRCOMM_MAGIC;
  106. /* Check if we should use IrLMP or IrTTP */
  107. if (service_type & IRCOMM_3_WIRE_RAW) {
  108. self->flow_status = FLOW_START;
  109. ret = ircomm_open_lsap(self);
  110. } else
  111. ret = ircomm_open_tsap(self);
  112. if (ret < 0) {
  113. kfree(self);
  114. return NULL;
  115. }
  116. self->service_type = service_type;
  117. self->line = line;
  118. hashbin_insert(ircomm, (irda_queue_t *) self, line, NULL);
  119. ircomm_next_state(self, IRCOMM_IDLE);
  120. return self;
  121. }
  122. EXPORT_SYMBOL(ircomm_open);
  123. /*
  124. * Function ircomm_close_instance (self)
  125. *
  126. * Remove IrCOMM instance
  127. *
  128. */
  129. static int __ircomm_close(struct ircomm_cb *self)
  130. {
  131. IRDA_DEBUG(2, "%s()\n", __func__ );
  132. /* Disconnect link if any */
  133. ircomm_do_event(self, IRCOMM_DISCONNECT_REQUEST, NULL, NULL);
  134. /* Remove TSAP */
  135. if (self->tsap) {
  136. irttp_close_tsap(self->tsap);
  137. self->tsap = NULL;
  138. }
  139. /* Remove LSAP */
  140. if (self->lsap) {
  141. irlmp_close_lsap(self->lsap);
  142. self->lsap = NULL;
  143. }
  144. self->magic = 0;
  145. kfree(self);
  146. return 0;
  147. }
  148. /*
  149. * Function ircomm_close (self)
  150. *
  151. * Closes and removes the specified IrCOMM instance
  152. *
  153. */
  154. int ircomm_close(struct ircomm_cb *self)
  155. {
  156. struct ircomm_cb *entry;
  157. IRDA_ASSERT(self != NULL, return -EIO;);
  158. IRDA_ASSERT(self->magic == IRCOMM_MAGIC, return -EIO;);
  159. IRDA_DEBUG(0, "%s()\n", __func__ );
  160. entry = hashbin_remove(ircomm, self->line, NULL);
  161. IRDA_ASSERT(entry == self, return -1;);
  162. return __ircomm_close(self);
  163. }
  164. EXPORT_SYMBOL(ircomm_close);
  165. /*
  166. * Function ircomm_connect_request (self, service_type)
  167. *
  168. * Impl. of this function is differ from one of the reference. This
  169. * function does discovery as well as sending connect request
  170. *
  171. */
  172. int ircomm_connect_request(struct ircomm_cb *self, __u8 dlsap_sel,
  173. __u32 saddr, __u32 daddr, struct sk_buff *skb,
  174. __u8 service_type)
  175. {
  176. struct ircomm_info info;
  177. int ret;
  178. IRDA_DEBUG(2 , "%s()\n", __func__ );
  179. IRDA_ASSERT(self != NULL, return -1;);
  180. IRDA_ASSERT(self->magic == IRCOMM_MAGIC, return -1;);
  181. self->service_type= service_type;
  182. info.dlsap_sel = dlsap_sel;
  183. info.saddr = saddr;
  184. info.daddr = daddr;
  185. ret = ircomm_do_event(self, IRCOMM_CONNECT_REQUEST, skb, &info);
  186. return ret;
  187. }
  188. EXPORT_SYMBOL(ircomm_connect_request);
  189. /*
  190. * Function ircomm_connect_indication (self, qos, skb)
  191. *
  192. * Notify user layer about the incoming connection
  193. *
  194. */
  195. void ircomm_connect_indication(struct ircomm_cb *self, struct sk_buff *skb,
  196. struct ircomm_info *info)
  197. {
  198. int clen = 0;
  199. IRDA_DEBUG(2, "%s()\n", __func__ );
  200. /* Check if the packet contains data on the control channel */
  201. if (skb->len > 0)
  202. clen = skb->data[0];
  203. /*
  204. * If there are any data hiding in the control channel, we must
  205. * deliver it first. The side effect is that the control channel
  206. * will be removed from the skb
  207. */
  208. if (self->notify.connect_indication)
  209. self->notify.connect_indication(self->notify.instance, self,
  210. info->qos, info->max_data_size,
  211. info->max_header_size, skb);
  212. else {
  213. IRDA_DEBUG(0, "%s(), missing handler\n", __func__ );
  214. }
  215. }
  216. /*
  217. * Function ircomm_connect_response (self, userdata, max_sdu_size)
  218. *
  219. * User accepts connection
  220. *
  221. */
  222. int ircomm_connect_response(struct ircomm_cb *self, struct sk_buff *userdata)
  223. {
  224. int ret;
  225. IRDA_ASSERT(self != NULL, return -1;);
  226. IRDA_ASSERT(self->magic == IRCOMM_MAGIC, return -1;);
  227. IRDA_DEBUG(4, "%s()\n", __func__ );
  228. ret = ircomm_do_event(self, IRCOMM_CONNECT_RESPONSE, userdata, NULL);
  229. return ret;
  230. }
  231. EXPORT_SYMBOL(ircomm_connect_response);
  232. /*
  233. * Function connect_confirm (self, skb)
  234. *
  235. * Notify user layer that the link is now connected
  236. *
  237. */
  238. void ircomm_connect_confirm(struct ircomm_cb *self, struct sk_buff *skb,
  239. struct ircomm_info *info)
  240. {
  241. IRDA_DEBUG(4, "%s()\n", __func__ );
  242. if (self->notify.connect_confirm )
  243. self->notify.connect_confirm(self->notify.instance,
  244. self, info->qos,
  245. info->max_data_size,
  246. info->max_header_size, skb);
  247. else {
  248. IRDA_DEBUG(0, "%s(), missing handler\n", __func__ );
  249. }
  250. }
  251. /*
  252. * Function ircomm_data_request (self, userdata)
  253. *
  254. * Send IrCOMM data to peer device
  255. *
  256. */
  257. int ircomm_data_request(struct ircomm_cb *self, struct sk_buff *skb)
  258. {
  259. int ret;
  260. IRDA_DEBUG(4, "%s()\n", __func__ );
  261. IRDA_ASSERT(self != NULL, return -EFAULT;);
  262. IRDA_ASSERT(self->magic == IRCOMM_MAGIC, return -EFAULT;);
  263. IRDA_ASSERT(skb != NULL, return -EFAULT;);
  264. ret = ircomm_do_event(self, IRCOMM_DATA_REQUEST, skb, NULL);
  265. return ret;
  266. }
  267. EXPORT_SYMBOL(ircomm_data_request);
  268. /*
  269. * Function ircomm_data_indication (self, skb)
  270. *
  271. * Data arrived, so deliver it to user
  272. *
  273. */
  274. void ircomm_data_indication(struct ircomm_cb *self, struct sk_buff *skb)
  275. {
  276. IRDA_DEBUG(4, "%s()\n", __func__ );
  277. IRDA_ASSERT(skb->len > 0, return;);
  278. if (self->notify.data_indication)
  279. self->notify.data_indication(self->notify.instance, self, skb);
  280. else {
  281. IRDA_DEBUG(0, "%s(), missing handler\n", __func__ );
  282. }
  283. }
  284. /*
  285. * Function ircomm_process_data (self, skb)
  286. *
  287. * Data arrived which may contain control channel data
  288. *
  289. */
  290. void ircomm_process_data(struct ircomm_cb *self, struct sk_buff *skb)
  291. {
  292. int clen;
  293. IRDA_ASSERT(skb->len > 0, return;);
  294. clen = skb->data[0];
  295. /*
  296. * Input validation check: a stir4200/mcp2150 combinations sometimes
  297. * results in frames with clen > remaining packet size. These are
  298. * illegal; if we throw away just this frame then it seems to carry on
  299. * fine
  300. */
  301. if (unlikely(skb->len < (clen + 1))) {
  302. IRDA_DEBUG(2, "%s() throwing away illegal frame\n",
  303. __func__ );
  304. return;
  305. }
  306. /*
  307. * If there are any data hiding in the control channel, we must
  308. * deliver it first. The side effect is that the control channel
  309. * will be removed from the skb
  310. */
  311. if (clen > 0)
  312. ircomm_control_indication(self, skb, clen);
  313. /* Remove control channel from data channel */
  314. skb_pull(skb, clen+1);
  315. if (skb->len)
  316. ircomm_data_indication(self, skb);
  317. else {
  318. IRDA_DEBUG(4, "%s(), data was control info only!\n",
  319. __func__ );
  320. }
  321. }
  322. /*
  323. * Function ircomm_control_request (self, params)
  324. *
  325. * Send control data to peer device
  326. *
  327. */
  328. int ircomm_control_request(struct ircomm_cb *self, struct sk_buff *skb)
  329. {
  330. int ret;
  331. IRDA_DEBUG(2, "%s()\n", __func__ );
  332. IRDA_ASSERT(self != NULL, return -EFAULT;);
  333. IRDA_ASSERT(self->magic == IRCOMM_MAGIC, return -EFAULT;);
  334. IRDA_ASSERT(skb != NULL, return -EFAULT;);
  335. ret = ircomm_do_event(self, IRCOMM_CONTROL_REQUEST, skb, NULL);
  336. return ret;
  337. }
  338. EXPORT_SYMBOL(ircomm_control_request);
  339. /*
  340. * Function ircomm_control_indication (self, skb)
  341. *
  342. * Data has arrived on the control channel
  343. *
  344. */
  345. static void ircomm_control_indication(struct ircomm_cb *self,
  346. struct sk_buff *skb, int clen)
  347. {
  348. IRDA_DEBUG(2, "%s()\n", __func__ );
  349. /* Use udata for delivering data on the control channel */
  350. if (self->notify.udata_indication) {
  351. struct sk_buff *ctrl_skb;
  352. /* We don't own the skb, so clone it */
  353. ctrl_skb = skb_clone(skb, GFP_ATOMIC);
  354. if (!ctrl_skb)
  355. return;
  356. /* Remove data channel from control channel */
  357. skb_trim(ctrl_skb, clen+1);
  358. self->notify.udata_indication(self->notify.instance, self,
  359. ctrl_skb);
  360. /* Drop reference count -
  361. * see ircomm_tty_control_indication(). */
  362. dev_kfree_skb(ctrl_skb);
  363. } else {
  364. IRDA_DEBUG(0, "%s(), missing handler\n", __func__ );
  365. }
  366. }
  367. /*
  368. * Function ircomm_disconnect_request (self, userdata, priority)
  369. *
  370. * User layer wants to disconnect the IrCOMM connection
  371. *
  372. */
  373. int ircomm_disconnect_request(struct ircomm_cb *self, struct sk_buff *userdata)
  374. {
  375. struct ircomm_info info;
  376. int ret;
  377. IRDA_DEBUG(2, "%s()\n", __func__ );
  378. IRDA_ASSERT(self != NULL, return -1;);
  379. IRDA_ASSERT(self->magic == IRCOMM_MAGIC, return -1;);
  380. ret = ircomm_do_event(self, IRCOMM_DISCONNECT_REQUEST, userdata,
  381. &info);
  382. return ret;
  383. }
  384. EXPORT_SYMBOL(ircomm_disconnect_request);
  385. /*
  386. * Function disconnect_indication (self, skb)
  387. *
  388. * Tell user that the link has been disconnected
  389. *
  390. */
  391. void ircomm_disconnect_indication(struct ircomm_cb *self, struct sk_buff *skb,
  392. struct ircomm_info *info)
  393. {
  394. IRDA_DEBUG(2, "%s()\n", __func__ );
  395. IRDA_ASSERT(info != NULL, return;);
  396. if (self->notify.disconnect_indication) {
  397. self->notify.disconnect_indication(self->notify.instance, self,
  398. info->reason, skb);
  399. } else {
  400. IRDA_DEBUG(0, "%s(), missing handler\n", __func__ );
  401. }
  402. }
  403. /*
  404. * Function ircomm_flow_request (self, flow)
  405. *
  406. *
  407. *
  408. */
  409. void ircomm_flow_request(struct ircomm_cb *self, LOCAL_FLOW flow)
  410. {
  411. IRDA_DEBUG(2, "%s()\n", __func__ );
  412. IRDA_ASSERT(self != NULL, return;);
  413. IRDA_ASSERT(self->magic == IRCOMM_MAGIC, return;);
  414. if (self->service_type == IRCOMM_3_WIRE_RAW)
  415. return;
  416. irttp_flow_request(self->tsap, flow);
  417. }
  418. EXPORT_SYMBOL(ircomm_flow_request);
  419. #ifdef CONFIG_PROC_FS
  420. static void *ircomm_seq_start(struct seq_file *seq, loff_t *pos)
  421. {
  422. struct ircomm_cb *self;
  423. loff_t off = 0;
  424. spin_lock_irq(&ircomm->hb_spinlock);
  425. for (self = (struct ircomm_cb *) hashbin_get_first(ircomm);
  426. self != NULL;
  427. self = (struct ircomm_cb *) hashbin_get_next(ircomm)) {
  428. if (off++ == *pos)
  429. break;
  430. }
  431. return self;
  432. }
  433. static void *ircomm_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  434. {
  435. ++*pos;
  436. return (void *) hashbin_get_next(ircomm);
  437. }
  438. static void ircomm_seq_stop(struct seq_file *seq, void *v)
  439. {
  440. spin_unlock_irq(&ircomm->hb_spinlock);
  441. }
  442. static int ircomm_seq_show(struct seq_file *seq, void *v)
  443. {
  444. const struct ircomm_cb *self = v;
  445. IRDA_ASSERT(self->magic == IRCOMM_MAGIC, return -EINVAL; );
  446. if(self->line < 0x10)
  447. seq_printf(seq, "ircomm%d", self->line);
  448. else
  449. seq_printf(seq, "irlpt%d", self->line - 0x10);
  450. seq_printf(seq,
  451. " state: %s, slsap_sel: %#02x, dlsap_sel: %#02x, mode:",
  452. ircomm_state[ self->state],
  453. self->slsap_sel, self->dlsap_sel);
  454. if(self->service_type & IRCOMM_3_WIRE_RAW)
  455. seq_printf(seq, " 3-wire-raw");
  456. if(self->service_type & IRCOMM_3_WIRE)
  457. seq_printf(seq, " 3-wire");
  458. if(self->service_type & IRCOMM_9_WIRE)
  459. seq_printf(seq, " 9-wire");
  460. if(self->service_type & IRCOMM_CENTRONICS)
  461. seq_printf(seq, " Centronics");
  462. seq_putc(seq, '\n');
  463. return 0;
  464. }
  465. static const struct seq_operations ircomm_seq_ops = {
  466. .start = ircomm_seq_start,
  467. .next = ircomm_seq_next,
  468. .stop = ircomm_seq_stop,
  469. .show = ircomm_seq_show,
  470. };
  471. static int ircomm_seq_open(struct inode *inode, struct file *file)
  472. {
  473. return seq_open(file, &ircomm_seq_ops);
  474. }
  475. #endif /* CONFIG_PROC_FS */
  476. MODULE_AUTHOR("Dag Brattli <dag@brattli.net>");
  477. MODULE_DESCRIPTION("IrCOMM protocol");
  478. MODULE_LICENSE("GPL");
  479. module_init(ircomm_init);
  480. module_exit(ircomm_cleanup);