xref: /openbmc/linux/drivers/usb/atm/ueagle-atm.c (revision 0883c2c0)
1 /*-
2  * Copyright (c) 2003, 2004
3  *	Damien Bergamini <damien.bergamini@free.fr>. All rights reserved.
4  *
5  * Copyright (c) 2005-2007 Matthieu Castet <castet.matthieu@free.fr>
6  * Copyright (c) 2005-2007 Stanislaw Gruszka <stf_xl@wp.pl>
7  *
8  * This software is available to you under a choice of one of two
9  * licenses. You may choose to be licensed under the terms of the GNU
10  * General Public License (GPL) Version 2, available from the file
11  * COPYING in the main directory of this source tree, or the
12  * BSD license below:
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice unmodified, this list of conditions, and the following
19  *    disclaimer.
20  * 2. Redistributions in binary form must reproduce the above copyright
21  *    notice, this list of conditions and the following disclaimer in the
22  *    documentation and/or other materials provided with the distribution.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  * GPL license :
37  * This program is free software; you can redistribute it and/or
38  * modify it under the terms of the GNU General Public License
39  * as published by the Free Software Foundation; either version 2
40  * of the License, or (at your option) any later version.
41  *
42  * This program is distributed in the hope that it will be useful,
43  * but WITHOUT ANY WARRANTY; without even the implied warranty of
44  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
45  * GNU General Public License for more details.
46  *
47  * You should have received a copy of the GNU General Public License
48  * along with this program; if not, write to the Free Software
49  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
50  *
51  *
52  * HISTORY : some part of the code was base on ueagle 1.3 BSD driver,
53  * Damien Bergamini agree to put his code under a DUAL GPL/BSD license.
54  *
55  * The rest of the code was was rewritten from scratch.
56  */
57 
58 #include <linux/module.h>
59 #include <linux/moduleparam.h>
60 #include <linux/crc32.h>
61 #include <linux/usb.h>
62 #include <linux/firmware.h>
63 #include <linux/ctype.h>
64 #include <linux/sched.h>
65 #include <linux/kthread.h>
66 #include <linux/mutex.h>
67 #include <linux/freezer.h>
68 #include <linux/slab.h>
69 #include <linux/kernel.h>
70 
71 #include <asm/unaligned.h>
72 
73 #include "usbatm.h"
74 
75 #define EAGLEUSBVERSION "ueagle 1.4"
76 
77 
78 /*
79  * Debug macros
80  */
81 #define uea_dbg(usb_dev, format, args...)	\
82 	do { \
83 		if (debug >= 1) \
84 			dev_dbg(&(usb_dev)->dev, \
85 				"[ueagle-atm dbg] %s: " format, \
86 					__func__, ##args); \
87 	} while (0)
88 
89 #define uea_vdbg(usb_dev, format, args...)	\
90 	do { \
91 		if (debug >= 2) \
92 			dev_dbg(&(usb_dev)->dev, \
93 				"[ueagle-atm vdbg]  " format, ##args); \
94 	} while (0)
95 
96 #define uea_enters(usb_dev) \
97 	uea_vdbg(usb_dev, "entering %s\n" , __func__)
98 
99 #define uea_leaves(usb_dev) \
100 	uea_vdbg(usb_dev, "leaving  %s\n" , __func__)
101 
102 #define uea_err(usb_dev, format, args...) \
103 	dev_err(&(usb_dev)->dev , "[UEAGLE-ATM] " format , ##args)
104 
105 #define uea_warn(usb_dev, format, args...) \
106 	dev_warn(&(usb_dev)->dev , "[Ueagle-atm] " format, ##args)
107 
108 #define uea_info(usb_dev, format, args...) \
109 	dev_info(&(usb_dev)->dev , "[ueagle-atm] " format, ##args)
110 
111 struct intr_pkt;
112 
113 /* cmv's from firmware */
114 struct uea_cmvs_v1 {
115 	u32 address;
116 	u16 offset;
117 	u32 data;
118 } __packed;
119 
120 struct uea_cmvs_v2 {
121 	u32 group;
122 	u32 address;
123 	u32 offset;
124 	u32 data;
125 } __packed;
126 
127 /* information about currently processed cmv */
128 struct cmv_dsc_e1 {
129 	u8 function;
130 	u16 idx;
131 	u32 address;
132 	u16 offset;
133 };
134 
135 struct cmv_dsc_e4 {
136 	u16 function;
137 	u16 offset;
138 	u16 address;
139 	u16 group;
140 };
141 
142 union cmv_dsc {
143 	struct cmv_dsc_e1 e1;
144 	struct cmv_dsc_e4 e4;
145 };
146 
147 struct uea_softc {
148 	struct usb_device *usb_dev;
149 	struct usbatm_data *usbatm;
150 
151 	int modem_index;
152 	unsigned int driver_info;
153 	int annex;
154 #define ANNEXA 0
155 #define ANNEXB 1
156 
157 	int booting;
158 	int reset;
159 
160 	wait_queue_head_t sync_q;
161 
162 	struct task_struct *kthread;
163 	u32 data;
164 	u32 data1;
165 
166 	int cmv_ack;
167 	union cmv_dsc cmv_dsc;
168 
169 	struct work_struct task;
170 	u16 pageno;
171 	u16 ovl;
172 
173 	const struct firmware *dsp_firm;
174 	struct urb *urb_int;
175 
176 	void (*dispatch_cmv)(struct uea_softc *, struct intr_pkt *);
177 	void (*schedule_load_page)(struct uea_softc *, struct intr_pkt *);
178 	int (*stat)(struct uea_softc *);
179 	int (*send_cmvs)(struct uea_softc *);
180 
181 	/* keep in sync with eaglectl */
182 	struct uea_stats {
183 		struct {
184 			u32 state;
185 			u32 flags;
186 			u32 mflags;
187 			u32 vidcpe;
188 			u32 vidco;
189 			u32 dsrate;
190 			u32 usrate;
191 			u32 dsunc;
192 			u32 usunc;
193 			u32 dscorr;
194 			u32 uscorr;
195 			u32 txflow;
196 			u32 rxflow;
197 			u32 usattenuation;
198 			u32 dsattenuation;
199 			u32 dsmargin;
200 			u32 usmargin;
201 			u32 firmid;
202 		} phy;
203 	} stats;
204 };
205 
206 /*
207  * Elsa IDs
208  */
209 #define ELSA_VID		0x05CC
210 #define ELSA_PID_PSTFIRM	0x3350
211 #define ELSA_PID_PREFIRM	0x3351
212 
213 #define ELSA_PID_A_PREFIRM	0x3352
214 #define ELSA_PID_A_PSTFIRM	0x3353
215 #define ELSA_PID_B_PREFIRM	0x3362
216 #define ELSA_PID_B_PSTFIRM	0x3363
217 
218 /*
219  * Devolo IDs : pots if (pid & 0x10)
220  */
221 #define DEVOLO_VID			0x1039
222 #define DEVOLO_EAGLE_I_A_PID_PSTFIRM	0x2110
223 #define DEVOLO_EAGLE_I_A_PID_PREFIRM	0x2111
224 
225 #define DEVOLO_EAGLE_I_B_PID_PSTFIRM	0x2100
226 #define DEVOLO_EAGLE_I_B_PID_PREFIRM	0x2101
227 
228 #define DEVOLO_EAGLE_II_A_PID_PSTFIRM	0x2130
229 #define DEVOLO_EAGLE_II_A_PID_PREFIRM	0x2131
230 
231 #define DEVOLO_EAGLE_II_B_PID_PSTFIRM	0x2120
232 #define DEVOLO_EAGLE_II_B_PID_PREFIRM	0x2121
233 
234 /*
235  * Reference design USB IDs
236  */
237 #define ANALOG_VID		0x1110
238 #define ADI930_PID_PREFIRM	0x9001
239 #define ADI930_PID_PSTFIRM	0x9000
240 
241 #define EAGLE_I_PID_PREFIRM	0x9010	/* Eagle I */
242 #define EAGLE_I_PID_PSTFIRM	0x900F	/* Eagle I */
243 
244 #define EAGLE_IIC_PID_PREFIRM	0x9024	/* Eagle IIC */
245 #define EAGLE_IIC_PID_PSTFIRM	0x9023	/* Eagle IIC */
246 
247 #define EAGLE_II_PID_PREFIRM	0x9022	/* Eagle II */
248 #define EAGLE_II_PID_PSTFIRM	0x9021	/* Eagle II */
249 
250 #define EAGLE_III_PID_PREFIRM	0x9032	/* Eagle III */
251 #define EAGLE_III_PID_PSTFIRM	0x9031	/* Eagle III */
252 
253 #define EAGLE_IV_PID_PREFIRM	0x9042  /* Eagle IV */
254 #define EAGLE_IV_PID_PSTFIRM	0x9041  /* Eagle IV */
255 
256 /*
257  * USR USB IDs
258  */
259 #define USR_VID			0x0BAF
260 #define MILLER_A_PID_PREFIRM	0x00F2
261 #define MILLER_A_PID_PSTFIRM	0x00F1
262 #define MILLER_B_PID_PREFIRM	0x00FA
263 #define MILLER_B_PID_PSTFIRM	0x00F9
264 #define HEINEKEN_A_PID_PREFIRM	0x00F6
265 #define HEINEKEN_A_PID_PSTFIRM	0x00F5
266 #define HEINEKEN_B_PID_PREFIRM	0x00F8
267 #define HEINEKEN_B_PID_PSTFIRM	0x00F7
268 
269 #define PREFIRM 0
270 #define PSTFIRM (1<<7)
271 #define AUTO_ANNEX_A (1<<8)
272 #define AUTO_ANNEX_B (1<<9)
273 
274 enum {
275 	ADI930 = 0,
276 	EAGLE_I,
277 	EAGLE_II,
278 	EAGLE_III,
279 	EAGLE_IV
280 };
281 
282 /* macros for both struct usb_device_id and struct uea_softc */
283 #define UEA_IS_PREFIRM(x) \
284 	(!((x)->driver_info & PSTFIRM))
285 #define UEA_CHIP_VERSION(x) \
286 	((x)->driver_info & 0xf)
287 
288 #define IS_ISDN(x) \
289 	((x)->annex & ANNEXB)
290 
291 #define INS_TO_USBDEV(ins) (ins->usb_dev)
292 
293 #define GET_STATUS(data) \
294 	((data >> 8) & 0xf)
295 
296 #define IS_OPERATIONAL(sc) \
297 	((UEA_CHIP_VERSION(sc) != EAGLE_IV) ? \
298 	(GET_STATUS(sc->stats.phy.state) == 2) : \
299 	(sc->stats.phy.state == 7))
300 
301 /*
302  * Set of macros to handle unaligned data in the firmware blob.
303  * The FW_GET_BYTE() macro is provided only for consistency.
304  */
305 
306 #define FW_GET_BYTE(p) (*((__u8 *) (p)))
307 
308 #define FW_DIR "ueagle-atm/"
309 #define EAGLE_FIRMWARE FW_DIR "eagle.fw"
310 #define ADI930_FIRMWARE FW_DIR "adi930.fw"
311 #define EAGLE_I_FIRMWARE FW_DIR "eagleI.fw"
312 #define EAGLE_II_FIRMWARE FW_DIR "eagleII.fw"
313 #define EAGLE_III_FIRMWARE FW_DIR "eagleIII.fw"
314 #define EAGLE_IV_FIRMWARE FW_DIR "eagleIV.fw"
315 
316 #define DSP4I_FIRMWARE FW_DIR "DSP4i.bin"
317 #define DSP4P_FIRMWARE FW_DIR "DSP4p.bin"
318 #define DSP9I_FIRMWARE FW_DIR "DSP9i.bin"
319 #define DSP9P_FIRMWARE FW_DIR "DSP9p.bin"
320 #define DSPEI_FIRMWARE FW_DIR "DSPei.bin"
321 #define DSPEP_FIRMWARE FW_DIR "DSPep.bin"
322 #define FPGA930_FIRMWARE FW_DIR "930-fpga.bin"
323 
324 #define CMV4P_FIRMWARE FW_DIR "CMV4p.bin"
325 #define CMV4PV2_FIRMWARE FW_DIR "CMV4p.bin.v2"
326 #define CMV4I_FIRMWARE FW_DIR "CMV4i.bin"
327 #define CMV4IV2_FIRMWARE FW_DIR "CMV4i.bin.v2"
328 #define CMV9P_FIRMWARE FW_DIR "CMV9p.bin"
329 #define CMV9PV2_FIRMWARE FW_DIR "CMV9p.bin.v2"
330 #define CMV9I_FIRMWARE FW_DIR "CMV9i.bin"
331 #define CMV9IV2_FIRMWARE FW_DIR "CMV9i.bin.v2"
332 #define CMVEP_FIRMWARE FW_DIR "CMVep.bin"
333 #define CMVEPV2_FIRMWARE FW_DIR "CMVep.bin.v2"
334 #define CMVEI_FIRMWARE FW_DIR "CMVei.bin"
335 #define CMVEIV2_FIRMWARE FW_DIR "CMVei.bin.v2"
336 
337 #define UEA_FW_NAME_MAX 30
338 #define NB_MODEM 4
339 
340 #define BULK_TIMEOUT 300
341 #define CTRL_TIMEOUT 1000
342 
343 #define ACK_TIMEOUT msecs_to_jiffies(3000)
344 
345 #define UEA_INTR_IFACE_NO	0
346 #define UEA_US_IFACE_NO		1
347 #define UEA_DS_IFACE_NO		2
348 
349 #define FASTEST_ISO_INTF	8
350 
351 #define UEA_BULK_DATA_PIPE	0x02
352 #define UEA_IDMA_PIPE		0x04
353 #define UEA_INTR_PIPE		0x04
354 #define UEA_ISO_DATA_PIPE	0x08
355 
356 #define UEA_E1_SET_BLOCK	0x0001
357 #define UEA_E4_SET_BLOCK	0x002c
358 #define UEA_SET_MODE		0x0003
359 #define UEA_SET_2183_DATA	0x0004
360 #define UEA_SET_TIMEOUT		0x0011
361 
362 #define UEA_LOOPBACK_OFF	0x0002
363 #define UEA_LOOPBACK_ON		0x0003
364 #define UEA_BOOT_IDMA		0x0006
365 #define UEA_START_RESET		0x0007
366 #define UEA_END_RESET		0x0008
367 
368 #define UEA_SWAP_MAILBOX	(0x3fcd | 0x4000)
369 #define UEA_MPTX_START		(0x3fce | 0x4000)
370 #define UEA_MPTX_MAILBOX	(0x3fd6 | 0x4000)
371 #define UEA_MPRX_MAILBOX	(0x3fdf | 0x4000)
372 
373 /* block information in eagle4 dsp firmware  */
374 struct block_index {
375 	__le32 PageOffset;
376 	__le32 NotLastBlock;
377 	__le32 dummy;
378 	__le32 PageSize;
379 	__le32 PageAddress;
380 	__le16 dummy1;
381 	__le16 PageNumber;
382 } __packed;
383 
384 #define E4_IS_BOOT_PAGE(PageSize) ((le32_to_cpu(PageSize)) & 0x80000000)
385 #define E4_PAGE_BYTES(PageSize) ((le32_to_cpu(PageSize) & 0x7fffffff) * 4)
386 
387 #define E4_L1_STRING_HEADER 0x10
388 #define E4_MAX_PAGE_NUMBER 0x58
389 #define E4_NO_SWAPPAGE_HEADERS 0x31
390 
391 /* l1_code is eagle4 dsp firmware format */
392 struct l1_code {
393 	u8 string_header[E4_L1_STRING_HEADER];
394 	u8 page_number_to_block_index[E4_MAX_PAGE_NUMBER];
395 	struct block_index page_header[E4_NO_SWAPPAGE_HEADERS];
396 	u8 code[0];
397 } __packed;
398 
399 /* structures describing a block within a DSP page */
400 struct block_info_e1 {
401 	__le16 wHdr;
402 	__le16 wAddress;
403 	__le16 wSize;
404 	__le16 wOvlOffset;
405 	__le16 wOvl;		/* overlay */
406 	__le16 wLast;
407 } __packed;
408 #define E1_BLOCK_INFO_SIZE 12
409 
410 struct block_info_e4 {
411 	__be16 wHdr;
412 	__u8 bBootPage;
413 	__u8 bPageNumber;
414 	__be32 dwSize;
415 	__be32 dwAddress;
416 	__be16 wReserved;
417 } __packed;
418 #define E4_BLOCK_INFO_SIZE 14
419 
420 #define UEA_BIHDR 0xabcd
421 #define UEA_RESERVED 0xffff
422 
423 /* constants describing cmv type */
424 #define E1_PREAMBLE 0x535c
425 #define E1_MODEMTOHOST 0x01
426 #define E1_HOSTTOMODEM 0x10
427 
428 #define E1_MEMACCESS 0x1
429 #define E1_ADSLDIRECTIVE 0x7
430 #define E1_FUNCTION_TYPE(f) ((f) >> 4)
431 #define E1_FUNCTION_SUBTYPE(f) ((f) & 0x0f)
432 
433 #define E4_MEMACCESS 0
434 #define E4_ADSLDIRECTIVE 0xf
435 #define E4_FUNCTION_TYPE(f) ((f) >> 8)
436 #define E4_FUNCTION_SIZE(f) ((f) & 0x0f)
437 #define E4_FUNCTION_SUBTYPE(f) (((f) >> 4) & 0x0f)
438 
439 /* for MEMACCESS */
440 #define E1_REQUESTREAD	0x0
441 #define E1_REQUESTWRITE	0x1
442 #define E1_REPLYREAD	0x2
443 #define E1_REPLYWRITE	0x3
444 
445 #define E4_REQUESTREAD	0x0
446 #define E4_REQUESTWRITE	0x4
447 #define E4_REPLYREAD	(E4_REQUESTREAD | 1)
448 #define E4_REPLYWRITE	(E4_REQUESTWRITE | 1)
449 
450 /* for ADSLDIRECTIVE */
451 #define E1_KERNELREADY 0x0
452 #define E1_MODEMREADY  0x1
453 
454 #define E4_KERNELREADY 0x0
455 #define E4_MODEMREADY  0x1
456 
457 #define E1_MAKEFUNCTION(t, s) (((t) & 0xf) << 4 | ((s) & 0xf))
458 #define E4_MAKEFUNCTION(t, st, s) (((t) & 0xf) << 8 | \
459 	((st) & 0xf) << 4 | ((s) & 0xf))
460 
461 #define E1_MAKESA(a, b, c, d)						\
462 	(((c) & 0xff) << 24 |						\
463 	 ((d) & 0xff) << 16 |						\
464 	 ((a) & 0xff) << 8  |						\
465 	 ((b) & 0xff))
466 
467 #define E1_GETSA1(a) ((a >> 8) & 0xff)
468 #define E1_GETSA2(a) (a & 0xff)
469 #define E1_GETSA3(a) ((a >> 24) & 0xff)
470 #define E1_GETSA4(a) ((a >> 16) & 0xff)
471 
472 #define E1_SA_CNTL E1_MAKESA('C', 'N', 'T', 'L')
473 #define E1_SA_DIAG E1_MAKESA('D', 'I', 'A', 'G')
474 #define E1_SA_INFO E1_MAKESA('I', 'N', 'F', 'O')
475 #define E1_SA_OPTN E1_MAKESA('O', 'P', 'T', 'N')
476 #define E1_SA_RATE E1_MAKESA('R', 'A', 'T', 'E')
477 #define E1_SA_STAT E1_MAKESA('S', 'T', 'A', 'T')
478 
479 #define E4_SA_CNTL 1
480 #define E4_SA_STAT 2
481 #define E4_SA_INFO 3
482 #define E4_SA_TEST 4
483 #define E4_SA_OPTN 5
484 #define E4_SA_RATE 6
485 #define E4_SA_DIAG 7
486 #define E4_SA_CNFG 8
487 
488 /* structures representing a CMV (Configuration and Management Variable) */
489 struct cmv_e1 {
490 	__le16 wPreamble;
491 	__u8 bDirection;
492 	__u8 bFunction;
493 	__le16 wIndex;
494 	__le32 dwSymbolicAddress;
495 	__le16 wOffsetAddress;
496 	__le32 dwData;
497 } __packed;
498 
499 struct cmv_e4 {
500 	__be16 wGroup;
501 	__be16 wFunction;
502 	__be16 wOffset;
503 	__be16 wAddress;
504 	__be32 dwData[6];
505 } __packed;
506 
507 /* structures representing swap information */
508 struct swap_info_e1 {
509 	__u8 bSwapPageNo;
510 	__u8 bOvl;		/* overlay */
511 } __packed;
512 
513 struct swap_info_e4 {
514 	__u8 bSwapPageNo;
515 } __packed;
516 
517 /* structures representing interrupt data */
518 #define e1_bSwapPageNo	u.e1.s1.swapinfo.bSwapPageNo
519 #define e1_bOvl		u.e1.s1.swapinfo.bOvl
520 #define e4_bSwapPageNo  u.e4.s1.swapinfo.bSwapPageNo
521 
522 #define INT_LOADSWAPPAGE 0x0001
523 #define INT_INCOMINGCMV  0x0002
524 
525 union intr_data_e1 {
526 	struct {
527 		struct swap_info_e1 swapinfo;
528 		__le16 wDataSize;
529 	} __packed s1;
530 	struct {
531 		struct cmv_e1 cmv;
532 		__le16 wDataSize;
533 	} __packed s2;
534 } __packed;
535 
536 union intr_data_e4 {
537 	struct {
538 		struct swap_info_e4 swapinfo;
539 		__le16 wDataSize;
540 	} __packed s1;
541 	struct {
542 		struct cmv_e4 cmv;
543 		__le16 wDataSize;
544 	} __packed s2;
545 } __packed;
546 
547 struct intr_pkt {
548 	__u8 bType;
549 	__u8 bNotification;
550 	__le16 wValue;
551 	__le16 wIndex;
552 	__le16 wLength;
553 	__le16 wInterrupt;
554 	union {
555 		union intr_data_e1 e1;
556 		union intr_data_e4 e4;
557 	} u;
558 } __packed;
559 
560 #define E1_INTR_PKT_SIZE 28
561 #define E4_INTR_PKT_SIZE 64
562 
563 static struct usb_driver uea_driver;
564 static DEFINE_MUTEX(uea_mutex);
565 static const char * const chip_name[] = {
566 	"ADI930", "Eagle I", "Eagle II", "Eagle III", "Eagle IV"};
567 
568 static int modem_index;
569 static unsigned int debug;
570 static unsigned int altsetting[NB_MODEM] = {
571 				[0 ... (NB_MODEM - 1)] = FASTEST_ISO_INTF};
572 static bool sync_wait[NB_MODEM];
573 static char *cmv_file[NB_MODEM];
574 static int annex[NB_MODEM];
575 
576 module_param(debug, uint, 0644);
577 MODULE_PARM_DESC(debug, "module debug level (0=off,1=on,2=verbose)");
578 module_param_array(altsetting, uint, NULL, 0644);
579 MODULE_PARM_DESC(altsetting, "alternate setting for incoming traffic: 0=bulk, "
580 			     "1=isoc slowest, ... , 8=isoc fastest (default)");
581 module_param_array(sync_wait, bool, NULL, 0644);
582 MODULE_PARM_DESC(sync_wait, "wait the synchronisation before starting ATM");
583 module_param_array(cmv_file, charp, NULL, 0644);
584 MODULE_PARM_DESC(cmv_file,
585 		"file name with configuration and management variables");
586 module_param_array(annex, uint, NULL, 0644);
587 MODULE_PARM_DESC(annex,
588 		"manually set annex a/b (0=auto, 1=annex a, 2=annex b)");
589 
590 #define uea_wait(sc, cond, timeo) \
591 ({ \
592 	int _r = wait_event_interruptible_timeout(sc->sync_q, \
593 			(cond) || kthread_should_stop(), timeo); \
594 	if (kthread_should_stop()) \
595 		_r = -ENODEV; \
596 	_r; \
597 })
598 
599 #define UPDATE_ATM_STAT(type, val) \
600 	do { \
601 		if (sc->usbatm->atm_dev) \
602 			sc->usbatm->atm_dev->type = val; \
603 	} while (0)
604 
605 #define UPDATE_ATM_SIGNAL(val) \
606 	do { \
607 		if (sc->usbatm->atm_dev) \
608 			atm_dev_signal_change(sc->usbatm->atm_dev, val); \
609 	} while (0)
610 
611 
612 /* Firmware loading */
613 #define LOAD_INTERNAL     0xA0
614 #define F8051_USBCS       0x7f92
615 
616 /**
617  * uea_send_modem_cmd - Send a command for pre-firmware devices.
618  */
619 static int uea_send_modem_cmd(struct usb_device *usb,
620 			      u16 addr, u16 size, const u8 *buff)
621 {
622 	int ret = -ENOMEM;
623 	u8 *xfer_buff;
624 
625 	xfer_buff = kmemdup(buff, size, GFP_KERNEL);
626 	if (xfer_buff) {
627 		ret = usb_control_msg(usb,
628 				      usb_sndctrlpipe(usb, 0),
629 				      LOAD_INTERNAL,
630 				      USB_DIR_OUT | USB_TYPE_VENDOR |
631 				      USB_RECIP_DEVICE, addr, 0, xfer_buff,
632 				      size, CTRL_TIMEOUT);
633 		kfree(xfer_buff);
634 	}
635 
636 	if (ret < 0)
637 		return ret;
638 
639 	return (ret == size) ? 0 : -EIO;
640 }
641 
642 static void uea_upload_pre_firmware(const struct firmware *fw_entry,
643 								void *context)
644 {
645 	struct usb_device *usb = context;
646 	const u8 *pfw;
647 	u8 value;
648 	u32 crc = 0;
649 	int ret, size;
650 
651 	uea_enters(usb);
652 	if (!fw_entry) {
653 		uea_err(usb, "firmware is not available\n");
654 		goto err;
655 	}
656 
657 	pfw = fw_entry->data;
658 	size = fw_entry->size;
659 	if (size < 4)
660 		goto err_fw_corrupted;
661 
662 	crc = get_unaligned_le32(pfw);
663 	pfw += 4;
664 	size -= 4;
665 	if (crc32_be(0, pfw, size) != crc)
666 		goto err_fw_corrupted;
667 
668 	/*
669 	 * Start to upload firmware : send reset
670 	 */
671 	value = 1;
672 	ret = uea_send_modem_cmd(usb, F8051_USBCS, sizeof(value), &value);
673 
674 	if (ret < 0) {
675 		uea_err(usb, "modem reset failed with error %d\n", ret);
676 		goto err;
677 	}
678 
679 	while (size > 3) {
680 		u8 len = FW_GET_BYTE(pfw);
681 		u16 add = get_unaligned_le16(pfw + 1);
682 
683 		size -= len + 3;
684 		if (size < 0)
685 			goto err_fw_corrupted;
686 
687 		ret = uea_send_modem_cmd(usb, add, len, pfw + 3);
688 		if (ret < 0) {
689 			uea_err(usb, "uploading firmware data failed "
690 					"with error %d\n", ret);
691 			goto err;
692 		}
693 		pfw += len + 3;
694 	}
695 
696 	if (size != 0)
697 		goto err_fw_corrupted;
698 
699 	/*
700 	 * Tell the modem we finish : de-assert reset
701 	 */
702 	value = 0;
703 	ret = uea_send_modem_cmd(usb, F8051_USBCS, 1, &value);
704 	if (ret < 0)
705 		uea_err(usb, "modem de-assert failed with error %d\n", ret);
706 	else
707 		uea_info(usb, "firmware uploaded\n");
708 
709 	goto err;
710 
711 err_fw_corrupted:
712 	uea_err(usb, "firmware is corrupted\n");
713 err:
714 	release_firmware(fw_entry);
715 	uea_leaves(usb);
716 }
717 
718 /**
719  * uea_load_firmware - Load usb firmware for pre-firmware devices.
720  */
721 static int uea_load_firmware(struct usb_device *usb, unsigned int ver)
722 {
723 	int ret;
724 	char *fw_name = EAGLE_FIRMWARE;
725 
726 	uea_enters(usb);
727 	uea_info(usb, "pre-firmware device, uploading firmware\n");
728 
729 	switch (ver) {
730 	case ADI930:
731 		fw_name = ADI930_FIRMWARE;
732 		break;
733 	case EAGLE_I:
734 		fw_name = EAGLE_I_FIRMWARE;
735 		break;
736 	case EAGLE_II:
737 		fw_name = EAGLE_II_FIRMWARE;
738 		break;
739 	case EAGLE_III:
740 		fw_name = EAGLE_III_FIRMWARE;
741 		break;
742 	case EAGLE_IV:
743 		fw_name = EAGLE_IV_FIRMWARE;
744 		break;
745 	}
746 
747 	ret = request_firmware_nowait(THIS_MODULE, 1, fw_name, &usb->dev,
748 					GFP_KERNEL, usb,
749 					uea_upload_pre_firmware);
750 	if (ret)
751 		uea_err(usb, "firmware %s is not available\n", fw_name);
752 	else
753 		uea_info(usb, "loading firmware %s\n", fw_name);
754 
755 	uea_leaves(usb);
756 	return ret;
757 }
758 
759 /* modem management : dsp firmware, send/read CMV, monitoring statistic
760  */
761 
762 /*
763  * Make sure that the DSP code provided is safe to use.
764  */
765 static int check_dsp_e1(const u8 *dsp, unsigned int len)
766 {
767 	u8 pagecount, blockcount;
768 	u16 blocksize;
769 	u32 pageoffset;
770 	unsigned int i, j, p, pp;
771 
772 	pagecount = FW_GET_BYTE(dsp);
773 	p = 1;
774 
775 	/* enough space for page offsets? */
776 	if (p + 4 * pagecount > len)
777 		return 1;
778 
779 	for (i = 0; i < pagecount; i++) {
780 
781 		pageoffset = get_unaligned_le32(dsp + p);
782 		p += 4;
783 
784 		if (pageoffset == 0)
785 			continue;
786 
787 		/* enough space for blockcount? */
788 		if (pageoffset >= len)
789 			return 1;
790 
791 		pp = pageoffset;
792 		blockcount = FW_GET_BYTE(dsp + pp);
793 		pp += 1;
794 
795 		for (j = 0; j < blockcount; j++) {
796 
797 			/* enough space for block header? */
798 			if (pp + 4 > len)
799 				return 1;
800 
801 			pp += 2;	/* skip blockaddr */
802 			blocksize = get_unaligned_le16(dsp + pp);
803 			pp += 2;
804 
805 			/* enough space for block data? */
806 			if (pp + blocksize > len)
807 				return 1;
808 
809 			pp += blocksize;
810 		}
811 	}
812 
813 	return 0;
814 }
815 
816 static int check_dsp_e4(const u8 *dsp, int len)
817 {
818 	int i;
819 	struct l1_code *p = (struct l1_code *) dsp;
820 	unsigned int sum = p->code - dsp;
821 
822 	if (len < sum)
823 		return 1;
824 
825 	if (strcmp("STRATIPHY ANEXA", p->string_header) != 0 &&
826 	    strcmp("STRATIPHY ANEXB", p->string_header) != 0)
827 		return 1;
828 
829 	for (i = 0; i < E4_MAX_PAGE_NUMBER; i++) {
830 		struct block_index *blockidx;
831 		u8 blockno = p->page_number_to_block_index[i];
832 		if (blockno >= E4_NO_SWAPPAGE_HEADERS)
833 			continue;
834 
835 		do {
836 			u64 l;
837 
838 			if (blockno >= E4_NO_SWAPPAGE_HEADERS)
839 				return 1;
840 
841 			blockidx = &p->page_header[blockno++];
842 			if ((u8 *)(blockidx + 1) - dsp  >= len)
843 				return 1;
844 
845 			if (le16_to_cpu(blockidx->PageNumber) != i)
846 				return 1;
847 
848 			l = E4_PAGE_BYTES(blockidx->PageSize);
849 			sum += l;
850 			l += le32_to_cpu(blockidx->PageOffset);
851 			if (l > len)
852 				return 1;
853 
854 		/* zero is zero regardless endianes */
855 		} while (blockidx->NotLastBlock);
856 	}
857 
858 	return (sum == len) ? 0 : 1;
859 }
860 
861 /*
862  * send data to the idma pipe
863  * */
864 static int uea_idma_write(struct uea_softc *sc, const void *data, u32 size)
865 {
866 	int ret = -ENOMEM;
867 	u8 *xfer_buff;
868 	int bytes_read;
869 
870 	xfer_buff = kmemdup(data, size, GFP_KERNEL);
871 	if (!xfer_buff) {
872 		uea_err(INS_TO_USBDEV(sc), "can't allocate xfer_buff\n");
873 		return ret;
874 	}
875 
876 	ret = usb_bulk_msg(sc->usb_dev,
877 			 usb_sndbulkpipe(sc->usb_dev, UEA_IDMA_PIPE),
878 			 xfer_buff, size, &bytes_read, BULK_TIMEOUT);
879 
880 	kfree(xfer_buff);
881 	if (ret < 0)
882 		return ret;
883 	if (size != bytes_read) {
884 		uea_err(INS_TO_USBDEV(sc), "size != bytes_read %d %d\n", size,
885 		       bytes_read);
886 		return -EIO;
887 	}
888 
889 	return 0;
890 }
891 
892 static int request_dsp(struct uea_softc *sc)
893 {
894 	int ret;
895 	char *dsp_name;
896 
897 	if (UEA_CHIP_VERSION(sc) == EAGLE_IV) {
898 		if (IS_ISDN(sc))
899 			dsp_name = DSP4I_FIRMWARE;
900 		else
901 			dsp_name = DSP4P_FIRMWARE;
902 	} else if (UEA_CHIP_VERSION(sc) == ADI930) {
903 		if (IS_ISDN(sc))
904 			dsp_name = DSP9I_FIRMWARE;
905 		else
906 			dsp_name = DSP9P_FIRMWARE;
907 	} else {
908 		if (IS_ISDN(sc))
909 			dsp_name = DSPEI_FIRMWARE;
910 		else
911 			dsp_name = DSPEP_FIRMWARE;
912 	}
913 
914 	ret = request_firmware(&sc->dsp_firm, dsp_name, &sc->usb_dev->dev);
915 	if (ret < 0) {
916 		uea_err(INS_TO_USBDEV(sc),
917 		       "requesting firmware %s failed with error %d\n",
918 			dsp_name, ret);
919 		return ret;
920 	}
921 
922 	if (UEA_CHIP_VERSION(sc) == EAGLE_IV)
923 		ret = check_dsp_e4(sc->dsp_firm->data, sc->dsp_firm->size);
924 	else
925 		ret = check_dsp_e1(sc->dsp_firm->data, sc->dsp_firm->size);
926 
927 	if (ret) {
928 		uea_err(INS_TO_USBDEV(sc), "firmware %s is corrupted\n",
929 		       dsp_name);
930 		release_firmware(sc->dsp_firm);
931 		sc->dsp_firm = NULL;
932 		return -EILSEQ;
933 	}
934 
935 	return 0;
936 }
937 
938 /*
939  * The uea_load_page() function must be called within a process context
940  */
941 static void uea_load_page_e1(struct work_struct *work)
942 {
943 	struct uea_softc *sc = container_of(work, struct uea_softc, task);
944 	u16 pageno = sc->pageno;
945 	u16 ovl = sc->ovl;
946 	struct block_info_e1 bi;
947 
948 	const u8 *p;
949 	u8 pagecount, blockcount;
950 	u16 blockaddr, blocksize;
951 	u32 pageoffset;
952 	int i;
953 
954 	/* reload firmware when reboot start and it's loaded already */
955 	if (ovl == 0 && pageno == 0) {
956 		release_firmware(sc->dsp_firm);
957 		sc->dsp_firm = NULL;
958 	}
959 
960 	if (sc->dsp_firm == NULL && request_dsp(sc) < 0)
961 		return;
962 
963 	p = sc->dsp_firm->data;
964 	pagecount = FW_GET_BYTE(p);
965 	p += 1;
966 
967 	if (pageno >= pagecount)
968 		goto bad1;
969 
970 	p += 4 * pageno;
971 	pageoffset = get_unaligned_le32(p);
972 
973 	if (pageoffset == 0)
974 		goto bad1;
975 
976 	p = sc->dsp_firm->data + pageoffset;
977 	blockcount = FW_GET_BYTE(p);
978 	p += 1;
979 
980 	uea_dbg(INS_TO_USBDEV(sc),
981 	       "sending %u blocks for DSP page %u\n", blockcount, pageno);
982 
983 	bi.wHdr = cpu_to_le16(UEA_BIHDR);
984 	bi.wOvl = cpu_to_le16(ovl);
985 	bi.wOvlOffset = cpu_to_le16(ovl | 0x8000);
986 
987 	for (i = 0; i < blockcount; i++) {
988 		blockaddr = get_unaligned_le16(p);
989 		p += 2;
990 
991 		blocksize = get_unaligned_le16(p);
992 		p += 2;
993 
994 		bi.wSize = cpu_to_le16(blocksize);
995 		bi.wAddress = cpu_to_le16(blockaddr);
996 		bi.wLast = cpu_to_le16((i == blockcount - 1) ? 1 : 0);
997 
998 		/* send block info through the IDMA pipe */
999 		if (uea_idma_write(sc, &bi, E1_BLOCK_INFO_SIZE))
1000 			goto bad2;
1001 
1002 		/* send block data through the IDMA pipe */
1003 		if (uea_idma_write(sc, p, blocksize))
1004 			goto bad2;
1005 
1006 		p += blocksize;
1007 	}
1008 
1009 	return;
1010 
1011 bad2:
1012 	uea_err(INS_TO_USBDEV(sc), "sending DSP block %u failed\n", i);
1013 	return;
1014 bad1:
1015 	uea_err(INS_TO_USBDEV(sc), "invalid DSP page %u requested\n", pageno);
1016 }
1017 
1018 static void __uea_load_page_e4(struct uea_softc *sc, u8 pageno, int boot)
1019 {
1020 	struct block_info_e4 bi;
1021 	struct block_index *blockidx;
1022 	struct l1_code *p = (struct l1_code *) sc->dsp_firm->data;
1023 	u8 blockno = p->page_number_to_block_index[pageno];
1024 
1025 	bi.wHdr = cpu_to_be16(UEA_BIHDR);
1026 	bi.bBootPage = boot;
1027 	bi.bPageNumber = pageno;
1028 	bi.wReserved = cpu_to_be16(UEA_RESERVED);
1029 
1030 	do {
1031 		const u8 *blockoffset;
1032 		unsigned int blocksize;
1033 
1034 		blockidx = &p->page_header[blockno];
1035 		blocksize = E4_PAGE_BYTES(blockidx->PageSize);
1036 		blockoffset = sc->dsp_firm->data + le32_to_cpu(
1037 							blockidx->PageOffset);
1038 
1039 		bi.dwSize = cpu_to_be32(blocksize);
1040 		bi.dwAddress = cpu_to_be32(le32_to_cpu(blockidx->PageAddress));
1041 
1042 		uea_dbg(INS_TO_USBDEV(sc),
1043 			"sending block %u for DSP page "
1044 			"%u size %u address %x\n",
1045 			blockno, pageno, blocksize,
1046 			le32_to_cpu(blockidx->PageAddress));
1047 
1048 		/* send block info through the IDMA pipe */
1049 		if (uea_idma_write(sc, &bi, E4_BLOCK_INFO_SIZE))
1050 			goto bad;
1051 
1052 		/* send block data through the IDMA pipe */
1053 		if (uea_idma_write(sc, blockoffset, blocksize))
1054 			goto bad;
1055 
1056 		blockno++;
1057 	} while (blockidx->NotLastBlock);
1058 
1059 	return;
1060 
1061 bad:
1062 	uea_err(INS_TO_USBDEV(sc), "sending DSP block %u failed\n", blockno);
1063 	return;
1064 }
1065 
1066 static void uea_load_page_e4(struct work_struct *work)
1067 {
1068 	struct uea_softc *sc = container_of(work, struct uea_softc, task);
1069 	u8 pageno = sc->pageno;
1070 	int i;
1071 	struct block_info_e4 bi;
1072 	struct l1_code *p;
1073 
1074 	uea_dbg(INS_TO_USBDEV(sc), "sending DSP page %u\n", pageno);
1075 
1076 	/* reload firmware when reboot start and it's loaded already */
1077 	if (pageno == 0) {
1078 		release_firmware(sc->dsp_firm);
1079 		sc->dsp_firm = NULL;
1080 	}
1081 
1082 	if (sc->dsp_firm == NULL && request_dsp(sc) < 0)
1083 		return;
1084 
1085 	p = (struct l1_code *) sc->dsp_firm->data;
1086 	if (pageno >= le16_to_cpu(p->page_header[0].PageNumber)) {
1087 		uea_err(INS_TO_USBDEV(sc), "invalid DSP "
1088 						"page %u requested\n", pageno);
1089 		return;
1090 	}
1091 
1092 	if (pageno != 0) {
1093 		__uea_load_page_e4(sc, pageno, 0);
1094 		return;
1095 	}
1096 
1097 	uea_dbg(INS_TO_USBDEV(sc),
1098 	       "sending Main DSP page %u\n", p->page_header[0].PageNumber);
1099 
1100 	for (i = 0; i < le16_to_cpu(p->page_header[0].PageNumber); i++) {
1101 		if (E4_IS_BOOT_PAGE(p->page_header[i].PageSize))
1102 			__uea_load_page_e4(sc, i, 1);
1103 	}
1104 
1105 	uea_dbg(INS_TO_USBDEV(sc) , "sending start bi\n");
1106 
1107 	bi.wHdr = cpu_to_be16(UEA_BIHDR);
1108 	bi.bBootPage = 0;
1109 	bi.bPageNumber = 0xff;
1110 	bi.wReserved = cpu_to_be16(UEA_RESERVED);
1111 	bi.dwSize = cpu_to_be32(E4_PAGE_BYTES(p->page_header[0].PageSize));
1112 	bi.dwAddress = cpu_to_be32(le32_to_cpu(p->page_header[0].PageAddress));
1113 
1114 	/* send block info through the IDMA pipe */
1115 	if (uea_idma_write(sc, &bi, E4_BLOCK_INFO_SIZE))
1116 		uea_err(INS_TO_USBDEV(sc), "sending DSP start bi failed\n");
1117 }
1118 
1119 static inline void wake_up_cmv_ack(struct uea_softc *sc)
1120 {
1121 	BUG_ON(sc->cmv_ack);
1122 	sc->cmv_ack = 1;
1123 	wake_up(&sc->sync_q);
1124 }
1125 
1126 static inline int wait_cmv_ack(struct uea_softc *sc)
1127 {
1128 	int ret = uea_wait(sc, sc->cmv_ack , ACK_TIMEOUT);
1129 
1130 	sc->cmv_ack = 0;
1131 
1132 	uea_dbg(INS_TO_USBDEV(sc), "wait_event_timeout : %d ms\n",
1133 			jiffies_to_msecs(ret));
1134 
1135 	if (ret < 0)
1136 		return ret;
1137 
1138 	return (ret == 0) ? -ETIMEDOUT : 0;
1139 }
1140 
1141 #define UCDC_SEND_ENCAPSULATED_COMMAND 0x00
1142 
1143 static int uea_request(struct uea_softc *sc,
1144 		u16 value, u16 index, u16 size, const void *data)
1145 {
1146 	u8 *xfer_buff;
1147 	int ret = -ENOMEM;
1148 
1149 	xfer_buff = kmemdup(data, size, GFP_KERNEL);
1150 	if (!xfer_buff) {
1151 		uea_err(INS_TO_USBDEV(sc), "can't allocate xfer_buff\n");
1152 		return ret;
1153 	}
1154 
1155 	ret = usb_control_msg(sc->usb_dev, usb_sndctrlpipe(sc->usb_dev, 0),
1156 			      UCDC_SEND_ENCAPSULATED_COMMAND,
1157 			      USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1158 			      value, index, xfer_buff, size, CTRL_TIMEOUT);
1159 
1160 	kfree(xfer_buff);
1161 	if (ret < 0) {
1162 		uea_err(INS_TO_USBDEV(sc), "usb_control_msg error %d\n", ret);
1163 		return ret;
1164 	}
1165 
1166 	if (ret != size) {
1167 		uea_err(INS_TO_USBDEV(sc),
1168 		       "usb_control_msg send only %d bytes (instead of %d)\n",
1169 		       ret, size);
1170 		return -EIO;
1171 	}
1172 
1173 	return 0;
1174 }
1175 
1176 static int uea_cmv_e1(struct uea_softc *sc,
1177 		u8 function, u32 address, u16 offset, u32 data)
1178 {
1179 	struct cmv_e1 cmv;
1180 	int ret;
1181 
1182 	uea_enters(INS_TO_USBDEV(sc));
1183 	uea_vdbg(INS_TO_USBDEV(sc), "Function : %d-%d, Address : %c%c%c%c, "
1184 			"offset : 0x%04x, data : 0x%08x\n",
1185 			E1_FUNCTION_TYPE(function),
1186 			E1_FUNCTION_SUBTYPE(function),
1187 			E1_GETSA1(address), E1_GETSA2(address),
1188 			E1_GETSA3(address),
1189 			E1_GETSA4(address), offset, data);
1190 
1191 	/* we send a request, but we expect a reply */
1192 	sc->cmv_dsc.e1.function = function | 0x2;
1193 	sc->cmv_dsc.e1.idx++;
1194 	sc->cmv_dsc.e1.address = address;
1195 	sc->cmv_dsc.e1.offset = offset;
1196 
1197 	cmv.wPreamble = cpu_to_le16(E1_PREAMBLE);
1198 	cmv.bDirection = E1_HOSTTOMODEM;
1199 	cmv.bFunction = function;
1200 	cmv.wIndex = cpu_to_le16(sc->cmv_dsc.e1.idx);
1201 	put_unaligned_le32(address, &cmv.dwSymbolicAddress);
1202 	cmv.wOffsetAddress = cpu_to_le16(offset);
1203 	put_unaligned_le32(data >> 16 | data << 16, &cmv.dwData);
1204 
1205 	ret = uea_request(sc, UEA_E1_SET_BLOCK, UEA_MPTX_START,
1206 							sizeof(cmv), &cmv);
1207 	if (ret < 0)
1208 		return ret;
1209 	ret = wait_cmv_ack(sc);
1210 	uea_leaves(INS_TO_USBDEV(sc));
1211 	return ret;
1212 }
1213 
1214 static int uea_cmv_e4(struct uea_softc *sc,
1215 		u16 function, u16 group, u16 address, u16 offset, u32 data)
1216 {
1217 	struct cmv_e4 cmv;
1218 	int ret;
1219 
1220 	uea_enters(INS_TO_USBDEV(sc));
1221 	memset(&cmv, 0, sizeof(cmv));
1222 
1223 	uea_vdbg(INS_TO_USBDEV(sc), "Function : %d-%d, Group : 0x%04x, "
1224 		 "Address : 0x%04x, offset : 0x%04x, data : 0x%08x\n",
1225 		 E4_FUNCTION_TYPE(function), E4_FUNCTION_SUBTYPE(function),
1226 		 group, address, offset, data);
1227 
1228 	/* we send a request, but we expect a reply */
1229 	sc->cmv_dsc.e4.function = function | (0x1 << 4);
1230 	sc->cmv_dsc.e4.offset = offset;
1231 	sc->cmv_dsc.e4.address = address;
1232 	sc->cmv_dsc.e4.group = group;
1233 
1234 	cmv.wFunction = cpu_to_be16(function);
1235 	cmv.wGroup = cpu_to_be16(group);
1236 	cmv.wAddress = cpu_to_be16(address);
1237 	cmv.wOffset = cpu_to_be16(offset);
1238 	cmv.dwData[0] = cpu_to_be32(data);
1239 
1240 	ret = uea_request(sc, UEA_E4_SET_BLOCK, UEA_MPTX_START,
1241 							sizeof(cmv), &cmv);
1242 	if (ret < 0)
1243 		return ret;
1244 	ret = wait_cmv_ack(sc);
1245 	uea_leaves(INS_TO_USBDEV(sc));
1246 	return ret;
1247 }
1248 
1249 static inline int uea_read_cmv_e1(struct uea_softc *sc,
1250 		u32 address, u16 offset, u32 *data)
1251 {
1252 	int ret = uea_cmv_e1(sc, E1_MAKEFUNCTION(E1_MEMACCESS, E1_REQUESTREAD),
1253 			  address, offset, 0);
1254 	if (ret < 0)
1255 		uea_err(INS_TO_USBDEV(sc),
1256 			"reading cmv failed with error %d\n", ret);
1257 	else
1258 		*data = sc->data;
1259 
1260 	return ret;
1261 }
1262 
1263 static inline int uea_read_cmv_e4(struct uea_softc *sc,
1264 		u8 size, u16 group, u16 address, u16 offset, u32 *data)
1265 {
1266 	int ret = uea_cmv_e4(sc, E4_MAKEFUNCTION(E4_MEMACCESS,
1267 							E4_REQUESTREAD, size),
1268 			  group, address, offset, 0);
1269 	if (ret < 0)
1270 		uea_err(INS_TO_USBDEV(sc),
1271 			"reading cmv failed with error %d\n", ret);
1272 	else {
1273 		*data = sc->data;
1274 		/* size is in 16-bit word quantities */
1275 		if (size > 2)
1276 			*(data + 1) = sc->data1;
1277 	}
1278 	return ret;
1279 }
1280 
1281 static inline int uea_write_cmv_e1(struct uea_softc *sc,
1282 		u32 address, u16 offset, u32 data)
1283 {
1284 	int ret = uea_cmv_e1(sc, E1_MAKEFUNCTION(E1_MEMACCESS, E1_REQUESTWRITE),
1285 			  address, offset, data);
1286 	if (ret < 0)
1287 		uea_err(INS_TO_USBDEV(sc),
1288 			"writing cmv failed with error %d\n", ret);
1289 
1290 	return ret;
1291 }
1292 
1293 static inline int uea_write_cmv_e4(struct uea_softc *sc,
1294 		u8 size, u16 group, u16 address, u16 offset, u32 data)
1295 {
1296 	int ret = uea_cmv_e4(sc, E4_MAKEFUNCTION(E4_MEMACCESS,
1297 							E4_REQUESTWRITE, size),
1298 			  group, address, offset, data);
1299 	if (ret < 0)
1300 		uea_err(INS_TO_USBDEV(sc),
1301 			"writing cmv failed with error %d\n", ret);
1302 
1303 	return ret;
1304 }
1305 
1306 static void uea_set_bulk_timeout(struct uea_softc *sc, u32 dsrate)
1307 {
1308 	int ret;
1309 	u16 timeout;
1310 
1311 	/* in bulk mode the modem have problem with high rate
1312 	 * changing internal timing could improve things, but the
1313 	 * value is mysterious.
1314 	 * ADI930 don't support it (-EPIPE error).
1315 	 */
1316 
1317 	if (UEA_CHIP_VERSION(sc) == ADI930 ||
1318 	    altsetting[sc->modem_index] > 0 ||
1319 	    sc->stats.phy.dsrate == dsrate)
1320 		return;
1321 
1322 	/* Original timming (1Mbit/s) from ADI (used in windows driver) */
1323 	timeout = (dsrate <= 1024*1024) ? 0 : 1;
1324 	ret = uea_request(sc, UEA_SET_TIMEOUT, timeout, 0, NULL);
1325 	uea_info(INS_TO_USBDEV(sc), "setting new timeout %d%s\n",
1326 		 timeout,  ret < 0 ? " failed" : "");
1327 
1328 }
1329 
1330 /*
1331  * Monitor the modem and update the stat
1332  * return 0 if everything is ok
1333  * return < 0 if an error occurs (-EAGAIN reboot needed)
1334  */
1335 static int uea_stat_e1(struct uea_softc *sc)
1336 {
1337 	u32 data;
1338 	int ret;
1339 
1340 	uea_enters(INS_TO_USBDEV(sc));
1341 	data = sc->stats.phy.state;
1342 
1343 	ret = uea_read_cmv_e1(sc, E1_SA_STAT, 0, &sc->stats.phy.state);
1344 	if (ret < 0)
1345 		return ret;
1346 
1347 	switch (GET_STATUS(sc->stats.phy.state)) {
1348 	case 0:		/* not yet synchronized */
1349 		uea_dbg(INS_TO_USBDEV(sc),
1350 		       "modem not yet synchronized\n");
1351 		return 0;
1352 
1353 	case 1:		/* initialization */
1354 		uea_dbg(INS_TO_USBDEV(sc), "modem initializing\n");
1355 		return 0;
1356 
1357 	case 2:		/* operational */
1358 		uea_vdbg(INS_TO_USBDEV(sc), "modem operational\n");
1359 		break;
1360 
1361 	case 3:		/* fail ... */
1362 		uea_info(INS_TO_USBDEV(sc), "modem synchronization failed"
1363 					" (may be try other cmv/dsp)\n");
1364 		return -EAGAIN;
1365 
1366 	case 4 ... 6:	/* test state */
1367 		uea_warn(INS_TO_USBDEV(sc),
1368 				"modem in test mode - not supported\n");
1369 		return -EAGAIN;
1370 
1371 	case 7:		/* fast-retain ... */
1372 		uea_info(INS_TO_USBDEV(sc), "modem in fast-retain mode\n");
1373 		return 0;
1374 	default:
1375 		uea_err(INS_TO_USBDEV(sc), "modem invalid SW mode %d\n",
1376 			GET_STATUS(sc->stats.phy.state));
1377 		return -EAGAIN;
1378 	}
1379 
1380 	if (GET_STATUS(data) != 2) {
1381 		uea_request(sc, UEA_SET_MODE, UEA_LOOPBACK_OFF, 0, NULL);
1382 		uea_info(INS_TO_USBDEV(sc), "modem operational\n");
1383 
1384 		/* release the dsp firmware as it is not needed until
1385 		 * the next failure
1386 		 */
1387 		release_firmware(sc->dsp_firm);
1388 		sc->dsp_firm = NULL;
1389 	}
1390 
1391 	/* always update it as atm layer could not be init when we switch to
1392 	 * operational state
1393 	 */
1394 	UPDATE_ATM_SIGNAL(ATM_PHY_SIG_FOUND);
1395 
1396 	/* wake up processes waiting for synchronization */
1397 	wake_up(&sc->sync_q);
1398 
1399 	ret = uea_read_cmv_e1(sc, E1_SA_DIAG, 2, &sc->stats.phy.flags);
1400 	if (ret < 0)
1401 		return ret;
1402 	sc->stats.phy.mflags |= sc->stats.phy.flags;
1403 
1404 	/* in case of a flags ( for example delineation LOSS (& 0x10)),
1405 	 * we check the status again in order to detect the failure earlier
1406 	 */
1407 	if (sc->stats.phy.flags) {
1408 		uea_dbg(INS_TO_USBDEV(sc), "Stat flag = 0x%x\n",
1409 		       sc->stats.phy.flags);
1410 		return 0;
1411 	}
1412 
1413 	ret = uea_read_cmv_e1(sc, E1_SA_RATE, 0, &data);
1414 	if (ret < 0)
1415 		return ret;
1416 
1417 	uea_set_bulk_timeout(sc, (data >> 16) * 32);
1418 	sc->stats.phy.dsrate = (data >> 16) * 32;
1419 	sc->stats.phy.usrate = (data & 0xffff) * 32;
1420 	UPDATE_ATM_STAT(link_rate, sc->stats.phy.dsrate * 1000 / 424);
1421 
1422 	ret = uea_read_cmv_e1(sc, E1_SA_DIAG, 23, &data);
1423 	if (ret < 0)
1424 		return ret;
1425 	sc->stats.phy.dsattenuation = (data & 0xff) / 2;
1426 
1427 	ret = uea_read_cmv_e1(sc, E1_SA_DIAG, 47, &data);
1428 	if (ret < 0)
1429 		return ret;
1430 	sc->stats.phy.usattenuation = (data & 0xff) / 2;
1431 
1432 	ret = uea_read_cmv_e1(sc, E1_SA_DIAG, 25, &sc->stats.phy.dsmargin);
1433 	if (ret < 0)
1434 		return ret;
1435 
1436 	ret = uea_read_cmv_e1(sc, E1_SA_DIAG, 49, &sc->stats.phy.usmargin);
1437 	if (ret < 0)
1438 		return ret;
1439 
1440 	ret = uea_read_cmv_e1(sc, E1_SA_DIAG, 51, &sc->stats.phy.rxflow);
1441 	if (ret < 0)
1442 		return ret;
1443 
1444 	ret = uea_read_cmv_e1(sc, E1_SA_DIAG, 52, &sc->stats.phy.txflow);
1445 	if (ret < 0)
1446 		return ret;
1447 
1448 	ret = uea_read_cmv_e1(sc, E1_SA_DIAG, 54, &sc->stats.phy.dsunc);
1449 	if (ret < 0)
1450 		return ret;
1451 
1452 	/* only for atu-c */
1453 	ret = uea_read_cmv_e1(sc, E1_SA_DIAG, 58, &sc->stats.phy.usunc);
1454 	if (ret < 0)
1455 		return ret;
1456 
1457 	ret = uea_read_cmv_e1(sc, E1_SA_DIAG, 53, &sc->stats.phy.dscorr);
1458 	if (ret < 0)
1459 		return ret;
1460 
1461 	/* only for atu-c */
1462 	ret = uea_read_cmv_e1(sc, E1_SA_DIAG, 57, &sc->stats.phy.uscorr);
1463 	if (ret < 0)
1464 		return ret;
1465 
1466 	ret = uea_read_cmv_e1(sc, E1_SA_INFO, 8, &sc->stats.phy.vidco);
1467 	if (ret < 0)
1468 		return ret;
1469 
1470 	ret = uea_read_cmv_e1(sc, E1_SA_INFO, 13, &sc->stats.phy.vidcpe);
1471 	if (ret < 0)
1472 		return ret;
1473 
1474 	return 0;
1475 }
1476 
1477 static int uea_stat_e4(struct uea_softc *sc)
1478 {
1479 	u32 data;
1480 	u32 tmp_arr[2];
1481 	int ret;
1482 
1483 	uea_enters(INS_TO_USBDEV(sc));
1484 	data = sc->stats.phy.state;
1485 
1486 	/* XXX only need to be done before operationnal... */
1487 	ret = uea_read_cmv_e4(sc, 1, E4_SA_STAT, 0, 0, &sc->stats.phy.state);
1488 	if (ret < 0)
1489 		return ret;
1490 
1491 	switch (sc->stats.phy.state) {
1492 	case 0x0:	/* not yet synchronized */
1493 	case 0x1:
1494 	case 0x3:
1495 	case 0x4:
1496 		uea_dbg(INS_TO_USBDEV(sc), "modem not yet "
1497 						"synchronized\n");
1498 		return 0;
1499 	case 0x5:	/* initialization */
1500 	case 0x6:
1501 	case 0x9:
1502 	case 0xa:
1503 		uea_dbg(INS_TO_USBDEV(sc), "modem initializing\n");
1504 		return 0;
1505 	case 0x2:	/* fail ... */
1506 		uea_info(INS_TO_USBDEV(sc), "modem synchronization "
1507 				"failed (may be try other cmv/dsp)\n");
1508 		return -EAGAIN;
1509 	case 0x7:	/* operational */
1510 		break;
1511 	default:
1512 		uea_warn(INS_TO_USBDEV(sc), "unknown state: %x\n",
1513 						sc->stats.phy.state);
1514 		return 0;
1515 	}
1516 
1517 	if (data != 7) {
1518 		uea_request(sc, UEA_SET_MODE, UEA_LOOPBACK_OFF, 0, NULL);
1519 		uea_info(INS_TO_USBDEV(sc), "modem operational\n");
1520 
1521 		/* release the dsp firmware as it is not needed until
1522 		 * the next failure
1523 		 */
1524 		release_firmware(sc->dsp_firm);
1525 		sc->dsp_firm = NULL;
1526 	}
1527 
1528 	/* always update it as atm layer could not be init when we switch to
1529 	 * operational state
1530 	 */
1531 	UPDATE_ATM_SIGNAL(ATM_PHY_SIG_FOUND);
1532 
1533 	/* wake up processes waiting for synchronization */
1534 	wake_up(&sc->sync_q);
1535 
1536 	/* TODO improve this state machine :
1537 	 * we need some CMV info : what they do and their unit
1538 	 * we should find the equivalent of eagle3- CMV
1539 	 */
1540 	/* check flags */
1541 	ret = uea_read_cmv_e4(sc, 1, E4_SA_DIAG, 0, 0, &sc->stats.phy.flags);
1542 	if (ret < 0)
1543 		return ret;
1544 	sc->stats.phy.mflags |= sc->stats.phy.flags;
1545 
1546 	/* in case of a flags ( for example delineation LOSS (& 0x10)),
1547 	 * we check the status again in order to detect the failure earlier
1548 	 */
1549 	if (sc->stats.phy.flags) {
1550 		uea_dbg(INS_TO_USBDEV(sc), "Stat flag = 0x%x\n",
1551 		       sc->stats.phy.flags);
1552 		if (sc->stats.phy.flags & 1) /* delineation LOSS */
1553 			return -EAGAIN;
1554 		if (sc->stats.phy.flags & 0x4000) /* Reset Flag */
1555 			return -EAGAIN;
1556 		return 0;
1557 	}
1558 
1559 	/* rate data may be in upper or lower half of 64 bit word, strange */
1560 	ret = uea_read_cmv_e4(sc, 4, E4_SA_RATE, 0, 0, tmp_arr);
1561 	if (ret < 0)
1562 		return ret;
1563 	data = (tmp_arr[0]) ? tmp_arr[0] : tmp_arr[1];
1564 	sc->stats.phy.usrate = data / 1000;
1565 
1566 	ret = uea_read_cmv_e4(sc, 4, E4_SA_RATE, 1, 0, tmp_arr);
1567 	if (ret < 0)
1568 		return ret;
1569 	data = (tmp_arr[0]) ? tmp_arr[0] : tmp_arr[1];
1570 	uea_set_bulk_timeout(sc, data / 1000);
1571 	sc->stats.phy.dsrate = data / 1000;
1572 	UPDATE_ATM_STAT(link_rate, sc->stats.phy.dsrate * 1000 / 424);
1573 
1574 	ret = uea_read_cmv_e4(sc, 1, E4_SA_INFO, 68, 1, &data);
1575 	if (ret < 0)
1576 		return ret;
1577 	sc->stats.phy.dsattenuation = data / 10;
1578 
1579 	ret = uea_read_cmv_e4(sc, 1, E4_SA_INFO, 69, 1, &data);
1580 	if (ret < 0)
1581 		return ret;
1582 	sc->stats.phy.usattenuation = data / 10;
1583 
1584 	ret = uea_read_cmv_e4(sc, 1, E4_SA_INFO, 68, 3, &data);
1585 	if (ret < 0)
1586 		return ret;
1587 	sc->stats.phy.dsmargin = data / 2;
1588 
1589 	ret = uea_read_cmv_e4(sc, 1, E4_SA_INFO, 69, 3, &data);
1590 	if (ret < 0)
1591 		return ret;
1592 	sc->stats.phy.usmargin = data / 10;
1593 
1594 	return 0;
1595 }
1596 
1597 static void cmvs_file_name(struct uea_softc *sc, char *const cmv_name, int ver)
1598 {
1599 	char file_arr[] = "CMVxy.bin";
1600 	char *file;
1601 
1602 	kernel_param_lock(THIS_MODULE);
1603 	/* set proper name corresponding modem version and line type */
1604 	if (cmv_file[sc->modem_index] == NULL) {
1605 		if (UEA_CHIP_VERSION(sc) == ADI930)
1606 			file_arr[3] = '9';
1607 		else if (UEA_CHIP_VERSION(sc) == EAGLE_IV)
1608 			file_arr[3] = '4';
1609 		else
1610 			file_arr[3] = 'e';
1611 
1612 		file_arr[4] = IS_ISDN(sc) ? 'i' : 'p';
1613 		file = file_arr;
1614 	} else
1615 		file = cmv_file[sc->modem_index];
1616 
1617 	strcpy(cmv_name, FW_DIR);
1618 	strlcat(cmv_name, file, UEA_FW_NAME_MAX);
1619 	if (ver == 2)
1620 		strlcat(cmv_name, ".v2", UEA_FW_NAME_MAX);
1621 	kernel_param_unlock(THIS_MODULE);
1622 }
1623 
1624 static int request_cmvs_old(struct uea_softc *sc,
1625 		 void **cmvs, const struct firmware **fw)
1626 {
1627 	int ret, size;
1628 	u8 *data;
1629 	char cmv_name[UEA_FW_NAME_MAX]; /* 30 bytes stack variable */
1630 
1631 	cmvs_file_name(sc, cmv_name, 1);
1632 	ret = request_firmware(fw, cmv_name, &sc->usb_dev->dev);
1633 	if (ret < 0) {
1634 		uea_err(INS_TO_USBDEV(sc),
1635 		       "requesting firmware %s failed with error %d\n",
1636 		       cmv_name, ret);
1637 		return ret;
1638 	}
1639 
1640 	data = (u8 *) (*fw)->data;
1641 	size = (*fw)->size;
1642 	if (size < 1)
1643 		goto err_fw_corrupted;
1644 
1645 	if (size != *data * sizeof(struct uea_cmvs_v1) + 1)
1646 		goto err_fw_corrupted;
1647 
1648 	*cmvs = (void *)(data + 1);
1649 	return *data;
1650 
1651 err_fw_corrupted:
1652 	uea_err(INS_TO_USBDEV(sc), "firmware %s is corrupted\n", cmv_name);
1653 	release_firmware(*fw);
1654 	return -EILSEQ;
1655 }
1656 
1657 static int request_cmvs(struct uea_softc *sc,
1658 		 void **cmvs, const struct firmware **fw, int *ver)
1659 {
1660 	int ret, size;
1661 	u32 crc;
1662 	u8 *data;
1663 	char cmv_name[UEA_FW_NAME_MAX]; /* 30 bytes stack variable */
1664 
1665 	cmvs_file_name(sc, cmv_name, 2);
1666 	ret = request_firmware(fw, cmv_name, &sc->usb_dev->dev);
1667 	if (ret < 0) {
1668 		/* if caller can handle old version, try to provide it */
1669 		if (*ver == 1) {
1670 			uea_warn(INS_TO_USBDEV(sc), "requesting "
1671 							"firmware %s failed, "
1672 				"try to get older cmvs\n", cmv_name);
1673 			return request_cmvs_old(sc, cmvs, fw);
1674 		}
1675 		uea_err(INS_TO_USBDEV(sc),
1676 		       "requesting firmware %s failed with error %d\n",
1677 		       cmv_name, ret);
1678 		return ret;
1679 	}
1680 
1681 	size = (*fw)->size;
1682 	data = (u8 *) (*fw)->data;
1683 	if (size < 4 || strncmp(data, "cmv2", 4) != 0) {
1684 		if (*ver == 1) {
1685 			uea_warn(INS_TO_USBDEV(sc), "firmware %s is corrupted,"
1686 				" try to get older cmvs\n", cmv_name);
1687 			release_firmware(*fw);
1688 			return request_cmvs_old(sc, cmvs, fw);
1689 		}
1690 		goto err_fw_corrupted;
1691 	}
1692 
1693 	*ver = 2;
1694 
1695 	data += 4;
1696 	size -= 4;
1697 	if (size < 5)
1698 		goto err_fw_corrupted;
1699 
1700 	crc = get_unaligned_le32(data);
1701 	data += 4;
1702 	size -= 4;
1703 	if (crc32_be(0, data, size) != crc)
1704 		goto err_fw_corrupted;
1705 
1706 	if (size != *data * sizeof(struct uea_cmvs_v2) + 1)
1707 		goto err_fw_corrupted;
1708 
1709 	*cmvs = (void *) (data + 1);
1710 	return *data;
1711 
1712 err_fw_corrupted:
1713 	uea_err(INS_TO_USBDEV(sc), "firmware %s is corrupted\n", cmv_name);
1714 	release_firmware(*fw);
1715 	return -EILSEQ;
1716 }
1717 
1718 static int uea_send_cmvs_e1(struct uea_softc *sc)
1719 {
1720 	int i, ret, len;
1721 	void *cmvs_ptr;
1722 	const struct firmware *cmvs_fw;
1723 	int ver = 1; /* we can handle v1 cmv firmware version; */
1724 
1725 	/* Enter in R-IDLE (cmv) until instructed otherwise */
1726 	ret = uea_write_cmv_e1(sc, E1_SA_CNTL, 0, 1);
1727 	if (ret < 0)
1728 		return ret;
1729 
1730 	/* Dump firmware version */
1731 	ret = uea_read_cmv_e1(sc, E1_SA_INFO, 10, &sc->stats.phy.firmid);
1732 	if (ret < 0)
1733 		return ret;
1734 	uea_info(INS_TO_USBDEV(sc), "ATU-R firmware version : %x\n",
1735 			sc->stats.phy.firmid);
1736 
1737 	/* get options */
1738 	ret = len = request_cmvs(sc, &cmvs_ptr, &cmvs_fw, &ver);
1739 	if (ret < 0)
1740 		return ret;
1741 
1742 	/* send options */
1743 	if (ver == 1) {
1744 		struct uea_cmvs_v1 *cmvs_v1 = cmvs_ptr;
1745 
1746 		uea_warn(INS_TO_USBDEV(sc), "use deprecated cmvs version, "
1747 			"please update your firmware\n");
1748 
1749 		for (i = 0; i < len; i++) {
1750 			ret = uea_write_cmv_e1(sc,
1751 				get_unaligned_le32(&cmvs_v1[i].address),
1752 				get_unaligned_le16(&cmvs_v1[i].offset),
1753 				get_unaligned_le32(&cmvs_v1[i].data));
1754 			if (ret < 0)
1755 				goto out;
1756 		}
1757 	} else if (ver == 2) {
1758 		struct uea_cmvs_v2 *cmvs_v2 = cmvs_ptr;
1759 
1760 		for (i = 0; i < len; i++) {
1761 			ret = uea_write_cmv_e1(sc,
1762 				get_unaligned_le32(&cmvs_v2[i].address),
1763 				(u16) get_unaligned_le32(&cmvs_v2[i].offset),
1764 				get_unaligned_le32(&cmvs_v2[i].data));
1765 			if (ret < 0)
1766 				goto out;
1767 		}
1768 	} else {
1769 		/* This really should not happen */
1770 		uea_err(INS_TO_USBDEV(sc), "bad cmvs version %d\n", ver);
1771 		goto out;
1772 	}
1773 
1774 	/* Enter in R-ACT-REQ */
1775 	ret = uea_write_cmv_e1(sc, E1_SA_CNTL, 0, 2);
1776 	uea_vdbg(INS_TO_USBDEV(sc), "Entering in R-ACT-REQ state\n");
1777 	uea_info(INS_TO_USBDEV(sc), "modem started, waiting "
1778 						"synchronization...\n");
1779 out:
1780 	release_firmware(cmvs_fw);
1781 	return ret;
1782 }
1783 
1784 static int uea_send_cmvs_e4(struct uea_softc *sc)
1785 {
1786 	int i, ret, len;
1787 	void *cmvs_ptr;
1788 	const struct firmware *cmvs_fw;
1789 	int ver = 2; /* we can only handle v2 cmv firmware version; */
1790 
1791 	/* Enter in R-IDLE (cmv) until instructed otherwise */
1792 	ret = uea_write_cmv_e4(sc, 1, E4_SA_CNTL, 0, 0, 1);
1793 	if (ret < 0)
1794 		return ret;
1795 
1796 	/* Dump firmware version */
1797 	/* XXX don't read the 3th byte as it is always 6 */
1798 	ret = uea_read_cmv_e4(sc, 2, E4_SA_INFO, 55, 0, &sc->stats.phy.firmid);
1799 	if (ret < 0)
1800 		return ret;
1801 	uea_info(INS_TO_USBDEV(sc), "ATU-R firmware version : %x\n",
1802 			sc->stats.phy.firmid);
1803 
1804 
1805 	/* get options */
1806 	ret = len = request_cmvs(sc, &cmvs_ptr, &cmvs_fw, &ver);
1807 	if (ret < 0)
1808 		return ret;
1809 
1810 	/* send options */
1811 	if (ver == 2) {
1812 		struct uea_cmvs_v2 *cmvs_v2 = cmvs_ptr;
1813 
1814 		for (i = 0; i < len; i++) {
1815 			ret = uea_write_cmv_e4(sc, 1,
1816 				get_unaligned_le32(&cmvs_v2[i].group),
1817 				get_unaligned_le32(&cmvs_v2[i].address),
1818 				get_unaligned_le32(&cmvs_v2[i].offset),
1819 				get_unaligned_le32(&cmvs_v2[i].data));
1820 			if (ret < 0)
1821 				goto out;
1822 		}
1823 	} else {
1824 		/* This really should not happen */
1825 		uea_err(INS_TO_USBDEV(sc), "bad cmvs version %d\n", ver);
1826 		goto out;
1827 	}
1828 
1829 	/* Enter in R-ACT-REQ */
1830 	ret = uea_write_cmv_e4(sc, 1, E4_SA_CNTL, 0, 0, 2);
1831 	uea_vdbg(INS_TO_USBDEV(sc), "Entering in R-ACT-REQ state\n");
1832 	uea_info(INS_TO_USBDEV(sc), "modem started, waiting "
1833 						"synchronization...\n");
1834 out:
1835 	release_firmware(cmvs_fw);
1836 	return ret;
1837 }
1838 
1839 /* Start boot post firmware modem:
1840  * - send reset commands through usb control pipe
1841  * - start workqueue for DSP loading
1842  * - send CMV options to modem
1843  */
1844 
1845 static int uea_start_reset(struct uea_softc *sc)
1846 {
1847 	u16 zero = 0;	/* ;-) */
1848 	int ret;
1849 
1850 	uea_enters(INS_TO_USBDEV(sc));
1851 	uea_info(INS_TO_USBDEV(sc), "(re)booting started\n");
1852 
1853 	/* mask interrupt */
1854 	sc->booting = 1;
1855 	/* We need to set this here because, a ack timeout could have occurred,
1856 	 * but before we start the reboot, the ack occurs and set this to 1.
1857 	 * So we will failed to wait Ready CMV.
1858 	 */
1859 	sc->cmv_ack = 0;
1860 	UPDATE_ATM_SIGNAL(ATM_PHY_SIG_LOST);
1861 
1862 	/* reset statistics */
1863 	memset(&sc->stats, 0, sizeof(struct uea_stats));
1864 
1865 	/* tell the modem that we want to boot in IDMA mode */
1866 	uea_request(sc, UEA_SET_MODE, UEA_LOOPBACK_ON, 0, NULL);
1867 	uea_request(sc, UEA_SET_MODE, UEA_BOOT_IDMA, 0, NULL);
1868 
1869 	/* enter reset mode */
1870 	uea_request(sc, UEA_SET_MODE, UEA_START_RESET, 0, NULL);
1871 
1872 	/* original driver use 200ms, but windows driver use 100ms */
1873 	ret = uea_wait(sc, 0, msecs_to_jiffies(100));
1874 	if (ret < 0)
1875 		return ret;
1876 
1877 	/* leave reset mode */
1878 	uea_request(sc, UEA_SET_MODE, UEA_END_RESET, 0, NULL);
1879 
1880 	if (UEA_CHIP_VERSION(sc) != EAGLE_IV) {
1881 		/* clear tx and rx mailboxes */
1882 		uea_request(sc, UEA_SET_2183_DATA, UEA_MPTX_MAILBOX, 2, &zero);
1883 		uea_request(sc, UEA_SET_2183_DATA, UEA_MPRX_MAILBOX, 2, &zero);
1884 		uea_request(sc, UEA_SET_2183_DATA, UEA_SWAP_MAILBOX, 2, &zero);
1885 	}
1886 
1887 	ret = uea_wait(sc, 0, msecs_to_jiffies(1000));
1888 	if (ret < 0)
1889 		return ret;
1890 
1891 	if (UEA_CHIP_VERSION(sc) == EAGLE_IV)
1892 		sc->cmv_dsc.e4.function = E4_MAKEFUNCTION(E4_ADSLDIRECTIVE,
1893 							E4_MODEMREADY, 1);
1894 	else
1895 		sc->cmv_dsc.e1.function = E1_MAKEFUNCTION(E1_ADSLDIRECTIVE,
1896 							E1_MODEMREADY);
1897 
1898 	/* demask interrupt */
1899 	sc->booting = 0;
1900 
1901 	/* start loading DSP */
1902 	sc->pageno = 0;
1903 	sc->ovl = 0;
1904 	schedule_work(&sc->task);
1905 
1906 	/* wait for modem ready CMV */
1907 	ret = wait_cmv_ack(sc);
1908 	if (ret < 0)
1909 		return ret;
1910 
1911 	uea_vdbg(INS_TO_USBDEV(sc), "Ready CMV received\n");
1912 
1913 	ret = sc->send_cmvs(sc);
1914 	if (ret < 0)
1915 		return ret;
1916 
1917 	sc->reset = 0;
1918 	uea_leaves(INS_TO_USBDEV(sc));
1919 	return ret;
1920 }
1921 
1922 /*
1923  * In case of an error wait 1s before rebooting the modem
1924  * if the modem don't request reboot (-EAGAIN).
1925  * Monitor the modem every 1s.
1926  */
1927 
1928 static int uea_kthread(void *data)
1929 {
1930 	struct uea_softc *sc = data;
1931 	int ret = -EAGAIN;
1932 
1933 	set_freezable();
1934 	uea_enters(INS_TO_USBDEV(sc));
1935 	while (!kthread_should_stop()) {
1936 		if (ret < 0 || sc->reset)
1937 			ret = uea_start_reset(sc);
1938 		if (!ret)
1939 			ret = sc->stat(sc);
1940 		if (ret != -EAGAIN)
1941 			uea_wait(sc, 0, msecs_to_jiffies(1000));
1942 		try_to_freeze();
1943 	}
1944 	uea_leaves(INS_TO_USBDEV(sc));
1945 	return ret;
1946 }
1947 
1948 /* Load second usb firmware for ADI930 chip */
1949 static int load_XILINX_firmware(struct uea_softc *sc)
1950 {
1951 	const struct firmware *fw_entry;
1952 	int ret, size, u, ln;
1953 	const u8 *pfw;
1954 	u8 value;
1955 	char *fw_name = FPGA930_FIRMWARE;
1956 
1957 	uea_enters(INS_TO_USBDEV(sc));
1958 
1959 	ret = request_firmware(&fw_entry, fw_name, &sc->usb_dev->dev);
1960 	if (ret) {
1961 		uea_err(INS_TO_USBDEV(sc), "firmware %s is not available\n",
1962 		       fw_name);
1963 		goto err0;
1964 	}
1965 
1966 	pfw = fw_entry->data;
1967 	size = fw_entry->size;
1968 	if (size != 0x577B) {
1969 		uea_err(INS_TO_USBDEV(sc), "firmware %s is corrupted\n",
1970 		       fw_name);
1971 		ret = -EILSEQ;
1972 		goto err1;
1973 	}
1974 	for (u = 0; u < size; u += ln) {
1975 		ln = min(size - u, 64);
1976 		ret = uea_request(sc, 0xe, 0, ln, pfw + u);
1977 		if (ret < 0) {
1978 			uea_err(INS_TO_USBDEV(sc),
1979 			       "elsa download data failed (%d)\n", ret);
1980 			goto err1;
1981 		}
1982 	}
1983 
1984 	/* finish to send the fpga */
1985 	ret = uea_request(sc, 0xe, 1, 0, NULL);
1986 	if (ret < 0) {
1987 		uea_err(INS_TO_USBDEV(sc),
1988 				"elsa download data failed (%d)\n", ret);
1989 		goto err1;
1990 	}
1991 
1992 	/* Tell the modem we finish : de-assert reset */
1993 	value = 0;
1994 	ret = uea_send_modem_cmd(sc->usb_dev, 0xe, 1, &value);
1995 	if (ret < 0)
1996 		uea_err(sc->usb_dev, "elsa de-assert failed with error"
1997 								" %d\n", ret);
1998 
1999 err1:
2000 	release_firmware(fw_entry);
2001 err0:
2002 	uea_leaves(INS_TO_USBDEV(sc));
2003 	return ret;
2004 }
2005 
2006 /* The modem send us an ack. First with check if it right */
2007 static void uea_dispatch_cmv_e1(struct uea_softc *sc, struct intr_pkt *intr)
2008 {
2009 	struct cmv_dsc_e1 *dsc = &sc->cmv_dsc.e1;
2010 	struct cmv_e1 *cmv = &intr->u.e1.s2.cmv;
2011 
2012 	uea_enters(INS_TO_USBDEV(sc));
2013 	if (le16_to_cpu(cmv->wPreamble) != E1_PREAMBLE)
2014 		goto bad1;
2015 
2016 	if (cmv->bDirection != E1_MODEMTOHOST)
2017 		goto bad1;
2018 
2019 	/* FIXME : ADI930 reply wrong preambule (func = 2, sub = 2) to
2020 	 * the first MEMACCESS cmv. Ignore it...
2021 	 */
2022 	if (cmv->bFunction != dsc->function) {
2023 		if (UEA_CHIP_VERSION(sc) == ADI930
2024 				&& cmv->bFunction ==  E1_MAKEFUNCTION(2, 2)) {
2025 			cmv->wIndex = cpu_to_le16(dsc->idx);
2026 			put_unaligned_le32(dsc->address,
2027 						&cmv->dwSymbolicAddress);
2028 			cmv->wOffsetAddress = cpu_to_le16(dsc->offset);
2029 		} else
2030 			goto bad2;
2031 	}
2032 
2033 	if (cmv->bFunction == E1_MAKEFUNCTION(E1_ADSLDIRECTIVE,
2034 							E1_MODEMREADY)) {
2035 		wake_up_cmv_ack(sc);
2036 		uea_leaves(INS_TO_USBDEV(sc));
2037 		return;
2038 	}
2039 
2040 	/* in case of MEMACCESS */
2041 	if (le16_to_cpu(cmv->wIndex) != dsc->idx ||
2042 	    get_unaligned_le32(&cmv->dwSymbolicAddress) != dsc->address ||
2043 	    le16_to_cpu(cmv->wOffsetAddress) != dsc->offset)
2044 		goto bad2;
2045 
2046 	sc->data = get_unaligned_le32(&cmv->dwData);
2047 	sc->data = sc->data << 16 | sc->data >> 16;
2048 
2049 	wake_up_cmv_ack(sc);
2050 	uea_leaves(INS_TO_USBDEV(sc));
2051 	return;
2052 
2053 bad2:
2054 	uea_err(INS_TO_USBDEV(sc), "unexpected cmv received, "
2055 			"Function : %d, Subfunction : %d\n",
2056 			E1_FUNCTION_TYPE(cmv->bFunction),
2057 			E1_FUNCTION_SUBTYPE(cmv->bFunction));
2058 	uea_leaves(INS_TO_USBDEV(sc));
2059 	return;
2060 
2061 bad1:
2062 	uea_err(INS_TO_USBDEV(sc), "invalid cmv received, "
2063 			"wPreamble %d, bDirection %d\n",
2064 			le16_to_cpu(cmv->wPreamble), cmv->bDirection);
2065 	uea_leaves(INS_TO_USBDEV(sc));
2066 }
2067 
2068 /* The modem send us an ack. First with check if it right */
2069 static void uea_dispatch_cmv_e4(struct uea_softc *sc, struct intr_pkt *intr)
2070 {
2071 	struct cmv_dsc_e4 *dsc = &sc->cmv_dsc.e4;
2072 	struct cmv_e4 *cmv = &intr->u.e4.s2.cmv;
2073 
2074 	uea_enters(INS_TO_USBDEV(sc));
2075 	uea_dbg(INS_TO_USBDEV(sc), "cmv %x %x %x %x %x %x\n",
2076 		be16_to_cpu(cmv->wGroup), be16_to_cpu(cmv->wFunction),
2077 		be16_to_cpu(cmv->wOffset), be16_to_cpu(cmv->wAddress),
2078 		be32_to_cpu(cmv->dwData[0]), be32_to_cpu(cmv->dwData[1]));
2079 
2080 	if (be16_to_cpu(cmv->wFunction) != dsc->function)
2081 		goto bad2;
2082 
2083 	if (be16_to_cpu(cmv->wFunction) == E4_MAKEFUNCTION(E4_ADSLDIRECTIVE,
2084 						E4_MODEMREADY, 1)) {
2085 		wake_up_cmv_ack(sc);
2086 		uea_leaves(INS_TO_USBDEV(sc));
2087 		return;
2088 	}
2089 
2090 	/* in case of MEMACCESS */
2091 	if (be16_to_cpu(cmv->wOffset) != dsc->offset ||
2092 	    be16_to_cpu(cmv->wGroup) != dsc->group ||
2093 	    be16_to_cpu(cmv->wAddress) != dsc->address)
2094 		goto bad2;
2095 
2096 	sc->data = be32_to_cpu(cmv->dwData[0]);
2097 	sc->data1 = be32_to_cpu(cmv->dwData[1]);
2098 	wake_up_cmv_ack(sc);
2099 	uea_leaves(INS_TO_USBDEV(sc));
2100 	return;
2101 
2102 bad2:
2103 	uea_err(INS_TO_USBDEV(sc), "unexpected cmv received, "
2104 			"Function : %d, Subfunction : %d\n",
2105 			E4_FUNCTION_TYPE(cmv->wFunction),
2106 			E4_FUNCTION_SUBTYPE(cmv->wFunction));
2107 	uea_leaves(INS_TO_USBDEV(sc));
2108 	return;
2109 }
2110 
2111 static void uea_schedule_load_page_e1(struct uea_softc *sc,
2112 						struct intr_pkt *intr)
2113 {
2114 	sc->pageno = intr->e1_bSwapPageNo;
2115 	sc->ovl = intr->e1_bOvl >> 4 | intr->e1_bOvl << 4;
2116 	schedule_work(&sc->task);
2117 }
2118 
2119 static void uea_schedule_load_page_e4(struct uea_softc *sc,
2120 						struct intr_pkt *intr)
2121 {
2122 	sc->pageno = intr->e4_bSwapPageNo;
2123 	schedule_work(&sc->task);
2124 }
2125 
2126 /*
2127  * interrupt handler
2128  */
2129 static void uea_intr(struct urb *urb)
2130 {
2131 	struct uea_softc *sc = urb->context;
2132 	struct intr_pkt *intr = urb->transfer_buffer;
2133 	int status = urb->status;
2134 
2135 	uea_enters(INS_TO_USBDEV(sc));
2136 
2137 	if (unlikely(status < 0)) {
2138 		uea_err(INS_TO_USBDEV(sc), "uea_intr() failed with %d\n",
2139 		       status);
2140 		return;
2141 	}
2142 
2143 	/* device-to-host interrupt */
2144 	if (intr->bType != 0x08 || sc->booting) {
2145 		uea_err(INS_TO_USBDEV(sc), "wrong interrupt\n");
2146 		goto resubmit;
2147 	}
2148 
2149 	switch (le16_to_cpu(intr->wInterrupt)) {
2150 	case INT_LOADSWAPPAGE:
2151 		sc->schedule_load_page(sc, intr);
2152 		break;
2153 
2154 	case INT_INCOMINGCMV:
2155 		sc->dispatch_cmv(sc, intr);
2156 		break;
2157 
2158 	default:
2159 		uea_err(INS_TO_USBDEV(sc), "unknown interrupt %u\n",
2160 		       le16_to_cpu(intr->wInterrupt));
2161 	}
2162 
2163 resubmit:
2164 	usb_submit_urb(sc->urb_int, GFP_ATOMIC);
2165 }
2166 
2167 /*
2168  * Start the modem : init the data and start kernel thread
2169  */
2170 static int uea_boot(struct uea_softc *sc)
2171 {
2172 	int ret, size;
2173 	struct intr_pkt *intr;
2174 
2175 	uea_enters(INS_TO_USBDEV(sc));
2176 
2177 	if (UEA_CHIP_VERSION(sc) == EAGLE_IV) {
2178 		size = E4_INTR_PKT_SIZE;
2179 		sc->dispatch_cmv = uea_dispatch_cmv_e4;
2180 		sc->schedule_load_page = uea_schedule_load_page_e4;
2181 		sc->stat = uea_stat_e4;
2182 		sc->send_cmvs = uea_send_cmvs_e4;
2183 		INIT_WORK(&sc->task, uea_load_page_e4);
2184 	} else {
2185 		size = E1_INTR_PKT_SIZE;
2186 		sc->dispatch_cmv = uea_dispatch_cmv_e1;
2187 		sc->schedule_load_page = uea_schedule_load_page_e1;
2188 		sc->stat = uea_stat_e1;
2189 		sc->send_cmvs = uea_send_cmvs_e1;
2190 		INIT_WORK(&sc->task, uea_load_page_e1);
2191 	}
2192 
2193 	init_waitqueue_head(&sc->sync_q);
2194 
2195 	if (UEA_CHIP_VERSION(sc) == ADI930)
2196 		load_XILINX_firmware(sc);
2197 
2198 	intr = kmalloc(size, GFP_KERNEL);
2199 	if (!intr) {
2200 		uea_err(INS_TO_USBDEV(sc),
2201 		       "cannot allocate interrupt package\n");
2202 		goto err0;
2203 	}
2204 
2205 	sc->urb_int = usb_alloc_urb(0, GFP_KERNEL);
2206 	if (!sc->urb_int) {
2207 		uea_err(INS_TO_USBDEV(sc), "cannot allocate interrupt URB\n");
2208 		goto err1;
2209 	}
2210 
2211 	usb_fill_int_urb(sc->urb_int, sc->usb_dev,
2212 			 usb_rcvintpipe(sc->usb_dev, UEA_INTR_PIPE),
2213 			 intr, size, uea_intr, sc,
2214 			 sc->usb_dev->actconfig->interface[0]->altsetting[0].
2215 			 endpoint[0].desc.bInterval);
2216 
2217 	ret = usb_submit_urb(sc->urb_int, GFP_KERNEL);
2218 	if (ret < 0) {
2219 		uea_err(INS_TO_USBDEV(sc),
2220 		       "urb submition failed with error %d\n", ret);
2221 		goto err1;
2222 	}
2223 
2224 	/* Create worker thread, but don't start it here.  Start it after
2225 	 * all usbatm generic initialization is done.
2226 	 */
2227 	sc->kthread = kthread_create(uea_kthread, sc, "ueagle-atm");
2228 	if (IS_ERR(sc->kthread)) {
2229 		uea_err(INS_TO_USBDEV(sc), "failed to create thread\n");
2230 		goto err2;
2231 	}
2232 
2233 	uea_leaves(INS_TO_USBDEV(sc));
2234 	return 0;
2235 
2236 err2:
2237 	usb_kill_urb(sc->urb_int);
2238 err1:
2239 	usb_free_urb(sc->urb_int);
2240 	sc->urb_int = NULL;
2241 	kfree(intr);
2242 err0:
2243 	uea_leaves(INS_TO_USBDEV(sc));
2244 	return -ENOMEM;
2245 }
2246 
2247 /*
2248  * Stop the modem : kill kernel thread and free data
2249  */
2250 static void uea_stop(struct uea_softc *sc)
2251 {
2252 	int ret;
2253 	uea_enters(INS_TO_USBDEV(sc));
2254 	ret = kthread_stop(sc->kthread);
2255 	uea_dbg(INS_TO_USBDEV(sc), "kthread finish with status %d\n", ret);
2256 
2257 	uea_request(sc, UEA_SET_MODE, UEA_LOOPBACK_ON, 0, NULL);
2258 
2259 	usb_kill_urb(sc->urb_int);
2260 	kfree(sc->urb_int->transfer_buffer);
2261 	usb_free_urb(sc->urb_int);
2262 
2263 	/* flush the work item, when no one can schedule it */
2264 	flush_work(&sc->task);
2265 
2266 	release_firmware(sc->dsp_firm);
2267 	uea_leaves(INS_TO_USBDEV(sc));
2268 }
2269 
2270 /* syfs interface */
2271 static struct uea_softc *dev_to_uea(struct device *dev)
2272 {
2273 	struct usb_interface *intf;
2274 	struct usbatm_data *usbatm;
2275 
2276 	intf = to_usb_interface(dev);
2277 	if (!intf)
2278 		return NULL;
2279 
2280 	usbatm = usb_get_intfdata(intf);
2281 	if (!usbatm)
2282 		return NULL;
2283 
2284 	return usbatm->driver_data;
2285 }
2286 
2287 static ssize_t read_status(struct device *dev, struct device_attribute *attr,
2288 		char *buf)
2289 {
2290 	int ret = -ENODEV;
2291 	struct uea_softc *sc;
2292 
2293 	mutex_lock(&uea_mutex);
2294 	sc = dev_to_uea(dev);
2295 	if (!sc)
2296 		goto out;
2297 	ret = snprintf(buf, 10, "%08x\n", sc->stats.phy.state);
2298 out:
2299 	mutex_unlock(&uea_mutex);
2300 	return ret;
2301 }
2302 
2303 static ssize_t reboot(struct device *dev, struct device_attribute *attr,
2304 		const char *buf, size_t count)
2305 {
2306 	int ret = -ENODEV;
2307 	struct uea_softc *sc;
2308 
2309 	mutex_lock(&uea_mutex);
2310 	sc = dev_to_uea(dev);
2311 	if (!sc)
2312 		goto out;
2313 	sc->reset = 1;
2314 	ret = count;
2315 out:
2316 	mutex_unlock(&uea_mutex);
2317 	return ret;
2318 }
2319 
2320 static DEVICE_ATTR(stat_status, S_IWUSR | S_IRUGO, read_status, reboot);
2321 
2322 static ssize_t read_human_status(struct device *dev,
2323 			struct device_attribute *attr, char *buf)
2324 {
2325 	int ret = -ENODEV;
2326 	int modem_state;
2327 	struct uea_softc *sc;
2328 
2329 	mutex_lock(&uea_mutex);
2330 	sc = dev_to_uea(dev);
2331 	if (!sc)
2332 		goto out;
2333 
2334 	if (UEA_CHIP_VERSION(sc) == EAGLE_IV) {
2335 		switch (sc->stats.phy.state) {
2336 		case 0x0:	/* not yet synchronized */
2337 		case 0x1:
2338 		case 0x3:
2339 		case 0x4:
2340 			modem_state = 0;
2341 			break;
2342 		case 0x5:	/* initialization */
2343 		case 0x6:
2344 		case 0x9:
2345 		case 0xa:
2346 			modem_state = 1;
2347 			break;
2348 		case 0x7:	/* operational */
2349 			modem_state = 2;
2350 			break;
2351 		case 0x2:	/* fail ... */
2352 			modem_state = 3;
2353 			break;
2354 		default:	/* unknown */
2355 			modem_state = 4;
2356 			break;
2357 		}
2358 	} else
2359 		modem_state = GET_STATUS(sc->stats.phy.state);
2360 
2361 	switch (modem_state) {
2362 	case 0:
2363 		ret = sprintf(buf, "Modem is booting\n");
2364 		break;
2365 	case 1:
2366 		ret = sprintf(buf, "Modem is initializing\n");
2367 		break;
2368 	case 2:
2369 		ret = sprintf(buf, "Modem is operational\n");
2370 		break;
2371 	case 3:
2372 		ret = sprintf(buf, "Modem synchronization failed\n");
2373 		break;
2374 	default:
2375 		ret = sprintf(buf, "Modem state is unknown\n");
2376 		break;
2377 	}
2378 out:
2379 	mutex_unlock(&uea_mutex);
2380 	return ret;
2381 }
2382 
2383 static DEVICE_ATTR(stat_human_status, S_IRUGO, read_human_status, NULL);
2384 
2385 static ssize_t read_delin(struct device *dev, struct device_attribute *attr,
2386 		char *buf)
2387 {
2388 	int ret = -ENODEV;
2389 	struct uea_softc *sc;
2390 	char *delin = "GOOD";
2391 
2392 	mutex_lock(&uea_mutex);
2393 	sc = dev_to_uea(dev);
2394 	if (!sc)
2395 		goto out;
2396 
2397 	if (UEA_CHIP_VERSION(sc) == EAGLE_IV) {
2398 		if (sc->stats.phy.flags & 0x4000)
2399 			delin = "RESET";
2400 		else if (sc->stats.phy.flags & 0x0001)
2401 			delin = "LOSS";
2402 	} else {
2403 		if (sc->stats.phy.flags & 0x0C00)
2404 			delin = "ERROR";
2405 		else if (sc->stats.phy.flags & 0x0030)
2406 			delin = "LOSS";
2407 	}
2408 
2409 	ret = sprintf(buf, "%s\n", delin);
2410 out:
2411 	mutex_unlock(&uea_mutex);
2412 	return ret;
2413 }
2414 
2415 static DEVICE_ATTR(stat_delin, S_IRUGO, read_delin, NULL);
2416 
2417 #define UEA_ATTR(name, reset)					\
2418 								\
2419 static ssize_t read_##name(struct device *dev,			\
2420 		struct device_attribute *attr, char *buf)	\
2421 {								\
2422 	int ret = -ENODEV;					\
2423 	struct uea_softc *sc;					\
2424 								\
2425 	mutex_lock(&uea_mutex);					\
2426 	sc = dev_to_uea(dev);					\
2427 	if (!sc)						\
2428 		goto out;					\
2429 	ret = snprintf(buf, 10, "%08x\n", sc->stats.phy.name);	\
2430 	if (reset)						\
2431 		sc->stats.phy.name = 0;				\
2432 out:								\
2433 	mutex_unlock(&uea_mutex);				\
2434 	return ret;						\
2435 }								\
2436 								\
2437 static DEVICE_ATTR(stat_##name, S_IRUGO, read_##name, NULL)
2438 
2439 UEA_ATTR(mflags, 1);
2440 UEA_ATTR(vidcpe, 0);
2441 UEA_ATTR(usrate, 0);
2442 UEA_ATTR(dsrate, 0);
2443 UEA_ATTR(usattenuation, 0);
2444 UEA_ATTR(dsattenuation, 0);
2445 UEA_ATTR(usmargin, 0);
2446 UEA_ATTR(dsmargin, 0);
2447 UEA_ATTR(txflow, 0);
2448 UEA_ATTR(rxflow, 0);
2449 UEA_ATTR(uscorr, 0);
2450 UEA_ATTR(dscorr, 0);
2451 UEA_ATTR(usunc, 0);
2452 UEA_ATTR(dsunc, 0);
2453 UEA_ATTR(firmid, 0);
2454 
2455 /* Retrieve the device End System Identifier (MAC) */
2456 
2457 static int uea_getesi(struct uea_softc *sc, u_char *esi)
2458 {
2459 	unsigned char mac_str[2 * ETH_ALEN + 1];
2460 	int i;
2461 	if (usb_string
2462 	    (sc->usb_dev, sc->usb_dev->descriptor.iSerialNumber, mac_str,
2463 	     sizeof(mac_str)) != 2 * ETH_ALEN)
2464 		return 1;
2465 
2466 	for (i = 0; i < ETH_ALEN; i++)
2467 		esi[i] = hex_to_bin(mac_str[2 * i]) * 16 +
2468 			 hex_to_bin(mac_str[2 * i + 1]);
2469 
2470 	return 0;
2471 }
2472 
2473 /* ATM stuff */
2474 static int uea_atm_open(struct usbatm_data *usbatm, struct atm_dev *atm_dev)
2475 {
2476 	struct uea_softc *sc = usbatm->driver_data;
2477 
2478 	return uea_getesi(sc, atm_dev->esi);
2479 }
2480 
2481 static int uea_heavy(struct usbatm_data *usbatm, struct usb_interface *intf)
2482 {
2483 	struct uea_softc *sc = usbatm->driver_data;
2484 
2485 	wait_event_interruptible(sc->sync_q, IS_OPERATIONAL(sc));
2486 
2487 	return 0;
2488 
2489 }
2490 
2491 static int claim_interface(struct usb_device *usb_dev,
2492 			   struct usbatm_data *usbatm, int ifnum)
2493 {
2494 	int ret;
2495 	struct usb_interface *intf = usb_ifnum_to_if(usb_dev, ifnum);
2496 
2497 	if (!intf) {
2498 		uea_err(usb_dev, "interface %d not found\n", ifnum);
2499 		return -ENODEV;
2500 	}
2501 
2502 	ret = usb_driver_claim_interface(&uea_driver, intf, usbatm);
2503 	if (ret != 0)
2504 		uea_err(usb_dev, "can't claim interface %d, error %d\n", ifnum,
2505 		       ret);
2506 	return ret;
2507 }
2508 
2509 static struct attribute *attrs[] = {
2510 	&dev_attr_stat_status.attr,
2511 	&dev_attr_stat_mflags.attr,
2512 	&dev_attr_stat_human_status.attr,
2513 	&dev_attr_stat_delin.attr,
2514 	&dev_attr_stat_vidcpe.attr,
2515 	&dev_attr_stat_usrate.attr,
2516 	&dev_attr_stat_dsrate.attr,
2517 	&dev_attr_stat_usattenuation.attr,
2518 	&dev_attr_stat_dsattenuation.attr,
2519 	&dev_attr_stat_usmargin.attr,
2520 	&dev_attr_stat_dsmargin.attr,
2521 	&dev_attr_stat_txflow.attr,
2522 	&dev_attr_stat_rxflow.attr,
2523 	&dev_attr_stat_uscorr.attr,
2524 	&dev_attr_stat_dscorr.attr,
2525 	&dev_attr_stat_usunc.attr,
2526 	&dev_attr_stat_dsunc.attr,
2527 	&dev_attr_stat_firmid.attr,
2528 	NULL,
2529 };
2530 static struct attribute_group attr_grp = {
2531 	.attrs = attrs,
2532 };
2533 
2534 static int uea_bind(struct usbatm_data *usbatm, struct usb_interface *intf,
2535 		   const struct usb_device_id *id)
2536 {
2537 	struct usb_device *usb = interface_to_usbdev(intf);
2538 	struct uea_softc *sc;
2539 	int ret, ifnum = intf->altsetting->desc.bInterfaceNumber;
2540 	unsigned int alt;
2541 
2542 	uea_enters(usb);
2543 
2544 	/* interface 0 is for firmware/monitoring */
2545 	if (ifnum != UEA_INTR_IFACE_NO)
2546 		return -ENODEV;
2547 
2548 	usbatm->flags = (sync_wait[modem_index] ? 0 : UDSL_SKIP_HEAVY_INIT);
2549 
2550 	/* interface 1 is for outbound traffic */
2551 	ret = claim_interface(usb, usbatm, UEA_US_IFACE_NO);
2552 	if (ret < 0)
2553 		return ret;
2554 
2555 	/* ADI930 has only 2 interfaces and inbound traffic is on interface 1 */
2556 	if (UEA_CHIP_VERSION(id) != ADI930) {
2557 		/* interface 2 is for inbound traffic */
2558 		ret = claim_interface(usb, usbatm, UEA_DS_IFACE_NO);
2559 		if (ret < 0)
2560 			return ret;
2561 	}
2562 
2563 	sc = kzalloc(sizeof(struct uea_softc), GFP_KERNEL);
2564 	if (!sc) {
2565 		uea_err(usb, "uea_init: not enough memory !\n");
2566 		return -ENOMEM;
2567 	}
2568 
2569 	sc->usb_dev = usb;
2570 	usbatm->driver_data = sc;
2571 	sc->usbatm = usbatm;
2572 	sc->modem_index = (modem_index < NB_MODEM) ? modem_index++ : 0;
2573 	sc->driver_info = id->driver_info;
2574 
2575 	/* first try to use module parameter */
2576 	if (annex[sc->modem_index] == 1)
2577 		sc->annex = ANNEXA;
2578 	else if (annex[sc->modem_index] == 2)
2579 		sc->annex = ANNEXB;
2580 	/* try to autodetect annex */
2581 	else if (sc->driver_info & AUTO_ANNEX_A)
2582 		sc->annex = ANNEXA;
2583 	else if (sc->driver_info & AUTO_ANNEX_B)
2584 		sc->annex = ANNEXB;
2585 	else
2586 		sc->annex = (le16_to_cpu
2587 		(sc->usb_dev->descriptor.bcdDevice) & 0x80) ? ANNEXB : ANNEXA;
2588 
2589 	alt = altsetting[sc->modem_index];
2590 	/* ADI930 don't support iso */
2591 	if (UEA_CHIP_VERSION(id) != ADI930 && alt > 0) {
2592 		if (alt <= 8 &&
2593 			usb_set_interface(usb, UEA_DS_IFACE_NO, alt) == 0) {
2594 			uea_dbg(usb, "set alternate %u for 2 interface\n", alt);
2595 			uea_info(usb, "using iso mode\n");
2596 			usbatm->flags |= UDSL_USE_ISOC | UDSL_IGNORE_EILSEQ;
2597 		} else {
2598 			uea_err(usb, "setting alternate %u failed for "
2599 					"2 interface, using bulk mode\n", alt);
2600 		}
2601 	}
2602 
2603 	ret = sysfs_create_group(&intf->dev.kobj, &attr_grp);
2604 	if (ret < 0)
2605 		goto error;
2606 
2607 	ret = uea_boot(sc);
2608 	if (ret < 0)
2609 		goto error_rm_grp;
2610 
2611 	return 0;
2612 
2613 error_rm_grp:
2614 	sysfs_remove_group(&intf->dev.kobj, &attr_grp);
2615 error:
2616 	kfree(sc);
2617 	return ret;
2618 }
2619 
2620 static void uea_unbind(struct usbatm_data *usbatm, struct usb_interface *intf)
2621 {
2622 	struct uea_softc *sc = usbatm->driver_data;
2623 
2624 	sysfs_remove_group(&intf->dev.kobj, &attr_grp);
2625 	uea_stop(sc);
2626 	kfree(sc);
2627 }
2628 
2629 static struct usbatm_driver uea_usbatm_driver = {
2630 	.driver_name = "ueagle-atm",
2631 	.bind = uea_bind,
2632 	.atm_start = uea_atm_open,
2633 	.unbind = uea_unbind,
2634 	.heavy_init = uea_heavy,
2635 	.bulk_in = UEA_BULK_DATA_PIPE,
2636 	.bulk_out = UEA_BULK_DATA_PIPE,
2637 	.isoc_in = UEA_ISO_DATA_PIPE,
2638 };
2639 
2640 static int uea_probe(struct usb_interface *intf, const struct usb_device_id *id)
2641 {
2642 	struct usb_device *usb = interface_to_usbdev(intf);
2643 	int ret;
2644 
2645 	uea_enters(usb);
2646 	uea_info(usb, "ADSL device founded vid (%#X) pid (%#X) Rev (%#X): %s\n",
2647 		le16_to_cpu(usb->descriptor.idVendor),
2648 		le16_to_cpu(usb->descriptor.idProduct),
2649 		le16_to_cpu(usb->descriptor.bcdDevice),
2650 		chip_name[UEA_CHIP_VERSION(id)]);
2651 
2652 	usb_reset_device(usb);
2653 
2654 	if (UEA_IS_PREFIRM(id))
2655 		return uea_load_firmware(usb, UEA_CHIP_VERSION(id));
2656 
2657 	ret = usbatm_usb_probe(intf, id, &uea_usbatm_driver);
2658 	if (ret == 0) {
2659 		struct usbatm_data *usbatm = usb_get_intfdata(intf);
2660 		struct uea_softc *sc = usbatm->driver_data;
2661 
2662 		/* Ensure carrier is initialized to off as early as possible */
2663 		UPDATE_ATM_SIGNAL(ATM_PHY_SIG_LOST);
2664 
2665 		/* Only start the worker thread when all init is done */
2666 		wake_up_process(sc->kthread);
2667 	}
2668 
2669 	return ret;
2670 }
2671 
2672 static void uea_disconnect(struct usb_interface *intf)
2673 {
2674 	struct usb_device *usb = interface_to_usbdev(intf);
2675 	int ifnum = intf->altsetting->desc.bInterfaceNumber;
2676 	uea_enters(usb);
2677 
2678 	/* ADI930 has 2 interfaces and eagle 3 interfaces.
2679 	 * Pre-firmware device has one interface
2680 	 */
2681 	if (usb->config->desc.bNumInterfaces != 1 && ifnum == 0) {
2682 		mutex_lock(&uea_mutex);
2683 		usbatm_usb_disconnect(intf);
2684 		mutex_unlock(&uea_mutex);
2685 		uea_info(usb, "ADSL device removed\n");
2686 	}
2687 
2688 	uea_leaves(usb);
2689 }
2690 
2691 /*
2692  * List of supported VID/PID
2693  */
2694 static const struct usb_device_id uea_ids[] = {
2695 	{USB_DEVICE(ANALOG_VID,	ADI930_PID_PREFIRM),
2696 		.driver_info = ADI930 | PREFIRM},
2697 	{USB_DEVICE(ANALOG_VID,	ADI930_PID_PSTFIRM),
2698 		.driver_info = ADI930 | PSTFIRM},
2699 	{USB_DEVICE(ANALOG_VID,	EAGLE_I_PID_PREFIRM),
2700 		.driver_info = EAGLE_I | PREFIRM},
2701 	{USB_DEVICE(ANALOG_VID,	EAGLE_I_PID_PSTFIRM),
2702 		.driver_info = EAGLE_I | PSTFIRM},
2703 	{USB_DEVICE(ANALOG_VID,	EAGLE_II_PID_PREFIRM),
2704 		.driver_info = EAGLE_II | PREFIRM},
2705 	{USB_DEVICE(ANALOG_VID,	EAGLE_II_PID_PSTFIRM),
2706 		.driver_info = EAGLE_II | PSTFIRM},
2707 	{USB_DEVICE(ANALOG_VID,	EAGLE_IIC_PID_PREFIRM),
2708 		.driver_info = EAGLE_II | PREFIRM},
2709 	{USB_DEVICE(ANALOG_VID,	EAGLE_IIC_PID_PSTFIRM),
2710 		.driver_info = EAGLE_II | PSTFIRM},
2711 	{USB_DEVICE(ANALOG_VID,	EAGLE_III_PID_PREFIRM),
2712 		.driver_info = EAGLE_III | PREFIRM},
2713 	{USB_DEVICE(ANALOG_VID,	EAGLE_III_PID_PSTFIRM),
2714 		.driver_info = EAGLE_III | PSTFIRM},
2715 	{USB_DEVICE(ANALOG_VID,	EAGLE_IV_PID_PREFIRM),
2716 		.driver_info = EAGLE_IV | PREFIRM},
2717 	{USB_DEVICE(ANALOG_VID,	EAGLE_IV_PID_PSTFIRM),
2718 		.driver_info = EAGLE_IV | PSTFIRM},
2719 	{USB_DEVICE(DEVOLO_VID,	DEVOLO_EAGLE_I_A_PID_PREFIRM),
2720 		.driver_info = EAGLE_I | PREFIRM},
2721 	{USB_DEVICE(DEVOLO_VID,	DEVOLO_EAGLE_I_A_PID_PSTFIRM),
2722 		.driver_info = EAGLE_I | PSTFIRM | AUTO_ANNEX_A},
2723 	{USB_DEVICE(DEVOLO_VID,	DEVOLO_EAGLE_I_B_PID_PREFIRM),
2724 		.driver_info = EAGLE_I | PREFIRM},
2725 	{USB_DEVICE(DEVOLO_VID,	DEVOLO_EAGLE_I_B_PID_PSTFIRM),
2726 		.driver_info = EAGLE_I | PSTFIRM | AUTO_ANNEX_B},
2727 	{USB_DEVICE(DEVOLO_VID,	DEVOLO_EAGLE_II_A_PID_PREFIRM),
2728 		.driver_info = EAGLE_II | PREFIRM},
2729 	{USB_DEVICE(DEVOLO_VID,	DEVOLO_EAGLE_II_A_PID_PSTFIRM),
2730 		.driver_info = EAGLE_II | PSTFIRM | AUTO_ANNEX_A},
2731 	{USB_DEVICE(DEVOLO_VID,	DEVOLO_EAGLE_II_B_PID_PREFIRM),
2732 		.driver_info = EAGLE_II | PREFIRM},
2733 	{USB_DEVICE(DEVOLO_VID,	DEVOLO_EAGLE_II_B_PID_PSTFIRM),
2734 		.driver_info = EAGLE_II | PSTFIRM | AUTO_ANNEX_B},
2735 	{USB_DEVICE(ELSA_VID,	ELSA_PID_PREFIRM),
2736 		.driver_info = ADI930 | PREFIRM},
2737 	{USB_DEVICE(ELSA_VID,	ELSA_PID_PSTFIRM),
2738 		.driver_info = ADI930 | PSTFIRM},
2739 	{USB_DEVICE(ELSA_VID,	ELSA_PID_A_PREFIRM),
2740 		.driver_info = ADI930 | PREFIRM},
2741 	{USB_DEVICE(ELSA_VID,	ELSA_PID_A_PSTFIRM),
2742 		.driver_info = ADI930 | PSTFIRM | AUTO_ANNEX_A},
2743 	{USB_DEVICE(ELSA_VID,	ELSA_PID_B_PREFIRM),
2744 		.driver_info = ADI930 | PREFIRM},
2745 	{USB_DEVICE(ELSA_VID,	ELSA_PID_B_PSTFIRM),
2746 		.driver_info = ADI930 | PSTFIRM | AUTO_ANNEX_B},
2747 	{USB_DEVICE(USR_VID,	MILLER_A_PID_PREFIRM),
2748 		.driver_info = EAGLE_I | PREFIRM},
2749 	{USB_DEVICE(USR_VID,	MILLER_A_PID_PSTFIRM),
2750 		.driver_info = EAGLE_I | PSTFIRM  | AUTO_ANNEX_A},
2751 	{USB_DEVICE(USR_VID,	MILLER_B_PID_PREFIRM),
2752 		.driver_info = EAGLE_I | PREFIRM},
2753 	{USB_DEVICE(USR_VID,	MILLER_B_PID_PSTFIRM),
2754 		.driver_info = EAGLE_I | PSTFIRM  | AUTO_ANNEX_B},
2755 	{USB_DEVICE(USR_VID,	HEINEKEN_A_PID_PREFIRM),
2756 		.driver_info = EAGLE_I | PREFIRM},
2757 	{USB_DEVICE(USR_VID,	HEINEKEN_A_PID_PSTFIRM),
2758 		.driver_info = EAGLE_I | PSTFIRM | AUTO_ANNEX_A},
2759 	{USB_DEVICE(USR_VID,	HEINEKEN_B_PID_PREFIRM),
2760 		.driver_info = EAGLE_I | PREFIRM},
2761 	{USB_DEVICE(USR_VID,	HEINEKEN_B_PID_PSTFIRM),
2762 		.driver_info = EAGLE_I | PSTFIRM | AUTO_ANNEX_B},
2763 	{}
2764 };
2765 
2766 /*
2767  * USB driver descriptor
2768  */
2769 static struct usb_driver uea_driver = {
2770 	.name = "ueagle-atm",
2771 	.id_table = uea_ids,
2772 	.probe = uea_probe,
2773 	.disconnect = uea_disconnect,
2774 };
2775 
2776 MODULE_DEVICE_TABLE(usb, uea_ids);
2777 
2778 module_usb_driver(uea_driver);
2779 
2780 MODULE_AUTHOR("Damien Bergamini/Matthieu Castet/Stanislaw W. Gruszka");
2781 MODULE_DESCRIPTION("ADI 930/Eagle USB ADSL Modem driver");
2782 MODULE_LICENSE("Dual BSD/GPL");
2783 MODULE_FIRMWARE(EAGLE_FIRMWARE);
2784 MODULE_FIRMWARE(ADI930_FIRMWARE);
2785 MODULE_FIRMWARE(EAGLE_I_FIRMWARE);
2786 MODULE_FIRMWARE(EAGLE_II_FIRMWARE);
2787 MODULE_FIRMWARE(EAGLE_III_FIRMWARE);
2788 MODULE_FIRMWARE(EAGLE_IV_FIRMWARE);
2789 MODULE_FIRMWARE(DSP4I_FIRMWARE);
2790 MODULE_FIRMWARE(DSP4P_FIRMWARE);
2791 MODULE_FIRMWARE(DSP9I_FIRMWARE);
2792 MODULE_FIRMWARE(DSP9P_FIRMWARE);
2793 MODULE_FIRMWARE(DSPEI_FIRMWARE);
2794 MODULE_FIRMWARE(DSPEP_FIRMWARE);
2795 MODULE_FIRMWARE(FPGA930_FIRMWARE);
2796 MODULE_FIRMWARE(CMV4P_FIRMWARE);
2797 MODULE_FIRMWARE(CMV4PV2_FIRMWARE);
2798 MODULE_FIRMWARE(CMV4I_FIRMWARE);
2799 MODULE_FIRMWARE(CMV4IV2_FIRMWARE);
2800 MODULE_FIRMWARE(CMV9P_FIRMWARE);
2801 MODULE_FIRMWARE(CMV9PV2_FIRMWARE);
2802 MODULE_FIRMWARE(CMV9I_FIRMWARE);
2803 MODULE_FIRMWARE(CMV9IV2_FIRMWARE);
2804 MODULE_FIRMWARE(CMVEP_FIRMWARE);
2805 MODULE_FIRMWARE(CMVEPV2_FIRMWARE);
2806 MODULE_FIRMWARE(CMVEI_FIRMWARE);
2807 MODULE_FIRMWARE(CMVEIV2_FIRMWARE);
2808