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