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