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