irda_device.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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->netdev_ops->ndo_do_ioctl) {
  130. IRDA_ERROR("%s: do_ioctl not impl. by device driver\n",
  131. __func__);
  132. return -1;
  133. }
  134. ret = (dev->netdev_ops->ndo_do_ioctl)(dev, (struct ifreq *) &req,
  135. SIOCGRECEIVING);
  136. if (ret < 0)
  137. return ret;
  138. return req.ifr_receiving;
  139. }
  140. static void __irda_task_delete(struct irda_task *task)
  141. {
  142. del_timer(&task->timer);
  143. kfree(task);
  144. }
  145. static void irda_task_delete(struct irda_task *task)
  146. {
  147. /* Unregister task */
  148. hashbin_remove(tasks, (long) task, NULL);
  149. __irda_task_delete(task);
  150. }
  151. /*
  152. * Function irda_task_kick (task)
  153. *
  154. * Tries to execute a task possible multiple times until the task is either
  155. * finished, or askes for a timeout. When a task is finished, we do post
  156. * processing, and notify the parent task, that is waiting for this task
  157. * to complete.
  158. */
  159. static int irda_task_kick(struct irda_task *task)
  160. {
  161. int finished = TRUE;
  162. int count = 0;
  163. int timeout;
  164. IRDA_DEBUG(2, "%s()\n", __func__);
  165. IRDA_ASSERT(task != NULL, return -1;);
  166. IRDA_ASSERT(task->magic == IRDA_TASK_MAGIC, return -1;);
  167. /* Execute task until it's finished, or askes for a timeout */
  168. do {
  169. timeout = task->function(task);
  170. if (count++ > 100) {
  171. IRDA_ERROR("%s: error in task handler!\n",
  172. __func__);
  173. irda_task_delete(task);
  174. return TRUE;
  175. }
  176. } while ((timeout == 0) && (task->state != IRDA_TASK_DONE));
  177. if (timeout < 0) {
  178. IRDA_ERROR("%s: Error executing task!\n", __func__);
  179. irda_task_delete(task);
  180. return TRUE;
  181. }
  182. /* Check if we are finished */
  183. if (task->state == IRDA_TASK_DONE) {
  184. del_timer(&task->timer);
  185. /* Do post processing */
  186. if (task->finished)
  187. task->finished(task);
  188. /* Notify parent */
  189. if (task->parent) {
  190. /* Check if parent is waiting for us to complete */
  191. if (task->parent->state == IRDA_TASK_CHILD_WAIT) {
  192. task->parent->state = IRDA_TASK_CHILD_DONE;
  193. /* Stop timer now that we are here */
  194. del_timer(&task->parent->timer);
  195. /* Kick parent task */
  196. irda_task_kick(task->parent);
  197. }
  198. }
  199. irda_task_delete(task);
  200. } else if (timeout > 0) {
  201. irda_start_timer(&task->timer, timeout, (void *) task,
  202. irda_task_timer_expired);
  203. finished = FALSE;
  204. } else {
  205. IRDA_DEBUG(0, "%s(), not finished, and no timeout!\n",
  206. __func__);
  207. finished = FALSE;
  208. }
  209. return finished;
  210. }
  211. /*
  212. * Function irda_task_timer_expired (data)
  213. *
  214. * Task time has expired. We now try to execute task (again), and restart
  215. * the timer if the task has not finished yet
  216. */
  217. static void irda_task_timer_expired(void *data)
  218. {
  219. struct irda_task *task;
  220. IRDA_DEBUG(2, "%s()\n", __func__);
  221. task = (struct irda_task *) data;
  222. irda_task_kick(task);
  223. }
  224. /*
  225. * Function irda_device_setup (dev)
  226. *
  227. * This function should be used by low level device drivers in a similar way
  228. * as ether_setup() is used by normal network device drivers
  229. */
  230. static void irda_device_setup(struct net_device *dev)
  231. {
  232. dev->hard_header_len = 0;
  233. dev->addr_len = LAP_ALEN;
  234. dev->type = ARPHRD_IRDA;
  235. dev->tx_queue_len = 8; /* Window size + 1 s-frame */
  236. memset(dev->broadcast, 0xff, LAP_ALEN);
  237. dev->mtu = 2048;
  238. dev->flags = IFF_NOARP;
  239. }
  240. /*
  241. * Funciton alloc_irdadev
  242. * Allocates and sets up an IRDA device in a manner similar to
  243. * alloc_etherdev.
  244. */
  245. struct net_device *alloc_irdadev(int sizeof_priv)
  246. {
  247. return alloc_netdev(sizeof_priv, "irda%d", irda_device_setup);
  248. }
  249. EXPORT_SYMBOL(alloc_irdadev);
  250. #ifdef CONFIG_ISA_DMA_API
  251. /*
  252. * Function setup_dma (idev, buffer, count, mode)
  253. *
  254. * Setup the DMA channel. Commonly used by LPC FIR drivers
  255. *
  256. */
  257. void irda_setup_dma(int channel, dma_addr_t buffer, int count, int mode)
  258. {
  259. unsigned long flags;
  260. flags = claim_dma_lock();
  261. disable_dma(channel);
  262. clear_dma_ff(channel);
  263. set_dma_mode(channel, mode);
  264. set_dma_addr(channel, buffer);
  265. set_dma_count(channel, count);
  266. enable_dma(channel);
  267. release_dma_lock(flags);
  268. }
  269. EXPORT_SYMBOL(irda_setup_dma);
  270. #endif