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