cycx_main.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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 const char cycx_drvname[] = "cyclomx";
  74. static const char cycx_fullname[] = "CYCLOM 2X(tm) Sync Card Driver";
  75. static const 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 = kcalloc(cycx_ncards, sizeof(struct cycx_device), GFP_KERNEL);
  102. if (!cycx_card_array)
  103. goto out;
  104. /* Register adapters with WAN router */
  105. for (cnt = 0; cnt < cycx_ncards; ++cnt) {
  106. struct cycx_device *card = &cycx_card_array[cnt];
  107. struct wan_device *wandev = &card->wandev;
  108. sprintf(card->devname, "%s%d", cycx_drvname, cnt + 1);
  109. wandev->magic = ROUTER_MAGIC;
  110. wandev->name = card->devname;
  111. wandev->private = card;
  112. wandev->setup = cycx_wan_setup;
  113. wandev->shutdown = cycx_wan_shutdown;
  114. err = register_wan_device(wandev);
  115. if (err) {
  116. printk(KERN_ERR "%s: %s registration failed with "
  117. "error %d!\n",
  118. cycx_drvname, card->devname, err);
  119. break;
  120. }
  121. }
  122. err = -ENODEV;
  123. if (!cnt) {
  124. kfree(cycx_card_array);
  125. goto out;
  126. }
  127. err = 0;
  128. cycx_ncards = cnt; /* adjust actual number of cards */
  129. out: return err;
  130. }
  131. /*
  132. * Module 'remove' entry point.
  133. * o unregister all adapters from the WAN router
  134. * o release all remaining system resources
  135. */
  136. static void __exit cycx_exit(void)
  137. {
  138. int i = 0;
  139. for (; i < cycx_ncards; ++i) {
  140. struct cycx_device *card = &cycx_card_array[i];
  141. unregister_wan_device(card->devname);
  142. }
  143. kfree(cycx_card_array);
  144. }
  145. /* WAN Device Driver Entry Points */
  146. /*
  147. * Setup/configure WAN link driver.
  148. * o check adapter state
  149. * o make sure firmware is present in configuration
  150. * o allocate interrupt vector
  151. * o setup Cyclom 2X hardware
  152. * o call appropriate routine to perform protocol-specific initialization
  153. *
  154. * This function is called when router handles ROUTER_SETUP IOCTL. The
  155. * configuration structure is in kernel memory (including extended data, if
  156. * any).
  157. */
  158. static int cycx_wan_setup(struct wan_device *wandev, wandev_conf_t *conf)
  159. {
  160. int rc = -EFAULT;
  161. struct cycx_device *card;
  162. int irq;
  163. /* Sanity checks */
  164. if (!wandev || !wandev->private || !conf)
  165. goto out;
  166. card = wandev->private;
  167. rc = -EBUSY;
  168. if (wandev->state != WAN_UNCONFIGURED)
  169. goto out;
  170. rc = -EINVAL;
  171. if (!conf->data_size || !conf->data) {
  172. printk(KERN_ERR "%s: firmware not found in configuration "
  173. "data!\n", wandev->name);
  174. goto out;
  175. }
  176. if (conf->irq <= 0) {
  177. printk(KERN_ERR "%s: can't configure without IRQ!\n",
  178. wandev->name);
  179. goto out;
  180. }
  181. /* Allocate IRQ */
  182. irq = conf->irq == 2 ? 9 : conf->irq; /* IRQ2 -> IRQ9 */
  183. if (request_irq(irq, cycx_isr, 0, wandev->name, card)) {
  184. printk(KERN_ERR "%s: can't reserve IRQ %d!\n",
  185. wandev->name, irq);
  186. goto out;
  187. }
  188. /* Configure hardware, load firmware, etc. */
  189. memset(&card->hw, 0, sizeof(card->hw));
  190. card->hw.irq = irq;
  191. card->hw.dpmsize = CYCX_WINDOWSIZE;
  192. card->hw.fwid = CFID_X25_2X;
  193. spin_lock_init(&card->lock);
  194. init_waitqueue_head(&card->wait_stats);
  195. rc = cycx_setup(&card->hw, conf->data, conf->data_size, conf->maddr);
  196. if (rc)
  197. goto out_irq;
  198. /* Initialize WAN device data space */
  199. wandev->irq = irq;
  200. wandev->dma = wandev->ioport = 0;
  201. wandev->maddr = (unsigned long)card->hw.dpmbase;
  202. wandev->msize = card->hw.dpmsize;
  203. wandev->hw_opt[2] = 0;
  204. wandev->hw_opt[3] = card->hw.fwid;
  205. /* Protocol-specific initialization */
  206. switch (card->hw.fwid) {
  207. #ifdef CONFIG_CYCLOMX_X25
  208. case CFID_X25_2X:
  209. rc = cycx_x25_wan_init(card, conf);
  210. break;
  211. #endif
  212. default:
  213. printk(KERN_ERR "%s: this firmware is not supported!\n",
  214. wandev->name);
  215. rc = -EINVAL;
  216. }
  217. if (rc) {
  218. cycx_down(&card->hw);
  219. goto out_irq;
  220. }
  221. rc = 0;
  222. out:
  223. return rc;
  224. out_irq:
  225. free_irq(irq, card);
  226. goto out;
  227. }
  228. /*
  229. * Shut down WAN link driver.
  230. * o shut down adapter hardware
  231. * o release system resources.
  232. *
  233. * This function is called by the router when device is being unregistered or
  234. * when it handles ROUTER_DOWN IOCTL.
  235. */
  236. static int cycx_wan_shutdown(struct wan_device *wandev)
  237. {
  238. int ret = -EFAULT;
  239. struct cycx_device *card;
  240. /* sanity checks */
  241. if (!wandev || !wandev->private)
  242. goto out;
  243. ret = 0;
  244. if (wandev->state == WAN_UNCONFIGURED)
  245. goto out;
  246. card = wandev->private;
  247. wandev->state = WAN_UNCONFIGURED;
  248. cycx_down(&card->hw);
  249. printk(KERN_INFO "%s: irq %d being freed!\n", wandev->name,
  250. wandev->irq);
  251. free_irq(wandev->irq, card);
  252. out: return ret;
  253. }
  254. /* Miscellaneous */
  255. /*
  256. * Cyclom 2X Interrupt Service Routine.
  257. * o acknowledge Cyclom 2X hardware interrupt.
  258. * o call protocol-specific interrupt service routine, if any.
  259. */
  260. static irqreturn_t cycx_isr(int irq, void *dev_id)
  261. {
  262. struct cycx_device *card = dev_id;
  263. if (card->wandev.state == WAN_UNCONFIGURED)
  264. goto out;
  265. if (card->in_isr) {
  266. printk(KERN_WARNING "%s: interrupt re-entrancy on IRQ %d!\n",
  267. card->devname, card->wandev.irq);
  268. goto out;
  269. }
  270. if (card->isr)
  271. card->isr(card);
  272. return IRQ_HANDLED;
  273. out:
  274. return IRQ_NONE;
  275. }
  276. /* Set WAN device state. */
  277. void cycx_set_state(struct cycx_device *card, int state)
  278. {
  279. unsigned long flags;
  280. char *string_state = NULL;
  281. spin_lock_irqsave(&card->lock, flags);
  282. if (card->wandev.state != state) {
  283. switch (state) {
  284. case WAN_CONNECTED:
  285. string_state = "connected!";
  286. break;
  287. case WAN_DISCONNECTED:
  288. string_state = "disconnected!";
  289. break;
  290. }
  291. printk(KERN_INFO "%s: link %s\n", card->devname, string_state);
  292. card->wandev.state = state;
  293. }
  294. card->state_tick = jiffies;
  295. spin_unlock_irqrestore(&card->lock, flags);
  296. }
  297. module_init(cycx_init);
  298. module_exit(cycx_exit);