icn.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696
  1. /* $Id: icn.c,v 1.65.6.8 2001/09/23 22:24:55 kai Exp $
  2. *
  3. * ISDN low-level module for the ICN active ISDN-Card.
  4. *
  5. * Copyright 1994,95,96 by Fritz Elfert (fritz@isdn4linux.de)
  6. *
  7. * This software may be used and distributed according to the terms
  8. * of the GNU General Public License, incorporated herein by reference.
  9. *
  10. */
  11. #include "icn.h"
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/slab.h>
  15. #include <linux/sched.h>
  16. static int portbase = ICN_BASEADDR;
  17. static unsigned long membase = ICN_MEMADDR;
  18. static char *icn_id = "\0";
  19. static char *icn_id2 = "\0";
  20. MODULE_DESCRIPTION("ISDN4Linux: Driver for ICN active ISDN card");
  21. MODULE_AUTHOR("Fritz Elfert");
  22. MODULE_LICENSE("GPL");
  23. module_param(portbase, int, 0);
  24. MODULE_PARM_DESC(portbase, "Port address of first card");
  25. module_param(membase, ulong, 0);
  26. MODULE_PARM_DESC(membase, "Shared memory address of all cards");
  27. module_param(icn_id, charp, 0);
  28. MODULE_PARM_DESC(icn_id, "ID-String of first card");
  29. module_param(icn_id2, charp, 0);
  30. MODULE_PARM_DESC(icn_id2, "ID-String of first card, second S0 (4B only)");
  31. /*
  32. * Verbose bootcode- and protocol-downloading.
  33. */
  34. #undef BOOT_DEBUG
  35. /*
  36. * Verbose Shmem-Mapping.
  37. */
  38. #undef MAP_DEBUG
  39. static char
  40. *revision = "$Revision: 1.65.6.8 $";
  41. static int icn_addcard(int, char *, char *);
  42. /*
  43. * Free send-queue completely.
  44. * Parameter:
  45. * card = pointer to card struct
  46. * channel = channel number
  47. */
  48. static void
  49. icn_free_queue(icn_card * card, int channel)
  50. {
  51. struct sk_buff_head *queue = &card->spqueue[channel];
  52. struct sk_buff *skb;
  53. skb_queue_purge(queue);
  54. card->xlen[channel] = 0;
  55. card->sndcount[channel] = 0;
  56. if ((skb = card->xskb[channel])) {
  57. card->xskb[channel] = NULL;
  58. dev_kfree_skb(skb);
  59. }
  60. }
  61. /* Put a value into a shift-register, highest bit first.
  62. * Parameters:
  63. * port = port for output (bit 0 is significant)
  64. * val = value to be output
  65. * firstbit = Bit-Number of highest bit
  66. * bitcount = Number of bits to output
  67. */
  68. static inline void
  69. icn_shiftout(unsigned short port,
  70. unsigned long val,
  71. int firstbit,
  72. int bitcount)
  73. {
  74. register u_char s;
  75. register u_char c;
  76. for (s = firstbit, c = bitcount; c > 0; s--, c--)
  77. OUTB_P((u_char) ((val >> s) & 1) ? 0xff : 0, port);
  78. }
  79. /*
  80. * disable a cards shared memory
  81. */
  82. static inline void
  83. icn_disable_ram(icn_card * card)
  84. {
  85. OUTB_P(0, ICN_MAPRAM);
  86. }
  87. /*
  88. * enable a cards shared memory
  89. */
  90. static inline void
  91. icn_enable_ram(icn_card * card)
  92. {
  93. OUTB_P(0xff, ICN_MAPRAM);
  94. }
  95. /*
  96. * Map a cards channel0 (Bank0/Bank8) or channel1 (Bank4/Bank12)
  97. *
  98. * must called with holding the devlock
  99. */
  100. static inline void
  101. icn_map_channel(icn_card * card, int channel)
  102. {
  103. #ifdef MAP_DEBUG
  104. printk(KERN_DEBUG "icn_map_channel %d %d\n", dev.channel, channel);
  105. #endif
  106. if ((channel == dev.channel) && (card == dev.mcard))
  107. return;
  108. if (dev.mcard)
  109. icn_disable_ram(dev.mcard);
  110. icn_shiftout(ICN_BANK, chan2bank[channel], 3, 4); /* Select Bank */
  111. icn_enable_ram(card);
  112. dev.mcard = card;
  113. dev.channel = channel;
  114. #ifdef MAP_DEBUG
  115. printk(KERN_DEBUG "icn_map_channel done\n");
  116. #endif
  117. }
  118. /*
  119. * Lock a cards channel.
  120. * Return 0 if requested card/channel is unmapped (failure).
  121. * Return 1 on success.
  122. *
  123. * must called with holding the devlock
  124. */
  125. static inline int
  126. icn_lock_channel(icn_card * card, int channel)
  127. {
  128. register int retval;
  129. #ifdef MAP_DEBUG
  130. printk(KERN_DEBUG "icn_lock_channel %d\n", channel);
  131. #endif
  132. if ((dev.channel == channel) && (card == dev.mcard)) {
  133. dev.chanlock++;
  134. retval = 1;
  135. #ifdef MAP_DEBUG
  136. printk(KERN_DEBUG "icn_lock_channel %d OK\n", channel);
  137. #endif
  138. } else {
  139. retval = 0;
  140. #ifdef MAP_DEBUG
  141. printk(KERN_DEBUG "icn_lock_channel %d FAILED, dc=%d\n", channel, dev.channel);
  142. #endif
  143. }
  144. return retval;
  145. }
  146. /*
  147. * Release current card/channel lock
  148. *
  149. * must called with holding the devlock
  150. */
  151. static inline void
  152. __icn_release_channel(void)
  153. {
  154. #ifdef MAP_DEBUG
  155. printk(KERN_DEBUG "icn_release_channel l=%d\n", dev.chanlock);
  156. #endif
  157. if (dev.chanlock > 0)
  158. dev.chanlock--;
  159. }
  160. /*
  161. * Release current card/channel lock
  162. */
  163. static inline void
  164. icn_release_channel(void)
  165. {
  166. ulong flags;
  167. spin_lock_irqsave(&dev.devlock, flags);
  168. __icn_release_channel();
  169. spin_unlock_irqrestore(&dev.devlock, flags);
  170. }
  171. /*
  172. * Try to map and lock a cards channel.
  173. * Return 1 on success, 0 on failure.
  174. */
  175. static inline int
  176. icn_trymaplock_channel(icn_card * card, int channel)
  177. {
  178. ulong flags;
  179. #ifdef MAP_DEBUG
  180. printk(KERN_DEBUG "trymaplock c=%d dc=%d l=%d\n", channel, dev.channel,
  181. dev.chanlock);
  182. #endif
  183. spin_lock_irqsave(&dev.devlock, flags);
  184. if ((!dev.chanlock) ||
  185. ((dev.channel == channel) && (dev.mcard == card))) {
  186. dev.chanlock++;
  187. icn_map_channel(card, channel);
  188. spin_unlock_irqrestore(&dev.devlock, flags);
  189. #ifdef MAP_DEBUG
  190. printk(KERN_DEBUG "trymaplock %d OK\n", channel);
  191. #endif
  192. return 1;
  193. }
  194. spin_unlock_irqrestore(&dev.devlock, flags);
  195. #ifdef MAP_DEBUG
  196. printk(KERN_DEBUG "trymaplock %d FAILED\n", channel);
  197. #endif
  198. return 0;
  199. }
  200. /*
  201. * Release current card/channel lock,
  202. * then map same or other channel without locking.
  203. */
  204. static inline void
  205. icn_maprelease_channel(icn_card * card, int channel)
  206. {
  207. ulong flags;
  208. #ifdef MAP_DEBUG
  209. printk(KERN_DEBUG "map_release c=%d l=%d\n", channel, dev.chanlock);
  210. #endif
  211. spin_lock_irqsave(&dev.devlock, flags);
  212. if (dev.chanlock > 0)
  213. dev.chanlock--;
  214. if (!dev.chanlock)
  215. icn_map_channel(card, channel);
  216. spin_unlock_irqrestore(&dev.devlock, flags);
  217. }
  218. /* Get Data from the B-Channel, assemble fragmented packets and put them
  219. * into receive-queue. Wake up any B-Channel-reading processes.
  220. * This routine is called via timer-callback from icn_pollbchan().
  221. */
  222. static void
  223. icn_pollbchan_receive(int channel, icn_card * card)
  224. {
  225. int mch = channel + ((card->secondhalf) ? 2 : 0);
  226. int eflag;
  227. int cnt;
  228. struct sk_buff *skb;
  229. if (icn_trymaplock_channel(card, mch)) {
  230. while (rbavl) {
  231. cnt = readb(&rbuf_l);
  232. if ((card->rcvidx[channel] + cnt) > 4000) {
  233. printk(KERN_WARNING
  234. "icn: (%s) bogus packet on ch%d, dropping.\n",
  235. CID,
  236. channel + 1);
  237. card->rcvidx[channel] = 0;
  238. eflag = 0;
  239. } else {
  240. memcpy_fromio(&card->rcvbuf[channel][card->rcvidx[channel]],
  241. &rbuf_d, cnt);
  242. card->rcvidx[channel] += cnt;
  243. eflag = readb(&rbuf_f);
  244. }
  245. rbnext;
  246. icn_maprelease_channel(card, mch & 2);
  247. if (!eflag) {
  248. if ((cnt = card->rcvidx[channel])) {
  249. if (!(skb = dev_alloc_skb(cnt))) {
  250. printk(KERN_WARNING "icn: receive out of memory\n");
  251. break;
  252. }
  253. memcpy(skb_put(skb, cnt), card->rcvbuf[channel], cnt);
  254. card->rcvidx[channel] = 0;
  255. card->interface.rcvcallb_skb(card->myid, channel, skb);
  256. }
  257. }
  258. if (!icn_trymaplock_channel(card, mch))
  259. break;
  260. }
  261. icn_maprelease_channel(card, mch & 2);
  262. }
  263. }
  264. /* Send data-packet to B-Channel, split it up into fragments of
  265. * ICN_FRAGSIZE length. If last fragment is sent out, signal
  266. * success to upper layers via statcallb with ISDN_STAT_BSENT argument.
  267. * This routine is called via timer-callback from icn_pollbchan() or
  268. * directly from icn_sendbuf().
  269. */
  270. static void
  271. icn_pollbchan_send(int channel, icn_card * card)
  272. {
  273. int mch = channel + ((card->secondhalf) ? 2 : 0);
  274. int cnt;
  275. unsigned long flags;
  276. struct sk_buff *skb;
  277. isdn_ctrl cmd;
  278. if (!(card->sndcount[channel] || card->xskb[channel] ||
  279. !skb_queue_empty(&card->spqueue[channel])))
  280. return;
  281. if (icn_trymaplock_channel(card, mch)) {
  282. while (sbfree &&
  283. (card->sndcount[channel] ||
  284. !skb_queue_empty(&card->spqueue[channel]) ||
  285. card->xskb[channel])) {
  286. spin_lock_irqsave(&card->lock, flags);
  287. if (card->xmit_lock[channel]) {
  288. spin_unlock_irqrestore(&card->lock, flags);
  289. break;
  290. }
  291. card->xmit_lock[channel]++;
  292. spin_unlock_irqrestore(&card->lock, flags);
  293. skb = card->xskb[channel];
  294. if (!skb) {
  295. skb = skb_dequeue(&card->spqueue[channel]);
  296. if (skb) {
  297. /* Pop ACK-flag off skb.
  298. * Store length to xlen.
  299. */
  300. if (*(skb_pull(skb,1)))
  301. card->xlen[channel] = skb->len;
  302. else
  303. card->xlen[channel] = 0;
  304. }
  305. }
  306. if (!skb)
  307. break;
  308. if (skb->len > ICN_FRAGSIZE) {
  309. writeb(0xff, &sbuf_f);
  310. cnt = ICN_FRAGSIZE;
  311. } else {
  312. writeb(0x0, &sbuf_f);
  313. cnt = skb->len;
  314. }
  315. writeb(cnt, &sbuf_l);
  316. memcpy_toio(&sbuf_d, skb->data, cnt);
  317. skb_pull(skb, cnt);
  318. sbnext; /* switch to next buffer */
  319. icn_maprelease_channel(card, mch & 2);
  320. spin_lock_irqsave(&card->lock, flags);
  321. card->sndcount[channel] -= cnt;
  322. if (!skb->len) {
  323. if (card->xskb[channel])
  324. card->xskb[channel] = NULL;
  325. card->xmit_lock[channel] = 0;
  326. spin_unlock_irqrestore(&card->lock, flags);
  327. dev_kfree_skb(skb);
  328. if (card->xlen[channel]) {
  329. cmd.command = ISDN_STAT_BSENT;
  330. cmd.driver = card->myid;
  331. cmd.arg = channel;
  332. cmd.parm.length = card->xlen[channel];
  333. card->interface.statcallb(&cmd);
  334. }
  335. } else {
  336. card->xskb[channel] = skb;
  337. card->xmit_lock[channel] = 0;
  338. spin_unlock_irqrestore(&card->lock, flags);
  339. }
  340. if (!icn_trymaplock_channel(card, mch))
  341. break;
  342. }
  343. icn_maprelease_channel(card, mch & 2);
  344. }
  345. }
  346. /* Send/Receive Data to/from the B-Channel.
  347. * This routine is called via timer-callback.
  348. * It schedules itself while any B-Channel is open.
  349. */
  350. static void
  351. icn_pollbchan(unsigned long data)
  352. {
  353. icn_card *card = (icn_card *) data;
  354. unsigned long flags;
  355. if (card->flags & ICN_FLAGS_B1ACTIVE) {
  356. icn_pollbchan_receive(0, card);
  357. icn_pollbchan_send(0, card);
  358. }
  359. if (card->flags & ICN_FLAGS_B2ACTIVE) {
  360. icn_pollbchan_receive(1, card);
  361. icn_pollbchan_send(1, card);
  362. }
  363. if (card->flags & (ICN_FLAGS_B1ACTIVE | ICN_FLAGS_B2ACTIVE)) {
  364. /* schedule b-channel polling again */
  365. spin_lock_irqsave(&card->lock, flags);
  366. mod_timer(&card->rb_timer, jiffies+ICN_TIMER_BCREAD);
  367. card->flags |= ICN_FLAGS_RBTIMER;
  368. spin_unlock_irqrestore(&card->lock, flags);
  369. } else
  370. card->flags &= ~ICN_FLAGS_RBTIMER;
  371. }
  372. typedef struct icn_stat {
  373. char *statstr;
  374. int command;
  375. int action;
  376. } icn_stat;
  377. /* *INDENT-OFF* */
  378. static icn_stat icn_stat_table[] =
  379. {
  380. {"BCON_", ISDN_STAT_BCONN, 1}, /* B-Channel connected */
  381. {"BDIS_", ISDN_STAT_BHUP, 2}, /* B-Channel disconnected */
  382. /*
  383. ** add d-channel connect and disconnect support to link-level
  384. */
  385. {"DCON_", ISDN_STAT_DCONN, 10}, /* D-Channel connected */
  386. {"DDIS_", ISDN_STAT_DHUP, 11}, /* D-Channel disconnected */
  387. {"DCAL_I", ISDN_STAT_ICALL, 3}, /* Incoming call dialup-line */
  388. {"DSCA_I", ISDN_STAT_ICALL, 3}, /* Incoming call 1TR6-SPV */
  389. {"FCALL", ISDN_STAT_ICALL, 4}, /* Leased line connection up */
  390. {"CIF", ISDN_STAT_CINF, 5}, /* Charge-info, 1TR6-type */
  391. {"AOC", ISDN_STAT_CINF, 6}, /* Charge-info, DSS1-type */
  392. {"CAU", ISDN_STAT_CAUSE, 7}, /* Cause code */
  393. {"TEI OK", ISDN_STAT_RUN, 0}, /* Card connected to wallplug */
  394. {"E_L1: ACT FAIL", ISDN_STAT_BHUP, 8}, /* Layer-1 activation failed */
  395. {"E_L2: DATA LIN", ISDN_STAT_BHUP, 8}, /* Layer-2 data link lost */
  396. {"E_L1: ACTIVATION FAILED",
  397. ISDN_STAT_BHUP, 8}, /* Layer-1 activation failed */
  398. {NULL, 0, -1}
  399. };
  400. /* *INDENT-ON* */
  401. /*
  402. * Check Statusqueue-Pointer from isdn-cards.
  403. * If there are new status-replies from the interface, check
  404. * them against B-Channel-connects/disconnects and set flags accordingly.
  405. * Wake-Up any processes, who are reading the status-device.
  406. * If there are B-Channels open, initiate a timer-callback to
  407. * icn_pollbchan().
  408. * This routine is called periodically via timer.
  409. */
  410. static void
  411. icn_parse_status(u_char * status, int channel, icn_card * card)
  412. {
  413. icn_stat *s = icn_stat_table;
  414. int action = -1;
  415. unsigned long flags;
  416. isdn_ctrl cmd;
  417. while (s->statstr) {
  418. if (!strncmp(status, s->statstr, strlen(s->statstr))) {
  419. cmd.command = s->command;
  420. action = s->action;
  421. break;
  422. }
  423. s++;
  424. }
  425. if (action == -1)
  426. return;
  427. cmd.driver = card->myid;
  428. cmd.arg = channel;
  429. switch (action) {
  430. case 11:
  431. spin_lock_irqsave(&card->lock, flags);
  432. icn_free_queue(card,channel);
  433. card->rcvidx[channel] = 0;
  434. if (card->flags &
  435. ((channel)?ICN_FLAGS_B2ACTIVE:ICN_FLAGS_B1ACTIVE)) {
  436. isdn_ctrl ncmd;
  437. card->flags &= ~((channel)?
  438. ICN_FLAGS_B2ACTIVE:ICN_FLAGS_B1ACTIVE);
  439. memset(&ncmd, 0, sizeof(ncmd));
  440. ncmd.driver = card->myid;
  441. ncmd.arg = channel;
  442. ncmd.command = ISDN_STAT_BHUP;
  443. spin_unlock_irqrestore(&card->lock, flags);
  444. card->interface.statcallb(&cmd);
  445. } else
  446. spin_unlock_irqrestore(&card->lock, flags);
  447. break;
  448. case 1:
  449. spin_lock_irqsave(&card->lock, flags);
  450. icn_free_queue(card,channel);
  451. card->flags |= (channel) ?
  452. ICN_FLAGS_B2ACTIVE : ICN_FLAGS_B1ACTIVE;
  453. spin_unlock_irqrestore(&card->lock, flags);
  454. break;
  455. case 2:
  456. spin_lock_irqsave(&card->lock, flags);
  457. card->flags &= ~((channel) ?
  458. ICN_FLAGS_B2ACTIVE : ICN_FLAGS_B1ACTIVE);
  459. icn_free_queue(card, channel);
  460. card->rcvidx[channel] = 0;
  461. spin_unlock_irqrestore(&card->lock, flags);
  462. break;
  463. case 3:
  464. {
  465. char *t = status + 6;
  466. char *s = strchr(t, ',');
  467. *s++ = '\0';
  468. strlcpy(cmd.parm.setup.phone, t,
  469. sizeof(cmd.parm.setup.phone));
  470. s = strchr(t = s, ',');
  471. *s++ = '\0';
  472. if (!strlen(t))
  473. cmd.parm.setup.si1 = 0;
  474. else
  475. cmd.parm.setup.si1 =
  476. simple_strtoul(t, NULL, 10);
  477. s = strchr(t = s, ',');
  478. *s++ = '\0';
  479. if (!strlen(t))
  480. cmd.parm.setup.si2 = 0;
  481. else
  482. cmd.parm.setup.si2 =
  483. simple_strtoul(t, NULL, 10);
  484. strlcpy(cmd.parm.setup.eazmsn, s,
  485. sizeof(cmd.parm.setup.eazmsn));
  486. }
  487. cmd.parm.setup.plan = 0;
  488. cmd.parm.setup.screen = 0;
  489. break;
  490. case 4:
  491. sprintf(cmd.parm.setup.phone, "LEASED%d", card->myid);
  492. sprintf(cmd.parm.setup.eazmsn, "%d", channel + 1);
  493. cmd.parm.setup.si1 = 7;
  494. cmd.parm.setup.si2 = 0;
  495. cmd.parm.setup.plan = 0;
  496. cmd.parm.setup.screen = 0;
  497. break;
  498. case 5:
  499. strlcpy(cmd.parm.num, status + 3, sizeof(cmd.parm.num));
  500. break;
  501. case 6:
  502. snprintf(cmd.parm.num, sizeof(cmd.parm.num), "%d",
  503. (int) simple_strtoul(status + 7, NULL, 16));
  504. break;
  505. case 7:
  506. status += 3;
  507. if (strlen(status) == 4)
  508. snprintf(cmd.parm.num, sizeof(cmd.parm.num), "%s%c%c",
  509. status + 2, *status, *(status + 1));
  510. else
  511. strlcpy(cmd.parm.num, status + 1, sizeof(cmd.parm.num));
  512. break;
  513. case 8:
  514. spin_lock_irqsave(&card->lock, flags);
  515. card->flags &= ~ICN_FLAGS_B1ACTIVE;
  516. icn_free_queue(card, 0);
  517. card->rcvidx[0] = 0;
  518. spin_unlock_irqrestore(&card->lock, flags);
  519. cmd.arg = 0;
  520. cmd.driver = card->myid;
  521. card->interface.statcallb(&cmd);
  522. cmd.command = ISDN_STAT_DHUP;
  523. cmd.arg = 0;
  524. cmd.driver = card->myid;
  525. card->interface.statcallb(&cmd);
  526. cmd.command = ISDN_STAT_BHUP;
  527. spin_lock_irqsave(&card->lock, flags);
  528. card->flags &= ~ICN_FLAGS_B2ACTIVE;
  529. icn_free_queue(card, 1);
  530. card->rcvidx[1] = 0;
  531. spin_unlock_irqrestore(&card->lock, flags);
  532. cmd.arg = 1;
  533. cmd.driver = card->myid;
  534. card->interface.statcallb(&cmd);
  535. cmd.command = ISDN_STAT_DHUP;
  536. cmd.arg = 1;
  537. cmd.driver = card->myid;
  538. break;
  539. }
  540. card->interface.statcallb(&cmd);
  541. return;
  542. }
  543. static void
  544. icn_putmsg(icn_card * card, unsigned char c)
  545. {
  546. ulong flags;
  547. spin_lock_irqsave(&card->lock, flags);
  548. *card->msg_buf_write++ = (c == 0xff) ? '\n' : c;
  549. if (card->msg_buf_write == card->msg_buf_read) {
  550. if (++card->msg_buf_read > card->msg_buf_end)
  551. card->msg_buf_read = card->msg_buf;
  552. }
  553. if (card->msg_buf_write > card->msg_buf_end)
  554. card->msg_buf_write = card->msg_buf;
  555. spin_unlock_irqrestore(&card->lock, flags);
  556. }
  557. static void
  558. icn_polldchan(unsigned long data)
  559. {
  560. icn_card *card = (icn_card *) data;
  561. int mch = card->secondhalf ? 2 : 0;
  562. int avail = 0;
  563. int left;
  564. u_char c;
  565. int ch;
  566. unsigned long flags;
  567. int i;
  568. u_char *p;
  569. isdn_ctrl cmd;
  570. if (icn_trymaplock_channel(card, mch)) {
  571. avail = msg_avail;
  572. for (left = avail, i = readb(&msg_o); left > 0; i++, left--) {
  573. c = readb(&dev.shmem->comm_buffers.iopc_buf[i & 0xff]);
  574. icn_putmsg(card, c);
  575. if (c == 0xff) {
  576. card->imsg[card->iptr] = 0;
  577. card->iptr = 0;
  578. if (card->imsg[0] == '0' && card->imsg[1] >= '0' &&
  579. card->imsg[1] <= '2' && card->imsg[2] == ';') {
  580. ch = (card->imsg[1] - '0') - 1;
  581. p = &card->imsg[3];
  582. icn_parse_status(p, ch, card);
  583. } else {
  584. p = card->imsg;
  585. if (!strncmp(p, "DRV1.", 5)) {
  586. u_char vstr[10];
  587. u_char *q = vstr;
  588. printk(KERN_INFO "icn: (%s) %s\n", CID, p);
  589. if (!strncmp(p + 7, "TC", 2)) {
  590. card->ptype = ISDN_PTYPE_1TR6;
  591. card->interface.features |= ISDN_FEATURE_P_1TR6;
  592. printk(KERN_INFO
  593. "icn: (%s) 1TR6-Protocol loaded and running\n", CID);
  594. }
  595. if (!strncmp(p + 7, "EC", 2)) {
  596. card->ptype = ISDN_PTYPE_EURO;
  597. card->interface.features |= ISDN_FEATURE_P_EURO;
  598. printk(KERN_INFO
  599. "icn: (%s) Euro-Protocol loaded and running\n", CID);
  600. }
  601. p = strstr(card->imsg, "BRV") + 3;
  602. while (*p) {
  603. if (*p >= '0' && *p <= '9')
  604. *q++ = *p;
  605. p++;
  606. }
  607. *q = '\0';
  608. strcat(vstr, "000");
  609. vstr[3] = '\0';
  610. card->fw_rev = (int) simple_strtoul(vstr, NULL, 10);
  611. continue;
  612. }
  613. }
  614. } else {
  615. card->imsg[card->iptr] = c;
  616. if (card->iptr < 59)
  617. card->iptr++;
  618. }
  619. }
  620. writeb((readb(&msg_o) + avail) & 0xff, &msg_o);
  621. icn_release_channel();
  622. }
  623. if (avail) {
  624. cmd.command = ISDN_STAT_STAVAIL;
  625. cmd.driver = card->myid;
  626. cmd.arg = avail;
  627. card->interface.statcallb(&cmd);
  628. }
  629. spin_lock_irqsave(&card->lock, flags);
  630. if (card->flags & (ICN_FLAGS_B1ACTIVE | ICN_FLAGS_B2ACTIVE))
  631. if (!(card->flags & ICN_FLAGS_RBTIMER)) {
  632. /* schedule b-channel polling */
  633. card->flags |= ICN_FLAGS_RBTIMER;
  634. del_timer(&card->rb_timer);
  635. card->rb_timer.function = icn_pollbchan;
  636. card->rb_timer.data = (unsigned long) card;
  637. card->rb_timer.expires = jiffies + ICN_TIMER_BCREAD;
  638. add_timer(&card->rb_timer);
  639. }
  640. /* schedule again */
  641. mod_timer(&card->st_timer, jiffies+ICN_TIMER_DCREAD);
  642. spin_unlock_irqrestore(&card->lock, flags);
  643. }
  644. /* Append a packet to the transmit buffer-queue.
  645. * Parameters:
  646. * channel = Number of B-channel
  647. * skb = pointer to sk_buff
  648. * card = pointer to card-struct
  649. * Return:
  650. * Number of bytes transferred, -E??? on error
  651. */
  652. static int
  653. icn_sendbuf(int channel, int ack, struct sk_buff *skb, icn_card * card)
  654. {
  655. int len = skb->len;
  656. unsigned long flags;
  657. struct sk_buff *nskb;
  658. if (len > 4000) {
  659. printk(KERN_WARNING
  660. "icn: Send packet too large\n");
  661. return -EINVAL;
  662. }
  663. if (len) {
  664. if (!(card->flags & (channel) ? ICN_FLAGS_B2ACTIVE : ICN_FLAGS_B1ACTIVE))
  665. return 0;
  666. if (card->sndcount[channel] > ICN_MAX_SQUEUE)
  667. return 0;
  668. #warning TODO test headroom or use skb->nb to flag ACK
  669. nskb = skb_clone(skb, GFP_ATOMIC);
  670. if (nskb) {
  671. /* Push ACK flag as one
  672. * byte in front of data.
  673. */
  674. *(skb_push(nskb, 1)) = ack?1:0;
  675. skb_queue_tail(&card->spqueue[channel], nskb);
  676. dev_kfree_skb(skb);
  677. } else
  678. len = 0;
  679. spin_lock_irqsave(&card->lock, flags);
  680. card->sndcount[channel] += len;
  681. spin_unlock_irqrestore(&card->lock, flags);
  682. }
  683. return len;
  684. }
  685. /*
  686. * Check card's status after starting the bootstrap loader.
  687. * On entry, the card's shared memory has already to be mapped.
  688. * Return:
  689. * 0 on success (Boot loader ready)
  690. * -EIO on failure (timeout)
  691. */
  692. static int
  693. icn_check_loader(int cardnumber)
  694. {
  695. int timer = 0;
  696. while (1) {
  697. #ifdef BOOT_DEBUG
  698. printk(KERN_DEBUG "Loader %d ?\n", cardnumber);
  699. #endif
  700. if (readb(&dev.shmem->data_control.scns) ||
  701. readb(&dev.shmem->data_control.scnr)) {
  702. if (timer++ > 5) {
  703. printk(KERN_WARNING
  704. "icn: Boot-Loader %d timed out.\n",
  705. cardnumber);
  706. icn_release_channel();
  707. return -EIO;
  708. }
  709. #ifdef BOOT_DEBUG
  710. printk(KERN_DEBUG "Loader %d TO?\n", cardnumber);
  711. #endif
  712. msleep_interruptible(ICN_BOOT_TIMEOUT1);
  713. } else {
  714. #ifdef BOOT_DEBUG
  715. printk(KERN_DEBUG "Loader %d OK\n", cardnumber);
  716. #endif
  717. icn_release_channel();
  718. return 0;
  719. }
  720. }
  721. }
  722. /* Load the boot-code into the interface-card's memory and start it.
  723. * Always called from user-process.
  724. *
  725. * Parameters:
  726. * buffer = pointer to packet
  727. * Return:
  728. * 0 if successfully loaded
  729. */
  730. #ifdef BOOT_DEBUG
  731. #define SLEEP(sec) { \
  732. int slsec = sec; \
  733. printk(KERN_DEBUG "SLEEP(%d)\n",slsec); \
  734. while (slsec) { \
  735. msleep_interruptible(1000); \
  736. slsec--; \
  737. } \
  738. }
  739. #else
  740. #define SLEEP(sec)
  741. #endif
  742. static int
  743. icn_loadboot(u_char __user * buffer, icn_card * card)
  744. {
  745. int ret;
  746. u_char *codebuf;
  747. unsigned long flags;
  748. #ifdef BOOT_DEBUG
  749. printk(KERN_DEBUG "icn_loadboot called, buffaddr=%08lx\n", (ulong) buffer);
  750. #endif
  751. if (!(codebuf = kmalloc(ICN_CODE_STAGE1, GFP_KERNEL))) {
  752. printk(KERN_WARNING "icn: Could not allocate code buffer\n");
  753. ret = -ENOMEM;
  754. goto out;
  755. }
  756. if (copy_from_user(codebuf, buffer, ICN_CODE_STAGE1)) {
  757. ret = -EFAULT;
  758. goto out_kfree;
  759. }
  760. if (!card->rvalid) {
  761. if (!request_region(card->port, ICN_PORTLEN, card->regname)) {
  762. printk(KERN_WARNING
  763. "icn: (%s) ports 0x%03x-0x%03x in use.\n",
  764. CID,
  765. card->port,
  766. card->port + ICN_PORTLEN);
  767. ret = -EBUSY;
  768. goto out_kfree;
  769. }
  770. card->rvalid = 1;
  771. if (card->doubleS0)
  772. card->other->rvalid = 1;
  773. }
  774. if (!dev.mvalid) {
  775. if (!request_mem_region(dev.memaddr, 0x4000, "icn-isdn (all cards)")) {
  776. printk(KERN_WARNING
  777. "icn: memory at 0x%08lx in use.\n", dev.memaddr);
  778. ret = -EBUSY;
  779. goto out_kfree;
  780. }
  781. dev.shmem = ioremap(dev.memaddr, 0x4000);
  782. dev.mvalid = 1;
  783. }
  784. OUTB_P(0, ICN_RUN); /* Reset Controller */
  785. OUTB_P(0, ICN_MAPRAM); /* Disable RAM */
  786. icn_shiftout(ICN_CFG, 0x0f, 3, 4); /* Windowsize= 16k */
  787. icn_shiftout(ICN_CFG, dev.memaddr, 23, 10); /* Set RAM-Addr. */
  788. #ifdef BOOT_DEBUG
  789. printk(KERN_DEBUG "shmem=%08lx\n", dev.memaddr);
  790. #endif
  791. SLEEP(1);
  792. #ifdef BOOT_DEBUG
  793. printk(KERN_DEBUG "Map Bank 0\n");
  794. #endif
  795. spin_lock_irqsave(&dev.devlock, flags);
  796. icn_map_channel(card, 0); /* Select Bank 0 */
  797. icn_lock_channel(card, 0); /* Lock Bank 0 */
  798. spin_unlock_irqrestore(&dev.devlock, flags);
  799. SLEEP(1);
  800. memcpy_toio(dev.shmem, codebuf, ICN_CODE_STAGE1); /* Copy code */
  801. #ifdef BOOT_DEBUG
  802. printk(KERN_DEBUG "Bootloader transferred\n");
  803. #endif
  804. if (card->doubleS0) {
  805. SLEEP(1);
  806. #ifdef BOOT_DEBUG
  807. printk(KERN_DEBUG "Map Bank 8\n");
  808. #endif
  809. spin_lock_irqsave(&dev.devlock, flags);
  810. __icn_release_channel();
  811. icn_map_channel(card, 2); /* Select Bank 8 */
  812. icn_lock_channel(card, 2); /* Lock Bank 8 */
  813. spin_unlock_irqrestore(&dev.devlock, flags);
  814. SLEEP(1);
  815. memcpy_toio(dev.shmem, codebuf, ICN_CODE_STAGE1); /* Copy code */
  816. #ifdef BOOT_DEBUG
  817. printk(KERN_DEBUG "Bootloader transferred\n");
  818. #endif
  819. }
  820. SLEEP(1);
  821. OUTB_P(0xff, ICN_RUN); /* Start Boot-Code */
  822. if ((ret = icn_check_loader(card->doubleS0 ? 2 : 1))) {
  823. goto out_kfree;
  824. }
  825. if (!card->doubleS0) {
  826. ret = 0;
  827. goto out_kfree;
  828. }
  829. /* reached only, if we have a Double-S0-Card */
  830. #ifdef BOOT_DEBUG
  831. printk(KERN_DEBUG "Map Bank 0\n");
  832. #endif
  833. spin_lock_irqsave(&dev.devlock, flags);
  834. icn_map_channel(card, 0); /* Select Bank 0 */
  835. icn_lock_channel(card, 0); /* Lock Bank 0 */
  836. spin_unlock_irqrestore(&dev.devlock, flags);
  837. SLEEP(1);
  838. ret = (icn_check_loader(1));
  839. out_kfree:
  840. kfree(codebuf);
  841. out:
  842. return ret;
  843. }
  844. static int
  845. icn_loadproto(u_char __user * buffer, icn_card * card)
  846. {
  847. register u_char __user *p = buffer;
  848. u_char codebuf[256];
  849. uint left = ICN_CODE_STAGE2;
  850. uint cnt;
  851. int timer;
  852. unsigned long flags;
  853. #ifdef BOOT_DEBUG
  854. printk(KERN_DEBUG "icn_loadproto called\n");
  855. #endif
  856. if (!access_ok(VERIFY_READ, buffer, ICN_CODE_STAGE2))
  857. return -EFAULT;
  858. timer = 0;
  859. spin_lock_irqsave(&dev.devlock, flags);
  860. if (card->secondhalf) {
  861. icn_map_channel(card, 2);
  862. icn_lock_channel(card, 2);
  863. } else {
  864. icn_map_channel(card, 0);
  865. icn_lock_channel(card, 0);
  866. }
  867. spin_unlock_irqrestore(&dev.devlock, flags);
  868. while (left) {
  869. if (sbfree) { /* If there is a free buffer... */
  870. cnt = left;
  871. if (cnt > 256)
  872. cnt = 256;
  873. if (copy_from_user(codebuf, p, cnt)) {
  874. icn_maprelease_channel(card, 0);
  875. return -EFAULT;
  876. }
  877. memcpy_toio(&sbuf_l, codebuf, cnt); /* copy data */
  878. sbnext; /* switch to next buffer */
  879. p += cnt;
  880. left -= cnt;
  881. timer = 0;
  882. } else {
  883. #ifdef BOOT_DEBUG
  884. printk(KERN_DEBUG "boot 2 !sbfree\n");
  885. #endif
  886. if (timer++ > 5) {
  887. icn_maprelease_channel(card, 0);
  888. return -EIO;
  889. }
  890. schedule_timeout_interruptible(10);
  891. }
  892. }
  893. writeb(0x20, &sbuf_n);
  894. timer = 0;
  895. while (1) {
  896. if (readb(&cmd_o) || readb(&cmd_i)) {
  897. #ifdef BOOT_DEBUG
  898. printk(KERN_DEBUG "Proto?\n");
  899. #endif
  900. if (timer++ > 5) {
  901. printk(KERN_WARNING
  902. "icn: (%s) Protocol timed out.\n",
  903. CID);
  904. #ifdef BOOT_DEBUG
  905. printk(KERN_DEBUG "Proto TO!\n");
  906. #endif
  907. icn_maprelease_channel(card, 0);
  908. return -EIO;
  909. }
  910. #ifdef BOOT_DEBUG
  911. printk(KERN_DEBUG "Proto TO?\n");
  912. #endif
  913. msleep_interruptible(ICN_BOOT_TIMEOUT1);
  914. } else {
  915. if ((card->secondhalf) || (!card->doubleS0)) {
  916. #ifdef BOOT_DEBUG
  917. printk(KERN_DEBUG "Proto loaded, install poll-timer %d\n",
  918. card->secondhalf);
  919. #endif
  920. spin_lock_irqsave(&card->lock, flags);
  921. init_timer(&card->st_timer);
  922. card->st_timer.expires = jiffies + ICN_TIMER_DCREAD;
  923. card->st_timer.function = icn_polldchan;
  924. card->st_timer.data = (unsigned long) card;
  925. add_timer(&card->st_timer);
  926. card->flags |= ICN_FLAGS_RUNNING;
  927. if (card->doubleS0) {
  928. init_timer(&card->other->st_timer);
  929. card->other->st_timer.expires = jiffies + ICN_TIMER_DCREAD;
  930. card->other->st_timer.function = icn_polldchan;
  931. card->other->st_timer.data = (unsigned long) card->other;
  932. add_timer(&card->other->st_timer);
  933. card->other->flags |= ICN_FLAGS_RUNNING;
  934. }
  935. spin_unlock_irqrestore(&card->lock, flags);
  936. }
  937. icn_maprelease_channel(card, 0);
  938. return 0;
  939. }
  940. }
  941. }
  942. /* Read the Status-replies from the Interface */
  943. static int
  944. icn_readstatus(u_char __user *buf, int len, icn_card * card)
  945. {
  946. int count;
  947. u_char __user *p;
  948. for (p = buf, count = 0; count < len; p++, count++) {
  949. if (card->msg_buf_read == card->msg_buf_write)
  950. return count;
  951. if (put_user(*card->msg_buf_read++, p))
  952. return -EFAULT;
  953. if (card->msg_buf_read > card->msg_buf_end)
  954. card->msg_buf_read = card->msg_buf;
  955. }
  956. return count;
  957. }
  958. /* Put command-strings into the command-queue of the Interface */
  959. static int
  960. icn_writecmd(const u_char * buf, int len, int user, icn_card * card)
  961. {
  962. int mch = card->secondhalf ? 2 : 0;
  963. int pp;
  964. int i;
  965. int count;
  966. int xcount;
  967. int ocount;
  968. int loop;
  969. unsigned long flags;
  970. int lastmap_channel;
  971. struct icn_card *lastmap_card;
  972. u_char *p;
  973. isdn_ctrl cmd;
  974. u_char msg[0x100];
  975. ocount = 1;
  976. xcount = loop = 0;
  977. while (len) {
  978. count = cmd_free;
  979. if (count > len)
  980. count = len;
  981. if (user) {
  982. if (copy_from_user(msg, buf, count))
  983. return -EFAULT;
  984. } else
  985. memcpy(msg, buf, count);
  986. spin_lock_irqsave(&dev.devlock, flags);
  987. lastmap_card = dev.mcard;
  988. lastmap_channel = dev.channel;
  989. icn_map_channel(card, mch);
  990. icn_putmsg(card, '>');
  991. for (p = msg, pp = readb(&cmd_i), i = count; i > 0; i--, p++, pp
  992. ++) {
  993. writeb((*p == '\n') ? 0xff : *p,
  994. &dev.shmem->comm_buffers.pcio_buf[pp & 0xff]);
  995. len--;
  996. xcount++;
  997. icn_putmsg(card, *p);
  998. if ((*p == '\n') && (i > 1)) {
  999. icn_putmsg(card, '>');
  1000. ocount++;
  1001. }
  1002. ocount++;
  1003. }
  1004. writeb((readb(&cmd_i) + count) & 0xff, &cmd_i);
  1005. if (lastmap_card)
  1006. icn_map_channel(lastmap_card, lastmap_channel);
  1007. spin_unlock_irqrestore(&dev.devlock, flags);
  1008. if (len) {
  1009. mdelay(1);
  1010. if (loop++ > 20)
  1011. break;
  1012. } else
  1013. break;
  1014. }
  1015. if (len && (!user))
  1016. printk(KERN_WARNING "icn: writemsg incomplete!\n");
  1017. cmd.command = ISDN_STAT_STAVAIL;
  1018. cmd.driver = card->myid;
  1019. cmd.arg = ocount;
  1020. card->interface.statcallb(&cmd);
  1021. return xcount;
  1022. }
  1023. /*
  1024. * Delete card's pending timers, send STOP to linklevel
  1025. */
  1026. static void
  1027. icn_stopcard(icn_card * card)
  1028. {
  1029. unsigned long flags;
  1030. isdn_ctrl cmd;
  1031. spin_lock_irqsave(&card->lock, flags);
  1032. if (card->flags & ICN_FLAGS_RUNNING) {
  1033. card->flags &= ~ICN_FLAGS_RUNNING;
  1034. del_timer(&card->st_timer);
  1035. del_timer(&card->rb_timer);
  1036. spin_unlock_irqrestore(&card->lock, flags);
  1037. cmd.command = ISDN_STAT_STOP;
  1038. cmd.driver = card->myid;
  1039. card->interface.statcallb(&cmd);
  1040. if (card->doubleS0)
  1041. icn_stopcard(card->other);
  1042. } else
  1043. spin_unlock_irqrestore(&card->lock, flags);
  1044. }
  1045. static void
  1046. icn_stopallcards(void)
  1047. {
  1048. icn_card *p = cards;
  1049. while (p) {
  1050. icn_stopcard(p);
  1051. p = p->next;
  1052. }
  1053. }
  1054. /*
  1055. * Unmap all cards, because some of them may be mapped accidetly during
  1056. * autoprobing of some network drivers (SMC-driver?)
  1057. */
  1058. static void
  1059. icn_disable_cards(void)
  1060. {
  1061. icn_card *card = cards;
  1062. while (card) {
  1063. if (!request_region(card->port, ICN_PORTLEN, "icn-isdn")) {
  1064. printk(KERN_WARNING
  1065. "icn: (%s) ports 0x%03x-0x%03x in use.\n",
  1066. CID,
  1067. card->port,
  1068. card->port + ICN_PORTLEN);
  1069. } else {
  1070. OUTB_P(0, ICN_RUN); /* Reset Controller */
  1071. OUTB_P(0, ICN_MAPRAM); /* Disable RAM */
  1072. release_region(card->port, ICN_PORTLEN);
  1073. }
  1074. card = card->next;
  1075. }
  1076. }
  1077. static int
  1078. icn_command(isdn_ctrl * c, icn_card * card)
  1079. {
  1080. ulong a;
  1081. ulong flags;
  1082. int i;
  1083. char cbuf[60];
  1084. isdn_ctrl cmd;
  1085. icn_cdef cdef;
  1086. char __user *arg;
  1087. switch (c->command) {
  1088. case ISDN_CMD_IOCTL:
  1089. memcpy(&a, c->parm.num, sizeof(ulong));
  1090. arg = (char __user *)a;
  1091. switch (c->arg) {
  1092. case ICN_IOCTL_SETMMIO:
  1093. if (dev.memaddr != (a & 0x0ffc000)) {
  1094. if (!request_mem_region(a & 0x0ffc000, 0x4000, "icn-isdn (all cards)")) {
  1095. printk(KERN_WARNING
  1096. "icn: memory at 0x%08lx in use.\n",
  1097. a & 0x0ffc000);
  1098. return -EINVAL;
  1099. }
  1100. release_mem_region(a & 0x0ffc000, 0x4000);
  1101. icn_stopallcards();
  1102. spin_lock_irqsave(&card->lock, flags);
  1103. if (dev.mvalid) {
  1104. iounmap(dev.shmem);
  1105. release_mem_region(dev.memaddr, 0x4000);
  1106. }
  1107. dev.mvalid = 0;
  1108. dev.memaddr = a & 0x0ffc000;
  1109. spin_unlock_irqrestore(&card->lock, flags);
  1110. printk(KERN_INFO
  1111. "icn: (%s) mmio set to 0x%08lx\n",
  1112. CID,
  1113. dev.memaddr);
  1114. }
  1115. break;
  1116. case ICN_IOCTL_GETMMIO:
  1117. return (long) dev.memaddr;
  1118. case ICN_IOCTL_SETPORT:
  1119. if (a == 0x300 || a == 0x310 || a == 0x320 || a == 0x330
  1120. || a == 0x340 || a == 0x350 || a == 0x360 ||
  1121. a == 0x308 || a == 0x318 || a == 0x328 || a == 0x338
  1122. || a == 0x348 || a == 0x358 || a == 0x368) {
  1123. if (card->port != (unsigned short) a) {
  1124. if (!request_region((unsigned short) a, ICN_PORTLEN, "icn-isdn")) {
  1125. printk(KERN_WARNING
  1126. "icn: (%s) ports 0x%03x-0x%03x in use.\n",
  1127. CID, (int) a, (int) a + ICN_PORTLEN);
  1128. return -EINVAL;
  1129. }
  1130. release_region((unsigned short) a, ICN_PORTLEN);
  1131. icn_stopcard(card);
  1132. spin_lock_irqsave(&card->lock, flags);
  1133. if (card->rvalid)
  1134. release_region(card->port, ICN_PORTLEN);
  1135. card->port = (unsigned short) a;
  1136. card->rvalid = 0;
  1137. if (card->doubleS0) {
  1138. card->other->port = (unsigned short) a;
  1139. card->other->rvalid = 0;
  1140. }
  1141. spin_unlock_irqrestore(&card->lock, flags);
  1142. printk(KERN_INFO
  1143. "icn: (%s) port set to 0x%03x\n",
  1144. CID, card->port);
  1145. }
  1146. } else
  1147. return -EINVAL;
  1148. break;
  1149. case ICN_IOCTL_GETPORT:
  1150. return (int) card->port;
  1151. case ICN_IOCTL_GETDOUBLE:
  1152. return (int) card->doubleS0;
  1153. case ICN_IOCTL_DEBUGVAR:
  1154. if (copy_to_user(arg,
  1155. &card,
  1156. sizeof(ulong)))
  1157. return -EFAULT;
  1158. a += sizeof(ulong);
  1159. {
  1160. ulong l = (ulong) & dev;
  1161. if (copy_to_user(arg,
  1162. &l,
  1163. sizeof(ulong)))
  1164. return -EFAULT;
  1165. }
  1166. return 0;
  1167. case ICN_IOCTL_LOADBOOT:
  1168. if (dev.firstload) {
  1169. icn_disable_cards();
  1170. dev.firstload = 0;
  1171. }
  1172. icn_stopcard(card);
  1173. return (icn_loadboot(arg, card));
  1174. case ICN_IOCTL_LOADPROTO:
  1175. icn_stopcard(card);
  1176. if ((i = (icn_loadproto(arg, card))))
  1177. return i;
  1178. if (card->doubleS0)
  1179. i = icn_loadproto(arg + ICN_CODE_STAGE2, card->other);
  1180. return i;
  1181. break;
  1182. case ICN_IOCTL_ADDCARD:
  1183. if (!dev.firstload)
  1184. return -EBUSY;
  1185. if (copy_from_user(&cdef,
  1186. arg,
  1187. sizeof(cdef)))
  1188. return -EFAULT;
  1189. return (icn_addcard(cdef.port, cdef.id1, cdef.id2));
  1190. break;
  1191. case ICN_IOCTL_LEASEDCFG:
  1192. if (a) {
  1193. if (!card->leased) {
  1194. card->leased = 1;
  1195. while (card->ptype == ISDN_PTYPE_UNKNOWN) {
  1196. msleep_interruptible(ICN_BOOT_TIMEOUT1);
  1197. }
  1198. msleep_interruptible(ICN_BOOT_TIMEOUT1);
  1199. sprintf(cbuf, "00;FV2ON\n01;EAZ%c\n02;EAZ%c\n",
  1200. (a & 1)?'1':'C', (a & 2)?'2':'C');
  1201. i = icn_writecmd(cbuf, strlen(cbuf), 0, card);
  1202. printk(KERN_INFO
  1203. "icn: (%s) Leased-line mode enabled\n",
  1204. CID);
  1205. cmd.command = ISDN_STAT_RUN;
  1206. cmd.driver = card->myid;
  1207. cmd.arg = 0;
  1208. card->interface.statcallb(&cmd);
  1209. }
  1210. } else {
  1211. if (card->leased) {
  1212. card->leased = 0;
  1213. sprintf(cbuf, "00;FV2OFF\n");
  1214. i = icn_writecmd(cbuf, strlen(cbuf), 0, card);
  1215. printk(KERN_INFO
  1216. "icn: (%s) Leased-line mode disabled\n",
  1217. CID);
  1218. cmd.command = ISDN_STAT_RUN;
  1219. cmd.driver = card->myid;
  1220. cmd.arg = 0;
  1221. card->interface.statcallb(&cmd);
  1222. }
  1223. }
  1224. return 0;
  1225. default:
  1226. return -EINVAL;
  1227. }
  1228. break;
  1229. case ISDN_CMD_DIAL:
  1230. if (!(card->flags & ICN_FLAGS_RUNNING))
  1231. return -ENODEV;
  1232. if (card->leased)
  1233. break;
  1234. if ((c->arg & 255) < ICN_BCH) {
  1235. char *p;
  1236. char dial[50];
  1237. char dcode[4];
  1238. a = c->arg;
  1239. p = c->parm.setup.phone;
  1240. if (*p == 's' || *p == 'S') {
  1241. /* Dial for SPV */
  1242. p++;
  1243. strcpy(dcode, "SCA");
  1244. } else
  1245. /* Normal Dial */
  1246. strcpy(dcode, "CAL");
  1247. strcpy(dial, p);
  1248. sprintf(cbuf, "%02d;D%s_R%s,%02d,%02d,%s\n", (int) (a + 1),
  1249. dcode, dial, c->parm.setup.si1,
  1250. c->parm.setup.si2, c->parm.setup.eazmsn);
  1251. i = icn_writecmd(cbuf, strlen(cbuf), 0, card);
  1252. }
  1253. break;
  1254. case ISDN_CMD_ACCEPTD:
  1255. if (!(card->flags & ICN_FLAGS_RUNNING))
  1256. return -ENODEV;
  1257. if (c->arg < ICN_BCH) {
  1258. a = c->arg + 1;
  1259. if (card->fw_rev >= 300) {
  1260. switch (card->l2_proto[a - 1]) {
  1261. case ISDN_PROTO_L2_X75I:
  1262. sprintf(cbuf, "%02d;BX75\n", (int) a);
  1263. break;
  1264. case ISDN_PROTO_L2_HDLC:
  1265. sprintf(cbuf, "%02d;BTRA\n", (int) a);
  1266. break;
  1267. }
  1268. i = icn_writecmd(cbuf, strlen(cbuf), 0, card);
  1269. }
  1270. sprintf(cbuf, "%02d;DCON_R\n", (int) a);
  1271. i = icn_writecmd(cbuf, strlen(cbuf), 0, card);
  1272. }
  1273. break;
  1274. case ISDN_CMD_ACCEPTB:
  1275. if (!(card->flags & ICN_FLAGS_RUNNING))
  1276. return -ENODEV;
  1277. if (c->arg < ICN_BCH) {
  1278. a = c->arg + 1;
  1279. if (card->fw_rev >= 300)
  1280. switch (card->l2_proto[a - 1]) {
  1281. case ISDN_PROTO_L2_X75I:
  1282. sprintf(cbuf, "%02d;BCON_R,BX75\n", (int) a);
  1283. break;
  1284. case ISDN_PROTO_L2_HDLC:
  1285. sprintf(cbuf, "%02d;BCON_R,BTRA\n", (int) a);
  1286. break;
  1287. } else
  1288. sprintf(cbuf, "%02d;BCON_R\n", (int) a);
  1289. i = icn_writecmd(cbuf, strlen(cbuf), 0, card);
  1290. }
  1291. break;
  1292. case ISDN_CMD_HANGUP:
  1293. if (!(card->flags & ICN_FLAGS_RUNNING))
  1294. return -ENODEV;
  1295. if (c->arg < ICN_BCH) {
  1296. a = c->arg + 1;
  1297. sprintf(cbuf, "%02d;BDIS_R\n%02d;DDIS_R\n", (int) a, (int) a);
  1298. i = icn_writecmd(cbuf, strlen(cbuf), 0, card);
  1299. }
  1300. break;
  1301. case ISDN_CMD_SETEAZ:
  1302. if (!(card->flags & ICN_FLAGS_RUNNING))
  1303. return -ENODEV;
  1304. if (card->leased)
  1305. break;
  1306. if (c->arg < ICN_BCH) {
  1307. a = c->arg + 1;
  1308. if (card->ptype == ISDN_PTYPE_EURO) {
  1309. sprintf(cbuf, "%02d;MS%s%s\n", (int) a,
  1310. c->parm.num[0] ? "N" : "ALL", c->parm.num);
  1311. } else
  1312. sprintf(cbuf, "%02d;EAZ%s\n", (int) a,
  1313. c->parm.num[0] ? (char *)(c->parm.num) : "0123456789");
  1314. i = icn_writecmd(cbuf, strlen(cbuf), 0, card);
  1315. }
  1316. break;
  1317. case ISDN_CMD_CLREAZ:
  1318. if (!(card->flags & ICN_FLAGS_RUNNING))
  1319. return -ENODEV;
  1320. if (card->leased)
  1321. break;
  1322. if (c->arg < ICN_BCH) {
  1323. a = c->arg + 1;
  1324. if (card->ptype == ISDN_PTYPE_EURO)
  1325. sprintf(cbuf, "%02d;MSNC\n", (int) a);
  1326. else
  1327. sprintf(cbuf, "%02d;EAZC\n", (int) a);
  1328. i = icn_writecmd(cbuf, strlen(cbuf), 0, card);
  1329. }
  1330. break;
  1331. case ISDN_CMD_SETL2:
  1332. if (!(card->flags & ICN_FLAGS_RUNNING))
  1333. return -ENODEV;
  1334. if ((c->arg & 255) < ICN_BCH) {
  1335. a = c->arg;
  1336. switch (a >> 8) {
  1337. case ISDN_PROTO_L2_X75I:
  1338. sprintf(cbuf, "%02d;BX75\n", (int) (a & 255) + 1);
  1339. break;
  1340. case ISDN_PROTO_L2_HDLC:
  1341. sprintf(cbuf, "%02d;BTRA\n", (int) (a & 255) + 1);
  1342. break;
  1343. default:
  1344. return -EINVAL;
  1345. }
  1346. i = icn_writecmd(cbuf, strlen(cbuf), 0, card);
  1347. card->l2_proto[a & 255] = (a >> 8);
  1348. }
  1349. break;
  1350. case ISDN_CMD_SETL3:
  1351. if (!(card->flags & ICN_FLAGS_RUNNING))
  1352. return -ENODEV;
  1353. return 0;
  1354. default:
  1355. return -EINVAL;
  1356. }
  1357. return 0;
  1358. }
  1359. /*
  1360. * Find card with given driverId
  1361. */
  1362. static inline icn_card *
  1363. icn_findcard(int driverid)
  1364. {
  1365. icn_card *p = cards;
  1366. while (p) {
  1367. if (p->myid == driverid)
  1368. return p;
  1369. p = p->next;
  1370. }
  1371. return (icn_card *) 0;
  1372. }
  1373. /*
  1374. * Wrapper functions for interface to linklevel
  1375. */
  1376. static int
  1377. if_command(isdn_ctrl * c)
  1378. {
  1379. icn_card *card = icn_findcard(c->driver);
  1380. if (card)
  1381. return (icn_command(c, card));
  1382. printk(KERN_ERR
  1383. "icn: if_command %d called with invalid driverId %d!\n",
  1384. c->command, c->driver);
  1385. return -ENODEV;
  1386. }
  1387. static int
  1388. if_writecmd(const u_char __user *buf, int len, int id, int channel)
  1389. {
  1390. icn_card *card = icn_findcard(id);
  1391. if (card) {
  1392. if (!(card->flags & ICN_FLAGS_RUNNING))
  1393. return -ENODEV;
  1394. return (icn_writecmd(buf, len, 1, card));
  1395. }
  1396. printk(KERN_ERR
  1397. "icn: if_writecmd called with invalid driverId!\n");
  1398. return -ENODEV;
  1399. }
  1400. static int
  1401. if_readstatus(u_char __user *buf, int len, int id, int channel)
  1402. {
  1403. icn_card *card = icn_findcard(id);
  1404. if (card) {
  1405. if (!(card->flags & ICN_FLAGS_RUNNING))
  1406. return -ENODEV;
  1407. return (icn_readstatus(buf, len, card));
  1408. }
  1409. printk(KERN_ERR
  1410. "icn: if_readstatus called with invalid driverId!\n");
  1411. return -ENODEV;
  1412. }
  1413. static int
  1414. if_sendbuf(int id, int channel, int ack, struct sk_buff *skb)
  1415. {
  1416. icn_card *card = icn_findcard(id);
  1417. if (card) {
  1418. if (!(card->flags & ICN_FLAGS_RUNNING))
  1419. return -ENODEV;
  1420. return (icn_sendbuf(channel, ack, skb, card));
  1421. }
  1422. printk(KERN_ERR
  1423. "icn: if_sendbuf called with invalid driverId!\n");
  1424. return -ENODEV;
  1425. }
  1426. /*
  1427. * Allocate a new card-struct, initialize it
  1428. * link it into cards-list and register it at linklevel.
  1429. */
  1430. static icn_card *
  1431. icn_initcard(int port, char *id)
  1432. {
  1433. icn_card *card;
  1434. int i;
  1435. if (!(card = kzalloc(sizeof(icn_card), GFP_KERNEL))) {
  1436. printk(KERN_WARNING
  1437. "icn: (%s) Could not allocate card-struct.\n", id);
  1438. return (icn_card *) 0;
  1439. }
  1440. spin_lock_init(&card->lock);
  1441. card->port = port;
  1442. card->interface.owner = THIS_MODULE;
  1443. card->interface.hl_hdrlen = 1;
  1444. card->interface.channels = ICN_BCH;
  1445. card->interface.maxbufsize = 4000;
  1446. card->interface.command = if_command;
  1447. card->interface.writebuf_skb = if_sendbuf;
  1448. card->interface.writecmd = if_writecmd;
  1449. card->interface.readstat = if_readstatus;
  1450. card->interface.features = ISDN_FEATURE_L2_X75I |
  1451. ISDN_FEATURE_L2_HDLC |
  1452. ISDN_FEATURE_L3_TRANS |
  1453. ISDN_FEATURE_P_UNKNOWN;
  1454. card->ptype = ISDN_PTYPE_UNKNOWN;
  1455. strlcpy(card->interface.id, id, sizeof(card->interface.id));
  1456. card->msg_buf_write = card->msg_buf;
  1457. card->msg_buf_read = card->msg_buf;
  1458. card->msg_buf_end = &card->msg_buf[sizeof(card->msg_buf) - 1];
  1459. for (i = 0; i < ICN_BCH; i++) {
  1460. card->l2_proto[i] = ISDN_PROTO_L2_X75I;
  1461. skb_queue_head_init(&card->spqueue[i]);
  1462. }
  1463. card->next = cards;
  1464. cards = card;
  1465. if (!register_isdn(&card->interface)) {
  1466. cards = cards->next;
  1467. printk(KERN_WARNING
  1468. "icn: Unable to register %s\n", id);
  1469. kfree(card);
  1470. return (icn_card *) 0;
  1471. }
  1472. card->myid = card->interface.channels;
  1473. sprintf(card->regname, "icn-isdn (%s)", card->interface.id);
  1474. return card;
  1475. }
  1476. static int
  1477. icn_addcard(int port, char *id1, char *id2)
  1478. {
  1479. icn_card *card;
  1480. icn_card *card2;
  1481. if (!(card = icn_initcard(port, id1))) {
  1482. return -EIO;
  1483. }
  1484. if (!strlen(id2)) {
  1485. printk(KERN_INFO
  1486. "icn: (%s) ICN-2B, port 0x%x added\n",
  1487. card->interface.id, port);
  1488. return 0;
  1489. }
  1490. if (!(card2 = icn_initcard(port, id2))) {
  1491. printk(KERN_INFO
  1492. "icn: (%s) half ICN-4B, port 0x%x added\n",
  1493. card2->interface.id, port);
  1494. return 0;
  1495. }
  1496. card->doubleS0 = 1;
  1497. card->secondhalf = 0;
  1498. card->other = card2;
  1499. card2->doubleS0 = 1;
  1500. card2->secondhalf = 1;
  1501. card2->other = card;
  1502. printk(KERN_INFO
  1503. "icn: (%s and %s) ICN-4B, port 0x%x added\n",
  1504. card->interface.id, card2->interface.id, port);
  1505. return 0;
  1506. }
  1507. #ifndef MODULE
  1508. static int __init
  1509. icn_setup(char *line)
  1510. {
  1511. char *p, *str;
  1512. int ints[3];
  1513. static char sid[20];
  1514. static char sid2[20];
  1515. str = get_options(line, 2, ints);
  1516. if (ints[0])
  1517. portbase = ints[1];
  1518. if (ints[0] > 1)
  1519. membase = (unsigned long)ints[2];
  1520. if (str && *str) {
  1521. strcpy(sid, str);
  1522. icn_id = sid;
  1523. if ((p = strchr(sid, ','))) {
  1524. *p++ = 0;
  1525. strcpy(sid2, p);
  1526. icn_id2 = sid2;
  1527. }
  1528. }
  1529. return(1);
  1530. }
  1531. __setup("icn=", icn_setup);
  1532. #endif /* MODULE */
  1533. static int __init icn_init(void)
  1534. {
  1535. char *p;
  1536. char rev[21];
  1537. memset(&dev, 0, sizeof(icn_dev));
  1538. dev.memaddr = (membase & 0x0ffc000);
  1539. dev.channel = -1;
  1540. dev.mcard = NULL;
  1541. dev.firstload = 1;
  1542. spin_lock_init(&dev.devlock);
  1543. if ((p = strchr(revision, ':'))) {
  1544. strncpy(rev, p + 1, 20);
  1545. rev[20] = '\0';
  1546. p = strchr(rev, '$');
  1547. if (p)
  1548. *p = 0;
  1549. } else
  1550. strcpy(rev, " ??? ");
  1551. printk(KERN_NOTICE "ICN-ISDN-driver Rev%smem=0x%08lx\n", rev,
  1552. dev.memaddr);
  1553. return (icn_addcard(portbase, icn_id, icn_id2));
  1554. }
  1555. static void __exit icn_exit(void)
  1556. {
  1557. isdn_ctrl cmd;
  1558. icn_card *card = cards;
  1559. icn_card *last, *tmpcard;
  1560. int i;
  1561. unsigned long flags;
  1562. icn_stopallcards();
  1563. while (card) {
  1564. cmd.command = ISDN_STAT_UNLOAD;
  1565. cmd.driver = card->myid;
  1566. card->interface.statcallb(&cmd);
  1567. spin_lock_irqsave(&card->lock, flags);
  1568. if (card->rvalid) {
  1569. OUTB_P(0, ICN_RUN); /* Reset Controller */
  1570. OUTB_P(0, ICN_MAPRAM); /* Disable RAM */
  1571. if (card->secondhalf || (!card->doubleS0)) {
  1572. release_region(card->port, ICN_PORTLEN);
  1573. card->rvalid = 0;
  1574. }
  1575. for (i = 0; i < ICN_BCH; i++)
  1576. icn_free_queue(card, i);
  1577. }
  1578. tmpcard = card->next;
  1579. spin_unlock_irqrestore(&card->lock, flags);
  1580. card = tmpcard;
  1581. }
  1582. card = cards;
  1583. cards = NULL;
  1584. while (card) {
  1585. last = card;
  1586. card = card->next;
  1587. kfree(last);
  1588. }
  1589. if (dev.mvalid) {
  1590. iounmap(dev.shmem);
  1591. release_mem_region(dev.memaddr, 0x4000);
  1592. }
  1593. printk(KERN_NOTICE "ICN-ISDN-driver unloaded\n");
  1594. }
  1595. module_init(icn_init);
  1596. module_exit(icn_exit);