blob: c5395246886d060b11c4ba2e12867c5784d2e211 [file] [log] [blame]
matthieu castetb72458a2005-11-07 23:27:13 +01001/*-
2 * Copyright (c) 2003, 2004
3 * Damien Bergamini <damien.bergamini@free.fr>. All rights reserved.
4 *
Stanislaw Gruszka0eb02262007-08-20 23:21:19 +02005 * Copyright (c) 2005-2007 Matthieu Castet <castet.matthieu@free.fr>
6 * Copyright (c) 2005-2007 Stanislaw Gruszka <stf_xl@wp.pl>
matthieu castetb72458a2005-11-07 23:27:13 +01007 *
8 * This software is available to you under a choice of one of two
9 * licenses. You may choose to be licensed under the terms of the GNU
10 * General Public License (GPL) Version 2, available from the file
11 * COPYING in the main directory of this source tree, or the
12 * BSD license below:
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice unmodified, this list of conditions, and the following
19 * disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * GPL license :
37 * This program is free software; you can redistribute it and/or
38 * modify it under the terms of the GNU General Public License
39 * as published by the Free Software Foundation; either version 2
40 * of the License, or (at your option) any later version.
41 *
42 * This program is distributed in the hope that it will be useful,
43 * but WITHOUT ANY WARRANTY; without even the implied warranty of
44 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
45 * GNU General Public License for more details.
46 *
47 * You should have received a copy of the GNU General Public License
48 * along with this program; if not, write to the Free Software
49 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
50 *
51 *
52 * HISTORY : some part of the code was base on ueagle 1.3 BSD driver,
53 * Damien Bergamini agree to put his code under a DUAL GPL/BSD license.
54 *
55 * The rest of the code was was rewritten from scratch.
56 */
57
58#include <linux/module.h>
59#include <linux/moduleparam.h>
60#include <linux/init.h>
61#include <linux/crc32.h>
62#include <linux/usb.h>
63#include <linux/firmware.h>
64#include <linux/ctype.h>
Randy Dunlap5371f802007-02-16 01:47:33 -080065#include <linux/sched.h>
matthieu castetb72458a2005-11-07 23:27:13 +010066#include <linux/kthread.h>
Arjan van de Venab3c81f2006-01-13 15:52:55 +010067#include <linux/mutex.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080068#include <linux/freezer.h>
69
matthieu castetb72458a2005-11-07 23:27:13 +010070#include <asm/unaligned.h>
71
72#include "usbatm.h"
73
matthieu casteta7a0c9c2006-10-03 21:44:11 +020074#define EAGLEUSBVERSION "ueagle 1.4"
matthieu castetb72458a2005-11-07 23:27:13 +010075
76
77/*
78 * Debug macros
79 */
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, \
Harvey Harrison441b62c2008-03-03 16:08:34 -080085 __func__, ##args); \
matthieu casteta7a0c9c2006-10-03 21:44:11 +020086 } while (0)
matthieu castetb72458a2005-11-07 23:27:13 +010087
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); \
matthieu casteta7a0c9c2006-10-03 21:44:11 +020093 } while (0)
matthieu castetb72458a2005-11-07 23:27:13 +010094
95#define uea_enters(usb_dev) \
Harvey Harrison441b62c2008-03-03 16:08:34 -080096 uea_vdbg(usb_dev, "entering %s\n", __func__)
matthieu castetb72458a2005-11-07 23:27:13 +010097
98#define uea_leaves(usb_dev) \
Harvey Harrison441b62c2008-03-03 16:08:34 -080099 uea_vdbg(usb_dev, "leaving %s\n", __func__)
matthieu castetb72458a2005-11-07 23:27:13 +0100100
101#define uea_err(usb_dev, format,args...) \
102 dev_err(&(usb_dev)->dev ,"[UEAGLE-ATM] " format , ##args)
103
104#define uea_warn(usb_dev, format,args...) \
105 dev_warn(&(usb_dev)->dev ,"[Ueagle-atm] " format, ##args)
106
107#define uea_info(usb_dev, format,args...) \
108 dev_info(&(usb_dev)->dev ,"[ueagle-atm] " format, ##args)
109
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +0200110struct intr_pkt;
111
112/* cmv's from firmware */
113struct uea_cmvs_v1 {
matthieu castetb72458a2005-11-07 23:27:13 +0100114 u32 address;
115 u16 offset;
116 u32 data;
117} __attribute__ ((packed));
118
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +0200119struct uea_cmvs_v2 {
120 u32 group;
121 u32 address;
122 u32 offset;
123 u32 data;
124} __attribute__ ((packed));
125
126/* information about currently processed cmv */
127struct cmv_dsc_e1 {
128 u8 function;
129 u16 idx;
130 u32 address;
131 u16 offset;
132};
133
134struct cmv_dsc_e4 {
135 u16 function;
136 u16 offset;
137 u16 address;
138 u16 group;
139};
140
141union cmv_dsc {
142 struct cmv_dsc_e1 e1;
143 struct cmv_dsc_e4 e4;
144};
145
matthieu castetb72458a2005-11-07 23:27:13 +0100146struct uea_softc {
147 struct usb_device *usb_dev;
148 struct usbatm_data *usbatm;
149
150 int modem_index;
151 unsigned int driver_info;
Stanislaw Gruszka603cf602007-08-20 23:21:01 +0200152 int annex;
153#define ANNEXA 0
154#define ANNEXB 1
matthieu castetb72458a2005-11-07 23:27:13 +0100155
156 int booting;
157 int reset;
158
159 wait_queue_head_t sync_q;
160
161 struct task_struct *kthread;
162 u32 data;
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +0200163 u32 data1;
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +0200164
matthieu castetb72458a2005-11-07 23:27:13 +0100165 int cmv_ack;
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +0200166 union cmv_dsc cmv_dsc;
matthieu castetb72458a2005-11-07 23:27:13 +0100167
168 struct work_struct task;
Stanislaw Gruszka04ea02f2007-08-20 23:21:10 +0200169 struct workqueue_struct *work_q;
matthieu castetb72458a2005-11-07 23:27:13 +0100170 u16 pageno;
171 u16 ovl;
172
173 const struct firmware *dsp_firm;
174 struct urb *urb_int;
175
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +0200176 void (*dispatch_cmv) (struct uea_softc *, struct intr_pkt *);
177 void (*schedule_load_page) (struct uea_softc *, struct intr_pkt *);
178 int (*stat) (struct uea_softc *);
179 int (*send_cmvs) (struct uea_softc *);
matthieu castetb72458a2005-11-07 23:27:13 +0100180
181 /* keep in sync with eaglectl */
182 struct uea_stats {
183 struct {
184 u32 state;
185 u32 flags;
186 u32 mflags;
187 u32 vidcpe;
188 u32 vidco;
189 u32 dsrate;
190 u32 usrate;
191 u32 dsunc;
192 u32 usunc;
193 u32 dscorr;
194 u32 uscorr;
195 u32 txflow;
196 u32 rxflow;
197 u32 usattenuation;
198 u32 dsattenuation;
199 u32 dsmargin;
200 u32 usmargin;
201 u32 firmid;
202 } phy;
203 } stats;
204};
205
206/*
207 * Elsa IDs
208 */
209#define ELSA_VID 0x05CC
210#define ELSA_PID_PSTFIRM 0x3350
211#define ELSA_PID_PREFIRM 0x3351
212
Stanislaw Gruszka603cf602007-08-20 23:21:01 +0200213#define ELSA_PID_A_PREFIRM 0x3352
214#define ELSA_PID_A_PSTFIRM 0x3353
215#define ELSA_PID_B_PREFIRM 0x3362
216#define ELSA_PID_B_PSTFIRM 0x3363
217
matthieu castetb72458a2005-11-07 23:27:13 +0100218/*
Stanislaw Gruszka603cf602007-08-20 23:21:01 +0200219 * Devolo IDs : pots if (pid & 0x10)
matthieu castetb72458a2005-11-07 23:27:13 +0100220 */
Stanislaw Gruszka603cf602007-08-20 23:21:01 +0200221#define DEVOLO_VID 0x1039
222#define DEVOLO_EAGLE_I_A_PID_PSTFIRM 0x2110
223#define DEVOLO_EAGLE_I_A_PID_PREFIRM 0x2111
224
225#define DEVOLO_EAGLE_I_B_PID_PSTFIRM 0x2100
226#define DEVOLO_EAGLE_I_B_PID_PREFIRM 0x2101
227
228#define DEVOLO_EAGLE_II_A_PID_PSTFIRM 0x2130
229#define DEVOLO_EAGLE_II_A_PID_PREFIRM 0x2131
230
231#define DEVOLO_EAGLE_II_B_PID_PSTFIRM 0x2120
232#define DEVOLO_EAGLE_II_B_PID_PREFIRM 0x2121
233
234/*
235 * Reference design USB IDs
236 */
237#define ANALOG_VID 0x1110
238#define ADI930_PID_PREFIRM 0x9001
239#define ADI930_PID_PSTFIRM 0x9000
240
matthieu castetb72458a2005-11-07 23:27:13 +0100241#define EAGLE_I_PID_PREFIRM 0x9010 /* Eagle I */
242#define EAGLE_I_PID_PSTFIRM 0x900F /* Eagle I */
243
244#define EAGLE_IIC_PID_PREFIRM 0x9024 /* Eagle IIC */
245#define EAGLE_IIC_PID_PSTFIRM 0x9023 /* Eagle IIC */
246
247#define EAGLE_II_PID_PREFIRM 0x9022 /* Eagle II */
248#define EAGLE_II_PID_PSTFIRM 0x9021 /* Eagle II */
249
matthieu castetb72458a2005-11-07 23:27:13 +0100250#define EAGLE_III_PID_PREFIRM 0x9032 /* Eagle III */
251#define EAGLE_III_PID_PSTFIRM 0x9031 /* Eagle III */
252
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +0200253#define EAGLE_IV_PID_PREFIRM 0x9042 /* Eagle IV */
254#define EAGLE_IV_PID_PSTFIRM 0x9041 /* Eagle IV */
255
matthieu castetb72458a2005-11-07 23:27:13 +0100256/*
257 * USR USB IDs
258 */
259#define USR_VID 0x0BAF
260#define MILLER_A_PID_PREFIRM 0x00F2
261#define MILLER_A_PID_PSTFIRM 0x00F1
262#define MILLER_B_PID_PREFIRM 0x00FA
263#define MILLER_B_PID_PSTFIRM 0x00F9
264#define HEINEKEN_A_PID_PREFIRM 0x00F6
265#define HEINEKEN_A_PID_PSTFIRM 0x00F5
266#define HEINEKEN_B_PID_PREFIRM 0x00F8
267#define HEINEKEN_B_PID_PSTFIRM 0x00F7
268
269#define PREFIRM 0
270#define PSTFIRM (1<<7)
Stanislaw Gruszka603cf602007-08-20 23:21:01 +0200271#define AUTO_ANNEX_A (1<<8)
272#define AUTO_ANNEX_B (1<<9)
273
matthieu castetb72458a2005-11-07 23:27:13 +0100274enum {
275 ADI930 = 0,
276 EAGLE_I,
277 EAGLE_II,
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +0200278 EAGLE_III,
279 EAGLE_IV
matthieu castetb72458a2005-11-07 23:27:13 +0100280};
281
282/* macros for both struct usb_device_id and struct uea_softc */
283#define UEA_IS_PREFIRM(x) \
284 (!((x)->driver_info & PSTFIRM))
285#define UEA_CHIP_VERSION(x) \
286 ((x)->driver_info & 0xf)
287
Stanislaw Gruszka603cf602007-08-20 23:21:01 +0200288#define IS_ISDN(x) \
289 ((x)->annex & ANNEXB)
matthieu castetb72458a2005-11-07 23:27:13 +0100290
291#define INS_TO_USBDEV(ins) ins->usb_dev
292
293#define GET_STATUS(data) \
294 ((data >> 8) & 0xf)
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +0200295
matthieu castetb72458a2005-11-07 23:27:13 +0100296#define IS_OPERATIONAL(sc) \
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +0200297 ((UEA_CHIP_VERSION(sc) != EAGLE_IV) ? \
298 (GET_STATUS(sc->stats.phy.state) == 2) : \
299 (sc->stats.phy.state == 7))
matthieu castetb72458a2005-11-07 23:27:13 +0100300
301/*
302 * Set of macros to handle unaligned data in the firmware blob.
303 * The FW_GET_BYTE() macro is provided only for consistency.
304 */
305
306#define FW_GET_BYTE(p) *((__u8 *) (p))
matthieu castetb72458a2005-11-07 23:27:13 +0100307
308#define FW_DIR "ueagle-atm/"
Samuel Ortizade901d2009-05-27 00:49:32 +0200309#define UEA_FW_NAME_MAX 30
matthieu castetb72458a2005-11-07 23:27:13 +0100310#define NB_MODEM 4
311
312#define BULK_TIMEOUT 300
313#define CTRL_TIMEOUT 1000
314
matthieu castet22fcceb2006-04-02 18:44:20 +0200315#define ACK_TIMEOUT msecs_to_jiffies(3000)
matthieu castetb72458a2005-11-07 23:27:13 +0100316
317#define UEA_INTR_IFACE_NO 0
318#define UEA_US_IFACE_NO 1
319#define UEA_DS_IFACE_NO 2
320
321#define FASTEST_ISO_INTF 8
322
323#define UEA_BULK_DATA_PIPE 0x02
324#define UEA_IDMA_PIPE 0x04
325#define UEA_INTR_PIPE 0x04
326#define UEA_ISO_DATA_PIPE 0x08
327
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +0200328#define UEA_E1_SET_BLOCK 0x0001
329#define UEA_E4_SET_BLOCK 0x002c
matthieu castetb72458a2005-11-07 23:27:13 +0100330#define UEA_SET_MODE 0x0003
331#define UEA_SET_2183_DATA 0x0004
332#define UEA_SET_TIMEOUT 0x0011
333
334#define UEA_LOOPBACK_OFF 0x0002
335#define UEA_LOOPBACK_ON 0x0003
336#define UEA_BOOT_IDMA 0x0006
337#define UEA_START_RESET 0x0007
338#define UEA_END_RESET 0x0008
339
340#define UEA_SWAP_MAILBOX (0x3fcd | 0x4000)
341#define UEA_MPTX_START (0x3fce | 0x4000)
342#define UEA_MPTX_MAILBOX (0x3fd6 | 0x4000)
343#define UEA_MPRX_MAILBOX (0x3fdf | 0x4000)
344
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +0200345/* block information in eagle4 dsp firmware */
346struct block_index {
347 __le32 PageOffset;
348 __le32 NotLastBlock;
349 __le32 dummy;
350 __le32 PageSize;
351 __le32 PageAddress;
352 __le16 dummy1;
353 __le16 PageNumber;
354} __attribute__ ((packed));
355
356#define E4_IS_BOOT_PAGE(PageSize) ((le32_to_cpu(PageSize)) & 0x80000000)
357#define E4_PAGE_BYTES(PageSize) ((le32_to_cpu(PageSize) & 0x7fffffff) * 4)
358
359#define E4_L1_STRING_HEADER 0x10
360#define E4_MAX_PAGE_NUMBER 0x58
361#define E4_NO_SWAPPAGE_HEADERS 0x31
362
363/* l1_code is eagle4 dsp firmware format */
364struct l1_code {
365 u8 string_header[E4_L1_STRING_HEADER];
366 u8 page_number_to_block_index[E4_MAX_PAGE_NUMBER];
367 struct block_index page_header[E4_NO_SWAPPAGE_HEADERS];
368 u8 code [0];
369} __attribute__ ((packed));
370
371/* structures describing a block within a DSP page */
372struct block_info_e1 {
matthieu castetb72458a2005-11-07 23:27:13 +0100373 __le16 wHdr;
matthieu castetb72458a2005-11-07 23:27:13 +0100374 __le16 wAddress;
375 __le16 wSize;
376 __le16 wOvlOffset;
377 __le16 wOvl; /* overlay */
378 __le16 wLast;
379} __attribute__ ((packed));
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +0200380#define E1_BLOCK_INFO_SIZE 12
matthieu castetb72458a2005-11-07 23:27:13 +0100381
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +0200382struct block_info_e4 {
383 __be16 wHdr;
384 __u8 bBootPage;
385 __u8 bPageNumber;
386 __be32 dwSize;
387 __be32 dwAddress;
388 __be16 wReserved;
389} __attribute__ ((packed));
390#define E4_BLOCK_INFO_SIZE 14
matthieu castetb72458a2005-11-07 23:27:13 +0100391
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +0200392#define UEA_BIHDR 0xabcd
393#define UEA_RESERVED 0xffff
394
395/* constants describing cmv type */
396#define E1_PREAMBLE 0x535c
397#define E1_MODEMTOHOST 0x01
398#define E1_HOSTTOMODEM 0x10
399
400#define E1_MEMACCESS 0x1
401#define E1_ADSLDIRECTIVE 0x7
402#define E1_FUNCTION_TYPE(f) ((f) >> 4)
403#define E1_FUNCTION_SUBTYPE(f) ((f) & 0x0f)
404
405#define E4_MEMACCESS 0
406#define E4_ADSLDIRECTIVE 0xf
407#define E4_FUNCTION_TYPE(f) ((f) >> 8)
408#define E4_FUNCTION_SIZE(f) ((f) & 0x0f)
409#define E4_FUNCTION_SUBTYPE(f) (((f) >> 4) & 0x0f)
410
matthieu castetb72458a2005-11-07 23:27:13 +0100411/* for MEMACCESS */
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +0200412#define E1_REQUESTREAD 0x0
413#define E1_REQUESTWRITE 0x1
414#define E1_REPLYREAD 0x2
415#define E1_REPLYWRITE 0x3
matthieu castetb72458a2005-11-07 23:27:13 +0100416
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +0200417#define E4_REQUESTREAD 0x0
418#define E4_REQUESTWRITE 0x4
419#define E4_REPLYREAD (E4_REQUESTREAD | 1)
420#define E4_REPLYWRITE (E4_REQUESTWRITE | 1)
421
422/* for ADSLDIRECTIVE */
423#define E1_KERNELREADY 0x0
424#define E1_MODEMREADY 0x1
425
426#define E4_KERNELREADY 0x0
427#define E4_MODEMREADY 0x1
428
429#define E1_MAKEFUNCTION(t, s) (((t) & 0xf) << 4 | ((s) & 0xf))
430#define E4_MAKEFUNCTION(t, st, s) (((t) & 0xf) << 8 | ((st) & 0xf) << 4 | ((s) & 0xf))
431
432#define E1_MAKESA(a, b, c, d) \
matthieu castetb72458a2005-11-07 23:27:13 +0100433 (((c) & 0xff) << 24 | \
434 ((d) & 0xff) << 16 | \
435 ((a) & 0xff) << 8 | \
436 ((b) & 0xff))
437
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +0200438#define E1_GETSA1(a) ((a >> 8) & 0xff)
439#define E1_GETSA2(a) (a & 0xff)
440#define E1_GETSA3(a) ((a >> 24) & 0xff)
441#define E1_GETSA4(a) ((a >> 16) & 0xff)
442
443#define E1_SA_CNTL E1_MAKESA('C', 'N', 'T', 'L')
444#define E1_SA_DIAG E1_MAKESA('D', 'I', 'A', 'G')
445#define E1_SA_INFO E1_MAKESA('I', 'N', 'F', 'O')
446#define E1_SA_OPTN E1_MAKESA('O', 'P', 'T', 'N')
447#define E1_SA_RATE E1_MAKESA('R', 'A', 'T', 'E')
448#define E1_SA_STAT E1_MAKESA('S', 'T', 'A', 'T')
449
450#define E4_SA_CNTL 1
451#define E4_SA_STAT 2
452#define E4_SA_INFO 3
453#define E4_SA_TEST 4
454#define E4_SA_OPTN 5
455#define E4_SA_RATE 6
456#define E4_SA_DIAG 7
457#define E4_SA_CNFG 8
458
459/* structures representing a CMV (Configuration and Management Variable) */
460struct cmv_e1 {
461 __le16 wPreamble;
462 __u8 bDirection;
463 __u8 bFunction;
464 __le16 wIndex;
465 __le32 dwSymbolicAddress;
matthieu castetb72458a2005-11-07 23:27:13 +0100466 __le16 wOffsetAddress;
467 __le32 dwData;
468} __attribute__ ((packed));
matthieu castetb72458a2005-11-07 23:27:13 +0100469
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +0200470struct cmv_e4 {
471 __be16 wGroup;
472 __be16 wFunction;
473 __be16 wOffset;
474 __be16 wAddress;
475 __be32 dwData [6];
476} __attribute__ ((packed));
477
478/* structures representing swap information */
479struct swap_info_e1 {
matthieu castetb72458a2005-11-07 23:27:13 +0100480 __u8 bSwapPageNo;
481 __u8 bOvl; /* overlay */
482} __attribute__ ((packed));
483
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +0200484struct swap_info_e4 {
485 __u8 bSwapPageNo;
486} __attribute__ ((packed));
487
488/* structures representing interrupt data */
489#define e1_bSwapPageNo u.e1.s1.swapinfo.bSwapPageNo
490#define e1_bOvl u.e1.s1.swapinfo.bOvl
491#define e4_bSwapPageNo u.e4.s1.swapinfo.bSwapPageNo
492
493#define INT_LOADSWAPPAGE 0x0001
494#define INT_INCOMINGCMV 0x0002
495
496union intr_data_e1 {
497 struct {
498 struct swap_info_e1 swapinfo;
499 __le16 wDataSize;
500 } __attribute__ ((packed)) s1;
501 struct {
502 struct cmv_e1 cmv;
503 __le16 wDataSize;
504 } __attribute__ ((packed)) s2;
505} __attribute__ ((packed));
506
507union intr_data_e4 {
508 struct {
509 struct swap_info_e4 swapinfo;
510 __le16 wDataSize;
511 } __attribute__ ((packed)) s1;
512 struct {
513 struct cmv_e4 cmv;
514 __le16 wDataSize;
515 } __attribute__ ((packed)) s2;
516} __attribute__ ((packed));
517
matthieu castetb72458a2005-11-07 23:27:13 +0100518struct intr_pkt {
519 __u8 bType;
520 __u8 bNotification;
521 __le16 wValue;
522 __le16 wIndex;
523 __le16 wLength;
524 __le16 wInterrupt;
matthieu castetb72458a2005-11-07 23:27:13 +0100525 union {
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +0200526 union intr_data_e1 e1;
527 union intr_data_e4 e4;
528 } u;
matthieu castetb72458a2005-11-07 23:27:13 +0100529} __attribute__ ((packed));
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +0200530
531#define E1_INTR_PKT_SIZE 28
532#define E4_INTR_PKT_SIZE 64
matthieu castetb72458a2005-11-07 23:27:13 +0100533
534static struct usb_driver uea_driver;
Arjan van de Venab3c81f2006-01-13 15:52:55 +0100535static DEFINE_MUTEX(uea_mutex);
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +0200536static const char *chip_name[] = {"ADI930", "Eagle I", "Eagle II", "Eagle III", "Eagle IV"};
matthieu castetb72458a2005-11-07 23:27:13 +0100537
538static int modem_index;
539static unsigned int debug;
Stanislaw Gruszka503add42007-08-20 23:21:06 +0200540static unsigned int altsetting[NB_MODEM] = {[0 ... (NB_MODEM - 1)] = FASTEST_ISO_INTF};
matthieu castetb72458a2005-11-07 23:27:13 +0100541static int sync_wait[NB_MODEM];
542static char *cmv_file[NB_MODEM];
Stanislaw Gruszka603cf602007-08-20 23:21:01 +0200543static int annex[NB_MODEM];
matthieu castetb72458a2005-11-07 23:27:13 +0100544
545module_param(debug, uint, 0644);
546MODULE_PARM_DESC(debug, "module debug level (0=off,1=on,2=verbose)");
Stanislaw Gruszka503add42007-08-20 23:21:06 +0200547module_param_array(altsetting, uint, NULL, 0644);
548MODULE_PARM_DESC(altsetting, "alternate setting for incoming traffic: 0=bulk, "
549 "1=isoc slowest, ... , 8=isoc fastest (default)");
matthieu castetb72458a2005-11-07 23:27:13 +0100550module_param_array(sync_wait, bool, NULL, 0644);
551MODULE_PARM_DESC(sync_wait, "wait the synchronisation before starting ATM");
552module_param_array(cmv_file, charp, NULL, 0644);
553MODULE_PARM_DESC(cmv_file,
554 "file name with configuration and management variables");
Stanislaw Gruszka603cf602007-08-20 23:21:01 +0200555module_param_array(annex, uint, NULL, 0644);
556MODULE_PARM_DESC(annex,
557 "manually set annex a/b (0=auto, 1=annex a, 2=annex b)");
matthieu castetb72458a2005-11-07 23:27:13 +0100558
Stanislaw Gruszka337427f2007-08-20 23:21:14 +0200559#define uea_wait(sc, cond, timeo) \
560({ \
561 int _r = wait_event_interruptible_timeout(sc->sync_q, \
562 (cond) || kthread_should_stop(), timeo); \
563 if (kthread_should_stop()) \
564 _r = -ENODEV; \
565 _r; \
566})
567
matthieu castetb72458a2005-11-07 23:27:13 +0100568#define UPDATE_ATM_STAT(type, val) \
569 do { \
570 if (sc->usbatm->atm_dev) \
571 sc->usbatm->atm_dev->type = val; \
572 } while (0)
573
574/* Firmware loading */
575#define LOAD_INTERNAL 0xA0
576#define F8051_USBCS 0x7f92
577
578/**
579 * uea_send_modem_cmd - Send a command for pre-firmware devices.
580 */
581static int uea_send_modem_cmd(struct usb_device *usb,
David Woodhouseaafcd2f2008-05-24 00:05:10 +0100582 u16 addr, u16 size, const u8 *buff)
matthieu castetb72458a2005-11-07 23:27:13 +0100583{
584 int ret = -ENOMEM;
585 u8 *xfer_buff;
586
Eric Sesterhenn5d7efe52006-10-26 21:06:24 +0200587 xfer_buff = kmemdup(buff, size, GFP_KERNEL);
matthieu castetb72458a2005-11-07 23:27:13 +0100588 if (xfer_buff) {
matthieu castetb72458a2005-11-07 23:27:13 +0100589 ret = usb_control_msg(usb,
590 usb_sndctrlpipe(usb, 0),
591 LOAD_INTERNAL,
592 USB_DIR_OUT | USB_TYPE_VENDOR |
593 USB_RECIP_DEVICE, addr, 0, xfer_buff,
594 size, CTRL_TIMEOUT);
595 kfree(xfer_buff);
596 }
597
598 if (ret < 0)
599 return ret;
600
601 return (ret == size) ? 0 : -EIO;
602}
603
604static void uea_upload_pre_firmware(const struct firmware *fw_entry, void *context)
605{
606 struct usb_device *usb = context;
David Woodhouseaafcd2f2008-05-24 00:05:10 +0100607 const u8 *pfw;
608 u8 value;
matthieu castetb72458a2005-11-07 23:27:13 +0100609 u32 crc = 0;
610 int ret, size;
611
612 uea_enters(usb);
613 if (!fw_entry) {
614 uea_err(usb, "firmware is not available\n");
615 goto err;
616 }
617
618 pfw = fw_entry->data;
619 size = fw_entry->size;
matthieu castet8d7802e2005-11-08 00:02:30 +0100620 if (size < 4)
621 goto err_fw_corrupted;
matthieu castetb72458a2005-11-07 23:27:13 +0100622
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700623 crc = get_unaligned_le32(pfw);
matthieu castetb72458a2005-11-07 23:27:13 +0100624 pfw += 4;
625 size -= 4;
matthieu castet8d7802e2005-11-08 00:02:30 +0100626 if (crc32_be(0, pfw, size) != crc)
627 goto err_fw_corrupted;
matthieu castetb72458a2005-11-07 23:27:13 +0100628
629 /*
Nick Andrew877d0312009-01-26 11:06:57 +0100630 * Start to upload firmware : send reset
matthieu castetb72458a2005-11-07 23:27:13 +0100631 */
632 value = 1;
633 ret = uea_send_modem_cmd(usb, F8051_USBCS, sizeof(value), &value);
634
635 if (ret < 0) {
636 uea_err(usb, "modem reset failed with error %d\n", ret);
637 goto err;
638 }
639
matthieu castet8d7802e2005-11-08 00:02:30 +0100640 while (size > 3) {
matthieu castetb72458a2005-11-07 23:27:13 +0100641 u8 len = FW_GET_BYTE(pfw);
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700642 u16 add = get_unaligned_le16(pfw + 1);
matthieu castet8d7802e2005-11-08 00:02:30 +0100643
644 size -= len + 3;
645 if (size < 0)
646 goto err_fw_corrupted;
647
matthieu castetb72458a2005-11-07 23:27:13 +0100648 ret = uea_send_modem_cmd(usb, add, len, pfw + 3);
649 if (ret < 0) {
650 uea_err(usb, "uploading firmware data failed "
651 "with error %d\n", ret);
652 goto err;
653 }
654 pfw += len + 3;
matthieu castetb72458a2005-11-07 23:27:13 +0100655 }
656
matthieu castet8d7802e2005-11-08 00:02:30 +0100657 if (size != 0)
658 goto err_fw_corrupted;
659
matthieu castetb72458a2005-11-07 23:27:13 +0100660 /*
661 * Tell the modem we finish : de-assert reset
662 */
663 value = 0;
664 ret = uea_send_modem_cmd(usb, F8051_USBCS, 1, &value);
665 if (ret < 0)
666 uea_err(usb, "modem de-assert failed with error %d\n", ret);
667 else
668 uea_info(usb, "firmware uploaded\n");
669
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100670 goto err;
matthieu castet8d7802e2005-11-08 00:02:30 +0100671
672err_fw_corrupted:
673 uea_err(usb, "firmware is corrupted\n");
matthieu castetb72458a2005-11-07 23:27:13 +0100674err:
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100675 release_firmware(fw_entry);
matthieu castetb72458a2005-11-07 23:27:13 +0100676 uea_leaves(usb);
677}
678
679/**
680 * uea_load_firmware - Load usb firmware for pre-firmware devices.
681 */
682static int uea_load_firmware(struct usb_device *usb, unsigned int ver)
683{
684 int ret;
685 char *fw_name = FW_DIR "eagle.fw";
686
687 uea_enters(usb);
688 uea_info(usb, "pre-firmware device, uploading firmware\n");
689
690 switch (ver) {
691 case ADI930:
692 fw_name = FW_DIR "adi930.fw";
693 break;
694 case EAGLE_I:
695 fw_name = FW_DIR "eagleI.fw";
696 break;
697 case EAGLE_II:
698 fw_name = FW_DIR "eagleII.fw";
699 break;
700 case EAGLE_III:
701 fw_name = FW_DIR "eagleIII.fw";
702 break;
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +0200703 case EAGLE_IV:
704 fw_name = FW_DIR "eagleIV.fw";
705 break;
matthieu castetb72458a2005-11-07 23:27:13 +0100706 }
707
Johannes Berg9ebfbd42009-10-29 12:36:02 +0100708 ret = request_firmware_nowait(THIS_MODULE, 1, fw_name, &usb->dev,
709 GFP_KERNEL, usb, uea_upload_pre_firmware);
matthieu castetb72458a2005-11-07 23:27:13 +0100710 if (ret)
711 uea_err(usb, "firmware %s is not available\n", fw_name);
712 else
713 uea_info(usb, "loading firmware %s\n", fw_name);
714
715 uea_leaves(usb);
716 return ret;
717}
718
719/* modem management : dsp firmware, send/read CMV, monitoring statistic
720 */
721
722/*
723 * Make sure that the DSP code provided is safe to use.
724 */
David Woodhouseaafcd2f2008-05-24 00:05:10 +0100725static int check_dsp_e1(const u8 *dsp, unsigned int len)
matthieu castetb72458a2005-11-07 23:27:13 +0100726{
727 u8 pagecount, blockcount;
728 u16 blocksize;
729 u32 pageoffset;
730 unsigned int i, j, p, pp;
731
matthieu castetb72458a2005-11-07 23:27:13 +0100732 pagecount = FW_GET_BYTE(dsp);
733 p = 1;
734
735 /* enough space for page offsets? */
736 if (p + 4 * pagecount > len)
737 return 1;
738
739 for (i = 0; i < pagecount; i++) {
740
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700741 pageoffset = get_unaligned_le32(dsp + p);
matthieu castetb72458a2005-11-07 23:27:13 +0100742 p += 4;
743
744 if (pageoffset == 0)
745 continue;
746
747 /* enough space for blockcount? */
748 if (pageoffset >= len)
749 return 1;
750
751 pp = pageoffset;
752 blockcount = FW_GET_BYTE(dsp + pp);
753 pp += 1;
754
755 for (j = 0; j < blockcount; j++) {
756
757 /* enough space for block header? */
758 if (pp + 4 > len)
759 return 1;
760
761 pp += 2; /* skip blockaddr */
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700762 blocksize = get_unaligned_le16(dsp + pp);
matthieu castetb72458a2005-11-07 23:27:13 +0100763 pp += 2;
764
765 /* enough space for block data? */
766 if (pp + blocksize > len)
767 return 1;
768
769 pp += blocksize;
770 }
771 }
772
773 return 0;
774}
775
David Woodhouseaafcd2f2008-05-24 00:05:10 +0100776static int check_dsp_e4(const u8 *dsp, int len)
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +0200777{
778 int i;
779 struct l1_code *p = (struct l1_code *) dsp;
780 unsigned int sum = p->code - dsp;
781
782 if (len < sum)
783 return 1;
784
785 if (strcmp("STRATIPHY ANEXA", p->string_header) != 0 &&
786 strcmp("STRATIPHY ANEXB", p->string_header) != 0)
787 return 1;
788
789 for (i = 0; i < E4_MAX_PAGE_NUMBER; i++) {
790 struct block_index *blockidx;
791 u8 blockno = p->page_number_to_block_index[i];
792 if (blockno >= E4_NO_SWAPPAGE_HEADERS)
793 continue;
794
795 do {
796 u64 l;
797
798 if (blockno >= E4_NO_SWAPPAGE_HEADERS)
799 return 1;
800
801 blockidx = &p->page_header[blockno++];
802 if ((u8 *)(blockidx + 1) - dsp >= len)
803 return 1;
804
805 if (le16_to_cpu(blockidx->PageNumber) != i)
806 return 1;
807
808 l = E4_PAGE_BYTES(blockidx->PageSize);
809 sum += l;
810 l += le32_to_cpu(blockidx->PageOffset);
811 if (l > len)
812 return 1;
813
814 /* zero is zero regardless endianes */
815 } while (blockidx->NotLastBlock);
816 }
817
818 return (sum == len) ? 0 : 1;
819}
820
matthieu castetb72458a2005-11-07 23:27:13 +0100821/*
822 * send data to the idma pipe
823 * */
David Woodhouseaafcd2f2008-05-24 00:05:10 +0100824static int uea_idma_write(struct uea_softc *sc, const void *data, u32 size)
matthieu castetb72458a2005-11-07 23:27:13 +0100825{
826 int ret = -ENOMEM;
827 u8 *xfer_buff;
828 int bytes_read;
829
Eric Sesterhenn5d7efe52006-10-26 21:06:24 +0200830 xfer_buff = kmemdup(data, size, GFP_KERNEL);
matthieu castetb72458a2005-11-07 23:27:13 +0100831 if (!xfer_buff) {
832 uea_err(INS_TO_USBDEV(sc), "can't allocate xfer_buff\n");
833 return ret;
834 }
835
matthieu castetb72458a2005-11-07 23:27:13 +0100836 ret = usb_bulk_msg(sc->usb_dev,
837 usb_sndbulkpipe(sc->usb_dev, UEA_IDMA_PIPE),
838 xfer_buff, size, &bytes_read, BULK_TIMEOUT);
839
840 kfree(xfer_buff);
841 if (ret < 0)
842 return ret;
843 if (size != bytes_read) {
844 uea_err(INS_TO_USBDEV(sc), "size != bytes_read %d %d\n", size,
845 bytes_read);
846 return -EIO;
847 }
848
849 return 0;
850}
851
852static int request_dsp(struct uea_softc *sc)
853{
854 int ret;
855 char *dsp_name;
856
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +0200857 if (UEA_CHIP_VERSION(sc) == EAGLE_IV) {
Stanislaw Gruszka603cf602007-08-20 23:21:01 +0200858 if (IS_ISDN(sc))
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +0200859 dsp_name = FW_DIR "DSP4i.bin";
860 else
861 dsp_name = FW_DIR "DSP4p.bin";
862 } else if (UEA_CHIP_VERSION(sc) == ADI930) {
Stanislaw Gruszka603cf602007-08-20 23:21:01 +0200863 if (IS_ISDN(sc))
matthieu castetb72458a2005-11-07 23:27:13 +0100864 dsp_name = FW_DIR "DSP9i.bin";
865 else
866 dsp_name = FW_DIR "DSP9p.bin";
867 } else {
Stanislaw Gruszka603cf602007-08-20 23:21:01 +0200868 if (IS_ISDN(sc))
matthieu castetb72458a2005-11-07 23:27:13 +0100869 dsp_name = FW_DIR "DSPei.bin";
870 else
871 dsp_name = FW_DIR "DSPep.bin";
872 }
873
matthieu castete40abaf2006-01-18 07:38:37 +0100874 ret = request_firmware(&sc->dsp_firm, dsp_name, &sc->usb_dev->dev);
matthieu castetb72458a2005-11-07 23:27:13 +0100875 if (ret < 0) {
876 uea_err(INS_TO_USBDEV(sc),
877 "requesting firmware %s failed with error %d\n",
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +0200878 dsp_name, ret);
matthieu castetb72458a2005-11-07 23:27:13 +0100879 return ret;
880 }
881
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +0200882 if (UEA_CHIP_VERSION(sc) == EAGLE_IV)
883 ret = check_dsp_e4(sc->dsp_firm->data, sc->dsp_firm->size);
884 else
885 ret = check_dsp_e1(sc->dsp_firm->data, sc->dsp_firm->size);
886
887 if (ret) {
matthieu castetb72458a2005-11-07 23:27:13 +0100888 uea_err(INS_TO_USBDEV(sc), "firmware %s is corrupted\n",
889 dsp_name);
890 release_firmware(sc->dsp_firm);
891 sc->dsp_firm = NULL;
892 return -EILSEQ;
893 }
894
895 return 0;
896}
897
898/*
899 * The uea_load_page() function must be called within a process context
900 */
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +0200901static void uea_load_page_e1(struct work_struct *work)
matthieu castetb72458a2005-11-07 23:27:13 +0100902{
David Howellsc4028952006-11-22 14:57:56 +0000903 struct uea_softc *sc = container_of(work, struct uea_softc, task);
matthieu castetb72458a2005-11-07 23:27:13 +0100904 u16 pageno = sc->pageno;
905 u16 ovl = sc->ovl;
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +0200906 struct block_info_e1 bi;
matthieu castetb72458a2005-11-07 23:27:13 +0100907
David Woodhouseaafcd2f2008-05-24 00:05:10 +0100908 const u8 *p;
matthieu castetb72458a2005-11-07 23:27:13 +0100909 u8 pagecount, blockcount;
910 u16 blockaddr, blocksize;
911 u32 pageoffset;
912 int i;
913
914 /* reload firmware when reboot start and it's loaded already */
915 if (ovl == 0 && pageno == 0 && sc->dsp_firm) {
916 release_firmware(sc->dsp_firm);
917 sc->dsp_firm = NULL;
918 }
919
920 if (sc->dsp_firm == NULL && request_dsp(sc) < 0)
921 return;
922
923 p = sc->dsp_firm->data;
924 pagecount = FW_GET_BYTE(p);
925 p += 1;
926
927 if (pageno >= pagecount)
928 goto bad1;
929
930 p += 4 * pageno;
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700931 pageoffset = get_unaligned_le32(p);
matthieu castetb72458a2005-11-07 23:27:13 +0100932
933 if (pageoffset == 0)
934 goto bad1;
935
936 p = sc->dsp_firm->data + pageoffset;
937 blockcount = FW_GET_BYTE(p);
938 p += 1;
939
940 uea_dbg(INS_TO_USBDEV(sc),
941 "sending %u blocks for DSP page %u\n", blockcount, pageno);
942
943 bi.wHdr = cpu_to_le16(UEA_BIHDR);
944 bi.wOvl = cpu_to_le16(ovl);
945 bi.wOvlOffset = cpu_to_le16(ovl | 0x8000);
946
947 for (i = 0; i < blockcount; i++) {
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700948 blockaddr = get_unaligned_le16(p);
matthieu castetb72458a2005-11-07 23:27:13 +0100949 p += 2;
950
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700951 blocksize = get_unaligned_le16(p);
matthieu castetb72458a2005-11-07 23:27:13 +0100952 p += 2;
953
954 bi.wSize = cpu_to_le16(blocksize);
955 bi.wAddress = cpu_to_le16(blockaddr);
956 bi.wLast = cpu_to_le16((i == blockcount - 1) ? 1 : 0);
957
958 /* send block info through the IDMA pipe */
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +0200959 if (uea_idma_write(sc, &bi, E1_BLOCK_INFO_SIZE))
matthieu castetb72458a2005-11-07 23:27:13 +0100960 goto bad2;
961
962 /* send block data through the IDMA pipe */
963 if (uea_idma_write(sc, p, blocksize))
964 goto bad2;
965
966 p += blocksize;
967 }
968
969 return;
970
971bad2:
972 uea_err(INS_TO_USBDEV(sc), "sending DSP block %u failed\n", i);
973 return;
974bad1:
matthieu castet2a99b502006-04-02 18:43:53 +0200975 uea_err(INS_TO_USBDEV(sc), "invalid DSP page %u requested\n", pageno);
matthieu castetb72458a2005-11-07 23:27:13 +0100976}
977
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +0200978static void __uea_load_page_e4(struct uea_softc *sc, u8 pageno, int boot)
979{
980 struct block_info_e4 bi;
981 struct block_index *blockidx;
982 struct l1_code *p = (struct l1_code *) sc->dsp_firm->data;
983 u8 blockno = p->page_number_to_block_index[pageno];
984
985 bi.wHdr = cpu_to_be16(UEA_BIHDR);
986 bi.bBootPage = boot;
987 bi.bPageNumber = pageno;
988 bi.wReserved = cpu_to_be16(UEA_RESERVED);
989
990 do {
David Woodhouseaafcd2f2008-05-24 00:05:10 +0100991 const u8 *blockoffset;
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +0200992 unsigned int blocksize;
993
994 blockidx = &p->page_header[blockno];
995 blocksize = E4_PAGE_BYTES(blockidx->PageSize);
996 blockoffset = sc->dsp_firm->data + le32_to_cpu(blockidx->PageOffset);
997
998 bi.dwSize = cpu_to_be32(blocksize);
Al Virofd05e722008-04-28 07:00:16 +0100999 bi.dwAddress = cpu_to_be32(le32_to_cpu(blockidx->PageAddress));
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001000
1001 uea_dbg(INS_TO_USBDEV(sc),
Joe Perchesdc0d5c12007-12-17 11:40:18 -08001002 "sending block %u for DSP page %u size %u address %x\n",
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001003 blockno, pageno, blocksize, le32_to_cpu(blockidx->PageAddress));
1004
1005 /* send block info through the IDMA pipe */
1006 if (uea_idma_write(sc, &bi, E4_BLOCK_INFO_SIZE))
1007 goto bad;
1008
1009 /* send block data through the IDMA pipe */
1010 if (uea_idma_write(sc, blockoffset, blocksize))
1011 goto bad;
1012
1013 blockno++;
1014 } while (blockidx->NotLastBlock);
1015
1016 return;
1017
1018bad:
1019 uea_err(INS_TO_USBDEV(sc), "sending DSP block %u failed\n", blockno);
1020 return;
1021}
1022
1023static void uea_load_page_e4(struct work_struct *work)
1024{
1025 struct uea_softc *sc = container_of(work, struct uea_softc, task);
1026 u8 pageno = sc->pageno;
1027 int i;
1028 struct block_info_e4 bi;
1029 struct l1_code *p;
1030
1031 uea_dbg(INS_TO_USBDEV(sc), "sending DSP page %u\n", pageno);
1032
1033 /* reload firmware when reboot start and it's loaded already */
1034 if (pageno == 0 && sc->dsp_firm) {
1035 release_firmware(sc->dsp_firm);
1036 sc->dsp_firm = NULL;
1037 }
1038
1039 if (sc->dsp_firm == NULL && request_dsp(sc) < 0)
1040 return;
1041
1042 p = (struct l1_code *) sc->dsp_firm->data;
Al Virofd05e722008-04-28 07:00:16 +01001043 if (pageno >= le16_to_cpu(p->page_header[0].PageNumber)) {
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001044 uea_err(INS_TO_USBDEV(sc), "invalid DSP page %u requested\n", pageno);
1045 return;
1046 }
1047
1048 if (pageno != 0) {
1049 __uea_load_page_e4(sc, pageno, 0);
1050 return;
1051 }
1052
1053 uea_dbg(INS_TO_USBDEV(sc),
1054 "sending Main DSP page %u\n", p->page_header[0].PageNumber);
1055
1056 for (i = 0; i < le16_to_cpu(p->page_header[0].PageNumber); i++) {
1057 if (E4_IS_BOOT_PAGE(p->page_header[i].PageSize))
1058 __uea_load_page_e4(sc, i, 1);
1059 }
1060
1061 uea_dbg(INS_TO_USBDEV(sc),"sending start bi\n");
1062
1063 bi.wHdr = cpu_to_be16(UEA_BIHDR);
1064 bi.bBootPage = 0;
1065 bi.bPageNumber = 0xff;
1066 bi.wReserved = cpu_to_be16(UEA_RESERVED);
1067 bi.dwSize = cpu_to_be32(E4_PAGE_BYTES(p->page_header[0].PageSize));
Al Virofd05e722008-04-28 07:00:16 +01001068 bi.dwAddress = cpu_to_be32(le32_to_cpu(p->page_header[0].PageAddress));
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001069
1070 /* send block info through the IDMA pipe */
1071 if (uea_idma_write(sc, &bi, E4_BLOCK_INFO_SIZE))
1072 uea_err(INS_TO_USBDEV(sc), "sending DSP start bi failed\n");
1073}
1074
matthieu castetb72458a2005-11-07 23:27:13 +01001075static inline void wake_up_cmv_ack(struct uea_softc *sc)
1076{
matthieu castet2a99b502006-04-02 18:43:53 +02001077 BUG_ON(sc->cmv_ack);
matthieu castetb72458a2005-11-07 23:27:13 +01001078 sc->cmv_ack = 1;
Stanislaw Gruszka337427f2007-08-20 23:21:14 +02001079 wake_up(&sc->sync_q);
matthieu castetb72458a2005-11-07 23:27:13 +01001080}
1081
1082static inline int wait_cmv_ack(struct uea_softc *sc)
1083{
Stanislaw Gruszka337427f2007-08-20 23:21:14 +02001084 int ret = uea_wait(sc, sc->cmv_ack , ACK_TIMEOUT);
1085
matthieu castetb72458a2005-11-07 23:27:13 +01001086 sc->cmv_ack = 0;
1087
matthieu castet2a99b502006-04-02 18:43:53 +02001088 uea_dbg(INS_TO_USBDEV(sc), "wait_event_timeout : %d ms\n",
1089 jiffies_to_msecs(ret));
1090
matthieu castetb72458a2005-11-07 23:27:13 +01001091 if (ret < 0)
1092 return ret;
1093
1094 return (ret == 0) ? -ETIMEDOUT : 0;
matthieu castetb72458a2005-11-07 23:27:13 +01001095}
1096
1097#define UCDC_SEND_ENCAPSULATED_COMMAND 0x00
1098
1099static int uea_request(struct uea_softc *sc,
David Woodhouseaafcd2f2008-05-24 00:05:10 +01001100 u16 value, u16 index, u16 size, const void *data)
matthieu castetb72458a2005-11-07 23:27:13 +01001101{
1102 u8 *xfer_buff;
1103 int ret = -ENOMEM;
1104
Eric Sesterhenn5d7efe52006-10-26 21:06:24 +02001105 xfer_buff = kmemdup(data, size, GFP_KERNEL);
matthieu castetb72458a2005-11-07 23:27:13 +01001106 if (!xfer_buff) {
1107 uea_err(INS_TO_USBDEV(sc), "can't allocate xfer_buff\n");
1108 return ret;
1109 }
matthieu castetb72458a2005-11-07 23:27:13 +01001110
1111 ret = usb_control_msg(sc->usb_dev, usb_sndctrlpipe(sc->usb_dev, 0),
1112 UCDC_SEND_ENCAPSULATED_COMMAND,
1113 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1114 value, index, xfer_buff, size, CTRL_TIMEOUT);
1115
1116 kfree(xfer_buff);
1117 if (ret < 0) {
1118 uea_err(INS_TO_USBDEV(sc), "usb_control_msg error %d\n", ret);
1119 return ret;
1120 }
1121
1122 if (ret != size) {
1123 uea_err(INS_TO_USBDEV(sc),
1124 "usb_control_msg send only %d bytes (instead of %d)\n",
1125 ret, size);
1126 return -EIO;
1127 }
1128
1129 return 0;
1130}
1131
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001132static int uea_cmv_e1(struct uea_softc *sc,
matthieu castetb72458a2005-11-07 23:27:13 +01001133 u8 function, u32 address, u16 offset, u32 data)
1134{
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001135 struct cmv_e1 cmv;
matthieu castetb72458a2005-11-07 23:27:13 +01001136 int ret;
1137
matthieu castet2a99b502006-04-02 18:43:53 +02001138 uea_enters(INS_TO_USBDEV(sc));
1139 uea_vdbg(INS_TO_USBDEV(sc), "Function : %d-%d, Address : %c%c%c%c, "
1140 "offset : 0x%04x, data : 0x%08x\n",
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001141 E1_FUNCTION_TYPE(function), E1_FUNCTION_SUBTYPE(function),
1142 E1_GETSA1(address), E1_GETSA2(address), E1_GETSA3(address),
1143 E1_GETSA4(address), offset, data);
matthieu castetb72458a2005-11-07 23:27:13 +01001144
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001145 /* we send a request, but we expect a reply */
1146 sc->cmv_dsc.e1.function = function | 0x2;
1147 sc->cmv_dsc.e1.idx++;
1148 sc->cmv_dsc.e1.address = address;
1149 sc->cmv_dsc.e1.offset = offset;
1150
1151 cmv.wPreamble = cpu_to_le16(E1_PREAMBLE);
1152 cmv.bDirection = E1_HOSTTOMODEM;
matthieu castetb72458a2005-11-07 23:27:13 +01001153 cmv.bFunction = function;
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001154 cmv.wIndex = cpu_to_le16(sc->cmv_dsc.e1.idx);
Harvey Harrisona5abdea2008-04-29 01:03:40 -07001155 put_unaligned_le32(address, &cmv.dwSymbolicAddress);
matthieu castetb72458a2005-11-07 23:27:13 +01001156 cmv.wOffsetAddress = cpu_to_le16(offset);
Harvey Harrisona5abdea2008-04-29 01:03:40 -07001157 put_unaligned_le32(data >> 16 | data << 16, &cmv.dwData);
matthieu castetb72458a2005-11-07 23:27:13 +01001158
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001159 ret = uea_request(sc, UEA_E1_SET_BLOCK, UEA_MPTX_START, sizeof(cmv), &cmv);
matthieu castetb72458a2005-11-07 23:27:13 +01001160 if (ret < 0)
1161 return ret;
matthieu castet2a99b502006-04-02 18:43:53 +02001162 ret = wait_cmv_ack(sc);
1163 uea_leaves(INS_TO_USBDEV(sc));
1164 return ret;
matthieu castetb72458a2005-11-07 23:27:13 +01001165}
1166
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001167static int uea_cmv_e4(struct uea_softc *sc,
1168 u16 function, u16 group, u16 address, u16 offset, u32 data)
1169{
1170 struct cmv_e4 cmv;
1171 int ret;
1172
1173 uea_enters(INS_TO_USBDEV(sc));
1174 memset(&cmv, 0, sizeof(cmv));
1175
1176 uea_vdbg(INS_TO_USBDEV(sc), "Function : %d-%d, Group : 0x%04x, "
1177 "Address : 0x%04x, offset : 0x%04x, data : 0x%08x\n",
1178 E4_FUNCTION_TYPE(function), E4_FUNCTION_SUBTYPE(function),
1179 group, address, offset, data);
1180
1181 /* we send a request, but we expect a reply */
1182 sc->cmv_dsc.e4.function = function | (0x1 << 4);
1183 sc->cmv_dsc.e4.offset = offset;
1184 sc->cmv_dsc.e4.address = address;
1185 sc->cmv_dsc.e4.group = group;
1186
1187 cmv.wFunction = cpu_to_be16(function);
1188 cmv.wGroup = cpu_to_be16(group);
1189 cmv.wAddress = cpu_to_be16(address);
1190 cmv.wOffset = cpu_to_be16(offset);
1191 cmv.dwData[0] = cpu_to_be32(data);
1192
1193 ret = uea_request(sc, UEA_E4_SET_BLOCK, UEA_MPTX_START, sizeof(cmv), &cmv);
1194 if (ret < 0)
1195 return ret;
1196 ret = wait_cmv_ack(sc);
1197 uea_leaves(INS_TO_USBDEV(sc));
1198 return ret;
1199}
1200
1201static inline int uea_read_cmv_e1(struct uea_softc *sc,
matthieu castetb72458a2005-11-07 23:27:13 +01001202 u32 address, u16 offset, u32 *data)
1203{
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001204 int ret = uea_cmv_e1(sc, E1_MAKEFUNCTION(E1_MEMACCESS, E1_REQUESTREAD),
matthieu castetb72458a2005-11-07 23:27:13 +01001205 address, offset, 0);
1206 if (ret < 0)
1207 uea_err(INS_TO_USBDEV(sc),
1208 "reading cmv failed with error %d\n", ret);
1209 else
1210 *data = sc->data;
1211
1212 return ret;
1213}
1214
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001215static inline int uea_read_cmv_e4(struct uea_softc *sc,
1216 u8 size, u16 group, u16 address, u16 offset, u32 *data)
1217{
1218 int ret = uea_cmv_e4(sc, E4_MAKEFUNCTION(E4_MEMACCESS, E4_REQUESTREAD, size),
1219 group, address, offset, 0);
1220 if (ret < 0)
1221 uea_err(INS_TO_USBDEV(sc),
1222 "reading cmv failed with error %d\n", ret);
1223 else {
1224 *data = sc->data;
1225 /* size is in 16-bit word quantities */
1226 if (size > 2)
1227 *(data + 1) = sc->data1;
1228 }
1229 return ret;
1230}
1231
1232static inline int uea_write_cmv_e1(struct uea_softc *sc,
matthieu castetb72458a2005-11-07 23:27:13 +01001233 u32 address, u16 offset, u32 data)
1234{
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001235 int ret = uea_cmv_e1(sc, E1_MAKEFUNCTION(E1_MEMACCESS, E1_REQUESTWRITE),
matthieu castetb72458a2005-11-07 23:27:13 +01001236 address, offset, data);
1237 if (ret < 0)
1238 uea_err(INS_TO_USBDEV(sc),
1239 "writing cmv failed with error %d\n", ret);
1240
1241 return ret;
1242}
1243
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001244static inline int uea_write_cmv_e4(struct uea_softc *sc,
1245 u8 size, u16 group, u16 address, u16 offset, u32 data)
1246{
1247 int ret = uea_cmv_e4(sc, E4_MAKEFUNCTION(E4_MEMACCESS, E4_REQUESTWRITE, size),
1248 group, address, offset, data);
1249 if (ret < 0)
1250 uea_err(INS_TO_USBDEV(sc),
1251 "writing cmv failed with error %d\n", ret);
1252
1253 return ret;
1254}
1255
1256static void uea_set_bulk_timeout(struct uea_softc *sc, u32 dsrate)
1257{
1258 int ret;
1259 u16 timeout;
1260
1261 /* in bulk mode the modem have problem with high rate
1262 * changing internal timing could improve things, but the
1263 * value is misterious.
1264 * ADI930 don't support it (-EPIPE error).
1265 */
1266
1267 if (UEA_CHIP_VERSION(sc) == ADI930 ||
Stanislaw Gruszka503add42007-08-20 23:21:06 +02001268 altsetting[sc->modem_index] > 0 ||
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001269 sc->stats.phy.dsrate == dsrate)
1270 return;
1271
1272 /* Original timming (1Mbit/s) from ADI (used in windows driver) */
1273 timeout = (dsrate <= 1024*1024) ? 0 : 1;
1274 ret = uea_request(sc, UEA_SET_TIMEOUT, timeout, 0, NULL);
1275 uea_info(INS_TO_USBDEV(sc), "setting new timeout %d%s\n",
1276 timeout, ret < 0 ? " failed" : "");
1277
1278}
1279
matthieu castetb72458a2005-11-07 23:27:13 +01001280/*
1281 * Monitor the modem and update the stat
1282 * return 0 if everything is ok
1283 * return < 0 if an error occurs (-EAGAIN reboot needed)
1284 */
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001285static int uea_stat_e1(struct uea_softc *sc)
matthieu castetb72458a2005-11-07 23:27:13 +01001286{
1287 u32 data;
1288 int ret;
1289
1290 uea_enters(INS_TO_USBDEV(sc));
1291 data = sc->stats.phy.state;
1292
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001293 ret = uea_read_cmv_e1(sc, E1_SA_STAT, 0, &sc->stats.phy.state);
matthieu castetb72458a2005-11-07 23:27:13 +01001294 if (ret < 0)
1295 return ret;
1296
1297 switch (GET_STATUS(sc->stats.phy.state)) {
1298 case 0: /* not yet synchronized */
1299 uea_dbg(INS_TO_USBDEV(sc),
1300 "modem not yet synchronized\n");
1301 return 0;
1302
1303 case 1: /* initialization */
1304 uea_dbg(INS_TO_USBDEV(sc), "modem initializing\n");
1305 return 0;
1306
1307 case 2: /* operational */
1308 uea_vdbg(INS_TO_USBDEV(sc), "modem operational\n");
1309 break;
1310
1311 case 3: /* fail ... */
matthieu casteta7a0c9c2006-10-03 21:44:11 +02001312 uea_info(INS_TO_USBDEV(sc), "modem synchronization failed"
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001313 " (may be try other cmv/dsp)\n");
matthieu castetb72458a2005-11-07 23:27:13 +01001314 return -EAGAIN;
1315
1316 case 4 ... 6: /* test state */
1317 uea_warn(INS_TO_USBDEV(sc),
1318 "modem in test mode - not supported\n");
1319 return -EAGAIN;
1320
1321 case 7: /* fast-retain ... */
1322 uea_info(INS_TO_USBDEV(sc), "modem in fast-retain mode\n");
1323 return 0;
1324 default:
1325 uea_err(INS_TO_USBDEV(sc), "modem invalid SW mode %d\n",
1326 GET_STATUS(sc->stats.phy.state));
1327 return -EAGAIN;
1328 }
1329
1330 if (GET_STATUS(data) != 2) {
1331 uea_request(sc, UEA_SET_MODE, UEA_LOOPBACK_OFF, 0, NULL);
1332 uea_info(INS_TO_USBDEV(sc), "modem operational\n");
1333
1334 /* release the dsp firmware as it is not needed until
1335 * the next failure
1336 */
1337 if (sc->dsp_firm) {
1338 release_firmware(sc->dsp_firm);
1339 sc->dsp_firm = NULL;
1340 }
matthieu castetb72458a2005-11-07 23:27:13 +01001341 }
1342
1343 /* always update it as atm layer could not be init when we switch to
1344 * operational state
1345 */
1346 UPDATE_ATM_STAT(signal, ATM_PHY_SIG_FOUND);
1347
1348 /* wake up processes waiting for synchronization */
1349 wake_up(&sc->sync_q);
1350
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001351 ret = uea_read_cmv_e1(sc, E1_SA_DIAG, 2, &sc->stats.phy.flags);
matthieu castetb72458a2005-11-07 23:27:13 +01001352 if (ret < 0)
1353 return ret;
1354 sc->stats.phy.mflags |= sc->stats.phy.flags;
1355
1356 /* in case of a flags ( for example delineation LOSS (& 0x10)),
1357 * we check the status again in order to detect the failure earlier
1358 */
1359 if (sc->stats.phy.flags) {
matthieu castet2a99b502006-04-02 18:43:53 +02001360 uea_dbg(INS_TO_USBDEV(sc), "Stat flag = 0x%x\n",
matthieu castetb72458a2005-11-07 23:27:13 +01001361 sc->stats.phy.flags);
1362 return 0;
1363 }
1364
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001365 ret = uea_read_cmv_e1(sc, E1_SA_RATE, 0, &data);
matthieu castetb72458a2005-11-07 23:27:13 +01001366 if (ret < 0)
1367 return ret;
1368
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001369 uea_set_bulk_timeout(sc, (data >> 16) * 32);
matthieu castetb72458a2005-11-07 23:27:13 +01001370 sc->stats.phy.dsrate = (data >> 16) * 32;
1371 sc->stats.phy.usrate = (data & 0xffff) * 32;
1372 UPDATE_ATM_STAT(link_rate, sc->stats.phy.dsrate * 1000 / 424);
1373
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001374 ret = uea_read_cmv_e1(sc, E1_SA_DIAG, 23, &data);
matthieu castetb72458a2005-11-07 23:27:13 +01001375 if (ret < 0)
1376 return ret;
1377 sc->stats.phy.dsattenuation = (data & 0xff) / 2;
1378
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001379 ret = uea_read_cmv_e1(sc, E1_SA_DIAG, 47, &data);
matthieu castetb72458a2005-11-07 23:27:13 +01001380 if (ret < 0)
1381 return ret;
1382 sc->stats.phy.usattenuation = (data & 0xff) / 2;
1383
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001384 ret = uea_read_cmv_e1(sc, E1_SA_DIAG, 25, &sc->stats.phy.dsmargin);
matthieu castetb72458a2005-11-07 23:27:13 +01001385 if (ret < 0)
1386 return ret;
1387
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001388 ret = uea_read_cmv_e1(sc, E1_SA_DIAG, 49, &sc->stats.phy.usmargin);
matthieu castetb72458a2005-11-07 23:27:13 +01001389 if (ret < 0)
1390 return ret;
1391
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001392 ret = uea_read_cmv_e1(sc, E1_SA_DIAG, 51, &sc->stats.phy.rxflow);
matthieu castetb72458a2005-11-07 23:27:13 +01001393 if (ret < 0)
1394 return ret;
1395
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001396 ret = uea_read_cmv_e1(sc, E1_SA_DIAG, 52, &sc->stats.phy.txflow);
matthieu castetb72458a2005-11-07 23:27:13 +01001397 if (ret < 0)
1398 return ret;
1399
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001400 ret = uea_read_cmv_e1(sc, E1_SA_DIAG, 54, &sc->stats.phy.dsunc);
matthieu castetb72458a2005-11-07 23:27:13 +01001401 if (ret < 0)
1402 return ret;
1403
1404 /* only for atu-c */
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001405 ret = uea_read_cmv_e1(sc, E1_SA_DIAG, 58, &sc->stats.phy.usunc);
matthieu castetb72458a2005-11-07 23:27:13 +01001406 if (ret < 0)
1407 return ret;
1408
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001409 ret = uea_read_cmv_e1(sc, E1_SA_DIAG, 53, &sc->stats.phy.dscorr);
matthieu castetb72458a2005-11-07 23:27:13 +01001410 if (ret < 0)
1411 return ret;
1412
1413 /* only for atu-c */
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001414 ret = uea_read_cmv_e1(sc, E1_SA_DIAG, 57, &sc->stats.phy.uscorr);
matthieu castetb72458a2005-11-07 23:27:13 +01001415 if (ret < 0)
1416 return ret;
1417
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001418 ret = uea_read_cmv_e1(sc, E1_SA_INFO, 8, &sc->stats.phy.vidco);
matthieu castetb72458a2005-11-07 23:27:13 +01001419 if (ret < 0)
1420 return ret;
1421
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001422 ret = uea_read_cmv_e1(sc, E1_SA_INFO, 13, &sc->stats.phy.vidcpe);
matthieu castetb72458a2005-11-07 23:27:13 +01001423 if (ret < 0)
1424 return ret;
1425
1426 return 0;
1427}
1428
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001429static int uea_stat_e4(struct uea_softc *sc)
matthieu castetb72458a2005-11-07 23:27:13 +01001430{
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001431 u32 data;
1432 u32 tmp_arr[2];
1433 int ret;
matthieu castetb72458a2005-11-07 23:27:13 +01001434
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001435 uea_enters(INS_TO_USBDEV(sc));
1436 data = sc->stats.phy.state;
1437
1438 /* XXX only need to be done before operationnal... */
1439 ret = uea_read_cmv_e4(sc, 1, E4_SA_STAT, 0, 0, &sc->stats.phy.state);
1440 if (ret < 0)
1441 return ret;
1442
1443 switch (sc->stats.phy.state) {
1444 case 0x0: /* not yet synchronized */
1445 case 0x1:
1446 case 0x3:
1447 case 0x4:
1448 uea_dbg(INS_TO_USBDEV(sc), "modem not yet synchronized\n");
1449 return 0;
1450 case 0x5: /* initialization */
1451 case 0x6:
1452 case 0x9:
1453 case 0xa:
1454 uea_dbg(INS_TO_USBDEV(sc), "modem initializing\n");
1455 return 0;
1456 case 0x2: /* fail ... */
1457 uea_info(INS_TO_USBDEV(sc), "modem synchronization failed"
1458 " (may be try other cmv/dsp)\n");
1459 return -EAGAIN;
1460 case 0x7: /* operational */
1461 break;
1462 default:
1463 uea_warn(INS_TO_USBDEV(sc), "unknown state: %x\n", sc->stats.phy.state);
1464 return 0;
1465 }
1466
1467 if (data != 7) {
1468 uea_request(sc, UEA_SET_MODE, UEA_LOOPBACK_OFF, 0, NULL);
1469 uea_info(INS_TO_USBDEV(sc), "modem operational\n");
1470
1471 /* release the dsp firmware as it is not needed until
1472 * the next failure
1473 */
1474 if (sc->dsp_firm) {
1475 release_firmware(sc->dsp_firm);
1476 sc->dsp_firm = NULL;
1477 }
1478 }
1479
1480 /* always update it as atm layer could not be init when we switch to
1481 * operational state
1482 */
1483 UPDATE_ATM_STAT(signal, ATM_PHY_SIG_FOUND);
1484
1485 /* wake up processes waiting for synchronization */
1486 wake_up(&sc->sync_q);
1487
1488 /* TODO improve this state machine :
1489 * we need some CMV info : what they do and their unit
1490 * we should find the equivalent of eagle3- CMV
1491 */
1492 /* check flags */
1493 ret = uea_read_cmv_e4(sc, 1, E4_SA_DIAG, 0, 0, &sc->stats.phy.flags);
1494 if (ret < 0)
1495 return ret;
1496 sc->stats.phy.mflags |= sc->stats.phy.flags;
1497
1498 /* in case of a flags ( for example delineation LOSS (& 0x10)),
1499 * we check the status again in order to detect the failure earlier
1500 */
1501 if (sc->stats.phy.flags) {
1502 uea_dbg(INS_TO_USBDEV(sc), "Stat flag = 0x%x\n",
1503 sc->stats.phy.flags);
1504 if (sc->stats.phy.flags & 1) //delineation LOSS
1505 return -EAGAIN;
1506 if (sc->stats.phy.flags & 0x4000) //Reset Flag
1507 return -EAGAIN;
1508 return 0;
1509 }
1510
1511 /* rate data may be in upper or lower half of 64 bit word, strange */
1512 ret = uea_read_cmv_e4(sc, 4, E4_SA_RATE, 0, 0, tmp_arr);
1513 if (ret < 0)
1514 return ret;
1515 data = (tmp_arr[0]) ? tmp_arr[0] : tmp_arr[1];
1516 sc->stats.phy.usrate = data / 1000;
1517
1518 ret = uea_read_cmv_e4(sc, 4, E4_SA_RATE, 1, 0, tmp_arr);
1519 if (ret < 0)
1520 return ret;
1521 data = (tmp_arr[0]) ? tmp_arr[0] : tmp_arr[1];
1522 uea_set_bulk_timeout(sc, data / 1000);
1523 sc->stats.phy.dsrate = data / 1000;
1524 UPDATE_ATM_STAT(link_rate, sc->stats.phy.dsrate * 1000 / 424);
1525
1526 ret = uea_read_cmv_e4(sc, 1, E4_SA_INFO, 68, 1, &data);
1527 if (ret < 0)
1528 return ret;
1529 sc->stats.phy.dsattenuation = data / 10;
1530
1531 ret = uea_read_cmv_e4(sc, 1, E4_SA_INFO, 69, 1, &data);
1532 if (ret < 0)
1533 return ret;
1534 sc->stats.phy.usattenuation = data / 10;
1535
1536 ret = uea_read_cmv_e4(sc, 1, E4_SA_INFO, 68, 3, &data);
1537 if (ret < 0)
1538 return ret;
1539 sc->stats.phy.dsmargin = data / 2;
1540
1541 ret = uea_read_cmv_e4(sc, 1, E4_SA_INFO, 69, 3, &data);
1542 if (ret < 0)
1543 return ret;
1544 sc->stats.phy.usmargin = data / 10;
1545
1546 return 0;
1547}
1548
1549static void cmvs_file_name(struct uea_softc *sc, char *const cmv_name, int ver)
1550{
1551 char file_arr[] = "CMVxy.bin";
1552 char *file;
1553
1554 /* set proper name corresponding modem version and line type */
matthieu castetb72458a2005-11-07 23:27:13 +01001555 if (cmv_file[sc->modem_index] == NULL) {
1556 if (UEA_CHIP_VERSION(sc) == ADI930)
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001557 file_arr[3] = '9';
1558 else if (UEA_CHIP_VERSION(sc) == EAGLE_IV)
1559 file_arr[3] = '4';
matthieu castetb72458a2005-11-07 23:27:13 +01001560 else
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001561 file_arr[3] = 'e';
1562
Stanislaw Gruszka603cf602007-08-20 23:21:01 +02001563 file_arr[4] = IS_ISDN(sc) ? 'i' : 'p';
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001564 file = file_arr;
matthieu castetb72458a2005-11-07 23:27:13 +01001565 } else
1566 file = cmv_file[sc->modem_index];
1567
1568 strcpy(cmv_name, FW_DIR);
Samuel Ortizade901d2009-05-27 00:49:32 +02001569 strlcat(cmv_name, file, UEA_FW_NAME_MAX);
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001570 if (ver == 2)
Samuel Ortizade901d2009-05-27 00:49:32 +02001571 strlcat(cmv_name, ".v2", UEA_FW_NAME_MAX);
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001572}
matthieu castetb72458a2005-11-07 23:27:13 +01001573
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001574static int request_cmvs_old(struct uea_softc *sc,
1575 void **cmvs, const struct firmware **fw)
1576{
1577 int ret, size;
1578 u8 *data;
Samuel Ortizade901d2009-05-27 00:49:32 +02001579 char cmv_name[UEA_FW_NAME_MAX]; /* 30 bytes stack variable */
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001580
1581 cmvs_file_name(sc, cmv_name, 1);
matthieu castetb72458a2005-11-07 23:27:13 +01001582 ret = request_firmware(fw, cmv_name, &sc->usb_dev->dev);
1583 if (ret < 0) {
1584 uea_err(INS_TO_USBDEV(sc),
1585 "requesting firmware %s failed with error %d\n",
1586 cmv_name, ret);
1587 return ret;
1588 }
1589
1590 data = (u8 *) (*fw)->data;
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001591 size = (*fw)->size;
1592 if (size < 1)
1593 goto err_fw_corrupted;
1594
1595 if (size != *data * sizeof(struct uea_cmvs_v1) + 1)
1596 goto err_fw_corrupted;
1597
1598 *cmvs = (void *)(data + 1);
1599 return *data;
1600
1601err_fw_corrupted:
1602 uea_err(INS_TO_USBDEV(sc), "firmware %s is corrupted\n", cmv_name);
1603 release_firmware(*fw);
1604 return -EILSEQ;
1605}
1606
1607static int request_cmvs(struct uea_softc *sc,
1608 void **cmvs, const struct firmware **fw, int *ver)
1609{
1610 int ret, size;
1611 u32 crc;
1612 u8 *data;
Samuel Ortizade901d2009-05-27 00:49:32 +02001613 char cmv_name[UEA_FW_NAME_MAX]; /* 30 bytes stack variable */
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001614
1615 cmvs_file_name(sc, cmv_name, 2);
1616 ret = request_firmware(fw, cmv_name, &sc->usb_dev->dev);
1617 if (ret < 0) {
1618 /* if caller can handle old version, try to provide it */
1619 if (*ver == 1) {
1620 uea_warn(INS_TO_USBDEV(sc), "requesting firmware %s failed, "
1621 "try to get older cmvs\n", cmv_name);
1622 return request_cmvs_old(sc, cmvs, fw);
1623 }
1624 uea_err(INS_TO_USBDEV(sc),
1625 "requesting firmware %s failed with error %d\n",
1626 cmv_name, ret);
1627 return ret;
matthieu castetb72458a2005-11-07 23:27:13 +01001628 }
1629
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001630 size = (*fw)->size;
1631 data = (u8 *) (*fw)->data;
1632 if (size < 4 || strncmp(data, "cmv2", 4) != 0) {
1633 if (*ver == 1) {
1634 uea_warn(INS_TO_USBDEV(sc), "firmware %s is corrupted, "
1635 "try to get older cmvs\n", cmv_name);
1636 release_firmware(*fw);
1637 return request_cmvs_old(sc, cmvs, fw);
1638 }
1639 goto err_fw_corrupted;
1640 }
1641
1642 *ver = 2;
1643
1644 data += 4;
1645 size -= 4;
1646 if (size < 5)
1647 goto err_fw_corrupted;
1648
Harvey Harrisona5abdea2008-04-29 01:03:40 -07001649 crc = get_unaligned_le32(data);
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001650 data += 4;
1651 size -= 4;
1652 if (crc32_be(0, data, size) != crc)
1653 goto err_fw_corrupted;
1654
1655 if (size != *data * sizeof(struct uea_cmvs_v2) + 1)
1656 goto err_fw_corrupted;
1657
1658 *cmvs = (void *) (data + 1);
matthieu castetb72458a2005-11-07 23:27:13 +01001659 return *data;
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001660
1661err_fw_corrupted:
1662 uea_err(INS_TO_USBDEV(sc), "firmware %s is corrupted\n", cmv_name);
1663 release_firmware(*fw);
1664 return -EILSEQ;
1665}
1666
1667static int uea_send_cmvs_e1(struct uea_softc *sc)
1668{
1669 int i, ret, len;
1670 void *cmvs_ptr;
1671 const struct firmware *cmvs_fw;
1672 int ver = 1; // we can handle v1 cmv firmware version;
1673
1674 /* Enter in R-IDLE (cmv) until instructed otherwise */
1675 ret = uea_write_cmv_e1(sc, E1_SA_CNTL, 0, 1);
1676 if (ret < 0)
1677 return ret;
1678
1679 /* Dump firmware version */
1680 ret = uea_read_cmv_e1(sc, E1_SA_INFO, 10, &sc->stats.phy.firmid);
1681 if (ret < 0)
1682 return ret;
1683 uea_info(INS_TO_USBDEV(sc), "ATU-R firmware version : %x\n",
1684 sc->stats.phy.firmid);
1685
1686 /* get options */
1687 ret = len = request_cmvs(sc, &cmvs_ptr, &cmvs_fw, &ver);
1688 if (ret < 0)
1689 return ret;
1690
1691 /* send options */
1692 if (ver == 1) {
1693 struct uea_cmvs_v1 *cmvs_v1 = cmvs_ptr;
1694
1695 uea_warn(INS_TO_USBDEV(sc), "use deprecated cmvs version, "
1696 "please update your firmware\n");
1697
1698 for (i = 0; i < len; i++) {
Harvey Harrisona5abdea2008-04-29 01:03:40 -07001699 ret = uea_write_cmv_e1(sc, get_unaligned_le32(&cmvs_v1[i].address),
1700 get_unaligned_le16(&cmvs_v1[i].offset),
1701 get_unaligned_le32(&cmvs_v1[i].data));
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001702 if (ret < 0)
1703 goto out;
1704 }
1705 } else if (ver == 2) {
1706 struct uea_cmvs_v2 *cmvs_v2 = cmvs_ptr;
1707
1708 for (i = 0; i < len; i++) {
Harvey Harrisona5abdea2008-04-29 01:03:40 -07001709 ret = uea_write_cmv_e1(sc, get_unaligned_le32(&cmvs_v2[i].address),
1710 (u16) get_unaligned_le32(&cmvs_v2[i].offset),
1711 get_unaligned_le32(&cmvs_v2[i].data));
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001712 if (ret < 0)
1713 goto out;
1714 }
1715 } else {
1716 /* This realy should not happen */
1717 uea_err(INS_TO_USBDEV(sc), "bad cmvs version %d\n", ver);
1718 goto out;
1719 }
1720
1721 /* Enter in R-ACT-REQ */
1722 ret = uea_write_cmv_e1(sc, E1_SA_CNTL, 0, 2);
1723 uea_vdbg(INS_TO_USBDEV(sc), "Entering in R-ACT-REQ state\n");
1724 uea_info(INS_TO_USBDEV(sc), "modem started, waiting synchronization...\n");
1725out:
1726 release_firmware(cmvs_fw);
1727 return ret;
1728}
1729
1730static int uea_send_cmvs_e4(struct uea_softc *sc)
1731{
1732 int i, ret, len;
1733 void *cmvs_ptr;
1734 const struct firmware *cmvs_fw;
1735 int ver = 2; // we can only handle v2 cmv firmware version;
1736
1737 /* Enter in R-IDLE (cmv) until instructed otherwise */
1738 ret = uea_write_cmv_e4(sc, 1, E4_SA_CNTL, 0, 0, 1);
1739 if (ret < 0)
1740 return ret;
1741
1742 /* Dump firmware version */
1743 /* XXX don't read the 3th byte as it is always 6 */
1744 ret = uea_read_cmv_e4(sc, 2, E4_SA_INFO, 55, 0, &sc->stats.phy.firmid);
1745 if (ret < 0)
1746 return ret;
1747 uea_info(INS_TO_USBDEV(sc), "ATU-R firmware version : %x\n",
1748 sc->stats.phy.firmid);
1749
1750
1751 /* get options */
1752 ret = len = request_cmvs(sc, &cmvs_ptr, &cmvs_fw, &ver);
1753 if (ret < 0)
1754 return ret;
1755
1756 /* send options */
1757 if (ver == 2) {
1758 struct uea_cmvs_v2 *cmvs_v2 = cmvs_ptr;
1759
1760 for (i = 0; i < len; i++) {
1761 ret = uea_write_cmv_e4(sc, 1,
Harvey Harrisona5abdea2008-04-29 01:03:40 -07001762 get_unaligned_le32(&cmvs_v2[i].group),
1763 get_unaligned_le32(&cmvs_v2[i].address),
1764 get_unaligned_le32(&cmvs_v2[i].offset),
1765 get_unaligned_le32(&cmvs_v2[i].data));
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001766 if (ret < 0)
1767 goto out;
1768 }
1769 } else {
1770 /* This realy should not happen */
1771 uea_err(INS_TO_USBDEV(sc), "bad cmvs version %d\n", ver);
1772 goto out;
1773 }
1774
1775 /* Enter in R-ACT-REQ */
1776 ret = uea_write_cmv_e4(sc, 1, E4_SA_CNTL, 0, 0, 2);
1777 uea_vdbg(INS_TO_USBDEV(sc), "Entering in R-ACT-REQ state\n");
1778 uea_info(INS_TO_USBDEV(sc), "modem started, waiting synchronization...\n");
1779out:
1780 release_firmware(cmvs_fw);
1781 return ret;
matthieu castetb72458a2005-11-07 23:27:13 +01001782}
1783
1784/* Start boot post firmware modem:
1785 * - send reset commands through usb control pipe
1786 * - start workqueue for DSP loading
1787 * - send CMV options to modem
1788 */
1789
1790static int uea_start_reset(struct uea_softc *sc)
1791{
1792 u16 zero = 0; /* ;-) */
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001793 int ret;
matthieu castetb72458a2005-11-07 23:27:13 +01001794
1795 uea_enters(INS_TO_USBDEV(sc));
1796 uea_info(INS_TO_USBDEV(sc), "(re)booting started\n");
1797
matthieu castet22fcceb2006-04-02 18:44:20 +02001798 /* mask interrupt */
matthieu castetb72458a2005-11-07 23:27:13 +01001799 sc->booting = 1;
matthieu castet22fcceb2006-04-02 18:44:20 +02001800 /* We need to set this here because, a ack timeout could have occured,
1801 * but before we start the reboot, the ack occurs and set this to 1.
1802 * So we will failed to wait Ready CMV.
1803 */
1804 sc->cmv_ack = 0;
matthieu castetb72458a2005-11-07 23:27:13 +01001805 UPDATE_ATM_STAT(signal, ATM_PHY_SIG_LOST);
1806
1807 /* reset statistics */
1808 memset(&sc->stats, 0, sizeof(struct uea_stats));
1809
1810 /* tell the modem that we want to boot in IDMA mode */
1811 uea_request(sc, UEA_SET_MODE, UEA_LOOPBACK_ON, 0, NULL);
1812 uea_request(sc, UEA_SET_MODE, UEA_BOOT_IDMA, 0, NULL);
1813
1814 /* enter reset mode */
1815 uea_request(sc, UEA_SET_MODE, UEA_START_RESET, 0, NULL);
1816
1817 /* original driver use 200ms, but windows driver use 100ms */
Stanislaw Gruszka337427f2007-08-20 23:21:14 +02001818 ret = uea_wait(sc, 0, msecs_to_jiffies(100));
1819 if (ret < 0)
1820 return ret;
matthieu castetb72458a2005-11-07 23:27:13 +01001821
1822 /* leave reset mode */
1823 uea_request(sc, UEA_SET_MODE, UEA_END_RESET, 0, NULL);
1824
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001825 if (UEA_CHIP_VERSION(sc) != EAGLE_IV) {
1826 /* clear tx and rx mailboxes */
1827 uea_request(sc, UEA_SET_2183_DATA, UEA_MPTX_MAILBOX, 2, &zero);
1828 uea_request(sc, UEA_SET_2183_DATA, UEA_MPRX_MAILBOX, 2, &zero);
1829 uea_request(sc, UEA_SET_2183_DATA, UEA_SWAP_MAILBOX, 2, &zero);
1830 }
matthieu castetb72458a2005-11-07 23:27:13 +01001831
Stanislaw Gruszka337427f2007-08-20 23:21:14 +02001832 ret = uea_wait(sc, 0, msecs_to_jiffies(1000));
1833 if (ret < 0)
1834 return ret;
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001835
1836 if (UEA_CHIP_VERSION(sc) == EAGLE_IV)
1837 sc->cmv_dsc.e4.function = E4_MAKEFUNCTION(E4_ADSLDIRECTIVE, E4_MODEMREADY, 1);
1838 else
1839 sc->cmv_dsc.e1.function = E1_MAKEFUNCTION(E1_ADSLDIRECTIVE, E1_MODEMREADY);
1840
matthieu castet22fcceb2006-04-02 18:44:20 +02001841 /* demask interrupt */
matthieu castetb72458a2005-11-07 23:27:13 +01001842 sc->booting = 0;
1843
1844 /* start loading DSP */
1845 sc->pageno = 0;
1846 sc->ovl = 0;
Stanislaw Gruszka04ea02f2007-08-20 23:21:10 +02001847 queue_work(sc->work_q, &sc->task);
matthieu castetb72458a2005-11-07 23:27:13 +01001848
1849 /* wait for modem ready CMV */
1850 ret = wait_cmv_ack(sc);
1851 if (ret < 0)
1852 return ret;
1853
matthieu castet2a99b502006-04-02 18:43:53 +02001854 uea_vdbg(INS_TO_USBDEV(sc), "Ready CMV received\n");
1855
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001856 ret = sc->send_cmvs(sc);
matthieu castetb72458a2005-11-07 23:27:13 +01001857 if (ret < 0)
1858 return ret;
1859
matthieu castetb72458a2005-11-07 23:27:13 +01001860 sc->reset = 0;
1861 uea_leaves(INS_TO_USBDEV(sc));
1862 return ret;
1863}
1864
1865/*
1866 * In case of an error wait 1s before rebooting the modem
1867 * if the modem don't request reboot (-EAGAIN).
1868 * Monitor the modem every 1s.
1869 */
1870
1871static int uea_kthread(void *data)
1872{
1873 struct uea_softc *sc = data;
1874 int ret = -EAGAIN;
1875
Rafael J. Wysocki83144182007-07-17 04:03:35 -07001876 set_freezable();
matthieu castetb72458a2005-11-07 23:27:13 +01001877 uea_enters(INS_TO_USBDEV(sc));
1878 while (!kthread_should_stop()) {
1879 if (ret < 0 || sc->reset)
1880 ret = uea_start_reset(sc);
1881 if (!ret)
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001882 ret = sc->stat(sc);
matthieu castetb72458a2005-11-07 23:27:13 +01001883 if (ret != -EAGAIN)
Stanislaw Gruszka337427f2007-08-20 23:21:14 +02001884 uea_wait(sc, 0, msecs_to_jiffies(1000));
Stanislaw Gruszka0eb02262007-08-20 23:21:19 +02001885 try_to_freeze();
matthieu castetb72458a2005-11-07 23:27:13 +01001886 }
1887 uea_leaves(INS_TO_USBDEV(sc));
1888 return ret;
1889}
1890
1891/* Load second usb firmware for ADI930 chip */
1892static int load_XILINX_firmware(struct uea_softc *sc)
1893{
1894 const struct firmware *fw_entry;
1895 int ret, size, u, ln;
David Woodhouseaafcd2f2008-05-24 00:05:10 +01001896 const u8 *pfw;
1897 u8 value;
matthieu castetb72458a2005-11-07 23:27:13 +01001898 char *fw_name = FW_DIR "930-fpga.bin";
1899
1900 uea_enters(INS_TO_USBDEV(sc));
1901
1902 ret = request_firmware(&fw_entry, fw_name, &sc->usb_dev->dev);
1903 if (ret) {
1904 uea_err(INS_TO_USBDEV(sc), "firmware %s is not available\n",
1905 fw_name);
1906 goto err0;
1907 }
1908
1909 pfw = fw_entry->data;
1910 size = fw_entry->size;
1911 if (size != 0x577B) {
1912 uea_err(INS_TO_USBDEV(sc), "firmware %s is corrupted\n",
1913 fw_name);
1914 ret = -EILSEQ;
1915 goto err1;
1916 }
1917 for (u = 0; u < size; u += ln) {
1918 ln = min(size - u, 64);
1919 ret = uea_request(sc, 0xe, 0, ln, pfw + u);
1920 if (ret < 0) {
1921 uea_err(INS_TO_USBDEV(sc),
1922 "elsa download data failed (%d)\n", ret);
1923 goto err1;
1924 }
1925 }
1926
matthieu castete40abaf2006-01-18 07:38:37 +01001927 /* finish to send the fpga */
matthieu castetb72458a2005-11-07 23:27:13 +01001928 ret = uea_request(sc, 0xe, 1, 0, NULL);
1929 if (ret < 0) {
1930 uea_err(INS_TO_USBDEV(sc),
1931 "elsa download data failed (%d)\n", ret);
1932 goto err1;
1933 }
1934
matthieu castete40abaf2006-01-18 07:38:37 +01001935 /* Tell the modem we finish : de-assert reset */
matthieu castetb72458a2005-11-07 23:27:13 +01001936 value = 0;
1937 ret = uea_send_modem_cmd(sc->usb_dev, 0xe, 1, &value);
1938 if (ret < 0)
1939 uea_err(sc->usb_dev, "elsa de-assert failed with error %d\n", ret);
1940
matthieu castetb72458a2005-11-07 23:27:13 +01001941err1:
1942 release_firmware(fw_entry);
1943err0:
1944 uea_leaves(INS_TO_USBDEV(sc));
1945 return ret;
1946}
1947
matthieu castete40abaf2006-01-18 07:38:37 +01001948/* The modem send us an ack. First with check if it right */
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001949static void uea_dispatch_cmv_e1(struct uea_softc *sc, struct intr_pkt *intr)
matthieu castetb72458a2005-11-07 23:27:13 +01001950{
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001951 struct cmv_dsc_e1 *dsc = &sc->cmv_dsc.e1;
1952 struct cmv_e1 *cmv = &intr->u.e1.s2.cmv;
1953
matthieu castetb72458a2005-11-07 23:27:13 +01001954 uea_enters(INS_TO_USBDEV(sc));
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001955 if (le16_to_cpu(cmv->wPreamble) != E1_PREAMBLE)
matthieu castetb72458a2005-11-07 23:27:13 +01001956 goto bad1;
1957
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001958 if (cmv->bDirection != E1_MODEMTOHOST)
matthieu castetb72458a2005-11-07 23:27:13 +01001959 goto bad1;
1960
1961 /* FIXME : ADI930 reply wrong preambule (func = 2, sub = 2) to
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001962 * the first MEMACCESS cmv. Ignore it...
matthieu castetb72458a2005-11-07 23:27:13 +01001963 */
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001964 if (cmv->bFunction != dsc->function) {
matthieu castetb72458a2005-11-07 23:27:13 +01001965 if (UEA_CHIP_VERSION(sc) == ADI930
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001966 && cmv->bFunction == E1_MAKEFUNCTION(2, 2)) {
1967 cmv->wIndex = cpu_to_le16(dsc->idx);
Harvey Harrisona5abdea2008-04-29 01:03:40 -07001968 put_unaligned_le32(dsc->address, &cmv->dwSymbolicAddress);
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001969 cmv->wOffsetAddress = cpu_to_le16(dsc->offset);
1970 } else
matthieu castetb72458a2005-11-07 23:27:13 +01001971 goto bad2;
1972 }
1973
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001974 if (cmv->bFunction == E1_MAKEFUNCTION(E1_ADSLDIRECTIVE, E1_MODEMREADY)) {
matthieu castetb72458a2005-11-07 23:27:13 +01001975 wake_up_cmv_ack(sc);
matthieu castet2a99b502006-04-02 18:43:53 +02001976 uea_leaves(INS_TO_USBDEV(sc));
matthieu castetb72458a2005-11-07 23:27:13 +01001977 return;
1978 }
1979
1980 /* in case of MEMACCESS */
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001981 if (le16_to_cpu(cmv->wIndex) != dsc->idx ||
Harvey Harrisona5abdea2008-04-29 01:03:40 -07001982 get_unaligned_le32(&cmv->dwSymbolicAddress) != dsc->address ||
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001983 le16_to_cpu(cmv->wOffsetAddress) != dsc->offset)
matthieu castetb72458a2005-11-07 23:27:13 +01001984 goto bad2;
1985
Harvey Harrisona5abdea2008-04-29 01:03:40 -07001986 sc->data = get_unaligned_le32(&cmv->dwData);
matthieu castetb72458a2005-11-07 23:27:13 +01001987 sc->data = sc->data << 16 | sc->data >> 16;
1988
1989 wake_up_cmv_ack(sc);
matthieu castet2a99b502006-04-02 18:43:53 +02001990 uea_leaves(INS_TO_USBDEV(sc));
matthieu castetb72458a2005-11-07 23:27:13 +01001991 return;
1992
1993bad2:
Joe Perchesfec8de32007-11-19 17:53:33 -08001994 uea_err(INS_TO_USBDEV(sc), "unexpected cmv received, "
matthieu castetb72458a2005-11-07 23:27:13 +01001995 "Function : %d, Subfunction : %d\n",
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001996 E1_FUNCTION_TYPE(cmv->bFunction),
1997 E1_FUNCTION_SUBTYPE(cmv->bFunction));
matthieu castet2a99b502006-04-02 18:43:53 +02001998 uea_leaves(INS_TO_USBDEV(sc));
matthieu castetb72458a2005-11-07 23:27:13 +01001999 return;
2000
2001bad1:
2002 uea_err(INS_TO_USBDEV(sc), "invalid cmv received, "
2003 "wPreamble %d, bDirection %d\n",
2004 le16_to_cpu(cmv->wPreamble), cmv->bDirection);
matthieu castet2a99b502006-04-02 18:43:53 +02002005 uea_leaves(INS_TO_USBDEV(sc));
matthieu castetb72458a2005-11-07 23:27:13 +01002006}
2007
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02002008/* The modem send us an ack. First with check if it right */
2009static void uea_dispatch_cmv_e4(struct uea_softc *sc, struct intr_pkt *intr)
2010{
2011 struct cmv_dsc_e4 *dsc = &sc->cmv_dsc.e4;
2012 struct cmv_e4 *cmv = &intr->u.e4.s2.cmv;
2013
2014 uea_enters(INS_TO_USBDEV(sc));
2015 uea_dbg(INS_TO_USBDEV(sc), "cmv %x %x %x %x %x %x\n",
2016 be16_to_cpu(cmv->wGroup), be16_to_cpu(cmv->wFunction),
2017 be16_to_cpu(cmv->wOffset), be16_to_cpu(cmv->wAddress),
2018 be32_to_cpu(cmv->dwData[0]), be32_to_cpu(cmv->dwData[1]));
2019
2020 if (be16_to_cpu(cmv->wFunction) != dsc->function)
2021 goto bad2;
2022
2023 if (be16_to_cpu(cmv->wFunction) == E4_MAKEFUNCTION(E4_ADSLDIRECTIVE, E4_MODEMREADY, 1)) {
2024 wake_up_cmv_ack(sc);
2025 uea_leaves(INS_TO_USBDEV(sc));
2026 return;
2027 }
2028
2029 /* in case of MEMACCESS */
2030 if (be16_to_cpu(cmv->wOffset) != dsc->offset ||
2031 be16_to_cpu(cmv->wGroup) != dsc->group ||
2032 be16_to_cpu(cmv->wAddress) != dsc->address)
2033 goto bad2;
2034
2035 sc->data = be32_to_cpu(cmv->dwData[0]);
2036 sc->data1 = be32_to_cpu(cmv->dwData[1]);
2037 wake_up_cmv_ack(sc);
2038 uea_leaves(INS_TO_USBDEV(sc));
2039 return;
2040
2041bad2:
Joe Perchesfec8de32007-11-19 17:53:33 -08002042 uea_err(INS_TO_USBDEV(sc), "unexpected cmv received, "
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02002043 "Function : %d, Subfunction : %d\n",
2044 E4_FUNCTION_TYPE(cmv->wFunction),
2045 E4_FUNCTION_SUBTYPE(cmv->wFunction));
2046 uea_leaves(INS_TO_USBDEV(sc));
2047 return;
2048}
2049
2050static void uea_schedule_load_page_e1(struct uea_softc *sc, struct intr_pkt *intr)
2051{
2052 sc->pageno = intr->e1_bSwapPageNo;
2053 sc->ovl = intr->e1_bOvl >> 4 | intr->e1_bOvl << 4;
Stanislaw Gruszka04ea02f2007-08-20 23:21:10 +02002054 queue_work(sc->work_q, &sc->task);
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02002055}
2056
2057static void uea_schedule_load_page_e4(struct uea_softc *sc, struct intr_pkt *intr)
2058{
2059 sc->pageno = intr->e4_bSwapPageNo;
Stanislaw Gruszka04ea02f2007-08-20 23:21:10 +02002060 queue_work(sc->work_q, &sc->task);
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02002061}
2062
matthieu castetb72458a2005-11-07 23:27:13 +01002063/*
2064 * interrupt handler
2065 */
David Howells7d12e782006-10-05 14:55:46 +01002066static void uea_intr(struct urb *urb)
matthieu castetb72458a2005-11-07 23:27:13 +01002067{
matthieu castete40abaf2006-01-18 07:38:37 +01002068 struct uea_softc *sc = urb->context;
2069 struct intr_pkt *intr = urb->transfer_buffer;
Greg Kroah-Hartman508330e2007-07-18 10:58:02 -07002070 int status = urb->status;
2071
matthieu castetb72458a2005-11-07 23:27:13 +01002072 uea_enters(INS_TO_USBDEV(sc));
2073
Greg Kroah-Hartman508330e2007-07-18 10:58:02 -07002074 if (unlikely(status < 0)) {
matthieu castetb72458a2005-11-07 23:27:13 +01002075 uea_err(INS_TO_USBDEV(sc), "uea_intr() failed with %d\n",
Greg Kroah-Hartman508330e2007-07-18 10:58:02 -07002076 status);
matthieu castetb72458a2005-11-07 23:27:13 +01002077 return;
2078 }
2079
matthieu castetb72458a2005-11-07 23:27:13 +01002080 /* device-to-host interrupt */
2081 if (intr->bType != 0x08 || sc->booting) {
matthieu castete40abaf2006-01-18 07:38:37 +01002082 uea_err(INS_TO_USBDEV(sc), "wrong interrupt\n");
matthieu castetb72458a2005-11-07 23:27:13 +01002083 goto resubmit;
2084 }
2085
2086 switch (le16_to_cpu(intr->wInterrupt)) {
2087 case INT_LOADSWAPPAGE:
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02002088 sc->schedule_load_page(sc, intr);
matthieu castetb72458a2005-11-07 23:27:13 +01002089 break;
2090
2091 case INT_INCOMINGCMV:
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02002092 sc->dispatch_cmv(sc, intr);
matthieu castetb72458a2005-11-07 23:27:13 +01002093 break;
2094
2095 default:
matthieu castete40abaf2006-01-18 07:38:37 +01002096 uea_err(INS_TO_USBDEV(sc), "unknown interrupt %u\n",
matthieu castetb72458a2005-11-07 23:27:13 +01002097 le16_to_cpu(intr->wInterrupt));
2098 }
2099
2100resubmit:
2101 usb_submit_urb(sc->urb_int, GFP_ATOMIC);
2102}
2103
2104/*
2105 * Start the modem : init the data and start kernel thread
2106 */
2107static int uea_boot(struct uea_softc *sc)
2108{
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02002109 int ret, size;
matthieu castetb72458a2005-11-07 23:27:13 +01002110 struct intr_pkt *intr;
2111
2112 uea_enters(INS_TO_USBDEV(sc));
2113
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02002114 if (UEA_CHIP_VERSION(sc) == EAGLE_IV) {
2115 size = E4_INTR_PKT_SIZE;
2116 sc->dispatch_cmv = uea_dispatch_cmv_e4;
2117 sc->schedule_load_page = uea_schedule_load_page_e4;
2118 sc->stat = uea_stat_e4;
2119 sc->send_cmvs = uea_send_cmvs_e4;
2120 INIT_WORK(&sc->task, uea_load_page_e4);
2121 } else {
2122 size = E1_INTR_PKT_SIZE;
2123 sc->dispatch_cmv = uea_dispatch_cmv_e1;
2124 sc->schedule_load_page = uea_schedule_load_page_e1;
2125 sc->stat = uea_stat_e1;
2126 sc->send_cmvs = uea_send_cmvs_e1;
2127 INIT_WORK(&sc->task, uea_load_page_e1);
2128 }
2129
matthieu castetb72458a2005-11-07 23:27:13 +01002130 init_waitqueue_head(&sc->sync_q);
matthieu castetb72458a2005-11-07 23:27:13 +01002131
Stanislaw Gruszka04ea02f2007-08-20 23:21:10 +02002132 sc->work_q = create_workqueue("ueagle-dsp");
2133 if (!sc->work_q) {
2134 uea_err(INS_TO_USBDEV(sc), "cannot allocate workqueue\n");
2135 uea_leaves(INS_TO_USBDEV(sc));
2136 return -ENOMEM;
2137 }
2138
matthieu castetb72458a2005-11-07 23:27:13 +01002139 if (UEA_CHIP_VERSION(sc) == ADI930)
2140 load_XILINX_firmware(sc);
2141
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02002142 intr = kmalloc(size, GFP_KERNEL);
matthieu castetb72458a2005-11-07 23:27:13 +01002143 if (!intr) {
2144 uea_err(INS_TO_USBDEV(sc),
2145 "cannot allocate interrupt package\n");
Stanislaw Gruszka04ea02f2007-08-20 23:21:10 +02002146 goto err0;
matthieu castetb72458a2005-11-07 23:27:13 +01002147 }
2148
2149 sc->urb_int = usb_alloc_urb(0, GFP_KERNEL);
2150 if (!sc->urb_int) {
2151 uea_err(INS_TO_USBDEV(sc), "cannot allocate interrupt URB\n");
Stanislaw Gruszka04ea02f2007-08-20 23:21:10 +02002152 goto err1;
matthieu castetb72458a2005-11-07 23:27:13 +01002153 }
2154
2155 usb_fill_int_urb(sc->urb_int, sc->usb_dev,
2156 usb_rcvintpipe(sc->usb_dev, UEA_INTR_PIPE),
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02002157 intr, size, uea_intr, sc,
matthieu castetb72458a2005-11-07 23:27:13 +01002158 sc->usb_dev->actconfig->interface[0]->altsetting[0].
2159 endpoint[0].desc.bInterval);
2160
2161 ret = usb_submit_urb(sc->urb_int, GFP_KERNEL);
2162 if (ret < 0) {
2163 uea_err(INS_TO_USBDEV(sc),
2164 "urb submition failed with error %d\n", ret);
Stanislaw Gruszka04ea02f2007-08-20 23:21:10 +02002165 goto err1;
matthieu castetb72458a2005-11-07 23:27:13 +01002166 }
2167
2168 sc->kthread = kthread_run(uea_kthread, sc, "ueagle-atm");
2169 if (sc->kthread == ERR_PTR(-ENOMEM)) {
2170 uea_err(INS_TO_USBDEV(sc), "failed to create thread\n");
2171 goto err2;
2172 }
2173
2174 uea_leaves(INS_TO_USBDEV(sc));
2175 return 0;
2176
2177err2:
2178 usb_kill_urb(sc->urb_int);
Stanislaw Gruszka04ea02f2007-08-20 23:21:10 +02002179err1:
matthieu castetb72458a2005-11-07 23:27:13 +01002180 usb_free_urb(sc->urb_int);
matthieu castet4d45e212006-04-02 18:45:46 +02002181 sc->urb_int = NULL;
2182 kfree(intr);
Stanislaw Gruszka04ea02f2007-08-20 23:21:10 +02002183err0:
2184 destroy_workqueue(sc->work_q);
matthieu castetb72458a2005-11-07 23:27:13 +01002185 uea_leaves(INS_TO_USBDEV(sc));
2186 return -ENOMEM;
2187}
2188
2189/*
2190 * Stop the modem : kill kernel thread and free data
2191 */
2192static void uea_stop(struct uea_softc *sc)
2193{
2194 int ret;
2195 uea_enters(INS_TO_USBDEV(sc));
2196 ret = kthread_stop(sc->kthread);
matthieu castete40abaf2006-01-18 07:38:37 +01002197 uea_dbg(INS_TO_USBDEV(sc), "kthread finish with status %d\n", ret);
matthieu castetb72458a2005-11-07 23:27:13 +01002198
matthieu castetb72458a2005-11-07 23:27:13 +01002199 uea_request(sc, UEA_SET_MODE, UEA_LOOPBACK_ON, 0, NULL);
2200
2201 usb_kill_urb(sc->urb_int);
2202 kfree(sc->urb_int->transfer_buffer);
2203 usb_free_urb(sc->urb_int);
2204
Stanislaw Gruszka04ea02f2007-08-20 23:21:10 +02002205 /* stop any pending boot process, when no one can schedule work */
2206 destroy_workqueue(sc->work_q);
2207
matthieu castetb72458a2005-11-07 23:27:13 +01002208 if (sc->dsp_firm)
2209 release_firmware(sc->dsp_firm);
2210 uea_leaves(INS_TO_USBDEV(sc));
2211}
2212
2213/* syfs interface */
2214static struct uea_softc *dev_to_uea(struct device *dev)
2215{
2216 struct usb_interface *intf;
2217 struct usbatm_data *usbatm;
2218
2219 intf = to_usb_interface(dev);
2220 if (!intf)
2221 return NULL;
2222
2223 usbatm = usb_get_intfdata(intf);
2224 if (!usbatm)
2225 return NULL;
2226
2227 return usbatm->driver_data;
2228}
2229
2230static ssize_t read_status(struct device *dev, struct device_attribute *attr,
2231 char *buf)
2232{
2233 int ret = -ENODEV;
2234 struct uea_softc *sc;
2235
Arjan van de Venab3c81f2006-01-13 15:52:55 +01002236 mutex_lock(&uea_mutex);
matthieu castetb72458a2005-11-07 23:27:13 +01002237 sc = dev_to_uea(dev);
2238 if (!sc)
2239 goto out;
2240 ret = snprintf(buf, 10, "%08x\n", sc->stats.phy.state);
2241out:
Arjan van de Venab3c81f2006-01-13 15:52:55 +01002242 mutex_unlock(&uea_mutex);
matthieu castetb72458a2005-11-07 23:27:13 +01002243 return ret;
2244}
2245
2246static ssize_t reboot(struct device *dev, struct device_attribute *attr,
2247 const char *buf, size_t count)
2248{
2249 int ret = -ENODEV;
2250 struct uea_softc *sc;
2251
Arjan van de Venab3c81f2006-01-13 15:52:55 +01002252 mutex_lock(&uea_mutex);
matthieu castetb72458a2005-11-07 23:27:13 +01002253 sc = dev_to_uea(dev);
2254 if (!sc)
2255 goto out;
2256 sc->reset = 1;
2257 ret = count;
2258out:
Arjan van de Venab3c81f2006-01-13 15:52:55 +01002259 mutex_unlock(&uea_mutex);
matthieu castetb72458a2005-11-07 23:27:13 +01002260 return ret;
2261}
2262
2263static DEVICE_ATTR(stat_status, S_IWUGO | S_IRUGO, read_status, reboot);
2264
2265static ssize_t read_human_status(struct device *dev, struct device_attribute *attr,
2266 char *buf)
2267{
2268 int ret = -ENODEV;
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02002269 int modem_state;
matthieu castetb72458a2005-11-07 23:27:13 +01002270 struct uea_softc *sc;
2271
Arjan van de Venab3c81f2006-01-13 15:52:55 +01002272 mutex_lock(&uea_mutex);
matthieu castetb72458a2005-11-07 23:27:13 +01002273 sc = dev_to_uea(dev);
2274 if (!sc)
2275 goto out;
2276
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02002277 if (UEA_CHIP_VERSION(sc) == EAGLE_IV) {
2278 switch (sc->stats.phy.state) {
2279 case 0x0: /* not yet synchronized */
2280 case 0x1:
2281 case 0x3:
2282 case 0x4:
2283 modem_state = 0;
2284 break;
2285 case 0x5: /* initialization */
2286 case 0x6:
2287 case 0x9:
2288 case 0xa:
2289 modem_state = 1;
2290 break;
2291 case 0x7: /* operational */
2292 modem_state = 2;
2293 break;
2294 case 0x2: /* fail ... */
2295 modem_state = 3;
2296 break;
2297 default: /* unknown */
2298 modem_state = 4;
2299 break;
2300 }
2301 } else
2302 modem_state = GET_STATUS(sc->stats.phy.state);
2303
2304 switch (modem_state) {
matthieu castetb72458a2005-11-07 23:27:13 +01002305 case 0:
2306 ret = sprintf(buf, "Modem is booting\n");
2307 break;
2308 case 1:
2309 ret = sprintf(buf, "Modem is initializing\n");
2310 break;
2311 case 2:
2312 ret = sprintf(buf, "Modem is operational\n");
2313 break;
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02002314 case 3:
matthieu castetb72458a2005-11-07 23:27:13 +01002315 ret = sprintf(buf, "Modem synchronization failed\n");
2316 break;
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02002317 default:
2318 ret = sprintf(buf, "Modem state is unknown\n");
2319 break;
matthieu castetb72458a2005-11-07 23:27:13 +01002320 }
2321out:
Arjan van de Venab3c81f2006-01-13 15:52:55 +01002322 mutex_unlock(&uea_mutex);
matthieu castetb72458a2005-11-07 23:27:13 +01002323 return ret;
2324}
2325
2326static DEVICE_ATTR(stat_human_status, S_IWUGO | S_IRUGO, read_human_status, NULL);
2327
2328static ssize_t read_delin(struct device *dev, struct device_attribute *attr,
2329 char *buf)
2330{
2331 int ret = -ENODEV;
2332 struct uea_softc *sc;
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02002333 char *delin = "GOOD";
matthieu castetb72458a2005-11-07 23:27:13 +01002334
Arjan van de Venab3c81f2006-01-13 15:52:55 +01002335 mutex_lock(&uea_mutex);
matthieu castetb72458a2005-11-07 23:27:13 +01002336 sc = dev_to_uea(dev);
2337 if (!sc)
2338 goto out;
2339
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02002340 if (UEA_CHIP_VERSION(sc) == EAGLE_IV) {
2341 if (sc->stats.phy.flags & 0x4000)
2342 delin = "RESET";
2343 else if (sc->stats.phy.flags & 0x0001)
2344 delin = "LOSS";
2345 } else {
2346 if (sc->stats.phy.flags & 0x0C00)
2347 delin = "ERROR";
2348 else if (sc->stats.phy.flags & 0x0030)
2349 delin = "LOSS";
2350 }
2351
2352 ret = sprintf(buf, "%s\n", delin);
matthieu castetb72458a2005-11-07 23:27:13 +01002353out:
Arjan van de Venab3c81f2006-01-13 15:52:55 +01002354 mutex_unlock(&uea_mutex);
matthieu castetb72458a2005-11-07 23:27:13 +01002355 return ret;
2356}
2357
2358static DEVICE_ATTR(stat_delin, S_IWUGO | S_IRUGO, read_delin, NULL);
2359
2360#define UEA_ATTR(name, reset) \
2361 \
2362static ssize_t read_##name(struct device *dev, \
2363 struct device_attribute *attr, char *buf) \
2364{ \
2365 int ret = -ENODEV; \
2366 struct uea_softc *sc; \
2367 \
matthieu castet2a99b502006-04-02 18:43:53 +02002368 mutex_lock(&uea_mutex); \
matthieu castetb72458a2005-11-07 23:27:13 +01002369 sc = dev_to_uea(dev); \
2370 if (!sc) \
2371 goto out; \
2372 ret = snprintf(buf, 10, "%08x\n", sc->stats.phy.name); \
2373 if (reset) \
2374 sc->stats.phy.name = 0; \
2375out: \
matthieu castet2a99b502006-04-02 18:43:53 +02002376 mutex_unlock(&uea_mutex); \
matthieu castetb72458a2005-11-07 23:27:13 +01002377 return ret; \
2378} \
2379 \
2380static DEVICE_ATTR(stat_##name, S_IRUGO, read_##name, NULL)
2381
2382UEA_ATTR(mflags, 1);
2383UEA_ATTR(vidcpe, 0);
2384UEA_ATTR(usrate, 0);
2385UEA_ATTR(dsrate, 0);
2386UEA_ATTR(usattenuation, 0);
2387UEA_ATTR(dsattenuation, 0);
2388UEA_ATTR(usmargin, 0);
2389UEA_ATTR(dsmargin, 0);
2390UEA_ATTR(txflow, 0);
2391UEA_ATTR(rxflow, 0);
2392UEA_ATTR(uscorr, 0);
2393UEA_ATTR(dscorr, 0);
2394UEA_ATTR(usunc, 0);
2395UEA_ATTR(dsunc, 0);
matthieu casteta7a0c9c2006-10-03 21:44:11 +02002396UEA_ATTR(firmid, 0);
matthieu castetb72458a2005-11-07 23:27:13 +01002397
2398/* Retrieve the device End System Identifier (MAC) */
2399
2400#define htoi(x) (isdigit(x) ? x-'0' : toupper(x)-'A'+10)
2401static int uea_getesi(struct uea_softc *sc, u_char * esi)
2402{
2403 unsigned char mac_str[2 * ETH_ALEN + 1];
2404 int i;
2405 if (usb_string
2406 (sc->usb_dev, sc->usb_dev->descriptor.iSerialNumber, mac_str,
2407 sizeof(mac_str)) != 2 * ETH_ALEN)
2408 return 1;
2409
2410 for (i = 0; i < ETH_ALEN; i++)
2411 esi[i] = htoi(mac_str[2 * i]) * 16 + htoi(mac_str[2 * i + 1]);
2412
2413 return 0;
2414}
2415
2416/* ATM stuff */
2417static int uea_atm_open(struct usbatm_data *usbatm, struct atm_dev *atm_dev)
2418{
2419 struct uea_softc *sc = usbatm->driver_data;
2420
2421 return uea_getesi(sc, atm_dev->esi);
2422}
2423
2424static int uea_heavy(struct usbatm_data *usbatm, struct usb_interface *intf)
2425{
2426 struct uea_softc *sc = usbatm->driver_data;
2427
matthieu castet531a39b2006-10-03 21:49:29 +02002428 wait_event_interruptible(sc->sync_q, IS_OPERATIONAL(sc));
matthieu castetb72458a2005-11-07 23:27:13 +01002429
2430 return 0;
2431
2432}
2433
2434static int claim_interface(struct usb_device *usb_dev,
2435 struct usbatm_data *usbatm, int ifnum)
2436{
2437 int ret;
2438 struct usb_interface *intf = usb_ifnum_to_if(usb_dev, ifnum);
2439
2440 if (!intf) {
2441 uea_err(usb_dev, "interface %d not found\n", ifnum);
2442 return -ENODEV;
2443 }
2444
2445 ret = usb_driver_claim_interface(&uea_driver, intf, usbatm);
2446 if (ret != 0)
2447 uea_err(usb_dev, "can't claim interface %d, error %d\n", ifnum,
2448 ret);
2449 return ret;
2450}
2451
Greg Kroah-Hartmane7ccdfe2006-08-28 11:43:25 -07002452static struct attribute *attrs[] = {
2453 &dev_attr_stat_status.attr,
2454 &dev_attr_stat_mflags.attr,
2455 &dev_attr_stat_human_status.attr,
2456 &dev_attr_stat_delin.attr,
2457 &dev_attr_stat_vidcpe.attr,
2458 &dev_attr_stat_usrate.attr,
2459 &dev_attr_stat_dsrate.attr,
2460 &dev_attr_stat_usattenuation.attr,
2461 &dev_attr_stat_dsattenuation.attr,
2462 &dev_attr_stat_usmargin.attr,
2463 &dev_attr_stat_dsmargin.attr,
2464 &dev_attr_stat_txflow.attr,
2465 &dev_attr_stat_rxflow.attr,
2466 &dev_attr_stat_uscorr.attr,
2467 &dev_attr_stat_dscorr.attr,
2468 &dev_attr_stat_usunc.attr,
2469 &dev_attr_stat_dsunc.attr,
matthieu casteta7a0c9c2006-10-03 21:44:11 +02002470 &dev_attr_stat_firmid.attr,
matthieu castet9ab99c82006-10-11 14:20:56 -07002471 NULL,
Greg Kroah-Hartmane7ccdfe2006-08-28 11:43:25 -07002472};
2473static struct attribute_group attr_grp = {
2474 .attrs = attrs,
2475};
2476
matthieu castetb72458a2005-11-07 23:27:13 +01002477static int uea_bind(struct usbatm_data *usbatm, struct usb_interface *intf,
Duncan Sands35644b02006-01-17 11:16:13 +01002478 const struct usb_device_id *id)
matthieu castetb72458a2005-11-07 23:27:13 +01002479{
2480 struct usb_device *usb = interface_to_usbdev(intf);
2481 struct uea_softc *sc;
2482 int ret, ifnum = intf->altsetting->desc.bInterfaceNumber;
Stanislaw Gruszka503add42007-08-20 23:21:06 +02002483 unsigned int alt;
matthieu castetb72458a2005-11-07 23:27:13 +01002484
2485 uea_enters(usb);
2486
2487 /* interface 0 is for firmware/monitoring */
2488 if (ifnum != UEA_INTR_IFACE_NO)
2489 return -ENODEV;
2490
Duncan Sands0dfcd3e2006-01-13 09:36:20 +01002491 usbatm->flags = (sync_wait[modem_index] ? 0 : UDSL_SKIP_HEAVY_INIT);
matthieu castetb72458a2005-11-07 23:27:13 +01002492
2493 /* interface 1 is for outbound traffic */
2494 ret = claim_interface(usb, usbatm, UEA_US_IFACE_NO);
2495 if (ret < 0)
2496 return ret;
2497
matthieu castete40abaf2006-01-18 07:38:37 +01002498 /* ADI930 has only 2 interfaces and inbound traffic is on interface 1 */
matthieu castetb72458a2005-11-07 23:27:13 +01002499 if (UEA_CHIP_VERSION(id) != ADI930) {
2500 /* interface 2 is for inbound traffic */
2501 ret = claim_interface(usb, usbatm, UEA_DS_IFACE_NO);
2502 if (ret < 0)
2503 return ret;
2504 }
2505
2506 sc = kzalloc(sizeof(struct uea_softc), GFP_KERNEL);
2507 if (!sc) {
matthieu castet584958c2006-04-02 18:44:48 +02002508 uea_err(usb, "uea_init: not enough memory !\n");
matthieu castetb72458a2005-11-07 23:27:13 +01002509 return -ENOMEM;
2510 }
2511
2512 sc->usb_dev = usb;
2513 usbatm->driver_data = sc;
2514 sc->usbatm = usbatm;
2515 sc->modem_index = (modem_index < NB_MODEM) ? modem_index++ : 0;
2516 sc->driver_info = id->driver_info;
2517
Stanislaw Gruszka603cf602007-08-20 23:21:01 +02002518 /* first try to use module parameter */
2519 if (annex[sc->modem_index] == 1)
2520 sc->annex = ANNEXA;
2521 else if (annex[sc->modem_index] == 2)
2522 sc->annex = ANNEXB;
2523 /* try to autodetect annex */
2524 else if (sc->driver_info & AUTO_ANNEX_A)
2525 sc->annex = ANNEXA;
2526 else if (sc->driver_info & AUTO_ANNEX_B)
2527 sc->annex = ANNEXB;
2528 else
2529 sc->annex = (le16_to_cpu(sc->usb_dev->descriptor.bcdDevice) & 0x80)?ANNEXB:ANNEXA;
2530
Stanislaw Gruszka503add42007-08-20 23:21:06 +02002531 alt = altsetting[sc->modem_index];
matthieu castet3c9666c2006-01-18 07:38:19 +01002532 /* ADI930 don't support iso */
Stanislaw Gruszka503add42007-08-20 23:21:06 +02002533 if (UEA_CHIP_VERSION(id) != ADI930 && alt > 0) {
2534 if (alt <= 8 && usb_set_interface(usb, UEA_DS_IFACE_NO, alt) == 0) {
2535 uea_dbg(usb, "set alternate %u for 2 interface\n", alt);
matthieu castet3c9666c2006-01-18 07:38:19 +01002536 uea_info(usb, "using iso mode\n");
2537 usbatm->flags |= UDSL_USE_ISOC | UDSL_IGNORE_EILSEQ;
2538 } else {
Stanislaw Gruszka503add42007-08-20 23:21:06 +02002539 uea_err(usb, "setting alternate %u failed for "
2540 "2 interface, using bulk mode\n", alt);
matthieu castet3c9666c2006-01-18 07:38:19 +01002541 }
2542 }
2543
matthieu castet9ab99c82006-10-11 14:20:56 -07002544 ret = sysfs_create_group(&intf->dev.kobj, &attr_grp);
2545 if (ret < 0)
2546 goto error;
2547
matthieu castetb72458a2005-11-07 23:27:13 +01002548 ret = uea_boot(sc);
matthieu castet9ab99c82006-10-11 14:20:56 -07002549 if (ret < 0)
Stanislaw Gruszka4c132e72007-08-20 23:20:49 +02002550 goto error_rm_grp;
matthieu castetb72458a2005-11-07 23:27:13 +01002551
matthieu castetb72458a2005-11-07 23:27:13 +01002552 return 0;
Stanislaw Gruszka4c132e72007-08-20 23:20:49 +02002553
2554error_rm_grp:
2555 sysfs_remove_group(&intf->dev.kobj, &attr_grp);
matthieu castet9ab99c82006-10-11 14:20:56 -07002556error:
2557 kfree(sc);
2558 return ret;
matthieu castetb72458a2005-11-07 23:27:13 +01002559}
2560
2561static void uea_unbind(struct usbatm_data *usbatm, struct usb_interface *intf)
2562{
2563 struct uea_softc *sc = usbatm->driver_data;
2564
matthieu castet9ab99c82006-10-11 14:20:56 -07002565 sysfs_remove_group(&intf->dev.kobj, &attr_grp);
matthieu castetb72458a2005-11-07 23:27:13 +01002566 uea_stop(sc);
2567 kfree(sc);
2568}
2569
2570static struct usbatm_driver uea_usbatm_driver = {
2571 .driver_name = "ueagle-atm",
matthieu castetb72458a2005-11-07 23:27:13 +01002572 .bind = uea_bind,
2573 .atm_start = uea_atm_open,
2574 .unbind = uea_unbind,
2575 .heavy_init = uea_heavy,
Duncan Sands80aae7a2006-01-13 10:59:23 +01002576 .bulk_in = UEA_BULK_DATA_PIPE,
2577 .bulk_out = UEA_BULK_DATA_PIPE,
matthieu castet3c9666c2006-01-18 07:38:19 +01002578 .isoc_in = UEA_ISO_DATA_PIPE,
matthieu castetb72458a2005-11-07 23:27:13 +01002579};
2580
2581static int uea_probe(struct usb_interface *intf, const struct usb_device_id *id)
2582{
2583 struct usb_device *usb = interface_to_usbdev(intf);
2584
2585 uea_enters(usb);
Stanislaw Gruszka603cf602007-08-20 23:21:01 +02002586 uea_info(usb, "ADSL device founded vid (%#X) pid (%#X) Rev (%#X): %s\n",
2587 le16_to_cpu(usb->descriptor.idVendor),
2588 le16_to_cpu(usb->descriptor.idProduct),
2589 le16_to_cpu(usb->descriptor.bcdDevice),
2590 chip_name[UEA_CHIP_VERSION(id)]);
matthieu castetb72458a2005-11-07 23:27:13 +01002591
2592 usb_reset_device(usb);
2593
2594 if (UEA_IS_PREFIRM(id))
2595 return uea_load_firmware(usb, UEA_CHIP_VERSION(id));
2596
2597 return usbatm_usb_probe(intf, id, &uea_usbatm_driver);
2598}
2599
2600static void uea_disconnect(struct usb_interface *intf)
2601{
2602 struct usb_device *usb = interface_to_usbdev(intf);
2603 int ifnum = intf->altsetting->desc.bInterfaceNumber;
2604 uea_enters(usb);
2605
2606 /* ADI930 has 2 interfaces and eagle 3 interfaces.
2607 * Pre-firmware device has one interface
2608 */
2609 if (usb->config->desc.bNumInterfaces != 1 && ifnum == 0) {
Arjan van de Venab3c81f2006-01-13 15:52:55 +01002610 mutex_lock(&uea_mutex);
matthieu castetb72458a2005-11-07 23:27:13 +01002611 usbatm_usb_disconnect(intf);
Arjan van de Venab3c81f2006-01-13 15:52:55 +01002612 mutex_unlock(&uea_mutex);
matthieu castetb72458a2005-11-07 23:27:13 +01002613 uea_info(usb, "ADSL device removed\n");
2614 }
2615
2616 uea_leaves(usb);
2617}
2618
2619/*
2620 * List of supported VID/PID
2621 */
2622static const struct usb_device_id uea_ids[] = {
Stanislaw Gruszka603cf602007-08-20 23:21:01 +02002623 {USB_DEVICE(ANALOG_VID, ADI930_PID_PREFIRM), .driver_info = ADI930 | PREFIRM},
2624 {USB_DEVICE(ANALOG_VID, ADI930_PID_PSTFIRM), .driver_info = ADI930 | PSTFIRM},
2625 {USB_DEVICE(ANALOG_VID, EAGLE_I_PID_PREFIRM), .driver_info = EAGLE_I | PREFIRM},
2626 {USB_DEVICE(ANALOG_VID, EAGLE_I_PID_PSTFIRM), .driver_info = EAGLE_I | PSTFIRM},
2627 {USB_DEVICE(ANALOG_VID, EAGLE_II_PID_PREFIRM), .driver_info = EAGLE_II | PREFIRM},
2628 {USB_DEVICE(ANALOG_VID, EAGLE_II_PID_PSTFIRM), .driver_info = EAGLE_II | PSTFIRM},
2629 {USB_DEVICE(ANALOG_VID, EAGLE_IIC_PID_PREFIRM), .driver_info = EAGLE_II | PREFIRM},
2630 {USB_DEVICE(ANALOG_VID, EAGLE_IIC_PID_PSTFIRM), .driver_info = EAGLE_II | PSTFIRM},
2631 {USB_DEVICE(ANALOG_VID, EAGLE_III_PID_PREFIRM), .driver_info = EAGLE_III | PREFIRM},
2632 {USB_DEVICE(ANALOG_VID, EAGLE_III_PID_PSTFIRM), .driver_info = EAGLE_III | PSTFIRM},
2633 {USB_DEVICE(ANALOG_VID, EAGLE_IV_PID_PREFIRM), .driver_info = EAGLE_IV | PREFIRM},
2634 {USB_DEVICE(ANALOG_VID, EAGLE_IV_PID_PSTFIRM), .driver_info = EAGLE_IV | PSTFIRM},
2635 {USB_DEVICE(DEVOLO_VID, DEVOLO_EAGLE_I_A_PID_PREFIRM), .driver_info = EAGLE_I | PREFIRM},
2636 {USB_DEVICE(DEVOLO_VID, DEVOLO_EAGLE_I_A_PID_PSTFIRM), .driver_info = EAGLE_I | PSTFIRM | AUTO_ANNEX_A},
2637 {USB_DEVICE(DEVOLO_VID, DEVOLO_EAGLE_I_B_PID_PREFIRM), .driver_info = EAGLE_I | PREFIRM},
2638 {USB_DEVICE(DEVOLO_VID, DEVOLO_EAGLE_I_B_PID_PSTFIRM), .driver_info = EAGLE_I | PSTFIRM | AUTO_ANNEX_B},
2639 {USB_DEVICE(DEVOLO_VID, DEVOLO_EAGLE_II_A_PID_PREFIRM), .driver_info = EAGLE_II | PREFIRM},
2640 {USB_DEVICE(DEVOLO_VID, DEVOLO_EAGLE_II_A_PID_PSTFIRM), .driver_info = EAGLE_II | PSTFIRM | AUTO_ANNEX_A},
2641 {USB_DEVICE(DEVOLO_VID, DEVOLO_EAGLE_II_B_PID_PREFIRM), .driver_info = EAGLE_II | PREFIRM},
2642 {USB_DEVICE(DEVOLO_VID, DEVOLO_EAGLE_II_B_PID_PSTFIRM), .driver_info = EAGLE_II | PSTFIRM | AUTO_ANNEX_B},
matthieu castetb72458a2005-11-07 23:27:13 +01002643 {USB_DEVICE(ELSA_VID, ELSA_PID_PREFIRM), .driver_info = ADI930 | PREFIRM},
2644 {USB_DEVICE(ELSA_VID, ELSA_PID_PSTFIRM), .driver_info = ADI930 | PSTFIRM},
Stanislaw Gruszka603cf602007-08-20 23:21:01 +02002645 {USB_DEVICE(ELSA_VID, ELSA_PID_A_PREFIRM), .driver_info = ADI930 | PREFIRM},
2646 {USB_DEVICE(ELSA_VID, ELSA_PID_A_PSTFIRM), .driver_info = ADI930 | PSTFIRM | AUTO_ANNEX_A},
2647 {USB_DEVICE(ELSA_VID, ELSA_PID_B_PREFIRM), .driver_info = ADI930 | PREFIRM},
2648 {USB_DEVICE(ELSA_VID, ELSA_PID_B_PSTFIRM), .driver_info = ADI930 | PSTFIRM | AUTO_ANNEX_B},
matthieu castetb72458a2005-11-07 23:27:13 +01002649 {USB_DEVICE(USR_VID, MILLER_A_PID_PREFIRM), .driver_info = EAGLE_I | PREFIRM},
Stanislaw Gruszka603cf602007-08-20 23:21:01 +02002650 {USB_DEVICE(USR_VID, MILLER_A_PID_PSTFIRM), .driver_info = EAGLE_I | PSTFIRM | AUTO_ANNEX_A},
matthieu castetb72458a2005-11-07 23:27:13 +01002651 {USB_DEVICE(USR_VID, MILLER_B_PID_PREFIRM), .driver_info = EAGLE_I | PREFIRM},
Stanislaw Gruszka603cf602007-08-20 23:21:01 +02002652 {USB_DEVICE(USR_VID, MILLER_B_PID_PSTFIRM), .driver_info = EAGLE_I | PSTFIRM | AUTO_ANNEX_B},
matthieu castetb72458a2005-11-07 23:27:13 +01002653 {USB_DEVICE(USR_VID, HEINEKEN_A_PID_PREFIRM),.driver_info = EAGLE_I | PREFIRM},
Stanislaw Gruszka603cf602007-08-20 23:21:01 +02002654 {USB_DEVICE(USR_VID, HEINEKEN_A_PID_PSTFIRM),.driver_info = EAGLE_I | PSTFIRM | AUTO_ANNEX_A},
matthieu castetb72458a2005-11-07 23:27:13 +01002655 {USB_DEVICE(USR_VID, HEINEKEN_B_PID_PREFIRM),.driver_info = EAGLE_I | PREFIRM},
Stanislaw Gruszka603cf602007-08-20 23:21:01 +02002656 {USB_DEVICE(USR_VID, HEINEKEN_B_PID_PSTFIRM),.driver_info = EAGLE_I | PSTFIRM | AUTO_ANNEX_B},
matthieu castetb72458a2005-11-07 23:27:13 +01002657 {}
2658};
2659
2660/*
2661 * USB driver descriptor
2662 */
2663static struct usb_driver uea_driver = {
matthieu castetb72458a2005-11-07 23:27:13 +01002664 .name = "ueagle-atm",
2665 .id_table = uea_ids,
2666 .probe = uea_probe,
2667 .disconnect = uea_disconnect,
2668};
2669
2670MODULE_DEVICE_TABLE(usb, uea_ids);
2671
2672/**
2673 * uea_init - Initialize the module.
2674 * Register to USB subsystem
2675 */
2676static int __init uea_init(void)
2677{
2678 printk(KERN_INFO "[ueagle-atm] driver " EAGLEUSBVERSION " loaded\n");
2679
2680 usb_register(&uea_driver);
2681
2682 return 0;
2683}
2684
2685module_init(uea_init);
2686
2687/**
2688 * uea_exit - Destroy module
2689 * Deregister with USB subsystem
2690 */
2691static void __exit uea_exit(void)
2692{
2693 /*
2694 * This calls automatically the uea_disconnect method if necessary:
2695 */
2696 usb_deregister(&uea_driver);
2697
2698 printk(KERN_INFO "[ueagle-atm] driver unloaded\n");
2699}
2700
2701module_exit(uea_exit);
2702
2703MODULE_AUTHOR("Damien Bergamini/Matthieu Castet/Stanislaw W. Gruszka");
2704MODULE_DESCRIPTION("ADI 930/Eagle USB ADSL Modem driver");
2705MODULE_LICENSE("Dual BSD/GPL");