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