sdlamain.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341
  1. /****************************************************************************
  2. * sdlamain.c WANPIPE(tm) Multiprotocol WAN Link Driver. Main module.
  3. *
  4. * Author: Nenad Corbic <ncorbic@sangoma.com>
  5. * Gideon Hack
  6. *
  7. * Copyright: (c) 1995-2000 Sangoma Technologies Inc.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version
  12. * 2 of the License, or (at your option) any later version.
  13. * ============================================================================
  14. * Dec 22, 2000 Nenad Corbic Updated for 2.4.X kernels.
  15. * Removed the polling routine.
  16. * Nov 13, 2000 Nenad Corbic Added hw probing on module load and dynamic
  17. * device allocation.
  18. * Nov 7, 2000 Nenad Corbic Fixed the Multi-Port PPP for kernels
  19. * 2.2.16 and above.
  20. * Aug 2, 2000 Nenad Corbic Block the Multi-Port PPP from running on
  21. * kernels 2.2.16 or greater. The SyncPPP
  22. * has changed.
  23. * Jul 25, 2000 Nenad Corbic Updated the Piggiback support for MultPPPP.
  24. * Jul 13, 2000 Nenad Corbic Added Multi-PPP support.
  25. * Feb 02, 2000 Nenad Corbic Fixed up piggyback probing and selection.
  26. * Sep 23, 1999 Nenad Corbic Added support for SMP
  27. * Sep 13, 1999 Nenad Corbic Each port is treated as a separate device.
  28. * Jun 02, 1999 Gideon Hack Added support for the S514 adapter.
  29. * Updates for Linux 2.2.X kernels.
  30. * Sep 17, 1998 Jaspreet Singh Updated for 2.1.121+ kernel
  31. * Nov 28, 1997 Jaspreet Singh Changed DRV_RELEASE to 1
  32. * Nov 10, 1997 Jaspreet Singh Changed sti() to restore_flags();
  33. * Nov 06, 1997 Jaspreet Singh Changed DRV_VERSION to 4 and DRV_RELEASE to 0
  34. * Oct 20, 1997 Jaspreet Singh Modified sdla_isr routine so that card->in_isr
  35. * assignments are taken out and placed in the
  36. * sdla_ppp.c, sdla_fr.c and sdla_x25.c isr
  37. * routines. Took out 'wandev->tx_int_enabled' and
  38. * replaced it with 'wandev->enable_tx_int'.
  39. * May 29, 1997 Jaspreet Singh Flow Control Problem
  40. * added "wandev->tx_int_enabled=1" line in the
  41. * init module. This line initializes the flag for
  42. * preventing Interrupt disabled with device set to
  43. * busy
  44. * Jan 15, 1997 Gene Kozin Version 3.1.0
  45. * o added UDP management stuff
  46. * Jan 02, 1997 Gene Kozin Initial version.
  47. *****************************************************************************/
  48. #include <linux/config.h> /* OS configuration options */
  49. #include <linux/stddef.h> /* offsetof(), etc. */
  50. #include <linux/errno.h> /* return codes */
  51. #include <linux/string.h> /* inline memset(), etc. */
  52. #include <linux/init.h>
  53. #include <linux/slab.h> /* kmalloc(), kfree() */
  54. #include <linux/kernel.h> /* printk(), and other useful stuff */
  55. #include <linux/module.h> /* support for loadable modules */
  56. #include <linux/ioport.h> /* request_region(), release_region() */
  57. #include <linux/wanrouter.h> /* WAN router definitions */
  58. #include <linux/wanpipe.h> /* WANPIPE common user API definitions */
  59. #include <linux/in.h>
  60. #include <asm/io.h> /* phys_to_virt() */
  61. #include <linux/pci.h>
  62. #include <linux/sdlapci.h>
  63. #include <linux/if_wanpipe_common.h>
  64. #include <asm/uaccess.h> /* kernel <-> user copy */
  65. #include <linux/inetdevice.h>
  66. #include <linux/ip.h>
  67. #include <net/route.h>
  68. #define KMEM_SAFETYZONE 8
  69. #ifndef CONFIG_WANPIPE_FR
  70. #define wpf_init(a,b) (-EPROTONOSUPPORT)
  71. #endif
  72. #ifndef CONFIG_WANPIPE_CHDLC
  73. #define wpc_init(a,b) (-EPROTONOSUPPORT)
  74. #endif
  75. #ifndef CONFIG_WANPIPE_X25
  76. #define wpx_init(a,b) (-EPROTONOSUPPORT)
  77. #endif
  78. #ifndef CONFIG_WANPIPE_PPP
  79. #define wpp_init(a,b) (-EPROTONOSUPPORT)
  80. #endif
  81. #ifndef CONFIG_WANPIPE_MULTPPP
  82. #define wsppp_init(a,b) (-EPROTONOSUPPORT)
  83. #endif
  84. /***********FOR DEBUGGING PURPOSES*********************************************
  85. static void * dbg_kmalloc(unsigned int size, int prio, int line) {
  86. int i = 0;
  87. void * v = kmalloc(size+sizeof(unsigned int)+2*KMEM_SAFETYZONE*8,prio);
  88. char * c1 = v;
  89. c1 += sizeof(unsigned int);
  90. *((unsigned int *)v) = size;
  91. for (i = 0; i < KMEM_SAFETYZONE; i++) {
  92. c1[0] = 'D'; c1[1] = 'E'; c1[2] = 'A'; c1[3] = 'D';
  93. c1[4] = 'B'; c1[5] = 'E'; c1[6] = 'E'; c1[7] = 'F';
  94. c1 += 8;
  95. }
  96. c1 += size;
  97. for (i = 0; i < KMEM_SAFETYZONE; i++) {
  98. c1[0] = 'M'; c1[1] = 'U'; c1[2] = 'N'; c1[3] = 'G';
  99. c1[4] = 'W'; c1[5] = 'A'; c1[6] = 'L'; c1[7] = 'L';
  100. c1 += 8;
  101. }
  102. v = ((char *)v) + sizeof(unsigned int) + KMEM_SAFETYZONE*8;
  103. printk(KERN_INFO "line %d kmalloc(%d,%d) = %p\n",line,size,prio,v);
  104. return v;
  105. }
  106. static void dbg_kfree(void * v, int line) {
  107. unsigned int * sp = (unsigned int *)(((char *)v) - (sizeof(unsigned int) + KMEM_SAFETYZONE*8));
  108. unsigned int size = *sp;
  109. char * c1 = ((char *)v) - KMEM_SAFETYZONE*8;
  110. int i = 0;
  111. for (i = 0; i < KMEM_SAFETYZONE; i++) {
  112. if ( c1[0] != 'D' || c1[1] != 'E' || c1[2] != 'A' || c1[3] != 'D'
  113. || c1[4] != 'B' || c1[5] != 'E' || c1[6] != 'E' || c1[7] != 'F') {
  114. printk(KERN_INFO "kmalloced block at %p has been corrupted (underrun)!\n",v);
  115. printk(KERN_INFO " %4x: %2x %2x %2x %2x %2x %2x %2x %2x\n", i*8,
  116. c1[0],c1[1],c1[2],c1[3],c1[4],c1[5],c1[6],c1[7] );
  117. }
  118. c1 += 8;
  119. }
  120. c1 += size;
  121. for (i = 0; i < KMEM_SAFETYZONE; i++) {
  122. if ( c1[0] != 'M' || c1[1] != 'U' || c1[2] != 'N' || c1[3] != 'G'
  123. || c1[4] != 'W' || c1[5] != 'A' || c1[6] != 'L' || c1[7] != 'L'
  124. ) {
  125. printk(KERN_INFO "kmalloced block at %p has been corrupted (overrun):\n",v);
  126. printk(KERN_INFO " %4x: %2x %2x %2x %2x %2x %2x %2x %2x\n", i*8,
  127. c1[0],c1[1],c1[2],c1[3],c1[4],c1[5],c1[6],c1[7] );
  128. }
  129. c1 += 8;
  130. }
  131. printk(KERN_INFO "line %d kfree(%p)\n",line,v);
  132. v = ((char *)v) - (sizeof(unsigned int) + KMEM_SAFETYZONE*8);
  133. kfree(v);
  134. }
  135. #define kmalloc(x,y) dbg_kmalloc(x,y,__LINE__)
  136. #define kfree(x) dbg_kfree(x,__LINE__)
  137. ******************************************************************************/
  138. /****** Defines & Macros ****************************************************/
  139. #ifdef _DEBUG_
  140. #define STATIC
  141. #else
  142. #define STATIC static
  143. #endif
  144. #define DRV_VERSION 5 /* version number */
  145. #define DRV_RELEASE 0 /* release (minor version) number */
  146. #define MAX_CARDS 16 /* max number of adapters */
  147. #ifndef CONFIG_WANPIPE_CARDS /* configurable option */
  148. #define CONFIG_WANPIPE_CARDS 1
  149. #endif
  150. #define CMD_OK 0 /* normal firmware return code */
  151. #define CMD_TIMEOUT 0xFF /* firmware command timed out */
  152. #define MAX_CMD_RETRY 10 /* max number of firmware retries */
  153. /****** Function Prototypes *************************************************/
  154. extern void disable_irq(unsigned int);
  155. extern void enable_irq(unsigned int);
  156. /* WAN link driver entry points */
  157. static int setup(struct wan_device* wandev, wandev_conf_t* conf);
  158. static int shutdown(struct wan_device* wandev);
  159. static int ioctl(struct wan_device* wandev, unsigned cmd, unsigned long arg);
  160. /* IOCTL handlers */
  161. static int ioctl_dump (sdla_t* card, sdla_dump_t* u_dump);
  162. static int ioctl_exec (sdla_t* card, sdla_exec_t* u_exec, int);
  163. /* Miscellaneous functions */
  164. STATIC irqreturn_t sdla_isr (int irq, void* dev_id, struct pt_regs *regs);
  165. static void release_hw (sdla_t *card);
  166. static int check_s508_conflicts (sdla_t* card,wandev_conf_t* conf, int*);
  167. static int check_s514_conflicts (sdla_t* card,wandev_conf_t* conf, int*);
  168. /****** Global Data **********************************************************
  169. * Note: All data must be explicitly initialized!!!
  170. */
  171. /* private data */
  172. static char drvname[] = "wanpipe";
  173. static char fullname[] = "WANPIPE(tm) Multiprotocol Driver";
  174. static char copyright[] = "(c) 1995-2000 Sangoma Technologies Inc.";
  175. static int ncards;
  176. static sdla_t* card_array; /* adapter data space */
  177. /* Wanpipe's own workqueue, used for all API's.
  178. * All protocol specific tasks will be inserted
  179. * into the "wanpipe_wq" workqueue.
  180. * The kernel workqueue mechanism will execute
  181. * all pending tasks in the "wanpipe_wq" workqueue.
  182. */
  183. struct workqueue_struct *wanpipe_wq;
  184. DECLARE_WORK(wanpipe_work, NULL, NULL);
  185. static int wanpipe_bh_critical;
  186. /******* Kernel Loadable Module Entry Points ********************************/
  187. /*============================================================================
  188. * Module 'insert' entry point.
  189. * o print announcement
  190. * o allocate adapter data space
  191. * o initialize static data
  192. * o register all cards with WAN router
  193. * o calibrate SDLA shared memory access delay.
  194. *
  195. * Return: 0 Ok
  196. * < 0 error.
  197. * Context: process
  198. */
  199. static int __init wanpipe_init(void)
  200. {
  201. int cnt, err = 0;
  202. printk(KERN_INFO "%s v%u.%u %s\n",
  203. fullname, DRV_VERSION, DRV_RELEASE, copyright);
  204. wanpipe_wq = create_workqueue("wanpipe_wq");
  205. if (!wanpipe_wq)
  206. return -ENOMEM;
  207. /* Probe for wanpipe cards and return the number found */
  208. printk(KERN_INFO "wanpipe: Probing for WANPIPE hardware.\n");
  209. ncards = wanpipe_hw_probe();
  210. if (ncards){
  211. printk(KERN_INFO "wanpipe: Allocating maximum %i devices: wanpipe%i - wanpipe%i.\n",ncards,1,ncards);
  212. }else{
  213. printk(KERN_INFO "wanpipe: No S514/S508 cards found, unloading modules!\n");
  214. destroy_workqueue(wanpipe_wq);
  215. return -ENODEV;
  216. }
  217. /* Verify number of cards and allocate adapter data space */
  218. card_array = kmalloc(sizeof(sdla_t) * ncards, GFP_KERNEL);
  219. if (card_array == NULL) {
  220. destroy_workqueue(wanpipe_wq);
  221. return -ENOMEM;
  222. }
  223. memset(card_array, 0, sizeof(sdla_t) * ncards);
  224. /* Register adapters with WAN router */
  225. for (cnt = 0; cnt < ncards; ++ cnt) {
  226. sdla_t* card = &card_array[cnt];
  227. struct wan_device* wandev = &card->wandev;
  228. card->next = NULL;
  229. sprintf(card->devname, "%s%d", drvname, cnt + 1);
  230. wandev->magic = ROUTER_MAGIC;
  231. wandev->name = card->devname;
  232. wandev->private = card;
  233. wandev->enable_tx_int = 0;
  234. wandev->setup = &setup;
  235. wandev->shutdown = &shutdown;
  236. wandev->ioctl = &ioctl;
  237. err = register_wan_device(wandev);
  238. if (err) {
  239. printk(KERN_INFO
  240. "%s: %s registration failed with error %d!\n",
  241. drvname, card->devname, err);
  242. break;
  243. }
  244. }
  245. if (cnt){
  246. ncards = cnt; /* adjust actual number of cards */
  247. }else {
  248. kfree(card_array);
  249. destroy_workqueue(wanpipe_wq);
  250. printk(KERN_INFO "IN Init Module: NO Cards registered\n");
  251. err = -ENODEV;
  252. }
  253. return err;
  254. }
  255. /*============================================================================
  256. * Module 'remove' entry point.
  257. * o unregister all adapters from the WAN router
  258. * o release all remaining system resources
  259. */
  260. static void __exit wanpipe_cleanup(void)
  261. {
  262. int i;
  263. if (!ncards)
  264. return;
  265. for (i = 0; i < ncards; ++i) {
  266. sdla_t* card = &card_array[i];
  267. unregister_wan_device(card->devname);
  268. }
  269. destroy_workqueue(wanpipe_wq);
  270. kfree(card_array);
  271. printk(KERN_INFO "\nwanpipe: WANPIPE Modules Unloaded.\n");
  272. }
  273. module_init(wanpipe_init);
  274. module_exit(wanpipe_cleanup);
  275. /******* WAN Device Driver Entry Points *************************************/
  276. /*============================================================================
  277. * Setup/configure WAN link driver.
  278. * o check adapter state
  279. * o make sure firmware is present in configuration
  280. * o make sure I/O port and IRQ are specified
  281. * o make sure I/O region is available
  282. * o allocate interrupt vector
  283. * o setup SDLA hardware
  284. * o call appropriate routine to perform protocol-specific initialization
  285. * o mark I/O region as used
  286. * o if this is the first active card, then schedule background task
  287. *
  288. * This function is called when router handles ROUTER_SETUP IOCTL. The
  289. * configuration structure is in kernel memory (including extended data, if
  290. * any).
  291. */
  292. static int setup(struct wan_device* wandev, wandev_conf_t* conf)
  293. {
  294. sdla_t* card;
  295. int err = 0;
  296. int irq=0;
  297. /* Sanity checks */
  298. if ((wandev == NULL) || (wandev->private == NULL) || (conf == NULL)){
  299. printk(KERN_INFO
  300. "%s: Failed Sdlamain Setup wandev %u, card %u, conf %u !\n",
  301. wandev->name,
  302. (unsigned int)wandev,(unsigned int)wandev->private,
  303. (unsigned int)conf);
  304. return -EFAULT;
  305. }
  306. printk(KERN_INFO "%s: Starting WAN Setup\n", wandev->name);
  307. card = wandev->private;
  308. if (wandev->state != WAN_UNCONFIGURED){
  309. printk(KERN_INFO "%s: failed sdlamain setup, busy!\n",
  310. wandev->name);
  311. return -EBUSY; /* already configured */
  312. }
  313. printk(KERN_INFO "\nProcessing WAN device %s...\n", wandev->name);
  314. /* Initialize the counters for each wandev
  315. * Used for counting number of times new_if and
  316. * del_if get called.
  317. */
  318. wandev->del_if_cnt = 0;
  319. wandev->new_if_cnt = 0;
  320. wandev->config_id = conf->config_id;
  321. if (!conf->data_size || (conf->data == NULL)) {
  322. printk(KERN_INFO
  323. "%s: firmware not found in configuration data!\n",
  324. wandev->name);
  325. return -EINVAL;
  326. }
  327. /* Check for resource conflicts and setup the
  328. * card for piggibacking if necessary */
  329. if(!conf->S514_CPU_no[0]) {
  330. if ((err=check_s508_conflicts(card,conf,&irq)) != 0){
  331. return err;
  332. }
  333. }else {
  334. if ((err=check_s514_conflicts(card,conf,&irq)) != 0){
  335. return err;
  336. }
  337. }
  338. /* If the current card has already been configured
  339. * or it's a piggyback card, do not try to allocate
  340. * resources.
  341. */
  342. if (!card->wandev.piggyback && !card->configured){
  343. /* Configure hardware, load firmware, etc. */
  344. memset(&card->hw, 0, sizeof(sdlahw_t));
  345. /* for an S514 adapter, pass the CPU number and the slot number read */
  346. /* from 'router.conf' to the 'sdla_setup()' function via the 'port' */
  347. /* parameter */
  348. if (conf->S514_CPU_no[0]){
  349. card->hw.S514_cpu_no[0] = conf->S514_CPU_no[0];
  350. card->hw.S514_slot_no = conf->PCI_slot_no;
  351. card->hw.auto_pci_cfg = conf->auto_pci_cfg;
  352. if (card->hw.auto_pci_cfg == WANOPT_YES){
  353. printk(KERN_INFO "%s: Setting CPU to %c and Slot to Auto\n",
  354. card->devname, card->hw.S514_cpu_no[0]);
  355. }else{
  356. printk(KERN_INFO "%s: Setting CPU to %c and Slot to %i\n",
  357. card->devname, card->hw.S514_cpu_no[0], card->hw.S514_slot_no);
  358. }
  359. }else{
  360. /* 508 Card io port and irq initialization */
  361. card->hw.port = conf->ioport;
  362. card->hw.irq = (conf->irq == 9) ? 2 : conf->irq;
  363. }
  364. /* Compute the virtual address of the card in kernel space */
  365. if(conf->maddr){
  366. card->hw.dpmbase = phys_to_virt(conf->maddr);
  367. }else{
  368. card->hw.dpmbase = (void *)conf->maddr;
  369. }
  370. card->hw.dpmsize = SDLA_WINDOWSIZE;
  371. /* set the adapter type if using an S514 adapter */
  372. card->hw.type = (conf->S514_CPU_no[0]) ? SDLA_S514 : conf->hw_opt[0];
  373. card->hw.pclk = conf->hw_opt[1];
  374. err = sdla_setup(&card->hw, conf->data, conf->data_size);
  375. if (err){
  376. printk(KERN_INFO "%s: Hardware setup Failed %i\n",
  377. card->devname,err);
  378. return err;
  379. }
  380. if(card->hw.type != SDLA_S514)
  381. irq = (conf->irq == 2) ? 9 : conf->irq; /* IRQ2 -> IRQ9 */
  382. else
  383. irq = card->hw.irq;
  384. /* request an interrupt vector - note that interrupts may be shared */
  385. /* when using the S514 PCI adapter */
  386. if(request_irq(irq, sdla_isr,
  387. (card->hw.type == SDLA_S514) ? SA_SHIRQ : 0,
  388. wandev->name, card)){
  389. printk(KERN_INFO "%s: Can't reserve IRQ %d!\n", wandev->name, irq);
  390. return -EINVAL;
  391. }
  392. }else{
  393. printk(KERN_INFO "%s: Card Configured %lu or Piggybacking %i!\n",
  394. wandev->name,card->configured,card->wandev.piggyback);
  395. }
  396. if (!card->configured){
  397. /* Initialize the Spin lock */
  398. printk(KERN_INFO "%s: Initializing for SMP\n",wandev->name);
  399. /* Piggyback spin lock has already been initialized,
  400. * in check_s514/s508_conflicts() */
  401. if (!card->wandev.piggyback){
  402. spin_lock_init(&card->wandev.lock);
  403. }
  404. /* Intialize WAN device data space */
  405. wandev->irq = irq;
  406. wandev->dma = 0;
  407. if(card->hw.type != SDLA_S514){
  408. wandev->ioport = card->hw.port;
  409. }else{
  410. wandev->S514_cpu_no[0] = card->hw.S514_cpu_no[0];
  411. wandev->S514_slot_no = card->hw.S514_slot_no;
  412. }
  413. wandev->maddr = (unsigned long)card->hw.dpmbase;
  414. wandev->msize = card->hw.dpmsize;
  415. wandev->hw_opt[0] = card->hw.type;
  416. wandev->hw_opt[1] = card->hw.pclk;
  417. wandev->hw_opt[2] = card->hw.memory;
  418. wandev->hw_opt[3] = card->hw.fwid;
  419. }
  420. /* Protocol-specific initialization */
  421. switch (card->hw.fwid) {
  422. case SFID_X25_502:
  423. case SFID_X25_508:
  424. printk(KERN_INFO "%s: Starting X.25 Protocol Init.\n",
  425. card->devname);
  426. err = wpx_init(card, conf);
  427. break;
  428. case SFID_FR502:
  429. case SFID_FR508:
  430. printk(KERN_INFO "%s: Starting Frame Relay Protocol Init.\n",
  431. card->devname);
  432. err = wpf_init(card, conf);
  433. break;
  434. case SFID_PPP502:
  435. case SFID_PPP508:
  436. printk(KERN_INFO "%s: Starting PPP Protocol Init.\n",
  437. card->devname);
  438. err = wpp_init(card, conf);
  439. break;
  440. case SFID_CHDLC508:
  441. case SFID_CHDLC514:
  442. if (conf->ft1){
  443. printk(KERN_INFO "%s: Starting FT1 CSU/DSU Config Driver.\n",
  444. card->devname);
  445. err = wpft1_init(card, conf);
  446. break;
  447. }else if (conf->config_id == WANCONFIG_MPPP){
  448. printk(KERN_INFO "%s: Starting Multi-Port PPP Protocol Init.\n",
  449. card->devname);
  450. err = wsppp_init(card,conf);
  451. break;
  452. }else{
  453. printk(KERN_INFO "%s: Starting CHDLC Protocol Init.\n",
  454. card->devname);
  455. err = wpc_init(card, conf);
  456. break;
  457. }
  458. default:
  459. printk(KERN_INFO "%s: Error, Firmware is not supported %X %X!\n",
  460. wandev->name,card->hw.fwid,SFID_CHDLC508);
  461. err = -EPROTONOSUPPORT;
  462. }
  463. if (err != 0){
  464. if (err == -EPROTONOSUPPORT){
  465. printk(KERN_INFO
  466. "%s: Error, Protocol selected has not been compiled!\n",
  467. card->devname);
  468. printk(KERN_INFO
  469. "%s: Re-configure the kernel and re-build the modules!\n",
  470. card->devname);
  471. }
  472. release_hw(card);
  473. wandev->state = WAN_UNCONFIGURED;
  474. return err;
  475. }
  476. /* Reserve I/O region and schedule background task */
  477. if(card->hw.type != SDLA_S514 && !card->wandev.piggyback)
  478. if (!request_region(card->hw.port, card->hw.io_range,
  479. wandev->name)) {
  480. printk(KERN_WARNING "port 0x%04x busy\n", card->hw.port);
  481. release_hw(card);
  482. wandev->state = WAN_UNCONFIGURED;
  483. return -EBUSY;
  484. }
  485. /* Only use the polling routine for the X25 protocol */
  486. card->wandev.critical=0;
  487. return 0;
  488. }
  489. /*==================================================================
  490. * configure_s508_card
  491. *
  492. * For a S508 adapter, check for a possible configuration error in that
  493. * we are loading an adapter in the same IO port as a previously loaded S508
  494. * card.
  495. */
  496. static int check_s508_conflicts (sdla_t* card,wandev_conf_t* conf, int *irq)
  497. {
  498. unsigned long smp_flags;
  499. int i;
  500. if (conf->ioport <= 0) {
  501. printk(KERN_INFO
  502. "%s: can't configure without I/O port address!\n",
  503. card->wandev.name);
  504. return -EINVAL;
  505. }
  506. if (conf->irq <= 0) {
  507. printk(KERN_INFO "%s: can't configure without IRQ!\n",
  508. card->wandev.name);
  509. return -EINVAL;
  510. }
  511. if (test_bit(0,&card->configured))
  512. return 0;
  513. /* Check for already loaded card with the same IO port and IRQ
  514. * If found, copy its hardware configuration and use its
  515. * resources (i.e. piggybacking)
  516. */
  517. for (i = 0; i < ncards; i++) {
  518. sdla_t *nxt_card = &card_array[i];
  519. /* Skip the current card ptr */
  520. if (nxt_card == card)
  521. continue;
  522. /* Find a card that is already configured with the
  523. * same IO Port */
  524. if ((nxt_card->hw.type == SDLA_S508) &&
  525. (nxt_card->hw.port == conf->ioport) &&
  526. (nxt_card->next == NULL)){
  527. /* We found a card the card that has same configuration
  528. * as us. This means, that we must setup this card in
  529. * piggibacking mode. However, only CHDLC and MPPP protocol
  530. * support this setup */
  531. if ((conf->config_id == WANCONFIG_CHDLC ||
  532. conf->config_id == WANCONFIG_MPPP) &&
  533. (nxt_card->wandev.config_id == WANCONFIG_CHDLC ||
  534. nxt_card->wandev.config_id == WANCONFIG_MPPP)){
  535. *irq = nxt_card->hw.irq;
  536. memcpy(&card->hw, &nxt_card->hw, sizeof(sdlahw_t));
  537. /* The master could already be running, we must
  538. * set this as a critical area */
  539. lock_adapter_irq(&nxt_card->wandev.lock, &smp_flags);
  540. nxt_card->next = card;
  541. card->next = nxt_card;
  542. card->wandev.piggyback = WANOPT_YES;
  543. /* We must initialise the piggiback spin lock here
  544. * since isr will try to lock card->next if it
  545. * exists */
  546. spin_lock_init(&card->wandev.lock);
  547. unlock_adapter_irq(&nxt_card->wandev.lock, &smp_flags);
  548. break;
  549. }else{
  550. /* Trying to run piggibacking with a wrong protocol */
  551. printk(KERN_INFO "%s: ERROR: Resource busy, ioport: 0x%x\n"
  552. "%s: This protocol doesn't support\n"
  553. "%s: multi-port operation!\n",
  554. card->devname,nxt_card->hw.port,
  555. card->devname,card->devname);
  556. return -EEXIST;
  557. }
  558. }
  559. }
  560. /* Make sure I/O port region is available only if we are the
  561. * master device. If we are running in piggybacking mode,
  562. * we will use the resources of the master card. */
  563. if (!card->wandev.piggyback) {
  564. struct resource *rr =
  565. request_region(conf->ioport, SDLA_MAXIORANGE, "sdlamain");
  566. release_region(conf->ioport, SDLA_MAXIORANGE);
  567. if (!rr) {
  568. printk(KERN_INFO
  569. "%s: I/O region 0x%X - 0x%X is in use!\n",
  570. card->wandev.name, conf->ioport,
  571. conf->ioport + SDLA_MAXIORANGE - 1);
  572. return -EINVAL;
  573. }
  574. }
  575. return 0;
  576. }
  577. /*==================================================================
  578. * configure_s514_card
  579. *
  580. * For a S514 adapter, check for a possible configuration error in that
  581. * we are loading an adapter in the same slot as a previously loaded S514
  582. * card.
  583. */
  584. static int check_s514_conflicts(sdla_t* card,wandev_conf_t* conf, int *irq)
  585. {
  586. unsigned long smp_flags;
  587. int i;
  588. if (test_bit(0,&card->configured))
  589. return 0;
  590. /* Check for already loaded card with the same IO port and IRQ
  591. * If found, copy its hardware configuration and use its
  592. * resources (i.e. piggybacking)
  593. */
  594. for (i = 0; i < ncards; i ++) {
  595. sdla_t* nxt_card = &card_array[i];
  596. if(nxt_card == card)
  597. continue;
  598. if((nxt_card->hw.type == SDLA_S514) &&
  599. (nxt_card->hw.S514_slot_no == conf->PCI_slot_no) &&
  600. (nxt_card->hw.S514_cpu_no[0] == conf->S514_CPU_no[0])&&
  601. (nxt_card->next == NULL)){
  602. if ((conf->config_id == WANCONFIG_CHDLC ||
  603. conf->config_id == WANCONFIG_MPPP) &&
  604. (nxt_card->wandev.config_id == WANCONFIG_CHDLC ||
  605. nxt_card->wandev.config_id == WANCONFIG_MPPP)){
  606. *irq = nxt_card->hw.irq;
  607. memcpy(&card->hw, &nxt_card->hw, sizeof(sdlahw_t));
  608. /* The master could already be running, we must
  609. * set this as a critical area */
  610. lock_adapter_irq(&nxt_card->wandev.lock,&smp_flags);
  611. nxt_card->next = card;
  612. card->next = nxt_card;
  613. card->wandev.piggyback = WANOPT_YES;
  614. /* We must initialise the piggiback spin lock here
  615. * since isr will try to lock card->next if it
  616. * exists */
  617. spin_lock_init(&card->wandev.lock);
  618. unlock_adapter_irq(&nxt_card->wandev.lock,&smp_flags);
  619. }else{
  620. /* Trying to run piggibacking with a wrong protocol */
  621. printk(KERN_INFO "%s: ERROR: Resource busy: CPU %c PCISLOT %i\n"
  622. "%s: This protocol doesn't support\n"
  623. "%s: multi-port operation!\n",
  624. card->devname,
  625. conf->S514_CPU_no[0],conf->PCI_slot_no,
  626. card->devname,card->devname);
  627. return -EEXIST;
  628. }
  629. }
  630. }
  631. return 0;
  632. }
  633. /*============================================================================
  634. * Shut down WAN link driver.
  635. * o shut down adapter hardware
  636. * o release system resources.
  637. *
  638. * This function is called by the router when device is being unregistered or
  639. * when it handles ROUTER_DOWN IOCTL.
  640. */
  641. static int shutdown(struct wan_device* wandev)
  642. {
  643. sdla_t *card;
  644. int err=0;
  645. /* sanity checks */
  646. if ((wandev == NULL) || (wandev->private == NULL)){
  647. return -EFAULT;
  648. }
  649. if (wandev->state == WAN_UNCONFIGURED){
  650. return 0;
  651. }
  652. card = wandev->private;
  653. if (card->tty_opt){
  654. if (card->tty_open){
  655. printk(KERN_INFO
  656. "%s: Shutdown Failed: TTY is still open\n",
  657. card->devname);
  658. return -EBUSY;
  659. }
  660. }
  661. wandev->state = WAN_UNCONFIGURED;
  662. set_bit(PERI_CRIT,(void*)&wandev->critical);
  663. /* In case of piggibacking, make sure that
  664. * we never try to shutdown both devices at the same
  665. * time, because they depend on one another */
  666. if (card->disable_comm){
  667. card->disable_comm(card);
  668. }
  669. /* Release Resources */
  670. release_hw(card);
  671. /* only free the allocated I/O range if not an S514 adapter */
  672. if (wandev->hw_opt[0] != SDLA_S514 && !card->configured){
  673. release_region(card->hw.port, card->hw.io_range);
  674. }
  675. if (!card->configured){
  676. memset(&card->hw, 0, sizeof(sdlahw_t));
  677. if (card->next){
  678. memset(&card->next->hw, 0, sizeof(sdlahw_t));
  679. }
  680. }
  681. clear_bit(PERI_CRIT,(void*)&wandev->critical);
  682. return err;
  683. }
  684. static void release_hw (sdla_t *card)
  685. {
  686. sdla_t *nxt_card;
  687. /* Check if next device exists */
  688. if (card->next){
  689. nxt_card = card->next;
  690. /* If next device is down then release resources */
  691. if (nxt_card->wandev.state == WAN_UNCONFIGURED){
  692. if (card->wandev.piggyback){
  693. /* If this device is piggyback then use
  694. * information of the master device
  695. */
  696. printk(KERN_INFO "%s: Piggyback shutting down\n",card->devname);
  697. sdla_down(&card->next->hw);
  698. free_irq(card->wandev.irq, card->next);
  699. card->configured = 0;
  700. card->next->configured = 0;
  701. card->wandev.piggyback = 0;
  702. }else{
  703. /* Master device shutting down */
  704. printk(KERN_INFO "%s: Master shutting down\n",card->devname);
  705. sdla_down(&card->hw);
  706. free_irq(card->wandev.irq, card);
  707. card->configured = 0;
  708. card->next->configured = 0;
  709. }
  710. }else{
  711. printk(KERN_INFO "%s: Device still running %i\n",
  712. nxt_card->devname,nxt_card->wandev.state);
  713. card->configured = 1;
  714. }
  715. }else{
  716. printk(KERN_INFO "%s: Master shutting down\n",card->devname);
  717. sdla_down(&card->hw);
  718. free_irq(card->wandev.irq, card);
  719. card->configured = 0;
  720. }
  721. return;
  722. }
  723. /*============================================================================
  724. * Driver I/O control.
  725. * o verify arguments
  726. * o perform requested action
  727. *
  728. * This function is called when router handles one of the reserved user
  729. * IOCTLs. Note that 'arg' stil points to user address space.
  730. */
  731. static int ioctl(struct wan_device* wandev, unsigned cmd, unsigned long arg)
  732. {
  733. sdla_t* card;
  734. int err;
  735. /* sanity checks */
  736. if ((wandev == NULL) || (wandev->private == NULL))
  737. return -EFAULT;
  738. if (wandev->state == WAN_UNCONFIGURED)
  739. return -ENODEV;
  740. card = wandev->private;
  741. if(card->hw.type != SDLA_S514){
  742. disable_irq(card->hw.irq);
  743. }
  744. if (test_bit(SEND_CRIT, (void*)&wandev->critical)) {
  745. return -EAGAIN;
  746. }
  747. switch (cmd) {
  748. case WANPIPE_DUMP:
  749. err = ioctl_dump(wandev->private, (void*)arg);
  750. break;
  751. case WANPIPE_EXEC:
  752. err = ioctl_exec(wandev->private, (void*)arg, cmd);
  753. break;
  754. default:
  755. err = -EINVAL;
  756. }
  757. return err;
  758. }
  759. /****** Driver IOCTL Handlers ***********************************************/
  760. /*============================================================================
  761. * Dump adapter memory to user buffer.
  762. * o verify request structure
  763. * o copy request structure to kernel data space
  764. * o verify length/offset
  765. * o verify user buffer
  766. * o copy adapter memory image to user buffer
  767. *
  768. * Note: when dumping memory, this routine switches curent dual-port memory
  769. * vector, so care must be taken to avoid racing conditions.
  770. */
  771. static int ioctl_dump (sdla_t* card, sdla_dump_t* u_dump)
  772. {
  773. sdla_dump_t dump;
  774. unsigned winsize;
  775. unsigned long oldvec; /* DPM window vector */
  776. unsigned long smp_flags;
  777. int err = 0;
  778. if(copy_from_user((void*)&dump, (void*)u_dump, sizeof(sdla_dump_t)))
  779. return -EFAULT;
  780. if ((dump.magic != WANPIPE_MAGIC) ||
  781. (dump.offset + dump.length > card->hw.memory))
  782. return -EINVAL;
  783. winsize = card->hw.dpmsize;
  784. if(card->hw.type != SDLA_S514) {
  785. lock_adapter_irq(&card->wandev.lock, &smp_flags);
  786. oldvec = card->hw.vector;
  787. while (dump.length) {
  788. /* current offset */
  789. unsigned pos = dump.offset % winsize;
  790. /* current vector */
  791. unsigned long vec = dump.offset - pos;
  792. unsigned len = (dump.length > (winsize - pos)) ?
  793. (winsize - pos) : dump.length;
  794. /* relocate window */
  795. if (sdla_mapmem(&card->hw, vec) != 0) {
  796. err = -EIO;
  797. break;
  798. }
  799. if(copy_to_user((void *)dump.ptr,
  800. (u8 *)card->hw.dpmbase + pos, len)){
  801. unlock_adapter_irq(&card->wandev.lock, &smp_flags);
  802. return -EFAULT;
  803. }
  804. dump.length -= len;
  805. dump.offset += len;
  806. dump.ptr = (char*)dump.ptr + len;
  807. }
  808. sdla_mapmem(&card->hw, oldvec);/* restore DPM window position */
  809. unlock_adapter_irq(&card->wandev.lock, &smp_flags);
  810. }else {
  811. if(copy_to_user((void *)dump.ptr,
  812. (u8 *)card->hw.dpmbase + dump.offset, dump.length)){
  813. return -EFAULT;
  814. }
  815. }
  816. return err;
  817. }
  818. /*============================================================================
  819. * Execute adapter firmware command.
  820. * o verify request structure
  821. * o copy request structure to kernel data space
  822. * o call protocol-specific 'exec' function
  823. */
  824. static int ioctl_exec (sdla_t* card, sdla_exec_t* u_exec, int cmd)
  825. {
  826. sdla_exec_t exec;
  827. int err=0;
  828. if (card->exec == NULL && cmd == WANPIPE_EXEC){
  829. return -ENODEV;
  830. }
  831. if(copy_from_user((void*)&exec, (void*)u_exec, sizeof(sdla_exec_t)))
  832. return -EFAULT;
  833. if ((exec.magic != WANPIPE_MAGIC) || (exec.cmd == NULL))
  834. return -EINVAL;
  835. switch (cmd) {
  836. case WANPIPE_EXEC:
  837. err = card->exec(card, exec.cmd, exec.data);
  838. break;
  839. }
  840. return err;
  841. }
  842. /******* Miscellaneous ******************************************************/
  843. /*============================================================================
  844. * SDLA Interrupt Service Routine.
  845. * o acknowledge SDLA hardware interrupt.
  846. * o call protocol-specific interrupt service routine, if any.
  847. */
  848. STATIC irqreturn_t sdla_isr (int irq, void* dev_id, struct pt_regs *regs)
  849. {
  850. #define card ((sdla_t*)dev_id)
  851. if(card->hw.type == SDLA_S514) { /* handle interrrupt on S514 */
  852. u32 int_status;
  853. unsigned char CPU_no = card->hw.S514_cpu_no[0];
  854. unsigned char card_found_for_IRQ;
  855. u8 IRQ_count = 0;
  856. for(;;) {
  857. read_S514_int_stat(&card->hw, &int_status);
  858. /* check if the interrupt is for this device */
  859. if(!((unsigned char)int_status &
  860. (IRQ_CPU_A | IRQ_CPU_B)))
  861. return IRQ_HANDLED;
  862. /* if the IRQ is for both CPUs on the same adapter, */
  863. /* then alter the interrupt status so as to handle */
  864. /* one CPU at a time */
  865. if(((unsigned char)int_status & (IRQ_CPU_A | IRQ_CPU_B))
  866. == (IRQ_CPU_A | IRQ_CPU_B)) {
  867. int_status &= (CPU_no == S514_CPU_A) ?
  868. ~IRQ_CPU_B : ~IRQ_CPU_A;
  869. }
  870. card_found_for_IRQ = 0;
  871. /* check to see that the CPU number for this device */
  872. /* corresponds to the interrupt status read */
  873. switch (CPU_no) {
  874. case S514_CPU_A:
  875. if((unsigned char)int_status &
  876. IRQ_CPU_A)
  877. card_found_for_IRQ = 1;
  878. break;
  879. case S514_CPU_B:
  880. if((unsigned char)int_status &
  881. IRQ_CPU_B)
  882. card_found_for_IRQ = 1;
  883. break;
  884. }
  885. /* exit if the interrupt is for another CPU on the */
  886. /* same IRQ */
  887. if(!card_found_for_IRQ)
  888. return IRQ_HANDLED;
  889. if (!card ||
  890. (card->wandev.state == WAN_UNCONFIGURED && !card->configured)){
  891. printk(KERN_INFO
  892. "Received IRQ %d for CPU #%c\n",
  893. irq, CPU_no);
  894. printk(KERN_INFO
  895. "IRQ for unconfigured adapter\n");
  896. S514_intack(&card->hw, int_status);
  897. return IRQ_HANDLED;
  898. }
  899. if (card->in_isr) {
  900. printk(KERN_INFO
  901. "%s: interrupt re-entrancy on IRQ %d\n",
  902. card->devname, card->wandev.irq);
  903. S514_intack(&card->hw, int_status);
  904. return IRQ_HANDLED;
  905. }
  906. spin_lock(&card->wandev.lock);
  907. if (card->next){
  908. spin_lock(&card->next->wandev.lock);
  909. }
  910. S514_intack(&card->hw, int_status);
  911. if (card->isr)
  912. card->isr(card);
  913. if (card->next){
  914. spin_unlock(&card->next->wandev.lock);
  915. }
  916. spin_unlock(&card->wandev.lock);
  917. /* handle a maximum of two interrupts (one for each */
  918. /* CPU on the adapter) before returning */
  919. if((++ IRQ_count) == 2)
  920. return IRQ_HANDLED;
  921. }
  922. }
  923. else { /* handle interrupt on S508 adapter */
  924. if (!card || ((card->wandev.state == WAN_UNCONFIGURED) && !card->configured))
  925. return IRQ_HANDLED;
  926. if (card->in_isr) {
  927. printk(KERN_INFO
  928. "%s: interrupt re-entrancy on IRQ %d!\n",
  929. card->devname, card->wandev.irq);
  930. return IRQ_HANDLED;
  931. }
  932. spin_lock(&card->wandev.lock);
  933. if (card->next){
  934. spin_lock(&card->next->wandev.lock);
  935. }
  936. sdla_intack(&card->hw);
  937. if (card->isr)
  938. card->isr(card);
  939. if (card->next){
  940. spin_unlock(&card->next->wandev.lock);
  941. }
  942. spin_unlock(&card->wandev.lock);
  943. }
  944. return IRQ_HANDLED;
  945. #undef card
  946. }
  947. /*============================================================================
  948. * This routine is called by the protocol-specific modules when network
  949. * interface is being open. The only reason we need this, is because we
  950. * have to call MOD_INC_USE_COUNT, but cannot include 'module.h' where it's
  951. * defined more than once into the same kernel module.
  952. */
  953. void wanpipe_open (sdla_t* card)
  954. {
  955. ++card->open_cnt;
  956. }
  957. /*============================================================================
  958. * This routine is called by the protocol-specific modules when network
  959. * interface is being closed. The only reason we need this, is because we
  960. * have to call MOD_DEC_USE_COUNT, but cannot include 'module.h' where it's
  961. * defined more than once into the same kernel module.
  962. */
  963. void wanpipe_close (sdla_t* card)
  964. {
  965. --card->open_cnt;
  966. }
  967. /*============================================================================
  968. * Set WAN device state.
  969. */
  970. void wanpipe_set_state (sdla_t* card, int state)
  971. {
  972. if (card->wandev.state != state) {
  973. switch (state) {
  974. case WAN_CONNECTED:
  975. printk (KERN_INFO "%s: link connected!\n",
  976. card->devname);
  977. break;
  978. case WAN_CONNECTING:
  979. printk (KERN_INFO "%s: link connecting...\n",
  980. card->devname);
  981. break;
  982. case WAN_DISCONNECTED:
  983. printk (KERN_INFO "%s: link disconnected!\n",
  984. card->devname);
  985. break;
  986. }
  987. card->wandev.state = state;
  988. }
  989. card->state_tick = jiffies;
  990. }
  991. sdla_t * wanpipe_find_card (char *name)
  992. {
  993. int cnt;
  994. for (cnt = 0; cnt < ncards; ++ cnt) {
  995. sdla_t* card = &card_array[cnt];
  996. if (!strcmp(card->devname,name))
  997. return card;
  998. }
  999. return NULL;
  1000. }
  1001. sdla_t * wanpipe_find_card_num (int num)
  1002. {
  1003. if (num < 1 || num > ncards)
  1004. return NULL;
  1005. num--;
  1006. return &card_array[num];
  1007. }
  1008. /*
  1009. * @work_pointer: work_struct to be done;
  1010. * should already have PREPARE_WORK() or
  1011. * INIT_WORK() done on it by caller;
  1012. */
  1013. void wanpipe_queue_work (struct work_struct *work_pointer)
  1014. {
  1015. if (test_and_set_bit(1, (void*)&wanpipe_bh_critical))
  1016. printk(KERN_INFO "CRITICAL IN QUEUING WORK\n");
  1017. queue_work(wanpipe_wq, work_pointer);
  1018. clear_bit(1,(void*)&wanpipe_bh_critical);
  1019. }
  1020. void wakeup_sk_bh(struct net_device *dev)
  1021. {
  1022. wanpipe_common_t *chan = dev->priv;
  1023. if (test_bit(0,&chan->common_critical))
  1024. return;
  1025. if (chan->sk && chan->tx_timer){
  1026. chan->tx_timer->expires=jiffies+1;
  1027. add_timer(chan->tx_timer);
  1028. }
  1029. }
  1030. int change_dev_flags(struct net_device *dev, unsigned flags)
  1031. {
  1032. struct ifreq if_info;
  1033. mm_segment_t fs = get_fs();
  1034. int err;
  1035. memset(&if_info, 0, sizeof(if_info));
  1036. strcpy(if_info.ifr_name, dev->name);
  1037. if_info.ifr_flags = flags;
  1038. set_fs(get_ds()); /* get user space block */
  1039. err = devinet_ioctl(SIOCSIFFLAGS, &if_info);
  1040. set_fs(fs);
  1041. return err;
  1042. }
  1043. unsigned long get_ip_address(struct net_device *dev, int option)
  1044. {
  1045. struct in_ifaddr *ifaddr;
  1046. struct in_device *in_dev;
  1047. if ((in_dev = __in_dev_get(dev)) == NULL){
  1048. return 0;
  1049. }
  1050. if ((ifaddr = in_dev->ifa_list)== NULL ){
  1051. return 0;
  1052. }
  1053. switch (option){
  1054. case WAN_LOCAL_IP:
  1055. return ifaddr->ifa_local;
  1056. break;
  1057. case WAN_POINTOPOINT_IP:
  1058. return ifaddr->ifa_address;
  1059. break;
  1060. case WAN_NETMASK_IP:
  1061. return ifaddr->ifa_mask;
  1062. break;
  1063. case WAN_BROADCAST_IP:
  1064. return ifaddr->ifa_broadcast;
  1065. break;
  1066. default:
  1067. return 0;
  1068. }
  1069. return 0;
  1070. }
  1071. void add_gateway(sdla_t *card, struct net_device *dev)
  1072. {
  1073. mm_segment_t oldfs;
  1074. struct rtentry route;
  1075. int res;
  1076. memset((char*)&route,0,sizeof(struct rtentry));
  1077. ((struct sockaddr_in *)
  1078. &(route.rt_dst))->sin_addr.s_addr = 0;
  1079. ((struct sockaddr_in *)
  1080. &(route.rt_dst))->sin_family = AF_INET;
  1081. ((struct sockaddr_in *)
  1082. &(route.rt_genmask))->sin_addr.s_addr = 0;
  1083. ((struct sockaddr_in *)
  1084. &(route.rt_genmask)) ->sin_family = AF_INET;
  1085. route.rt_flags = 0;
  1086. route.rt_dev = dev->name;
  1087. oldfs = get_fs();
  1088. set_fs(get_ds());
  1089. res = ip_rt_ioctl(SIOCADDRT,&route);
  1090. set_fs(oldfs);
  1091. if (res == 0){
  1092. printk(KERN_INFO "%s: Gateway added for %s\n",
  1093. card->devname,dev->name);
  1094. }
  1095. return;
  1096. }
  1097. MODULE_LICENSE("GPL");
  1098. /****** End *********************************************************/