bas-gigaset.c 71.6 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
 *
 * =====================================================================
 *	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/usb.h>
#include <linux/module.h>
#include <linux/moduleparam.h>

/* Version Information */
22
#define DRIVER_AUTHOR "Tilman Schmidt <tilman@imap.cc>, Hansjoerg Lipp <hjlipp@web.de>, Stefan Eilers"
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
#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"

41 42
/* length limit according to Siemens 3070usb-protokoll.doc ch. 2.1 */
#define IF_WRITEBUF 264
43

44 45 46
/* interrupt pipe message size according to ibid. ch. 2.2 */
#define IP_MSGSIZE 3

47 48
/* Values for the Gigaset 307x */
#define USB_GIGA_VENDOR_ID      0x0681
49 50
#define USB_3070_PRODUCT_ID     0x0001
#define USB_3075_PRODUCT_ID     0x0002
51 52 53 54
#define USB_SX303_PRODUCT_ID    0x0021
#define USB_SX353_PRODUCT_ID    0x0022

/* table of devices that work with this driver */
T
Tilman Schmidt 已提交
55
static const struct usb_device_id gigaset_table[] = {
56 57
	{ USB_DEVICE(USB_GIGA_VENDOR_ID, USB_3070_PRODUCT_ID) },
	{ USB_DEVICE(USB_GIGA_VENDOR_ID, USB_3075_PRODUCT_ID) },
58 59 60 61 62 63 64
	{ 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);

65
/*======================= local function prototypes ==========================*/
66

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

T
Tilman Schmidt 已提交
74 75 76 77 78 79 80 81
/* functions called before/after suspend */
static int gigaset_suspend(struct usb_interface *intf, pm_message_t message);
static int gigaset_resume(struct usb_interface *intf);

/* functions called before/after device reset */
static int gigaset_pre_reset(struct usb_interface *intf);
static int gigaset_post_reset(struct usb_interface *intf);

82
static int atread_submit(struct cardstate *, int);
83
static void stopurbs(struct bas_bc_state *);
84
static int req_submit(struct bc_state *, int, int, int);
85 86
static int atwrite_submit(struct cardstate *, unsigned char *, int);
static int start_cbsend(struct cardstate *);
87

88
/*============================================================================*/
89 90

struct bas_cardstate {
91 92
	struct usb_device	*udev;		/* USB device pointer */
	struct usb_interface	*interface;	/* interface for this device */
93 94
	unsigned char		minor;		/* starting minor number */

95
	struct urb		*urb_ctrl;	/* control pipe default URB */
96 97
	struct usb_ctrlrequest	dr_ctrl;
	struct timer_list	timer_ctrl;	/* control request timeout */
98
	int			retry_ctrl;
99 100

	struct timer_list	timer_atrdy;	/* AT command ready timeout */
101
	struct urb		*urb_cmd_out;	/* for sending AT commands */
102 103 104
	struct usb_ctrlrequest	dr_cmd_out;
	int			retry_cmd_out;

105
	struct urb		*urb_cmd_in;	/* for receiving AT replies */
106 107
	struct usb_ctrlrequest	dr_cmd_in;
	struct timer_list	timer_cmd_in;	/* receive request timeout */
108
	unsigned char		*rcvbuf;	/* AT reply receive buffer */
109

110
	struct urb		*urb_int_in;	/* URB for interrupt pipe */
111
	unsigned char		*int_in_buf;
112 113 114
	struct work_struct	int_in_wq;	/* for usb_clear_halt() */
	struct timer_list	timer_int_in;	/* int read retry delay */
	int			retry_int_in;
115 116

	spinlock_t		lock;		/* locks all following */
T
Tilman Schmidt 已提交
117
	int			basstate;	/* bitmap (BS_*) */
118
	int			pending;	/* uncompleted base request */
T
Tilman Schmidt 已提交
119
	wait_queue_head_t	waitqueue;
120 121 122 123 124 125
	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) */
126 127 128 129 130 131 132 133
#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 */
T
Tilman Schmidt 已提交
134
#define BS_SUSPEND	0x100	/* USB port suspended */
T
Tilman Schmidt 已提交
135
#define BS_RESETTING	0x200	/* waiting for HD_RESET_INTERRUPT_PIPE_ACK */
136 137


T
Tilman Schmidt 已提交
138
static struct gigaset_driver *driver;
139 140 141 142 143 144 145

/* 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,
T
Tilman Schmidt 已提交
146 147 148 149 150
	.suspend =	gigaset_suspend,
	.resume =	gigaset_resume,
	.reset_resume =	gigaset_post_reset,
	.pre_reset =	gigaset_pre_reset,
	.post_reset =	gigaset_post_reset,
151
	.disable_hub_initiated_lpm = 1,
152 153
};

154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
/* 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:
176
		return "too many isoc frames requested";
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
	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;
	}
}

195 196 197 198 199 200 201 202 203 204
/* 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:
205
		return "unlinked (sync)";
206
	case -EINPROGRESS:
207
		return "URB still pending";
208
	case -EPROTO:
209
		return "bitstuff error, timeout, or unknown USB error";
210
	case -EILSEQ:
211
		return "CRC mismatch, timeout, or unknown USB error";
212
	case -ETIME:
213
		return "USB response timeout";
214 215 216 217 218 219 220
	case -EPIPE:
		return "endpoint stalled";
	case -ECOMM:
		return "IN buffer overrun";
	case -ENOSR:
		return "OUT buffer underrun";
	case -EOVERFLOW:
221
		return "endpoint babble";
222
	case -EREMOTEIO:
223
		return "short packet";
224 225
	case -ENODEV:
		return "device removed";
226
	case -EXDEV:
227
		return "partial isoc transfer";
228
	case -EINVAL:
229
		return "ISO madness";
230 231
	case -ECONNRESET:
		return "unlinked (async)";
232
	case -ESHUTDOWN:
233
		return "device shut down";
234
	default:
235
		snprintf(unkmsg, sizeof(unkmsg), "unknown status %d", status);
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
		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,
260
			    struct urb *urb)
261 262 263
{
#ifdef CONFIG_GIGASET_DEBUG
	int i;
264
	gig_dbg(level, "%s urb(0x%08lx)->{", tag, (unsigned long) urb);
265
	if (urb) {
266 267
		gig_dbg(level,
			"  dev=0x%08lx, pipe=%s:EP%d/DV%d:%s, "
268
			"hcpriv=0x%08lx, transfer_flags=0x%x,",
269 270 271 272
			(unsigned long) urb->dev,
			usb_pipetype_str(urb->pipe),
			usb_pipeendpoint(urb->pipe), usb_pipedevice(urb->pipe),
			usb_pipein(urb->pipe) ? "in" : "out",
273
			(unsigned long) urb->hcpriv,
274 275 276
			urb->transfer_flags);
		gig_dbg(level,
			"  transfer_buffer=0x%08lx[%d], actual_length=%d, "
277
			"setup_packet=0x%08lx,",
278 279
			(unsigned long) urb->transfer_buffer,
			urb->transfer_buffer_length, urb->actual_length,
280
			(unsigned long) urb->setup_packet);
281 282 283 284 285 286 287 288 289 290
		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);
291
		for (i = 0; i < urb->number_of_packets; i++) {
292 293
			struct usb_iso_packet_descriptor *pifd
				= &urb->iso_frame_desc[i];
294 295 296 297 298
			gig_dbg(level,
				"    {offset=%u, length=%u, actual_length=%u, "
				"status=%u}",
				pifd->offset, pifd->length, pifd->actual_length,
				pifd->status);
299 300
		}
	}
301
	gig_dbg(level, "}}");
302 303 304 305 306
#endif
}

/* read/set modem control bits etc. (m10x only) */
static int gigaset_set_modem_ctrl(struct cardstate *cs, unsigned old_state,
307
				  unsigned new_state)
308 309 310 311 312 313 314 315 316 317 318 319 320 321
{
	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;
}

T
Tilman Schmidt 已提交
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
/* set/clear bits in base connection state, return previous state
 */
static inline int update_basstate(struct bas_cardstate *ucs,
				  int set, int clear)
{
	unsigned long flags;
	int state;

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

337 338 339 340
/* 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.
341
 * cs->lock must not be held.
342 343 344 345 346 347 348
 * argument:
 *	B channel control structure
 */
static inline void error_hangup(struct bc_state *bcs)
{
	struct cardstate *cs = bcs->cs;

T
Tilman Schmidt 已提交
349
	gigaset_add_event(cs, &bcs->at_state, EV_HUP, NULL, 0, NULL);
350 351 352 353 354
	gigaset_schedule_event(cs);
}

/* error_reset
 * reset Gigaset device because of an unrecoverable error
355
 * This function may be called from any context, and takes care of
356
 * scheduling the necessary actions for execution outside of interrupt context.
357
 * cs->hw.bas->lock must not be held.
358 359 360 361 362
 * argument:
 *	controller state structure
 */
static inline void error_reset(struct cardstate *cs)
{
T
Tilman Schmidt 已提交
363 364
	/* reset interrupt pipe to recover (ignore errors) */
	update_basstate(cs->hw.bas, BS_RESETTING, 0);
365 366 367
	if (req_submit(cs->bcs, HD_RESET_INTERRUPT_PIPE, 0, BAS_TIMEOUT))
		/* submission failed, escalate to USB port reset */
		usb_queue_reset_device(cs->hw.bas->interface);
368 369 370 371 372
}

/* check_pending
 * check for completion of pending control request
 * parameter:
373
 *	ucs	hardware specific controller state structure
374 375 376 377 378 379 380 381 382 383
 */
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:
T
Tilman Schmidt 已提交
384
		if (ucs->basstate & BS_ATOPEN)
385 386 387
			ucs->pending = 0;
		break;
	case HD_OPEN_B1CHANNEL:
T
Tilman Schmidt 已提交
388
		if (ucs->basstate & BS_B1OPEN)
389 390 391
			ucs->pending = 0;
		break;
	case HD_OPEN_B2CHANNEL:
T
Tilman Schmidt 已提交
392
		if (ucs->basstate & BS_B2OPEN)
393 394 395
			ucs->pending = 0;
		break;
	case HD_CLOSE_ATCHANNEL:
T
Tilman Schmidt 已提交
396
		if (!(ucs->basstate & BS_ATOPEN))
397 398 399
			ucs->pending = 0;
		break;
	case HD_CLOSE_B1CHANNEL:
T
Tilman Schmidt 已提交
400
		if (!(ucs->basstate & BS_B1OPEN))
401 402 403
			ucs->pending = 0;
		break;
	case HD_CLOSE_B2CHANNEL:
T
Tilman Schmidt 已提交
404
		if (!(ucs->basstate & BS_B2OPEN))
405 406 407 408 409
			ucs->pending = 0;
		break;
	case HD_DEVICE_INIT_ACK:		/* no reply expected */
		ucs->pending = 0;
		break;
T
Tilman Schmidt 已提交
410 411 412 413
	case HD_RESET_INTERRUPT_PIPE:
		if (!(ucs->basstate & BS_RESETTING))
			ucs->pending = 0;
		break;
414 415 416 417
	/*
	 * HD_READ_ATMESSAGE and HD_WRITE_ATMESSAGE are handled separately
	 * and should never end up here
	 */
418
	default:
419 420 421
		dev_warn(&ucs->interface->dev,
			 "unknown pending request 0x%02x cleared\n",
			 ucs->pending);
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
		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;
439
	struct bas_cardstate *ucs = cs->hw.bas;
440
	int rc;
441 442

	if (!ucs->rcvbuf_size) {
443
		gig_dbg(DEBUG_USBREQ, "%s: no receive in progress", __func__);
444 445 446
		return;
	}

447
	if (ucs->retry_cmd_in++ >= BAS_RETRY) {
448 449 450
		dev_err(cs->dev,
			"control read: timeout, giving up after %d tries\n",
			ucs->retry_cmd_in);
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
		kfree(ucs->rcvbuf);
		ucs->rcvbuf = NULL;
		ucs->rcvbuf_size = 0;
		error_reset(cs);
		return;
	}

	gig_dbg(DEBUG_USBREQ, "%s: timeout, retry %d",
		__func__, ucs->retry_cmd_in);
	rc = atread_submit(cs, BAS_TIMEOUT);
	if (rc < 0) {
		kfree(ucs->rcvbuf);
		ucs->rcvbuf = NULL;
		ucs->rcvbuf_size = 0;
		if (rc != -ENODEV)
			error_reset(cs);
467
	}
468 469
}

470 471 472 473 474 475 476
/* 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
 */
477
static void read_ctrl_callback(struct urb *urb)
478 479 480 481
{
	struct inbuf_t *inbuf = urb->context;
	struct cardstate *cs = inbuf->cs;
	struct bas_cardstate *ucs = cs->hw.bas;
482
	int status = urb->status;
483 484 485 486
	unsigned numbytes;
	int rc;

	update_basstate(ucs, 0, BS_ATRDPEND);
T
Tilman Schmidt 已提交
487
	wake_up(&ucs->waitqueue);
488 489
	del_timer(&ucs->timer_cmd_in);

490
	switch (status) {
491 492 493 494
	case 0:				/* normal completion */
		numbytes = urb->actual_length;
		if (unlikely(numbytes != ucs->rcvbuf_size)) {
			dev_warn(cs->dev,
495
				 "control read: received %d chars, expected %d\n",
496 497 498 499 500
				 numbytes, ucs->rcvbuf_size);
			if (numbytes > ucs->rcvbuf_size)
				numbytes = ucs->rcvbuf_size;
		}

501 502 503 504
		/* copy received bytes to inbuf, notify event layer */
		if (gigaset_fill_inbuf(inbuf, ucs->rcvbuf, numbytes)) {
			gig_dbg(DEBUG_INTR, "%s-->BH", __func__);
			gigaset_schedule_event(cs);
505 506 507 508 509 510 511 512
		}
		break;

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

518
	default:			/* other errors: retry */
519
		if (ucs->retry_cmd_in++ < BAS_RETRY) {
520 521
			gig_dbg(DEBUG_USBREQ, "%s: %s, retry %d", __func__,
				get_usb_statmsg(status), ucs->retry_cmd_in);
522
			rc = atread_submit(cs, BAS_TIMEOUT);
523 524
			if (rc >= 0)
				/* successfully resubmitted, skip freeing */
525
				return;
526 527 528
			if (rc == -ENODEV)
				/* disconnect, no further action necessary */
				break;
529
		}
530 531
		dev_err(cs->dev, "control read: %s, giving up after %d tries\n",
			get_usb_statmsg(status), ucs->retry_cmd_in);
532 533 534
		error_reset(cs);
	}

535
	/* read finished, free buffer */
536 537 538 539 540
	kfree(ucs->rcvbuf);
	ucs->rcvbuf = NULL;
	ucs->rcvbuf_size = 0;
}

541
/* atread_submit
542
 * submit an HD_READ_ATMESSAGE command URB and optionally start a timeout
543 544 545 546 547 548 549 550 551 552
 * 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)
{
553
	struct bas_cardstate *ucs = cs->hw.bas;
T
Tilman Schmidt 已提交
554
	int basstate;
555 556
	int ret;

557 558
	gig_dbg(DEBUG_USBREQ, "-------> HD_READ_ATMESSAGE (%d)",
		ucs->rcvbuf_size);
559

T
Tilman Schmidt 已提交
560 561
	basstate = update_basstate(ucs, BS_ATRDPEND, 0);
	if (basstate & BS_ATRDPEND) {
562 563
		dev_err(cs->dev,
			"could not submit HD_READ_ATMESSAGE: URB busy\n");
564 565 566
		return -EBUSY;
	}

T
Tilman Schmidt 已提交
567 568 569 570 571 572 573 574 575
	if (basstate & BS_SUSPEND) {
		dev_notice(cs->dev,
			   "HD_READ_ATMESSAGE not submitted, "
			   "suspend in progress\n");
		update_basstate(ucs, 0, BS_ATRDPEND);
		/* treat like disconnect */
		return -ENODEV;
	}

576 577 578 579 580 581
	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,
582
			     usb_rcvctrlpipe(ucs->udev, 0),
T
Tilman Schmidt 已提交
583
			     (unsigned char *) &ucs->dr_cmd_in,
584 585
			     ucs->rcvbuf, ucs->rcvbuf_size,
			     read_ctrl_callback, cs->inbuf);
586

T
Tilman Schmidt 已提交
587 588
	ret = usb_submit_urb(ucs->urb_cmd_in, GFP_ATOMIC);
	if (ret != 0) {
589
		update_basstate(ucs, 0, BS_ATRDPEND);
590
		dev_err(cs->dev, "could not submit HD_READ_ATMESSAGE: %s\n",
591
			get_usb_rcmsg(ret));
592 593 594 595
		return ret;
	}

	if (timeout > 0) {
596
		gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
597
		mod_timer(&ucs->timer_cmd_in, jiffies + timeout * HZ / 10);
598 599 600 601
	}
	return 0;
}

602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
/* int_in_work
 * workqueue routine to clear halt on interrupt in endpoint
 */

static void int_in_work(struct work_struct *work)
{
	struct bas_cardstate *ucs =
		container_of(work, struct bas_cardstate, int_in_wq);
	struct urb *urb = ucs->urb_int_in;
	struct cardstate *cs = urb->context;
	int rc;

	/* clear halt condition */
	rc = usb_clear_halt(ucs->udev, urb->pipe);
	gig_dbg(DEBUG_USBREQ, "clear_halt: %s", get_usb_rcmsg(rc));
	if (rc == 0)
		/* success, resubmit interrupt read URB */
		rc = usb_submit_urb(urb, GFP_ATOMIC);
620 621 622 623 624 625 626

	switch (rc) {
	case 0:		/* success */
	case -ENODEV:	/* device gone */
	case -EINVAL:	/* URB already resubmitted, or terminal badness */
		break;
	default:	/* failure: try to recover by resetting the device */
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
		dev_err(cs->dev, "clear halt failed: %s\n", get_usb_rcmsg(rc));
		rc = usb_lock_device_for_reset(ucs->udev, ucs->interface);
		if (rc == 0) {
			rc = usb_reset_device(ucs->udev);
			usb_unlock_device(ucs->udev);
		}
	}
	ucs->retry_int_in = 0;
}

/* int_in_resubmit
 * timer routine for interrupt read delayed resubmit
 * argument:
 *	controller state structure
 */
static void int_in_resubmit(unsigned long data)
{
	struct cardstate *cs = (struct cardstate *) data;
	struct bas_cardstate *ucs = cs->hw.bas;
	int rc;

	if (ucs->retry_int_in++ >= BAS_RETRY) {
		dev_err(cs->dev, "interrupt read: giving up after %d tries\n",
			ucs->retry_int_in);
		usb_queue_reset_device(ucs->interface);
		return;
	}

	gig_dbg(DEBUG_USBREQ, "%s: retry %d", __func__, ucs->retry_int_in);
	rc = usb_submit_urb(ucs->urb_int_in, GFP_ATOMIC);
	if (rc != 0 && rc != -ENODEV) {
		dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
			get_usb_rcmsg(rc));
		usb_queue_reset_device(ucs->interface);
	}
}

664 665 666 667 668 669 670
/* 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
 */
671
static void read_int_callback(struct urb *urb)
672
{
673 674
	struct cardstate *cs = urb->context;
	struct bas_cardstate *ucs = cs->hw.bas;
675
	struct bc_state *bcs;
676
	int status = urb->status;
677
	unsigned long flags;
678
	int rc;
679 680 681
	unsigned l;
	int channel;

682
	switch (status) {
683
	case 0:			/* success */
684
		ucs->retry_int_in = 0;
685
		break;
686 687 688
	case -EPIPE:			/* endpoint stalled */
		schedule_work(&ucs->int_in_wq);
		/* fall through */
689 690
	case -ENOENT:			/* cancelled */
	case -ECONNRESET:		/* cancelled (async) */
691
	case -EINPROGRESS:		/* pending */
692 693 694
	case -ENODEV:			/* device removed */
	case -ESHUTDOWN:		/* device shut down */
		/* no further action necessary */
695
		gig_dbg(DEBUG_USBREQ, "%s: %s",
696
			__func__, get_usb_statmsg(status));
697
		return;
698 699 700 701 702 703 704
	case -EPROTO:			/* protocol error or unplug */
	case -EILSEQ:
	case -ETIME:
		/* resubmit after delay */
		gig_dbg(DEBUG_USBREQ, "%s: %s",
			__func__, get_usb_statmsg(status));
		mod_timer(&ucs->timer_int_in, jiffies + HZ / 10);
705
		return;
706
	default:		/* other errors: just resubmit */
707
		dev_warn(cs->dev, "interrupt read: %s\n",
708
			 get_usb_statmsg(status));
709 710 711
		goto resubmit;
	}

712
	/* drop incomplete packets even if the missing bytes wouldn't matter */
713
	if (unlikely(urb->actual_length < IP_MSGSIZE)) {
714 715 716 717 718
		dev_warn(cs->dev, "incomplete interrupt packet (%d bytes)\n",
			 urb->actual_length);
		goto resubmit;
	}

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

722 723 724
	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]);
725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769

	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,
770 771 772 773 774
			   &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));
775 776 777 778
		break;

	case HD_RECEIVEATDATA_ACK:	/* AT response ready to be received */
		if (!l) {
779
			dev_warn(cs->dev,
780
				 "HD_RECEIVEATDATA_ACK with length 0 ignored\n");
781 782 783
			break;
		}
		spin_lock_irqsave(&cs->lock, flags);
784 785 786
		if (ucs->basstate & BS_ATRDPEND) {
			spin_unlock_irqrestore(&cs->lock, flags);
			dev_warn(cs->dev,
787
				 "HD_RECEIVEATDATA_ACK(%d) during HD_READ_ATMESSAGE(%d) ignored\n",
788 789 790
				 l, ucs->rcvbuf_size);
			break;
		}
791
		if (ucs->rcvbuf_size) {
792
			/* throw away previous buffer - we have no queue */
793
			dev_err(cs->dev,
794 795 796 797
				"receive AT data overrun, %d bytes lost\n",
				ucs->rcvbuf_size);
			kfree(ucs->rcvbuf);
			ucs->rcvbuf_size = 0;
798
		}
T
Tilman Schmidt 已提交
799 800
		ucs->rcvbuf = kmalloc(l, GFP_ATOMIC);
		if (ucs->rcvbuf == NULL) {
801
			spin_unlock_irqrestore(&cs->lock, flags);
802
			dev_err(cs->dev, "out of memory receiving AT data\n");
803 804 805 806
			break;
		}
		ucs->rcvbuf_size = l;
		ucs->retry_cmd_in = 0;
T
Tilman Schmidt 已提交
807 808
		rc = atread_submit(cs, BAS_TIMEOUT);
		if (rc < 0) {
809 810 811 812 813
			kfree(ucs->rcvbuf);
			ucs->rcvbuf = NULL;
			ucs->rcvbuf_size = 0;
		}
		spin_unlock_irqrestore(&cs->lock, flags);
814 815
		if (rc < 0 && rc != -ENODEV)
			error_reset(cs);
816 817 818
		break;

	case HD_RESET_INTERRUPT_PIPE_ACK:
T
Tilman Schmidt 已提交
819 820
		update_basstate(ucs, 0, BS_RESETTING);
		dev_notice(cs->dev, "interrupt pipe reset\n");
821 822 823
		break;

	case HD_SUSPEND_END:
824
		gig_dbg(DEBUG_USBREQ, "HD_SUSPEND_END");
825 826 827
		break;

	default:
828 829 830
		dev_warn(cs->dev,
			 "unknown Gigaset signal 0x%02x (%u) ignored\n",
			 (int) ucs->int_in_buf[0], l);
831 832 833
	}

	check_pending(ucs);
T
Tilman Schmidt 已提交
834
	wake_up(&ucs->waitqueue);
835 836

resubmit:
837
	rc = usb_submit_urb(urb, GFP_ATOMIC);
838
	if (unlikely(rc != 0 && rc != -ENODEV)) {
839
		dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
840
			get_usb_rcmsg(rc));
841 842 843 844 845 846 847 848 849 850 851
		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
 */
852
static void read_iso_callback(struct urb *urb)
853 854 855
{
	struct bc_state *bcs;
	struct bas_bc_state *ubc;
856
	int status = urb->status;
857 858 859 860
	unsigned long flags;
	int i, rc;

	/* status codes not worth bothering the tasklet with */
861 862 863 864 865
	if (unlikely(status == -ENOENT ||
		     status == -ECONNRESET ||
		     status == -EINPROGRESS ||
		     status == -ENODEV ||
		     status == -ESHUTDOWN)) {
866
		gig_dbg(DEBUG_ISO, "%s: %s",
867
			__func__, get_usb_statmsg(status));
868 869 870
		return;
	}

871
	bcs = urb->context;
872 873 874 875 876 877
	ubc = bcs->hw.bas;

	spin_lock_irqsave(&ubc->isoinlock, flags);
	if (likely(ubc->isoindone == NULL)) {
		/* pass URB to tasklet */
		ubc->isoindone = urb;
878
		ubc->isoinstatus = status;
879
		tasklet_hi_schedule(&ubc->rcvd_tasklet);
880 881
	} else {
		/* tasklet still busy, drop data and resubmit URB */
882
		gig_dbg(DEBUG_ISO, "%s: overrun", __func__);
883
		ubc->loststatus = status;
884 885 886
		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 &&
887
				     urb->iso_frame_desc[i].status != -EINPROGRESS))
888 889 890 891
				ubc->loststatus = urb->iso_frame_desc[i].status;
			urb->iso_frame_desc[i].status = 0;
			urb->iso_frame_desc[i].actual_length = 0;
		}
T
Tilman Schmidt 已提交
892
		if (likely(ubc->running)) {
893 894
			/* urb->dev is clobbered by USB subsystem */
			urb->dev = bcs->cs->hw.bas->udev;
895 896
			urb->transfer_flags = URB_ISO_ASAP;
			urb->number_of_packets = BAS_NUMFRAMES;
897
			rc = usb_submit_urb(urb, GFP_ATOMIC);
898
			if (unlikely(rc != 0 && rc != -ENODEV)) {
899
				dev_err(bcs->cs->dev,
900
					"could not resubmit isoc read URB: %s\n",
901
					get_usb_rcmsg(rc));
902 903 904 905 906 907 908 909 910 911 912 913 914 915 916
				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
 */
917
static void write_iso_callback(struct urb *urb)
918 919 920
{
	struct isow_urbctx_t *ucx;
	struct bas_bc_state *ubc;
921
	int status = urb->status;
922 923 924
	unsigned long flags;

	/* status codes not worth bothering the tasklet with */
925 926 927 928 929
	if (unlikely(status == -ENOENT ||
		     status == -ECONNRESET ||
		     status == -EINPROGRESS ||
		     status == -ENODEV ||
		     status == -ESHUTDOWN)) {
930
		gig_dbg(DEBUG_ISO, "%s: %s",
931
			__func__, get_usb_statmsg(status));
932 933 934 935
		return;
	}

	/* pass URB context to tasklet */
936
	ucx = urb->context;
937
	ubc = ucx->bcs->hw.bas;
938
	ucx->status = status;
939 940 941 942 943

	spin_lock_irqsave(&ubc->isooutlock, flags);
	ubc->isooutovfl = ubc->isooutdone;
	ubc->isooutdone = ucx;
	spin_unlock_irqrestore(&ubc->isooutlock, flags);
944
	tasklet_hi_schedule(&ubc->sent_tasklet);
945 946 947 948 949 950 951 952 953 954 955 956
}

/* 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)
{
957
	struct bas_bc_state *ubc = bcs->hw.bas;
958 959 960 961 962
	struct urb *urb;
	int j, k;
	int rc;

	/* initialize L2 reception */
963
	if (bcs->proto2 == L2_HDLC)
964 965 966
		bcs->inputstate |= INS_flag_hunt;

	/* submit all isochronous input URBs */
T
Tilman Schmidt 已提交
967
	ubc->running = 1;
968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991
	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);
T
Tilman Schmidt 已提交
992 993
		rc = usb_submit_urb(urb, GFP_ATOMIC);
		if (rc != 0)
994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
			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;
	}

T
Tilman Schmidt 已提交
1025
	/* keep one URB free, submit the others */
1026
	for (k = 0; k < BAS_OUTURBS - 1; ++k) {
1027
		dump_urb(DEBUG_ISO, "Initial isoc write", urb);
1028
		rc = usb_submit_urb(ubc->isoouturbs[k].urb, GFP_ATOMIC);
1029
		if (rc != 0)
1030 1031 1032
			goto error;
	}
	dump_urb(DEBUG_ISO, "Initial isoc write (free)", urb);
1033
	ubc->isooutfree = &ubc->isoouturbs[BAS_OUTURBS - 1];
1034 1035
	ubc->isooutdone = ubc->isooutovfl = NULL;
	return 0;
1036
error:
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
	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;

T
Tilman Schmidt 已提交
1051
	ubc->running = 0;
1052 1053 1054

	for (k = 0; k < BAS_INURBS; ++k) {
		rc = usb_unlink_urb(ubc->isoinurbs[k]);
1055
		gig_dbg(DEBUG_ISO,
1056 1057
			"%s: isoc input URB %d unlinked, result = %s",
			__func__, k, get_usb_rcmsg(rc));
1058 1059 1060 1061
	}

	for (k = 0; k < BAS_OUTURBS; ++k) {
		rc = usb_unlink_urb(ubc->isoouturbs[k].urb);
1062
		gig_dbg(DEBUG_ISO,
1063 1064
			"%s: isoc output URB %d unlinked, result = %s",
			__func__, k, get_usb_rcmsg(rc));
1065 1066 1067 1068 1069 1070 1071 1072 1073
	}
}

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

/* submit_iso_write_urb
 * fill and submit the next isochronous write URB
 * parameters:
1074
 *	ucx	context structure containing URB
1075 1076 1077 1078 1079 1080 1081
 * 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)
{
1082 1083
	struct urb *urb = ucx->urb;
	struct bas_bc_state *ubc = ucx->bcs->hw.bas;
1084 1085 1086
	struct usb_iso_packet_descriptor *ifd;
	int corrbytes, nframe, rc;

1087 1088
	/* urb->dev is clobbered by USB subsystem */
	urb->dev = ucx->bcs->cs->hw.bas->udev;
1089 1090 1091 1092 1093 1094 1095 1096 1097
	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;
T
Tilman Schmidt 已提交
1098 1099
		corrbytes = atomic_read(&ubc->corrbytes);
		if (corrbytes != 0) {
1100 1101
			gig_dbg(DEBUG_ISO, "%s: corrbytes=%d",
				__func__, corrbytes);
1102 1103 1104 1105 1106 1107 1108 1109 1110
			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 */
1111 1112 1113
		rc = gigaset_isowbuf_getbytes(ubc->isooutbuf, ifd->length);
		if (rc < 0) {
			if (rc == -EBUSY) {
1114 1115 1116 1117
				gig_dbg(DEBUG_ISO,
					"%s: buffer busy at frame %d",
					__func__, nframe);
				/* tasklet will be restarted from
1118
				   gigaset_isoc_send_skb() */
1119
			} else {
1120 1121
				dev_err(ucx->bcs->cs->dev,
					"%s: buffer error %d at frame %d\n",
1122 1123
					__func__, rc, nframe);
				return rc;
1124 1125 1126
			}
			break;
		}
1127
		ifd->offset = rc;
T
Tilman Schmidt 已提交
1128
		ucx->limit = ubc->isooutbuf->nextread;
1129 1130 1131
		ifd->status = 0;
		ifd->actual_length = 0;
	}
1132 1133 1134
	if (unlikely(nframe == 0))
		return 0;	/* no data to send */
	urb->number_of_packets = nframe;
1135

1136
	rc = usb_submit_urb(urb, GFP_ATOMIC);
1137 1138 1139 1140 1141
	if (unlikely(rc)) {
		if (rc == -ENODEV)
			/* device removed - give up silently */
			gig_dbg(DEBUG_ISO, "%s: disconnected", __func__);
		else
1142
			dev_err(ucx->bcs->cs->dev,
1143
				"could not submit isoc write URB: %s\n",
1144 1145
				get_usb_rcmsg(rc));
		return rc;
1146
	}
1147
	++ubc->numsub;
1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158
	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)
{
1159 1160 1161
	struct bc_state *bcs = (struct bc_state *) data;
	struct bas_bc_state *ubc = bcs->hw.bas;
	struct cardstate *cs = bcs->cs;
1162 1163
	struct isow_urbctx_t *done, *next, *ovfl;
	struct urb *urb;
1164
	int status;
1165 1166 1167 1168 1169
	struct usb_iso_packet_descriptor *ifd;
	unsigned long flags;
	int i;
	struct sk_buff *skb;
	int len;
1170
	int rc;
1171 1172 1173

	/* loop while completed URBs arrive in time */
	for (;;) {
T
Tilman Schmidt 已提交
1174
		if (unlikely(!(ubc->running))) {
1175
			gig_dbg(DEBUG_ISO, "%s: not running", __func__);
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
			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) {
1187
			dev_err(cs->dev, "isoc write underrun\n");
1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
			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) {
1200 1201
			rc = submit_iso_write_urb(next);
			if (unlikely(rc <= 0 && rc != -ENODEV)) {
1202 1203 1204 1205 1206 1207 1208 1209 1210
				/* 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 */
1211
					dev_err(cs->dev,
1212
						"losing isoc write URB\n");
1213 1214 1215 1216 1217 1218 1219
					error_hangup(bcs);
				}
			}
		}

		/* process completed URB */
		urb = done->urb;
1220 1221
		status = done->status;
		switch (status) {
1222 1223 1224 1225
		case -EXDEV:			/* partial completion */
			gig_dbg(DEBUG_ISO, "%s: URB partially completed",
				__func__);
			/* fall through - what's the difference anyway? */
1226
		case 0:				/* normal completion */
1227 1228 1229
			/* inspect individual frames
			 * assumptions (for lack of documentation):
			 * - actual_length bytes of first frame in error are
1230
			 *   successfully sent
1231 1232 1233 1234 1235 1236
			 * - all following frames are not sent at all
			 */
			for (i = 0; i < BAS_NUMFRAMES; i++) {
				ifd = &urb->iso_frame_desc[i];
				if (ifd->status ||
				    ifd->actual_length != ifd->length) {
1237
					dev_warn(cs->dev,
1238
						 "isoc write: frame %d[%d/%d]: %s\n",
1239 1240 1241
						 i, ifd->actual_length,
						 ifd->length,
						 get_usb_statmsg(ifd->status));
1242 1243 1244 1245
					break;
				}
			}
			break;
1246
		case -EPIPE:			/* stall - probably underrun */
1247
			dev_err(cs->dev, "isoc write: stalled\n");
1248 1249
			error_hangup(bcs);
			break;
1250 1251
		default:			/* other errors */
			dev_warn(cs->dev, "isoc write: %s\n",
1252
				 get_usb_statmsg(status));
1253 1254 1255 1256
		}

		/* mark the write buffer area covered by this URB as free */
		if (done->limit >= 0)
T
Tilman Schmidt 已提交
1257
			ubc->isooutbuf->read = done->limit;
1258 1259 1260 1261 1262 1263 1264 1265

		/* 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 */
1266 1267
			rc = submit_iso_write_urb(next);
			if (unlikely(rc <= 0 && rc != -ENODEV)) {
1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280
				/* 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);
1281 1282
			gig_dbg(DEBUG_ISO, "%s: skb requeued, qlen=%d",
				__func__, skb_queue_len(&bcs->squeue));
1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301
			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)
{
1302 1303 1304
	struct bc_state *bcs = (struct bc_state *) data;
	struct bas_bc_state *ubc = bcs->hw.bas;
	struct cardstate *cs = bcs->cs;
1305
	struct urb *urb;
1306
	int status;
1307
	struct usb_iso_packet_descriptor *ifd;
1308 1309 1310 1311 1312 1313 1314 1315
	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);
T
Tilman Schmidt 已提交
1316 1317
		urb = ubc->isoindone;
		if (!urb) {
1318 1319 1320
			spin_unlock_irqrestore(&ubc->isoinlock, flags);
			return;
		}
1321
		status = ubc->isoinstatus;
1322 1323
		ubc->isoindone = NULL;
		if (unlikely(ubc->loststatus != -EINPROGRESS)) {
1324
			dev_warn(cs->dev,
1325
				 "isoc read overrun, URB dropped (status: %s, %d bytes)\n",
1326 1327
				 get_usb_statmsg(ubc->loststatus),
				 ubc->isoinlost);
1328 1329 1330 1331
			ubc->loststatus = -EINPROGRESS;
		}
		spin_unlock_irqrestore(&ubc->isoinlock, flags);

T
Tilman Schmidt 已提交
1332
		if (unlikely(!(ubc->running))) {
1333 1334 1335
			gig_dbg(DEBUG_ISO,
				"%s: channel not running, "
				"dropped URB with status: %s",
1336
				__func__, get_usb_statmsg(status));
1337 1338 1339
			return;
		}

1340
		switch (status) {
1341 1342
		case 0:				/* normal completion */
			break;
1343 1344
		case -EXDEV:			/* inspect individual frames
						   (we do that anyway) */
1345 1346
			gig_dbg(DEBUG_ISO, "%s: URB partially completed",
				__func__);
1347 1348 1349
			break;
		case -ENOENT:
		case -ECONNRESET:
1350 1351
		case -EINPROGRESS:
			gig_dbg(DEBUG_ISO, "%s: %s",
1352
				__func__, get_usb_statmsg(status));
1353 1354
			continue;		/* -> skip */
		case -EPIPE:
1355
			dev_err(cs->dev, "isoc read: stalled\n");
1356 1357
			error_hangup(bcs);
			continue;		/* -> skip */
1358 1359
		default:			/* other error */
			dev_warn(cs->dev, "isoc read: %s\n",
1360
				 get_usb_statmsg(status));
1361 1362 1363 1364 1365 1366
			goto error;
		}

		rcvbuf = urb->transfer_buffer;
		totleft = urb->actual_length;
		for (frame = 0; totleft > 0 && frame < BAS_NUMFRAMES; frame++) {
1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382
			ifd = &urb->iso_frame_desc[frame];
			numbytes = ifd->actual_length;
			switch (ifd->status) {
			case 0:			/* success */
				break;
			case -EPROTO:		/* protocol error or unplug */
			case -EILSEQ:
			case -ETIME:
				/* probably just disconnected, ignore */
				gig_dbg(DEBUG_ISO,
					"isoc read: frame %d[%d]: %s\n",
					frame, numbytes,
					get_usb_statmsg(ifd->status));
				break;
			default:		/* other error */
				/* report, assume transferred bytes are ok */
1383
				dev_warn(cs->dev,
1384
					 "isoc read: frame %d[%d]: %s\n",
1385
					 frame, numbytes,
1386 1387
					 get_usb_statmsg(ifd->status));
			}
1388
			if (unlikely(numbytes > BAS_MAXFRAME))
1389
				dev_warn(cs->dev,
1390 1391 1392
					 "isoc read: frame %d[%d]: %s\n",
					 frame, numbytes,
					 "exceeds max frame size");
1393
			if (unlikely(numbytes > totleft)) {
1394
				dev_warn(cs->dev,
1395 1396 1397
					 "isoc read: frame %d[%d]: %s\n",
					 frame, numbytes,
					 "exceeds total transfer length");
1398
				numbytes = totleft;
1399
			}
1400
			offset = ifd->offset;
1401
			if (unlikely(offset + numbytes > BAS_INBUFSIZE)) {
1402
				dev_warn(cs->dev,
1403 1404 1405
					 "isoc read: frame %d[%d]: %s\n",
					 frame, numbytes,
					 "exceeds end of buffer");
1406
				numbytes = BAS_INBUFSIZE - offset;
1407 1408 1409 1410 1411
			}
			gigaset_isoc_receive(rcvbuf + offset, numbytes, bcs);
			totleft -= numbytes;
		}
		if (unlikely(totleft > 0))
1412
			dev_warn(cs->dev, "isoc read: %d data bytes missing\n",
1413
				 totleft);
1414

T
Tilman Schmidt 已提交
1415
error:
1416 1417 1418 1419 1420
		/* 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;
		}
1421 1422
		/* urb->dev is clobbered by USB subsystem */
		urb->dev = bcs->cs->hw.bas->udev;
1423 1424
		urb->transfer_flags = URB_ISO_ASAP;
		urb->number_of_packets = BAS_NUMFRAMES;
1425
		rc = usb_submit_urb(urb, GFP_ATOMIC);
1426
		if (unlikely(rc != 0 && rc != -ENODEV)) {
1427
			dev_err(cs->dev,
1428
				"could not resubmit isoc read URB: %s\n",
1429
				get_usb_rcmsg(rc));
1430
			dump_urb(DEBUG_ISO, "resubmit isoc read", urb);
1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441
			error_hangup(bcs);
		}
	}
}

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

/* req_timeout
 * timeout routine for control output request
 * argument:
1442
 *	controller state structure
1443 1444 1445
 */
static void req_timeout(unsigned long data)
{
1446 1447
	struct cardstate *cs = (struct cardstate *) data;
	struct bas_cardstate *ucs = cs->hw.bas;
1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459
	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 */
1460
		gig_dbg(DEBUG_USBREQ, "%s: no request pending", __func__);
1461 1462 1463
		break;

	case HD_OPEN_ATCHANNEL:
1464 1465
		dev_err(cs->dev, "timeout opening AT channel\n");
		error_reset(cs);
1466 1467 1468
		break;

	case HD_OPEN_B1CHANNEL:
1469 1470 1471 1472 1473 1474 1475
		dev_err(cs->dev, "timeout opening channel 1\n");
		error_hangup(&cs->bcs[0]);
		break;

	case HD_OPEN_B2CHANNEL:
		dev_err(cs->dev, "timeout opening channel 2\n");
		error_hangup(&cs->bcs[1]);
1476 1477 1478
		break;

	case HD_CLOSE_ATCHANNEL:
1479 1480
		dev_err(cs->dev, "timeout closing AT channel\n");
		error_reset(cs);
1481 1482 1483
		break;

	case HD_CLOSE_B1CHANNEL:
1484 1485 1486 1487 1488 1489 1490
		dev_err(cs->dev, "timeout closing channel 1\n");
		error_reset(cs);
		break;

	case HD_CLOSE_B2CHANNEL:
		dev_err(cs->dev, "timeout closing channel 2\n");
		error_reset(cs);
1491 1492
		break;

T
Tilman Schmidt 已提交
1493 1494
	case HD_RESET_INTERRUPT_PIPE:
		/* error recovery escalation */
1495
		dev_err(cs->dev,
T
Tilman Schmidt 已提交
1496
			"reset interrupt pipe timeout, attempting USB reset\n");
1497
		usb_queue_reset_device(ucs->interface);
T
Tilman Schmidt 已提交
1498 1499
		break;

1500
	default:
1501
		dev_warn(cs->dev, "request 0x%02x timed out, clearing\n",
1502
			 pending);
1503
	}
T
Tilman Schmidt 已提交
1504 1505

	wake_up(&ucs->waitqueue);
1506 1507 1508 1509 1510 1511 1512 1513 1514
}

/* 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
 */
1515
static void write_ctrl_callback(struct urb *urb)
1516
{
1517
	struct bas_cardstate *ucs = urb->context;
1518
	int status = urb->status;
1519
	int rc;
1520 1521
	unsigned long flags;

1522
	/* check status */
1523
	switch (status) {
1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541
	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",
1542
			__func__, get_usb_statmsg(status));
1543
		break;
1544 1545

	default:				/* any failure */
T
Tilman Schmidt 已提交
1546 1547
		/* don't retry if suspend requested */
		if (++ucs->retry_ctrl > BAS_RETRY ||
T
Tilman Schmidt 已提交
1548
		    (ucs->basstate & BS_SUSPEND)) {
1549 1550 1551
			dev_err(&ucs->interface->dev,
				"control request 0x%02x failed: %s\n",
				ucs->dr_ctrl.bRequest,
1552
				get_usb_statmsg(status));
1553 1554 1555 1556
			break;		/* give up */
		}
		dev_notice(&ucs->interface->dev,
			   "control request 0x%02x: %s, retry %d\n",
1557
			   ucs->dr_ctrl.bRequest, get_usb_statmsg(status),
1558 1559 1560
			   ucs->retry_ctrl);
		/* urb->dev is clobbered by USB subsystem */
		urb->dev = ucs->udev;
1561
		rc = usb_submit_urb(urb, GFP_ATOMIC);
1562 1563 1564 1565 1566 1567 1568 1569
		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;
1570
	}
1571 1572 1573 1574 1575

	/* failed, clear pending request */
	spin_lock_irqsave(&ucs->lock, flags);
	del_timer(&ucs->timer_ctrl);
	ucs->pending = 0;
1576
	spin_unlock_irqrestore(&ucs->lock, flags);
T
Tilman Schmidt 已提交
1577
	wake_up(&ucs->waitqueue);
1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594
}

/* 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)
{
1595
	struct bas_cardstate *ucs = bcs->cs->hw.bas;
1596 1597 1598
	int ret;
	unsigned long flags;

1599
	gig_dbg(DEBUG_USBREQ, "-------> 0x%02x (%d)", req, val);
1600 1601 1602 1603

	spin_lock_irqsave(&ucs->lock, flags);
	if (ucs->pending) {
		spin_unlock_irqrestore(&ucs->lock, flags);
1604 1605 1606 1607
		dev_err(bcs->cs->dev,
			"submission of request 0x%02x failed: "
			"request 0x%02x still pending\n",
			req, ucs->pending);
1608 1609 1610 1611 1612 1613 1614 1615 1616
		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,
1617
			     usb_sndctrlpipe(ucs->udev, 0),
T
Tilman Schmidt 已提交
1618
			     (unsigned char *) &ucs->dr_ctrl, NULL, 0,
1619
			     write_ctrl_callback, ucs);
1620
	ucs->retry_ctrl = 0;
1621
	ret = usb_submit_urb(ucs->urb_ctrl, GFP_ATOMIC);
1622
	if (unlikely(ret)) {
1623
		dev_err(bcs->cs->dev, "could not submit request 0x%02x: %s\n",
1624
			req, get_usb_rcmsg(ret));
1625 1626 1627 1628 1629 1630
		spin_unlock_irqrestore(&ucs->lock, flags);
		return ret;
	}
	ucs->pending = req;

	if (timeout > 0) {
1631
		gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
1632
		mod_timer(&ucs->timer_ctrl, jiffies + timeout * HZ / 10);
1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648
	}

	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)
{
T
Tilman Schmidt 已提交
1649
	struct cardstate *cs = bcs->cs;
1650
	int req, ret;
1651 1652
	unsigned long flags;

T
Tilman Schmidt 已提交
1653 1654
	spin_lock_irqsave(&cs->lock, flags);
	if (unlikely(!cs->connected)) {
1655
		gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
T
Tilman Schmidt 已提交
1656
		spin_unlock_irqrestore(&cs->lock, flags);
1657 1658
		return -ENODEV;
	}
1659

T
Tilman Schmidt 已提交
1660
	if (cs->hw.bas->basstate & BS_SUSPEND) {
T
Tilman Schmidt 已提交
1661
		dev_notice(cs->dev,
1662
			   "not starting isoc I/O, suspend in progress\n");
T
Tilman Schmidt 已提交
1663 1664 1665 1666
		spin_unlock_irqrestore(&cs->lock, flags);
		return -EHOSTUNREACH;
	}

T
Tilman Schmidt 已提交
1667 1668
	ret = starturbs(bcs);
	if (ret < 0) {
1669
		spin_unlock_irqrestore(&cs->lock, flags);
T
Tilman Schmidt 已提交
1670
		dev_err(cs->dev,
1671
			"could not start isoc I/O for channel B%d: %s\n",
1672 1673 1674 1675
			bcs->channel + 1,
			ret == -EFAULT ? "null URB" : get_usb_rcmsg(ret));
		if (ret != -ENODEV)
			error_hangup(bcs);
1676 1677 1678 1679
		return ret;
	}

	req = bcs->channel ? HD_OPEN_B2CHANNEL : HD_OPEN_B1CHANNEL;
T
Tilman Schmidt 已提交
1680 1681
	ret = req_submit(bcs, req, 0, BAS_TIMEOUT);
	if (ret < 0) {
T
Tilman Schmidt 已提交
1682
		dev_err(cs->dev, "could not open channel B%d\n",
1683
			bcs->channel + 1);
1684 1685
		stopurbs(bcs->hw.bas);
	}
1686

T
Tilman Schmidt 已提交
1687
	spin_unlock_irqrestore(&cs->lock, flags);
1688 1689
	if (ret < 0 && ret != -ENODEV)
		error_hangup(bcs);
1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704
	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)
{
T
Tilman Schmidt 已提交
1705
	struct cardstate *cs = bcs->cs;
1706
	int req, ret;
1707 1708
	unsigned long flags;

T
Tilman Schmidt 已提交
1709 1710 1711
	spin_lock_irqsave(&cs->lock, flags);
	if (unlikely(!cs->connected)) {
		spin_unlock_irqrestore(&cs->lock, flags);
1712 1713 1714
		gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
		return -ENODEV;
	}
1715

T
Tilman Schmidt 已提交
1716
	if (!(cs->hw.bas->basstate & (bcs->channel ? BS_B2OPEN : BS_B1OPEN))) {
1717
		/* channel not running: just signal common.c */
T
Tilman Schmidt 已提交
1718
		spin_unlock_irqrestore(&cs->lock, flags);
1719 1720 1721 1722
		gigaset_bchannel_down(bcs);
		return 0;
	}

1723
	/* channel running: tell device to close it */
1724
	req = bcs->channel ? HD_CLOSE_B2CHANNEL : HD_CLOSE_B1CHANNEL;
T
Tilman Schmidt 已提交
1725 1726
	ret = req_submit(bcs, req, 0, BAS_TIMEOUT);
	if (ret < 0)
T
Tilman Schmidt 已提交
1727
		dev_err(cs->dev, "closing channel B%d failed\n",
1728 1729
			bcs->channel + 1);

T
Tilman Schmidt 已提交
1730
	spin_unlock_irqrestore(&cs->lock, flags);
1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744
	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)
{
1745
	struct cmdbuf_t *cb = cs->cmdbuf;
1746 1747 1748

	/* unqueue completed buffer */
	cs->cmdbytes -= cs->curlen;
T
Tilman Schmidt 已提交
1749
	gig_dbg(DEBUG_OUTPUT, "write_command: sent %u bytes, %u left",
1750
		cs->curlen, cs->cmdbytes);
T
Tilman Schmidt 已提交
1751 1752
	if (cb->next != NULL) {
		cs->cmdbuf = cb->next;
1753 1754 1755
		cs->cmdbuf->prev = NULL;
		cs->curlen = cs->cmdbuf->len;
	} else {
T
Tilman Schmidt 已提交
1756
		cs->cmdbuf = NULL;
1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773
		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
 */
1774
static void write_command_callback(struct urb *urb)
1775
{
1776 1777
	struct cardstate *cs = urb->context;
	struct bas_cardstate *ucs = cs->hw.bas;
1778
	int status = urb->status;
1779 1780
	unsigned long flags;

1781
	update_basstate(ucs, 0, BS_ATWRPEND);
T
Tilman Schmidt 已提交
1782
	wake_up(&ucs->waitqueue);
1783

1784
	/* check status */
1785
	switch (status) {
1786 1787
	case 0:					/* normal completion */
		break;
1788 1789
	case -ENOENT:			/* cancelled */
	case -ECONNRESET:		/* cancelled (async) */
1790
	case -EINPROGRESS:		/* pending */
1791 1792
	case -ENODEV:			/* device removed */
	case -ESHUTDOWN:		/* device shut down */
1793
		/* ignore silently */
1794
		gig_dbg(DEBUG_USBREQ, "%s: %s",
1795
			__func__, get_usb_statmsg(status));
1796 1797 1798
		return;
	default:				/* any failure */
		if (++ucs->retry_cmd_out > BAS_RETRY) {
1799 1800 1801
			dev_warn(cs->dev,
				 "command write: %s, "
				 "giving up after %d retries\n",
1802
				 get_usb_statmsg(status),
1803
				 ucs->retry_cmd_out);
1804 1805
			break;
		}
T
Tilman Schmidt 已提交
1806
		if (ucs->basstate & BS_SUSPEND) {
T
Tilman Schmidt 已提交
1807 1808 1809 1810 1811 1812
			dev_warn(cs->dev,
				 "command write: %s, "
				 "won't retry - suspend requested\n",
				 get_usb_statmsg(status));
			break;
		}
1813
		if (cs->cmdbuf == NULL) {
1814 1815 1816
			dev_warn(cs->dev,
				 "command write: %s, "
				 "cannot retry - cmdbuf gone\n",
1817
				 get_usb_statmsg(status));
1818 1819
			break;
		}
1820
		dev_notice(cs->dev, "command write: %s, retry %d\n",
1821
			   get_usb_statmsg(status), ucs->retry_cmd_out);
1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842
		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;
1843
	struct bas_cardstate *ucs = cs->hw.bas;
1844

1845
	dev_warn(cs->dev, "timeout waiting for HD_READY_SEND_ATDATA\n");
1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864

	/* 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)
{
1865
	struct bas_cardstate *ucs = cs->hw.bas;
1866
	int rc;
1867

1868
	gig_dbg(DEBUG_USBREQ, "-------> HD_WRITE_ATMESSAGE (%d)", len);
1869

1870
	if (update_basstate(ucs, BS_ATWRPEND, 0) & BS_ATWRPEND) {
1871 1872
		dev_err(cs->dev,
			"could not submit HD_WRITE_ATMESSAGE: URB busy\n");
1873 1874 1875 1876 1877 1878 1879 1880 1881 1882
		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),
T
Tilman Schmidt 已提交
1883
			     (unsigned char *) &ucs->dr_cmd_out, buf, len,
1884
			     write_command_callback, cs);
1885
	rc = usb_submit_urb(ucs->urb_cmd_out, GFP_ATOMIC);
1886 1887
	if (unlikely(rc)) {
		update_basstate(ucs, 0, BS_ATWRPEND);
1888
		dev_err(cs->dev, "could not submit HD_WRITE_ATMESSAGE: %s\n",
1889 1890
			get_usb_rcmsg(rc));
		return rc;
1891 1892
	}

1893 1894
	/* submitted successfully, start timeout if necessary */
	if (!(update_basstate(ucs, BS_ATTIMER, BS_ATREADY) & BS_ATTIMER)) {
1895 1896
		gig_dbg(DEBUG_OUTPUT, "setting ATREADY timeout of %d/10 secs",
			ATRDY_TIMEOUT);
1897
		mod_timer(&ucs->timer_atrdy, jiffies + ATRDY_TIMEOUT * HZ / 10);
1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912
	}
	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;
1913
	struct bas_cardstate *ucs = cs->hw.bas;
1914 1915 1916 1917
	unsigned long flags;
	int rc;
	int retval = 0;

T
Tilman Schmidt 已提交
1918
	/* check if suspend requested */
T
Tilman Schmidt 已提交
1919
	if (ucs->basstate & BS_SUSPEND) {
T
Tilman Schmidt 已提交
1920
		gig_dbg(DEBUG_OUTPUT, "suspending");
T
Tilman Schmidt 已提交
1921 1922 1923
		return -EHOSTUNREACH;
	}

1924
	/* check if AT channel is open */
T
Tilman Schmidt 已提交
1925
	if (!(ucs->basstate & BS_ATOPEN)) {
T
Tilman Schmidt 已提交
1926
		gig_dbg(DEBUG_OUTPUT, "AT channel not open");
1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940
		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);

T
Tilman Schmidt 已提交
1941
	while ((cb = cs->cmdbuf) != NULL && (ucs->basstate & BS_ATREADY)) {
1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962
		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
1963
 *	cb		command buffer structure
1964 1965 1966 1967
 * return value:
 *	number of bytes queued on success
 *	error code < 0 on error
 */
1968
static int gigaset_write_cmd(struct cardstate *cs, struct cmdbuf_t *cb)
1969 1970
{
	unsigned long flags;
1971
	int rc;
1972

T
Tilman Schmidt 已提交
1973
	gigaset_dbg_buffer(cs->mstate != MS_LOCKED ?
1974
			   DEBUG_TRANSCMD : DEBUG_LOCKCMD,
1975
			   "CMD Transmit", cb->len, cb->buf);
1976

T
Tilman Schmidt 已提交
1977 1978 1979 1980
	/* translate "+++" escape sequence sent as a single separate command
	 * into "close AT channel" command for error recovery
	 * The next command will reopen the AT channel automatically.
	 */
1981
	if (cb->len == 3 && !memcmp(cb->buf, "+++", 3)) {
1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003
		/* If an HD_RECEIVEATDATA_ACK message remains unhandled
		 * because of an error, the base never sends another one.
		 * The response channel is thus effectively blocked.
		 * Closing and reopening the AT channel does *not* clear
		 * this condition.
		 * As a stopgap measure, submit a zero-length AT read
		 * before closing the AT channel. This has the undocumented
		 * effect of triggering a new HD_RECEIVEATDATA_ACK message
		 * from the base if necessary.
		 * The subsequent AT channel close then discards any pending
		 * messages.
		 */
		spin_lock_irqsave(&cs->lock, flags);
		if (!(cs->hw.bas->basstate & BS_ATRDPEND)) {
			kfree(cs->hw.bas->rcvbuf);
			cs->hw.bas->rcvbuf = NULL;
			cs->hw.bas->rcvbuf_size = 0;
			cs->hw.bas->retry_cmd_in = 0;
			atread_submit(cs, 0);
		}
		spin_unlock_irqrestore(&cs->lock, flags);

T
Tilman Schmidt 已提交
2004
		rc = req_submit(cs->bcs, HD_CLOSE_ATCHANNEL, 0, BAS_TIMEOUT);
2005 2006
		if (cb->wake_tasklet)
			tasklet_schedule(cb->wake_tasklet);
D
Dan Carpenter 已提交
2007 2008 2009 2010
		if (!rc)
			rc = cb->len;
		kfree(cb);
		return rc;
2011 2012 2013 2014 2015 2016 2017 2018
	}

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

2025 2026 2027 2028
	spin_lock_irqsave(&cs->lock, flags);
	if (unlikely(!cs->connected)) {
		spin_unlock_irqrestore(&cs->lock, flags);
		gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
2029 2030 2031 2032 2033
		/* flush command queue */
		spin_lock_irqsave(&cs->cmdlock, flags);
		while (cs->cmdbuf != NULL)
			complete_cb(cs);
		spin_unlock_irqrestore(&cs->cmdlock, flags);
2034 2035
		return -ENODEV;
	}
2036
	rc = start_cbsend(cs);
2037
	spin_unlock_irqrestore(&cs->lock, flags);
2038
	return rc < 0 ? rc : cb->len;
2039 2040 2041 2042
}

/* gigaset_write_room
 * tty_driver.write_room interface routine
2043 2044
 * return number of characters the driver will accept to be written via
 * gigaset_write_cmd
2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064
 * 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)
{
T
Tilman Schmidt 已提交
2065
	return cs->cmdbytes;
2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087
}

/* 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
 */
2088
static void gigaset_freebcshw(struct bc_state *bcs)
2089
{
2090 2091 2092 2093
	struct bas_bc_state *ubc = bcs->hw.bas;
	int i;

	if (!ubc)
2094
		return;
2095

2096
	/* kill URBs and tasklets before freeing - better safe than sorry */
T
Tilman Schmidt 已提交
2097
	ubc->running = 0;
2098
	gig_dbg(DEBUG_INIT, "%s: killing isoc URBs", __func__);
2099 2100 2101 2102 2103 2104 2105 2106
	for (i = 0; i < BAS_OUTURBS; ++i) {
		usb_kill_urb(ubc->isoouturbs[i].urb);
		usb_free_urb(ubc->isoouturbs[i].urb);
	}
	for (i = 0; i < BAS_INURBS; ++i) {
		usb_kill_urb(ubc->isoinurbs[i]);
		usb_free_urb(ubc->isoinurbs[i]);
	}
2107 2108 2109 2110
	tasklet_kill(&ubc->sent_tasklet);
	tasklet_kill(&ubc->rcvd_tasklet);
	kfree(ubc->isooutbuf);
	kfree(ubc);
2111 2112 2113 2114 2115 2116 2117
	bcs->hw.bas = NULL;
}

/* Initialize hardware dependent part of the B channel structure
 * parameter:
 *	bcs	B channel structure
 * return value:
2118
 *	0 on success, error code < 0 on failure
2119 2120 2121 2122 2123 2124 2125 2126
 */
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) {
2127
		pr_err("out of memory\n");
2128
		return -ENOMEM;
2129 2130
	}

T
Tilman Schmidt 已提交
2131
	ubc->running = 0;
2132 2133 2134 2135 2136 2137 2138 2139
	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;
T
Tilman Schmidt 已提交
2140 2141
	ubc->isooutbuf = kmalloc(sizeof(struct isowbuf_t), GFP_KERNEL);
	if (!ubc->isooutbuf) {
2142
		pr_err("out of memory\n");
2143 2144
		kfree(ubc);
		bcs->hw.bas = NULL;
2145
		return -ENOMEM;
2146 2147
	}
	tasklet_init(&ubc->sent_tasklet,
2148
		     write_iso_tasklet, (unsigned long) bcs);
2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168

	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,
2169
		     read_iso_tasklet, (unsigned long) bcs);
2170
	return 0;
2171 2172 2173 2174 2175 2176
}

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

T
Tilman Schmidt 已提交
2177
	bcs->hw.bas->running = 0;
2178 2179 2180 2181 2182 2183 2184 2185 2186
	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)
{
2187
	/* timers, URBs and rcvbuf are disposed of in disconnect */
2188
	kfree(cs->hw.bas->int_in_buf);
2189
	kfree(cs->hw.bas);
2190
	cs->hw.bas = NULL;
2191 2192
}

2193 2194 2195 2196 2197 2198
/* Initialize hardware dependent part of the cardstate structure
 * parameter:
 *	cs	cardstate structure
 * return value:
 *	0 on success, error code < 0 on failure
 */
2199 2200 2201 2202 2203
static int gigaset_initcshw(struct cardstate *cs)
{
	struct bas_cardstate *ucs;

	cs->hw.bas = ucs = kmalloc(sizeof *ucs, GFP_KERNEL);
2204 2205
	if (!ucs) {
		pr_err("out of memory\n");
2206
		return -ENOMEM;
2207
	}
2208 2209 2210 2211
	ucs->int_in_buf = kmalloc(IP_MSGSIZE, GFP_KERNEL);
	if (!ucs->int_in_buf) {
		kfree(ucs);
		pr_err("out of memory\n");
2212
		return -ENOMEM;
2213
	}
2214 2215 2216 2217 2218 2219 2220 2221 2222

	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;

T
Tilman Schmidt 已提交
2223
	ucs->basstate = 0;
2224 2225 2226
	setup_timer(&ucs->timer_ctrl, req_timeout, (unsigned long) cs);
	setup_timer(&ucs->timer_atrdy, atrdy_timeout, (unsigned long) cs);
	setup_timer(&ucs->timer_cmd_in, cmd_in_timeout, (unsigned long) cs);
2227
	setup_timer(&ucs->timer_int_in, int_in_resubmit, (unsigned long) cs);
T
Tilman Schmidt 已提交
2228
	init_waitqueue_head(&ucs->waitqueue);
2229
	INIT_WORK(&ucs->int_in_wq, int_in_work);
2230

2231
	return 0;
2232 2233 2234 2235 2236 2237 2238 2239 2240 2241
}

/* 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)
{
2242
	struct bas_cardstate *ucs = cs->hw.bas;
2243 2244 2245
	struct bas_bc_state *ubc;
	int i, j;

2246
	gig_dbg(DEBUG_INIT, "%s: killing URBs", __func__);
T
Tilman Schmidt 已提交
2247
	for (j = 0; j < BAS_CHANNELS; ++j) {
2248
		ubc = cs->bcs[j].hw.bas;
2249 2250 2251 2252 2253 2254 2255 2256 2257 2258
		for (i = 0; i < BAS_OUTURBS; ++i) {
			usb_kill_urb(ubc->isoouturbs[i].urb);
			usb_free_urb(ubc->isoouturbs[i].urb);
			ubc->isoouturbs[i].urb = NULL;
		}
		for (i = 0; i < BAS_INURBS; ++i) {
			usb_kill_urb(ubc->isoinurbs[i]);
			usb_free_urb(ubc->isoinurbs[i]);
			ubc->isoinurbs[i] = NULL;
		}
2259
	}
2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271
	usb_kill_urb(ucs->urb_int_in);
	usb_free_urb(ucs->urb_int_in);
	ucs->urb_int_in = NULL;
	usb_kill_urb(ucs->urb_cmd_out);
	usb_free_urb(ucs->urb_cmd_out);
	ucs->urb_cmd_out = NULL;
	usb_kill_urb(ucs->urb_cmd_in);
	usb_free_urb(ucs->urb_cmd_in);
	ucs->urb_cmd_in = NULL;
	usb_kill_urb(ucs->urb_ctrl);
	usb_free_urb(ucs->urb_ctrl);
	ucs->urb_ctrl = NULL;
2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287
}

/* 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;
2288
	int rc;
2289

T
Tilman Schmidt 已提交
2290
	gig_dbg(DEBUG_INIT,
2291 2292 2293
		"%s: Check if device matches .. (Vendor: 0x%x, Product: 0x%x)",
		__func__, le16_to_cpu(udev->descriptor.idVendor),
		le16_to_cpu(udev->descriptor.idProduct));
2294 2295 2296 2297

	/* set required alternate setting */
	hostif = interface->cur_altsetting;
	if (hostif->desc.bAlternateSetting != 3) {
T
Tilman Schmidt 已提交
2298
		gig_dbg(DEBUG_INIT,
2299 2300
			"%s: wrong alternate setting %d - trying to switch",
			__func__, hostif->desc.bAlternateSetting);
T
Tilman Schmidt 已提交
2301 2302
		if (usb_set_interface(udev, hostif->desc.bInterfaceNumber, 3)
		    < 0) {
2303 2304 2305 2306
			dev_warn(&udev->dev, "usb_set_interface failed, "
				 "device %d interface %d altsetting %d\n",
				 udev->devnum, hostif->desc.bInterfaceNumber,
				 hostif->desc.bAlternateSetting);
2307 2308 2309 2310 2311 2312 2313 2314
			return -ENODEV;
		}
		hostif = interface->cur_altsetting;
	}

	/* Reject application specific interfaces
	 */
	if (hostif->desc.bInterfaceClass != 255) {
2315 2316
		dev_warn(&udev->dev, "%s: bInterfaceClass == %d\n",
			 __func__, hostif->desc.bInterfaceClass);
2317 2318 2319
		return -ENODEV;
	}

2320 2321 2322
	if (hostif->desc.bNumEndpoints < 1)
		return -ENODEV;

2323 2324 2325 2326
	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));
2327

2328
	/* allocate memory for our device state and initialize it */
T
Tilman Schmidt 已提交
2329 2330 2331
	cs = gigaset_initcs(driver, BAS_CHANNELS, 0, 0, cidmode,
			    GIGASET_MODULENAME);
	if (!cs)
2332 2333
		return -ENODEV;
	ucs = cs->hw.bas;
2334 2335 2336

	/* save off device structure ptrs for later use */
	usb_get_dev(udev);
2337 2338
	ucs->udev = udev;
	ucs->interface = interface;
2339
	cs->dev = &interface->dev;
2340 2341 2342 2343 2344 2345

	/* allocate URBs:
	 * - one for the interrupt pipe
	 * - three for the different uses of the default control pipe
	 * - three for each isochronous pipe
	 */
2346 2347 2348 2349
	if (!(ucs->urb_int_in = usb_alloc_urb(0, GFP_KERNEL)) ||
	    !(ucs->urb_cmd_in = usb_alloc_urb(0, GFP_KERNEL)) ||
	    !(ucs->urb_cmd_out = usb_alloc_urb(0, GFP_KERNEL)) ||
	    !(ucs->urb_ctrl = usb_alloc_urb(0, GFP_KERNEL)))
2350
		goto allocerr;
2351

T
Tilman Schmidt 已提交
2352
	for (j = 0; j < BAS_CHANNELS; ++j) {
2353
		ubc = cs->bcs[j].hw.bas;
2354 2355
		for (i = 0; i < BAS_OUTURBS; ++i)
			if (!(ubc->isoouturbs[i].urb =
2356
			      usb_alloc_urb(BAS_NUMFRAMES, GFP_KERNEL)))
2357 2358 2359
				goto allocerr;
		for (i = 0; i < BAS_INURBS; ++i)
			if (!(ubc->isoinurbs[i] =
2360
			      usb_alloc_urb(BAS_NUMFRAMES, GFP_KERNEL)))
2361
				goto allocerr;
2362 2363 2364 2365 2366 2367 2368 2369
	}

	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,
2370
			 usb_rcvintpipe(udev,
2371
					usb_endpoint_num(endpoint)),
2372
			 ucs->int_in_buf, IP_MSGSIZE, read_int_callback, cs,
2373
			 endpoint->bInterval);
T
Tilman Schmidt 已提交
2374 2375
	rc = usb_submit_urb(ucs->urb_int_in, GFP_KERNEL);
	if (rc != 0) {
2376
		dev_err(cs->dev, "could not submit interrupt URB: %s\n",
2377
			get_usb_rcmsg(rc));
2378 2379
		goto error;
	}
2380
	ucs->retry_int_in = 0;
2381 2382

	/* tell the device that the driver is ready */
T
Tilman Schmidt 已提交
2383 2384
	rc = req_submit(cs->bcs, HD_DEVICE_INIT_ACK, 0, 0);
	if (rc != 0)
2385 2386 2387 2388
		goto error;

	/* tell common part that the device is ready */
	if (startmode == SM_LOCKED)
T
Tilman Schmidt 已提交
2389
		cs->mstate = MS_LOCKED;
2390 2391 2392 2393

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

2394 2395
	rc = gigaset_start(cs);
	if (rc < 0)
2396 2397
		goto error;

2398 2399
	return 0;

2400 2401
allocerr:
	dev_err(cs->dev, "could not allocate URBs\n");
2402
	rc = -ENOMEM;
2403 2404
error:
	freeurbs(cs);
2405
	usb_set_intfdata(interface, NULL);
2406
	usb_put_dev(udev);
T
Tilman Schmidt 已提交
2407
	gigaset_freecs(cs);
2408
	return rc;
2409 2410 2411 2412 2413 2414 2415 2416 2417
}

/* 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;
2418
	int j;
2419 2420 2421 2422 2423

	cs = usb_get_intfdata(interface);

	ucs = cs->hw.bas;

2424
	dev_info(cs->dev, "disconnecting Gigaset base\n");
2425 2426

	/* mark base as not ready, all channels disconnected */
T
Tilman Schmidt 已提交
2427
	ucs->basstate = 0;
2428 2429

	/* tell LL all channels are down */
T
Tilman Schmidt 已提交
2430
	for (j = 0; j < BAS_CHANNELS; ++j)
2431 2432 2433
		gigaset_bchannel_down(cs->bcs + j);

	/* stop driver (common part) */
2434
	gigaset_stop(cs);
2435

2436
	/* stop delayed work and URBs, free ressources */
2437 2438 2439
	del_timer_sync(&ucs->timer_ctrl);
	del_timer_sync(&ucs->timer_atrdy);
	del_timer_sync(&ucs->timer_cmd_in);
2440 2441
	del_timer_sync(&ucs->timer_int_in);
	cancel_work_sync(&ucs->int_in_wq);
2442
	freeurbs(cs);
2443
	usb_set_intfdata(interface, NULL);
2444 2445 2446
	kfree(ucs->rcvbuf);
	ucs->rcvbuf = NULL;
	ucs->rcvbuf_size = 0;
2447 2448 2449 2450
	usb_put_dev(ucs->udev);
	ucs->interface = NULL;
	ucs->udev = NULL;
	cs->dev = NULL;
T
Tilman Schmidt 已提交
2451
	gigaset_freecs(cs);
2452 2453
}

T
Tilman Schmidt 已提交
2454
/* gigaset_suspend
2455 2456 2457
 * This function is called before the USB connection is suspended
 * or before the USB device is reset.
 * In the latter case, message == PMSG_ON.
T
Tilman Schmidt 已提交
2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472
 */
static int gigaset_suspend(struct usb_interface *intf, pm_message_t message)
{
	struct cardstate *cs = usb_get_intfdata(intf);
	struct bas_cardstate *ucs = cs->hw.bas;
	int rc;

	/* set suspend flag; this stops AT command/response traffic */
	if (update_basstate(ucs, BS_SUSPEND, 0) & BS_SUSPEND) {
		gig_dbg(DEBUG_SUSPEND, "already suspended");
		return 0;
	}

	/* wait a bit for blocking conditions to go away */
	rc = wait_event_timeout(ucs->waitqueue,
2473 2474 2475
				!(ucs->basstate &
				  (BS_B1OPEN | BS_B2OPEN | BS_ATRDPEND | BS_ATWRPEND)),
				BAS_TIMEOUT * HZ / 10);
T
Tilman Schmidt 已提交
2476 2477 2478
	gig_dbg(DEBUG_SUSPEND, "wait_event_timeout() -> %d", rc);

	/* check for conditions preventing suspend */
2479
	if (ucs->basstate & (BS_B1OPEN | BS_B2OPEN | BS_ATRDPEND | BS_ATWRPEND)) {
T
Tilman Schmidt 已提交
2480
		dev_warn(cs->dev, "cannot suspend:\n");
T
Tilman Schmidt 已提交
2481
		if (ucs->basstate & BS_B1OPEN)
T
Tilman Schmidt 已提交
2482
			dev_warn(cs->dev, " B channel 1 open\n");
T
Tilman Schmidt 已提交
2483
		if (ucs->basstate & BS_B2OPEN)
T
Tilman Schmidt 已提交
2484
			dev_warn(cs->dev, " B channel 2 open\n");
T
Tilman Schmidt 已提交
2485
		if (ucs->basstate & BS_ATRDPEND)
T
Tilman Schmidt 已提交
2486
			dev_warn(cs->dev, " receiving AT reply\n");
T
Tilman Schmidt 已提交
2487
		if (ucs->basstate & BS_ATWRPEND)
T
Tilman Schmidt 已提交
2488 2489 2490 2491 2492 2493
			dev_warn(cs->dev, " sending AT command\n");
		update_basstate(ucs, 0, BS_SUSPEND);
		return -EBUSY;
	}

	/* close AT channel if open */
T
Tilman Schmidt 已提交
2494
	if (ucs->basstate & BS_ATOPEN) {
T
Tilman Schmidt 已提交
2495 2496 2497 2498 2499 2500 2501
		gig_dbg(DEBUG_SUSPEND, "closing AT channel");
		rc = req_submit(cs->bcs, HD_CLOSE_ATCHANNEL, 0, 0);
		if (rc) {
			update_basstate(ucs, 0, BS_SUSPEND);
			return rc;
		}
		wait_event_timeout(ucs->waitqueue, !ucs->pending,
2502
				   BAS_TIMEOUT * HZ / 10);
T
Tilman Schmidt 已提交
2503 2504 2505
		/* in case of timeout, proceed anyway */
	}

2506
	/* kill all URBs and delayed work that might still be pending */
T
Tilman Schmidt 已提交
2507 2508 2509
	usb_kill_urb(ucs->urb_ctrl);
	usb_kill_urb(ucs->urb_int_in);
	del_timer_sync(&ucs->timer_ctrl);
2510 2511
	del_timer_sync(&ucs->timer_atrdy);
	del_timer_sync(&ucs->timer_cmd_in);
2512
	del_timer_sync(&ucs->timer_int_in);
2513 2514 2515 2516 2517 2518

	/* don't try to cancel int_in_wq from within reset as it
	 * might be the one requesting the reset
	 */
	if (message.event != PM_EVENT_ON)
		cancel_work_sync(&ucs->int_in_wq);
T
Tilman Schmidt 已提交
2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539

	gig_dbg(DEBUG_SUSPEND, "suspend complete");
	return 0;
}

/* gigaset_resume
 * This function is called after the USB connection has been resumed.
 */
static int gigaset_resume(struct usb_interface *intf)
{
	struct cardstate *cs = usb_get_intfdata(intf);
	struct bas_cardstate *ucs = cs->hw.bas;
	int rc;

	/* resubmit interrupt URB for spontaneous messages from base */
	rc = usb_submit_urb(ucs->urb_int_in, GFP_KERNEL);
	if (rc) {
		dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
			get_usb_rcmsg(rc));
		return rc;
	}
2540
	ucs->retry_int_in = 0;
T
Tilman Schmidt 已提交
2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569

	/* clear suspend flag to reallow activity */
	update_basstate(ucs, 0, BS_SUSPEND);

	gig_dbg(DEBUG_SUSPEND, "resume complete");
	return 0;
}

/* gigaset_pre_reset
 * This function is called before the USB connection is reset.
 */
static int gigaset_pre_reset(struct usb_interface *intf)
{
	/* handle just like suspend */
	return gigaset_suspend(intf, PMSG_ON);
}

/* gigaset_post_reset
 * This function is called after the USB connection has been reset.
 */
static int gigaset_post_reset(struct usb_interface *intf)
{
	/* FIXME: send HD_DEVICE_INIT_ACK? */

	/* resume operations */
	return gigaset_resume(intf);
}


2570
static const struct gigaset_ops gigops = {
2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586
	.write_cmd = gigaset_write_cmd,
	.write_room = gigaset_write_room,
	.chars_in_buffer = gigaset_chars_in_buffer,
	.brkchars = gigaset_brkchars,
	.init_bchannel = gigaset_init_bchannel,
	.close_bchannel = gigaset_close_bchannel,
	.initbcshw = gigaset_initbcshw,
	.freebcshw = gigaset_freebcshw,
	.reinitbcshw = gigaset_reinitbcshw,
	.initcshw = gigaset_initcshw,
	.freecshw = gigaset_freecshw,
	.set_modem_ctrl = gigaset_set_modem_ctrl,
	.baud_rate = gigaset_baud_rate,
	.set_line_ctrl = gigaset_set_line_ctrl,
	.send_skb = gigaset_isoc_send_skb,
	.handle_input = gigaset_isoc_input,
2587 2588 2589 2590 2591 2592 2593 2594 2595
};

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

2596
	/* allocate memory for our driver state and initialize it */
T
Tilman Schmidt 已提交
2597 2598 2599 2600
	driver = gigaset_initdriver(GIGASET_MINOR, GIGASET_MINORS,
				    GIGASET_MODULENAME, GIGASET_DEVNAME,
				    &gigops, THIS_MODULE);
	if (driver == NULL)
2601 2602 2603 2604 2605
		goto error;

	/* register this driver with the USB subsystem */
	result = usb_register(&gigaset_usb_driver);
	if (result < 0) {
2606
		pr_err("error %d registering USB driver\n", -result);
2607 2608 2609
		goto error;
	}

2610
	pr_info(DRIVER_DESC "\n");
2611 2612
	return 0;

T
Tilman Schmidt 已提交
2613
error:
2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624
	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)
{
T
Tilman Schmidt 已提交
2625 2626
	struct bas_cardstate *ucs;
	int i;
2627

2628
	gigaset_blockdriver(driver); /* => probe will fail
2629 2630
				      * => no gigaset_start any more
				      */
2631

T
Tilman Schmidt 已提交
2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661
	/* stop all connected devices */
	for (i = 0; i < driver->minors; i++) {
		if (gigaset_shutdown(driver->cs + i) < 0)
			continue;		/* no device */
		/* from now on, no isdn callback should be possible */

		/* close all still open channels */
		ucs = driver->cs[i].hw.bas;
		if (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 (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 (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);
		}
		ucs->basstate = 0;
2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679
	}

	/* 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_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");