xref: /linux/drivers/usb/atm/ueagle-atm.c (revision 06d07429858317ded2db7986113a9e0129cd599b)
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 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_freezable_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 	}
1900 	uea_leaves(INS_TO_USBDEV(sc));
1901 	return ret;
1902 }
1903 
1904 /* Load second usb firmware for ADI930 chip */
1905 static int load_XILINX_firmware(struct uea_softc *sc)
1906 {
1907 	const struct firmware *fw_entry;
1908 	int ret, size, u, ln;
1909 	const u8 *pfw;
1910 	u8 value;
1911 	char *fw_name = FPGA930_FIRMWARE;
1912 
1913 	uea_enters(INS_TO_USBDEV(sc));
1914 
1915 	ret = request_firmware(&fw_entry, fw_name, &sc->usb_dev->dev);
1916 	if (ret) {
1917 		uea_err(INS_TO_USBDEV(sc), "firmware %s is not available\n",
1918 		       fw_name);
1919 		goto err0;
1920 	}
1921 
1922 	pfw = fw_entry->data;
1923 	size = fw_entry->size;
1924 	if (size != 0x577B) {
1925 		uea_err(INS_TO_USBDEV(sc), "firmware %s is corrupted\n",
1926 		       fw_name);
1927 		ret = -EILSEQ;
1928 		goto err1;
1929 	}
1930 	for (u = 0; u < size; u += ln) {
1931 		ln = min(size - u, 64);
1932 		ret = uea_request(sc, 0xe, 0, ln, pfw + u);
1933 		if (ret < 0) {
1934 			uea_err(INS_TO_USBDEV(sc),
1935 			       "elsa download data failed (%d)\n", ret);
1936 			goto err1;
1937 		}
1938 	}
1939 
1940 	/* finish to send the fpga */
1941 	ret = uea_request(sc, 0xe, 1, 0, NULL);
1942 	if (ret < 0) {
1943 		uea_err(INS_TO_USBDEV(sc),
1944 				"elsa download data failed (%d)\n", ret);
1945 		goto err1;
1946 	}
1947 
1948 	/* Tell the modem we finish : de-assert reset */
1949 	value = 0;
1950 	ret = uea_send_modem_cmd(sc->usb_dev, 0xe, 1, &value);
1951 	if (ret < 0)
1952 		uea_err(sc->usb_dev, "elsa de-assert failed with error"
1953 								" %d\n", ret);
1954 
1955 err1:
1956 	release_firmware(fw_entry);
1957 err0:
1958 	uea_leaves(INS_TO_USBDEV(sc));
1959 	return ret;
1960 }
1961 
1962 /* The modem send us an ack. First with check if it right */
1963 static void uea_dispatch_cmv_e1(struct uea_softc *sc, struct intr_pkt *intr)
1964 {
1965 	struct cmv_dsc_e1 *dsc = &sc->cmv_dsc.e1;
1966 	struct cmv_e1 *cmv = &intr->u.e1.s2.cmv;
1967 
1968 	uea_enters(INS_TO_USBDEV(sc));
1969 	if (le16_to_cpu(cmv->wPreamble) != E1_PREAMBLE)
1970 		goto bad1;
1971 
1972 	if (cmv->bDirection != E1_MODEMTOHOST)
1973 		goto bad1;
1974 
1975 	/* FIXME : ADI930 reply wrong preambule (func = 2, sub = 2) to
1976 	 * the first MEMACCESS cmv. Ignore it...
1977 	 */
1978 	if (cmv->bFunction != dsc->function) {
1979 		if (UEA_CHIP_VERSION(sc) == ADI930
1980 				&& cmv->bFunction ==  E1_MAKEFUNCTION(2, 2)) {
1981 			cmv->wIndex = cpu_to_le16(dsc->idx);
1982 			put_unaligned_le32(dsc->address,
1983 						&cmv->dwSymbolicAddress);
1984 			cmv->wOffsetAddress = cpu_to_le16(dsc->offset);
1985 		} else
1986 			goto bad2;
1987 	}
1988 
1989 	if (cmv->bFunction == E1_MAKEFUNCTION(E1_ADSLDIRECTIVE,
1990 							E1_MODEMREADY)) {
1991 		wake_up_cmv_ack(sc);
1992 		uea_leaves(INS_TO_USBDEV(sc));
1993 		return;
1994 	}
1995 
1996 	/* in case of MEMACCESS */
1997 	if (le16_to_cpu(cmv->wIndex) != dsc->idx ||
1998 	    get_unaligned_le32(&cmv->dwSymbolicAddress) != dsc->address ||
1999 	    le16_to_cpu(cmv->wOffsetAddress) != dsc->offset)
2000 		goto bad2;
2001 
2002 	sc->data = get_unaligned_le32(&cmv->dwData);
2003 	sc->data = sc->data << 16 | sc->data >> 16;
2004 
2005 	wake_up_cmv_ack(sc);
2006 	uea_leaves(INS_TO_USBDEV(sc));
2007 	return;
2008 
2009 bad2:
2010 	uea_err(INS_TO_USBDEV(sc), "unexpected cmv received, "
2011 			"Function : %d, Subfunction : %d\n",
2012 			E1_FUNCTION_TYPE(cmv->bFunction),
2013 			E1_FUNCTION_SUBTYPE(cmv->bFunction));
2014 	uea_leaves(INS_TO_USBDEV(sc));
2015 	return;
2016 
2017 bad1:
2018 	uea_err(INS_TO_USBDEV(sc), "invalid cmv received, "
2019 			"wPreamble %d, bDirection %d\n",
2020 			le16_to_cpu(cmv->wPreamble), cmv->bDirection);
2021 	uea_leaves(INS_TO_USBDEV(sc));
2022 }
2023 
2024 /* The modem send us an ack. First with check if it right */
2025 static void uea_dispatch_cmv_e4(struct uea_softc *sc, struct intr_pkt *intr)
2026 {
2027 	struct cmv_dsc_e4 *dsc = &sc->cmv_dsc.e4;
2028 	struct cmv_e4 *cmv = &intr->u.e4.s2.cmv;
2029 
2030 	uea_enters(INS_TO_USBDEV(sc));
2031 	uea_dbg(INS_TO_USBDEV(sc), "cmv %x %x %x %x %x %x\n",
2032 		be16_to_cpu(cmv->wGroup), be16_to_cpu(cmv->wFunction),
2033 		be16_to_cpu(cmv->wOffset), be16_to_cpu(cmv->wAddress),
2034 		be32_to_cpu(cmv->dwData[0]), be32_to_cpu(cmv->dwData[1]));
2035 
2036 	if (be16_to_cpu(cmv->wFunction) != dsc->function)
2037 		goto bad2;
2038 
2039 	if (be16_to_cpu(cmv->wFunction) == E4_MAKEFUNCTION(E4_ADSLDIRECTIVE,
2040 						E4_MODEMREADY, 1)) {
2041 		wake_up_cmv_ack(sc);
2042 		uea_leaves(INS_TO_USBDEV(sc));
2043 		return;
2044 	}
2045 
2046 	/* in case of MEMACCESS */
2047 	if (be16_to_cpu(cmv->wOffset) != dsc->offset ||
2048 	    be16_to_cpu(cmv->wGroup) != dsc->group ||
2049 	    be16_to_cpu(cmv->wAddress) != dsc->address)
2050 		goto bad2;
2051 
2052 	sc->data = be32_to_cpu(cmv->dwData[0]);
2053 	sc->data1 = be32_to_cpu(cmv->dwData[1]);
2054 	wake_up_cmv_ack(sc);
2055 	uea_leaves(INS_TO_USBDEV(sc));
2056 	return;
2057 
2058 bad2:
2059 	uea_err(INS_TO_USBDEV(sc), "unexpected cmv received, "
2060 			"Function : %d, Subfunction : %d\n",
2061 			E4_FUNCTION_TYPE(cmv->wFunction),
2062 			E4_FUNCTION_SUBTYPE(cmv->wFunction));
2063 	uea_leaves(INS_TO_USBDEV(sc));
2064 	return;
2065 }
2066 
2067 static void uea_schedule_load_page_e1(struct uea_softc *sc,
2068 						struct intr_pkt *intr)
2069 {
2070 	sc->pageno = intr->e1_bSwapPageNo;
2071 	sc->ovl = intr->e1_bOvl >> 4 | intr->e1_bOvl << 4;
2072 	schedule_work(&sc->task);
2073 }
2074 
2075 static void uea_schedule_load_page_e4(struct uea_softc *sc,
2076 						struct intr_pkt *intr)
2077 {
2078 	sc->pageno = intr->e4_bSwapPageNo;
2079 	schedule_work(&sc->task);
2080 }
2081 
2082 /*
2083  * interrupt handler
2084  */
2085 static void uea_intr(struct urb *urb)
2086 {
2087 	struct uea_softc *sc = urb->context;
2088 	struct intr_pkt *intr = urb->transfer_buffer;
2089 	int status = urb->status;
2090 
2091 	uea_enters(INS_TO_USBDEV(sc));
2092 
2093 	if (unlikely(status < 0)) {
2094 		uea_err(INS_TO_USBDEV(sc), "uea_intr() failed with %d\n",
2095 		       status);
2096 		return;
2097 	}
2098 
2099 	/* device-to-host interrupt */
2100 	if (intr->bType != 0x08 || sc->booting) {
2101 		uea_err(INS_TO_USBDEV(sc), "wrong interrupt\n");
2102 		goto resubmit;
2103 	}
2104 
2105 	switch (le16_to_cpu(intr->wInterrupt)) {
2106 	case INT_LOADSWAPPAGE:
2107 		sc->schedule_load_page(sc, intr);
2108 		break;
2109 
2110 	case INT_INCOMINGCMV:
2111 		sc->dispatch_cmv(sc, intr);
2112 		break;
2113 
2114 	default:
2115 		uea_err(INS_TO_USBDEV(sc), "unknown interrupt %u\n",
2116 		       le16_to_cpu(intr->wInterrupt));
2117 	}
2118 
2119 resubmit:
2120 	usb_submit_urb(sc->urb_int, GFP_ATOMIC);
2121 }
2122 
2123 /*
2124  * Start the modem : init the data and start kernel thread
2125  */
2126 static int uea_boot(struct uea_softc *sc, struct usb_interface *intf)
2127 {
2128 	struct intr_pkt *intr;
2129 	int ret = -ENOMEM;
2130 	int size;
2131 
2132 	uea_enters(INS_TO_USBDEV(sc));
2133 
2134 	if (UEA_CHIP_VERSION(sc) == EAGLE_IV) {
2135 		size = E4_INTR_PKT_SIZE;
2136 		sc->dispatch_cmv = uea_dispatch_cmv_e4;
2137 		sc->schedule_load_page = uea_schedule_load_page_e4;
2138 		sc->stat = uea_stat_e4;
2139 		sc->send_cmvs = uea_send_cmvs_e4;
2140 		INIT_WORK(&sc->task, uea_load_page_e4);
2141 	} else {
2142 		size = E1_INTR_PKT_SIZE;
2143 		sc->dispatch_cmv = uea_dispatch_cmv_e1;
2144 		sc->schedule_load_page = uea_schedule_load_page_e1;
2145 		sc->stat = uea_stat_e1;
2146 		sc->send_cmvs = uea_send_cmvs_e1;
2147 		INIT_WORK(&sc->task, uea_load_page_e1);
2148 	}
2149 
2150 	init_waitqueue_head(&sc->sync_q);
2151 
2152 	if (UEA_CHIP_VERSION(sc) == ADI930)
2153 		load_XILINX_firmware(sc);
2154 
2155 	if (intf->cur_altsetting->desc.bNumEndpoints < 1) {
2156 		ret = -ENODEV;
2157 		goto err0;
2158 	}
2159 
2160 	intr = kmalloc(size, GFP_KERNEL);
2161 	if (!intr)
2162 		goto err0;
2163 
2164 	sc->urb_int = usb_alloc_urb(0, GFP_KERNEL);
2165 	if (!sc->urb_int)
2166 		goto err1;
2167 
2168 	usb_fill_int_urb(sc->urb_int, sc->usb_dev,
2169 			 usb_rcvintpipe(sc->usb_dev, UEA_INTR_PIPE),
2170 			 intr, size, uea_intr, sc,
2171 			 intf->cur_altsetting->endpoint[0].desc.bInterval);
2172 
2173 	ret = usb_submit_urb(sc->urb_int, GFP_KERNEL);
2174 	if (ret < 0) {
2175 		uea_err(INS_TO_USBDEV(sc),
2176 		       "urb submission failed with error %d\n", ret);
2177 		goto err1;
2178 	}
2179 
2180 	/* Create worker thread, but don't start it here.  Start it after
2181 	 * all usbatm generic initialization is done.
2182 	 */
2183 	sc->kthread = kthread_create(uea_kthread, sc, "ueagle-atm");
2184 	if (IS_ERR(sc->kthread)) {
2185 		uea_err(INS_TO_USBDEV(sc), "failed to create thread\n");
2186 		ret = PTR_ERR(sc->kthread);
2187 		goto err2;
2188 	}
2189 
2190 	uea_leaves(INS_TO_USBDEV(sc));
2191 	return 0;
2192 
2193 err2:
2194 	usb_kill_urb(sc->urb_int);
2195 err1:
2196 	usb_free_urb(sc->urb_int);
2197 	sc->urb_int = NULL;
2198 	kfree(intr);
2199 err0:
2200 	uea_leaves(INS_TO_USBDEV(sc));
2201 	return ret;
2202 }
2203 
2204 /*
2205  * Stop the modem : kill kernel thread and free data
2206  */
2207 static void uea_stop(struct uea_softc *sc)
2208 {
2209 	int ret;
2210 	uea_enters(INS_TO_USBDEV(sc));
2211 	ret = kthread_stop(sc->kthread);
2212 	uea_dbg(INS_TO_USBDEV(sc), "kthread finish with status %d\n", ret);
2213 
2214 	uea_request(sc, UEA_SET_MODE, UEA_LOOPBACK_ON, 0, NULL);
2215 
2216 	usb_kill_urb(sc->urb_int);
2217 	kfree(sc->urb_int->transfer_buffer);
2218 	usb_free_urb(sc->urb_int);
2219 
2220 	/* flush the work item, when no one can schedule it */
2221 	flush_work(&sc->task);
2222 
2223 	release_firmware(sc->dsp_firm);
2224 	uea_leaves(INS_TO_USBDEV(sc));
2225 }
2226 
2227 /* syfs interface */
2228 static struct uea_softc *dev_to_uea(struct device *dev)
2229 {
2230 	struct usb_interface *intf;
2231 	struct usbatm_data *usbatm;
2232 
2233 	intf = to_usb_interface(dev);
2234 	if (!intf)
2235 		return NULL;
2236 
2237 	usbatm = usb_get_intfdata(intf);
2238 	if (!usbatm)
2239 		return NULL;
2240 
2241 	return usbatm->driver_data;
2242 }
2243 
2244 static ssize_t stat_status_show(struct device *dev, struct device_attribute *attr,
2245 		char *buf)
2246 {
2247 	int ret = -ENODEV;
2248 	struct uea_softc *sc;
2249 
2250 	mutex_lock(&uea_mutex);
2251 	sc = dev_to_uea(dev);
2252 	if (!sc)
2253 		goto out;
2254 	ret = sysfs_emit(buf, "%08x\n", sc->stats.phy.state);
2255 out:
2256 	mutex_unlock(&uea_mutex);
2257 	return ret;
2258 }
2259 
2260 static ssize_t stat_status_store(struct device *dev, struct device_attribute *attr,
2261 		const char *buf, size_t count)
2262 {
2263 	int ret = -ENODEV;
2264 	struct uea_softc *sc;
2265 
2266 	mutex_lock(&uea_mutex);
2267 	sc = dev_to_uea(dev);
2268 	if (!sc)
2269 		goto out;
2270 	sc->reset = 1;
2271 	ret = count;
2272 out:
2273 	mutex_unlock(&uea_mutex);
2274 	return ret;
2275 }
2276 
2277 static DEVICE_ATTR_RW(stat_status);
2278 
2279 static ssize_t stat_human_status_show(struct device *dev,
2280 			struct device_attribute *attr, char *buf)
2281 {
2282 	int ret = -ENODEV;
2283 	int modem_state;
2284 	struct uea_softc *sc;
2285 
2286 	mutex_lock(&uea_mutex);
2287 	sc = dev_to_uea(dev);
2288 	if (!sc)
2289 		goto out;
2290 
2291 	if (UEA_CHIP_VERSION(sc) == EAGLE_IV) {
2292 		switch (sc->stats.phy.state) {
2293 		case 0x0:	/* not yet synchronized */
2294 		case 0x1:
2295 		case 0x3:
2296 		case 0x4:
2297 			modem_state = 0;
2298 			break;
2299 		case 0x5:	/* initialization */
2300 		case 0x6:
2301 		case 0x9:
2302 		case 0xa:
2303 			modem_state = 1;
2304 			break;
2305 		case 0x7:	/* operational */
2306 			modem_state = 2;
2307 			break;
2308 		case 0x2:	/* fail ... */
2309 			modem_state = 3;
2310 			break;
2311 		default:	/* unknown */
2312 			modem_state = 4;
2313 			break;
2314 		}
2315 	} else
2316 		modem_state = GET_STATUS(sc->stats.phy.state);
2317 
2318 	switch (modem_state) {
2319 	case 0:
2320 		ret = sysfs_emit(buf, "Modem is booting\n");
2321 		break;
2322 	case 1:
2323 		ret = sysfs_emit(buf, "Modem is initializing\n");
2324 		break;
2325 	case 2:
2326 		ret = sysfs_emit(buf, "Modem is operational\n");
2327 		break;
2328 	case 3:
2329 		ret = sysfs_emit(buf, "Modem synchronization failed\n");
2330 		break;
2331 	default:
2332 		ret = sysfs_emit(buf, "Modem state is unknown\n");
2333 		break;
2334 	}
2335 out:
2336 	mutex_unlock(&uea_mutex);
2337 	return ret;
2338 }
2339 
2340 static DEVICE_ATTR_RO(stat_human_status);
2341 
2342 static ssize_t stat_delin_show(struct device *dev, struct device_attribute *attr,
2343 		char *buf)
2344 {
2345 	int ret = -ENODEV;
2346 	struct uea_softc *sc;
2347 	char *delin = "GOOD";
2348 
2349 	mutex_lock(&uea_mutex);
2350 	sc = dev_to_uea(dev);
2351 	if (!sc)
2352 		goto out;
2353 
2354 	if (UEA_CHIP_VERSION(sc) == EAGLE_IV) {
2355 		if (sc->stats.phy.flags & 0x4000)
2356 			delin = "RESET";
2357 		else if (sc->stats.phy.flags & 0x0001)
2358 			delin = "LOSS";
2359 	} else {
2360 		if (sc->stats.phy.flags & 0x0C00)
2361 			delin = "ERROR";
2362 		else if (sc->stats.phy.flags & 0x0030)
2363 			delin = "LOSS";
2364 	}
2365 
2366 	ret = sysfs_emit(buf, "%s\n", delin);
2367 out:
2368 	mutex_unlock(&uea_mutex);
2369 	return ret;
2370 }
2371 
2372 static DEVICE_ATTR_RO(stat_delin);
2373 
2374 #define UEA_ATTR(name, reset)					\
2375 								\
2376 static ssize_t stat_##name##_show(struct device *dev,		\
2377 		struct device_attribute *attr, char *buf)	\
2378 {								\
2379 	int ret = -ENODEV;					\
2380 	struct uea_softc *sc;					\
2381 								\
2382 	mutex_lock(&uea_mutex);					\
2383 	sc = dev_to_uea(dev);					\
2384 	if (!sc)						\
2385 		goto out;					\
2386 	ret = sysfs_emit(buf, "%08x\n", sc->stats.phy.name);	\
2387 	if (reset)						\
2388 		sc->stats.phy.name = 0;				\
2389 out:								\
2390 	mutex_unlock(&uea_mutex);				\
2391 	return ret;						\
2392 }								\
2393 								\
2394 static DEVICE_ATTR_RO(stat_##name)
2395 
2396 UEA_ATTR(mflags, 1);
2397 UEA_ATTR(vidcpe, 0);
2398 UEA_ATTR(usrate, 0);
2399 UEA_ATTR(dsrate, 0);
2400 UEA_ATTR(usattenuation, 0);
2401 UEA_ATTR(dsattenuation, 0);
2402 UEA_ATTR(usmargin, 0);
2403 UEA_ATTR(dsmargin, 0);
2404 UEA_ATTR(txflow, 0);
2405 UEA_ATTR(rxflow, 0);
2406 UEA_ATTR(uscorr, 0);
2407 UEA_ATTR(dscorr, 0);
2408 UEA_ATTR(usunc, 0);
2409 UEA_ATTR(dsunc, 0);
2410 UEA_ATTR(firmid, 0);
2411 
2412 /* Retrieve the device End System Identifier (MAC) */
2413 
2414 static int uea_getesi(struct uea_softc *sc, u_char *esi)
2415 {
2416 	unsigned char mac_str[2 * ETH_ALEN + 1];
2417 	int i;
2418 	if (usb_string
2419 	    (sc->usb_dev, sc->usb_dev->descriptor.iSerialNumber, mac_str,
2420 	     sizeof(mac_str)) != 2 * ETH_ALEN)
2421 		return 1;
2422 
2423 	for (i = 0; i < ETH_ALEN; i++)
2424 		esi[i] = hex_to_bin(mac_str[2 * i]) * 16 +
2425 			 hex_to_bin(mac_str[2 * i + 1]);
2426 
2427 	return 0;
2428 }
2429 
2430 /* ATM stuff */
2431 static int uea_atm_open(struct usbatm_data *usbatm, struct atm_dev *atm_dev)
2432 {
2433 	struct uea_softc *sc = usbatm->driver_data;
2434 
2435 	return uea_getesi(sc, atm_dev->esi);
2436 }
2437 
2438 static int uea_heavy(struct usbatm_data *usbatm, struct usb_interface *intf)
2439 {
2440 	struct uea_softc *sc = usbatm->driver_data;
2441 
2442 	wait_event_interruptible(sc->sync_q, IS_OPERATIONAL(sc));
2443 
2444 	return 0;
2445 
2446 }
2447 
2448 static int claim_interface(struct usb_device *usb_dev,
2449 			   struct usbatm_data *usbatm, int ifnum)
2450 {
2451 	int ret;
2452 	struct usb_interface *intf = usb_ifnum_to_if(usb_dev, ifnum);
2453 
2454 	if (!intf) {
2455 		uea_err(usb_dev, "interface %d not found\n", ifnum);
2456 		return -ENODEV;
2457 	}
2458 
2459 	ret = usb_driver_claim_interface(&uea_driver, intf, usbatm);
2460 	if (ret != 0)
2461 		uea_err(usb_dev, "can't claim interface %d, error %d\n", ifnum,
2462 		       ret);
2463 	return ret;
2464 }
2465 
2466 static struct attribute *uea_attrs[] = {
2467 	&dev_attr_stat_status.attr,
2468 	&dev_attr_stat_mflags.attr,
2469 	&dev_attr_stat_human_status.attr,
2470 	&dev_attr_stat_delin.attr,
2471 	&dev_attr_stat_vidcpe.attr,
2472 	&dev_attr_stat_usrate.attr,
2473 	&dev_attr_stat_dsrate.attr,
2474 	&dev_attr_stat_usattenuation.attr,
2475 	&dev_attr_stat_dsattenuation.attr,
2476 	&dev_attr_stat_usmargin.attr,
2477 	&dev_attr_stat_dsmargin.attr,
2478 	&dev_attr_stat_txflow.attr,
2479 	&dev_attr_stat_rxflow.attr,
2480 	&dev_attr_stat_uscorr.attr,
2481 	&dev_attr_stat_dscorr.attr,
2482 	&dev_attr_stat_usunc.attr,
2483 	&dev_attr_stat_dsunc.attr,
2484 	&dev_attr_stat_firmid.attr,
2485 	NULL,
2486 };
2487 ATTRIBUTE_GROUPS(uea);
2488 
2489 static int uea_bind(struct usbatm_data *usbatm, struct usb_interface *intf,
2490 		   const struct usb_device_id *id)
2491 {
2492 	struct usb_device *usb = interface_to_usbdev(intf);
2493 	struct uea_softc *sc;
2494 	int ret, ifnum = intf->altsetting->desc.bInterfaceNumber;
2495 	unsigned int alt;
2496 
2497 	uea_enters(usb);
2498 
2499 	/* interface 0 is for firmware/monitoring */
2500 	if (ifnum != UEA_INTR_IFACE_NO)
2501 		return -ENODEV;
2502 
2503 	usbatm->flags = (sync_wait[modem_index] ? 0 : UDSL_SKIP_HEAVY_INIT);
2504 
2505 	/* interface 1 is for outbound traffic */
2506 	ret = claim_interface(usb, usbatm, UEA_US_IFACE_NO);
2507 	if (ret < 0)
2508 		return ret;
2509 
2510 	/* ADI930 has only 2 interfaces and inbound traffic is on interface 1 */
2511 	if (UEA_CHIP_VERSION(id) != ADI930) {
2512 		/* interface 2 is for inbound traffic */
2513 		ret = claim_interface(usb, usbatm, UEA_DS_IFACE_NO);
2514 		if (ret < 0)
2515 			return ret;
2516 	}
2517 
2518 	sc = kzalloc(sizeof(struct uea_softc), GFP_KERNEL);
2519 	if (!sc)
2520 		return -ENOMEM;
2521 
2522 	sc->usb_dev = usb;
2523 	usbatm->driver_data = sc;
2524 	sc->usbatm = usbatm;
2525 	sc->modem_index = (modem_index < NB_MODEM) ? modem_index++ : 0;
2526 	sc->driver_info = id->driver_info;
2527 
2528 	/* first try to use module parameter */
2529 	if (annex[sc->modem_index] == 1)
2530 		sc->annex = ANNEXA;
2531 	else if (annex[sc->modem_index] == 2)
2532 		sc->annex = ANNEXB;
2533 	/* try to autodetect annex */
2534 	else if (sc->driver_info & AUTO_ANNEX_A)
2535 		sc->annex = ANNEXA;
2536 	else if (sc->driver_info & AUTO_ANNEX_B)
2537 		sc->annex = ANNEXB;
2538 	else
2539 		sc->annex = (le16_to_cpu
2540 		(sc->usb_dev->descriptor.bcdDevice) & 0x80) ? ANNEXB : ANNEXA;
2541 
2542 	alt = altsetting[sc->modem_index];
2543 	/* ADI930 don't support iso */
2544 	if (UEA_CHIP_VERSION(id) != ADI930 && alt > 0) {
2545 		if (alt <= 8 &&
2546 			usb_set_interface(usb, UEA_DS_IFACE_NO, alt) == 0) {
2547 			uea_dbg(usb, "set alternate %u for 2 interface\n", alt);
2548 			uea_info(usb, "using iso mode\n");
2549 			usbatm->flags |= UDSL_USE_ISOC | UDSL_IGNORE_EILSEQ;
2550 		} else {
2551 			uea_err(usb, "setting alternate %u failed for "
2552 					"2 interface, using bulk mode\n", alt);
2553 		}
2554 	}
2555 
2556 	ret = uea_boot(sc, intf);
2557 	if (ret < 0)
2558 		goto error;
2559 
2560 	return 0;
2561 
2562 error:
2563 	kfree(sc);
2564 	return ret;
2565 }
2566 
2567 static void uea_unbind(struct usbatm_data *usbatm, struct usb_interface *intf)
2568 {
2569 	struct uea_softc *sc = usbatm->driver_data;
2570 
2571 	uea_stop(sc);
2572 	kfree(sc);
2573 }
2574 
2575 static struct usbatm_driver uea_usbatm_driver = {
2576 	.driver_name = "ueagle-atm",
2577 	.bind = uea_bind,
2578 	.atm_start = uea_atm_open,
2579 	.unbind = uea_unbind,
2580 	.heavy_init = uea_heavy,
2581 	.bulk_in = UEA_BULK_DATA_PIPE,
2582 	.bulk_out = UEA_BULK_DATA_PIPE,
2583 	.isoc_in = UEA_ISO_DATA_PIPE,
2584 };
2585 
2586 static int uea_probe(struct usb_interface *intf, const struct usb_device_id *id)
2587 {
2588 	struct usb_device *usb = interface_to_usbdev(intf);
2589 	int ret;
2590 
2591 	uea_enters(usb);
2592 	uea_info(usb, "ADSL device founded vid (%#X) pid (%#X) Rev (%#X): %s\n",
2593 		le16_to_cpu(usb->descriptor.idVendor),
2594 		le16_to_cpu(usb->descriptor.idProduct),
2595 		le16_to_cpu(usb->descriptor.bcdDevice),
2596 		chip_name[UEA_CHIP_VERSION(id)]);
2597 
2598 	usb_reset_device(usb);
2599 
2600 	if (UEA_IS_PREFIRM(id))
2601 		return uea_load_firmware(usb, UEA_CHIP_VERSION(id));
2602 
2603 	ret = usbatm_usb_probe(intf, id, &uea_usbatm_driver);
2604 	if (ret == 0) {
2605 		struct usbatm_data *usbatm = usb_get_intfdata(intf);
2606 		struct uea_softc *sc = usbatm->driver_data;
2607 
2608 		/* Ensure carrier is initialized to off as early as possible */
2609 		UPDATE_ATM_SIGNAL(ATM_PHY_SIG_LOST);
2610 
2611 		/* Only start the worker thread when all init is done */
2612 		wake_up_process(sc->kthread);
2613 	}
2614 
2615 	return ret;
2616 }
2617 
2618 static void uea_disconnect(struct usb_interface *intf)
2619 {
2620 	struct usb_device *usb = interface_to_usbdev(intf);
2621 	int ifnum = intf->altsetting->desc.bInterfaceNumber;
2622 	uea_enters(usb);
2623 
2624 	/* ADI930 has 2 interfaces and eagle 3 interfaces.
2625 	 * Pre-firmware device has one interface
2626 	 */
2627 	if (usb->config->desc.bNumInterfaces != 1 && ifnum == 0) {
2628 		mutex_lock(&uea_mutex);
2629 		usbatm_usb_disconnect(intf);
2630 		mutex_unlock(&uea_mutex);
2631 		uea_info(usb, "ADSL device removed\n");
2632 	}
2633 
2634 	uea_leaves(usb);
2635 }
2636 
2637 /*
2638  * List of supported VID/PID
2639  */
2640 static const struct usb_device_id uea_ids[] = {
2641 	{USB_DEVICE(ANALOG_VID,	ADI930_PID_PREFIRM),
2642 		.driver_info = ADI930 | PREFIRM},
2643 	{USB_DEVICE(ANALOG_VID,	ADI930_PID_PSTFIRM),
2644 		.driver_info = ADI930 | PSTFIRM},
2645 	{USB_DEVICE(ANALOG_VID,	EAGLE_I_PID_PREFIRM),
2646 		.driver_info = EAGLE_I | PREFIRM},
2647 	{USB_DEVICE(ANALOG_VID,	EAGLE_I_PID_PSTFIRM),
2648 		.driver_info = EAGLE_I | PSTFIRM},
2649 	{USB_DEVICE(ANALOG_VID,	EAGLE_II_PID_PREFIRM),
2650 		.driver_info = EAGLE_II | PREFIRM},
2651 	{USB_DEVICE(ANALOG_VID,	EAGLE_II_PID_PSTFIRM),
2652 		.driver_info = EAGLE_II | PSTFIRM},
2653 	{USB_DEVICE(ANALOG_VID,	EAGLE_IIC_PID_PREFIRM),
2654 		.driver_info = EAGLE_II | PREFIRM},
2655 	{USB_DEVICE(ANALOG_VID,	EAGLE_IIC_PID_PSTFIRM),
2656 		.driver_info = EAGLE_II | PSTFIRM},
2657 	{USB_DEVICE(ANALOG_VID,	EAGLE_III_PID_PREFIRM),
2658 		.driver_info = EAGLE_III | PREFIRM},
2659 	{USB_DEVICE(ANALOG_VID,	EAGLE_III_PID_PSTFIRM),
2660 		.driver_info = EAGLE_III | PSTFIRM},
2661 	{USB_DEVICE(ANALOG_VID,	EAGLE_IV_PID_PREFIRM),
2662 		.driver_info = EAGLE_IV | PREFIRM},
2663 	{USB_DEVICE(ANALOG_VID,	EAGLE_IV_PID_PSTFIRM),
2664 		.driver_info = EAGLE_IV | PSTFIRM},
2665 	{USB_DEVICE(DEVOLO_VID,	DEVOLO_EAGLE_I_A_PID_PREFIRM),
2666 		.driver_info = EAGLE_I | PREFIRM},
2667 	{USB_DEVICE(DEVOLO_VID,	DEVOLO_EAGLE_I_A_PID_PSTFIRM),
2668 		.driver_info = EAGLE_I | PSTFIRM | AUTO_ANNEX_A},
2669 	{USB_DEVICE(DEVOLO_VID,	DEVOLO_EAGLE_I_B_PID_PREFIRM),
2670 		.driver_info = EAGLE_I | PREFIRM},
2671 	{USB_DEVICE(DEVOLO_VID,	DEVOLO_EAGLE_I_B_PID_PSTFIRM),
2672 		.driver_info = EAGLE_I | PSTFIRM | AUTO_ANNEX_B},
2673 	{USB_DEVICE(DEVOLO_VID,	DEVOLO_EAGLE_II_A_PID_PREFIRM),
2674 		.driver_info = EAGLE_II | PREFIRM},
2675 	{USB_DEVICE(DEVOLO_VID,	DEVOLO_EAGLE_II_A_PID_PSTFIRM),
2676 		.driver_info = EAGLE_II | PSTFIRM | AUTO_ANNEX_A},
2677 	{USB_DEVICE(DEVOLO_VID,	DEVOLO_EAGLE_II_B_PID_PREFIRM),
2678 		.driver_info = EAGLE_II | PREFIRM},
2679 	{USB_DEVICE(DEVOLO_VID,	DEVOLO_EAGLE_II_B_PID_PSTFIRM),
2680 		.driver_info = EAGLE_II | PSTFIRM | AUTO_ANNEX_B},
2681 	{USB_DEVICE(ELSA_VID,	ELSA_PID_PREFIRM),
2682 		.driver_info = ADI930 | PREFIRM},
2683 	{USB_DEVICE(ELSA_VID,	ELSA_PID_PSTFIRM),
2684 		.driver_info = ADI930 | PSTFIRM},
2685 	{USB_DEVICE(ELSA_VID,	ELSA_PID_A_PREFIRM),
2686 		.driver_info = ADI930 | PREFIRM},
2687 	{USB_DEVICE(ELSA_VID,	ELSA_PID_A_PSTFIRM),
2688 		.driver_info = ADI930 | PSTFIRM | AUTO_ANNEX_A},
2689 	{USB_DEVICE(ELSA_VID,	ELSA_PID_B_PREFIRM),
2690 		.driver_info = ADI930 | PREFIRM},
2691 	{USB_DEVICE(ELSA_VID,	ELSA_PID_B_PSTFIRM),
2692 		.driver_info = ADI930 | PSTFIRM | AUTO_ANNEX_B},
2693 	{USB_DEVICE(USR_VID,	MILLER_A_PID_PREFIRM),
2694 		.driver_info = EAGLE_I | PREFIRM},
2695 	{USB_DEVICE(USR_VID,	MILLER_A_PID_PSTFIRM),
2696 		.driver_info = EAGLE_I | PSTFIRM  | AUTO_ANNEX_A},
2697 	{USB_DEVICE(USR_VID,	MILLER_B_PID_PREFIRM),
2698 		.driver_info = EAGLE_I | PREFIRM},
2699 	{USB_DEVICE(USR_VID,	MILLER_B_PID_PSTFIRM),
2700 		.driver_info = EAGLE_I | PSTFIRM  | AUTO_ANNEX_B},
2701 	{USB_DEVICE(USR_VID,	HEINEKEN_A_PID_PREFIRM),
2702 		.driver_info = EAGLE_I | PREFIRM},
2703 	{USB_DEVICE(USR_VID,	HEINEKEN_A_PID_PSTFIRM),
2704 		.driver_info = EAGLE_I | PSTFIRM | AUTO_ANNEX_A},
2705 	{USB_DEVICE(USR_VID,	HEINEKEN_B_PID_PREFIRM),
2706 		.driver_info = EAGLE_I | PREFIRM},
2707 	{USB_DEVICE(USR_VID,	HEINEKEN_B_PID_PSTFIRM),
2708 		.driver_info = EAGLE_I | PSTFIRM | AUTO_ANNEX_B},
2709 	{}
2710 };
2711 
2712 /*
2713  * USB driver descriptor
2714  */
2715 static struct usb_driver uea_driver = {
2716 	.name = "ueagle-atm",
2717 	.id_table = uea_ids,
2718 	.probe = uea_probe,
2719 	.disconnect = uea_disconnect,
2720 	.dev_groups = uea_groups,
2721 };
2722 
2723 MODULE_DEVICE_TABLE(usb, uea_ids);
2724 
2725 module_usb_driver(uea_driver);
2726 
2727 MODULE_AUTHOR("Damien Bergamini/Matthieu Castet/Stanislaw W. Gruszka");
2728 MODULE_DESCRIPTION("ADI 930/Eagle USB ADSL Modem driver");
2729 MODULE_LICENSE("Dual BSD/GPL");
2730 MODULE_FIRMWARE(EAGLE_FIRMWARE);
2731 MODULE_FIRMWARE(ADI930_FIRMWARE);
2732 MODULE_FIRMWARE(EAGLE_I_FIRMWARE);
2733 MODULE_FIRMWARE(EAGLE_II_FIRMWARE);
2734 MODULE_FIRMWARE(EAGLE_III_FIRMWARE);
2735 MODULE_FIRMWARE(EAGLE_IV_FIRMWARE);
2736 MODULE_FIRMWARE(DSP4I_FIRMWARE);
2737 MODULE_FIRMWARE(DSP4P_FIRMWARE);
2738 MODULE_FIRMWARE(DSP9I_FIRMWARE);
2739 MODULE_FIRMWARE(DSP9P_FIRMWARE);
2740 MODULE_FIRMWARE(DSPEI_FIRMWARE);
2741 MODULE_FIRMWARE(DSPEP_FIRMWARE);
2742 MODULE_FIRMWARE(FPGA930_FIRMWARE);
2743 MODULE_FIRMWARE(CMV4P_FIRMWARE);
2744 MODULE_FIRMWARE(CMV4PV2_FIRMWARE);
2745 MODULE_FIRMWARE(CMV4I_FIRMWARE);
2746 MODULE_FIRMWARE(CMV4IV2_FIRMWARE);
2747 MODULE_FIRMWARE(CMV9P_FIRMWARE);
2748 MODULE_FIRMWARE(CMV9PV2_FIRMWARE);
2749 MODULE_FIRMWARE(CMV9I_FIRMWARE);
2750 MODULE_FIRMWARE(CMV9IV2_FIRMWARE);
2751 MODULE_FIRMWARE(CMVEP_FIRMWARE);
2752 MODULE_FIRMWARE(CMVEPV2_FIRMWARE);
2753 MODULE_FIRMWARE(CMVEI_FIRMWARE);
2754 MODULE_FIRMWARE(CMVEIV2_FIRMWARE);
2755