irda_device.c 8.0 KB

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