bas-gigaset.c 65.1 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
 *
 * =====================================================================
 *	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 */
27
#define DRIVER_AUTHOR "Tilman Schmidt <tilman@imap.cc>, Hansjoerg Lipp <hjlipp@web.de>, Stefan Eilers"
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
#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_DEVNAME    "ttyGB"

46 47
/* length limit according to Siemens 3070usb-protokoll.doc ch. 2.1 */
#define IF_WRITEBUF 264
48 49 50

/* Values for the Gigaset 307x */
#define USB_GIGA_VENDOR_ID      0x0681
51 52
#define USB_3070_PRODUCT_ID     0x0001
#define USB_3075_PRODUCT_ID     0x0002
53 54 55 56 57
#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 [] = {
58 59
	{ USB_DEVICE(USB_GIGA_VENDOR_ID, USB_3070_PRODUCT_ID) },
	{ USB_DEVICE(USB_GIGA_VENDOR_ID, USB_3075_PRODUCT_ID) },
60 61 62 63 64 65 66
	{ 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);

67
/*======================= local function prototypes ==========================*/
68

69
/* function called if a new device belonging to this driver is connected */
70 71 72 73 74 75
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);

76
static int atread_submit(struct cardstate *, int);
77
static void stopurbs(struct bas_bc_state *);
78
static int req_submit(struct bc_state *, int, int, int);
79 80
static int atwrite_submit(struct cardstate *, unsigned char *, int);
static int start_cbsend(struct cardstate *);
81

82
/*============================================================================*/
83 84

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

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

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

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

104
	struct urb		*urb_int_in;	/* URB for interrupt pipe */
105 106 107 108 109 110 111 112 113 114 115
	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) */
116 117 118 119 120 121 122 123
#define BS_ATOPEN	0x001	/* AT channel open */
#define BS_B1OPEN	0x002	/* B channel 1 open */
#define BS_B2OPEN	0x004	/* B channel 2 open */
#define BS_ATREADY	0x008	/* base ready for AT command */
#define BS_INIT		0x010	/* base has signalled INIT_OK */
#define BS_ATTIMER	0x020	/* waiting for HD_READY_SEND_ATDATA */
#define BS_ATRDPEND	0x040	/* urb_cmd_in in use */
#define BS_ATWRPEND	0x080	/* urb_cmd_out in use */
124 125 126 127 128 129 130 131 132 133 134 135 136


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,
};

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
/* get message text for usb_submit_urb return code
 */
static char *get_usb_rcmsg(int rc)
{
	static char unkmsg[28];

	switch (rc) {
	case 0:
		return "success";
	case -ENOMEM:
		return "out of memory";
	case -ENODEV:
		return "device not present";
	case -ENOENT:
		return "endpoint not present";
	case -ENXIO:
		return "URB type not supported";
	case -EINVAL:
		return "invalid argument";
	case -EAGAIN:
		return "start frame too early or too much scheduled";
	case -EFBIG:
		return "too many isochronous frames requested";
	case -EPIPE:
		return "endpoint stalled";
	case -EMSGSIZE:
		return "invalid packet size";
	case -ENOSPC:
		return "would overcommit USB bandwidth";
	case -ESHUTDOWN:
		return "device shut down";
	case -EPERM:
		return "reject flag set";
	case -EHOSTUNREACH:
		return "device suspended";
	default:
		snprintf(unkmsg, sizeof(unkmsg), "unknown error %d", rc);
		return unkmsg;
	}
}

178 179 180 181 182 183 184 185 186 187
/* 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:
188
		return "unlinked (sync)";
189 190 191
	case -EINPROGRESS:
		return "pending";
	case -EPROTO:
192
		return "bit stuffing error, timeout, or unknown USB error";
193
	case -EILSEQ:
194
		return "CRC mismatch, timeout, or unknown USB error";
195
	case -ETIME:
196
		return "timed out";
197 198 199 200 201 202 203 204
	case -EPIPE:
		return "endpoint stalled";
	case -ECOMM:
		return "IN buffer overrun";
	case -ENOSR:
		return "OUT buffer underrun";
	case -EOVERFLOW:
		return "too much data";
205 206
	case -EREMOTEIO:
		return "short packet detected";
207 208
	case -ENODEV:
		return "device removed";
209 210 211 212
	case -EXDEV:
		return "partial isochronous transfer";
	case -EINVAL:
		return "invalid argument";
213 214
	case -ECONNRESET:
		return "unlinked (async)";
215
	case -ESHUTDOWN:
216
		return "device shut down";
217
	default:
218
		snprintf(unkmsg, sizeof(unkmsg), "unknown status %d", status);
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
		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,
243
			    struct urb *urb)
244 245 246
{
#ifdef CONFIG_GIGASET_DEBUG
	int i;
247
	gig_dbg(level, "%s urb(0x%08lx)->{", tag, (unsigned long) urb);
248
	if (urb) {
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
		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);
274
		for (i = 0; i < urb->number_of_packets; i++) {
275 276
			struct usb_iso_packet_descriptor *pifd
				= &urb->iso_frame_desc[i];
277 278 279 280 281
			gig_dbg(level,
				"    {offset=%u, length=%u, actual_length=%u, "
				"status=%u}",
				pifd->offset, pifd->length, pifd->actual_length,
				pifd->status);
282 283
		}
	}
284
	gig_dbg(level, "}}");
285 286 287 288 289
#endif
}

/* read/set modem control bits etc. (m10x only) */
static int gigaset_set_modem_ctrl(struct cardstate *cs, unsigned old_state,
290
				  unsigned new_state)
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
{
	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.
309
 * cs->lock must not be held.
310 311 312 313 314 315 316
 * argument:
 *	B channel control structure
 */
static inline void error_hangup(struct bc_state *bcs)
{
	struct cardstate *cs = bcs->cs;

317 318
	gig_dbg(DEBUG_ANY, "%s: scheduling HUP for channel %d",
		__func__, bcs->channel);
319

320 321
	if (!gigaset_add_event(cs, &bcs->at_state, EV_HUP, NULL, 0, NULL))
		dev_err(cs->dev, "event queue full\n");
322 323 324 325 326 327

	gigaset_schedule_event(cs);
}

/* error_reset
 * reset Gigaset device because of an unrecoverable error
328
 * This function may be called from any context, and takes care of
329
 * scheduling the necessary actions for execution outside of interrupt context.
330
 * cs->lock must not be held.
331 332 333 334 335
 * argument:
 *	controller state structure
 */
static inline void error_reset(struct cardstate *cs)
{
336 337 338
	/* close AT command channel to recover (ignore errors) */
	req_submit(cs->bcs, HD_CLOSE_ATCHANNEL, 0, BAS_TIMEOUT);

339
	//FIXME try to recover without bothering the user
340 341
	dev_err(cs->dev,
	    "unrecoverable error - please disconnect Gigaset base to reset\n");
342 343 344 345 346
}

/* check_pending
 * check for completion of pending control request
 * parameter:
347
 *	ucs	hardware specific controller state structure
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
 */
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:
388 389 390
		dev_warn(&ucs->interface->dev,
			 "unknown pending request 0x%02x cleared\n",
			 ucs->pending);
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
		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;
408
	struct bas_cardstate *ucs = cs->hw.bas;
409
	int rc;
410 411

	if (!ucs->rcvbuf_size) {
412
		gig_dbg(DEBUG_USBREQ, "%s: no receive in progress", __func__);
413 414 415
		return;
	}

416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
	if (ucs->retry_cmd_in++ < BAS_RETRY) {
		dev_notice(cs->dev, "control read: timeout, retry %d\n",
			   ucs->retry_cmd_in);
		rc = atread_submit(cs, BAS_TIMEOUT);
		if (rc >= 0 || rc == -ENODEV)
			/* resubmitted or disconnected */
			/* - bypass regular exit block */
			return;
	} else {
		dev_err(cs->dev,
			"control read: timeout, giving up after %d tries\n",
			ucs->retry_cmd_in);
	}
	kfree(ucs->rcvbuf);
	ucs->rcvbuf = NULL;
	ucs->rcvbuf_size = 0;
	error_reset(cs);
433 434
}

435 436 437 438 439 440 441
/* set/clear bits in base connection state, return previous state
 */
inline static int update_basstate(struct bas_cardstate *ucs,
				  int set, int clear)
{
	unsigned long flags;
	int state;
442

443 444 445 446 447 448
	spin_lock_irqsave(&ucs->lock, flags);
	state = atomic_read(&ucs->basstate);
	atomic_set(&ucs->basstate, (state & ~clear) | set);
	spin_unlock_irqrestore(&ucs->lock, flags);
	return state;
}
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 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 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
/* 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)
{
	struct inbuf_t *inbuf = urb->context;
	struct cardstate *cs = inbuf->cs;
	struct bas_cardstate *ucs = cs->hw.bas;
	int have_data = 0;
	unsigned numbytes;
	int rc;

	update_basstate(ucs, 0, BS_ATRDPEND);

	if (!ucs->rcvbuf_size) {
		dev_warn(cs->dev, "%s: no receive in progress\n", __func__);
		return;
	}

	del_timer(&ucs->timer_cmd_in);

	switch (urb->status) {
	case 0:				/* normal completion */
		numbytes = urb->actual_length;
		if (unlikely(numbytes != ucs->rcvbuf_size)) {
			dev_warn(cs->dev,
			       "control read: received %d chars, expected %d\n",
				 numbytes, ucs->rcvbuf_size);
			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;
			rc = atread_submit(cs, BAS_TIMEOUT);
			if (rc >= 0 || rc == -ENODEV)
				/* resubmitted or disconnected */
				/* - bypass regular exit block */
				return;
			error_reset(cs);
		}
		break;

	case -ENOENT:			/* cancelled */
	case -ECONNRESET:		/* cancelled (async) */
	case -EINPROGRESS:		/* pending */
	case -ENODEV:			/* device removed */
	case -ESHUTDOWN:		/* device shut down */
		/* no action necessary */
		gig_dbg(DEBUG_USBREQ, "%s: %s",
			__func__, get_usb_statmsg(urb->status));
		break;

	default:			/* severe trouble */
		dev_warn(cs->dev, "control read: %s\n",
			 get_usb_statmsg(urb->status));
		if (ucs->retry_cmd_in++ < BAS_RETRY) {
			dev_notice(cs->dev, "control read: retry %d\n",
				   ucs->retry_cmd_in);
			rc = atread_submit(cs, BAS_TIMEOUT);
			if (rc >= 0 || rc == -ENODEV)
				/* resubmitted or disconnected */
				/* - bypass regular exit block */
				return;
		} else {
			dev_err(cs->dev,
				"control read: giving up after %d tries\n",
				ucs->retry_cmd_in);
		}
		error_reset(cs);
	}

	kfree(ucs->rcvbuf);
	ucs->rcvbuf = NULL;
	ucs->rcvbuf_size = 0;
	if (have_data) {
		gig_dbg(DEBUG_INTR, "%s-->BH", __func__);
		gigaset_schedule_event(cs);
	}
}

540
/* atread_submit
541
 * submit an HD_READ_ATMESSAGE command URB and optionally start a timeout
542 543 544 545 546 547 548 549 550 551
 * parameters:
 *	cs	controller state structure
 *	timeout	timeout in 1/10 sec., 0: none
 * return value:
 *	0 on success
 *	-EBUSY if another request is pending
 *	any URB submission error code
 */
static int atread_submit(struct cardstate *cs, int timeout)
{
552
	struct bas_cardstate *ucs = cs->hw.bas;
553 554
	int ret;

555 556
	gig_dbg(DEBUG_USBREQ, "-------> HD_READ_ATMESSAGE (%d)",
		ucs->rcvbuf_size);
557

558
	if (update_basstate(ucs, BS_ATRDPEND, 0) & BS_ATRDPEND) {
559 560
		dev_err(cs->dev,
			"could not submit HD_READ_ATMESSAGE: URB busy\n");
561 562 563 564 565 566 567 568 569
		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,
570 571 572 573
			     usb_rcvctrlpipe(ucs->udev, 0),
			     (unsigned char*) & ucs->dr_cmd_in,
			     ucs->rcvbuf, ucs->rcvbuf_size,
			     read_ctrl_callback, cs->inbuf);
574 575

	if ((ret = usb_submit_urb(ucs->urb_cmd_in, SLAB_ATOMIC)) != 0) {
576
		update_basstate(ucs, 0, BS_ATRDPEND);
577
		dev_err(cs->dev, "could not submit HD_READ_ATMESSAGE: %s\n",
578
			get_usb_rcmsg(ret));
579 580 581 582
		return ret;
	}

	if (timeout > 0) {
583
		gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
		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;
}

/* 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)
{
601 602
	struct cardstate *cs = urb->context;
	struct bas_cardstate *ucs = cs->hw.bas;
603 604
	struct bc_state *bcs;
	unsigned long flags;
605
	int rc;
606 607 608 609 610 611
	unsigned l;
	int channel;

	switch (urb->status) {
	case 0:			/* success */
		break;
612 613
	case -ENOENT:			/* cancelled */
	case -ECONNRESET:		/* cancelled (async) */
614 615
	case -EINPROGRESS:		/* pending */
		/* ignore silently */
616 617
		gig_dbg(DEBUG_USBREQ, "%s: %s",
			__func__, get_usb_statmsg(urb->status));
618
		return;
619 620 621 622 623
	case -ENODEV:			/* device removed */
	case -ESHUTDOWN:		/* device shut down */
		//FIXME use this as disconnect indicator?
		gig_dbg(DEBUG_USBREQ, "%s: device disconnected", __func__);
		return;
624
	default:		/* severe trouble */
625 626
		dev_warn(cs->dev, "interrupt read: %s\n",
			 get_usb_statmsg(urb->status));
627 628 629 630
		//FIXME corrective action? resubmission always ok?
		goto resubmit;
	}

631 632 633 634 635 636 637
	/* drop incomplete packets even if the missing bytes wouldn't matter */
	if (unlikely(urb->actual_length < 3)) {
		dev_warn(cs->dev, "incomplete interrupt packet (%d bytes)\n",
			 urb->actual_length);
		goto resubmit;
	}

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

641 642 643
	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]);
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688

	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,
689 690 691 692 693
			   &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));
694 695 696 697
		break;

	case HD_RECEIVEATDATA_ACK:	/* AT response ready to be received */
		if (!l) {
698 699
			dev_warn(cs->dev,
				"HD_RECEIVEATDATA_ACK with length 0 ignored\n");
700 701 702 703
			break;
		}
		spin_lock_irqsave(&cs->lock, flags);
		if (ucs->rcvbuf_size) {
704
			/* throw away previous buffer - we have no queue */
705
			dev_err(cs->dev,
706 707 708 709
				"receive AT data overrun, %d bytes lost\n",
				ucs->rcvbuf_size);
			kfree(ucs->rcvbuf);
			ucs->rcvbuf_size = 0;
710 711 712
		}
		if ((ucs->rcvbuf = kmalloc(l, GFP_ATOMIC)) == NULL) {
			spin_unlock_irqrestore(&cs->lock, flags);
713 714
			dev_err(cs->dev, "out of memory receiving AT data\n");
			error_reset(cs);
715 716 717 718
			break;
		}
		ucs->rcvbuf_size = l;
		ucs->retry_cmd_in = 0;
719
		if ((rc = atread_submit(cs, BAS_TIMEOUT)) < 0) {
720 721 722
			kfree(ucs->rcvbuf);
			ucs->rcvbuf = NULL;
			ucs->rcvbuf_size = 0;
723
			if (rc != -ENODEV) {
724
				//FIXME corrective action?
725
				spin_unlock_irqrestore(&cs->lock, flags);
726
				error_reset(cs);
727 728
				break;
			}
729 730 731 732 733
		}
		spin_unlock_irqrestore(&cs->lock, flags);
		break;

	case HD_RESET_INTERRUPT_PIPE_ACK:
734
		gig_dbg(DEBUG_USBREQ, "HD_RESET_INTERRUPT_PIPE_ACK");
735 736 737
		break;

	case HD_SUSPEND_END:
738
		gig_dbg(DEBUG_USBREQ, "HD_SUSPEND_END");
739 740 741
		break;

	default:
742 743 744
		dev_warn(cs->dev,
			 "unknown Gigaset signal 0x%02x (%u) ignored\n",
			 (int) ucs->int_in_buf[0], l);
745 746 747 748 749
	}

	check_pending(ucs);

resubmit:
750 751
	rc = usb_submit_urb(urb, SLAB_ATOMIC);
	if (unlikely(rc != 0 && rc != -ENODEV)) {
752
		dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
753
			get_usb_rcmsg(rc));
754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
		error_reset(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 */
773 774 775 776 777
	if (unlikely(urb->status == -ENOENT ||
		     urb->status == -ECONNRESET ||
		     urb->status == -EINPROGRESS ||
		     urb->status == -ENODEV ||
		     urb->status == -ESHUTDOWN)) {
778 779
		gig_dbg(DEBUG_ISO, "%s: %s",
			__func__, get_usb_statmsg(urb->status));
780 781 782
		return;
	}

783
	bcs = urb->context;
784 785 786 787 788 789 790 791 792 793 794 795 796
	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 &&
797 798
				     urb->iso_frame_desc[i].status !=
								-EINPROGRESS))
799 800 801 802 803
				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))) {
804 805
			/* urb->dev is clobbered by USB subsystem */
			urb->dev = bcs->cs->hw.bas->udev;
806 807
			urb->transfer_flags = URB_ISO_ASAP;
			urb->number_of_packets = BAS_NUMFRAMES;
808 809
			gig_dbg(DEBUG_ISO, "%s: isoc read overrun/resubmit",
				__func__);
810
			rc = usb_submit_urb(urb, SLAB_ATOMIC);
811
			if (unlikely(rc != 0 && rc != -ENODEV)) {
812 813
				dev_err(bcs->cs->dev,
					"could not resubmit isochronous read "
814
					"URB: %s\n", get_usb_rcmsg(rc));
815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836
				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 */
837 838 839 840 841
	if (unlikely(urb->status == -ENOENT ||
		     urb->status == -ECONNRESET ||
		     urb->status == -EINPROGRESS ||
		     urb->status == -ENODEV ||
		     urb->status == -ESHUTDOWN)) {
842 843
		gig_dbg(DEBUG_ISO, "%s: %s",
			__func__, get_usb_statmsg(urb->status));
844 845 846 847
		return;
	}

	/* pass URB context to tasklet */
848
	ucx = urb->context;
849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867
	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)
{
868
	struct bas_bc_state *ubc = bcs->hw.bas;
869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902
	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) {
			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);
903
		if ((rc = usb_submit_urb(urb, SLAB_ATOMIC)) != 0)
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938
			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) {
			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);
939
		if (rc != 0)
940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964
			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]);
965
		gig_dbg(DEBUG_ISO,
966 967
			"%s: isoc input URB %d unlinked, result = %s",
			__func__, k, get_usb_rcmsg(rc));
968 969 970 971
	}

	for (k = 0; k < BAS_OUTURBS; ++k) {
		rc = usb_unlink_urb(ubc->isoouturbs[k].urb);
972
		gig_dbg(DEBUG_ISO,
973 974
			"%s: isoc output URB %d unlinked, result = %s",
			__func__, k, get_usb_rcmsg(rc));
975 976 977 978 979 980 981 982 983
	}
}

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

/* submit_iso_write_urb
 * fill and submit the next isochronous write URB
 * parameters:
984
 *	ucx	context structure containing URB
985 986 987 988 989 990 991
 * 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)
{
992 993
	struct urb *urb = ucx->urb;
	struct bas_bc_state *ubc = ucx->bcs->hw.bas;
994 995 996
	struct usb_iso_packet_descriptor *ifd;
	int corrbytes, nframe, rc;

997 998
	/* urb->dev is clobbered by USB subsystem */
	urb->dev = ucx->bcs->cs->hw.bas->udev;
999 1000 1001 1002 1003 1004 1005 1006 1007 1008
	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) {
1009 1010
			gig_dbg(DEBUG_ISO, "%s: corrbytes=%d",
				__func__, corrbytes);
1011 1012 1013 1014 1015 1016 1017 1018 1019
			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 */
1020 1021
		ifd->offset = gigaset_isowbuf_getbytes(ubc->isooutbuf,
						       ifd->length);
1022 1023
		if (ifd->offset < 0) {
			if (ifd->offset == -EBUSY) {
1024 1025 1026 1027 1028
				gig_dbg(DEBUG_ISO,
					"%s: buffer busy at frame %d",
					__func__, nframe);
				/* tasklet will be restarted from
				   gigaset_send_skb() */
1029
			} else {
1030 1031 1032
				dev_err(ucx->bcs->cs->dev,
					"%s: buffer error %d at frame %d\n",
					__func__, ifd->offset, nframe);
1033 1034 1035 1036 1037 1038 1039 1040
				return ifd->offset;
			}
			break;
		}
		ucx->limit = atomic_read(&ubc->isooutbuf->nextread);
		ifd->status = 0;
		ifd->actual_length = 0;
	}
1041 1042 1043
	if (unlikely(nframe == 0))
		return 0;	/* no data to send */
	urb->number_of_packets = nframe;
1044

1045 1046 1047 1048 1049 1050
	rc = usb_submit_urb(urb, SLAB_ATOMIC);
	if (unlikely(rc)) {
		if (rc == -ENODEV)
			/* device removed - give up silently */
			gig_dbg(DEBUG_ISO, "%s: disconnected", __func__);
		else
1051 1052
			dev_err(ucx->bcs->cs->dev,
				"could not submit isochronous write URB: %s\n",
1053 1054
				get_usb_rcmsg(rc));
		return rc;
1055
	}
1056
	++ubc->numsub;
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
	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)
{
1068 1069 1070
	struct bc_state *bcs = (struct bc_state *) data;
	struct bas_bc_state *ubc = bcs->hw.bas;
	struct cardstate *cs = bcs->cs;
1071 1072 1073 1074 1075 1076 1077 1078
	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;
1079
	int rc;
1080 1081 1082 1083

	/* loop while completed URBs arrive in time */
	for (;;) {
		if (unlikely(!(atomic_read(&ubc->running)))) {
1084
			gig_dbg(DEBUG_ISO, "%s: not running", __func__);
1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
			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) {
1096
			dev_err(cs->dev, "isochronous write buffer underrun\n");
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
			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) {
1109 1110
			rc = submit_iso_write_urb(next);
			if (unlikely(rc <= 0 && rc != -ENODEV)) {
1111 1112 1113 1114 1115 1116 1117 1118 1119
				/* 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 */
1120 1121
					dev_err(cs->dev,
					      "losing isochronous write URB\n");
1122 1123 1124 1125 1126 1127 1128 1129
					error_hangup(bcs);
				}
			}
		}

		/* process completed URB */
		urb = done->urb;
		switch (urb->status) {
1130 1131 1132 1133
		case -EXDEV:			/* partial completion */
			gig_dbg(DEBUG_ISO, "%s: URB partially completed",
				__func__);
			/* fall through - what's the difference anyway? */
1134
		case 0:				/* normal completion */
1135 1136 1137
			/* inspect individual frames
			 * assumptions (for lack of documentation):
			 * - actual_length bytes of first frame in error are
1138
			 *   successfully sent
1139 1140
			 * - all following frames are not sent at all
			 */
1141
			offset = done->limit;	/* default (no error) */
1142 1143 1144 1145
			for (i = 0; i < BAS_NUMFRAMES; i++) {
				ifd = &urb->iso_frame_desc[i];
				if (ifd->status ||
				    ifd->actual_length != ifd->length) {
1146 1147 1148
					dev_warn(cs->dev,
					     "isochronous write: frame %d: %s, "
					     "only %d of %d bytes sent\n",
1149 1150 1151
					     i, get_usb_statmsg(ifd->status),
					     ifd->actual_length, ifd->length);
					offset = (ifd->offset +
1152 1153
						  ifd->actual_length)
						 % BAS_OUTBUFSIZE;
1154 1155 1156 1157 1158 1159 1160 1161 1162
					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) {
1163 1164 1165
					dev_warn(cs->dev,
					     "isochronous write: frame %d: %s, "
					     "%d of %d bytes sent\n",
1166 1167 1168
					     i, get_usb_statmsg(ifd->status),
					     ifd->actual_length, ifd->length);
					offset = (ifd->offset +
1169 1170
						  ifd->actual_length)
						 % BAS_OUTBUFSIZE;
1171 1172 1173 1174 1175
					break;
				}
			}
#endif
			break;
1176
		case -EPIPE:			/* stall - probably underrun */
1177
			dev_err(cs->dev, "isochronous write stalled\n");
1178 1179 1180
			error_hangup(bcs);
			break;
		default:			/* severe trouble */
1181 1182
			dev_warn(cs->dev, "isochronous write: %s\n",
				 get_usb_statmsg(urb->status));
1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195
		}

		/* 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 */
1196 1197
			rc = submit_iso_write_urb(next);
			if (unlikely(rc <= 0 && rc != -ENODEV)) {
1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210
				/* 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);
1211 1212
			gig_dbg(DEBUG_ISO, "%s: skb requeued, qlen=%d",
				__func__, skb_queue_len(&bcs->squeue));
1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231
			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)
{
1232 1233 1234
	struct bc_state *bcs = (struct bc_state *) data;
	struct bas_bc_state *ubc = bcs->hw.bas;
	struct cardstate *cs = bcs->cs;
1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249
	struct urb *urb;
	char *rcvbuf;
	unsigned long flags;
	int totleft, numbytes, offset, frame, rc;

	/* loop while more completed URBs arrive in the meantime */
	for (;;) {
		/* 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)) {
1250 1251 1252 1253 1254
			dev_warn(cs->dev,
				 "isochronous read overrun, "
				 "dropped URB with status: %s, %d bytes lost\n",
				 get_usb_statmsg(ubc->loststatus),
				 ubc->isoinlost);
1255 1256 1257 1258 1259
			ubc->loststatus = -EINPROGRESS;
		}
		spin_unlock_irqrestore(&ubc->isoinlock, flags);

		if (unlikely(!(atomic_read(&ubc->running)))) {
1260 1261 1262 1263
			gig_dbg(DEBUG_ISO,
				"%s: channel not running, "
				"dropped URB with status: %s",
				__func__, get_usb_statmsg(urb->status));
1264 1265 1266 1267 1268 1269
			return;
		}

		switch (urb->status) {
		case 0:				/* normal completion */
			break;
1270 1271
		case -EXDEV:			/* inspect individual frames
						   (we do that anyway) */
1272 1273
			gig_dbg(DEBUG_ISO, "%s: URB partially completed",
				__func__);
1274 1275 1276
			break;
		case -ENOENT:
		case -ECONNRESET:
1277 1278 1279
		case -EINPROGRESS:
			gig_dbg(DEBUG_ISO, "%s: %s",
				__func__, get_usb_statmsg(urb->status));
1280 1281
			continue;		/* -> skip */
		case -EPIPE:
1282
			dev_err(cs->dev, "isochronous read stalled\n");
1283 1284 1285
			error_hangup(bcs);
			continue;		/* -> skip */
		default:			/* severe trouble */
1286 1287
			dev_warn(cs->dev, "isochronous read: %s\n",
				 get_usb_statmsg(urb->status));
1288 1289 1290 1291 1292 1293 1294
			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)) {
1295 1296 1297 1298 1299
				dev_warn(cs->dev,
					 "isochronous read: frame %d: %s\n",
					 frame,
					 get_usb_statmsg(
					    urb->iso_frame_desc[frame].status));
1300 1301 1302 1303
				break;
			}
			numbytes = urb->iso_frame_desc[frame].actual_length;
			if (unlikely(numbytes > BAS_MAXFRAME)) {
1304 1305 1306 1307
				dev_warn(cs->dev,
					 "isochronous read: frame %d: "
					 "numbytes (%d) > BAS_MAXFRAME\n",
					 frame, numbytes);
1308 1309 1310
				break;
			}
			if (unlikely(numbytes > totleft)) {
1311 1312 1313 1314
				dev_warn(cs->dev,
					 "isochronous read: frame %d: "
					 "numbytes (%d) > totleft (%d)\n",
					 frame, numbytes, totleft);
1315 1316 1317 1318
				break;
			}
			offset = urb->iso_frame_desc[frame].offset;
			if (unlikely(offset + numbytes > BAS_INBUFSIZE)) {
1319 1320 1321 1322 1323
				dev_warn(cs->dev,
					 "isochronous read: frame %d: "
					 "offset (%d) + numbytes (%d) "
					 "> BAS_INBUFSIZE\n",
					 frame, offset, numbytes);
1324 1325 1326 1327 1328 1329
				break;
			}
			gigaset_isoc_receive(rcvbuf + offset, numbytes, bcs);
			totleft -= numbytes;
		}
		if (unlikely(totleft > 0))
1330 1331 1332
			dev_warn(cs->dev,
				 "isochronous read: %d data bytes missing\n",
				 totleft);
1333 1334 1335 1336 1337 1338 1339

	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;
		}
1340 1341
		/* urb->dev is clobbered by USB subsystem */
		urb->dev = bcs->cs->hw.bas->udev;
1342 1343
		urb->transfer_flags = URB_ISO_ASAP;
		urb->number_of_packets = BAS_NUMFRAMES;
1344 1345
		rc = usb_submit_urb(urb, SLAB_ATOMIC);
		if (unlikely(rc != 0 && rc != -ENODEV)) {
1346 1347
			dev_err(cs->dev,
				"could not resubmit isochronous read URB: %s\n",
1348
				get_usb_rcmsg(rc));
1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365
			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;
1366
	struct bas_cardstate *ucs = bcs->cs->hw.bas;
1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378
	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 */
1379
		gig_dbg(DEBUG_USBREQ, "%s: no request pending", __func__);
1380 1381 1382
		break;

	case HD_OPEN_ATCHANNEL:
1383
		dev_err(bcs->cs->dev, "timeout opening AT channel\n");
1384 1385 1386 1387 1388
		error_reset(bcs->cs);
		break;

	case HD_OPEN_B2CHANNEL:
	case HD_OPEN_B1CHANNEL:
1389 1390
		dev_err(bcs->cs->dev, "timeout opening channel %d\n",
			bcs->channel + 1);
1391 1392 1393 1394
		error_hangup(bcs);
		break;

	case HD_CLOSE_ATCHANNEL:
1395
		dev_err(bcs->cs->dev, "timeout closing AT channel\n");
1396 1397 1398 1399
		break;

	case HD_CLOSE_B2CHANNEL:
	case HD_CLOSE_B1CHANNEL:
1400 1401
		dev_err(bcs->cs->dev, "timeout closing channel %d\n",
			bcs->channel + 1);
1402
		error_reset(bcs->cs);
1403 1404 1405
		break;

	default:
1406 1407
		dev_warn(bcs->cs->dev, "request 0x%02x timed out, clearing\n",
			 pending);
1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419
	}
}

/* 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)
{
1420
	struct bas_cardstate *ucs = urb->context;
1421
	int rc;
1422 1423
	unsigned long flags;

1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444
	/* check status */
	switch (urb->status) {
	case 0:					/* normal completion */
		spin_lock_irqsave(&ucs->lock, flags);
		switch (ucs->pending) {
		case HD_DEVICE_INIT_ACK:	/* no reply expected */
			del_timer(&ucs->timer_ctrl);
			ucs->pending = 0;
			break;
		}
		spin_unlock_irqrestore(&ucs->lock, flags);
		return;

	case -ENOENT:			/* cancelled */
	case -ECONNRESET:		/* cancelled (async) */
	case -EINPROGRESS:		/* pending */
	case -ENODEV:			/* device removed */
	case -ESHUTDOWN:		/* device shut down */
		/* ignore silently */
		gig_dbg(DEBUG_USBREQ, "%s: %s",
			__func__, get_usb_statmsg(urb->status));
1445
		break;
1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469

	default:				/* any failure */
		if (++ucs->retry_ctrl > BAS_RETRY) {
			dev_err(&ucs->interface->dev,
				"control request 0x%02x failed: %s\n",
				ucs->dr_ctrl.bRequest,
				get_usb_statmsg(urb->status));
			break;		/* give up */
		}
		dev_notice(&ucs->interface->dev,
			   "control request 0x%02x: %s, retry %d\n",
			   ucs->dr_ctrl.bRequest, get_usb_statmsg(urb->status),
			   ucs->retry_ctrl);
		/* urb->dev is clobbered by USB subsystem */
		urb->dev = ucs->udev;
		rc = usb_submit_urb(urb, SLAB_ATOMIC);
		if (unlikely(rc)) {
			dev_err(&ucs->interface->dev,
				"could not resubmit request 0x%02x: %s\n",
				ucs->dr_ctrl.bRequest, get_usb_rcmsg(rc));
			break;
		}
		/* resubmitted */
		return;
1470
	}
1471 1472 1473 1474 1475

	/* failed, clear pending request */
	spin_lock_irqsave(&ucs->lock, flags);
	del_timer(&ucs->timer_ctrl);
	ucs->pending = 0;
1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493
	spin_unlock_irqrestore(&ucs->lock, flags);
}

/* req_submit
 * submit a control output request without message buffer to the Gigaset base
 * and optionally start a timeout
 * parameters:
 *	bcs	B channel control structure
 *	req	control request code (HD_*)
 *	val	control request parameter value (set to 0 if unused)
 *	timeout	timeout in seconds (0: no timeout)
 * return value:
 *	0 on success
 *	-EBUSY if another request is pending
 *	any URB submission error code
 */
static int req_submit(struct bc_state *bcs, int req, int val, int timeout)
{
1494
	struct bas_cardstate *ucs = bcs->cs->hw.bas;
1495 1496 1497
	int ret;
	unsigned long flags;

1498
	gig_dbg(DEBUG_USBREQ, "-------> 0x%02x (%d)", req, val);
1499 1500 1501 1502

	spin_lock_irqsave(&ucs->lock, flags);
	if (ucs->pending) {
		spin_unlock_irqrestore(&ucs->lock, flags);
1503 1504 1505 1506
		dev_err(bcs->cs->dev,
			"submission of request 0x%02x failed: "
			"request 0x%02x still pending\n",
			req, ucs->pending);
1507 1508 1509 1510 1511 1512 1513 1514 1515
		return -EBUSY;
	}

	ucs->dr_ctrl.bRequestType = OUT_VENDOR_REQ;
	ucs->dr_ctrl.bRequest = req;
	ucs->dr_ctrl.wValue = cpu_to_le16(val);
	ucs->dr_ctrl.wIndex = 0;
	ucs->dr_ctrl.wLength = 0;
	usb_fill_control_urb(ucs->urb_ctrl, ucs->udev,
1516 1517 1518
			     usb_sndctrlpipe(ucs->udev, 0),
			     (unsigned char*) &ucs->dr_ctrl, NULL, 0,
			     write_ctrl_callback, ucs);
1519 1520 1521
	ucs->retry_ctrl = 0;
	ret = usb_submit_urb(ucs->urb_ctrl, SLAB_ATOMIC);
	if (unlikely(ret)) {
1522
		dev_err(bcs->cs->dev, "could not submit request 0x%02x: %s\n",
1523
			req, get_usb_rcmsg(ret));
1524 1525 1526 1527 1528 1529
		spin_unlock_irqrestore(&ucs->lock, flags);
		return ret;
	}
	ucs->pending = req;

	if (timeout > 0) {
1530
		gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551
		ucs->timer_ctrl.expires = jiffies + timeout * HZ / 10;
		ucs->timer_ctrl.data = (unsigned long) bcs;
		ucs->timer_ctrl.function = req_timeout;
		add_timer(&ucs->timer_ctrl);
	}

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

/* gigaset_init_bchannel
 * called by common.c to connect a B channel
 * initialize isochronous I/O and tell the Gigaset base to open the channel
 * argument:
 *	B channel control structure
 * return value:
 *	0 on success, error code < 0 on error
 */
static int gigaset_init_bchannel(struct bc_state *bcs)
{
	int req, ret;
1552 1553 1554 1555 1556 1557 1558 1559
	unsigned long flags;

	spin_lock_irqsave(&bcs->cs->lock, flags);
	if (unlikely(!bcs->cs->connected)) {
		gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
		spin_unlock_irqrestore(&bcs->cs->lock, flags);
		return -ENODEV;
	}
1560 1561

	if ((ret = starturbs(bcs)) < 0) {
1562
		dev_err(bcs->cs->dev,
1563 1564 1565 1566 1567 1568
			"could not start isochronous I/O for channel B%d: %s\n",
			bcs->channel + 1,
			ret == -EFAULT ? "null URB" : get_usb_rcmsg(ret));
		if (ret != -ENODEV)
			error_hangup(bcs);
		spin_unlock_irqrestore(&bcs->cs->lock, flags);
1569 1570 1571 1572 1573
		return ret;
	}

	req = bcs->channel ? HD_OPEN_B2CHANNEL : HD_OPEN_B1CHANNEL;
	if ((ret = req_submit(bcs, req, 0, BAS_TIMEOUT)) < 0) {
1574 1575
		dev_err(bcs->cs->dev, "could not open channel B%d\n",
			bcs->channel + 1);
1576
		stopurbs(bcs->hw.bas);
1577 1578
		if (ret != -ENODEV)
			error_hangup(bcs);
1579
	}
1580 1581

	spin_unlock_irqrestore(&bcs->cs->lock, flags);
1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597
	return ret;
}

/* gigaset_close_bchannel
 * called by common.c to disconnect a B channel
 * tell the Gigaset base to close the channel
 * stopping isochronous I/O and LL notification will be done when the
 * acknowledgement for the close arrives
 * argument:
 *	B channel control structure
 * return value:
 *	0 on success, error code < 0 on error
 */
static int gigaset_close_bchannel(struct bc_state *bcs)
{
	int req, ret;
1598 1599 1600 1601 1602 1603 1604 1605
	unsigned long flags;

	spin_lock_irqsave(&bcs->cs->lock, flags);
	if (unlikely(!bcs->cs->connected)) {
		spin_unlock_irqrestore(&bcs->cs->lock, flags);
		gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
		return -ENODEV;
	}
1606 1607 1608 1609

	if (!(atomic_read(&bcs->cs->hw.bas->basstate) &
	      (bcs->channel ? BS_B2OPEN : BS_B1OPEN))) {
		/* channel not running: just signal common.c */
1610
		spin_unlock_irqrestore(&bcs->cs->lock, flags);
1611 1612 1613 1614
		gigaset_bchannel_down(bcs);
		return 0;
	}

1615
	/* channel running: tell device to close it */
1616 1617
	req = bcs->channel ? HD_CLOSE_B2CHANNEL : HD_CLOSE_B1CHANNEL;
	if ((ret = req_submit(bcs, req, 0, BAS_TIMEOUT)) < 0)
1618 1619 1620 1621
		dev_err(bcs->cs->dev, "closing channel B%d failed\n",
			bcs->channel + 1);

	spin_unlock_irqrestore(&bcs->cs->lock, flags);
1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635
	return ret;
}

/* Device Operations */
/* ================= */

/* complete_cb
 * unqueue first command buffer from queue, waking any sleepers
 * must be called with cs->cmdlock held
 * parameter:
 *	cs	controller state structure
 */
static void complete_cb(struct cardstate *cs)
{
1636
	struct cmdbuf_t *cb = cs->cmdbuf;
1637 1638 1639

	/* unqueue completed buffer */
	cs->cmdbytes -= cs->curlen;
1640 1641 1642
	gig_dbg(DEBUG_TRANSCMD|DEBUG_LOCKCMD,
		"write_command: sent %u bytes, %u left",
		cs->curlen, cs->cmdbytes);
1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665
	if ((cs->cmdbuf = cb->next) != NULL) {
		cs->cmdbuf->prev = NULL;
		cs->curlen = cs->cmdbuf->len;
	} else {
		cs->lastcmdbuf = NULL;
		cs->curlen = 0;
	}

	if (cb->wake_tasklet)
		tasklet_schedule(cb->wake_tasklet);

	kfree(cb);
}

/* write_command_callback
 * USB completion handler for AT command transmission
 * called by the USB subsystem in interrupt context
 * parameter:
 *	urb	USB request block of completed request
 *		urb->context = controller state structure
 */
static void write_command_callback(struct urb *urb, struct pt_regs *regs)
{
1666 1667
	struct cardstate *cs = urb->context;
	struct bas_cardstate *ucs = cs->hw.bas;
1668 1669
	unsigned long flags;

1670 1671
	update_basstate(ucs, 0, BS_ATWRPEND);

1672 1673 1674 1675
	/* check status */
	switch (urb->status) {
	case 0:					/* normal completion */
		break;
1676 1677
	case -ENOENT:			/* cancelled */
	case -ECONNRESET:		/* cancelled (async) */
1678
	case -EINPROGRESS:		/* pending */
1679 1680
	case -ENODEV:			/* device removed */
	case -ESHUTDOWN:		/* device shut down */
1681
		/* ignore silently */
1682 1683
		gig_dbg(DEBUG_USBREQ, "%s: %s",
			__func__, get_usb_statmsg(urb->status));
1684 1685 1686
		return;
	default:				/* any failure */
		if (++ucs->retry_cmd_out > BAS_RETRY) {
1687 1688 1689 1690 1691
			dev_warn(cs->dev,
				 "command write: %s, "
				 "giving up after %d retries\n",
				 get_usb_statmsg(urb->status),
				 ucs->retry_cmd_out);
1692 1693 1694
			break;
		}
		if (cs->cmdbuf == NULL) {
1695 1696 1697 1698
			dev_warn(cs->dev,
				 "command write: %s, "
				 "cannot retry - cmdbuf gone\n",
				 get_usb_statmsg(urb->status));
1699 1700
			break;
		}
1701 1702
		dev_notice(cs->dev, "command write: %s, retry %d\n",
			   get_usb_statmsg(urb->status), ucs->retry_cmd_out);
1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723
		if (atwrite_submit(cs, cs->cmdbuf->buf, cs->cmdbuf->len) >= 0)
			/* resubmitted - bypass regular exit block */
			return;
		/* command send failed, assume base still waiting */
		update_basstate(ucs, BS_ATREADY, 0);
	}

	spin_lock_irqsave(&cs->cmdlock, flags);
	if (cs->cmdbuf != NULL)
		complete_cb(cs);
	spin_unlock_irqrestore(&cs->cmdlock, flags);
}

/* atrdy_timeout
 * timeout routine for AT command transmission
 * argument:
 *	controller state structure
 */
static void atrdy_timeout(unsigned long data)
{
	struct cardstate *cs = (struct cardstate *) data;
1724
	struct bas_cardstate *ucs = cs->hw.bas;
1725

1726
	dev_warn(cs->dev, "timeout waiting for HD_READY_SEND_ATDATA\n");
1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745

	/* fake the missing signal - what else can I do? */
	update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
	start_cbsend(cs);
}

/* atwrite_submit
 * submit an HD_WRITE_ATMESSAGE command URB
 * parameters:
 *	cs	controller state structure
 *	buf	buffer containing command to send
 *	len	length of command to send
 * return value:
 *	0 on success
 *	-EBUSY if another request is pending
 *	any URB submission error code
 */
static int atwrite_submit(struct cardstate *cs, unsigned char *buf, int len)
{
1746
	struct bas_cardstate *ucs = cs->hw.bas;
1747
	int rc;
1748

1749
	gig_dbg(DEBUG_USBREQ, "-------> HD_WRITE_ATMESSAGE (%d)", len);
1750

1751
	if (update_basstate(ucs, BS_ATWRPEND, 0) & BS_ATWRPEND) {
1752 1753
		dev_err(cs->dev,
			"could not submit HD_WRITE_ATMESSAGE: URB busy\n");
1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765
		return -EBUSY;
	}

	ucs->dr_cmd_out.bRequestType = OUT_VENDOR_REQ;
	ucs->dr_cmd_out.bRequest = HD_WRITE_ATMESSAGE;
	ucs->dr_cmd_out.wValue = 0;
	ucs->dr_cmd_out.wIndex = 0;
	ucs->dr_cmd_out.wLength = cpu_to_le16(len);
	usb_fill_control_urb(ucs->urb_cmd_out, ucs->udev,
			     usb_sndctrlpipe(ucs->udev, 0),
			     (unsigned char*) &ucs->dr_cmd_out, buf, len,
			     write_command_callback, cs);
1766 1767 1768
	rc = usb_submit_urb(ucs->urb_cmd_out, SLAB_ATOMIC);
	if (unlikely(rc)) {
		update_basstate(ucs, 0, BS_ATWRPEND);
1769
		dev_err(cs->dev, "could not submit HD_WRITE_ATMESSAGE: %s\n",
1770 1771
			get_usb_rcmsg(rc));
		return rc;
1772 1773
	}

1774 1775
	/* submitted successfully, start timeout if necessary */
	if (!(update_basstate(ucs, BS_ATTIMER, BS_ATREADY) & BS_ATTIMER)) {
1776 1777
		gig_dbg(DEBUG_OUTPUT, "setting ATREADY timeout of %d/10 secs",
			ATRDY_TIMEOUT);
1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796
		ucs->timer_atrdy.expires = jiffies + ATRDY_TIMEOUT * HZ / 10;
		ucs->timer_atrdy.data = (unsigned long) cs;
		ucs->timer_atrdy.function = atrdy_timeout;
		add_timer(&ucs->timer_atrdy);
	}
	return 0;
}

/* start_cbsend
 * start transmission of AT command queue if necessary
 * parameter:
 *	cs		controller state structure
 * return value:
 *	0 on success
 *	error code < 0 on error
 */
static int start_cbsend(struct cardstate *cs)
{
	struct cmdbuf_t *cb;
1797
	struct bas_cardstate *ucs = cs->hw.bas;
1798 1799 1800 1801 1802 1803
	unsigned long flags;
	int rc;
	int retval = 0;

	/* check if AT channel is open */
	if (!(atomic_read(&ucs->basstate) & BS_ATOPEN)) {
1804
		gig_dbg(DEBUG_TRANSCMD|DEBUG_LOCKCMD, "AT channel not open");
1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843
		rc = req_submit(cs->bcs, HD_OPEN_ATCHANNEL, 0, BAS_TIMEOUT);
		if (rc < 0) {
			/* flush command queue */
			spin_lock_irqsave(&cs->cmdlock, flags);
			while (cs->cmdbuf != NULL)
				complete_cb(cs);
			spin_unlock_irqrestore(&cs->cmdlock, flags);
		}
		return rc;
	}

	/* try to send first command in queue */
	spin_lock_irqsave(&cs->cmdlock, flags);

	while ((cb = cs->cmdbuf) != NULL &&
	       atomic_read(&ucs->basstate) & BS_ATREADY) {
		ucs->retry_cmd_out = 0;
		rc = atwrite_submit(cs, cb->buf, cb->len);
		if (unlikely(rc)) {
			retval = rc;
			complete_cb(cs);
		}
	}

	spin_unlock_irqrestore(&cs->cmdlock, flags);
	return retval;
}

/* gigaset_write_cmd
 * This function is called by the device independent part of the driver
 * to transmit an AT command string to the Gigaset device.
 * It encapsulates the device specific method for transmission over the
 * direct USB connection to the base.
 * The command string is added to the queue of commands to send, and
 * USB transmission is started if necessary.
 * parameters:
 *	cs		controller state structure
 *	buf		command string to send
 *	len		number of bytes to send (max. IF_WRITEBUF)
1844 1845
 *	wake_tasklet	tasklet to run when transmission is completed
 *			(NULL if none)
1846 1847 1848 1849 1850
 * return value:
 *	number of bytes queued on success
 *	error code < 0 on error
 */
static int gigaset_write_cmd(struct cardstate *cs,
1851 1852
			     const unsigned char *buf, int len,
			     struct tasklet_struct *wake_tasklet)
1853 1854 1855 1856 1857 1858
{
	struct cmdbuf_t *cb;
	unsigned long flags;
	int status;

	gigaset_dbg_buffer(atomic_read(&cs->mstate) != MS_LOCKED ?
1859
			     DEBUG_TRANSCMD : DEBUG_LOCKCMD,
1860
			   "CMD Transmit", len, buf);
1861 1862 1863 1864 1865 1866 1867

	if (len <= 0)
		return 0;			/* nothing to do */

	if (len > IF_WRITEBUF)
		len = IF_WRITEBUF;
	if (!(cb = kmalloc(sizeof(struct cmdbuf_t) + len, GFP_ATOMIC))) {
1868
		dev_err(cs->dev, "%s: out of memory\n", __func__);
1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889
		return -ENOMEM;
	}

	memcpy(cb->buf, buf, len);
	cb->len = len;
	cb->offset = 0;
	cb->next = NULL;
	cb->wake_tasklet = wake_tasklet;

	spin_lock_irqsave(&cs->cmdlock, flags);
	cb->prev = cs->lastcmdbuf;
	if (cs->lastcmdbuf)
		cs->lastcmdbuf->next = cb;
	else {
		cs->cmdbuf = cb;
		cs->curlen = len;
	}
	cs->cmdbytes += len;
	cs->lastcmdbuf = cb;
	spin_unlock_irqrestore(&cs->cmdlock, flags);

1890 1891 1892 1893 1894 1895
	spin_lock_irqsave(&cs->lock, flags);
	if (unlikely(!cs->connected)) {
		spin_unlock_irqrestore(&cs->lock, flags);
		gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
		return -ENODEV;
	}
1896
	status = start_cbsend(cs);
1897
	spin_unlock_irqrestore(&cs->lock, flags);
1898 1899 1900 1901 1902
	return status < 0 ? status : len;
}

/* gigaset_write_room
 * tty_driver.write_room interface routine
1903 1904
 * return number of characters the driver will accept to be written via
 * gigaset_write_cmd
1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958
 * parameter:
 *	controller state structure
 * return value:
 *	number of characters
 */
static int gigaset_write_room(struct cardstate *cs)
{
	return IF_WRITEBUF;
}

/* gigaset_chars_in_buffer
 * tty_driver.chars_in_buffer interface routine
 * return number of characters waiting to be sent
 * parameter:
 *	controller state structure
 * return value:
 *	number of characters
 */
static int gigaset_chars_in_buffer(struct cardstate *cs)
{
	unsigned long flags;
	unsigned bytes;

	spin_lock_irqsave(&cs->cmdlock, flags);
	bytes = cs->cmdbytes;
	spin_unlock_irqrestore(&cs->cmdlock, flags);

	return bytes;
}

/* gigaset_brkchars
 * implementation of ioctl(GIGASET_BRKCHARS)
 * parameter:
 *	controller state structure
 * return value:
 *	-EINVAL (unimplemented function)
 */
static int gigaset_brkchars(struct cardstate *cs, const unsigned char buf[6])
{
	return -EINVAL;
}


/* Device Initialization/Shutdown */
/* ============================== */

/* Free hardware dependent part of the B channel structure
 * parameter:
 *	bcs	B channel structure
 * return value:
 *	!=0 on success
 */
static int gigaset_freebcshw(struct bc_state *bcs)
{
1959 1960 1961 1962
	struct bas_bc_state *ubc = bcs->hw.bas;
	int i;

	if (!ubc)
1963 1964
		return 0;

1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984
	/* kill URBs and tasklets before freeing - better safe than sorry */
	atomic_set(&ubc->running, 0);
	for (i = 0; i < BAS_OUTURBS; ++i)
		if (ubc->isoouturbs[i].urb) {
			gig_dbg(DEBUG_INIT, "%s: killing iso out URB %d",
				__func__, i);
			usb_kill_urb(ubc->isoouturbs[i].urb);
			usb_free_urb(ubc->isoouturbs[i].urb);
		}
	for (i = 0; i < BAS_INURBS; ++i)
		if (ubc->isoinurbs[i]) {
			gig_dbg(DEBUG_INIT, "%s: killing iso in URB %d",
				__func__, i);
			usb_kill_urb(ubc->isoinurbs[i]);
			usb_free_urb(ubc->isoinurbs[i]);
		}
	tasklet_kill(&ubc->sent_tasklet);
	tasklet_kill(&ubc->rcvd_tasklet);
	kfree(ubc->isooutbuf);
	kfree(ubc);
1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021
	bcs->hw.bas = NULL;
	return 1;
}

/* Initialize hardware dependent part of the B channel structure
 * parameter:
 *	bcs	B channel structure
 * return value:
 *	!=0 on success
 */
static int gigaset_initbcshw(struct bc_state *bcs)
{
	int i;
	struct bas_bc_state *ubc;

	bcs->hw.bas = ubc = kmalloc(sizeof(struct bas_bc_state), GFP_KERNEL);
	if (!ubc) {
		err("could not allocate bas_bc_state");
		return 0;
	}

	atomic_set(&ubc->running, 0);
	atomic_set(&ubc->corrbytes, 0);
	spin_lock_init(&ubc->isooutlock);
	for (i = 0; i < BAS_OUTURBS; ++i) {
		ubc->isoouturbs[i].urb = NULL;
		ubc->isoouturbs[i].bcs = bcs;
	}
	ubc->isooutdone = ubc->isooutfree = ubc->isooutovfl = NULL;
	ubc->numsub = 0;
	if (!(ubc->isooutbuf = kmalloc(sizeof(struct isowbuf_t), GFP_KERNEL))) {
		err("could not allocate isochronous output buffer");
		kfree(ubc);
		bcs->hw.bas = NULL;
		return 0;
	}
	tasklet_init(&ubc->sent_tasklet,
2022
		     &write_iso_tasklet, (unsigned long) bcs);
2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042

	spin_lock_init(&ubc->isoinlock);
	for (i = 0; i < BAS_INURBS; ++i)
		ubc->isoinurbs[i] = NULL;
	ubc->isoindone = NULL;
	ubc->loststatus = -EINPROGRESS;
	ubc->isoinlost = 0;
	ubc->seqlen = 0;
	ubc->inbyte = 0;
	ubc->inbits = 0;
	ubc->goodbytes = 0;
	ubc->alignerrs = 0;
	ubc->fcserrs = 0;
	ubc->frameerrs = 0;
	ubc->giants = 0;
	ubc->runts = 0;
	ubc->aborts = 0;
	ubc->shared0s = 0;
	ubc->stolen0s = 0;
	tasklet_init(&ubc->rcvd_tasklet,
2043
		     &read_iso_tasklet, (unsigned long) bcs);
2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060
	return 1;
}

static void gigaset_reinitbcshw(struct bc_state *bcs)
{
	struct bas_bc_state *ubc = bcs->hw.bas;

	atomic_set(&bcs->hw.bas->running, 0);
	atomic_set(&bcs->hw.bas->corrbytes, 0);
	bcs->hw.bas->numsub = 0;
	spin_lock_init(&ubc->isooutlock);
	spin_lock_init(&ubc->isoinlock);
	ubc->loststatus = -EINPROGRESS;
}

static void gigaset_freecshw(struct cardstate *cs)
{
2061
	/* timers, URBs and rcvbuf are disposed of in disconnect */
2062
	kfree(cs->hw.bas);
2063
	cs->hw.bas = NULL;
2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097
}

static int gigaset_initcshw(struct cardstate *cs)
{
	struct bas_cardstate *ucs;

	cs->hw.bas = ucs = kmalloc(sizeof *ucs, GFP_KERNEL);
	if (!ucs)
		return 0;

	ucs->urb_cmd_in = NULL;
	ucs->urb_cmd_out = NULL;
	ucs->rcvbuf = NULL;
	ucs->rcvbuf_size = 0;

	spin_lock_init(&ucs->lock);
	ucs->pending = 0;

	atomic_set(&ucs->basstate, 0);
	init_timer(&ucs->timer_ctrl);
	init_timer(&ucs->timer_atrdy);
	init_timer(&ucs->timer_cmd_in);

	return 1;
}

/* freeurbs
 * unlink and deallocate all URBs unconditionally
 * caller must make sure that no commands are still in progress
 * parameter:
 *	cs	controller state structure
 */
static void freeurbs(struct cardstate *cs)
{
2098
	struct bas_cardstate *ucs = cs->hw.bas;
2099 2100 2101 2102 2103 2104 2105 2106
	struct bas_bc_state *ubc;
	int i, j;

	for (j = 0; j < 2; ++j) {
		ubc = cs->bcs[j].hw.bas;
		for (i = 0; i < BAS_OUTURBS; ++i)
			if (ubc->isoouturbs[i].urb) {
				usb_kill_urb(ubc->isoouturbs[i].urb);
2107 2108 2109
				gig_dbg(DEBUG_INIT,
					"%s: isoc output URB %d/%d unlinked",
					__func__, j, i);
2110 2111 2112 2113 2114 2115
				usb_free_urb(ubc->isoouturbs[i].urb);
				ubc->isoouturbs[i].urb = NULL;
			}
		for (i = 0; i < BAS_INURBS; ++i)
			if (ubc->isoinurbs[i]) {
				usb_kill_urb(ubc->isoinurbs[i]);
2116 2117 2118
				gig_dbg(DEBUG_INIT,
					"%s: isoc input URB %d/%d unlinked",
					__func__, j, i);
2119 2120 2121 2122 2123 2124
				usb_free_urb(ubc->isoinurbs[i]);
				ubc->isoinurbs[i] = NULL;
			}
	}
	if (ucs->urb_int_in) {
		usb_kill_urb(ucs->urb_int_in);
2125 2126
		gig_dbg(DEBUG_INIT, "%s: interrupt input URB unlinked",
			__func__);
2127 2128 2129 2130 2131
		usb_free_urb(ucs->urb_int_in);
		ucs->urb_int_in = NULL;
	}
	if (ucs->urb_cmd_out) {
		usb_kill_urb(ucs->urb_cmd_out);
2132 2133
		gig_dbg(DEBUG_INIT, "%s: command output URB unlinked",
			__func__);
2134 2135 2136 2137 2138
		usb_free_urb(ucs->urb_cmd_out);
		ucs->urb_cmd_out = NULL;
	}
	if (ucs->urb_cmd_in) {
		usb_kill_urb(ucs->urb_cmd_in);
2139 2140
		gig_dbg(DEBUG_INIT, "%s: command input URB unlinked",
			__func__);
2141 2142 2143 2144 2145
		usb_free_urb(ucs->urb_cmd_in);
		ucs->urb_cmd_in = NULL;
	}
	if (ucs->urb_ctrl) {
		usb_kill_urb(ucs->urb_ctrl);
2146 2147
		gig_dbg(DEBUG_INIT, "%s: control output URB unlinked",
			__func__);
2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166
		usb_free_urb(ucs->urb_ctrl);
		ucs->urb_ctrl = NULL;
	}
}

/* gigaset_probe
 * This function is called when a new USB device is connected.
 * It checks whether the new device is handled by this driver.
 */
static int gigaset_probe(struct usb_interface *interface,
			 const struct usb_device_id *id)
{
	struct usb_host_interface *hostif;
	struct usb_device *udev = interface_to_usbdev(interface);
	struct cardstate *cs = NULL;
	struct bas_cardstate *ucs = NULL;
	struct bas_bc_state *ubc;
	struct usb_endpoint_descriptor *endpoint;
	int i, j;
2167
	int rc;
2168

2169 2170 2171 2172
	gig_dbg(DEBUG_ANY,
		"%s: Check if device matches .. (Vendor: 0x%x, Product: 0x%x)",
		__func__, le16_to_cpu(udev->descriptor.idVendor),
		le16_to_cpu(udev->descriptor.idProduct));
2173 2174 2175 2176

	/* set required alternate setting */
	hostif = interface->cur_altsetting;
	if (hostif->desc.bAlternateSetting != 3) {
2177 2178 2179
		gig_dbg(DEBUG_ANY,
			"%s: wrong alternate setting %d - trying to switch",
			__func__, hostif->desc.bAlternateSetting);
2180
		if (usb_set_interface(udev, hostif->desc.bInterfaceNumber, 3) < 0) {
2181 2182 2183 2184
			dev_warn(&udev->dev, "usb_set_interface failed, "
				 "device %d interface %d altsetting %d\n",
				 udev->devnum, hostif->desc.bInterfaceNumber,
				 hostif->desc.bAlternateSetting);
2185 2186 2187 2188 2189 2190 2191 2192
			return -ENODEV;
		}
		hostif = interface->cur_altsetting;
	}

	/* Reject application specific interfaces
	 */
	if (hostif->desc.bInterfaceClass != 255) {
2193 2194
		dev_warn(&udev->dev, "%s: bInterfaceClass == %d\n",
			 __func__, hostif->desc.bInterfaceClass);
2195 2196 2197
		return -ENODEV;
	}

2198 2199 2200 2201
	dev_info(&udev->dev,
		 "%s: Device matched (Vendor: 0x%x, Product: 0x%x)\n",
		 __func__, le16_to_cpu(udev->descriptor.idVendor),
		 le16_to_cpu(udev->descriptor.idProduct));
2202 2203 2204

	cs = gigaset_getunassignedcs(driver);
	if (!cs) {
2205
		dev_err(&udev->dev, "no free cardstate\n");
2206 2207 2208
		return -ENODEV;
	}
	ucs = cs->hw.bas;
2209 2210 2211

	/* save off device structure ptrs for later use */
	usb_get_dev(udev);
2212 2213
	ucs->udev = udev;
	ucs->interface = interface;
2214
	cs->dev = &interface->dev;
2215 2216 2217 2218 2219 2220

	/* allocate URBs:
	 * - one for the interrupt pipe
	 * - three for the different uses of the default control pipe
	 * - three for each isochronous pipe
	 */
2221 2222 2223 2224 2225
	if (!(ucs->urb_int_in = usb_alloc_urb(0, SLAB_KERNEL)) ||
	    !(ucs->urb_cmd_in = usb_alloc_urb(0, SLAB_KERNEL)) ||
	    !(ucs->urb_cmd_out = usb_alloc_urb(0, SLAB_KERNEL)) ||
	    !(ucs->urb_ctrl = usb_alloc_urb(0, SLAB_KERNEL)))
		goto allocerr;
2226 2227 2228

	for (j = 0; j < 2; ++j) {
		ubc = cs->bcs[j].hw.bas;
2229 2230 2231 2232 2233 2234 2235 2236
		for (i = 0; i < BAS_OUTURBS; ++i)
			if (!(ubc->isoouturbs[i].urb =
			      usb_alloc_urb(BAS_NUMFRAMES, SLAB_KERNEL)))
				goto allocerr;
		for (i = 0; i < BAS_INURBS; ++i)
			if (!(ubc->isoinurbs[i] =
			      usb_alloc_urb(BAS_NUMFRAMES, SLAB_KERNEL)))
				goto allocerr;
2237 2238 2239 2240 2241 2242 2243 2244
	}

	ucs->rcvbuf = NULL;
	ucs->rcvbuf_size = 0;

	/* Fill the interrupt urb and send it to the core */
	endpoint = &hostif->endpoint[0].desc;
	usb_fill_int_urb(ucs->urb_int_in, udev,
2245 2246 2247 2248
			 usb_rcvintpipe(udev,
					(endpoint->bEndpointAddress) & 0x0f),
			 ucs->int_in_buf, 3, read_int_callback, cs,
			 endpoint->bInterval);
2249
	if ((rc = usb_submit_urb(ucs->urb_int_in, SLAB_KERNEL)) != 0) {
2250
		dev_err(cs->dev, "could not submit interrupt URB: %s\n",
2251
			get_usb_rcmsg(rc));
2252 2253 2254 2255
		goto error;
	}

	/* tell the device that the driver is ready */
2256
	if ((rc = req_submit(cs->bcs, HD_DEVICE_INIT_ACK, 0, 0)) != 0)
2257 2258 2259 2260 2261 2262 2263 2264 2265
		goto error;

	/* tell common part that the device is ready */
	if (startmode == SM_LOCKED)
		atomic_set(&cs->mstate, MS_LOCKED);

	/* save address of controller structure */
	usb_set_intfdata(interface, cs);

2266 2267 2268
	if (!gigaset_start(cs))
		goto error;

2269 2270
	return 0;

2271 2272
allocerr:
	dev_err(cs->dev, "could not allocate URBs\n");
2273 2274
error:
	freeurbs(cs);
2275
	usb_set_intfdata(interface, NULL);
2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286
	gigaset_unassign(cs);
	return -ENODEV;
}

/* gigaset_disconnect
 * This function is called when the Gigaset base is unplugged.
 */
static void gigaset_disconnect(struct usb_interface *interface)
{
	struct cardstate *cs;
	struct bas_cardstate *ucs;
2287
	int j;
2288 2289 2290 2291 2292

	cs = usb_get_intfdata(interface);

	ucs = cs->hw.bas;

2293
	dev_info(cs->dev, "disconnecting Gigaset base\n");
2294 2295 2296 2297 2298 2299 2300 2301 2302 2303

	/* mark base as not ready, all channels disconnected */
	atomic_set(&ucs->basstate, 0);

	/* tell LL all channels are down */
	//FIXME shouldn't gigaset_stop() do this?
	for (j = 0; j < 2; ++j)
		gigaset_bchannel_down(cs->bcs + j);

	/* stop driver (common part) */
2304
	gigaset_stop(cs);
2305 2306 2307 2308 2309

	/* stop timers and URBs, free ressources */
	del_timer_sync(&ucs->timer_ctrl);
	del_timer_sync(&ucs->timer_atrdy);
	del_timer_sync(&ucs->timer_cmd_in);
2310
	freeurbs(cs);
2311
	usb_set_intfdata(interface, NULL);
2312 2313 2314
	kfree(ucs->rcvbuf);
	ucs->rcvbuf = NULL;
	ucs->rcvbuf_size = 0;
2315 2316 2317 2318
	usb_put_dev(ucs->udev);
	ucs->interface = NULL;
	ucs->udev = NULL;
	cs->dev = NULL;
2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349
	gigaset_unassign(cs);
}

static struct gigaset_ops gigops = {
	gigaset_write_cmd,
	gigaset_write_room,
	gigaset_chars_in_buffer,
	gigaset_brkchars,
	gigaset_init_bchannel,
	gigaset_close_bchannel,
	gigaset_initbcshw,
	gigaset_freebcshw,
	gigaset_reinitbcshw,
	gigaset_initcshw,
	gigaset_freecshw,
	gigaset_set_modem_ctrl,
	gigaset_baud_rate,
	gigaset_set_line_ctrl,
	gigaset_isoc_send_skb,
	gigaset_isoc_input,
};

/* bas_gigaset_init
 * This function is called after the kernel module is loaded.
 */
static int __init bas_gigaset_init(void)
{
	int result;

	/* allocate memory for our driver state and intialize it */
	if ((driver = gigaset_initdriver(GIGASET_MINOR, GIGASET_MINORS,
2350
				       GIGASET_MODULENAME, GIGASET_DEVNAME,
2351
				       &gigops, THIS_MODULE)) == NULL)
2352 2353 2354
		goto error;

	/* allocate memory for our device state and intialize it */
2355 2356
	cardstate = gigaset_initcs(driver, 2, 0, 0, cidmode,
				   GIGASET_MODULENAME);
2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384
	if (!cardstate)
		goto error;

	/* register this driver with the USB subsystem */
	result = usb_register(&gigaset_usb_driver);
	if (result < 0) {
		err("usb_register failed (error %d)", -result);
		goto error;
	}

	info(DRIVER_AUTHOR);
	info(DRIVER_DESC);
	return 0;

error:	if (cardstate)
		gigaset_freecs(cardstate);
	cardstate = NULL;
	if (driver)
		gigaset_freedriver(driver);
	driver = NULL;
	return -1;
}

/* bas_gigaset_exit
 * This function is called before the kernel module is unloaded.
 */
static void __exit bas_gigaset_exit(void)
{
2385 2386
	struct bas_cardstate *ucs = cardstate->hw.bas;

2387
	gigaset_blockdriver(driver); /* => probe will fail
2388 2389
				      * => no gigaset_start any more
				      */
2390 2391 2392 2393

	gigaset_shutdown(cardstate);
	/* from now on, no isdn callback should be possible */

2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411
	/* close all still open channels */
	if (atomic_read(&ucs->basstate) & BS_B1OPEN) {
		gig_dbg(DEBUG_INIT, "closing B1 channel");
		usb_control_msg(ucs->udev, usb_sndctrlpipe(ucs->udev, 0),
				HD_CLOSE_B1CHANNEL, OUT_VENDOR_REQ, 0, 0,
				NULL, 0, BAS_TIMEOUT);
	}
	if (atomic_read(&ucs->basstate) & BS_B2OPEN) {
		gig_dbg(DEBUG_INIT, "closing B2 channel");
		usb_control_msg(ucs->udev, usb_sndctrlpipe(ucs->udev, 0),
				HD_CLOSE_B2CHANNEL, OUT_VENDOR_REQ, 0, 0,
				NULL, 0, BAS_TIMEOUT);
	}
	if (atomic_read(&ucs->basstate) & BS_ATOPEN) {
		gig_dbg(DEBUG_INIT, "closing AT channel");
		usb_control_msg(ucs->udev, usb_sndctrlpipe(ucs->udev, 0),
				HD_CLOSE_ATCHANNEL, OUT_VENDOR_REQ, 0, 0,
				NULL, 0, BAS_TIMEOUT);
2412
	}
2413
	atomic_set(&ucs->basstate, 0);
2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432

	/* deregister this driver with the USB subsystem */
	usb_deregister(&gigaset_usb_driver);
	/* this will call the disconnect-callback */
	/* from now on, no disconnect/probe callback should be running */

	gigaset_freecs(cardstate);
	cardstate = NULL;
	gigaset_freedriver(driver);
	driver = NULL;
}


module_init(bas_gigaset_init);
module_exit(bas_gigaset_exit);

MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_LICENSE("GPL");