blob: 5f71ff3aee35f01203e36b3344f2d7dec3d1f40a [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>
67#include <linux/version.h>
Arjan van de Venab3c81f2006-01-13 15:52:55 +010068#include <linux/mutex.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080069#include <linux/freezer.h>
70
matthieu castetb72458a2005-11-07 23:27:13 +010071#include <asm/unaligned.h>
72
73#include "usbatm.h"
74
matthieu casteta7a0c9c2006-10-03 21:44:11 +020075#define EAGLEUSBVERSION "ueagle 1.4"
matthieu castetb72458a2005-11-07 23:27:13 +010076
77
78/*
79 * Debug macros
80 */
81#define uea_dbg(usb_dev, format, args...) \
82 do { \
83 if (debug >= 1) \
84 dev_dbg(&(usb_dev)->dev, \
85 "[ueagle-atm dbg] %s: " format, \
Harvey Harrison441b62c2008-03-03 16:08:34 -080086 __func__, ##args); \
matthieu casteta7a0c9c2006-10-03 21:44:11 +020087 } while (0)
matthieu castetb72458a2005-11-07 23:27:13 +010088
89#define uea_vdbg(usb_dev, format, args...) \
90 do { \
91 if (debug >= 2) \
92 dev_dbg(&(usb_dev)->dev, \
93 "[ueagle-atm vdbg] " format, ##args); \
matthieu casteta7a0c9c2006-10-03 21:44:11 +020094 } while (0)
matthieu castetb72458a2005-11-07 23:27:13 +010095
96#define uea_enters(usb_dev) \
Harvey Harrison441b62c2008-03-03 16:08:34 -080097 uea_vdbg(usb_dev, "entering %s\n", __func__)
matthieu castetb72458a2005-11-07 23:27:13 +010098
99#define uea_leaves(usb_dev) \
Harvey Harrison441b62c2008-03-03 16:08:34 -0800100 uea_vdbg(usb_dev, "leaving %s\n", __func__)
matthieu castetb72458a2005-11-07 23:27:13 +0100101
102#define uea_err(usb_dev, format,args...) \
103 dev_err(&(usb_dev)->dev ,"[UEAGLE-ATM] " format , ##args)
104
105#define uea_warn(usb_dev, format,args...) \
106 dev_warn(&(usb_dev)->dev ,"[Ueagle-atm] " format, ##args)
107
108#define uea_info(usb_dev, format,args...) \
109 dev_info(&(usb_dev)->dev ,"[ueagle-atm] " format, ##args)
110
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +0200111struct intr_pkt;
112
113/* cmv's from firmware */
114struct uea_cmvs_v1 {
matthieu castetb72458a2005-11-07 23:27:13 +0100115 u32 address;
116 u16 offset;
117 u32 data;
118} __attribute__ ((packed));
119
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +0200120struct uea_cmvs_v2 {
121 u32 group;
122 u32 address;
123 u32 offset;
124 u32 data;
125} __attribute__ ((packed));
126
127/* information about currently processed cmv */
128struct cmv_dsc_e1 {
129 u8 function;
130 u16 idx;
131 u32 address;
132 u16 offset;
133};
134
135struct cmv_dsc_e4 {
136 u16 function;
137 u16 offset;
138 u16 address;
139 u16 group;
140};
141
142union cmv_dsc {
143 struct cmv_dsc_e1 e1;
144 struct cmv_dsc_e4 e4;
145};
146
matthieu castetb72458a2005-11-07 23:27:13 +0100147struct uea_softc {
148 struct usb_device *usb_dev;
149 struct usbatm_data *usbatm;
150
151 int modem_index;
152 unsigned int driver_info;
Stanislaw Gruszka603cf602007-08-20 23:21:01 +0200153 int annex;
154#define ANNEXA 0
155#define ANNEXB 1
matthieu castetb72458a2005-11-07 23:27:13 +0100156
157 int booting;
158 int reset;
159
160 wait_queue_head_t sync_q;
161
162 struct task_struct *kthread;
163 u32 data;
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +0200164 u32 data1;
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +0200165
matthieu castetb72458a2005-11-07 23:27:13 +0100166 int cmv_ack;
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +0200167 union cmv_dsc cmv_dsc;
matthieu castetb72458a2005-11-07 23:27:13 +0100168
169 struct work_struct task;
Stanislaw Gruszka04ea02f2007-08-20 23:21:10 +0200170 struct workqueue_struct *work_q;
matthieu castetb72458a2005-11-07 23:27:13 +0100171 u16 pageno;
172 u16 ovl;
173
174 const struct firmware *dsp_firm;
175 struct urb *urb_int;
176
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +0200177 void (*dispatch_cmv) (struct uea_softc *, struct intr_pkt *);
178 void (*schedule_load_page) (struct uea_softc *, struct intr_pkt *);
179 int (*stat) (struct uea_softc *);
180 int (*send_cmvs) (struct uea_softc *);
matthieu castetb72458a2005-11-07 23:27:13 +0100181
182 /* keep in sync with eaglectl */
183 struct uea_stats {
184 struct {
185 u32 state;
186 u32 flags;
187 u32 mflags;
188 u32 vidcpe;
189 u32 vidco;
190 u32 dsrate;
191 u32 usrate;
192 u32 dsunc;
193 u32 usunc;
194 u32 dscorr;
195 u32 uscorr;
196 u32 txflow;
197 u32 rxflow;
198 u32 usattenuation;
199 u32 dsattenuation;
200 u32 dsmargin;
201 u32 usmargin;
202 u32 firmid;
203 } phy;
204 } stats;
205};
206
207/*
208 * Elsa IDs
209 */
210#define ELSA_VID 0x05CC
211#define ELSA_PID_PSTFIRM 0x3350
212#define ELSA_PID_PREFIRM 0x3351
213
Stanislaw Gruszka603cf602007-08-20 23:21:01 +0200214#define ELSA_PID_A_PREFIRM 0x3352
215#define ELSA_PID_A_PSTFIRM 0x3353
216#define ELSA_PID_B_PREFIRM 0x3362
217#define ELSA_PID_B_PSTFIRM 0x3363
218
matthieu castetb72458a2005-11-07 23:27:13 +0100219/*
Stanislaw Gruszka603cf602007-08-20 23:21:01 +0200220 * Devolo IDs : pots if (pid & 0x10)
matthieu castetb72458a2005-11-07 23:27:13 +0100221 */
Stanislaw Gruszka603cf602007-08-20 23:21:01 +0200222#define DEVOLO_VID 0x1039
223#define DEVOLO_EAGLE_I_A_PID_PSTFIRM 0x2110
224#define DEVOLO_EAGLE_I_A_PID_PREFIRM 0x2111
225
226#define DEVOLO_EAGLE_I_B_PID_PSTFIRM 0x2100
227#define DEVOLO_EAGLE_I_B_PID_PREFIRM 0x2101
228
229#define DEVOLO_EAGLE_II_A_PID_PSTFIRM 0x2130
230#define DEVOLO_EAGLE_II_A_PID_PREFIRM 0x2131
231
232#define DEVOLO_EAGLE_II_B_PID_PSTFIRM 0x2120
233#define DEVOLO_EAGLE_II_B_PID_PREFIRM 0x2121
234
235/*
236 * Reference design USB IDs
237 */
238#define ANALOG_VID 0x1110
239#define ADI930_PID_PREFIRM 0x9001
240#define ADI930_PID_PSTFIRM 0x9000
241
matthieu castetb72458a2005-11-07 23:27:13 +0100242#define EAGLE_I_PID_PREFIRM 0x9010 /* Eagle I */
243#define EAGLE_I_PID_PSTFIRM 0x900F /* Eagle I */
244
245#define EAGLE_IIC_PID_PREFIRM 0x9024 /* Eagle IIC */
246#define EAGLE_IIC_PID_PSTFIRM 0x9023 /* Eagle IIC */
247
248#define EAGLE_II_PID_PREFIRM 0x9022 /* Eagle II */
249#define EAGLE_II_PID_PSTFIRM 0x9021 /* Eagle II */
250
matthieu castetb72458a2005-11-07 23:27:13 +0100251#define EAGLE_III_PID_PREFIRM 0x9032 /* Eagle III */
252#define EAGLE_III_PID_PSTFIRM 0x9031 /* Eagle III */
253
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +0200254#define EAGLE_IV_PID_PREFIRM 0x9042 /* Eagle IV */
255#define EAGLE_IV_PID_PSTFIRM 0x9041 /* Eagle IV */
256
matthieu castetb72458a2005-11-07 23:27:13 +0100257/*
258 * USR USB IDs
259 */
260#define USR_VID 0x0BAF
261#define MILLER_A_PID_PREFIRM 0x00F2
262#define MILLER_A_PID_PSTFIRM 0x00F1
263#define MILLER_B_PID_PREFIRM 0x00FA
264#define MILLER_B_PID_PSTFIRM 0x00F9
265#define HEINEKEN_A_PID_PREFIRM 0x00F6
266#define HEINEKEN_A_PID_PSTFIRM 0x00F5
267#define HEINEKEN_B_PID_PREFIRM 0x00F8
268#define HEINEKEN_B_PID_PSTFIRM 0x00F7
269
270#define PREFIRM 0
271#define PSTFIRM (1<<7)
Stanislaw Gruszka603cf602007-08-20 23:21:01 +0200272#define AUTO_ANNEX_A (1<<8)
273#define AUTO_ANNEX_B (1<<9)
274
matthieu castetb72458a2005-11-07 23:27:13 +0100275enum {
276 ADI930 = 0,
277 EAGLE_I,
278 EAGLE_II,
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +0200279 EAGLE_III,
280 EAGLE_IV
matthieu castetb72458a2005-11-07 23:27:13 +0100281};
282
283/* macros for both struct usb_device_id and struct uea_softc */
284#define UEA_IS_PREFIRM(x) \
285 (!((x)->driver_info & PSTFIRM))
286#define UEA_CHIP_VERSION(x) \
287 ((x)->driver_info & 0xf)
288
Stanislaw Gruszka603cf602007-08-20 23:21:01 +0200289#define IS_ISDN(x) \
290 ((x)->annex & ANNEXB)
matthieu castetb72458a2005-11-07 23:27:13 +0100291
292#define INS_TO_USBDEV(ins) ins->usb_dev
293
294#define GET_STATUS(data) \
295 ((data >> 8) & 0xf)
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +0200296
matthieu castetb72458a2005-11-07 23:27:13 +0100297#define IS_OPERATIONAL(sc) \
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +0200298 ((UEA_CHIP_VERSION(sc) != EAGLE_IV) ? \
299 (GET_STATUS(sc->stats.phy.state) == 2) : \
300 (sc->stats.phy.state == 7))
matthieu castetb72458a2005-11-07 23:27:13 +0100301
302/*
303 * Set of macros to handle unaligned data in the firmware blob.
304 * The FW_GET_BYTE() macro is provided only for consistency.
305 */
306
307#define FW_GET_BYTE(p) *((__u8 *) (p))
matthieu castetb72458a2005-11-07 23:27:13 +0100308
309#define FW_DIR "ueagle-atm/"
310#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,
582 u16 addr, u16 size, u8 * buff)
583{
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;
607 u8 *pfw, value;
608 u32 crc = 0;
609 int ret, size;
610
611 uea_enters(usb);
612 if (!fw_entry) {
613 uea_err(usb, "firmware is not available\n");
614 goto err;
615 }
616
617 pfw = fw_entry->data;
618 size = fw_entry->size;
matthieu castet8d7802e2005-11-08 00:02:30 +0100619 if (size < 4)
620 goto err_fw_corrupted;
matthieu castetb72458a2005-11-07 23:27:13 +0100621
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700622 crc = get_unaligned_le32(pfw);
matthieu castetb72458a2005-11-07 23:27:13 +0100623 pfw += 4;
624 size -= 4;
matthieu castet8d7802e2005-11-08 00:02:30 +0100625 if (crc32_be(0, pfw, size) != crc)
626 goto err_fw_corrupted;
matthieu castetb72458a2005-11-07 23:27:13 +0100627
628 /*
629 * Start to upload formware : send reset
630 */
631 value = 1;
632 ret = uea_send_modem_cmd(usb, F8051_USBCS, sizeof(value), &value);
633
634 if (ret < 0) {
635 uea_err(usb, "modem reset failed with error %d\n", ret);
636 goto err;
637 }
638
matthieu castet8d7802e2005-11-08 00:02:30 +0100639 while (size > 3) {
matthieu castetb72458a2005-11-07 23:27:13 +0100640 u8 len = FW_GET_BYTE(pfw);
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700641 u16 add = get_unaligned_le16(pfw + 1);
matthieu castet8d7802e2005-11-08 00:02:30 +0100642
643 size -= len + 3;
644 if (size < 0)
645 goto err_fw_corrupted;
646
matthieu castetb72458a2005-11-07 23:27:13 +0100647 ret = uea_send_modem_cmd(usb, add, len, pfw + 3);
648 if (ret < 0) {
649 uea_err(usb, "uploading firmware data failed "
650 "with error %d\n", ret);
651 goto err;
652 }
653 pfw += len + 3;
matthieu castetb72458a2005-11-07 23:27:13 +0100654 }
655
matthieu castet8d7802e2005-11-08 00:02:30 +0100656 if (size != 0)
657 goto err_fw_corrupted;
658
matthieu castetb72458a2005-11-07 23:27:13 +0100659 /*
660 * Tell the modem we finish : de-assert reset
661 */
662 value = 0;
663 ret = uea_send_modem_cmd(usb, F8051_USBCS, 1, &value);
664 if (ret < 0)
665 uea_err(usb, "modem de-assert failed with error %d\n", ret);
666 else
667 uea_info(usb, "firmware uploaded\n");
668
matthieu castet8d7802e2005-11-08 00:02:30 +0100669 uea_leaves(usb);
670 return;
671
672err_fw_corrupted:
673 uea_err(usb, "firmware is corrupted\n");
matthieu castetb72458a2005-11-07 23:27:13 +0100674err:
675 uea_leaves(usb);
676}
677
678/**
679 * uea_load_firmware - Load usb firmware for pre-firmware devices.
680 */
681static int uea_load_firmware(struct usb_device *usb, unsigned int ver)
682{
683 int ret;
684 char *fw_name = FW_DIR "eagle.fw";
685
686 uea_enters(usb);
687 uea_info(usb, "pre-firmware device, uploading firmware\n");
688
689 switch (ver) {
690 case ADI930:
691 fw_name = FW_DIR "adi930.fw";
692 break;
693 case EAGLE_I:
694 fw_name = FW_DIR "eagleI.fw";
695 break;
696 case EAGLE_II:
697 fw_name = FW_DIR "eagleII.fw";
698 break;
699 case EAGLE_III:
700 fw_name = FW_DIR "eagleIII.fw";
701 break;
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +0200702 case EAGLE_IV:
703 fw_name = FW_DIR "eagleIV.fw";
704 break;
matthieu castetb72458a2005-11-07 23:27:13 +0100705 }
706
707 ret = request_firmware_nowait(THIS_MODULE, 1, fw_name, &usb->dev, usb, uea_upload_pre_firmware);
708 if (ret)
709 uea_err(usb, "firmware %s is not available\n", fw_name);
710 else
711 uea_info(usb, "loading firmware %s\n", fw_name);
712
713 uea_leaves(usb);
714 return ret;
715}
716
717/* modem management : dsp firmware, send/read CMV, monitoring statistic
718 */
719
720/*
721 * Make sure that the DSP code provided is safe to use.
722 */
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +0200723static int check_dsp_e1(u8 *dsp, unsigned int len)
matthieu castetb72458a2005-11-07 23:27:13 +0100724{
725 u8 pagecount, blockcount;
726 u16 blocksize;
727 u32 pageoffset;
728 unsigned int i, j, p, pp;
729
matthieu castetb72458a2005-11-07 23:27:13 +0100730 pagecount = FW_GET_BYTE(dsp);
731 p = 1;
732
733 /* enough space for page offsets? */
734 if (p + 4 * pagecount > len)
735 return 1;
736
737 for (i = 0; i < pagecount; i++) {
738
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700739 pageoffset = get_unaligned_le32(dsp + p);
matthieu castetb72458a2005-11-07 23:27:13 +0100740 p += 4;
741
742 if (pageoffset == 0)
743 continue;
744
745 /* enough space for blockcount? */
746 if (pageoffset >= len)
747 return 1;
748
749 pp = pageoffset;
750 blockcount = FW_GET_BYTE(dsp + pp);
751 pp += 1;
752
753 for (j = 0; j < blockcount; j++) {
754
755 /* enough space for block header? */
756 if (pp + 4 > len)
757 return 1;
758
759 pp += 2; /* skip blockaddr */
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700760 blocksize = get_unaligned_le16(dsp + pp);
matthieu castetb72458a2005-11-07 23:27:13 +0100761 pp += 2;
762
763 /* enough space for block data? */
764 if (pp + blocksize > len)
765 return 1;
766
767 pp += blocksize;
768 }
769 }
770
771 return 0;
772}
773
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +0200774static int check_dsp_e4(u8 *dsp, int len)
775{
776 int i;
777 struct l1_code *p = (struct l1_code *) dsp;
778 unsigned int sum = p->code - dsp;
779
780 if (len < sum)
781 return 1;
782
783 if (strcmp("STRATIPHY ANEXA", p->string_header) != 0 &&
784 strcmp("STRATIPHY ANEXB", p->string_header) != 0)
785 return 1;
786
787 for (i = 0; i < E4_MAX_PAGE_NUMBER; i++) {
788 struct block_index *blockidx;
789 u8 blockno = p->page_number_to_block_index[i];
790 if (blockno >= E4_NO_SWAPPAGE_HEADERS)
791 continue;
792
793 do {
794 u64 l;
795
796 if (blockno >= E4_NO_SWAPPAGE_HEADERS)
797 return 1;
798
799 blockidx = &p->page_header[blockno++];
800 if ((u8 *)(blockidx + 1) - dsp >= len)
801 return 1;
802
803 if (le16_to_cpu(blockidx->PageNumber) != i)
804 return 1;
805
806 l = E4_PAGE_BYTES(blockidx->PageSize);
807 sum += l;
808 l += le32_to_cpu(blockidx->PageOffset);
809 if (l > len)
810 return 1;
811
812 /* zero is zero regardless endianes */
813 } while (blockidx->NotLastBlock);
814 }
815
816 return (sum == len) ? 0 : 1;
817}
818
matthieu castetb72458a2005-11-07 23:27:13 +0100819/*
820 * send data to the idma pipe
821 * */
822static int uea_idma_write(struct uea_softc *sc, void *data, u32 size)
823{
824 int ret = -ENOMEM;
825 u8 *xfer_buff;
826 int bytes_read;
827
Eric Sesterhenn5d7efe52006-10-26 21:06:24 +0200828 xfer_buff = kmemdup(data, size, GFP_KERNEL);
matthieu castetb72458a2005-11-07 23:27:13 +0100829 if (!xfer_buff) {
830 uea_err(INS_TO_USBDEV(sc), "can't allocate xfer_buff\n");
831 return ret;
832 }
833
matthieu castetb72458a2005-11-07 23:27:13 +0100834 ret = usb_bulk_msg(sc->usb_dev,
835 usb_sndbulkpipe(sc->usb_dev, UEA_IDMA_PIPE),
836 xfer_buff, size, &bytes_read, BULK_TIMEOUT);
837
838 kfree(xfer_buff);
839 if (ret < 0)
840 return ret;
841 if (size != bytes_read) {
842 uea_err(INS_TO_USBDEV(sc), "size != bytes_read %d %d\n", size,
843 bytes_read);
844 return -EIO;
845 }
846
847 return 0;
848}
849
850static int request_dsp(struct uea_softc *sc)
851{
852 int ret;
853 char *dsp_name;
854
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +0200855 if (UEA_CHIP_VERSION(sc) == EAGLE_IV) {
Stanislaw Gruszka603cf602007-08-20 23:21:01 +0200856 if (IS_ISDN(sc))
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +0200857 dsp_name = FW_DIR "DSP4i.bin";
858 else
859 dsp_name = FW_DIR "DSP4p.bin";
860 } else if (UEA_CHIP_VERSION(sc) == ADI930) {
Stanislaw Gruszka603cf602007-08-20 23:21:01 +0200861 if (IS_ISDN(sc))
matthieu castetb72458a2005-11-07 23:27:13 +0100862 dsp_name = FW_DIR "DSP9i.bin";
863 else
864 dsp_name = FW_DIR "DSP9p.bin";
865 } else {
Stanislaw Gruszka603cf602007-08-20 23:21:01 +0200866 if (IS_ISDN(sc))
matthieu castetb72458a2005-11-07 23:27:13 +0100867 dsp_name = FW_DIR "DSPei.bin";
868 else
869 dsp_name = FW_DIR "DSPep.bin";
870 }
871
matthieu castete40abaf2006-01-18 07:38:37 +0100872 ret = request_firmware(&sc->dsp_firm, dsp_name, &sc->usb_dev->dev);
matthieu castetb72458a2005-11-07 23:27:13 +0100873 if (ret < 0) {
874 uea_err(INS_TO_USBDEV(sc),
875 "requesting firmware %s failed with error %d\n",
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +0200876 dsp_name, ret);
matthieu castetb72458a2005-11-07 23:27:13 +0100877 return ret;
878 }
879
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +0200880 if (UEA_CHIP_VERSION(sc) == EAGLE_IV)
881 ret = check_dsp_e4(sc->dsp_firm->data, sc->dsp_firm->size);
882 else
883 ret = check_dsp_e1(sc->dsp_firm->data, sc->dsp_firm->size);
884
885 if (ret) {
matthieu castetb72458a2005-11-07 23:27:13 +0100886 uea_err(INS_TO_USBDEV(sc), "firmware %s is corrupted\n",
887 dsp_name);
888 release_firmware(sc->dsp_firm);
889 sc->dsp_firm = NULL;
890 return -EILSEQ;
891 }
892
893 return 0;
894}
895
896/*
897 * The uea_load_page() function must be called within a process context
898 */
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +0200899static void uea_load_page_e1(struct work_struct *work)
matthieu castetb72458a2005-11-07 23:27:13 +0100900{
David Howellsc4028952006-11-22 14:57:56 +0000901 struct uea_softc *sc = container_of(work, struct uea_softc, task);
matthieu castetb72458a2005-11-07 23:27:13 +0100902 u16 pageno = sc->pageno;
903 u16 ovl = sc->ovl;
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +0200904 struct block_info_e1 bi;
matthieu castetb72458a2005-11-07 23:27:13 +0100905
906 u8 *p;
907 u8 pagecount, blockcount;
908 u16 blockaddr, blocksize;
909 u32 pageoffset;
910 int i;
911
912 /* reload firmware when reboot start and it's loaded already */
913 if (ovl == 0 && pageno == 0 && sc->dsp_firm) {
914 release_firmware(sc->dsp_firm);
915 sc->dsp_firm = NULL;
916 }
917
918 if (sc->dsp_firm == NULL && request_dsp(sc) < 0)
919 return;
920
921 p = sc->dsp_firm->data;
922 pagecount = FW_GET_BYTE(p);
923 p += 1;
924
925 if (pageno >= pagecount)
926 goto bad1;
927
928 p += 4 * pageno;
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700929 pageoffset = get_unaligned_le32(p);
matthieu castetb72458a2005-11-07 23:27:13 +0100930
931 if (pageoffset == 0)
932 goto bad1;
933
934 p = sc->dsp_firm->data + pageoffset;
935 blockcount = FW_GET_BYTE(p);
936 p += 1;
937
938 uea_dbg(INS_TO_USBDEV(sc),
939 "sending %u blocks for DSP page %u\n", blockcount, pageno);
940
941 bi.wHdr = cpu_to_le16(UEA_BIHDR);
942 bi.wOvl = cpu_to_le16(ovl);
943 bi.wOvlOffset = cpu_to_le16(ovl | 0x8000);
944
945 for (i = 0; i < blockcount; i++) {
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700946 blockaddr = get_unaligned_le16(p);
matthieu castetb72458a2005-11-07 23:27:13 +0100947 p += 2;
948
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700949 blocksize = get_unaligned_le16(p);
matthieu castetb72458a2005-11-07 23:27:13 +0100950 p += 2;
951
952 bi.wSize = cpu_to_le16(blocksize);
953 bi.wAddress = cpu_to_le16(blockaddr);
954 bi.wLast = cpu_to_le16((i == blockcount - 1) ? 1 : 0);
955
956 /* send block info through the IDMA pipe */
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +0200957 if (uea_idma_write(sc, &bi, E1_BLOCK_INFO_SIZE))
matthieu castetb72458a2005-11-07 23:27:13 +0100958 goto bad2;
959
960 /* send block data through the IDMA pipe */
961 if (uea_idma_write(sc, p, blocksize))
962 goto bad2;
963
964 p += blocksize;
965 }
966
967 return;
968
969bad2:
970 uea_err(INS_TO_USBDEV(sc), "sending DSP block %u failed\n", i);
971 return;
972bad1:
matthieu castet2a99b502006-04-02 18:43:53 +0200973 uea_err(INS_TO_USBDEV(sc), "invalid DSP page %u requested\n", pageno);
matthieu castetb72458a2005-11-07 23:27:13 +0100974}
975
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +0200976static void __uea_load_page_e4(struct uea_softc *sc, u8 pageno, int boot)
977{
978 struct block_info_e4 bi;
979 struct block_index *blockidx;
980 struct l1_code *p = (struct l1_code *) sc->dsp_firm->data;
981 u8 blockno = p->page_number_to_block_index[pageno];
982
983 bi.wHdr = cpu_to_be16(UEA_BIHDR);
984 bi.bBootPage = boot;
985 bi.bPageNumber = pageno;
986 bi.wReserved = cpu_to_be16(UEA_RESERVED);
987
988 do {
989 u8 *blockoffset;
990 unsigned int blocksize;
991
992 blockidx = &p->page_header[blockno];
993 blocksize = E4_PAGE_BYTES(blockidx->PageSize);
994 blockoffset = sc->dsp_firm->data + le32_to_cpu(blockidx->PageOffset);
995
996 bi.dwSize = cpu_to_be32(blocksize);
Al Virofd05e722008-04-28 07:00:16 +0100997 bi.dwAddress = cpu_to_be32(le32_to_cpu(blockidx->PageAddress));
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +0200998
999 uea_dbg(INS_TO_USBDEV(sc),
Joe Perchesdc0d5c12007-12-17 11:40:18 -08001000 "sending block %u for DSP page %u size %u address %x\n",
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001001 blockno, pageno, blocksize, le32_to_cpu(blockidx->PageAddress));
1002
1003 /* send block info through the IDMA pipe */
1004 if (uea_idma_write(sc, &bi, E4_BLOCK_INFO_SIZE))
1005 goto bad;
1006
1007 /* send block data through the IDMA pipe */
1008 if (uea_idma_write(sc, blockoffset, blocksize))
1009 goto bad;
1010
1011 blockno++;
1012 } while (blockidx->NotLastBlock);
1013
1014 return;
1015
1016bad:
1017 uea_err(INS_TO_USBDEV(sc), "sending DSP block %u failed\n", blockno);
1018 return;
1019}
1020
1021static void uea_load_page_e4(struct work_struct *work)
1022{
1023 struct uea_softc *sc = container_of(work, struct uea_softc, task);
1024 u8 pageno = sc->pageno;
1025 int i;
1026 struct block_info_e4 bi;
1027 struct l1_code *p;
1028
1029 uea_dbg(INS_TO_USBDEV(sc), "sending DSP page %u\n", pageno);
1030
1031 /* reload firmware when reboot start and it's loaded already */
1032 if (pageno == 0 && sc->dsp_firm) {
1033 release_firmware(sc->dsp_firm);
1034 sc->dsp_firm = NULL;
1035 }
1036
1037 if (sc->dsp_firm == NULL && request_dsp(sc) < 0)
1038 return;
1039
1040 p = (struct l1_code *) sc->dsp_firm->data;
Al Virofd05e722008-04-28 07:00:16 +01001041 if (pageno >= le16_to_cpu(p->page_header[0].PageNumber)) {
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001042 uea_err(INS_TO_USBDEV(sc), "invalid DSP page %u requested\n", pageno);
1043 return;
1044 }
1045
1046 if (pageno != 0) {
1047 __uea_load_page_e4(sc, pageno, 0);
1048 return;
1049 }
1050
1051 uea_dbg(INS_TO_USBDEV(sc),
1052 "sending Main DSP page %u\n", p->page_header[0].PageNumber);
1053
1054 for (i = 0; i < le16_to_cpu(p->page_header[0].PageNumber); i++) {
1055 if (E4_IS_BOOT_PAGE(p->page_header[i].PageSize))
1056 __uea_load_page_e4(sc, i, 1);
1057 }
1058
1059 uea_dbg(INS_TO_USBDEV(sc),"sending start bi\n");
1060
1061 bi.wHdr = cpu_to_be16(UEA_BIHDR);
1062 bi.bBootPage = 0;
1063 bi.bPageNumber = 0xff;
1064 bi.wReserved = cpu_to_be16(UEA_RESERVED);
1065 bi.dwSize = cpu_to_be32(E4_PAGE_BYTES(p->page_header[0].PageSize));
Al Virofd05e722008-04-28 07:00:16 +01001066 bi.dwAddress = cpu_to_be32(le32_to_cpu(p->page_header[0].PageAddress));
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001067
1068 /* send block info through the IDMA pipe */
1069 if (uea_idma_write(sc, &bi, E4_BLOCK_INFO_SIZE))
1070 uea_err(INS_TO_USBDEV(sc), "sending DSP start bi failed\n");
1071}
1072
matthieu castetb72458a2005-11-07 23:27:13 +01001073static inline void wake_up_cmv_ack(struct uea_softc *sc)
1074{
matthieu castet2a99b502006-04-02 18:43:53 +02001075 BUG_ON(sc->cmv_ack);
matthieu castetb72458a2005-11-07 23:27:13 +01001076 sc->cmv_ack = 1;
Stanislaw Gruszka337427f2007-08-20 23:21:14 +02001077 wake_up(&sc->sync_q);
matthieu castetb72458a2005-11-07 23:27:13 +01001078}
1079
1080static inline int wait_cmv_ack(struct uea_softc *sc)
1081{
Stanislaw Gruszka337427f2007-08-20 23:21:14 +02001082 int ret = uea_wait(sc, sc->cmv_ack , ACK_TIMEOUT);
1083
matthieu castetb72458a2005-11-07 23:27:13 +01001084 sc->cmv_ack = 0;
1085
matthieu castet2a99b502006-04-02 18:43:53 +02001086 uea_dbg(INS_TO_USBDEV(sc), "wait_event_timeout : %d ms\n",
1087 jiffies_to_msecs(ret));
1088
matthieu castetb72458a2005-11-07 23:27:13 +01001089 if (ret < 0)
1090 return ret;
1091
1092 return (ret == 0) ? -ETIMEDOUT : 0;
matthieu castetb72458a2005-11-07 23:27:13 +01001093}
1094
1095#define UCDC_SEND_ENCAPSULATED_COMMAND 0x00
1096
1097static int uea_request(struct uea_softc *sc,
1098 u16 value, u16 index, u16 size, void *data)
1099{
1100 u8 *xfer_buff;
1101 int ret = -ENOMEM;
1102
Eric Sesterhenn5d7efe52006-10-26 21:06:24 +02001103 xfer_buff = kmemdup(data, size, GFP_KERNEL);
matthieu castetb72458a2005-11-07 23:27:13 +01001104 if (!xfer_buff) {
1105 uea_err(INS_TO_USBDEV(sc), "can't allocate xfer_buff\n");
1106 return ret;
1107 }
matthieu castetb72458a2005-11-07 23:27:13 +01001108
1109 ret = usb_control_msg(sc->usb_dev, usb_sndctrlpipe(sc->usb_dev, 0),
1110 UCDC_SEND_ENCAPSULATED_COMMAND,
1111 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1112 value, index, xfer_buff, size, CTRL_TIMEOUT);
1113
1114 kfree(xfer_buff);
1115 if (ret < 0) {
1116 uea_err(INS_TO_USBDEV(sc), "usb_control_msg error %d\n", ret);
1117 return ret;
1118 }
1119
1120 if (ret != size) {
1121 uea_err(INS_TO_USBDEV(sc),
1122 "usb_control_msg send only %d bytes (instead of %d)\n",
1123 ret, size);
1124 return -EIO;
1125 }
1126
1127 return 0;
1128}
1129
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001130static int uea_cmv_e1(struct uea_softc *sc,
matthieu castetb72458a2005-11-07 23:27:13 +01001131 u8 function, u32 address, u16 offset, u32 data)
1132{
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001133 struct cmv_e1 cmv;
matthieu castetb72458a2005-11-07 23:27:13 +01001134 int ret;
1135
matthieu castet2a99b502006-04-02 18:43:53 +02001136 uea_enters(INS_TO_USBDEV(sc));
1137 uea_vdbg(INS_TO_USBDEV(sc), "Function : %d-%d, Address : %c%c%c%c, "
1138 "offset : 0x%04x, data : 0x%08x\n",
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001139 E1_FUNCTION_TYPE(function), E1_FUNCTION_SUBTYPE(function),
1140 E1_GETSA1(address), E1_GETSA2(address), E1_GETSA3(address),
1141 E1_GETSA4(address), offset, data);
matthieu castetb72458a2005-11-07 23:27:13 +01001142
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001143 /* we send a request, but we expect a reply */
1144 sc->cmv_dsc.e1.function = function | 0x2;
1145 sc->cmv_dsc.e1.idx++;
1146 sc->cmv_dsc.e1.address = address;
1147 sc->cmv_dsc.e1.offset = offset;
1148
1149 cmv.wPreamble = cpu_to_le16(E1_PREAMBLE);
1150 cmv.bDirection = E1_HOSTTOMODEM;
matthieu castetb72458a2005-11-07 23:27:13 +01001151 cmv.bFunction = function;
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001152 cmv.wIndex = cpu_to_le16(sc->cmv_dsc.e1.idx);
Harvey Harrisona5abdea2008-04-29 01:03:40 -07001153 put_unaligned_le32(address, &cmv.dwSymbolicAddress);
matthieu castetb72458a2005-11-07 23:27:13 +01001154 cmv.wOffsetAddress = cpu_to_le16(offset);
Harvey Harrisona5abdea2008-04-29 01:03:40 -07001155 put_unaligned_le32(data >> 16 | data << 16, &cmv.dwData);
matthieu castetb72458a2005-11-07 23:27:13 +01001156
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001157 ret = uea_request(sc, UEA_E1_SET_BLOCK, UEA_MPTX_START, sizeof(cmv), &cmv);
matthieu castetb72458a2005-11-07 23:27:13 +01001158 if (ret < 0)
1159 return ret;
matthieu castet2a99b502006-04-02 18:43:53 +02001160 ret = wait_cmv_ack(sc);
1161 uea_leaves(INS_TO_USBDEV(sc));
1162 return ret;
matthieu castetb72458a2005-11-07 23:27:13 +01001163}
1164
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001165static int uea_cmv_e4(struct uea_softc *sc,
1166 u16 function, u16 group, u16 address, u16 offset, u32 data)
1167{
1168 struct cmv_e4 cmv;
1169 int ret;
1170
1171 uea_enters(INS_TO_USBDEV(sc));
1172 memset(&cmv, 0, sizeof(cmv));
1173
1174 uea_vdbg(INS_TO_USBDEV(sc), "Function : %d-%d, Group : 0x%04x, "
1175 "Address : 0x%04x, offset : 0x%04x, data : 0x%08x\n",
1176 E4_FUNCTION_TYPE(function), E4_FUNCTION_SUBTYPE(function),
1177 group, address, offset, data);
1178
1179 /* we send a request, but we expect a reply */
1180 sc->cmv_dsc.e4.function = function | (0x1 << 4);
1181 sc->cmv_dsc.e4.offset = offset;
1182 sc->cmv_dsc.e4.address = address;
1183 sc->cmv_dsc.e4.group = group;
1184
1185 cmv.wFunction = cpu_to_be16(function);
1186 cmv.wGroup = cpu_to_be16(group);
1187 cmv.wAddress = cpu_to_be16(address);
1188 cmv.wOffset = cpu_to_be16(offset);
1189 cmv.dwData[0] = cpu_to_be32(data);
1190
1191 ret = uea_request(sc, UEA_E4_SET_BLOCK, UEA_MPTX_START, sizeof(cmv), &cmv);
1192 if (ret < 0)
1193 return ret;
1194 ret = wait_cmv_ack(sc);
1195 uea_leaves(INS_TO_USBDEV(sc));
1196 return ret;
1197}
1198
1199static inline int uea_read_cmv_e1(struct uea_softc *sc,
matthieu castetb72458a2005-11-07 23:27:13 +01001200 u32 address, u16 offset, u32 *data)
1201{
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001202 int ret = uea_cmv_e1(sc, E1_MAKEFUNCTION(E1_MEMACCESS, E1_REQUESTREAD),
matthieu castetb72458a2005-11-07 23:27:13 +01001203 address, offset, 0);
1204 if (ret < 0)
1205 uea_err(INS_TO_USBDEV(sc),
1206 "reading cmv failed with error %d\n", ret);
1207 else
1208 *data = sc->data;
1209
1210 return ret;
1211}
1212
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001213static inline int uea_read_cmv_e4(struct uea_softc *sc,
1214 u8 size, u16 group, u16 address, u16 offset, u32 *data)
1215{
1216 int ret = uea_cmv_e4(sc, E4_MAKEFUNCTION(E4_MEMACCESS, E4_REQUESTREAD, size),
1217 group, address, offset, 0);
1218 if (ret < 0)
1219 uea_err(INS_TO_USBDEV(sc),
1220 "reading cmv failed with error %d\n", ret);
1221 else {
1222 *data = sc->data;
1223 /* size is in 16-bit word quantities */
1224 if (size > 2)
1225 *(data + 1) = sc->data1;
1226 }
1227 return ret;
1228}
1229
1230static inline int uea_write_cmv_e1(struct uea_softc *sc,
matthieu castetb72458a2005-11-07 23:27:13 +01001231 u32 address, u16 offset, u32 data)
1232{
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001233 int ret = uea_cmv_e1(sc, E1_MAKEFUNCTION(E1_MEMACCESS, E1_REQUESTWRITE),
matthieu castetb72458a2005-11-07 23:27:13 +01001234 address, offset, data);
1235 if (ret < 0)
1236 uea_err(INS_TO_USBDEV(sc),
1237 "writing cmv failed with error %d\n", ret);
1238
1239 return ret;
1240}
1241
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001242static inline int uea_write_cmv_e4(struct uea_softc *sc,
1243 u8 size, u16 group, u16 address, u16 offset, u32 data)
1244{
1245 int ret = uea_cmv_e4(sc, E4_MAKEFUNCTION(E4_MEMACCESS, E4_REQUESTWRITE, size),
1246 group, address, offset, data);
1247 if (ret < 0)
1248 uea_err(INS_TO_USBDEV(sc),
1249 "writing cmv failed with error %d\n", ret);
1250
1251 return ret;
1252}
1253
1254static void uea_set_bulk_timeout(struct uea_softc *sc, u32 dsrate)
1255{
1256 int ret;
1257 u16 timeout;
1258
1259 /* in bulk mode the modem have problem with high rate
1260 * changing internal timing could improve things, but the
1261 * value is misterious.
1262 * ADI930 don't support it (-EPIPE error).
1263 */
1264
1265 if (UEA_CHIP_VERSION(sc) == ADI930 ||
Stanislaw Gruszka503add42007-08-20 23:21:06 +02001266 altsetting[sc->modem_index] > 0 ||
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001267 sc->stats.phy.dsrate == dsrate)
1268 return;
1269
1270 /* Original timming (1Mbit/s) from ADI (used in windows driver) */
1271 timeout = (dsrate <= 1024*1024) ? 0 : 1;
1272 ret = uea_request(sc, UEA_SET_TIMEOUT, timeout, 0, NULL);
1273 uea_info(INS_TO_USBDEV(sc), "setting new timeout %d%s\n",
1274 timeout, ret < 0 ? " failed" : "");
1275
1276}
1277
matthieu castetb72458a2005-11-07 23:27:13 +01001278/*
1279 * Monitor the modem and update the stat
1280 * return 0 if everything is ok
1281 * return < 0 if an error occurs (-EAGAIN reboot needed)
1282 */
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001283static int uea_stat_e1(struct uea_softc *sc)
matthieu castetb72458a2005-11-07 23:27:13 +01001284{
1285 u32 data;
1286 int ret;
1287
1288 uea_enters(INS_TO_USBDEV(sc));
1289 data = sc->stats.phy.state;
1290
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001291 ret = uea_read_cmv_e1(sc, E1_SA_STAT, 0, &sc->stats.phy.state);
matthieu castetb72458a2005-11-07 23:27:13 +01001292 if (ret < 0)
1293 return ret;
1294
1295 switch (GET_STATUS(sc->stats.phy.state)) {
1296 case 0: /* not yet synchronized */
1297 uea_dbg(INS_TO_USBDEV(sc),
1298 "modem not yet synchronized\n");
1299 return 0;
1300
1301 case 1: /* initialization */
1302 uea_dbg(INS_TO_USBDEV(sc), "modem initializing\n");
1303 return 0;
1304
1305 case 2: /* operational */
1306 uea_vdbg(INS_TO_USBDEV(sc), "modem operational\n");
1307 break;
1308
1309 case 3: /* fail ... */
matthieu casteta7a0c9c2006-10-03 21:44:11 +02001310 uea_info(INS_TO_USBDEV(sc), "modem synchronization failed"
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001311 " (may be try other cmv/dsp)\n");
matthieu castetb72458a2005-11-07 23:27:13 +01001312 return -EAGAIN;
1313
1314 case 4 ... 6: /* test state */
1315 uea_warn(INS_TO_USBDEV(sc),
1316 "modem in test mode - not supported\n");
1317 return -EAGAIN;
1318
1319 case 7: /* fast-retain ... */
1320 uea_info(INS_TO_USBDEV(sc), "modem in fast-retain mode\n");
1321 return 0;
1322 default:
1323 uea_err(INS_TO_USBDEV(sc), "modem invalid SW mode %d\n",
1324 GET_STATUS(sc->stats.phy.state));
1325 return -EAGAIN;
1326 }
1327
1328 if (GET_STATUS(data) != 2) {
1329 uea_request(sc, UEA_SET_MODE, UEA_LOOPBACK_OFF, 0, NULL);
1330 uea_info(INS_TO_USBDEV(sc), "modem operational\n");
1331
1332 /* release the dsp firmware as it is not needed until
1333 * the next failure
1334 */
1335 if (sc->dsp_firm) {
1336 release_firmware(sc->dsp_firm);
1337 sc->dsp_firm = NULL;
1338 }
matthieu castetb72458a2005-11-07 23:27:13 +01001339 }
1340
1341 /* always update it as atm layer could not be init when we switch to
1342 * operational state
1343 */
1344 UPDATE_ATM_STAT(signal, ATM_PHY_SIG_FOUND);
1345
1346 /* wake up processes waiting for synchronization */
1347 wake_up(&sc->sync_q);
1348
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001349 ret = uea_read_cmv_e1(sc, E1_SA_DIAG, 2, &sc->stats.phy.flags);
matthieu castetb72458a2005-11-07 23:27:13 +01001350 if (ret < 0)
1351 return ret;
1352 sc->stats.phy.mflags |= sc->stats.phy.flags;
1353
1354 /* in case of a flags ( for example delineation LOSS (& 0x10)),
1355 * we check the status again in order to detect the failure earlier
1356 */
1357 if (sc->stats.phy.flags) {
matthieu castet2a99b502006-04-02 18:43:53 +02001358 uea_dbg(INS_TO_USBDEV(sc), "Stat flag = 0x%x\n",
matthieu castetb72458a2005-11-07 23:27:13 +01001359 sc->stats.phy.flags);
1360 return 0;
1361 }
1362
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001363 ret = uea_read_cmv_e1(sc, E1_SA_RATE, 0, &data);
matthieu castetb72458a2005-11-07 23:27:13 +01001364 if (ret < 0)
1365 return ret;
1366
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001367 uea_set_bulk_timeout(sc, (data >> 16) * 32);
matthieu castetb72458a2005-11-07 23:27:13 +01001368 sc->stats.phy.dsrate = (data >> 16) * 32;
1369 sc->stats.phy.usrate = (data & 0xffff) * 32;
1370 UPDATE_ATM_STAT(link_rate, sc->stats.phy.dsrate * 1000 / 424);
1371
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001372 ret = uea_read_cmv_e1(sc, E1_SA_DIAG, 23, &data);
matthieu castetb72458a2005-11-07 23:27:13 +01001373 if (ret < 0)
1374 return ret;
1375 sc->stats.phy.dsattenuation = (data & 0xff) / 2;
1376
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001377 ret = uea_read_cmv_e1(sc, E1_SA_DIAG, 47, &data);
matthieu castetb72458a2005-11-07 23:27:13 +01001378 if (ret < 0)
1379 return ret;
1380 sc->stats.phy.usattenuation = (data & 0xff) / 2;
1381
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001382 ret = uea_read_cmv_e1(sc, E1_SA_DIAG, 25, &sc->stats.phy.dsmargin);
matthieu castetb72458a2005-11-07 23:27:13 +01001383 if (ret < 0)
1384 return ret;
1385
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001386 ret = uea_read_cmv_e1(sc, E1_SA_DIAG, 49, &sc->stats.phy.usmargin);
matthieu castetb72458a2005-11-07 23:27:13 +01001387 if (ret < 0)
1388 return ret;
1389
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001390 ret = uea_read_cmv_e1(sc, E1_SA_DIAG, 51, &sc->stats.phy.rxflow);
matthieu castetb72458a2005-11-07 23:27:13 +01001391 if (ret < 0)
1392 return ret;
1393
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001394 ret = uea_read_cmv_e1(sc, E1_SA_DIAG, 52, &sc->stats.phy.txflow);
matthieu castetb72458a2005-11-07 23:27:13 +01001395 if (ret < 0)
1396 return ret;
1397
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001398 ret = uea_read_cmv_e1(sc, E1_SA_DIAG, 54, &sc->stats.phy.dsunc);
matthieu castetb72458a2005-11-07 23:27:13 +01001399 if (ret < 0)
1400 return ret;
1401
1402 /* only for atu-c */
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001403 ret = uea_read_cmv_e1(sc, E1_SA_DIAG, 58, &sc->stats.phy.usunc);
matthieu castetb72458a2005-11-07 23:27:13 +01001404 if (ret < 0)
1405 return ret;
1406
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001407 ret = uea_read_cmv_e1(sc, E1_SA_DIAG, 53, &sc->stats.phy.dscorr);
matthieu castetb72458a2005-11-07 23:27:13 +01001408 if (ret < 0)
1409 return ret;
1410
1411 /* only for atu-c */
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001412 ret = uea_read_cmv_e1(sc, E1_SA_DIAG, 57, &sc->stats.phy.uscorr);
matthieu castetb72458a2005-11-07 23:27:13 +01001413 if (ret < 0)
1414 return ret;
1415
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001416 ret = uea_read_cmv_e1(sc, E1_SA_INFO, 8, &sc->stats.phy.vidco);
matthieu castetb72458a2005-11-07 23:27:13 +01001417 if (ret < 0)
1418 return ret;
1419
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001420 ret = uea_read_cmv_e1(sc, E1_SA_INFO, 13, &sc->stats.phy.vidcpe);
matthieu castetb72458a2005-11-07 23:27:13 +01001421 if (ret < 0)
1422 return ret;
1423
1424 return 0;
1425}
1426
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001427static int uea_stat_e4(struct uea_softc *sc)
matthieu castetb72458a2005-11-07 23:27:13 +01001428{
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001429 u32 data;
1430 u32 tmp_arr[2];
1431 int ret;
matthieu castetb72458a2005-11-07 23:27:13 +01001432
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001433 uea_enters(INS_TO_USBDEV(sc));
1434 data = sc->stats.phy.state;
1435
1436 /* XXX only need to be done before operationnal... */
1437 ret = uea_read_cmv_e4(sc, 1, E4_SA_STAT, 0, 0, &sc->stats.phy.state);
1438 if (ret < 0)
1439 return ret;
1440
1441 switch (sc->stats.phy.state) {
1442 case 0x0: /* not yet synchronized */
1443 case 0x1:
1444 case 0x3:
1445 case 0x4:
1446 uea_dbg(INS_TO_USBDEV(sc), "modem not yet synchronized\n");
1447 return 0;
1448 case 0x5: /* initialization */
1449 case 0x6:
1450 case 0x9:
1451 case 0xa:
1452 uea_dbg(INS_TO_USBDEV(sc), "modem initializing\n");
1453 return 0;
1454 case 0x2: /* fail ... */
1455 uea_info(INS_TO_USBDEV(sc), "modem synchronization failed"
1456 " (may be try other cmv/dsp)\n");
1457 return -EAGAIN;
1458 case 0x7: /* operational */
1459 break;
1460 default:
1461 uea_warn(INS_TO_USBDEV(sc), "unknown state: %x\n", sc->stats.phy.state);
1462 return 0;
1463 }
1464
1465 if (data != 7) {
1466 uea_request(sc, UEA_SET_MODE, UEA_LOOPBACK_OFF, 0, NULL);
1467 uea_info(INS_TO_USBDEV(sc), "modem operational\n");
1468
1469 /* release the dsp firmware as it is not needed until
1470 * the next failure
1471 */
1472 if (sc->dsp_firm) {
1473 release_firmware(sc->dsp_firm);
1474 sc->dsp_firm = NULL;
1475 }
1476 }
1477
1478 /* always update it as atm layer could not be init when we switch to
1479 * operational state
1480 */
1481 UPDATE_ATM_STAT(signal, ATM_PHY_SIG_FOUND);
1482
1483 /* wake up processes waiting for synchronization */
1484 wake_up(&sc->sync_q);
1485
1486 /* TODO improve this state machine :
1487 * we need some CMV info : what they do and their unit
1488 * we should find the equivalent of eagle3- CMV
1489 */
1490 /* check flags */
1491 ret = uea_read_cmv_e4(sc, 1, E4_SA_DIAG, 0, 0, &sc->stats.phy.flags);
1492 if (ret < 0)
1493 return ret;
1494 sc->stats.phy.mflags |= sc->stats.phy.flags;
1495
1496 /* in case of a flags ( for example delineation LOSS (& 0x10)),
1497 * we check the status again in order to detect the failure earlier
1498 */
1499 if (sc->stats.phy.flags) {
1500 uea_dbg(INS_TO_USBDEV(sc), "Stat flag = 0x%x\n",
1501 sc->stats.phy.flags);
1502 if (sc->stats.phy.flags & 1) //delineation LOSS
1503 return -EAGAIN;
1504 if (sc->stats.phy.flags & 0x4000) //Reset Flag
1505 return -EAGAIN;
1506 return 0;
1507 }
1508
1509 /* rate data may be in upper or lower half of 64 bit word, strange */
1510 ret = uea_read_cmv_e4(sc, 4, E4_SA_RATE, 0, 0, tmp_arr);
1511 if (ret < 0)
1512 return ret;
1513 data = (tmp_arr[0]) ? tmp_arr[0] : tmp_arr[1];
1514 sc->stats.phy.usrate = data / 1000;
1515
1516 ret = uea_read_cmv_e4(sc, 4, E4_SA_RATE, 1, 0, tmp_arr);
1517 if (ret < 0)
1518 return ret;
1519 data = (tmp_arr[0]) ? tmp_arr[0] : tmp_arr[1];
1520 uea_set_bulk_timeout(sc, data / 1000);
1521 sc->stats.phy.dsrate = data / 1000;
1522 UPDATE_ATM_STAT(link_rate, sc->stats.phy.dsrate * 1000 / 424);
1523
1524 ret = uea_read_cmv_e4(sc, 1, E4_SA_INFO, 68, 1, &data);
1525 if (ret < 0)
1526 return ret;
1527 sc->stats.phy.dsattenuation = data / 10;
1528
1529 ret = uea_read_cmv_e4(sc, 1, E4_SA_INFO, 69, 1, &data);
1530 if (ret < 0)
1531 return ret;
1532 sc->stats.phy.usattenuation = data / 10;
1533
1534 ret = uea_read_cmv_e4(sc, 1, E4_SA_INFO, 68, 3, &data);
1535 if (ret < 0)
1536 return ret;
1537 sc->stats.phy.dsmargin = data / 2;
1538
1539 ret = uea_read_cmv_e4(sc, 1, E4_SA_INFO, 69, 3, &data);
1540 if (ret < 0)
1541 return ret;
1542 sc->stats.phy.usmargin = data / 10;
1543
1544 return 0;
1545}
1546
1547static void cmvs_file_name(struct uea_softc *sc, char *const cmv_name, int ver)
1548{
1549 char file_arr[] = "CMVxy.bin";
1550 char *file;
1551
1552 /* set proper name corresponding modem version and line type */
matthieu castetb72458a2005-11-07 23:27:13 +01001553 if (cmv_file[sc->modem_index] == NULL) {
1554 if (UEA_CHIP_VERSION(sc) == ADI930)
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001555 file_arr[3] = '9';
1556 else if (UEA_CHIP_VERSION(sc) == EAGLE_IV)
1557 file_arr[3] = '4';
matthieu castetb72458a2005-11-07 23:27:13 +01001558 else
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001559 file_arr[3] = 'e';
1560
Stanislaw Gruszka603cf602007-08-20 23:21:01 +02001561 file_arr[4] = IS_ISDN(sc) ? 'i' : 'p';
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001562 file = file_arr;
matthieu castetb72458a2005-11-07 23:27:13 +01001563 } else
1564 file = cmv_file[sc->modem_index];
1565
1566 strcpy(cmv_name, FW_DIR);
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001567 strlcat(cmv_name, file, FIRMWARE_NAME_MAX);
1568 if (ver == 2)
1569 strlcat(cmv_name, ".v2", FIRMWARE_NAME_MAX);
1570}
matthieu castetb72458a2005-11-07 23:27:13 +01001571
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001572static int request_cmvs_old(struct uea_softc *sc,
1573 void **cmvs, const struct firmware **fw)
1574{
1575 int ret, size;
1576 u8 *data;
1577 char cmv_name[FIRMWARE_NAME_MAX]; /* 30 bytes stack variable */
1578
1579 cmvs_file_name(sc, cmv_name, 1);
matthieu castetb72458a2005-11-07 23:27:13 +01001580 ret = request_firmware(fw, cmv_name, &sc->usb_dev->dev);
1581 if (ret < 0) {
1582 uea_err(INS_TO_USBDEV(sc),
1583 "requesting firmware %s failed with error %d\n",
1584 cmv_name, ret);
1585 return ret;
1586 }
1587
1588 data = (u8 *) (*fw)->data;
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001589 size = (*fw)->size;
1590 if (size < 1)
1591 goto err_fw_corrupted;
1592
1593 if (size != *data * sizeof(struct uea_cmvs_v1) + 1)
1594 goto err_fw_corrupted;
1595
1596 *cmvs = (void *)(data + 1);
1597 return *data;
1598
1599err_fw_corrupted:
1600 uea_err(INS_TO_USBDEV(sc), "firmware %s is corrupted\n", cmv_name);
1601 release_firmware(*fw);
1602 return -EILSEQ;
1603}
1604
1605static int request_cmvs(struct uea_softc *sc,
1606 void **cmvs, const struct firmware **fw, int *ver)
1607{
1608 int ret, size;
1609 u32 crc;
1610 u8 *data;
1611 char cmv_name[FIRMWARE_NAME_MAX]; /* 30 bytes stack variable */
1612
1613 cmvs_file_name(sc, cmv_name, 2);
1614 ret = request_firmware(fw, cmv_name, &sc->usb_dev->dev);
1615 if (ret < 0) {
1616 /* if caller can handle old version, try to provide it */
1617 if (*ver == 1) {
1618 uea_warn(INS_TO_USBDEV(sc), "requesting firmware %s failed, "
1619 "try to get older cmvs\n", cmv_name);
1620 return request_cmvs_old(sc, cmvs, fw);
1621 }
1622 uea_err(INS_TO_USBDEV(sc),
1623 "requesting firmware %s failed with error %d\n",
1624 cmv_name, ret);
1625 return ret;
matthieu castetb72458a2005-11-07 23:27:13 +01001626 }
1627
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001628 size = (*fw)->size;
1629 data = (u8 *) (*fw)->data;
1630 if (size < 4 || strncmp(data, "cmv2", 4) != 0) {
1631 if (*ver == 1) {
1632 uea_warn(INS_TO_USBDEV(sc), "firmware %s is corrupted, "
1633 "try to get older cmvs\n", cmv_name);
1634 release_firmware(*fw);
1635 return request_cmvs_old(sc, cmvs, fw);
1636 }
1637 goto err_fw_corrupted;
1638 }
1639
1640 *ver = 2;
1641
1642 data += 4;
1643 size -= 4;
1644 if (size < 5)
1645 goto err_fw_corrupted;
1646
Harvey Harrisona5abdea2008-04-29 01:03:40 -07001647 crc = get_unaligned_le32(data);
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001648 data += 4;
1649 size -= 4;
1650 if (crc32_be(0, data, size) != crc)
1651 goto err_fw_corrupted;
1652
1653 if (size != *data * sizeof(struct uea_cmvs_v2) + 1)
1654 goto err_fw_corrupted;
1655
1656 *cmvs = (void *) (data + 1);
matthieu castetb72458a2005-11-07 23:27:13 +01001657 return *data;
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001658
1659err_fw_corrupted:
1660 uea_err(INS_TO_USBDEV(sc), "firmware %s is corrupted\n", cmv_name);
1661 release_firmware(*fw);
1662 return -EILSEQ;
1663}
1664
1665static int uea_send_cmvs_e1(struct uea_softc *sc)
1666{
1667 int i, ret, len;
1668 void *cmvs_ptr;
1669 const struct firmware *cmvs_fw;
1670 int ver = 1; // we can handle v1 cmv firmware version;
1671
1672 /* Enter in R-IDLE (cmv) until instructed otherwise */
1673 ret = uea_write_cmv_e1(sc, E1_SA_CNTL, 0, 1);
1674 if (ret < 0)
1675 return ret;
1676
1677 /* Dump firmware version */
1678 ret = uea_read_cmv_e1(sc, E1_SA_INFO, 10, &sc->stats.phy.firmid);
1679 if (ret < 0)
1680 return ret;
1681 uea_info(INS_TO_USBDEV(sc), "ATU-R firmware version : %x\n",
1682 sc->stats.phy.firmid);
1683
1684 /* get options */
1685 ret = len = request_cmvs(sc, &cmvs_ptr, &cmvs_fw, &ver);
1686 if (ret < 0)
1687 return ret;
1688
1689 /* send options */
1690 if (ver == 1) {
1691 struct uea_cmvs_v1 *cmvs_v1 = cmvs_ptr;
1692
1693 uea_warn(INS_TO_USBDEV(sc), "use deprecated cmvs version, "
1694 "please update your firmware\n");
1695
1696 for (i = 0; i < len; i++) {
Harvey Harrisona5abdea2008-04-29 01:03:40 -07001697 ret = uea_write_cmv_e1(sc, get_unaligned_le32(&cmvs_v1[i].address),
1698 get_unaligned_le16(&cmvs_v1[i].offset),
1699 get_unaligned_le32(&cmvs_v1[i].data));
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001700 if (ret < 0)
1701 goto out;
1702 }
1703 } else if (ver == 2) {
1704 struct uea_cmvs_v2 *cmvs_v2 = cmvs_ptr;
1705
1706 for (i = 0; i < len; i++) {
Harvey Harrisona5abdea2008-04-29 01:03:40 -07001707 ret = uea_write_cmv_e1(sc, get_unaligned_le32(&cmvs_v2[i].address),
1708 (u16) get_unaligned_le32(&cmvs_v2[i].offset),
1709 get_unaligned_le32(&cmvs_v2[i].data));
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001710 if (ret < 0)
1711 goto out;
1712 }
1713 } else {
1714 /* This realy should not happen */
1715 uea_err(INS_TO_USBDEV(sc), "bad cmvs version %d\n", ver);
1716 goto out;
1717 }
1718
1719 /* Enter in R-ACT-REQ */
1720 ret = uea_write_cmv_e1(sc, E1_SA_CNTL, 0, 2);
1721 uea_vdbg(INS_TO_USBDEV(sc), "Entering in R-ACT-REQ state\n");
1722 uea_info(INS_TO_USBDEV(sc), "modem started, waiting synchronization...\n");
1723out:
1724 release_firmware(cmvs_fw);
1725 return ret;
1726}
1727
1728static int uea_send_cmvs_e4(struct uea_softc *sc)
1729{
1730 int i, ret, len;
1731 void *cmvs_ptr;
1732 const struct firmware *cmvs_fw;
1733 int ver = 2; // we can only handle v2 cmv firmware version;
1734
1735 /* Enter in R-IDLE (cmv) until instructed otherwise */
1736 ret = uea_write_cmv_e4(sc, 1, E4_SA_CNTL, 0, 0, 1);
1737 if (ret < 0)
1738 return ret;
1739
1740 /* Dump firmware version */
1741 /* XXX don't read the 3th byte as it is always 6 */
1742 ret = uea_read_cmv_e4(sc, 2, E4_SA_INFO, 55, 0, &sc->stats.phy.firmid);
1743 if (ret < 0)
1744 return ret;
1745 uea_info(INS_TO_USBDEV(sc), "ATU-R firmware version : %x\n",
1746 sc->stats.phy.firmid);
1747
1748
1749 /* get options */
1750 ret = len = request_cmvs(sc, &cmvs_ptr, &cmvs_fw, &ver);
1751 if (ret < 0)
1752 return ret;
1753
1754 /* send options */
1755 if (ver == 2) {
1756 struct uea_cmvs_v2 *cmvs_v2 = cmvs_ptr;
1757
1758 for (i = 0; i < len; i++) {
1759 ret = uea_write_cmv_e4(sc, 1,
Harvey Harrisona5abdea2008-04-29 01:03:40 -07001760 get_unaligned_le32(&cmvs_v2[i].group),
1761 get_unaligned_le32(&cmvs_v2[i].address),
1762 get_unaligned_le32(&cmvs_v2[i].offset),
1763 get_unaligned_le32(&cmvs_v2[i].data));
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001764 if (ret < 0)
1765 goto out;
1766 }
1767 } else {
1768 /* This realy should not happen */
1769 uea_err(INS_TO_USBDEV(sc), "bad cmvs version %d\n", ver);
1770 goto out;
1771 }
1772
1773 /* Enter in R-ACT-REQ */
1774 ret = uea_write_cmv_e4(sc, 1, E4_SA_CNTL, 0, 0, 2);
1775 uea_vdbg(INS_TO_USBDEV(sc), "Entering in R-ACT-REQ state\n");
1776 uea_info(INS_TO_USBDEV(sc), "modem started, waiting synchronization...\n");
1777out:
1778 release_firmware(cmvs_fw);
1779 return ret;
matthieu castetb72458a2005-11-07 23:27:13 +01001780}
1781
1782/* Start boot post firmware modem:
1783 * - send reset commands through usb control pipe
1784 * - start workqueue for DSP loading
1785 * - send CMV options to modem
1786 */
1787
1788static int uea_start_reset(struct uea_softc *sc)
1789{
1790 u16 zero = 0; /* ;-) */
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001791 int ret;
matthieu castetb72458a2005-11-07 23:27:13 +01001792
1793 uea_enters(INS_TO_USBDEV(sc));
1794 uea_info(INS_TO_USBDEV(sc), "(re)booting started\n");
1795
matthieu castet22fcceb2006-04-02 18:44:20 +02001796 /* mask interrupt */
matthieu castetb72458a2005-11-07 23:27:13 +01001797 sc->booting = 1;
matthieu castet22fcceb2006-04-02 18:44:20 +02001798 /* We need to set this here because, a ack timeout could have occured,
1799 * but before we start the reboot, the ack occurs and set this to 1.
1800 * So we will failed to wait Ready CMV.
1801 */
1802 sc->cmv_ack = 0;
matthieu castetb72458a2005-11-07 23:27:13 +01001803 UPDATE_ATM_STAT(signal, ATM_PHY_SIG_LOST);
1804
1805 /* reset statistics */
1806 memset(&sc->stats, 0, sizeof(struct uea_stats));
1807
1808 /* tell the modem that we want to boot in IDMA mode */
1809 uea_request(sc, UEA_SET_MODE, UEA_LOOPBACK_ON, 0, NULL);
1810 uea_request(sc, UEA_SET_MODE, UEA_BOOT_IDMA, 0, NULL);
1811
1812 /* enter reset mode */
1813 uea_request(sc, UEA_SET_MODE, UEA_START_RESET, 0, NULL);
1814
1815 /* original driver use 200ms, but windows driver use 100ms */
Stanislaw Gruszka337427f2007-08-20 23:21:14 +02001816 ret = uea_wait(sc, 0, msecs_to_jiffies(100));
1817 if (ret < 0)
1818 return ret;
matthieu castetb72458a2005-11-07 23:27:13 +01001819
1820 /* leave reset mode */
1821 uea_request(sc, UEA_SET_MODE, UEA_END_RESET, 0, NULL);
1822
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001823 if (UEA_CHIP_VERSION(sc) != EAGLE_IV) {
1824 /* clear tx and rx mailboxes */
1825 uea_request(sc, UEA_SET_2183_DATA, UEA_MPTX_MAILBOX, 2, &zero);
1826 uea_request(sc, UEA_SET_2183_DATA, UEA_MPRX_MAILBOX, 2, &zero);
1827 uea_request(sc, UEA_SET_2183_DATA, UEA_SWAP_MAILBOX, 2, &zero);
1828 }
matthieu castetb72458a2005-11-07 23:27:13 +01001829
Stanislaw Gruszka337427f2007-08-20 23:21:14 +02001830 ret = uea_wait(sc, 0, msecs_to_jiffies(1000));
1831 if (ret < 0)
1832 return ret;
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001833
1834 if (UEA_CHIP_VERSION(sc) == EAGLE_IV)
1835 sc->cmv_dsc.e4.function = E4_MAKEFUNCTION(E4_ADSLDIRECTIVE, E4_MODEMREADY, 1);
1836 else
1837 sc->cmv_dsc.e1.function = E1_MAKEFUNCTION(E1_ADSLDIRECTIVE, E1_MODEMREADY);
1838
matthieu castet22fcceb2006-04-02 18:44:20 +02001839 /* demask interrupt */
matthieu castetb72458a2005-11-07 23:27:13 +01001840 sc->booting = 0;
1841
1842 /* start loading DSP */
1843 sc->pageno = 0;
1844 sc->ovl = 0;
Stanislaw Gruszka04ea02f2007-08-20 23:21:10 +02001845 queue_work(sc->work_q, &sc->task);
matthieu castetb72458a2005-11-07 23:27:13 +01001846
1847 /* wait for modem ready CMV */
1848 ret = wait_cmv_ack(sc);
1849 if (ret < 0)
1850 return ret;
1851
matthieu castet2a99b502006-04-02 18:43:53 +02001852 uea_vdbg(INS_TO_USBDEV(sc), "Ready CMV received\n");
1853
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001854 ret = sc->send_cmvs(sc);
matthieu castetb72458a2005-11-07 23:27:13 +01001855 if (ret < 0)
1856 return ret;
1857
matthieu castetb72458a2005-11-07 23:27:13 +01001858 sc->reset = 0;
1859 uea_leaves(INS_TO_USBDEV(sc));
1860 return ret;
1861}
1862
1863/*
1864 * In case of an error wait 1s before rebooting the modem
1865 * if the modem don't request reboot (-EAGAIN).
1866 * Monitor the modem every 1s.
1867 */
1868
1869static int uea_kthread(void *data)
1870{
1871 struct uea_softc *sc = data;
1872 int ret = -EAGAIN;
1873
Rafael J. Wysocki83144182007-07-17 04:03:35 -07001874 set_freezable();
matthieu castetb72458a2005-11-07 23:27:13 +01001875 uea_enters(INS_TO_USBDEV(sc));
1876 while (!kthread_should_stop()) {
1877 if (ret < 0 || sc->reset)
1878 ret = uea_start_reset(sc);
1879 if (!ret)
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001880 ret = sc->stat(sc);
matthieu castetb72458a2005-11-07 23:27:13 +01001881 if (ret != -EAGAIN)
Stanislaw Gruszka337427f2007-08-20 23:21:14 +02001882 uea_wait(sc, 0, msecs_to_jiffies(1000));
Stanislaw Gruszka0eb02262007-08-20 23:21:19 +02001883 try_to_freeze();
matthieu castetb72458a2005-11-07 23:27:13 +01001884 }
1885 uea_leaves(INS_TO_USBDEV(sc));
1886 return ret;
1887}
1888
1889/* Load second usb firmware for ADI930 chip */
1890static int load_XILINX_firmware(struct uea_softc *sc)
1891{
1892 const struct firmware *fw_entry;
1893 int ret, size, u, ln;
1894 u8 *pfw, value;
1895 char *fw_name = FW_DIR "930-fpga.bin";
1896
1897 uea_enters(INS_TO_USBDEV(sc));
1898
1899 ret = request_firmware(&fw_entry, fw_name, &sc->usb_dev->dev);
1900 if (ret) {
1901 uea_err(INS_TO_USBDEV(sc), "firmware %s is not available\n",
1902 fw_name);
1903 goto err0;
1904 }
1905
1906 pfw = fw_entry->data;
1907 size = fw_entry->size;
1908 if (size != 0x577B) {
1909 uea_err(INS_TO_USBDEV(sc), "firmware %s is corrupted\n",
1910 fw_name);
1911 ret = -EILSEQ;
1912 goto err1;
1913 }
1914 for (u = 0; u < size; u += ln) {
1915 ln = min(size - u, 64);
1916 ret = uea_request(sc, 0xe, 0, ln, pfw + u);
1917 if (ret < 0) {
1918 uea_err(INS_TO_USBDEV(sc),
1919 "elsa download data failed (%d)\n", ret);
1920 goto err1;
1921 }
1922 }
1923
matthieu castete40abaf2006-01-18 07:38:37 +01001924 /* finish to send the fpga */
matthieu castetb72458a2005-11-07 23:27:13 +01001925 ret = uea_request(sc, 0xe, 1, 0, NULL);
1926 if (ret < 0) {
1927 uea_err(INS_TO_USBDEV(sc),
1928 "elsa download data failed (%d)\n", ret);
1929 goto err1;
1930 }
1931
matthieu castete40abaf2006-01-18 07:38:37 +01001932 /* Tell the modem we finish : de-assert reset */
matthieu castetb72458a2005-11-07 23:27:13 +01001933 value = 0;
1934 ret = uea_send_modem_cmd(sc->usb_dev, 0xe, 1, &value);
1935 if (ret < 0)
1936 uea_err(sc->usb_dev, "elsa de-assert failed with error %d\n", ret);
1937
matthieu castetb72458a2005-11-07 23:27:13 +01001938err1:
1939 release_firmware(fw_entry);
1940err0:
1941 uea_leaves(INS_TO_USBDEV(sc));
1942 return ret;
1943}
1944
matthieu castete40abaf2006-01-18 07:38:37 +01001945/* The modem send us an ack. First with check if it right */
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001946static void uea_dispatch_cmv_e1(struct uea_softc *sc, struct intr_pkt *intr)
matthieu castetb72458a2005-11-07 23:27:13 +01001947{
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001948 struct cmv_dsc_e1 *dsc = &sc->cmv_dsc.e1;
1949 struct cmv_e1 *cmv = &intr->u.e1.s2.cmv;
1950
matthieu castetb72458a2005-11-07 23:27:13 +01001951 uea_enters(INS_TO_USBDEV(sc));
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001952 if (le16_to_cpu(cmv->wPreamble) != E1_PREAMBLE)
matthieu castetb72458a2005-11-07 23:27:13 +01001953 goto bad1;
1954
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001955 if (cmv->bDirection != E1_MODEMTOHOST)
matthieu castetb72458a2005-11-07 23:27:13 +01001956 goto bad1;
1957
1958 /* FIXME : ADI930 reply wrong preambule (func = 2, sub = 2) to
1959 * the first MEMACESS cmv. Ignore it...
1960 */
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001961 if (cmv->bFunction != dsc->function) {
matthieu castetb72458a2005-11-07 23:27:13 +01001962 if (UEA_CHIP_VERSION(sc) == ADI930
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001963 && cmv->bFunction == E1_MAKEFUNCTION(2, 2)) {
1964 cmv->wIndex = cpu_to_le16(dsc->idx);
Harvey Harrisona5abdea2008-04-29 01:03:40 -07001965 put_unaligned_le32(dsc->address, &cmv->dwSymbolicAddress);
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001966 cmv->wOffsetAddress = cpu_to_le16(dsc->offset);
1967 } else
matthieu castetb72458a2005-11-07 23:27:13 +01001968 goto bad2;
1969 }
1970
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001971 if (cmv->bFunction == E1_MAKEFUNCTION(E1_ADSLDIRECTIVE, E1_MODEMREADY)) {
matthieu castetb72458a2005-11-07 23:27:13 +01001972 wake_up_cmv_ack(sc);
matthieu castet2a99b502006-04-02 18:43:53 +02001973 uea_leaves(INS_TO_USBDEV(sc));
matthieu castetb72458a2005-11-07 23:27:13 +01001974 return;
1975 }
1976
1977 /* in case of MEMACCESS */
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001978 if (le16_to_cpu(cmv->wIndex) != dsc->idx ||
Harvey Harrisona5abdea2008-04-29 01:03:40 -07001979 get_unaligned_le32(&cmv->dwSymbolicAddress) != dsc->address ||
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001980 le16_to_cpu(cmv->wOffsetAddress) != dsc->offset)
matthieu castetb72458a2005-11-07 23:27:13 +01001981 goto bad2;
1982
Harvey Harrisona5abdea2008-04-29 01:03:40 -07001983 sc->data = get_unaligned_le32(&cmv->dwData);
matthieu castetb72458a2005-11-07 23:27:13 +01001984 sc->data = sc->data << 16 | sc->data >> 16;
1985
1986 wake_up_cmv_ack(sc);
matthieu castet2a99b502006-04-02 18:43:53 +02001987 uea_leaves(INS_TO_USBDEV(sc));
matthieu castetb72458a2005-11-07 23:27:13 +01001988 return;
1989
1990bad2:
Joe Perchesfec8de32007-11-19 17:53:33 -08001991 uea_err(INS_TO_USBDEV(sc), "unexpected cmv received, "
matthieu castetb72458a2005-11-07 23:27:13 +01001992 "Function : %d, Subfunction : %d\n",
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02001993 E1_FUNCTION_TYPE(cmv->bFunction),
1994 E1_FUNCTION_SUBTYPE(cmv->bFunction));
matthieu castet2a99b502006-04-02 18:43:53 +02001995 uea_leaves(INS_TO_USBDEV(sc));
matthieu castetb72458a2005-11-07 23:27:13 +01001996 return;
1997
1998bad1:
1999 uea_err(INS_TO_USBDEV(sc), "invalid cmv received, "
2000 "wPreamble %d, bDirection %d\n",
2001 le16_to_cpu(cmv->wPreamble), cmv->bDirection);
matthieu castet2a99b502006-04-02 18:43:53 +02002002 uea_leaves(INS_TO_USBDEV(sc));
matthieu castetb72458a2005-11-07 23:27:13 +01002003}
2004
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02002005/* The modem send us an ack. First with check if it right */
2006static void uea_dispatch_cmv_e4(struct uea_softc *sc, struct intr_pkt *intr)
2007{
2008 struct cmv_dsc_e4 *dsc = &sc->cmv_dsc.e4;
2009 struct cmv_e4 *cmv = &intr->u.e4.s2.cmv;
2010
2011 uea_enters(INS_TO_USBDEV(sc));
2012 uea_dbg(INS_TO_USBDEV(sc), "cmv %x %x %x %x %x %x\n",
2013 be16_to_cpu(cmv->wGroup), be16_to_cpu(cmv->wFunction),
2014 be16_to_cpu(cmv->wOffset), be16_to_cpu(cmv->wAddress),
2015 be32_to_cpu(cmv->dwData[0]), be32_to_cpu(cmv->dwData[1]));
2016
2017 if (be16_to_cpu(cmv->wFunction) != dsc->function)
2018 goto bad2;
2019
2020 if (be16_to_cpu(cmv->wFunction) == E4_MAKEFUNCTION(E4_ADSLDIRECTIVE, E4_MODEMREADY, 1)) {
2021 wake_up_cmv_ack(sc);
2022 uea_leaves(INS_TO_USBDEV(sc));
2023 return;
2024 }
2025
2026 /* in case of MEMACCESS */
2027 if (be16_to_cpu(cmv->wOffset) != dsc->offset ||
2028 be16_to_cpu(cmv->wGroup) != dsc->group ||
2029 be16_to_cpu(cmv->wAddress) != dsc->address)
2030 goto bad2;
2031
2032 sc->data = be32_to_cpu(cmv->dwData[0]);
2033 sc->data1 = be32_to_cpu(cmv->dwData[1]);
2034 wake_up_cmv_ack(sc);
2035 uea_leaves(INS_TO_USBDEV(sc));
2036 return;
2037
2038bad2:
Joe Perchesfec8de32007-11-19 17:53:33 -08002039 uea_err(INS_TO_USBDEV(sc), "unexpected cmv received, "
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02002040 "Function : %d, Subfunction : %d\n",
2041 E4_FUNCTION_TYPE(cmv->wFunction),
2042 E4_FUNCTION_SUBTYPE(cmv->wFunction));
2043 uea_leaves(INS_TO_USBDEV(sc));
2044 return;
2045}
2046
2047static void uea_schedule_load_page_e1(struct uea_softc *sc, struct intr_pkt *intr)
2048{
2049 sc->pageno = intr->e1_bSwapPageNo;
2050 sc->ovl = intr->e1_bOvl >> 4 | intr->e1_bOvl << 4;
Stanislaw Gruszka04ea02f2007-08-20 23:21:10 +02002051 queue_work(sc->work_q, &sc->task);
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02002052}
2053
2054static void uea_schedule_load_page_e4(struct uea_softc *sc, struct intr_pkt *intr)
2055{
2056 sc->pageno = intr->e4_bSwapPageNo;
Stanislaw Gruszka04ea02f2007-08-20 23:21:10 +02002057 queue_work(sc->work_q, &sc->task);
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02002058}
2059
matthieu castetb72458a2005-11-07 23:27:13 +01002060/*
2061 * interrupt handler
2062 */
David Howells7d12e782006-10-05 14:55:46 +01002063static void uea_intr(struct urb *urb)
matthieu castetb72458a2005-11-07 23:27:13 +01002064{
matthieu castete40abaf2006-01-18 07:38:37 +01002065 struct uea_softc *sc = urb->context;
2066 struct intr_pkt *intr = urb->transfer_buffer;
Greg Kroah-Hartman508330e2007-07-18 10:58:02 -07002067 int status = urb->status;
2068
matthieu castetb72458a2005-11-07 23:27:13 +01002069 uea_enters(INS_TO_USBDEV(sc));
2070
Greg Kroah-Hartman508330e2007-07-18 10:58:02 -07002071 if (unlikely(status < 0)) {
matthieu castetb72458a2005-11-07 23:27:13 +01002072 uea_err(INS_TO_USBDEV(sc), "uea_intr() failed with %d\n",
Greg Kroah-Hartman508330e2007-07-18 10:58:02 -07002073 status);
matthieu castetb72458a2005-11-07 23:27:13 +01002074 return;
2075 }
2076
matthieu castetb72458a2005-11-07 23:27:13 +01002077 /* device-to-host interrupt */
2078 if (intr->bType != 0x08 || sc->booting) {
matthieu castete40abaf2006-01-18 07:38:37 +01002079 uea_err(INS_TO_USBDEV(sc), "wrong interrupt\n");
matthieu castetb72458a2005-11-07 23:27:13 +01002080 goto resubmit;
2081 }
2082
2083 switch (le16_to_cpu(intr->wInterrupt)) {
2084 case INT_LOADSWAPPAGE:
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02002085 sc->schedule_load_page(sc, intr);
matthieu castetb72458a2005-11-07 23:27:13 +01002086 break;
2087
2088 case INT_INCOMINGCMV:
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02002089 sc->dispatch_cmv(sc, intr);
matthieu castetb72458a2005-11-07 23:27:13 +01002090 break;
2091
2092 default:
matthieu castete40abaf2006-01-18 07:38:37 +01002093 uea_err(INS_TO_USBDEV(sc), "unknown interrupt %u\n",
matthieu castetb72458a2005-11-07 23:27:13 +01002094 le16_to_cpu(intr->wInterrupt));
2095 }
2096
2097resubmit:
2098 usb_submit_urb(sc->urb_int, GFP_ATOMIC);
2099}
2100
2101/*
2102 * Start the modem : init the data and start kernel thread
2103 */
2104static int uea_boot(struct uea_softc *sc)
2105{
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02002106 int ret, size;
matthieu castetb72458a2005-11-07 23:27:13 +01002107 struct intr_pkt *intr;
2108
2109 uea_enters(INS_TO_USBDEV(sc));
2110
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02002111 if (UEA_CHIP_VERSION(sc) == EAGLE_IV) {
2112 size = E4_INTR_PKT_SIZE;
2113 sc->dispatch_cmv = uea_dispatch_cmv_e4;
2114 sc->schedule_load_page = uea_schedule_load_page_e4;
2115 sc->stat = uea_stat_e4;
2116 sc->send_cmvs = uea_send_cmvs_e4;
2117 INIT_WORK(&sc->task, uea_load_page_e4);
2118 } else {
2119 size = E1_INTR_PKT_SIZE;
2120 sc->dispatch_cmv = uea_dispatch_cmv_e1;
2121 sc->schedule_load_page = uea_schedule_load_page_e1;
2122 sc->stat = uea_stat_e1;
2123 sc->send_cmvs = uea_send_cmvs_e1;
2124 INIT_WORK(&sc->task, uea_load_page_e1);
2125 }
2126
matthieu castetb72458a2005-11-07 23:27:13 +01002127 init_waitqueue_head(&sc->sync_q);
matthieu castetb72458a2005-11-07 23:27:13 +01002128
Stanislaw Gruszka04ea02f2007-08-20 23:21:10 +02002129 sc->work_q = create_workqueue("ueagle-dsp");
2130 if (!sc->work_q) {
2131 uea_err(INS_TO_USBDEV(sc), "cannot allocate workqueue\n");
2132 uea_leaves(INS_TO_USBDEV(sc));
2133 return -ENOMEM;
2134 }
2135
matthieu castetb72458a2005-11-07 23:27:13 +01002136 if (UEA_CHIP_VERSION(sc) == ADI930)
2137 load_XILINX_firmware(sc);
2138
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02002139 intr = kmalloc(size, GFP_KERNEL);
matthieu castetb72458a2005-11-07 23:27:13 +01002140 if (!intr) {
2141 uea_err(INS_TO_USBDEV(sc),
2142 "cannot allocate interrupt package\n");
Stanislaw Gruszka04ea02f2007-08-20 23:21:10 +02002143 goto err0;
matthieu castetb72458a2005-11-07 23:27:13 +01002144 }
2145
2146 sc->urb_int = usb_alloc_urb(0, GFP_KERNEL);
2147 if (!sc->urb_int) {
2148 uea_err(INS_TO_USBDEV(sc), "cannot allocate interrupt URB\n");
Stanislaw Gruszka04ea02f2007-08-20 23:21:10 +02002149 goto err1;
matthieu castetb72458a2005-11-07 23:27:13 +01002150 }
2151
2152 usb_fill_int_urb(sc->urb_int, sc->usb_dev,
2153 usb_rcvintpipe(sc->usb_dev, UEA_INTR_PIPE),
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02002154 intr, size, uea_intr, sc,
matthieu castetb72458a2005-11-07 23:27:13 +01002155 sc->usb_dev->actconfig->interface[0]->altsetting[0].
2156 endpoint[0].desc.bInterval);
2157
2158 ret = usb_submit_urb(sc->urb_int, GFP_KERNEL);
2159 if (ret < 0) {
2160 uea_err(INS_TO_USBDEV(sc),
2161 "urb submition failed with error %d\n", ret);
Stanislaw Gruszka04ea02f2007-08-20 23:21:10 +02002162 goto err1;
matthieu castetb72458a2005-11-07 23:27:13 +01002163 }
2164
2165 sc->kthread = kthread_run(uea_kthread, sc, "ueagle-atm");
2166 if (sc->kthread == ERR_PTR(-ENOMEM)) {
2167 uea_err(INS_TO_USBDEV(sc), "failed to create thread\n");
2168 goto err2;
2169 }
2170
2171 uea_leaves(INS_TO_USBDEV(sc));
2172 return 0;
2173
2174err2:
2175 usb_kill_urb(sc->urb_int);
Stanislaw Gruszka04ea02f2007-08-20 23:21:10 +02002176err1:
matthieu castetb72458a2005-11-07 23:27:13 +01002177 usb_free_urb(sc->urb_int);
matthieu castet4d45e212006-04-02 18:45:46 +02002178 sc->urb_int = NULL;
2179 kfree(intr);
Stanislaw Gruszka04ea02f2007-08-20 23:21:10 +02002180err0:
2181 destroy_workqueue(sc->work_q);
matthieu castetb72458a2005-11-07 23:27:13 +01002182 uea_leaves(INS_TO_USBDEV(sc));
2183 return -ENOMEM;
2184}
2185
2186/*
2187 * Stop the modem : kill kernel thread and free data
2188 */
2189static void uea_stop(struct uea_softc *sc)
2190{
2191 int ret;
2192 uea_enters(INS_TO_USBDEV(sc));
2193 ret = kthread_stop(sc->kthread);
matthieu castete40abaf2006-01-18 07:38:37 +01002194 uea_dbg(INS_TO_USBDEV(sc), "kthread finish with status %d\n", ret);
matthieu castetb72458a2005-11-07 23:27:13 +01002195
matthieu castetb72458a2005-11-07 23:27:13 +01002196 uea_request(sc, UEA_SET_MODE, UEA_LOOPBACK_ON, 0, NULL);
2197
2198 usb_kill_urb(sc->urb_int);
2199 kfree(sc->urb_int->transfer_buffer);
2200 usb_free_urb(sc->urb_int);
2201
Stanislaw Gruszka04ea02f2007-08-20 23:21:10 +02002202 /* stop any pending boot process, when no one can schedule work */
2203 destroy_workqueue(sc->work_q);
2204
matthieu castetb72458a2005-11-07 23:27:13 +01002205 if (sc->dsp_firm)
2206 release_firmware(sc->dsp_firm);
2207 uea_leaves(INS_TO_USBDEV(sc));
2208}
2209
2210/* syfs interface */
2211static struct uea_softc *dev_to_uea(struct device *dev)
2212{
2213 struct usb_interface *intf;
2214 struct usbatm_data *usbatm;
2215
2216 intf = to_usb_interface(dev);
2217 if (!intf)
2218 return NULL;
2219
2220 usbatm = usb_get_intfdata(intf);
2221 if (!usbatm)
2222 return NULL;
2223
2224 return usbatm->driver_data;
2225}
2226
2227static ssize_t read_status(struct device *dev, struct device_attribute *attr,
2228 char *buf)
2229{
2230 int ret = -ENODEV;
2231 struct uea_softc *sc;
2232
Arjan van de Venab3c81f2006-01-13 15:52:55 +01002233 mutex_lock(&uea_mutex);
matthieu castetb72458a2005-11-07 23:27:13 +01002234 sc = dev_to_uea(dev);
2235 if (!sc)
2236 goto out;
2237 ret = snprintf(buf, 10, "%08x\n", sc->stats.phy.state);
2238out:
Arjan van de Venab3c81f2006-01-13 15:52:55 +01002239 mutex_unlock(&uea_mutex);
matthieu castetb72458a2005-11-07 23:27:13 +01002240 return ret;
2241}
2242
2243static ssize_t reboot(struct device *dev, struct device_attribute *attr,
2244 const char *buf, size_t count)
2245{
2246 int ret = -ENODEV;
2247 struct uea_softc *sc;
2248
Arjan van de Venab3c81f2006-01-13 15:52:55 +01002249 mutex_lock(&uea_mutex);
matthieu castetb72458a2005-11-07 23:27:13 +01002250 sc = dev_to_uea(dev);
2251 if (!sc)
2252 goto out;
2253 sc->reset = 1;
2254 ret = count;
2255out:
Arjan van de Venab3c81f2006-01-13 15:52:55 +01002256 mutex_unlock(&uea_mutex);
matthieu castetb72458a2005-11-07 23:27:13 +01002257 return ret;
2258}
2259
2260static DEVICE_ATTR(stat_status, S_IWUGO | S_IRUGO, read_status, reboot);
2261
2262static ssize_t read_human_status(struct device *dev, struct device_attribute *attr,
2263 char *buf)
2264{
2265 int ret = -ENODEV;
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02002266 int modem_state;
matthieu castetb72458a2005-11-07 23:27:13 +01002267 struct uea_softc *sc;
2268
Arjan van de Venab3c81f2006-01-13 15:52:55 +01002269 mutex_lock(&uea_mutex);
matthieu castetb72458a2005-11-07 23:27:13 +01002270 sc = dev_to_uea(dev);
2271 if (!sc)
2272 goto out;
2273
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02002274 if (UEA_CHIP_VERSION(sc) == EAGLE_IV) {
2275 switch (sc->stats.phy.state) {
2276 case 0x0: /* not yet synchronized */
2277 case 0x1:
2278 case 0x3:
2279 case 0x4:
2280 modem_state = 0;
2281 break;
2282 case 0x5: /* initialization */
2283 case 0x6:
2284 case 0x9:
2285 case 0xa:
2286 modem_state = 1;
2287 break;
2288 case 0x7: /* operational */
2289 modem_state = 2;
2290 break;
2291 case 0x2: /* fail ... */
2292 modem_state = 3;
2293 break;
2294 default: /* unknown */
2295 modem_state = 4;
2296 break;
2297 }
2298 } else
2299 modem_state = GET_STATUS(sc->stats.phy.state);
2300
2301 switch (modem_state) {
matthieu castetb72458a2005-11-07 23:27:13 +01002302 case 0:
2303 ret = sprintf(buf, "Modem is booting\n");
2304 break;
2305 case 1:
2306 ret = sprintf(buf, "Modem is initializing\n");
2307 break;
2308 case 2:
2309 ret = sprintf(buf, "Modem is operational\n");
2310 break;
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02002311 case 3:
matthieu castetb72458a2005-11-07 23:27:13 +01002312 ret = sprintf(buf, "Modem synchronization failed\n");
2313 break;
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02002314 default:
2315 ret = sprintf(buf, "Modem state is unknown\n");
2316 break;
matthieu castetb72458a2005-11-07 23:27:13 +01002317 }
2318out:
Arjan van de Venab3c81f2006-01-13 15:52:55 +01002319 mutex_unlock(&uea_mutex);
matthieu castetb72458a2005-11-07 23:27:13 +01002320 return ret;
2321}
2322
2323static DEVICE_ATTR(stat_human_status, S_IWUGO | S_IRUGO, read_human_status, NULL);
2324
2325static ssize_t read_delin(struct device *dev, struct device_attribute *attr,
2326 char *buf)
2327{
2328 int ret = -ENODEV;
2329 struct uea_softc *sc;
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02002330 char *delin = "GOOD";
matthieu castetb72458a2005-11-07 23:27:13 +01002331
Arjan van de Venab3c81f2006-01-13 15:52:55 +01002332 mutex_lock(&uea_mutex);
matthieu castetb72458a2005-11-07 23:27:13 +01002333 sc = dev_to_uea(dev);
2334 if (!sc)
2335 goto out;
2336
Stanislaw Gruszkac8e46372007-08-20 23:23:12 +02002337 if (UEA_CHIP_VERSION(sc) == EAGLE_IV) {
2338 if (sc->stats.phy.flags & 0x4000)
2339 delin = "RESET";
2340 else if (sc->stats.phy.flags & 0x0001)
2341 delin = "LOSS";
2342 } else {
2343 if (sc->stats.phy.flags & 0x0C00)
2344 delin = "ERROR";
2345 else if (sc->stats.phy.flags & 0x0030)
2346 delin = "LOSS";
2347 }
2348
2349 ret = sprintf(buf, "%s\n", delin);
matthieu castetb72458a2005-11-07 23:27:13 +01002350out:
Arjan van de Venab3c81f2006-01-13 15:52:55 +01002351 mutex_unlock(&uea_mutex);
matthieu castetb72458a2005-11-07 23:27:13 +01002352 return ret;
2353}
2354
2355static DEVICE_ATTR(stat_delin, S_IWUGO | S_IRUGO, read_delin, NULL);
2356
2357#define UEA_ATTR(name, reset) \
2358 \
2359static ssize_t read_##name(struct device *dev, \
2360 struct device_attribute *attr, char *buf) \
2361{ \
2362 int ret = -ENODEV; \
2363 struct uea_softc *sc; \
2364 \
matthieu castet2a99b502006-04-02 18:43:53 +02002365 mutex_lock(&uea_mutex); \
matthieu castetb72458a2005-11-07 23:27:13 +01002366 sc = dev_to_uea(dev); \
2367 if (!sc) \
2368 goto out; \
2369 ret = snprintf(buf, 10, "%08x\n", sc->stats.phy.name); \
2370 if (reset) \
2371 sc->stats.phy.name = 0; \
2372out: \
matthieu castet2a99b502006-04-02 18:43:53 +02002373 mutex_unlock(&uea_mutex); \
matthieu castetb72458a2005-11-07 23:27:13 +01002374 return ret; \
2375} \
2376 \
2377static DEVICE_ATTR(stat_##name, S_IRUGO, read_##name, NULL)
2378
2379UEA_ATTR(mflags, 1);
2380UEA_ATTR(vidcpe, 0);
2381UEA_ATTR(usrate, 0);
2382UEA_ATTR(dsrate, 0);
2383UEA_ATTR(usattenuation, 0);
2384UEA_ATTR(dsattenuation, 0);
2385UEA_ATTR(usmargin, 0);
2386UEA_ATTR(dsmargin, 0);
2387UEA_ATTR(txflow, 0);
2388UEA_ATTR(rxflow, 0);
2389UEA_ATTR(uscorr, 0);
2390UEA_ATTR(dscorr, 0);
2391UEA_ATTR(usunc, 0);
2392UEA_ATTR(dsunc, 0);
matthieu casteta7a0c9c2006-10-03 21:44:11 +02002393UEA_ATTR(firmid, 0);
matthieu castetb72458a2005-11-07 23:27:13 +01002394
2395/* Retrieve the device End System Identifier (MAC) */
2396
2397#define htoi(x) (isdigit(x) ? x-'0' : toupper(x)-'A'+10)
2398static int uea_getesi(struct uea_softc *sc, u_char * esi)
2399{
2400 unsigned char mac_str[2 * ETH_ALEN + 1];
2401 int i;
2402 if (usb_string
2403 (sc->usb_dev, sc->usb_dev->descriptor.iSerialNumber, mac_str,
2404 sizeof(mac_str)) != 2 * ETH_ALEN)
2405 return 1;
2406
2407 for (i = 0; i < ETH_ALEN; i++)
2408 esi[i] = htoi(mac_str[2 * i]) * 16 + htoi(mac_str[2 * i + 1]);
2409
2410 return 0;
2411}
2412
2413/* ATM stuff */
2414static int uea_atm_open(struct usbatm_data *usbatm, struct atm_dev *atm_dev)
2415{
2416 struct uea_softc *sc = usbatm->driver_data;
2417
2418 return uea_getesi(sc, atm_dev->esi);
2419}
2420
2421static int uea_heavy(struct usbatm_data *usbatm, struct usb_interface *intf)
2422{
2423 struct uea_softc *sc = usbatm->driver_data;
2424
matthieu castet531a39b2006-10-03 21:49:29 +02002425 wait_event_interruptible(sc->sync_q, IS_OPERATIONAL(sc));
matthieu castetb72458a2005-11-07 23:27:13 +01002426
2427 return 0;
2428
2429}
2430
2431static int claim_interface(struct usb_device *usb_dev,
2432 struct usbatm_data *usbatm, int ifnum)
2433{
2434 int ret;
2435 struct usb_interface *intf = usb_ifnum_to_if(usb_dev, ifnum);
2436
2437 if (!intf) {
2438 uea_err(usb_dev, "interface %d not found\n", ifnum);
2439 return -ENODEV;
2440 }
2441
2442 ret = usb_driver_claim_interface(&uea_driver, intf, usbatm);
2443 if (ret != 0)
2444 uea_err(usb_dev, "can't claim interface %d, error %d\n", ifnum,
2445 ret);
2446 return ret;
2447}
2448
Greg Kroah-Hartmane7ccdfe2006-08-28 11:43:25 -07002449static struct attribute *attrs[] = {
2450 &dev_attr_stat_status.attr,
2451 &dev_attr_stat_mflags.attr,
2452 &dev_attr_stat_human_status.attr,
2453 &dev_attr_stat_delin.attr,
2454 &dev_attr_stat_vidcpe.attr,
2455 &dev_attr_stat_usrate.attr,
2456 &dev_attr_stat_dsrate.attr,
2457 &dev_attr_stat_usattenuation.attr,
2458 &dev_attr_stat_dsattenuation.attr,
2459 &dev_attr_stat_usmargin.attr,
2460 &dev_attr_stat_dsmargin.attr,
2461 &dev_attr_stat_txflow.attr,
2462 &dev_attr_stat_rxflow.attr,
2463 &dev_attr_stat_uscorr.attr,
2464 &dev_attr_stat_dscorr.attr,
2465 &dev_attr_stat_usunc.attr,
2466 &dev_attr_stat_dsunc.attr,
matthieu casteta7a0c9c2006-10-03 21:44:11 +02002467 &dev_attr_stat_firmid.attr,
matthieu castet9ab99c82006-10-11 14:20:56 -07002468 NULL,
Greg Kroah-Hartmane7ccdfe2006-08-28 11:43:25 -07002469};
2470static struct attribute_group attr_grp = {
2471 .attrs = attrs,
2472};
2473
matthieu castetb72458a2005-11-07 23:27:13 +01002474static int uea_bind(struct usbatm_data *usbatm, struct usb_interface *intf,
Duncan Sands35644b02006-01-17 11:16:13 +01002475 const struct usb_device_id *id)
matthieu castetb72458a2005-11-07 23:27:13 +01002476{
2477 struct usb_device *usb = interface_to_usbdev(intf);
2478 struct uea_softc *sc;
2479 int ret, ifnum = intf->altsetting->desc.bInterfaceNumber;
Stanislaw Gruszka503add42007-08-20 23:21:06 +02002480 unsigned int alt;
matthieu castetb72458a2005-11-07 23:27:13 +01002481
2482 uea_enters(usb);
2483
2484 /* interface 0 is for firmware/monitoring */
2485 if (ifnum != UEA_INTR_IFACE_NO)
2486 return -ENODEV;
2487
Duncan Sands0dfcd3e2006-01-13 09:36:20 +01002488 usbatm->flags = (sync_wait[modem_index] ? 0 : UDSL_SKIP_HEAVY_INIT);
matthieu castetb72458a2005-11-07 23:27:13 +01002489
2490 /* interface 1 is for outbound traffic */
2491 ret = claim_interface(usb, usbatm, UEA_US_IFACE_NO);
2492 if (ret < 0)
2493 return ret;
2494
matthieu castete40abaf2006-01-18 07:38:37 +01002495 /* ADI930 has only 2 interfaces and inbound traffic is on interface 1 */
matthieu castetb72458a2005-11-07 23:27:13 +01002496 if (UEA_CHIP_VERSION(id) != ADI930) {
2497 /* interface 2 is for inbound traffic */
2498 ret = claim_interface(usb, usbatm, UEA_DS_IFACE_NO);
2499 if (ret < 0)
2500 return ret;
2501 }
2502
2503 sc = kzalloc(sizeof(struct uea_softc), GFP_KERNEL);
2504 if (!sc) {
matthieu castet584958c2006-04-02 18:44:48 +02002505 uea_err(usb, "uea_init: not enough memory !\n");
matthieu castetb72458a2005-11-07 23:27:13 +01002506 return -ENOMEM;
2507 }
2508
2509 sc->usb_dev = usb;
2510 usbatm->driver_data = sc;
2511 sc->usbatm = usbatm;
2512 sc->modem_index = (modem_index < NB_MODEM) ? modem_index++ : 0;
2513 sc->driver_info = id->driver_info;
2514
Stanislaw Gruszka603cf602007-08-20 23:21:01 +02002515 /* first try to use module parameter */
2516 if (annex[sc->modem_index] == 1)
2517 sc->annex = ANNEXA;
2518 else if (annex[sc->modem_index] == 2)
2519 sc->annex = ANNEXB;
2520 /* try to autodetect annex */
2521 else if (sc->driver_info & AUTO_ANNEX_A)
2522 sc->annex = ANNEXA;
2523 else if (sc->driver_info & AUTO_ANNEX_B)
2524 sc->annex = ANNEXB;
2525 else
2526 sc->annex = (le16_to_cpu(sc->usb_dev->descriptor.bcdDevice) & 0x80)?ANNEXB:ANNEXA;
2527
Stanislaw Gruszka503add42007-08-20 23:21:06 +02002528 alt = altsetting[sc->modem_index];
matthieu castet3c9666c2006-01-18 07:38:19 +01002529 /* ADI930 don't support iso */
Stanislaw Gruszka503add42007-08-20 23:21:06 +02002530 if (UEA_CHIP_VERSION(id) != ADI930 && alt > 0) {
2531 if (alt <= 8 && usb_set_interface(usb, UEA_DS_IFACE_NO, alt) == 0) {
2532 uea_dbg(usb, "set alternate %u for 2 interface\n", alt);
matthieu castet3c9666c2006-01-18 07:38:19 +01002533 uea_info(usb, "using iso mode\n");
2534 usbatm->flags |= UDSL_USE_ISOC | UDSL_IGNORE_EILSEQ;
2535 } else {
Stanislaw Gruszka503add42007-08-20 23:21:06 +02002536 uea_err(usb, "setting alternate %u failed for "
2537 "2 interface, using bulk mode\n", alt);
matthieu castet3c9666c2006-01-18 07:38:19 +01002538 }
2539 }
2540
matthieu castet9ab99c82006-10-11 14:20:56 -07002541 ret = sysfs_create_group(&intf->dev.kobj, &attr_grp);
2542 if (ret < 0)
2543 goto error;
2544
matthieu castetb72458a2005-11-07 23:27:13 +01002545 ret = uea_boot(sc);
matthieu castet9ab99c82006-10-11 14:20:56 -07002546 if (ret < 0)
Stanislaw Gruszka4c132e72007-08-20 23:20:49 +02002547 goto error_rm_grp;
matthieu castetb72458a2005-11-07 23:27:13 +01002548
matthieu castetb72458a2005-11-07 23:27:13 +01002549 return 0;
Stanislaw Gruszka4c132e72007-08-20 23:20:49 +02002550
2551error_rm_grp:
2552 sysfs_remove_group(&intf->dev.kobj, &attr_grp);
matthieu castet9ab99c82006-10-11 14:20:56 -07002553error:
2554 kfree(sc);
2555 return ret;
matthieu castetb72458a2005-11-07 23:27:13 +01002556}
2557
2558static void uea_unbind(struct usbatm_data *usbatm, struct usb_interface *intf)
2559{
2560 struct uea_softc *sc = usbatm->driver_data;
2561
matthieu castet9ab99c82006-10-11 14:20:56 -07002562 sysfs_remove_group(&intf->dev.kobj, &attr_grp);
matthieu castetb72458a2005-11-07 23:27:13 +01002563 uea_stop(sc);
2564 kfree(sc);
2565}
2566
2567static struct usbatm_driver uea_usbatm_driver = {
2568 .driver_name = "ueagle-atm",
matthieu castetb72458a2005-11-07 23:27:13 +01002569 .bind = uea_bind,
2570 .atm_start = uea_atm_open,
2571 .unbind = uea_unbind,
2572 .heavy_init = uea_heavy,
Duncan Sands80aae7a2006-01-13 10:59:23 +01002573 .bulk_in = UEA_BULK_DATA_PIPE,
2574 .bulk_out = UEA_BULK_DATA_PIPE,
matthieu castet3c9666c2006-01-18 07:38:19 +01002575 .isoc_in = UEA_ISO_DATA_PIPE,
matthieu castetb72458a2005-11-07 23:27:13 +01002576};
2577
2578static int uea_probe(struct usb_interface *intf, const struct usb_device_id *id)
2579{
2580 struct usb_device *usb = interface_to_usbdev(intf);
2581
2582 uea_enters(usb);
Stanislaw Gruszka603cf602007-08-20 23:21:01 +02002583 uea_info(usb, "ADSL device founded vid (%#X) pid (%#X) Rev (%#X): %s\n",
2584 le16_to_cpu(usb->descriptor.idVendor),
2585 le16_to_cpu(usb->descriptor.idProduct),
2586 le16_to_cpu(usb->descriptor.bcdDevice),
2587 chip_name[UEA_CHIP_VERSION(id)]);
matthieu castetb72458a2005-11-07 23:27:13 +01002588
2589 usb_reset_device(usb);
2590
2591 if (UEA_IS_PREFIRM(id))
2592 return uea_load_firmware(usb, UEA_CHIP_VERSION(id));
2593
2594 return usbatm_usb_probe(intf, id, &uea_usbatm_driver);
2595}
2596
2597static void uea_disconnect(struct usb_interface *intf)
2598{
2599 struct usb_device *usb = interface_to_usbdev(intf);
2600 int ifnum = intf->altsetting->desc.bInterfaceNumber;
2601 uea_enters(usb);
2602
2603 /* ADI930 has 2 interfaces and eagle 3 interfaces.
2604 * Pre-firmware device has one interface
2605 */
2606 if (usb->config->desc.bNumInterfaces != 1 && ifnum == 0) {
Arjan van de Venab3c81f2006-01-13 15:52:55 +01002607 mutex_lock(&uea_mutex);
matthieu castetb72458a2005-11-07 23:27:13 +01002608 usbatm_usb_disconnect(intf);
Arjan van de Venab3c81f2006-01-13 15:52:55 +01002609 mutex_unlock(&uea_mutex);
matthieu castetb72458a2005-11-07 23:27:13 +01002610 uea_info(usb, "ADSL device removed\n");
2611 }
2612
2613 uea_leaves(usb);
2614}
2615
2616/*
2617 * List of supported VID/PID
2618 */
2619static const struct usb_device_id uea_ids[] = {
Stanislaw Gruszka603cf602007-08-20 23:21:01 +02002620 {USB_DEVICE(ANALOG_VID, ADI930_PID_PREFIRM), .driver_info = ADI930 | PREFIRM},
2621 {USB_DEVICE(ANALOG_VID, ADI930_PID_PSTFIRM), .driver_info = ADI930 | PSTFIRM},
2622 {USB_DEVICE(ANALOG_VID, EAGLE_I_PID_PREFIRM), .driver_info = EAGLE_I | PREFIRM},
2623 {USB_DEVICE(ANALOG_VID, EAGLE_I_PID_PSTFIRM), .driver_info = EAGLE_I | PSTFIRM},
2624 {USB_DEVICE(ANALOG_VID, EAGLE_II_PID_PREFIRM), .driver_info = EAGLE_II | PREFIRM},
2625 {USB_DEVICE(ANALOG_VID, EAGLE_II_PID_PSTFIRM), .driver_info = EAGLE_II | PSTFIRM},
2626 {USB_DEVICE(ANALOG_VID, EAGLE_IIC_PID_PREFIRM), .driver_info = EAGLE_II | PREFIRM},
2627 {USB_DEVICE(ANALOG_VID, EAGLE_IIC_PID_PSTFIRM), .driver_info = EAGLE_II | PSTFIRM},
2628 {USB_DEVICE(ANALOG_VID, EAGLE_III_PID_PREFIRM), .driver_info = EAGLE_III | PREFIRM},
2629 {USB_DEVICE(ANALOG_VID, EAGLE_III_PID_PSTFIRM), .driver_info = EAGLE_III | PSTFIRM},
2630 {USB_DEVICE(ANALOG_VID, EAGLE_IV_PID_PREFIRM), .driver_info = EAGLE_IV | PREFIRM},
2631 {USB_DEVICE(ANALOG_VID, EAGLE_IV_PID_PSTFIRM), .driver_info = EAGLE_IV | PSTFIRM},
2632 {USB_DEVICE(DEVOLO_VID, DEVOLO_EAGLE_I_A_PID_PREFIRM), .driver_info = EAGLE_I | PREFIRM},
2633 {USB_DEVICE(DEVOLO_VID, DEVOLO_EAGLE_I_A_PID_PSTFIRM), .driver_info = EAGLE_I | PSTFIRM | AUTO_ANNEX_A},
2634 {USB_DEVICE(DEVOLO_VID, DEVOLO_EAGLE_I_B_PID_PREFIRM), .driver_info = EAGLE_I | PREFIRM},
2635 {USB_DEVICE(DEVOLO_VID, DEVOLO_EAGLE_I_B_PID_PSTFIRM), .driver_info = EAGLE_I | PSTFIRM | AUTO_ANNEX_B},
2636 {USB_DEVICE(DEVOLO_VID, DEVOLO_EAGLE_II_A_PID_PREFIRM), .driver_info = EAGLE_II | PREFIRM},
2637 {USB_DEVICE(DEVOLO_VID, DEVOLO_EAGLE_II_A_PID_PSTFIRM), .driver_info = EAGLE_II | PSTFIRM | AUTO_ANNEX_A},
2638 {USB_DEVICE(DEVOLO_VID, DEVOLO_EAGLE_II_B_PID_PREFIRM), .driver_info = EAGLE_II | PREFIRM},
2639 {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 +01002640 {USB_DEVICE(ELSA_VID, ELSA_PID_PREFIRM), .driver_info = ADI930 | PREFIRM},
2641 {USB_DEVICE(ELSA_VID, ELSA_PID_PSTFIRM), .driver_info = ADI930 | PSTFIRM},
Stanislaw Gruszka603cf602007-08-20 23:21:01 +02002642 {USB_DEVICE(ELSA_VID, ELSA_PID_A_PREFIRM), .driver_info = ADI930 | PREFIRM},
2643 {USB_DEVICE(ELSA_VID, ELSA_PID_A_PSTFIRM), .driver_info = ADI930 | PSTFIRM | AUTO_ANNEX_A},
2644 {USB_DEVICE(ELSA_VID, ELSA_PID_B_PREFIRM), .driver_info = ADI930 | PREFIRM},
2645 {USB_DEVICE(ELSA_VID, ELSA_PID_B_PSTFIRM), .driver_info = ADI930 | PSTFIRM | AUTO_ANNEX_B},
matthieu castetb72458a2005-11-07 23:27:13 +01002646 {USB_DEVICE(USR_VID, MILLER_A_PID_PREFIRM), .driver_info = EAGLE_I | PREFIRM},
Stanislaw Gruszka603cf602007-08-20 23:21:01 +02002647 {USB_DEVICE(USR_VID, MILLER_A_PID_PSTFIRM), .driver_info = EAGLE_I | PSTFIRM | AUTO_ANNEX_A},
matthieu castetb72458a2005-11-07 23:27:13 +01002648 {USB_DEVICE(USR_VID, MILLER_B_PID_PREFIRM), .driver_info = EAGLE_I | PREFIRM},
Stanislaw Gruszka603cf602007-08-20 23:21:01 +02002649 {USB_DEVICE(USR_VID, MILLER_B_PID_PSTFIRM), .driver_info = EAGLE_I | PSTFIRM | AUTO_ANNEX_B},
matthieu castetb72458a2005-11-07 23:27:13 +01002650 {USB_DEVICE(USR_VID, HEINEKEN_A_PID_PREFIRM),.driver_info = EAGLE_I | PREFIRM},
Stanislaw Gruszka603cf602007-08-20 23:21:01 +02002651 {USB_DEVICE(USR_VID, HEINEKEN_A_PID_PSTFIRM),.driver_info = EAGLE_I | PSTFIRM | AUTO_ANNEX_A},
matthieu castetb72458a2005-11-07 23:27:13 +01002652 {USB_DEVICE(USR_VID, HEINEKEN_B_PID_PREFIRM),.driver_info = EAGLE_I | PREFIRM},
Stanislaw Gruszka603cf602007-08-20 23:21:01 +02002653 {USB_DEVICE(USR_VID, HEINEKEN_B_PID_PSTFIRM),.driver_info = EAGLE_I | PSTFIRM | AUTO_ANNEX_B},
matthieu castetb72458a2005-11-07 23:27:13 +01002654 {}
2655};
2656
2657/*
2658 * USB driver descriptor
2659 */
2660static struct usb_driver uea_driver = {
matthieu castetb72458a2005-11-07 23:27:13 +01002661 .name = "ueagle-atm",
2662 .id_table = uea_ids,
2663 .probe = uea_probe,
2664 .disconnect = uea_disconnect,
2665};
2666
2667MODULE_DEVICE_TABLE(usb, uea_ids);
2668
2669/**
2670 * uea_init - Initialize the module.
2671 * Register to USB subsystem
2672 */
2673static int __init uea_init(void)
2674{
2675 printk(KERN_INFO "[ueagle-atm] driver " EAGLEUSBVERSION " loaded\n");
2676
2677 usb_register(&uea_driver);
2678
2679 return 0;
2680}
2681
2682module_init(uea_init);
2683
2684/**
2685 * uea_exit - Destroy module
2686 * Deregister with USB subsystem
2687 */
2688static void __exit uea_exit(void)
2689{
2690 /*
2691 * This calls automatically the uea_disconnect method if necessary:
2692 */
2693 usb_deregister(&uea_driver);
2694
2695 printk(KERN_INFO "[ueagle-atm] driver unloaded\n");
2696}
2697
2698module_exit(uea_exit);
2699
2700MODULE_AUTHOR("Damien Bergamini/Matthieu Castet/Stanislaw W. Gruszka");
2701MODULE_DESCRIPTION("ADI 930/Eagle USB ADSL Modem driver");
2702MODULE_LICENSE("Dual BSD/GPL");