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