speedtch.c 35.3 KB
Newer Older
1
/******************************************************************************
2
 *  speedtouch.c  -  Alcatel SpeedTouch USB xDSL modem driver
3 4
 *
 *  Copyright (C) 2001, Alcatel
5
 *  Copyright (C) 2003, Duncan Sands
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 *  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.
 *
 *  This program is distributed in the hope that it will be useful, but WITHOUT
 *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 *  more details.
 *
 *  You should have received a copy of the GNU General Public License along with
 *  this program; if not, write to the Free Software Foundation, Inc., 59
 *  Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 ******************************************************************************/

23
/*
24 25 26 27 28 29 30 31 32 33 34 35 36
 *  Written by Johan Verrept, maintained by Duncan Sands (duncan.sands@wanadoo.fr)
 *
 *  1.6:	- No longer opens a connection if the firmware is not loaded
 *  		- Added support for the speedtouch 330
 *  		- Removed the limit on the number of devices
 *  		- Module now autoloads on device plugin
 *  		- Merged relevant parts of sarlib
 *  		- Replaced the kernel thread with a tasklet
 *  		- New packet transmission code
 *  		- Changed proc file contents
 *  		- Fixed all known SMP races
 *  		- Many fixes and cleanups
 *  		- Various fixes by Oliver Neukum (oliver@neukum.name)
37
 *
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
 *  1.5A:	- Version for inclusion in 2.5 series kernel
 *		- Modifications by Richard Purdie (rpurdie@rpsys.net)
 *		- made compatible with kernel 2.5.6 onwards by changing
 *		udsl_usb_send_data_context->urb to a pointer and adding code
 *		to alloc and free it
 *		- remove_wait_queue() added to udsl_atm_processqueue_thread()
 *
 *  1.5:	- fixed memory leak when atmsar_decode_aal5 returned NULL.
 *		(reported by stephen.robinson@zen.co.uk)
 *
 *  1.4:	- changed the spin_lock() under interrupt to spin_lock_irqsave()
 *		- unlink all active send urbs of a vcc that is being closed.
 *
 *  1.3.1:	- added the version number
 *
 *  1.3:	- Added multiple send urb support
 *		- fixed memory leak and vcc->tx_inuse starvation bug
 *		  when not enough memory left in vcc.
 *
 *  1.2:	- Fixed race condition in udsl_usb_send_data()
 *  1.1:	- Turned off packet debugging
 *
60 61
 */

62
#include <asm/semaphore.h>
63 64 65 66 67 68 69 70 71 72
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/timer.h>
#include <linux/errno.h>
#include <linux/proc_fs.h>
#include <linux/slab.h>
#include <linux/list.h>
#include <asm/uaccess.h>
#include <linux/smp_lock.h>
73
#include <linux/interrupt.h>
74 75
#include <linux/atm.h>
#include <linux/atmdev.h>
76
#include <linux/crc32.h>
David S. Miller's avatar
David S. Miller committed
77
#include <linux/init.h>
78 79

/*
80 81
#define DEBUG
#define VERBOSE_DEBUG
82 83
*/

84
#include <linux/usb.h>
85 86


87
#ifdef VERBOSE_DEBUG
88
static int udsl_print_packet (const unsigned char *data, int len);
89 90
#define PACKETDEBUG(arg...)	udsl_print_packet (arg)
#define vdbg(arg...)		dbg (arg)
91 92
#else
#define PACKETDEBUG(arg...)
93
#define vdbg(arg...)
94 95
#endif

96 97 98
#define DRIVER_AUTHOR	"Johan Verrept, Duncan Sands <duncan.sands@wanadoo.fr>"
#define DRIVER_DESC	"Alcatel SpeedTouch USB driver"
#define DRIVER_VERSION	"1.6"
99

100 101
static const char udsl_driver_name [] = "speedtch";

102 103 104
#define SPEEDTOUCH_VENDORID		0x06b9
#define SPEEDTOUCH_PRODUCTID		0x4061

105 106
#define UDSL_NUM_RCV_URBS		1
#define UDSL_NUM_SND_URBS		1
107
#define UDSL_NUM_RCV_BUFS		(2*UDSL_NUM_RCV_URBS)
108
#define UDSL_NUM_SND_BUFS		(2*UDSL_NUM_SND_URBS)
109
#define UDSL_RCV_BUF_SIZE		32 /* ATM cells */
110
#define UDSL_SND_BUF_SIZE		64 /* ATM cells */
111

112 113
#define UDSL_IOCTL_LINE_UP		1
#define UDSL_IOCTL_LINE_DOWN		2
114 115 116 117

#define UDSL_ENDPOINT_DATA_OUT		0x07
#define UDSL_ENDPOINT_DATA_IN		0x87

118
#define ATM_CELL_HEADER			(ATM_CELL_SIZE - ATM_CELL_PAYLOAD)
119
#define UDSL_NUM_CELLS(x)		(((x) + ATM_AAL5_TRAILER + ATM_CELL_PAYLOAD - 1) / ATM_CELL_PAYLOAD)
120

121
#define hex2int(c) ( (c >= '0') && (c <= '9') ? (c - '0') : ((c & 0xf) + 9) )
122

123 124
static struct usb_device_id udsl_usb_ids [] = {
	{ USB_DEVICE (SPEEDTOUCH_VENDORID, SPEEDTOUCH_PRODUCTID) },
125
	{ }
126 127
};

128 129
MODULE_DEVICE_TABLE (usb, udsl_usb_ids);

130
/* receive */
131

132 133 134 135 136
struct udsl_receive_buffer {
	struct list_head list;
	unsigned char *base;
	unsigned int filled_cells;
};
137

138
struct udsl_receiver {
139
	struct list_head list;
140
	struct udsl_receive_buffer *buffer;
141 142 143 144
	struct urb *urb;
	struct udsl_instance_data *instance;
};

145 146 147 148 149 150 151 152 153
struct udsl_vcc_data {
	/* vpi/vci lookup */
	struct list_head list;
	short vpi;
	int vci;
	struct atm_vcc *vcc;

	/* raw cell reassembly */
	struct sk_buff *skb;
154
	unsigned int max_pdu;
155 156 157 158
};

/* send */

159 160 161 162 163 164 165
struct udsl_send_buffer {
	struct list_head list;
	unsigned char *base;
	unsigned char *free_start;
	unsigned int free_cells;
};

166
struct udsl_sender {
167 168 169
	struct list_head list;
	struct udsl_send_buffer *buffer;
	struct urb *urb;
170 171 172
	struct udsl_instance_data *instance;
};

173 174 175 176 177
struct udsl_control {
	struct atm_skb_data atm_data;
	unsigned int num_cells;
	unsigned int num_entire;
	unsigned int pdu_padding;
178
	unsigned char cell_header [ATM_CELL_HEADER];
179 180 181 182 183
	unsigned char aal5_trailer [ATM_AAL5_TRAILER];
};

#define UDSL_SKB(x)		((struct udsl_control *)(x)->cb)

184
/* main driver data */
185 186

struct udsl_instance_data {
187 188
	struct semaphore serialize;

189
	/* USB device part */
190
	struct usb_device *usb_dev;
191
	char description [64];
192
	int firmware_loaded;
193

194
	/* ATM device part */
195
	struct atm_dev *atm_dev;
196
	struct list_head vcc_list;
197

198 199
	/* receive */
	struct udsl_receiver receivers [UDSL_NUM_RCV_URBS];
200
	struct udsl_receive_buffer receive_buffers [UDSL_NUM_RCV_BUFS];
201

202
	spinlock_t receive_lock;
203
	struct list_head spare_receivers;
204
	struct list_head filled_receive_buffers;
205

206
	struct tasklet_struct receive_tasklet;
207
	struct list_head spare_receive_buffers;
208

209 210 211
	/* send */
	struct udsl_sender senders [UDSL_NUM_SND_URBS];
	struct udsl_send_buffer send_buffers [UDSL_NUM_SND_BUFS];
212 213 214 215 216

	struct sk_buff_head sndqueue;

	spinlock_t send_lock;
	struct list_head spare_senders;
217
	struct list_head spare_send_buffers;
218 219

	struct tasklet_struct send_tasklet;
220 221
	struct sk_buff *current_skb;			/* being emptied */
	struct udsl_send_buffer *current_buffer;	/* being filled */
222
	struct list_head filled_send_buffers;
223 224
};

225
/* ATM */
226

227
static void udsl_atm_dev_close (struct atm_dev *dev);
228 229 230 231
static int udsl_atm_open (struct atm_vcc *vcc, short vpi, int vci);
static void udsl_atm_close (struct atm_vcc *vcc);
static int udsl_atm_ioctl (struct atm_dev *dev, unsigned int cmd, void *arg);
static int udsl_atm_send (struct atm_vcc *vcc, struct sk_buff *skb);
232
static int udsl_atm_proc_read (struct atm_dev *atm_dev, loff_t *pos, char *page);
233 234

static struct atmdev_ops udsl_atm_devops = {
235
	.dev_close =	udsl_atm_dev_close,
236 237 238 239 240
	.open =		udsl_atm_open,
	.close =	udsl_atm_close,
	.ioctl =	udsl_atm_ioctl,
	.send =		udsl_atm_send,
	.proc_read =	udsl_atm_proc_read,
241
	.owner =	THIS_MODULE,
242 243
};

244 245 246
/* USB */

static int udsl_usb_probe (struct usb_interface *intf, const struct usb_device_id *id);
247
static void udsl_usb_disconnect (struct usb_interface *intf);
248
static int udsl_usb_ioctl (struct usb_interface *intf, unsigned int code, void *user_data);
249 250

static struct usb_driver udsl_usb_driver = {
251
	.owner =	THIS_MODULE,
252 253 254 255 256
	.name =		udsl_driver_name,
	.probe =	udsl_usb_probe,
	.disconnect =	udsl_usb_disconnect,
	.ioctl =	udsl_usb_ioctl,
	.id_table =	udsl_usb_ids,
257 258
};

259

260 261 262 263
/*************
**  decode  **
*************/

264 265 266 267 268
static inline struct udsl_vcc_data *udsl_find_vcc (struct udsl_instance_data *instance, short vpi, int vci)
{
	struct udsl_vcc_data *vcc;

	list_for_each_entry (vcc, &instance->vcc_list, list)
269
		if ((vcc->vci == vci) && (vcc->vpi == vpi))
270 271 272
			return vcc;
	return NULL;
}
273

274
static void udsl_extract_cells (struct udsl_instance_data *instance, unsigned char *source, unsigned int howmany)
275
{
276 277 278 279 280 281 282 283 284 285 286 287
	struct udsl_vcc_data *cached_vcc = NULL;
	struct atm_vcc *vcc;
	struct sk_buff *skb;
	struct udsl_vcc_data *vcc_data;
	int cached_vci = 0;
	unsigned int i;
	unsigned int length;
	unsigned int pdu_length;
	int pti;
	int vci;
	short cached_vpi = 0;
	short vpi;
288

289 290 291 292
	for (i = 0; i < howmany; i++, source += ATM_CELL_SIZE) {
		vpi = ((source [0] & 0x0f) << 4) | (source [1] >> 4);
		vci = ((source [1] & 0x0f) << 12) | (source [2] << 4) | (source [3] >> 4);
		pti = (source [3] & 0x2) != 0;
293

294
		vdbg ("udsl_extract_cells: vpi %hd, vci %d, pti %d", vpi, vci, pti);
295

296 297 298 299 300 301
		if (cached_vcc && (vci == cached_vci) && (vpi == cached_vpi))
			vcc_data = cached_vcc;
		else if ((vcc_data = udsl_find_vcc (instance, vpi, vci))) {
			cached_vcc = vcc_data;
			cached_vpi = vpi;
			cached_vci = vci;
302
		} else {
303 304
			dbg ("udsl_extract_cells: unknown vpi/vci (%hd/%d)!", vpi, vci);
			continue;
305 306
		}

307
		vcc = vcc_data->vcc;
308

309 310 311 312 313
		if (!vcc_data->skb && !(vcc_data->skb = dev_alloc_skb (vcc_data->max_pdu))) {
			dbg ("udsl_extract_cells: no memory for skb (vcc: 0x%p)!", vcc);
			if (pti)
				atomic_inc (&vcc->stats->rx_err);
			continue;
314
		}
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377

		skb = vcc_data->skb;

		if (skb->len + ATM_CELL_PAYLOAD > vcc_data->max_pdu) {
			dbg ("udsl_extract_cells: buffer overrun (max_pdu: %u, skb->len %u, vcc: 0x%p)", vcc_data->max_pdu, skb->len, vcc);
			/* discard cells already received */
			skb_trim (skb, 0);
			BUG_ON (vcc_data->max_pdu < ATM_CELL_PAYLOAD);
		}

		memcpy (skb->tail, source + ATM_CELL_HEADER, ATM_CELL_PAYLOAD);
		__skb_put (skb, ATM_CELL_PAYLOAD);

		if (pti) {
			length = (source [ATM_CELL_SIZE - 6] << 8) + source [ATM_CELL_SIZE - 5];

			/* guard against overflow */
			if (length > ATM_MAX_AAL5_PDU) {
				dbg ("udsl_extract_cells: bogus length %u (vcc: 0x%p)", length, vcc);
				goto drop;
			}

			pdu_length = UDSL_NUM_CELLS (length) * ATM_CELL_PAYLOAD;

			if (skb->len < pdu_length) {
				dbg ("udsl_extract_cells: bogus pdu_length %u (skb->len: %u, vcc: 0x%p)", pdu_length, skb->len, vcc);
				goto drop;
			}

			if (crc32_be (~0, skb->tail - pdu_length, pdu_length) != 0xc704dd7b) {
				dbg ("udsl_extract_cells: packet failed crc check (vcc: 0x%p)", vcc);
				goto drop;
			}

			if (!atm_charge (vcc, skb->truesize)) {
				dbg ("udsl_extract_cells: failed atm_charge (skb->truesize: %u)", skb->truesize);
				goto drop_no_stats; /* atm_charge increments rx_drop */
			}

			/* now that we are sure to send the skb, it is ok to change skb->data */
			if (skb->len > pdu_length)
				skb_pull (skb, skb->len - pdu_length); /* discard initial junk */

			skb_trim (skb, length); /* drop zero padding and trailer */

			atomic_inc (&vcc->stats->rx);

			PACKETDEBUG (skb->data, skb->len);

			vdbg ("udsl_extract_cells: sending skb 0x%p, skb->len %u, skb->truesize %u", skb, skb->len, skb->truesize);

			vcc->push (vcc, skb);

			vcc_data->skb = NULL;

			continue;

drop:
			atomic_inc (&vcc->stats->rx_err);
drop_no_stats:
			skb_trim (skb, 0);
		}
	}
378
}
379 380


381 382 383 384
/*************
**  encode  **
*************/

385
static const unsigned char zeros [ATM_CELL_PAYLOAD];
386

387 388
static void udsl_groom_skb (struct atm_vcc *vcc, struct sk_buff *skb)
{
389
	struct udsl_control *ctrl = UDSL_SKB (skb);
390
	unsigned int zero_padding;
391 392 393 394 395 396 397 398 399
	u32 crc;

	ctrl->atm_data.vcc = vcc;
	ctrl->cell_header [0] = vcc->vpi >> 4;
	ctrl->cell_header [1] = (vcc->vpi << 4) | (vcc->vci >> 12);
	ctrl->cell_header [2] = vcc->vci >> 4;
	ctrl->cell_header [3] = vcc->vci << 4;
	ctrl->cell_header [4] = 0xec;

400
	ctrl->num_cells = UDSL_NUM_CELLS (skb->len);
401 402 403 404 405 406 407 408 409 410 411 412 413 414
	ctrl->num_entire = skb->len / ATM_CELL_PAYLOAD;

	zero_padding = ctrl->num_cells * ATM_CELL_PAYLOAD - skb->len - ATM_AAL5_TRAILER;

	if (ctrl->num_entire + 1 < ctrl->num_cells)
		ctrl->pdu_padding = zero_padding - (ATM_CELL_PAYLOAD - ATM_AAL5_TRAILER);
	else
		ctrl->pdu_padding = zero_padding;

	ctrl->aal5_trailer [0] = 0; /* UU = 0 */
	ctrl->aal5_trailer [1] = 0; /* CPI = 0 */
	ctrl->aal5_trailer [2] = skb->len >> 8;
	ctrl->aal5_trailer [3] = skb->len;

415
	crc = crc32_be (~0, skb->data, skb->len);
416
	crc = crc32_be (crc, zeros, zero_padding);
417
	crc = crc32_be (crc, ctrl->aal5_trailer, 4);
418 419 420 421 422 423 424 425
	crc = ~crc;

	ctrl->aal5_trailer [4] = crc >> 24;
	ctrl->aal5_trailer [5] = crc >> 16;
	ctrl->aal5_trailer [6] = crc >> 8;
	ctrl->aal5_trailer [7] = crc;
}

426 427
static unsigned int udsl_write_cells (unsigned int howmany, struct sk_buff *skb, unsigned char **target_p)
{
428
	struct udsl_control *ctrl = UDSL_SKB (skb);
429 430
	unsigned char *target = *target_p;
	unsigned int nc, ne, i;
431

432
	vdbg ("udsl_write_cells: howmany=%u, skb->len=%d, num_cells=%u, num_entire=%u, pdu_padding=%u", howmany, skb->len, ctrl->num_cells, ctrl->num_entire, ctrl->pdu_padding);
433

434 435
	nc = ctrl->num_cells;
	ne = min (howmany, ctrl->num_entire);
436

437 438 439
	for (i = 0; i < ne; i++) {
		memcpy (target, ctrl->cell_header, ATM_CELL_HEADER);
		target += ATM_CELL_HEADER;
440 441 442 443 444
		memcpy (target, skb->data, ATM_CELL_PAYLOAD);
		target += ATM_CELL_PAYLOAD;
		__skb_pull (skb, ATM_CELL_PAYLOAD);
	}

445 446 447 448 449 450 451
	ctrl->num_entire -= ne;

	if (!(ctrl->num_cells -= ne) || !(howmany -= ne))
		goto out;

	memcpy (target, ctrl->cell_header, ATM_CELL_HEADER);
	target += ATM_CELL_HEADER;
452 453 454 455 456 457
	memcpy (target, skb->data, skb->len);
	target += skb->len;
	__skb_pull (skb, skb->len);
	memset (target, 0, ctrl->pdu_padding);
	target += ctrl->pdu_padding;

458 459 460 461 462 463 464 465 466 467 468
	if (--ctrl->num_cells) {
		if (!--howmany) {
			ctrl->pdu_padding = ATM_CELL_PAYLOAD - ATM_AAL5_TRAILER;
			goto out;
		}

		memcpy (target, ctrl->cell_header, ATM_CELL_HEADER);
		target += ATM_CELL_HEADER;
		memset (target, 0, ATM_CELL_PAYLOAD - ATM_AAL5_TRAILER);
		target += ATM_CELL_PAYLOAD - ATM_AAL5_TRAILER;

469
		BUG_ON (--ctrl->num_cells);
470 471
	}

472 473 474 475 476 477 478 479
	memcpy (target, ctrl->aal5_trailer, ATM_AAL5_TRAILER);
	target += ATM_AAL5_TRAILER;
	/* set pti bit in last cell */
	*(target + 3 - ATM_CELL_SIZE) |= 0x2;

out:
	*target_p = target;
	return nc - ctrl->num_cells;
480 481 482
}


483 484 485 486 487 488
/**************
**  receive  **
**************/

static void udsl_complete_receive (struct urb *urb, struct pt_regs *regs)
{
489
	struct udsl_receive_buffer *buf;
490 491 492 493
	struct udsl_instance_data *instance;
	struct udsl_receiver *rcv;
	unsigned long flags;

494
	if (!urb || !(rcv = urb->context)) {
495
		dbg ("udsl_complete_receive: bad urb!");
496 497 498
		return;
	}

499 500 501 502 503 504 505 506
	instance = rcv->instance;
	buf = rcv->buffer;

	buf->filled_cells = urb->actual_length / ATM_CELL_SIZE;

	vdbg ("udsl_complete_receive: urb 0x%p, status %d, actual_length %d, filled_cells %u, rcv 0x%p, buf 0x%p", urb, urb->status, urb->actual_length, buf->filled_cells, rcv, buf);

	BUG_ON (buf->filled_cells > UDSL_RCV_BUF_SIZE);
507

508
	/* may not be in_interrupt() */
509 510 511 512 513 514
	spin_lock_irqsave (&instance->receive_lock, flags);
	list_add (&rcv->list, &instance->spare_receivers);
	list_add_tail (&buf->list, &instance->filled_receive_buffers);
	if (likely (!urb->status))
		tasklet_schedule (&instance->receive_tasklet);
	spin_unlock_irqrestore (&instance->receive_lock, flags);
515 516 517 518
}

static void udsl_process_receive (unsigned long data)
{
519
	struct udsl_receive_buffer *buf;
520 521
	struct udsl_instance_data *instance = (struct udsl_instance_data *) data;
	struct udsl_receiver *rcv;
522
	int err;
523

524 525 526 527 528
made_progress:
	while (!list_empty (&instance->spare_receive_buffers)) {
		spin_lock_irq (&instance->receive_lock);
		if (list_empty (&instance->spare_receivers)) {
			spin_unlock_irq (&instance->receive_lock);
529
			break;
530 531 532 533
		}
		rcv = list_entry (instance->spare_receivers.next, struct udsl_receiver, list);
		list_del (&rcv->list);
		spin_unlock_irq (&instance->receive_lock);
534

535 536
		buf = list_entry (instance->spare_receive_buffers.next, struct udsl_receive_buffer, list);
		list_del (&buf->list);
537

538
		rcv->buffer = buf;
539 540 541 542

		usb_fill_bulk_urb (rcv->urb,
				   instance->usb_dev,
				   usb_rcvbulkpipe (instance->usb_dev, UDSL_ENDPOINT_DATA_IN),
543
				   buf->base,
544
				   UDSL_RCV_BUF_SIZE * ATM_CELL_SIZE,
545 546 547
				   udsl_complete_receive,
				   rcv);

548 549 550 551 552 553 554 555 556
		vdbg ("udsl_process_receive: sending urb 0x%p, rcv 0x%p, buf 0x%p", rcv->urb, rcv, buf);

		if ((err = usb_submit_urb(rcv->urb, GFP_ATOMIC)) < 0) {
			dbg ("udsl_process_receive: urb submission failed (%d)!", err);
			list_add (&buf->list, &instance->spare_receive_buffers);
			spin_lock_irq (&instance->receive_lock);
			list_add (&rcv->list, &instance->spare_receivers);
			spin_unlock_irq (&instance->receive_lock);
			break;
557 558 559
		}
	}

560 561 562 563 564 565 566 567 568 569 570 571
	spin_lock_irq (&instance->receive_lock);
	if (list_empty (&instance->filled_receive_buffers)) {
		spin_unlock_irq (&instance->receive_lock);
		return; /* done - no more buffers */
	}
	buf = list_entry (instance->filled_receive_buffers.next, struct udsl_receive_buffer, list);
	list_del (&buf->list);
	spin_unlock_irq (&instance->receive_lock);
	vdbg ("udsl_process_receive: processing buf 0x%p", buf);
	udsl_extract_cells (instance, buf->base, buf->filled_cells);
	list_add (&buf->list, &instance->spare_receive_buffers);
	goto made_progress;
572 573 574
}


575 576 577 578
/***********
**  send  **
***********/

579 580 581
static void udsl_complete_send (struct urb *urb, struct pt_regs *regs)
{
	struct udsl_instance_data *instance;
582
	struct udsl_sender *snd;
583 584 585
	unsigned long flags;

	if (!urb || !(snd = urb->context) || !(instance = snd->instance)) {
586
		dbg ("udsl_complete_send: bad urb!");
587 588 589
		return;
	}

590
	vdbg ("udsl_complete_send: urb 0x%p, status %d, snd 0x%p, buf 0x%p", urb, urb->status, snd, snd->buffer);
591

592 593 594
	/* may not be in_interrupt() */
	spin_lock_irqsave (&instance->send_lock, flags);
	list_add (&snd->list, &instance->spare_senders);
595
	list_add (&snd->buffer->list, &instance->spare_send_buffers);
596
	tasklet_schedule (&instance->send_tasklet);
597 598 599
	spin_unlock_irqrestore (&instance->send_lock, flags);
}

600 601
static void udsl_process_send (unsigned long data)
{
602
	struct udsl_send_buffer *buf;
603
	struct udsl_instance_data *instance = (struct udsl_instance_data *) data;
604
	struct sk_buff *skb;
605
	struct udsl_sender *snd;
606 607
	int err;
	unsigned int num_written;
608 609

made_progress:
610
	spin_lock_irq (&instance->send_lock);
611
	while (!list_empty (&instance->spare_senders)) {
612 613
		if (!list_empty (&instance->filled_send_buffers)) {
			buf = list_entry (instance->filled_send_buffers.next, struct udsl_send_buffer, list);
614 615 616 617 618 619
			list_del (&buf->list);
		} else if ((buf = instance->current_buffer)) {
			instance->current_buffer = NULL;
		} else /* all buffers empty */
			break;

620
		snd = list_entry (instance->spare_senders.next, struct udsl_sender, list);
621
		list_del (&snd->list);
622
		spin_unlock_irq (&instance->send_lock);
623 624 625 626 627 628

		snd->buffer = buf;
	        usb_fill_bulk_urb (snd->urb,
				   instance->usb_dev,
				   usb_sndbulkpipe (instance->usb_dev, UDSL_ENDPOINT_DATA_OUT),
				   buf->base,
629
				   (UDSL_SND_BUF_SIZE - buf->free_cells) * ATM_CELL_SIZE,
630 631 632
				   udsl_complete_send,
				   snd);

633
		vdbg ("udsl_process_send: submitting urb 0x%p (%d cells), snd 0x%p, buf 0x%p", snd->urb, UDSL_SND_BUF_SIZE - buf->free_cells, snd, buf);
634

635
		if ((err = usb_submit_urb(snd->urb, GFP_ATOMIC)) < 0) {
636
			dbg ("udsl_process_send: urb submission failed (%d)!", err);
637
			spin_lock_irq (&instance->send_lock);
638
			list_add (&snd->list, &instance->spare_senders);
639
			spin_unlock_irq (&instance->send_lock);
640
			list_add (&buf->list, &instance->filled_send_buffers);
641
			return; /* bail out */
642 643
		}

644
		spin_lock_irq (&instance->send_lock);
645
	} /* while */
646
	spin_unlock_irq (&instance->send_lock);
647

648
	if (!instance->current_skb && !(instance->current_skb = skb_dequeue (&instance->sndqueue)))
649
		return; /* done - no more skbs */
650 651 652 653

	skb = instance->current_skb;

	if (!(buf = instance->current_buffer)) {
654
		spin_lock_irq (&instance->send_lock);
655
		if (list_empty (&instance->spare_send_buffers)) {
656
			instance->current_buffer = NULL;
657
			spin_unlock_irq (&instance->send_lock);
658
			return; /* done - no more buffers */
659
		}
660
		buf = list_entry (instance->spare_send_buffers.next, struct udsl_send_buffer, list);
661
		list_del (&buf->list);
662
		spin_unlock_irq (&instance->send_lock);
663 664

		buf->free_start = buf->base;
665
		buf->free_cells = UDSL_SND_BUF_SIZE;
666 667 668 669

		instance->current_buffer = buf;
	}

670
	num_written = udsl_write_cells (buf->free_cells, skb, &buf->free_start);
671

672
	vdbg ("udsl_process_send: wrote %u cells from skb 0x%p to buffer 0x%p", num_written, skb, buf);
673

674
	if (!(buf->free_cells -= num_written)) {
675
		list_add_tail (&buf->list, &instance->filled_send_buffers);
676 677 678
		instance->current_buffer = NULL;
	}

679
	vdbg ("udsl_process_send: buffer contains %d cells, %d left", UDSL_SND_BUF_SIZE - buf->free_cells, buf->free_cells);
680 681 682 683 684 685 686

	if (!UDSL_SKB (skb)->num_cells) {
		struct atm_vcc *vcc = UDSL_SKB (skb)->atm_data.vcc;

		if (vcc->pop)
			vcc->pop (vcc, skb);
		else
687
			dev_kfree_skb (skb);
688
		instance->current_skb = NULL;
689

690
		atomic_inc (&vcc->stats->tx);
691 692 693 694 695
	}

	goto made_progress;
}

696
static void udsl_cancel_send (struct udsl_instance_data *instance, struct atm_vcc *vcc)
697 698 699
{
	struct sk_buff *skb, *n;

700
	dbg ("udsl_cancel_send entered");
701
	spin_lock_irq (&instance->sndqueue.lock);
702 703
	for (skb = instance->sndqueue.next, n = skb->next; skb != (struct sk_buff *)&instance->sndqueue; skb = n, n = skb->next)
		if (UDSL_SKB (skb)->atm_data.vcc == vcc) {
704
			dbg ("udsl_cancel_send: popping skb 0x%p", skb);
705 706 707 708
			__skb_unlink (skb, &instance->sndqueue);
			if (vcc->pop)
				vcc->pop (vcc, skb);
			else
709
				dev_kfree_skb (skb);
710
		}
711
	spin_unlock_irq (&instance->sndqueue.lock);
712 713 714

	tasklet_disable (&instance->send_tasklet);
	if ((skb = instance->current_skb) && (UDSL_SKB (skb)->atm_data.vcc == vcc)) {
715
		dbg ("udsl_cancel_send: popping current skb (0x%p)", skb);
716 717 718 719
		instance->current_skb = NULL;
		if (vcc->pop)
			vcc->pop (vcc, skb);
		else
720
			dev_kfree_skb (skb);
721 722
	}
	tasklet_enable (&instance->send_tasklet);
723
	dbg ("udsl_cancel_send done");
724 725 726 727 728 729
}

static int udsl_atm_send (struct atm_vcc *vcc, struct sk_buff *skb)
{
	struct udsl_instance_data *instance = vcc->dev->dev_data;

730
	vdbg ("udsl_atm_send called (skb 0x%p, len %u)", skb, skb->len);
731

732
	if (!instance || !instance->usb_dev) {
733
		dbg ("udsl_atm_send: NULL data!");
734
		return -ENODEV;
735 736 737
	}

	if (vcc->qos.aal != ATM_AAL5) {
738
		dbg ("udsl_atm_send: unsupported ATM type %d!", vcc->qos.aal);
739 740 741 742
		return -EINVAL;
	}

	if (skb->len > ATM_MAX_AAL5_PDU) {
743
		dbg ("udsl_atm_send: packet too long (%d vs %d)!", skb->len, ATM_MAX_AAL5_PDU);
744 745 746 747 748 749 750 751 752 753
		return -EINVAL;
	}

	PACKETDEBUG (skb->data, skb->len);

	udsl_groom_skb (vcc, skb);
	skb_queue_tail (&instance->sndqueue, skb);
	tasklet_schedule (&instance->send_tasklet);

	return 0;
754 755 756
}


757 758 759
/**********
**  ATM  **
**********/
760

761
static void udsl_atm_dev_close (struct atm_dev *dev)
762
{
763
	struct udsl_instance_data *instance = dev->dev_data;
764

765
	if (!instance) {
766
		dbg ("udsl_atm_dev_close: NULL instance!");
767
		return;
768
	}
769

770
	dbg ("udsl_atm_dev_close: queue has %u elements", instance->sndqueue.qlen);
771

772
	tasklet_kill (&instance->receive_tasklet);
773
	tasklet_kill (&instance->send_tasklet);
774
	kfree (instance);
775
	dev->dev_data = NULL;
776 777
}

778
static int udsl_atm_proc_read (struct atm_dev *atm_dev, loff_t *pos, char *page)
779
{
780
	struct udsl_instance_data *instance = atm_dev->dev_data;
781 782
	int left = *pos;

783
	if (!instance) {
784
		dbg ("udsl_atm_proc_read: NULL instance!");
785 786 787
		return -ENODEV;
	}

788
	if (!left--)
789 790 791 792
		return sprintf (page, "%s\n", instance->description);

	if (!left--)
		return sprintf (page, "MAC: %02x:%02x:%02x:%02x:%02x:%02x\n",
793 794
				atm_dev->esi [0], atm_dev->esi [1], atm_dev->esi [2],
				atm_dev->esi [3], atm_dev->esi [4], atm_dev->esi [5]);
795 796 797 798 799 800 801 802 803

	if (!left--)
		return sprintf (page, "AAL5: tx %d ( %d err ), rx %d ( %d err, %d drop )\n",
				atomic_read (&atm_dev->stats.aal5.tx),
				atomic_read (&atm_dev->stats.aal5.tx_err),
				atomic_read (&atm_dev->stats.aal5.rx),
				atomic_read (&atm_dev->stats.aal5.rx_err),
				atomic_read (&atm_dev->stats.aal5.rx_drop));

804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827
	if (!left--) {
		switch (atm_dev->signal) {
		case ATM_PHY_SIG_FOUND:
			sprintf (page, "Line up");
			break;
		case ATM_PHY_SIG_LOST:
			sprintf (page, "Line down");
			break;
		default:
			sprintf (page, "Line state unknown");
			break;
		}

		if (instance->usb_dev) {
			if (!instance->firmware_loaded)
				strcat (page, ", no firmware\n");
			else
				strcat (page, ", firmware loaded\n");
		} else
			strcat (page, ", disconnected\n");

		return strlen (page);
	}

828 829 830
	return 0;
}

831
static int udsl_atm_open (struct atm_vcc *vcc, short vpi, int vci)
832
{
833
	struct udsl_instance_data *instance = vcc->dev->dev_data;
834
	struct udsl_vcc_data *new;
835

836
	dbg ("udsl_atm_open: vpi %hd, vci %d", vpi, vci);
837

838
	if (!instance || !instance->usb_dev) {
839
		dbg ("udsl_atm_open: NULL data!");
840 841 842
		return -ENODEV;
	}

843 844 845
	if ((vpi == ATM_VPI_ANY) || (vci == ATM_VCI_ANY))
		return -EINVAL;

846
	/* only support AAL5 */
847
	if ((vcc->qos.aal != ATM_AAL5) || (vcc->qos.rxtp.max_sdu < 0) || (vcc->qos.rxtp.max_sdu > ATM_MAX_AAL5_PDU))
848 849
		return -EINVAL;

850
	if (!instance->firmware_loaded) {
851
		dbg ("udsl_atm_open: firmware not loaded!");
852 853 854
		return -EAGAIN;
	}

855 856 857 858
	down (&instance->serialize); /* vs self, udsl_atm_close */

	if (udsl_find_vcc (instance, vpi, vci)) {
		up (&instance->serialize);
859
		return -EADDRINUSE;
860
	}
861

862 863
	if (!(new = kmalloc (sizeof (struct udsl_vcc_data), GFP_KERNEL))) {
		up (&instance->serialize);
864
		return -ENOMEM;
865
	}
866

867
	memset (new, 0, sizeof (struct udsl_vcc_data));
868
	new->vcc = vcc;
869 870
	new->vpi = vpi;
	new->vci = vci;
871
	new->max_pdu = max (1, UDSL_NUM_CELLS (vcc->qos.rxtp.max_sdu)) * ATM_CELL_PAYLOAD;
872 873

	vcc->dev_data = new;
874 875
	vcc->vpi = vpi;
	vcc->vci = vci;
876

877
	tasklet_disable (&instance->receive_tasklet);
878
	list_add (&new->list, &instance->vcc_list);
879
	tasklet_enable (&instance->receive_tasklet);
880

881 882 883 884
	set_bit (ATM_VF_ADDR, &vcc->flags);
	set_bit (ATM_VF_PARTIAL, &vcc->flags);
	set_bit (ATM_VF_READY, &vcc->flags);

885 886
	up (&instance->serialize);

887
	tasklet_schedule (&instance->receive_tasklet);
888

889
	dbg ("udsl_atm_open: allocated vcc data 0x%p (max_pdu: %u)", new, new->max_pdu);
890

891 892 893
	return 0;
}

894
static void udsl_atm_close (struct atm_vcc *vcc)
895
{
896
	struct udsl_instance_data *instance = vcc->dev->dev_data;
897
	struct udsl_vcc_data *vcc_data = vcc->dev_data;
898

899
	dbg ("udsl_atm_close called");
900

901
	if (!instance || !vcc_data) {
902
		dbg ("udsl_atm_close: NULL data!");
903 904 905
		return;
	}

906
	dbg ("udsl_atm_close: deallocating vcc 0x%p with vpi %d vci %d", vcc_data, vcc_data->vpi, vcc_data->vci);
907

908
	udsl_cancel_send (instance, vcc);
909

910 911 912
	down (&instance->serialize); /* vs self, udsl_atm_open */

	tasklet_disable (&instance->receive_tasklet);
913
	list_del (&vcc_data->list);
914
	tasklet_enable (&instance->receive_tasklet);
915

916
	if (vcc_data->skb)
917
		dev_kfree_skb (vcc_data->skb);
918
	vcc_data->skb = NULL;
919

920
	kfree (vcc_data);
921 922 923 924
	vcc->dev_data = NULL;

	vcc->vpi = ATM_VPI_UNSPEC;
	vcc->vci = ATM_VCI_UNSPEC;
925 926
	clear_bit (ATM_VF_READY, &vcc->flags);
	clear_bit (ATM_VF_PARTIAL, &vcc->flags);
927 928
	clear_bit (ATM_VF_ADDR, &vcc->flags);

929 930
	up (&instance->serialize);

931
	dbg ("udsl_atm_close successful");
932
}
933

934
static int udsl_atm_ioctl (struct atm_dev *dev, unsigned int cmd, void *arg)
935 936 937 938 939 940 941
{
	switch (cmd) {
	case ATM_QUERYLOOP:
		return put_user (ATM_LM_NONE, (int *) arg) ? -EFAULT : 0;
	default:
		return -ENOIOCTLCMD;
	}
942
}
943 944


945 946 947
/**********
**  USB  **
**********/
948

949 950 951 952 953 954 955
static int udsl_set_alternate (struct udsl_instance_data *instance)
{
	down (&instance->serialize); /* vs self */
	if (!instance->firmware_loaded) {
		int ret;

		if ((ret = usb_set_interface (instance->usb_dev, 1, 1)) < 0) {
956
			dbg ("udsl_set_alternate: usb_set_interface returned %d!", ret);
957 958 959 960 961 962
			up (&instance->serialize);
			return ret;
		}
		instance->firmware_loaded = 1;
	}
	up (&instance->serialize);
963 964 965

	tasklet_schedule (&instance->receive_tasklet);

966 967 968
	return 0;
}

969
static int udsl_usb_ioctl (struct usb_interface *intf, unsigned int code, void *user_data)
970
{
971
	struct udsl_instance_data *instance = usb_get_intfdata (intf);
972

973
	dbg ("udsl_usb_ioctl entered");
974 975

	if (!instance) {
976
		dbg ("udsl_usb_ioctl: NULL instance!");
977 978 979
		return -ENODEV;
	}

980
	switch (code) {
981
	case UDSL_IOCTL_LINE_UP:
982
		instance->atm_dev->signal = ATM_PHY_SIG_FOUND;
983
		return udsl_set_alternate (instance);
984
	case UDSL_IOCTL_LINE_DOWN:
985
		instance->atm_dev->signal = ATM_PHY_SIG_LOST;
986
		return 0;
987
	default:
988
		return -ENOTTY;
989 990 991
	}
}

992
static int udsl_usb_probe (struct usb_interface *intf, const struct usb_device_id *id)
993
{
994
	struct usb_device *dev = interface_to_usbdev(intf);
995
	int ifnum = intf->altsetting->desc.bInterfaceNumber;
996
	struct udsl_instance_data *instance;
997
	unsigned char mac_str [13];
998 999
	int i, length;
	char *buf;
1000

1001 1002
	dbg ("udsl_usb_probe: trying device with vendor=0x%x, product=0x%x, ifnum %d",
	     dev->descriptor.idVendor, dev->descriptor.idProduct, ifnum);
1003 1004 1005 1006

	if ((dev->descriptor.bDeviceClass != USB_CLASS_VENDOR_SPEC) ||
	    (dev->descriptor.idVendor != SPEEDTOUCH_VENDORID) ||
	    (dev->descriptor.idProduct != SPEEDTOUCH_PRODUCTID) || (ifnum != 1))
1007
		return -ENODEV;
1008

1009
	dbg ("udsl_usb_probe: device accepted");
1010

1011 1012
	/* instance init */
	if (!(instance = kmalloc (sizeof (struct udsl_instance_data), GFP_KERNEL))) {
1013
		dbg ("udsl_usb_probe: no memory for instance data!");
1014
		return -ENOMEM;
1015 1016 1017
	}

	memset (instance, 0, sizeof (struct udsl_instance_data));
1018

1019 1020
	init_MUTEX (&instance->serialize);

1021
	instance->usb_dev = dev;
1022

1023 1024
	INIT_LIST_HEAD (&instance->vcc_list);

1025
	spin_lock_init (&instance->receive_lock);
1026
	INIT_LIST_HEAD (&instance->spare_receivers);
1027
	INIT_LIST_HEAD (&instance->filled_receive_buffers);
1028

1029
	tasklet_init (&instance->receive_tasklet, udsl_process_receive, (unsigned long) instance);
1030
	INIT_LIST_HEAD (&instance->spare_receive_buffers);
1031

1032 1033
	skb_queue_head_init (&instance->sndqueue);

1034 1035
	spin_lock_init (&instance->send_lock);
	INIT_LIST_HEAD (&instance->spare_senders);
1036
	INIT_LIST_HEAD (&instance->spare_send_buffers);
1037 1038

	tasklet_init (&instance->send_tasklet, udsl_process_send, (unsigned long) instance);
1039
	INIT_LIST_HEAD (&instance->filled_send_buffers);
1040 1041

	/* receive init */
1042 1043
	for (i = 0; i < UDSL_NUM_RCV_URBS; i++) {
		struct udsl_receiver *rcv = &(instance->receivers [i]);
1044

1045
		if (!(rcv->urb = usb_alloc_urb (0, GFP_KERNEL))) {
1046
			dbg ("udsl_usb_probe: no memory for receive urb %d!", i);
1047
			goto fail;
1048 1049
		}

1050
		rcv->instance = instance;
1051

1052
		list_add (&rcv->list, &instance->spare_receivers);
1053
	}
1054

1055 1056
	for (i = 0; i < UDSL_NUM_RCV_BUFS; i++) {
		struct udsl_receive_buffer *buf = &(instance->receive_buffers [i]);
1057

1058 1059 1060 1061 1062 1063
		if (!(buf->base = kmalloc (UDSL_RCV_BUF_SIZE * ATM_CELL_SIZE, GFP_KERNEL))) {
			dbg ("udsl_usb_probe: no memory for receive buffer %d!", i);
			goto fail;
		}

		list_add (&buf->list, &instance->spare_receive_buffers);
1064 1065
	}

1066
	/* send init */
1067 1068
	for (i = 0; i < UDSL_NUM_SND_URBS; i++) {
		struct udsl_sender *snd = &(instance->senders [i]);
1069 1070

		if (!(snd->urb = usb_alloc_urb (0, GFP_KERNEL))) {
1071
			dbg ("udsl_usb_probe: no memory for send urb %d!", i);
1072
			goto fail;
1073 1074 1075
		}

		snd->instance = instance;
1076 1077 1078 1079

		list_add (&snd->list, &instance->spare_senders);
	}

1080 1081
	for (i = 0; i < UDSL_NUM_SND_BUFS; i++) {
		struct udsl_send_buffer *buf = &(instance->send_buffers [i]);
1082

1083
		if (!(buf->base = kmalloc (UDSL_SND_BUF_SIZE * ATM_CELL_SIZE, GFP_KERNEL))) {
1084
			dbg ("udsl_usb_probe: no memory for send buffer %d!", i);
1085 1086 1087
			goto fail;
		}

1088
		list_add (&buf->list, &instance->spare_send_buffers);
1089 1090
	}

1091
	/* ATM init */
1092
	if (!(instance->atm_dev = atm_dev_register (udsl_driver_name, &udsl_atm_devops, -1, 0))) {
1093
		dbg ("udsl_usb_probe: failed to register ATM device!");
1094
		goto fail;
1095 1096
	}

1097 1098
	instance->atm_dev->ci_range.vpi_bits = ATM_CI_MAX;
	instance->atm_dev->ci_range.vci_bits = ATM_CI_MAX;
1099
	instance->atm_dev->signal = ATM_PHY_SIG_UNKNOWN;
1100

1101
	/* temp init ATM device, set to 128kbit */
1102
	instance->atm_dev->link_rate = 128 * 1000 / 424;
1103 1104

	/* set MAC address, it is stored in the serial number */
1105 1106 1107
	memset (instance->atm_dev->esi, 0, sizeof (instance->atm_dev->esi));
	if (usb_string (dev, dev->descriptor.iSerialNumber, mac_str, sizeof (mac_str)) == 12)
		for (i = 0; i < 6; i++)
1108
			instance->atm_dev->esi [i] = (hex2int (mac_str [i * 2]) * 16) + (hex2int (mac_str [i * 2 + 1]));
1109

1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
	/* device description */
	buf = instance->description;
	length = sizeof (instance->description);

	if ((i = usb_string (dev, dev->descriptor.iProduct, buf, length)) < 0)
		goto finish;

	buf += i;
	length -= i;

	i = snprintf (buf, length, " (");
	buf += i;
	length -= i;

	if (length <= 0 || (i = usb_make_path (dev, buf, length)) < 0)
		goto finish;
1126

1127 1128 1129 1130 1131 1132 1133
	buf += i;
	length -= i;

	snprintf (buf, length, ")");

finish:
	/* ready for ATM callbacks */
1134
	wmb ();
1135 1136
	instance->atm_dev->dev_data = instance;

1137
	usb_set_intfdata (intf, instance);
1138

1139
	return 0;
1140

1141
fail:
1142 1143
	for (i = 0; i < UDSL_NUM_SND_BUFS; i++)
		kfree (instance->send_buffers [i].base);
1144

1145 1146
	for (i = 0; i < UDSL_NUM_SND_URBS; i++)
		usb_free_urb (instance->senders [i].urb);
1147

1148 1149
	for (i = 0; i < UDSL_NUM_RCV_BUFS; i++)
		kfree (instance->receive_buffers [i].base);
1150

1151 1152
	for (i = 0; i < UDSL_NUM_RCV_URBS; i++)
		usb_free_urb (instance->receivers [i].urb);
1153

1154
	kfree (instance);
1155 1156

	return -ENOMEM;
1157 1158
}

1159
static void udsl_usb_disconnect (struct usb_interface *intf)
1160
{
1161
	struct udsl_instance_data *instance = usb_get_intfdata (intf);
1162
	struct list_head *pos;
1163
	unsigned int count;
1164
	int result, i;
1165

1166
	dbg ("udsl_usb_disconnect entered");
1167

1168
	usb_set_intfdata (intf, NULL);
1169

1170
	if (!instance) {
1171
		dbg ("udsl_usb_disconnect: NULL instance!");
1172 1173 1174
		return;
	}

1175
	/* receive finalize */
1176
	tasklet_disable (&instance->receive_tasklet);
1177

1178 1179
	for (i = 0; i < UDSL_NUM_RCV_URBS; i++)
		if ((result = usb_unlink_urb (instance->receivers [i].urb)) < 0)
1180
			dbg ("udsl_usb_disconnect: usb_unlink_urb on receive urb %d returned %d", i, result);
1181

1182 1183
	/* wait for completion handlers to finish */
	do {
1184 1185 1186 1187
		count = 0;
		spin_lock_irq (&instance->receive_lock);
		list_for_each (pos, &instance->spare_receivers)
			if (++count > UDSL_NUM_RCV_URBS)
1188
				panic (__FILE__ ": memory corruption detected at line %d!\n", __LINE__);
1189
		spin_unlock_irq (&instance->receive_lock);
1190

1191
		dbg ("udsl_usb_disconnect: found %u spare receivers", count);
1192

1193
		if (count == UDSL_NUM_RCV_URBS)
1194 1195
			break;

1196 1197
		set_current_state (TASK_RUNNING);
		schedule ();
1198 1199
	} while (1);

1200
	/* no need to take the spinlock */
1201 1202
	INIT_LIST_HEAD (&instance->filled_receive_buffers);
	INIT_LIST_HEAD (&instance->spare_receive_buffers);
1203

1204
	tasklet_enable (&instance->receive_tasklet);
1205

1206 1207
	for (i = 0; i < UDSL_NUM_RCV_URBS; i++)
		usb_free_urb (instance->receivers [i].urb);
1208

1209 1210
	for (i = 0; i < UDSL_NUM_RCV_BUFS; i++)
		kfree (instance->receive_buffers [i].base);
1211

1212
	/* send finalize */
1213
	tasklet_disable (&instance->send_tasklet);
1214

1215 1216
	for (i = 0; i < UDSL_NUM_SND_URBS; i++)
		if ((result = usb_unlink_urb (instance->senders [i].urb)) < 0)
1217
			dbg ("udsl_usb_disconnect: usb_unlink_urb on send urb %d returned %d", i, result);
1218

1219 1220 1221
	/* wait for completion handlers to finish */
	do {
		count = 0;
1222
		spin_lock_irq (&instance->send_lock);
1223
		list_for_each (pos, &instance->spare_senders)
1224
			if (++count > UDSL_NUM_SND_URBS)
1225
				panic (__FILE__ ": memory corruption detected at line %d!\n", __LINE__);
1226
		spin_unlock_irq (&instance->send_lock);
1227

1228
		dbg ("udsl_usb_disconnect: found %u spare senders", count);
1229

1230
		if (count == UDSL_NUM_SND_URBS)
1231 1232
			break;

1233 1234
		set_current_state (TASK_RUNNING);
		schedule ();
1235
	} while (1);
1236

1237 1238
	/* no need to take the spinlock */
	INIT_LIST_HEAD (&instance->spare_senders);
1239
	INIT_LIST_HEAD (&instance->spare_send_buffers);
1240 1241
	instance->current_buffer = NULL;

1242
	tasklet_enable (&instance->send_tasklet);
1243

1244 1245
	for (i = 0; i < UDSL_NUM_SND_URBS; i++)
		usb_free_urb (instance->senders [i].urb);
1246

1247 1248
	for (i = 0; i < UDSL_NUM_SND_BUFS; i++)
		kfree (instance->send_buffers [i].base);
1249

1250
	wmb ();
1251 1252
	instance->usb_dev = NULL;

1253
	/* ATM finalize */
1254
	shutdown_atm_dev (instance->atm_dev); /* frees instance, kills tasklets */
1255 1256
}

1257

1258 1259 1260
/***********
**  init  **
***********/
1261

1262
static int __init udsl_usb_init (void)
1263
{
1264 1265
	struct sk_buff *skb; /* dummy for sizeof */

1266
	dbg ("udsl_usb_init: driver version " DRIVER_VERSION);
1267

1268 1269 1270 1271 1272
	if (sizeof (struct udsl_control) > sizeof (skb->cb)) {
		printk (KERN_ERR __FILE__ ": unusable with this kernel!\n");
		return -EIO;
	}

1273 1274 1275
	return usb_register (&udsl_usb_driver);
}

1276
static void __exit udsl_usb_cleanup (void)
1277
{
1278
	dbg ("udsl_usb_cleanup entered");
1279

1280 1281 1282
	usb_deregister (&udsl_usb_driver);
}

1283 1284
module_init (udsl_usb_init);
module_exit (udsl_usb_cleanup);
1285

1286 1287 1288 1289 1290
MODULE_AUTHOR (DRIVER_AUTHOR);
MODULE_DESCRIPTION (DRIVER_DESC);
MODULE_LICENSE ("GPL");


1291 1292 1293
/************
**  debug  **
************/
1294

1295
#ifdef VERBOSE_DEBUG
1296
static int udsl_print_packet (const unsigned char *data, int len)
1297
{
1298
	unsigned char buffer [256];
1299 1300 1301
	int i = 0, j = 0;

	for (i = 0; i < len;) {
1302
		buffer [0] = '\0';
1303 1304
		sprintf (buffer, "%.3d :", i);
		for (j = 0; (j < 16) && (i < len); j++, i++) {
1305
			sprintf (buffer, "%s %2.2x", buffer, data [i]);
1306
		}
1307
		dbg ("%s", buffer);
1308 1309
	}
	return i;
1310
}
1311
#endif