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