trivial: Fix misspelling of firmware
[deliverable/linux.git] / drivers / usb / atm / ueagle-atm.c
CommitLineData
b72458a8 1/*-
2 * Copyright (c) 2003, 2004
3 * Damien Bergamini <damien.bergamini@free.fr>. All rights reserved.
4 *
0eb0226c
SG
5 * Copyright (c) 2005-2007 Matthieu Castet <castet.matthieu@free.fr>
6 * Copyright (c) 2005-2007 Stanislaw Gruszka <stf_xl@wp.pl>
b72458a8 7 *
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>
5371f80a 65#include <linux/sched.h>
b72458a8 66#include <linux/kthread.h>
ab3c81ff 67#include <linux/mutex.h>
7dfb7103
NC
68#include <linux/freezer.h>
69
b72458a8 70#include <asm/unaligned.h>
71
72#include "usbatm.h"
73
a7a0c9cd 74#define EAGLEUSBVERSION "ueagle 1.4"
b72458a8 75
76
77/*
78 * Debug macros
79 */
80#define uea_dbg(usb_dev, format, args...) \
81 do { \
82 if (debug >= 1) \
83 dev_dbg(&(usb_dev)->dev, \
84 "[ueagle-atm dbg] %s: " format, \
441b62c1 85 __func__, ##args); \
a7a0c9cd 86 } while (0)
b72458a8 87
88#define uea_vdbg(usb_dev, format, args...) \
89 do { \
90 if (debug >= 2) \
91 dev_dbg(&(usb_dev)->dev, \
92 "[ueagle-atm vdbg] " format, ##args); \
a7a0c9cd 93 } while (0)
b72458a8 94
95#define uea_enters(usb_dev) \
441b62c1 96 uea_vdbg(usb_dev, "entering %s\n", __func__)
b72458a8 97
98#define uea_leaves(usb_dev) \
441b62c1 99 uea_vdbg(usb_dev, "leaving %s\n", __func__)
b72458a8 100
101#define uea_err(usb_dev, format,args...) \
102 dev_err(&(usb_dev)->dev ,"[UEAGLE-ATM] " format , ##args)
103
104#define uea_warn(usb_dev, format,args...) \
105 dev_warn(&(usb_dev)->dev ,"[Ueagle-atm] " format, ##args)
106
107#define uea_info(usb_dev, format,args...) \
108 dev_info(&(usb_dev)->dev ,"[ueagle-atm] " format, ##args)
109
c8e46379
SG
110struct intr_pkt;
111
112/* cmv's from firmware */
113struct uea_cmvs_v1 {
b72458a8 114 u32 address;
115 u16 offset;
116 u32 data;
117} __attribute__ ((packed));
118
c8e46379
SG
119struct uea_cmvs_v2 {
120 u32 group;
121 u32 address;
122 u32 offset;
123 u32 data;
124} __attribute__ ((packed));
125
126/* information about currently processed cmv */
127struct cmv_dsc_e1 {
128 u8 function;
129 u16 idx;
130 u32 address;
131 u16 offset;
132};
133
134struct cmv_dsc_e4 {
135 u16 function;
136 u16 offset;
137 u16 address;
138 u16 group;
139};
140
141union cmv_dsc {
142 struct cmv_dsc_e1 e1;
143 struct cmv_dsc_e4 e4;
144};
145
b72458a8 146struct uea_softc {
147 struct usb_device *usb_dev;
148 struct usbatm_data *usbatm;
149
150 int modem_index;
151 unsigned int driver_info;
603cf608
SG
152 int annex;
153#define ANNEXA 0
154#define ANNEXB 1
b72458a8 155
156 int booting;
157 int reset;
158
159 wait_queue_head_t sync_q;
160
161 struct task_struct *kthread;
162 u32 data;
c8e46379 163 u32 data1;
c8e46379 164
b72458a8 165 int cmv_ack;
c8e46379 166 union cmv_dsc cmv_dsc;
b72458a8 167
168 struct work_struct task;
04ea02f5 169 struct workqueue_struct *work_q;
b72458a8 170 u16 pageno;
171 u16 ovl;
172
173 const struct firmware *dsp_firm;
174 struct urb *urb_int;
175
c8e46379
SG
176 void (*dispatch_cmv) (struct uea_softc *, struct intr_pkt *);
177 void (*schedule_load_page) (struct uea_softc *, struct intr_pkt *);
178 int (*stat) (struct uea_softc *);
179 int (*send_cmvs) (struct uea_softc *);
b72458a8 180
181 /* keep in sync with eaglectl */
182 struct uea_stats {
183 struct {
184 u32 state;
185 u32 flags;
186 u32 mflags;
187 u32 vidcpe;
188 u32 vidco;
189 u32 dsrate;
190 u32 usrate;
191 u32 dsunc;
192 u32 usunc;
193 u32 dscorr;
194 u32 uscorr;
195 u32 txflow;
196 u32 rxflow;
197 u32 usattenuation;
198 u32 dsattenuation;
199 u32 dsmargin;
200 u32 usmargin;
201 u32 firmid;
202 } phy;
203 } stats;
204};
205
206/*
207 * Elsa IDs
208 */
209#define ELSA_VID 0x05CC
210#define ELSA_PID_PSTFIRM 0x3350
211#define ELSA_PID_PREFIRM 0x3351
212
603cf608
SG
213#define ELSA_PID_A_PREFIRM 0x3352
214#define ELSA_PID_A_PSTFIRM 0x3353
215#define ELSA_PID_B_PREFIRM 0x3362
216#define ELSA_PID_B_PSTFIRM 0x3363
217
b72458a8 218/*
603cf608 219 * Devolo IDs : pots if (pid & 0x10)
b72458a8 220 */
603cf608
SG
221#define DEVOLO_VID 0x1039
222#define DEVOLO_EAGLE_I_A_PID_PSTFIRM 0x2110
223#define DEVOLO_EAGLE_I_A_PID_PREFIRM 0x2111
224
225#define DEVOLO_EAGLE_I_B_PID_PSTFIRM 0x2100
226#define DEVOLO_EAGLE_I_B_PID_PREFIRM 0x2101
227
228#define DEVOLO_EAGLE_II_A_PID_PSTFIRM 0x2130
229#define DEVOLO_EAGLE_II_A_PID_PREFIRM 0x2131
230
231#define DEVOLO_EAGLE_II_B_PID_PSTFIRM 0x2120
232#define DEVOLO_EAGLE_II_B_PID_PREFIRM 0x2121
233
234/*
235 * Reference design USB IDs
236 */
237#define ANALOG_VID 0x1110
238#define ADI930_PID_PREFIRM 0x9001
239#define ADI930_PID_PSTFIRM 0x9000
240
b72458a8 241#define EAGLE_I_PID_PREFIRM 0x9010 /* Eagle I */
242#define EAGLE_I_PID_PSTFIRM 0x900F /* Eagle I */
243
244#define EAGLE_IIC_PID_PREFIRM 0x9024 /* Eagle IIC */
245#define EAGLE_IIC_PID_PSTFIRM 0x9023 /* Eagle IIC */
246
247#define EAGLE_II_PID_PREFIRM 0x9022 /* Eagle II */
248#define EAGLE_II_PID_PSTFIRM 0x9021 /* Eagle II */
249
b72458a8 250#define EAGLE_III_PID_PREFIRM 0x9032 /* Eagle III */
251#define EAGLE_III_PID_PSTFIRM 0x9031 /* Eagle III */
252
c8e46379
SG
253#define EAGLE_IV_PID_PREFIRM 0x9042 /* Eagle IV */
254#define EAGLE_IV_PID_PSTFIRM 0x9041 /* Eagle IV */
255
b72458a8 256/*
257 * USR USB IDs
258 */
259#define USR_VID 0x0BAF
260#define MILLER_A_PID_PREFIRM 0x00F2
261#define MILLER_A_PID_PSTFIRM 0x00F1
262#define MILLER_B_PID_PREFIRM 0x00FA
263#define MILLER_B_PID_PSTFIRM 0x00F9
264#define HEINEKEN_A_PID_PREFIRM 0x00F6
265#define HEINEKEN_A_PID_PSTFIRM 0x00F5
266#define HEINEKEN_B_PID_PREFIRM 0x00F8
267#define HEINEKEN_B_PID_PSTFIRM 0x00F7
268
269#define PREFIRM 0
270#define PSTFIRM (1<<7)
603cf608
SG
271#define AUTO_ANNEX_A (1<<8)
272#define AUTO_ANNEX_B (1<<9)
273
b72458a8 274enum {
275 ADI930 = 0,
276 EAGLE_I,
277 EAGLE_II,
c8e46379
SG
278 EAGLE_III,
279 EAGLE_IV
b72458a8 280};
281
282/* macros for both struct usb_device_id and struct uea_softc */
283#define UEA_IS_PREFIRM(x) \
284 (!((x)->driver_info & PSTFIRM))
285#define UEA_CHIP_VERSION(x) \
286 ((x)->driver_info & 0xf)
287
603cf608
SG
288#define IS_ISDN(x) \
289 ((x)->annex & ANNEXB)
b72458a8 290
291#define INS_TO_USBDEV(ins) ins->usb_dev
292
293#define GET_STATUS(data) \
294 ((data >> 8) & 0xf)
c8e46379 295
b72458a8 296#define IS_OPERATIONAL(sc) \
c8e46379
SG
297 ((UEA_CHIP_VERSION(sc) != EAGLE_IV) ? \
298 (GET_STATUS(sc->stats.phy.state) == 2) : \
299 (sc->stats.phy.state == 7))
b72458a8 300
301/*
302 * Set of macros to handle unaligned data in the firmware blob.
303 * The FW_GET_BYTE() macro is provided only for consistency.
304 */
305
306#define FW_GET_BYTE(p) *((__u8 *) (p))
b72458a8 307
308#define FW_DIR "ueagle-atm/"
309#define NB_MODEM 4
310
311#define BULK_TIMEOUT 300
312#define CTRL_TIMEOUT 1000
313
22fcceb5 314#define ACK_TIMEOUT msecs_to_jiffies(3000)
b72458a8 315
316#define UEA_INTR_IFACE_NO 0
317#define UEA_US_IFACE_NO 1
318#define UEA_DS_IFACE_NO 2
319
320#define FASTEST_ISO_INTF 8
321
322#define UEA_BULK_DATA_PIPE 0x02
323#define UEA_IDMA_PIPE 0x04
324#define UEA_INTR_PIPE 0x04
325#define UEA_ISO_DATA_PIPE 0x08
326
c8e46379
SG
327#define UEA_E1_SET_BLOCK 0x0001
328#define UEA_E4_SET_BLOCK 0x002c
b72458a8 329#define UEA_SET_MODE 0x0003
330#define UEA_SET_2183_DATA 0x0004
331#define UEA_SET_TIMEOUT 0x0011
332
333#define UEA_LOOPBACK_OFF 0x0002
334#define UEA_LOOPBACK_ON 0x0003
335#define UEA_BOOT_IDMA 0x0006
336#define UEA_START_RESET 0x0007
337#define UEA_END_RESET 0x0008
338
339#define UEA_SWAP_MAILBOX (0x3fcd | 0x4000)
340#define UEA_MPTX_START (0x3fce | 0x4000)
341#define UEA_MPTX_MAILBOX (0x3fd6 | 0x4000)
342#define UEA_MPRX_MAILBOX (0x3fdf | 0x4000)
343
c8e46379
SG
344/* block information in eagle4 dsp firmware */
345struct block_index {
346 __le32 PageOffset;
347 __le32 NotLastBlock;
348 __le32 dummy;
349 __le32 PageSize;
350 __le32 PageAddress;
351 __le16 dummy1;
352 __le16 PageNumber;
353} __attribute__ ((packed));
354
355#define E4_IS_BOOT_PAGE(PageSize) ((le32_to_cpu(PageSize)) & 0x80000000)
356#define E4_PAGE_BYTES(PageSize) ((le32_to_cpu(PageSize) & 0x7fffffff) * 4)
357
358#define E4_L1_STRING_HEADER 0x10
359#define E4_MAX_PAGE_NUMBER 0x58
360#define E4_NO_SWAPPAGE_HEADERS 0x31
361
362/* l1_code is eagle4 dsp firmware format */
363struct l1_code {
364 u8 string_header[E4_L1_STRING_HEADER];
365 u8 page_number_to_block_index[E4_MAX_PAGE_NUMBER];
366 struct block_index page_header[E4_NO_SWAPPAGE_HEADERS];
367 u8 code [0];
368} __attribute__ ((packed));
369
370/* structures describing a block within a DSP page */
371struct block_info_e1 {
b72458a8 372 __le16 wHdr;
b72458a8 373 __le16 wAddress;
374 __le16 wSize;
375 __le16 wOvlOffset;
376 __le16 wOvl; /* overlay */
377 __le16 wLast;
378} __attribute__ ((packed));
c8e46379
SG
379#define E1_BLOCK_INFO_SIZE 12
380
381struct block_info_e4 {
382 __be16 wHdr;
383 __u8 bBootPage;
384 __u8 bPageNumber;
385 __be32 dwSize;
386 __be32 dwAddress;
387 __be16 wReserved;
388} __attribute__ ((packed));
389#define E4_BLOCK_INFO_SIZE 14
b72458a8 390
c8e46379
SG
391#define UEA_BIHDR 0xabcd
392#define UEA_RESERVED 0xffff
393
394/* constants describing cmv type */
395#define E1_PREAMBLE 0x535c
396#define E1_MODEMTOHOST 0x01
397#define E1_HOSTTOMODEM 0x10
398
399#define E1_MEMACCESS 0x1
400#define E1_ADSLDIRECTIVE 0x7
401#define E1_FUNCTION_TYPE(f) ((f) >> 4)
402#define E1_FUNCTION_SUBTYPE(f) ((f) & 0x0f)
403
404#define E4_MEMACCESS 0
405#define E4_ADSLDIRECTIVE 0xf
406#define E4_FUNCTION_TYPE(f) ((f) >> 8)
407#define E4_FUNCTION_SIZE(f) ((f) & 0x0f)
408#define E4_FUNCTION_SUBTYPE(f) (((f) >> 4) & 0x0f)
b72458a8 409
b72458a8 410/* for MEMACCESS */
c8e46379
SG
411#define E1_REQUESTREAD 0x0
412#define E1_REQUESTWRITE 0x1
413#define E1_REPLYREAD 0x2
414#define E1_REPLYWRITE 0x3
415
416#define E4_REQUESTREAD 0x0
417#define E4_REQUESTWRITE 0x4
418#define E4_REPLYREAD (E4_REQUESTREAD | 1)
419#define E4_REPLYWRITE (E4_REQUESTWRITE | 1)
420
b72458a8 421/* for ADSLDIRECTIVE */
c8e46379
SG
422#define E1_KERNELREADY 0x0
423#define E1_MODEMREADY 0x1
b72458a8 424
c8e46379
SG
425#define E4_KERNELREADY 0x0
426#define E4_MODEMREADY 0x1
427
428#define E1_MAKEFUNCTION(t, s) (((t) & 0xf) << 4 | ((s) & 0xf))
429#define E4_MAKEFUNCTION(t, st, s) (((t) & 0xf) << 8 | ((st) & 0xf) << 4 | ((s) & 0xf))
430
431#define E1_MAKESA(a, b, c, d) \
b72458a8 432 (((c) & 0xff) << 24 | \
433 ((d) & 0xff) << 16 | \
434 ((a) & 0xff) << 8 | \
435 ((b) & 0xff))
c8e46379
SG
436
437#define E1_GETSA1(a) ((a >> 8) & 0xff)
438#define E1_GETSA2(a) (a & 0xff)
439#define E1_GETSA3(a) ((a >> 24) & 0xff)
440#define E1_GETSA4(a) ((a >> 16) & 0xff)
441
442#define E1_SA_CNTL E1_MAKESA('C', 'N', 'T', 'L')
443#define E1_SA_DIAG E1_MAKESA('D', 'I', 'A', 'G')
444#define E1_SA_INFO E1_MAKESA('I', 'N', 'F', 'O')
445#define E1_SA_OPTN E1_MAKESA('O', 'P', 'T', 'N')
446#define E1_SA_RATE E1_MAKESA('R', 'A', 'T', 'E')
447#define E1_SA_STAT E1_MAKESA('S', 'T', 'A', 'T')
448
449#define E4_SA_CNTL 1
450#define E4_SA_STAT 2
451#define E4_SA_INFO 3
452#define E4_SA_TEST 4
453#define E4_SA_OPTN 5
454#define E4_SA_RATE 6
455#define E4_SA_DIAG 7
456#define E4_SA_CNFG 8
457
458/* structures representing a CMV (Configuration and Management Variable) */
459struct cmv_e1 {
460 __le16 wPreamble;
461 __u8 bDirection;
462 __u8 bFunction;
463 __le16 wIndex;
464 __le32 dwSymbolicAddress;
b72458a8 465 __le16 wOffsetAddress;
466 __le32 dwData;
467} __attribute__ ((packed));
b72458a8 468
c8e46379
SG
469struct cmv_e4 {
470 __be16 wGroup;
471 __be16 wFunction;
472 __be16 wOffset;
473 __be16 wAddress;
474 __be32 dwData [6];
475} __attribute__ ((packed));
476
477/* structures representing swap information */
478struct swap_info_e1 {
b72458a8 479 __u8 bSwapPageNo;
480 __u8 bOvl; /* overlay */
481} __attribute__ ((packed));
482
c8e46379
SG
483struct swap_info_e4 {
484 __u8 bSwapPageNo;
485} __attribute__ ((packed));
486
487/* structures representing interrupt data */
488#define e1_bSwapPageNo u.e1.s1.swapinfo.bSwapPageNo
489#define e1_bOvl u.e1.s1.swapinfo.bOvl
490#define e4_bSwapPageNo u.e4.s1.swapinfo.bSwapPageNo
491
492#define INT_LOADSWAPPAGE 0x0001
493#define INT_INCOMINGCMV 0x0002
494
495union intr_data_e1 {
496 struct {
497 struct swap_info_e1 swapinfo;
498 __le16 wDataSize;
499 } __attribute__ ((packed)) s1;
500 struct {
501 struct cmv_e1 cmv;
502 __le16 wDataSize;
503 } __attribute__ ((packed)) s2;
504} __attribute__ ((packed));
505
506union intr_data_e4 {
507 struct {
508 struct swap_info_e4 swapinfo;
509 __le16 wDataSize;
510 } __attribute__ ((packed)) s1;
511 struct {
512 struct cmv_e4 cmv;
513 __le16 wDataSize;
514 } __attribute__ ((packed)) s2;
515} __attribute__ ((packed));
516
b72458a8 517struct intr_pkt {
518 __u8 bType;
519 __u8 bNotification;
520 __le16 wValue;
521 __le16 wIndex;
522 __le16 wLength;
523 __le16 wInterrupt;
b72458a8 524 union {
c8e46379
SG
525 union intr_data_e1 e1;
526 union intr_data_e4 e4;
527 } u;
b72458a8 528} __attribute__ ((packed));
c8e46379
SG
529
530#define E1_INTR_PKT_SIZE 28
531#define E4_INTR_PKT_SIZE 64
b72458a8 532
533static struct usb_driver uea_driver;
ab3c81ff 534static DEFINE_MUTEX(uea_mutex);
c8e46379 535static const char *chip_name[] = {"ADI930", "Eagle I", "Eagle II", "Eagle III", "Eagle IV"};
b72458a8 536
537static int modem_index;
538static unsigned int debug;
503add46 539static unsigned int altsetting[NB_MODEM] = {[0 ... (NB_MODEM - 1)] = FASTEST_ISO_INTF};
b72458a8 540static int sync_wait[NB_MODEM];
541static char *cmv_file[NB_MODEM];
603cf608 542static int annex[NB_MODEM];
b72458a8 543
544module_param(debug, uint, 0644);
545MODULE_PARM_DESC(debug, "module debug level (0=off,1=on,2=verbose)");
503add46
SG
546module_param_array(altsetting, uint, NULL, 0644);
547MODULE_PARM_DESC(altsetting, "alternate setting for incoming traffic: 0=bulk, "
548 "1=isoc slowest, ... , 8=isoc fastest (default)");
b72458a8 549module_param_array(sync_wait, bool, NULL, 0644);
550MODULE_PARM_DESC(sync_wait, "wait the synchronisation before starting ATM");
551module_param_array(cmv_file, charp, NULL, 0644);
552MODULE_PARM_DESC(cmv_file,
553 "file name with configuration and management variables");
603cf608
SG
554module_param_array(annex, uint, NULL, 0644);
555MODULE_PARM_DESC(annex,
556 "manually set annex a/b (0=auto, 1=annex a, 2=annex b)");
b72458a8 557
337427f9
SG
558#define uea_wait(sc, cond, timeo) \
559({ \
560 int _r = wait_event_interruptible_timeout(sc->sync_q, \
561 (cond) || kthread_should_stop(), timeo); \
562 if (kthread_should_stop()) \
563 _r = -ENODEV; \
564 _r; \
565})
566
b72458a8 567#define UPDATE_ATM_STAT(type, val) \
568 do { \
569 if (sc->usbatm->atm_dev) \
570 sc->usbatm->atm_dev->type = val; \
571 } while (0)
572
573/* Firmware loading */
574#define LOAD_INTERNAL 0xA0
575#define F8051_USBCS 0x7f92
576
577/**
578 * uea_send_modem_cmd - Send a command for pre-firmware devices.
579 */
580static int uea_send_modem_cmd(struct usb_device *usb,
aafcd2f7 581 u16 addr, u16 size, const u8 *buff)
b72458a8 582{
583 int ret = -ENOMEM;
584 u8 *xfer_buff;
585
5d7efe5b 586 xfer_buff = kmemdup(buff, size, GFP_KERNEL);
b72458a8 587 if (xfer_buff) {
b72458a8 588 ret = usb_control_msg(usb,
589 usb_sndctrlpipe(usb, 0),
590 LOAD_INTERNAL,
591 USB_DIR_OUT | USB_TYPE_VENDOR |
592 USB_RECIP_DEVICE, addr, 0, xfer_buff,
593 size, CTRL_TIMEOUT);
594 kfree(xfer_buff);
595 }
596
597 if (ret < 0)
598 return ret;
599
600 return (ret == size) ? 0 : -EIO;
601}
602
603static void uea_upload_pre_firmware(const struct firmware *fw_entry, void *context)
604{
605 struct usb_device *usb = context;
aafcd2f7
DW
606 const u8 *pfw;
607 u8 value;
b72458a8 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;
8d7802ed 619 if (size < 4)
620 goto err_fw_corrupted;
b72458a8 621
a5abdeaf 622 crc = get_unaligned_le32(pfw);
b72458a8 623 pfw += 4;
624 size -= 4;
8d7802ed 625 if (crc32_be(0, pfw, size) != crc)
626 goto err_fw_corrupted;
b72458a8 627
628 /*
877d0310 629 * Start to upload firmware : send reset
b72458a8 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
8d7802ed 639 while (size > 3) {
b72458a8 640 u8 len = FW_GET_BYTE(pfw);
a5abdeaf 641 u16 add = get_unaligned_le16(pfw + 1);
8d7802ed 642
643 size -= len + 3;
644 if (size < 0)
645 goto err_fw_corrupted;
646
b72458a8 647 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;
b72458a8 654 }
655
8d7802ed 656 if (size != 0)
657 goto err_fw_corrupted;
658
b72458a8 659 /*
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
8d7802ed 669 uea_leaves(usb);
670 return;
671
672err_fw_corrupted:
673 uea_err(usb, "firmware is corrupted\n");
b72458a8 674err:
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;
c8e46379
SG
702 case EAGLE_IV:
703 fw_name = FW_DIR "eagleIV.fw";
704 break;
b72458a8 705 }
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 */
aafcd2f7 723static int check_dsp_e1(const u8 *dsp, unsigned int len)
b72458a8 724{
725 u8 pagecount, blockcount;
726 u16 blocksize;
727 u32 pageoffset;
728 unsigned int i, j, p, pp;
729
b72458a8 730 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
a5abdeaf 739 pageoffset = get_unaligned_le32(dsp + p);
b72458a8 740 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 */
a5abdeaf 760 blocksize = get_unaligned_le16(dsp + pp);
b72458a8 761 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
aafcd2f7 774static int check_dsp_e4(const u8 *dsp, int len)
c8e46379
SG
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
b72458a8 819/*
820 * send data to the idma pipe
821 * */
aafcd2f7 822static int uea_idma_write(struct uea_softc *sc, const void *data, u32 size)
b72458a8 823{
824 int ret = -ENOMEM;
825 u8 *xfer_buff;
826 int bytes_read;
827
5d7efe5b 828 xfer_buff = kmemdup(data, size, GFP_KERNEL);
b72458a8 829 if (!xfer_buff) {
830 uea_err(INS_TO_USBDEV(sc), "can't allocate xfer_buff\n");
831 return ret;
832 }
833
b72458a8 834 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
c8e46379 855 if (UEA_CHIP_VERSION(sc) == EAGLE_IV) {
603cf608 856 if (IS_ISDN(sc))
c8e46379
SG
857 dsp_name = FW_DIR "DSP4i.bin";
858 else
859 dsp_name = FW_DIR "DSP4p.bin";
860 } else if (UEA_CHIP_VERSION(sc) == ADI930) {
603cf608 861 if (IS_ISDN(sc))
b72458a8 862 dsp_name = FW_DIR "DSP9i.bin";
863 else
864 dsp_name = FW_DIR "DSP9p.bin";
865 } else {
603cf608 866 if (IS_ISDN(sc))
b72458a8 867 dsp_name = FW_DIR "DSPei.bin";
868 else
869 dsp_name = FW_DIR "DSPep.bin";
870 }
871
e40abaf6 872 ret = request_firmware(&sc->dsp_firm, dsp_name, &sc->usb_dev->dev);
b72458a8 873 if (ret < 0) {
874 uea_err(INS_TO_USBDEV(sc),
875 "requesting firmware %s failed with error %d\n",
c8e46379 876 dsp_name, ret);
b72458a8 877 return ret;
878 }
879
c8e46379
SG
880 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) {
b72458a8 886 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 */
c8e46379 899static void uea_load_page_e1(struct work_struct *work)
b72458a8 900{
c4028958 901 struct uea_softc *sc = container_of(work, struct uea_softc, task);
b72458a8 902 u16 pageno = sc->pageno;
903 u16 ovl = sc->ovl;
c8e46379 904 struct block_info_e1 bi;
b72458a8 905
aafcd2f7 906 const u8 *p;
b72458a8 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;
a5abdeaf 929 pageoffset = get_unaligned_le32(p);
b72458a8 930
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++) {
a5abdeaf 946 blockaddr = get_unaligned_le16(p);
b72458a8 947 p += 2;
948
a5abdeaf 949 blocksize = get_unaligned_le16(p);
b72458a8 950 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 */
c8e46379 957 if (uea_idma_write(sc, &bi, E1_BLOCK_INFO_SIZE))
b72458a8 958 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:
2a99b507 973 uea_err(INS_TO_USBDEV(sc), "invalid DSP page %u requested\n", pageno);
b72458a8 974}
975
c8e46379
SG
976static 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 {
aafcd2f7 989 const u8 *blockoffset;
c8e46379
SG
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);
fd05e720 997 bi.dwAddress = cpu_to_be32(le32_to_cpu(blockidx->PageAddress));
c8e46379
SG
998
999 uea_dbg(INS_TO_USBDEV(sc),
dc0d5c1e 1000 "sending block %u for DSP page %u size %u address %x\n",
c8e46379
SG
1001 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;
fd05e720 1041 if (pageno >= le16_to_cpu(p->page_header[0].PageNumber)) {
c8e46379
SG
1042 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));
fd05e720 1066 bi.dwAddress = cpu_to_be32(le32_to_cpu(p->page_header[0].PageAddress));
c8e46379
SG
1067
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
b72458a8 1073static inline void wake_up_cmv_ack(struct uea_softc *sc)
1074{
2a99b507 1075 BUG_ON(sc->cmv_ack);
b72458a8 1076 sc->cmv_ack = 1;
337427f9 1077 wake_up(&sc->sync_q);
b72458a8 1078}
1079
1080static inline int wait_cmv_ack(struct uea_softc *sc)
1081{
337427f9
SG
1082 int ret = uea_wait(sc, sc->cmv_ack , ACK_TIMEOUT);
1083
b72458a8 1084 sc->cmv_ack = 0;
1085
2a99b507 1086 uea_dbg(INS_TO_USBDEV(sc), "wait_event_timeout : %d ms\n",
1087 jiffies_to_msecs(ret));
1088
b72458a8 1089 if (ret < 0)
1090 return ret;
1091
1092 return (ret == 0) ? -ETIMEDOUT : 0;
b72458a8 1093}
1094
1095#define UCDC_SEND_ENCAPSULATED_COMMAND 0x00
1096
1097static int uea_request(struct uea_softc *sc,
aafcd2f7 1098 u16 value, u16 index, u16 size, const void *data)
b72458a8 1099{
1100 u8 *xfer_buff;
1101 int ret = -ENOMEM;
1102
5d7efe5b 1103 xfer_buff = kmemdup(data, size, GFP_KERNEL);
b72458a8 1104 if (!xfer_buff) {
1105 uea_err(INS_TO_USBDEV(sc), "can't allocate xfer_buff\n");
1106 return ret;
1107 }
b72458a8 1108
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
c8e46379 1130static int uea_cmv_e1(struct uea_softc *sc,
b72458a8 1131 u8 function, u32 address, u16 offset, u32 data)
1132{
c8e46379 1133 struct cmv_e1 cmv;
b72458a8 1134 int ret;
1135
2a99b507 1136 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",
c8e46379
SG
1139 E1_FUNCTION_TYPE(function), E1_FUNCTION_SUBTYPE(function),
1140 E1_GETSA1(address), E1_GETSA2(address), E1_GETSA3(address),
1141 E1_GETSA4(address), offset, data);
1142
b72458a8 1143 /* we send a request, but we expect a reply */
c8e46379
SG
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;
b72458a8 1148
c8e46379
SG
1149 cmv.wPreamble = cpu_to_le16(E1_PREAMBLE);
1150 cmv.bDirection = E1_HOSTTOMODEM;
b72458a8 1151 cmv.bFunction = function;
c8e46379 1152 cmv.wIndex = cpu_to_le16(sc->cmv_dsc.e1.idx);
a5abdeaf 1153 put_unaligned_le32(address, &cmv.dwSymbolicAddress);
b72458a8 1154 cmv.wOffsetAddress = cpu_to_le16(offset);
a5abdeaf 1155 put_unaligned_le32(data >> 16 | data << 16, &cmv.dwData);
b72458a8 1156
c8e46379
SG
1157 ret = uea_request(sc, UEA_E1_SET_BLOCK, UEA_MPTX_START, sizeof(cmv), &cmv);
1158 if (ret < 0)
1159 return ret;
1160 ret = wait_cmv_ack(sc);
1161 uea_leaves(INS_TO_USBDEV(sc));
1162 return ret;
1163}
1164
1165static 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);
b72458a8 1192 if (ret < 0)
1193 return ret;
2a99b507 1194 ret = wait_cmv_ack(sc);
1195 uea_leaves(INS_TO_USBDEV(sc));
1196 return ret;
b72458a8 1197}
1198
c8e46379 1199static inline int uea_read_cmv_e1(struct uea_softc *sc,
b72458a8 1200 u32 address, u16 offset, u32 *data)
1201{
c8e46379 1202 int ret = uea_cmv_e1(sc, E1_MAKEFUNCTION(E1_MEMACCESS, E1_REQUESTREAD),
b72458a8 1203 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
c8e46379
SG
1213static 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,
b72458a8 1231 u32 address, u16 offset, u32 data)
1232{
c8e46379 1233 int ret = uea_cmv_e1(sc, E1_MAKEFUNCTION(E1_MEMACCESS, E1_REQUESTWRITE),
b72458a8 1234 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
c8e46379
SG
1242static 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 ||
503add46 1266 altsetting[sc->modem_index] > 0 ||
c8e46379
SG
1267 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
b72458a8 1278/*
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 */
c8e46379 1283static int uea_stat_e1(struct uea_softc *sc)
b72458a8 1284{
1285 u32 data;
1286 int ret;
1287
1288 uea_enters(INS_TO_USBDEV(sc));
1289 data = sc->stats.phy.state;
1290
c8e46379 1291 ret = uea_read_cmv_e1(sc, E1_SA_STAT, 0, &sc->stats.phy.state);
b72458a8 1292 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 ... */
a7a0c9cd 1310 uea_info(INS_TO_USBDEV(sc), "modem synchronization failed"
c8e46379 1311 " (may be try other cmv/dsp)\n");
b72458a8 1312 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 }
b72458a8 1339 }
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
c8e46379 1349 ret = uea_read_cmv_e1(sc, E1_SA_DIAG, 2, &sc->stats.phy.flags);
b72458a8 1350 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) {
2a99b507 1358 uea_dbg(INS_TO_USBDEV(sc), "Stat flag = 0x%x\n",
b72458a8 1359 sc->stats.phy.flags);
1360 return 0;
1361 }
1362
c8e46379 1363 ret = uea_read_cmv_e1(sc, E1_SA_RATE, 0, &data);
b72458a8 1364 if (ret < 0)
1365 return ret;
1366
c8e46379 1367 uea_set_bulk_timeout(sc, (data >> 16) * 32);
b72458a8 1368 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
c8e46379 1372 ret = uea_read_cmv_e1(sc, E1_SA_DIAG, 23, &data);
b72458a8 1373 if (ret < 0)
1374 return ret;
1375 sc->stats.phy.dsattenuation = (data & 0xff) / 2;
1376
c8e46379 1377 ret = uea_read_cmv_e1(sc, E1_SA_DIAG, 47, &data);
b72458a8 1378 if (ret < 0)
1379 return ret;
1380 sc->stats.phy.usattenuation = (data & 0xff) / 2;
1381
c8e46379 1382 ret = uea_read_cmv_e1(sc, E1_SA_DIAG, 25, &sc->stats.phy.dsmargin);
b72458a8 1383 if (ret < 0)
1384 return ret;
1385
c8e46379 1386 ret = uea_read_cmv_e1(sc, E1_SA_DIAG, 49, &sc->stats.phy.usmargin);
b72458a8 1387 if (ret < 0)
1388 return ret;
1389
c8e46379 1390 ret = uea_read_cmv_e1(sc, E1_SA_DIAG, 51, &sc->stats.phy.rxflow);
b72458a8 1391 if (ret < 0)
1392 return ret;
1393
c8e46379 1394 ret = uea_read_cmv_e1(sc, E1_SA_DIAG, 52, &sc->stats.phy.txflow);
b72458a8 1395 if (ret < 0)
1396 return ret;
1397
c8e46379 1398 ret = uea_read_cmv_e1(sc, E1_SA_DIAG, 54, &sc->stats.phy.dsunc);
b72458a8 1399 if (ret < 0)
1400 return ret;
1401
1402 /* only for atu-c */
c8e46379 1403 ret = uea_read_cmv_e1(sc, E1_SA_DIAG, 58, &sc->stats.phy.usunc);
b72458a8 1404 if (ret < 0)
1405 return ret;
1406
c8e46379 1407 ret = uea_read_cmv_e1(sc, E1_SA_DIAG, 53, &sc->stats.phy.dscorr);
b72458a8 1408 if (ret < 0)
1409 return ret;
1410
1411 /* only for atu-c */
c8e46379 1412 ret = uea_read_cmv_e1(sc, E1_SA_DIAG, 57, &sc->stats.phy.uscorr);
b72458a8 1413 if (ret < 0)
1414 return ret;
1415
c8e46379 1416 ret = uea_read_cmv_e1(sc, E1_SA_INFO, 8, &sc->stats.phy.vidco);
b72458a8 1417 if (ret < 0)
1418 return ret;
1419
c8e46379 1420 ret = uea_read_cmv_e1(sc, E1_SA_INFO, 13, &sc->stats.phy.vidcpe);
b72458a8 1421 if (ret < 0)
1422 return ret;
1423
1424 return 0;
1425}
1426
c8e46379 1427static int uea_stat_e4(struct uea_softc *sc)
b72458a8 1428{
c8e46379
SG
1429 u32 data;
1430 u32 tmp_arr[2];
1431 int ret;
1432
1433 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";
b72458a8 1550 char *file;
b72458a8 1551
c8e46379 1552 /* set proper name corresponding modem version and line type */
b72458a8 1553 if (cmv_file[sc->modem_index] == NULL) {
1554 if (UEA_CHIP_VERSION(sc) == ADI930)
c8e46379
SG
1555 file_arr[3] = '9';
1556 else if (UEA_CHIP_VERSION(sc) == EAGLE_IV)
1557 file_arr[3] = '4';
b72458a8 1558 else
c8e46379
SG
1559 file_arr[3] = 'e';
1560
603cf608 1561 file_arr[4] = IS_ISDN(sc) ? 'i' : 'p';
c8e46379 1562 file = file_arr;
b72458a8 1563 } else
1564 file = cmv_file[sc->modem_index];
1565
1566 strcpy(cmv_name, FW_DIR);
c8e46379
SG
1567 strlcat(cmv_name, file, FIRMWARE_NAME_MAX);
1568 if (ver == 2)
1569 strlcat(cmv_name, ".v2", FIRMWARE_NAME_MAX);
1570}
1571
1572static 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 */
b72458a8 1578
c8e46379 1579 cmvs_file_name(sc, cmv_name, 1);
b72458a8 1580 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;
c8e46379
SG
1589 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;
1626 }
1627
1628 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;
b72458a8 1638 }
1639
c8e46379
SG
1640 *ver = 2;
1641
1642 data += 4;
1643 size -= 4;
1644 if (size < 5)
1645 goto err_fw_corrupted;
1646
a5abdeaf 1647 crc = get_unaligned_le32(data);
c8e46379
SG
1648 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);
b72458a8 1657 return *data;
c8e46379
SG
1658
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++) {
a5abdeaf
HH
1697 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));
c8e46379
SG
1700 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++) {
a5abdeaf
HH
1707 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));
c8e46379
SG
1710 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,
a5abdeaf
HH
1760 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));
c8e46379
SG
1764 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;
b72458a8 1780}
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; /* ;-) */
c8e46379 1791 int ret;
b72458a8 1792
1793 uea_enters(INS_TO_USBDEV(sc));
1794 uea_info(INS_TO_USBDEV(sc), "(re)booting started\n");
1795
22fcceb5 1796 /* mask interrupt */
b72458a8 1797 sc->booting = 1;
22fcceb5 1798 /* 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;
b72458a8 1803 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 */
337427f9
SG
1816 ret = uea_wait(sc, 0, msecs_to_jiffies(100));
1817 if (ret < 0)
1818 return ret;
b72458a8 1819
1820 /* leave reset mode */
1821 uea_request(sc, UEA_SET_MODE, UEA_END_RESET, 0, NULL);
1822
c8e46379
SG
1823 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 }
b72458a8 1829
337427f9
SG
1830 ret = uea_wait(sc, 0, msecs_to_jiffies(1000));
1831 if (ret < 0)
1832 return ret;
c8e46379
SG
1833
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
22fcceb5 1839 /* demask interrupt */
b72458a8 1840 sc->booting = 0;
1841
1842 /* start loading DSP */
1843 sc->pageno = 0;
1844 sc->ovl = 0;
04ea02f5 1845 queue_work(sc->work_q, &sc->task);
b72458a8 1846
1847 /* wait for modem ready CMV */
1848 ret = wait_cmv_ack(sc);
1849 if (ret < 0)
1850 return ret;
1851
2a99b507 1852 uea_vdbg(INS_TO_USBDEV(sc), "Ready CMV received\n");
1853
c8e46379 1854 ret = sc->send_cmvs(sc);
b72458a8 1855 if (ret < 0)
1856 return ret;
1857
b72458a8 1858 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
83144186 1874 set_freezable();
b72458a8 1875 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)
c8e46379 1880 ret = sc->stat(sc);
b72458a8 1881 if (ret != -EAGAIN)
337427f9 1882 uea_wait(sc, 0, msecs_to_jiffies(1000));
0eb0226c 1883 try_to_freeze();
b72458a8 1884 }
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;
aafcd2f7
DW
1894 const u8 *pfw;
1895 u8 value;
b72458a8 1896 char *fw_name = FW_DIR "930-fpga.bin";
1897
1898 uea_enters(INS_TO_USBDEV(sc));
1899
1900 ret = request_firmware(&fw_entry, fw_name, &sc->usb_dev->dev);
1901 if (ret) {
1902 uea_err(INS_TO_USBDEV(sc), "firmware %s is not available\n",
1903 fw_name);
1904 goto err0;
1905 }
1906
1907 pfw = fw_entry->data;
1908 size = fw_entry->size;
1909 if (size != 0x577B) {
1910 uea_err(INS_TO_USBDEV(sc), "firmware %s is corrupted\n",
1911 fw_name);
1912 ret = -EILSEQ;
1913 goto err1;
1914 }
1915 for (u = 0; u < size; u += ln) {
1916 ln = min(size - u, 64);
1917 ret = uea_request(sc, 0xe, 0, ln, pfw + u);
1918 if (ret < 0) {
1919 uea_err(INS_TO_USBDEV(sc),
1920 "elsa download data failed (%d)\n", ret);
1921 goto err1;
1922 }
1923 }
1924
e40abaf6 1925 /* finish to send the fpga */
b72458a8 1926 ret = uea_request(sc, 0xe, 1, 0, NULL);
1927 if (ret < 0) {
1928 uea_err(INS_TO_USBDEV(sc),
1929 "elsa download data failed (%d)\n", ret);
1930 goto err1;
1931 }
1932
e40abaf6 1933 /* Tell the modem we finish : de-assert reset */
b72458a8 1934 value = 0;
1935 ret = uea_send_modem_cmd(sc->usb_dev, 0xe, 1, &value);
1936 if (ret < 0)
1937 uea_err(sc->usb_dev, "elsa de-assert failed with error %d\n", ret);
1938
b72458a8 1939err1:
1940 release_firmware(fw_entry);
1941err0:
1942 uea_leaves(INS_TO_USBDEV(sc));
1943 return ret;
1944}
1945
e40abaf6 1946/* The modem send us an ack. First with check if it right */
c8e46379 1947static void uea_dispatch_cmv_e1(struct uea_softc *sc, struct intr_pkt *intr)
b72458a8 1948{
c8e46379
SG
1949 struct cmv_dsc_e1 *dsc = &sc->cmv_dsc.e1;
1950 struct cmv_e1 *cmv = &intr->u.e1.s2.cmv;
1951
b72458a8 1952 uea_enters(INS_TO_USBDEV(sc));
c8e46379 1953 if (le16_to_cpu(cmv->wPreamble) != E1_PREAMBLE)
b72458a8 1954 goto bad1;
1955
c8e46379 1956 if (cmv->bDirection != E1_MODEMTOHOST)
b72458a8 1957 goto bad1;
1958
1959 /* FIXME : ADI930 reply wrong preambule (func = 2, sub = 2) to
1960 * the first MEMACESS cmv. Ignore it...
1961 */
c8e46379 1962 if (cmv->bFunction != dsc->function) {
b72458a8 1963 if (UEA_CHIP_VERSION(sc) == ADI930
c8e46379
SG
1964 && cmv->bFunction == E1_MAKEFUNCTION(2, 2)) {
1965 cmv->wIndex = cpu_to_le16(dsc->idx);
a5abdeaf 1966 put_unaligned_le32(dsc->address, &cmv->dwSymbolicAddress);
c8e46379
SG
1967 cmv->wOffsetAddress = cpu_to_le16(dsc->offset);
1968 } else
b72458a8 1969 goto bad2;
1970 }
1971
c8e46379 1972 if (cmv->bFunction == E1_MAKEFUNCTION(E1_ADSLDIRECTIVE, E1_MODEMREADY)) {
b72458a8 1973 wake_up_cmv_ack(sc);
2a99b507 1974 uea_leaves(INS_TO_USBDEV(sc));
b72458a8 1975 return;
1976 }
1977
1978 /* in case of MEMACCESS */
c8e46379 1979 if (le16_to_cpu(cmv->wIndex) != dsc->idx ||
a5abdeaf 1980 get_unaligned_le32(&cmv->dwSymbolicAddress) != dsc->address ||
c8e46379 1981 le16_to_cpu(cmv->wOffsetAddress) != dsc->offset)
b72458a8 1982 goto bad2;
1983
a5abdeaf 1984 sc->data = get_unaligned_le32(&cmv->dwData);
b72458a8 1985 sc->data = sc->data << 16 | sc->data >> 16;
1986
1987 wake_up_cmv_ack(sc);
2a99b507 1988 uea_leaves(INS_TO_USBDEV(sc));
b72458a8 1989 return;
1990
1991bad2:
fec8de3a 1992 uea_err(INS_TO_USBDEV(sc), "unexpected cmv received, "
b72458a8 1993 "Function : %d, Subfunction : %d\n",
c8e46379
SG
1994 E1_FUNCTION_TYPE(cmv->bFunction),
1995 E1_FUNCTION_SUBTYPE(cmv->bFunction));
2a99b507 1996 uea_leaves(INS_TO_USBDEV(sc));
b72458a8 1997 return;
1998
1999bad1:
2000 uea_err(INS_TO_USBDEV(sc), "invalid cmv received, "
2001 "wPreamble %d, bDirection %d\n",
2002 le16_to_cpu(cmv->wPreamble), cmv->bDirection);
2a99b507 2003 uea_leaves(INS_TO_USBDEV(sc));
b72458a8 2004}
2005
c8e46379
SG
2006/* The modem send us an ack. First with check if it right */
2007static void uea_dispatch_cmv_e4(struct uea_softc *sc, struct intr_pkt *intr)
2008{
2009 struct cmv_dsc_e4 *dsc = &sc->cmv_dsc.e4;
2010 struct cmv_e4 *cmv = &intr->u.e4.s2.cmv;
2011
2012 uea_enters(INS_TO_USBDEV(sc));
2013 uea_dbg(INS_TO_USBDEV(sc), "cmv %x %x %x %x %x %x\n",
2014 be16_to_cpu(cmv->wGroup), be16_to_cpu(cmv->wFunction),
2015 be16_to_cpu(cmv->wOffset), be16_to_cpu(cmv->wAddress),
2016 be32_to_cpu(cmv->dwData[0]), be32_to_cpu(cmv->dwData[1]));
2017
2018 if (be16_to_cpu(cmv->wFunction) != dsc->function)
2019 goto bad2;
2020
2021 if (be16_to_cpu(cmv->wFunction) == E4_MAKEFUNCTION(E4_ADSLDIRECTIVE, E4_MODEMREADY, 1)) {
2022 wake_up_cmv_ack(sc);
2023 uea_leaves(INS_TO_USBDEV(sc));
2024 return;
2025 }
2026
2027 /* in case of MEMACCESS */
2028 if (be16_to_cpu(cmv->wOffset) != dsc->offset ||
2029 be16_to_cpu(cmv->wGroup) != dsc->group ||
2030 be16_to_cpu(cmv->wAddress) != dsc->address)
2031 goto bad2;
2032
2033 sc->data = be32_to_cpu(cmv->dwData[0]);
2034 sc->data1 = be32_to_cpu(cmv->dwData[1]);
2035 wake_up_cmv_ack(sc);
2036 uea_leaves(INS_TO_USBDEV(sc));
2037 return;
2038
2039bad2:
fec8de3a 2040 uea_err(INS_TO_USBDEV(sc), "unexpected cmv received, "
c8e46379
SG
2041 "Function : %d, Subfunction : %d\n",
2042 E4_FUNCTION_TYPE(cmv->wFunction),
2043 E4_FUNCTION_SUBTYPE(cmv->wFunction));
2044 uea_leaves(INS_TO_USBDEV(sc));
2045 return;
2046}
2047
2048static void uea_schedule_load_page_e1(struct uea_softc *sc, struct intr_pkt *intr)
2049{
2050 sc->pageno = intr->e1_bSwapPageNo;
2051 sc->ovl = intr->e1_bOvl >> 4 | intr->e1_bOvl << 4;
04ea02f5 2052 queue_work(sc->work_q, &sc->task);
c8e46379
SG
2053}
2054
2055static void uea_schedule_load_page_e4(struct uea_softc *sc, struct intr_pkt *intr)
2056{
2057 sc->pageno = intr->e4_bSwapPageNo;
04ea02f5 2058 queue_work(sc->work_q, &sc->task);
c8e46379
SG
2059}
2060
b72458a8 2061/*
2062 * interrupt handler
2063 */
7d12e780 2064static void uea_intr(struct urb *urb)
b72458a8 2065{
e40abaf6 2066 struct uea_softc *sc = urb->context;
2067 struct intr_pkt *intr = urb->transfer_buffer;
508330eb
GKH
2068 int status = urb->status;
2069
b72458a8 2070 uea_enters(INS_TO_USBDEV(sc));
2071
508330eb 2072 if (unlikely(status < 0)) {
b72458a8 2073 uea_err(INS_TO_USBDEV(sc), "uea_intr() failed with %d\n",
508330eb 2074 status);
b72458a8 2075 return;
2076 }
2077
b72458a8 2078 /* device-to-host interrupt */
2079 if (intr->bType != 0x08 || sc->booting) {
e40abaf6 2080 uea_err(INS_TO_USBDEV(sc), "wrong interrupt\n");
b72458a8 2081 goto resubmit;
2082 }
2083
2084 switch (le16_to_cpu(intr->wInterrupt)) {
2085 case INT_LOADSWAPPAGE:
c8e46379 2086 sc->schedule_load_page(sc, intr);
b72458a8 2087 break;
2088
2089 case INT_INCOMINGCMV:
c8e46379 2090 sc->dispatch_cmv(sc, intr);
b72458a8 2091 break;
2092
2093 default:
e40abaf6 2094 uea_err(INS_TO_USBDEV(sc), "unknown interrupt %u\n",
b72458a8 2095 le16_to_cpu(intr->wInterrupt));
2096 }
2097
2098resubmit:
2099 usb_submit_urb(sc->urb_int, GFP_ATOMIC);
2100}
2101
2102/*
2103 * Start the modem : init the data and start kernel thread
2104 */
2105static int uea_boot(struct uea_softc *sc)
2106{
c8e46379 2107 int ret, size;
b72458a8 2108 struct intr_pkt *intr;
2109
2110 uea_enters(INS_TO_USBDEV(sc));
2111
c8e46379
SG
2112 if (UEA_CHIP_VERSION(sc) == EAGLE_IV) {
2113 size = E4_INTR_PKT_SIZE;
2114 sc->dispatch_cmv = uea_dispatch_cmv_e4;
2115 sc->schedule_load_page = uea_schedule_load_page_e4;
2116 sc->stat = uea_stat_e4;
2117 sc->send_cmvs = uea_send_cmvs_e4;
2118 INIT_WORK(&sc->task, uea_load_page_e4);
2119 } else {
2120 size = E1_INTR_PKT_SIZE;
2121 sc->dispatch_cmv = uea_dispatch_cmv_e1;
2122 sc->schedule_load_page = uea_schedule_load_page_e1;
2123 sc->stat = uea_stat_e1;
2124 sc->send_cmvs = uea_send_cmvs_e1;
2125 INIT_WORK(&sc->task, uea_load_page_e1);
2126 }
2127
b72458a8 2128 init_waitqueue_head(&sc->sync_q);
b72458a8 2129
04ea02f5
SG
2130 sc->work_q = create_workqueue("ueagle-dsp");
2131 if (!sc->work_q) {
2132 uea_err(INS_TO_USBDEV(sc), "cannot allocate workqueue\n");
2133 uea_leaves(INS_TO_USBDEV(sc));
2134 return -ENOMEM;
2135 }
2136
b72458a8 2137 if (UEA_CHIP_VERSION(sc) == ADI930)
2138 load_XILINX_firmware(sc);
2139
c8e46379 2140 intr = kmalloc(size, GFP_KERNEL);
b72458a8 2141 if (!intr) {
2142 uea_err(INS_TO_USBDEV(sc),
2143 "cannot allocate interrupt package\n");
04ea02f5 2144 goto err0;
b72458a8 2145 }
2146
2147 sc->urb_int = usb_alloc_urb(0, GFP_KERNEL);
2148 if (!sc->urb_int) {
2149 uea_err(INS_TO_USBDEV(sc), "cannot allocate interrupt URB\n");
04ea02f5 2150 goto err1;
b72458a8 2151 }
2152
2153 usb_fill_int_urb(sc->urb_int, sc->usb_dev,
2154 usb_rcvintpipe(sc->usb_dev, UEA_INTR_PIPE),
c8e46379 2155 intr, size, uea_intr, sc,
b72458a8 2156 sc->usb_dev->actconfig->interface[0]->altsetting[0].
2157 endpoint[0].desc.bInterval);
2158
2159 ret = usb_submit_urb(sc->urb_int, GFP_KERNEL);
2160 if (ret < 0) {
2161 uea_err(INS_TO_USBDEV(sc),
2162 "urb submition failed with error %d\n", ret);
04ea02f5 2163 goto err1;
b72458a8 2164 }
2165
2166 sc->kthread = kthread_run(uea_kthread, sc, "ueagle-atm");
2167 if (sc->kthread == ERR_PTR(-ENOMEM)) {
2168 uea_err(INS_TO_USBDEV(sc), "failed to create thread\n");
2169 goto err2;
2170 }
2171
2172 uea_leaves(INS_TO_USBDEV(sc));
2173 return 0;
2174
2175err2:
2176 usb_kill_urb(sc->urb_int);
04ea02f5 2177err1:
b72458a8 2178 usb_free_urb(sc->urb_int);
4d45e218 2179 sc->urb_int = NULL;
2180 kfree(intr);
04ea02f5
SG
2181err0:
2182 destroy_workqueue(sc->work_q);
b72458a8 2183 uea_leaves(INS_TO_USBDEV(sc));
2184 return -ENOMEM;
2185}
2186
2187/*
2188 * Stop the modem : kill kernel thread and free data
2189 */
2190static void uea_stop(struct uea_softc *sc)
2191{
2192 int ret;
2193 uea_enters(INS_TO_USBDEV(sc));
2194 ret = kthread_stop(sc->kthread);
e40abaf6 2195 uea_dbg(INS_TO_USBDEV(sc), "kthread finish with status %d\n", ret);
b72458a8 2196
b72458a8 2197 uea_request(sc, UEA_SET_MODE, UEA_LOOPBACK_ON, 0, NULL);
2198
2199 usb_kill_urb(sc->urb_int);
2200 kfree(sc->urb_int->transfer_buffer);
2201 usb_free_urb(sc->urb_int);
2202
04ea02f5
SG
2203 /* stop any pending boot process, when no one can schedule work */
2204 destroy_workqueue(sc->work_q);
2205
b72458a8 2206 if (sc->dsp_firm)
2207 release_firmware(sc->dsp_firm);
2208 uea_leaves(INS_TO_USBDEV(sc));
2209}
2210
2211/* syfs interface */
2212static struct uea_softc *dev_to_uea(struct device *dev)
2213{
2214 struct usb_interface *intf;
2215 struct usbatm_data *usbatm;
2216
2217 intf = to_usb_interface(dev);
2218 if (!intf)
2219 return NULL;
2220
2221 usbatm = usb_get_intfdata(intf);
2222 if (!usbatm)
2223 return NULL;
2224
2225 return usbatm->driver_data;
2226}
2227
2228static ssize_t read_status(struct device *dev, struct device_attribute *attr,
2229 char *buf)
2230{
2231 int ret = -ENODEV;
2232 struct uea_softc *sc;
2233
ab3c81ff 2234 mutex_lock(&uea_mutex);
b72458a8 2235 sc = dev_to_uea(dev);
2236 if (!sc)
2237 goto out;
2238 ret = snprintf(buf, 10, "%08x\n", sc->stats.phy.state);
2239out:
ab3c81ff 2240 mutex_unlock(&uea_mutex);
b72458a8 2241 return ret;
2242}
2243
2244static ssize_t reboot(struct device *dev, struct device_attribute *attr,
2245 const char *buf, size_t count)
2246{
2247 int ret = -ENODEV;
2248 struct uea_softc *sc;
2249
ab3c81ff 2250 mutex_lock(&uea_mutex);
b72458a8 2251 sc = dev_to_uea(dev);
2252 if (!sc)
2253 goto out;
2254 sc->reset = 1;
2255 ret = count;
2256out:
ab3c81ff 2257 mutex_unlock(&uea_mutex);
b72458a8 2258 return ret;
2259}
2260
2261static DEVICE_ATTR(stat_status, S_IWUGO | S_IRUGO, read_status, reboot);
2262
2263static ssize_t read_human_status(struct device *dev, struct device_attribute *attr,
2264 char *buf)
2265{
2266 int ret = -ENODEV;
c8e46379 2267 int modem_state;
b72458a8 2268 struct uea_softc *sc;
2269
ab3c81ff 2270 mutex_lock(&uea_mutex);
b72458a8 2271 sc = dev_to_uea(dev);
2272 if (!sc)
2273 goto out;
2274
c8e46379
SG
2275 if (UEA_CHIP_VERSION(sc) == EAGLE_IV) {
2276 switch (sc->stats.phy.state) {
2277 case 0x0: /* not yet synchronized */
2278 case 0x1:
2279 case 0x3:
2280 case 0x4:
2281 modem_state = 0;
2282 break;
2283 case 0x5: /* initialization */
2284 case 0x6:
2285 case 0x9:
2286 case 0xa:
2287 modem_state = 1;
2288 break;
2289 case 0x7: /* operational */
2290 modem_state = 2;
2291 break;
2292 case 0x2: /* fail ... */
2293 modem_state = 3;
2294 break;
2295 default: /* unknown */
2296 modem_state = 4;
2297 break;
2298 }
2299 } else
2300 modem_state = GET_STATUS(sc->stats.phy.state);
2301
2302 switch (modem_state) {
b72458a8 2303 case 0:
2304 ret = sprintf(buf, "Modem is booting\n");
2305 break;
2306 case 1:
2307 ret = sprintf(buf, "Modem is initializing\n");
2308 break;
2309 case 2:
2310 ret = sprintf(buf, "Modem is operational\n");
2311 break;
c8e46379 2312 case 3:
b72458a8 2313 ret = sprintf(buf, "Modem synchronization failed\n");
2314 break;
c8e46379
SG
2315 default:
2316 ret = sprintf(buf, "Modem state is unknown\n");
2317 break;
b72458a8 2318 }
2319out:
ab3c81ff 2320 mutex_unlock(&uea_mutex);
b72458a8 2321 return ret;
2322}
2323
2324static DEVICE_ATTR(stat_human_status, S_IWUGO | S_IRUGO, read_human_status, NULL);
2325
2326static ssize_t read_delin(struct device *dev, struct device_attribute *attr,
2327 char *buf)
2328{
2329 int ret = -ENODEV;
2330 struct uea_softc *sc;
c8e46379 2331 char *delin = "GOOD";
b72458a8 2332
ab3c81ff 2333 mutex_lock(&uea_mutex);
b72458a8 2334 sc = dev_to_uea(dev);
2335 if (!sc)
2336 goto out;
2337
c8e46379
SG
2338 if (UEA_CHIP_VERSION(sc) == EAGLE_IV) {
2339 if (sc->stats.phy.flags & 0x4000)
2340 delin = "RESET";
2341 else if (sc->stats.phy.flags & 0x0001)
2342 delin = "LOSS";
2343 } else {
2344 if (sc->stats.phy.flags & 0x0C00)
2345 delin = "ERROR";
2346 else if (sc->stats.phy.flags & 0x0030)
2347 delin = "LOSS";
2348 }
2349
2350 ret = sprintf(buf, "%s\n", delin);
b72458a8 2351out:
ab3c81ff 2352 mutex_unlock(&uea_mutex);
b72458a8 2353 return ret;
2354}
2355
2356static DEVICE_ATTR(stat_delin, S_IWUGO | S_IRUGO, read_delin, NULL);
2357
2358#define UEA_ATTR(name, reset) \
2359 \
2360static ssize_t read_##name(struct device *dev, \
2361 struct device_attribute *attr, char *buf) \
2362{ \
2363 int ret = -ENODEV; \
2364 struct uea_softc *sc; \
2365 \
2a99b507 2366 mutex_lock(&uea_mutex); \
b72458a8 2367 sc = dev_to_uea(dev); \
2368 if (!sc) \
2369 goto out; \
2370 ret = snprintf(buf, 10, "%08x\n", sc->stats.phy.name); \
2371 if (reset) \
2372 sc->stats.phy.name = 0; \
2373out: \
2a99b507 2374 mutex_unlock(&uea_mutex); \
b72458a8 2375 return ret; \
2376} \
2377 \
2378static DEVICE_ATTR(stat_##name, S_IRUGO, read_##name, NULL)
2379
2380UEA_ATTR(mflags, 1);
2381UEA_ATTR(vidcpe, 0);
2382UEA_ATTR(usrate, 0);
2383UEA_ATTR(dsrate, 0);
2384UEA_ATTR(usattenuation, 0);
2385UEA_ATTR(dsattenuation, 0);
2386UEA_ATTR(usmargin, 0);
2387UEA_ATTR(dsmargin, 0);
2388UEA_ATTR(txflow, 0);
2389UEA_ATTR(rxflow, 0);
2390UEA_ATTR(uscorr, 0);
2391UEA_ATTR(dscorr, 0);
2392UEA_ATTR(usunc, 0);
2393UEA_ATTR(dsunc, 0);
a7a0c9cd 2394UEA_ATTR(firmid, 0);
b72458a8 2395
2396/* Retrieve the device End System Identifier (MAC) */
2397
2398#define htoi(x) (isdigit(x) ? x-'0' : toupper(x)-'A'+10)
2399static int uea_getesi(struct uea_softc *sc, u_char * esi)
2400{
2401 unsigned char mac_str[2 * ETH_ALEN + 1];
2402 int i;
2403 if (usb_string
2404 (sc->usb_dev, sc->usb_dev->descriptor.iSerialNumber, mac_str,
2405 sizeof(mac_str)) != 2 * ETH_ALEN)
2406 return 1;
2407
2408 for (i = 0; i < ETH_ALEN; i++)
2409 esi[i] = htoi(mac_str[2 * i]) * 16 + htoi(mac_str[2 * i + 1]);
2410
2411 return 0;
2412}
2413
2414/* ATM stuff */
2415static int uea_atm_open(struct usbatm_data *usbatm, struct atm_dev *atm_dev)
2416{
2417 struct uea_softc *sc = usbatm->driver_data;
2418
2419 return uea_getesi(sc, atm_dev->esi);
2420}
2421
2422static int uea_heavy(struct usbatm_data *usbatm, struct usb_interface *intf)
2423{
2424 struct uea_softc *sc = usbatm->driver_data;
2425
531a39bb 2426 wait_event_interruptible(sc->sync_q, IS_OPERATIONAL(sc));
b72458a8 2427
2428 return 0;
2429
2430}
2431
2432static int claim_interface(struct usb_device *usb_dev,
2433 struct usbatm_data *usbatm, int ifnum)
2434{
2435 int ret;
2436 struct usb_interface *intf = usb_ifnum_to_if(usb_dev, ifnum);
2437
2438 if (!intf) {
2439 uea_err(usb_dev, "interface %d not found\n", ifnum);
2440 return -ENODEV;
2441 }
2442
2443 ret = usb_driver_claim_interface(&uea_driver, intf, usbatm);
2444 if (ret != 0)
2445 uea_err(usb_dev, "can't claim interface %d, error %d\n", ifnum,
2446 ret);
2447 return ret;
2448}
2449
e7ccdfec
GKH
2450static struct attribute *attrs[] = {
2451 &dev_attr_stat_status.attr,
2452 &dev_attr_stat_mflags.attr,
2453 &dev_attr_stat_human_status.attr,
2454 &dev_attr_stat_delin.attr,
2455 &dev_attr_stat_vidcpe.attr,
2456 &dev_attr_stat_usrate.attr,
2457 &dev_attr_stat_dsrate.attr,
2458 &dev_attr_stat_usattenuation.attr,
2459 &dev_attr_stat_dsattenuation.attr,
2460 &dev_attr_stat_usmargin.attr,
2461 &dev_attr_stat_dsmargin.attr,
2462 &dev_attr_stat_txflow.attr,
2463 &dev_attr_stat_rxflow.attr,
2464 &dev_attr_stat_uscorr.attr,
2465 &dev_attr_stat_dscorr.attr,
2466 &dev_attr_stat_usunc.attr,
2467 &dev_attr_stat_dsunc.attr,
a7a0c9cd 2468 &dev_attr_stat_firmid.attr,
9ab99c8c 2469 NULL,
e7ccdfec
GKH
2470};
2471static struct attribute_group attr_grp = {
2472 .attrs = attrs,
2473};
2474
b72458a8 2475static int uea_bind(struct usbatm_data *usbatm, struct usb_interface *intf,
35644b0c 2476 const struct usb_device_id *id)
b72458a8 2477{
2478 struct usb_device *usb = interface_to_usbdev(intf);
2479 struct uea_softc *sc;
2480 int ret, ifnum = intf->altsetting->desc.bInterfaceNumber;
503add46 2481 unsigned int alt;
b72458a8 2482
2483 uea_enters(usb);
2484
2485 /* interface 0 is for firmware/monitoring */
2486 if (ifnum != UEA_INTR_IFACE_NO)
2487 return -ENODEV;
2488
0dfcd3e4 2489 usbatm->flags = (sync_wait[modem_index] ? 0 : UDSL_SKIP_HEAVY_INIT);
b72458a8 2490
2491 /* interface 1 is for outbound traffic */
2492 ret = claim_interface(usb, usbatm, UEA_US_IFACE_NO);
2493 if (ret < 0)
2494 return ret;
2495
e40abaf6 2496 /* ADI930 has only 2 interfaces and inbound traffic is on interface 1 */
b72458a8 2497 if (UEA_CHIP_VERSION(id) != ADI930) {
2498 /* interface 2 is for inbound traffic */
2499 ret = claim_interface(usb, usbatm, UEA_DS_IFACE_NO);
2500 if (ret < 0)
2501 return ret;
2502 }
2503
2504 sc = kzalloc(sizeof(struct uea_softc), GFP_KERNEL);
2505 if (!sc) {
584958c3 2506 uea_err(usb, "uea_init: not enough memory !\n");
b72458a8 2507 return -ENOMEM;
2508 }
2509
2510 sc->usb_dev = usb;
2511 usbatm->driver_data = sc;
2512 sc->usbatm = usbatm;
2513 sc->modem_index = (modem_index < NB_MODEM) ? modem_index++ : 0;
2514 sc->driver_info = id->driver_info;
2515
603cf608
SG
2516 /* first try to use module parameter */
2517 if (annex[sc->modem_index] == 1)
2518 sc->annex = ANNEXA;
2519 else if (annex[sc->modem_index] == 2)
2520 sc->annex = ANNEXB;
2521 /* try to autodetect annex */
2522 else if (sc->driver_info & AUTO_ANNEX_A)
2523 sc->annex = ANNEXA;
2524 else if (sc->driver_info & AUTO_ANNEX_B)
2525 sc->annex = ANNEXB;
2526 else
2527 sc->annex = (le16_to_cpu(sc->usb_dev->descriptor.bcdDevice) & 0x80)?ANNEXB:ANNEXA;
2528
503add46 2529 alt = altsetting[sc->modem_index];
3c9666cc 2530 /* ADI930 don't support iso */
503add46
SG
2531 if (UEA_CHIP_VERSION(id) != ADI930 && alt > 0) {
2532 if (alt <= 8 && usb_set_interface(usb, UEA_DS_IFACE_NO, alt) == 0) {
2533 uea_dbg(usb, "set alternate %u for 2 interface\n", alt);
3c9666cc 2534 uea_info(usb, "using iso mode\n");
2535 usbatm->flags |= UDSL_USE_ISOC | UDSL_IGNORE_EILSEQ;
2536 } else {
503add46
SG
2537 uea_err(usb, "setting alternate %u failed for "
2538 "2 interface, using bulk mode\n", alt);
3c9666cc 2539 }
2540 }
2541
9ab99c8c 2542 ret = sysfs_create_group(&intf->dev.kobj, &attr_grp);
2543 if (ret < 0)
2544 goto error;
2545
b72458a8 2546 ret = uea_boot(sc);
9ab99c8c 2547 if (ret < 0)
4c132e77 2548 goto error_rm_grp;
b72458a8 2549
b72458a8 2550 return 0;
4c132e77
SG
2551
2552error_rm_grp:
2553 sysfs_remove_group(&intf->dev.kobj, &attr_grp);
9ab99c8c 2554error:
2555 kfree(sc);
2556 return ret;
b72458a8 2557}
2558
2559static void uea_unbind(struct usbatm_data *usbatm, struct usb_interface *intf)
2560{
2561 struct uea_softc *sc = usbatm->driver_data;
2562
9ab99c8c 2563 sysfs_remove_group(&intf->dev.kobj, &attr_grp);
b72458a8 2564 uea_stop(sc);
2565 kfree(sc);
2566}
2567
2568static struct usbatm_driver uea_usbatm_driver = {
2569 .driver_name = "ueagle-atm",
b72458a8 2570 .bind = uea_bind,
2571 .atm_start = uea_atm_open,
2572 .unbind = uea_unbind,
2573 .heavy_init = uea_heavy,
80aae7a1
DS
2574 .bulk_in = UEA_BULK_DATA_PIPE,
2575 .bulk_out = UEA_BULK_DATA_PIPE,
3c9666cc 2576 .isoc_in = UEA_ISO_DATA_PIPE,
b72458a8 2577};
2578
2579static int uea_probe(struct usb_interface *intf, const struct usb_device_id *id)
2580{
2581 struct usb_device *usb = interface_to_usbdev(intf);
2582
2583 uea_enters(usb);
603cf608
SG
2584 uea_info(usb, "ADSL device founded vid (%#X) pid (%#X) Rev (%#X): %s\n",
2585 le16_to_cpu(usb->descriptor.idVendor),
2586 le16_to_cpu(usb->descriptor.idProduct),
2587 le16_to_cpu(usb->descriptor.bcdDevice),
2588 chip_name[UEA_CHIP_VERSION(id)]);
b72458a8 2589
2590 usb_reset_device(usb);
2591
2592 if (UEA_IS_PREFIRM(id))
2593 return uea_load_firmware(usb, UEA_CHIP_VERSION(id));
2594
2595 return usbatm_usb_probe(intf, id, &uea_usbatm_driver);
2596}
2597
2598static void uea_disconnect(struct usb_interface *intf)
2599{
2600 struct usb_device *usb = interface_to_usbdev(intf);
2601 int ifnum = intf->altsetting->desc.bInterfaceNumber;
2602 uea_enters(usb);
2603
2604 /* ADI930 has 2 interfaces and eagle 3 interfaces.
2605 * Pre-firmware device has one interface
2606 */
2607 if (usb->config->desc.bNumInterfaces != 1 && ifnum == 0) {
ab3c81ff 2608 mutex_lock(&uea_mutex);
b72458a8 2609 usbatm_usb_disconnect(intf);
ab3c81ff 2610 mutex_unlock(&uea_mutex);
b72458a8 2611 uea_info(usb, "ADSL device removed\n");
2612 }
2613
2614 uea_leaves(usb);
2615}
2616
2617/*
2618 * List of supported VID/PID
2619 */
2620static const struct usb_device_id uea_ids[] = {
603cf608
SG
2621 {USB_DEVICE(ANALOG_VID, ADI930_PID_PREFIRM), .driver_info = ADI930 | PREFIRM},
2622 {USB_DEVICE(ANALOG_VID, ADI930_PID_PSTFIRM), .driver_info = ADI930 | PSTFIRM},
2623 {USB_DEVICE(ANALOG_VID, EAGLE_I_PID_PREFIRM), .driver_info = EAGLE_I | PREFIRM},
2624 {USB_DEVICE(ANALOG_VID, EAGLE_I_PID_PSTFIRM), .driver_info = EAGLE_I | PSTFIRM},
2625 {USB_DEVICE(ANALOG_VID, EAGLE_II_PID_PREFIRM), .driver_info = EAGLE_II | PREFIRM},
2626 {USB_DEVICE(ANALOG_VID, EAGLE_II_PID_PSTFIRM), .driver_info = EAGLE_II | PSTFIRM},
2627 {USB_DEVICE(ANALOG_VID, EAGLE_IIC_PID_PREFIRM), .driver_info = EAGLE_II | PREFIRM},
2628 {USB_DEVICE(ANALOG_VID, EAGLE_IIC_PID_PSTFIRM), .driver_info = EAGLE_II | PSTFIRM},
2629 {USB_DEVICE(ANALOG_VID, EAGLE_III_PID_PREFIRM), .driver_info = EAGLE_III | PREFIRM},
2630 {USB_DEVICE(ANALOG_VID, EAGLE_III_PID_PSTFIRM), .driver_info = EAGLE_III | PSTFIRM},
2631 {USB_DEVICE(ANALOG_VID, EAGLE_IV_PID_PREFIRM), .driver_info = EAGLE_IV | PREFIRM},
2632 {USB_DEVICE(ANALOG_VID, EAGLE_IV_PID_PSTFIRM), .driver_info = EAGLE_IV | PSTFIRM},
2633 {USB_DEVICE(DEVOLO_VID, DEVOLO_EAGLE_I_A_PID_PREFIRM), .driver_info = EAGLE_I | PREFIRM},
2634 {USB_DEVICE(DEVOLO_VID, DEVOLO_EAGLE_I_A_PID_PSTFIRM), .driver_info = EAGLE_I | PSTFIRM | AUTO_ANNEX_A},
2635 {USB_DEVICE(DEVOLO_VID, DEVOLO_EAGLE_I_B_PID_PREFIRM), .driver_info = EAGLE_I | PREFIRM},
2636 {USB_DEVICE(DEVOLO_VID, DEVOLO_EAGLE_I_B_PID_PSTFIRM), .driver_info = EAGLE_I | PSTFIRM | AUTO_ANNEX_B},
2637 {USB_DEVICE(DEVOLO_VID, DEVOLO_EAGLE_II_A_PID_PREFIRM), .driver_info = EAGLE_II | PREFIRM},
2638 {USB_DEVICE(DEVOLO_VID, DEVOLO_EAGLE_II_A_PID_PSTFIRM), .driver_info = EAGLE_II | PSTFIRM | AUTO_ANNEX_A},
2639 {USB_DEVICE(DEVOLO_VID, DEVOLO_EAGLE_II_B_PID_PREFIRM), .driver_info = EAGLE_II | PREFIRM},
2640 {USB_DEVICE(DEVOLO_VID, DEVOLO_EAGLE_II_B_PID_PSTFIRM), .driver_info = EAGLE_II | PSTFIRM | AUTO_ANNEX_B},
b72458a8 2641 {USB_DEVICE(ELSA_VID, ELSA_PID_PREFIRM), .driver_info = ADI930 | PREFIRM},
2642 {USB_DEVICE(ELSA_VID, ELSA_PID_PSTFIRM), .driver_info = ADI930 | PSTFIRM},
603cf608
SG
2643 {USB_DEVICE(ELSA_VID, ELSA_PID_A_PREFIRM), .driver_info = ADI930 | PREFIRM},
2644 {USB_DEVICE(ELSA_VID, ELSA_PID_A_PSTFIRM), .driver_info = ADI930 | PSTFIRM | AUTO_ANNEX_A},
2645 {USB_DEVICE(ELSA_VID, ELSA_PID_B_PREFIRM), .driver_info = ADI930 | PREFIRM},
2646 {USB_DEVICE(ELSA_VID, ELSA_PID_B_PSTFIRM), .driver_info = ADI930 | PSTFIRM | AUTO_ANNEX_B},
b72458a8 2647 {USB_DEVICE(USR_VID, MILLER_A_PID_PREFIRM), .driver_info = EAGLE_I | PREFIRM},
603cf608 2648 {USB_DEVICE(USR_VID, MILLER_A_PID_PSTFIRM), .driver_info = EAGLE_I | PSTFIRM | AUTO_ANNEX_A},
b72458a8 2649 {USB_DEVICE(USR_VID, MILLER_B_PID_PREFIRM), .driver_info = EAGLE_I | PREFIRM},
603cf608 2650 {USB_DEVICE(USR_VID, MILLER_B_PID_PSTFIRM), .driver_info = EAGLE_I | PSTFIRM | AUTO_ANNEX_B},
b72458a8 2651 {USB_DEVICE(USR_VID, HEINEKEN_A_PID_PREFIRM),.driver_info = EAGLE_I | PREFIRM},
603cf608 2652 {USB_DEVICE(USR_VID, HEINEKEN_A_PID_PSTFIRM),.driver_info = EAGLE_I | PSTFIRM | AUTO_ANNEX_A},
b72458a8 2653 {USB_DEVICE(USR_VID, HEINEKEN_B_PID_PREFIRM),.driver_info = EAGLE_I | PREFIRM},
603cf608 2654 {USB_DEVICE(USR_VID, HEINEKEN_B_PID_PSTFIRM),.driver_info = EAGLE_I | PSTFIRM | AUTO_ANNEX_B},
b72458a8 2655 {}
2656};
2657
2658/*
2659 * USB driver descriptor
2660 */
2661static struct usb_driver uea_driver = {
b72458a8 2662 .name = "ueagle-atm",
2663 .id_table = uea_ids,
2664 .probe = uea_probe,
2665 .disconnect = uea_disconnect,
2666};
2667
2668MODULE_DEVICE_TABLE(usb, uea_ids);
2669
2670/**
2671 * uea_init - Initialize the module.
2672 * Register to USB subsystem
2673 */
2674static int __init uea_init(void)
2675{
2676 printk(KERN_INFO "[ueagle-atm] driver " EAGLEUSBVERSION " loaded\n");
2677
2678 usb_register(&uea_driver);
2679
2680 return 0;
2681}
2682
2683module_init(uea_init);
2684
2685/**
2686 * uea_exit - Destroy module
2687 * Deregister with USB subsystem
2688 */
2689static void __exit uea_exit(void)
2690{
2691 /*
2692 * This calls automatically the uea_disconnect method if necessary:
2693 */
2694 usb_deregister(&uea_driver);
2695
2696 printk(KERN_INFO "[ueagle-atm] driver unloaded\n");
2697}
2698
2699module_exit(uea_exit);
2700
2701MODULE_AUTHOR("Damien Bergamini/Matthieu Castet/Stanislaw W. Gruszka");
2702MODULE_DESCRIPTION("ADI 930/Eagle USB ADSL Modem driver");
2703MODULE_LICENSE("Dual BSD/GPL");
This page took 0.529136 seconds and 5 git commands to generate.