| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* $Id: module.c,v 1.14.6.4 2001/09/23 22:24:32 kai Exp $ | 
 | 2 |  * | 
 | 3 |  * ISDN lowlevel-module for the IBM ISDN-S0 Active 2000. | 
 | 4 |  * | 
 | 5 |  * Author       Fritz Elfert | 
 | 6 |  * Copyright    by Fritz Elfert      <fritz@isdn4linux.de> | 
 | 7 |  *  | 
 | 8 |  * This software may be used and distributed according to the terms | 
 | 9 |  * of the GNU General Public License, incorporated herein by reference. | 
 | 10 |  * | 
 | 11 |  * Thanks to Friedemann Baitinger and IBM Germany | 
 | 12 |  * | 
 | 13 |  */ | 
 | 14 |  | 
 | 15 | #include "act2000.h" | 
 | 16 | #include "act2000_isa.h" | 
 | 17 | #include "capi.h" | 
 | 18 | #include <linux/module.h> | 
 | 19 | #include <linux/init.h> | 
 | 20 |  | 
 | 21 | static unsigned short act2000_isa_ports[] = | 
 | 22 | { | 
 | 23 |         0x0200, 0x0240, 0x0280, 0x02c0, 0x0300, 0x0340, 0x0380, | 
 | 24 |         0xcfe0, 0xcfa0, 0xcf60, 0xcf20, 0xcee0, 0xcea0, 0xce60, | 
 | 25 | }; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 |  | 
 | 27 | static act2000_card *cards = (act2000_card *) NULL; | 
 | 28 |  | 
 | 29 | /* Parameters to be set by insmod */ | 
 | 30 | static int   act_bus  =  0; | 
 | 31 | static int   act_port = -1;  /* -1 = Autoprobe  */ | 
 | 32 | static int   act_irq  = -1; | 
 | 33 | static char *act_id   = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"; | 
 | 34 |  | 
 | 35 | MODULE_DESCRIPTION(       "ISDN4Linux: Driver for IBM Active 2000 ISDN card"); | 
 | 36 | MODULE_AUTHOR(            "Fritz Elfert"); | 
 | 37 | MODULE_LICENSE(           "GPL"); | 
 | 38 | MODULE_PARM_DESC(act_bus, "BusType of first card, 1=ISA, 2=MCA, 3=PCMCIA, currently only ISA"); | 
 | 39 | MODULE_PARM_DESC(membase, "Base port address of first card"); | 
 | 40 | MODULE_PARM_DESC(act_irq, "IRQ of first card"); | 
 | 41 | MODULE_PARM_DESC(act_id,  "ID-String of first card"); | 
 | 42 | module_param(act_bus,  int, 0); | 
 | 43 | module_param(act_port, int, 0); | 
 | 44 | module_param(act_irq, int, 0); | 
 | 45 | module_param(act_id, charp, 0); | 
 | 46 |  | 
 | 47 | static int act2000_addcard(int, int, int, char *); | 
 | 48 |  | 
 | 49 | static act2000_chan * | 
 | 50 | find_channel(act2000_card *card, int channel) | 
 | 51 | { | 
 | 52 | 	if ((channel >= 0) && (channel < ACT2000_BCH)) | 
 | 53 |         	return &(card->bch[channel]); | 
 | 54 | 	printk(KERN_WARNING "act2000: Invalid channel %d\n", channel); | 
 | 55 | 	return NULL; | 
 | 56 | } | 
 | 57 |  | 
 | 58 | /* | 
 | 59 |  * Free MSN list | 
 | 60 |  */ | 
 | 61 | static void | 
 | 62 | act2000_clear_msn(act2000_card *card) | 
 | 63 | { | 
 | 64 | 	struct msn_entry *p = card->msn_list; | 
 | 65 | 	struct msn_entry *q; | 
 | 66 | 	unsigned long flags; | 
 | 67 |  | 
 | 68 | 	spin_lock_irqsave(&card->lock, flags); | 
 | 69 | 	card->msn_list = NULL; | 
 | 70 | 	spin_unlock_irqrestore(&card->lock, flags); | 
 | 71 | 	while (p) { | 
 | 72 | 		q  = p->next; | 
 | 73 | 		kfree(p); | 
 | 74 | 		p = q; | 
 | 75 | 	} | 
 | 76 | } | 
 | 77 |  | 
 | 78 | /* | 
 | 79 |  * Find an MSN entry in the list. | 
 | 80 |  * If ia5 != 0, return IA5-encoded EAZ, else | 
 | 81 |  * return a bitmask with corresponding bit set. | 
 | 82 |  */ | 
 | 83 | static __u16 | 
 | 84 | act2000_find_msn(act2000_card *card, char *msn, int ia5) | 
 | 85 | { | 
 | 86 |         struct msn_entry *p = card->msn_list; | 
 | 87 | 	__u8 eaz = '0'; | 
 | 88 |  | 
 | 89 | 	while (p) { | 
 | 90 | 		if (!strcmp(p->msn, msn)) { | 
 | 91 | 			eaz = p->eaz; | 
 | 92 | 			break; | 
 | 93 | 		} | 
 | 94 | 		p = p->next; | 
 | 95 | 	} | 
 | 96 | 	if (!ia5) | 
 | 97 | 		return (1 << (eaz - '0')); | 
 | 98 | 	else | 
 | 99 | 		return eaz; | 
 | 100 | } | 
 | 101 |  | 
 | 102 | /* | 
 | 103 |  * Find an EAZ entry in the list. | 
 | 104 |  * return a string with corresponding msn. | 
 | 105 |  */ | 
 | 106 | char * | 
 | 107 | act2000_find_eaz(act2000_card *card, char eaz) | 
 | 108 | { | 
 | 109 |         struct msn_entry *p = card->msn_list; | 
 | 110 |  | 
 | 111 | 	while (p) { | 
 | 112 | 		if (p->eaz == eaz) | 
 | 113 | 			return(p->msn); | 
 | 114 | 		p = p->next; | 
 | 115 | 	} | 
 | 116 | 	return("\0"); | 
 | 117 | } | 
 | 118 |  | 
 | 119 | /* | 
 | 120 |  * Add or delete an MSN to the MSN list | 
 | 121 |  * | 
 | 122 |  * First character of msneaz is EAZ, rest is MSN. | 
 | 123 |  * If length of eazmsn is 1, delete that entry. | 
 | 124 |  */ | 
 | 125 | static int | 
 | 126 | act2000_set_msn(act2000_card *card, char *eazmsn) | 
 | 127 | { | 
 | 128 |         struct msn_entry *p = card->msn_list; | 
 | 129 |         struct msn_entry *q = NULL; | 
 | 130 | 	unsigned long flags; | 
 | 131 | 	int i; | 
 | 132 | 	 | 
 | 133 | 	if (!strlen(eazmsn)) | 
 | 134 | 		return 0; | 
 | 135 | 	if (strlen(eazmsn) > 16) | 
 | 136 | 		return -EINVAL; | 
 | 137 | 	for (i = 0; i < strlen(eazmsn); i++) | 
 | 138 | 		if (!isdigit(eazmsn[i])) | 
 | 139 | 			return -EINVAL; | 
 | 140 |         if (strlen(eazmsn) == 1) { | 
 | 141 | 		/* Delete a single MSN */ | 
 | 142 | 		while (p) { | 
 | 143 | 			if (p->eaz == eazmsn[0]) { | 
 | 144 | 				spin_lock_irqsave(&card->lock, flags); | 
 | 145 | 				if (q) | 
 | 146 | 					q->next = p->next; | 
 | 147 | 				else | 
 | 148 | 					card->msn_list = p->next; | 
 | 149 | 				spin_unlock_irqrestore(&card->lock, flags); | 
 | 150 | 				kfree(p); | 
 | 151 | 				printk(KERN_DEBUG | 
 | 152 | 				       "Mapping for EAZ %c deleted\n", | 
 | 153 | 				       eazmsn[0]); | 
 | 154 | 				return 0; | 
 | 155 | 			} | 
 | 156 | 			q = p; | 
 | 157 | 			p = p->next; | 
 | 158 | 		} | 
 | 159 | 		return 0; | 
 | 160 |         } | 
 | 161 | 	/* Add a single MSN */ | 
 | 162 | 	while (p) { | 
 | 163 | 		/* Found in list, replace MSN */ | 
 | 164 | 		if (p->eaz == eazmsn[0]) { | 
 | 165 | 			spin_lock_irqsave(&card->lock, flags); | 
 | 166 | 			strcpy(p->msn, &eazmsn[1]); | 
 | 167 | 			spin_unlock_irqrestore(&card->lock, flags); | 
 | 168 | 			printk(KERN_DEBUG | 
 | 169 | 			       "Mapping for EAZ %c changed to %s\n", | 
 | 170 | 			       eazmsn[0], | 
 | 171 | 			       &eazmsn[1]); | 
 | 172 | 			return 0; | 
 | 173 | 		} | 
 | 174 | 		p = p->next; | 
 | 175 | 	} | 
 | 176 | 	/* Not found in list, add new entry */ | 
 | 177 | 	p = kmalloc(sizeof(msn_entry), GFP_KERNEL); | 
 | 178 | 	if (!p) | 
 | 179 | 		return -ENOMEM; | 
 | 180 | 	p->eaz = eazmsn[0]; | 
 | 181 | 	strcpy(p->msn, &eazmsn[1]); | 
 | 182 | 	p->next = card->msn_list; | 
 | 183 | 	spin_lock_irqsave(&card->lock, flags); | 
 | 184 | 	card->msn_list = p; | 
 | 185 | 	spin_unlock_irqrestore(&card->lock, flags); | 
 | 186 | 	printk(KERN_DEBUG | 
 | 187 | 	       "Mapping %c -> %s added\n", | 
 | 188 | 	       eazmsn[0], | 
 | 189 | 	       &eazmsn[1]); | 
 | 190 | 	return 0; | 
 | 191 | } | 
 | 192 |  | 
 | 193 | static void | 
| David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 194 | act2000_transmit(struct work_struct *work) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | { | 
| David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 196 | 	struct act2000_card *card = | 
 | 197 | 		container_of(work, struct act2000_card, snd_tq); | 
 | 198 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | 	switch (card->bus) { | 
 | 200 | 		case ACT2000_BUS_ISA: | 
 | 201 | 			act2000_isa_send(card); | 
 | 202 | 			break; | 
 | 203 | 		case ACT2000_BUS_PCMCIA: | 
 | 204 | 		case ACT2000_BUS_MCA: | 
 | 205 | 		default: | 
 | 206 | 			printk(KERN_WARNING | 
 | 207 | 			       "act2000_transmit: Illegal bustype %d\n", card->bus); | 
 | 208 | 	} | 
 | 209 | } | 
 | 210 |  | 
 | 211 | static void | 
| David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 212 | act2000_receive(struct work_struct *work) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | { | 
| David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 214 | 	struct act2000_card *card = | 
 | 215 | 		container_of(work, struct act2000_card, poll_tq); | 
 | 216 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | 	switch (card->bus) { | 
 | 218 | 		case ACT2000_BUS_ISA: | 
 | 219 | 			act2000_isa_receive(card); | 
 | 220 | 			break; | 
 | 221 | 		case ACT2000_BUS_PCMCIA: | 
 | 222 | 		case ACT2000_BUS_MCA: | 
 | 223 | 		default: | 
 | 224 | 			printk(KERN_WARNING | 
 | 225 | 			       "act2000_receive: Illegal bustype %d\n", card->bus); | 
 | 226 | 	} | 
 | 227 | } | 
 | 228 |  | 
 | 229 | static void | 
 | 230 | act2000_poll(unsigned long data) | 
 | 231 | { | 
 | 232 | 	act2000_card * card = (act2000_card *)data; | 
 | 233 | 	unsigned long flags; | 
 | 234 |  | 
| David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 235 | 	act2000_receive(&card->poll_tq); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | 	spin_lock_irqsave(&card->lock, flags); | 
 | 237 | 	mod_timer(&card->ptimer, jiffies+3); | 
 | 238 | 	spin_unlock_irqrestore(&card->lock, flags); | 
 | 239 | } | 
 | 240 |  | 
 | 241 | static int | 
 | 242 | act2000_command(act2000_card * card, isdn_ctrl * c) | 
 | 243 | { | 
 | 244 |         ulong a; | 
 | 245 |         act2000_chan *chan; | 
 | 246 | 	act2000_cdef cdef; | 
 | 247 | 	isdn_ctrl cmd; | 
 | 248 | 	char tmp[17]; | 
 | 249 | 	int ret; | 
 | 250 | 	unsigned long flags; | 
 | 251 | 	void __user *arg; | 
 | 252 |   | 
 | 253 |         switch (c->command) { | 
 | 254 | 		case ISDN_CMD_IOCTL: | 
 | 255 | 			memcpy(&a, c->parm.num, sizeof(ulong)); | 
 | 256 | 			arg = (void __user *)a; | 
 | 257 | 			switch (c->arg) { | 
 | 258 | 				case ACT2000_IOCTL_LOADBOOT: | 
 | 259 | 					switch (card->bus) { | 
 | 260 | 						case ACT2000_BUS_ISA: | 
 | 261 | 							ret = act2000_isa_download(card, | 
 | 262 | 									   arg); | 
 | 263 | 							if (!ret) { | 
 | 264 | 								card->flags |= ACT2000_FLAGS_LOADED; | 
 | 265 | 								if (!(card->flags & ACT2000_FLAGS_IVALID)) { | 
 | 266 | 									card->ptimer.expires = jiffies + 3; | 
 | 267 | 									card->ptimer.function = act2000_poll; | 
 | 268 | 									card->ptimer.data = (unsigned long)card; | 
 | 269 | 									add_timer(&card->ptimer); | 
 | 270 | 								} | 
 | 271 | 								actcapi_manufacturer_req_errh(card); | 
 | 272 | 							} | 
 | 273 | 							break; | 
 | 274 | 						default: | 
 | 275 | 							printk(KERN_WARNING | 
 | 276 | 							       "act2000: Illegal BUS type %d\n", | 
 | 277 | 							       card->bus); | 
 | 278 | 							ret = -EIO; | 
 | 279 | 					} | 
 | 280 | 					return ret; | 
 | 281 | 				case ACT2000_IOCTL_SETPROTO: | 
 | 282 | 					card->ptype = a?ISDN_PTYPE_EURO:ISDN_PTYPE_1TR6; | 
 | 283 | 					if (!(card->flags & ACT2000_FLAGS_RUNNING)) | 
 | 284 | 						return 0; | 
 | 285 | 					actcapi_manufacturer_req_net(card); | 
 | 286 | 					return 0; | 
 | 287 | 				case ACT2000_IOCTL_SETMSN: | 
 | 288 | 					if (copy_from_user(tmp, arg, | 
 | 289 | 							   sizeof(tmp))) | 
 | 290 | 						return -EFAULT; | 
 | 291 | 					if ((ret = act2000_set_msn(card, tmp))) | 
 | 292 | 						return ret; | 
 | 293 | 					if (card->flags & ACT2000_FLAGS_RUNNING) | 
 | 294 | 						return(actcapi_manufacturer_req_msn(card)); | 
 | 295 | 					return 0; | 
 | 296 | 				case ACT2000_IOCTL_ADDCARD: | 
 | 297 | 					if (copy_from_user(&cdef, arg, | 
 | 298 | 							   sizeof(cdef))) | 
 | 299 | 						return -EFAULT; | 
 | 300 | 					if (act2000_addcard(cdef.bus, cdef.port, cdef.irq, cdef.id)) | 
 | 301 | 						return -EIO; | 
 | 302 | 					return 0; | 
 | 303 | 				case ACT2000_IOCTL_TEST: | 
 | 304 | 					if (!(card->flags & ACT2000_FLAGS_RUNNING)) | 
 | 305 | 						return -ENODEV; | 
 | 306 | 					return 0; | 
 | 307 | 				default: | 
 | 308 | 					return -EINVAL; | 
 | 309 | 			} | 
 | 310 | 			break; | 
 | 311 | 		case ISDN_CMD_DIAL: | 
| Alexey Dobriyan | eaa0ff1 | 2008-02-06 01:36:06 -0800 | [diff] [blame] | 312 | 			if (!(card->flags & ACT2000_FLAGS_RUNNING)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | 				return -ENODEV; | 
 | 314 | 			if (!(chan = find_channel(card, c->arg & 0x0f))) | 
 | 315 | 				break; | 
 | 316 | 			spin_lock_irqsave(&card->lock, flags); | 
 | 317 | 			if (chan->fsm_state != ACT2000_STATE_NULL) { | 
 | 318 | 				spin_unlock_irqrestore(&card->lock, flags); | 
 | 319 | 				printk(KERN_WARNING "Dial on channel with state %d\n", | 
 | 320 | 					chan->fsm_state); | 
 | 321 | 				return -EBUSY; | 
 | 322 | 			} | 
 | 323 | 			if (card->ptype == ISDN_PTYPE_EURO) | 
 | 324 | 				tmp[0] = act2000_find_msn(card, c->parm.setup.eazmsn, 1); | 
 | 325 | 			else | 
 | 326 | 				tmp[0] = c->parm.setup.eazmsn[0]; | 
 | 327 | 			chan->fsm_state = ACT2000_STATE_OCALL; | 
 | 328 | 			chan->callref = 0xffff; | 
 | 329 | 			spin_unlock_irqrestore(&card->lock, flags); | 
 | 330 | 			ret = actcapi_connect_req(card, chan, c->parm.setup.phone, | 
 | 331 | 						  tmp[0], c->parm.setup.si1, | 
 | 332 | 						  c->parm.setup.si2); | 
 | 333 | 			if (ret) { | 
 | 334 | 				cmd.driver = card->myid; | 
 | 335 | 				cmd.command = ISDN_STAT_DHUP; | 
 | 336 | 				cmd.arg &= 0x0f; | 
 | 337 | 				card->interface.statcallb(&cmd); | 
 | 338 | 			} | 
 | 339 | 			return ret; | 
 | 340 | 		case ISDN_CMD_ACCEPTD: | 
| Alexey Dobriyan | eaa0ff1 | 2008-02-06 01:36:06 -0800 | [diff] [blame] | 341 | 			if (!(card->flags & ACT2000_FLAGS_RUNNING)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | 				return -ENODEV; | 
 | 343 | 			if (!(chan = find_channel(card, c->arg & 0x0f))) | 
 | 344 | 				break; | 
 | 345 | 			if (chan->fsm_state == ACT2000_STATE_ICALL) | 
 | 346 | 				actcapi_select_b2_protocol_req(card, chan); | 
 | 347 | 			return 0; | 
 | 348 | 		case ISDN_CMD_ACCEPTB: | 
| Alexey Dobriyan | eaa0ff1 | 2008-02-06 01:36:06 -0800 | [diff] [blame] | 349 | 			if (!(card->flags & ACT2000_FLAGS_RUNNING)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | 				return -ENODEV; | 
 | 351 | 			return 0; | 
 | 352 | 		case ISDN_CMD_HANGUP: | 
| Alexey Dobriyan | eaa0ff1 | 2008-02-06 01:36:06 -0800 | [diff] [blame] | 353 | 			if (!(card->flags & ACT2000_FLAGS_RUNNING)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | 				return -ENODEV; | 
 | 355 | 			if (!(chan = find_channel(card, c->arg & 0x0f))) | 
 | 356 | 				break; | 
 | 357 | 			switch (chan->fsm_state) { | 
 | 358 | 				case ACT2000_STATE_ICALL: | 
 | 359 | 				case ACT2000_STATE_BSETUP: | 
 | 360 | 					actcapi_connect_resp(card, chan, 0x15); | 
 | 361 | 					break; | 
 | 362 | 				case ACT2000_STATE_ACTIVE: | 
 | 363 | 					actcapi_disconnect_b3_req(card, chan); | 
 | 364 | 					break; | 
 | 365 | 			} | 
 | 366 | 			return 0; | 
 | 367 | 		case ISDN_CMD_SETEAZ: | 
| Alexey Dobriyan | eaa0ff1 | 2008-02-06 01:36:06 -0800 | [diff] [blame] | 368 | 			if (!(card->flags & ACT2000_FLAGS_RUNNING)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | 				return -ENODEV; | 
 | 370 | 			if (!(chan = find_channel(card, c->arg & 0x0f))) | 
 | 371 | 				break; | 
 | 372 | 			if (strlen(c->parm.num)) { | 
 | 373 | 				if (card->ptype == ISDN_PTYPE_EURO) { | 
 | 374 | 					chan->eazmask = act2000_find_msn(card, c->parm.num, 0); | 
 | 375 | 				} | 
 | 376 | 				if (card->ptype == ISDN_PTYPE_1TR6) { | 
 | 377 | 					int i; | 
 | 378 | 					chan->eazmask = 0; | 
 | 379 | 					for (i = 0; i < strlen(c->parm.num); i++) | 
 | 380 | 						if (isdigit(c->parm.num[i])) | 
 | 381 | 							chan->eazmask |= (1 << (c->parm.num[i] - '0')); | 
 | 382 | 				} | 
 | 383 | 			} else | 
 | 384 | 				chan->eazmask = 0x3ff; | 
 | 385 | 			actcapi_listen_req(card); | 
 | 386 | 			return 0; | 
 | 387 | 		case ISDN_CMD_CLREAZ: | 
| Alexey Dobriyan | eaa0ff1 | 2008-02-06 01:36:06 -0800 | [diff] [blame] | 388 | 			if (!(card->flags & ACT2000_FLAGS_RUNNING)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | 				return -ENODEV; | 
 | 390 | 			if (!(chan = find_channel(card, c->arg & 0x0f))) | 
 | 391 | 				break; | 
 | 392 | 			chan->eazmask = 0; | 
 | 393 | 			actcapi_listen_req(card); | 
 | 394 | 			return 0; | 
 | 395 | 		case ISDN_CMD_SETL2: | 
| Alexey Dobriyan | eaa0ff1 | 2008-02-06 01:36:06 -0800 | [diff] [blame] | 396 | 			if (!(card->flags & ACT2000_FLAGS_RUNNING)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | 				return -ENODEV; | 
 | 398 | 			if (!(chan = find_channel(card, c->arg & 0x0f))) | 
 | 399 | 				break; | 
 | 400 | 			chan->l2prot = (c->arg >> 8); | 
 | 401 | 			return 0; | 
 | 402 | 		case ISDN_CMD_SETL3: | 
| Alexey Dobriyan | eaa0ff1 | 2008-02-06 01:36:06 -0800 | [diff] [blame] | 403 | 			if (!(card->flags & ACT2000_FLAGS_RUNNING)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | 				return -ENODEV; | 
 | 405 | 			if ((c->arg >> 8) != ISDN_PROTO_L3_TRANS) { | 
 | 406 | 				printk(KERN_WARNING "L3 protocol unknown\n"); | 
 | 407 | 				return -1; | 
 | 408 | 			} | 
 | 409 | 			if (!(chan = find_channel(card, c->arg & 0x0f))) | 
 | 410 | 				break; | 
 | 411 | 			chan->l3prot = (c->arg >> 8); | 
 | 412 | 			return 0; | 
 | 413 |         } | 
 | 414 | 	 | 
 | 415 |         return -EINVAL; | 
 | 416 | } | 
 | 417 |  | 
 | 418 | static int | 
 | 419 | act2000_sendbuf(act2000_card *card, int channel, int ack, struct sk_buff *skb) | 
 | 420 | { | 
 | 421 |         struct sk_buff *xmit_skb; | 
 | 422 |         int len; | 
 | 423 |         act2000_chan *chan; | 
 | 424 | 	actcapi_msg *msg; | 
 | 425 |  | 
 | 426 |         if (!(chan = find_channel(card, channel))) | 
 | 427 | 		return -1; | 
 | 428 |         if (chan->fsm_state != ACT2000_STATE_ACTIVE) | 
 | 429 |                 return -1; | 
 | 430 |         len = skb->len; | 
 | 431 |         if ((chan->queued + len) >= ACT2000_MAX_QUEUED) | 
 | 432 |                 return 0; | 
 | 433 | 	if (!len) | 
 | 434 | 		return 0; | 
 | 435 | 	if (skb_headroom(skb) < 19) { | 
 | 436 | 		printk(KERN_WARNING "act2000_sendbuf: Headroom only %d\n", | 
 | 437 | 		       skb_headroom(skb)); | 
 | 438 | 		xmit_skb = alloc_skb(len + 19, GFP_ATOMIC); | 
 | 439 | 		if (!xmit_skb) { | 
 | 440 | 			printk(KERN_WARNING "act2000_sendbuf: Out of memory\n"); | 
 | 441 | 			return 0; | 
 | 442 | 		} | 
 | 443 | 		skb_reserve(xmit_skb, 19); | 
| Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 444 | 		skb_copy_from_linear_data(skb, skb_put(xmit_skb, len), len); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | 	} else { | 
 | 446 | 		xmit_skb = skb_clone(skb, GFP_ATOMIC); | 
 | 447 | 		if (!xmit_skb) { | 
 | 448 | 			printk(KERN_WARNING "act2000_sendbuf: Out of memory\n"); | 
 | 449 | 			return 0; | 
 | 450 | 		} | 
 | 451 | 	} | 
 | 452 | 	dev_kfree_skb(skb); | 
 | 453 | 	msg = (actcapi_msg *)skb_push(xmit_skb, 19); | 
 | 454 | 	msg->hdr.len = 19 + len; | 
 | 455 | 	msg->hdr.applicationID = 1; | 
 | 456 | 	msg->hdr.cmd.cmd = 0x86; | 
 | 457 | 	msg->hdr.cmd.subcmd = 0x00; | 
 | 458 | 	msg->hdr.msgnum = actcapi_nextsmsg(card); | 
 | 459 | 	msg->msg.data_b3_req.datalen = len; | 
 | 460 | 	msg->msg.data_b3_req.blocknr = (msg->hdr.msgnum & 0xff); | 
 | 461 | 	msg->msg.data_b3_req.fakencci = MAKE_NCCI(chan->plci, 0, chan->ncci); | 
 | 462 | 	msg->msg.data_b3_req.flags = ack; /* Will be set to 0 on actual sending */ | 
 | 463 | 	actcapi_debug_msg(xmit_skb, 1); | 
 | 464 |         chan->queued += len; | 
 | 465 | 	skb_queue_tail(&card->sndq, xmit_skb); | 
 | 466 | 	act2000_schedule_tx(card); | 
 | 467 |         return len; | 
 | 468 | } | 
 | 469 |  | 
 | 470 |  | 
 | 471 | /* Read the Status-replies from the Interface */ | 
 | 472 | static int | 
 | 473 | act2000_readstatus(u_char __user * buf, int len, act2000_card * card) | 
 | 474 | { | 
 | 475 |         int count; | 
 | 476 |         u_char __user *p; | 
 | 477 |  | 
 | 478 |         for (p = buf, count = 0; count < len; p++, count++) { | 
 | 479 |                 if (card->status_buf_read == card->status_buf_write) | 
 | 480 |                         return count; | 
 | 481 | 		put_user(*card->status_buf_read++, p); | 
 | 482 |                 if (card->status_buf_read > card->status_buf_end) | 
 | 483 |                         card->status_buf_read = card->status_buf; | 
 | 484 |         } | 
 | 485 |         return count; | 
 | 486 | } | 
 | 487 |  | 
 | 488 | /* | 
 | 489 |  * Find card with given driverId | 
 | 490 |  */ | 
 | 491 | static inline act2000_card * | 
 | 492 | act2000_findcard(int driverid) | 
 | 493 | { | 
 | 494 |         act2000_card *p = cards; | 
 | 495 |  | 
 | 496 |         while (p) { | 
 | 497 |                 if (p->myid == driverid) | 
 | 498 |                         return p; | 
 | 499 |                 p = p->next; | 
 | 500 |         } | 
 | 501 |         return (act2000_card *) 0; | 
 | 502 | } | 
 | 503 |  | 
 | 504 | /* | 
 | 505 |  * Wrapper functions for interface to linklevel | 
 | 506 |  */ | 
 | 507 | static int | 
 | 508 | if_command(isdn_ctrl * c) | 
 | 509 | { | 
 | 510 |         act2000_card *card = act2000_findcard(c->driver); | 
 | 511 |  | 
 | 512 |         if (card) | 
 | 513 |                 return (act2000_command(card, c)); | 
 | 514 |         printk(KERN_ERR | 
 | 515 |              "act2000: if_command %d called with invalid driverId %d!\n", | 
 | 516 |                c->command, c->driver); | 
 | 517 |         return -ENODEV; | 
 | 518 | } | 
 | 519 |  | 
 | 520 | static int | 
 | 521 | if_writecmd(const u_char __user *buf, int len, int id, int channel) | 
 | 522 | { | 
 | 523 |         act2000_card *card = act2000_findcard(id); | 
 | 524 |  | 
 | 525 |         if (card) { | 
| Alexey Dobriyan | eaa0ff1 | 2008-02-06 01:36:06 -0800 | [diff] [blame] | 526 |                 if (!(card->flags & ACT2000_FLAGS_RUNNING)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 |                         return -ENODEV; | 
 | 528 |                 return (len); | 
 | 529 |         } | 
 | 530 |         printk(KERN_ERR | 
 | 531 |                "act2000: if_writecmd called with invalid driverId!\n"); | 
 | 532 |         return -ENODEV; | 
 | 533 | } | 
 | 534 |  | 
 | 535 | static int | 
 | 536 | if_readstatus(u_char __user * buf, int len, int id, int channel) | 
 | 537 | { | 
 | 538 |         act2000_card *card = act2000_findcard(id); | 
 | 539 | 	 | 
 | 540 |         if (card) { | 
| Alexey Dobriyan | eaa0ff1 | 2008-02-06 01:36:06 -0800 | [diff] [blame] | 541 |                 if (!(card->flags & ACT2000_FLAGS_RUNNING)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 |                         return -ENODEV; | 
 | 543 |                 return (act2000_readstatus(buf, len, card)); | 
 | 544 |         } | 
 | 545 |         printk(KERN_ERR | 
 | 546 |                "act2000: if_readstatus called with invalid driverId!\n"); | 
 | 547 |         return -ENODEV; | 
 | 548 | } | 
 | 549 |  | 
 | 550 | static int | 
 | 551 | if_sendbuf(int id, int channel, int ack, struct sk_buff *skb) | 
 | 552 | { | 
 | 553 |         act2000_card *card = act2000_findcard(id); | 
 | 554 | 	 | 
 | 555 |         if (card) { | 
| Alexey Dobriyan | eaa0ff1 | 2008-02-06 01:36:06 -0800 | [diff] [blame] | 556 |                 if (!(card->flags & ACT2000_FLAGS_RUNNING)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 |                         return -ENODEV; | 
 | 558 | 		return (act2000_sendbuf(card, channel, ack, skb)); | 
 | 559 |         } | 
 | 560 |         printk(KERN_ERR | 
 | 561 |                "act2000: if_sendbuf called with invalid driverId!\n"); | 
 | 562 |         return -ENODEV; | 
 | 563 | } | 
 | 564 |  | 
 | 565 |  | 
 | 566 | /* | 
 | 567 |  * Allocate a new card-struct, initialize it | 
 | 568 |  * link it into cards-list. | 
 | 569 |  */ | 
 | 570 | static void | 
 | 571 | act2000_alloccard(int bus, int port, int irq, char *id) | 
 | 572 | { | 
 | 573 | 	int i; | 
 | 574 |         act2000_card *card; | 
| Burman Yan | 41f9693 | 2006-12-08 02:39:35 -0800 | [diff] [blame] | 575 |         if (!(card = kzalloc(sizeof(act2000_card), GFP_KERNEL))) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 |                 printk(KERN_WARNING | 
 | 577 | 		       "act2000: (%s) Could not allocate card-struct.\n", id); | 
 | 578 |                 return; | 
 | 579 |         } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 |         spin_lock_init(&card->lock); | 
 | 581 |         spin_lock_init(&card->mnlock); | 
 | 582 | 	skb_queue_head_init(&card->sndq); | 
 | 583 | 	skb_queue_head_init(&card->rcvq); | 
 | 584 | 	skb_queue_head_init(&card->ackq); | 
| David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 585 | 	INIT_WORK(&card->snd_tq, act2000_transmit); | 
 | 586 | 	INIT_WORK(&card->rcv_tq, actcapi_dispatch); | 
 | 587 | 	INIT_WORK(&card->poll_tq, act2000_receive); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | 	init_timer(&card->ptimer); | 
 | 589 | 	card->interface.owner = THIS_MODULE; | 
 | 590 |         card->interface.channels = ACT2000_BCH; | 
 | 591 |         card->interface.maxbufsize = 4000; | 
 | 592 |         card->interface.command = if_command; | 
 | 593 |         card->interface.writebuf_skb = if_sendbuf; | 
 | 594 |         card->interface.writecmd = if_writecmd; | 
 | 595 |         card->interface.readstat = if_readstatus; | 
 | 596 |         card->interface.features = | 
 | 597 | 		ISDN_FEATURE_L2_X75I | | 
 | 598 | 		ISDN_FEATURE_L2_HDLC | | 
 | 599 | 		ISDN_FEATURE_L3_TRANS | | 
 | 600 | 		ISDN_FEATURE_P_UNKNOWN; | 
 | 601 |         card->interface.hl_hdrlen = 20; | 
 | 602 |         card->ptype = ISDN_PTYPE_EURO; | 
 | 603 |         strlcpy(card->interface.id, id, sizeof(card->interface.id)); | 
 | 604 |         for (i=0; i<ACT2000_BCH; i++) { | 
 | 605 |                 card->bch[i].plci = 0x8000; | 
 | 606 |                 card->bch[i].ncci = 0x8000; | 
 | 607 |                 card->bch[i].l2prot = ISDN_PROTO_L2_X75I; | 
 | 608 |                 card->bch[i].l3prot = ISDN_PROTO_L3_TRANS; | 
 | 609 |         } | 
 | 610 |         card->myid = -1; | 
 | 611 |         card->bus = bus; | 
 | 612 |         card->port = port; | 
 | 613 |         card->irq = irq; | 
 | 614 |         card->next = cards; | 
 | 615 |         cards = card; | 
 | 616 | } | 
 | 617 |  | 
 | 618 | /* | 
 | 619 |  * register card at linklevel | 
 | 620 |  */ | 
 | 621 | static int | 
 | 622 | act2000_registercard(act2000_card * card) | 
 | 623 | { | 
 | 624 |         switch (card->bus) { | 
 | 625 | 		case ACT2000_BUS_ISA: | 
 | 626 | 			break; | 
 | 627 | 		case ACT2000_BUS_MCA: | 
 | 628 | 		case ACT2000_BUS_PCMCIA: | 
 | 629 | 		default: | 
 | 630 | 			printk(KERN_WARNING | 
 | 631 | 			       "act2000: Illegal BUS type %d\n", | 
 | 632 | 			       card->bus); | 
 | 633 | 			return -1; | 
 | 634 |         } | 
 | 635 |         if (!register_isdn(&card->interface)) { | 
 | 636 |                 printk(KERN_WARNING | 
 | 637 |                        "act2000: Unable to register %s\n", | 
 | 638 |                        card->interface.id); | 
 | 639 |                 return -1; | 
 | 640 |         } | 
 | 641 |         card->myid = card->interface.channels; | 
 | 642 |         sprintf(card->regname, "act2000-isdn (%s)", card->interface.id); | 
 | 643 |         return 0; | 
 | 644 | } | 
 | 645 |  | 
 | 646 | static void | 
 | 647 | unregister_card(act2000_card * card) | 
 | 648 | { | 
 | 649 |         isdn_ctrl cmd; | 
 | 650 |  | 
 | 651 |         cmd.command = ISDN_STAT_UNLOAD; | 
 | 652 |         cmd.driver = card->myid; | 
 | 653 |         card->interface.statcallb(&cmd); | 
 | 654 |         switch (card->bus) { | 
 | 655 | 		case ACT2000_BUS_ISA: | 
 | 656 | 			act2000_isa_release(card); | 
 | 657 | 			break; | 
 | 658 | 		case ACT2000_BUS_MCA: | 
 | 659 | 		case ACT2000_BUS_PCMCIA: | 
 | 660 | 		default: | 
 | 661 | 			printk(KERN_WARNING | 
 | 662 | 			       "act2000: Invalid BUS type %d\n", | 
 | 663 | 			       card->bus); | 
 | 664 | 			break; | 
 | 665 |         } | 
 | 666 | } | 
 | 667 |  | 
 | 668 | static int | 
 | 669 | act2000_addcard(int bus, int port, int irq, char *id) | 
 | 670 | { | 
 | 671 | 	act2000_card *p; | 
 | 672 | 	act2000_card *q = NULL; | 
 | 673 | 	int initialized; | 
 | 674 | 	int added = 0; | 
 | 675 | 	int failed = 0; | 
 | 676 | 	int i; | 
 | 677 |  | 
 | 678 | 	if (!bus) | 
 | 679 | 		bus = ACT2000_BUS_ISA; | 
 | 680 | 	if (port != -1) { | 
 | 681 | 		/* Port defined, do fixed setup */ | 
 | 682 | 		act2000_alloccard(bus, port, irq, id); | 
 | 683 | 	} else { | 
 | 684 | 		/* No port defined, perform autoprobing. | 
 | 685 | 		 * This may result in more than one card detected. | 
 | 686 | 		 */ | 
 | 687 | 		switch (bus) { | 
| Karsten Keil | ba2d6cc | 2009-07-24 18:26:08 +0200 | [diff] [blame] | 688 | 		case ACT2000_BUS_ISA: | 
 | 689 | 			for (i = 0; i < ARRAY_SIZE(act2000_isa_ports); i++) | 
 | 690 | 				if (act2000_isa_detect(act2000_isa_ports[i])) { | 
 | 691 | 					printk(KERN_INFO "act2000: Detected " | 
 | 692 | 						"ISA card at port 0x%x\n", | 
 | 693 | 						act2000_isa_ports[i]); | 
 | 694 | 					act2000_alloccard(bus, | 
 | 695 | 						act2000_isa_ports[i], irq, id); | 
 | 696 | 				} | 
 | 697 | 			break; | 
 | 698 | 		case ACT2000_BUS_MCA: | 
 | 699 | 		case ACT2000_BUS_PCMCIA: | 
 | 700 | 		default: | 
 | 701 | 			printk(KERN_WARNING | 
 | 702 | 				"act2000: addcard: Invalid BUS type %d\n", bus); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | 		} | 
 | 704 | 	} | 
 | 705 | 	if (!cards) | 
 | 706 | 		return 1; | 
 | 707 |         p = cards; | 
 | 708 |         while (p) { | 
 | 709 | 		initialized = 0; | 
 | 710 | 		if (!p->interface.statcallb) { | 
 | 711 | 			/* Not yet registered. | 
 | 712 | 			 * Try to register and activate it. | 
 | 713 | 			 */ | 
 | 714 | 			added++; | 
 | 715 | 			switch (p->bus) { | 
 | 716 | 				case ACT2000_BUS_ISA: | 
 | 717 | 					if (act2000_isa_detect(p->port)) { | 
 | 718 | 						if (act2000_registercard(p)) | 
 | 719 | 							break; | 
 | 720 | 						if (act2000_isa_config_port(p, p->port)) { | 
 | 721 | 							printk(KERN_WARNING | 
 | 722 | 							       "act2000: Could not request port 0x%04x\n", | 
 | 723 | 							       p->port); | 
 | 724 | 							unregister_card(p); | 
 | 725 | 							p->interface.statcallb = NULL; | 
 | 726 | 							break; | 
 | 727 | 						} | 
 | 728 | 						if (act2000_isa_config_irq(p, p->irq)) { | 
 | 729 | 							printk(KERN_INFO | 
 | 730 | 							       "act2000: No IRQ available, fallback to polling\n"); | 
 | 731 | 							/* Fall back to polled operation */ | 
 | 732 | 							p->irq = 0; | 
 | 733 | 						} | 
 | 734 | 						printk(KERN_INFO | 
 | 735 | 						       "act2000: ISA" | 
 | 736 | 						       "-type card at port " | 
 | 737 | 						       "0x%04x ", | 
 | 738 | 						       p->port); | 
 | 739 | 						if (p->irq) | 
 | 740 | 							printk("irq %d\n", p->irq); | 
 | 741 | 						else | 
 | 742 | 							printk("polled\n"); | 
 | 743 | 						initialized = 1; | 
 | 744 | 					} | 
 | 745 | 					break; | 
 | 746 | 				case ACT2000_BUS_MCA: | 
 | 747 | 				case ACT2000_BUS_PCMCIA: | 
 | 748 | 				default: | 
 | 749 | 					printk(KERN_WARNING | 
 | 750 | 					       "act2000: addcard: Invalid BUS type %d\n", | 
 | 751 | 					       p->bus); | 
 | 752 | 			} | 
 | 753 | 		} else | 
 | 754 | 			/* Card already initialized */ | 
 | 755 | 			initialized = 1; | 
 | 756 |                 if (initialized) { | 
 | 757 | 			/* Init OK, next card ... */ | 
 | 758 |                         q = p; | 
 | 759 |                         p = p->next; | 
 | 760 |                 } else { | 
 | 761 |                         /* Init failed, remove card from list, free memory */ | 
 | 762 |                         printk(KERN_WARNING | 
 | 763 |                                "act2000: Initialization of %s failed\n", | 
 | 764 |                                p->interface.id); | 
 | 765 |                         if (q) { | 
 | 766 |                                 q->next = p->next; | 
 | 767 |                                 kfree(p); | 
 | 768 |                                 p = q->next; | 
 | 769 |                         } else { | 
 | 770 |                                 cards = p->next; | 
 | 771 |                                 kfree(p); | 
 | 772 |                                 p = cards; | 
 | 773 |                         } | 
 | 774 | 			failed++; | 
 | 775 |                 } | 
 | 776 | 	} | 
 | 777 |         return (added - failed); | 
 | 778 | } | 
 | 779 |  | 
 | 780 | #define DRIVERNAME "IBM Active 2000 ISDN driver" | 
 | 781 |  | 
 | 782 | static int __init act2000_init(void) | 
 | 783 | { | 
 | 784 |         printk(KERN_INFO "%s\n", DRIVERNAME); | 
 | 785 |         if (!cards) | 
 | 786 | 		act2000_addcard(act_bus, act_port, act_irq, act_id); | 
 | 787 |         if (!cards) | 
 | 788 |                 printk(KERN_INFO "act2000: No cards defined yet\n"); | 
 | 789 |         return 0; | 
 | 790 | } | 
 | 791 |  | 
 | 792 | static void __exit act2000_exit(void) | 
 | 793 | { | 
 | 794 |         act2000_card *card = cards; | 
 | 795 |         act2000_card *last; | 
 | 796 |         while (card) { | 
 | 797 |                 unregister_card(card); | 
 | 798 | 		del_timer(&card->ptimer); | 
 | 799 |                 card = card->next; | 
 | 800 |         } | 
 | 801 |         card = cards; | 
 | 802 |         while (card) { | 
 | 803 |                 last = card; | 
 | 804 |                 card = card->next; | 
 | 805 | 		act2000_clear_msn(last); | 
 | 806 |                 kfree(last); | 
 | 807 |         } | 
 | 808 |         printk(KERN_INFO "%s unloaded\n", DRIVERNAME); | 
 | 809 | } | 
 | 810 |  | 
 | 811 | module_init(act2000_init); | 
 | 812 | module_exit(act2000_exit); |