hfc_usb.c 44.0 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3
/*
 * hfc_usb.c
 *
4
 * $Id: hfc_usb.c,v 2.3.2.24 2007/10/14 08:40:29 mbachem Exp $
L
Linus Torvalds 已提交
5 6 7
 *
 * modular HiSax ISDN driver for Colognechip HFC-S USB chip
 *
M
Martin Bachem 已提交
8 9
 * Authors : Peter Sprenger (sprenger@moving-bytes.de)
 *           Martin Bachem (m.bachem@gmx.de, info@colognechip.com)
L
Linus Torvalds 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
 *
 *           based on the first hfc_usb driver of
 *           Werner Cornelius (werner@isdn-development.de)
 *
 * 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, 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * See Version Histroy at the bottom of this file
 *
*/

#include <linux/types.h>
#include <linux/stddef.h>
#include <linux/timer.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel_stat.h>
#include <linux/usb.h>
#include <linux/kernel.h>
M
Martin Bachem 已提交
40 41
#include <linux/sched.h>
#include <linux/moduleparam.h>
L
Linus Torvalds 已提交
42 43 44 45 46
#include "hisax.h"
#include "hisax_if.h"
#include "hfc_usb.h"

static const char *hfcusb_revision =
47
    "$Revision: 2.3.2.24 $ $Date: 2007/10/14 08:40:29 $ ";
L
Linus Torvalds 已提交
48 49

/* Hisax debug support
M
Martin Bachem 已提交
50
*  debug flags defined in hfc_usb.h as HFCUSB_DBG_[*]
L
Linus Torvalds 已提交
51 52 53 54 55
*/
#define __debug_variable hfc_debug
#include "hisax_debug.h"
static u_int debug;
module_param(debug, uint, 0);
56
static int hfc_debug;
M
Martin Bachem 已提交
57

L
Linus Torvalds 已提交
58

M
Martin Bachem 已提交
59 60 61 62 63 64
/* private vendor specific data */
typedef struct {
	__u8 led_scheme;	// led display scheme
	signed short led_bits[8];	// array of 8 possible LED bitmask settings
	char *vend_name;	// device name
} hfcsusb_vdata;
L
Linus Torvalds 已提交
65

M
Martin Bachem 已提交
66
/* VID/PID device list */
M
Martin Bachem 已提交
67 68
static struct usb_device_id hfcusb_idtab[] = {
	{
69
	 USB_DEVICE(0x0959, 0x2bd0),
M
Martin Bachem 已提交
70 71 72 73 74
	 .driver_info = (unsigned long) &((hfcsusb_vdata)
			  {LED_OFF, {4, 0, 2, 1},
			   "ISDN USB TA (Cologne Chip HFC-S USB based)"}),
	},
	{
75
	 USB_DEVICE(0x0675, 0x1688),
M
Martin Bachem 已提交
76 77 78 79 80
	 .driver_info = (unsigned long) &((hfcsusb_vdata)
			  {LED_SCHEME1, {1, 2, 0, 0},
			   "DrayTek miniVigor 128 USB ISDN TA"}),
	},
	{
81
	 USB_DEVICE(0x07b0, 0x0007),
M
Martin Bachem 已提交
82 83 84 85 86
	 .driver_info = (unsigned long) &((hfcsusb_vdata)
			  {LED_SCHEME1, {0x80, -64, -32, -16},
			   "Billion tiny USB ISDN TA 128"}),
	},
	{
87
	 USB_DEVICE(0x0742, 0x2008),
M
Martin Bachem 已提交
88 89 90
	 .driver_info = (unsigned long) &((hfcsusb_vdata)
			  {LED_SCHEME1, {4, 0, 2, 1},
			   "Stollmann USB TA"}),
M
Martin Bachem 已提交
91
	},
M
Martin Bachem 已提交
92
	{
93
	 USB_DEVICE(0x0742, 0x2009),
M
Martin Bachem 已提交
94 95 96
	 .driver_info = (unsigned long) &((hfcsusb_vdata)
			  {LED_SCHEME1, {4, 0, 2, 1},
			   "Aceex USB ISDN TA"}),
M
Martin Bachem 已提交
97
	},
M
Martin Bachem 已提交
98
	{
99
	 USB_DEVICE(0x0742, 0x200A),
M
Martin Bachem 已提交
100 101 102
	 .driver_info = (unsigned long) &((hfcsusb_vdata)
			  {LED_SCHEME1, {4, 0, 2, 1},
			   "OEM USB ISDN TA"}),
M
Martin Bachem 已提交
103
	},
M
Martin Bachem 已提交
104
	{
105
	 USB_DEVICE(0x08e3, 0x0301),
M
Martin Bachem 已提交
106 107 108
	 .driver_info = (unsigned long) &((hfcsusb_vdata)
			  {LED_SCHEME1, {2, 0, 1, 4},
			   "Olitec USB RNIS"}),
M
Martin Bachem 已提交
109
	},
M
Martin Bachem 已提交
110
	{
111
	 USB_DEVICE(0x07fa, 0x0846),
M
Martin Bachem 已提交
112 113 114
	 .driver_info = (unsigned long) &((hfcsusb_vdata)
			  {LED_SCHEME1, {0x80, -64, -32, -16},
			   "Bewan Modem RNIS USB"}),
M
Martin Bachem 已提交
115
	},
M
Martin Bachem 已提交
116
	{
117
	 USB_DEVICE(0x07fa, 0x0847),
M
Martin Bachem 已提交
118 119 120
	 .driver_info = (unsigned long) &((hfcsusb_vdata)
			  {LED_SCHEME1, {0x80, -64, -32, -16},
			   "Djinn Numeris USB"}),
M
Martin Bachem 已提交
121
	},
M
Martin Bachem 已提交
122
	{
123
	 USB_DEVICE(0x07b0, 0x0006),
M
Martin Bachem 已提交
124 125 126
	 .driver_info = (unsigned long) &((hfcsusb_vdata)
			  {LED_SCHEME1, {0x80, -64, -32, -16},
			   "Twister ISDN TA"}),
M
Martin Bachem 已提交
127
	},
128 129 130 131 132 133
	{
	 USB_DEVICE(0x071d, 0x1005),
	 .driver_info = (unsigned long) &((hfcsusb_vdata)
			  {LED_SCHEME1, {0x02, 0, 0x01, 0x04},
			   "Eicon DIVA USB 4.0"}),
	},
134
	{ }
L
Linus Torvalds 已提交
135 136 137 138 139 140 141 142 143 144 145
};

/* structure defining input+output fifos (interrupt/bulk mode) */
struct usb_fifo;		/* forward definition */
typedef struct iso_urb_struct {
	struct urb *purb;
	__u8 buffer[ISO_BUFFER_SIZE];	/* buffer incoming/outgoing data */
	struct usb_fifo *owner_fifo;	/* pointer to owner fifo */
} iso_urb_struct;

struct hfcusb_data;		/* forward definition */
M
Martin Bachem 已提交
146

L
Linus Torvalds 已提交
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
typedef struct usb_fifo {
	int fifonum;		/* fifo index attached to this structure */
	int active;		/* fifo is currently active */
	struct hfcusb_data *hfc;	/* pointer to main structure */
	int pipe;		/* address of endpoint */
	__u8 usb_packet_maxlen;	/* maximum length for usb transfer */
	unsigned int max_size;	/* maximum size of receive/send packet */
	__u8 intervall;		/* interrupt interval */
	struct sk_buff *skbuff;	/* actual used buffer */
	struct urb *urb;	/* transfer structure for usb routines */
	__u8 buffer[128];	/* buffer incoming/outgoing data */
	int bit_line;		/* how much bits are in the fifo? */

	volatile __u8 usb_transfer_mode;	/* switched between ISO and INT */
	iso_urb_struct iso[2];	/* need two urbs to have one always for pending */
	struct hisax_if *hif;	/* hisax interface */
	int delete_flg;		/* only delete skbuff once */
	int last_urblen;	/* remember length of last packet */
} usb_fifo;

/* structure holding all data for one device */
typedef struct hfcusb_data {
	/* HiSax Interface for loadable Layer1 drivers */
M
Martin Bachem 已提交
170
	struct hisax_d_if d_if;		/* see hisax_if.h */
L
Linus Torvalds 已提交
171 172 173 174 175 176 177
	struct hisax_b_if b_if[2];	/* see hisax_if.h */
	int protocol;

	struct usb_device *dev;	/* our device */
	int if_used;		/* used interface number */
	int alt_used;		/* used alternate config */
	int ctrl_paksize;	/* control pipe packet size */
M
Martin Bachem 已提交
178 179
	int ctrl_in_pipe,	/* handles for control pipe */
	    ctrl_out_pipe;
L
Linus Torvalds 已提交
180 181 182 183
	int cfg_used;		/* configuration index used */
	int vend_idx;		/* vendor found */
	int b_mode[2];		/* B-channel mode */
	int l1_activated;	/* layer 1 activated */
M
Martin Bachem 已提交
184
	int disc_flag;		/* TRUE if device was disonnected to avoid some USB actions */
L
Linus Torvalds 已提交
185 186 187 188 189 190 191 192 193 194
	int packet_size, iso_packet_size;

	/* control pipe background handling */
	ctrl_buft ctrl_buff[HFC_CTRL_BUFSIZE];	/* buffer holding queued data */
	volatile int ctrl_in_idx, ctrl_out_idx, ctrl_cnt;	/* input/output pointer + count */
	struct urb *ctrl_urb;	/* transfer structure for control channel */

	struct usb_ctrlrequest ctrl_write;	/* buffer for control write request */
	struct usb_ctrlrequest ctrl_read;	/* same for read request */

195
	__u8 old_led_state, led_state;
L
Linus Torvalds 已提交
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217

	volatile __u8 threshold_mask;	/* threshold actually reported */
	volatile __u8 bch_enables;	/* or mask for sctrl_r and sctrl register values */

	usb_fifo fifos[HFCUSB_NUM_FIFOS];	/* structure holding all fifo data */

	volatile __u8 l1_state;	/* actual l1 state */
	struct timer_list t3_timer;	/* timer 3 for activation/deactivation */
	struct timer_list t4_timer;	/* timer 4 for activation/deactivation */
} hfcusb_data;


static void collect_rx_frame(usb_fifo * fifo, __u8 * data, int len,
			     int finish);

static inline const char *
symbolic(struct hfcusb_symbolic_list list[], const int num)
{
	int i;
	for (i = 0; list[i].name != NULL; i++)
		if (list[i].num == num)
			return (list[i].name);
218
	return "<unknown ERROR>";
L
Linus Torvalds 已提交
219 220 221 222 223 224 225 226 227 228 229
}

static void
ctrl_start_transfer(hfcusb_data * hfc)
{
	if (hfc->ctrl_cnt) {
		hfc->ctrl_urb->pipe = hfc->ctrl_out_pipe;
		hfc->ctrl_urb->setup_packet = (u_char *) & hfc->ctrl_write;
		hfc->ctrl_urb->transfer_buffer = NULL;
		hfc->ctrl_urb->transfer_buffer_length = 0;
		hfc->ctrl_write.wIndex =
230
		    cpu_to_le16(hfc->ctrl_buff[hfc->ctrl_out_idx].hfc_reg);
L
Linus Torvalds 已提交
231
		hfc->ctrl_write.wValue =
232
		    cpu_to_le16(hfc->ctrl_buff[hfc->ctrl_out_idx].reg_val);
L
Linus Torvalds 已提交
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256

		usb_submit_urb(hfc->ctrl_urb, GFP_ATOMIC);	/* start transfer */
	}
}				/* ctrl_start_transfer */

static int
queue_control_request(hfcusb_data * hfc, __u8 reg, __u8 val, int action)
{
	ctrl_buft *buf;

	if (hfc->ctrl_cnt >= HFC_CTRL_BUFSIZE)
		return (1);	/* no space left */
	buf = &hfc->ctrl_buff[hfc->ctrl_in_idx];	/* pointer to new index */
	buf->hfc_reg = reg;
	buf->reg_val = val;
	buf->action = action;
	if (++hfc->ctrl_in_idx >= HFC_CTRL_BUFSIZE)
		hfc->ctrl_in_idx = 0;	/* pointer wrap */
	if (++hfc->ctrl_cnt == 1)
		ctrl_start_transfer(hfc);
	return (0);
}

static void
257
ctrl_complete(struct urb *urb)
L
Linus Torvalds 已提交
258 259 260 261 262 263 264 265 266 267 268 269 270
{
	hfcusb_data *hfc = (hfcusb_data *) urb->context;
	ctrl_buft *buf;

	urb->dev = hfc->dev;
	if (hfc->ctrl_cnt) {
		buf = &hfc->ctrl_buff[hfc->ctrl_out_idx];
		hfc->ctrl_cnt--;	/* decrement actual count */
		if (++hfc->ctrl_out_idx >= HFC_CTRL_BUFSIZE)
			hfc->ctrl_out_idx = 0;	/* pointer wrap */

		ctrl_start_transfer(hfc);	/* start next transfer */
	}
271
}
L
Linus Torvalds 已提交
272 273 274 275 276 277 278 279 280 281 282 283

/* write led data to auxport & invert if necessary */
static void
write_led(hfcusb_data * hfc, __u8 led_state)
{
	if (led_state != hfc->old_led_state) {
		hfc->old_led_state = led_state;
		queue_control_request(hfc, HFCUSB_P_DATA, led_state, 1);
	}
}

static void
284
set_led_bit(hfcusb_data * hfc, signed short led_bits, int on)
L
Linus Torvalds 已提交
285
{
286
	if (on) {
L
Linus Torvalds 已提交
287
		if (led_bits < 0)
288
			hfc->led_state &= ~abs(led_bits);
L
Linus Torvalds 已提交
289
		else
290
			hfc->led_state |= led_bits;
L
Linus Torvalds 已提交
291 292
	} else {
		if (led_bits < 0)
293
			hfc->led_state |= abs(led_bits);
L
Linus Torvalds 已提交
294
		else
295
			hfc->led_state &= ~led_bits;
L
Linus Torvalds 已提交
296 297 298
	}
}

M
Martin Bachem 已提交
299
/* handle LED requests */
L
Linus Torvalds 已提交
300 301 302
static void
handle_led(hfcusb_data * hfc, int event)
{
M
Martin Bachem 已提交
303 304 305
	hfcsusb_vdata *driver_info =
	    (hfcsusb_vdata *) hfcusb_idtab[hfc->vend_idx].driver_info;

L
Linus Torvalds 已提交
306
	/* if no scheme -> no LED action */
M
Martin Bachem 已提交
307
	if (driver_info->led_scheme == LED_OFF)
L
Linus Torvalds 已提交
308 309 310 311
		return;

	switch (event) {
		case LED_POWER_ON:
312 313 314 315
			set_led_bit(hfc, driver_info->led_bits[0], 1);
			set_led_bit(hfc, driver_info->led_bits[1], 0);
			set_led_bit(hfc, driver_info->led_bits[2], 0);
			set_led_bit(hfc, driver_info->led_bits[3], 0);
L
Linus Torvalds 已提交
316
			break;
M
Martin Bachem 已提交
317
		case LED_POWER_OFF:
318 319 320 321
			set_led_bit(hfc, driver_info->led_bits[0], 0);
			set_led_bit(hfc, driver_info->led_bits[1], 0);
			set_led_bit(hfc, driver_info->led_bits[2], 0);
			set_led_bit(hfc, driver_info->led_bits[3], 0);
L
Linus Torvalds 已提交
322 323
			break;
		case LED_S0_ON:
324
			set_led_bit(hfc, driver_info->led_bits[1], 1);
L
Linus Torvalds 已提交
325 326
			break;
		case LED_S0_OFF:
327
			set_led_bit(hfc, driver_info->led_bits[1], 0);
L
Linus Torvalds 已提交
328 329
			break;
		case LED_B1_ON:
330
			set_led_bit(hfc, driver_info->led_bits[2], 1);
L
Linus Torvalds 已提交
331 332
			break;
		case LED_B1_OFF:
333
			set_led_bit(hfc, driver_info->led_bits[2], 0);
L
Linus Torvalds 已提交
334 335
			break;
		case LED_B2_ON:
336
			set_led_bit(hfc, driver_info->led_bits[3], 1);
L
Linus Torvalds 已提交
337 338
			break;
		case LED_B2_OFF:
339
			set_led_bit(hfc, driver_info->led_bits[3], 0);
L
Linus Torvalds 已提交
340 341 342 343 344
			break;
	}
	write_led(hfc, hfc->led_state);
}

M
Martin Bachem 已提交
345
/* ISDN l1 timer T3 expires */
L
Linus Torvalds 已提交
346 347 348 349 350
static void
l1_timer_expire_t3(hfcusb_data * hfc)
{
	hfc->d_if.ifc.l1l2(&hfc->d_if.ifc, PH_DEACTIVATE | INDICATION,
			   NULL);
M
Martin Bachem 已提交
351 352

	DBG(HFCUSB_DBG_STATES,
L
Linus Torvalds 已提交
353
	    "HFC-S USB: PH_DEACTIVATE | INDICATION sent (T3 expire)");
M
Martin Bachem 已提交
354 355

	hfc->l1_activated = 0;
L
Linus Torvalds 已提交
356 357 358 359 360 361
	handle_led(hfc, LED_S0_OFF);
	/* deactivate : */
	queue_control_request(hfc, HFCUSB_STATES, 0x10, 1);
	queue_control_request(hfc, HFCUSB_STATES, 3, 1);
}

M
Martin Bachem 已提交
362
/* ISDN l1 timer T4 expires */
L
Linus Torvalds 已提交
363 364 365 366 367
static void
l1_timer_expire_t4(hfcusb_data * hfc)
{
	hfc->d_if.ifc.l1l2(&hfc->d_if.ifc, PH_DEACTIVATE | INDICATION,
			   NULL);
M
Martin Bachem 已提交
368 369

	DBG(HFCUSB_DBG_STATES,
L
Linus Torvalds 已提交
370
	    "HFC-S USB: PH_DEACTIVATE | INDICATION sent (T4 expire)");
M
Martin Bachem 已提交
371 372

	hfc->l1_activated = 0;
L
Linus Torvalds 已提交
373 374 375
	handle_led(hfc, LED_S0_OFF);
}

M
Martin Bachem 已提交
376
/* S0 state changed */
L
Linus Torvalds 已提交
377
static void
M
Martin Bachem 已提交
378
s0_state_handler(hfcusb_data * hfc, __u8 state)
L
Linus Torvalds 已提交
379 380 381 382 383 384 385
{
	__u8 old_state;

	old_state = hfc->l1_state;
	if (state == old_state || state < 1 || state > 8)
		return;

M
Martin Bachem 已提交
386 387 388
	DBG(HFCUSB_DBG_STATES, "HFC-S USB: S0 statechange(%d -> %d)",
	    old_state, state);

L
Linus Torvalds 已提交
389 390 391
	if (state < 4 || state == 7 || state == 8) {
		if (timer_pending(&hfc->t3_timer))
			del_timer(&hfc->t3_timer);
M
Martin Bachem 已提交
392
		DBG(HFCUSB_DBG_STATES, "HFC-S USB: T3 deactivated");
L
Linus Torvalds 已提交
393 394 395 396
	}
	if (state >= 7) {
		if (timer_pending(&hfc->t4_timer))
			del_timer(&hfc->t4_timer);
M
Martin Bachem 已提交
397
		DBG(HFCUSB_DBG_STATES, "HFC-S USB: T4 deactivated");
L
Linus Torvalds 已提交
398 399 400 401 402
	}

	if (state == 7 && !hfc->l1_activated) {
		hfc->d_if.ifc.l1l2(&hfc->d_if.ifc,
				   PH_ACTIVATE | INDICATION, NULL);
M
Martin Bachem 已提交
403 404
		DBG(HFCUSB_DBG_STATES, "HFC-S USB: PH_ACTIVATE | INDICATION sent");
		hfc->l1_activated = 1;
L
Linus Torvalds 已提交
405 406 407
		handle_led(hfc, LED_S0_ON);
	} else if (state <= 3 /* && activated */ ) {
		if (old_state == 7 || old_state == 8) {
M
Martin Bachem 已提交
408
			DBG(HFCUSB_DBG_STATES, "HFC-S USB: T4 activated");
L
Linus Torvalds 已提交
409 410 411 412 413 414 415 416 417
			if (!timer_pending(&hfc->t4_timer)) {
				hfc->t4_timer.expires =
				    jiffies + (HFC_TIMER_T4 * HZ) / 1000;
				add_timer(&hfc->t4_timer);
			}
		} else {
			hfc->d_if.ifc.l1l2(&hfc->d_if.ifc,
					   PH_DEACTIVATE | INDICATION,
					   NULL);
M
Martin Bachem 已提交
418
			DBG(HFCUSB_DBG_STATES,
L
Linus Torvalds 已提交
419
			    "HFC-S USB: PH_DEACTIVATE | INDICATION sent");
M
Martin Bachem 已提交
420
			hfc->l1_activated = 0;
L
Linus Torvalds 已提交
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
			handle_led(hfc, LED_S0_OFF);
		}
	}
	hfc->l1_state = state;
}

static void
fill_isoc_urb(struct urb *urb, struct usb_device *dev, unsigned int pipe,
	      void *buf, int num_packets, int packet_size, int interval,
	      usb_complete_t complete, void *context)
{
	int k;

	urb->dev = dev;
	urb->pipe = pipe;
	urb->complete = complete;
	urb->number_of_packets = num_packets;
	urb->transfer_buffer_length = packet_size * num_packets;
	urb->context = context;
	urb->transfer_buffer = buf;
	urb->transfer_flags = URB_ISO_ASAP;
	urb->actual_length = 0;
	urb->interval = interval;
	for (k = 0; k < num_packets; k++) {
		urb->iso_frame_desc[k].offset = packet_size * k;
		urb->iso_frame_desc[k].length = packet_size;
		urb->iso_frame_desc[k].actual_length = 0;
	}
}

/* allocs urbs and start isoc transfer with two pending urbs to avoid
M
Martin Bachem 已提交
452 453
 * gaps in the transfer chain
 */
L
Linus Torvalds 已提交
454 455 456 457 458 459
static int
start_isoc_chain(usb_fifo * fifo, int num_packets_per_urb,
		 usb_complete_t complete, int packet_size)
{
	int i, k, errcode;

M
Martin Bachem 已提交
460 461
	DBG(HFCUSB_DBG_INIT, "HFC-S USB: starting ISO-URBs for fifo:%d\n",
	    fifo->fifonum);
L
Linus Torvalds 已提交
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505

	/* allocate Memory for Iso out Urbs */
	for (i = 0; i < 2; i++) {
		if (!(fifo->iso[i].purb)) {
			fifo->iso[i].purb =
			    usb_alloc_urb(num_packets_per_urb, GFP_KERNEL);
			if (!(fifo->iso[i].purb)) {
				printk(KERN_INFO
				       "alloc urb for fifo %i failed!!!",
				       fifo->fifonum);
			}
			fifo->iso[i].owner_fifo = (struct usb_fifo *) fifo;

			/* Init the first iso */
			if (ISO_BUFFER_SIZE >=
			    (fifo->usb_packet_maxlen *
			     num_packets_per_urb)) {
				fill_isoc_urb(fifo->iso[i].purb,
					      fifo->hfc->dev, fifo->pipe,
					      fifo->iso[i].buffer,
					      num_packets_per_urb,
					      fifo->usb_packet_maxlen,
					      fifo->intervall, complete,
					      &fifo->iso[i]);
				memset(fifo->iso[i].buffer, 0,
				       sizeof(fifo->iso[i].buffer));
				/* defining packet delimeters in fifo->buffer */
				for (k = 0; k < num_packets_per_urb; k++) {
					fifo->iso[i].purb->
					    iso_frame_desc[k].offset =
					    k * packet_size;
					fifo->iso[i].purb->
					    iso_frame_desc[k].length =
					    packet_size;
				}
			} else {
				printk(KERN_INFO
				       "HFC-S USB: ISO Buffer size to small!\n");
			}
		}
		fifo->bit_line = BITLINE_INF;

		errcode = usb_submit_urb(fifo->iso[i].purb, GFP_KERNEL);
		fifo->active = (errcode >= 0) ? 1 : 0;
M
Martin Bachem 已提交
506 507 508
		if (errcode < 0)
			printk(KERN_INFO "HFC-S USB: usb_submit_urb URB nr:%d, error(%i): '%s'\n",
			       i, errcode, symbolic(urb_errlist, errcode));
L
Linus Torvalds 已提交
509 510 511 512 513 514 515 516 517 518 519 520
	}
	return (fifo->active);
}

/* stops running iso chain and frees their pending urbs */
static void
stop_isoc_chain(usb_fifo * fifo)
{
	int i;

	for (i = 0; i < 2; i++) {
		if (fifo->iso[i].purb) {
M
Martin Bachem 已提交
521
			DBG(HFCUSB_DBG_INIT,
L
Linus Torvalds 已提交
522 523
			    "HFC-S USB: Stopping iso chain for fifo %i.%i",
			    fifo->fifonum, i);
524
			usb_kill_urb(fifo->iso[i].purb);
L
Linus Torvalds 已提交
525 526 527 528
			usb_free_urb(fifo->iso[i].purb);
			fifo->iso[i].purb = NULL;
		}
	}
M
Martin Bachem 已提交
529

530 531 532
	usb_kill_urb(fifo->urb);
	usb_free_urb(fifo->urb);
	fifo->urb = NULL;
L
Linus Torvalds 已提交
533 534 535 536 537 538 539 540 541 542
	fifo->active = 0;
}

/* defines how much ISO packets are handled in one URB */
static int iso_packets[8] =
    { ISOC_PACKETS_B, ISOC_PACKETS_B, ISOC_PACKETS_B, ISOC_PACKETS_B,
	ISOC_PACKETS_D, ISOC_PACKETS_D, ISOC_PACKETS_D, ISOC_PACKETS_D
};

static void
543
tx_iso_complete(struct urb *urb)
L
Linus Torvalds 已提交
544 545 546 547 548 549 550 551 552 553 554 555 556 557
{
	iso_urb_struct *context_iso_urb = (iso_urb_struct *) urb->context;
	usb_fifo *fifo = context_iso_urb->owner_fifo;
	hfcusb_data *hfc = fifo->hfc;
	int k, tx_offset, num_isoc_packets, sink, len, current_len,
	    errcode;
	int frame_complete, transp_mode, fifon, status;
	__u8 threshbit;

	fifon = fifo->fifonum;
	status = urb->status;

	tx_offset = 0;

M
Martin Bachem 已提交
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
	/* ISO transfer only partially completed,
	   look at individual frame status for details */
	if (status == -EXDEV) {
		DBG(HFCUSB_DBG_VERBOSE_USB, "HFC-S USB: tx_iso_complete with -EXDEV"
		    ", urb->status %d, fifonum %d\n",
		    status, fifon);

		for (k = 0; k < iso_packets[fifon]; ++k) {
			errcode = urb->iso_frame_desc[k].status;
			if (errcode)
				DBG(HFCUSB_DBG_VERBOSE_USB, "HFC-S USB: tx_iso_complete "
				       "packet %i, status: %i\n",
				       k, errcode);
		}

		// clear status, so go on with ISO transfers
		status = 0;
	}

L
Linus Torvalds 已提交
577 578 579
	if (fifo->active && !status) {
		transp_mode = 0;
		if (fifon < 4 && hfc->b_mode[fifon / 2] == L1_MODE_TRANS)
M
Martin Bachem 已提交
580
			transp_mode = 1;
L
Linus Torvalds 已提交
581 582

		/* is FifoFull-threshold set for our channel? */
M
Martin Bachem 已提交
583
		threshbit = (hfc->threshold_mask & (1 << fifon));
L
Linus Torvalds 已提交
584 585 586 587 588 589 590 591 592 593 594 595 596 597
		num_isoc_packets = iso_packets[fifon];

		/* predict dataflow to avoid fifo overflow */
		if (fifon >= HFCUSB_D_TX) {
			sink = (threshbit) ? SINK_DMIN : SINK_DMAX;
		} else {
			sink = (threshbit) ? SINK_MIN : SINK_MAX;
		}
		fill_isoc_urb(urb, fifo->hfc->dev, fifo->pipe,
			      context_iso_urb->buffer, num_isoc_packets,
			      fifo->usb_packet_maxlen, fifo->intervall,
			      tx_iso_complete, urb->context);
		memset(context_iso_urb->buffer, 0,
		       sizeof(context_iso_urb->buffer));
M
Martin Bachem 已提交
598 599 600
		frame_complete = 0;

		/* Generate next ISO Packets */
L
Linus Torvalds 已提交
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
		for (k = 0; k < num_isoc_packets; ++k) {
			if (fifo->skbuff) {
				len = fifo->skbuff->len;
				/* we lower data margin every msec */
				fifo->bit_line -= sink;
				current_len = (0 - fifo->bit_line) / 8;
				/* maximum 15 byte for every ISO packet makes our life easier */
				if (current_len > 14)
					current_len = 14;
				current_len =
				    (len <=
				     current_len) ? len : current_len;
				/* how much bit do we put on the line? */
				fifo->bit_line += current_len * 8;

				context_iso_urb->buffer[tx_offset] = 0;
				if (current_len == len) {
					if (!transp_mode) {
						/* here frame completion */
						context_iso_urb->
						    buffer[tx_offset] = 1;
						/* add 2 byte flags and 16bit CRC at end of ISDN frame */
						fifo->bit_line += 32;
					}
M
Martin Bachem 已提交
625
					frame_complete = 1;
L
Linus Torvalds 已提交
626 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
				}

				memcpy(context_iso_urb->buffer +
				       tx_offset + 1, fifo->skbuff->data,
				       current_len);
				skb_pull(fifo->skbuff, current_len);

				/* define packet delimeters within the URB buffer */
				urb->iso_frame_desc[k].offset = tx_offset;
				urb->iso_frame_desc[k].length =
				    current_len + 1;

				tx_offset += (current_len + 1);
			} else {
				urb->iso_frame_desc[k].offset =
				    tx_offset++;

				urb->iso_frame_desc[k].length = 1;
				fifo->bit_line -= sink;	/* we lower data margin every msec */

				if (fifo->bit_line < BITLINE_INF) {
					fifo->bit_line = BITLINE_INF;
				}
			}

			if (frame_complete) {
M
Martin Bachem 已提交
652
				fifo->delete_flg = 1;
L
Linus Torvalds 已提交
653 654
				fifo->hif->l1l2(fifo->hif,
						PH_DATA | CONFIRM,
A
Alan Cox 已提交
655
						(void *) (unsigned long) fifo->skbuff->
L
Linus Torvalds 已提交
656 657 658 659
						truesize);
				if (fifo->skbuff && fifo->delete_flg) {
					dev_kfree_skb_any(fifo->skbuff);
					fifo->skbuff = NULL;
M
Martin Bachem 已提交
660
					fifo->delete_flg = 0;
L
Linus Torvalds 已提交
661
				}
M
Martin Bachem 已提交
662
				frame_complete = 0;
L
Linus Torvalds 已提交
663 664 665 666 667
			}
		}
		errcode = usb_submit_urb(urb, GFP_ATOMIC);
		if (errcode < 0) {
			printk(KERN_INFO
M
Martin Bachem 已提交
668
			       "HFC-S USB: error submitting ISO URB: %d\n",
L
Linus Torvalds 已提交
669 670 671 672 673
			       errcode);
		}
	} else {
		if (status && !hfc->disc_flag) {
			printk(KERN_INFO
M
Martin Bachem 已提交
674 675
			       "HFC-S USB: tx_iso_complete: error(%i): '%s', fifonum=%d\n",
			       status, symbolic(urb_errlist, status), fifon);
L
Linus Torvalds 已提交
676 677
		}
	}
M
Martin Bachem 已提交
678
}
L
Linus Torvalds 已提交
679 680

static void
681
rx_iso_complete(struct urb *urb)
L
Linus Torvalds 已提交
682 683 684 685 686 687 688 689 690 691 692 693 694 695
{
	iso_urb_struct *context_iso_urb = (iso_urb_struct *) urb->context;
	usb_fifo *fifo = context_iso_urb->owner_fifo;
	hfcusb_data *hfc = fifo->hfc;
	int k, len, errcode, offset, num_isoc_packets, fifon, maxlen,
	    status;
	unsigned int iso_status;
	__u8 *buf;
	static __u8 eof[8];

	fifon = fifo->fifonum;
	status = urb->status;

	if (urb->status == -EOVERFLOW) {
M
Martin Bachem 已提交
696 697 698 699 700 701 702 703 704 705 706
		DBG(HFCUSB_DBG_VERBOSE_USB,
		    "HFC-USB: ignoring USB DATAOVERRUN fifo(%i)", fifon);
		status = 0;
	}

	/* ISO transfer only partially completed,
	   look at individual frame status for details */
	if (status == -EXDEV) {
		DBG(HFCUSB_DBG_VERBOSE_USB, "HFC-S USB: rx_iso_complete with -EXDEV "
		    "urb->status %d, fifonum %d\n",
		    status, fifon);
L
Linus Torvalds 已提交
707 708
		status = 0;
	}
M
Martin Bachem 已提交
709

L
Linus Torvalds 已提交
710 711 712 713 714 715 716 717
	if (fifo->active && !status) {
		num_isoc_packets = iso_packets[fifon];
		maxlen = fifo->usb_packet_maxlen;
		for (k = 0; k < num_isoc_packets; ++k) {
			len = urb->iso_frame_desc[k].actual_length;
			offset = urb->iso_frame_desc[k].offset;
			buf = context_iso_urb->buffer + offset;
			iso_status = urb->iso_frame_desc[k].status;
M
Martin Bachem 已提交
718

L
Linus Torvalds 已提交
719
			if (iso_status && !hfc->disc_flag)
M
Martin Bachem 已提交
720 721 722 723
				DBG(HFCUSB_DBG_VERBOSE_USB,
				    "HFC-S USB: rx_iso_complete "
				    "ISO packet %i, status: %i\n",
				    k, iso_status);
L
Linus Torvalds 已提交
724

M
Martin Bachem 已提交
725 726
			if (fifon == HFCUSB_D_RX) {
				DBG(HFCUSB_DBG_VERBOSE_USB,
L
Linus Torvalds 已提交
727
				       "HFC-S USB: ISO-D-RX lst_urblen:%2d "
M
Martin Bachem 已提交
728
				       "act_urblen:%2d max-urblen:%2d EOF:0x%0x",
L
Linus Torvalds 已提交
729 730
				       fifo->last_urblen, len, maxlen,
				       eof[5]);
M
Martin Bachem 已提交
731 732

				DBG_PACKET(HFCUSB_DBG_VERBOSE_USB, buf, len);
L
Linus Torvalds 已提交
733
			}
M
Martin Bachem 已提交
734

L
Linus Torvalds 已提交
735 736 737 738 739
			if (fifo->last_urblen != maxlen) {
				/* the threshold mask is in the 2nd status byte */
				hfc->threshold_mask = buf[1];
				/* care for L1 state only for D-Channel
				   to avoid overlapped iso completions */
M
Martin Bachem 已提交
740
				if (fifon == HFCUSB_D_RX) {
L
Linus Torvalds 已提交
741 742
					/* the S0 state is in the upper half
					   of the 1st status byte */
M
Martin Bachem 已提交
743
					s0_state_handler(hfc, buf[0] >> 4);
L
Linus Torvalds 已提交
744 745 746 747 748
				}
				eof[fifon] = buf[0] & 1;
				if (len > 2)
					collect_rx_frame(fifo, buf + 2,
							 len - 2,
M
Martin Bachem 已提交
749
							 (len < maxlen) ?
L
Linus Torvalds 已提交
750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
							 eof[fifon] : 0);
			} else {
				collect_rx_frame(fifo, buf, len,
						 (len <
						  maxlen) ? eof[fifon] :
						 0);
			}
			fifo->last_urblen = len;
		}

		fill_isoc_urb(urb, fifo->hfc->dev, fifo->pipe,
			      context_iso_urb->buffer, num_isoc_packets,
			      fifo->usb_packet_maxlen, fifo->intervall,
			      rx_iso_complete, urb->context);
		errcode = usb_submit_urb(urb, GFP_ATOMIC);
		if (errcode < 0) {
M
Martin Bachem 已提交
766 767
			printk(KERN_ERR
			       "HFC-S USB: error submitting ISO URB: %d\n",
L
Linus Torvalds 已提交
768 769 770 771
			       errcode);
		}
	} else {
		if (status && !hfc->disc_flag) {
M
Martin Bachem 已提交
772
			printk(KERN_ERR
L
Linus Torvalds 已提交
773 774 775 776 777
			       "HFC-S USB: rx_iso_complete : "
			       "urb->status %d, fifonum %d\n",
			       status, fifon);
		}
	}
M
Martin Bachem 已提交
778
}
L
Linus Torvalds 已提交
779

M
Martin Bachem 已提交
780
/* collect rx data from INT- and ISO-URBs  */
L
Linus Torvalds 已提交
781 782 783 784 785
static void
collect_rx_frame(usb_fifo * fifo, __u8 * data, int len, int finish)
{
	hfcusb_data *hfc = fifo->hfc;
	int transp_mode, fifon;
M
Martin Bachem 已提交
786

L
Linus Torvalds 已提交
787 788 789
	fifon = fifo->fifonum;
	transp_mode = 0;
	if (fifon < 4 && hfc->b_mode[fifon / 2] == L1_MODE_TRANS)
M
Martin Bachem 已提交
790
		transp_mode = 1;
L
Linus Torvalds 已提交
791 792 793 794

	if (!fifo->skbuff) {
		fifo->skbuff = dev_alloc_skb(fifo->max_size + 3);
		if (!fifo->skbuff) {
M
Martin Bachem 已提交
795 796
			printk(KERN_ERR
			       "HFC-S USB: cannot allocate buffer for fifo(%d)\n",
L
Linus Torvalds 已提交
797 798 799 800 801 802 803 804
			       fifon);
			return;
		}
	}
	if (len) {
		if (fifo->skbuff->len + len < fifo->max_size) {
			memcpy(skb_put(fifo->skbuff, len), data, len);
		} else {
M
Martin Bachem 已提交
805 806
			DBG(HFCUSB_DBG_FIFO_ERR,
			       "HCF-USB: got frame exceeded fifo->max_size(%d) fifo(%d)",
L
Linus Torvalds 已提交
807
			       fifo->max_size, fifon);
M
Martin Bachem 已提交
808 809
			DBG_SKB(HFCUSB_DBG_VERBOSE_USB, fifo->skbuff);
			skb_trim(fifo->skbuff, 0);
L
Linus Torvalds 已提交
810 811 812 813 814 815 816 817 818 819
		}
	}
	if (transp_mode && fifo->skbuff->len >= 128) {
		fifo->hif->l1l2(fifo->hif, PH_DATA | INDICATION,
				fifo->skbuff);
		fifo->skbuff = NULL;
		return;
	}
	/* we have a complete hdlc packet */
	if (finish) {
820 821
		if (fifo->skbuff->len > 3 &&
				!fifo->skbuff->data[fifo->skbuff->len - 1]) {
M
Martin Bachem 已提交
822 823 824 825 826 827 828

			if (fifon == HFCUSB_D_RX) {
				DBG(HFCUSB_DBG_DCHANNEL,
				    "HFC-S USB: D-RX len(%d)", fifo->skbuff->len);
				DBG_SKB(HFCUSB_DBG_DCHANNEL, fifo->skbuff);
			}

L
Linus Torvalds 已提交
829 830 831 832 833 834 835 836 837 838 839 840
			/* remove CRC & status */
			skb_trim(fifo->skbuff, fifo->skbuff->len - 3);
			if (fifon == HFCUSB_PCM_RX) {
				fifo->hif->l1l2(fifo->hif,
						PH_DATA_E | INDICATION,
						fifo->skbuff);
			} else
				fifo->hif->l1l2(fifo->hif,
						PH_DATA | INDICATION,
						fifo->skbuff);
			fifo->skbuff = NULL;	/* buffer was freed from upper layer */
		} else {
M
Martin Bachem 已提交
841 842 843 844
			DBG(HFCUSB_DBG_FIFO_ERR,
			    "HFC-S USB: ERROR frame len(%d) fifo(%d)",
			    fifo->skbuff->len, fifon);
			DBG_SKB(HFCUSB_DBG_VERBOSE_USB, fifo->skbuff);
L
Linus Torvalds 已提交
845 846 847 848 849 850
			skb_trim(fifo->skbuff, 0);
		}
	}
}

static void
M
Martin Bachem 已提交
851
rx_int_complete(struct urb *urb)
L
Linus Torvalds 已提交
852 853 854 855 856 857 858 859 860 861 862 863
{
	int len;
	int status;
	__u8 *buf, maxlen, fifon;
	usb_fifo *fifo = (usb_fifo *) urb->context;
	hfcusb_data *hfc = fifo->hfc;
	static __u8 eof[8];

	urb->dev = hfc->dev;	/* security init */

	fifon = fifo->fifonum;
	if ((!fifo->active) || (urb->status)) {
M
Martin Bachem 已提交
864
		DBG(HFCUSB_DBG_INIT, "HFC-S USB: RX-Fifo %i is going down (%i)",
L
Linus Torvalds 已提交
865
		    fifon, urb->status);
M
Martin Bachem 已提交
866

L
Linus Torvalds 已提交
867 868 869 870 871 872 873 874 875 876 877
		fifo->urb->interval = 0;	/* cancel automatic rescheduling */
		if (fifo->skbuff) {
			dev_kfree_skb_any(fifo->skbuff);
			fifo->skbuff = NULL;
		}
		return;
	}
	len = urb->actual_length;
	buf = fifo->buffer;
	maxlen = fifo->usb_packet_maxlen;

M
Martin Bachem 已提交
878 879 880 881 882 883 884
	if (fifon == HFCUSB_D_RX) {
		DBG(HFCUSB_DBG_VERBOSE_USB,
		       "HFC-S USB: INT-D-RX lst_urblen:%2d "
		       "act_urblen:%2d max-urblen:%2d EOF:0x%0x",
		       fifo->last_urblen, len, maxlen,
		       eof[5]);
		DBG_PACKET(HFCUSB_DBG_VERBOSE_USB, buf, len);
L
Linus Torvalds 已提交
885 886 887 888 889 890
	}

	if (fifo->last_urblen != fifo->usb_packet_maxlen) {
		/* the threshold mask is in the 2nd status byte */
		hfc->threshold_mask = buf[1];
		/* the S0 state is in the upper half of the 1st status byte */
M
Martin Bachem 已提交
891
		s0_state_handler(hfc, buf[0] >> 4);
L
Linus Torvalds 已提交
892 893 894 895 896 897 898 899 900 901 902 903 904 905
		eof[fifon] = buf[0] & 1;
		/* if we have more than the 2 status bytes -> collect data */
		if (len > 2)
			collect_rx_frame(fifo, buf + 2,
					 urb->actual_length - 2,
					 (len < maxlen) ? eof[fifon] : 0);
	} else {
		collect_rx_frame(fifo, buf, urb->actual_length,
				 (len < maxlen) ? eof[fifon] : 0);
	}
	fifo->last_urblen = urb->actual_length;
	status = usb_submit_urb(urb, GFP_ATOMIC);
	if (status) {
		printk(KERN_INFO
M
Martin Bachem 已提交
906
		       "HFC-S USB: %s error resubmitting URB fifo(%d)\n",
907
		       __func__, fifon);
L
Linus Torvalds 已提交
908
	}
M
Martin Bachem 已提交
909
}
L
Linus Torvalds 已提交
910

M
Martin Bachem 已提交
911
/* start initial INT-URB for certain fifo */
L
Linus Torvalds 已提交
912 913 914 915 916
static void
start_int_fifo(usb_fifo * fifo)
{
	int errcode;

M
Martin Bachem 已提交
917 918
	DBG(HFCUSB_DBG_INIT, "HFC-S USB: starting RX INT-URB for fifo:%d\n",
	    fifo->fifonum);
L
Linus Torvalds 已提交
919 920 921 922 923 924 925 926

	if (!fifo->urb) {
		fifo->urb = usb_alloc_urb(0, GFP_KERNEL);
		if (!fifo->urb)
			return;
	}
	usb_fill_int_urb(fifo->urb, fifo->hfc->dev, fifo->pipe,
			 fifo->buffer, fifo->usb_packet_maxlen,
M
Martin Bachem 已提交
927
			 rx_int_complete, fifo, fifo->intervall);
L
Linus Torvalds 已提交
928 929 930
	fifo->active = 1;	/* must be marked active */
	errcode = usb_submit_urb(fifo->urb, GFP_KERNEL);
	if (errcode) {
M
Martin Bachem 已提交
931
		printk(KERN_ERR
L
Linus Torvalds 已提交
932 933 934 935 936
		       "HFC-S USB: submit URB error(start_int_info): status:%i\n",
		       errcode);
		fifo->active = 0;
		fifo->skbuff = NULL;
	}
M
Martin Bachem 已提交
937
}
L
Linus Torvalds 已提交
938 939

static void
M
Martin Bachem 已提交
940
setup_bchannel(hfcusb_data * hfc, int channel, int mode)
L
Linus Torvalds 已提交
941 942 943 944 945 946
{
	__u8 val, idx_table[2] = { 0, 2 };

	if (hfc->disc_flag) {
		return;
	}
M
Martin Bachem 已提交
947 948
	DBG(HFCUSB_DBG_STATES, "HFC-S USB: setting channel %d to mode %d",
	    channel, mode);
L
Linus Torvalds 已提交
949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995
	hfc->b_mode[channel] = mode;

	/* setup CON_HDLC */
	val = 0;
	if (mode != L1_MODE_NULL)
		val = 8;	/* enable fifo? */
	if (mode == L1_MODE_TRANS)
		val |= 2;	/* set transparent bit */

	/* set FIFO to transmit register */
	queue_control_request(hfc, HFCUSB_FIFO, idx_table[channel], 1);
	queue_control_request(hfc, HFCUSB_CON_HDLC, val, 1);
	/* reset fifo */
	queue_control_request(hfc, HFCUSB_INC_RES_F, 2, 1);
	/* set FIFO to receive register */
	queue_control_request(hfc, HFCUSB_FIFO, idx_table[channel] + 1, 1);
	queue_control_request(hfc, HFCUSB_CON_HDLC, val, 1);
	/* reset fifo */
	queue_control_request(hfc, HFCUSB_INC_RES_F, 2, 1);

	val = 0x40;
	if (hfc->b_mode[0])
		val |= 1;
	if (hfc->b_mode[1])
		val |= 2;
	queue_control_request(hfc, HFCUSB_SCTRL, val, 1);

	val = 0;
	if (hfc->b_mode[0])
		val |= 1;
	if (hfc->b_mode[1])
		val |= 2;
	queue_control_request(hfc, HFCUSB_SCTRL_R, val, 1);

	if (mode == L1_MODE_NULL) {
		if (channel)
			handle_led(hfc, LED_B2_OFF);
		else
			handle_led(hfc, LED_B1_OFF);
	} else {
		if (channel)
			handle_led(hfc, LED_B2_ON);
		else
			handle_led(hfc, LED_B1_ON);
	}
}

996
static void
L
Linus Torvalds 已提交
997 998 999 1000 1001 1002 1003 1004
hfc_usb_l2l1(struct hisax_if *my_hisax_if, int pr, void *arg)
{
	usb_fifo *fifo = my_hisax_if->priv;
	hfcusb_data *hfc = fifo->hfc;

	switch (pr) {
		case PH_ACTIVATE | REQUEST:
			if (fifo->fifonum == HFCUSB_D_TX) {
M
Martin Bachem 已提交
1005
				DBG(HFCUSB_DBG_STATES,
L
Linus Torvalds 已提交
1006
				    "HFC_USB: hfc_usb_d_l2l1 D-chan: PH_ACTIVATE | REQUEST");
M
Martin Bachem 已提交
1007

L
Linus Torvalds 已提交
1008 1009 1010 1011 1012 1013
				if (hfc->l1_state != 3
				    && hfc->l1_state != 7) {
					hfc->d_if.ifc.l1l2(&hfc->d_if.ifc,
							   PH_DEACTIVATE |
							   INDICATION,
							   NULL);
M
Martin Bachem 已提交
1014
					DBG(HFCUSB_DBG_STATES,
L
Linus Torvalds 已提交
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
					    "HFC-S USB: PH_DEACTIVATE | INDICATION sent (not state 3 or 7)");
				} else {
					if (hfc->l1_state == 7) {	/* l1 already active */
						hfc->d_if.ifc.l1l2(&hfc->
								   d_if.
								   ifc,
								   PH_ACTIVATE
								   |
								   INDICATION,
								   NULL);
M
Martin Bachem 已提交
1025
						DBG(HFCUSB_DBG_STATES,
L
Linus Torvalds 已提交
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
						    "HFC-S USB: PH_ACTIVATE | INDICATION sent again ;)");
					} else {
						/* force sending sending INFO1 */
						queue_control_request(hfc,
								      HFCUSB_STATES,
								      0x14,
								      1);
						mdelay(1);
						/* start l1 activation */
						queue_control_request(hfc,
								      HFCUSB_STATES,
								      0x04,
								      1);
						if (!timer_pending
						    (&hfc->t3_timer)) {
							hfc->t3_timer.
							    expires =
							    jiffies +
							    (HFC_TIMER_T3 *
							     HZ) / 1000;
							add_timer(&hfc->
								  t3_timer);
						}
					}
				}
			} else {
M
Martin Bachem 已提交
1052 1053 1054
				DBG(HFCUSB_DBG_STATES,
				    "HFC_USB: hfc_usb_d_l2l1 B-chan: PH_ACTIVATE | REQUEST");
				setup_bchannel(hfc,
L
Linus Torvalds 已提交
1055 1056
					    (fifo->fifonum ==
					     HFCUSB_B1_TX) ? 0 : 1,
A
Alan Cox 已提交
1057
					    (long) arg);
L
Linus Torvalds 已提交
1058 1059 1060 1061 1062 1063 1064
				fifo->hif->l1l2(fifo->hif,
						PH_ACTIVATE | INDICATION,
						NULL);
			}
			break;
		case PH_DEACTIVATE | REQUEST:
			if (fifo->fifonum == HFCUSB_D_TX) {
M
Martin Bachem 已提交
1065
				DBG(HFCUSB_DBG_STATES,
L
Linus Torvalds 已提交
1066 1067
				    "HFC_USB: hfc_usb_d_l2l1 D-chan: PH_DEACTIVATE | REQUEST");
			} else {
M
Martin Bachem 已提交
1068
				DBG(HFCUSB_DBG_STATES,
L
Linus Torvalds 已提交
1069
				    "HFC_USB: hfc_usb_d_l2l1 Bx-chan: PH_DEACTIVATE | REQUEST");
M
Martin Bachem 已提交
1070
				setup_bchannel(hfc,
L
Linus Torvalds 已提交
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
					    (fifo->fifonum ==
					     HFCUSB_B1_TX) ? 0 : 1,
					    (int) L1_MODE_NULL);
				fifo->hif->l1l2(fifo->hif,
						PH_DEACTIVATE | INDICATION,
						NULL);
			}
			break;
		case PH_DATA | REQUEST:
			if (fifo->skbuff && fifo->delete_flg) {
				dev_kfree_skb_any(fifo->skbuff);
				fifo->skbuff = NULL;
M
Martin Bachem 已提交
1083
				fifo->delete_flg = 0;
L
Linus Torvalds 已提交
1084 1085 1086 1087
			}
			fifo->skbuff = arg;	/* we have a new buffer */
			break;
		default:
M
Martin Bachem 已提交
1088 1089
			DBG(HFCUSB_DBG_STATES,
			       "HFC_USB: hfc_usb_d_l2l1: unkown state : %#x", pr);
L
Linus Torvalds 已提交
1090 1091 1092 1093
			break;
	}
}

M
Martin Bachem 已提交
1094
/* initial init HFC-S USB chip registers, HiSax interface, USB URBs */
L
Linus Torvalds 已提交
1095
static int
M
Martin Bachem 已提交
1096
hfc_usb_init(hfcusb_data * hfc)
L
Linus Torvalds 已提交
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120
{
	usb_fifo *fifo;
	int i, err;
	u_char b;
	struct hisax_b_if *p_b_if[2];

	/* check the chip id */
	if (read_usb(hfc, HFCUSB_CHIP_ID, &b) != 1) {
		printk(KERN_INFO "HFC-USB: cannot read chip id\n");
		return (1);
	}
	if (b != HFCUSB_CHIPID) {
		printk(KERN_INFO "HFC-S USB: Invalid chip id 0x%02x\n", b);
		return (1);
	}

	/* first set the needed config, interface and alternate */
	err = usb_set_interface(hfc->dev, hfc->if_used, hfc->alt_used);

	/* do Chip reset */
	write_usb(hfc, HFCUSB_CIRM, 8);
	/* aux = output, reset off */
	write_usb(hfc, HFCUSB_CIRM, 0x10);

M
Martin Bachem 已提交
1121
	/* set USB_SIZE to match wMaxPacketSize for INT or BULK transfers */
L
Linus Torvalds 已提交
1122 1123 1124
	write_usb(hfc, HFCUSB_USB_SIZE,
		  (hfc->packet_size / 8) | ((hfc->packet_size / 8) << 4));

M
Martin Bachem 已提交
1125
	/* set USB_SIZE_I to match wMaxPacketSize for ISO transfers */
L
Linus Torvalds 已提交
1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163
	write_usb(hfc, HFCUSB_USB_SIZE_I, hfc->iso_packet_size);

	/* enable PCM/GCI master mode */
	write_usb(hfc, HFCUSB_MST_MODE1, 0);	/* set default values */
	write_usb(hfc, HFCUSB_MST_MODE0, 1);	/* enable master mode */

	/* init the fifos */
	write_usb(hfc, HFCUSB_F_THRES,
		  (HFCUSB_TX_THRESHOLD /
		   8) | ((HFCUSB_RX_THRESHOLD / 8) << 4));

	fifo = hfc->fifos;
	for (i = 0; i < HFCUSB_NUM_FIFOS; i++) {
		write_usb(hfc, HFCUSB_FIFO, i);	/* select the desired fifo */
		fifo[i].skbuff = NULL;	/* init buffer pointer */
		fifo[i].max_size =
		    (i <= HFCUSB_B2_RX) ? MAX_BCH_SIZE : MAX_DFRAME_LEN;
		fifo[i].last_urblen = 0;
		/* set 2 bit for D- & E-channel */
		write_usb(hfc, HFCUSB_HDLC_PAR,
			  ((i <= HFCUSB_B2_RX) ? 0 : 2));
		/* rx hdlc, enable IFF for D-channel */
		write_usb(hfc, HFCUSB_CON_HDLC,
			  ((i == HFCUSB_D_TX) ? 0x09 : 0x08));
		write_usb(hfc, HFCUSB_INC_RES_F, 2);	/* reset the fifo */
	}

	write_usb(hfc, HFCUSB_CLKDEL, 0x0f);	/* clock delay value */
	write_usb(hfc, HFCUSB_STATES, 3 | 0x10);	/* set deactivated mode */
	write_usb(hfc, HFCUSB_STATES, 3);	/* enable state machine */

	write_usb(hfc, HFCUSB_SCTRL_R, 0);	/* disable both B receivers */
	write_usb(hfc, HFCUSB_SCTRL, 0x40);	/* disable B transmitters + capacitive mode */

	/* set both B-channel to not connected */
	hfc->b_mode[0] = L1_MODE_NULL;
	hfc->b_mode[1] = L1_MODE_NULL;

M
Martin Bachem 已提交
1164 1165
	hfc->l1_activated = 0;
	hfc->disc_flag = 0;
L
Linus Torvalds 已提交
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181
	hfc->led_state = 0;
	hfc->old_led_state = 0;

	/* init the t3 timer */
	init_timer(&hfc->t3_timer);
	hfc->t3_timer.data = (long) hfc;
	hfc->t3_timer.function = (void *) l1_timer_expire_t3;

	/* init the t4 timer */
	init_timer(&hfc->t4_timer);
	hfc->t4_timer.data = (long) hfc;
	hfc->t4_timer.function = (void *) l1_timer_expire_t4;

	/* init the background machinery for control requests */
	hfc->ctrl_read.bRequestType = 0xc0;
	hfc->ctrl_read.bRequest = 1;
1182
	hfc->ctrl_read.wLength = cpu_to_le16(1);
L
Linus Torvalds 已提交
1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
	hfc->ctrl_write.bRequestType = 0x40;
	hfc->ctrl_write.bRequest = 0;
	hfc->ctrl_write.wLength = 0;
	usb_fill_control_urb(hfc->ctrl_urb,
			     hfc->dev,
			     hfc->ctrl_out_pipe,
			     (u_char *) & hfc->ctrl_write,
			     NULL, 0, ctrl_complete, hfc);
	/* Init All Fifos */
	for (i = 0; i < HFCUSB_NUM_FIFOS; i++) {
		hfc->fifos[i].iso[0].purb = NULL;
		hfc->fifos[i].iso[1].purb = NULL;
		hfc->fifos[i].active = 0;
	}
	/* register Modul to upper Hisax Layers */
	hfc->d_if.owner = THIS_MODULE;
	hfc->d_if.ifc.priv = &hfc->fifos[HFCUSB_D_TX];
	hfc->d_if.ifc.l2l1 = hfc_usb_l2l1;
	for (i = 0; i < 2; i++) {
		hfc->b_if[i].ifc.priv = &hfc->fifos[HFCUSB_B1_TX + i * 2];
		hfc->b_if[i].ifc.l2l1 = hfc_usb_l2l1;
		p_b_if[i] = &hfc->b_if[i];
	}
	/* default Prot: EURO ISDN, should be a module_param */
	hfc->protocol = 2;
1208 1209 1210 1211 1212
	i = hisax_register(&hfc->d_if, p_b_if, "hfc_usb", hfc->protocol);
	if (i) {
		printk(KERN_INFO "HFC-S USB: hisax_register -> %d\n", i);
		return i;
	}
L
Linus Torvalds 已提交
1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254

#ifdef CONFIG_HISAX_DEBUG
	hfc_debug = debug;
#endif

	for (i = 0; i < 4; i++)
		hfc->fifos[i].hif = &p_b_if[i / 2]->ifc;
	for (i = 4; i < 8; i++)
		hfc->fifos[i].hif = &hfc->d_if.ifc;

	/* 3 (+1) INT IN + 3 ISO OUT */
	if (hfc->cfg_used == CNF_3INT3ISO || hfc->cfg_used == CNF_4INT3ISO) {
		start_int_fifo(hfc->fifos + HFCUSB_D_RX);
		if (hfc->fifos[HFCUSB_PCM_RX].pipe)
			start_int_fifo(hfc->fifos + HFCUSB_PCM_RX);
		start_int_fifo(hfc->fifos + HFCUSB_B1_RX);
		start_int_fifo(hfc->fifos + HFCUSB_B2_RX);
	}
	/* 3 (+1) ISO IN + 3 ISO OUT */
	if (hfc->cfg_used == CNF_3ISO3ISO || hfc->cfg_used == CNF_4ISO3ISO) {
		start_isoc_chain(hfc->fifos + HFCUSB_D_RX, ISOC_PACKETS_D,
				 rx_iso_complete, 16);
		if (hfc->fifos[HFCUSB_PCM_RX].pipe)
			start_isoc_chain(hfc->fifos + HFCUSB_PCM_RX,
					 ISOC_PACKETS_D, rx_iso_complete,
					 16);
		start_isoc_chain(hfc->fifos + HFCUSB_B1_RX, ISOC_PACKETS_B,
				 rx_iso_complete, 16);
		start_isoc_chain(hfc->fifos + HFCUSB_B2_RX, ISOC_PACKETS_B,
				 rx_iso_complete, 16);
	}

	start_isoc_chain(hfc->fifos + HFCUSB_D_TX, ISOC_PACKETS_D,
			 tx_iso_complete, 1);
	start_isoc_chain(hfc->fifos + HFCUSB_B1_TX, ISOC_PACKETS_B,
			 tx_iso_complete, 1);
	start_isoc_chain(hfc->fifos + HFCUSB_B2_TX, ISOC_PACKETS_B,
			 tx_iso_complete, 1);

	handle_led(hfc, LED_POWER_ON);

	return (0);
M
Martin Bachem 已提交
1255
}
L
Linus Torvalds 已提交
1256

M
Martin Bachem 已提交
1257
/* initial callback for each plugged USB device */
L
Linus Torvalds 已提交
1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
static int
hfc_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
{
	struct usb_device *dev = interface_to_usbdev(intf);
	hfcusb_data *context;
	struct usb_host_interface *iface = intf->cur_altsetting;
	struct usb_host_interface *iface_used = NULL;
	struct usb_host_endpoint *ep;
	int ifnum = iface->desc.bInterfaceNumber;
	int i, idx, alt_idx, probe_alt_setting, vend_idx, cfg_used, *vcf,
	    attr, cfg_found, cidx, ep_addr;
	int cmptbl[16], small_match, iso_packet_size, packet_size,
	    alt_used = 0;
M
Martin Bachem 已提交
1271
	hfcsusb_vdata *driver_info;
L
Linus Torvalds 已提交
1272 1273

	vend_idx = 0xffff;
M
Martin Bachem 已提交
1274
	for (i = 0; hfcusb_idtab[i].idVendor; i++) {
1275 1276
		if ((le16_to_cpu(dev->descriptor.idVendor) == hfcusb_idtab[i].idVendor)
		    && (le16_to_cpu(dev->descriptor.idProduct) == hfcusb_idtab[i].idProduct)) {
L
Linus Torvalds 已提交
1277
			vend_idx = i;
M
Martin Bachem 已提交
1278 1279
			continue;
		}
L
Linus Torvalds 已提交
1280
	}
M
Martin Bachem 已提交
1281

L
Linus Torvalds 已提交
1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301
	printk(KERN_INFO
	       "HFC-S USB: probing interface(%d) actalt(%d) minor(%d)\n",
	       ifnum, iface->desc.bAlternateSetting, intf->minor);

	if (vend_idx != 0xffff) {
		/* if vendor and product ID is OK, start probing alternate settings */
		alt_idx = 0;
		small_match = 0xffff;

		/* default settings */
		iso_packet_size = 16;
		packet_size = 64;

		while (alt_idx < intf->num_altsetting) {
			iface = intf->altsetting + alt_idx;
			probe_alt_setting = iface->desc.bAlternateSetting;
			cfg_used = 0;

			/* check for config EOL element */
			while (validconf[cfg_used][0]) {
M
Martin Bachem 已提交
1302
				cfg_found = 1;
L
Linus Torvalds 已提交
1303 1304 1305
				vcf = validconf[cfg_used];
				/* first endpoint descriptor */
				ep = iface->endpoint;
M
Martin Bachem 已提交
1306

L
Linus Torvalds 已提交
1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
				memcpy(cmptbl, vcf, 16 * sizeof(int));

				/* check for all endpoints in this alternate setting */
				for (i = 0; i < iface->desc.bNumEndpoints;
				     i++) {
					ep_addr =
					    ep->desc.bEndpointAddress;
					/* get endpoint base */
					idx = ((ep_addr & 0x7f) - 1) * 2;
					if (ep_addr & 0x80)
						idx++;
					attr = ep->desc.bmAttributes;
					if (cmptbl[idx] == EP_NUL) {
M
Martin Bachem 已提交
1320
						cfg_found = 0;
L
Linus Torvalds 已提交
1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332
					}
					if (attr == USB_ENDPOINT_XFER_INT
					    && cmptbl[idx] == EP_INT)
						cmptbl[idx] = EP_NUL;
					if (attr == USB_ENDPOINT_XFER_BULK
					    && cmptbl[idx] == EP_BLK)
						cmptbl[idx] = EP_NUL;
					if (attr == USB_ENDPOINT_XFER_ISOC
					    && cmptbl[idx] == EP_ISO)
						cmptbl[idx] = EP_NUL;

					/* check if all INT endpoints match minimum interval */
M
Martin Bachem 已提交
1333 1334 1335
					if ((attr == USB_ENDPOINT_XFER_INT)
					    && (ep->desc.bInterval < vcf[17])) {
						cfg_found = 0;
L
Linus Torvalds 已提交
1336 1337 1338 1339 1340 1341 1342
					}
					ep++;
				}
				for (i = 0; i < 16; i++) {
					/* all entries must be EP_NOP or EP_NUL for a valid config */
					if (cmptbl[i] != EP_NOP
					    && cmptbl[i] != EP_NUL)
M
Martin Bachem 已提交
1343
						cfg_found = 0;
L
Linus Torvalds 已提交
1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355
				}
				if (cfg_found) {
					if (cfg_used < small_match) {
						small_match = cfg_used;
						alt_used =
						    probe_alt_setting;
						iface_used = iface;
					}
				}
				cfg_used++;
			}
			alt_idx++;
M
Martin Bachem 已提交
1356
		} /* (alt_idx < intf->num_altsetting) */
L
Linus Torvalds 已提交
1357 1358 1359 1360

		/* found a valid USB Ta Endpint config */
		if (small_match != 0xffff) {
			iface = iface_used;
M
Martin Bachem 已提交
1361
			if (!(context = kzalloc(sizeof(hfcusb_data), GFP_KERNEL)))
L
Linus Torvalds 已提交
1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392
				return (-ENOMEM);	/* got no mem */

			ep = iface->endpoint;
			vcf = validconf[small_match];

			for (i = 0; i < iface->desc.bNumEndpoints; i++) {
				ep_addr = ep->desc.bEndpointAddress;
				/* get endpoint base */
				idx = ((ep_addr & 0x7f) - 1) * 2;
				if (ep_addr & 0x80)
					idx++;
				cidx = idx & 7;
				attr = ep->desc.bmAttributes;

				/* init Endpoints */
				if (vcf[idx] != EP_NOP
				    && vcf[idx] != EP_NUL) {
					switch (attr) {
						case USB_ENDPOINT_XFER_INT:
							context->
							    fifos[cidx].
							    pipe =
							    usb_rcvintpipe
							    (dev,
							     ep->desc.
							     bEndpointAddress);
							context->
							    fifos[cidx].
							    usb_transfer_mode
							    = USB_INT;
							packet_size =
1393
							    le16_to_cpu(ep->desc.wMaxPacketSize);
L
Linus Torvalds 已提交
1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420
							break;
						case USB_ENDPOINT_XFER_BULK:
							if (ep_addr & 0x80)
								context->
								    fifos
								    [cidx].
								    pipe =
								    usb_rcvbulkpipe
								    (dev,
								     ep->
								     desc.
								     bEndpointAddress);
							else
								context->
								    fifos
								    [cidx].
								    pipe =
								    usb_sndbulkpipe
								    (dev,
								     ep->
								     desc.
								     bEndpointAddress);
							context->
							    fifos[cidx].
							    usb_transfer_mode
							    = USB_BULK;
							packet_size =
1421
							    le16_to_cpu(ep->desc.wMaxPacketSize);
L
Linus Torvalds 已提交
1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448
							break;
						case USB_ENDPOINT_XFER_ISOC:
							if (ep_addr & 0x80)
								context->
								    fifos
								    [cidx].
								    pipe =
								    usb_rcvisocpipe
								    (dev,
								     ep->
								     desc.
								     bEndpointAddress);
							else
								context->
								    fifos
								    [cidx].
								    pipe =
								    usb_sndisocpipe
								    (dev,
								     ep->
								     desc.
								     bEndpointAddress);
							context->
							    fifos[cidx].
							    usb_transfer_mode
							    = USB_ISOC;
							iso_packet_size =
1449
							    le16_to_cpu(ep->desc.wMaxPacketSize);
L
Linus Torvalds 已提交
1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461
							break;
						default:
							context->
							    fifos[cidx].
							    pipe = 0;
					}	/* switch attribute */

					if (context->fifos[cidx].pipe) {
						context->fifos[cidx].
						    fifonum = cidx;
						context->fifos[cidx].hfc =
						    context;
1462 1463
						context->fifos[cidx].usb_packet_maxlen =
						    le16_to_cpu(ep->desc.wMaxPacketSize);
L
Linus Torvalds 已提交
1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488
						context->fifos[cidx].
						    intervall =
						    ep->desc.bInterval;
						context->fifos[cidx].
						    skbuff = NULL;
					}
				}
				ep++;
			}
			context->dev = dev;	/* save device */
			context->if_used = ifnum;	/* save used interface */
			context->alt_used = alt_used;	/* and alternate config */
			context->ctrl_paksize = dev->descriptor.bMaxPacketSize0;	/* control size */
			context->cfg_used = vcf[16];	/* store used config */
			context->vend_idx = vend_idx;	/* store found vendor */
			context->packet_size = packet_size;
			context->iso_packet_size = iso_packet_size;

			/* create the control pipes needed for register access */
			context->ctrl_in_pipe =
			    usb_rcvctrlpipe(context->dev, 0);
			context->ctrl_out_pipe =
			    usb_sndctrlpipe(context->dev, 0);
			context->ctrl_urb = usb_alloc_urb(0, GFP_KERNEL);

M
Martin Bachem 已提交
1489 1490 1491 1492 1493
			driver_info =
			    (hfcsusb_vdata *) hfcusb_idtab[vend_idx].
			    driver_info;
			printk(KERN_INFO "HFC-S USB: detected \"%s\"\n",
			       driver_info->vend_name);
M
Martin Bachem 已提交
1494 1495 1496

			DBG(HFCUSB_DBG_INIT,
			    "HFC-S USB: Endpoint-Config: %s (if=%d alt=%d), E-Channel(%d)",
L
Linus Torvalds 已提交
1497
			    conf_str[small_match], context->if_used,
M
Martin Bachem 已提交
1498 1499 1500
			    context->alt_used,
			    validconf[small_match][18]);

L
Linus Torvalds 已提交
1501
			/* init the chip and register the driver */
M
Martin Bachem 已提交
1502
			if (hfc_usb_init(context)) {
1503 1504 1505
				usb_kill_urb(context->ctrl_urb);
				usb_free_urb(context->ctrl_urb);
				context->ctrl_urb = NULL;
L
Linus Torvalds 已提交
1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518
				kfree(context);
				return (-EIO);
			}
			usb_set_intfdata(intf, context);
			return (0);
		}
	} else {
		printk(KERN_INFO
		       "HFC-S USB: no valid vendor found in USB descriptor\n");
	}
	return (-EIO);
}

M
Martin Bachem 已提交
1519
/* callback for unplugged USB device */
L
Linus Torvalds 已提交
1520
static void
1521
hfc_usb_disconnect(struct usb_interface *intf)
L
Linus Torvalds 已提交
1522 1523 1524
{
	hfcusb_data *context = usb_get_intfdata(intf);
	int i;
M
Martin Bachem 已提交
1525 1526

	handle_led(context, LED_POWER_OFF);
1527
	schedule_timeout(HZ / 100);
M
Martin Bachem 已提交
1528

L
Linus Torvalds 已提交
1529
	printk(KERN_INFO "HFC-S USB: device disconnect\n");
M
Martin Bachem 已提交
1530
	context->disc_flag = 1;
L
Linus Torvalds 已提交
1531
	usb_set_intfdata(intf, NULL);
1532

L
Linus Torvalds 已提交
1533 1534 1535 1536
	if (timer_pending(&context->t3_timer))
		del_timer(&context->t3_timer);
	if (timer_pending(&context->t4_timer))
		del_timer(&context->t4_timer);
M
Martin Bachem 已提交
1537

L
Linus Torvalds 已提交
1538 1539 1540 1541 1542
	/* tell all fifos to terminate */
	for (i = 0; i < HFCUSB_NUM_FIFOS; i++) {
		if (context->fifos[i].usb_transfer_mode == USB_ISOC) {
			if (context->fifos[i].active > 0) {
				stop_isoc_chain(&context->fifos[i]);
M
Martin Bachem 已提交
1543 1544
				DBG(HFCUSB_DBG_INIT,
				    "HFC-S USB: %s stopping ISOC chain Fifo(%i)",
1545
				    __func__, i);
L
Linus Torvalds 已提交
1546 1547 1548 1549
			}
		} else {
			if (context->fifos[i].active > 0) {
				context->fifos[i].active = 0;
M
Martin Bachem 已提交
1550 1551
				DBG(HFCUSB_DBG_INIT,
				    "HFC-S USB: %s unlinking URB for Fifo(%i)",
1552
				    __func__, i);
L
Linus Torvalds 已提交
1553
			}
1554 1555 1556
			usb_kill_urb(context->fifos[i].urb);
			usb_free_urb(context->fifos[i].urb);
			context->fifos[i].urb = NULL;
L
Linus Torvalds 已提交
1557 1558 1559
		}
		context->fifos[i].active = 0;
	}
1560 1561 1562
	usb_kill_urb(context->ctrl_urb);
	usb_free_urb(context->ctrl_urb);
	context->ctrl_urb = NULL;
L
Linus Torvalds 已提交
1563 1564
	hisax_unregister(&context->d_if);
	kfree(context);		/* free our structure again */
M
Martin Bachem 已提交
1565
}
L
Linus Torvalds 已提交
1566 1567

static struct usb_driver hfc_drv = {
M
Martin Bachem 已提交
1568 1569 1570 1571
	.name  = "hfc_usb",
	.id_table = hfcusb_idtab,
	.probe = hfc_usb_probe,
	.disconnect = hfc_usb_disconnect,
L
Linus Torvalds 已提交
1572
};
M
Martin Bachem 已提交
1573

L
Linus Torvalds 已提交
1574
static void __exit
M
Martin Bachem 已提交
1575
hfc_usb_mod_exit(void)
L
Linus Torvalds 已提交
1576
{
M
Martin Bachem 已提交
1577
	usb_deregister(&hfc_drv); /* release our driver */
L
Linus Torvalds 已提交
1578 1579 1580 1581
	printk(KERN_INFO "HFC-S USB: module removed\n");
}

static int __init
M
Martin Bachem 已提交
1582
hfc_usb_mod_init(void)
L
Linus Torvalds 已提交
1583
{
M
Martin Bachem 已提交
1584
	char revstr[30], datestr[30], dummy[30];
L
Linus Torvalds 已提交
1585
#ifndef CONFIG_HISAX_DEBUG
M
Martin Bachem 已提交
1586
	hfc_debug = debug;
L
Linus Torvalds 已提交
1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601
#endif
	sscanf(hfcusb_revision,
	       "%s %s $ %s %s %s $ ", dummy, revstr,
	       dummy, datestr, dummy);
	printk(KERN_INFO
	       "HFC-S USB: driver module revision %s date %s loaded, (debug=%i)\n",
	       revstr, datestr, debug);
	if (usb_register(&hfc_drv)) {
		printk(KERN_INFO
		       "HFC-S USB: Unable to register HFC-S USB module at usb stack\n");
		return (-1);	/* unable to register */
	}
	return (0);
}

M
Martin Bachem 已提交
1602 1603
module_init(hfc_usb_mod_init);
module_exit(hfc_usb_mod_exit);
L
Linus Torvalds 已提交
1604 1605 1606
MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_LICENSE("GPL");
M
Martin Bachem 已提交
1607
MODULE_DEVICE_TABLE(usb, hfcusb_idtab);