ircomm_core.c 14 KB

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