irda_device.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*********************************************************************
  2. *
  3. * Filename: irda_device.c
  4. * Version: 0.9
  5. * Description: Utility functions used by the device drivers
  6. * Status: Experimental.
  7. * Author: Dag Brattli <dagb@cs.uit.no>
  8. * Created at: Sat Oct 9 09:22:27 1999
  9. * Modified at: Sun Jan 23 17:41:24 2000
  10. * Modified by: Dag Brattli <dagb@cs.uit.no>
  11. *
  12. * Copyright (c) 1999-2000 Dag Brattli, All Rights Reserved.
  13. * Copyright (c) 2000-2001 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/string.h>
  32. #include <linux/proc_fs.h>
  33. #include <linux/skbuff.h>
  34. #include <linux/capability.h>
  35. #include <linux/if.h>
  36. #include <linux/if_ether.h>
  37. #include <linux/if_arp.h>
  38. #include <linux/netdevice.h>
  39. #include <linux/init.h>
  40. #include <linux/tty.h>
  41. #include <linux/kmod.h>
  42. #include <linux/spinlock.h>
  43. #include <asm/ioctls.h>
  44. #include <asm/uaccess.h>
  45. #include <asm/dma.h>
  46. #include <asm/io.h>
  47. #include <net/irda/irda_device.h>
  48. #include <net/irda/irlap.h>
  49. #include <net/irda/timer.h>
  50. #include <net/irda/wrapper.h>
  51. static void __irda_task_delete(struct irda_task *task);
  52. static hashbin_t *dongles = NULL;
  53. static hashbin_t *tasks = NULL;
  54. static void irda_task_timer_expired(void *data);
  55. int __init irda_device_init( void)
  56. {
  57. dongles = hashbin_new(HB_NOLOCK);
  58. if (dongles == NULL) {
  59. IRDA_WARNING("IrDA: Can't allocate dongles hashbin!\n");
  60. return -ENOMEM;
  61. }
  62. spin_lock_init(&dongles->hb_spinlock);
  63. tasks = hashbin_new(HB_LOCK);
  64. if (tasks == NULL) {
  65. IRDA_WARNING("IrDA: Can't allocate tasks hashbin!\n");
  66. hashbin_delete(dongles, NULL);
  67. return -ENOMEM;
  68. }
  69. /* We no longer initialise the driver ourselves here, we let
  70. * the system do it for us... - Jean II */
  71. return 0;
  72. }
  73. static void leftover_dongle(void *arg)
  74. {
  75. struct dongle_reg *reg = arg;
  76. IRDA_WARNING("IrDA: Dongle type %x not unregistered\n",
  77. reg->type);
  78. }
  79. void irda_device_cleanup(void)
  80. {
  81. IRDA_DEBUG(4, "%s()\n", __func__);
  82. hashbin_delete(tasks, (FREE_FUNC) __irda_task_delete);
  83. hashbin_delete(dongles, leftover_dongle);
  84. }
  85. /*
  86. * Function irda_device_set_media_busy (self, status)
  87. *
  88. * Called when we have detected that another station is transmitting
  89. * in contention mode.
  90. */
  91. void irda_device_set_media_busy(struct net_device *dev, int status)
  92. {
  93. struct irlap_cb *self;
  94. IRDA_DEBUG(4, "%s(%s)\n", __func__, status ? "TRUE" : "FALSE");
  95. self = (struct irlap_cb *) dev->atalk_ptr;
  96. /* Some drivers may enable the receive interrupt before calling
  97. * irlap_open(), or they may disable the receive interrupt
  98. * after calling irlap_close().
  99. * The IrDA stack is protected from this in irlap_driver_rcv().
  100. * However, the driver calls directly the wrapper, that calls
  101. * us directly. Make sure we protect ourselves.
  102. * Jean II */
  103. if (!self || self->magic != LAP_MAGIC)
  104. return;
  105. if (status) {
  106. self->media_busy = TRUE;
  107. if (status == SMALL)
  108. irlap_start_mbusy_timer(self, SMALLBUSY_TIMEOUT);
  109. else
  110. irlap_start_mbusy_timer(self, MEDIABUSY_TIMEOUT);
  111. IRDA_DEBUG( 4, "Media busy!\n");
  112. } else {
  113. self->media_busy = FALSE;
  114. irlap_stop_mbusy_timer(self);
  115. }
  116. }
  117. EXPORT_SYMBOL(irda_device_set_media_busy);
  118. /*
  119. * Function irda_device_is_receiving (dev)
  120. *
  121. * Check if the device driver is currently receiving data
  122. *
  123. */
  124. int irda_device_is_receiving(struct net_device *dev)
  125. {
  126. struct if_irda_req req;
  127. int ret;
  128. IRDA_DEBUG(2, "%s()\n", __func__);
  129. if (!dev->do_ioctl) {
  130. IRDA_ERROR("%s: do_ioctl not impl. by device driver\n",
  131. __func__);
  132. return -1;
  133. }
  134. ret = dev->do_ioctl(dev, (struct ifreq *) &req, SIOCGRECEIVING);
  135. if (ret < 0)
  136. return ret;
  137. return req.ifr_receiving;
  138. }
  139. static void __irda_task_delete(struct irda_task *task)
  140. {
  141. del_timer(&task->timer);
  142. kfree(task);
  143. }
  144. static void irda_task_delete(struct irda_task *task)
  145. {
  146. /* Unregister task */
  147. hashbin_remove(tasks, (long) task, NULL);
  148. __irda_task_delete(task);
  149. }
  150. /*
  151. * Function irda_task_kick (task)
  152. *
  153. * Tries to execute a task possible multiple times until the task is either
  154. * finished, or askes for a timeout. When a task is finished, we do post
  155. * processing, and notify the parent task, that is waiting for this task
  156. * to complete.
  157. */
  158. static int irda_task_kick(struct irda_task *task)
  159. {
  160. int finished = TRUE;
  161. int count = 0;
  162. int timeout;
  163. IRDA_DEBUG(2, "%s()\n", __func__);
  164. IRDA_ASSERT(task != NULL, return -1;);
  165. IRDA_ASSERT(task->magic == IRDA_TASK_MAGIC, return -1;);
  166. /* Execute task until it's finished, or askes for a timeout */
  167. do {
  168. timeout = task->function(task);
  169. if (count++ > 100) {
  170. IRDA_ERROR("%s: error in task handler!\n",
  171. __func__);
  172. irda_task_delete(task);
  173. return TRUE;
  174. }
  175. } while ((timeout == 0) && (task->state != IRDA_TASK_DONE));
  176. if (timeout < 0) {
  177. IRDA_ERROR("%s: Error executing task!\n", __func__);
  178. irda_task_delete(task);
  179. return TRUE;
  180. }
  181. /* Check if we are finished */
  182. if (task->state == IRDA_TASK_DONE) {
  183. del_timer(&task->timer);
  184. /* Do post processing */
  185. if (task->finished)
  186. task->finished(task);
  187. /* Notify parent */
  188. if (task->parent) {
  189. /* Check if parent is waiting for us to complete */
  190. if (task->parent->state == IRDA_TASK_CHILD_WAIT) {
  191. task->parent->state = IRDA_TASK_CHILD_DONE;
  192. /* Stop timer now that we are here */
  193. del_timer(&task->parent->timer);
  194. /* Kick parent task */
  195. irda_task_kick(task->parent);
  196. }
  197. }
  198. irda_task_delete(task);
  199. } else if (timeout > 0) {
  200. irda_start_timer(&task->timer, timeout, (void *) task,
  201. irda_task_timer_expired);
  202. finished = FALSE;
  203. } else {
  204. IRDA_DEBUG(0, "%s(), not finished, and no timeout!\n",
  205. __func__);
  206. finished = FALSE;
  207. }
  208. return finished;
  209. }
  210. /*
  211. * Function irda_task_timer_expired (data)
  212. *
  213. * Task time has expired. We now try to execute task (again), and restart
  214. * the timer if the task has not finished yet
  215. */
  216. static void irda_task_timer_expired(void *data)
  217. {
  218. struct irda_task *task;
  219. IRDA_DEBUG(2, "%s()\n", __func__);
  220. task = (struct irda_task *) data;
  221. irda_task_kick(task);
  222. }
  223. /*
  224. * Function irda_device_setup (dev)
  225. *
  226. * This function should be used by low level device drivers in a similar way
  227. * as ether_setup() is used by normal network device drivers
  228. */
  229. static void irda_device_setup(struct net_device *dev)
  230. {
  231. dev->hard_header_len = 0;
  232. dev->addr_len = LAP_ALEN;
  233. dev->type = ARPHRD_IRDA;
  234. dev->tx_queue_len = 8; /* Window size + 1 s-frame */
  235. memset(dev->broadcast, 0xff, LAP_ALEN);
  236. dev->mtu = 2048;
  237. dev->flags = IFF_NOARP;
  238. }
  239. /*
  240. * Funciton alloc_irdadev
  241. * Allocates and sets up an IRDA device in a manner similar to
  242. * alloc_etherdev.
  243. */
  244. struct net_device *alloc_irdadev(int sizeof_priv)
  245. {
  246. return alloc_netdev(sizeof_priv, "irda%d", irda_device_setup);
  247. }
  248. EXPORT_SYMBOL(alloc_irdadev);
  249. #ifdef CONFIG_ISA_DMA_API
  250. /*
  251. * Function setup_dma (idev, buffer, count, mode)
  252. *
  253. * Setup the DMA channel. Commonly used by LPC FIR drivers
  254. *
  255. */
  256. void irda_setup_dma(int channel, dma_addr_t buffer, int count, int mode)
  257. {
  258. unsigned long flags;
  259. flags = claim_dma_lock();
  260. disable_dma(channel);
  261. clear_dma_ff(channel);
  262. set_dma_mode(channel, mode);
  263. set_dma_addr(channel, buffer);
  264. set_dma_count(channel, count);
  265. enable_dma(channel);
  266. release_dma_lock(flags);
  267. }
  268. EXPORT_SYMBOL(irda_setup_dma);
  269. #endif