cycx_main.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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-2003 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. * Please look at the bitkeeper changelog (or any other scm tool that ends up
  17. * importing bitkeeper changelog or that replaces bitkeeper in the future as
  18. * main tool for linux development).
  19. *
  20. * 2001/05/09 acme Fix MODULE_DESC for debug, .bss nitpicks,
  21. * some cleanups
  22. * 2000/07/13 acme remove useless #ifdef MODULE and crap
  23. * #if KERNEL_VERSION > blah
  24. * 2000/07/06 acme __exit at cyclomx_cleanup
  25. * 2000/04/02 acme dprintk and cycx_debug
  26. * module_init/module_exit
  27. * 2000/01/21 acme rename cyclomx_open to cyclomx_mod_inc_use_count
  28. * and cyclomx_close to cyclomx_mod_dec_use_count
  29. * 2000/01/08 acme cleanup
  30. * 1999/11/06 acme cycx_down back to life (it needs to be
  31. * called to iounmap the dpmbase)
  32. * 1999/08/09 acme removed references to enable_tx_int
  33. * use spinlocks instead of cli/sti in
  34. * cyclomx_set_state
  35. * 1999/05/19 acme works directly linked into the kernel
  36. * init_waitqueue_head for 2.3.* kernel
  37. * 1999/05/18 acme major cleanup (polling not needed), etc
  38. * 1998/08/28 acme minor cleanup (ioctls for firmware deleted)
  39. * queue_task activated
  40. * 1998/08/08 acme Initial version.
  41. */
  42. #include <linux/config.h> /* OS configuration options */
  43. #include <linux/stddef.h> /* offsetof(), etc. */
  44. #include <linux/errno.h> /* return codes */
  45. #include <linux/string.h> /* inline memset(), etc. */
  46. #include <linux/slab.h> /* kmalloc(), kfree() */
  47. #include <linux/kernel.h> /* printk(), and other useful stuff */
  48. #include <linux/module.h> /* support for loadable modules */
  49. #include <linux/ioport.h> /* request_region(), release_region() */
  50. #include <linux/wanrouter.h> /* WAN router definitions */
  51. #include <linux/cyclomx.h> /* cyclomx common user API definitions */
  52. #include <linux/init.h> /* __init (when not using as a module) */
  53. unsigned int cycx_debug;
  54. MODULE_AUTHOR("Arnaldo Carvalho de Melo");
  55. MODULE_DESCRIPTION("Cyclom 2X Sync Card Driver.");
  56. MODULE_LICENSE("GPL");
  57. module_param(cycx_debug, int, 0);
  58. MODULE_PARM_DESC(cycx_debug, "cyclomx debug level");
  59. /* Defines & Macros */
  60. #define CYCX_DRV_VERSION 0 /* version number */
  61. #define CYCX_DRV_RELEASE 11 /* release (minor version) number */
  62. #define CYCX_MAX_CARDS 1 /* max number of adapters */
  63. #define CONFIG_CYCX_CARDS 1
  64. /* Function Prototypes */
  65. /* WAN link driver entry points */
  66. static int cycx_wan_setup(struct wan_device *wandev, wandev_conf_t *conf);
  67. static int cycx_wan_shutdown(struct wan_device *wandev);
  68. /* Miscellaneous functions */
  69. static irqreturn_t 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 cycx_drvname[] = "cyclomx";
  75. static char cycx_fullname[] = "CYCLOM 2X(tm) Sync Card Driver";
  76. static char cycx_copyright[] = "(c) 1998-2003 Arnaldo Carvalho de Melo "
  77. "<acme@conectiva.com.br>";
  78. static int cycx_ncards = CONFIG_CYCX_CARDS;
  79. static struct cycx_device *cycx_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. static int __init cycx_init(void)
  94. {
  95. int cnt, err = -ENOMEM;
  96. printk(KERN_INFO "%s v%u.%u %s\n",
  97. cycx_fullname, CYCX_DRV_VERSION, CYCX_DRV_RELEASE,
  98. cycx_copyright);
  99. /* Verify number of cards and allocate adapter data space */
  100. cycx_ncards = min_t(int, cycx_ncards, CYCX_MAX_CARDS);
  101. cycx_ncards = max_t(int, cycx_ncards, 1);
  102. cycx_card_array = kmalloc(sizeof(struct cycx_device) * cycx_ncards,
  103. GFP_KERNEL);
  104. if (!cycx_card_array)
  105. goto out;
  106. memset(cycx_card_array, 0, sizeof(struct cycx_device) * cycx_ncards);
  107. /* Register adapters with WAN router */
  108. for (cnt = 0; cnt < cycx_ncards; ++cnt) {
  109. struct cycx_device *card = &cycx_card_array[cnt];
  110. struct wan_device *wandev = &card->wandev;
  111. sprintf(card->devname, "%s%d", cycx_drvname, cnt + 1);
  112. wandev->magic = ROUTER_MAGIC;
  113. wandev->name = card->devname;
  114. wandev->private = card;
  115. wandev->setup = cycx_wan_setup;
  116. wandev->shutdown = cycx_wan_shutdown;
  117. err = register_wan_device(wandev);
  118. if (err) {
  119. printk(KERN_ERR "%s: %s registration failed with "
  120. "error %d!\n",
  121. cycx_drvname, card->devname, err);
  122. break;
  123. }
  124. }
  125. err = -ENODEV;
  126. if (!cnt) {
  127. kfree(cycx_card_array);
  128. goto out;
  129. }
  130. err = 0;
  131. cycx_ncards = cnt; /* adjust actual number of cards */
  132. out: return err;
  133. }
  134. /*
  135. * Module 'remove' entry point.
  136. * o unregister all adapters from the WAN router
  137. * o release all remaining system resources
  138. */
  139. static void __exit cycx_exit(void)
  140. {
  141. int i = 0;
  142. for (; i < cycx_ncards; ++i) {
  143. struct cycx_device *card = &cycx_card_array[i];
  144. unregister_wan_device(card->devname);
  145. }
  146. kfree(cycx_card_array);
  147. }
  148. /* WAN Device Driver Entry Points */
  149. /*
  150. * Setup/configure WAN link driver.
  151. * o check adapter state
  152. * o make sure firmware is present in configuration
  153. * o allocate interrupt vector
  154. * o setup Cyclom 2X hardware
  155. * o call appropriate routine to perform protocol-specific initialization
  156. *
  157. * This function is called when router handles ROUTER_SETUP IOCTL. The
  158. * configuration structure is in kernel memory (including extended data, if
  159. * any).
  160. */
  161. static int cycx_wan_setup(struct wan_device *wandev, wandev_conf_t *conf)
  162. {
  163. int rc = -EFAULT;
  164. struct cycx_device *card;
  165. int irq;
  166. /* Sanity checks */
  167. if (!wandev || !wandev->private || !conf)
  168. goto out;
  169. card = wandev->private;
  170. rc = -EBUSY;
  171. if (wandev->state != WAN_UNCONFIGURED)
  172. goto out;
  173. rc = -EINVAL;
  174. if (!conf->data_size || !conf->data) {
  175. printk(KERN_ERR "%s: firmware not found in configuration "
  176. "data!\n", wandev->name);
  177. goto out;
  178. }
  179. if (conf->irq <= 0) {
  180. printk(KERN_ERR "%s: can't configure without IRQ!\n",
  181. wandev->name);
  182. goto out;
  183. }
  184. /* Allocate IRQ */
  185. irq = conf->irq == 2 ? 9 : conf->irq; /* IRQ2 -> IRQ9 */
  186. if (request_irq(irq, cycx_isr, 0, wandev->name, card)) {
  187. printk(KERN_ERR "%s: can't reserve IRQ %d!\n",
  188. wandev->name, irq);
  189. goto out;
  190. }
  191. /* Configure hardware, load firmware, etc. */
  192. memset(&card->hw, 0, sizeof(card->hw));
  193. card->hw.irq = irq;
  194. card->hw.dpmsize = CYCX_WINDOWSIZE;
  195. card->hw.fwid = CFID_X25_2X;
  196. spin_lock_init(&card->lock);
  197. init_waitqueue_head(&card->wait_stats);
  198. rc = cycx_setup(&card->hw, conf->data, conf->data_size, conf->maddr);
  199. if (rc)
  200. goto out_irq;
  201. /* Initialize WAN device data space */
  202. wandev->irq = irq;
  203. wandev->dma = wandev->ioport = 0;
  204. wandev->maddr = (unsigned long)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. rc = cycx_x25_wan_init(card, conf);
  213. break;
  214. #endif
  215. default:
  216. printk(KERN_ERR "%s: this firmware is not supported!\n",
  217. wandev->name);
  218. rc = -EINVAL;
  219. }
  220. if (rc) {
  221. cycx_down(&card->hw);
  222. goto out_irq;
  223. }
  224. rc = 0;
  225. out:
  226. return rc;
  227. out_irq:
  228. free_irq(irq, card);
  229. goto out;
  230. }
  231. /*
  232. * Shut down WAN link driver.
  233. * o shut down adapter hardware
  234. * o release system resources.
  235. *
  236. * This function is called by the router when device is being unregistered or
  237. * when it handles ROUTER_DOWN IOCTL.
  238. */
  239. static int cycx_wan_shutdown(struct wan_device *wandev)
  240. {
  241. int ret = -EFAULT;
  242. struct cycx_device *card;
  243. /* sanity checks */
  244. if (!wandev || !wandev->private)
  245. goto out;
  246. ret = 0;
  247. if (wandev->state == WAN_UNCONFIGURED)
  248. goto out;
  249. card = wandev->private;
  250. wandev->state = WAN_UNCONFIGURED;
  251. cycx_down(&card->hw);
  252. printk(KERN_INFO "%s: irq %d being freed!\n", wandev->name,
  253. wandev->irq);
  254. free_irq(wandev->irq, card);
  255. out: return ret;
  256. }
  257. /* Miscellaneous */
  258. /*
  259. * Cyclom 2X Interrupt Service Routine.
  260. * o acknowledge Cyclom 2X hardware interrupt.
  261. * o call protocol-specific interrupt service routine, if any.
  262. */
  263. static irqreturn_t cycx_isr(int irq, void *dev_id, struct pt_regs *regs)
  264. {
  265. struct cycx_device *card = (struct cycx_device *)dev_id;
  266. if (!card || card->wandev.state == WAN_UNCONFIGURED)
  267. goto out;
  268. if (card->in_isr) {
  269. printk(KERN_WARNING "%s: interrupt re-entrancy on IRQ %d!\n",
  270. card->devname, card->wandev.irq);
  271. goto out;
  272. }
  273. if (card->isr)
  274. card->isr(card);
  275. return IRQ_HANDLED;
  276. out:
  277. return IRQ_NONE;
  278. }
  279. /* Set WAN device state. */
  280. void cycx_set_state(struct cycx_device *card, int state)
  281. {
  282. unsigned long flags;
  283. char *string_state = NULL;
  284. spin_lock_irqsave(&card->lock, flags);
  285. if (card->wandev.state != state) {
  286. switch (state) {
  287. case WAN_CONNECTED:
  288. string_state = "connected!";
  289. break;
  290. case WAN_DISCONNECTED:
  291. string_state = "disconnected!";
  292. break;
  293. }
  294. printk(KERN_INFO "%s: link %s\n", card->devname, string_state);
  295. card->wandev.state = state;
  296. }
  297. card->state_tick = jiffies;
  298. spin_unlock_irqrestore(&card->lock, flags);
  299. }
  300. module_init(cycx_init);
  301. module_exit(cycx_exit);