irda_device.c 12 KB

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