cycx_main.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:10k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2. * cycx_main.c Cyclades Cyclom 2X WAN Link Driver. Main module.
  3. *
  4. * Author: Arnaldo Carvalho de Melo <acme@conectiva.com.br>
  5. *
  6. * Copyright: (c) 1998-2001 Arnaldo Carvalho de Melo
  7. *
  8. * Based on sdlamain.c by Gene Kozin <genek@compuserve.com> &
  9. *  Jaspreet Singh <jaspreet@sangoma.com>
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version
  14. * 2 of the License, or (at your option) any later version.
  15. * ============================================================================
  16. * 2001/05/09 acme Fix MODULE_DESC for debug, .bss nitpicks,
  17. *  some cleanups
  18. * 2000/07/13 acme remove useless #ifdef MODULE and crap
  19. * #if KERNEL_VERSION > blah
  20. * 2000/07/06 acme __exit at cyclomx_cleanup
  21. * 2000/04/02 acme dprintk and cycx_debug
  22. *  module_init/module_exit
  23. * 2000/01/21 acme rename cyclomx_open to cyclomx_mod_inc_use_count
  24. * and cyclomx_close to cyclomx_mod_dec_use_count
  25. * 2000/01/08 acme cleanup
  26. * 1999/11/06 acme cycx_down back to life (it needs to be
  27. * called to iounmap the dpmbase)
  28. * 1999/08/09 acme removed references to enable_tx_int
  29. * use spinlocks instead of cli/sti in
  30. * cyclomx_set_state
  31. * 1999/05/19 acme works directly linked into the kernel
  32. * init_waitqueue_head for 2.3.* kernel
  33. * 1999/05/18 acme major cleanup (polling not needed), etc
  34. * 1998/08/28 acme minor cleanup (ioctls for firmware deleted)
  35. * queue_task activated
  36. * 1998/08/08 acme Initial version.
  37. */
  38. #include <linux/config.h> /* OS configuration options */
  39. #include <linux/stddef.h> /* offsetof(), etc. */
  40. #include <linux/errno.h> /* return codes */
  41. #include <linux/string.h> /* inline memset(), etc. */
  42. #include <linux/slab.h> /* kmalloc(), kfree() */
  43. #include <linux/kernel.h> /* printk(), and other useful stuff */
  44. #include <linux/module.h> /* support for loadable modules */
  45. #include <linux/ioport.h> /* request_region(), release_region() */
  46. #include <linux/tqueue.h> /* for kernel task queues */
  47. #include <linux/wanrouter.h> /* WAN router definitions */
  48. #include <linux/cyclomx.h> /* cyclomx common user API definitions */
  49. #include <asm/uaccess.h> /* kernel <-> user copy */
  50. #include <linux/init.h>         /* __init (when not using as a module) */
  51. /* Debug */
  52. unsigned int cycx_debug;
  53. MODULE_AUTHOR("Arnaldo Carvalho de Melo");
  54. MODULE_DESCRIPTION("Cyclom 2X Sync Card Driver.");
  55. MODULE_LICENSE("GPL");
  56. MODULE_PARM(cycx_debug, "i");
  57. MODULE_PARM_DESC(cycx_debug, "cyclomx debug level");
  58. /* Defines & Macros */
  59. #define DRV_VERSION 0 /* version number */
  60. #define DRV_RELEASE 10 /* release (minor version) number */
  61. #define MAX_CARDS 1 /* max number of adapters */
  62. #define CONFIG_CYCLOMX_CARDS 1
  63. /* Function Prototypes */
  64. /* WAN link driver entry points */
  65. static int setup (wan_device_t *wandev, wandev_conf_t *conf);
  66. static int shutdown (wan_device_t *wandev);
  67. static int ioctl (wan_device_t *wandev, unsigned cmd, unsigned long arg);
  68. /* Miscellaneous functions */
  69. static void cycx_isr (int irq, void *dev_id, struct pt_regs *regs);
  70. /* Global Data
  71.  * Note: All data must be explicitly initialized!!!
  72.  */
  73. /* private data */
  74. static char drvname[] = "cyclomx";
  75. static char fullname[] = "CYCLOM 2X(tm) Sync Card Driver";
  76. static char copyright[] = "(c) 1998-2001 Arnaldo Carvalho de Melo "
  77.   "<acme@conectiva.com.br>";
  78. static int ncards = CONFIG_CYCLOMX_CARDS;
  79. static cycx_t *card_array; /* adapter data space */
  80. /* Kernel Loadable Module Entry Points */
  81. /*
  82.  * Module 'insert' entry point.
  83.  * o print announcement
  84.  * o allocate adapter data space
  85.  * o initialize static data
  86.  * o register all cards with WAN router
  87.  * o calibrate Cyclom 2X shared memory access delay.
  88.  *
  89.  * Return: 0 Ok
  90.  * < 0 error.
  91.  * Context: process
  92.  */
  93. int __init cyclomx_init (void)
  94. {
  95. int cnt, err = -ENOMEM;
  96. printk(KERN_INFO "%s v%u.%u %sn",
  97. fullname, DRV_VERSION, DRV_RELEASE, copyright);
  98. /* Verify number of cards and allocate adapter data space */
  99. ncards = min_t(int, ncards, MAX_CARDS);
  100. ncards = max_t(int, ncards, 1);
  101. card_array = kmalloc(sizeof(cycx_t) * ncards, GFP_KERNEL);
  102. if (!card_array)
  103. goto out;
  104. memset(card_array, 0, sizeof(cycx_t) * ncards);
  105. /* Register adapters with WAN router */
  106. for (cnt = 0; cnt < ncards; ++cnt) {
  107. cycx_t *card = &card_array[cnt];
  108. wan_device_t *wandev = &card->wandev;
  109. sprintf(card->devname, "%s%d", drvname, cnt + 1);
  110. wandev->magic    = ROUTER_MAGIC;
  111. wandev->name     = card->devname;
  112. wandev->private  = card;
  113. wandev->setup    = setup;
  114. wandev->shutdown = shutdown;
  115. wandev->ioctl    = ioctl;
  116. err = register_wan_device(wandev);
  117. if (err) {
  118. printk(KERN_ERR "%s: %s registration failed with "
  119. "error %d!n",
  120. drvname, card->devname, err);
  121. break;
  122. }
  123. }
  124. err = -ENODEV;
  125. if (!cnt) {
  126. kfree(card_array);
  127. goto out;
  128. }
  129. err = 0;
  130. ncards = cnt; /* adjust actual number of cards */
  131. out: return err;
  132. }
  133. /*
  134.  * Module 'remove' entry point.
  135.  * o unregister all adapters from the WAN router
  136.  * o release all remaining system resources
  137.  */
  138. static void __exit cyclomx_cleanup (void)
  139. {
  140. int i = 0;
  141. for (; i < ncards; ++i) {
  142. cycx_t *card = &card_array[i];
  143. unregister_wan_device(card->devname);
  144. }
  145. kfree(card_array);
  146. }
  147. /* WAN Device Driver Entry Points */
  148. /*
  149.  * Setup/configure WAN link driver.
  150.  * o check adapter state
  151.  * o make sure firmware is present in configuration
  152.  * o allocate interrupt vector
  153.  * o setup Cyclom 2X hardware
  154.  * o call appropriate routine to perform protocol-specific initialization
  155.  *
  156.  * This function is called when router handles ROUTER_SETUP IOCTL. The
  157.  * configuration structure is in kernel memory (including extended data, if
  158.  * any).
  159.  */
  160. static int setup (wan_device_t *wandev, wandev_conf_t *conf)
  161. {
  162. int err = -EFAULT;
  163. cycx_t *card;
  164. int irq;
  165. /* Sanity checks */
  166. if (!wandev || !wandev->private || !conf)
  167. goto out;
  168. card = wandev->private;
  169. err = -EBUSY;
  170. if (wandev->state != WAN_UNCONFIGURED)
  171. goto out;
  172. err = -EINVAL;
  173. if (!conf->data_size || !conf->data) {
  174. printk(KERN_ERR "%s: firmware not found in configuration "
  175. "data!n", wandev->name);
  176. goto out;
  177. }
  178. if (conf->irq <= 0) {
  179. printk(KERN_ERR "%s: can't configure without IRQ!n",
  180. wandev->name);
  181. goto out;
  182. }
  183. /* Allocate IRQ */
  184. irq = conf->irq == 2 ? 9 : conf->irq; /* IRQ2 -> IRQ9 */
  185. if (request_irq(irq, cycx_isr, 0, wandev->name, card)) {
  186. printk(KERN_ERR "%s: can't reserve IRQ %d!n",
  187. wandev->name, irq);
  188. goto out;
  189. }
  190. /* Configure hardware, load firmware, etc. */
  191. memset(&card->hw, 0, sizeof(cycxhw_t));
  192. card->hw.irq  = irq;
  193. card->hw.dpmbase = conf->maddr;
  194. card->hw.dpmsize = CYCX_WINDOWSIZE;
  195. card->hw.fwid  = CFID_X25_2X;
  196. card->lock  = SPIN_LOCK_UNLOCKED;
  197. init_waitqueue_head(&card->wait_stats);
  198. err = cycx_setup(&card->hw, conf->data, conf->data_size);
  199. if (err)
  200. goto out_irq;
  201. /* Initialize WAN device data space */
  202. wandev->irq       = irq;
  203. wandev->dma       = wandev->ioport = 0;
  204. wandev->maddr     = card->hw.dpmbase;
  205. wandev->msize     = card->hw.dpmsize;
  206. wandev->hw_opt[2] = 0;
  207. wandev->hw_opt[3] = card->hw.fwid;
  208. /* Protocol-specific initialization */
  209. switch (card->hw.fwid) {
  210. #ifdef CONFIG_CYCLOMX_X25
  211. case CFID_X25_2X:
  212. err = cyx_init(card, conf);
  213. break;
  214. #endif
  215. default:
  216. printk(KERN_ERR "%s: this firmware is not supported!n",
  217. wandev->name);
  218. err = -EINVAL;
  219. }
  220. if (err) {
  221. cycx_down(&card->hw);
  222. goto out_irq;
  223. }
  224. err = 0;
  225. out: return err;
  226. out_irq:
  227. free_irq(irq, card);
  228. goto out;
  229. }
  230. /*
  231.  * Shut down WAN link driver. 
  232.  * o shut down adapter hardware
  233.  * o release system resources.
  234.  *
  235.  * This function is called by the router when device is being unregistered or
  236.  * when it handles ROUTER_DOWN IOCTL.
  237.  */
  238. static int shutdown (wan_device_t *wandev)
  239. {
  240. int ret = -EFAULT;
  241. cycx_t *card;
  242. /* sanity checks */
  243. if (!wandev || !wandev->private)
  244. goto out;
  245. ret = 0;
  246. if (wandev->state == WAN_UNCONFIGURED)
  247. goto out;
  248. card = wandev->private;
  249. wandev->state = WAN_UNCONFIGURED;
  250. cycx_down(&card->hw);
  251. printk(KERN_INFO "%s: irq %d being freed!n", wandev->name,
  252. wandev->irq);
  253. free_irq(wandev->irq, card);
  254. out: return ret;
  255. }
  256. /*
  257.  * Driver I/O control. 
  258.  * o verify arguments
  259.  * o perform requested action
  260.  *
  261.  * This function is called when router handles one of the reserved user
  262.  * IOCTLs.  Note that 'arg' still points to user address space.
  263.  *
  264.  * no reserved ioctls for the cyclom 2x up to now
  265.  */
  266. static int ioctl (wan_device_t *wandev, unsigned cmd, unsigned long arg)
  267. {
  268. return -EINVAL;
  269. }
  270. /* Miscellaneous */
  271. /*
  272.  * Cyclom 2X Interrupt Service Routine.
  273.  * o acknowledge Cyclom 2X hardware interrupt.
  274.  * o call protocol-specific interrupt service routine, if any.
  275.  */
  276. static void cycx_isr (int irq, void *dev_id, struct pt_regs *regs)
  277. {
  278. cycx_t *card = (cycx_t *)dev_id;
  279. if (!card || card->wandev.state == WAN_UNCONFIGURED)
  280. goto out;
  281. if (card->in_isr) {
  282. printk(KERN_WARNING "%s: interrupt re-entrancy on IRQ %d!n",
  283.     card->devname, card->wandev.irq);
  284. goto out;
  285. }
  286. if (card->isr)
  287. card->isr(card);
  288. out: return;
  289. }
  290. /*
  291.  * This routine is called by the protocol-specific modules when network
  292.  * interface is being open.  The only reason we need this, is because we
  293.  * have to call MOD_INC_USE_COUNT, but cannot include 'module.h' where it's
  294.  * defined more than once into the same kernel module.
  295.  */
  296. void cyclomx_mod_inc_use_count (cycx_t *card)
  297. {
  298. ++card->open_cnt;
  299. MOD_INC_USE_COUNT;
  300. }
  301. /*
  302.  * This routine is called by the protocol-specific modules when network
  303.  * interface is being closed.  The only reason we need this, is because we
  304.  * have to call MOD_DEC_USE_COUNT, but cannot include 'module.h' where it's
  305.  * defined more than once into the same kernel module.
  306.  */
  307. void cyclomx_mod_dec_use_count (cycx_t *card)
  308. {
  309. --card->open_cnt;
  310. MOD_DEC_USE_COUNT;
  311. }
  312. /* Set WAN device state.  */
  313. void cyclomx_set_state (cycx_t *card, int state)
  314. {
  315. unsigned long flags;
  316. char *string_state = NULL;
  317. spin_lock_irqsave(&card->lock, flags);
  318. if (card->wandev.state != state) {
  319. switch (state) {
  320. case WAN_CONNECTED:
  321. string_state = "connected!";
  322. break;
  323. case WAN_DISCONNECTED:
  324. string_state = "disconnected!";
  325. break;
  326. }
  327. printk(KERN_INFO "%s: link %sn", card->devname, string_state);
  328. card->wandev.state = state;
  329. }
  330. card->state_tick = jiffies;
  331. spin_unlock_irqrestore(&card->lock, flags);
  332. }
  333. module_init(cyclomx_init);
  334. module_exit(cyclomx_cleanup);