ueagle-atm.c 67.9 KB
Newer Older
1 2 3 4
/*-
 * Copyright (c) 2003, 2004
 *	Damien Bergamini <damien.bergamini@free.fr>. All rights reserved.
 *
S
Stanislaw Gruszka 已提交
5 6
 * Copyright (c) 2005-2007 Matthieu Castet <castet.matthieu@free.fr>
 * Copyright (c) 2005-2007 Stanislaw Gruszka <stf_xl@wp.pl>
7 8 9 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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
 *
 * This software is available to you under a choice of one of two
 * licenses. You may choose to be licensed under the terms of the GNU
 * General Public License (GPL) Version 2, available from the file
 * COPYING in the main directory of this source tree, or the
 * BSD license below:
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice unmodified, this list of conditions, and the following
 *    disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * GPL license :
 * 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.
 *
 *
 * HISTORY : some part of the code was base on ueagle 1.3 BSD driver,
 * Damien Bergamini agree to put his code under a DUAL GPL/BSD license.
 *
 * The rest of the code was was rewritten from scratch.
 */

#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>
#include <linux/crc32.h>
#include <linux/usb.h>
#include <linux/firmware.h>
#include <linux/ctype.h>
R
Randy Dunlap 已提交
65
#include <linux/sched.h>
66
#include <linux/kthread.h>
67
#include <linux/mutex.h>
68
#include <linux/freezer.h>
69
#include <linux/slab.h>
70
#include <linux/kernel.h>
71

72 73 74 75
#include <asm/unaligned.h>

#include "usbatm.h"

M
matthieu castet 已提交
76
#define EAGLEUSBVERSION "ueagle 1.4"
77 78 79 80 81 82 83 84 85 86


/*
 * Debug macros
 */
#define uea_dbg(usb_dev, format, args...)	\
	do { \
		if (debug >= 1) \
			dev_dbg(&(usb_dev)->dev, \
				"[ueagle-atm dbg] %s: " format, \
87
					__func__, ##args); \
M
matthieu castet 已提交
88
	} while (0)
89 90 91 92 93 94

#define uea_vdbg(usb_dev, format, args...)	\
	do { \
		if (debug >= 2) \
			dev_dbg(&(usb_dev)->dev, \
				"[ueagle-atm vdbg]  " format, ##args); \
M
matthieu castet 已提交
95
	} while (0)
96 97

#define uea_enters(usb_dev) \
98
	uea_vdbg(usb_dev, "entering %s\n" , __func__)
99 100

#define uea_leaves(usb_dev) \
101
	uea_vdbg(usb_dev, "leaving  %s\n" , __func__)
102

103 104
#define uea_err(usb_dev, format, args...) \
	dev_err(&(usb_dev)->dev , "[UEAGLE-ATM] " format , ##args)
105

106 107
#define uea_warn(usb_dev, format, args...) \
	dev_warn(&(usb_dev)->dev , "[Ueagle-atm] " format, ##args)
108

109 110
#define uea_info(usb_dev, format, args...) \
	dev_info(&(usb_dev)->dev , "[ueagle-atm] " format, ##args)
111

112 113 114 115
struct intr_pkt;

/* cmv's from firmware */
struct uea_cmvs_v1 {
116 117 118 119 120
	u32 address;
	u16 offset;
	u32 data;
} __attribute__ ((packed));

121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
struct uea_cmvs_v2 {
	u32 group;
	u32 address;
	u32 offset;
	u32 data;
} __attribute__ ((packed));

/* information about currently processed cmv */
struct cmv_dsc_e1 {
	u8 function;
	u16 idx;
	u32 address;
	u16 offset;
};

struct cmv_dsc_e4 {
	u16 function;
	u16 offset;
	u16 address;
	u16 group;
};

union cmv_dsc {
	struct cmv_dsc_e1 e1;
	struct cmv_dsc_e4 e4;
};

148 149 150 151 152 153
struct uea_softc {
	struct usb_device *usb_dev;
	struct usbatm_data *usbatm;

	int modem_index;
	unsigned int driver_info;
154 155 156
	int annex;
#define ANNEXA 0
#define ANNEXB 1
157 158 159 160 161 162 163 164

	int booting;
	int reset;

	wait_queue_head_t sync_q;

	struct task_struct *kthread;
	u32 data;
165 166
	u32 data1;

167
	int cmv_ack;
168
	union cmv_dsc cmv_dsc;
169 170

	struct work_struct task;
171
	struct workqueue_struct *work_q;
172 173 174 175 176 177
	u16 pageno;
	u16 ovl;

	const struct firmware *dsp_firm;
	struct urb *urb_int;

178 179 180 181
	void (*dispatch_cmv) (struct uea_softc *, struct intr_pkt *);
	void (*schedule_load_page) (struct uea_softc *, struct intr_pkt *);
	int (*stat) (struct uea_softc *);
	int (*send_cmvs) (struct uea_softc *);
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214

	/* keep in sync with eaglectl */
	struct uea_stats {
		struct {
			u32 state;
			u32 flags;
			u32 mflags;
			u32 vidcpe;
			u32 vidco;
			u32 dsrate;
			u32 usrate;
			u32 dsunc;
			u32 usunc;
			u32 dscorr;
			u32 uscorr;
			u32 txflow;
			u32 rxflow;
			u32 usattenuation;
			u32 dsattenuation;
			u32 dsmargin;
			u32 usmargin;
			u32 firmid;
		} phy;
	} stats;
};

/*
 * Elsa IDs
 */
#define ELSA_VID		0x05CC
#define ELSA_PID_PSTFIRM	0x3350
#define ELSA_PID_PREFIRM	0x3351

215 216 217 218 219
#define ELSA_PID_A_PREFIRM	0x3352
#define ELSA_PID_A_PSTFIRM	0x3353
#define ELSA_PID_B_PREFIRM	0x3362
#define ELSA_PID_B_PSTFIRM	0x3363

220
/*
221
 * Devolo IDs : pots if (pid & 0x10)
222
 */
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
#define DEVOLO_VID			0x1039
#define DEVOLO_EAGLE_I_A_PID_PSTFIRM	0x2110
#define DEVOLO_EAGLE_I_A_PID_PREFIRM	0x2111

#define DEVOLO_EAGLE_I_B_PID_PSTFIRM	0x2100
#define DEVOLO_EAGLE_I_B_PID_PREFIRM	0x2101

#define DEVOLO_EAGLE_II_A_PID_PSTFIRM	0x2130
#define DEVOLO_EAGLE_II_A_PID_PREFIRM	0x2131

#define DEVOLO_EAGLE_II_B_PID_PSTFIRM	0x2120
#define DEVOLO_EAGLE_II_B_PID_PREFIRM	0x2121

/*
 * Reference design USB IDs
 */
#define ANALOG_VID		0x1110
#define ADI930_PID_PREFIRM	0x9001
#define ADI930_PID_PSTFIRM	0x9000

243 244 245 246 247 248 249 250 251 252 253 254
#define EAGLE_I_PID_PREFIRM	0x9010	/* Eagle I */
#define EAGLE_I_PID_PSTFIRM	0x900F	/* Eagle I */

#define EAGLE_IIC_PID_PREFIRM	0x9024	/* Eagle IIC */
#define EAGLE_IIC_PID_PSTFIRM	0x9023	/* Eagle IIC */

#define EAGLE_II_PID_PREFIRM	0x9022	/* Eagle II */
#define EAGLE_II_PID_PSTFIRM	0x9021	/* Eagle II */

#define EAGLE_III_PID_PREFIRM	0x9032	/* Eagle III */
#define EAGLE_III_PID_PSTFIRM	0x9031	/* Eagle III */

255 256 257
#define EAGLE_IV_PID_PREFIRM	0x9042  /* Eagle IV */
#define EAGLE_IV_PID_PSTFIRM	0x9041  /* Eagle IV */

258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
/*
 * USR USB IDs
 */
#define USR_VID			0x0BAF
#define MILLER_A_PID_PREFIRM	0x00F2
#define MILLER_A_PID_PSTFIRM	0x00F1
#define MILLER_B_PID_PREFIRM	0x00FA
#define MILLER_B_PID_PSTFIRM	0x00F9
#define HEINEKEN_A_PID_PREFIRM	0x00F6
#define HEINEKEN_A_PID_PSTFIRM	0x00F5
#define HEINEKEN_B_PID_PREFIRM	0x00F8
#define HEINEKEN_B_PID_PSTFIRM	0x00F7

#define PREFIRM 0
#define PSTFIRM (1<<7)
273 274 275
#define AUTO_ANNEX_A (1<<8)
#define AUTO_ANNEX_B (1<<9)

276 277 278 279
enum {
	ADI930 = 0,
	EAGLE_I,
	EAGLE_II,
280 281
	EAGLE_III,
	EAGLE_IV
282 283 284 285 286 287 288 289
};

/* macros for both struct usb_device_id and struct uea_softc */
#define UEA_IS_PREFIRM(x) \
	(!((x)->driver_info & PSTFIRM))
#define UEA_CHIP_VERSION(x) \
	((x)->driver_info & 0xf)

290 291
#define IS_ISDN(x) \
	((x)->annex & ANNEXB)
292

293
#define INS_TO_USBDEV(ins) (ins->usb_dev)
294 295 296

#define GET_STATUS(data) \
	((data >> 8) & 0xf)
297

298
#define IS_OPERATIONAL(sc) \
299 300 301
	((UEA_CHIP_VERSION(sc) != EAGLE_IV) ? \
	(GET_STATUS(sc->stats.phy.state) == 2) : \
	(sc->stats.phy.state == 7))
302 303 304 305 306 307

/*
 * Set of macros to handle unaligned data in the firmware blob.
 * The FW_GET_BYTE() macro is provided only for consistency.
 */

308
#define FW_GET_BYTE(p) (*((__u8 *) (p)))
309 310

#define FW_DIR "ueagle-atm/"
311
#define UEA_FW_NAME_MAX 30
312 313 314 315 316
#define NB_MODEM 4

#define BULK_TIMEOUT 300
#define CTRL_TIMEOUT 1000

317
#define ACK_TIMEOUT msecs_to_jiffies(3000)
318

319
#define UEA_INTR_IFACE_NO	0
320 321 322 323 324 325 326 327 328 329
#define UEA_US_IFACE_NO		1
#define UEA_DS_IFACE_NO		2

#define FASTEST_ISO_INTF	8

#define UEA_BULK_DATA_PIPE	0x02
#define UEA_IDMA_PIPE		0x04
#define UEA_INTR_PIPE		0x04
#define UEA_ISO_DATA_PIPE	0x08

330
#define UEA_E1_SET_BLOCK	0x0001
331
#define UEA_E4_SET_BLOCK	0x002c
332
#define UEA_SET_MODE		0x0003
333 334 335 336 337 338 339 340 341 342 343 344 345 346
#define UEA_SET_2183_DATA	0x0004
#define UEA_SET_TIMEOUT		0x0011

#define UEA_LOOPBACK_OFF	0x0002
#define UEA_LOOPBACK_ON		0x0003
#define UEA_BOOT_IDMA		0x0006
#define UEA_START_RESET		0x0007
#define UEA_END_RESET		0x0008

#define UEA_SWAP_MAILBOX	(0x3fcd | 0x4000)
#define UEA_MPTX_START		(0x3fce | 0x4000)
#define UEA_MPTX_MAILBOX	(0x3fd6 | 0x4000)
#define UEA_MPRX_MAILBOX	(0x3fdf | 0x4000)

347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
/* block information in eagle4 dsp firmware  */
struct block_index {
	__le32 PageOffset;
	__le32 NotLastBlock;
	__le32 dummy;
	__le32 PageSize;
	__le32 PageAddress;
	__le16 dummy1;
	__le16 PageNumber;
} __attribute__ ((packed));

#define E4_IS_BOOT_PAGE(PageSize) ((le32_to_cpu(PageSize)) & 0x80000000)
#define E4_PAGE_BYTES(PageSize) ((le32_to_cpu(PageSize) & 0x7fffffff) * 4)

#define E4_L1_STRING_HEADER 0x10
#define E4_MAX_PAGE_NUMBER 0x58
#define E4_NO_SWAPPAGE_HEADERS 0x31

/* l1_code is eagle4 dsp firmware format */
struct l1_code {
	u8 string_header[E4_L1_STRING_HEADER];
	u8 page_number_to_block_index[E4_MAX_PAGE_NUMBER];
	struct block_index page_header[E4_NO_SWAPPAGE_HEADERS];
370
	u8 code[0];
371 372 373 374
} __attribute__ ((packed));

/* structures describing a block within a DSP page */
struct block_info_e1 {
375 376 377 378 379 380 381
	__le16 wHdr;
	__le16 wAddress;
	__le16 wSize;
	__le16 wOvlOffset;
	__le16 wOvl;		/* overlay */
	__le16 wLast;
} __attribute__ ((packed));
382 383 384 385 386 387 388 389 390 391 392
#define E1_BLOCK_INFO_SIZE 12

struct block_info_e4 {
	__be16 wHdr;
	__u8 bBootPage;
	__u8 bPageNumber;
	__be32 dwSize;
	__be32 dwAddress;
	__be16 wReserved;
} __attribute__ ((packed));
#define E4_BLOCK_INFO_SIZE 14
393

394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
#define UEA_BIHDR 0xabcd
#define UEA_RESERVED 0xffff

/* constants describing cmv type */
#define E1_PREAMBLE 0x535c
#define E1_MODEMTOHOST 0x01
#define E1_HOSTTOMODEM 0x10

#define E1_MEMACCESS 0x1
#define E1_ADSLDIRECTIVE 0x7
#define E1_FUNCTION_TYPE(f) ((f) >> 4)
#define E1_FUNCTION_SUBTYPE(f) ((f) & 0x0f)

#define E4_MEMACCESS 0
#define E4_ADSLDIRECTIVE 0xf
#define E4_FUNCTION_TYPE(f) ((f) >> 8)
#define E4_FUNCTION_SIZE(f) ((f) & 0x0f)
#define E4_FUNCTION_SUBTYPE(f) (((f) >> 4) & 0x0f)
412 413

/* for MEMACCESS */
414 415 416 417 418 419 420 421 422 423
#define E1_REQUESTREAD	0x0
#define E1_REQUESTWRITE	0x1
#define E1_REPLYREAD	0x2
#define E1_REPLYWRITE	0x3

#define E4_REQUESTREAD	0x0
#define E4_REQUESTWRITE	0x4
#define E4_REPLYREAD	(E4_REQUESTREAD | 1)
#define E4_REPLYWRITE	(E4_REQUESTWRITE | 1)

424
/* for ADSLDIRECTIVE */
425 426
#define E1_KERNELREADY 0x0
#define E1_MODEMREADY  0x1
427

428 429 430 431
#define E4_KERNELREADY 0x0
#define E4_MODEMREADY  0x1

#define E1_MAKEFUNCTION(t, s) (((t) & 0xf) << 4 | ((s) & 0xf))
432 433
#define E4_MAKEFUNCTION(t, st, s) (((t) & 0xf) << 8 | \
	((st) & 0xf) << 4 | ((s) & 0xf))
434 435

#define E1_MAKESA(a, b, c, d)						\
436 437 438 439
	(((c) & 0xff) << 24 |						\
	 ((d) & 0xff) << 16 |						\
	 ((a) & 0xff) << 8  |						\
	 ((b) & 0xff))
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468

#define E1_GETSA1(a) ((a >> 8) & 0xff)
#define E1_GETSA2(a) (a & 0xff)
#define E1_GETSA3(a) ((a >> 24) & 0xff)
#define E1_GETSA4(a) ((a >> 16) & 0xff)

#define E1_SA_CNTL E1_MAKESA('C', 'N', 'T', 'L')
#define E1_SA_DIAG E1_MAKESA('D', 'I', 'A', 'G')
#define E1_SA_INFO E1_MAKESA('I', 'N', 'F', 'O')
#define E1_SA_OPTN E1_MAKESA('O', 'P', 'T', 'N')
#define E1_SA_RATE E1_MAKESA('R', 'A', 'T', 'E')
#define E1_SA_STAT E1_MAKESA('S', 'T', 'A', 'T')

#define E4_SA_CNTL 1
#define E4_SA_STAT 2
#define E4_SA_INFO 3
#define E4_SA_TEST 4
#define E4_SA_OPTN 5
#define E4_SA_RATE 6
#define E4_SA_DIAG 7
#define E4_SA_CNFG 8

/* structures representing a CMV (Configuration and Management Variable) */
struct cmv_e1 {
	__le16 wPreamble;
	__u8 bDirection;
	__u8 bFunction;
	__le16 wIndex;
	__le32 dwSymbolicAddress;
469 470 471 472
	__le16 wOffsetAddress;
	__le32 dwData;
} __attribute__ ((packed));

473 474 475 476 477
struct cmv_e4 {
	__be16 wGroup;
	__be16 wFunction;
	__be16 wOffset;
	__be16 wAddress;
478
	__be32 dwData[6];
479 480 481 482
} __attribute__ ((packed));

/* structures representing swap information */
struct swap_info_e1 {
483 484 485 486
	__u8 bSwapPageNo;
	__u8 bOvl;		/* overlay */
} __attribute__ ((packed));

487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
struct swap_info_e4 {
	__u8 bSwapPageNo;
} __attribute__ ((packed));

/* structures representing interrupt data */
#define e1_bSwapPageNo	u.e1.s1.swapinfo.bSwapPageNo
#define e1_bOvl		u.e1.s1.swapinfo.bOvl
#define e4_bSwapPageNo  u.e4.s1.swapinfo.bSwapPageNo

#define INT_LOADSWAPPAGE 0x0001
#define INT_INCOMINGCMV  0x0002

union intr_data_e1 {
	struct {
		struct swap_info_e1 swapinfo;
		__le16 wDataSize;
	} __attribute__ ((packed)) s1;
	struct {
		struct cmv_e1 cmv;
		__le16 wDataSize;
	} __attribute__ ((packed)) s2;
} __attribute__ ((packed));

union intr_data_e4 {
	struct {
		struct swap_info_e4 swapinfo;
		__le16 wDataSize;
	} __attribute__ ((packed)) s1;
	struct {
		struct cmv_e4 cmv;
		__le16 wDataSize;
	} __attribute__ ((packed)) s2;
} __attribute__ ((packed));

521 522 523 524 525 526 527 528
struct intr_pkt {
	__u8 bType;
	__u8 bNotification;
	__le16 wValue;
	__le16 wIndex;
	__le16 wLength;
	__le16 wInterrupt;
	union {
529 530 531
		union intr_data_e1 e1;
		union intr_data_e4 e4;
	} u;
532
} __attribute__ ((packed));
533 534 535

#define E1_INTR_PKT_SIZE 28
#define E4_INTR_PKT_SIZE 64
536 537

static struct usb_driver uea_driver;
538
static DEFINE_MUTEX(uea_mutex);
539 540
static const char *chip_name[] = {"ADI930", "Eagle I", "Eagle II", "Eagle III",
								"Eagle IV"};
541 542 543

static int modem_index;
static unsigned int debug;
544 545
static unsigned int altsetting[NB_MODEM] = {
				[0 ... (NB_MODEM - 1)] = FASTEST_ISO_INTF};
546 547
static int sync_wait[NB_MODEM];
static char *cmv_file[NB_MODEM];
548
static int annex[NB_MODEM];
549 550 551

module_param(debug, uint, 0644);
MODULE_PARM_DESC(debug, "module debug level (0=off,1=on,2=verbose)");
552 553 554
module_param_array(altsetting, uint, NULL, 0644);
MODULE_PARM_DESC(altsetting, "alternate setting for incoming traffic: 0=bulk, "
			     "1=isoc slowest, ... , 8=isoc fastest (default)");
555 556 557 558 559
module_param_array(sync_wait, bool, NULL, 0644);
MODULE_PARM_DESC(sync_wait, "wait the synchronisation before starting ATM");
module_param_array(cmv_file, charp, NULL, 0644);
MODULE_PARM_DESC(cmv_file,
		"file name with configuration and management variables");
560 561
module_param_array(annex, uint, NULL, 0644);
MODULE_PARM_DESC(annex,
562
		"manually set annex a/b (0=auto, 1=annex a, 2=annex b)");
563

564 565 566 567 568 569 570 571 572
#define uea_wait(sc, cond, timeo) \
({ \
	int _r = wait_event_interruptible_timeout(sc->sync_q, \
			(cond) || kthread_should_stop(), timeo); \
	if (kthread_should_stop()) \
		_r = -ENODEV; \
	_r; \
})

573 574 575 576 577 578
#define UPDATE_ATM_STAT(type, val) \
	do { \
		if (sc->usbatm->atm_dev) \
			sc->usbatm->atm_dev->type = val; \
	} while (0)

579 580 581 582 583 584 585
#define UPDATE_ATM_SIGNAL(val) \
	do { \
		if (sc->usbatm->atm_dev) \
			atm_dev_signal_change(sc->usbatm->atm_dev, val); \
	} while (0)


586 587 588 589 590 591 592 593
/* Firmware loading */
#define LOAD_INTERNAL     0xA0
#define F8051_USBCS       0x7f92

/**
 * uea_send_modem_cmd - Send a command for pre-firmware devices.
 */
static int uea_send_modem_cmd(struct usb_device *usb,
594
			      u16 addr, u16 size, const u8 *buff)
595 596 597 598
{
	int ret = -ENOMEM;
	u8 *xfer_buff;

599
	xfer_buff = kmemdup(buff, size, GFP_KERNEL);
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
	if (xfer_buff) {
		ret = usb_control_msg(usb,
				      usb_sndctrlpipe(usb, 0),
				      LOAD_INTERNAL,
				      USB_DIR_OUT | USB_TYPE_VENDOR |
				      USB_RECIP_DEVICE, addr, 0, xfer_buff,
				      size, CTRL_TIMEOUT);
		kfree(xfer_buff);
	}

	if (ret < 0)
		return ret;

	return (ret == size) ? 0 : -EIO;
}

616 617
static void uea_upload_pre_firmware(const struct firmware *fw_entry,
								void *context)
618 619
{
	struct usb_device *usb = context;
620 621
	const u8 *pfw;
	u8 value;
622 623 624 625 626 627 628 629 630 631 632
	u32 crc = 0;
	int ret, size;

	uea_enters(usb);
	if (!fw_entry) {
		uea_err(usb, "firmware is not available\n");
		goto err;
	}

	pfw = fw_entry->data;
	size = fw_entry->size;
633 634
	if (size < 4)
		goto err_fw_corrupted;
635

636
	crc = get_unaligned_le32(pfw);
637 638
	pfw += 4;
	size -= 4;
639 640
	if (crc32_be(0, pfw, size) != crc)
		goto err_fw_corrupted;
641 642

	/*
643
	 * Start to upload firmware : send reset
644 645 646 647 648 649 650 651 652
	 */
	value = 1;
	ret = uea_send_modem_cmd(usb, F8051_USBCS, sizeof(value), &value);

	if (ret < 0) {
		uea_err(usb, "modem reset failed with error %d\n", ret);
		goto err;
	}

653
	while (size > 3) {
654
		u8 len = FW_GET_BYTE(pfw);
655
		u16 add = get_unaligned_le16(pfw + 1);
656 657 658 659 660

		size -= len + 3;
		if (size < 0)
			goto err_fw_corrupted;

661 662 663 664 665 666 667 668 669
		ret = uea_send_modem_cmd(usb, add, len, pfw + 3);
		if (ret < 0) {
			uea_err(usb, "uploading firmware data failed "
					"with error %d\n", ret);
			goto err;
		}
		pfw += len + 3;
	}

670 671 672
	if (size != 0)
		goto err_fw_corrupted;

673 674 675 676 677 678 679 680 681 682
	/*
	 * Tell the modem we finish : de-assert reset
	 */
	value = 0;
	ret = uea_send_modem_cmd(usb, F8051_USBCS, 1, &value);
	if (ret < 0)
		uea_err(usb, "modem de-assert failed with error %d\n", ret);
	else
		uea_info(usb, "firmware uploaded\n");

683
	goto err;
684 685 686

err_fw_corrupted:
	uea_err(usb, "firmware is corrupted\n");
687
err:
688
	release_firmware(fw_entry);
689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
	uea_leaves(usb);
}

/**
 * uea_load_firmware - Load usb firmware for pre-firmware devices.
 */
static int uea_load_firmware(struct usb_device *usb, unsigned int ver)
{
	int ret;
	char *fw_name = FW_DIR "eagle.fw";

	uea_enters(usb);
	uea_info(usb, "pre-firmware device, uploading firmware\n");

	switch (ver) {
	case ADI930:
		fw_name = FW_DIR "adi930.fw";
		break;
	case EAGLE_I:
		fw_name = FW_DIR "eagleI.fw";
		break;
	case EAGLE_II:
		fw_name = FW_DIR "eagleII.fw";
		break;
	case EAGLE_III:
		fw_name = FW_DIR "eagleIII.fw";
		break;
716 717 718
	case EAGLE_IV:
		fw_name = FW_DIR "eagleIV.fw";
		break;
719 720
	}

721
	ret = request_firmware_nowait(THIS_MODULE, 1, fw_name, &usb->dev,
722 723
					GFP_KERNEL, usb,
					uea_upload_pre_firmware);
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
	if (ret)
		uea_err(usb, "firmware %s is not available\n", fw_name);
	else
		uea_info(usb, "loading firmware %s\n", fw_name);

	uea_leaves(usb);
	return ret;
}

/* modem management : dsp firmware, send/read CMV, monitoring statistic
 */

/*
 * Make sure that the DSP code provided is safe to use.
 */
739
static int check_dsp_e1(const u8 *dsp, unsigned int len)
740 741 742 743 744 745 746 747 748 749 750 751 752 753 754
{
	u8 pagecount, blockcount;
	u16 blocksize;
	u32 pageoffset;
	unsigned int i, j, p, pp;

	pagecount = FW_GET_BYTE(dsp);
	p = 1;

	/* enough space for page offsets? */
	if (p + 4 * pagecount > len)
		return 1;

	for (i = 0; i < pagecount; i++) {

755
		pageoffset = get_unaligned_le32(dsp + p);
756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775
		p += 4;

		if (pageoffset == 0)
			continue;

		/* enough space for blockcount? */
		if (pageoffset >= len)
			return 1;

		pp = pageoffset;
		blockcount = FW_GET_BYTE(dsp + pp);
		pp += 1;

		for (j = 0; j < blockcount; j++) {

			/* enough space for block header? */
			if (pp + 4 > len)
				return 1;

			pp += 2;	/* skip blockaddr */
776
			blocksize = get_unaligned_le16(dsp + pp);
777 778 779 780 781 782 783 784 785 786 787 788 789
			pp += 2;

			/* enough space for block data? */
			if (pp + blocksize > len)
				return 1;

			pp += blocksize;
		}
	}

	return 0;
}

790
static int check_dsp_e4(const u8 *dsp, int len)
791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834
{
	int i;
	struct l1_code *p = (struct l1_code *) dsp;
	unsigned int sum = p->code - dsp;

	if (len < sum)
		return 1;

	if (strcmp("STRATIPHY ANEXA", p->string_header) != 0 &&
	    strcmp("STRATIPHY ANEXB", p->string_header) != 0)
		return 1;

	for (i = 0; i < E4_MAX_PAGE_NUMBER; i++) {
		struct block_index *blockidx;
		u8 blockno = p->page_number_to_block_index[i];
		if (blockno >= E4_NO_SWAPPAGE_HEADERS)
			continue;

		do {
			u64 l;

			if (blockno >= E4_NO_SWAPPAGE_HEADERS)
				return 1;

			blockidx = &p->page_header[blockno++];
			if ((u8 *)(blockidx + 1) - dsp  >= len)
				return 1;

			if (le16_to_cpu(blockidx->PageNumber) != i)
				return 1;

			l = E4_PAGE_BYTES(blockidx->PageSize);
			sum += l;
			l += le32_to_cpu(blockidx->PageOffset);
			if (l > len)
				return 1;

		/* zero is zero regardless endianes */
		} while (blockidx->NotLastBlock);
	}

	return (sum == len) ? 0 : 1;
}

835 836 837
/*
 * send data to the idma pipe
 * */
838
static int uea_idma_write(struct uea_softc *sc, const void *data, u32 size)
839 840 841 842 843
{
	int ret = -ENOMEM;
	u8 *xfer_buff;
	int bytes_read;

844
	xfer_buff = kmemdup(data, size, GFP_KERNEL);
845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870
	if (!xfer_buff) {
		uea_err(INS_TO_USBDEV(sc), "can't allocate xfer_buff\n");
		return ret;
	}

	ret = usb_bulk_msg(sc->usb_dev,
			 usb_sndbulkpipe(sc->usb_dev, UEA_IDMA_PIPE),
			 xfer_buff, size, &bytes_read, BULK_TIMEOUT);

	kfree(xfer_buff);
	if (ret < 0)
		return ret;
	if (size != bytes_read) {
		uea_err(INS_TO_USBDEV(sc), "size != bytes_read %d %d\n", size,
		       bytes_read);
		return -EIO;
	}

	return 0;
}

static int request_dsp(struct uea_softc *sc)
{
	int ret;
	char *dsp_name;

871
	if (UEA_CHIP_VERSION(sc) == EAGLE_IV) {
872
		if (IS_ISDN(sc))
873 874 875 876
			dsp_name = FW_DIR "DSP4i.bin";
		else
			dsp_name = FW_DIR "DSP4p.bin";
	} else if (UEA_CHIP_VERSION(sc) == ADI930) {
877
		if (IS_ISDN(sc))
878 879 880 881
			dsp_name = FW_DIR "DSP9i.bin";
		else
			dsp_name = FW_DIR "DSP9p.bin";
	} else {
882
		if (IS_ISDN(sc))
883 884 885 886 887
			dsp_name = FW_DIR "DSPei.bin";
		else
			dsp_name = FW_DIR "DSPep.bin";
	}

M
matthieu castet 已提交
888
	ret = request_firmware(&sc->dsp_firm, dsp_name, &sc->usb_dev->dev);
889 890 891
	if (ret < 0) {
		uea_err(INS_TO_USBDEV(sc),
		       "requesting firmware %s failed with error %d\n",
892
			dsp_name, ret);
893 894 895
		return ret;
	}

896 897 898 899 900 901
	if (UEA_CHIP_VERSION(sc) == EAGLE_IV)
		ret = check_dsp_e4(sc->dsp_firm->data, sc->dsp_firm->size);
	else
		ret = check_dsp_e1(sc->dsp_firm->data, sc->dsp_firm->size);

	if (ret) {
902 903 904 905 906 907 908 909 910 911 912 913 914
		uea_err(INS_TO_USBDEV(sc), "firmware %s is corrupted\n",
		       dsp_name);
		release_firmware(sc->dsp_firm);
		sc->dsp_firm = NULL;
		return -EILSEQ;
	}

	return 0;
}

/*
 * The uea_load_page() function must be called within a process context
 */
915
static void uea_load_page_e1(struct work_struct *work)
916
{
D
David Howells 已提交
917
	struct uea_softc *sc = container_of(work, struct uea_softc, task);
918 919
	u16 pageno = sc->pageno;
	u16 ovl = sc->ovl;
920
	struct block_info_e1 bi;
921

922
	const u8 *p;
923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944
	u8 pagecount, blockcount;
	u16 blockaddr, blocksize;
	u32 pageoffset;
	int i;

	/* reload firmware when reboot start and it's loaded already */
	if (ovl == 0 && pageno == 0 && sc->dsp_firm) {
		release_firmware(sc->dsp_firm);
		sc->dsp_firm = NULL;
	}

	if (sc->dsp_firm == NULL && request_dsp(sc) < 0)
		return;

	p = sc->dsp_firm->data;
	pagecount = FW_GET_BYTE(p);
	p += 1;

	if (pageno >= pagecount)
		goto bad1;

	p += 4 * pageno;
945
	pageoffset = get_unaligned_le32(p);
946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961

	if (pageoffset == 0)
		goto bad1;

	p = sc->dsp_firm->data + pageoffset;
	blockcount = FW_GET_BYTE(p);
	p += 1;

	uea_dbg(INS_TO_USBDEV(sc),
	       "sending %u blocks for DSP page %u\n", blockcount, pageno);

	bi.wHdr = cpu_to_le16(UEA_BIHDR);
	bi.wOvl = cpu_to_le16(ovl);
	bi.wOvlOffset = cpu_to_le16(ovl | 0x8000);

	for (i = 0; i < blockcount; i++) {
962
		blockaddr = get_unaligned_le16(p);
963 964
		p += 2;

965
		blocksize = get_unaligned_le16(p);
966 967 968 969 970 971 972
		p += 2;

		bi.wSize = cpu_to_le16(blocksize);
		bi.wAddress = cpu_to_le16(blockaddr);
		bi.wLast = cpu_to_le16((i == blockcount - 1) ? 1 : 0);

		/* send block info through the IDMA pipe */
973
		if (uea_idma_write(sc, &bi, E1_BLOCK_INFO_SIZE))
974 975 976 977 978 979 980 981 982 983 984 985 986 987 988
			goto bad2;

		/* send block data through the IDMA pipe */
		if (uea_idma_write(sc, p, blocksize))
			goto bad2;

		p += blocksize;
	}

	return;

bad2:
	uea_err(INS_TO_USBDEV(sc), "sending DSP block %u failed\n", i);
	return;
bad1:
M
matthieu castet 已提交
989
	uea_err(INS_TO_USBDEV(sc), "invalid DSP page %u requested\n", pageno);
990 991
}

992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004
static void __uea_load_page_e4(struct uea_softc *sc, u8 pageno, int boot)
{
	struct block_info_e4 bi;
	struct block_index *blockidx;
	struct l1_code *p = (struct l1_code *) sc->dsp_firm->data;
	u8 blockno = p->page_number_to_block_index[pageno];

	bi.wHdr = cpu_to_be16(UEA_BIHDR);
	bi.bBootPage = boot;
	bi.bPageNumber = pageno;
	bi.wReserved = cpu_to_be16(UEA_RESERVED);

	do {
1005
		const u8 *blockoffset;
1006 1007 1008 1009
		unsigned int blocksize;

		blockidx = &p->page_header[blockno];
		blocksize = E4_PAGE_BYTES(blockidx->PageSize);
1010 1011
		blockoffset = sc->dsp_firm->data + le32_to_cpu(
							blockidx->PageOffset);
1012 1013

		bi.dwSize = cpu_to_be32(blocksize);
A
Al Viro 已提交
1014
		bi.dwAddress = cpu_to_be32(le32_to_cpu(blockidx->PageAddress));
1015 1016

		uea_dbg(INS_TO_USBDEV(sc),
1017 1018 1019 1020
			"sending block %u for DSP page "
			"%u size %u address %x\n",
			blockno, pageno, blocksize,
			le32_to_cpu(blockidx->PageAddress));
1021 1022 1023 1024 1025 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 1052 1053 1054 1055 1056 1057 1058 1059

		/* send block info through the IDMA pipe */
		if (uea_idma_write(sc, &bi, E4_BLOCK_INFO_SIZE))
			goto bad;

		/* send block data through the IDMA pipe */
		if (uea_idma_write(sc, blockoffset, blocksize))
			goto bad;

		blockno++;
	} while (blockidx->NotLastBlock);

	return;

bad:
	uea_err(INS_TO_USBDEV(sc), "sending DSP block %u failed\n", blockno);
	return;
}

static void uea_load_page_e4(struct work_struct *work)
{
	struct uea_softc *sc = container_of(work, struct uea_softc, task);
	u8 pageno = sc->pageno;
	int i;
	struct block_info_e4 bi;
	struct l1_code *p;

	uea_dbg(INS_TO_USBDEV(sc), "sending DSP page %u\n", pageno);

	/* reload firmware when reboot start and it's loaded already */
	if (pageno == 0 && sc->dsp_firm) {
		release_firmware(sc->dsp_firm);
		sc->dsp_firm = NULL;
	}

	if (sc->dsp_firm == NULL && request_dsp(sc) < 0)
		return;

	p = (struct l1_code *) sc->dsp_firm->data;
A
Al Viro 已提交
1060
	if (pageno >= le16_to_cpu(p->page_header[0].PageNumber)) {
1061 1062
		uea_err(INS_TO_USBDEV(sc), "invalid DSP "
						"page %u requested\n", pageno);
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
		return;
	}

	if (pageno != 0) {
		__uea_load_page_e4(sc, pageno, 0);
		return;
	}

	uea_dbg(INS_TO_USBDEV(sc),
	       "sending Main DSP page %u\n", p->page_header[0].PageNumber);

	for (i = 0; i < le16_to_cpu(p->page_header[0].PageNumber); i++) {
		if (E4_IS_BOOT_PAGE(p->page_header[i].PageSize))
			__uea_load_page_e4(sc, i, 1);
	}

1079
	uea_dbg(INS_TO_USBDEV(sc) , "sending start bi\n");
1080 1081 1082 1083 1084 1085

	bi.wHdr = cpu_to_be16(UEA_BIHDR);
	bi.bBootPage = 0;
	bi.bPageNumber = 0xff;
	bi.wReserved = cpu_to_be16(UEA_RESERVED);
	bi.dwSize = cpu_to_be32(E4_PAGE_BYTES(p->page_header[0].PageSize));
A
Al Viro 已提交
1086
	bi.dwAddress = cpu_to_be32(le32_to_cpu(p->page_header[0].PageAddress));
1087 1088 1089 1090 1091 1092

	/* send block info through the IDMA pipe */
	if (uea_idma_write(sc, &bi, E4_BLOCK_INFO_SIZE))
		uea_err(INS_TO_USBDEV(sc), "sending DSP start bi failed\n");
}

1093 1094
static inline void wake_up_cmv_ack(struct uea_softc *sc)
{
M
matthieu castet 已提交
1095
	BUG_ON(sc->cmv_ack);
1096
	sc->cmv_ack = 1;
1097
	wake_up(&sc->sync_q);
1098 1099 1100 1101
}

static inline int wait_cmv_ack(struct uea_softc *sc)
{
1102 1103
	int ret = uea_wait(sc, sc->cmv_ack , ACK_TIMEOUT);

1104 1105
	sc->cmv_ack = 0;

M
matthieu castet 已提交
1106 1107 1108
	uea_dbg(INS_TO_USBDEV(sc), "wait_event_timeout : %d ms\n",
			jiffies_to_msecs(ret));

1109 1110 1111 1112 1113 1114 1115 1116 1117
	if (ret < 0)
		return ret;

	return (ret == 0) ? -ETIMEDOUT : 0;
}

#define UCDC_SEND_ENCAPSULATED_COMMAND 0x00

static int uea_request(struct uea_softc *sc,
1118
		u16 value, u16 index, u16 size, const void *data)
1119 1120 1121 1122
{
	u8 *xfer_buff;
	int ret = -ENOMEM;

1123
	xfer_buff = kmemdup(data, size, GFP_KERNEL);
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149
	if (!xfer_buff) {
		uea_err(INS_TO_USBDEV(sc), "can't allocate xfer_buff\n");
		return ret;
	}

	ret = usb_control_msg(sc->usb_dev, usb_sndctrlpipe(sc->usb_dev, 0),
			      UCDC_SEND_ENCAPSULATED_COMMAND,
			      USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
			      value, index, xfer_buff, size, CTRL_TIMEOUT);

	kfree(xfer_buff);
	if (ret < 0) {
		uea_err(INS_TO_USBDEV(sc), "usb_control_msg error %d\n", ret);
		return ret;
	}

	if (ret != size) {
		uea_err(INS_TO_USBDEV(sc),
		       "usb_control_msg send only %d bytes (instead of %d)\n",
		       ret, size);
		return -EIO;
	}

	return 0;
}

1150
static int uea_cmv_e1(struct uea_softc *sc,
1151 1152
		u8 function, u32 address, u16 offset, u32 data)
{
1153
	struct cmv_e1 cmv;
1154 1155
	int ret;

M
matthieu castet 已提交
1156 1157 1158
	uea_enters(INS_TO_USBDEV(sc));
	uea_vdbg(INS_TO_USBDEV(sc), "Function : %d-%d, Address : %c%c%c%c, "
			"offset : 0x%04x, data : 0x%08x\n",
1159 1160 1161 1162
			E1_FUNCTION_TYPE(function),
			E1_FUNCTION_SUBTYPE(function),
			E1_GETSA1(address), E1_GETSA2(address),
			E1_GETSA3(address),
1163 1164
			E1_GETSA4(address), offset, data);

1165
	/* we send a request, but we expect a reply */
1166 1167 1168 1169
	sc->cmv_dsc.e1.function = function | 0x2;
	sc->cmv_dsc.e1.idx++;
	sc->cmv_dsc.e1.address = address;
	sc->cmv_dsc.e1.offset = offset;
1170

1171 1172
	cmv.wPreamble = cpu_to_le16(E1_PREAMBLE);
	cmv.bDirection = E1_HOSTTOMODEM;
1173
	cmv.bFunction = function;
1174
	cmv.wIndex = cpu_to_le16(sc->cmv_dsc.e1.idx);
1175
	put_unaligned_le32(address, &cmv.dwSymbolicAddress);
1176
	cmv.wOffsetAddress = cpu_to_le16(offset);
1177
	put_unaligned_le32(data >> 16 | data << 16, &cmv.dwData);
1178

1179 1180
	ret = uea_request(sc, UEA_E1_SET_BLOCK, UEA_MPTX_START,
							sizeof(cmv), &cmv);
1181 1182 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 1208 1209 1210 1211 1212 1213
	if (ret < 0)
		return ret;
	ret = wait_cmv_ack(sc);
	uea_leaves(INS_TO_USBDEV(sc));
	return ret;
}

static int uea_cmv_e4(struct uea_softc *sc,
		u16 function, u16 group, u16 address, u16 offset, u32 data)
{
	struct cmv_e4 cmv;
	int ret;

	uea_enters(INS_TO_USBDEV(sc));
	memset(&cmv, 0, sizeof(cmv));

	uea_vdbg(INS_TO_USBDEV(sc), "Function : %d-%d, Group : 0x%04x, "
		 "Address : 0x%04x, offset : 0x%04x, data : 0x%08x\n",
		 E4_FUNCTION_TYPE(function), E4_FUNCTION_SUBTYPE(function),
		 group, address, offset, data);

	/* we send a request, but we expect a reply */
	sc->cmv_dsc.e4.function = function | (0x1 << 4);
	sc->cmv_dsc.e4.offset = offset;
	sc->cmv_dsc.e4.address = address;
	sc->cmv_dsc.e4.group = group;

	cmv.wFunction = cpu_to_be16(function);
	cmv.wGroup = cpu_to_be16(group);
	cmv.wAddress = cpu_to_be16(address);
	cmv.wOffset = cpu_to_be16(offset);
	cmv.dwData[0] = cpu_to_be32(data);

1214 1215
	ret = uea_request(sc, UEA_E4_SET_BLOCK, UEA_MPTX_START,
							sizeof(cmv), &cmv);
1216 1217
	if (ret < 0)
		return ret;
M
matthieu castet 已提交
1218 1219 1220
	ret = wait_cmv_ack(sc);
	uea_leaves(INS_TO_USBDEV(sc));
	return ret;
1221 1222
}

1223
static inline int uea_read_cmv_e1(struct uea_softc *sc,
1224 1225
		u32 address, u16 offset, u32 *data)
{
1226
	int ret = uea_cmv_e1(sc, E1_MAKEFUNCTION(E1_MEMACCESS, E1_REQUESTREAD),
1227 1228 1229 1230 1231
			  address, offset, 0);
	if (ret < 0)
		uea_err(INS_TO_USBDEV(sc),
			"reading cmv failed with error %d\n", ret);
	else
1232
		*data = sc->data;
1233 1234 1235 1236

	return ret;
}

1237 1238 1239
static inline int uea_read_cmv_e4(struct uea_softc *sc,
		u8 size, u16 group, u16 address, u16 offset, u32 *data)
{
1240 1241
	int ret = uea_cmv_e4(sc, E4_MAKEFUNCTION(E4_MEMACCESS,
							E4_REQUESTREAD, size),
1242 1243 1244 1245 1246
			  group, address, offset, 0);
	if (ret < 0)
		uea_err(INS_TO_USBDEV(sc),
			"reading cmv failed with error %d\n", ret);
	else {
1247
		*data = sc->data;
1248 1249 1250 1251 1252 1253 1254 1255
		/* size is in 16-bit word quantities */
		if (size > 2)
			*(data + 1) = sc->data1;
	}
	return ret;
}

static inline int uea_write_cmv_e1(struct uea_softc *sc,
1256 1257
		u32 address, u16 offset, u32 data)
{
1258
	int ret = uea_cmv_e1(sc, E1_MAKEFUNCTION(E1_MEMACCESS, E1_REQUESTWRITE),
1259 1260 1261 1262 1263 1264 1265 1266
			  address, offset, data);
	if (ret < 0)
		uea_err(INS_TO_USBDEV(sc),
			"writing cmv failed with error %d\n", ret);

	return ret;
}

1267 1268 1269
static inline int uea_write_cmv_e4(struct uea_softc *sc,
		u8 size, u16 group, u16 address, u16 offset, u32 data)
{
1270 1271
	int ret = uea_cmv_e4(sc, E4_MAKEFUNCTION(E4_MEMACCESS,
							E4_REQUESTWRITE, size),
1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291
			  group, address, offset, data);
	if (ret < 0)
		uea_err(INS_TO_USBDEV(sc),
			"writing cmv failed with error %d\n", ret);

	return ret;
}

static void uea_set_bulk_timeout(struct uea_softc *sc, u32 dsrate)
{
	int ret;
	u16 timeout;

	/* in bulk mode the modem have problem with high rate
	 * changing internal timing could improve things, but the
	 * value is misterious.
	 * ADI930 don't support it (-EPIPE error).
	 */

	if (UEA_CHIP_VERSION(sc) == ADI930 ||
1292
	    altsetting[sc->modem_index] > 0 ||
1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303
	    sc->stats.phy.dsrate == dsrate)
		return;

	/* Original timming (1Mbit/s) from ADI (used in windows driver) */
	timeout = (dsrate <= 1024*1024) ? 0 : 1;
	ret = uea_request(sc, UEA_SET_TIMEOUT, timeout, 0, NULL);
	uea_info(INS_TO_USBDEV(sc), "setting new timeout %d%s\n",
		 timeout,  ret < 0 ? " failed" : "");

}

1304 1305 1306 1307 1308
/*
 * Monitor the modem and update the stat
 * return 0 if everything is ok
 * return < 0 if an error occurs (-EAGAIN reboot needed)
 */
1309
static int uea_stat_e1(struct uea_softc *sc)
1310 1311 1312 1313 1314 1315 1316
{
	u32 data;
	int ret;

	uea_enters(INS_TO_USBDEV(sc));
	data = sc->stats.phy.state;

1317
	ret = uea_read_cmv_e1(sc, E1_SA_STAT, 0, &sc->stats.phy.state);
1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335
	if (ret < 0)
		return ret;

	switch (GET_STATUS(sc->stats.phy.state)) {
	case 0:		/* not yet synchronized */
		uea_dbg(INS_TO_USBDEV(sc),
		       "modem not yet synchronized\n");
		return 0;

	case 1:		/* initialization */
		uea_dbg(INS_TO_USBDEV(sc), "modem initializing\n");
		return 0;

	case 2:		/* operational */
		uea_vdbg(INS_TO_USBDEV(sc), "modem operational\n");
		break;

	case 3:		/* fail ... */
M
matthieu castet 已提交
1336
		uea_info(INS_TO_USBDEV(sc), "modem synchronization failed"
1337
					" (may be try other cmv/dsp)\n");
1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369
		return -EAGAIN;

	case 4 ... 6:	/* test state */
		uea_warn(INS_TO_USBDEV(sc),
				"modem in test mode - not supported\n");
		return -EAGAIN;

	case 7:		/* fast-retain ... */
		uea_info(INS_TO_USBDEV(sc), "modem in fast-retain mode\n");
		return 0;
	default:
		uea_err(INS_TO_USBDEV(sc), "modem invalid SW mode %d\n",
			GET_STATUS(sc->stats.phy.state));
		return -EAGAIN;
	}

	if (GET_STATUS(data) != 2) {
		uea_request(sc, UEA_SET_MODE, UEA_LOOPBACK_OFF, 0, NULL);
		uea_info(INS_TO_USBDEV(sc), "modem operational\n");

		/* release the dsp firmware as it is not needed until
		 * the next failure
		 */
		if (sc->dsp_firm) {
			release_firmware(sc->dsp_firm);
			sc->dsp_firm = NULL;
		}
	}

	/* always update it as atm layer could not be init when we switch to
	 * operational state
	 */
1370
	UPDATE_ATM_SIGNAL(ATM_PHY_SIG_FOUND);
1371 1372 1373 1374

	/* wake up processes waiting for synchronization */
	wake_up(&sc->sync_q);

1375
	ret = uea_read_cmv_e1(sc, E1_SA_DIAG, 2, &sc->stats.phy.flags);
1376 1377 1378 1379 1380 1381 1382 1383
	if (ret < 0)
		return ret;
	sc->stats.phy.mflags |= sc->stats.phy.flags;

	/* in case of a flags ( for example delineation LOSS (& 0x10)),
	 * we check the status again in order to detect the failure earlier
	 */
	if (sc->stats.phy.flags) {
M
matthieu castet 已提交
1384
		uea_dbg(INS_TO_USBDEV(sc), "Stat flag = 0x%x\n",
1385 1386 1387 1388
		       sc->stats.phy.flags);
		return 0;
	}

1389
	ret = uea_read_cmv_e1(sc, E1_SA_RATE, 0, &data);
1390 1391 1392
	if (ret < 0)
		return ret;

1393
	uea_set_bulk_timeout(sc, (data >> 16) * 32);
1394 1395 1396 1397
	sc->stats.phy.dsrate = (data >> 16) * 32;
	sc->stats.phy.usrate = (data & 0xffff) * 32;
	UPDATE_ATM_STAT(link_rate, sc->stats.phy.dsrate * 1000 / 424);

1398
	ret = uea_read_cmv_e1(sc, E1_SA_DIAG, 23, &data);
1399 1400 1401 1402
	if (ret < 0)
		return ret;
	sc->stats.phy.dsattenuation = (data & 0xff) / 2;

1403
	ret = uea_read_cmv_e1(sc, E1_SA_DIAG, 47, &data);
1404 1405 1406 1407
	if (ret < 0)
		return ret;
	sc->stats.phy.usattenuation = (data & 0xff) / 2;

1408
	ret = uea_read_cmv_e1(sc, E1_SA_DIAG, 25, &sc->stats.phy.dsmargin);
1409 1410 1411
	if (ret < 0)
		return ret;

1412
	ret = uea_read_cmv_e1(sc, E1_SA_DIAG, 49, &sc->stats.phy.usmargin);
1413 1414 1415
	if (ret < 0)
		return ret;

1416
	ret = uea_read_cmv_e1(sc, E1_SA_DIAG, 51, &sc->stats.phy.rxflow);
1417 1418 1419
	if (ret < 0)
		return ret;

1420
	ret = uea_read_cmv_e1(sc, E1_SA_DIAG, 52, &sc->stats.phy.txflow);
1421 1422 1423
	if (ret < 0)
		return ret;

1424
	ret = uea_read_cmv_e1(sc, E1_SA_DIAG, 54, &sc->stats.phy.dsunc);
1425 1426 1427 1428
	if (ret < 0)
		return ret;

	/* only for atu-c */
1429
	ret = uea_read_cmv_e1(sc, E1_SA_DIAG, 58, &sc->stats.phy.usunc);
1430 1431 1432
	if (ret < 0)
		return ret;

1433
	ret = uea_read_cmv_e1(sc, E1_SA_DIAG, 53, &sc->stats.phy.dscorr);
1434 1435 1436 1437
	if (ret < 0)
		return ret;

	/* only for atu-c */
1438
	ret = uea_read_cmv_e1(sc, E1_SA_DIAG, 57, &sc->stats.phy.uscorr);
1439 1440 1441
	if (ret < 0)
		return ret;

1442
	ret = uea_read_cmv_e1(sc, E1_SA_INFO, 8, &sc->stats.phy.vidco);
1443 1444 1445
	if (ret < 0)
		return ret;

1446
	ret = uea_read_cmv_e1(sc, E1_SA_INFO, 13, &sc->stats.phy.vidcpe);
1447 1448 1449 1450 1451 1452
	if (ret < 0)
		return ret;

	return 0;
}

1453
static int uea_stat_e4(struct uea_softc *sc)
1454
{
1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467
	u32 data;
	u32 tmp_arr[2];
	int ret;

	uea_enters(INS_TO_USBDEV(sc));
	data = sc->stats.phy.state;

	/* XXX only need to be done before operationnal... */
	ret = uea_read_cmv_e4(sc, 1, E4_SA_STAT, 0, 0, &sc->stats.phy.state);
	if (ret < 0)
		return ret;

	switch (sc->stats.phy.state) {
1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490
	case 0x0:	/* not yet synchronized */
	case 0x1:
	case 0x3:
	case 0x4:
		uea_dbg(INS_TO_USBDEV(sc), "modem not yet "
						"synchronized\n");
		return 0;
	case 0x5:	/* initialization */
	case 0x6:
	case 0x9:
	case 0xa:
		uea_dbg(INS_TO_USBDEV(sc), "modem initializing\n");
		return 0;
	case 0x2:	/* fail ... */
		uea_info(INS_TO_USBDEV(sc), "modem synchronization "
				"failed (may be try other cmv/dsp)\n");
		return -EAGAIN;
	case 0x7:	/* operational */
		break;
	default:
		uea_warn(INS_TO_USBDEV(sc), "unknown state: %x\n",
						sc->stats.phy.state);
		return 0;
1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508
	}

	if (data != 7) {
		uea_request(sc, UEA_SET_MODE, UEA_LOOPBACK_OFF, 0, NULL);
		uea_info(INS_TO_USBDEV(sc), "modem operational\n");

		/* release the dsp firmware as it is not needed until
		 * the next failure
		 */
		if (sc->dsp_firm) {
			release_firmware(sc->dsp_firm);
			sc->dsp_firm = NULL;
		}
	}

	/* always update it as atm layer could not be init when we switch to
	 * operational state
	 */
1509
	UPDATE_ATM_SIGNAL(ATM_PHY_SIG_FOUND);
1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529

	/* wake up processes waiting for synchronization */
	wake_up(&sc->sync_q);

	/* TODO improve this state machine :
	 * we need some CMV info : what they do and their unit
	 * we should find the equivalent of eagle3- CMV
	 */
	/* check flags */
	ret = uea_read_cmv_e4(sc, 1, E4_SA_DIAG, 0, 0, &sc->stats.phy.flags);
	if (ret < 0)
		return ret;
	sc->stats.phy.mflags |= sc->stats.phy.flags;

	/* in case of a flags ( for example delineation LOSS (& 0x10)),
	 * we check the status again in order to detect the failure earlier
	 */
	if (sc->stats.phy.flags) {
		uea_dbg(INS_TO_USBDEV(sc), "Stat flag = 0x%x\n",
		       sc->stats.phy.flags);
1530
		if (sc->stats.phy.flags & 1) /* delineation LOSS */
1531
			return -EAGAIN;
1532
		if (sc->stats.phy.flags & 0x4000) /* Reset Flag */
1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577
			return -EAGAIN;
		return 0;
	}

	/* rate data may be in upper or lower half of 64 bit word, strange */
	ret = uea_read_cmv_e4(sc, 4, E4_SA_RATE, 0, 0, tmp_arr);
	if (ret < 0)
		return ret;
	data = (tmp_arr[0]) ? tmp_arr[0] : tmp_arr[1];
	sc->stats.phy.usrate = data / 1000;

	ret = uea_read_cmv_e4(sc, 4, E4_SA_RATE, 1, 0, tmp_arr);
	if (ret < 0)
		return ret;
	data = (tmp_arr[0]) ? tmp_arr[0] : tmp_arr[1];
	uea_set_bulk_timeout(sc, data / 1000);
	sc->stats.phy.dsrate = data / 1000;
	UPDATE_ATM_STAT(link_rate, sc->stats.phy.dsrate * 1000 / 424);

	ret = uea_read_cmv_e4(sc, 1, E4_SA_INFO, 68, 1, &data);
	if (ret < 0)
		return ret;
	sc->stats.phy.dsattenuation = data / 10;

	ret = uea_read_cmv_e4(sc, 1, E4_SA_INFO, 69, 1, &data);
	if (ret < 0)
		return ret;
	sc->stats.phy.usattenuation = data / 10;

	ret = uea_read_cmv_e4(sc, 1, E4_SA_INFO, 68, 3, &data);
	if (ret < 0)
		return ret;
	sc->stats.phy.dsmargin = data / 2;

	ret = uea_read_cmv_e4(sc, 1, E4_SA_INFO, 69, 3, &data);
	if (ret < 0)
		return ret;
	sc->stats.phy.usmargin = data / 10;

	return 0;
}

static void cmvs_file_name(struct uea_softc *sc, char *const cmv_name, int ver)
{
	char file_arr[] = "CMVxy.bin";
1578 1579
	char *file;

1580
	kparam_block_sysfs_write(cmv_file);
1581
	/* set proper name corresponding modem version and line type */
1582 1583
	if (cmv_file[sc->modem_index] == NULL) {
		if (UEA_CHIP_VERSION(sc) == ADI930)
1584 1585 1586
			file_arr[3] = '9';
		else if (UEA_CHIP_VERSION(sc) == EAGLE_IV)
			file_arr[3] = '4';
1587
		else
1588 1589
			file_arr[3] = 'e';

1590
		file_arr[4] = IS_ISDN(sc) ? 'i' : 'p';
1591
		file = file_arr;
1592 1593 1594 1595
	} else
		file = cmv_file[sc->modem_index];

	strcpy(cmv_name, FW_DIR);
1596
	strlcat(cmv_name, file, UEA_FW_NAME_MAX);
1597
	if (ver == 2)
1598
		strlcat(cmv_name, ".v2", UEA_FW_NAME_MAX);
1599
	kparam_unblock_sysfs_write(cmv_file);
1600 1601 1602 1603 1604 1605 1606
}

static int request_cmvs_old(struct uea_softc *sc,
		 void **cmvs, const struct firmware **fw)
{
	int ret, size;
	u8 *data;
1607
	char cmv_name[UEA_FW_NAME_MAX]; /* 30 bytes stack variable */
1608

1609
	cmvs_file_name(sc, cmv_name, 1);
1610 1611 1612 1613 1614 1615 1616 1617 1618
	ret = request_firmware(fw, cmv_name, &sc->usb_dev->dev);
	if (ret < 0) {
		uea_err(INS_TO_USBDEV(sc),
		       "requesting firmware %s failed with error %d\n",
		       cmv_name, ret);
		return ret;
	}

	data = (u8 *) (*fw)->data;
1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640
	size = (*fw)->size;
	if (size < 1)
		goto err_fw_corrupted;

	if (size != *data * sizeof(struct uea_cmvs_v1) + 1)
		goto err_fw_corrupted;

	*cmvs = (void *)(data + 1);
	return *data;

err_fw_corrupted:
	uea_err(INS_TO_USBDEV(sc), "firmware %s is corrupted\n", cmv_name);
	release_firmware(*fw);
	return -EILSEQ;
}

static int request_cmvs(struct uea_softc *sc,
		 void **cmvs, const struct firmware **fw, int *ver)
{
	int ret, size;
	u32 crc;
	u8 *data;
1641
	char cmv_name[UEA_FW_NAME_MAX]; /* 30 bytes stack variable */
1642 1643 1644 1645 1646 1647

	cmvs_file_name(sc, cmv_name, 2);
	ret = request_firmware(fw, cmv_name, &sc->usb_dev->dev);
	if (ret < 0) {
		/* if caller can handle old version, try to provide it */
		if (*ver == 1) {
1648 1649
			uea_warn(INS_TO_USBDEV(sc), "requesting "
							"firmware %s failed, "
1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662
				"try to get older cmvs\n", cmv_name);
			return request_cmvs_old(sc, cmvs, fw);
		}
		uea_err(INS_TO_USBDEV(sc),
		       "requesting firmware %s failed with error %d\n",
		       cmv_name, ret);
		return ret;
	}

	size = (*fw)->size;
	data = (u8 *) (*fw)->data;
	if (size < 4 || strncmp(data, "cmv2", 4) != 0) {
		if (*ver == 1) {
1663 1664
			uea_warn(INS_TO_USBDEV(sc), "firmware %s is corrupted,"
				" try to get older cmvs\n", cmv_name);
1665 1666 1667 1668
			release_firmware(*fw);
			return request_cmvs_old(sc, cmvs, fw);
		}
		goto err_fw_corrupted;
1669 1670
	}

1671 1672 1673 1674 1675 1676 1677
	*ver = 2;

	data += 4;
	size -= 4;
	if (size < 5)
		goto err_fw_corrupted;

1678
	crc = get_unaligned_le32(data);
1679 1680 1681 1682 1683 1684 1685 1686 1687
	data += 4;
	size -= 4;
	if (crc32_be(0, data, size) != crc)
		goto err_fw_corrupted;

	if (size != *data * sizeof(struct uea_cmvs_v2) + 1)
		goto err_fw_corrupted;

	*cmvs = (void *) (data + 1);
1688
	return *data;
1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700

err_fw_corrupted:
	uea_err(INS_TO_USBDEV(sc), "firmware %s is corrupted\n", cmv_name);
	release_firmware(*fw);
	return -EILSEQ;
}

static int uea_send_cmvs_e1(struct uea_softc *sc)
{
	int i, ret, len;
	void *cmvs_ptr;
	const struct firmware *cmvs_fw;
1701
	int ver = 1; /* we can handle v1 cmv firmware version; */
1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715

	/* Enter in R-IDLE (cmv) until instructed otherwise */
	ret = uea_write_cmv_e1(sc, E1_SA_CNTL, 0, 1);
	if (ret < 0)
		return ret;

	/* Dump firmware version */
	ret = uea_read_cmv_e1(sc, E1_SA_INFO, 10, &sc->stats.phy.firmid);
	if (ret < 0)
		return ret;
	uea_info(INS_TO_USBDEV(sc), "ATU-R firmware version : %x\n",
			sc->stats.phy.firmid);

	/* get options */
1716
	ret = len = request_cmvs(sc, &cmvs_ptr, &cmvs_fw, &ver);
1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727
	if (ret < 0)
		return ret;

	/* send options */
	if (ver == 1) {
		struct uea_cmvs_v1 *cmvs_v1 = cmvs_ptr;

		uea_warn(INS_TO_USBDEV(sc), "use deprecated cmvs version, "
			"please update your firmware\n");

		for (i = 0; i < len; i++) {
1728 1729 1730 1731
			ret = uea_write_cmv_e1(sc,
				get_unaligned_le32(&cmvs_v1[i].address),
				get_unaligned_le16(&cmvs_v1[i].offset),
				get_unaligned_le32(&cmvs_v1[i].data));
1732 1733 1734 1735 1736 1737 1738
			if (ret < 0)
				goto out;
		}
	} else if (ver == 2) {
		struct uea_cmvs_v2 *cmvs_v2 = cmvs_ptr;

		for (i = 0; i < len; i++) {
1739 1740 1741 1742
			ret = uea_write_cmv_e1(sc,
				get_unaligned_le32(&cmvs_v2[i].address),
				(u16) get_unaligned_le32(&cmvs_v2[i].offset),
				get_unaligned_le32(&cmvs_v2[i].data));
1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754
			if (ret < 0)
				goto out;
		}
	} else {
		/* This realy should not happen */
		uea_err(INS_TO_USBDEV(sc), "bad cmvs version %d\n", ver);
		goto out;
	}

	/* Enter in R-ACT-REQ */
	ret = uea_write_cmv_e1(sc, E1_SA_CNTL, 0, 2);
	uea_vdbg(INS_TO_USBDEV(sc), "Entering in R-ACT-REQ state\n");
1755 1756
	uea_info(INS_TO_USBDEV(sc), "modem started, waiting "
						"synchronization...\n");
1757 1758 1759 1760 1761 1762 1763 1764 1765 1766
out:
	release_firmware(cmvs_fw);
	return ret;
}

static int uea_send_cmvs_e4(struct uea_softc *sc)
{
	int i, ret, len;
	void *cmvs_ptr;
	const struct firmware *cmvs_fw;
1767
	int ver = 2; /* we can only handle v2 cmv firmware version; */
1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783

	/* Enter in R-IDLE (cmv) until instructed otherwise */
	ret = uea_write_cmv_e4(sc, 1, E4_SA_CNTL, 0, 0, 1);
	if (ret < 0)
		return ret;

	/* Dump firmware version */
	/* XXX don't read the 3th byte as it is always 6 */
	ret = uea_read_cmv_e4(sc, 2, E4_SA_INFO, 55, 0, &sc->stats.phy.firmid);
	if (ret < 0)
		return ret;
	uea_info(INS_TO_USBDEV(sc), "ATU-R firmware version : %x\n",
			sc->stats.phy.firmid);


	/* get options */
1784
	ret = len = request_cmvs(sc, &cmvs_ptr, &cmvs_fw, &ver);
1785 1786 1787 1788 1789 1790 1791 1792 1793
	if (ret < 0)
		return ret;

	/* send options */
	if (ver == 2) {
		struct uea_cmvs_v2 *cmvs_v2 = cmvs_ptr;

		for (i = 0; i < len; i++) {
			ret = uea_write_cmv_e4(sc, 1,
1794 1795 1796 1797
				get_unaligned_le32(&cmvs_v2[i].group),
				get_unaligned_le32(&cmvs_v2[i].address),
				get_unaligned_le32(&cmvs_v2[i].offset),
				get_unaligned_le32(&cmvs_v2[i].data));
1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809
			if (ret < 0)
				goto out;
		}
	} else {
		/* This realy should not happen */
		uea_err(INS_TO_USBDEV(sc), "bad cmvs version %d\n", ver);
		goto out;
	}

	/* Enter in R-ACT-REQ */
	ret = uea_write_cmv_e4(sc, 1, E4_SA_CNTL, 0, 0, 2);
	uea_vdbg(INS_TO_USBDEV(sc), "Entering in R-ACT-REQ state\n");
1810 1811
	uea_info(INS_TO_USBDEV(sc), "modem started, waiting "
						"synchronization...\n");
1812 1813 1814
out:
	release_firmware(cmvs_fw);
	return ret;
1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825
}

/* Start boot post firmware modem:
 * - send reset commands through usb control pipe
 * - start workqueue for DSP loading
 * - send CMV options to modem
 */

static int uea_start_reset(struct uea_softc *sc)
{
	u16 zero = 0;	/* ;-) */
1826
	int ret;
1827 1828 1829 1830

	uea_enters(INS_TO_USBDEV(sc));
	uea_info(INS_TO_USBDEV(sc), "(re)booting started\n");

1831
	/* mask interrupt */
1832
	sc->booting = 1;
1833 1834 1835 1836 1837
	/* We need to set this here because, a ack timeout could have occured,
	 * but before we start the reboot, the ack occurs and set this to 1.
	 * So we will failed to wait Ready CMV.
	 */
	sc->cmv_ack = 0;
1838
	UPDATE_ATM_SIGNAL(ATM_PHY_SIG_LOST);
1839 1840 1841 1842 1843 1844 1845 1846

	/* reset statistics */
	memset(&sc->stats, 0, sizeof(struct uea_stats));

	/* tell the modem that we want to boot in IDMA mode */
	uea_request(sc, UEA_SET_MODE, UEA_LOOPBACK_ON, 0, NULL);
	uea_request(sc, UEA_SET_MODE, UEA_BOOT_IDMA, 0, NULL);

1847
	/* enter reset mode */
1848 1849 1850
	uea_request(sc, UEA_SET_MODE, UEA_START_RESET, 0, NULL);

	/* original driver use 200ms, but windows driver use 100ms */
1851 1852 1853
	ret = uea_wait(sc, 0, msecs_to_jiffies(100));
	if (ret < 0)
		return ret;
1854 1855 1856 1857

	/* leave reset mode */
	uea_request(sc, UEA_SET_MODE, UEA_END_RESET, 0, NULL);

1858
	if (UEA_CHIP_VERSION(sc) != EAGLE_IV) {
1859
		/* clear tx and rx mailboxes */
1860 1861 1862 1863
		uea_request(sc, UEA_SET_2183_DATA, UEA_MPTX_MAILBOX, 2, &zero);
		uea_request(sc, UEA_SET_2183_DATA, UEA_MPRX_MAILBOX, 2, &zero);
		uea_request(sc, UEA_SET_2183_DATA, UEA_SWAP_MAILBOX, 2, &zero);
	}
1864

1865 1866 1867
	ret = uea_wait(sc, 0, msecs_to_jiffies(1000));
	if (ret < 0)
		return ret;
1868 1869

	if (UEA_CHIP_VERSION(sc) == EAGLE_IV)
1870 1871
		sc->cmv_dsc.e4.function = E4_MAKEFUNCTION(E4_ADSLDIRECTIVE,
							E4_MODEMREADY, 1);
1872
	else
1873 1874
		sc->cmv_dsc.e1.function = E1_MAKEFUNCTION(E1_ADSLDIRECTIVE,
							E1_MODEMREADY);
1875

1876
	/* demask interrupt */
1877 1878 1879 1880 1881
	sc->booting = 0;

	/* start loading DSP */
	sc->pageno = 0;
	sc->ovl = 0;
1882
	queue_work(sc->work_q, &sc->task);
1883 1884 1885 1886 1887 1888

	/* wait for modem ready CMV */
	ret = wait_cmv_ack(sc);
	if (ret < 0)
		return ret;

M
matthieu castet 已提交
1889 1890
	uea_vdbg(INS_TO_USBDEV(sc), "Ready CMV received\n");

1891
	ret = sc->send_cmvs(sc);
1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910
	if (ret < 0)
		return ret;

	sc->reset = 0;
	uea_leaves(INS_TO_USBDEV(sc));
	return ret;
}

/*
 * In case of an error wait 1s before rebooting the modem
 * if the modem don't request reboot (-EAGAIN).
 * Monitor the modem every 1s.
 */

static int uea_kthread(void *data)
{
	struct uea_softc *sc = data;
	int ret = -EAGAIN;

1911
	set_freezable();
1912 1913 1914 1915 1916
	uea_enters(INS_TO_USBDEV(sc));
	while (!kthread_should_stop()) {
		if (ret < 0 || sc->reset)
			ret = uea_start_reset(sc);
		if (!ret)
1917
			ret = sc->stat(sc);
1918
		if (ret != -EAGAIN)
1919
			uea_wait(sc, 0, msecs_to_jiffies(1000));
S
Stanislaw Gruszka 已提交
1920
		try_to_freeze();
1921 1922 1923 1924 1925 1926 1927 1928 1929 1930
	}
	uea_leaves(INS_TO_USBDEV(sc));
	return ret;
}

/* Load second usb firmware for ADI930 chip */
static int load_XILINX_firmware(struct uea_softc *sc)
{
	const struct firmware *fw_entry;
	int ret, size, u, ln;
1931 1932
	const u8 *pfw;
	u8 value;
1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961
	char *fw_name = FW_DIR "930-fpga.bin";

	uea_enters(INS_TO_USBDEV(sc));

	ret = request_firmware(&fw_entry, fw_name, &sc->usb_dev->dev);
	if (ret) {
		uea_err(INS_TO_USBDEV(sc), "firmware %s is not available\n",
		       fw_name);
		goto err0;
	}

	pfw = fw_entry->data;
	size = fw_entry->size;
	if (size != 0x577B) {
		uea_err(INS_TO_USBDEV(sc), "firmware %s is corrupted\n",
		       fw_name);
		ret = -EILSEQ;
		goto err1;
	}
	for (u = 0; u < size; u += ln) {
		ln = min(size - u, 64);
		ret = uea_request(sc, 0xe, 0, ln, pfw + u);
		if (ret < 0) {
			uea_err(INS_TO_USBDEV(sc),
			       "elsa download data failed (%d)\n", ret);
			goto err1;
		}
	}

M
matthieu castet 已提交
1962
	/* finish to send the fpga */
1963 1964 1965 1966 1967 1968 1969
	ret = uea_request(sc, 0xe, 1, 0, NULL);
	if (ret < 0) {
		uea_err(INS_TO_USBDEV(sc),
				"elsa download data failed (%d)\n", ret);
		goto err1;
	}

M
matthieu castet 已提交
1970
	/* Tell the modem we finish : de-assert reset */
1971 1972 1973
	value = 0;
	ret = uea_send_modem_cmd(sc->usb_dev, 0xe, 1, &value);
	if (ret < 0)
1974 1975
		uea_err(sc->usb_dev, "elsa de-assert failed with error"
								" %d\n", ret);
1976 1977 1978 1979 1980 1981 1982 1983

err1:
	release_firmware(fw_entry);
err0:
	uea_leaves(INS_TO_USBDEV(sc));
	return ret;
}

M
matthieu castet 已提交
1984
/* The modem send us an ack. First with check if it right */
1985
static void uea_dispatch_cmv_e1(struct uea_softc *sc, struct intr_pkt *intr)
1986
{
1987 1988 1989
	struct cmv_dsc_e1 *dsc = &sc->cmv_dsc.e1;
	struct cmv_e1 *cmv = &intr->u.e1.s2.cmv;

1990
	uea_enters(INS_TO_USBDEV(sc));
1991
	if (le16_to_cpu(cmv->wPreamble) != E1_PREAMBLE)
1992 1993
		goto bad1;

1994
	if (cmv->bDirection != E1_MODEMTOHOST)
1995 1996 1997
		goto bad1;

	/* FIXME : ADI930 reply wrong preambule (func = 2, sub = 2) to
1998
	 * the first MEMACCESS cmv. Ignore it...
1999
	 */
2000
	if (cmv->bFunction != dsc->function) {
2001
		if (UEA_CHIP_VERSION(sc) == ADI930
2002 2003
				&& cmv->bFunction ==  E1_MAKEFUNCTION(2, 2)) {
			cmv->wIndex = cpu_to_le16(dsc->idx);
2004 2005
			put_unaligned_le32(dsc->address,
						&cmv->dwSymbolicAddress);
2006 2007
			cmv->wOffsetAddress = cpu_to_le16(dsc->offset);
		} else
2008 2009 2010
			goto bad2;
	}

2011 2012
	if (cmv->bFunction == E1_MAKEFUNCTION(E1_ADSLDIRECTIVE,
							E1_MODEMREADY)) {
2013
		wake_up_cmv_ack(sc);
M
matthieu castet 已提交
2014
		uea_leaves(INS_TO_USBDEV(sc));
2015 2016 2017 2018
		return;
	}

	/* in case of MEMACCESS */
2019
	if (le16_to_cpu(cmv->wIndex) != dsc->idx ||
2020
	    get_unaligned_le32(&cmv->dwSymbolicAddress) != dsc->address ||
2021
	    le16_to_cpu(cmv->wOffsetAddress) != dsc->offset)
2022 2023
		goto bad2;

2024
	sc->data = get_unaligned_le32(&cmv->dwData);
2025 2026 2027
	sc->data = sc->data << 16 | sc->data >> 16;

	wake_up_cmv_ack(sc);
M
matthieu castet 已提交
2028
	uea_leaves(INS_TO_USBDEV(sc));
2029 2030 2031
	return;

bad2:
2032
	uea_err(INS_TO_USBDEV(sc), "unexpected cmv received, "
2033
			"Function : %d, Subfunction : %d\n",
2034 2035
			E1_FUNCTION_TYPE(cmv->bFunction),
			E1_FUNCTION_SUBTYPE(cmv->bFunction));
M
matthieu castet 已提交
2036
	uea_leaves(INS_TO_USBDEV(sc));
2037 2038 2039 2040 2041 2042
	return;

bad1:
	uea_err(INS_TO_USBDEV(sc), "invalid cmv received, "
			"wPreamble %d, bDirection %d\n",
			le16_to_cpu(cmv->wPreamble), cmv->bDirection);
M
matthieu castet 已提交
2043
	uea_leaves(INS_TO_USBDEV(sc));
2044 2045
}

2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060
/* The modem send us an ack. First with check if it right */
static void uea_dispatch_cmv_e4(struct uea_softc *sc, struct intr_pkt *intr)
{
	struct cmv_dsc_e4 *dsc = &sc->cmv_dsc.e4;
	struct cmv_e4 *cmv = &intr->u.e4.s2.cmv;

	uea_enters(INS_TO_USBDEV(sc));
	uea_dbg(INS_TO_USBDEV(sc), "cmv %x %x %x %x %x %x\n",
		be16_to_cpu(cmv->wGroup), be16_to_cpu(cmv->wFunction),
		be16_to_cpu(cmv->wOffset), be16_to_cpu(cmv->wAddress),
		be32_to_cpu(cmv->dwData[0]), be32_to_cpu(cmv->dwData[1]));

	if (be16_to_cpu(cmv->wFunction) != dsc->function)
		goto bad2;

2061 2062
	if (be16_to_cpu(cmv->wFunction) == E4_MAKEFUNCTION(E4_ADSLDIRECTIVE,
						E4_MODEMREADY, 1)) {
2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080
		wake_up_cmv_ack(sc);
		uea_leaves(INS_TO_USBDEV(sc));
		return;
	}

	/* in case of MEMACCESS */
	if (be16_to_cpu(cmv->wOffset) != dsc->offset ||
	    be16_to_cpu(cmv->wGroup) != dsc->group ||
	    be16_to_cpu(cmv->wAddress) != dsc->address)
		goto bad2;

	sc->data = be32_to_cpu(cmv->dwData[0]);
	sc->data1 = be32_to_cpu(cmv->dwData[1]);
	wake_up_cmv_ack(sc);
	uea_leaves(INS_TO_USBDEV(sc));
	return;

bad2:
2081
	uea_err(INS_TO_USBDEV(sc), "unexpected cmv received, "
2082 2083 2084 2085 2086 2087 2088
			"Function : %d, Subfunction : %d\n",
			E4_FUNCTION_TYPE(cmv->wFunction),
			E4_FUNCTION_SUBTYPE(cmv->wFunction));
	uea_leaves(INS_TO_USBDEV(sc));
	return;
}

2089 2090
static void uea_schedule_load_page_e1(struct uea_softc *sc,
						struct intr_pkt *intr)
2091 2092 2093
{
	sc->pageno = intr->e1_bSwapPageNo;
	sc->ovl = intr->e1_bOvl >> 4 | intr->e1_bOvl << 4;
2094
	queue_work(sc->work_q, &sc->task);
2095 2096
}

2097 2098
static void uea_schedule_load_page_e4(struct uea_softc *sc,
						struct intr_pkt *intr)
2099 2100
{
	sc->pageno = intr->e4_bSwapPageNo;
2101
	queue_work(sc->work_q, &sc->task);
2102 2103
}

2104 2105 2106
/*
 * interrupt handler
 */
2107
static void uea_intr(struct urb *urb)
2108
{
M
matthieu castet 已提交
2109 2110
	struct uea_softc *sc = urb->context;
	struct intr_pkt *intr = urb->transfer_buffer;
2111 2112
	int status = urb->status;

2113 2114
	uea_enters(INS_TO_USBDEV(sc));

2115
	if (unlikely(status < 0)) {
2116
		uea_err(INS_TO_USBDEV(sc), "uea_intr() failed with %d\n",
2117
		       status);
2118 2119 2120 2121 2122
		return;
	}

	/* device-to-host interrupt */
	if (intr->bType != 0x08 || sc->booting) {
M
matthieu castet 已提交
2123
		uea_err(INS_TO_USBDEV(sc), "wrong interrupt\n");
2124 2125 2126 2127 2128
		goto resubmit;
	}

	switch (le16_to_cpu(intr->wInterrupt)) {
	case INT_LOADSWAPPAGE:
2129
		sc->schedule_load_page(sc, intr);
2130 2131 2132
		break;

	case INT_INCOMINGCMV:
2133
		sc->dispatch_cmv(sc, intr);
2134 2135 2136
		break;

	default:
M
matthieu castet 已提交
2137
		uea_err(INS_TO_USBDEV(sc), "unknown interrupt %u\n",
2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149
		       le16_to_cpu(intr->wInterrupt));
	}

resubmit:
	usb_submit_urb(sc->urb_int, GFP_ATOMIC);
}

/*
 * Start the modem : init the data and start kernel thread
 */
static int uea_boot(struct uea_softc *sc)
{
2150
	int ret, size;
2151 2152 2153 2154
	struct intr_pkt *intr;

	uea_enters(INS_TO_USBDEV(sc));

2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170
	if (UEA_CHIP_VERSION(sc) == EAGLE_IV) {
		size = E4_INTR_PKT_SIZE;
		sc->dispatch_cmv = uea_dispatch_cmv_e4;
		sc->schedule_load_page = uea_schedule_load_page_e4;
		sc->stat = uea_stat_e4;
		sc->send_cmvs = uea_send_cmvs_e4;
		INIT_WORK(&sc->task, uea_load_page_e4);
	} else {
		size = E1_INTR_PKT_SIZE;
		sc->dispatch_cmv = uea_dispatch_cmv_e1;
		sc->schedule_load_page = uea_schedule_load_page_e1;
		sc->stat = uea_stat_e1;
		sc->send_cmvs = uea_send_cmvs_e1;
		INIT_WORK(&sc->task, uea_load_page_e1);
	}

2171 2172
	init_waitqueue_head(&sc->sync_q);

2173 2174 2175 2176 2177 2178 2179
	sc->work_q = create_workqueue("ueagle-dsp");
	if (!sc->work_q) {
		uea_err(INS_TO_USBDEV(sc), "cannot allocate workqueue\n");
		uea_leaves(INS_TO_USBDEV(sc));
		return -ENOMEM;
	}

2180 2181 2182
	if (UEA_CHIP_VERSION(sc) == ADI930)
		load_XILINX_firmware(sc);

2183
	intr = kmalloc(size, GFP_KERNEL);
2184 2185 2186
	if (!intr) {
		uea_err(INS_TO_USBDEV(sc),
		       "cannot allocate interrupt package\n");
2187
		goto err0;
2188 2189 2190 2191 2192
	}

	sc->urb_int = usb_alloc_urb(0, GFP_KERNEL);
	if (!sc->urb_int) {
		uea_err(INS_TO_USBDEV(sc), "cannot allocate interrupt URB\n");
2193
		goto err1;
2194 2195 2196 2197
	}

	usb_fill_int_urb(sc->urb_int, sc->usb_dev,
			 usb_rcvintpipe(sc->usb_dev, UEA_INTR_PIPE),
2198
			 intr, size, uea_intr, sc,
2199 2200 2201 2202 2203 2204 2205
			 sc->usb_dev->actconfig->interface[0]->altsetting[0].
			 endpoint[0].desc.bInterval);

	ret = usb_submit_urb(sc->urb_int, GFP_KERNEL);
	if (ret < 0) {
		uea_err(INS_TO_USBDEV(sc),
		       "urb submition failed with error %d\n", ret);
2206
		goto err1;
2207 2208
	}

2209 2210 2211 2212 2213
	/* Create worker thread, but don't start it here.  Start it after
	 * all usbatm generic initialization is done.
	 */
	sc->kthread = kthread_create(uea_kthread, sc, "ueagle-atm");
	if (IS_ERR(sc->kthread)) {
2214 2215 2216 2217 2218 2219 2220 2221 2222
		uea_err(INS_TO_USBDEV(sc), "failed to create thread\n");
		goto err2;
	}

	uea_leaves(INS_TO_USBDEV(sc));
	return 0;

err2:
	usb_kill_urb(sc->urb_int);
2223
err1:
2224
	usb_free_urb(sc->urb_int);
2225 2226
	sc->urb_int = NULL;
	kfree(intr);
2227 2228
err0:
	destroy_workqueue(sc->work_q);
2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240
	uea_leaves(INS_TO_USBDEV(sc));
	return -ENOMEM;
}

/*
 * Stop the modem : kill kernel thread and free data
 */
static void uea_stop(struct uea_softc *sc)
{
	int ret;
	uea_enters(INS_TO_USBDEV(sc));
	ret = kthread_stop(sc->kthread);
M
matthieu castet 已提交
2241
	uea_dbg(INS_TO_USBDEV(sc), "kthread finish with status %d\n", ret);
2242 2243 2244 2245 2246 2247 2248

	uea_request(sc, UEA_SET_MODE, UEA_LOOPBACK_ON, 0, NULL);

	usb_kill_urb(sc->urb_int);
	kfree(sc->urb_int->transfer_buffer);
	usb_free_urb(sc->urb_int);

2249 2250 2251
	/* stop any pending boot process, when no one can schedule work */
	destroy_workqueue(sc->work_q);

2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279
	if (sc->dsp_firm)
		release_firmware(sc->dsp_firm);
	uea_leaves(INS_TO_USBDEV(sc));
}

/* syfs interface */
static struct uea_softc *dev_to_uea(struct device *dev)
{
	struct usb_interface *intf;
	struct usbatm_data *usbatm;

	intf = to_usb_interface(dev);
	if (!intf)
		return NULL;

	usbatm = usb_get_intfdata(intf);
	if (!usbatm)
		return NULL;

	return usbatm->driver_data;
}

static ssize_t read_status(struct device *dev, struct device_attribute *attr,
		char *buf)
{
	int ret = -ENODEV;
	struct uea_softc *sc;

2280
	mutex_lock(&uea_mutex);
2281 2282 2283 2284 2285
	sc = dev_to_uea(dev);
	if (!sc)
		goto out;
	ret = snprintf(buf, 10, "%08x\n", sc->stats.phy.state);
out:
2286
	mutex_unlock(&uea_mutex);
2287 2288 2289 2290 2291 2292 2293 2294 2295
	return ret;
}

static ssize_t reboot(struct device *dev, struct device_attribute *attr,
		const char *buf, size_t count)
{
	int ret = -ENODEV;
	struct uea_softc *sc;

2296
	mutex_lock(&uea_mutex);
2297 2298 2299 2300 2301 2302
	sc = dev_to_uea(dev);
	if (!sc)
		goto out;
	sc->reset = 1;
	ret = count;
out:
2303
	mutex_unlock(&uea_mutex);
2304 2305 2306
	return ret;
}

2307
static DEVICE_ATTR(stat_status, S_IWUSR | S_IRUGO, read_status, reboot);
2308

2309 2310
static ssize_t read_human_status(struct device *dev,
			struct device_attribute *attr, char *buf)
2311 2312
{
	int ret = -ENODEV;
2313
	int modem_state;
2314 2315
	struct uea_softc *sc;

2316
	mutex_lock(&uea_mutex);
2317 2318 2319 2320
	sc = dev_to_uea(dev);
	if (!sc)
		goto out;

2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334
	if (UEA_CHIP_VERSION(sc) == EAGLE_IV) {
		switch (sc->stats.phy.state) {
		case 0x0:	/* not yet synchronized */
		case 0x1:
		case 0x3:
		case 0x4:
			modem_state = 0;
			break;
		case 0x5:	/* initialization */
		case 0x6:
		case 0x9:
		case 0xa:
			modem_state = 1;
			break;
2335
		case 0x7:	/* operational */
2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348
			modem_state = 2;
			break;
		case 0x2:	/* fail ... */
			modem_state = 3;
			break;
		default:	/* unknown */
			modem_state = 4;
			break;
		}
	} else
		modem_state = GET_STATUS(sc->stats.phy.state);

	switch (modem_state) {
2349 2350 2351 2352 2353 2354 2355 2356 2357
	case 0:
		ret = sprintf(buf, "Modem is booting\n");
		break;
	case 1:
		ret = sprintf(buf, "Modem is initializing\n");
		break;
	case 2:
		ret = sprintf(buf, "Modem is operational\n");
		break;
2358
	case 3:
2359 2360
		ret = sprintf(buf, "Modem synchronization failed\n");
		break;
2361 2362 2363
	default:
		ret = sprintf(buf, "Modem state is unknown\n");
		break;
2364 2365
	}
out:
2366
	mutex_unlock(&uea_mutex);
2367 2368 2369
	return ret;
}

2370
static DEVICE_ATTR(stat_human_status, S_IRUGO, read_human_status, NULL);
2371 2372 2373 2374 2375 2376

static ssize_t read_delin(struct device *dev, struct device_attribute *attr,
		char *buf)
{
	int ret = -ENODEV;
	struct uea_softc *sc;
2377
	char *delin = "GOOD";
2378

2379
	mutex_lock(&uea_mutex);
2380 2381 2382 2383
	sc = dev_to_uea(dev);
	if (!sc)
		goto out;

2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396
	if (UEA_CHIP_VERSION(sc) == EAGLE_IV) {
		if (sc->stats.phy.flags & 0x4000)
			delin = "RESET";
		else if (sc->stats.phy.flags & 0x0001)
			delin = "LOSS";
	} else {
		if (sc->stats.phy.flags & 0x0C00)
			delin = "ERROR";
		else if (sc->stats.phy.flags & 0x0030)
			delin = "LOSS";
	}

	ret = sprintf(buf, "%s\n", delin);
2397
out:
2398
	mutex_unlock(&uea_mutex);
2399 2400 2401
	return ret;
}

2402
static DEVICE_ATTR(stat_delin, S_IRUGO, read_delin, NULL);
2403

2404
#define UEA_ATTR(name, reset)					\
2405
								\
2406
static ssize_t read_##name(struct device *dev,			\
2407
		struct device_attribute *attr, char *buf)	\
2408 2409 2410 2411 2412
{								\
	int ret = -ENODEV;					\
	struct uea_softc *sc;					\
								\
	mutex_lock(&uea_mutex);					\
2413
	sc = dev_to_uea(dev);					\
2414 2415
	if (!sc)						\
		goto out;					\
2416 2417 2418
	ret = snprintf(buf, 10, "%08x\n", sc->stats.phy.name);	\
	if (reset)						\
		sc->stats.phy.name = 0;				\
2419 2420 2421 2422
out:								\
	mutex_unlock(&uea_mutex);				\
	return ret;						\
}								\
2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439
								\
static DEVICE_ATTR(stat_##name, S_IRUGO, read_##name, NULL)

UEA_ATTR(mflags, 1);
UEA_ATTR(vidcpe, 0);
UEA_ATTR(usrate, 0);
UEA_ATTR(dsrate, 0);
UEA_ATTR(usattenuation, 0);
UEA_ATTR(dsattenuation, 0);
UEA_ATTR(usmargin, 0);
UEA_ATTR(dsmargin, 0);
UEA_ATTR(txflow, 0);
UEA_ATTR(rxflow, 0);
UEA_ATTR(uscorr, 0);
UEA_ATTR(dscorr, 0);
UEA_ATTR(usunc, 0);
UEA_ATTR(dsunc, 0);
M
matthieu castet 已提交
2440
UEA_ATTR(firmid, 0);
2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453

/* Retrieve the device End System Identifier (MAC) */

static int uea_getesi(struct uea_softc *sc, u_char * esi)
{
	unsigned char mac_str[2 * ETH_ALEN + 1];
	int i;
	if (usb_string
	    (sc->usb_dev, sc->usb_dev->descriptor.iSerialNumber, mac_str,
	     sizeof(mac_str)) != 2 * ETH_ALEN)
		return 1;

	for (i = 0; i < ETH_ALEN; i++)
2454 2455
		esi[i] = hex_to_bin(mac_str[2 * i]) * 16 +
			 hex_to_bin(mac_str[2 * i + 1]);
2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471

	return 0;
}

/* ATM stuff */
static int uea_atm_open(struct usbatm_data *usbatm, struct atm_dev *atm_dev)
{
	struct uea_softc *sc = usbatm->driver_data;

	return uea_getesi(sc, atm_dev->esi);
}

static int uea_heavy(struct usbatm_data *usbatm, struct usb_interface *intf)
{
	struct uea_softc *sc = usbatm->driver_data;

M
matthieu castet 已提交
2472
	wait_event_interruptible(sc->sync_q, IS_OPERATIONAL(sc));
2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495

	return 0;

}

static int claim_interface(struct usb_device *usb_dev,
			   struct usbatm_data *usbatm, int ifnum)
{
	int ret;
	struct usb_interface *intf = usb_ifnum_to_if(usb_dev, ifnum);

	if (!intf) {
		uea_err(usb_dev, "interface %d not found\n", ifnum);
		return -ENODEV;
	}

	ret = usb_driver_claim_interface(&uea_driver, intf, usbatm);
	if (ret != 0)
		uea_err(usb_dev, "can't claim interface %d, error %d\n", ifnum,
		       ret);
	return ret;
}

2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513
static struct attribute *attrs[] = {
	&dev_attr_stat_status.attr,
	&dev_attr_stat_mflags.attr,
	&dev_attr_stat_human_status.attr,
	&dev_attr_stat_delin.attr,
	&dev_attr_stat_vidcpe.attr,
	&dev_attr_stat_usrate.attr,
	&dev_attr_stat_dsrate.attr,
	&dev_attr_stat_usattenuation.attr,
	&dev_attr_stat_dsattenuation.attr,
	&dev_attr_stat_usmargin.attr,
	&dev_attr_stat_dsmargin.attr,
	&dev_attr_stat_txflow.attr,
	&dev_attr_stat_rxflow.attr,
	&dev_attr_stat_uscorr.attr,
	&dev_attr_stat_dscorr.attr,
	&dev_attr_stat_usunc.attr,
	&dev_attr_stat_dsunc.attr,
M
matthieu castet 已提交
2514
	&dev_attr_stat_firmid.attr,
M
matthieu castet 已提交
2515
	NULL,
2516 2517 2518 2519 2520
};
static struct attribute_group attr_grp = {
	.attrs = attrs,
};

2521
static int uea_bind(struct usbatm_data *usbatm, struct usb_interface *intf,
D
Duncan Sands 已提交
2522
		   const struct usb_device_id *id)
2523 2524 2525 2526
{
	struct usb_device *usb = interface_to_usbdev(intf);
	struct uea_softc *sc;
	int ret, ifnum = intf->altsetting->desc.bInterfaceNumber;
2527
	unsigned int alt;
2528 2529 2530 2531 2532 2533 2534

	uea_enters(usb);

	/* interface 0 is for firmware/monitoring */
	if (ifnum != UEA_INTR_IFACE_NO)
		return -ENODEV;

D
Duncan Sands 已提交
2535
	usbatm->flags = (sync_wait[modem_index] ? 0 : UDSL_SKIP_HEAVY_INIT);
2536 2537 2538 2539 2540 2541

	/* interface 1 is for outbound traffic */
	ret = claim_interface(usb, usbatm, UEA_US_IFACE_NO);
	if (ret < 0)
		return ret;

M
matthieu castet 已提交
2542
	/* ADI930 has only 2 interfaces and inbound traffic is on interface 1 */
2543 2544 2545 2546 2547 2548 2549 2550 2551
	if (UEA_CHIP_VERSION(id) != ADI930) {
		/* interface 2 is for inbound traffic */
		ret = claim_interface(usb, usbatm, UEA_DS_IFACE_NO);
		if (ret < 0)
			return ret;
	}

	sc = kzalloc(sizeof(struct uea_softc), GFP_KERNEL);
	if (!sc) {
2552
		uea_err(usb, "uea_init: not enough memory !\n");
2553 2554 2555 2556 2557 2558 2559 2560 2561
		return -ENOMEM;
	}

	sc->usb_dev = usb;
	usbatm->driver_data = sc;
	sc->usbatm = usbatm;
	sc->modem_index = (modem_index < NB_MODEM) ? modem_index++ : 0;
	sc->driver_info = id->driver_info;

2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572
	/* first try to use module parameter */
	if (annex[sc->modem_index] == 1)
		sc->annex = ANNEXA;
	else if (annex[sc->modem_index] == 2)
		sc->annex = ANNEXB;
	/* try to autodetect annex */
	else if (sc->driver_info & AUTO_ANNEX_A)
		sc->annex = ANNEXA;
	else if (sc->driver_info & AUTO_ANNEX_B)
		sc->annex = ANNEXB;
	else
2573 2574
		sc->annex = (le16_to_cpu
		(sc->usb_dev->descriptor.bcdDevice) & 0x80) ? ANNEXB : ANNEXA;
2575

2576
	alt = altsetting[sc->modem_index];
2577
	/* ADI930 don't support iso */
2578
	if (UEA_CHIP_VERSION(id) != ADI930 && alt > 0) {
2579 2580
		if (alt <= 8 &&
			usb_set_interface(usb, UEA_DS_IFACE_NO, alt) == 0) {
2581
			uea_dbg(usb, "set alternate %u for 2 interface\n", alt);
2582 2583 2584
			uea_info(usb, "using iso mode\n");
			usbatm->flags |= UDSL_USE_ISOC | UDSL_IGNORE_EILSEQ;
		} else {
2585 2586
			uea_err(usb, "setting alternate %u failed for "
					"2 interface, using bulk mode\n", alt);
2587 2588 2589
		}
	}

M
matthieu castet 已提交
2590 2591 2592 2593
	ret = sysfs_create_group(&intf->dev.kobj, &attr_grp);
	if (ret < 0)
		goto error;

2594
	ret = uea_boot(sc);
M
matthieu castet 已提交
2595
	if (ret < 0)
2596
		goto error_rm_grp;
2597 2598

	return 0;
2599 2600 2601

error_rm_grp:
	sysfs_remove_group(&intf->dev.kobj, &attr_grp);
M
matthieu castet 已提交
2602 2603 2604
error:
	kfree(sc);
	return ret;
2605 2606 2607 2608 2609 2610
}

static void uea_unbind(struct usbatm_data *usbatm, struct usb_interface *intf)
{
	struct uea_softc *sc = usbatm->driver_data;

M
matthieu castet 已提交
2611
	sysfs_remove_group(&intf->dev.kobj, &attr_grp);
2612 2613 2614 2615 2616 2617 2618 2619 2620 2621
	uea_stop(sc);
	kfree(sc);
}

static struct usbatm_driver uea_usbatm_driver = {
	.driver_name = "ueagle-atm",
	.bind = uea_bind,
	.atm_start = uea_atm_open,
	.unbind = uea_unbind,
	.heavy_init = uea_heavy,
2622 2623
	.bulk_in = UEA_BULK_DATA_PIPE,
	.bulk_out = UEA_BULK_DATA_PIPE,
2624
	.isoc_in = UEA_ISO_DATA_PIPE,
2625 2626 2627 2628 2629
};

static int uea_probe(struct usb_interface *intf, const struct usb_device_id *id)
{
	struct usb_device *usb = interface_to_usbdev(intf);
2630
	int ret;
2631 2632

	uea_enters(usb);
2633 2634 2635 2636 2637
	uea_info(usb, "ADSL device founded vid (%#X) pid (%#X) Rev (%#X): %s\n",
		le16_to_cpu(usb->descriptor.idVendor),
		le16_to_cpu(usb->descriptor.idProduct),
		le16_to_cpu(usb->descriptor.bcdDevice),
		chip_name[UEA_CHIP_VERSION(id)]);
2638 2639 2640 2641 2642 2643

	usb_reset_device(usb);

	if (UEA_IS_PREFIRM(id))
		return uea_load_firmware(usb, UEA_CHIP_VERSION(id));

2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656
	ret = usbatm_usb_probe(intf, id, &uea_usbatm_driver);
	if (ret == 0) {
		struct usbatm_data *usbatm = usb_get_intfdata(intf);
		struct uea_softc *sc = usbatm->driver_data;

		/* Ensure carrier is initialized to off as early as possible */
		UPDATE_ATM_SIGNAL(ATM_PHY_SIG_LOST);

		/* Only start the worker thread when all init is done */
		wake_up_process(sc->kthread);
	}

	return ret;
2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668
}

static void uea_disconnect(struct usb_interface *intf)
{
	struct usb_device *usb = interface_to_usbdev(intf);
	int ifnum = intf->altsetting->desc.bInterfaceNumber;
	uea_enters(usb);

	/* ADI930 has 2 interfaces and eagle 3 interfaces.
	 * Pre-firmware device has one interface
	 */
	if (usb->config->desc.bNumInterfaces != 1 && ifnum == 0) {
2669
		mutex_lock(&uea_mutex);
2670
		usbatm_usb_disconnect(intf);
2671
		mutex_unlock(&uea_mutex);
2672 2673 2674 2675 2676 2677 2678 2679 2680 2681
		uea_info(usb, "ADSL device removed\n");
	}

	uea_leaves(usb);
}

/*
 * List of supported VID/PID
 */
static const struct usb_device_id uea_ids[] = {
2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749
	{USB_DEVICE(ANALOG_VID,	ADI930_PID_PREFIRM),
		.driver_info = ADI930 | PREFIRM},
	{USB_DEVICE(ANALOG_VID,	ADI930_PID_PSTFIRM),
		.driver_info = ADI930 | PSTFIRM},
	{USB_DEVICE(ANALOG_VID,	EAGLE_I_PID_PREFIRM),
		.driver_info = EAGLE_I | PREFIRM},
	{USB_DEVICE(ANALOG_VID,	EAGLE_I_PID_PSTFIRM),
		.driver_info = EAGLE_I | PSTFIRM},
	{USB_DEVICE(ANALOG_VID,	EAGLE_II_PID_PREFIRM),
		.driver_info = EAGLE_II | PREFIRM},
	{USB_DEVICE(ANALOG_VID,	EAGLE_II_PID_PSTFIRM),
		.driver_info = EAGLE_II | PSTFIRM},
	{USB_DEVICE(ANALOG_VID,	EAGLE_IIC_PID_PREFIRM),
		.driver_info = EAGLE_II | PREFIRM},
	{USB_DEVICE(ANALOG_VID,	EAGLE_IIC_PID_PSTFIRM),
		.driver_info = EAGLE_II | PSTFIRM},
	{USB_DEVICE(ANALOG_VID,	EAGLE_III_PID_PREFIRM),
		.driver_info = EAGLE_III | PREFIRM},
	{USB_DEVICE(ANALOG_VID,	EAGLE_III_PID_PSTFIRM),
		.driver_info = EAGLE_III | PSTFIRM},
	{USB_DEVICE(ANALOG_VID,	EAGLE_IV_PID_PREFIRM),
		.driver_info = EAGLE_IV | PREFIRM},
	{USB_DEVICE(ANALOG_VID,	EAGLE_IV_PID_PSTFIRM),
		.driver_info = EAGLE_IV | PSTFIRM},
	{USB_DEVICE(DEVOLO_VID,	DEVOLO_EAGLE_I_A_PID_PREFIRM),
		.driver_info = EAGLE_I | PREFIRM},
	{USB_DEVICE(DEVOLO_VID,	DEVOLO_EAGLE_I_A_PID_PSTFIRM),
		.driver_info = EAGLE_I | PSTFIRM | AUTO_ANNEX_A},
	{USB_DEVICE(DEVOLO_VID,	DEVOLO_EAGLE_I_B_PID_PREFIRM),
		.driver_info = EAGLE_I | PREFIRM},
	{USB_DEVICE(DEVOLO_VID,	DEVOLO_EAGLE_I_B_PID_PSTFIRM),
		.driver_info = EAGLE_I | PSTFIRM | AUTO_ANNEX_B},
	{USB_DEVICE(DEVOLO_VID,	DEVOLO_EAGLE_II_A_PID_PREFIRM),
		.driver_info = EAGLE_II | PREFIRM},
	{USB_DEVICE(DEVOLO_VID,	DEVOLO_EAGLE_II_A_PID_PSTFIRM),
		.driver_info = EAGLE_II | PSTFIRM | AUTO_ANNEX_A},
	{USB_DEVICE(DEVOLO_VID,	DEVOLO_EAGLE_II_B_PID_PREFIRM),
		.driver_info = EAGLE_II | PREFIRM},
	{USB_DEVICE(DEVOLO_VID,	DEVOLO_EAGLE_II_B_PID_PSTFIRM),
		.driver_info = EAGLE_II | PSTFIRM | AUTO_ANNEX_B},
	{USB_DEVICE(ELSA_VID,	ELSA_PID_PREFIRM),
		.driver_info = ADI930 | PREFIRM},
	{USB_DEVICE(ELSA_VID,	ELSA_PID_PSTFIRM),
		.driver_info = ADI930 | PSTFIRM},
	{USB_DEVICE(ELSA_VID,	ELSA_PID_A_PREFIRM),
		.driver_info = ADI930 | PREFIRM},
	{USB_DEVICE(ELSA_VID,	ELSA_PID_A_PSTFIRM),
		.driver_info = ADI930 | PSTFIRM | AUTO_ANNEX_A},
	{USB_DEVICE(ELSA_VID,	ELSA_PID_B_PREFIRM),
		.driver_info = ADI930 | PREFIRM},
	{USB_DEVICE(ELSA_VID,	ELSA_PID_B_PSTFIRM),
		.driver_info = ADI930 | PSTFIRM | AUTO_ANNEX_B},
	{USB_DEVICE(USR_VID,	MILLER_A_PID_PREFIRM),
		.driver_info = EAGLE_I | PREFIRM},
	{USB_DEVICE(USR_VID,	MILLER_A_PID_PSTFIRM),
		.driver_info = EAGLE_I | PSTFIRM  | AUTO_ANNEX_A},
	{USB_DEVICE(USR_VID,	MILLER_B_PID_PREFIRM),
		.driver_info = EAGLE_I | PREFIRM},
	{USB_DEVICE(USR_VID,	MILLER_B_PID_PSTFIRM),
		.driver_info = EAGLE_I | PSTFIRM  | AUTO_ANNEX_B},
	{USB_DEVICE(USR_VID,	HEINEKEN_A_PID_PREFIRM),
		.driver_info = EAGLE_I | PREFIRM},
	{USB_DEVICE(USR_VID,	HEINEKEN_A_PID_PSTFIRM),
		.driver_info = EAGLE_I | PSTFIRM | AUTO_ANNEX_A},
	{USB_DEVICE(USR_VID,	HEINEKEN_B_PID_PREFIRM),
		.driver_info = EAGLE_I | PREFIRM},
	{USB_DEVICE(USR_VID,	HEINEKEN_B_PID_PSTFIRM),
		.driver_info = EAGLE_I | PSTFIRM | AUTO_ANNEX_B},
2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798
	{}
};

/*
 * USB driver descriptor
 */
static struct usb_driver uea_driver = {
	.name = "ueagle-atm",
	.id_table = uea_ids,
	.probe = uea_probe,
	.disconnect = uea_disconnect,
};

MODULE_DEVICE_TABLE(usb, uea_ids);

/**
 * uea_init - Initialize the module.
 *      Register to USB subsystem
 */
static int __init uea_init(void)
{
	printk(KERN_INFO "[ueagle-atm] driver " EAGLEUSBVERSION " loaded\n");

	usb_register(&uea_driver);

	return 0;
}

module_init(uea_init);

/**
 * uea_exit  -  Destroy module
 *    Deregister with USB subsystem
 */
static void __exit uea_exit(void)
{
	/*
	 * This calls automatically the uea_disconnect method if necessary:
	 */
	usb_deregister(&uea_driver);

	printk(KERN_INFO "[ueagle-atm] driver unloaded\n");
}

module_exit(uea_exit);

MODULE_AUTHOR("Damien Bergamini/Matthieu Castet/Stanislaw W. Gruszka");
MODULE_DESCRIPTION("ADI 930/Eagle USB ADSL Modem driver");
MODULE_LICENSE("Dual BSD/GPL");