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