bas-gigaset.c 61.4 KB
Newer Older
1 2 3 4 5
/*
 * USB driver for Gigaset 307x base via direct USB connection.
 *
 * Copyright (c) 2001 by Hansjoerg Lipp <hjlipp@web.de>,
 *                       Tilman Schmidt <tilman@imap.cc>,
6
 *                       Stefan Eilers.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 *
 * Based on usb-gigaset.c.
 *
 * =====================================================================
 *	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"

#include <linux/errno.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/timer.h>
#include <linux/usb.h>
#include <linux/module.h>
#include <linux/moduleparam.h>

/* Version Information */
29
#define DRIVER_AUTHOR "Tilman Schmidt <tilman@imap.cc>, Hansjoerg Lipp <hjlipp@web.de>, Stefan Eilers"
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
#define DRIVER_DESC "USB Driver for Gigaset 307x"


/* Module parameters */

static int startmode = SM_ISDN;
static int cidmode = 1;

module_param(startmode, int, S_IRUGO);
module_param(cidmode, int, S_IRUGO);
MODULE_PARM_DESC(startmode, "start in isdn4linux mode");
MODULE_PARM_DESC(cidmode, "Call-ID mode");

#define GIGASET_MINORS     1
#define GIGASET_MINOR      16
#define GIGASET_MODULENAME "bas_gigaset"
#define GIGASET_DEVFSNAME  "gig/bas/"
#define GIGASET_DEVNAME    "ttyGB"

#define IF_WRITEBUF 256 //FIXME

/* Values for the Gigaset 307x */
#define USB_GIGA_VENDOR_ID      0x0681
#define USB_GIGA_PRODUCT_ID     0x0001
#define USB_4175_PRODUCT_ID     0x0002
#define USB_SX303_PRODUCT_ID    0x0021
#define USB_SX353_PRODUCT_ID    0x0022

/* table of devices that work with this driver */
static struct usb_device_id gigaset_table [] = {
	{ USB_DEVICE(USB_GIGA_VENDOR_ID, USB_GIGA_PRODUCT_ID) },
	{ USB_DEVICE(USB_GIGA_VENDOR_ID, USB_4175_PRODUCT_ID) },
	{ USB_DEVICE(USB_GIGA_VENDOR_ID, USB_SX303_PRODUCT_ID) },
	{ USB_DEVICE(USB_GIGA_VENDOR_ID, USB_SX353_PRODUCT_ID) },
	{ } /* Terminating entry */
};

MODULE_DEVICE_TABLE(usb, gigaset_table);

/*======================= local function prototypes =============================*/

/* This function is called if a new device is connected to the USB port. It
 * checks whether this new device belongs to this driver.
 */
static int gigaset_probe(struct usb_interface *interface,
			 const struct usb_device_id *id);

/* Function will be called if the device is unplugged */
static void gigaset_disconnect(struct usb_interface *interface);


/*==============================================================================*/

struct bas_cardstate {
84 85
	struct usb_device	*udev;		/* USB device pointer */
	struct usb_interface	*interface;	/* interface for this device */
86 87
	unsigned char		minor;		/* starting minor number */

88
	struct urb		*urb_ctrl;	/* control pipe default URB */
89 90 91 92
	struct usb_ctrlrequest	dr_ctrl;
	struct timer_list	timer_ctrl;	/* control request timeout */

	struct timer_list	timer_atrdy;	/* AT command ready timeout */
93
	struct urb		*urb_cmd_out;	/* for sending AT commands */
94 95 96
	struct usb_ctrlrequest	dr_cmd_out;
	int			retry_cmd_out;

97
	struct urb		*urb_cmd_in;	/* for receiving AT replies */
98 99
	struct usb_ctrlrequest	dr_cmd_in;
	struct timer_list	timer_cmd_in;	/* receive request timeout */
100
	unsigned char		*rcvbuf;	/* AT reply receive buffer */
101

102
	struct urb		*urb_int_in;	/* URB for interrupt pipe */
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
	unsigned char		int_in_buf[3];

	spinlock_t		lock;		/* locks all following */
	atomic_t		basstate;	/* bitmap (BS_*) */
	int			pending;	/* uncompleted base request */
	int			rcvbuf_size;	/* size of AT receive buffer */
						/* 0: no receive in progress */
	int			retry_cmd_in;	/* receive req retry count */
};

/* status of direct USB connection to 307x base (bits in basstate) */
#define BS_ATOPEN	0x001
#define BS_B1OPEN	0x002
#define BS_B2OPEN	0x004
#define BS_ATREADY	0x008
#define BS_INIT		0x010
#define BS_ATTIMER	0x020


static struct gigaset_driver *driver = NULL;
static struct cardstate *cardstate = NULL;

/* usb specific object needed to register this driver with the usb subsystem */
static struct usb_driver gigaset_usb_driver = {
	.name =         GIGASET_MODULENAME,
	.probe =        gigaset_probe,
	.disconnect =   gigaset_disconnect,
	.id_table =     gigaset_table,
};

/* get message text for USB status code
 */
static char *get_usb_statmsg(int status)
{
	static char unkmsg[28];

	switch (status) {
	case 0:
		return "success";
	case -ENOENT:
		return "canceled";
	case -ECONNRESET:
		return "canceled (async)";
	case -EINPROGRESS:
		return "pending";
	case -EPROTO:
		return "bit stuffing or unknown USB error";
	case -EILSEQ:
		return "Illegal byte sequence (CRC mismatch)";
	case -EPIPE:
		return "babble detect or endpoint stalled";
	case -ENOSR:
		return "buffer error";
	case -ETIMEDOUT:
		return "timed out";
	case -ENODEV:
		return "device not present";
	case -EREMOTEIO:
		return "short packet detected";
	case -EXDEV:
		return "partial isochronous transfer";
	case -EINVAL:
		return "invalid argument";
	case -ENXIO:
		return "URB already queued";
	case -EAGAIN:
		return "isochronous start frame too early or too much scheduled";
	case -EFBIG:
		return "too many isochronous frames requested";
	case -EMSGSIZE:
		return "endpoint message size zero";
	case -ESHUTDOWN:
		return "endpoint shutdown";
	case -EBUSY:
		return "another request pending";
	default:
		snprintf(unkmsg, sizeof(unkmsg), "unknown error %d", status);
		return unkmsg;
	}
}

/* usb_pipetype_str
 * retrieve string representation of USB pipe type
 */
static inline char *usb_pipetype_str(int pipe)
{
	if (usb_pipeisoc(pipe))
		return "Isoc";
	if (usb_pipeint(pipe))
		return "Int";
	if (usb_pipecontrol(pipe))
		return "Ctrl";
	if (usb_pipebulk(pipe))
		return "Bulk";
	return "?";
}

/* dump_urb
 * write content of URB to syslog for debugging
 */
static inline void dump_urb(enum debuglevel level, const char *tag,
204
			    struct urb *urb)
205 206 207
{
#ifdef CONFIG_GIGASET_DEBUG
	int i;
208
	gig_dbg(level, "%s urb(0x%08lx)->{", tag, (unsigned long) urb);
209
	if (urb) {
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
		gig_dbg(level,
			"  dev=0x%08lx, pipe=%s:EP%d/DV%d:%s, "
			"status=%d, hcpriv=0x%08lx, transfer_flags=0x%x,",
			(unsigned long) urb->dev,
			usb_pipetype_str(urb->pipe),
			usb_pipeendpoint(urb->pipe), usb_pipedevice(urb->pipe),
			usb_pipein(urb->pipe) ? "in" : "out",
			urb->status, (unsigned long) urb->hcpriv,
			urb->transfer_flags);
		gig_dbg(level,
			"  transfer_buffer=0x%08lx[%d], actual_length=%d, "
			"bandwidth=%d, setup_packet=0x%08lx,",
			(unsigned long) urb->transfer_buffer,
			urb->transfer_buffer_length, urb->actual_length,
			urb->bandwidth, (unsigned long) urb->setup_packet);
		gig_dbg(level,
			"  start_frame=%d, number_of_packets=%d, interval=%d, "
			"error_count=%d,",
			urb->start_frame, urb->number_of_packets, urb->interval,
			urb->error_count);
		gig_dbg(level,
			"  context=0x%08lx, complete=0x%08lx, "
			"iso_frame_desc[]={",
			(unsigned long) urb->context,
			(unsigned long) urb->complete);
235
		for (i = 0; i < urb->number_of_packets; i++) {
236 237
			struct usb_iso_packet_descriptor *pifd
				= &urb->iso_frame_desc[i];
238 239 240 241 242
			gig_dbg(level,
				"    {offset=%u, length=%u, actual_length=%u, "
				"status=%u}",
				pifd->offset, pifd->length, pifd->actual_length,
				pifd->status);
243 244
		}
	}
245
	gig_dbg(level, "}}");
246 247 248 249 250
#endif
}

/* read/set modem control bits etc. (m10x only) */
static int gigaset_set_modem_ctrl(struct cardstate *cs, unsigned old_state,
251
				  unsigned new_state)
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
{
	return -EINVAL;
}

static int gigaset_baud_rate(struct cardstate *cs, unsigned cflag)
{
	return -EINVAL;
}

static int gigaset_set_line_ctrl(struct cardstate *cs, unsigned cflag)
{
	return -EINVAL;
}

/* error_hangup
 * hang up any existing connection because of an unrecoverable error
 * This function may be called from any context and takes care of scheduling
 * the necessary actions for execution outside of interrupt context.
 * argument:
 *	B channel control structure
 */
static inline void error_hangup(struct bc_state *bcs)
{
	struct cardstate *cs = bcs->cs;

277 278
	gig_dbg(DEBUG_ANY, "%s: scheduling HUP for channel %d",
		__func__, bcs->channel);
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297

	if (!gigaset_add_event(cs, &bcs->at_state, EV_HUP, NULL, 0, NULL)) {
		//FIXME what should we do?
		return;
	}

	gigaset_schedule_event(cs);
}

/* error_reset
 * reset Gigaset device because of an unrecoverable error
 * This function may be called from any context and takes care of scheduling
 * the necessary actions for execution outside of interrupt context.
 * argument:
 *	controller state structure
 */
static inline void error_reset(struct cardstate *cs)
{
	//FIXME try to recover without bothering the user
298 299
	dev_err(cs->dev,
	    "unrecoverable error - please disconnect Gigaset base to reset\n");
300 301 302 303 304
}

/* check_pending
 * check for completion of pending control request
 * parameter:
305
 *	ucs	hardware specific controller state structure
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 343 344 345
 */
static void check_pending(struct bas_cardstate *ucs)
{
	unsigned long flags;

	spin_lock_irqsave(&ucs->lock, flags);
	switch (ucs->pending) {
	case 0:
		break;
	case HD_OPEN_ATCHANNEL:
		if (atomic_read(&ucs->basstate) & BS_ATOPEN)
			ucs->pending = 0;
		break;
	case HD_OPEN_B1CHANNEL:
		if (atomic_read(&ucs->basstate) & BS_B1OPEN)
			ucs->pending = 0;
		break;
	case HD_OPEN_B2CHANNEL:
		if (atomic_read(&ucs->basstate) & BS_B2OPEN)
			ucs->pending = 0;
		break;
	case HD_CLOSE_ATCHANNEL:
		if (!(atomic_read(&ucs->basstate) & BS_ATOPEN))
			ucs->pending = 0;
		break;
	case HD_CLOSE_B1CHANNEL:
		if (!(atomic_read(&ucs->basstate) & BS_B1OPEN))
			ucs->pending = 0;
		break;
	case HD_CLOSE_B2CHANNEL:
		if (!(atomic_read(&ucs->basstate) & BS_B2OPEN))
			ucs->pending = 0;
		break;
	case HD_DEVICE_INIT_ACK:		/* no reply expected */
		ucs->pending = 0;
		break;
	/* HD_READ_ATMESSAGE, HD_WRITE_ATMESSAGE, HD_RESET_INTERRUPTPIPE
	 * are handled separately and should never end up here
	 */
	default:
346 347 348
		dev_warn(&ucs->interface->dev,
			 "unknown pending request 0x%02x cleared\n",
			 ucs->pending);
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
		ucs->pending = 0;
	}

	if (!ucs->pending)
		del_timer(&ucs->timer_ctrl);

	spin_unlock_irqrestore(&ucs->lock, flags);
}

/* cmd_in_timeout
 * timeout routine for command input request
 * argument:
 *	controller state structure
 */
static void cmd_in_timeout(unsigned long data)
{
	struct cardstate *cs = (struct cardstate *) data;
366
	struct bas_cardstate *ucs = cs->hw.bas;
367 368 369
	unsigned long flags;

	spin_lock_irqsave(&cs->lock, flags);
370 371
	if (unlikely(!atomic_read(&cs->connected))) {
		gig_dbg(DEBUG_USBREQ, "%s: disconnected", __func__);
372 373 374 375
		spin_unlock_irqrestore(&cs->lock, flags);
		return;
	}
	if (!ucs->rcvbuf_size) {
376
		gig_dbg(DEBUG_USBREQ, "%s: no receive in progress", __func__);
377 378 379 380 381
		spin_unlock_irqrestore(&cs->lock, flags);
		return;
	}
	spin_unlock_irqrestore(&cs->lock, flags);

382
	dev_err(cs->dev, "timeout reading AT response\n");
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
	error_reset(cs);	//FIXME retry?
}


static void read_ctrl_callback(struct urb *urb, struct pt_regs *regs);

/* atread_submit
 * submit an HD_READ_ATMESSAGE command URB
 * parameters:
 *	cs	controller state structure
 *	timeout	timeout in 1/10 sec., 0: none
 * return value:
 *	0 on success
 *	-EINVAL if a NULL pointer is encountered somewhere
 *	-EBUSY if another request is pending
 *	any URB submission error code
 */
static int atread_submit(struct cardstate *cs, int timeout)
{
402
	struct bas_cardstate *ucs = cs->hw.bas;
403 404
	int ret;

405 406
	gig_dbg(DEBUG_USBREQ, "-------> HD_READ_ATMESSAGE (%d)",
		ucs->rcvbuf_size);
407 408

	if (ucs->urb_cmd_in->status == -EINPROGRESS) {
409 410
		dev_err(cs->dev,
			"could not submit HD_READ_ATMESSAGE: URB busy\n");
411 412 413 414 415 416 417 418 419
		return -EBUSY;
	}

	ucs->dr_cmd_in.bRequestType = IN_VENDOR_REQ;
	ucs->dr_cmd_in.bRequest = HD_READ_ATMESSAGE;
	ucs->dr_cmd_in.wValue = 0;
	ucs->dr_cmd_in.wIndex = 0;
	ucs->dr_cmd_in.wLength = cpu_to_le16(ucs->rcvbuf_size);
	usb_fill_control_urb(ucs->urb_cmd_in, ucs->udev,
420 421 422 423
			     usb_rcvctrlpipe(ucs->udev, 0),
			     (unsigned char*) & ucs->dr_cmd_in,
			     ucs->rcvbuf, ucs->rcvbuf_size,
			     read_ctrl_callback, cs->inbuf);
424 425

	if ((ret = usb_submit_urb(ucs->urb_cmd_in, SLAB_ATOMIC)) != 0) {
426 427
		dev_err(cs->dev, "could not submit HD_READ_ATMESSAGE: %s\n",
			get_usb_statmsg(ret));
428 429 430 431
		return ret;
	}

	if (timeout > 0) {
432
		gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
433 434 435 436 437 438 439 440 441 442 443 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
		ucs->timer_cmd_in.expires = jiffies + timeout * HZ / 10;
		ucs->timer_cmd_in.data = (unsigned long) cs;
		ucs->timer_cmd_in.function = cmd_in_timeout;
		add_timer(&ucs->timer_cmd_in);
	}
	return 0;
}

static void stopurbs(struct bas_bc_state *);
static int start_cbsend(struct cardstate *);

/* set/clear bits in base connection state
 */
inline static void update_basstate(struct bas_cardstate *ucs,
				   int set, int clear)
{
	unsigned long flags;
	int state;

	spin_lock_irqsave(&ucs->lock, flags);
	state = atomic_read(&ucs->basstate);
	state &= ~clear;
	state |= set;
	atomic_set(&ucs->basstate, state);
	spin_unlock_irqrestore(&ucs->lock, flags);
}


/* read_int_callback
 * USB completion handler for interrupt pipe input
 * called by the USB subsystem in interrupt context
 * parameter:
 *	urb	USB request block
 *		urb->context = controller state structure
 */
static void read_int_callback(struct urb *urb, struct pt_regs *regs)
{
470 471
	struct cardstate *cs = urb->context;
	struct bas_cardstate *ucs = cs->hw.bas;
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
	struct bc_state *bcs;
	unsigned long flags;
	int status;
	unsigned l;
	int channel;

	if (unlikely(!atomic_read(&cs->connected))) {
		warn("%s: disconnected", __func__);
		return;
	}

	switch (urb->status) {
	case 0:			/* success */
		break;
	case -ENOENT:			/* canceled */
	case -ECONNRESET:		/* canceled (async) */
	case -EINPROGRESS:		/* pending */
		/* ignore silently */
490 491
		gig_dbg(DEBUG_USBREQ, "%s: %s",
			__func__, get_usb_statmsg(urb->status));
492 493
		return;
	default:		/* severe trouble */
494 495
		dev_warn(cs->dev, "interrupt read: %s\n",
			 get_usb_statmsg(urb->status));
496 497 498 499 500 501 502
		//FIXME corrective action? resubmission always ok?
		goto resubmit;
	}

	l = (unsigned) ucs->int_in_buf[1] +
	    (((unsigned) ucs->int_in_buf[2]) << 8);

503 504 505
	gig_dbg(DEBUG_USBREQ, "<-------%d: 0x%02x (%u [0x%02x 0x%02x])",
		urb->actual_length, (int)ucs->int_in_buf[0], l,
		(int)ucs->int_in_buf[1], (int)ucs->int_in_buf[2]);
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550

	channel = 0;

	switch (ucs->int_in_buf[0]) {
	case HD_DEVICE_INIT_OK:
		update_basstate(ucs, BS_INIT, 0);
		break;

	case HD_READY_SEND_ATDATA:
		del_timer(&ucs->timer_atrdy);
		update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
		start_cbsend(cs);
		break;

	case HD_OPEN_B2CHANNEL_ACK:
		++channel;
	case HD_OPEN_B1CHANNEL_ACK:
		bcs = cs->bcs + channel;
		update_basstate(ucs, BS_B1OPEN << channel, 0);
		gigaset_bchannel_up(bcs);
		break;

	case HD_OPEN_ATCHANNEL_ACK:
		update_basstate(ucs, BS_ATOPEN, 0);
		start_cbsend(cs);
		break;

	case HD_CLOSE_B2CHANNEL_ACK:
		++channel;
	case HD_CLOSE_B1CHANNEL_ACK:
		bcs = cs->bcs + channel;
		update_basstate(ucs, 0, BS_B1OPEN << channel);
		stopurbs(bcs->hw.bas);
		gigaset_bchannel_down(bcs);
		break;

	case HD_CLOSE_ATCHANNEL_ACK:
		update_basstate(ucs, 0, BS_ATOPEN);
		break;

	case HD_B2_FLOW_CONTROL:
		++channel;
	case HD_B1_FLOW_CONTROL:
		bcs = cs->bcs + channel;
		atomic_add((l - BAS_NORMFRAME) * BAS_CORRFRAMES,
551 552 553 554 555
			   &bcs->hw.bas->corrbytes);
		gig_dbg(DEBUG_ISO,
			"Flow control (channel %d, sub %d): 0x%02x => %d",
			channel, bcs->hw.bas->numsub, l,
			atomic_read(&bcs->hw.bas->corrbytes));
556 557 558 559
		break;

	case HD_RECEIVEATDATA_ACK:	/* AT response ready to be received */
		if (!l) {
560 561
			dev_warn(cs->dev,
				"HD_RECEIVEATDATA_ACK with length 0 ignored\n");
562 563 564 565 566
			break;
		}
		spin_lock_irqsave(&cs->lock, flags);
		if (ucs->rcvbuf_size) {
			spin_unlock_irqrestore(&cs->lock, flags);
567 568
			dev_err(cs->dev,
				"receive AT data overrun, %d bytes lost\n", l);
569 570 571 572 573
			error_reset(cs);	//FIXME reschedule
			break;
		}
		if ((ucs->rcvbuf = kmalloc(l, GFP_ATOMIC)) == NULL) {
			spin_unlock_irqrestore(&cs->lock, flags);
574
			dev_err(cs->dev, "out of memory, %d bytes lost\n", l);
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
			error_reset(cs);	//FIXME reschedule
			break;
		}
		ucs->rcvbuf_size = l;
		ucs->retry_cmd_in = 0;
		if ((status = atread_submit(cs, BAS_TIMEOUT)) < 0) {
			kfree(ucs->rcvbuf);
			ucs->rcvbuf = NULL;
			ucs->rcvbuf_size = 0;
			error_reset(cs);	//FIXME reschedule
		}
		spin_unlock_irqrestore(&cs->lock, flags);
		break;

	case HD_RESET_INTERRUPT_PIPE_ACK:
590
		gig_dbg(DEBUG_USBREQ, "HD_RESET_INTERRUPT_PIPE_ACK");
591 592 593
		break;

	case HD_SUSPEND_END:
594
		gig_dbg(DEBUG_USBREQ, "HD_SUSPEND_END");
595 596 597
		break;

	default:
598 599 600
		dev_warn(cs->dev,
			 "unknown Gigaset signal 0x%02x (%u) ignored\n",
			 (int) ucs->int_in_buf[0], l);
601 602 603 604 605 606 607
	}

	check_pending(ucs);

resubmit:
	status = usb_submit_urb(urb, SLAB_ATOMIC);
	if (unlikely(status)) {
608 609
		dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
			get_usb_statmsg(status));
610 611 612 613 614 615 616 617 618 619 620 621 622
		error_reset(cs);
	}
}

/* read_ctrl_callback
 * USB completion handler for control pipe input
 * called by the USB subsystem in interrupt context
 * parameter:
 *	urb	USB request block
 *		urb->context = inbuf structure for controller state
 */
static void read_ctrl_callback(struct urb *urb, struct pt_regs *regs)
{
623 624 625 626
	struct inbuf_t *inbuf = urb->context;
	struct cardstate *cs = inbuf->cs;
	struct bas_cardstate *ucs = cs->hw.bas;
	int have_data = 0;
627 628 629 630
	unsigned numbytes;
	unsigned long flags;

	spin_lock_irqsave(&cs->lock, flags);
631
	if (unlikely(!atomic_read(&cs->connected))) {
632 633 634 635 636 637
		warn("%s: disconnected", __func__);
		spin_unlock_irqrestore(&cs->lock, flags);
		return;
	}

	if (!ucs->rcvbuf_size) {
638
		dev_warn(cs->dev, "%s: no receive in progress\n", __func__);
639 640 641 642 643 644 645 646 647 648
		spin_unlock_irqrestore(&cs->lock, flags);
		return;
	}

	del_timer(&ucs->timer_cmd_in);

	switch (urb->status) {
	case 0:				/* normal completion */
		numbytes = urb->actual_length;
		if (unlikely(numbytes == 0)) {
649 650
			dev_warn(cs->dev,
				 "control read: empty block received\n");
651 652 653
			goto retry;
		}
		if (unlikely(numbytes != ucs->rcvbuf_size)) {
654 655 656
			dev_warn(cs->dev,
			       "control read: received %d chars, expected %d\n",
				 numbytes, ucs->rcvbuf_size);
657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675
			if (numbytes > ucs->rcvbuf_size)
				numbytes = ucs->rcvbuf_size;
		}

		/* copy received bytes to inbuf */
		have_data = gigaset_fill_inbuf(inbuf, ucs->rcvbuf, numbytes);

		if (unlikely(numbytes < ucs->rcvbuf_size)) {
			/* incomplete - resubmit for remaining bytes */
			ucs->rcvbuf_size -= numbytes;
			ucs->retry_cmd_in = 0;
			goto retry;
		}
		break;

	case -ENOENT:			/* canceled */
	case -ECONNRESET:		/* canceled (async) */
	case -EINPROGRESS:		/* pending */
		/* no action necessary */
676 677
		gig_dbg(DEBUG_USBREQ, "%s: %s",
			__func__, get_usb_statmsg(urb->status));
678 679 680
		break;

	default:			/* severe trouble */
681 682
		dev_warn(cs->dev, "control read: %s\n",
			 get_usb_statmsg(urb->status));
683 684
	retry:
		if (ucs->retry_cmd_in++ < BAS_RETRY) {
685 686
			dev_notice(cs->dev, "control read: retry %d\n",
				   ucs->retry_cmd_in);
687 688 689 690 691 692
			if (atread_submit(cs, BAS_TIMEOUT) >= 0) {
				/* resubmitted - bypass regular exit block */
				spin_unlock_irqrestore(&cs->lock, flags);
				return;
			}
		} else {
693 694 695
			dev_err(cs->dev,
				"control read: giving up after %d tries\n",
				ucs->retry_cmd_in);
696 697 698 699 700 701 702 703 704
		}
		error_reset(cs);
	}

	kfree(ucs->rcvbuf);
	ucs->rcvbuf = NULL;
	ucs->rcvbuf_size = 0;
	spin_unlock_irqrestore(&cs->lock, flags);
	if (have_data) {
705
		gig_dbg(DEBUG_INTR, "%s-->BH", __func__);
706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
		gigaset_schedule_event(cs);
	}
}

/* read_iso_callback
 * USB completion handler for B channel isochronous input
 * called by the USB subsystem in interrupt context
 * parameter:
 *	urb	USB request block of completed request
 *		urb->context = bc_state structure
 */
static void read_iso_callback(struct urb *urb, struct pt_regs *regs)
{
	struct bc_state *bcs;
	struct bas_bc_state *ubc;
	unsigned long flags;
	int i, rc;

	/* status codes not worth bothering the tasklet with */
	if (unlikely(urb->status == -ENOENT || urb->status == -ECONNRESET ||
726 727 728
		     urb->status == -EINPROGRESS)) {
		gig_dbg(DEBUG_ISO, "%s: %s",
			__func__, get_usb_statmsg(urb->status));
729 730 731
		return;
	}

732
	bcs = urb->context;
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752
	ubc = bcs->hw.bas;

	spin_lock_irqsave(&ubc->isoinlock, flags);
	if (likely(ubc->isoindone == NULL)) {
		/* pass URB to tasklet */
		ubc->isoindone = urb;
		tasklet_schedule(&ubc->rcvd_tasklet);
	} else {
		/* tasklet still busy, drop data and resubmit URB */
		ubc->loststatus = urb->status;
		for (i = 0; i < BAS_NUMFRAMES; i++) {
			ubc->isoinlost += urb->iso_frame_desc[i].actual_length;
			if (unlikely(urb->iso_frame_desc[i].status != 0 &&
				     urb->iso_frame_desc[i].status != -EINPROGRESS)) {
				ubc->loststatus = urb->iso_frame_desc[i].status;
			}
			urb->iso_frame_desc[i].status = 0;
			urb->iso_frame_desc[i].actual_length = 0;
		}
		if (likely(atomic_read(&ubc->running))) {
753 754
			/* urb->dev is clobbered by USB subsystem */
			urb->dev = bcs->cs->hw.bas->udev;
755 756
			urb->transfer_flags = URB_ISO_ASAP;
			urb->number_of_packets = BAS_NUMFRAMES;
757 758
			gig_dbg(DEBUG_ISO, "%s: isoc read overrun/resubmit",
				__func__);
759 760
			rc = usb_submit_urb(urb, SLAB_ATOMIC);
			if (unlikely(rc != 0)) {
761 762 763
				dev_err(bcs->cs->dev,
					"could not resubmit isochronous read "
					"URB: %s\n", get_usb_statmsg(rc));
764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786
				dump_urb(DEBUG_ISO, "isoc read", urb);
				error_hangup(bcs);
			}
		}
	}
	spin_unlock_irqrestore(&ubc->isoinlock, flags);
}

/* write_iso_callback
 * USB completion handler for B channel isochronous output
 * called by the USB subsystem in interrupt context
 * parameter:
 *	urb	USB request block of completed request
 *		urb->context = isow_urbctx_t structure
 */
static void write_iso_callback(struct urb *urb, struct pt_regs *regs)
{
	struct isow_urbctx_t *ucx;
	struct bas_bc_state *ubc;
	unsigned long flags;

	/* status codes not worth bothering the tasklet with */
	if (unlikely(urb->status == -ENOENT || urb->status == -ECONNRESET ||
787 788 789
		     urb->status == -EINPROGRESS)) {
		gig_dbg(DEBUG_ISO, "%s: %s",
			__func__, get_usb_statmsg(urb->status));
790 791 792 793
		return;
	}

	/* pass URB context to tasklet */
794
	ucx = urb->context;
795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813
	ubc = ucx->bcs->hw.bas;

	spin_lock_irqsave(&ubc->isooutlock, flags);
	ubc->isooutovfl = ubc->isooutdone;
	ubc->isooutdone = ucx;
	spin_unlock_irqrestore(&ubc->isooutlock, flags);
	tasklet_schedule(&ubc->sent_tasklet);
}

/* starturbs
 * prepare and submit USB request blocks for isochronous input and output
 * argument:
 *	B channel control structure
 * return value:
 *	0 on success
 *	< 0 on error (no URBs submitted)
 */
static int starturbs(struct bc_state *bcs)
{
814
	struct bas_bc_state *ubc = bcs->hw.bas;
815 816 817 818 819 820 821 822 823 824 825 826 827
	struct urb *urb;
	int j, k;
	int rc;

	/* initialize L2 reception */
	if (bcs->proto2 == ISDN_PROTO_L2_HDLC)
		bcs->inputstate |= INS_flag_hunt;

	/* submit all isochronous input URBs */
	atomic_set(&ubc->running, 1);
	for (k = 0; k < BAS_INURBS; k++) {
		urb = ubc->isoinurbs[k];
		if (!urb) {
828
			dev_err(bcs->cs->dev, "isoinurbs[%d]==NULL\n", k);
829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850
			rc = -EFAULT;
			goto error;
		}

		urb->dev = bcs->cs->hw.bas->udev;
		urb->pipe = usb_rcvisocpipe(urb->dev, 3 + 2 * bcs->channel);
		urb->transfer_flags = URB_ISO_ASAP;
		urb->transfer_buffer = ubc->isoinbuf + k * BAS_INBUFSIZE;
		urb->transfer_buffer_length = BAS_INBUFSIZE;
		urb->number_of_packets = BAS_NUMFRAMES;
		urb->interval = BAS_FRAMETIME;
		urb->complete = read_iso_callback;
		urb->context = bcs;
		for (j = 0; j < BAS_NUMFRAMES; j++) {
			urb->iso_frame_desc[j].offset = j * BAS_MAXFRAME;
			urb->iso_frame_desc[j].length = BAS_MAXFRAME;
			urb->iso_frame_desc[j].status = 0;
			urb->iso_frame_desc[j].actual_length = 0;
		}

		dump_urb(DEBUG_ISO, "Initial isoc read", urb);
		if ((rc = usb_submit_urb(urb, SLAB_ATOMIC)) != 0) {
851 852 853
			dev_err(bcs->cs->dev,
			       "could not submit isochronous read URB %d: %s\n",
				k, get_usb_statmsg(rc));
854 855 856 857 858 859 860 861 862 863 864
			goto error;
		}
	}

	/* initialize L2 transmission */
	gigaset_isowbuf_init(ubc->isooutbuf, PPP_FLAG);

	/* set up isochronous output URBs for flag idling */
	for (k = 0; k < BAS_OUTURBS; ++k) {
		urb = ubc->isoouturbs[k].urb;
		if (!urb) {
865
			dev_err(bcs->cs->dev, "isoouturbs[%d].urb==NULL\n", k);
866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891
			rc = -EFAULT;
			goto error;
		}
		urb->dev = bcs->cs->hw.bas->udev;
		urb->pipe = usb_sndisocpipe(urb->dev, 4 + 2 * bcs->channel);
		urb->transfer_flags = URB_ISO_ASAP;
		urb->transfer_buffer = ubc->isooutbuf->data;
		urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
		urb->number_of_packets = BAS_NUMFRAMES;
		urb->interval = BAS_FRAMETIME;
		urb->complete = write_iso_callback;
		urb->context = &ubc->isoouturbs[k];
		for (j = 0; j < BAS_NUMFRAMES; ++j) {
			urb->iso_frame_desc[j].offset = BAS_OUTBUFSIZE;
			urb->iso_frame_desc[j].length = BAS_NORMFRAME;
			urb->iso_frame_desc[j].status = 0;
			urb->iso_frame_desc[j].actual_length = 0;
		}
		ubc->isoouturbs[k].limit = -1;
	}

	/* submit two URBs, keep third one */
	for (k = 0; k < 2; ++k) {
		dump_urb(DEBUG_ISO, "Initial isoc write", urb);
		rc = usb_submit_urb(ubc->isoouturbs[k].urb, SLAB_ATOMIC);
		if (rc != 0) {
892 893 894
			dev_err(bcs->cs->dev,
			      "could not submit isochronous write URB %d: %s\n",
				k, get_usb_statmsg(rc));
895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
			goto error;
		}
	}
	dump_urb(DEBUG_ISO, "Initial isoc write (free)", urb);
	ubc->isooutfree = &ubc->isoouturbs[2];
	ubc->isooutdone = ubc->isooutovfl = NULL;
	return 0;
 error:
	stopurbs(ubc);
	return rc;
}

/* stopurbs
 * cancel the USB request blocks for isochronous input and output
 * errors are silently ignored
 * argument:
 *	B channel control structure
 */
static void stopurbs(struct bas_bc_state *ubc)
{
	int k, rc;

	atomic_set(&ubc->running, 0);

	for (k = 0; k < BAS_INURBS; ++k) {
		rc = usb_unlink_urb(ubc->isoinurbs[k]);
921 922 923
		gig_dbg(DEBUG_ISO,
			"%s: isoc input URB %d unlinked, result = %d",
			__func__, k, rc);
924 925 926 927
	}

	for (k = 0; k < BAS_OUTURBS; ++k) {
		rc = usb_unlink_urb(ubc->isoouturbs[k].urb);
928 929 930
		gig_dbg(DEBUG_ISO,
			"%s: isoc output URB %d unlinked, result = %d",
			__func__, k, rc);
931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947
	}
}

/* Isochronous Write - Bottom Half */
/* =============================== */

/* submit_iso_write_urb
 * fill and submit the next isochronous write URB
 * parameters:
 *	bcs	B channel state structure
 * return value:
 *	number of frames submitted in URB
 *	0 if URB not submitted because no data available (isooutbuf busy)
 *	error code < 0 on error
 */
static int submit_iso_write_urb(struct isow_urbctx_t *ucx)
{
948 949
	struct urb *urb = ucx->urb;
	struct bas_bc_state *ubc = ucx->bcs->hw.bas;
950 951 952
	struct usb_iso_packet_descriptor *ifd;
	int corrbytes, nframe, rc;

953 954
	/* urb->dev is clobbered by USB subsystem */
	urb->dev = ucx->bcs->cs->hw.bas->udev;
955 956 957 958 959 960 961 962 963 964
	urb->transfer_flags = URB_ISO_ASAP;
	urb->transfer_buffer = ubc->isooutbuf->data;
	urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);

	for (nframe = 0; nframe < BAS_NUMFRAMES; nframe++) {
		ifd = &urb->iso_frame_desc[nframe];

		/* compute frame length according to flow control */
		ifd->length = BAS_NORMFRAME;
		if ((corrbytes = atomic_read(&ubc->corrbytes)) != 0) {
965 966
			gig_dbg(DEBUG_ISO, "%s: corrbytes=%d",
				__func__, corrbytes);
967 968 969 970 971 972 973 974 975
			if (corrbytes > BAS_HIGHFRAME - BAS_NORMFRAME)
				corrbytes = BAS_HIGHFRAME - BAS_NORMFRAME;
			else if (corrbytes < BAS_LOWFRAME - BAS_NORMFRAME)
				corrbytes = BAS_LOWFRAME - BAS_NORMFRAME;
			ifd->length += corrbytes;
			atomic_add(-corrbytes, &ubc->corrbytes);
		}

		/* retrieve block of data to send */
976 977
		ifd->offset = gigaset_isowbuf_getbytes(ubc->isooutbuf,
						       ifd->length);
978 979
		if (ifd->offset < 0) {
			if (ifd->offset == -EBUSY) {
980 981 982 983 984
				gig_dbg(DEBUG_ISO,
					"%s: buffer busy at frame %d",
					__func__, nframe);
				/* tasklet will be restarted from
				   gigaset_send_skb() */
985
			} else {
986 987 988
				dev_err(ucx->bcs->cs->dev,
					"%s: buffer error %d at frame %d\n",
					__func__, ifd->offset, nframe);
989 990 991 992 993 994 995 996 997 998
				return ifd->offset;
			}
			break;
		}
		ucx->limit = atomic_read(&ubc->isooutbuf->nextread);
		ifd->status = 0;
		ifd->actual_length = 0;
	}
	if ((urb->number_of_packets = nframe) > 0) {
		if ((rc = usb_submit_urb(urb, SLAB_ATOMIC)) != 0) {
999 1000 1001
			dev_err(ucx->bcs->cs->dev,
				"could not submit isochronous write URB: %s\n",
				get_usb_statmsg(rc));
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
			dump_urb(DEBUG_ISO, "isoc write", urb);
			return rc;
		}
		++ubc->numsub;
	}
	return nframe;
}

/* write_iso_tasklet
 * tasklet scheduled when an isochronous output URB from the Gigaset device
 * has completed
 * parameter:
 *	data	B channel state structure
 */
static void write_iso_tasklet(unsigned long data)
{
1018 1019 1020
	struct bc_state *bcs = (struct bc_state *) data;
	struct bas_bc_state *ubc = bcs->hw.bas;
	struct cardstate *cs = bcs->cs;
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
	struct isow_urbctx_t *done, *next, *ovfl;
	struct urb *urb;
	struct usb_iso_packet_descriptor *ifd;
	int offset;
	unsigned long flags;
	int i;
	struct sk_buff *skb;
	int len;

	/* loop while completed URBs arrive in time */
	for (;;) {
		if (unlikely(!atomic_read(&cs->connected))) {
			warn("%s: disconnected", __func__);
			return;
		}

		if (unlikely(!(atomic_read(&ubc->running)))) {
1038
			gig_dbg(DEBUG_ISO, "%s: not running", __func__);
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
			return;
		}

		/* retrieve completed URBs */
		spin_lock_irqsave(&ubc->isooutlock, flags);
		done = ubc->isooutdone;
		ubc->isooutdone = NULL;
		ovfl = ubc->isooutovfl;
		ubc->isooutovfl = NULL;
		spin_unlock_irqrestore(&ubc->isooutlock, flags);
		if (ovfl) {
1050
			dev_err(cs->dev, "isochronous write buffer underrun\n");
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
			error_hangup(bcs);
			break;
		}
		if (!done)
			break;

		/* submit free URB if available */
		spin_lock_irqsave(&ubc->isooutlock, flags);
		next = ubc->isooutfree;
		ubc->isooutfree = NULL;
		spin_unlock_irqrestore(&ubc->isooutlock, flags);
		if (next) {
			if (submit_iso_write_urb(next) <= 0) {
				/* could not submit URB, put it back */
				spin_lock_irqsave(&ubc->isooutlock, flags);
				if (ubc->isooutfree == NULL) {
					ubc->isooutfree = next;
					next = NULL;
				}
				spin_unlock_irqrestore(&ubc->isooutlock, flags);
				if (next) {
					/* couldn't put it back */
1073 1074
					dev_err(cs->dev,
					      "losing isochronous write URB\n");
1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
					error_hangup(bcs);
				}
			}
		}

		/* process completed URB */
		urb = done->urb;
		switch (urb->status) {
		case 0:				/* normal completion */
			break;
		case -EXDEV:			/* inspect individual frames */
			/* assumptions (for lack of documentation):
1087 1088
			 * - actual_length bytes of the frame in error are
			 *   successfully sent
1089 1090
			 * - all following frames are not sent at all
			 */
1091 1092
			gig_dbg(DEBUG_ISO, "%s: URB partially completed",
				__func__);
1093 1094 1095 1096 1097
			offset = done->limit;	/* just in case */
			for (i = 0; i < BAS_NUMFRAMES; i++) {
				ifd = &urb->iso_frame_desc[i];
				if (ifd->status ||
				    ifd->actual_length != ifd->length) {
1098 1099 1100
					dev_warn(cs->dev,
					     "isochronous write: frame %d: %s, "
					     "only %d of %d bytes sent\n",
1101 1102 1103
					     i, get_usb_statmsg(ifd->status),
					     ifd->actual_length, ifd->length);
					offset = (ifd->offset +
1104 1105
						  ifd->actual_length)
						 % BAS_OUTBUFSIZE;
1106 1107 1108 1109 1110 1111 1112 1113 1114
					break;
				}
			}
#ifdef CONFIG_GIGASET_DEBUG
			/* check assumption on remaining frames */
			for (; i < BAS_NUMFRAMES; i++) {
				ifd = &urb->iso_frame_desc[i];
				if (ifd->status != -EINPROGRESS
				    || ifd->actual_length != 0) {
1115 1116 1117
					dev_warn(cs->dev,
					     "isochronous write: frame %d: %s, "
					     "%d of %d bytes sent\n",
1118 1119 1120
					     i, get_usb_statmsg(ifd->status),
					     ifd->actual_length, ifd->length);
					offset = (ifd->offset +
1121 1122
						  ifd->actual_length)
						 % BAS_OUTBUFSIZE;
1123 1124 1125 1126 1127
					break;
				}
			}
#endif
			break;
1128 1129
		case -EPIPE:		//FIXME is this the code for "underrun"?
			dev_err(cs->dev, "isochronous write stalled\n");
1130 1131 1132
			error_hangup(bcs);
			break;
		default:			/* severe trouble */
1133 1134
			dev_warn(cs->dev, "isochronous write: %s\n",
				 get_usb_statmsg(urb->status));
1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161
		}

		/* mark the write buffer area covered by this URB as free */
		if (done->limit >= 0)
			atomic_set(&ubc->isooutbuf->read, done->limit);

		/* mark URB as free */
		spin_lock_irqsave(&ubc->isooutlock, flags);
		next = ubc->isooutfree;
		ubc->isooutfree = done;
		spin_unlock_irqrestore(&ubc->isooutlock, flags);
		if (next) {
			/* only one URB still active - resubmit one */
			if (submit_iso_write_urb(next) <= 0) {
				/* couldn't submit */
				error_hangup(bcs);
			}
		}
	}

	/* process queued SKBs */
	while ((skb = skb_dequeue(&bcs->squeue))) {
		/* copy to output buffer, doing L2 encapsulation */
		len = skb->len;
		if (gigaset_isoc_buildframe(bcs, skb->data, len) == -EAGAIN) {
			/* insufficient buffer space, push back onto queue */
			skb_queue_head(&bcs->squeue, skb);
1162 1163
			gig_dbg(DEBUG_ISO, "%s: skb requeued, qlen=%d",
				__func__, skb_queue_len(&bcs->squeue));
1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182
			break;
		}
		skb_pull(skb, len);
		gigaset_skb_sent(bcs, skb);
		dev_kfree_skb_any(skb);
	}
}

/* Isochronous Read - Bottom Half */
/* ============================== */

/* read_iso_tasklet
 * tasklet scheduled when an isochronous input URB from the Gigaset device
 * has completed
 * parameter:
 *	data	B channel state structure
 */
static void read_iso_tasklet(unsigned long data)
{
1183 1184 1185
	struct bc_state *bcs = (struct bc_state *) data;
	struct bas_bc_state *ubc = bcs->hw.bas;
	struct cardstate *cs = bcs->cs;
1186 1187 1188 1189 1190 1191 1192
	struct urb *urb;
	char *rcvbuf;
	unsigned long flags;
	int totleft, numbytes, offset, frame, rc;

	/* loop while more completed URBs arrive in the meantime */
	for (;;) {
1193
		if (unlikely(!atomic_read(&cs->connected))) {
1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205
			warn("%s: disconnected", __func__);
			return;
		}

		/* retrieve URB */
		spin_lock_irqsave(&ubc->isoinlock, flags);
		if (!(urb = ubc->isoindone)) {
			spin_unlock_irqrestore(&ubc->isoinlock, flags);
			return;
		}
		ubc->isoindone = NULL;
		if (unlikely(ubc->loststatus != -EINPROGRESS)) {
1206 1207 1208 1209 1210
			dev_warn(cs->dev,
				 "isochronous read overrun, "
				 "dropped URB with status: %s, %d bytes lost\n",
				 get_usb_statmsg(ubc->loststatus),
				 ubc->isoinlost);
1211 1212 1213 1214 1215
			ubc->loststatus = -EINPROGRESS;
		}
		spin_unlock_irqrestore(&ubc->isoinlock, flags);

		if (unlikely(!(atomic_read(&ubc->running)))) {
1216 1217 1218 1219
			gig_dbg(DEBUG_ISO,
				"%s: channel not running, "
				"dropped URB with status: %s",
				__func__, get_usb_statmsg(urb->status));
1220 1221 1222 1223 1224 1225
			return;
		}

		switch (urb->status) {
		case 0:				/* normal completion */
			break;
1226 1227
		case -EXDEV:			/* inspect individual frames
						   (we do that anyway) */
1228 1229
			gig_dbg(DEBUG_ISO, "%s: URB partially completed",
				__func__);
1230 1231 1232
			break;
		case -ENOENT:
		case -ECONNRESET:
1233
			gig_dbg(DEBUG_ISO, "%s: URB canceled", __func__);
1234 1235
			continue;		/* -> skip */
		case -EINPROGRESS:		/* huh? */
1236
			gig_dbg(DEBUG_ISO, "%s: URB still pending", __func__);
1237 1238
			continue;		/* -> skip */
		case -EPIPE:
1239
			dev_err(cs->dev, "isochronous read stalled\n");
1240 1241 1242
			error_hangup(bcs);
			continue;		/* -> skip */
		default:			/* severe trouble */
1243 1244
			dev_warn(cs->dev, "isochronous read: %s\n",
				 get_usb_statmsg(urb->status));
1245 1246 1247 1248 1249 1250 1251
			goto error;
		}

		rcvbuf = urb->transfer_buffer;
		totleft = urb->actual_length;
		for (frame = 0; totleft > 0 && frame < BAS_NUMFRAMES; frame++) {
			if (unlikely(urb->iso_frame_desc[frame].status)) {
1252 1253 1254 1255 1256
				dev_warn(cs->dev,
					 "isochronous read: frame %d: %s\n",
					 frame,
					 get_usb_statmsg(
					    urb->iso_frame_desc[frame].status));
1257 1258 1259 1260
				break;
			}
			numbytes = urb->iso_frame_desc[frame].actual_length;
			if (unlikely(numbytes > BAS_MAXFRAME)) {
1261 1262 1263 1264
				dev_warn(cs->dev,
					 "isochronous read: frame %d: "
					 "numbytes (%d) > BAS_MAXFRAME\n",
					 frame, numbytes);
1265 1266 1267
				break;
			}
			if (unlikely(numbytes > totleft)) {
1268 1269 1270 1271
				dev_warn(cs->dev,
					 "isochronous read: frame %d: "
					 "numbytes (%d) > totleft (%d)\n",
					 frame, numbytes, totleft);
1272 1273 1274 1275
				break;
			}
			offset = urb->iso_frame_desc[frame].offset;
			if (unlikely(offset + numbytes > BAS_INBUFSIZE)) {
1276 1277 1278 1279 1280
				dev_warn(cs->dev,
					 "isochronous read: frame %d: "
					 "offset (%d) + numbytes (%d) "
					 "> BAS_INBUFSIZE\n",
					 frame, offset, numbytes);
1281 1282 1283 1284 1285 1286
				break;
			}
			gigaset_isoc_receive(rcvbuf + offset, numbytes, bcs);
			totleft -= numbytes;
		}
		if (unlikely(totleft > 0))
1287 1288 1289
			dev_warn(cs->dev,
				 "isochronous read: %d data bytes missing\n",
				 totleft);
1290 1291 1292 1293 1294 1295 1296

	error:
		/* URB processed, resubmit */
		for (frame = 0; frame < BAS_NUMFRAMES; frame++) {
			urb->iso_frame_desc[frame].status = 0;
			urb->iso_frame_desc[frame].actual_length = 0;
		}
1297 1298
		/* urb->dev is clobbered by USB subsystem */
		urb->dev = bcs->cs->hw.bas->udev;
1299 1300 1301
		urb->transfer_flags = URB_ISO_ASAP;
		urb->number_of_packets = BAS_NUMFRAMES;
		if ((rc = usb_submit_urb(urb, SLAB_ATOMIC)) != 0) {
1302 1303 1304
			dev_err(cs->dev,
				"could not resubmit isochronous read URB: %s\n",
				get_usb_statmsg(rc));
1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321
			dump_urb(DEBUG_ISO, "resubmit iso read", urb);
			error_hangup(bcs);
		}
	}
}

/* Channel Operations */
/* ================== */

/* req_timeout
 * timeout routine for control output request
 * argument:
 *	B channel control structure
 */
static void req_timeout(unsigned long data)
{
	struct bc_state *bcs = (struct bc_state *) data;
1322
	struct bas_cardstate *ucs = bcs->cs->hw.bas;
1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334
	int pending;
	unsigned long flags;

	check_pending(ucs);

	spin_lock_irqsave(&ucs->lock, flags);
	pending = ucs->pending;
	ucs->pending = 0;
	spin_unlock_irqrestore(&ucs->lock, flags);

	switch (pending) {
	case 0:					/* no pending request */
1335
		gig_dbg(DEBUG_USBREQ, "%s: no request pending", __func__);
1336 1337 1338
		break;

	case HD_OPEN_ATCHANNEL:
1339
		dev_err(bcs->cs->dev, "timeout opening AT channel\n");
1340 1341 1342 1343 1344
		error_reset(bcs->cs);
		break;

	case HD_OPEN_B2CHANNEL:
	case HD_OPEN_B1CHANNEL:
1345 1346
		dev_err(bcs->cs->dev, "timeout opening channel %d\n",
			bcs->channel + 1);
1347 1348 1349 1350
		error_hangup(bcs);
		break;

	case HD_CLOSE_ATCHANNEL:
1351
		dev_err(bcs->cs->dev, "timeout closing AT channel\n");
1352 1353 1354 1355
		break;

	case HD_CLOSE_B2CHANNEL:
	case HD_CLOSE_B1CHANNEL:
1356 1357
		dev_err(bcs->cs->dev, "timeout closing channel %d\n",
			bcs->channel + 1);
1358 1359 1360
		break;

	default:
1361 1362
		dev_warn(bcs->cs->dev, "request 0x%02x timed out, clearing\n",
			 pending);
1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374
	}
}

/* write_ctrl_callback
 * USB completion handler for control pipe output
 * called by the USB subsystem in interrupt context
 * parameter:
 *	urb	USB request block of completed request
 *		urb->context = hardware specific controller state structure
 */
static void write_ctrl_callback(struct urb *urb, struct pt_regs *regs)
{
1375
	struct bas_cardstate *ucs = urb->context;
1376 1377 1378 1379
	unsigned long flags;

	spin_lock_irqsave(&ucs->lock, flags);
	if (urb->status && ucs->pending) {
1380 1381 1382
		dev_err(&ucs->interface->dev,
			"control request 0x%02x failed: %s\n",
			ucs->pending, get_usb_statmsg(urb-