linux 2.6.16.38 w/ vs2.0.3-rc1
[linux-2.6.git] / drivers / usb / atm / ueagle-atm.c
1 /*-
2  * Copyright (c) 2003, 2004
3  *      Damien Bergamini <damien.bergamini@free.fr>. All rights reserved.
4  *
5  * Copyright (c) 2005 Matthieu Castet <castet.matthieu@free.fr>
6  *
7  * This software is available to you under a choice of one of two
8  * licenses. You may choose to be licensed under the terms of the GNU
9  * General Public License (GPL) Version 2, available from the file
10  * COPYING in the main directory of this source tree, or the
11  * BSD license below:
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice unmodified, this list of conditions, and the following
18  *    disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  * GPL license :
36  * This program is free software; you can redistribute it and/or
37  * modify it under the terms of the GNU General Public License
38  * as published by the Free Software Foundation; either version 2
39  * of the License, or (at your option) any later version.
40  *
41  * This program is distributed in the hope that it will be useful,
42  * but WITHOUT ANY WARRANTY; without even the implied warranty of
43  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
44  * GNU General Public License for more details.
45  *
46  * You should have received a copy of the GNU General Public License
47  * along with this program; if not, write to the Free Software
48  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
49  *
50  *
51  * HISTORY : some part of the code was base on ueagle 1.3 BSD driver,
52  * Damien Bergamini agree to put his code under a DUAL GPL/BSD license.
53  *
54  * The rest of the code was was rewritten from scratch.
55  */
56
57 #include <linux/module.h>
58 #include <linux/moduleparam.h>
59 #include <linux/init.h>
60 #include <linux/crc32.h>
61 #include <linux/usb.h>
62 #include <linux/firmware.h>
63 #include <linux/ctype.h>
64 #include <linux/kthread.h>
65 #include <linux/version.h>
66 #include <linux/mutex.h>
67 #include <asm/unaligned.h>
68
69 #include "usbatm.h"
70
71 #define EAGLEUSBVERSION "ueagle 1.2"
72
73
74 /*
75  * Debug macros
76  */
77 #define uea_dbg(usb_dev, format, args...)       \
78         do { \
79                 if (debug >= 1) \
80                         dev_dbg(&(usb_dev)->dev, \
81                                 "[ueagle-atm dbg] %s: " format, \
82                                         __FUNCTION__, ##args); \
83        } while (0)
84
85 #define uea_vdbg(usb_dev, format, args...)      \
86         do { \
87                 if (debug >= 2) \
88                         dev_dbg(&(usb_dev)->dev, \
89                                 "[ueagle-atm vdbg]  " format, ##args); \
90        } while (0)
91
92 #define uea_enters(usb_dev) \
93         uea_vdbg(usb_dev, "entering %s\n", __FUNCTION__)
94
95 #define uea_leaves(usb_dev) \
96         uea_vdbg(usb_dev, "leaving  %s\n", __FUNCTION__)
97
98 #define uea_err(usb_dev, format,args...) \
99         dev_err(&(usb_dev)->dev ,"[UEAGLE-ATM] " format , ##args)
100
101 #define uea_warn(usb_dev, format,args...) \
102         dev_warn(&(usb_dev)->dev ,"[Ueagle-atm] " format, ##args)
103
104 #define uea_info(usb_dev, format,args...) \
105         dev_info(&(usb_dev)->dev ,"[ueagle-atm] " format, ##args)
106
107 struct uea_cmvs {
108         u32 address;
109         u16 offset;
110         u32 data;
111 } __attribute__ ((packed));
112
113 struct uea_softc {
114         struct usb_device *usb_dev;
115         struct usbatm_data *usbatm;
116
117         int modem_index;
118         unsigned int driver_info;
119
120         int booting;
121         int reset;
122
123         wait_queue_head_t sync_q;
124
125         struct task_struct *kthread;
126         u32 data;
127         wait_queue_head_t cmv_ack_wait;
128         int cmv_ack;
129
130         struct work_struct task;
131         u16 pageno;
132         u16 ovl;
133
134         const struct firmware *dsp_firm;
135         struct urb *urb_int;
136
137         u8 cmv_function;
138         u16 cmv_idx;
139         u32 cmv_address;
140         u16 cmv_offset;
141
142         /* keep in sync with eaglectl */
143         struct uea_stats {
144                 struct {
145                         u32 state;
146                         u32 flags;
147                         u32 mflags;
148                         u32 vidcpe;
149                         u32 vidco;
150                         u32 dsrate;
151                         u32 usrate;
152                         u32 dsunc;
153                         u32 usunc;
154                         u32 dscorr;
155                         u32 uscorr;
156                         u32 txflow;
157                         u32 rxflow;
158                         u32 usattenuation;
159                         u32 dsattenuation;
160                         u32 dsmargin;
161                         u32 usmargin;
162                         u32 firmid;
163                 } phy;
164         } stats;
165 };
166
167 /*
168  * Elsa IDs
169  */
170 #define ELSA_VID                0x05CC
171 #define ELSA_PID_PSTFIRM        0x3350
172 #define ELSA_PID_PREFIRM        0x3351
173
174 /*
175  * Sagem USB IDs
176  */
177 #define EAGLE_VID               0x1110
178 #define EAGLE_I_PID_PREFIRM     0x9010  /* Eagle I */
179 #define EAGLE_I_PID_PSTFIRM     0x900F  /* Eagle I */
180
181 #define EAGLE_IIC_PID_PREFIRM   0x9024  /* Eagle IIC */
182 #define EAGLE_IIC_PID_PSTFIRM   0x9023  /* Eagle IIC */
183
184 #define EAGLE_II_PID_PREFIRM    0x9022  /* Eagle II */
185 #define EAGLE_II_PID_PSTFIRM    0x9021  /* Eagle II */
186
187 /*
188  *  Eagle III Pid
189  */
190 #define EAGLE_III_PID_PREFIRM   0x9032  /* Eagle III */
191 #define EAGLE_III_PID_PSTFIRM   0x9031  /* Eagle III */
192
193 /*
194  * USR USB IDs
195  */
196 #define USR_VID                 0x0BAF
197 #define MILLER_A_PID_PREFIRM    0x00F2
198 #define MILLER_A_PID_PSTFIRM    0x00F1
199 #define MILLER_B_PID_PREFIRM    0x00FA
200 #define MILLER_B_PID_PSTFIRM    0x00F9
201 #define HEINEKEN_A_PID_PREFIRM  0x00F6
202 #define HEINEKEN_A_PID_PSTFIRM  0x00F5
203 #define HEINEKEN_B_PID_PREFIRM  0x00F8
204 #define HEINEKEN_B_PID_PSTFIRM  0x00F7
205
206 #define PREFIRM 0
207 #define PSTFIRM (1<<7)
208 enum {
209         ADI930 = 0,
210         EAGLE_I,
211         EAGLE_II,
212         EAGLE_III
213 };
214
215 /* macros for both struct usb_device_id and struct uea_softc */
216 #define UEA_IS_PREFIRM(x) \
217         (!((x)->driver_info & PSTFIRM))
218 #define UEA_CHIP_VERSION(x) \
219         ((x)->driver_info & 0xf)
220
221 #define IS_ISDN(sc) \
222         (le16_to_cpu(sc->usb_dev->descriptor.bcdDevice) & 0x80)
223
224 #define INS_TO_USBDEV(ins) ins->usb_dev
225
226 #define GET_STATUS(data) \
227         ((data >> 8) & 0xf)
228 #define IS_OPERATIONAL(sc) \
229         (GET_STATUS(sc->stats.phy.state) == 2)
230
231 /*
232  * Set of macros to handle unaligned data in the firmware blob.
233  * The FW_GET_BYTE() macro is provided only for consistency.
234  */
235
236 #define FW_GET_BYTE(p)  *((__u8 *) (p))
237 #define FW_GET_WORD(p)  le16_to_cpu(get_unaligned((__le16 *) (p)))
238 #define FW_GET_LONG(p)  le32_to_cpu(get_unaligned((__le32 *) (p)))
239
240 #define FW_DIR "ueagle-atm/"
241 #define NB_MODEM 4
242
243 #define BULK_TIMEOUT 300
244 #define CTRL_TIMEOUT 1000
245
246 #define ACK_TIMEOUT msecs_to_jiffies(1500)
247
248 #define UEA_INTR_IFACE_NO       0
249 #define UEA_US_IFACE_NO         1
250 #define UEA_DS_IFACE_NO         2
251
252 #define FASTEST_ISO_INTF        8
253
254 #define UEA_BULK_DATA_PIPE      0x02
255 #define UEA_IDMA_PIPE           0x04
256 #define UEA_INTR_PIPE           0x04
257 #define UEA_ISO_DATA_PIPE       0x08
258
259 #define UEA_SET_BLOCK           0x0001
260 #define UEA_SET_MODE            0x0003
261 #define UEA_SET_2183_DATA       0x0004
262 #define UEA_SET_TIMEOUT         0x0011
263
264 #define UEA_LOOPBACK_OFF        0x0002
265 #define UEA_LOOPBACK_ON         0x0003
266 #define UEA_BOOT_IDMA           0x0006
267 #define UEA_START_RESET         0x0007
268 #define UEA_END_RESET           0x0008
269
270 #define UEA_SWAP_MAILBOX        (0x3fcd | 0x4000)
271 #define UEA_MPTX_START          (0x3fce | 0x4000)
272 #define UEA_MPTX_MAILBOX        (0x3fd6 | 0x4000)
273 #define UEA_MPRX_MAILBOX        (0x3fdf | 0x4000)
274
275 /* structure describing a block within a DSP page */
276 struct block_info {
277         __le16 wHdr;
278 #define UEA_BIHDR 0xabcd
279         __le16 wAddress;
280         __le16 wSize;
281         __le16 wOvlOffset;
282         __le16 wOvl;            /* overlay */
283         __le16 wLast;
284 } __attribute__ ((packed));
285 #define BLOCK_INFO_SIZE 12
286
287 /* structure representing a CMV (Configuration and Management Variable) */
288 struct cmv {
289         __le16 wPreamble;
290 #define PREAMBLE 0x535c
291         __u8 bDirection;
292 #define MODEMTOHOST 0x01
293 #define HOSTTOMODEM 0x10
294         __u8 bFunction;
295 #define FUNCTION_TYPE(f)    ((f) >> 4)
296 #define MEMACCESS       0x1
297 #define ADSLDIRECTIVE   0x7
298
299 #define FUNCTION_SUBTYPE(f) ((f) & 0x0f)
300 /* for MEMACCESS */
301 #define REQUESTREAD     0x0
302 #define REQUESTWRITE    0x1
303 #define REPLYREAD       0x2
304 #define REPLYWRITE      0x3
305 /* for ADSLDIRECTIVE */
306 #define KERNELREADY     0x0
307 #define MODEMREADY      0x1
308
309 #define MAKEFUNCTION(t, s) (((t) & 0xf) << 4 | ((s) & 0xf))
310         __le16 wIndex;
311         __le32 dwSymbolicAddress;
312 #define MAKESA(a, b, c, d)                                              \
313         (((c) & 0xff) << 24 |                                           \
314          ((d) & 0xff) << 16 |                                           \
315          ((a) & 0xff) << 8  |                                           \
316          ((b) & 0xff))
317
318 #define SA_CNTL MAKESA('C', 'N', 'T', 'L')
319 #define SA_DIAG MAKESA('D', 'I', 'A', 'G')
320 #define SA_INFO MAKESA('I', 'N', 'F', 'O')
321 #define SA_OPTN MAKESA('O', 'P', 'T', 'N')
322 #define SA_RATE MAKESA('R', 'A', 'T', 'E')
323 #define SA_STAT MAKESA('S', 'T', 'A', 'T')
324         __le16 wOffsetAddress;
325         __le32 dwData;
326 } __attribute__ ((packed));
327 #define CMV_SIZE 16
328
329 /* structure representing swap information */
330 struct swap_info {
331         __u8 bSwapPageNo;
332         __u8 bOvl;              /* overlay */
333 } __attribute__ ((packed));
334
335 /* structure representing interrupt data */
336 struct intr_pkt {
337         __u8 bType;
338         __u8 bNotification;
339         __le16 wValue;
340         __le16 wIndex;
341         __le16 wLength;
342         __le16 wInterrupt;
343 #define INT_LOADSWAPPAGE 0x0001
344 #define INT_INCOMINGCMV  0x0002
345         union {
346                 struct {
347                         struct swap_info swapinfo;
348                         __le16 wDataSize;
349                 } __attribute__ ((packed)) s1;
350
351                 struct {
352                         struct cmv cmv;
353                         __le16 wDataSize;
354                 } __attribute__ ((packed)) s2;
355         } __attribute__ ((packed)) u;
356 #define bSwapPageNo     u.s1.swapinfo.bSwapPageNo
357 #define bOvl            u.s1.swapinfo.bOvl
358 } __attribute__ ((packed));
359 #define INTR_PKT_SIZE 28
360
361 static struct usb_driver uea_driver;
362 static DEFINE_MUTEX(uea_mutex);
363 static const char *chip_name[] = {"ADI930", "Eagle I", "Eagle II", "Eagle III"};
364
365 static int modem_index;
366 static unsigned int debug;
367 static int use_iso[NB_MODEM] = {[0 ... (NB_MODEM - 1)] = 1};
368 static int sync_wait[NB_MODEM];
369 static char *cmv_file[NB_MODEM];
370
371 module_param(debug, uint, 0644);
372 MODULE_PARM_DESC(debug, "module debug level (0=off,1=on,2=verbose)");
373 module_param_array(use_iso, bool, NULL, 0644);
374 MODULE_PARM_DESC(use_iso, "use isochronous usb pipe for incoming traffic");
375 module_param_array(sync_wait, bool, NULL, 0644);
376 MODULE_PARM_DESC(sync_wait, "wait the synchronisation before starting ATM");
377 module_param_array(cmv_file, charp, NULL, 0644);
378 MODULE_PARM_DESC(cmv_file,
379                 "file name with configuration and management variables");
380
381 #define UPDATE_ATM_STAT(type, val) \
382         do { \
383                 if (sc->usbatm->atm_dev) \
384                         sc->usbatm->atm_dev->type = val; \
385         } while (0)
386
387 /* Firmware loading */
388 #define LOAD_INTERNAL     0xA0
389 #define F8051_USBCS       0x7f92
390
391 /**
392  * uea_send_modem_cmd - Send a command for pre-firmware devices.
393  */
394 static int uea_send_modem_cmd(struct usb_device *usb,
395                 u16 addr, u16 size, u8 * buff)
396 {
397         int ret = -ENOMEM;
398         u8 *xfer_buff;
399
400         xfer_buff = kmalloc(size, GFP_KERNEL);
401         if (xfer_buff) {
402                 memcpy(xfer_buff, buff, size);
403                 ret = usb_control_msg(usb,
404                                       usb_sndctrlpipe(usb, 0),
405                                       LOAD_INTERNAL,
406                                       USB_DIR_OUT | USB_TYPE_VENDOR |
407                                       USB_RECIP_DEVICE, addr, 0, xfer_buff,
408                                       size, CTRL_TIMEOUT);
409                 kfree(xfer_buff);
410         }
411
412         if (ret < 0)
413                 return ret;
414
415         return (ret == size) ? 0 : -EIO;
416 }
417
418 static void uea_upload_pre_firmware(const struct firmware *fw_entry, void *context)
419 {
420         struct usb_device *usb = context;
421         u8 *pfw, value;
422         u32 crc = 0;
423         int ret, size;
424
425         uea_enters(usb);
426         if (!fw_entry) {
427                 uea_err(usb, "firmware is not available\n");
428                 goto err;
429         }
430
431         pfw = fw_entry->data;
432         size = fw_entry->size;
433         if (size < 4)
434                 goto err_fw_corrupted;
435
436         crc = FW_GET_LONG(pfw);
437         pfw += 4;
438         size -= 4;
439         if (crc32_be(0, pfw, size) != crc)
440                 goto err_fw_corrupted;
441
442         /*
443          * Start to upload formware : send reset
444          */
445         value = 1;
446         ret = uea_send_modem_cmd(usb, F8051_USBCS, sizeof(value), &value);
447
448         if (ret < 0) {
449                 uea_err(usb, "modem reset failed with error %d\n", ret);
450                 goto err;
451         }
452
453         while (size > 3) {
454                 u8 len = FW_GET_BYTE(pfw);
455                 u16 add = FW_GET_WORD(pfw + 1);
456
457                 size -= len + 3;
458                 if (size < 0)
459                         goto err_fw_corrupted;
460
461                 ret = uea_send_modem_cmd(usb, add, len, pfw + 3);
462                 if (ret < 0) {
463                         uea_err(usb, "uploading firmware data failed "
464                                         "with error %d\n", ret);
465                         goto err;
466                 }
467                 pfw += len + 3;
468         }
469
470         if (size != 0)
471                 goto err_fw_corrupted;
472
473         /*
474          * Tell the modem we finish : de-assert reset
475          */
476         value = 0;
477         ret = uea_send_modem_cmd(usb, F8051_USBCS, 1, &value);
478         if (ret < 0)
479                 uea_err(usb, "modem de-assert failed with error %d\n", ret);
480         else
481                 uea_info(usb, "firmware uploaded\n");
482
483         uea_leaves(usb);
484         return;
485
486 err_fw_corrupted:
487         uea_err(usb, "firmware is corrupted\n");
488 err:
489         uea_leaves(usb);
490 }
491
492 /**
493  * uea_load_firmware - Load usb firmware for pre-firmware devices.
494  */
495 static int uea_load_firmware(struct usb_device *usb, unsigned int ver)
496 {
497         int ret;
498         char *fw_name = FW_DIR "eagle.fw";
499
500         uea_enters(usb);
501         uea_info(usb, "pre-firmware device, uploading firmware\n");
502
503         switch (ver) {
504         case ADI930:
505                 fw_name = FW_DIR "adi930.fw";
506                 break;
507         case EAGLE_I:
508                 fw_name = FW_DIR "eagleI.fw";
509                 break;
510         case EAGLE_II:
511                 fw_name = FW_DIR "eagleII.fw";
512                 break;
513         case EAGLE_III:
514                 fw_name = FW_DIR "eagleIII.fw";
515                 break;
516         }
517
518         ret = request_firmware_nowait(THIS_MODULE, 1, fw_name, &usb->dev, usb, uea_upload_pre_firmware);
519         if (ret)
520                 uea_err(usb, "firmware %s is not available\n", fw_name);
521         else
522                 uea_info(usb, "loading firmware %s\n", fw_name);
523
524         uea_leaves(usb);
525         return ret;
526 }
527
528 /* modem management : dsp firmware, send/read CMV, monitoring statistic
529  */
530
531 /*
532  * Make sure that the DSP code provided is safe to use.
533  */
534 static int check_dsp(u8 *dsp, unsigned int len)
535 {
536         u8 pagecount, blockcount;
537         u16 blocksize;
538         u32 pageoffset;
539         unsigned int i, j, p, pp;
540
541         pagecount = FW_GET_BYTE(dsp);
542         p = 1;
543
544         /* enough space for page offsets? */
545         if (p + 4 * pagecount > len)
546                 return 1;
547
548         for (i = 0; i < pagecount; i++) {
549
550                 pageoffset = FW_GET_LONG(dsp + p);
551                 p += 4;
552
553                 if (pageoffset == 0)
554                         continue;
555
556                 /* enough space for blockcount? */
557                 if (pageoffset >= len)
558                         return 1;
559
560                 pp = pageoffset;
561                 blockcount = FW_GET_BYTE(dsp + pp);
562                 pp += 1;
563
564                 for (j = 0; j < blockcount; j++) {
565
566                         /* enough space for block header? */
567                         if (pp + 4 > len)
568                                 return 1;
569
570                         pp += 2;        /* skip blockaddr */
571                         blocksize = FW_GET_WORD(dsp + pp);
572                         pp += 2;
573
574                         /* enough space for block data? */
575                         if (pp + blocksize > len)
576                                 return 1;
577
578                         pp += blocksize;
579                 }
580         }
581
582         return 0;
583 }
584
585 /*
586  * send data to the idma pipe
587  * */
588 static int uea_idma_write(struct uea_softc *sc, void *data, u32 size)
589 {
590         int ret = -ENOMEM;
591         u8 *xfer_buff;
592         int bytes_read;
593
594         xfer_buff = kmalloc(size, GFP_KERNEL);
595         if (!xfer_buff) {
596                 uea_err(INS_TO_USBDEV(sc), "can't allocate xfer_buff\n");
597                 return ret;
598         }
599
600         memcpy(xfer_buff, data, size);
601
602         ret = usb_bulk_msg(sc->usb_dev,
603                          usb_sndbulkpipe(sc->usb_dev, UEA_IDMA_PIPE),
604                          xfer_buff, size, &bytes_read, BULK_TIMEOUT);
605
606         kfree(xfer_buff);
607         if (ret < 0)
608                 return ret;
609         if (size != bytes_read) {
610                 uea_err(INS_TO_USBDEV(sc), "size != bytes_read %d %d\n", size,
611                        bytes_read);
612                 return -EIO;
613         }
614
615         return 0;
616 }
617
618 static int request_dsp(struct uea_softc *sc)
619 {
620         int ret;
621         char *dsp_name;
622
623         if (UEA_CHIP_VERSION(sc) == ADI930) {
624                 if (IS_ISDN(sc))
625                         dsp_name = FW_DIR "DSP9i.bin";
626                 else
627                         dsp_name = FW_DIR "DSP9p.bin";
628         } else {
629                 if (IS_ISDN(sc))
630                         dsp_name = FW_DIR "DSPei.bin";
631                 else
632                         dsp_name = FW_DIR "DSPep.bin";
633         }
634
635         ret = request_firmware(&sc->dsp_firm, dsp_name, &sc->usb_dev->dev);
636         if (ret < 0) {
637                 uea_err(INS_TO_USBDEV(sc),
638                        "requesting firmware %s failed with error %d\n",
639                        dsp_name, ret);
640                 return ret;
641         }
642
643         if (check_dsp(sc->dsp_firm->data, sc->dsp_firm->size)) {
644                 uea_err(INS_TO_USBDEV(sc), "firmware %s is corrupted\n",
645                        dsp_name);
646                 release_firmware(sc->dsp_firm);
647                 sc->dsp_firm = NULL;
648                 return -EILSEQ;
649         }
650
651         return 0;
652 }
653
654 /*
655  * The uea_load_page() function must be called within a process context
656  */
657 static void uea_load_page(void *xsc)
658 {
659         struct uea_softc *sc = xsc;
660         u16 pageno = sc->pageno;
661         u16 ovl = sc->ovl;
662         struct block_info bi;
663
664         u8 *p;
665         u8 pagecount, blockcount;
666         u16 blockaddr, blocksize;
667         u32 pageoffset;
668         int i;
669
670         /* reload firmware when reboot start and it's loaded already */
671         if (ovl == 0 && pageno == 0 && sc->dsp_firm) {
672                 release_firmware(sc->dsp_firm);
673                 sc->dsp_firm = NULL;
674         }
675
676         if (sc->dsp_firm == NULL && request_dsp(sc) < 0)
677                 return;
678
679         p = sc->dsp_firm->data;
680         pagecount = FW_GET_BYTE(p);
681         p += 1;
682
683         if (pageno >= pagecount)
684                 goto bad1;
685
686         p += 4 * pageno;
687         pageoffset = FW_GET_LONG(p);
688
689         if (pageoffset == 0)
690                 goto bad1;
691
692         p = sc->dsp_firm->data + pageoffset;
693         blockcount = FW_GET_BYTE(p);
694         p += 1;
695
696         uea_dbg(INS_TO_USBDEV(sc),
697                "sending %u blocks for DSP page %u\n", blockcount, pageno);
698
699         bi.wHdr = cpu_to_le16(UEA_BIHDR);
700         bi.wOvl = cpu_to_le16(ovl);
701         bi.wOvlOffset = cpu_to_le16(ovl | 0x8000);
702
703         for (i = 0; i < blockcount; i++) {
704                 blockaddr = FW_GET_WORD(p);
705                 p += 2;
706
707                 blocksize = FW_GET_WORD(p);
708                 p += 2;
709
710                 bi.wSize = cpu_to_le16(blocksize);
711                 bi.wAddress = cpu_to_le16(blockaddr);
712                 bi.wLast = cpu_to_le16((i == blockcount - 1) ? 1 : 0);
713
714                 /* send block info through the IDMA pipe */
715                 if (uea_idma_write(sc, &bi, BLOCK_INFO_SIZE))
716                         goto bad2;
717
718                 /* send block data through the IDMA pipe */
719                 if (uea_idma_write(sc, p, blocksize))
720                         goto bad2;
721
722                 p += blocksize;
723         }
724
725         return;
726
727 bad2:
728         uea_err(INS_TO_USBDEV(sc), "sending DSP block %u failed\n", i);
729         return;
730 bad1:
731         uea_err(INS_TO_USBDEV(sc), "invalid DSP page %u requested\n",pageno);
732 }
733
734 static inline void wake_up_cmv_ack(struct uea_softc *sc)
735 {
736         sc->cmv_ack = 1;
737         wake_up(&sc->cmv_ack_wait);
738 }
739
740 static inline int wait_cmv_ack(struct uea_softc *sc)
741 {
742         int ret = wait_event_timeout(sc->cmv_ack_wait,
743                                                    sc->cmv_ack, ACK_TIMEOUT);
744         sc->cmv_ack = 0;
745
746         if (ret < 0)
747                 return ret;
748
749         return (ret == 0) ? -ETIMEDOUT : 0;
750 }
751
752 #define UCDC_SEND_ENCAPSULATED_COMMAND 0x00
753
754 static int uea_request(struct uea_softc *sc,
755                 u16 value, u16 index, u16 size, void *data)
756 {
757         u8 *xfer_buff;
758         int ret = -ENOMEM;
759
760         xfer_buff = kmalloc(size, GFP_KERNEL);
761         if (!xfer_buff) {
762                 uea_err(INS_TO_USBDEV(sc), "can't allocate xfer_buff\n");
763                 return ret;
764         }
765         memcpy(xfer_buff, data, size);
766
767         ret = usb_control_msg(sc->usb_dev, usb_sndctrlpipe(sc->usb_dev, 0),
768                               UCDC_SEND_ENCAPSULATED_COMMAND,
769                               USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
770                               value, index, xfer_buff, size, CTRL_TIMEOUT);
771
772         kfree(xfer_buff);
773         if (ret < 0) {
774                 uea_err(INS_TO_USBDEV(sc), "usb_control_msg error %d\n", ret);
775                 return ret;
776         }
777
778         if (ret != size) {
779                 uea_err(INS_TO_USBDEV(sc),
780                        "usb_control_msg send only %d bytes (instead of %d)\n",
781                        ret, size);
782                 return -EIO;
783         }
784
785         return 0;
786 }
787
788 static int uea_cmv(struct uea_softc *sc,
789                 u8 function, u32 address, u16 offset, u32 data)
790 {
791         struct cmv cmv;
792         int ret;
793
794         /* we send a request, but we expect a reply */
795         sc->cmv_function = function | 0x2;
796         sc->cmv_idx++;
797         sc->cmv_address = address;
798         sc->cmv_offset = offset;
799
800         cmv.wPreamble = cpu_to_le16(PREAMBLE);
801         cmv.bDirection = HOSTTOMODEM;
802         cmv.bFunction = function;
803         cmv.wIndex = cpu_to_le16(sc->cmv_idx);
804         put_unaligned(cpu_to_le32(address), &cmv.dwSymbolicAddress);
805         cmv.wOffsetAddress = cpu_to_le16(offset);
806         put_unaligned(cpu_to_le32(data >> 16 | data << 16), &cmv.dwData);
807
808         ret = uea_request(sc, UEA_SET_BLOCK, UEA_MPTX_START, CMV_SIZE, &cmv);
809         if (ret < 0)
810                 return ret;
811         return wait_cmv_ack(sc);
812 }
813
814 static inline int uea_read_cmv(struct uea_softc *sc,
815                 u32 address, u16 offset, u32 *data)
816 {
817         int ret = uea_cmv(sc, MAKEFUNCTION(MEMACCESS, REQUESTREAD),
818                           address, offset, 0);
819         if (ret < 0)
820                 uea_err(INS_TO_USBDEV(sc),
821                         "reading cmv failed with error %d\n", ret);
822         else
823                 *data = sc->data;
824
825         return ret;
826 }
827
828 static inline int uea_write_cmv(struct uea_softc *sc,
829                 u32 address, u16 offset, u32 data)
830 {
831         int ret = uea_cmv(sc, MAKEFUNCTION(MEMACCESS, REQUESTWRITE),
832                           address, offset, data);
833         if (ret < 0)
834                 uea_err(INS_TO_USBDEV(sc),
835                         "writing cmv failed with error %d\n", ret);
836
837         return ret;
838 }
839
840 /*
841  * Monitor the modem and update the stat
842  * return 0 if everything is ok
843  * return < 0 if an error occurs (-EAGAIN reboot needed)
844  */
845 static int uea_stat(struct uea_softc *sc)
846 {
847         u32 data;
848         int ret;
849
850         uea_enters(INS_TO_USBDEV(sc));
851         data = sc->stats.phy.state;
852
853         ret = uea_read_cmv(sc, SA_STAT, 0, &sc->stats.phy.state);
854         if (ret < 0)
855                 return ret;
856
857         switch (GET_STATUS(sc->stats.phy.state)) {
858         case 0:         /* not yet synchronized */
859                 uea_dbg(INS_TO_USBDEV(sc),
860                        "modem not yet synchronized\n");
861                 return 0;
862
863         case 1:         /* initialization */
864                 uea_dbg(INS_TO_USBDEV(sc), "modem initializing\n");
865                 return 0;
866
867         case 2:         /* operational */
868                 uea_vdbg(INS_TO_USBDEV(sc), "modem operational\n");
869                 break;
870
871         case 3:         /* fail ... */
872                 uea_info(INS_TO_USBDEV(sc), "modem synchronization failed\n");
873                 return -EAGAIN;
874
875         case 4 ... 6:   /* test state */
876                 uea_warn(INS_TO_USBDEV(sc),
877                                 "modem in test mode - not supported\n");
878                 return -EAGAIN;
879
880         case 7:         /* fast-retain ... */
881                 uea_info(INS_TO_USBDEV(sc), "modem in fast-retain mode\n");
882                 return 0;
883         default:
884                 uea_err(INS_TO_USBDEV(sc), "modem invalid SW mode %d\n",
885                         GET_STATUS(sc->stats.phy.state));
886                 return -EAGAIN;
887         }
888
889         if (GET_STATUS(data) != 2) {
890                 uea_request(sc, UEA_SET_MODE, UEA_LOOPBACK_OFF, 0, NULL);
891                 uea_info(INS_TO_USBDEV(sc), "modem operational\n");
892
893                 /* release the dsp firmware as it is not needed until
894                  * the next failure
895                  */
896                 if (sc->dsp_firm) {
897                         release_firmware(sc->dsp_firm);
898                         sc->dsp_firm = NULL;
899                 }
900
901                 ret = uea_read_cmv(sc, SA_INFO, 10, &sc->stats.phy.firmid);
902                 if (ret < 0)
903                         return ret;
904                 uea_info(INS_TO_USBDEV(sc), "ATU-R firmware version : %x\n",
905                                 sc->stats.phy.firmid);
906         }
907
908         /* always update it as atm layer could not be init when we switch to
909          * operational state
910          */
911         UPDATE_ATM_STAT(signal, ATM_PHY_SIG_FOUND);
912
913         /* wake up processes waiting for synchronization */
914         wake_up(&sc->sync_q);
915
916         ret = uea_read_cmv(sc, SA_DIAG, 2, &sc->stats.phy.flags);
917         if (ret < 0)
918                 return ret;
919         sc->stats.phy.mflags |= sc->stats.phy.flags;
920
921         /* in case of a flags ( for example delineation LOSS (& 0x10)),
922          * we check the status again in order to detect the failure earlier
923          */
924         if (sc->stats.phy.flags) {
925                 uea_dbg(INS_TO_USBDEV(sc), "Stat flag = %d\n",
926                        sc->stats.phy.flags);
927                 return 0;
928         }
929
930         ret = uea_read_cmv(sc, SA_RATE, 0, &data);
931         if (ret < 0)
932                 return ret;
933
934         /* in bulk mode the modem have problem with high rate
935          * changing internal timing could improve things, but the
936          * value is misterious.
937          * ADI930 don't support it (-EPIPE error).
938          */
939         if (UEA_CHIP_VERSION(sc) != ADI930
940                     && !use_iso[sc->modem_index]
941                     && sc->stats.phy.dsrate != (data >> 16) * 32) {
942                 /* Original timming from ADI(used in windows driver)
943                  * 0x20ffff>>16 * 32 = 32 * 32 = 1Mbits
944                  */
945                 u16 timeout = (data <= 0x20ffff) ? 0 : 1;
946                 ret = uea_request(sc, UEA_SET_TIMEOUT, timeout, 0, NULL);
947                 uea_info(INS_TO_USBDEV(sc),
948                                 "setting new timeout %d%s\n", timeout,
949                                 ret < 0?" failed":"");
950         }
951         sc->stats.phy.dsrate = (data >> 16) * 32;
952         sc->stats.phy.usrate = (data & 0xffff) * 32;
953         UPDATE_ATM_STAT(link_rate, sc->stats.phy.dsrate * 1000 / 424);
954
955         ret = uea_read_cmv(sc, SA_DIAG, 23, &data);
956         if (ret < 0)
957                 return ret;
958         sc->stats.phy.dsattenuation = (data & 0xff) / 2;
959
960         ret = uea_read_cmv(sc, SA_DIAG, 47, &data);
961         if (ret < 0)
962                 return ret;
963         sc->stats.phy.usattenuation = (data & 0xff) / 2;
964
965         ret = uea_read_cmv(sc, SA_DIAG, 25, &sc->stats.phy.dsmargin);
966         if (ret < 0)
967                 return ret;
968
969         ret = uea_read_cmv(sc, SA_DIAG, 49, &sc->stats.phy.usmargin);
970         if (ret < 0)
971                 return ret;
972
973         ret = uea_read_cmv(sc, SA_DIAG, 51, &sc->stats.phy.rxflow);
974         if (ret < 0)
975                 return ret;
976
977         ret = uea_read_cmv(sc, SA_DIAG, 52, &sc->stats.phy.txflow);
978         if (ret < 0)
979                 return ret;
980
981         ret = uea_read_cmv(sc, SA_DIAG, 54, &sc->stats.phy.dsunc);
982         if (ret < 0)
983                 return ret;
984
985         /* only for atu-c */
986         ret = uea_read_cmv(sc, SA_DIAG, 58, &sc->stats.phy.usunc);
987         if (ret < 0)
988                 return ret;
989
990         ret = uea_read_cmv(sc, SA_DIAG, 53, &sc->stats.phy.dscorr);
991         if (ret < 0)
992                 return ret;
993
994         /* only for atu-c */
995         ret = uea_read_cmv(sc, SA_DIAG, 57, &sc->stats.phy.uscorr);
996         if (ret < 0)
997                 return ret;
998
999         ret = uea_read_cmv(sc, SA_INFO, 8, &sc->stats.phy.vidco);
1000         if (ret < 0)
1001                 return ret;
1002
1003         ret = uea_read_cmv(sc, SA_INFO, 13, &sc->stats.phy.vidcpe);
1004         if (ret < 0)
1005                 return ret;
1006
1007         return 0;
1008 }
1009
1010 static int request_cmvs(struct uea_softc *sc,
1011                  struct uea_cmvs **cmvs, const struct firmware **fw)
1012 {
1013         int ret, size;
1014         u8 *data;
1015         char *file;
1016         char cmv_name[FIRMWARE_NAME_MAX]; /* 30 bytes stack variable */
1017
1018         if (cmv_file[sc->modem_index] == NULL) {
1019                 if (UEA_CHIP_VERSION(sc) == ADI930)
1020                         file = (IS_ISDN(sc)) ? "CMV9i.bin" : "CMV9p.bin";
1021                 else
1022                         file = (IS_ISDN(sc)) ? "CMVei.bin" : "CMVep.bin";
1023         } else
1024                 file = cmv_file[sc->modem_index];
1025
1026         strcpy(cmv_name, FW_DIR);
1027         strlcat(cmv_name, file, sizeof(cmv_name));
1028
1029         ret = request_firmware(fw, cmv_name, &sc->usb_dev->dev);
1030         if (ret < 0) {
1031                 uea_err(INS_TO_USBDEV(sc),
1032                        "requesting firmware %s failed with error %d\n",
1033                        cmv_name, ret);
1034                 return ret;
1035         }
1036
1037         data = (u8 *) (*fw)->data;
1038         size = *data * sizeof(struct uea_cmvs) + 1;
1039         if (size != (*fw)->size) {
1040                 uea_err(INS_TO_USBDEV(sc), "firmware %s is corrupted\n",
1041                        cmv_name);
1042                 release_firmware(*fw);
1043                 return -EILSEQ;
1044         }
1045
1046         *cmvs = (struct uea_cmvs *)(data + 1);
1047         return *data;
1048 }
1049
1050 /* Start boot post firmware modem:
1051  * - send reset commands through usb control pipe
1052  * - start workqueue for DSP loading
1053  * - send CMV options to modem
1054  */
1055
1056 static int uea_start_reset(struct uea_softc *sc)
1057 {
1058         u16 zero = 0;   /* ;-) */
1059         int i, len, ret;
1060         struct uea_cmvs *cmvs;
1061         const struct firmware *cmvs_fw;
1062
1063         uea_enters(INS_TO_USBDEV(sc));
1064         uea_info(INS_TO_USBDEV(sc), "(re)booting started\n");
1065
1066         sc->booting = 1;
1067         UPDATE_ATM_STAT(signal, ATM_PHY_SIG_LOST);
1068
1069         /* reset statistics */
1070         memset(&sc->stats, 0, sizeof(struct uea_stats));
1071
1072         /* tell the modem that we want to boot in IDMA mode */
1073         uea_request(sc, UEA_SET_MODE, UEA_LOOPBACK_ON, 0, NULL);
1074         uea_request(sc, UEA_SET_MODE, UEA_BOOT_IDMA, 0, NULL);
1075
1076         /* enter reset mode */
1077         uea_request(sc, UEA_SET_MODE, UEA_START_RESET, 0, NULL);
1078
1079         /* original driver use 200ms, but windows driver use 100ms */
1080         msleep(100);
1081
1082         /* leave reset mode */
1083         uea_request(sc, UEA_SET_MODE, UEA_END_RESET, 0, NULL);
1084
1085         /* clear tx and rx mailboxes */
1086         uea_request(sc, UEA_SET_2183_DATA, UEA_MPTX_MAILBOX, 2, &zero);
1087         uea_request(sc, UEA_SET_2183_DATA, UEA_MPRX_MAILBOX, 2, &zero);
1088         uea_request(sc, UEA_SET_2183_DATA, UEA_SWAP_MAILBOX, 2, &zero);
1089
1090         msleep(1000);
1091         sc->cmv_function = MAKEFUNCTION(ADSLDIRECTIVE, MODEMREADY);
1092         sc->booting = 0;
1093
1094         /* start loading DSP */
1095         sc->pageno = 0;
1096         sc->ovl = 0;
1097         schedule_work(&sc->task);
1098
1099         /* wait for modem ready CMV */
1100         ret = wait_cmv_ack(sc);
1101         if (ret < 0)
1102                 return ret;
1103
1104         /* Enter in R-IDLE (cmv) until instructed otherwise */
1105         ret = uea_write_cmv(sc, SA_CNTL, 0, 1);
1106         if (ret < 0)
1107                 return ret;
1108
1109         /* get options */
1110         ret = len = request_cmvs(sc, &cmvs, &cmvs_fw);
1111         if (ret < 0)
1112                 return ret;
1113
1114         /* send options */
1115         for (i = 0; i < len; i++) {
1116                 ret = uea_write_cmv(sc, FW_GET_LONG(&cmvs[i].address),
1117                                         FW_GET_WORD(&cmvs[i].offset),
1118                                         FW_GET_LONG(&cmvs[i].data));
1119                 if (ret < 0)
1120                         goto out;
1121         }
1122         /* Enter in R-ACT-REQ */
1123         ret = uea_write_cmv(sc, SA_CNTL, 0, 2);
1124 out:
1125         release_firmware(cmvs_fw);
1126         sc->reset = 0;
1127         uea_leaves(INS_TO_USBDEV(sc));
1128         return ret;
1129 }
1130
1131 /*
1132  * In case of an error wait 1s before rebooting the modem
1133  * if the modem don't request reboot (-EAGAIN).
1134  * Monitor the modem every 1s.
1135  */
1136
1137 static int uea_kthread(void *data)
1138 {
1139         struct uea_softc *sc = data;
1140         int ret = -EAGAIN;
1141
1142         uea_enters(INS_TO_USBDEV(sc));
1143         while (!kthread_should_stop()) {
1144                 if (ret < 0 || sc->reset)
1145                         ret = uea_start_reset(sc);
1146                 if (!ret)
1147                         ret = uea_stat(sc);
1148                 if (ret != -EAGAIN)
1149                         msleep(1000);
1150         }
1151         uea_leaves(INS_TO_USBDEV(sc));
1152         return ret;
1153 }
1154
1155 /* Load second usb firmware for ADI930 chip */
1156 static int load_XILINX_firmware(struct uea_softc *sc)
1157 {
1158         const struct firmware *fw_entry;
1159         int ret, size, u, ln;
1160         u8 *pfw, value;
1161         char *fw_name = FW_DIR "930-fpga.bin";
1162
1163         uea_enters(INS_TO_USBDEV(sc));
1164
1165         ret = request_firmware(&fw_entry, fw_name, &sc->usb_dev->dev);
1166         if (ret) {
1167                 uea_err(INS_TO_USBDEV(sc), "firmware %s is not available\n",
1168                        fw_name);
1169                 goto err0;
1170         }
1171
1172         pfw = fw_entry->data;
1173         size = fw_entry->size;
1174         if (size != 0x577B) {
1175                 uea_err(INS_TO_USBDEV(sc), "firmware %s is corrupted\n",
1176                        fw_name);
1177                 ret = -EILSEQ;
1178                 goto err1;
1179         }
1180         for (u = 0; u < size; u += ln) {
1181                 ln = min(size - u, 64);
1182                 ret = uea_request(sc, 0xe, 0, ln, pfw + u);
1183                 if (ret < 0) {
1184                         uea_err(INS_TO_USBDEV(sc),
1185                                "elsa download data failed (%d)\n", ret);
1186                         goto err1;
1187                 }
1188         }
1189
1190         /* finish to send the fpga */
1191         ret = uea_request(sc, 0xe, 1, 0, NULL);
1192         if (ret < 0) {
1193                 uea_err(INS_TO_USBDEV(sc),
1194                                 "elsa download data failed (%d)\n", ret);
1195                 goto err1;
1196         }
1197
1198         /* Tell the modem we finish : de-assert reset */
1199         value = 0;
1200         ret = uea_send_modem_cmd(sc->usb_dev, 0xe, 1, &value);
1201         if (ret < 0)
1202                 uea_err(sc->usb_dev, "elsa de-assert failed with error %d\n", ret);
1203
1204
1205 err1:
1206         release_firmware(fw_entry);
1207 err0:
1208         uea_leaves(INS_TO_USBDEV(sc));
1209         return ret;
1210 }
1211
1212 /* The modem send us an ack. First with check if it right */
1213 static void uea_dispatch_cmv(struct uea_softc *sc, struct cmv* cmv)
1214 {
1215         uea_enters(INS_TO_USBDEV(sc));
1216         if (le16_to_cpu(cmv->wPreamble) != PREAMBLE)
1217                 goto bad1;
1218
1219         if (cmv->bDirection != MODEMTOHOST)
1220                 goto bad1;
1221
1222         /* FIXME : ADI930 reply wrong preambule (func = 2, sub = 2) to
1223          * the first MEMACESS cmv. Ignore it...
1224          */
1225         if (cmv->bFunction != sc->cmv_function) {
1226                 if (UEA_CHIP_VERSION(sc) == ADI930
1227                                 && cmv->bFunction ==  MAKEFUNCTION(2, 2)) {
1228                         cmv->wIndex = cpu_to_le16(sc->cmv_idx);
1229                         put_unaligned(cpu_to_le32(sc->cmv_address), &cmv->dwSymbolicAddress);
1230                         cmv->wOffsetAddress = cpu_to_le16(sc->cmv_offset);
1231                 }
1232                 else
1233                         goto bad2;
1234         }
1235
1236         if (cmv->bFunction == MAKEFUNCTION(ADSLDIRECTIVE, MODEMREADY)) {
1237                 wake_up_cmv_ack(sc);
1238                 return;
1239         }
1240
1241         /* in case of MEMACCESS */
1242         if (le16_to_cpu(cmv->wIndex) != sc->cmv_idx ||
1243             le32_to_cpu(get_unaligned(&cmv->dwSymbolicAddress)) !=
1244             sc->cmv_address
1245             || le16_to_cpu(cmv->wOffsetAddress) != sc->cmv_offset)
1246                 goto bad2;
1247
1248         sc->data = le32_to_cpu(get_unaligned(&cmv->dwData));
1249         sc->data = sc->data << 16 | sc->data >> 16;
1250
1251         wake_up_cmv_ack(sc);
1252         return;
1253
1254 bad2:
1255         uea_err(INS_TO_USBDEV(sc), "unexpected cmv received,"
1256                         "Function : %d, Subfunction : %d\n",
1257                         FUNCTION_TYPE(cmv->bFunction),
1258                         FUNCTION_SUBTYPE(cmv->bFunction));
1259         return;
1260
1261 bad1:
1262         uea_err(INS_TO_USBDEV(sc), "invalid cmv received, "
1263                         "wPreamble %d, bDirection %d\n",
1264                         le16_to_cpu(cmv->wPreamble), cmv->bDirection);
1265 }
1266
1267 /*
1268  * interrupt handler
1269  */
1270 static void uea_intr(struct urb *urb, struct pt_regs *regs)
1271 {
1272         struct uea_softc *sc = urb->context;
1273         struct intr_pkt *intr = urb->transfer_buffer;
1274         uea_enters(INS_TO_USBDEV(sc));
1275
1276         if (unlikely(urb->status < 0)) {
1277                 uea_err(INS_TO_USBDEV(sc), "uea_intr() failed with %d\n",
1278                        urb->status);
1279                 return;
1280         }
1281
1282         /* device-to-host interrupt */
1283         if (intr->bType != 0x08 || sc->booting) {
1284                 uea_err(INS_TO_USBDEV(sc), "wrong interrupt\n");
1285                 goto resubmit;
1286         }
1287
1288         switch (le16_to_cpu(intr->wInterrupt)) {
1289         case INT_LOADSWAPPAGE:
1290                 sc->pageno = intr->bSwapPageNo;
1291                 sc->ovl = intr->bOvl >> 4 | intr->bOvl << 4;
1292                 schedule_work(&sc->task);
1293                 break;
1294
1295         case INT_INCOMINGCMV:
1296                 uea_dispatch_cmv(sc, &intr->u.s2.cmv);
1297                 break;
1298
1299         default:
1300                 uea_err(INS_TO_USBDEV(sc), "unknown interrupt %u\n",
1301                        le16_to_cpu(intr->wInterrupt));
1302         }
1303
1304 resubmit:
1305         usb_submit_urb(sc->urb_int, GFP_ATOMIC);
1306 }
1307
1308 /*
1309  * Start the modem : init the data and start kernel thread
1310  */
1311 static int uea_boot(struct uea_softc *sc)
1312 {
1313         int ret;
1314         struct intr_pkt *intr;
1315
1316         uea_enters(INS_TO_USBDEV(sc));
1317
1318         INIT_WORK(&sc->task, uea_load_page, sc);
1319         init_waitqueue_head(&sc->sync_q);
1320         init_waitqueue_head(&sc->cmv_ack_wait);
1321
1322         if (UEA_CHIP_VERSION(sc) == ADI930)
1323                 load_XILINX_firmware(sc);
1324
1325         intr = kmalloc(INTR_PKT_SIZE, GFP_KERNEL);
1326         if (!intr) {
1327                 uea_err(INS_TO_USBDEV(sc),
1328                        "cannot allocate interrupt package\n");
1329                 uea_leaves(INS_TO_USBDEV(sc));
1330                 return -ENOMEM;
1331         }
1332
1333         sc->urb_int = usb_alloc_urb(0, GFP_KERNEL);
1334         if (!sc->urb_int) {
1335                 uea_err(INS_TO_USBDEV(sc), "cannot allocate interrupt URB\n");
1336                 goto err;
1337         }
1338
1339         usb_fill_int_urb(sc->urb_int, sc->usb_dev,
1340                          usb_rcvintpipe(sc->usb_dev, UEA_INTR_PIPE),
1341                          intr, INTR_PKT_SIZE, uea_intr, sc,
1342                          sc->usb_dev->actconfig->interface[0]->altsetting[0].
1343                          endpoint[0].desc.bInterval);
1344
1345         ret = usb_submit_urb(sc->urb_int, GFP_KERNEL);
1346         if (ret < 0) {
1347                 uea_err(INS_TO_USBDEV(sc),
1348                        "urb submition failed with error %d\n", ret);
1349                 goto err1;
1350         }
1351
1352         sc->kthread = kthread_run(uea_kthread, sc, "ueagle-atm");
1353         if (sc->kthread == ERR_PTR(-ENOMEM)) {
1354                 uea_err(INS_TO_USBDEV(sc), "failed to create thread\n");
1355                 goto err2;
1356         }
1357
1358         uea_leaves(INS_TO_USBDEV(sc));
1359         return 0;
1360
1361 err2:
1362         usb_kill_urb(sc->urb_int);
1363 err1:
1364         kfree(intr);
1365 err:
1366         usb_free_urb(sc->urb_int);
1367         uea_leaves(INS_TO_USBDEV(sc));
1368         return -ENOMEM;
1369 }
1370
1371 /*
1372  * Stop the modem : kill kernel thread and free data
1373  */
1374 static void uea_stop(struct uea_softc *sc)
1375 {
1376         int ret;
1377         uea_enters(INS_TO_USBDEV(sc));
1378         ret = kthread_stop(sc->kthread);
1379         uea_dbg(INS_TO_USBDEV(sc), "kthread finish with status %d\n", ret);
1380
1381         /* stop any pending boot process */
1382         flush_scheduled_work();
1383
1384         uea_request(sc, UEA_SET_MODE, UEA_LOOPBACK_ON, 0, NULL);
1385
1386         usb_kill_urb(sc->urb_int);
1387         kfree(sc->urb_int->transfer_buffer);
1388         usb_free_urb(sc->urb_int);
1389
1390         if (sc->dsp_firm)
1391                 release_firmware(sc->dsp_firm);
1392         uea_leaves(INS_TO_USBDEV(sc));
1393 }
1394
1395 /* syfs interface */
1396 static struct uea_softc *dev_to_uea(struct device *dev)
1397 {
1398         struct usb_interface *intf;
1399         struct usbatm_data *usbatm;
1400
1401         intf = to_usb_interface(dev);
1402         if (!intf)
1403                 return NULL;
1404
1405         usbatm = usb_get_intfdata(intf);
1406         if (!usbatm)
1407                 return NULL;
1408
1409         return usbatm->driver_data;
1410 }
1411
1412 static ssize_t read_status(struct device *dev, struct device_attribute *attr,
1413                 char *buf)
1414 {
1415         int ret = -ENODEV;
1416         struct uea_softc *sc;
1417
1418         mutex_lock(&uea_mutex);
1419         sc = dev_to_uea(dev);
1420         if (!sc)
1421                 goto out;
1422         ret = snprintf(buf, 10, "%08x\n", sc->stats.phy.state);
1423 out:
1424         mutex_unlock(&uea_mutex);
1425         return ret;
1426 }
1427
1428 static ssize_t reboot(struct device *dev, struct device_attribute *attr,
1429                 const char *buf, size_t count)
1430 {
1431         int ret = -ENODEV;
1432         struct uea_softc *sc;
1433
1434         mutex_lock(&uea_mutex);
1435         sc = dev_to_uea(dev);
1436         if (!sc)
1437                 goto out;
1438         sc->reset = 1;
1439         ret = count;
1440 out:
1441         mutex_unlock(&uea_mutex);
1442         return ret;
1443 }
1444
1445 static DEVICE_ATTR(stat_status, S_IWUGO | S_IRUGO, read_status, reboot);
1446
1447 static ssize_t read_human_status(struct device *dev, struct device_attribute *attr,
1448                 char *buf)
1449 {
1450         int ret = -ENODEV;
1451         struct uea_softc *sc;
1452
1453         mutex_lock(&uea_mutex);
1454         sc = dev_to_uea(dev);
1455         if (!sc)
1456                 goto out;
1457
1458         switch (GET_STATUS(sc->stats.phy.state)) {
1459         case 0:
1460                 ret = sprintf(buf, "Modem is booting\n");
1461                 break;
1462         case 1:
1463                 ret = sprintf(buf, "Modem is initializing\n");
1464                 break;
1465         case 2:
1466                 ret = sprintf(buf, "Modem is operational\n");
1467                 break;
1468         default:
1469                 ret = sprintf(buf, "Modem synchronization failed\n");
1470                 break;
1471         }
1472 out:
1473         mutex_unlock(&uea_mutex);
1474         return ret;
1475 }
1476
1477 static DEVICE_ATTR(stat_human_status, S_IWUGO | S_IRUGO, read_human_status, NULL);
1478
1479 static ssize_t read_delin(struct device *dev, struct device_attribute *attr,
1480                 char *buf)
1481 {
1482         int ret = -ENODEV;
1483         struct uea_softc *sc;
1484
1485         mutex_lock(&uea_mutex);
1486         sc = dev_to_uea(dev);
1487         if (!sc)
1488                 goto out;
1489
1490         if (sc->stats.phy.flags & 0x0C00)
1491                 ret = sprintf(buf, "ERROR\n");
1492         else if (sc->stats.phy.flags & 0x0030)
1493                 ret = sprintf(buf, "LOSS\n");
1494         else
1495                 ret = sprintf(buf, "GOOD\n");
1496 out:
1497         mutex_unlock(&uea_mutex);
1498         return ret;
1499 }
1500
1501 static DEVICE_ATTR(stat_delin, S_IWUGO | S_IRUGO, read_delin, NULL);
1502
1503 #define UEA_ATTR(name, reset)                                   \
1504                                                                 \
1505 static ssize_t read_##name(struct device *dev,                  \
1506                 struct device_attribute *attr, char *buf)       \
1507 {                                                               \
1508         int ret = -ENODEV;                                      \
1509         struct uea_softc *sc;                                   \
1510                                                                 \
1511         mutex_lock(&uea_mutex);                                         \
1512         sc = dev_to_uea(dev);                                   \
1513         if (!sc)                                                \
1514                 goto out;                                       \
1515         ret = snprintf(buf, 10, "%08x\n", sc->stats.phy.name);  \
1516         if (reset)                                              \
1517                 sc->stats.phy.name = 0;                         \
1518 out:                                                            \
1519         mutex_unlock(&uea_mutex);                                       \
1520         return ret;                                             \
1521 }                                                               \
1522                                                                 \
1523 static DEVICE_ATTR(stat_##name, S_IRUGO, read_##name, NULL)
1524
1525 UEA_ATTR(mflags, 1);
1526 UEA_ATTR(vidcpe, 0);
1527 UEA_ATTR(usrate, 0);
1528 UEA_ATTR(dsrate, 0);
1529 UEA_ATTR(usattenuation, 0);
1530 UEA_ATTR(dsattenuation, 0);
1531 UEA_ATTR(usmargin, 0);
1532 UEA_ATTR(dsmargin, 0);
1533 UEA_ATTR(txflow, 0);
1534 UEA_ATTR(rxflow, 0);
1535 UEA_ATTR(uscorr, 0);
1536 UEA_ATTR(dscorr, 0);
1537 UEA_ATTR(usunc, 0);
1538 UEA_ATTR(dsunc, 0);
1539
1540 /* Retrieve the device End System Identifier (MAC) */
1541
1542 #define htoi(x) (isdigit(x) ? x-'0' : toupper(x)-'A'+10)
1543 static int uea_getesi(struct uea_softc *sc, u_char * esi)
1544 {
1545         unsigned char mac_str[2 * ETH_ALEN + 1];
1546         int i;
1547         if (usb_string
1548             (sc->usb_dev, sc->usb_dev->descriptor.iSerialNumber, mac_str,
1549              sizeof(mac_str)) != 2 * ETH_ALEN)
1550                 return 1;
1551
1552         for (i = 0; i < ETH_ALEN; i++)
1553                 esi[i] = htoi(mac_str[2 * i]) * 16 + htoi(mac_str[2 * i + 1]);
1554
1555         return 0;
1556 }
1557
1558 /* ATM stuff */
1559 static int uea_atm_open(struct usbatm_data *usbatm, struct atm_dev *atm_dev)
1560 {
1561         struct uea_softc *sc = usbatm->driver_data;
1562
1563         return uea_getesi(sc, atm_dev->esi);
1564 }
1565
1566 static int uea_heavy(struct usbatm_data *usbatm, struct usb_interface *intf)
1567 {
1568         struct uea_softc *sc = usbatm->driver_data;
1569
1570         wait_event(sc->sync_q, IS_OPERATIONAL(sc));
1571
1572         return 0;
1573
1574 }
1575
1576 static int claim_interface(struct usb_device *usb_dev,
1577                            struct usbatm_data *usbatm, int ifnum)
1578 {
1579         int ret;
1580         struct usb_interface *intf = usb_ifnum_to_if(usb_dev, ifnum);
1581
1582         if (!intf) {
1583                 uea_err(usb_dev, "interface %d not found\n", ifnum);
1584                 return -ENODEV;
1585         }
1586
1587         ret = usb_driver_claim_interface(&uea_driver, intf, usbatm);
1588         if (ret != 0)
1589                 uea_err(usb_dev, "can't claim interface %d, error %d\n", ifnum,
1590                        ret);
1591         return ret;
1592 }
1593
1594 static void create_fs_entries(struct uea_softc *sc, struct usb_interface *intf)
1595 {
1596         /* sysfs interface */
1597         device_create_file(&intf->dev, &dev_attr_stat_status);
1598         device_create_file(&intf->dev, &dev_attr_stat_mflags);
1599         device_create_file(&intf->dev, &dev_attr_stat_human_status);
1600         device_create_file(&intf->dev, &dev_attr_stat_delin);
1601         device_create_file(&intf->dev, &dev_attr_stat_vidcpe);
1602         device_create_file(&intf->dev, &dev_attr_stat_usrate);
1603         device_create_file(&intf->dev, &dev_attr_stat_dsrate);
1604         device_create_file(&intf->dev, &dev_attr_stat_usattenuation);
1605         device_create_file(&intf->dev, &dev_attr_stat_dsattenuation);
1606         device_create_file(&intf->dev, &dev_attr_stat_usmargin);
1607         device_create_file(&intf->dev, &dev_attr_stat_dsmargin);
1608         device_create_file(&intf->dev, &dev_attr_stat_txflow);
1609         device_create_file(&intf->dev, &dev_attr_stat_rxflow);
1610         device_create_file(&intf->dev, &dev_attr_stat_uscorr);
1611         device_create_file(&intf->dev, &dev_attr_stat_dscorr);
1612         device_create_file(&intf->dev, &dev_attr_stat_usunc);
1613         device_create_file(&intf->dev, &dev_attr_stat_dsunc);
1614 }
1615
1616 static int uea_bind(struct usbatm_data *usbatm, struct usb_interface *intf,
1617                    const struct usb_device_id *id)
1618 {
1619         struct usb_device *usb = interface_to_usbdev(intf);
1620         struct uea_softc *sc;
1621         int ret, ifnum = intf->altsetting->desc.bInterfaceNumber;
1622
1623         uea_enters(usb);
1624
1625         /* interface 0 is for firmware/monitoring */
1626         if (ifnum != UEA_INTR_IFACE_NO)
1627                 return -ENODEV;
1628
1629         usbatm->flags = (sync_wait[modem_index] ? 0 : UDSL_SKIP_HEAVY_INIT);
1630
1631         /* interface 1 is for outbound traffic */
1632         ret = claim_interface(usb, usbatm, UEA_US_IFACE_NO);
1633         if (ret < 0)
1634                 return ret;
1635
1636         /* ADI930 has only 2 interfaces and inbound traffic is on interface 1 */
1637         if (UEA_CHIP_VERSION(id) != ADI930) {
1638                 /* interface 2 is for inbound traffic */
1639                 ret = claim_interface(usb, usbatm, UEA_DS_IFACE_NO);
1640                 if (ret < 0)
1641                         return ret;
1642         }
1643
1644         sc = kzalloc(sizeof(struct uea_softc), GFP_KERNEL);
1645         if (!sc) {
1646                 uea_err(INS_TO_USBDEV(sc), "uea_init: not enough memory !\n");
1647                 return -ENOMEM;
1648         }
1649
1650         sc->usb_dev = usb;
1651         usbatm->driver_data = sc;
1652         sc->usbatm = usbatm;
1653         sc->modem_index = (modem_index < NB_MODEM) ? modem_index++ : 0;
1654         sc->driver_info = id->driver_info;
1655
1656         /* ADI930 don't support iso */
1657         if (UEA_CHIP_VERSION(id) != ADI930 && use_iso[sc->modem_index]) {
1658                 int i;
1659
1660                 /* try set fastest alternate for inbound traffic interface */
1661                 for (i = FASTEST_ISO_INTF; i > 0; i--)
1662                         if (usb_set_interface(usb, UEA_DS_IFACE_NO, i) == 0)
1663                                 break;
1664
1665                 if (i > 0) {
1666                         uea_dbg(usb, "set alternate %d for 2 interface\n", i);
1667                         uea_info(usb, "using iso mode\n");
1668                         usbatm->flags |= UDSL_USE_ISOC | UDSL_IGNORE_EILSEQ;
1669                 } else {
1670                         uea_err(usb, "setting any alternate failed for "
1671                                         "2 interface, using bulk mode\n");
1672                 }
1673         }
1674
1675         ret = uea_boot(sc);
1676         if (ret < 0) {
1677                 kfree(sc);
1678                 return ret;
1679         }
1680
1681         create_fs_entries(sc, intf);
1682         return 0;
1683 }
1684
1685 static void destroy_fs_entries(struct uea_softc *sc, struct usb_interface *intf)
1686 {
1687         /* sysfs interface */
1688         device_remove_file(&intf->dev, &dev_attr_stat_status);
1689         device_remove_file(&intf->dev, &dev_attr_stat_mflags);
1690         device_remove_file(&intf->dev, &dev_attr_stat_human_status);
1691         device_remove_file(&intf->dev, &dev_attr_stat_delin);
1692         device_remove_file(&intf->dev, &dev_attr_stat_vidcpe);
1693         device_remove_file(&intf->dev, &dev_attr_stat_usrate);
1694         device_remove_file(&intf->dev, &dev_attr_stat_dsrate);
1695         device_remove_file(&intf->dev, &dev_attr_stat_usattenuation);
1696         device_remove_file(&intf->dev, &dev_attr_stat_dsattenuation);
1697         device_remove_file(&intf->dev, &dev_attr_stat_usmargin);
1698         device_remove_file(&intf->dev, &dev_attr_stat_dsmargin);
1699         device_remove_file(&intf->dev, &dev_attr_stat_txflow);
1700         device_remove_file(&intf->dev, &dev_attr_stat_rxflow);
1701         device_remove_file(&intf->dev, &dev_attr_stat_uscorr);
1702         device_remove_file(&intf->dev, &dev_attr_stat_dscorr);
1703         device_remove_file(&intf->dev, &dev_attr_stat_usunc);
1704         device_remove_file(&intf->dev, &dev_attr_stat_dsunc);
1705 }
1706
1707 static void uea_unbind(struct usbatm_data *usbatm, struct usb_interface *intf)
1708 {
1709         struct uea_softc *sc = usbatm->driver_data;
1710
1711         destroy_fs_entries(sc, intf);
1712         uea_stop(sc);
1713         kfree(sc);
1714 }
1715
1716 static struct usbatm_driver uea_usbatm_driver = {
1717         .driver_name = "ueagle-atm",
1718         .bind = uea_bind,
1719         .atm_start = uea_atm_open,
1720         .unbind = uea_unbind,
1721         .heavy_init = uea_heavy,
1722         .bulk_in = UEA_BULK_DATA_PIPE,
1723         .bulk_out = UEA_BULK_DATA_PIPE,
1724         .isoc_in = UEA_ISO_DATA_PIPE,
1725 };
1726
1727 static int uea_probe(struct usb_interface *intf, const struct usb_device_id *id)
1728 {
1729         struct usb_device *usb = interface_to_usbdev(intf);
1730
1731         uea_enters(usb);
1732         uea_info(usb, "ADSL device founded vid (%#X) pid (%#X) : %s\n",
1733                le16_to_cpu(usb->descriptor.idVendor),
1734                le16_to_cpu(usb->descriptor.idProduct),
1735                chip_name[UEA_CHIP_VERSION(id)]);
1736
1737         usb_reset_device(usb);
1738
1739         if (UEA_IS_PREFIRM(id))
1740                 return uea_load_firmware(usb, UEA_CHIP_VERSION(id));
1741
1742         return usbatm_usb_probe(intf, id, &uea_usbatm_driver);
1743 }
1744
1745 static void uea_disconnect(struct usb_interface *intf)
1746 {
1747         struct usb_device *usb = interface_to_usbdev(intf);
1748         int ifnum = intf->altsetting->desc.bInterfaceNumber;
1749         uea_enters(usb);
1750
1751         /* ADI930 has 2 interfaces and eagle 3 interfaces.
1752          * Pre-firmware device has one interface
1753          */
1754         if (usb->config->desc.bNumInterfaces != 1 && ifnum == 0) {
1755                 mutex_lock(&uea_mutex);
1756                 usbatm_usb_disconnect(intf);
1757                 mutex_unlock(&uea_mutex);
1758                 uea_info(usb, "ADSL device removed\n");
1759         }
1760
1761         uea_leaves(usb);
1762 }
1763
1764 /*
1765  * List of supported VID/PID
1766  */
1767 static const struct usb_device_id uea_ids[] = {
1768         {USB_DEVICE(ELSA_VID,   ELSA_PID_PREFIRM),      .driver_info = ADI930 | PREFIRM},
1769         {USB_DEVICE(ELSA_VID,   ELSA_PID_PSTFIRM),      .driver_info = ADI930 | PSTFIRM},
1770         {USB_DEVICE(EAGLE_VID,  EAGLE_I_PID_PREFIRM),   .driver_info = EAGLE_I | PREFIRM},
1771         {USB_DEVICE(EAGLE_VID,  EAGLE_I_PID_PSTFIRM),   .driver_info = EAGLE_I | PSTFIRM},
1772         {USB_DEVICE(EAGLE_VID,  EAGLE_II_PID_PREFIRM),  .driver_info = EAGLE_II | PREFIRM},
1773         {USB_DEVICE(EAGLE_VID,  EAGLE_II_PID_PSTFIRM),  .driver_info = EAGLE_II | PSTFIRM},
1774         {USB_DEVICE(EAGLE_VID,  EAGLE_IIC_PID_PREFIRM), .driver_info = EAGLE_II | PREFIRM},
1775         {USB_DEVICE(EAGLE_VID,  EAGLE_IIC_PID_PSTFIRM), .driver_info = EAGLE_II | PSTFIRM},
1776         {USB_DEVICE(EAGLE_VID,  EAGLE_III_PID_PREFIRM), .driver_info = EAGLE_III | PREFIRM},
1777         {USB_DEVICE(EAGLE_VID,  EAGLE_III_PID_PSTFIRM), .driver_info = EAGLE_III | PSTFIRM},
1778         {USB_DEVICE(USR_VID,    MILLER_A_PID_PREFIRM),  .driver_info = EAGLE_I | PREFIRM},
1779         {USB_DEVICE(USR_VID,    MILLER_A_PID_PSTFIRM),  .driver_info = EAGLE_I | PSTFIRM},
1780         {USB_DEVICE(USR_VID,    MILLER_B_PID_PREFIRM),  .driver_info = EAGLE_I | PREFIRM},
1781         {USB_DEVICE(USR_VID,    MILLER_B_PID_PSTFIRM),  .driver_info = EAGLE_I | PSTFIRM},
1782         {USB_DEVICE(USR_VID,    HEINEKEN_A_PID_PREFIRM),.driver_info = EAGLE_I | PREFIRM},
1783         {USB_DEVICE(USR_VID,    HEINEKEN_A_PID_PSTFIRM),.driver_info = EAGLE_I | PSTFIRM},
1784         {USB_DEVICE(USR_VID,    HEINEKEN_B_PID_PREFIRM),.driver_info = EAGLE_I | PREFIRM},
1785         {USB_DEVICE(USR_VID,    HEINEKEN_B_PID_PSTFIRM),.driver_info = EAGLE_I | PSTFIRM},
1786         {}
1787 };
1788
1789 /*
1790  * USB driver descriptor
1791  */
1792 static struct usb_driver uea_driver = {
1793         .name = "ueagle-atm",
1794         .id_table = uea_ids,
1795         .probe = uea_probe,
1796         .disconnect = uea_disconnect,
1797 };
1798
1799 MODULE_DEVICE_TABLE(usb, uea_ids);
1800
1801 /**
1802  * uea_init - Initialize the module.
1803  *      Register to USB subsystem
1804  */
1805 static int __init uea_init(void)
1806 {
1807         printk(KERN_INFO "[ueagle-atm] driver " EAGLEUSBVERSION " loaded\n");
1808
1809         usb_register(&uea_driver);
1810
1811         return 0;
1812 }
1813
1814 module_init(uea_init);
1815
1816 /**
1817  * uea_exit  -  Destroy module
1818  *    Deregister with USB subsystem
1819  */
1820 static void __exit uea_exit(void)
1821 {
1822         /*
1823          * This calls automatically the uea_disconnect method if necessary:
1824          */
1825         usb_deregister(&uea_driver);
1826
1827         printk(KERN_INFO "[ueagle-atm] driver unloaded\n");
1828 }
1829
1830 module_exit(uea_exit);
1831
1832 MODULE_AUTHOR("Damien Bergamini/Matthieu Castet/Stanislaw W. Gruszka");
1833 MODULE_DESCRIPTION("ADI 930/Eagle USB ADSL Modem driver");
1834 MODULE_LICENSE("Dual BSD/GPL");