cycx_main.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  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. #include <linux/interrupt.h>
  54. unsigned int cycx_debug;
  55. MODULE_AUTHOR("Arnaldo Carvalho de Melo");
  56. MODULE_DESCRIPTION("Cyclom 2X Sync Card Driver.");
  57. MODULE_LICENSE("GPL");
  58. module_param(cycx_debug, int, 0);
  59. MODULE_PARM_DESC(cycx_debug, "cyclomx debug level");
  60. /* Defines & Macros */
  61. #define CYCX_DRV_VERSION 0 /* version number */
  62. #define CYCX_DRV_RELEASE 11 /* release (minor version) number */
  63. #define CYCX_MAX_CARDS 1 /* max number of adapters */
  64. #define CONFIG_CYCX_CARDS 1
  65. /* Function Prototypes */
  66. /* WAN link driver entry points */
  67. static int cycx_wan_setup(struct wan_device *wandev, wandev_conf_t *conf);
  68. static int cycx_wan_shutdown(struct wan_device *wandev);
  69. /* Miscellaneous functions */
  70. static irqreturn_t cycx_isr(int irq, void *dev_id);
  71. /* Global Data
  72. * Note: All data must be explicitly initialized!!!
  73. */
  74. /* private data */
  75. static const char cycx_drvname[] = "cyclomx";
  76. static const char cycx_fullname[] = "CYCLOM 2X(tm) Sync Card Driver";
  77. static const char cycx_copyright[] = "(c) 1998-2003 Arnaldo Carvalho de Melo "
  78. "<acme@conectiva.com.br>";
  79. static int cycx_ncards = CONFIG_CYCX_CARDS;
  80. static struct cycx_device *cycx_card_array; /* adapter data space */
  81. /* Kernel Loadable Module Entry Points */
  82. /*
  83. * Module 'insert' entry point.
  84. * o print announcement
  85. * o allocate adapter data space
  86. * o initialize static data
  87. * o register all cards with WAN router
  88. * o calibrate Cyclom 2X shared memory access delay.
  89. *
  90. * Return: 0 Ok
  91. * < 0 error.
  92. * Context: process
  93. */
  94. static int __init cycx_init(void)
  95. {
  96. int cnt, err = -ENOMEM;
  97. pr_info("%s v%u.%u %s\n",
  98. cycx_fullname, CYCX_DRV_VERSION, CYCX_DRV_RELEASE,
  99. cycx_copyright);
  100. /* Verify number of cards and allocate adapter data space */
  101. cycx_ncards = min_t(int, cycx_ncards, CYCX_MAX_CARDS);
  102. cycx_ncards = max_t(int, cycx_ncards, 1);
  103. cycx_card_array = kcalloc(cycx_ncards, sizeof(struct cycx_device), GFP_KERNEL);
  104. if (!cycx_card_array)
  105. goto out;
  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. pr_err("%s registration failed with error %d!\n",
  119. card->devname, err);
  120. break;
  121. }
  122. }
  123. err = -ENODEV;
  124. if (!cnt) {
  125. kfree(cycx_card_array);
  126. goto out;
  127. }
  128. err = 0;
  129. cycx_ncards = cnt; /* adjust actual number of cards */
  130. out: return err;
  131. }
  132. /*
  133. * Module 'remove' entry point.
  134. * o unregister all adapters from the WAN router
  135. * o release all remaining system resources
  136. */
  137. static void __exit cycx_exit(void)
  138. {
  139. int i = 0;
  140. for (; i < cycx_ncards; ++i) {
  141. struct cycx_device *card = &cycx_card_array[i];
  142. unregister_wan_device(card->devname);
  143. }
  144. kfree(cycx_card_array);
  145. }
  146. /* WAN Device Driver Entry Points */
  147. /*
  148. * Setup/configure WAN link driver.
  149. * o check adapter state
  150. * o make sure firmware is present in configuration
  151. * o allocate interrupt vector
  152. * o setup Cyclom 2X hardware
  153. * o call appropriate routine to perform protocol-specific initialization
  154. *
  155. * This function is called when router handles ROUTER_SETUP IOCTL. The
  156. * configuration structure is in kernel memory (including extended data, if
  157. * any).
  158. */
  159. static int cycx_wan_setup(struct wan_device *wandev, wandev_conf_t *conf)
  160. {
  161. int rc = -EFAULT;
  162. struct cycx_device *card;
  163. int irq;
  164. /* Sanity checks */
  165. if (!wandev || !wandev->private || !conf)
  166. goto out;
  167. card = wandev->private;
  168. rc = -EBUSY;
  169. if (wandev->state != WAN_UNCONFIGURED)
  170. goto out;
  171. rc = -EINVAL;
  172. if (!conf->data_size || !conf->data) {
  173. pr_err("%s: firmware not found in configuration data!\n",
  174. wandev->name);
  175. goto out;
  176. }
  177. if (conf->irq <= 0) {
  178. pr_err("%s: can't configure without IRQ!\n", 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. pr_err("%s: can't reserve IRQ %d!\n", wandev->name, irq);
  185. goto out;
  186. }
  187. /* Configure hardware, load firmware, etc. */
  188. memset(&card->hw, 0, sizeof(card->hw));
  189. card->hw.irq = irq;
  190. card->hw.dpmsize = CYCX_WINDOWSIZE;
  191. card->hw.fwid = CFID_X25_2X;
  192. spin_lock_init(&card->lock);
  193. init_waitqueue_head(&card->wait_stats);
  194. rc = cycx_setup(&card->hw, conf->data, conf->data_size, conf->maddr);
  195. if (rc)
  196. goto out_irq;
  197. /* Initialize WAN device data space */
  198. wandev->irq = irq;
  199. wandev->dma = wandev->ioport = 0;
  200. wandev->maddr = (unsigned long)card->hw.dpmbase;
  201. wandev->msize = card->hw.dpmsize;
  202. wandev->hw_opt[2] = 0;
  203. wandev->hw_opt[3] = card->hw.fwid;
  204. /* Protocol-specific initialization */
  205. switch (card->hw.fwid) {
  206. #ifdef CONFIG_CYCLOMX_X25
  207. case CFID_X25_2X:
  208. rc = cycx_x25_wan_init(card, conf);
  209. break;
  210. #endif
  211. default:
  212. pr_err("%s: this firmware is not supported!\n", wandev->name);
  213. rc = -EINVAL;
  214. }
  215. if (rc) {
  216. cycx_down(&card->hw);
  217. goto out_irq;
  218. }
  219. rc = 0;
  220. out:
  221. return rc;
  222. out_irq:
  223. free_irq(irq, card);
  224. goto out;
  225. }
  226. /*
  227. * Shut down WAN link driver.
  228. * o shut down adapter hardware
  229. * o release system resources.
  230. *
  231. * This function is called by the router when device is being unregistered or
  232. * when it handles ROUTER_DOWN IOCTL.
  233. */
  234. static int cycx_wan_shutdown(struct wan_device *wandev)
  235. {
  236. int ret = -EFAULT;
  237. struct cycx_device *card;
  238. /* sanity checks */
  239. if (!wandev || !wandev->private)
  240. goto out;
  241. ret = 0;
  242. if (wandev->state == WAN_UNCONFIGURED)
  243. goto out;
  244. card = wandev->private;
  245. wandev->state = WAN_UNCONFIGURED;
  246. cycx_down(&card->hw);
  247. pr_info("%s: irq %d being freed!\n", wandev->name, wandev->irq);
  248. free_irq(wandev->irq, card);
  249. out: return ret;
  250. }
  251. /* Miscellaneous */
  252. /*
  253. * Cyclom 2X Interrupt Service Routine.
  254. * o acknowledge Cyclom 2X hardware interrupt.
  255. * o call protocol-specific interrupt service routine, if any.
  256. */
  257. static irqreturn_t cycx_isr(int irq, void *dev_id)
  258. {
  259. struct cycx_device *card = dev_id;
  260. if (card->wandev.state == WAN_UNCONFIGURED)
  261. goto out;
  262. if (card->in_isr) {
  263. pr_warn("%s: interrupt re-entrancy on IRQ %d!\n",
  264. card->devname, card->wandev.irq);
  265. goto out;
  266. }
  267. if (card->isr)
  268. card->isr(card);
  269. return IRQ_HANDLED;
  270. out:
  271. return IRQ_NONE;
  272. }
  273. /* Set WAN device state. */
  274. void cycx_set_state(struct cycx_device *card, int state)
  275. {
  276. unsigned long flags;
  277. char *string_state = NULL;
  278. spin_lock_irqsave(&card->lock, flags);
  279. if (card->wandev.state != state) {
  280. switch (state) {
  281. case WAN_CONNECTED:
  282. string_state = "connected!";
  283. break;
  284. case WAN_DISCONNECTED:
  285. string_state = "disconnected!";
  286. break;
  287. }
  288. pr_info("%s: link %s\n", card->devname, string_state);
  289. card->wandev.state = state;
  290. }
  291. card->state_tick = jiffies;
  292. spin_unlock_irqrestore(&card->lock, flags);
  293. }
  294. module_init(cycx_init);
  295. module_exit(cycx_exit);