irda_device.c 7.9 KB

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