i4l.c 15.3 KB
Newer Older
1 2 3
/*
 * Stuff used by all variants of the driver
 *
4 5 6
 * Copyright (c) 2001 by Stefan Eilers,
 *                       Hansjoerg Lipp <hjlipp@web.de>,
 *                       Tilman Schmidt <tilman@imap.cc>.
7 8 9 10 11 12 13 14 15 16 17
 *
 * =====================================================================
 *	This program is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU General Public License as
 *	published by the Free Software Foundation; either version 2 of
 *	the License, or (at your option) any later version.
 * =====================================================================
 */

#include "gigaset.h"

18
/* == Handling of I4L IO =====================================================*/
19 20 21 22 23 24 25 26 27

/* writebuf_from_LL
 * called by LL to transmit data on an open channel
 * inserts the buffer data into the send queue and starts the transmission
 * Note that this operation must not sleep!
 * When the buffer is processed completely, gigaset_skb_sent() should be called.
 * parameters:
 *	driverID	driver ID as assigned by LL
 *	channel		channel number
28 29
 *	ack		if != 0 LL wants to be notified on completion via
 *			statcallb(ISDN_STAT_BSENT)
30 31 32 33 34 35
 *	skb		skb containing data to send
 * return value:
 *	number of accepted bytes
 *	0 if temporarily unable to accept data (out of buffer space)
 *	<0 on error (eg. -EINVAL)
 */
36 37
static int writebuf_from_LL(int driverID, int channel, int ack,
			    struct sk_buff *skb)
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
{
	struct cardstate *cs;
	struct bc_state *bcs;
	unsigned len;
	unsigned skblen;

	if (!(cs = gigaset_get_cs_by_id(driverID))) {
		err("%s: invalid driver ID (%d)", __func__, driverID);
		return -ENODEV;
	}
	if (channel < 0 || channel >= cs->channels) {
		err("%s: invalid channel ID (%d)", __func__, channel);
		return -ENODEV;
	}
	bcs = &cs->bcs[channel];
	len = skb->len;

55 56 57 58
	gig_dbg(DEBUG_LLDATA,
		"Receiving data from LL (id: %d, ch: %d, ack: %d, sz: %d)",
		driverID, channel, ack, len);

59 60
	if (!len) {
		if (ack)
61
			notice("%s: not ACKing empty packet", __func__);
62 63 64
		return 0;
	}
	if (len > MAX_BUF_SIZE) {
65
		err("%s: packet too large (%d bytes)", __func__, len);
66 67 68 69 70 71
		return -EINVAL;
	}

	skblen = ack ? len : 0;
	skb->head[0] = skblen & 0xff;
	skb->head[1] = skblen >> 8;
72 73
	gig_dbg(DEBUG_MCMD, "skb: len=%u, skblen=%u: %02x %02x",
		len, skblen, (unsigned) skb->head[0], (unsigned) skb->head[1]);
74 75

	/* pass to device-specific module */
76
	return cs->ops->send_skb(bcs, skb);
77 78 79 80 81 82 83 84 85 86
}

void gigaset_skb_sent(struct bc_state *bcs, struct sk_buff *skb)
{
	unsigned len;
	isdn_ctrl response;

	++bcs->trans_up;

	if (skb->len)
87 88
		dev_warn(bcs->cs->dev, "%s: skb->len==%d\n",
			 __func__, skb->len);
89 90 91 92

	len = (unsigned char) skb->head[0] |
	      (unsigned) (unsigned char) skb->head[1] << 8;
	if (len) {
93 94
		gig_dbg(DEBUG_MCMD, "ACKing to LL (id: %d, ch: %d, sz: %u)",
			bcs->cs->myid, bcs->channel, len);
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117

		response.driver = bcs->cs->myid;
		response.command = ISDN_STAT_BSENT;
		response.arg = bcs->channel;
		response.parm.length = len;
		bcs->cs->iif.statcallb(&response);
	}
}
EXPORT_SYMBOL_GPL(gigaset_skb_sent);

/* This function will be called by LL to send commands
 * NOTE: LL ignores the returned value, for commands other than ISDN_CMD_IOCTL,
 * so don't put too much effort into it.
 */
static int command_from_LL(isdn_ctrl *cntrl)
{
	struct cardstate *cs = gigaset_get_cs_by_id(cntrl->driver);
	struct bc_state *bcs;
	int retval = 0;
	struct setup_parm *sp;

	gigaset_debugdrivers();

118
	if (!cs) {
119 120 121 122 123 124 125
		warn("LL tried to access unknown device with nr. %d",
		     cntrl->driver);
		return -ENODEV;
	}

	switch (cntrl->command) {
	case ISDN_CMD_IOCTL:
126 127
		gig_dbg(DEBUG_ANY, "ISDN_CMD_IOCTL (driver: %d, arg: %ld)",
			cntrl->driver, cntrl->arg);
128 129 130 131 132

		warn("ISDN_CMD_IOCTL is not supported.");
		return -EINVAL;

	case ISDN_CMD_DIAL:
133 134 135 136 137 138
		gig_dbg(DEBUG_ANY,
			"ISDN_CMD_DIAL (driver: %d, ch: %ld, "
			"phone: %s, ownmsn: %s, si1: %d, si2: %d)",
			cntrl->driver, cntrl->arg,
			cntrl->parm.setup.phone, cntrl->parm.setup.eazmsn,
			cntrl->parm.setup.si1, cntrl->parm.setup.si2);
139 140

		if (cntrl->arg >= cs->channels) {
141 142
			err("ISDN_CMD_DIAL: invalid channel (%d)",
			    (int) cntrl->arg);
143 144 145 146 147 148
			return -EINVAL;
		}

		bcs = cs->bcs + cntrl->arg;

		if (!gigaset_get_channel(bcs)) {
149
			err("ISDN_CMD_DIAL: channel not free");
150 151 152 153 154 155 156 157 158 159 160
			return -EBUSY;
		}

		sp = kmalloc(sizeof *sp, GFP_ATOMIC);
		if (!sp) {
			gigaset_free_channel(bcs);
			err("ISDN_CMD_DIAL: out of memory");
			return -ENOMEM;
		}
		*sp = cntrl->parm.setup;

T
Tilman Schmidt 已提交
161 162
		if (!gigaset_add_event(cs, &bcs->at_state, EV_DIAL, sp,
				       bcs->at_state.seq_index, NULL)) {
163 164 165 166 167 168
			//FIXME what should we do?
			kfree(sp);
			gigaset_free_channel(bcs);
			return -ENOMEM;
		}

169
		gig_dbg(DEBUG_CMD, "scheduling DIAL");
170 171 172
		gigaset_schedule_event(cs);
		break;
	case ISDN_CMD_ACCEPTD: //FIXME
173
		gig_dbg(DEBUG_ANY, "ISDN_CMD_ACCEPTD");
174 175

		if (cntrl->arg >= cs->channels) {
176 177
			err("ISDN_CMD_ACCEPTD: invalid channel (%d)",
			    (int) cntrl->arg);
178 179 180 181
			return -EINVAL;
		}

		if (!gigaset_add_event(cs, &cs->bcs[cntrl->arg].at_state,
182
				       EV_ACCEPT, NULL, 0, NULL)) {
183 184 185 186
			//FIXME what should we do?
			return -ENOMEM;
		}

187
		gig_dbg(DEBUG_CMD, "scheduling ACCEPT");
188 189 190 191
		gigaset_schedule_event(cs);

		break;
	case ISDN_CMD_ACCEPTB:
192
		gig_dbg(DEBUG_ANY, "ISDN_CMD_ACCEPTB");
193 194
		break;
	case ISDN_CMD_HANGUP:
195 196
		gig_dbg(DEBUG_ANY, "ISDN_CMD_HANGUP (ch: %d)",
			(int) cntrl->arg);
197 198 199 200 201 202 203 204

		if (cntrl->arg >= cs->channels) {
			err("ISDN_CMD_HANGUP: invalid channel (%u)",
			    (unsigned) cntrl->arg);
			return -EINVAL;
		}

		if (!gigaset_add_event(cs, &cs->bcs[cntrl->arg].at_state,
205
				       EV_HUP, NULL, 0, NULL)) {
206 207 208 209
			//FIXME what should we do?
			return -ENOMEM;
		}

210
		gig_dbg(DEBUG_CMD, "scheduling HUP");
211 212 213
		gigaset_schedule_event(cs);

		break;
214
	case ISDN_CMD_CLREAZ: /* Do not signal incoming signals */ //FIXME
215
		gig_dbg(DEBUG_ANY, "ISDN_CMD_CLREAZ");
216
		break;
217
	case ISDN_CMD_SETEAZ: /* Signal incoming calls for given MSN */ //FIXME
218 219 220
		gig_dbg(DEBUG_ANY,
			"ISDN_CMD_SETEAZ (id: %d, ch: %ld, number: %s)",
			cntrl->driver, cntrl->arg, cntrl->parm.num);
221
		break;
222
	case ISDN_CMD_SETL2: /* Set L2 to given protocol */
223 224
		gig_dbg(DEBUG_ANY, "ISDN_CMD_SETL2 (ch: %ld, proto: %lx)",
			cntrl->arg & 0xff, (cntrl->arg >> 8));
225 226

		if ((cntrl->arg & 0xff) >= cs->channels) {
227
			err("ISDN_CMD_SETL2: invalid channel (%u)",
228 229 230 231 232
			    (unsigned) cntrl->arg & 0xff);
			return -EINVAL;
		}

		if (!gigaset_add_event(cs, &cs->bcs[cntrl->arg & 0xff].at_state,
233 234
				       EV_PROTO_L2, NULL, cntrl->arg >> 8,
				       NULL)) {
235 236 237 238
			//FIXME what should we do?
			return -ENOMEM;
		}

239
		gig_dbg(DEBUG_CMD, "scheduling PROTO_L2");
240 241
		gigaset_schedule_event(cs);
		break;
242
	case ISDN_CMD_SETL3: /* Set L3 to given protocol */
243 244
		gig_dbg(DEBUG_ANY, "ISDN_CMD_SETL3 (ch: %ld, proto: %lx)",
			cntrl->arg & 0xff, (cntrl->arg >> 8));
245 246

		if ((cntrl->arg & 0xff) >= cs->channels) {
247
			err("ISDN_CMD_SETL3: invalid channel (%u)",
248 249 250 251 252
			    (unsigned) cntrl->arg & 0xff);
			return -EINVAL;
		}

		if (cntrl->arg >> 8 != ISDN_PROTO_L3_TRANS) {
253 254
			err("ISDN_CMD_SETL3: invalid protocol %lu",
			    cntrl->arg >> 8);
255 256 257 258 259
			return -EINVAL;
		}

		break;
	case ISDN_CMD_PROCEED:
260
		gig_dbg(DEBUG_ANY, "ISDN_CMD_PROCEED"); //FIXME
261 262
		break;
	case ISDN_CMD_ALERT:
263
		gig_dbg(DEBUG_ANY, "ISDN_CMD_ALERT"); //FIXME
264
		if (cntrl->arg >= cs->channels) {
265 266
			err("ISDN_CMD_ALERT: invalid channel (%d)",
			    (int) cntrl->arg);
267 268 269 270 271 272 273
			return -EINVAL;
		}
		//bcs = cs->bcs + cntrl->arg;
		//bcs->proto2 = -1;
		// FIXME
		break;
	case ISDN_CMD_REDIR:
274
		gig_dbg(DEBUG_ANY, "ISDN_CMD_REDIR"); //FIXME
275 276
		break;
	case ISDN_CMD_PROT_IO:
277
		gig_dbg(DEBUG_ANY, "ISDN_CMD_PROT_IO");
278 279
		break;
	case ISDN_CMD_FAXCMD:
280
		gig_dbg(DEBUG_ANY, "ISDN_CMD_FAXCMD");
281 282
		break;
	case ISDN_CMD_GETL2:
283
		gig_dbg(DEBUG_ANY, "ISDN_CMD_GETL2");
284 285
		break;
	case ISDN_CMD_GETL3:
286
		gig_dbg(DEBUG_ANY, "ISDN_CMD_GETL3");
287 288
		break;
	case ISDN_CMD_GETEAZ:
289
		gig_dbg(DEBUG_ANY, "ISDN_CMD_GETEAZ");
290 291
		break;
	case ISDN_CMD_SETSIL:
292
		gig_dbg(DEBUG_ANY, "ISDN_CMD_SETSIL");
293 294
		break;
	case ISDN_CMD_GETSIL:
295
		gig_dbg(DEBUG_ANY, "ISDN_CMD_GETSIL");
296 297
		break;
	default:
298
		err("unknown command %d from LL", cntrl->command);
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
		return -EINVAL;
	}

	return retval;
}

void gigaset_i4l_cmd(struct cardstate *cs, int cmd)
{
	isdn_ctrl command;

	command.driver = cs->myid;
	command.command = cmd;
	command.arg = 0;
	cs->iif.statcallb(&command);
}

void gigaset_i4l_channel_cmd(struct bc_state *bcs, int cmd)
{
	isdn_ctrl command;

	command.driver = bcs->cs->myid;
	command.command = cmd;
	command.arg = bcs->channel;
	bcs->cs->iif.statcallb(&command);
}

int gigaset_isdn_setup_dial(struct at_state_t *at_state, void *data)
{
	struct bc_state *bcs = at_state->bcs;
	unsigned proto;
	const char *bc;
	size_t length[AT_NUM];
	size_t l;
	int i;
	struct setup_parm *sp = data;

	switch (bcs->proto2) {
	case ISDN_PROTO_L2_HDLC:
		proto = 1; /* 0: Bitsynchron, 1: HDLC, 2: voice */
		break;
	case ISDN_PROTO_L2_TRANS:
		proto = 2; /* 0: Bitsynchron, 1: HDLC, 2: voice */
		break;
	default:
343 344
		dev_err(bcs->cs->dev, "%s: invalid L2 protocol: %u\n",
			__func__, bcs->proto2);
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
		return -EINVAL;
	}

	switch (sp->si1) {
	case 1:		/* audio */
		bc = "9090A3";	/* 3.1 kHz audio, A-law */
		break;
	case 7:		/* data */
	default:	/* hope the app knows what it is doing */
		bc = "8890";	/* unrestricted digital information */
	}
	//FIXME add missing si1 values from 1TR6, inspect si2, set HLC/LLC

	length[AT_DIAL ] = 1 + strlen(sp->phone) + 1 + 1;
	l = strlen(sp->eazmsn);
	length[AT_MSN  ] = l ? 6 + l + 1 + 1 : 0;
	length[AT_BC   ] = 5 + strlen(bc) + 1 + 1;
	length[AT_PROTO] = 6 + 1 + 1 + 1; /* proto: 1 character */
	length[AT_ISO  ] = 6 + 1 + 1 + 1; /* channel: 1 character */
	length[AT_TYPE ] = 6 + 1 + 1 + 1; /* call type: 1 character */
	length[AT_HLC  ] = 0;

	for (i = 0; i < AT_NUM; ++i) {
		kfree(bcs->commands[i]);
		bcs->commands[i] = NULL;
		if (length[i] &&
		    !(bcs->commands[i] = kmalloc(length[i], GFP_ATOMIC))) {
372
			dev_err(bcs->cs->dev, "out of memory\n");
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
			return -ENOMEM;
		}
	}

	/* type = 1: extern, 0: intern, 2: recall, 3: door, 4: centrex */
	if (sp->phone[0] == '*' && sp->phone[1] == '*') {
		/* internal call: translate ** prefix to CTP value */
		snprintf(bcs->commands[AT_DIAL], length[AT_DIAL],
			 "D%s\r", sp->phone+2);
		strncpy(bcs->commands[AT_TYPE], "^SCTP=0\r", length[AT_TYPE]);
	} else {
		snprintf(bcs->commands[AT_DIAL], length[AT_DIAL],
			 "D%s\r", sp->phone);
		strncpy(bcs->commands[AT_TYPE], "^SCTP=1\r", length[AT_TYPE]);
	}

	if (bcs->commands[AT_MSN])
390 391 392 393 394 395 396 397
		snprintf(bcs->commands[AT_MSN], length[AT_MSN],
			 "^SMSN=%s\r", sp->eazmsn);
	snprintf(bcs->commands[AT_BC   ], length[AT_BC   ],
		 "^SBC=%s\r", bc);
	snprintf(bcs->commands[AT_PROTO], length[AT_PROTO],
		 "^SBPR=%u\r", proto);
	snprintf(bcs->commands[AT_ISO  ], length[AT_ISO  ],
		 "^SISO=%u\r", (unsigned)bcs->channel + 1);
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416

	return 0;
}

int gigaset_isdn_setup_accept(struct at_state_t *at_state)
{
	unsigned proto;
	size_t length[AT_NUM];
	int i;
	struct bc_state *bcs = at_state->bcs;

	switch (bcs->proto2) {
	case ISDN_PROTO_L2_HDLC:
		proto = 1; /* 0: Bitsynchron, 1: HDLC, 2: voice */
		break;
	case ISDN_PROTO_L2_TRANS:
		proto = 2; /* 0: Bitsynchron, 1: HDLC, 2: voice */
		break;
	default:
417 418
		dev_err(at_state->cs->dev, "%s: invalid protocol: %u\n",
			__func__, bcs->proto2);
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
		return -EINVAL;
	}

	length[AT_DIAL ] = 0;
	length[AT_MSN  ] = 0;
	length[AT_BC   ] = 0;
	length[AT_PROTO] = 6 + 1 + 1 + 1; /* proto: 1 character */
	length[AT_ISO  ] = 6 + 1 + 1 + 1; /* channel: 1 character */
	length[AT_TYPE ] = 0;
	length[AT_HLC  ] = 0;

	for (i = 0; i < AT_NUM; ++i) {
		kfree(bcs->commands[i]);
		bcs->commands[i] = NULL;
		if (length[i] &&
		    !(bcs->commands[i] = kmalloc(length[i], GFP_ATOMIC))) {
435
			dev_err(at_state->cs->dev, "out of memory\n");
436 437 438 439
			return -ENOMEM;
		}
	}

440 441 442 443
	snprintf(bcs->commands[AT_PROTO], length[AT_PROTO],
		 "^SBPR=%u\r", proto);
	snprintf(bcs->commands[AT_ISO  ], length[AT_ISO  ],
		 "^SISO=%u\r", (unsigned) bcs->channel + 1);
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473

	return 0;
}

int gigaset_isdn_icall(struct at_state_t *at_state)
{
	struct cardstate *cs = at_state->cs;
	struct bc_state *bcs = at_state->bcs;
	isdn_ctrl response;
	int retval;

	/* fill ICALL structure */
	response.parm.setup.si1 = 0;	/* default: unknown */
	response.parm.setup.si2 = 0;
	response.parm.setup.screen = 0;	//FIXME how to set these?
	response.parm.setup.plan = 0;
	if (!at_state->str_var[STR_ZBC]) {
		/* no BC (internal call): assume speech, A-law */
		response.parm.setup.si1 = 1;
	} else if (!strcmp(at_state->str_var[STR_ZBC], "8890")) {
		/* unrestricted digital information */
		response.parm.setup.si1 = 7;
	} else if (!strcmp(at_state->str_var[STR_ZBC], "8090A3")) {
		/* speech, A-law */
		response.parm.setup.si1 = 1;
	} else if (!strcmp(at_state->str_var[STR_ZBC], "9090A3")) {
		/* 3,1 kHz audio, A-law */
		response.parm.setup.si1 = 1;
		response.parm.setup.si2 = 2;
	} else {
474
		dev_warn(cs->dev, "RING ignored - unsupported BC %s\n",
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
		     at_state->str_var[STR_ZBC]);
		return ICALL_IGNORE;
	}
	if (at_state->str_var[STR_NMBR]) {
		strncpy(response.parm.setup.phone, at_state->str_var[STR_NMBR],
			sizeof response.parm.setup.phone - 1);
		response.parm.setup.phone[sizeof response.parm.setup.phone - 1] = 0;
	} else
		response.parm.setup.phone[0] = 0;
	if (at_state->str_var[STR_ZCPN]) {
		strncpy(response.parm.setup.eazmsn, at_state->str_var[STR_ZCPN],
			sizeof response.parm.setup.eazmsn - 1);
		response.parm.setup.eazmsn[sizeof response.parm.setup.eazmsn - 1] = 0;
	} else
		response.parm.setup.eazmsn[0] = 0;

	if (!bcs) {
492
		dev_notice(cs->dev, "no channel for incoming call\n");
493 494 495
		response.command = ISDN_STAT_ICALLW;
		response.arg = 0; //FIXME
	} else {
496
		gig_dbg(DEBUG_CMD, "Sending ICALL");
497 498 499 500 501
		response.command = ISDN_STAT_ICALL;
		response.arg = bcs->channel; //FIXME
	}
	response.driver = cs->myid;
	retval = cs->iif.statcallb(&response);
502
	gig_dbg(DEBUG_CMD, "Response: %d", retval);
503 504 505 506 507 508 509 510 511
	switch (retval) {
	case 0:	/* no takers */
		return ICALL_IGNORE;
	case 1:	/* alerting */
		bcs->chstate |= CHS_NOTIFY_LL;
		return ICALL_ACCEPT;
	case 2:	/* reject */
		return ICALL_REJECT;
	case 3:	/* incomplete */
512 513
		dev_warn(cs->dev,
		       "LL requested unsupported feature: Incomplete Number\n");
514 515 516 517 518 519 520
		return ICALL_IGNORE;
	case 4:	/* proceeding */
		/* Gigaset will send ALERTING anyway.
		 * There doesn't seem to be a way to avoid this.
		 */
		return ICALL_ACCEPT;
	case 5:	/* deflect */
521 522
		dev_warn(cs->dev,
			 "LL requested unsupported feature: Call Deflection\n");
523 524
		return ICALL_IGNORE;
	default:
525
		dev_err(cs->dev, "LL error %d on ICALL\n", retval);
526 527 528 529 530 531 532 533 534
		return ICALL_IGNORE;
	}
}

/* Set Callback function pointer */
int gigaset_register_to_LL(struct cardstate *cs, const char *isdnid)
{
	isdn_if *iif = &cs->iif;

535
	gig_dbg(DEBUG_ANY, "Register driver capabilities to LL");
536 537 538 539 540 541 542 543

	//iif->id[sizeof(iif->id) - 1]=0;
	//strncpy(iif->id, isdnid, sizeof(iif->id) - 1);
	if (snprintf(iif->id, sizeof iif->id, "%s_%u", isdnid, cs->minor_index)
	    >= sizeof iif->id)
		return -ENOMEM; //FIXME EINVAL/...??

	iif->owner = THIS_MODULE;
544
	iif->channels = cs->channels;
545
	iif->maxbufsize = MAX_BUF_SIZE;
546
	iif->features = ISDN_FEATURE_L2_TRANS |
547 548 549 550 551 552
		ISDN_FEATURE_L2_HDLC |
#ifdef GIG_X75
		ISDN_FEATURE_L2_X75I |
#endif
		ISDN_FEATURE_L3_TRANS |
		ISDN_FEATURE_P_EURO;
553
	iif->hl_hdrlen = HW_HDR_LEN;		/* Area for storing ack */
554 555
	iif->command = command_from_LL;
	iif->writebuf_skb = writebuf_from_LL;
556 557 558 559
	iif->writecmd = NULL;			/* Don't support isdnctrl */
	iif->readstat = NULL;			/* Don't support isdnctrl */
	iif->rcvcallb_skb = NULL;		/* Will be set by LL */
	iif->statcallb = NULL;			/* Will be set by LL */
560 561 562 563

	if (!register_isdn(iif))
		return 0;

564
	cs->myid = iif->channels;		/* Set my device id */
565 566
	return 1;
}