irda_device.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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/config.h>
  32. #include <linux/string.h>
  33. #include <linux/proc_fs.h>
  34. #include <linux/skbuff.h>
  35. #include <linux/capability.h>
  36. #include <linux/if.h>
  37. #include <linux/if_ether.h>
  38. #include <linux/if_arp.h>
  39. #include <linux/netdevice.h>
  40. #include <linux/init.h>
  41. #include <linux/tty.h>
  42. #include <linux/kmod.h>
  43. #include <linux/spinlock.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. #ifdef CONFIG_IRDA_DEBUG
  56. static const char *task_state[] = {
  57. "IRDA_TASK_INIT",
  58. "IRDA_TASK_DONE",
  59. "IRDA_TASK_WAIT",
  60. "IRDA_TASK_WAIT1",
  61. "IRDA_TASK_WAIT2",
  62. "IRDA_TASK_WAIT3",
  63. "IRDA_TASK_CHILD_INIT",
  64. "IRDA_TASK_CHILD_WAIT",
  65. "IRDA_TASK_CHILD_DONE",
  66. };
  67. #endif /* CONFIG_IRDA_DEBUG */
  68. static void irda_task_timer_expired(void *data);
  69. int __init irda_device_init( void)
  70. {
  71. dongles = hashbin_new(HB_NOLOCK);
  72. if (dongles == NULL) {
  73. IRDA_WARNING("IrDA: Can't allocate dongles hashbin!\n");
  74. return -ENOMEM;
  75. }
  76. spin_lock_init(&dongles->hb_spinlock);
  77. tasks = hashbin_new(HB_LOCK);
  78. if (tasks == NULL) {
  79. IRDA_WARNING("IrDA: Can't allocate tasks hashbin!\n");
  80. hashbin_delete(dongles, NULL);
  81. return -ENOMEM;
  82. }
  83. /* We no longer initialise the driver ourselves here, we let
  84. * the system do it for us... - Jean II */
  85. return 0;
  86. }
  87. static void __exit leftover_dongle(void *arg)
  88. {
  89. struct dongle_reg *reg = arg;
  90. IRDA_WARNING("IrDA: Dongle type %x not unregistered\n",
  91. reg->type);
  92. }
  93. void __exit irda_device_cleanup(void)
  94. {
  95. IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
  96. hashbin_delete(tasks, (FREE_FUNC) __irda_task_delete);
  97. hashbin_delete(dongles, leftover_dongle);
  98. }
  99. /*
  100. * Function irda_device_set_media_busy (self, status)
  101. *
  102. * Called when we have detected that another station is transmitting
  103. * in contention mode.
  104. */
  105. void irda_device_set_media_busy(struct net_device *dev, int status)
  106. {
  107. struct irlap_cb *self;
  108. IRDA_DEBUG(4, "%s(%s)\n", __FUNCTION__, status ? "TRUE" : "FALSE");
  109. self = (struct irlap_cb *) dev->atalk_ptr;
  110. /* Some drivers may enable the receive interrupt before calling
  111. * irlap_open(), or they may disable the receive interrupt
  112. * after calling irlap_close().
  113. * The IrDA stack is protected from this in irlap_driver_rcv().
  114. * However, the driver calls directly the wrapper, that calls
  115. * us directly. Make sure we protect ourselves.
  116. * Jean II */
  117. if (!self || self->magic != LAP_MAGIC)
  118. return;
  119. if (status) {
  120. self->media_busy = TRUE;
  121. if (status == SMALL)
  122. irlap_start_mbusy_timer(self, SMALLBUSY_TIMEOUT);
  123. else
  124. irlap_start_mbusy_timer(self, MEDIABUSY_TIMEOUT);
  125. IRDA_DEBUG( 4, "Media busy!\n");
  126. } else {
  127. self->media_busy = FALSE;
  128. irlap_stop_mbusy_timer(self);
  129. }
  130. }
  131. EXPORT_SYMBOL(irda_device_set_media_busy);
  132. /*
  133. * Function irda_device_is_receiving (dev)
  134. *
  135. * Check if the device driver is currently receiving data
  136. *
  137. */
  138. int irda_device_is_receiving(struct net_device *dev)
  139. {
  140. struct if_irda_req req;
  141. int ret;
  142. IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
  143. if (!dev->do_ioctl) {
  144. IRDA_ERROR("%s: do_ioctl not impl. by device driver\n",
  145. __FUNCTION__);
  146. return -1;
  147. }
  148. ret = dev->do_ioctl(dev, (struct ifreq *) &req, SIOCGRECEIVING);
  149. if (ret < 0)
  150. return ret;
  151. return req.ifr_receiving;
  152. }
  153. void irda_task_next_state(struct irda_task *task, IRDA_TASK_STATE state)
  154. {
  155. IRDA_DEBUG(2, "%s(), state = %s\n", __FUNCTION__, task_state[state]);
  156. task->state = state;
  157. }
  158. EXPORT_SYMBOL(irda_task_next_state);
  159. static void __irda_task_delete(struct irda_task *task)
  160. {
  161. del_timer(&task->timer);
  162. kfree(task);
  163. }
  164. void irda_task_delete(struct irda_task *task)
  165. {
  166. /* Unregister task */
  167. hashbin_remove(tasks, (long) task, NULL);
  168. __irda_task_delete(task);
  169. }
  170. EXPORT_SYMBOL(irda_task_delete);
  171. /*
  172. * Function irda_task_kick (task)
  173. *
  174. * Tries to execute a task possible multiple times until the task is either
  175. * finished, or askes for a timeout. When a task is finished, we do post
  176. * processing, and notify the parent task, that is waiting for this task
  177. * to complete.
  178. */
  179. static int irda_task_kick(struct irda_task *task)
  180. {
  181. int finished = TRUE;
  182. int count = 0;
  183. int timeout;
  184. IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
  185. IRDA_ASSERT(task != NULL, return -1;);
  186. IRDA_ASSERT(task->magic == IRDA_TASK_MAGIC, return -1;);
  187. /* Execute task until it's finished, or askes for a timeout */
  188. do {
  189. timeout = task->function(task);
  190. if (count++ > 100) {
  191. IRDA_ERROR("%s: error in task handler!\n",
  192. __FUNCTION__);
  193. irda_task_delete(task);
  194. return TRUE;
  195. }
  196. } while ((timeout == 0) && (task->state != IRDA_TASK_DONE));
  197. if (timeout < 0) {
  198. IRDA_ERROR("%s: Error executing task!\n", __FUNCTION__);
  199. irda_task_delete(task);
  200. return TRUE;
  201. }
  202. /* Check if we are finished */
  203. if (task->state == IRDA_TASK_DONE) {
  204. del_timer(&task->timer);
  205. /* Do post processing */
  206. if (task->finished)
  207. task->finished(task);
  208. /* Notify parent */
  209. if (task->parent) {
  210. /* Check if parent is waiting for us to complete */
  211. if (task->parent->state == IRDA_TASK_CHILD_WAIT) {
  212. task->parent->state = IRDA_TASK_CHILD_DONE;
  213. /* Stop timer now that we are here */
  214. del_timer(&task->parent->timer);
  215. /* Kick parent task */
  216. irda_task_kick(task->parent);
  217. }
  218. }
  219. irda_task_delete(task);
  220. } else if (timeout > 0) {
  221. irda_start_timer(&task->timer, timeout, (void *) task,
  222. irda_task_timer_expired);
  223. finished = FALSE;
  224. } else {
  225. IRDA_DEBUG(0, "%s(), not finished, and no timeout!\n",
  226. __FUNCTION__);
  227. finished = FALSE;
  228. }
  229. return finished;
  230. }
  231. /*
  232. * Function irda_task_execute (instance, function, finished)
  233. *
  234. * This function registers and tries to execute tasks that may take some
  235. * time to complete. We do it this hairy way since we may have been
  236. * called from interrupt context, so it's not possible to use
  237. * schedule_timeout()
  238. * Two important notes :
  239. * o Make sure you irda_task_delete(task); in case you delete the
  240. * calling instance.
  241. * o No real need to lock when calling this function, but you may
  242. * want to lock within the task handler.
  243. * Jean II
  244. */
  245. struct irda_task *irda_task_execute(void *instance,
  246. IRDA_TASK_CALLBACK function,
  247. IRDA_TASK_CALLBACK finished,
  248. struct irda_task *parent, void *param)
  249. {
  250. struct irda_task *task;
  251. IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
  252. task = kmalloc(sizeof(struct irda_task), GFP_ATOMIC);
  253. if (!task)
  254. return NULL;
  255. task->state = IRDA_TASK_INIT;
  256. task->instance = instance;
  257. task->function = function;
  258. task->finished = finished;
  259. task->parent = parent;
  260. task->param = param;
  261. task->magic = IRDA_TASK_MAGIC;
  262. init_timer(&task->timer);
  263. /* Register task */
  264. hashbin_insert(tasks, (irda_queue_t *) task, (long) task, NULL);
  265. /* No time to waste, so lets get going! */
  266. return irda_task_kick(task) ? NULL : task;
  267. }
  268. EXPORT_SYMBOL(irda_task_execute);
  269. /*
  270. * Function irda_task_timer_expired (data)
  271. *
  272. * Task time has expired. We now try to execute task (again), and restart
  273. * the timer if the task has not finished yet
  274. */
  275. static void irda_task_timer_expired(void *data)
  276. {
  277. struct irda_task *task;
  278. IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
  279. task = (struct irda_task *) data;
  280. irda_task_kick(task);
  281. }
  282. /*
  283. * Function irda_device_setup (dev)
  284. *
  285. * This function should be used by low level device drivers in a similar way
  286. * as ether_setup() is used by normal network device drivers
  287. */
  288. static void irda_device_setup(struct net_device *dev)
  289. {
  290. dev->hard_header_len = 0;
  291. dev->addr_len = 0;
  292. dev->type = ARPHRD_IRDA;
  293. dev->tx_queue_len = 8; /* Window size + 1 s-frame */
  294. memset(dev->broadcast, 0xff, 4);
  295. dev->mtu = 2048;
  296. dev->flags = IFF_NOARP;
  297. }
  298. /*
  299. * Funciton alloc_irdadev
  300. * Allocates and sets up an IRDA device in a manner similar to
  301. * alloc_etherdev.
  302. */
  303. struct net_device *alloc_irdadev(int sizeof_priv)
  304. {
  305. return alloc_netdev(sizeof_priv, "irda%d", irda_device_setup);
  306. }
  307. EXPORT_SYMBOL(alloc_irdadev);
  308. /*
  309. * Function irda_device_init_dongle (self, type, qos)
  310. *
  311. * Initialize attached dongle.
  312. *
  313. * Important : request_module require us to call this function with
  314. * a process context and irq enabled. - Jean II
  315. */
  316. dongle_t *irda_device_dongle_init(struct net_device *dev, int type)
  317. {
  318. struct dongle_reg *reg;
  319. dongle_t *dongle = NULL;
  320. might_sleep();
  321. spin_lock(&dongles->hb_spinlock);
  322. reg = hashbin_find(dongles, type, NULL);
  323. #ifdef CONFIG_KMOD
  324. /* Try to load the module needed */
  325. if (!reg && capable(CAP_SYS_MODULE)) {
  326. spin_unlock(&dongles->hb_spinlock);
  327. request_module("irda-dongle-%d", type);
  328. spin_lock(&dongles->hb_spinlock);
  329. reg = hashbin_find(dongles, type, NULL);
  330. }
  331. #endif
  332. if (!reg || !try_module_get(reg->owner) ) {
  333. IRDA_ERROR("IrDA: Unable to find requested dongle type %x\n",
  334. type);
  335. goto out;
  336. }
  337. /* Allocate dongle info for this instance */
  338. dongle = kmalloc(sizeof(dongle_t), GFP_KERNEL);
  339. if (!dongle)
  340. goto out;
  341. memset(dongle, 0, sizeof(dongle_t));
  342. /* Bind the registration info to this particular instance */
  343. dongle->issue = reg;
  344. dongle->dev = dev;
  345. out:
  346. spin_unlock(&dongles->hb_spinlock);
  347. return dongle;
  348. }
  349. EXPORT_SYMBOL(irda_device_dongle_init);
  350. /*
  351. * Function irda_device_dongle_cleanup (dongle)
  352. */
  353. int irda_device_dongle_cleanup(dongle_t *dongle)
  354. {
  355. IRDA_ASSERT(dongle != NULL, return -1;);
  356. dongle->issue->close(dongle);
  357. module_put(dongle->issue->owner);
  358. kfree(dongle);
  359. return 0;
  360. }
  361. EXPORT_SYMBOL(irda_device_dongle_cleanup);
  362. /*
  363. * Function irda_device_register_dongle (dongle)
  364. */
  365. int irda_device_register_dongle(struct dongle_reg *new)
  366. {
  367. spin_lock(&dongles->hb_spinlock);
  368. /* Check if this dongle has been registered before */
  369. if (hashbin_find(dongles, new->type, NULL)) {
  370. IRDA_MESSAGE("%s: Dongle type %x already registered\n",
  371. __FUNCTION__, new->type);
  372. } else {
  373. /* Insert IrDA dongle into hashbin */
  374. hashbin_insert(dongles, (irda_queue_t *) new, new->type, NULL);
  375. }
  376. spin_unlock(&dongles->hb_spinlock);
  377. return 0;
  378. }
  379. EXPORT_SYMBOL(irda_device_register_dongle);
  380. /*
  381. * Function irda_device_unregister_dongle (dongle)
  382. *
  383. * Unregister dongle, and remove dongle from list of registered dongles
  384. *
  385. */
  386. void irda_device_unregister_dongle(struct dongle_reg *dongle)
  387. {
  388. struct dongle *node;
  389. spin_lock(&dongles->hb_spinlock);
  390. node = hashbin_remove(dongles, dongle->type, NULL);
  391. if (!node)
  392. IRDA_ERROR("%s: dongle not found!\n", __FUNCTION__);
  393. spin_unlock(&dongles->hb_spinlock);
  394. }
  395. EXPORT_SYMBOL(irda_device_unregister_dongle);
  396. #ifdef CONFIG_ISA_DMA_API
  397. /*
  398. * Function setup_dma (idev, buffer, count, mode)
  399. *
  400. * Setup the DMA channel. Commonly used by LPC FIR drivers
  401. *
  402. */
  403. void irda_setup_dma(int channel, dma_addr_t buffer, int count, int mode)
  404. {
  405. unsigned long flags;
  406. flags = claim_dma_lock();
  407. disable_dma(channel);
  408. clear_dma_ff(channel);
  409. set_dma_mode(channel, mode);
  410. set_dma_addr(channel, buffer);
  411. set_dma_count(channel, count);
  412. enable_dma(channel);
  413. release_dma_lock(flags);
  414. }
  415. EXPORT_SYMBOL(irda_setup_dma);
  416. #endif