cycx_main.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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/stddef.h> /* offsetof(), etc. */
  43. #include <linux/errno.h> /* return codes */
  44. #include <linux/string.h> /* inline memset(), etc. */
  45. #include <linux/slab.h> /* kmalloc(), kfree() */
  46. #include <linux/kernel.h> /* printk(), and other useful stuff */
  47. #include <linux/module.h> /* support for loadable modules */
  48. #include <linux/ioport.h> /* request_region(), release_region() */
  49. #include <linux/wanrouter.h> /* WAN router definitions */
  50. #include <linux/cyclomx.h> /* cyclomx common user API definitions */
  51. #include <linux/init.h> /* __init (when not using as a module) */
  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_param(cycx_debug, int, 0);
  57. MODULE_PARM_DESC(cycx_debug, "cyclomx debug level");
  58. /* Defines & Macros */
  59. #define CYCX_DRV_VERSION 0 /* version number */
  60. #define CYCX_DRV_RELEASE 11 /* release (minor version) number */
  61. #define CYCX_MAX_CARDS 1 /* max number of adapters */
  62. #define CONFIG_CYCX_CARDS 1
  63. /* Function Prototypes */
  64. /* WAN link driver entry points */
  65. static int cycx_wan_setup(struct wan_device *wandev, wandev_conf_t *conf);
  66. static int cycx_wan_shutdown(struct wan_device *wandev);
  67. /* Miscellaneous functions */
  68. static irqreturn_t cycx_isr(int irq, void *dev_id);
  69. /* Global Data
  70. * Note: All data must be explicitly initialized!!!
  71. */
  72. /* private data */
  73. static char cycx_drvname[] = "cyclomx";
  74. static char cycx_fullname[] = "CYCLOM 2X(tm) Sync Card Driver";
  75. static char cycx_copyright[] = "(c) 1998-2003 Arnaldo Carvalho de Melo "
  76. "<acme@conectiva.com.br>";
  77. static int cycx_ncards = CONFIG_CYCX_CARDS;
  78. static struct cycx_device *cycx_card_array; /* adapter data space */
  79. /* Kernel Loadable Module Entry Points */
  80. /*
  81. * Module 'insert' entry point.
  82. * o print announcement
  83. * o allocate adapter data space
  84. * o initialize static data
  85. * o register all cards with WAN router
  86. * o calibrate Cyclom 2X shared memory access delay.
  87. *
  88. * Return: 0 Ok
  89. * < 0 error.
  90. * Context: process
  91. */
  92. static int __init cycx_init(void)
  93. {
  94. int cnt, err = -ENOMEM;
  95. printk(KERN_INFO "%s v%u.%u %s\n",
  96. cycx_fullname, CYCX_DRV_VERSION, CYCX_DRV_RELEASE,
  97. cycx_copyright);
  98. /* Verify number of cards and allocate adapter data space */
  99. cycx_ncards = min_t(int, cycx_ncards, CYCX_MAX_CARDS);
  100. cycx_ncards = max_t(int, cycx_ncards, 1);
  101. cycx_card_array = kmalloc(sizeof(struct cycx_device) * cycx_ncards,
  102. GFP_KERNEL);
  103. if (!cycx_card_array)
  104. goto out;
  105. memset(cycx_card_array, 0, sizeof(struct cycx_device) * cycx_ncards);
  106. /* Register adapters with WAN router */
  107. for (cnt = 0; cnt < cycx_ncards; ++cnt) {
  108. struct cycx_device *card = &cycx_card_array[cnt];
  109. struct wan_device *wandev = &card->wandev;
  110. sprintf(card->devname, "%s%d", cycx_drvname, cnt + 1);
  111. wandev->magic = ROUTER_MAGIC;
  112. wandev->name = card->devname;
  113. wandev->private = card;
  114. wandev->setup = cycx_wan_setup;
  115. wandev->shutdown = cycx_wan_shutdown;
  116. err = register_wan_device(wandev);
  117. if (err) {
  118. printk(KERN_ERR "%s: %s registration failed with "
  119. "error %d!\n",
  120. cycx_drvname, card->devname, err);
  121. break;
  122. }
  123. }
  124. err = -ENODEV;
  125. if (!cnt) {
  126. kfree(cycx_card_array);
  127. goto out;
  128. }
  129. err = 0;
  130. cycx_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 cycx_exit(void)
  139. {
  140. int i = 0;
  141. for (; i < cycx_ncards; ++i) {
  142. struct cycx_device *card = &cycx_card_array[i];
  143. unregister_wan_device(card->devname);
  144. }
  145. kfree(cycx_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 cycx_wan_setup(struct wan_device *wandev, wandev_conf_t *conf)
  161. {
  162. int rc = -EFAULT;
  163. struct cycx_device *card;
  164. int irq;
  165. /* Sanity checks */
  166. if (!wandev || !wandev->private || !conf)
  167. goto out;
  168. card = wandev->private;
  169. rc = -EBUSY;
  170. if (wandev->state != WAN_UNCONFIGURED)
  171. goto out;
  172. rc = -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(card->hw));
  192. card->hw.irq = irq;
  193. card->hw.dpmsize = CYCX_WINDOWSIZE;
  194. card->hw.fwid = CFID_X25_2X;
  195. spin_lock_init(&card->lock);
  196. init_waitqueue_head(&card->wait_stats);
  197. rc = cycx_setup(&card->hw, conf->data, conf->data_size, conf->maddr);
  198. if (rc)
  199. goto out_irq;
  200. /* Initialize WAN device data space */
  201. wandev->irq = irq;
  202. wandev->dma = wandev->ioport = 0;
  203. wandev->maddr = (unsigned long)card->hw.dpmbase;
  204. wandev->msize = card->hw.dpmsize;
  205. wandev->hw_opt[2] = 0;
  206. wandev->hw_opt[3] = card->hw.fwid;
  207. /* Protocol-specific initialization */
  208. switch (card->hw.fwid) {
  209. #ifdef CONFIG_CYCLOMX_X25
  210. case CFID_X25_2X:
  211. rc = cycx_x25_wan_init(card, conf);
  212. break;
  213. #endif
  214. default:
  215. printk(KERN_ERR "%s: this firmware is not supported!\n",
  216. wandev->name);
  217. rc = -EINVAL;
  218. }
  219. if (rc) {
  220. cycx_down(&card->hw);
  221. goto out_irq;
  222. }
  223. rc = 0;
  224. out:
  225. return rc;
  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 cycx_wan_shutdown(struct wan_device *wandev)
  239. {
  240. int ret = -EFAULT;
  241. struct cycx_device *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. /* Miscellaneous */
  257. /*
  258. * Cyclom 2X Interrupt Service Routine.
  259. * o acknowledge Cyclom 2X hardware interrupt.
  260. * o call protocol-specific interrupt service routine, if any.
  261. */
  262. static irqreturn_t cycx_isr(int irq, void *dev_id)
  263. {
  264. struct cycx_device *card = dev_id;
  265. if (card->wandev.state == WAN_UNCONFIGURED)
  266. goto out;
  267. if (card->in_isr) {
  268. printk(KERN_WARNING "%s: interrupt re-entrancy on IRQ %d!\n",
  269. card->devname, card->wandev.irq);
  270. goto out;
  271. }
  272. if (card->isr)
  273. card->isr(card);
  274. return IRQ_HANDLED;
  275. out:
  276. return IRQ_NONE;
  277. }
  278. /* Set WAN device state. */
  279. void cycx_set_state(struct cycx_device *card, int state)
  280. {
  281. unsigned long flags;
  282. char *string_state = NULL;
  283. spin_lock_irqsave(&card->lock, flags);
  284. if (card->wandev.state != state) {
  285. switch (state) {
  286. case WAN_CONNECTED:
  287. string_state = "connected!";
  288. break;
  289. case WAN_DISCONNECTED:
  290. string_state = "disconnected!";
  291. break;
  292. }
  293. printk(KERN_INFO "%s: link %s\n", card->devname, string_state);
  294. card->wandev.state = state;
  295. }
  296. card->state_tick = jiffies;
  297. spin_unlock_irqrestore(&card->lock, flags);
  298. }
  299. module_init(cycx_init);
  300. module_exit(cycx_exit);