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