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