ircomm_core.c 14 KB

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