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