irda_device.c 8.0 KB

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