bas-gigaset.c 67.5 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 */
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);

Tilman Schmidt's avatar
Tilman Schmidt committed
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

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


135
static struct gigaset_driver *driver;
136 137 138 139 140 141 142

/* 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,
Tilman Schmidt's avatar
Tilman Schmidt committed
143 144 145 146 147
	.suspend =	gigaset_suspend,
	.resume =	gigaset_resume,
	.reset_resume =	gigaset_post_reset,
	.pre_reset =	gigaset_pre_reset,
	.post_reset =	gigaset_post_reset,
148 149
};

150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
/* 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;
	}
}

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

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

318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
/* 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;
}

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

345
	gigaset_add_event(cs, &bcs->at_state, EV_HUP, NULL, 0, NULL);
346 347 348 349 350
	gigaset_schedule_event(cs);
}

/* error_reset
 * reset Gigaset device because of an unrecoverable error
351
 * This function may be called from any context, and takes care of
352
 * scheduling the necessary actions for execution outside of interrupt context.
353
 * cs->lock must not be held.
354 355 356 357 358
 * argument:
 *	controller state structure
 */
static inline void error_reset(struct cardstate *cs)
{
359 360 361
	/* reset interrupt pipe to recover (ignore errors) */
	update_basstate(cs->hw.bas, BS_RESETTING, 0);
	req_submit(cs->bcs, HD_RESET_INTERRUPT_PIPE, 0, BAS_TIMEOUT);
362 363 364 365 366
}

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

	if (!ucs->rcvbuf_size) {
437
		gig_dbg(DEBUG_USBREQ, "%s: no receive in progress", __func__);
438 439 440
		return;
	}

441
	if (ucs->retry_cmd_in++ >= BAS_RETRY) {
442 443 444
		dev_err(cs->dev,
			"control read: timeout, giving up after %d tries\n",
			ucs->retry_cmd_in);
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
		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);
461
	}
462 463
}

464 465 466 467 468 469 470
/* 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
 */
471
static void read_ctrl_callback(struct urb *urb)
472 473 474 475
{
	struct inbuf_t *inbuf = urb->context;
	struct cardstate *cs = inbuf->cs;
	struct bas_cardstate *ucs = cs->hw.bas;
476
	int status = urb->status;
477 478 479 480
	unsigned numbytes;
	int rc;

	update_basstate(ucs, 0, BS_ATRDPEND);
Tilman Schmidt's avatar
Tilman Schmidt committed
481
	wake_up(&ucs->waitqueue);
482 483
	del_timer(&ucs->timer_cmd_in);

484
	switch (status) {
485 486 487 488 489 490 491 492 493 494
	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;
		}

495 496 497 498
		/* 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);
499 500 501 502 503 504 505 506
		}
		break;

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

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

529
	/* read finished, free buffer */
530 531 532 533 534
	kfree(ucs->rcvbuf);
	ucs->rcvbuf = NULL;
	ucs->rcvbuf_size = 0;
}

535
/* atread_submit
536
 * submit an HD_READ_ATMESSAGE command URB and optionally start a timeout
537 538 539 540 541 542 543 544 545 546
 * 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)
{
547
	struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidt's avatar
Tilman Schmidt committed
548
	int basstate;
549 550
	int ret;

551 552
	gig_dbg(DEBUG_USBREQ, "-------> HD_READ_ATMESSAGE (%d)",
		ucs->rcvbuf_size);
553

Tilman Schmidt's avatar
Tilman Schmidt committed
554 555
	basstate = update_basstate(ucs, BS_ATRDPEND, 0);
	if (basstate & BS_ATRDPEND) {
556 557
		dev_err(cs->dev,
			"could not submit HD_READ_ATMESSAGE: URB busy\n");
558 559 560
		return -EBUSY;
	}

Tilman Schmidt's avatar
Tilman Schmidt committed
561 562 563 564 565 566 567 568 569
	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;
	}

570 571 572 573 574 575
	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,
576
			     usb_rcvctrlpipe(ucs->udev, 0),
577
			     (unsigned char *) &ucs->dr_cmd_in,
578 579
			     ucs->rcvbuf, ucs->rcvbuf_size,
			     read_ctrl_callback, cs->inbuf);
580

581 582
	ret = usb_submit_urb(ucs->urb_cmd_in, GFP_ATOMIC);
	if (ret != 0) {
583
		update_basstate(ucs, 0, BS_ATRDPEND);
584
		dev_err(cs->dev, "could not submit HD_READ_ATMESSAGE: %s\n",
585
			get_usb_rcmsg(ret));
586 587 588 589
		return ret;
	}

	if (timeout > 0) {
590
		gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
		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
 */
606
static void read_int_callback(struct urb *urb)
607
{
608 609
	struct cardstate *cs = urb->context;
	struct bas_cardstate *ucs = cs->hw.bas;
610
	struct bc_state *bcs;
611
	int status = urb->status;
612
	unsigned long flags;
613
	int rc;
614 615 616
	unsigned l;
	int channel;

617
	switch (status) {
618 619
	case 0:			/* success */
		break;
620 621
	case -ENOENT:			/* cancelled */
	case -ECONNRESET:		/* cancelled (async) */
622 623
	case -EINPROGRESS:		/* pending */
		/* ignore silently */
624
		gig_dbg(DEBUG_USBREQ, "%s: %s",
625
			__func__, get_usb_statmsg(status));
626
		return;
627 628 629 630
	case -ENODEV:			/* device removed */
	case -ESHUTDOWN:		/* device shut down */
		gig_dbg(DEBUG_USBREQ, "%s: device disconnected", __func__);
		return;
631
	default:		/* severe trouble */
632
		dev_warn(cs->dev, "interrupt read: %s\n",
633
			 get_usb_statmsg(status));
634 635 636
		goto resubmit;
	}

637
	/* drop incomplete packets even if the missing bytes wouldn't matter */
638
	if (unlikely(urb->actual_length < IP_MSGSIZE)) {
639 640 641 642 643
		dev_warn(cs->dev, "incomplete interrupt packet (%d bytes)\n",
			 urb->actual_length);
		goto resubmit;
	}

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

647 648 649
	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]);
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 689 690 691 692 693 694

	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,
695 696 697 698 699
			   &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));
700 701 702 703
		break;

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

	case HD_RESET_INTERRUPT_PIPE_ACK:
741 742
		update_basstate(ucs, 0, BS_RESETTING);
		dev_notice(cs->dev, "interrupt pipe reset\n");
743 744 745
		break;

	case HD_SUSPEND_END:
746
		gig_dbg(DEBUG_USBREQ, "HD_SUSPEND_END");
747 748 749
		break;

	default:
750 751 752
		dev_warn(cs->dev,
			 "unknown Gigaset signal 0x%02x (%u) ignored\n",
			 (int) ucs->int_in_buf[0], l);
753 754 755
	}

	check_pending(ucs);
Tilman Schmidt's avatar
Tilman Schmidt committed
756
	wake_up(&ucs->waitqueue);
757 758

resubmit:
759
	rc = usb_submit_urb(urb, GFP_ATOMIC);
760
	if (unlikely(rc != 0 && rc != -ENODEV)) {
761
		dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
762
			get_usb_rcmsg(rc));
763 764 765 766 767 768 769 770 771 772 773
		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
 */
774
static void read_iso_callback(struct urb *urb)
775 776 777
{
	struct bc_state *bcs;
	struct bas_bc_state *ubc;
778
	int status = urb->status;
779 780 781 782
	unsigned long flags;
	int i, rc;

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

793
	bcs = urb->context;
794 795 796 797 798 799
	ubc = bcs->hw.bas;

	spin_lock_irqsave(&ubc->isoinlock, flags);
	if (likely(ubc->isoindone == NULL)) {
		/* pass URB to tasklet */
		ubc->isoindone = urb;
800
		ubc->isoinstatus = status;
801
		tasklet_hi_schedule(&ubc->rcvd_tasklet);
802 803
	} else {
		/* tasklet still busy, drop data and resubmit URB */
804
		ubc->loststatus = status;
805 806 807
		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 &&
808 809
				     urb->iso_frame_desc[i].status !=
								-EINPROGRESS))
810 811 812 813
				ubc->loststatus = urb->iso_frame_desc[i].status;
			urb->iso_frame_desc[i].status = 0;
			urb->iso_frame_desc[i].actual_length = 0;
		}
Tilman Schmidt's avatar
Tilman Schmidt committed
814
		if (likely(ubc->running)) {
815 816
			/* urb->dev is clobbered by USB subsystem */
			urb->dev = bcs->cs->hw.bas->udev;
817 818
			urb->transfer_flags = URB_ISO_ASAP;
			urb->number_of_packets = BAS_NUMFRAMES;
819 820
			gig_dbg(DEBUG_ISO, "%s: isoc read overrun/resubmit",
				__func__);
821
			rc = usb_submit_urb(urb, GFP_ATOMIC);
822
			if (unlikely(rc != 0 && rc != -ENODEV)) {
823 824
				dev_err(bcs->cs->dev,
					"could not resubmit isochronous read "
825
					"URB: %s\n", get_usb_rcmsg(rc));
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840
				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
 */
841
static void write_iso_callback(struct urb *urb)
842 843 844
{
	struct isow_urbctx_t *ucx;
	struct bas_bc_state *ubc;
845
	int status = urb->status;
846 847 848
	unsigned long flags;

	/* status codes not worth bothering the tasklet with */
849 850 851 852 853
	if (unlikely(status == -ENOENT ||
		     status == -ECONNRESET ||
		     status == -EINPROGRESS ||
		     status == -ENODEV ||
		     status == -ESHUTDOWN)) {
854
		gig_dbg(DEBUG_ISO, "%s: %s",
855
			__func__, get_usb_statmsg(status));
856 857 858 859
		return;
	}

	/* pass URB context to tasklet */
860
	ucx = urb->context;
861
	ubc = ucx->bcs->hw.bas;
862
	ucx->status = status;
863 864 865 866 867

	spin_lock_irqsave(&ubc->isooutlock, flags);
	ubc->isooutovfl = ubc->isooutdone;
	ubc->isooutdone = ucx;
	spin_unlock_irqrestore(&ubc->isooutlock, flags);
868
	tasklet_hi_schedule(&ubc->sent_tasklet);
869 870 871 872 873 874 875 876 877 878 879 880
}

/* 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)
{
881
	struct bas_bc_state *ubc = bcs->hw.bas;
882 883 884 885 886
	struct urb *urb;
	int j, k;
	int rc;

	/* initialize L2 reception */
887
	if (bcs->proto2 == L2_HDLC)
888 889 890
		bcs->inputstate |= INS_flag_hunt;

	/* submit all isochronous input URBs */
Tilman Schmidt's avatar
Tilman Schmidt committed
891
	ubc->running = 1;
892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915
	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);
916 917
		rc = usb_submit_urb(urb, GFP_ATOMIC);
		if (rc != 0)
918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948
			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;
	}

Tilman Schmidt's avatar
Tilman Schmidt committed
949 950
	/* keep one URB free, submit the others */
	for (k = 0; k < BAS_OUTURBS-1; ++k) {
951
		dump_urb(DEBUG_ISO, "Initial isoc write", urb);
952
		rc = usb_submit_urb(ubc->isoouturbs[k].urb, GFP_ATOMIC);
953
		if (rc != 0)
954 955 956
			goto error;
	}
	dump_urb(DEBUG_ISO, "Initial isoc write (free)", urb);
Tilman Schmidt's avatar
Tilman Schmidt committed
957
	ubc->isooutfree = &ubc->isoouturbs[BAS_OUTURBS-1];
958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974
	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;

Tilman Schmidt's avatar
Tilman Schmidt committed
975
	ubc->running = 0;
976 977 978

	for (k = 0; k < BAS_INURBS; ++k) {
		rc = usb_unlink_urb(ubc->isoinurbs[k]);
979
		gig_dbg(DEBUG_ISO,
980 981
			"%s: isoc input URB %d unlinked, result = %s",
			__func__, k, get_usb_rcmsg(rc));
982 983 984 985
	}

	for (k = 0; k < BAS_OUTURBS; ++k) {
		rc = usb_unlink_urb(ubc->isoouturbs[k].urb);
986
		gig_dbg(DEBUG_ISO,
987 988
			"%s: isoc output URB %d unlinked, result = %s",
			__func__, k, get_usb_rcmsg(rc));
989 990 991 992 993 994 995 996 997
	}
}

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

/* submit_iso_write_urb
 * fill and submit the next isochronous write URB
 * parameters:
998
 *	ucx	context structure containing URB
999 1000 1001 1002 1003 1004 1005
 * 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)
{
1006 1007
	struct urb *urb = ucx->urb;
	struct bas_bc_state *ubc = ucx->bcs->hw.bas;
1008 1009 1010
	struct usb_iso_packet_descriptor *ifd;
	int corrbytes, nframe, rc;

1011 1012
	/* urb->dev is clobbered by USB subsystem */
	urb->dev = ucx->bcs->cs->hw.bas->udev;
1013 1014 1015 1016 1017 1018 1019 1020 1021
	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;
1022 1023
		corrbytes = atomic_read(&ubc->corrbytes);
		if (corrbytes != 0) {
1024 1025
			gig_dbg(DEBUG_ISO, "%s: corrbytes=%d",
				__func__, corrbytes);
1026 1027 1028 1029 1030 1031 1032 1033 1034
			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 */
1035 1036 1037
		rc = gigaset_isowbuf_getbytes(ubc->isooutbuf, ifd->length);
		if (rc < 0) {
			if (rc == -EBUSY) {
1038 1039 1040 1041
				gig_dbg(DEBUG_ISO,
					"%s: buffer busy at frame %d",
					__func__, nframe);
				/* tasklet will be restarted from
1042
				   gigaset_isoc_send_skb() */
1043
			} else {
1044 1045
				dev_err(ucx->bcs->cs->dev,
					"%s: buffer error %d at frame %d\n",
1046 1047
					__func__, rc, nframe);
				return rc;
1048 1049 1050
			}
			break;
		}
1051
		ifd->offset = rc;
Tilman Schmidt's avatar
Tilman Schmidt committed
1052
		ucx->limit = ubc->