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